Patch* PatchCache::get(const float bitmapWidth, const float bitmapHeight, const float pixelWidth, const float pixelHeight, const int32_t* xDivs, const int32_t* yDivs, const uint32_t* colors, const uint32_t width, const uint32_t height, const int8_t numColors) { uint32_t quadCount = countQuad(xDivs, yDivs, width, height, bitmapWidth, bitmapHeight); int8_t transparentQuads = 0; uint32_t colorKey = 0; if (uint8_t(numColors) < sizeof(uint32_t) * 4) { for (int8_t i = 0; i < numColors; i++) { if (colors[i] == 0x0) { transparentQuads++; colorKey |= 0x1 << i; } } } // If the 9patch is made of only transparent quads if (transparentQuads == int8_t((width + 1) * (height + 1))) { return NULL; } const PatchDescription description(bitmapWidth, bitmapHeight, pixelWidth, pixelHeight, width, height, quadCount, transparentQuads, colorKey); ssize_t index = mCache.indexOfKey(description); Patch* mesh = NULL; if (index >= 0) { mesh = mCache.valueAt(index); } if (!mesh) { PATCH_LOGD("New patch mesh " "xCount=%d yCount=%d, w=%.2f h=%.2f, bw=%.2f bh=%.2f", width, height, pixelWidth, pixelHeight, bitmapWidth, bitmapHeight); mesh = new Patch(width, height, transparentQuads); mesh->updateColorKey(colorKey); mesh->copy(xDivs, yDivs); mesh->updateVertices(bitmapWidth, bitmapHeight, 0.0f, 0.0f, pixelWidth, pixelHeight); if (mCache.size() >= mMaxEntries) { delete mCache.valueAt(mCache.size() - 1); mCache.removeItemsAt(mCache.size() - 1, 1); } mCache.add(description, mesh); } else if (!mesh->matches(xDivs, yDivs, colorKey)) { PATCH_LOGD("Patch mesh does not match, refreshing vertices"); PATCH_LOGD("Old patch mesh " "xCount=%d yCount=%d, w=%.2f h=%.2f, bw=%.2f bh=%.2f", width, height, pixelWidth, pixelHeight, bitmapWidth, bitmapHeight); mesh->updateVertices(bitmapWidth, bitmapHeight, 0.0f, 0.0f, pixelWidth, pixelHeight); } return mesh; }
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 oldQuadCount = quadCount; quadCount++; if (x1 < 0.0f) x1 = 0.0f; if (x2 < 0.0f) x2 = 0.0f; if (y1 < 0.0f) y1 = 0.0f; if (y2 < 0.0f) y2 = 0.0f; // Skip degenerate and transparent (empty) quads if ((mColors[oldQuadCount] == 0) || x1 >= x2 || y1 >= y2) { #if DEBUG_PATCHES_EMPTY_VERTICES PATCH_LOGD(" quad %d (empty)", oldQuadCount); PATCH_LOGD(" left, top = %.2f, %.2f\t\tu1, v1 = %.8f, %.8f", x1, y1, u1, v1); PATCH_LOGD(" right, bottom = %.2f, %.2f\t\tu2, v2 = %.8f, %.8f", x2, y2, u2, v2); #endif return; } // Record all non empty quads if (hasEmptyQuads) { Rect bounds(x1, y1, x2, y2); quads.add(bounds); } mUvMapper.map(u1, v1, u2, v2); TextureVertex::set(vertex++, x1, y1, u1, v1); TextureVertex::set(vertex++, x2, y1, u2, v1); TextureVertex::set(vertex++, x1, y2, u1, v2); TextureVertex::set(vertex++, x2, y2, u2, v2); verticesCount += 4; indexCount += 6; #if DEBUG_PATCHES_VERTICES PATCH_LOGD(" quad %d", oldQuadCount); PATCH_LOGD(" left, top = %.2f, %.2f\t\tu1, v1 = %.8f, %.8f", x1, y1, u1, v1); PATCH_LOGD(" right, bottom = %.2f, %.2f\t\tu2, v2 = %.8f, %.8f", x2, y2, u2, v2); #endif }