2011-11-18 10:28:17 -08:00
|
|
|
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
|
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Android code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Mozilla Foundation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2009-2010
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Patrick Walton <pcwalton@mozilla.com>
|
2011-11-23 11:07:29 -08:00
|
|
|
* Chris Lord <chrislord.net@gmail.com>
|
2011-11-18 10:28:17 -08:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
package org.mozilla.gecko.gfx;
|
|
|
|
|
|
|
|
import org.mozilla.gecko.gfx.BufferedCairoImage;
|
|
|
|
import org.mozilla.gecko.gfx.IntSize;
|
2011-12-01 14:05:41 -08:00
|
|
|
import org.mozilla.gecko.gfx.Layer.RenderContext;
|
2011-11-18 10:28:17 -08:00
|
|
|
import org.mozilla.gecko.gfx.LayerController;
|
|
|
|
import org.mozilla.gecko.gfx.LayerView;
|
|
|
|
import org.mozilla.gecko.gfx.NinePatchTileLayer;
|
|
|
|
import org.mozilla.gecko.gfx.SingleTileLayer;
|
|
|
|
import org.mozilla.gecko.gfx.TextureReaper;
|
2012-01-31 06:40:58 -08:00
|
|
|
import org.mozilla.gecko.gfx.TextureGenerator;
|
2011-11-18 10:28:17 -08:00
|
|
|
import org.mozilla.gecko.gfx.TextLayer;
|
|
|
|
import org.mozilla.gecko.gfx.TileLayer;
|
|
|
|
import android.content.Context;
|
2011-12-09 07:01:31 -08:00
|
|
|
import android.content.SharedPreferences;
|
2011-11-23 11:07:47 -08:00
|
|
|
import android.graphics.Point;
|
|
|
|
import android.graphics.PointF;
|
2011-11-18 10:28:17 -08:00
|
|
|
import android.graphics.Rect;
|
|
|
|
import android.graphics.RectF;
|
2012-02-02 01:02:32 -08:00
|
|
|
import android.graphics.Region;
|
|
|
|
import android.graphics.RegionIterator;
|
2011-11-18 10:28:17 -08:00
|
|
|
import android.opengl.GLSurfaceView;
|
2011-12-09 07:01:26 -08:00
|
|
|
import android.os.SystemClock;
|
2011-11-18 10:28:17 -08:00
|
|
|
import android.util.DisplayMetrics;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.view.WindowManager;
|
|
|
|
import javax.microedition.khronos.egl.EGLConfig;
|
|
|
|
import javax.microedition.khronos.opengles.GL10;
|
2012-01-30 19:45:38 -08:00
|
|
|
import java.nio.IntBuffer;
|
2012-01-31 06:40:58 -08:00
|
|
|
import java.util.ArrayList;
|
2011-11-18 10:28:17 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The layer renderer implements the rendering logic for a layer view.
|
|
|
|
*/
|
|
|
|
public class LayerRenderer implements GLSurfaceView.Renderer {
|
2011-12-09 13:05:24 -08:00
|
|
|
private static final String LOGTAG = "GeckoLayerRenderer";
|
2012-02-02 01:02:32 -08:00
|
|
|
private static final String PROFTAG = "GeckoLayerRendererProf";
|
2011-12-09 13:05:24 -08:00
|
|
|
|
2011-12-09 07:01:26 -08:00
|
|
|
/*
|
|
|
|
* The amount of time a frame is allowed to take to render before we declare it a dropped
|
|
|
|
* frame.
|
|
|
|
*/
|
|
|
|
private static final int MAX_FRAME_TIME = 16; /* 1000 ms / 60 FPS */
|
|
|
|
|
2011-12-09 07:01:28 -08:00
|
|
|
private static final int FRAME_RATE_METER_WIDTH = 64;
|
|
|
|
private static final int FRAME_RATE_METER_HEIGHT = 32;
|
|
|
|
|
2011-12-01 14:05:41 -08:00
|
|
|
private final LayerView mView;
|
2012-01-20 06:27:09 -08:00
|
|
|
private final SingleTileLayer mBackgroundLayer;
|
2012-01-23 20:10:24 -08:00
|
|
|
private final CheckerboardImage mCheckerboardImage;
|
2011-12-01 14:05:41 -08:00
|
|
|
private final SingleTileLayer mCheckerboardLayer;
|
|
|
|
private final NinePatchTileLayer mShadowLayer;
|
2011-12-09 07:01:26 -08:00
|
|
|
private final TextLayer mFrameRateLayer;
|
2011-12-01 14:05:41 -08:00
|
|
|
private final ScrollbarLayer mHorizScrollLayer;
|
|
|
|
private final ScrollbarLayer mVertScrollLayer;
|
2011-12-09 13:05:24 -08:00
|
|
|
private final FadeRunnable mFadeRunnable;
|
|
|
|
private RenderContext mLastPageContext;
|
2011-12-15 15:45:52 -08:00
|
|
|
private int mMaxTextureSize;
|
2011-11-18 10:28:17 -08:00
|
|
|
|
2012-01-31 06:40:58 -08:00
|
|
|
private ArrayList<Layer> mExtraLayers = new ArrayList<Layer>();
|
|
|
|
|
2011-12-09 07:01:26 -08:00
|
|
|
// Dropped frames display
|
|
|
|
private int[] mFrameTimings;
|
|
|
|
private int mCurrentFrame, mFrameTimingsSum, mDroppedFrames;
|
2011-12-09 07:01:31 -08:00
|
|
|
private boolean mShowFrameRate;
|
2011-11-18 10:28:17 -08:00
|
|
|
|
2012-02-02 01:02:32 -08:00
|
|
|
// Render profiling output
|
|
|
|
private int mFramesRendered;
|
|
|
|
private float mCompleteFramesRendered;
|
|
|
|
private boolean mProfileRender;
|
|
|
|
private long mProfileOutputTime;
|
|
|
|
|
2012-01-30 19:45:38 -08:00
|
|
|
/* Used by robocop for testing purposes */
|
|
|
|
private IntBuffer mPixelBuffer;
|
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
public LayerRenderer(LayerView view) {
|
|
|
|
mView = view;
|
|
|
|
|
|
|
|
LayerController controller = view.getController();
|
2011-12-01 14:05:41 -08:00
|
|
|
|
2012-01-20 06:27:09 -08:00
|
|
|
CairoImage backgroundImage = new BufferedCairoImage(controller.getBackgroundPattern());
|
|
|
|
mBackgroundLayer = new SingleTileLayer(true, backgroundImage);
|
|
|
|
|
2012-01-23 20:10:24 -08:00
|
|
|
mCheckerboardImage = new CheckerboardImage();
|
|
|
|
mCheckerboardLayer = new SingleTileLayer(true, mCheckerboardImage);
|
2011-12-01 14:05:41 -08:00
|
|
|
|
2011-11-18 18:07:14 -08:00
|
|
|
CairoImage shadowImage = new BufferedCairoImage(controller.getShadowPattern());
|
2011-12-01 14:05:41 -08:00
|
|
|
mShadowLayer = new NinePatchTileLayer(shadowImage);
|
|
|
|
|
2011-12-09 07:01:28 -08:00
|
|
|
IntSize frameRateLayerSize = new IntSize(FRAME_RATE_METER_WIDTH, FRAME_RATE_METER_HEIGHT);
|
|
|
|
mFrameRateLayer = TextLayer.create(frameRateLayerSize, "-- ms/--");
|
|
|
|
|
2011-12-01 14:05:41 -08:00
|
|
|
mHorizScrollLayer = ScrollbarLayer.create(false);
|
|
|
|
mVertScrollLayer = ScrollbarLayer.create(true);
|
2011-12-09 13:05:24 -08:00
|
|
|
mFadeRunnable = new FadeRunnable();
|
2011-11-18 10:28:17 -08:00
|
|
|
|
2011-12-09 07:01:26 -08:00
|
|
|
mFrameTimings = new int[60];
|
|
|
|
mCurrentFrame = mFrameTimingsSum = mDroppedFrames = 0;
|
2011-12-09 07:01:31 -08:00
|
|
|
mShowFrameRate = false;
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
|
2012-02-02 01:02:32 -08:00
|
|
|
checkMonitoringEnabled();
|
2011-12-09 07:01:31 -08:00
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
|
|
|
|
gl.glDisable(GL10.GL_DITHER);
|
|
|
|
gl.glEnable(GL10.GL_TEXTURE_2D);
|
2011-12-15 15:45:52 -08:00
|
|
|
|
|
|
|
int maxTextureSizeResult[] = new int[1];
|
|
|
|
gl.glGetIntegerv(GL10.GL_MAX_TEXTURE_SIZE, maxTextureSizeResult, 0);
|
|
|
|
mMaxTextureSize = maxTextureSizeResult[0];
|
2012-01-31 06:40:58 -08:00
|
|
|
|
|
|
|
TextureGenerator.get().fill();
|
2011-12-15 15:45:52 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getMaxTextureSize() {
|
|
|
|
return mMaxTextureSize;
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
|
2012-01-31 06:40:58 -08:00
|
|
|
public void addLayer(Layer layer) {
|
|
|
|
LayerController controller = mView.getController();
|
|
|
|
|
|
|
|
synchronized (controller) {
|
|
|
|
if (mExtraLayers.contains(layer)) {
|
|
|
|
mExtraLayers.remove(layer);
|
|
|
|
}
|
|
|
|
|
|
|
|
mExtraLayers.add(layer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void removeLayer(Layer layer) {
|
|
|
|
LayerController controller = mView.getController();
|
|
|
|
|
|
|
|
synchronized (controller) {
|
|
|
|
mExtraLayers.remove(layer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-01 14:05:41 -08:00
|
|
|
/**
|
|
|
|
* Called whenever a new frame is about to be drawn.
|
|
|
|
*/
|
2011-11-18 10:28:17 -08:00
|
|
|
public void onDrawFrame(GL10 gl) {
|
2011-12-09 07:01:26 -08:00
|
|
|
long frameStartTime = SystemClock.uptimeMillis();
|
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
TextureReaper.get().reap(gl);
|
2012-01-31 06:40:58 -08:00
|
|
|
TextureGenerator.get().fill();
|
2011-11-18 10:28:17 -08:00
|
|
|
|
|
|
|
LayerController controller = mView.getController();
|
2011-12-13 14:43:08 -08:00
|
|
|
RenderContext screenContext = createScreenContext();
|
|
|
|
|
2012-01-06 03:22:52 -08:00
|
|
|
boolean updated = true;
|
|
|
|
|
2011-12-13 14:43:08 -08:00
|
|
|
synchronized (controller) {
|
|
|
|
Layer rootLayer = controller.getRoot();
|
|
|
|
RenderContext pageContext = createPageContext();
|
|
|
|
|
|
|
|
if (!pageContext.fuzzyEquals(mLastPageContext)) {
|
|
|
|
// the viewport or page changed, so show the scrollbars again
|
|
|
|
// as per UX decision
|
|
|
|
mVertScrollLayer.unfade();
|
|
|
|
mHorizScrollLayer.unfade();
|
|
|
|
mFadeRunnable.scheduleStartFade(ScrollbarLayer.FADE_DELAY);
|
|
|
|
} else if (mFadeRunnable.timeToFade()) {
|
|
|
|
boolean stillFading = mVertScrollLayer.fade() | mHorizScrollLayer.fade();
|
|
|
|
if (stillFading) {
|
|
|
|
mFadeRunnable.scheduleNextFadeFrame();
|
|
|
|
}
|
2011-12-09 13:05:24 -08:00
|
|
|
}
|
2011-12-13 14:43:08 -08:00
|
|
|
mLastPageContext = pageContext;
|
|
|
|
|
|
|
|
/* Update layers. */
|
2012-01-06 03:22:52 -08:00
|
|
|
if (rootLayer != null) updated &= rootLayer.update(gl, pageContext);
|
2012-01-20 06:27:09 -08:00
|
|
|
updated &= mBackgroundLayer.update(gl, screenContext);
|
2012-01-06 03:22:52 -08:00
|
|
|
updated &= mShadowLayer.update(gl, pageContext);
|
2012-01-23 20:10:24 -08:00
|
|
|
updateCheckerboardLayer(gl, screenContext);
|
2012-01-06 03:22:52 -08:00
|
|
|
updated &= mFrameRateLayer.update(gl, screenContext);
|
|
|
|
updated &= mVertScrollLayer.update(gl, pageContext);
|
|
|
|
updated &= mHorizScrollLayer.update(gl, pageContext);
|
2011-12-13 14:43:08 -08:00
|
|
|
|
2012-01-31 06:40:58 -08:00
|
|
|
for (Layer layer : mExtraLayers)
|
|
|
|
updated &= layer.update(gl, pageContext);
|
|
|
|
|
2011-12-13 14:43:08 -08:00
|
|
|
/* Draw the background. */
|
2012-01-20 06:27:09 -08:00
|
|
|
mBackgroundLayer.draw(screenContext);
|
2011-12-13 14:43:08 -08:00
|
|
|
|
|
|
|
/* Draw the drop shadow, if we need to. */
|
|
|
|
Rect pageRect = getPageRect();
|
|
|
|
RectF untransformedPageRect = new RectF(0.0f, 0.0f, pageRect.width(),
|
|
|
|
pageRect.height());
|
|
|
|
if (!untransformedPageRect.contains(controller.getViewport()))
|
|
|
|
mShadowLayer.draw(pageContext);
|
|
|
|
|
|
|
|
/* Draw the checkerboard. */
|
|
|
|
Rect scissorRect = transformToScissorRect(pageRect);
|
|
|
|
gl.glEnable(GL10.GL_SCISSOR_TEST);
|
|
|
|
gl.glScissor(scissorRect.left, scissorRect.top,
|
|
|
|
scissorRect.width(), scissorRect.height());
|
|
|
|
|
|
|
|
mCheckerboardLayer.draw(screenContext);
|
|
|
|
|
|
|
|
/* Draw the layer the client added to us. */
|
|
|
|
if (rootLayer != null)
|
|
|
|
rootLayer.draw(pageContext);
|
|
|
|
|
|
|
|
gl.glDisable(GL10.GL_SCISSOR_TEST);
|
|
|
|
|
2012-01-31 06:40:58 -08:00
|
|
|
/* Draw any extra layers that were added (likely plugins) */
|
|
|
|
for (Layer layer : mExtraLayers)
|
|
|
|
layer.draw(pageContext);
|
|
|
|
|
2011-12-13 14:43:08 -08:00
|
|
|
/* Draw the vertical scrollbar. */
|
|
|
|
IntSize screenSize = new IntSize(controller.getViewportSize());
|
|
|
|
if (pageRect.height() > screenSize.height)
|
|
|
|
mVertScrollLayer.draw(pageContext);
|
|
|
|
|
|
|
|
/* Draw the horizontal scrollbar. */
|
|
|
|
if (pageRect.width() > screenSize.width)
|
|
|
|
mHorizScrollLayer.draw(pageContext);
|
2012-02-02 01:02:32 -08:00
|
|
|
|
|
|
|
/* Measure how much of the screen is checkerboarding */
|
|
|
|
if ((rootLayer != null) &&
|
|
|
|
(mProfileRender || PanningPerfAPI.isRecordingCheckerboard())) {
|
|
|
|
// Find out how much of the viewport area is valid
|
|
|
|
Rect viewport = RectUtils.round(pageContext.viewport);
|
|
|
|
Region validRegion = rootLayer.getValidRegion(pageContext);
|
|
|
|
validRegion.op(viewport, Region.Op.INTERSECT);
|
|
|
|
|
|
|
|
float checkerboard = 0.0f;
|
|
|
|
if (!(validRegion.isRect() && validRegion.getBounds().equals(viewport))) {
|
|
|
|
int screenArea = viewport.width() * viewport.height();
|
|
|
|
validRegion.op(viewport, Region.Op.REVERSE_DIFFERENCE);
|
|
|
|
|
|
|
|
// XXX The assumption here is that a Region never has overlapping
|
|
|
|
// rects. This is true, as evidenced by reading the SkRegion
|
|
|
|
// source, but is not mentioned in the Android documentation,
|
|
|
|
// and so is liable to change.
|
|
|
|
// If it does change, this code will need to be reevaluated.
|
|
|
|
Rect r = new Rect();
|
|
|
|
int checkerboardArea = 0;
|
|
|
|
for (RegionIterator i = new RegionIterator(validRegion); i.next(r);) {
|
|
|
|
checkerboardArea += r.width() * r.height();
|
|
|
|
}
|
|
|
|
|
|
|
|
checkerboard = checkerboardArea / (float)screenArea;
|
|
|
|
}
|
|
|
|
|
|
|
|
PanningPerfAPI.recordCheckerboard(checkerboard);
|
|
|
|
|
|
|
|
mCompleteFramesRendered += 1.0f - checkerboard;
|
|
|
|
mFramesRendered ++;
|
|
|
|
|
|
|
|
if (frameStartTime - mProfileOutputTime > 1000) {
|
|
|
|
mProfileOutputTime = frameStartTime;
|
|
|
|
printCheckerboardStats();
|
|
|
|
}
|
|
|
|
}
|
2011-12-09 13:05:24 -08:00
|
|
|
}
|
2011-11-21 15:15:29 -08:00
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
/* Draw the FPS. */
|
2011-12-09 07:01:31 -08:00
|
|
|
if (mShowFrameRate) {
|
|
|
|
updateDroppedFrames(frameStartTime);
|
2012-02-02 01:02:32 -08:00
|
|
|
|
2011-12-09 07:01:31 -08:00
|
|
|
try {
|
|
|
|
gl.glEnable(GL10.GL_BLEND);
|
2011-12-19 14:40:01 -08:00
|
|
|
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
|
2011-12-09 07:01:31 -08:00
|
|
|
mFrameRateLayer.draw(screenContext);
|
|
|
|
} finally {
|
|
|
|
gl.glDisable(GL10.GL_BLEND);
|
|
|
|
}
|
2011-12-01 14:05:41 -08:00
|
|
|
}
|
2011-12-01 11:11:10 -08:00
|
|
|
|
2012-01-06 03:22:52 -08:00
|
|
|
// If a layer update requires further work, schedule another redraw
|
|
|
|
if (!updated)
|
|
|
|
mView.requestRender();
|
|
|
|
|
2011-12-01 11:11:10 -08:00
|
|
|
PanningPerfAPI.recordFrameTime();
|
2012-01-30 19:45:38 -08:00
|
|
|
|
|
|
|
/* Used by robocop for testing purposes */
|
|
|
|
IntBuffer pixelBuffer = mPixelBuffer;
|
|
|
|
if (updated && pixelBuffer != null) {
|
|
|
|
synchronized (pixelBuffer) {
|
|
|
|
pixelBuffer.position(0);
|
|
|
|
gl.glReadPixels(0, 0, (int)screenContext.viewport.width(), (int)screenContext.viewport.height(), GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, pixelBuffer);
|
|
|
|
pixelBuffer.notify();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-02 01:02:32 -08:00
|
|
|
private void printCheckerboardStats() {
|
|
|
|
Log.d(PROFTAG, "Frames rendered over last 1000ms: " + mCompleteFramesRendered + "/" + mFramesRendered);
|
|
|
|
mFramesRendered = 0;
|
|
|
|
mCompleteFramesRendered = 0;
|
|
|
|
}
|
|
|
|
|
2012-01-30 19:45:38 -08:00
|
|
|
/** Used by robocop for testing purposes. Not for production use! */
|
|
|
|
IntBuffer getPixels() {
|
|
|
|
IntBuffer pixelBuffer = IntBuffer.allocate(mView.getWidth() * mView.getHeight());
|
|
|
|
synchronized (pixelBuffer) {
|
|
|
|
mPixelBuffer = pixelBuffer;
|
|
|
|
mView.requestRender();
|
|
|
|
try {
|
|
|
|
pixelBuffer.wait();
|
|
|
|
} catch (InterruptedException ie) {
|
|
|
|
}
|
|
|
|
mPixelBuffer = null;
|
|
|
|
}
|
|
|
|
return pixelBuffer;
|
2011-12-01 14:05:41 -08:00
|
|
|
}
|
2011-11-21 15:15:29 -08:00
|
|
|
|
2011-12-01 14:05:41 -08:00
|
|
|
private RenderContext createScreenContext() {
|
|
|
|
LayerController layerController = mView.getController();
|
|
|
|
IntSize viewportSize = new IntSize(layerController.getViewportSize());
|
|
|
|
RectF viewport = new RectF(0.0f, 0.0f, viewportSize.width, viewportSize.height);
|
|
|
|
FloatSize pageSize = new FloatSize(layerController.getPageSize());
|
|
|
|
return new RenderContext(viewport, pageSize, 1.0f);
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
|
2011-12-01 14:05:41 -08:00
|
|
|
private RenderContext createPageContext() {
|
|
|
|
LayerController layerController = mView.getController();
|
2011-11-18 10:28:17 -08:00
|
|
|
|
2011-12-01 14:05:41 -08:00
|
|
|
Rect viewport = new Rect();
|
|
|
|
layerController.getViewport().round(viewport);
|
2011-11-23 11:07:47 -08:00
|
|
|
|
2011-12-01 14:05:41 -08:00
|
|
|
FloatSize pageSize = new FloatSize(layerController.getPageSize());
|
|
|
|
float zoomFactor = layerController.getZoomFactor();
|
|
|
|
return new RenderContext(new RectF(viewport), pageSize, zoomFactor);
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
private Rect getPageRect() {
|
|
|
|
LayerController controller = mView.getController();
|
2011-11-23 11:07:47 -08:00
|
|
|
|
|
|
|
Point origin = PointUtils.round(controller.getOrigin());
|
|
|
|
IntSize pageSize = new IntSize(controller.getPageSize());
|
|
|
|
|
|
|
|
origin.negate();
|
|
|
|
|
|
|
|
return new Rect(origin.x, origin.y,
|
|
|
|
origin.x + pageSize.width, origin.y + pageSize.height);
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
|
2011-11-23 11:07:47 -08:00
|
|
|
private Rect transformToScissorRect(Rect rect) {
|
2011-11-18 10:28:17 -08:00
|
|
|
LayerController controller = mView.getController();
|
2011-11-23 11:07:47 -08:00
|
|
|
IntSize screenSize = new IntSize(controller.getViewportSize());
|
2011-11-18 10:28:17 -08:00
|
|
|
|
|
|
|
int left = Math.max(0, rect.left);
|
|
|
|
int top = Math.max(0, rect.top);
|
|
|
|
int right = Math.min(screenSize.width, rect.right);
|
|
|
|
int bottom = Math.min(screenSize.height, rect.bottom);
|
2011-11-23 11:07:47 -08:00
|
|
|
|
|
|
|
return new Rect(left, screenSize.height - bottom, right,
|
|
|
|
(screenSize.height - bottom) + (bottom - top));
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
|
2011-11-21 07:06:20 -08:00
|
|
|
public void onSurfaceChanged(GL10 gl, final int width, final int height) {
|
2011-11-18 10:28:17 -08:00
|
|
|
gl.glViewport(0, 0, width, height);
|
|
|
|
|
2011-11-21 07:06:20 -08:00
|
|
|
// updating the state in the view/controller/client should be
|
|
|
|
// done on the main UI thread, not the GL renderer thread
|
|
|
|
mView.post(new Runnable() {
|
|
|
|
public void run() {
|
2011-11-23 11:07:29 -08:00
|
|
|
mView.setViewportSize(new IntSize(width, height));
|
2011-12-09 07:01:28 -08:00
|
|
|
moveFrameRateLayer(width, height);
|
2011-11-21 07:06:20 -08:00
|
|
|
}
|
|
|
|
});
|
2011-11-18 10:28:17 -08:00
|
|
|
|
|
|
|
/* TODO: Throw away tile images? */
|
|
|
|
}
|
|
|
|
|
2011-12-09 07:01:26 -08:00
|
|
|
private void updateDroppedFrames(long frameStartTime) {
|
|
|
|
int frameElapsedTime = (int)(SystemClock.uptimeMillis() - frameStartTime);
|
2011-11-30 09:27:13 -08:00
|
|
|
|
2011-12-09 07:01:26 -08:00
|
|
|
/* Update the running statistics. */
|
|
|
|
mFrameTimingsSum -= mFrameTimings[mCurrentFrame];
|
|
|
|
mFrameTimingsSum += frameElapsedTime;
|
|
|
|
mDroppedFrames -= (mFrameTimings[mCurrentFrame] + 1) / MAX_FRAME_TIME;
|
|
|
|
mDroppedFrames += (frameElapsedTime + 1) / MAX_FRAME_TIME;
|
2011-11-18 18:07:14 -08:00
|
|
|
|
2012-01-06 12:21:49 -08:00
|
|
|
mFrameTimings[mCurrentFrame] = frameElapsedTime;
|
2011-12-09 07:01:26 -08:00
|
|
|
mCurrentFrame = (mCurrentFrame + 1) % mFrameTimings.length;
|
2011-11-30 09:27:13 -08:00
|
|
|
|
2011-12-09 07:01:26 -08:00
|
|
|
int averageTime = mFrameTimingsSum / mFrameTimings.length;
|
|
|
|
mFrameRateLayer.beginTransaction();
|
|
|
|
try {
|
|
|
|
mFrameRateLayer.setText(averageTime + " ms/" + mDroppedFrames);
|
|
|
|
} finally {
|
|
|
|
mFrameRateLayer.endTransaction();
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-09 07:01:28 -08:00
|
|
|
/* Given the new dimensions for the surface, moves the frame rate layer appropriately. */
|
|
|
|
private void moveFrameRateLayer(int width, int height) {
|
|
|
|
mFrameRateLayer.beginTransaction();
|
|
|
|
try {
|
|
|
|
Point origin = new Point(width - FRAME_RATE_METER_WIDTH - 8,
|
|
|
|
height - FRAME_RATE_METER_HEIGHT + 8);
|
|
|
|
mFrameRateLayer.setOrigin(origin);
|
|
|
|
} finally {
|
|
|
|
mFrameRateLayer.endTransaction();
|
|
|
|
}
|
|
|
|
}
|
2011-12-09 07:01:31 -08:00
|
|
|
|
2012-02-02 01:02:32 -08:00
|
|
|
private void checkMonitoringEnabled() {
|
2011-12-09 07:01:31 -08:00
|
|
|
/* Do this I/O off the main thread to minimize its impact on startup time. */
|
|
|
|
new Thread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
Context context = mView.getContext();
|
|
|
|
SharedPreferences preferences = context.getSharedPreferences("GeckoApp", 0);
|
|
|
|
mShowFrameRate = preferences.getBoolean("showFrameRate", false);
|
2012-02-02 01:02:32 -08:00
|
|
|
mProfileRender = Log.isLoggable(PROFTAG, Log.DEBUG);
|
2011-12-09 07:01:31 -08:00
|
|
|
}
|
2012-01-02 21:57:06 -08:00
|
|
|
}).start();
|
2011-12-09 07:01:31 -08:00
|
|
|
}
|
2011-12-09 13:05:24 -08:00
|
|
|
|
2012-01-23 20:10:24 -08:00
|
|
|
private void updateCheckerboardLayer(GL10 gl, RenderContext renderContext) {
|
2012-02-03 12:35:01 -08:00
|
|
|
int checkerboardColor = mView.getController().getCheckerboardColor();
|
|
|
|
boolean showChecks = mView.getController().checkerboardShouldShowChecks();
|
|
|
|
if (checkerboardColor == mCheckerboardImage.getColor() &&
|
|
|
|
showChecks == mCheckerboardImage.getShowChecks()) {
|
2012-01-23 20:10:24 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mCheckerboardLayer.beginTransaction();
|
|
|
|
try {
|
2012-02-03 12:35:01 -08:00
|
|
|
mCheckerboardImage.update(showChecks, checkerboardColor);
|
2012-01-23 20:10:24 -08:00
|
|
|
mCheckerboardLayer.invalidate();
|
|
|
|
} finally {
|
|
|
|
mCheckerboardLayer.endTransaction();
|
|
|
|
}
|
|
|
|
|
|
|
|
mCheckerboardLayer.update(gl, renderContext);
|
|
|
|
}
|
|
|
|
|
2011-12-09 13:05:24 -08:00
|
|
|
class FadeRunnable implements Runnable {
|
|
|
|
private boolean mStarted;
|
|
|
|
private long mRunAt;
|
|
|
|
|
|
|
|
void scheduleStartFade(long delay) {
|
|
|
|
mRunAt = SystemClock.elapsedRealtime() + delay;
|
|
|
|
if (!mStarted) {
|
|
|
|
mView.postDelayed(this, delay);
|
|
|
|
mStarted = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void scheduleNextFadeFrame() {
|
|
|
|
if (mStarted) {
|
|
|
|
Log.e(LOGTAG, "scheduleNextFadeFrame() called while scheduled for starting fade");
|
|
|
|
}
|
|
|
|
mView.postDelayed(this, 1000L / 60L); // request another frame at 60fps
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean timeToFade() {
|
|
|
|
return !mStarted;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void run() {
|
|
|
|
long timeDelta = mRunAt - SystemClock.elapsedRealtime();
|
|
|
|
if (timeDelta > 0) {
|
|
|
|
// the run-at time was pushed back, so reschedule
|
|
|
|
mView.postDelayed(this, timeDelta);
|
|
|
|
} else {
|
|
|
|
// reached the run-at time, execute
|
|
|
|
mStarted = false;
|
|
|
|
mView.requestRender();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-12-09 07:01:28 -08:00
|
|
|
}
|