From 309c4ec3c6b6e3e57faa8491c2417a8abd52564f Mon Sep 17 00:00:00 2001 From: Kartikaya Gupta Date: Tue, 15 Nov 2011 12:11:53 -0500 Subject: [PATCH] Bug 702416 - Replace IntRect with android.graphics.Rect [r=pcwalton] --- embedding/android/GeckoApp.java | 4 +- embedding/android/Makefile.in | 2 +- embedding/android/gfx/FloatRect.java | 6 +- .../android/gfx/GeckoSoftwareLayerClient.java | 23 +++---- embedding/android/gfx/LayerClient.java | 1 - embedding/android/gfx/LayerController.java | 6 +- embedding/android/gfx/LayerRenderer.java | 31 ++++----- .../gfx/{IntRect.java => RectUtils.java} | 63 +++++-------------- embedding/android/gfx/TileLayer.java | 17 +++-- embedding/android/ui/PanZoomController.java | 1 - embedding/android/ui/ViewportController.java | 1 - 11 files changed, 65 insertions(+), 90 deletions(-) rename embedding/android/gfx/{IntRect.java => RectUtils.java} (52%) diff --git a/embedding/android/GeckoApp.java b/embedding/android/GeckoApp.java index 9797759ef48..c7f9450efa7 100644 --- a/embedding/android/GeckoApp.java +++ b/embedding/android/GeckoApp.java @@ -42,11 +42,11 @@ package org.mozilla.gecko; import org.mozilla.gecko.gfx.GeckoSoftwareLayerClient; import org.mozilla.gecko.gfx.FloatRect; -import org.mozilla.gecko.gfx.IntRect; import org.mozilla.gecko.gfx.IntSize; import org.mozilla.gecko.gfx.LayerController; import org.mozilla.gecko.gfx.LayerView; import org.mozilla.gecko.gfx.PlaceholderLayerClient; +import org.mozilla.gecko.gfx.RectUtils; import org.mozilla.gecko.Tab.HistoryEntry; import java.io.*; @@ -751,7 +751,7 @@ abstract public class GeckoApp }); connectGeckoLayerClient(); } else if (event.equals("PanZoom:Ack")) { - final IntRect rect = new IntRect(message.getJSONObject("rect")); + Rect rect = RectUtils.create(message.getJSONObject("rect")); mSoftwareLayerClient.jsPanZoomCompleted(rect); } else if (event.equals("PanZoom:Resize")) { IntSize size = new IntSize(message.getJSONObject("size")); diff --git a/embedding/android/Makefile.in b/embedding/android/Makefile.in index b21b4f9b3e5..395f6042278 100644 --- a/embedding/android/Makefile.in +++ b/embedding/android/Makefile.in @@ -76,7 +76,6 @@ JAVAFILES = \ gfx/FloatRect.java \ gfx/GeckoSoftwareLayerClient.java \ gfx/InputConnectionHandler.java \ - gfx/IntRect.java \ gfx/IntSize.java \ gfx/Layer.java \ gfx/LayerClient.java \ @@ -86,6 +85,7 @@ JAVAFILES = \ gfx/NinePatchTileLayer.java \ gfx/PlaceholderLayerClient.java \ gfx/PointUtils.java \ + gfx/RectUtils.java \ gfx/SingleTileLayer.java \ gfx/TextureReaper.java \ gfx/TextLayer.java \ diff --git a/embedding/android/gfx/FloatRect.java b/embedding/android/gfx/FloatRect.java index 2f7e12d83b7..785a8919eb3 100644 --- a/embedding/android/gfx/FloatRect.java +++ b/embedding/android/gfx/FloatRect.java @@ -38,7 +38,7 @@ package org.mozilla.gecko.gfx; import android.graphics.PointF; -import org.mozilla.gecko.gfx.IntRect; +import android.graphics.Rect; public class FloatRect { public final float x, y, width, height; @@ -47,8 +47,8 @@ public class FloatRect { x = inX; y = inY; width = inWidth; height = inHeight; } - public FloatRect(IntRect intRect) { - x = intRect.x; y = intRect.y; width = intRect.width; height = intRect.height; + public FloatRect(Rect intRect) { + x = intRect.left; y = intRect.top; width = intRect.width(); height = intRect.height(); } @Override diff --git a/embedding/android/gfx/GeckoSoftwareLayerClient.java b/embedding/android/gfx/GeckoSoftwareLayerClient.java index 19bc7491bb0..4424410d8fb 100644 --- a/embedding/android/gfx/GeckoSoftwareLayerClient.java +++ b/embedding/android/gfx/GeckoSoftwareLayerClient.java @@ -39,7 +39,6 @@ package org.mozilla.gecko.gfx; import org.mozilla.gecko.gfx.CairoImage; import org.mozilla.gecko.gfx.FloatRect; -import org.mozilla.gecko.gfx.IntRect; import org.mozilla.gecko.gfx.IntSize; import org.mozilla.gecko.gfx.LayerClient; import org.mozilla.gecko.gfx.LayerController; @@ -51,6 +50,7 @@ import org.mozilla.gecko.GeckoAppShell; import org.mozilla.gecko.GeckoEvent; import android.content.Context; import android.graphics.Point; +import android.graphics.Rect; import android.util.Log; import java.nio.ByteBuffer; import java.util.concurrent.Semaphore; @@ -74,7 +74,7 @@ public class GeckoSoftwareLayerClient extends LayerClient { private FloatRect mGeckoVisibleRect; /* The viewport rect that Gecko is currently displaying. */ - private IntRect mJSPanningToRect; + private Rect mJSPanningToRect; /* The rect that we just told chrome JavaScript to pan to. */ private boolean mWaitingForJSPanZoom; @@ -155,15 +155,15 @@ public class GeckoSoftwareLayerClient extends LayerClient { mTileLayer.origin = layerRect.getOrigin(); } - repaint(new IntRect(x, y, width, height)); + repaint(new Rect(x, y, x + width, y + height)); } - private void repaint(IntRect rect) { + private void repaint(Rect rect) { mTileLayer.paintSubimage(mCairoImage, rect); } /** Called whenever the chrome JS finishes panning or zooming to some location. */ - public void jsPanZoomCompleted(IntRect rect) { + public void jsPanZoomCompleted(Rect rect) { mGeckoVisibleRect = new FloatRect(rect); if (mWaitingForJSPanZoom) render(); @@ -234,10 +234,11 @@ public class GeckoSoftwareLayerClient extends LayerClient { /* Otherwise, we need to get Gecko's visible rect equal to our visible rect before we can * safely draw. If we're just waiting for chrome JavaScript to catch up, we do nothing. * This check avoids bombarding the chrome JavaScript with messages. */ - IntRect panToRect = new IntRect((int)Math.round(viewportRect.x), - (int)Math.round(viewportRect.y), - LayerController.TILE_WIDTH, - LayerController.TILE_HEIGHT); + int viewportRectX = (int)Math.round(viewportRect.x); + int viewportRectY = (int)Math.round(viewportRect.y); + Rect panToRect = new Rect(viewportRectX, viewportRectY, + viewportRectX + LayerController.TILE_WIDTH, + viewportRectY + LayerController.TILE_HEIGHT); if (mWaitingForJSPanZoom && mJSPanningToRect != null && mJSPanningToRect.equals(panToRect)) { @@ -248,8 +249,8 @@ public class GeckoSoftwareLayerClient extends LayerClient { * set a flag to remind us to try the redraw again. */ GeckoAppShell.sendEventToGecko(new GeckoEvent("PanZoom:PanZoom", - "{\"x\": " + panToRect.x + ", \"y\": " + panToRect.y + - ", \"width\": " + panToRect.width + ", \"height\": " + panToRect.height + + "{\"x\": " + panToRect.left + ", \"y\": " + panToRect.top + + ", \"width\": " + panToRect.width() + ", \"height\": " + panToRect.height() + ", \"zoomFactor\": " + getZoomFactor() + "}")); mJSPanningToRect = panToRect; diff --git a/embedding/android/gfx/LayerClient.java b/embedding/android/gfx/LayerClient.java index 97756f7b021..73d13202b0d 100644 --- a/embedding/android/gfx/LayerClient.java +++ b/embedding/android/gfx/LayerClient.java @@ -37,7 +37,6 @@ package org.mozilla.gecko.gfx; -import org.mozilla.gecko.gfx.IntRect; import org.mozilla.gecko.gfx.IntSize; import org.mozilla.gecko.gfx.LayerController; diff --git a/embedding/android/gfx/LayerController.java b/embedding/android/gfx/LayerController.java index e05bf79fa64..9b4a7dd4833 100644 --- a/embedding/android/gfx/LayerController.java +++ b/embedding/android/gfx/LayerController.java @@ -38,7 +38,6 @@ package org.mozilla.gecko.gfx; import org.mozilla.gecko.gfx.FloatRect; -import org.mozilla.gecko.gfx.IntRect; import org.mozilla.gecko.gfx.IntSize; import org.mozilla.gecko.gfx.Layer; import org.mozilla.gecko.gfx.LayerClient; @@ -50,6 +49,7 @@ import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.PointF; +import android.graphics.Rect; import android.util.Log; import android.view.MotionEvent; import android.view.GestureDetector; @@ -222,8 +222,8 @@ public class LayerController { // Returns true if a checkerboard is about to be visible. private boolean aboutToCheckerboard() { - IntRect pageRect = new IntRect(0, 0, mPageSize.width, mPageSize.height); - IntRect adjustedPageRect = pageRect.contract(DANGER_ZONE_X, DANGER_ZONE_Y); + Rect pageRect = new Rect(0, 0, mPageSize.width, mPageSize.height); + Rect adjustedPageRect = RectUtils.contract(pageRect, DANGER_ZONE_X, DANGER_ZONE_Y); FloatRect visiblePageRect = mVisibleRect.intersect(new FloatRect(adjustedPageRect)); FloatRect adjustedTileRect = getTileRect().contract(DANGER_ZONE_X, DANGER_ZONE_Y); return !adjustedTileRect.contains(visiblePageRect); diff --git a/embedding/android/gfx/LayerRenderer.java b/embedding/android/gfx/LayerRenderer.java index 24eb1ad8a54..daaa3f2d7be 100644 --- a/embedding/android/gfx/LayerRenderer.java +++ b/embedding/android/gfx/LayerRenderer.java @@ -39,7 +39,6 @@ package org.mozilla.gecko.gfx; import org.mozilla.gecko.gfx.BufferedCairoImage; import org.mozilla.gecko.gfx.FloatRect; -import org.mozilla.gecko.gfx.IntRect; import org.mozilla.gecko.gfx.IntSize; import org.mozilla.gecko.gfx.LayerController; import org.mozilla.gecko.gfx.LayerView; @@ -49,6 +48,7 @@ import org.mozilla.gecko.gfx.TextureReaper; import org.mozilla.gecko.gfx.TextLayer; import org.mozilla.gecko.gfx.TileLayer; import android.content.Context; +import android.graphics.Rect; import android.opengl.GLSurfaceView; import android.util.DisplayMetrics; import android.util.Log; @@ -114,11 +114,11 @@ public class LayerRenderer implements GLSurfaceView.Renderer { mShadowLayer.draw(gl); /* Draw the checkerboard. */ - IntRect pageRect = clampToScreen(getPageRect()); + Rect pageRect = clampToScreen(getPageRect()); IntSize screenSize = controller.getScreenSize(); gl.glEnable(GL10.GL_SCISSOR_TEST); - gl.glScissor(pageRect.x, screenSize.height - (pageRect.y + pageRect.height), - pageRect.width, pageRect.height); + gl.glScissor(pageRect.left, screenSize.height - pageRect.bottom, + pageRect.width(), pageRect.height()); gl.glLoadIdentity(); mCheckerboardLayer.draw(gl); @@ -153,27 +153,28 @@ public class LayerRenderer implements GLSurfaceView.Renderer { gl.glTranslatef(-visibleRect.x, -visibleRect.y, 0.0f); } - private IntRect getPageRect() { + private Rect getPageRect() { LayerController controller = mView.getController(); float zoomFactor = controller.getZoomFactor(); FloatRect visibleRect = controller.getVisibleRect(); IntSize pageSize = controller.getPageSize(); - return new IntRect((int)Math.round(-zoomFactor * visibleRect.x), - (int)Math.round(-zoomFactor * visibleRect.y), - (int)Math.round(zoomFactor * pageSize.width), - (int)Math.round(zoomFactor * pageSize.height)); + int x = (int)Math.round(-zoomFactor * visibleRect.x); + int y = (int)Math.round(-zoomFactor * visibleRect.y); + return new Rect(x, y, + x + (int)Math.round(zoomFactor * pageSize.width), + y + (int)Math.round(zoomFactor * pageSize.height)); } - private IntRect clampToScreen(IntRect rect) { + private Rect clampToScreen(Rect rect) { LayerController controller = mView.getController(); IntSize screenSize = controller.getScreenSize(); - int left = Math.max(0, rect.x); - int top = Math.max(0, rect.y); - int right = Math.min(screenSize.width, rect.getRight()); - int bottom = Math.min(screenSize.height, rect.getBottom()); - return new IntRect(left, top, right - left, bottom - top); + 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); + return new Rect(left, top, right, bottom); } public void onSurfaceChanged(GL10 gl, int width, int height) { diff --git a/embedding/android/gfx/IntRect.java b/embedding/android/gfx/RectUtils.java similarity index 52% rename from embedding/android/gfx/IntRect.java rename to embedding/android/gfx/RectUtils.java index 5c9ae3044ed..6e76dcae414 100644 --- a/embedding/android/gfx/IntRect.java +++ b/embedding/android/gfx/RectUtils.java @@ -15,11 +15,11 @@ * 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 + * Portions created by the Initial Developer are Copyright (C) 2011 * the Initial Developer. All Rights Reserved. * * Contributor(s): - * Patrick Walton + * Kartikaya Gupta * * 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 @@ -37,58 +37,29 @@ package org.mozilla.gecko.gfx; -import android.graphics.Point; +import android.graphics.Rect; import org.json.JSONException; import org.json.JSONObject; -public class IntRect implements Cloneable { - public final int x, y, width, height; - - public IntRect(int inX, int inY, int inWidth, int inHeight) { - x = inX; y = inY; width = inWidth; height = inHeight; - } - - public IntRect(JSONObject json) { +public final class RectUtils { + public static Rect create(JSONObject json) { try { - x = json.getInt("x"); - y = json.getInt("y"); - width = json.getInt("width"); - height = json.getInt("height"); + int x = json.getInt("x"); + int y = json.getInt("y"); + int width = json.getInt("width"); + int height = json.getInt("height"); + return new Rect(x, y, x + width, y + height); } catch (JSONException e) { throw new RuntimeException(e); } } - @Override - public Object clone() { return new IntRect(x, y, width, height); } - - @Override - public boolean equals(Object other) { - if (!(other instanceof IntRect)) - return false; - IntRect otherRect = (IntRect)other; - return x == otherRect.x && y == otherRect.y && width == otherRect.width && - height == otherRect.height; - } - - @Override - public String toString() { return "(" + x + "," + y + "," + width + "," + height + ")"; } - - public Point getOrigin() { return new Point(x, y); } - public Point getCenter() { return new Point(x + width / 2, y + height / 2); } - - public int getRight() { return x + width; } - public int getBottom() { return y + height; } - - /** Contracts a rectangle by the given number of units in each direction, from the center. */ - public IntRect contract(int lessWidth, int lessHeight) { - float halfWidth = width / 2.0f - lessWidth, halfHeight = height / 2.0f - lessHeight; - Point center = getCenter(); - return new IntRect((int)Math.round((float)center.x - halfWidth), - (int)Math.round((float)center.y - halfHeight), - (int)Math.round(halfWidth * 2.0f), - (int)Math.round(halfHeight * 2.0f)); + public static Rect contract(Rect rect, int lessWidth, int lessHeight) { + float halfLessWidth = (float)lessWidth / 2.0f; + float halfLessHeight = (float)lessHeight / 2.0f; + return new Rect((int)Math.round((float)rect.left + halfLessWidth), + (int)Math.round((float)rect.top + halfLessHeight), + (int)Math.round((float)rect.right - halfLessWidth), + (int)Math.round((float)rect.bottom - halfLessHeight)); } } - - diff --git a/embedding/android/gfx/TileLayer.java b/embedding/android/gfx/TileLayer.java index 0a706b113eb..5cccb311f34 100644 --- a/embedding/android/gfx/TileLayer.java +++ b/embedding/android/gfx/TileLayer.java @@ -41,6 +41,7 @@ import org.mozilla.gecko.gfx.CairoImage; import org.mozilla.gecko.gfx.IntSize; import org.mozilla.gecko.gfx.Layer; import org.mozilla.gecko.gfx.TextureReaper; +import android.graphics.Rect; import android.util.Log; import javax.microedition.khronos.opengles.GL10; import java.nio.Buffer; @@ -58,8 +59,12 @@ public abstract class TileLayer extends Layer { private IntSize mSize; private int[] mTextureIDs; - private IntRect mTextureUploadRect; - /* The rect that needs to be uploaded to the texture. */ + /* The rect that needs to be uploaded to the texture. + * This field should not be exposed to other classes, since it is + * mutated by calls to union(), and this may lead to odd behavior + * if other classes assume it is immutable. + */ + private Rect mTextureUploadRect; public TileLayer(boolean repeat) { super(); @@ -102,7 +107,7 @@ public abstract class TileLayer extends Layer { gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); } - public void paintSubimage(CairoImage image, IntRect rect) { + public void paintSubimage(CairoImage image, Rect rect) { mImage = image; mTextureUploadRect = rect; @@ -116,7 +121,7 @@ public abstract class TileLayer extends Layer { } public void paintImage(CairoImage image) { - paintSubimage(image, new IntRect(0, 0, image.getWidth(), image.getHeight())); + paintSubimage(image, new Rect(0, 0, image.getWidth(), image.getHeight())); } private void uploadTexture(GL10 gl) { @@ -156,10 +161,10 @@ public abstract class TileLayer extends Layer { */ Buffer viewBuffer = buffer.slice(); int bpp = CairoUtils.bitsPerPixelForCairoFormat(cairoFormat) / 8; - viewBuffer.position(mTextureUploadRect.y * width * bpp); + viewBuffer.position(mTextureUploadRect.top * width * bpp); gl.glTexSubImage2D(gl.GL_TEXTURE_2D, - 0, 0, mTextureUploadRect.y, width, mTextureUploadRect.height, + 0, 0, mTextureUploadRect.top, width, mTextureUploadRect.height(), format, type, viewBuffer); } } finally { diff --git a/embedding/android/ui/PanZoomController.java b/embedding/android/ui/PanZoomController.java index ed007fab415..577e75d86ef 100644 --- a/embedding/android/ui/PanZoomController.java +++ b/embedding/android/ui/PanZoomController.java @@ -39,7 +39,6 @@ package org.mozilla.gecko.ui; import org.json.JSONObject; import org.mozilla.gecko.gfx.FloatRect; -import org.mozilla.gecko.gfx.IntRect; import org.mozilla.gecko.gfx.IntSize; import org.mozilla.gecko.gfx.LayerController; import org.mozilla.gecko.GeckoAppShell; diff --git a/embedding/android/ui/ViewportController.java b/embedding/android/ui/ViewportController.java index 439be1ad54b..9991548fb22 100644 --- a/embedding/android/ui/ViewportController.java +++ b/embedding/android/ui/ViewportController.java @@ -39,7 +39,6 @@ package org.mozilla.gecko.ui; import android.graphics.PointF; import org.mozilla.gecko.gfx.FloatRect; -import org.mozilla.gecko.gfx.IntRect; import org.mozilla.gecko.gfx.IntSize; import org.mozilla.gecko.gfx.LayerController;