diff options
Diffstat (limited to 'libs/hwui/Patch.cpp')
-rw-r--r-- | libs/hwui/Patch.cpp | 179 |
1 files changed, 179 insertions, 0 deletions
diff --git a/libs/hwui/Patch.cpp b/libs/hwui/Patch.cpp new file mode 100644 index 0000000..9b2d476 --- /dev/null +++ b/libs/hwui/Patch.cpp @@ -0,0 +1,179 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define LOG_TAG "OpenGLRenderer" + +#include <cmath> + +#include <utils/Log.h> + +#include "Patch.h" +#include "Caches.h" + +namespace android { +namespace uirenderer { + +/////////////////////////////////////////////////////////////////////////////// +// Constructors/destructor +/////////////////////////////////////////////////////////////////////////////// + +Patch::Patch(const uint32_t xCount, const uint32_t yCount, const int8_t emptyQuads) { + // 2 triangles per patch, 3 vertices per triangle + verticesCount = ((xCount + 1) * (yCount + 1) - emptyQuads) * 2 * 3; + mVertices = new TextureVertex[verticesCount]; + hasEmptyQuads = emptyQuads > 0; + + glGenBuffers(1, &meshBuffer); +} + +Patch::~Patch() { + delete[] mVertices; + glDeleteBuffers(1, &meshBuffer); +} + +/////////////////////////////////////////////////////////////////////////////// +// Vertices management +/////////////////////////////////////////////////////////////////////////////// + +void Patch::updateVertices(const float bitmapWidth, const float bitmapHeight, + float left, float top, float right, float bottom, + const int32_t* xDivs, const int32_t* yDivs, + const uint32_t width, const uint32_t height, const uint32_t colorKey) { + if (hasEmptyQuads) quads.clear(); + + const uint32_t xStretchCount = (width + 1) >> 1; + const uint32_t yStretchCount = (height + 1) >> 1; + + float stretchX = 0.0f; + float stretchY = 0.0; + + const float meshWidth = right - left; + + if (xStretchCount > 0) { + uint32_t stretchSize = 0; + for (uint32_t i = 1; i < width; i += 2) { + stretchSize += xDivs[i] - xDivs[i - 1]; + } + const float xStretchTex = stretchSize; + const float fixed = bitmapWidth - stretchSize; + const float xStretch = right - left - fixed; + stretchX = xStretch / xStretchTex; + } + + if (yStretchCount > 0) { + uint32_t stretchSize = 0; + for (uint32_t i = 1; i < height; i += 2) { + stretchSize += yDivs[i] - yDivs[i - 1]; + } + const float yStretchTex = stretchSize; + const float fixed = bitmapHeight - stretchSize; + const float yStretch = bottom - top - fixed; + stretchY = yStretch / yStretchTex; + } + + TextureVertex* vertex = mVertices; + uint32_t quadCount = 0; + + float previousStepY = 0.0f; + + float y1 = 0.0f; + float v1 = 0.0f; + + for (uint32_t i = 0; i < height; i++) { + float stepY = yDivs[i]; + + float y2 = 0.0f; + if (i & 1) { + const float segment = stepY - previousStepY; + y2 = y1 + segment * stretchY; + } else { + y2 = y1 + stepY - previousStepY; + } + float v2 = fmax(0.0f, stepY - 0.5f) / bitmapHeight; + + generateRow(vertex, y1, y2, v1, v2, xDivs, width, stretchX, + right - left, bitmapWidth, quadCount, colorKey); + + y1 = y2; + v1 = (stepY + 0.5f) / bitmapHeight; + + previousStepY = stepY; + } + + generateRow(vertex, y1, bottom - top, v1, 1.0f, xDivs, width, stretchX, + right - left, bitmapWidth, quadCount, colorKey); + + Caches::getInstance().bindMeshBuffer(meshBuffer); + glBufferData(GL_ARRAY_BUFFER, sizeof(TextureVertex) * verticesCount, + mVertices, GL_STATIC_DRAW); +} + +void Patch::generateRow(TextureVertex*& vertex, float y1, float y2, float v1, float v2, + const int32_t xDivs[], uint32_t xCount, float stretchX, float width, float bitmapWidth, + uint32_t& quadCount, const uint32_t colorKey) { + float previousStepX = 0.0f; + + float x1 = 0.0f; + float u1 = 0.0f; + + // Generate the row quad by quad + for (uint32_t i = 0; i < xCount; i++) { + float stepX = xDivs[i]; + + float x2 = 0.0f; + if (i & 1) { + const float segment = stepX - previousStepX; + x2 = x1 + segment * stretchX; + } else { + x2 = x1 + stepX - previousStepX; + } + float u2 = fmax(0.0f, stepX - 0.5f) / bitmapWidth; + + generateQuad(vertex, x1, y1, x2, y2, u1, v1, u2, v2, quadCount, colorKey); + + x1 = x2; + u1 = (stepX + 0.5f) / bitmapWidth; + + previousStepX = stepX; + } + + generateQuad(vertex, x1, y1, width, y2, u1, v1, 1.0f, v2, quadCount, colorKey); +} + +void Patch::generateQuad(TextureVertex*& vertex, float x1, float y1, float x2, float y2, + float u1, float v1, float u2, float v2, uint32_t& quadCount, const uint32_t colorKey) { + if (((colorKey >> quadCount++) & 0x1) == 1) { + return; + } + + if (hasEmptyQuads) { + Rect bounds(x1, y1, x2, y2); + quads.add(bounds); + } + + // Left triangle + TextureVertex::set(vertex++, x1, y1, u1, v1); + TextureVertex::set(vertex++, x2, y1, u2, v1); + TextureVertex::set(vertex++, x1, y2, u1, v2); + + // Right triangle + TextureVertex::set(vertex++, x1, y2, u1, v2); + TextureVertex::set(vertex++, x2, y1, u2, v1); + TextureVertex::set(vertex++, x2, y2, u2, v2); +} + +}; // namespace uirenderer +}; // namespace android |