| 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.Color; |
| 9 |
import android.graphics.Point; |
| 10 |
import android.graphics.Rect; |
| 11 |
import android.graphics.RectF; |
| 12 |
import android.opengl.GLES20; |
| 13 |
import android.opengl.GLSurfaceView; |
| 14 |
import android.os.SystemClock; |
| 15 |
import android.util.Log; |
| 16 |
|
| 17 |
import org.libreoffice.kit.DirectBufferAllocator; |
| 18 |
import org.mozilla.gecko.gfx.Layer.RenderContext; |
| 19 |
|
| 20 |
import java.nio.ByteBuffer; |
| 21 |
import java.nio.ByteOrder; |
| 22 |
import java.nio.FloatBuffer; |
| 23 |
import java.util.concurrent.CopyOnWriteArrayList; |
| 24 |
|
| 25 |
import javax.microedition.khronos.egl.EGLConfig; |
| 26 |
import javax.microedition.khronos.opengles.GL10; |
| 27 |
|
| 28 |
/** |
| 29 |
* The layer renderer implements the rendering logic for a layer view. |
| 30 |
*/ |
| 31 |
public class LayerRenderer implements GLSurfaceView.Renderer { |
| 32 |
private static final String LOGTAG = "GeckoLayerRenderer"; |
| 33 |
|
| 34 |
private final LayerView mView; |
| 35 |
private final SingleTileLayer mBackgroundLayer; |
| 36 |
private final NinePatchTileLayer mShadowLayer; |
| 37 |
private final ScrollbarLayer mHorizScrollLayer; |
| 38 |
private final ScrollbarLayer mVertScrollLayer; |
| 39 |
private final FadeRunnable mFadeRunnable; |
| 40 |
private ByteBuffer mCoordByteBuffer; |
| 41 |
private FloatBuffer mCoordBuffer; |
| 42 |
private RenderContext mLastPageContext; |
| 43 |
|
| 44 |
private final CopyOnWriteArrayList<Layer> mExtraLayers = new CopyOnWriteArrayList<Layer>(); |
| 45 |
|
| 46 |
// Used by GLES 2.0 |
| 47 |
private int mProgram; |
| 48 |
private int mPositionHandle; |
| 49 |
private int mTextureHandle; |
| 50 |
private int mSampleHandle; |
| 51 |
private int mTMatrixHandle; |
| 52 |
|
| 53 |
// column-major matrix applied to each vertex to shift the viewport from |
| 54 |
// one ranging from (-1, -1),(1,1) to (0,0),(1,1) and to scale all sizes by |
| 55 |
// a factor of 2 to fill up the screen |
| 56 |
public static final float[] DEFAULT_TEXTURE_MATRIX = { |
| 57 |
2.0f, 0.0f, 0.0f, 0.0f, |
| 58 |
0.0f, 2.0f, 0.0f, 0.0f, |
| 59 |
0.0f, 0.0f, 2.0f, 0.0f, |
| 60 |
-1.0f, -1.0f, 0.0f, 1.0f |
| 61 |
}; |
| 62 |
|
| 63 |
private static final int COORD_BUFFER_SIZE = 20; |
| 64 |
|
| 65 |
// The shaders run on the GPU directly, the vertex shader is only applying the |
| 66 |
// matrix transform detailed above |
| 67 |
|
| 68 |
// Note we flip the y-coordinate in the vertex shader from a |
| 69 |
// coordinate system with (0,0) in the top left to one with (0,0) in |
| 70 |
// the bottom left. |
| 71 |
|
| 72 |
public static final String DEFAULT_VERTEX_SHADER = |
| 73 |
"uniform mat4 uTMatrix;\n" + |
| 74 |
"attribute vec4 vPosition;\n" + |
| 75 |
"attribute vec2 aTexCoord;\n" + |
| 76 |
"varying vec2 vTexCoord;\n" + |
| 77 |
"void main() {\n" + |
| 78 |
" gl_Position = uTMatrix * vPosition;\n" + |
| 79 |
" vTexCoord.x = aTexCoord.x;\n" + |
| 80 |
" vTexCoord.y = 1.0 - aTexCoord.y;\n" + |
| 81 |
"}\n"; |
| 82 |
|
| 83 |
// We use highp because the screenshot textures |
| 84 |
// we use are large and we stretch them a lot |
| 85 |
// so we need all the precision we can get. |
| 86 |
// Unfortunately, highp is not required by ES 2.0 |
| 87 |
// so on GPU's like Mali we end up getting mediump |
| 88 |
public static final String DEFAULT_FRAGMENT_SHADER = |
| 89 |
"precision highp float;\n" + |
| 90 |
"varying vec2 vTexCoord;\n" + |
| 91 |
"uniform sampler2D sTexture;\n" + |
| 92 |
"void main() {\n" + |
| 93 |
" gl_FragColor = texture2D(sTexture, vTexCoord);\n" + |
| 94 |
"}\n"; |
| 95 |
|
| 96 |
public LayerRenderer(LayerView view) { |
| 97 |
mView = view; |
| 98 |
|
| 99 |
CairoImage backgroundImage = new BufferedCairoImage(view.getBackgroundPattern()); |
| 100 |
mBackgroundLayer = new SingleTileLayer(true, backgroundImage); |
| 101 |
|
| 102 |
CairoImage shadowImage = new BufferedCairoImage(view.getShadowPattern()); |
| 103 |
mShadowLayer = new NinePatchTileLayer(shadowImage); |
| 104 |
|
| 105 |
mHorizScrollLayer = ScrollbarLayer.create(this, false); |
| 106 |
mVertScrollLayer = ScrollbarLayer.create(this, true); |
| 107 |
mFadeRunnable = new FadeRunnable(); |
| 108 |
|
| 109 |
// Initialize the FloatBuffer that will be used to store all vertices and texture |
| 110 |
// coordinates in draw() commands. |
| 111 |
mCoordByteBuffer = DirectBufferAllocator.allocate(COORD_BUFFER_SIZE * 4); |
| 112 |
mCoordByteBuffer.order(ByteOrder.nativeOrder()); |
| 113 |
mCoordBuffer = mCoordByteBuffer.asFloatBuffer(); |
| 114 |
} |
| 115 |
|
| 116 |
@Override |
| 117 |
protected void finalize() throws Throwable { |
| 118 |
try { |
| 119 |
DirectBufferAllocator.free(mCoordByteBuffer); |
| 120 |
mCoordByteBuffer = null; |
| 121 |
mCoordBuffer = null; |
| 122 |
} finally { |
| 123 |
super.finalize(); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
public void destroy() { |
| 128 |
DirectBufferAllocator.free(mCoordByteBuffer); |
| 129 |
mCoordByteBuffer = null; |
| 130 |
mCoordBuffer = null; |
| 131 |
mBackgroundLayer.destroy(); |
| 132 |
mShadowLayer.destroy(); |
| 133 |
mHorizScrollLayer.destroy(); |
| 134 |
mVertScrollLayer.destroy(); |
| 135 |
} |
| 136 |
|
| 137 |
@Override |
| 138 |
public void onSurfaceCreated(GL10 gl, EGLConfig config) { |
| 139 |
createDefaultProgram(); |
| 140 |
activateDefaultProgram(); |
| 141 |
} |
| 142 |
|
| 143 |
public void createDefaultProgram() { |
| 144 |
int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, DEFAULT_VERTEX_SHADER); |
| 145 |
int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, DEFAULT_FRAGMENT_SHADER); |
| 146 |
|
| 147 |
mProgram = GLES20.glCreateProgram(); |
| 148 |
GLES20.glAttachShader(mProgram, vertexShader); // add the vertex shader to program |
| 149 |
GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program |
| 150 |
GLES20.glLinkProgram(mProgram); // creates OpenGL program executables |
| 151 |
|
| 152 |
// Get handles to the vertex shader's vPosition, aTexCoord, sTexture, and uTMatrix members. |
| 153 |
mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition"); |
| 154 |
mTextureHandle = GLES20.glGetAttribLocation(mProgram, "aTexCoord"); |
| 155 |
mSampleHandle = GLES20.glGetUniformLocation(mProgram, "sTexture"); |
| 156 |
mTMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uTMatrix"); |
| 157 |
} |
| 158 |
|
| 159 |
// Activates the shader program. |
| 160 |
public void activateDefaultProgram() { |
| 161 |
// Add the program to the OpenGL environment |
| 162 |
GLES20.glUseProgram(mProgram); |
| 163 |
|
| 164 |
// Set the transformation matrix |
| 165 |
GLES20.glUniformMatrix4fv(mTMatrixHandle, 1, false, DEFAULT_TEXTURE_MATRIX, 0); |
| 166 |
|
| 167 |
// Enable the arrays from which we get the vertex and texture coordinates |
| 168 |
GLES20.glEnableVertexAttribArray(mPositionHandle); |
| 169 |
GLES20.glEnableVertexAttribArray(mTextureHandle); |
| 170 |
|
| 171 |
GLES20.glUniform1i(mSampleHandle, 0); |
| 172 |
} |
| 173 |
|
| 174 |
// Deactivates the shader program. This must be done to avoid crashes after returning to the |
| 175 |
// Gecko C++ compositor from Java. |
| 176 |
public void deactivateDefaultProgram() { |
| 177 |
GLES20.glDisableVertexAttribArray(mTextureHandle); |
| 178 |
GLES20.glDisableVertexAttribArray(mPositionHandle); |
| 179 |
GLES20.glUseProgram(0); |
| 180 |
} |
| 181 |
|
| 182 |
public void addLayer(Layer layer) { |
| 183 |
synchronized (mExtraLayers) { |
| 184 |
if (mExtraLayers.contains(layer)) { |
| 185 |
mExtraLayers.remove(layer); |
| 186 |
} |
| 187 |
|
| 188 |
mExtraLayers.add(layer); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
public void removeLayer(Layer layer) { |
| 193 |
synchronized (mExtraLayers) { |
| 194 |
mExtraLayers.remove(layer); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Called whenever a new frame is about to be drawn. |
| 200 |
*/ |
| 201 |
@Override |
| 202 |
public void onDrawFrame(GL10 gl) { |
| 203 |
Frame frame = new Frame(mView.getLayerClient().getViewportMetrics()); |
| 204 |
synchronized (mView.getLayerClient()) { |
| 205 |
frame.beginDrawing(); |
| 206 |
frame.drawBackground(); |
| 207 |
frame.drawRootLayer(); |
| 208 |
frame.drawForeground(); |
| 209 |
frame.endDrawing(); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
private RenderContext createScreenContext(ImmutableViewportMetrics metrics) { |
| 214 |
RectF viewport = new RectF(0.0f, 0.0f, metrics.getWidth(), metrics.getHeight()); |
| 215 |
RectF pageRect = new RectF(metrics.getPageRect()); |
| 216 |
return createContext(viewport, pageRect, 1.0f); |
| 217 |
} |
| 218 |
|
| 219 |
private RenderContext createPageContext(ImmutableViewportMetrics metrics) { |
| 220 |
Rect viewport = RectUtils.round(metrics.getViewport()); |
| 221 |
RectF pageRect = metrics.getPageRect(); |
| 222 |
float zoomFactor = metrics.zoomFactor; |
| 223 |
return createContext(new RectF(viewport), pageRect, zoomFactor); |
| 224 |
} |
| 225 |
|
| 226 |
private RenderContext createContext(RectF viewport, RectF pageRect, float zoomFactor) { |
| 227 |
return new RenderContext(viewport, pageRect, zoomFactor, mPositionHandle, mTextureHandle, |
| 228 |
mCoordBuffer); |
| 229 |
} |
| 230 |
|
| 231 |
@Override |
| 232 |
public void onSurfaceChanged(GL10 gl, final int width, final int height) { |
| 233 |
GLES20.glViewport(0, 0, width, height); |
| 234 |
} |
| 235 |
|
| 236 |
/* |
| 237 |
* create a vertex shader type (GLES20.GL_VERTEX_SHADER) |
| 238 |
* or a fragment shader type (GLES20.GL_FRAGMENT_SHADER) |
| 239 |
*/ |
| 240 |
public static int loadShader(int type, String shaderCode) { |
| 241 |
int shader = GLES20.glCreateShader(type); |
| 242 |
GLES20.glShaderSource(shader, shaderCode); |
| 243 |
GLES20.glCompileShader(shader); |
| 244 |
return shader; |
| 245 |
} |
| 246 |
|
| 247 |
class FadeRunnable implements Runnable { |
| 248 |
private boolean mStarted; |
| 249 |
private long mRunAt; |
| 250 |
|
| 251 |
void scheduleStartFade(long delay) { |
| 252 |
mRunAt = SystemClock.elapsedRealtime() + delay; |
| 253 |
if (!mStarted) { |
| 254 |
mView.postDelayed(this, delay); |
| 255 |
mStarted = true; |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
void scheduleNextFadeFrame() { |
| 260 |
if (mStarted) { |
| 261 |
Log.e(LOGTAG, "scheduleNextFadeFrame() called while scheduled for starting fade"); |
| 262 |
} |
| 263 |
mView.postDelayed(this, 1000L / 60L); // request another frame at 60fps |
| 264 |
} |
| 265 |
|
| 266 |
boolean timeToFade() { |
| 267 |
return !mStarted; |
| 268 |
} |
| 269 |
|
| 270 |
public void run() { |
| 271 |
long timeDelta = mRunAt - SystemClock.elapsedRealtime(); |
| 272 |
if (timeDelta > 0) { |
| 273 |
// the run-at time was pushed back, so reschedule |
| 274 |
mView.postDelayed(this, timeDelta); |
| 275 |
} else { |
| 276 |
// reached the run-at time, execute |
| 277 |
mStarted = false; |
| 278 |
mView.requestRender(); |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
public class Frame { |
| 284 |
// A fixed snapshot of the viewport metrics that this frame is using to render content. |
| 285 |
private final ImmutableViewportMetrics mFrameMetrics; |
| 286 |
// A rendering context for page-positioned layers. |
| 287 |
private final RenderContext mPageContext; |
| 288 |
// A rendering context for screen-positioned layers. |
| 289 |
private final RenderContext mScreenContext; |
| 290 |
// Whether a layer was updated. |
| 291 |
private boolean mUpdated; |
| 292 |
private final Rect mPageRect; |
| 293 |
|
| 294 |
public Frame(ImmutableViewportMetrics metrics) { |
| 295 |
mFrameMetrics = metrics; |
| 296 |
mPageContext = createPageContext(metrics); |
| 297 |
mScreenContext = createScreenContext(metrics); |
| 298 |
mPageRect = getPageRect(); |
| 299 |
} |
| 300 |
|
| 301 |
private void setScissorRect() { |
| 302 |
Rect scissorRect = transformToScissorRect(mPageRect); |
| 303 |
GLES20.glEnable(GLES20.GL_SCISSOR_TEST); |
| 304 |
GLES20.glScissor(scissorRect.left, scissorRect.top, |
| 305 |
scissorRect.width(), scissorRect.height()); |
| 306 |
} |
| 307 |
|
| 308 |
private Rect transformToScissorRect(Rect rect) { |
| 309 |
IntSize screenSize = new IntSize(mFrameMetrics.getSize()); |
| 310 |
|
| 311 |
int left = Math.max(0, rect.left); |
| 312 |
int top = Math.max(0, rect.top); |
| 313 |
int right = Math.min(screenSize.width, rect.right); |
| 314 |
int bottom = Math.min(screenSize.height, rect.bottom); |
| 315 |
|
| 316 |
return new Rect(left, screenSize.height - bottom, right, |
| 317 |
(screenSize.height - bottom) + (bottom - top)); |
| 318 |
} |
| 319 |
|
| 320 |
private Rect getPageRect() { |
| 321 |
Point origin = PointUtils.round(mFrameMetrics.getOrigin()); |
| 322 |
Rect pageRect = RectUtils.round(mFrameMetrics.getPageRect()); |
| 323 |
pageRect.offset(-origin.x, -origin.y); |
| 324 |
return pageRect; |
| 325 |
} |
| 326 |
|
| 327 |
public void beginDrawing() { |
| 328 |
TextureReaper.get().reap(); |
| 329 |
TextureGenerator.get().fill(); |
| 330 |
|
| 331 |
mUpdated = true; |
| 332 |
|
| 333 |
Layer rootLayer = mView.getLayerClient().getRoot(); |
| 334 |
Layer lowResLayer = mView.getLayerClient().getLowResLayer(); |
| 335 |
|
| 336 |
if (!mPageContext.fuzzyEquals(mLastPageContext)) { |
| 337 |
// the viewport or page changed, so show the scrollbars again |
| 338 |
// as per UX decision |
| 339 |
mVertScrollLayer.unfade(); |
| 340 |
mHorizScrollLayer.unfade(); |
| 341 |
mFadeRunnable.scheduleStartFade(ScrollbarLayer.FADE_DELAY); |
| 342 |
} else if (mFadeRunnable.timeToFade()) { |
| 343 |
boolean stillFading = mVertScrollLayer.fade() | mHorizScrollLayer.fade(); |
| 344 |
if (stillFading) { |
| 345 |
mFadeRunnable.scheduleNextFadeFrame(); |
| 346 |
} |
| 347 |
} |
| 348 |
mLastPageContext = mPageContext; |
| 349 |
|
| 350 |
/* Update layers. */ |
| 351 |
if (rootLayer != null) mUpdated &= rootLayer.update(mPageContext); // called on compositor thread |
| 352 |
if (lowResLayer != null) mUpdated &= lowResLayer.update(mPageContext); // called on compositor thread |
| 353 |
mUpdated &= mBackgroundLayer.update(mScreenContext); // called on compositor thread |
| 354 |
mUpdated &= mShadowLayer.update(mPageContext); // called on compositor thread |
| 355 |
mUpdated &= mVertScrollLayer.update(mPageContext); // called on compositor thread |
| 356 |
mUpdated &= mHorizScrollLayer.update(mPageContext); // called on compositor thread |
| 357 |
|
| 358 |
for (Layer layer : mExtraLayers) |
| 359 |
mUpdated &= layer.update(mPageContext); // called on compositor thread |
| 360 |
} |
| 361 |
|
| 362 |
public void drawBackground() { |
| 363 |
GLES20.glDisable(GLES20.GL_SCISSOR_TEST); |
| 364 |
|
| 365 |
/* Update background color. */ |
| 366 |
final int backgroundColor = Color.WHITE; |
| 367 |
|
| 368 |
/* Clear to the page background colour. The bits set here need to |
| 369 |
* match up with those used in gfx/layers/opengl/LayerManagerOGL.cpp. |
| 370 |
*/ |
| 371 |
GLES20.glClearColor(((backgroundColor >> 16) & 0xFF) / 255.0f, |
| 372 |
((backgroundColor >> 8) & 0xFF) / 255.0f, |
| 373 |
(backgroundColor & 0xFF) / 255.0f, |
| 374 |
0.0f); |
| 375 |
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | |
| 376 |
GLES20.GL_DEPTH_BUFFER_BIT); |
| 377 |
|
| 378 |
/* Draw the background. */ |
| 379 |
mBackgroundLayer.setMask(mPageRect); |
| 380 |
mBackgroundLayer.draw(mScreenContext); |
| 381 |
|
| 382 |
/* Draw the drop shadow, if we need to. */ |
| 383 |
RectF untransformedPageRect = new RectF(0.0f, 0.0f, mPageRect.width(), |
| 384 |
mPageRect.height()); |
| 385 |
if (!untransformedPageRect.contains(mFrameMetrics.getViewport())) |
| 386 |
mShadowLayer.draw(mPageContext); |
| 387 |
|
| 388 |
/* Scissor around the page-rect, in case the page has shrunk |
| 389 |
* since the screenshot layer was last updated. |
| 390 |
*/ |
| 391 |
setScissorRect(); // Calls glEnable(GL_SCISSOR_TEST)) |
| 392 |
} |
| 393 |
|
| 394 |
// Draws the layer the client added to us. |
| 395 |
void drawRootLayer() { |
| 396 |
Layer lowResLayer = mView.getLayerClient().getLowResLayer(); |
| 397 |
if (lowResLayer == null) { |
| 398 |
return; |
| 399 |
} |
| 400 |
lowResLayer.draw(mPageContext); |
| 401 |
|
| 402 |
Layer rootLayer = mView.getLayerClient().getRoot(); |
| 403 |
if (rootLayer == null) { |
| 404 |
return; |
| 405 |
} |
| 406 |
|
| 407 |
rootLayer.draw(mPageContext); |
| 408 |
} |
| 409 |
|
| 410 |
public void drawForeground() { |
| 411 |
/* Draw any extra layers that were added (likely plugins) */ |
| 412 |
for (Layer layer : mExtraLayers) { |
| 413 |
if (!layer.usesDefaultProgram()) |
| 414 |
deactivateDefaultProgram(); |
| 415 |
|
| 416 |
layer.draw(mPageContext); |
| 417 |
|
| 418 |
if (!layer.usesDefaultProgram()) |
| 419 |
activateDefaultProgram(); |
| 420 |
} |
| 421 |
|
| 422 |
/* Draw the vertical scrollbar. */ |
| 423 |
if (mPageRect.height() > mFrameMetrics.getHeight()) |
| 424 |
mVertScrollLayer.draw(mPageContext); |
| 425 |
|
| 426 |
/* Draw the horizontal scrollbar. */ |
| 427 |
if (mPageRect.width() > mFrameMetrics.getWidth()) |
| 428 |
mHorizScrollLayer.draw(mPageContext); |
| 429 |
} |
| 430 |
|
| 431 |
public void endDrawing() { |
| 432 |
// If a layer update requires further work, schedule another redraw |
| 433 |
if (!mUpdated) |
| 434 |
mView.requestRender(); |
| 435 |
} |
| 436 |
} |
| 437 |
} |