LayerRenderer.cpp revision 40667676e542a9daeafeac9904c30004e8706fd3
16c319ca1275c8db892c39b48fc54864c949f9171Romain Guy/*
26c319ca1275c8db892c39b48fc54864c949f9171Romain Guy * Copyright (C) 2011 The Android Open Source Project
36c319ca1275c8db892c39b48fc54864c949f9171Romain Guy *
46c319ca1275c8db892c39b48fc54864c949f9171Romain Guy * Licensed under the Apache License, Version 2.0 (the "License");
56c319ca1275c8db892c39b48fc54864c949f9171Romain Guy * you may not use this file except in compliance with the License.
66c319ca1275c8db892c39b48fc54864c949f9171Romain Guy * You may obtain a copy of the License at
76c319ca1275c8db892c39b48fc54864c949f9171Romain Guy *
86c319ca1275c8db892c39b48fc54864c949f9171Romain Guy *      http://www.apache.org/licenses/LICENSE-2.0
96c319ca1275c8db892c39b48fc54864c949f9171Romain Guy *
106c319ca1275c8db892c39b48fc54864c949f9171Romain Guy * Unless required by applicable law or agreed to in writing, software
116c319ca1275c8db892c39b48fc54864c949f9171Romain Guy * distributed under the License is distributed on an "AS IS" BASIS,
126c319ca1275c8db892c39b48fc54864c949f9171Romain Guy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
136c319ca1275c8db892c39b48fc54864c949f9171Romain Guy * See the License for the specific language governing permissions and
146c319ca1275c8db892c39b48fc54864c949f9171Romain Guy * limitations under the License.
156c319ca1275c8db892c39b48fc54864c949f9171Romain Guy */
166c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
176c319ca1275c8db892c39b48fc54864c949f9171Romain Guy#define LOG_TAG "OpenGLRenderer"
186c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
193a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy#include <ui/Rect.h>
203a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy
2109b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy#include "LayerCache.h"
226c319ca1275c8db892c39b48fc54864c949f9171Romain Guy#include "LayerRenderer.h"
231fc883b271707c4206ae20cc9a935d7bd4a7485eRomain Guy#include "Properties.h"
243a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy#include "Rect.h"
256c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
266c319ca1275c8db892c39b48fc54864c949f9171Romain Guynamespace android {
276c319ca1275c8db892c39b48fc54864c949f9171Romain Guynamespace uirenderer {
286c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
296c319ca1275c8db892c39b48fc54864c949f9171Romain Guy///////////////////////////////////////////////////////////////////////////////
306c319ca1275c8db892c39b48fc54864c949f9171Romain Guy// Rendering
316c319ca1275c8db892c39b48fc54864c949f9171Romain Guy///////////////////////////////////////////////////////////////////////////////
326c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
337d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guyvoid LayerRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) {
34ada830f639591b99c3e40de22b07296c7932a33fRomain Guy    LAYER_RENDERER_LOGD("Rendering into layer, fbo = %d", mLayer->fbo);
351fc883b271707c4206ae20cc9a935d7bd4a7485eRomain Guy
3662687ec12cb8e0b1d4044a235b1387b9a8c3b4b4Romain Guy    glBindFramebuffer(GL_FRAMEBUFFER, mLayer->fbo);
3762687ec12cb8e0b1d4044a235b1387b9a8c3b4b4Romain Guy
3809b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy    const float width = mLayer->layer.getWidth();
3909b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy    const float height = mLayer->layer.getHeight();
4009b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy
41c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy#if RENDER_LAYERS_AS_REGIONS
423a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy    Rect dirty(left, top, right, bottom);
433a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy    if (dirty.isEmpty() || (dirty.left <= 0 && dirty.top <= 0 &&
4409b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy            dirty.right >= width && dirty.bottom >= height)) {
453a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy        mLayer->region.clear();
4609b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy        dirty.set(0.0f, 0.0f, width, height);
473a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy    } else {
4809b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy        dirty.intersect(0.0f, 0.0f, width, height);
493a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy        android::Rect r(dirty.left, dirty.top, dirty.right, dirty.bottom);
503a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy        mLayer->region.subtractSelf(r);
513a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy    }
52c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy
533a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy    OpenGLRenderer::prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, opaque);
543a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy#else
5509b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy    OpenGLRenderer::prepareDirty(0.0f, 0.0f, width, height, opaque);
563a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy#endif
576c319ca1275c8db892c39b48fc54864c949f9171Romain Guy}
586c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
596c319ca1275c8db892c39b48fc54864c949f9171Romain Guyvoid LayerRenderer::finish() {
606c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    OpenGLRenderer::finish();
611fc883b271707c4206ae20cc9a935d7bd4a7485eRomain Guy
62f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy    generateMesh();
63f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy
64678e0adbdbac5fa3f25ca8371fdff1b3182f67e9Chet Haase    LAYER_RENDERER_LOGD("Finished rendering into layer, fbo = %d", mLayer->fbo);
6542f3a4b7c35dd1ea1922356eb3611af8cde05179Romain Guy
6642f3a4b7c35dd1ea1922356eb3611af8cde05179Romain Guy    // No need to unbind our FBO, this will be taken care of by the caller
6742f3a4b7c35dd1ea1922356eb3611af8cde05179Romain Guy    // who will invoke OpenGLRenderer::resume()
6842f3a4b7c35dd1ea1922356eb3611af8cde05179Romain Guy}
6942f3a4b7c35dd1ea1922356eb3611af8cde05179Romain Guy
7042f3a4b7c35dd1ea1922356eb3611af8cde05179Romain GuyGLint LayerRenderer::getTargetFbo() {
7142f3a4b7c35dd1ea1922356eb3611af8cde05179Romain Guy    return mLayer->fbo;
726c319ca1275c8db892c39b48fc54864c949f9171Romain Guy}
736c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
746c319ca1275c8db892c39b48fc54864c949f9171Romain Guy///////////////////////////////////////////////////////////////////////////////
75f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy// Dirty region tracking
76f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy///////////////////////////////////////////////////////////////////////////////
77f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy
78f219da5e32e85deb442468ee9a63bb28eb198557Romain Guybool LayerRenderer::hasLayer() {
79f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy    return true;
80f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy}
81f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy
82f219da5e32e85deb442468ee9a63bb28eb198557Romain GuyRegion* LayerRenderer::getRegion() {
83f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy#if RENDER_LAYERS_AS_REGIONS
84f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy    if (getSnapshot()->flags & Snapshot::kFlagFboTarget) {
85f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy        return OpenGLRenderer::getRegion();
86f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy    }
87f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy    return &mLayer->region;
88f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy#else
89f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy    return OpenGLRenderer::getRegion();
90f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy#endif
91f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy}
92f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy
93f219da5e32e85deb442468ee9a63bb28eb198557Romain Guyvoid LayerRenderer::generateMesh() {
94f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy#if RENDER_LAYERS_AS_REGIONS
95f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy    if (mLayer->region.isRect() || mLayer->region.isEmpty()) {
96f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy        if (mLayer->mesh) {
97f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy            delete mLayer->mesh;
98f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy            delete mLayer->meshIndices;
99f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy
100f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy            mLayer->mesh = NULL;
101f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy            mLayer->meshIndices = NULL;
102f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy            mLayer->meshElementCount = 0;
103f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy        }
10440667676e542a9daeafeac9904c30004e8706fd3Romain Guy
10540667676e542a9daeafeac9904c30004e8706fd3Romain Guy        const android::Rect& bounds = mLayer->region.getBounds();
10640667676e542a9daeafeac9904c30004e8706fd3Romain Guy        mLayer->regionRect.set(bounds.leftTop().x, bounds.leftTop().y,
10740667676e542a9daeafeac9904c30004e8706fd3Romain Guy                bounds.rightBottom().x, bounds.rightBottom().y);
10840667676e542a9daeafeac9904c30004e8706fd3Romain Guy
10940667676e542a9daeafeac9904c30004e8706fd3Romain Guy        const float texX = 1.0f / float(mLayer->width);
11040667676e542a9daeafeac9904c30004e8706fd3Romain Guy        const float texY = 1.0f / float(mLayer->height);
11140667676e542a9daeafeac9904c30004e8706fd3Romain Guy        const float height = mLayer->layer.getHeight();
11240667676e542a9daeafeac9904c30004e8706fd3Romain Guy        mLayer->texCoords.set(
11340667676e542a9daeafeac9904c30004e8706fd3Romain Guy                mLayer->regionRect.left * texX,
11440667676e542a9daeafeac9904c30004e8706fd3Romain Guy                (height - mLayer->regionRect.top) * texY,
11540667676e542a9daeafeac9904c30004e8706fd3Romain Guy                mLayer->regionRect.right * texX,
11640667676e542a9daeafeac9904c30004e8706fd3Romain Guy                (height - mLayer->regionRect.bottom) * texY);
11740667676e542a9daeafeac9904c30004e8706fd3Romain Guy
118f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy        return;
119f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy    }
120f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy
121f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy    size_t count;
122f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy    const android::Rect* rects = mLayer->region.getArray(&count);
123f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy
124f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy    GLsizei elementCount = count * 6;
125f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy
126f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy    if (mLayer->mesh && mLayer->meshElementCount < elementCount) {
127f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy        delete mLayer->mesh;
128f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy        delete mLayer->meshIndices;
129f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy
130f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy        mLayer->mesh = NULL;
131f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy        mLayer->meshIndices = NULL;
132f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy    }
133f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy
1344f09f5417b5c14fd31336dacfff86972e7885a03Romain Guy    bool rebuildIndices = false;
135f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy    if (!mLayer->mesh) {
136f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy        mLayer->mesh = new TextureVertex[count * 4];
137f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy        mLayer->meshIndices = new uint16_t[elementCount];
1384f09f5417b5c14fd31336dacfff86972e7885a03Romain Guy        rebuildIndices = true;
139f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy    }
1404f09f5417b5c14fd31336dacfff86972e7885a03Romain Guy    mLayer->meshElementCount = elementCount;
141f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy
142f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy    const float texX = 1.0f / float(mLayer->width);
143f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy    const float texY = 1.0f / float(mLayer->height);
144f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy    const float height = mLayer->layer.getHeight();
145f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy
146f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy    TextureVertex* mesh = mLayer->mesh;
147f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy    uint16_t* indices = mLayer->meshIndices;
148f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy
149f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy    for (size_t i = 0; i < count; i++) {
150f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy        const android::Rect* r = &rects[i];
151f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy
152f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy        const float u1 = r->left * texX;
153f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy        const float v1 = (height - r->top) * texY;
154f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy        const float u2 = r->right * texX;
155f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy        const float v2 = (height - r->bottom) * texY;
156f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy
157f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy        TextureVertex::set(mesh++, r->left, r->top, u1, v1);
158f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy        TextureVertex::set(mesh++, r->right, r->top, u2, v1);
159f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy        TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
160f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy        TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
161f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy
1624f09f5417b5c14fd31336dacfff86972e7885a03Romain Guy        if (rebuildIndices) {
1634f09f5417b5c14fd31336dacfff86972e7885a03Romain Guy            uint16_t quad = i * 4;
1644f09f5417b5c14fd31336dacfff86972e7885a03Romain Guy            int index = i * 6;
1654f09f5417b5c14fd31336dacfff86972e7885a03Romain Guy            indices[index    ] = quad;       // top-left
1664f09f5417b5c14fd31336dacfff86972e7885a03Romain Guy            indices[index + 1] = quad + 1;   // top-right
1674f09f5417b5c14fd31336dacfff86972e7885a03Romain Guy            indices[index + 2] = quad + 2;   // bottom-left
1684f09f5417b5c14fd31336dacfff86972e7885a03Romain Guy            indices[index + 3] = quad + 2;   // bottom-left
1694f09f5417b5c14fd31336dacfff86972e7885a03Romain Guy            indices[index + 4] = quad + 1;   // top-right
1704f09f5417b5c14fd31336dacfff86972e7885a03Romain Guy            indices[index + 5] = quad + 3;   // bottom-right
1714f09f5417b5c14fd31336dacfff86972e7885a03Romain Guy        }
172f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy    }
173f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy#endif
174f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy}
175f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy
176f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy///////////////////////////////////////////////////////////////////////////////
177f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy// Layers management
1786c319ca1275c8db892c39b48fc54864c949f9171Romain Guy///////////////////////////////////////////////////////////////////////////////
1796c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
180ada830f639591b99c3e40de22b07296c7932a33fRomain GuyLayer* LayerRenderer::createLayer(uint32_t width, uint32_t height, bool isOpaque) {
1811fc883b271707c4206ae20cc9a935d7bd4a7485eRomain Guy    LAYER_RENDERER_LOGD("Creating new layer %dx%d", width, height);
1821fc883b271707c4206ae20cc9a935d7bd4a7485eRomain Guy
18309b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy    GLuint fbo = Caches::getInstance().fboCache.get();
18409b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy    if (!fbo) {
18509b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy        LOGW("Could not obtain an FBO");
18609b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy        return NULL;
1871fc883b271707c4206ae20cc9a935d7bd4a7485eRomain Guy    }
1881fc883b271707c4206ae20cc9a935d7bd4a7485eRomain Guy
1896c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    glActiveTexture(GL_TEXTURE0);
19009b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy    Layer* layer = Caches::getInstance().layerCache.get(width, height);
19109b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy    if (!layer) {
19209b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy        LOGW("Could not obtain a layer");
19309b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy        return NULL;
19409b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy    }
1956c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
19609b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy    layer->fbo = fbo;
19709b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy    layer->layer.set(0.0f, 0.0f, width, height);
19809b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy    layer->texCoords.set(0.0f, height / float(layer->height),
19909b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy            width / float(layer->width), 0.0f);
20009b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy    layer->alpha = 255;
20109b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy    layer->mode = SkXfermode::kSrcOver_Mode;
20209b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy    layer->blend = !isOpaque;
20309b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy    layer->colorFilter = NULL;
20409b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy    layer->region.clear();
2056c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
20609b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy    GLuint previousFbo;
20709b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy    glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*) &previousFbo);
2086c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
20909b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy    glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
21009b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy    glBindTexture(GL_TEXTURE_2D, layer->texture);
2116c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
21209b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy    // Initialize the texture if needed
21309b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy    if (layer->empty) {
21409b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy        layer->empty = false;
21509b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->width, layer->height, 0,
21609b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy                GL_RGBA, GL_UNSIGNED_BYTE, NULL);
2176c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
21809b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy        if (glGetError() != GL_NO_ERROR) {
21909b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy            LOGD("Could not allocate texture");
22009b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy            glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
22109b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy            glDeleteTextures(1, &layer->texture);
22209b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy            Caches::getInstance().fboCache.put(fbo);
22309b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy            delete layer;
22409b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy            return NULL;
22509b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy        }
2266c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    }
2276c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
2286c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
229ada830f639591b99c3e40de22b07296c7932a33fRomain Guy            layer->texture, 0);
2306c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
23140a787f89a6f4c20f2c7fce29e5bc53954305f34Romain Guy    glDisable(GL_SCISSOR_TEST);
23240a787f89a6f4c20f2c7fce29e5bc53954305f34Romain Guy    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
23340a787f89a6f4c20f2c7fce29e5bc53954305f34Romain Guy    glClear(GL_COLOR_BUFFER_BIT);
23440a787f89a6f4c20f2c7fce29e5bc53954305f34Romain Guy    glEnable(GL_SCISSOR_TEST);
23540a787f89a6f4c20f2c7fce29e5bc53954305f34Romain Guy
2366c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
2376c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
238ada830f639591b99c3e40de22b07296c7932a33fRomain Guy    return layer;
2396c319ca1275c8db892c39b48fc54864c949f9171Romain Guy}
2406c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
241ada830f639591b99c3e40de22b07296c7932a33fRomain Guybool LayerRenderer::resizeLayer(Layer* layer, uint32_t width, uint32_t height) {
242ada830f639591b99c3e40de22b07296c7932a33fRomain Guy    if (layer) {
243ada830f639591b99c3e40de22b07296c7932a33fRomain Guy        LAYER_RENDERER_LOGD("Resizing layer fbo = %d to %dx%d", layer->fbo, width, height);
2441fc883b271707c4206ae20cc9a935d7bd4a7485eRomain Guy
24509b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy        if (Caches::getInstance().layerCache.resize(layer, width, height)) {
24609b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy            layer->layer.set(0.0f, 0.0f, width, height);
24709b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy            layer->texCoords.set(0.0f, height / float(layer->height),
24809b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy                    width / float(layer->width), 0.0f);
24909b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy        } else {
25009b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy            if (layer->texture) glDeleteTextures(1, &layer->texture);
25109b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy            delete layer;
252ada830f639591b99c3e40de22b07296c7932a33fRomain Guy            return false;
253ada830f639591b99c3e40de22b07296c7932a33fRomain Guy        }
254ada830f639591b99c3e40de22b07296c7932a33fRomain Guy    }
25509b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy
256ada830f639591b99c3e40de22b07296c7932a33fRomain Guy    return true;
2576c319ca1275c8db892c39b48fc54864c949f9171Romain Guy}
2586c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
259ada830f639591b99c3e40de22b07296c7932a33fRomain Guyvoid LayerRenderer::destroyLayer(Layer* layer) {
260ada830f639591b99c3e40de22b07296c7932a33fRomain Guy    if (layer) {
261ada830f639591b99c3e40de22b07296c7932a33fRomain Guy        LAYER_RENDERER_LOGD("Destroying layer, fbo = %d", layer->fbo);
262ada830f639591b99c3e40de22b07296c7932a33fRomain Guy
26309b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy        if (layer->fbo) {
26409b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy            Caches::getInstance().fboCache.put(layer->fbo);
26509b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy        }
2661fc883b271707c4206ae20cc9a935d7bd4a7485eRomain Guy
26709b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy        if (!Caches::getInstance().layerCache.put(layer)) {
26809b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy            if (layer->texture) glDeleteTextures(1, &layer->texture);
26909b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy            delete layer;
27009b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy        } else {
27109b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy            layer->region.clear();
27209b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy        }
273ada830f639591b99c3e40de22b07296c7932a33fRomain Guy    }
2746c319ca1275c8db892c39b48fc54864c949f9171Romain Guy}
2756c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
276ada830f639591b99c3e40de22b07296c7932a33fRomain Guyvoid LayerRenderer::destroyLayerDeferred(Layer* layer) {
277ada830f639591b99c3e40de22b07296c7932a33fRomain Guy    if (layer) {
278ada830f639591b99c3e40de22b07296c7932a33fRomain Guy        LAYER_RENDERER_LOGD("Deferring layer destruction, fbo = %d", layer->fbo);
2791fc883b271707c4206ae20cc9a935d7bd4a7485eRomain Guy
280ada830f639591b99c3e40de22b07296c7932a33fRomain Guy        Caches::getInstance().deleteLayerDeferred(layer);
281ada830f639591b99c3e40de22b07296c7932a33fRomain Guy    }
28257066eb64c9a190d1afc87bb060bbb2d31e5b86cRomain Guy}
28357066eb64c9a190d1afc87bb060bbb2d31e5b86cRomain Guy
2846c319ca1275c8db892c39b48fc54864c949f9171Romain Guy}; // namespace uirenderer
2856c319ca1275c8db892c39b48fc54864c949f9171Romain Guy}; // namespace android
286