Home | History | Annotate | Download | only in hwui
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #define LOG_TAG "OpenGLRenderer"
     18 
     19 #include <cmath>
     20 
     21 #include <utils/Log.h>
     22 
     23 #include "Patch.h"
     24 #include "Caches.h"
     25 #include "Properties.h"
     26 
     27 namespace android {
     28 namespace uirenderer {
     29 
     30 ///////////////////////////////////////////////////////////////////////////////
     31 // Constructors/destructor
     32 ///////////////////////////////////////////////////////////////////////////////
     33 
     34 Patch::Patch(const uint32_t xCount, const uint32_t yCount, const int8_t emptyQuads):
     35         mXCount(xCount), mYCount(yCount), mEmptyQuads(emptyQuads) {
     36     // Initialized with the maximum number of vertices we will need
     37     // 2 triangles per patch, 3 vertices per triangle
     38     uint32_t maxVertices = ((xCount + 1) * (yCount + 1) - emptyQuads) * 2 * 3;
     39     mVertices = new TextureVertex[maxVertices];
     40     mAllocatedVerticesCount = 0;
     41 
     42     verticesCount = 0;
     43     hasEmptyQuads = emptyQuads > 0;
     44 
     45     mColorKey = 0;
     46     mXDivs = new int32_t[mXCount];
     47     mYDivs = new int32_t[mYCount];
     48 
     49     PATCH_LOGD("    patch: xCount = %d, yCount = %d, emptyQuads = %d, max vertices = %d",
     50             xCount, yCount, emptyQuads, maxVertices);
     51 
     52     glGenBuffers(1, &meshBuffer);
     53 }
     54 
     55 Patch::~Patch() {
     56     delete[] mVertices;
     57     delete[] mXDivs;
     58     delete[] mYDivs;
     59     glDeleteBuffers(1, &meshBuffer);
     60 }
     61 
     62 ///////////////////////////////////////////////////////////////////////////////
     63 // Patch management
     64 ///////////////////////////////////////////////////////////////////////////////
     65 
     66 void Patch::copy(const int32_t* xDivs, const int32_t* yDivs) {
     67     memcpy(mXDivs, xDivs, mXCount * sizeof(int32_t));
     68     memcpy(mYDivs, yDivs, mYCount * sizeof(int32_t));
     69 }
     70 
     71 void Patch::updateColorKey(const uint32_t colorKey) {
     72     mColorKey = colorKey;
     73 }
     74 
     75 bool Patch::matches(const int32_t* xDivs, const int32_t* yDivs,
     76         const uint32_t colorKey, const int8_t emptyQuads) {
     77 
     78     bool matches = true;
     79 
     80     if (mEmptyQuads != emptyQuads) {
     81         mEmptyQuads = emptyQuads;
     82         hasEmptyQuads = emptyQuads > 0;
     83         matches = false;
     84     }
     85 
     86     if (mColorKey != colorKey) {
     87         updateColorKey(colorKey);
     88         matches = false;
     89     }
     90 
     91     if (memcmp(mXDivs, xDivs, mXCount * sizeof(int32_t))) {
     92         memcpy(mXDivs, xDivs, mXCount * sizeof(int32_t));
     93         matches = false;
     94     }
     95 
     96     if (memcmp(mYDivs, yDivs, mYCount * sizeof(int32_t))) {
     97         memcpy(mYDivs, yDivs, mYCount * sizeof(int32_t));
     98         matches = false;
     99     }
    100 
    101     return matches;
    102 }
    103 
    104 ///////////////////////////////////////////////////////////////////////////////
    105 // Vertices management
    106 ///////////////////////////////////////////////////////////////////////////////
    107 
    108 void Patch::updateVertices(const float bitmapWidth, const float bitmapHeight,
    109         float left, float top, float right, float bottom) {
    110     if (hasEmptyQuads) quads.clear();
    111 
    112     // Reset the vertices count here, we will count exactly how many
    113     // vertices we actually need when generating the quads
    114     verticesCount = 0;
    115 
    116     const uint32_t xStretchCount = (mXCount + 1) >> 1;
    117     const uint32_t yStretchCount = (mYCount + 1) >> 1;
    118 
    119     float stretchX = 0.0f;
    120     float stretchY = 0.0f;
    121 
    122     float rescaleX = 1.0f;
    123     float rescaleY = 1.0f;
    124 
    125     if (xStretchCount > 0) {
    126         uint32_t stretchSize = 0;
    127         for (uint32_t i = 1; i < mXCount; i += 2) {
    128             stretchSize += mXDivs[i] - mXDivs[i - 1];
    129         }
    130         const float xStretchTex = stretchSize;
    131         const float fixed = bitmapWidth - stretchSize;
    132         const float xStretch = fmaxf(right - left - fixed, 0.0f);
    133         stretchX = xStretch / xStretchTex;
    134         rescaleX = fixed == 0.0f ? 0.0f : fminf(fmaxf(right - left, 0.0f) / fixed, 1.0f);
    135     }
    136 
    137     if (yStretchCount > 0) {
    138         uint32_t stretchSize = 0;
    139         for (uint32_t i = 1; i < mYCount; i += 2) {
    140             stretchSize += mYDivs[i] - mYDivs[i - 1];
    141         }
    142         const float yStretchTex = stretchSize;
    143         const float fixed = bitmapHeight - stretchSize;
    144         const float yStretch = fmaxf(bottom - top - fixed, 0.0f);
    145         stretchY = yStretch / yStretchTex;
    146         rescaleY = fixed == 0.0f ? 0.0f : fminf(fmaxf(bottom - top, 0.0f) / fixed, 1.0f);
    147     }
    148 
    149     TextureVertex* vertex = mVertices;
    150     uint32_t quadCount = 0;
    151 
    152     float previousStepY = 0.0f;
    153 
    154     float y1 = 0.0f;
    155     float y2 = 0.0f;
    156     float v1 = 0.0f;
    157 
    158     for (uint32_t i = 0; i < mYCount; i++) {
    159         float stepY = mYDivs[i];
    160         const float segment = stepY - previousStepY;
    161 
    162         if (i & 1) {
    163             y2 = y1 + floorf(segment * stretchY + 0.5f);
    164         } else {
    165             y2 = y1 + segment * rescaleY;
    166         }
    167 
    168         float vOffset = y1 == y2 ? 0.0f : 0.5 - (0.5 * segment / (y2 - y1));
    169         float v2 = fmax(0.0f, stepY - vOffset) / bitmapHeight;
    170         v1 += vOffset / bitmapHeight;
    171 
    172         if (stepY > 0.0f) {
    173 #if DEBUG_EXPLODE_PATCHES
    174             y1 += i * EXPLODE_GAP;
    175             y2 += i * EXPLODE_GAP;
    176 #endif
    177             generateRow(vertex, y1, y2, v1, v2, stretchX, rescaleX, right - left,
    178                     bitmapWidth, quadCount);
    179 #if DEBUG_EXPLODE_PATCHES
    180             y2 -= i * EXPLODE_GAP;
    181 #endif
    182         }
    183 
    184         y1 = y2;
    185         v1 = stepY / bitmapHeight;
    186 
    187         previousStepY = stepY;
    188     }
    189 
    190     if (previousStepY != bitmapHeight) {
    191         y2 = bottom - top;
    192 #if DEBUG_EXPLODE_PATCHES
    193         y1 += mYCount * EXPLODE_GAP;
    194         y2 += mYCount * EXPLODE_GAP;
    195 #endif
    196         generateRow(vertex, y1, y2, v1, 1.0f, stretchX, rescaleX, right - left,
    197                 bitmapWidth, quadCount);
    198     }
    199 
    200     if (verticesCount > 0) {
    201         Caches& caches = Caches::getInstance();
    202         caches.bindMeshBuffer(meshBuffer);
    203         if (mAllocatedVerticesCount < verticesCount) {
    204             glBufferData(GL_ARRAY_BUFFER, sizeof(TextureVertex) * verticesCount,
    205                     mVertices, GL_DYNAMIC_DRAW);
    206             mAllocatedVerticesCount = verticesCount;
    207         } else {
    208             glBufferSubData(GL_ARRAY_BUFFER, 0,
    209                     sizeof(TextureVertex) * verticesCount, mVertices);
    210         }
    211         caches.resetVertexPointers();
    212     }
    213 
    214     PATCH_LOGD("    patch: new vertices count = %d", verticesCount);
    215 }
    216 
    217 void Patch::generateRow(TextureVertex*& vertex, float y1, float y2, float v1, float v2,
    218         float stretchX, float rescaleX, float width, float bitmapWidth, uint32_t& quadCount) {
    219     float previousStepX = 0.0f;
    220 
    221     float x1 = 0.0f;
    222     float x2 = 0.0f;
    223     float u1 = 0.0f;
    224 
    225     // Generate the row quad by quad
    226     for (uint32_t i = 0; i < mXCount; i++) {
    227         float stepX = mXDivs[i];
    228         const float segment = stepX - previousStepX;
    229 
    230         if (i & 1) {
    231             x2 = x1 + floorf(segment * stretchX + 0.5f);
    232         } else {
    233             x2 = x1 + segment * rescaleX;
    234         }
    235 
    236         float uOffset = x1 == x2 ? 0.0f : 0.5 - (0.5 * segment / (x2 - x1));
    237         float u2 = fmax(0.0f, stepX - uOffset) / bitmapWidth;
    238         u1 += uOffset / bitmapWidth;
    239 
    240         if (stepX > 0.0f) {
    241 #if DEBUG_EXPLODE_PATCHES
    242             x1 += i * EXPLODE_GAP;
    243             x2 += i * EXPLODE_GAP;
    244 #endif
    245             generateQuad(vertex, x1, y1, x2, y2, u1, v1, u2, v2, quadCount);
    246 #if DEBUG_EXPLODE_PATCHES
    247             x2 -= i * EXPLODE_GAP;
    248 #endif
    249         }
    250 
    251         x1 = x2;
    252         u1 = stepX / bitmapWidth;
    253 
    254         previousStepX = stepX;
    255     }
    256 
    257     if (previousStepX != bitmapWidth) {
    258         x2 = width;
    259 #if DEBUG_EXPLODE_PATCHES
    260         x1 += mXCount * EXPLODE_GAP;
    261         x2 += mXCount * EXPLODE_GAP;
    262 #endif
    263         generateQuad(vertex, x1, y1, x2, y2, u1, v1, 1.0f, v2, quadCount);
    264     }
    265 }
    266 
    267 void Patch::generateQuad(TextureVertex*& vertex, float x1, float y1, float x2, float y2,
    268             float u1, float v1, float u2, float v2, uint32_t& quadCount) {
    269     const uint32_t oldQuadCount = quadCount;
    270     quadCount++;
    271 
    272     if (x1 < 0.0f) x1 = 0.0f;
    273     if (x2 < 0.0f) x2 = 0.0f;
    274     if (y1 < 0.0f) y1 = 0.0f;
    275     if (y2 < 0.0f) y2 = 0.0f;
    276 
    277     // Skip degenerate and transparent (empty) quads
    278     if (((mColorKey >> oldQuadCount) & 0x1) || x1 >= x2 || y1 >= y2) {
    279 #if DEBUG_PATCHES_EMPTY_VERTICES
    280         PATCH_LOGD("    quad %d (empty)", oldQuadCount);
    281         PATCH_LOGD("        left,  top    = %.2f, %.2f\t\tu1, v1 = %.4f, %.4f", x1, y1, u1, v1);
    282         PATCH_LOGD("        right, bottom = %.2f, %.2f\t\tu2, v2 = %.4f, %.4f", x2, y2, u2, v2);
    283 #endif
    284         return;
    285     }
    286 
    287     // Record all non empty quads
    288     if (hasEmptyQuads) {
    289         Rect bounds(x1, y1, x2, y2);
    290         quads.add(bounds);
    291     }
    292 
    293     // Left triangle
    294     TextureVertex::set(vertex++, x1, y1, u1, v1);
    295     TextureVertex::set(vertex++, x2, y1, u2, v1);
    296     TextureVertex::set(vertex++, x1, y2, u1, v2);
    297 
    298     // Right triangle
    299     TextureVertex::set(vertex++, x1, y2, u1, v2);
    300     TextureVertex::set(vertex++, x2, y1, u2, v1);
    301     TextureVertex::set(vertex++, x2, y2, u2, v2);
    302 
    303     // A quad is made of 2 triangles, 6 vertices
    304     verticesCount += 6;
    305 
    306 #if DEBUG_PATCHES_VERTICES
    307     PATCH_LOGD("    quad %d", oldQuadCount);
    308     PATCH_LOGD("        left,  top    = %.2f, %.2f\t\tu1, v1 = %.4f, %.4f", x1, y1, u1, v1);
    309     PATCH_LOGD("        right, bottom = %.2f, %.2f\t\tu2, v2 = %.4f, %.4f", x2, y2, u2, v2);
    310 #endif
    311 }
    312 
    313 }; // namespace uirenderer
    314 }; // namespace android
    315