michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.gfx; michael@0: michael@0: import android.graphics.RectF; michael@0: import android.opengl.GLES20; michael@0: michael@0: import java.nio.FloatBuffer; michael@0: michael@0: /** michael@0: * Encapsulates the logic needed to draw a nine-patch bitmap using OpenGL ES. michael@0: * michael@0: * For more information on nine-patch bitmaps, see the following document: michael@0: * http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch michael@0: */ michael@0: public class NinePatchTileLayer extends TileLayer { michael@0: private static final int PATCH_SIZE = 16; michael@0: private static final int TEXTURE_SIZE = 64; michael@0: michael@0: public NinePatchTileLayer(CairoImage image) { michael@0: super(image, TileLayer.PaintMode.NORMAL); michael@0: } michael@0: michael@0: @Override michael@0: public void draw(RenderContext context) { michael@0: if (!initialized()) michael@0: return; michael@0: michael@0: GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA); michael@0: GLES20.glEnable(GLES20.GL_BLEND); michael@0: michael@0: GLES20.glActiveTexture(GLES20.GL_TEXTURE0); michael@0: GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, getTextureID()); michael@0: drawPatches(context); michael@0: } michael@0: michael@0: private void drawPatches(RenderContext context) { michael@0: /* michael@0: * We divide the nine-patch bitmap up as follows: michael@0: * michael@0: * +---+---+---+ michael@0: * | 0 | 1 | 2 | michael@0: * +---+---+---+ michael@0: * | 3 | | 4 | michael@0: * +---+---+---+ michael@0: * | 5 | 6 | 7 | michael@0: * +---+---+---+ michael@0: */ michael@0: michael@0: // page is the rect of the "missing" center spot in the picture above michael@0: RectF page = context.pageRect; michael@0: michael@0: drawPatch(context, 0, PATCH_SIZE * 3, /* 0 */ michael@0: page.left - PATCH_SIZE, page.top - PATCH_SIZE, PATCH_SIZE, PATCH_SIZE); michael@0: drawPatch(context, PATCH_SIZE, PATCH_SIZE * 3, /* 1 */ michael@0: page.left, page.top - PATCH_SIZE, page.width(), PATCH_SIZE); michael@0: drawPatch(context, PATCH_SIZE * 2, PATCH_SIZE * 3, /* 2 */ michael@0: page.right, page.top - PATCH_SIZE, PATCH_SIZE, PATCH_SIZE); michael@0: drawPatch(context, 0, PATCH_SIZE * 2, /* 3 */ michael@0: page.left - PATCH_SIZE, page.top, PATCH_SIZE, page.height()); michael@0: drawPatch(context, PATCH_SIZE * 2, PATCH_SIZE * 2, /* 4 */ michael@0: page.right, page.top, PATCH_SIZE, page.height()); michael@0: drawPatch(context, 0, PATCH_SIZE, /* 5 */ michael@0: page.left - PATCH_SIZE, page.bottom, PATCH_SIZE, PATCH_SIZE); michael@0: drawPatch(context, PATCH_SIZE, PATCH_SIZE, /* 6 */ michael@0: page.left, page.bottom, page.width(), PATCH_SIZE); michael@0: drawPatch(context, PATCH_SIZE * 2, PATCH_SIZE, /* 7 */ michael@0: page.right, page.bottom, PATCH_SIZE, PATCH_SIZE); michael@0: } michael@0: michael@0: private void drawPatch(RenderContext context, int textureX, int textureY, michael@0: float tileX, float tileY, float tileWidth, float tileHeight) { michael@0: RectF viewport = context.viewport; michael@0: float viewportHeight = viewport.height(); michael@0: float drawX = tileX - viewport.left - context.offset.x; michael@0: float drawY = viewportHeight - (tileY + tileHeight - viewport.top) - context.offset.y; michael@0: michael@0: float[] coords = { michael@0: //x, y, z, texture_x, texture_y michael@0: drawX/viewport.width(), drawY/viewport.height(), 0, michael@0: textureX/(float)TEXTURE_SIZE, textureY/(float)TEXTURE_SIZE, michael@0: michael@0: drawX/viewport.width(), (drawY+tileHeight)/viewport.height(), 0, michael@0: textureX/(float)TEXTURE_SIZE, (textureY+PATCH_SIZE)/(float)TEXTURE_SIZE, michael@0: michael@0: (drawX+tileWidth)/viewport.width(), drawY/viewport.height(), 0, michael@0: (textureX+PATCH_SIZE)/(float)TEXTURE_SIZE, textureY/(float)TEXTURE_SIZE, michael@0: michael@0: (drawX+tileWidth)/viewport.width(), (drawY+tileHeight)/viewport.height(), 0, michael@0: (textureX+PATCH_SIZE)/(float)TEXTURE_SIZE, (textureY+PATCH_SIZE)/(float)TEXTURE_SIZE michael@0: michael@0: }; michael@0: michael@0: // Get the buffer and handles from the context michael@0: FloatBuffer coordBuffer = context.coordBuffer; michael@0: int positionHandle = context.positionHandle; michael@0: int textureHandle = context.textureHandle; michael@0: michael@0: // Make sure we are at position zero in the buffer in case other draw methods did not clean michael@0: // up after themselves michael@0: coordBuffer.position(0); michael@0: coordBuffer.put(coords); michael@0: michael@0: // Unbind any the current array buffer so we can use client side buffers michael@0: GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0); michael@0: michael@0: // Vertex coordinates are x,y,z starting at position 0 into the buffer. michael@0: coordBuffer.position(0); michael@0: GLES20.glVertexAttribPointer(positionHandle, 3, GLES20.GL_FLOAT, false, 20, coordBuffer); michael@0: michael@0: // Texture coordinates are texture_x, texture_y starting at position 3 into the buffer. michael@0: coordBuffer.position(3); michael@0: GLES20.glVertexAttribPointer(textureHandle, 2, GLES20.GL_FLOAT, false, 20, coordBuffer); michael@0: michael@0: GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, michael@0: GLES20.GL_CLAMP_TO_EDGE); michael@0: GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, michael@0: GLES20.GL_CLAMP_TO_EDGE); michael@0: michael@0: // Use bilinear filtering for both magnification and minimization of the texture. This michael@0: // applies only to the shadow layer so we do not incur a high overhead. michael@0: GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, michael@0: GLES20.GL_LINEAR); michael@0: GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, michael@0: GLES20.GL_LINEAR); michael@0: michael@0: GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); michael@0: } michael@0: }