LibreOffice Module android (master) 1
Layer.java
Go to the documentation of this file.
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
6package org.mozilla.gecko.gfx;
7
8import android.graphics.Rect;
9import android.graphics.RectF;
10import android.graphics.Region;
11
13
14import java.nio.FloatBuffer;
15import java.util.concurrent.locks.ReentrantLock;
16
17public abstract class Layer {
18 private final ReentrantLock mTransactionLock;
19 private boolean mInTransaction;
21 private float mNewResolution;
22
23 protected Rect mPosition;
24 protected float mResolution;
25 protected boolean mUsesDefaultProgram = true;
26
27 public Layer() {
28 this(null);
29 }
30
31 public Layer(IntSize size) {
32 mTransactionLock = new ReentrantLock();
33 if (size == null) {
34 mPosition = new Rect();
35 } else {
36 mPosition = new Rect(0, 0, size.width, size.height);
37 }
38 mResolution = 1.0f;
39 }
40
45 public final boolean update(RenderContext context) {
46 if (mTransactionLock.isHeldByCurrentThread()) {
47 throw new RuntimeException("draw() called while transaction lock held by this " +
48 "thread?!");
49 }
50
51 if (mTransactionLock.tryLock()) {
52 try {
53 performUpdates(context);
54 return true;
55 } finally {
56 mTransactionLock.unlock();
57 }
58 }
59
60 return false;
61 }
62
64 public abstract void draw(RenderContext context);
65
67 protected RectF getBounds(RenderContext context) {
68 return RectUtils.scale(new RectF(mPosition), context.zoomFactor / mResolution);
69 }
70
76 public Region getValidRegion(RenderContext context) {
77 return new Region(RectUtils.round(getBounds(context)));
78 }
79
87 public void beginTransaction() {
88 if (mTransactionLock.isHeldByCurrentThread())
89 throw new RuntimeException("Nested transactions are not supported");
90 mTransactionLock.lock();
91 mInTransaction = true;
93 }
94
96 public void endTransaction() {
97 if (!mInTransaction)
98 throw new RuntimeException("endTransaction() called outside a transaction");
99 mInTransaction = false;
100 mTransactionLock.unlock();
101 }
102
104 protected boolean inTransaction() {
105 return mInTransaction;
106 }
107
109 public Rect getPosition() {
110 return mPosition;
111 }
112
114 public void setPosition(Rect newPosition) {
115 if (!mInTransaction)
116 throw new RuntimeException("setPosition() is only valid inside a transaction");
117 mNewPosition = newPosition;
118 }
119
121 public float getResolution() {
122 return mResolution;
123 }
124
130 public void setResolution(float newResolution) {
131 if (!mInTransaction)
132 throw new RuntimeException("setResolution() is only valid inside a transaction");
133 mNewResolution = newResolution;
134 }
135
136 public boolean usesDefaultProgram() {
137 return mUsesDefaultProgram;
138 }
139
146 protected void performUpdates(RenderContext context) {
147 if (mNewPosition != null) {
149 mNewPosition = null;
150 }
151 if (mNewResolution != 0.0f) {
153 mNewResolution = 0.0f;
154 }
155 }
156
162 protected final void fillRectCoordBuffer(float[] dest, RectF rect, float viewWidth, float viewHeight,
163 Rect cropRect, float texWidth, float texHeight) {
164 //x, y, z, texture_x, texture_y
165 dest[0] = rect.left / viewWidth;
166 dest[1] = rect.bottom / viewHeight;
167 dest[2] = 0;
168 dest[3] = cropRect.left / texWidth;
169 dest[4] = cropRect.top / texHeight;
170
171 dest[5] = rect.left / viewWidth;
172 dest[6] = rect.top / viewHeight;
173 dest[7] = 0;
174 dest[8] = cropRect.left / texWidth;
175 dest[9] = cropRect.bottom / texHeight;
176
177 dest[10] = rect.right / viewWidth;
178 dest[11] = rect.bottom / viewHeight;
179 dest[12] = 0;
180 dest[13] = cropRect.right / texWidth;
181 dest[14] = cropRect.top / texHeight;
182
183 dest[15] = rect.right / viewWidth;
184 dest[16] = rect.top / viewHeight;
185 dest[17] = 0;
186 dest[18] = cropRect.right / texWidth;
187 dest[19] = cropRect.bottom / texHeight;
188 }
189
190 public static class RenderContext {
191 public final RectF viewport;
192 public final RectF pageRect;
193 public final float zoomFactor;
194 public final int positionHandle;
195 public final int textureHandle;
196 public final FloatBuffer coordBuffer;
197
198 public RenderContext(RectF aViewport, RectF aPageRect, float aZoomFactor,
199 int aPositionHandle, int aTextureHandle, FloatBuffer aCoordBuffer) {
200 viewport = aViewport;
201 pageRect = aPageRect;
202 zoomFactor = aZoomFactor;
203 positionHandle = aPositionHandle;
204 textureHandle = aTextureHandle;
205 coordBuffer = aCoordBuffer;
206 }
207
208 public boolean fuzzyEquals(RenderContext other) {
209 if (other == null) {
210 return false;
211 }
215 }
216 }
217}
218
RenderContext(RectF aViewport, RectF aPageRect, float aZoomFactor, int aPositionHandle, int aTextureHandle, FloatBuffer aCoordBuffer)
Definition: Layer.java:198
boolean fuzzyEquals(RenderContext other)
Definition: Layer.java:208
final ReentrantLock mTransactionLock
Definition: Layer.java:18
void endTransaction()
Call this when you're done modifying the layer.
Definition: Layer.java:96
boolean inTransaction()
Returns true if the layer is currently in a transaction and false otherwise.
Definition: Layer.java:104
void performUpdates(RenderContext context)
Subclasses may override this method to perform custom layer updates.
Definition: Layer.java:146
Layer(IntSize size)
Definition: Layer.java:31
void beginTransaction()
Call this before modifying the layer.
Definition: Layer.java:87
final void fillRectCoordBuffer(float[] dest, RectF rect, float viewWidth, float viewHeight, Rect cropRect, float texWidth, float texHeight)
This function fills in the provided dest array with values to render a texture.
Definition: Layer.java:162
void setResolution(float newResolution)
Sets the layer resolution.
Definition: Layer.java:130
float getResolution()
Returns the current layer's resolution.
Definition: Layer.java:121
abstract void draw(RenderContext context)
Subclasses override this function to draw the layer.
final boolean update(RenderContext context)
Updates the layer.
Definition: Layer.java:45
RectF getBounds(RenderContext context)
Given the intrinsic size of the layer, returns the pixel boundaries of the layer rect.
Definition: Layer.java:67
Rect getPosition()
Returns the current layer position.
Definition: Layer.java:109
Region getValidRegion(RenderContext context)
Returns the region of the layer that is considered valid.
Definition: Layer.java:76
boolean usesDefaultProgram()
Definition: Layer.java:136
void setPosition(Rect newPosition)
Sets the position.
Definition: Layer.java:114
static RectF scale(RectF rect, float scale)
Definition: RectUtils.java:44
static Rect round(RectF rect)
Returns the nearest integer rect of the given rect.
Definition: RectUtils.java:61
static boolean fuzzyEquals(RectF a, RectF b)
Definition: RectUtils.java:89
static boolean fuzzyEquals(float a, float b)
Definition: FloatUtils.java:13
size