From 3b748a44c6bd2ea05fe16839caf73dbe50bd7ae9 Mon Sep 17 00:00:00 2001 From: Romain Guy Date: Wed, 17 Apr 2013 18:54:38 -0700 Subject: Pack preloaded framework assets in a texture atlas When the Android runtime starts, the system preloads a series of assets in the Zygote process. These assets are shared across all processes. Unfortunately, each one of these assets is later uploaded in its own OpenGL texture, once per process. This wastes memory and generates unnecessary OpenGL state changes. This CL introduces an asset server that provides an atlas to all processes. Note: bitmaps used by skia shaders are *not* sampled from the atlas. It's an uncommon use case and would require extra texture transforms in the GL shaders. WHAT IS THE ASSETS ATLAS The "assets atlas" is a single, shareable graphic buffer that contains all the system's preloaded bitmap drawables (this includes 9-patches.) The atlas is made of two distinct objects: the graphic buffer that contains the actual pixels and the map which indicates where each preloaded bitmap can be found in the atlas (essentially a pair of x and y coordinates.) HOW IS THE ASSETS ATLAS GENERATED Because we need to support a wide variety of devices and because it is easy to change the list of preloaded drawables, the atlas is generated at runtime, during the startup phase of the system process. There are several steps that lead to the atlas generation: 1. If the device is booting for the first time, or if the device was updated, we need to find the best atlas configuration. To do so, the atlas service tries a number of width, height and algorithm variations that allows us to pack as many assets as possible while using as little memory as possible. Once a best configuration is found, it gets written to disk in /data/system/framework_atlas 2. Given a best configuration (algorithm variant, dimensions and number of bitmaps that can be packed in the atlas), the atlas service packs all the preloaded bitmaps into a single graphic buffer object. 3. The packing is done using Skia in a temporary native bitmap. The Skia bitmap is then copied into the graphic buffer using OpenGL ES to benefit from texture swizzling. HOW PROCESSES USE THE ATLAS Whenever a process' hardware renderer initializes its EGL context, it queries the atlas service for the graphic buffer and the map. It is important to remember that both the context and the map will be valid for the lifetime of the hardware renderer (if the system process goes down, all apps get killed as well.) Every time the hardware renderer needs to render a bitmap, it first checks whether the bitmap can be found in the assets atlas. When the bitmap is part of the atlas, texture coordinates are remapped appropriately before rendering. Change-Id: I8eaecf53e7f6a33d90da3d0047c5ceec89ea3af0 --- libs/hwui/Program.cpp | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) (limited to 'libs/hwui/Program.cpp') diff --git a/libs/hwui/Program.cpp b/libs/hwui/Program.cpp index 14a23765b301..c127d684be32 100644 --- a/libs/hwui/Program.cpp +++ b/libs/hwui/Program.cpp @@ -15,6 +15,9 @@ */ #define LOG_TAG "OpenGLRenderer" +#define ATRACE_TAG ATRACE_TAG_VIEW + +#include #include "Program.h" @@ -25,7 +28,6 @@ namespace uirenderer { // Base program /////////////////////////////////////////////////////////////////////////////// -// TODO: Program instance should be created from a factory method Program::Program(const ProgramDescription& description, const char* vertex, const char* fragment) { mInitialized = false; mHasColorUniform = false; @@ -50,7 +52,9 @@ Program::Program(const ProgramDescription& description, const char* vertex, cons texCoords = -1; } + ATRACE_BEGIN("linkProgram"); glLinkProgram(mProgramId); + ATRACE_END(); GLint status; glGetProgramiv(mProgramId, GL_LINK_STATUS, &status); @@ -87,6 +91,9 @@ Program::Program(const ProgramDescription& description, const char* vertex, cons Program::~Program() { if (mInitialized) { + // This would ideally happen after linking the program + // but Tegra drivers, especially when perfhud is enabled, + // sometimes crash if we do so glDetachShader(mProgramId, mVertexShader); glDetachShader(mProgramId, mFragmentShader); @@ -132,6 +139,8 @@ int Program::getUniform(const char* name) { } GLuint Program::buildShader(const char* source, GLenum type) { + ATRACE_CALL(); + GLuint shader = glCreateShader(type); glShaderSource(shader, 1, &source, 0); glCompileShader(shader); @@ -153,20 +162,24 @@ GLuint Program::buildShader(const char* source, GLenum type) { void Program::set(const mat4& projectionMatrix, const mat4& modelViewMatrix, const mat4& transformMatrix, bool offset) { - mat4 p(projectionMatrix); - if (offset) { - // offset screenspace xy by an amount that compensates for typical precision - // issues in GPU hardware that tends to paint hor/vert lines in pixels shifted - // up and to the left. - // This offset value is based on an assumption that some hardware may use as - // little as 12.4 precision, so we offset by slightly more than 1/16. - p.translate(.065, .065, 0); + if (projectionMatrix != mProjection) { + if (CC_LIKELY(!offset)) { + glUniformMatrix4fv(projection, 1, GL_FALSE, &projectionMatrix.data[0]); + } else { + mat4 p(projectionMatrix); + // offset screenspace xy by an amount that compensates for typical precision + // issues in GPU hardware that tends to paint hor/vert lines in pixels shifted + // up and to the left. + // This offset value is based on an assumption that some hardware may use as + // little as 12.4 precision, so we offset by slightly more than 1/16. + p.translate(.065, .065, 0); + glUniformMatrix4fv(projection, 1, GL_FALSE, &p.data[0]); + } + mProjection = projectionMatrix; } mat4 t(transformMatrix); t.multiply(modelViewMatrix); - - glUniformMatrix4fv(projection, 1, GL_FALSE, &p.data[0]); glUniformMatrix4fv(transform, 1, GL_FALSE, &t.data[0]); } -- cgit v1.2.3-59-g8ed1b From 4c2547fa9244e78115cde0a259291053108c3dc7 Mon Sep 17 00:00:00 2001 From: Romain Guy Date: Tue, 11 Jun 2013 16:19:24 -0700 Subject: Avoid 9patch cache lookups when possible This optimization saves up to 0.3ms per frame on the Play Store's front page, on a Nexus 4 device. Change-Id: Iaa4ef33c6e3b37e175efd5b9eea9ef59b43f14f3 --- libs/hwui/DisplayListOp.h | 19 ++++++++++++++++--- libs/hwui/LayerRenderer.cpp | 2 +- libs/hwui/Matrix.h | 25 +++++++++++++++++++++---- libs/hwui/OpenGLRenderer.cpp | 19 ++++++++++++------- libs/hwui/OpenGLRenderer.h | 2 +- libs/hwui/PatchCache.cpp | 17 +++++++++++------ libs/hwui/PatchCache.h | 9 ++++++++- libs/hwui/Program.cpp | 2 +- 8 files changed, 71 insertions(+), 24 deletions(-) (limited to 'libs/hwui/Program.cpp') diff --git a/libs/hwui/DisplayListOp.h b/libs/hwui/DisplayListOp.h index 6839b18b1cad..028decdbfbd6 100644 --- a/libs/hwui/DisplayListOp.h +++ b/libs/hwui/DisplayListOp.h @@ -932,13 +932,22 @@ public: DrawPatchOp(SkBitmap* bitmap, Res_png_9patch* patch, float left, float top, float right, float bottom, int alpha, SkXfermode::Mode mode) : DrawBoundedOp(left, top, right, bottom, 0), - mBitmap(bitmap), mPatch(patch), mAlpha(alpha), mMode(mode) { + mBitmap(bitmap), mPatch(patch), mAlpha(alpha), mMode(mode), + mGenerationId(0), mMesh(NULL) { mEntry = Caches::getInstance().assetAtlas.getEntry(bitmap); }; virtual status_t applyDraw(OpenGLRenderer& renderer, Rect& dirty) { - // NOTE: not calling the virtual method, which takes a paint - return renderer.drawPatch(mBitmap, mPatch, mEntry, mLocalBounds.left, mLocalBounds.top, + if (!mMesh || renderer.getCaches().patchCache.getGenerationId() != mGenerationId) { + PatchCache& cache = renderer.getCaches().patchCache; + mMesh = cache.get(mEntry, mBitmap->width(), mBitmap->height(), + mLocalBounds.right - mLocalBounds.left, mLocalBounds.bottom - mLocalBounds.top, + mPatch); + mGenerationId = cache.getGenerationId(); + } + // We're not calling the public variant of drawPatch() here + // This method won't perform the quickReject() since we've already done it at this point + return renderer.drawPatch(mBitmap, mMesh, mEntry, mLocalBounds.left, mLocalBounds.top, mLocalBounds.right, mLocalBounds.bottom, mAlpha, mMode); } @@ -957,8 +966,12 @@ public: private: SkBitmap* mBitmap; Res_png_9patch* mPatch; + int mAlpha; SkXfermode::Mode mMode; + + uint32_t mGenerationId; + const Patch* mMesh; AssetAtlas::Entry* mEntry; }; diff --git a/libs/hwui/LayerRenderer.cpp b/libs/hwui/LayerRenderer.cpp index 79e0b0cee8cb..cfb1e97e235e 100644 --- a/libs/hwui/LayerRenderer.cpp +++ b/libs/hwui/LayerRenderer.cpp @@ -467,7 +467,7 @@ bool LayerRenderer::copyLayer(Layer* layer, SkBitmap* bitmap) { mat4 texTransform(layer->getTexTransform()); mat4 invert; - invert.translate(0.0f, 1.0f, 0.0f); + invert.translate(0.0f, 1.0f); invert.scale(1.0f, -1.0f, 1.0f); layer->getTexTransform().multiply(invert); diff --git a/libs/hwui/Matrix.h b/libs/hwui/Matrix.h index 75e280cf502b..df744bef6aca 100644 --- a/libs/hwui/Matrix.h +++ b/libs/hwui/Matrix.h @@ -128,10 +128,27 @@ public: void multiply(float v); - void translate(float x, float y, float z) { - Matrix4 u; - u.loadTranslate(x, y, z); - multiply(u); + void translate(float x, float y) { + if ((getType() & sGeometryMask) == kTypeTranslate) { + data[kTranslateX] += x; + data[kTranslateY] += y; + } else { + // Doing a translation will only affect the translate bit of the type + // Save the type + uint32_t type = mType; + + Matrix4 u; + u.loadTranslate(x, y, 0.0f); + multiply(u); + + // Restore the type and fix the translate bit + mType = type; + if (data[kTranslateX] != 0.0f || data[kTranslateY] != 0.0f) { + mType |= kTypeTranslate; + } else { + mType &= ~kTypeTranslate; + } + } } void scale(float sx, float sy, float sz) { diff --git a/libs/hwui/OpenGLRenderer.cpp b/libs/hwui/OpenGLRenderer.cpp index f5343b161b44..d95a62c19d39 100644 --- a/libs/hwui/OpenGLRenderer.cpp +++ b/libs/hwui/OpenGLRenderer.cpp @@ -1407,7 +1407,7 @@ void OpenGLRenderer::setFullScreenClip() { /////////////////////////////////////////////////////////////////////////////// void OpenGLRenderer::translate(float dx, float dy) { - currentTransform().translate(dx, dy, 0.0f); + currentTransform().translate(dx, dy); } void OpenGLRenderer::rotate(float degrees) { @@ -2337,20 +2337,25 @@ status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch, SkXfermode::Mode mode; getAlphaAndMode(paint, &alpha, &mode); - return drawPatch(bitmap, patch, mCaches.assetAtlas.getEntry(bitmap), - left, top, right, bottom, alpha, mode); + if (quickReject(left, top, right, bottom)) { + return DrawGlInfo::kStatusDone; + } + + AssetAtlas::Entry* entry = mCaches.assetAtlas.getEntry(bitmap); + const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(), + right - left, bottom - top, patch); + + return drawPatch(bitmap, mesh, entry, left, top, right, bottom, alpha, mode); } -status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch, +status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const Patch* mesh, AssetAtlas::Entry* entry, float left, float top, float right, float bottom, int alpha, SkXfermode::Mode mode) { + if (quickReject(left, top, right, bottom)) { return DrawGlInfo::kStatusDone; } - const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(), - right - left, bottom - top, patch); - if (CC_LIKELY(mesh && mesh->verticesCount > 0)) { mCaches.activeTexture(0); Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap); diff --git a/libs/hwui/OpenGLRenderer.h b/libs/hwui/OpenGLRenderer.h index e9ea2f32cccc..ce4ce42bb8f8 100644 --- a/libs/hwui/OpenGLRenderer.h +++ b/libs/hwui/OpenGLRenderer.h @@ -266,7 +266,7 @@ public: float* vertices, int* colors, SkPaint* paint); virtual status_t drawPatch(SkBitmap* bitmap, Res_png_9patch* patch, float left, float top, float right, float bottom, SkPaint* paint); - status_t drawPatch(SkBitmap* bitmap, Res_png_9patch* patch, AssetAtlas::Entry* entry, + status_t drawPatch(SkBitmap* bitmap, const Patch* mesh, AssetAtlas::Entry* entry, float left, float top, float right, float bottom, int alpha, SkXfermode::Mode mode); virtual status_t drawColor(int color, SkXfermode::Mode mode); virtual status_t drawRect(float left, float top, float right, float bottom, SkPaint* paint); diff --git a/libs/hwui/PatchCache.cpp b/libs/hwui/PatchCache.cpp index c6ed275e3cae..c23e99155a11 100644 --- a/libs/hwui/PatchCache.cpp +++ b/libs/hwui/PatchCache.cpp @@ -30,7 +30,9 @@ namespace uirenderer { // Constructors/destructor /////////////////////////////////////////////////////////////////////////////// -PatchCache::PatchCache(): mCache(LruCache::kUnlimitedCapacity) { +PatchCache::PatchCache(): + mSize(0), mCache(LruCache::kUnlimitedCapacity), + mMeshBuffer(0), mGenerationId(0) { char property[PROPERTY_VALUE_MAX]; if (property_get(PROPERTY_PATCH_CACHE_SIZE, property, NULL) > 0) { INIT_LOGD(" Setting patch cache size to %skB", property); @@ -39,8 +41,6 @@ PatchCache::PatchCache(): mCache(LruCache::kUnlimitedC INIT_LOGD(" Using default patch cache size of %.2fkB", DEFAULT_PATCH_CACHE_SIZE); mMaxSize = KB(DEFAULT_PATCH_CACHE_SIZE); } - mSize = 0; - mMeshBuffer = 0; } PatchCache::~PatchCache() { @@ -58,7 +58,7 @@ void PatchCache::init(Caches& caches) { caches.resetVertexPointers(); if (created) { - glBufferData(GL_ARRAY_BUFFER, mMaxSize, NULL, GL_DYNAMIC_DRAW); + createVertexBuffer(); } } @@ -99,6 +99,12 @@ void PatchCache::clearCache() { mCache.clear(); } +void PatchCache::createVertexBuffer() { + glBufferData(GL_ARRAY_BUFFER, mMaxSize, NULL, GL_DYNAMIC_DRAW); + mSize = 0; + mGenerationId++; +} + const Patch* PatchCache::get(const AssetAtlas::Entry* entry, const uint32_t bitmapWidth, const uint32_t bitmapHeight, const float pixelWidth, const float pixelHeight, const Res_png_9patch* patch) { @@ -127,8 +133,7 @@ const Patch* PatchCache::get(const AssetAtlas::Entry* entry, uint32_t size = newMesh->getSize(); if (mSize + size > mMaxSize) { clearCache(); - glBufferData(GL_ARRAY_BUFFER, mMaxSize, NULL, GL_DYNAMIC_DRAW); - mSize = 0; + createVertexBuffer(); } newMesh->offset = (GLintptr) mSize; diff --git a/libs/hwui/PatchCache.h b/libs/hwui/PatchCache.h index 530dad05d72c..1829b89dbfc0 100644 --- a/libs/hwui/PatchCache.h +++ b/libs/hwui/PatchCache.h @@ -70,8 +70,13 @@ public: return mMeshBuffer; } + uint32_t getGenerationId() const { + return mGenerationId; + } + private: void clearCache(); + void createVertexBuffer(); struct PatchDescription { PatchDescription(): mPatch(NULL), mBitmapWidth(0), mBitmapHeight(0), @@ -122,9 +127,11 @@ private: uint32_t mMaxSize; uint32_t mSize; + LruCache mCache; + GLuint mMeshBuffer; - LruCache mCache; + uint32_t mGenerationId; }; // class PatchCache }; // namespace uirenderer diff --git a/libs/hwui/Program.cpp b/libs/hwui/Program.cpp index c127d684be32..9e4670e62dcd 100644 --- a/libs/hwui/Program.cpp +++ b/libs/hwui/Program.cpp @@ -172,7 +172,7 @@ void Program::set(const mat4& projectionMatrix, const mat4& modelViewMatrix, // up and to the left. // This offset value is based on an assumption that some hardware may use as // little as 12.4 precision, so we offset by slightly more than 1/16. - p.translate(.065, .065, 0); + p.translate(.065, .065); glUniformMatrix4fv(projection, 1, GL_FALSE, &p.data[0]); } mProjection = projectionMatrix; -- cgit v1.2.3-59-g8ed1b From 32f05e343c5ffb17f3235942bcda651bd3b9f1d6 Mon Sep 17 00:00:00 2001 From: Chris Craik Date: Tue, 17 Sep 2013 16:20:29 -0700 Subject: Conservatively estimate geometry bounds bug:10761696 Avoids a case where a rect with top coordinate of (e.g.) 0.51f is assumed to not draw in the first row of pixels, which leads to it not being clipped. Since rounding can cause it to render in this first pixel anyway, we very slightly expand geometry bounds. Now, in ambiguous cases, the geometry bounds are expanded so clipping is more likely to happen. Change-Id: I119b7c7720de07bac1634549724ffb63935567fc --- libs/hwui/OpenGLRenderer.cpp | 9 +-------- libs/hwui/Program.cpp | 3 ++- libs/hwui/Rect.h | 40 +++++++++++++++++++++++++++++++--------- libs/hwui/Vertex.h | 9 +++++++++ 4 files changed, 43 insertions(+), 18 deletions(-) (limited to 'libs/hwui/Program.cpp') diff --git a/libs/hwui/OpenGLRenderer.cpp b/libs/hwui/OpenGLRenderer.cpp index 2066f697dca8..89a82fdc4a84 100644 --- a/libs/hwui/OpenGLRenderer.cpp +++ b/libs/hwui/OpenGLRenderer.cpp @@ -1628,14 +1628,7 @@ bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, fl Rect r(left, top, right, bottom); currentTransform().mapRect(r); - - if (snapOut) { - // snapOut is generally used to account for 1 pixel ramp (in window coordinates) - // outside of the provided rect boundaries in tessellated AA geometry - r.snapOutToPixelBoundaries(); - } else { - r.snapToPixelBoundaries(); - } + r.snapGeometryToPixelBoundaries(snapOut); Rect clipRect(*mSnapshot->clipRect); clipRect.snapToPixelBoundaries(); diff --git a/libs/hwui/Program.cpp b/libs/hwui/Program.cpp index 9e4670e62dcd..7814a01ad929 100644 --- a/libs/hwui/Program.cpp +++ b/libs/hwui/Program.cpp @@ -20,6 +20,7 @@ #include #include "Program.h" +#include "Vertex.h" namespace android { namespace uirenderer { @@ -172,7 +173,7 @@ void Program::set(const mat4& projectionMatrix, const mat4& modelViewMatrix, // up and to the left. // This offset value is based on an assumption that some hardware may use as // little as 12.4 precision, so we offset by slightly more than 1/16. - p.translate(.065, .065); + p.translate(Vertex::gGeometryFudgeFactor, Vertex::gGeometryFudgeFactor); glUniformMatrix4fv(projection, 1, GL_FALSE, &p.data[0]); } mProjection = projectionMatrix; diff --git a/libs/hwui/Rect.h b/libs/hwui/Rect.h index 7605307f6eaf..dabd8d440e17 100644 --- a/libs/hwui/Rect.h +++ b/libs/hwui/Rect.h @@ -21,6 +21,8 @@ #include +#include "Vertex.h" + namespace android { namespace uirenderer { @@ -171,17 +173,37 @@ public: } /** - * Similar to snapToPixelBoundaries, but used for AA geometry with a ramp perimeter. + * Similar to snapToPixelBoundaries, but estimates bounds conservatively to handle GL rounding + * errors. * - * We inset the data by a fudge factor of slightly over 1/16 (similar to when drawing non-AA - * lines) before rounding out so that insignificant amounts of ramp geometry (esp. from rounding - * errors) are ignored. + * This function should be used whenever estimating the damage rect of geometry already mapped + * into layer space. */ - void snapOutToPixelBoundaries() { - left = floorf(left + 0.065f); - top = floorf(top + 0.065f); - right = ceilf(right - 0.065f); - bottom = ceilf(bottom - 0.065f); + void snapGeometryToPixelBoundaries(bool snapOut) { + if (snapOut) { + /* For AA geometry with a ramp perimeter, don't snap by rounding - AA geometry will have + * a 0.5 pixel perimeter not accounted for in its bounds. Instead, snap by + * conservatively rounding out the bounds with floor/ceil. + * + * In order to avoid changing integer bounds with floor/ceil due to rounding errors + * inset the bounds first by the fudge factor. Very small fraction-of-a-pixel errors + * from this inset will only incur similarly small errors in output, due to transparency + * in extreme outside of the geometry. + */ + left = floorf(left + Vertex::gGeometryFudgeFactor); + top = floorf(top + Vertex::gGeometryFudgeFactor); + right = ceilf(right - Vertex::gGeometryFudgeFactor); + bottom = ceilf(bottom - Vertex::gGeometryFudgeFactor); + } else { + /* For other geometry, we do the regular rounding in order to snap, but also outset the + * bounds by a fudge factor. This ensures that ambiguous geometry (e.g. a non-AA Rect + * with top left at (0.5, 0.5)) will err on the side of a larger damage rect. + */ + left = floorf(left + 0.5f - Vertex::gGeometryFudgeFactor); + top = floorf(top + 0.5f - Vertex::gGeometryFudgeFactor); + right = floorf(right + 0.5f + Vertex::gGeometryFudgeFactor); + bottom = floorf(bottom + 0.5f + Vertex::gGeometryFudgeFactor); + } } void snapToPixelBoundaries() { diff --git a/libs/hwui/Vertex.h b/libs/hwui/Vertex.h index c06762f7609a..790d4fcb1c61 100644 --- a/libs/hwui/Vertex.h +++ b/libs/hwui/Vertex.h @@ -26,6 +26,15 @@ namespace uirenderer { * Simple structure to describe a vertex with a position and a texture. */ struct Vertex { + /** + * Fudge-factor used to disambiguate geometry pixel positioning. + * + * Used to offset lines and points to avoid ambiguous intersection with pixel centers (see + * Program::set()), and used to make geometry damage rect calculation conservative (see + * Rect::snapGeometryToPixelBoundaries()) + */ + static const float gGeometryFudgeFactor = 0.0656f; + float position[2]; static inline void set(Vertex* vertex, float x, float y) { -- cgit v1.2.3-59-g8ed1b