Bug 748718 - Fix drawing in SingleTileLayer.java. r=kats a=mfinkle
authorChris Lord <chrislord.net@gmail.com>
Thu, 26 Apr 2012 13:45:06 -0400
changeset 95568 3f863ee66bef5432caaba58101d943ed984c824d
parent 95567 686e78d4493bce6453a6ccb55bffe77914273e1a
child 95569 6929c774dc4c589505fa28fec532bd05ade128be
push id886
push userlsblakk@mozilla.com
push dateMon, 04 Jun 2012 19:57:52 +0000
treeherdermozilla-beta@bbd8d5efd6d1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskats, mfinkle
bugs748718
milestone14.0a2
Bug 748718 - Fix drawing in SingleTileLayer.java. r=kats a=mfinkle SingleTileLayer draws upside-down and doesn't handle non-repeating/stretched layers correctly.
mobile/android/base/gfx/SingleTileLayer.java
--- a/mobile/android/base/gfx/SingleTileLayer.java
+++ b/mobile/android/base/gfx/SingleTileLayer.java
@@ -82,25 +82,34 @@ public class SingleTileLayer extends Til
     @Override
     public void draw(RenderContext context) {
         // mTextureIDs may be null here during startup if Layer.java's draw method
         // failed to acquire the transaction lock and call performUpdates.
         if (!initialized())
             return;
 
         RectF bounds;
-        Rect position = getPosition();
+        RectF textureBounds;
         RectF viewport = context.viewport;
 
-        if (repeats() || stretches()) {
+        if (repeats()) {
+            // If we're repeating, we want to adjust the texture bounds so that
+            // the texture repeats the correct number of times when drawn at
+            // the size of the viewport.
+            bounds = getBounds(context);
+            textureBounds = new RectF(0.0f, 0.0f, bounds.width(), bounds.height());
             bounds = new RectF(0.0f, 0.0f, viewport.width(), viewport.height());
-            int width = Math.round(viewport.width());
-            int height = Math.round(viewport.height());
+        } else if (stretches()) {
+            // If we're stretching, we just want the bounds and texture bounds
+            // to fit to the page.
+            bounds = new RectF(0.0f, 0.0f, context.pageSize.width, context.pageSize.height);
+            textureBounds = bounds;
         } else {
             bounds = getBounds(context);
+            textureBounds = bounds;
         }
 
         Rect intBounds = new Rect();
         bounds.roundOut(intBounds);
         Region maskedBounds = new Region(intBounds);
         if (mMask != null) {
             maskedBounds.op(mMask, Region.Op.DIFFERENCE);
             if (maskedBounds.isEmpty())
@@ -113,38 +122,41 @@ public class SingleTileLayer extends Til
         for (Rect subRect = new Rect(); i.next(subRect);) {
             // Compensate for rounding errors at the edge of the tile caused by
             // the roundOut above
             RectF subRectF = new RectF(Math.max(bounds.left, (float)subRect.left),
                                        Math.max(bounds.top, (float)subRect.top),
                                        Math.min(bounds.right, (float)subRect.right),
                                        Math.min(bounds.bottom, (float)subRect.bottom));
 
+            // This is the left/top/right/bottom of the rect, relative to the
+            // bottom-left of the layer, to use for texture coordinates.
             int[] cropRect = new int[] { Math.round(subRectF.left - bounds.left),
-                                         Math.round(subRectF.bottom - bounds.top),
+                                         Math.round(bounds.bottom - subRectF.top),
                                          Math.round(subRectF.right - bounds.left),
-                                         Math.round(subRectF.top - bounds.top) };
+                                         Math.round(bounds.bottom - subRectF.bottom) };
 
-            float height = subRectF.height();
             float left = subRectF.left - viewport.left;
-            float top = viewport.height() - (subRectF.top + height - viewport.top);
+            float top = viewport.bottom - subRectF.bottom;
+            float right = left + subRectF.width();
+            float bottom = top + subRectF.height();
 
             float[] coords = {
                 //x, y, z, texture_x, texture_y
-                left/viewport.width(), top/viewport.height(), 0,
-                cropRect[0]/(float)position.width(), cropRect[1]/(float)position.height(),
+                left/viewport.width(), bottom/viewport.height(), 0,
+                cropRect[0]/textureBounds.width(), cropRect[1]/textureBounds.height(),
 
-                left/viewport.width(), (top+height)/viewport.height(), 0,
-                cropRect[0]/(float)position.width(), cropRect[3]/(float)position.height(),
+                left/viewport.width(), top/viewport.height(), 0,
+                cropRect[0]/textureBounds.width(), cropRect[3]/textureBounds.height(),
 
-                (left+subRectF.width())/viewport.width(), top/viewport.height(), 0,
-                cropRect[2]/(float)position.width(), cropRect[1]/(float)position.height(),
+                right/viewport.width(), bottom/viewport.height(), 0,
+                cropRect[2]/textureBounds.width(), cropRect[1]/textureBounds.height(),
 
-                (left+subRectF.width())/viewport.width(), (top+height)/viewport.height(), 0,
-                cropRect[2]/(float)position.width(), cropRect[3]/(float)position.height()
+                right/viewport.width(), top/viewport.height(), 0,
+                cropRect[2]/textureBounds.width(), cropRect[3]/textureBounds.height()
             };
 
             FloatBuffer coordBuffer = context.coordBuffer;
             int positionHandle = context.positionHandle;
             int textureHandle = context.textureHandle;
 
             GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, getTextureID());