Debian Codegraph @

android/source/src/java/org/mozilla/gecko/gfx/Layer.java

raw
1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 package org.mozilla.gecko.gfx;
7
8 import android.graphics.Rect;
9 import android.graphics.RectF;
10 import android.graphics.Region;
11
12 import org.mozilla.gecko.util.FloatUtils;
13
14 import java.nio.FloatBuffer;
15 import java.util.concurrent.locks.ReentrantLock;
16
17 public abstract class Layer {
18 private final ReentrantLock mTransactionLock;
19 private boolean mInTransaction;
20 private Rect mNewPosition;
21
22 protected Rect mPosition;
23 protected boolean mUsesDefaultProgram = true;
24
25 public Layer() {
26 this(null);
27 }
28
29 public Layer(IntSize size) {
30 mTransactionLock = new ReentrantLock();
31 if (size == null) {
32 mPosition = new Rect();
33 } else {
34 mPosition = new Rect(0, 0, size.width, size.height);
35 }
36 }
37
38 /**
39 * Updates the layer. This returns false if there is still work to be done
40 * after this update.
41 */
42 public final boolean update(RenderContext context) {
43 if (mTransactionLock.isHeldByCurrentThread()) {
44 throw new RuntimeException("draw() called while transaction lock held by this " +
45 "thread?!");
46 }
47
48 if (mTransactionLock.tryLock()) {
49 try {
50 performUpdates(context);
51 return true;
52 } finally {
53 mTransactionLock.unlock();
54 }
55 }
56
57 return false;
58 }
59
60 /** Subclasses override this function to draw the layer. */
61 public abstract void draw(RenderContext context);
62
63 /** Given the intrinsic size of the layer, returns the pixel boundaries of the layer rect. */
64 protected RectF getBounds(RenderContext context) {
65 return RectUtils.scale(new RectF(mPosition), context.zoomFactor);
66 }
67
68 /**
69 * Returns the region of the layer that is considered valid. The default
70 * implementation of this will return the bounds of the layer, but this
71 * may be overridden.
72 */
73 public Region getValidRegion(RenderContext context) {
74 return new Region(RectUtils.round(getBounds(context)));
75 }
76
77 /**
78 * Call this before modifying the layer. Note that, for TileLayers, "modifying the layer"
79 * includes altering the underlying CairoImage in any way. Thus you must call this function
80 * before modifying the byte buffer associated with this layer.
81 *
82 * This function may block, so you should never call this on the main UI thread.
83 */
84 public void beginTransaction() {
85 if (mTransactionLock.isHeldByCurrentThread())
86 throw new RuntimeException("Nested transactions are not supported");
87 mTransactionLock.lock();
88 mInTransaction = true;
89 }
90
91 /** Call this when you're done modifying the layer. */
92 public void endTransaction() {
93 if (!mInTransaction)
94 throw new RuntimeException("endTransaction() called outside a transaction");
95 mInTransaction = false;
96 mTransactionLock.unlock();
97 }
98
99 /** Returns true if the layer is currently in a transaction and false otherwise. */
100 protected boolean inTransaction() {
101 return mInTransaction;
102 }
103
104 /** Returns the current layer position. */
105 public Rect getPosition() {
106 return mPosition;
107 }
108
109 /** Sets the position. Only valid inside a transaction. */
110 public void setPosition(Rect newPosition) {
111 if (!mInTransaction)
112 throw new RuntimeException("setPosition() is only valid inside a transaction");
113 mNewPosition = newPosition;
114 }
115
116 public boolean usesDefaultProgram() {
117 return mUsesDefaultProgram;
118 }
119
120 /**
121 * Subclasses may override this method to perform custom layer updates. This will be called
122 * with the transaction lock held. Subclass implementations of this method must call the
123 * superclass implementation. Returns false if there is still work to be done after this
124 * update is complete.
125 */
126 protected void performUpdates(RenderContext context) {
127 if (mNewPosition != null) {
128 mPosition = mNewPosition;
129 mNewPosition = null;
130 }
131 }
132
133 /**
134 * This function fills in the provided <tt>dest</tt> array with values to render a texture.
135 * The array is filled with 4 sets of {x, y, z, texture_x, texture_y} values (so 20 values
136 * in total) corresponding to the corners of the rect.
137 */
138 protected final void fillRectCoordBuffer(float[] dest, RectF rect, float viewWidth, float viewHeight,
139 Rect cropRect, float texWidth, float texHeight) {
140 //x, y, z, texture_x, texture_y
141 dest[0] = rect.left / viewWidth;
142 dest[1] = rect.bottom / viewHeight;
143 dest[2] = 0;
144 dest[3] = cropRect.left / texWidth;
145 dest[4] = cropRect.top / texHeight;
146
147 dest[5] = rect.left / viewWidth;
148 dest[6] = rect.top / viewHeight;
149 dest[7] = 0;
150 dest[8] = cropRect.left / texWidth;
151 dest[9] = cropRect.bottom / texHeight;
152
153 dest[10] = rect.right / viewWidth;
154 dest[11] = rect.bottom / viewHeight;
155 dest[12] = 0;
156 dest[13] = cropRect.right / texWidth;
157 dest[14] = cropRect.top / texHeight;
158
159 dest[15] = rect.right / viewWidth;
160 dest[16] = rect.top / viewHeight;
161 dest[17] = 0;
162 dest[18] = cropRect.right / texWidth;
163 dest[19] = cropRect.bottom / texHeight;
164 }
165
166 public static class RenderContext {
167 public final RectF viewport;
168 public final RectF pageRect;
169 public final float zoomFactor;
170 public final int positionHandle;
171 public final int textureHandle;
172 public final FloatBuffer coordBuffer;
173
174 public RenderContext(RectF aViewport, RectF aPageRect, float aZoomFactor,
175 int aPositionHandle, int aTextureHandle, FloatBuffer aCoordBuffer) {
176 viewport = aViewport;
177 pageRect = aPageRect;
178 zoomFactor = aZoomFactor;
179 positionHandle = aPositionHandle;
180 textureHandle = aTextureHandle;
181 coordBuffer = aCoordBuffer;
182 }
183
184 public boolean fuzzyEquals(RenderContext other) {
185 if (other == null) {
186 return false;
187 }
188 return RectUtils.fuzzyEquals(viewport, other.viewport)
189 && RectUtils.fuzzyEquals(pageRect, other.pageRect)
190 && FloatUtils.fuzzyEquals(zoomFactor, other.zoomFactor);
191 }
192 }
193 }
194