(Part 5) Move overrides and abstract/interface implementations up from GeckoGLLayerClient into GeckoLayerClient
authorKartikaya Gupta <kgupta@mozilla.com>
Fri, 17 Feb 2012 10:33:23 -0500
changeset 89155 6e7b99c38e50e5c17a0521c2a4bcd6d0a2630bea
parent 89154 2bcd576a2483ea79587b64e0ff79dfa61986443a
child 89156 8a2ec0ed8ce0795d7e7f0faac058846ebf9b28a1
push id22242
push userkgupta@mozilla.com
push dateWed, 14 Mar 2012 15:19:09 +0000
treeherdermozilla-central@936ef50fa498 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
milestone13.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
(Part 5) Move overrides and abstract/interface implementations up from GeckoGLLayerClient into GeckoLayerClient
mobile/android/base/gfx/GeckoGLLayerClient.java
mobile/android/base/gfx/GeckoLayerClient.java
--- a/mobile/android/base/gfx/GeckoGLLayerClient.java
+++ b/mobile/android/base/gfx/GeckoGLLayerClient.java
@@ -43,142 +43,25 @@ import android.content.Context;
 import android.graphics.Bitmap;
 import android.graphics.Point;
 import android.graphics.PointF;
 import android.graphics.Rect;
 import android.graphics.RectF;
 import android.util.Log;
 import android.view.View;
 
-public class GeckoGLLayerClient extends GeckoLayerClient
-                                implements FlexibleGLSurfaceView.Listener, VirtualLayer.Listener {
+public class GeckoGLLayerClient extends GeckoLayerClient {
     private static final String LOGTAG = "GeckoGLLayerClient";
 
-    private LayerRenderer mLayerRenderer;
     private boolean mLayerRendererInitialized;
 
     public GeckoGLLayerClient(Context context) {
         super(context);
     }
 
-    @Override
-    public Rect beginDrawing(int width, int height, int tileWidth, int tileHeight,
-                             String metadata, boolean hasDirectTexture) {
-        Rect bufferRect = super.beginDrawing(width, height, tileWidth, tileHeight,
-                                             metadata, hasDirectTexture);
-        if (bufferRect == null) {
-            return null;
-        }
-
-        // Be sure to adjust the buffer size; if it's not at least as large as the viewport size,
-        // ViewportMetrics.getOptimumViewportOffset() gets awfully confused and severe display
-        // corruption results!
-        if (mBufferSize.width != width || mBufferSize.height != height) {
-            mBufferSize = new IntSize(width, height);
-        }
-
-        return bufferRect;
-    }
-
-    @Override
-    protected boolean handleDirectTextureChange(boolean hasDirectTexture) {
-        Log.e(LOGTAG, "### handleDirectTextureChange");
-        if (mTileLayer != null) {
-            return false;
-        }
-
-        Log.e(LOGTAG, "### Creating virtual layer");
-        VirtualLayer virtualLayer = new VirtualLayer();
-        virtualLayer.setListener(this);
-        virtualLayer.setSize(getBufferSize());
-        mLayerController.setRoot(virtualLayer);
-        mTileLayer = virtualLayer;
-
-        sendResizeEventIfNecessary(true);
-        return true;
-    }
-
-    @Override
-    public void setLayerController(LayerController layerController) {
-        super.setLayerController(layerController);
-
-        LayerView view = layerController.getView();
-        view.setListener(this);
-
-        mLayerRenderer = new LayerRenderer(view);
-    }
-
-    @Override
-    protected boolean shouldDrawProceed(int tileWidth, int tileHeight) {
-        Log.e(LOGTAG, "### shouldDrawProceed");
-        // Always draw.
-        return true;
-    }
-
-    @Override
-    protected void updateLayerAfterDraw(Rect updatedRect) {
-        Log.e(LOGTAG, "### updateLayerAfterDraw");
-        // Nothing to do.
-    }
-
-    @Override
-    protected IntSize getBufferSize() {
-        View view = mLayerController.getView();
-        IntSize size = new IntSize(view.getWidth(), view.getHeight());
-        Log.e(LOGTAG, "### getBufferSize " + size);
-        return size;
-    }
-
-    @Override
-    protected IntSize getTileSize() {
-        Log.e(LOGTAG, "### getTileSize " + getBufferSize());
-        return getBufferSize();
-    }
-
-    @Override
-    protected void tileLayerUpdated() {
-        // Set the new origin and resolution instantly.
-        mTileLayer.performUpdates(null);
-    }
-
-    @Override
-    public Bitmap getBitmap() {
-        Log.e(LOGTAG, "### getBitmap");
-        IntSize size = getBufferSize();
-        try {
-            return Bitmap.createBitmap(size.width, size.height, Bitmap.Config.RGB_565);
-        } catch (OutOfMemoryError oom) {
-            Log.e(LOGTAG, "Unable to create bitmap", oom);
-            return null;
-        }
-    }
-
-    public void dimensionsChanged(Point newOrigin, float newResolution) {
-        Log.e(LOGTAG, "### dimensionsChanged " + newOrigin + " " + newResolution);
-    }
-
-    /* Informs Gecko that the screen size has changed. */
-    @Override
-    protected void sendResizeEventIfNecessary(boolean force) {
-        Log.e(LOGTAG, "### sendResizeEventIfNecessary " + force);
-
-        IntSize newSize = getBufferSize();
-        if (!force && mScreenSize != null && mScreenSize.equals(newSize)) {
-            return;
-        }
-
-        mScreenSize = newSize;
-
-        Log.e(LOGTAG, "### Screen-size changed to " + mScreenSize);
-        GeckoEvent event = GeckoEvent.createSizeChangedEvent(mScreenSize.width, mScreenSize.height,
-                                                             mScreenSize.width, mScreenSize.height,
-                                                             mScreenSize.width, mScreenSize.height);
-        GeckoAppShell.sendEventToGecko(event);
-    }
-
     /** This function is invoked by Gecko via JNI; be careful when modifying signature. */
     public ViewTransform getViewTransform() {
         Log.e(LOGTAG, "### getViewTransform()");
 
         // NB: We don't begin a transaction here because this can be called in a synchronous
         // manner between beginDrawing() and endDrawing(), and that will cause a deadlock.
 
         synchronized (mLayerController) {
@@ -189,38 +72,16 @@ public class GeckoGLLayerClient extends 
             float scrollY = viewportOrigin.y;
             float zoomFactor = viewportMetrics.getZoomFactor();
             Log.e(LOGTAG, "### Viewport metrics = " + viewportMetrics + " tile reso = " +
                   mTileLayer.getResolution());
             return new ViewTransform(scrollX, scrollY, zoomFactor);
         }
     }
 
-    public void renderRequested() {
-        Log.e(LOGTAG, "### Render requested, scheduling composite");
-        GeckoAppShell.scheduleComposite();
-    }
-
-    public void compositionPauseRequested() {
-        Log.e(LOGTAG, "### Scheduling PauseComposition");
-        GeckoAppShell.schedulePauseComposition();
-    }
-
-    public void compositionResumeRequested() {
-        Log.e(LOGTAG, "### Scheduling ResumeComposition");
-        GeckoAppShell.scheduleResumeComposition();
-    }
-
-    public void surfaceChanged(int width, int height) {
-        compositionPauseRequested();
-        mLayerController.setViewportSize(new FloatSize(width, height));
-        compositionResumeRequested();
-        renderRequested();
-    }
-
     /** This function is invoked by Gecko via JNI; be careful when modifying signature. */
     public LayerRenderer.Frame createFrame() {
         // Create the shaders and textures if necessary.
         if (!mLayerRendererInitialized) {
             mLayerRenderer.createProgram();
             mLayerRendererInitialized = true;
         }
 
--- a/mobile/android/base/gfx/GeckoLayerClient.java
+++ b/mobile/android/base/gfx/GeckoLayerClient.java
@@ -43,29 +43,34 @@ import org.mozilla.gecko.GeckoApp;
 import org.mozilla.gecko.GeckoAppShell;
 import org.mozilla.gecko.GeckoEvent;
 import org.mozilla.gecko.GeckoEventListener;
 import org.json.JSONException;
 import org.json.JSONObject;
 import android.content.Context;
 import android.graphics.Bitmap;
 import android.graphics.Color;
+import android.graphics.Point;
 import android.graphics.PointF;
 import android.graphics.Rect;
 import android.graphics.RectF;
 import android.os.SystemClock;
 import android.util.DisplayMetrics;
 import android.util.Log;
+import android.view.View;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
-public abstract class GeckoLayerClient implements GeckoEventListener {
+public abstract class GeckoLayerClient implements GeckoEventListener,
+                                                  FlexibleGLSurfaceView.Listener,
+                                                  VirtualLayer.Listener {
     private static final String LOGTAG = "GeckoLayerClient";
 
     protected LayerController mLayerController;
+    protected LayerRenderer mLayerRenderer;
 
     protected IntSize mScreenSize;
     protected IntSize mBufferSize;
 
     protected Layer mTileLayer;
 
     /* The viewport that Gecko is currently displaying. */
     protected ViewportMetrics mGeckoViewport;
@@ -87,24 +92,16 @@ public abstract class GeckoLayerClient i
 
     private String mLastCheckerboardColor;
 
     private static Pattern sColorPattern;
 
     /* Used by robocop for testing purposes */
     private DrawListener mDrawListener;
 
-    protected abstract boolean handleDirectTextureChange(boolean hasDirectTexture);
-    protected abstract boolean shouldDrawProceed(int tileWidth, int tileHeight);
-    protected abstract void updateLayerAfterDraw(Rect updatedRect);
-    protected abstract IntSize getBufferSize();
-    protected abstract IntSize getTileSize();
-    protected abstract void tileLayerUpdated();
-    public abstract Bitmap getBitmap();
-
     public GeckoLayerClient(Context context) {
         mScreenSize = new IntSize(0, 0);
         mBufferSize = new IntSize(0, 0);
     }
 
     /** Attaches the root layer to the layer controller so that Gecko appears. */
     public void setLayerController(LayerController layerController) {
         mLayerController = layerController;
@@ -113,16 +110,21 @@ public abstract class GeckoLayerClient i
         if (mGeckoViewport != null) {
             layerController.setViewportMetrics(mGeckoViewport);
         }
 
         GeckoAppShell.registerGeckoEventListener("Viewport:UpdateAndDraw", this);
         GeckoAppShell.registerGeckoEventListener("Viewport:UpdateLater", this);
 
         sendResizeEventIfNecessary();
+
+        LayerView view = layerController.getView();
+        view.setListener(this);
+
+        mLayerRenderer = new LayerRenderer(view);
     }
 
     /** This function is invoked by Gecko via JNI; be careful when modifying signature. */
     public Rect beginDrawing(int width, int height, int tileWidth, int tileHeight,
                              String metadata, boolean hasDirectTexture) {
         Log.e(LOGTAG, "### beginDrawing " + width + " " + height + " " + tileWidth + " " +
               tileHeight + " " + hasDirectTexture);
 
@@ -189,16 +191,24 @@ public abstract class GeckoLayerClient i
                     mTileLayer.endTransaction();
                 }
                 return null;
             }
             bufferRect.offset(Math.round(-currentOrigin.x), Math.round(-currentOrigin.y));
         }
 
         mTileLayer.beginTransaction(mLayerController.getView());
+
+        // Be sure to adjust the buffer size; if it's not at least as large as the viewport size,
+        // ViewportMetrics.getOptimumViewportOffset() gets awfully confused and severe display
+        // corruption results!
+        if (mBufferSize.width != width || mBufferSize.height != height) {
+            mBufferSize = new IntSize(width, height);
+        }
+
         return bufferRect;
     }
 
     /** This function is invoked by Gecko via JNI; be careful when modifying signature.
      *
      * TODO: Would be cleaner if this took an android.graphics.Rect instead, but that would require
      * a little more JNI magic.
      */
@@ -250,37 +260,27 @@ public abstract class GeckoLayerClient i
             mLayerController.abortPanZoomAnimation();
         }
     }
 
     /* Informs Gecko that the screen size has changed. */
     protected void sendResizeEventIfNecessary(boolean force) {
         Log.e(LOGTAG, "### sendResizeEventIfNecessary " + force);
 
-        DisplayMetrics metrics = new DisplayMetrics();
-        GeckoApp.mAppContext.getWindowManager().getDefaultDisplay().getMetrics(metrics);
-
-        // Return immediately if the screen size hasn't changed or the viewport
-        // size is zero (which indicates that the rendering surface hasn't been
-        // allocated yet).
-        boolean screenSizeChanged = (metrics.widthPixels != mScreenSize.width ||
-                                     metrics.heightPixels != mScreenSize.height);
-        boolean viewportSizeValid = (mLayerController != null &&
-                                     mLayerController.getViewportSize().isPositive());
-        if (!(force || (screenSizeChanged && viewportSizeValid))) {
+        IntSize newSize = getBufferSize();
+        if (!force && mScreenSize != null && mScreenSize.equals(newSize)) {
             return;
         }
 
-        mScreenSize = new IntSize(metrics.widthPixels, metrics.heightPixels);
-        IntSize bufferSize = getBufferSize(), tileSize = getTileSize();
+        mScreenSize = newSize;
 
         Log.e(LOGTAG, "### Screen-size changed to " + mScreenSize);
-        GeckoEvent event = GeckoEvent.createSizeChangedEvent(bufferSize.width, bufferSize.height,
-                                                             metrics.widthPixels, metrics.heightPixels,
-                                                             tileSize.width, tileSize.height);
+        GeckoEvent event = GeckoEvent.createSizeChangedEvent(mScreenSize.width, mScreenSize.height,
+                                                             mScreenSize.width, mScreenSize.height,
+                                                             mScreenSize.width, mScreenSize.height);
         GeckoAppShell.sendEventToGecko(event);
     }
 
     // Parses a color from an RGB triple of the form "rgb([0-9]+, [0-9]+, [0-9]+)". If the color
     // cannot be parsed, returns white.
     private static int parseColorFromGecko(String string) {
         if (sColorPattern == null) {
             sColorPattern = Pattern.compile("rgb\\((\\d+),\\s*(\\d+),\\s*(\\d+)\\)");
@@ -292,16 +292,72 @@ public abstract class GeckoLayerClient i
         }
 
         int r = Integer.parseInt(matcher.group(1));
         int g = Integer.parseInt(matcher.group(2));
         int b = Integer.parseInt(matcher.group(3));
         return Color.rgb(r, g, b);
     }
 
+    protected boolean handleDirectTextureChange(boolean hasDirectTexture) {
+        Log.e(LOGTAG, "### handleDirectTextureChange");
+        if (mTileLayer != null) {
+            return false;
+        }
+
+        Log.e(LOGTAG, "### Creating virtual layer");
+        VirtualLayer virtualLayer = new VirtualLayer();
+        virtualLayer.setListener(this);
+        virtualLayer.setSize(getBufferSize());
+        mLayerController.setRoot(virtualLayer);
+        mTileLayer = virtualLayer;
+
+        sendResizeEventIfNecessary(true);
+        return true;
+    }
+
+    protected boolean shouldDrawProceed(int tileWidth, int tileHeight) {
+        Log.e(LOGTAG, "### shouldDrawProceed");
+        // Always draw.
+        return true;
+    }
+
+    protected void updateLayerAfterDraw(Rect updatedRect) {
+        Log.e(LOGTAG, "### updateLayerAfterDraw");
+        // Nothing to do.
+    }
+
+    protected IntSize getBufferSize() {
+        View view = mLayerController.getView();
+        IntSize size = new IntSize(view.getWidth(), view.getHeight());
+        Log.e(LOGTAG, "### getBufferSize " + size);
+        return size;
+    }
+
+    protected IntSize getTileSize() {
+        Log.e(LOGTAG, "### getTileSize " + getBufferSize());
+        return getBufferSize();
+    }
+
+    protected void tileLayerUpdated() {
+        // Set the new origin and resolution instantly.
+        mTileLayer.performUpdates(null);
+    }
+
+    public Bitmap getBitmap() {
+        Log.e(LOGTAG, "### getBitmap");
+        IntSize size = getBufferSize();
+        try {
+            return Bitmap.createBitmap(size.width, size.height, Bitmap.Config.RGB_565);
+        } catch (OutOfMemoryError oom) {
+            Log.e(LOGTAG, "Unable to create bitmap", oom);
+            return null;
+        }
+    }
+
     public void render() {
         adjustViewportWithThrottling();
     }
 
     private void adjustViewportWithThrottling() {
         if (!mLayerController.getRedrawHint())
             return;
 
@@ -379,16 +435,47 @@ public abstract class GeckoLayerClient i
             return new ViewportMetrics(mGeckoViewport);
         return null;
     }
 
     private void sendResizeEventIfNecessary() {
         sendResizeEventIfNecessary(false);
     }
 
+    /** Implementation of FlexibleGLSurfaceView.Listener */
+    public void renderRequested() {
+        Log.e(LOGTAG, "### Render requested, scheduling composite");
+        GeckoAppShell.scheduleComposite();
+    }
+
+    /** Implementation of FlexibleGLSurfaceView.Listener */
+    public void compositionPauseRequested() {
+        Log.e(LOGTAG, "### Scheduling PauseComposition");
+        GeckoAppShell.schedulePauseComposition();
+    }
+
+    /** Implementation of FlexibleGLSurfaceView.Listener */
+    public void compositionResumeRequested() {
+        Log.e(LOGTAG, "### Scheduling ResumeComposition");
+        GeckoAppShell.scheduleResumeComposition();
+    }
+
+    /** Implementation of FlexibleGLSurfaceView.Listener */
+    public void surfaceChanged(int width, int height) {
+        compositionPauseRequested();
+        mLayerController.setViewportSize(new FloatSize(width, height));
+        compositionResumeRequested();
+        renderRequested();
+    }
+
+    /** Implementation of VirtualLayer.Listener */
+    public void dimensionsChanged(Point newOrigin, float newResolution) {
+        Log.e(LOGTAG, "### dimensionsChanged " + newOrigin + " " + newResolution);
+    }
+
     /** Used by robocop for testing purposes. Not for production use! This is called via reflection by robocop. */
     public void setDrawListener(DrawListener listener) {
         mDrawListener = listener;
     }
 
     /** Used by robocop for testing purposes. Not for production use! This is used via reflection by robocop. */
     public interface DrawListener {
         public void drawFinished(int x, int y, int width, int height);