mobile/android/base/gfx/NinePatchTileLayer.java

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
michael@0 2 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 package org.mozilla.gecko.gfx;
michael@0 7
michael@0 8 import android.graphics.RectF;
michael@0 9 import android.opengl.GLES20;
michael@0 10
michael@0 11 import java.nio.FloatBuffer;
michael@0 12
michael@0 13 /**
michael@0 14 * Encapsulates the logic needed to draw a nine-patch bitmap using OpenGL ES.
michael@0 15 *
michael@0 16 * For more information on nine-patch bitmaps, see the following document:
michael@0 17 * http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch
michael@0 18 */
michael@0 19 public class NinePatchTileLayer extends TileLayer {
michael@0 20 private static final int PATCH_SIZE = 16;
michael@0 21 private static final int TEXTURE_SIZE = 64;
michael@0 22
michael@0 23 public NinePatchTileLayer(CairoImage image) {
michael@0 24 super(image, TileLayer.PaintMode.NORMAL);
michael@0 25 }
michael@0 26
michael@0 27 @Override
michael@0 28 public void draw(RenderContext context) {
michael@0 29 if (!initialized())
michael@0 30 return;
michael@0 31
michael@0 32 GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
michael@0 33 GLES20.glEnable(GLES20.GL_BLEND);
michael@0 34
michael@0 35 GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
michael@0 36 GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, getTextureID());
michael@0 37 drawPatches(context);
michael@0 38 }
michael@0 39
michael@0 40 private void drawPatches(RenderContext context) {
michael@0 41 /*
michael@0 42 * We divide the nine-patch bitmap up as follows:
michael@0 43 *
michael@0 44 * +---+---+---+
michael@0 45 * | 0 | 1 | 2 |
michael@0 46 * +---+---+---+
michael@0 47 * | 3 | | 4 |
michael@0 48 * +---+---+---+
michael@0 49 * | 5 | 6 | 7 |
michael@0 50 * +---+---+---+
michael@0 51 */
michael@0 52
michael@0 53 // page is the rect of the "missing" center spot in the picture above
michael@0 54 RectF page = context.pageRect;
michael@0 55
michael@0 56 drawPatch(context, 0, PATCH_SIZE * 3, /* 0 */
michael@0 57 page.left - PATCH_SIZE, page.top - PATCH_SIZE, PATCH_SIZE, PATCH_SIZE);
michael@0 58 drawPatch(context, PATCH_SIZE, PATCH_SIZE * 3, /* 1 */
michael@0 59 page.left, page.top - PATCH_SIZE, page.width(), PATCH_SIZE);
michael@0 60 drawPatch(context, PATCH_SIZE * 2, PATCH_SIZE * 3, /* 2 */
michael@0 61 page.right, page.top - PATCH_SIZE, PATCH_SIZE, PATCH_SIZE);
michael@0 62 drawPatch(context, 0, PATCH_SIZE * 2, /* 3 */
michael@0 63 page.left - PATCH_SIZE, page.top, PATCH_SIZE, page.height());
michael@0 64 drawPatch(context, PATCH_SIZE * 2, PATCH_SIZE * 2, /* 4 */
michael@0 65 page.right, page.top, PATCH_SIZE, page.height());
michael@0 66 drawPatch(context, 0, PATCH_SIZE, /* 5 */
michael@0 67 page.left - PATCH_SIZE, page.bottom, PATCH_SIZE, PATCH_SIZE);
michael@0 68 drawPatch(context, PATCH_SIZE, PATCH_SIZE, /* 6 */
michael@0 69 page.left, page.bottom, page.width(), PATCH_SIZE);
michael@0 70 drawPatch(context, PATCH_SIZE * 2, PATCH_SIZE, /* 7 */
michael@0 71 page.right, page.bottom, PATCH_SIZE, PATCH_SIZE);
michael@0 72 }
michael@0 73
michael@0 74 private void drawPatch(RenderContext context, int textureX, int textureY,
michael@0 75 float tileX, float tileY, float tileWidth, float tileHeight) {
michael@0 76 RectF viewport = context.viewport;
michael@0 77 float viewportHeight = viewport.height();
michael@0 78 float drawX = tileX - viewport.left - context.offset.x;
michael@0 79 float drawY = viewportHeight - (tileY + tileHeight - viewport.top) - context.offset.y;
michael@0 80
michael@0 81 float[] coords = {
michael@0 82 //x, y, z, texture_x, texture_y
michael@0 83 drawX/viewport.width(), drawY/viewport.height(), 0,
michael@0 84 textureX/(float)TEXTURE_SIZE, textureY/(float)TEXTURE_SIZE,
michael@0 85
michael@0 86 drawX/viewport.width(), (drawY+tileHeight)/viewport.height(), 0,
michael@0 87 textureX/(float)TEXTURE_SIZE, (textureY+PATCH_SIZE)/(float)TEXTURE_SIZE,
michael@0 88
michael@0 89 (drawX+tileWidth)/viewport.width(), drawY/viewport.height(), 0,
michael@0 90 (textureX+PATCH_SIZE)/(float)TEXTURE_SIZE, textureY/(float)TEXTURE_SIZE,
michael@0 91
michael@0 92 (drawX+tileWidth)/viewport.width(), (drawY+tileHeight)/viewport.height(), 0,
michael@0 93 (textureX+PATCH_SIZE)/(float)TEXTURE_SIZE, (textureY+PATCH_SIZE)/(float)TEXTURE_SIZE
michael@0 94
michael@0 95 };
michael@0 96
michael@0 97 // Get the buffer and handles from the context
michael@0 98 FloatBuffer coordBuffer = context.coordBuffer;
michael@0 99 int positionHandle = context.positionHandle;
michael@0 100 int textureHandle = context.textureHandle;
michael@0 101
michael@0 102 // Make sure we are at position zero in the buffer in case other draw methods did not clean
michael@0 103 // up after themselves
michael@0 104 coordBuffer.position(0);
michael@0 105 coordBuffer.put(coords);
michael@0 106
michael@0 107 // Unbind any the current array buffer so we can use client side buffers
michael@0 108 GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
michael@0 109
michael@0 110 // Vertex coordinates are x,y,z starting at position 0 into the buffer.
michael@0 111 coordBuffer.position(0);
michael@0 112 GLES20.glVertexAttribPointer(positionHandle, 3, GLES20.GL_FLOAT, false, 20, coordBuffer);
michael@0 113
michael@0 114 // Texture coordinates are texture_x, texture_y starting at position 3 into the buffer.
michael@0 115 coordBuffer.position(3);
michael@0 116 GLES20.glVertexAttribPointer(textureHandle, 2, GLES20.GL_FLOAT, false, 20, coordBuffer);
michael@0 117
michael@0 118 GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S,
michael@0 119 GLES20.GL_CLAMP_TO_EDGE);
michael@0 120 GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T,
michael@0 121 GLES20.GL_CLAMP_TO_EDGE);
michael@0 122
michael@0 123 // Use bilinear filtering for both magnification and minimization of the texture. This
michael@0 124 // applies only to the shadow layer so we do not incur a high overhead.
michael@0 125 GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER,
michael@0 126 GLES20.GL_LINEAR);
michael@0 127 GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER,
michael@0 128 GLES20.GL_LINEAR);
michael@0 129
michael@0 130 GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
michael@0 131 }
michael@0 132 }

mercurial