LayerRenderer.cpp revision 40667676e542a9daeafeac9904c30004e8706fd3
1/*
2 * Copyright (C) 2011 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 <ui/Rect.h>
20
21#include "LayerCache.h"
22#include "LayerRenderer.h"
23#include "Properties.h"
24#include "Rect.h"
25
26namespace android {
27namespace uirenderer {
28
29///////////////////////////////////////////////////////////////////////////////
30// Rendering
31///////////////////////////////////////////////////////////////////////////////
32
33void LayerRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) {
34    LAYER_RENDERER_LOGD("Rendering into layer, fbo = %d", mLayer->fbo);
35
36    glBindFramebuffer(GL_FRAMEBUFFER, mLayer->fbo);
37
38    const float width = mLayer->layer.getWidth();
39    const float height = mLayer->layer.getHeight();
40
41#if RENDER_LAYERS_AS_REGIONS
42    Rect dirty(left, top, right, bottom);
43    if (dirty.isEmpty() || (dirty.left <= 0 && dirty.top <= 0 &&
44            dirty.right >= width && dirty.bottom >= height)) {
45        mLayer->region.clear();
46        dirty.set(0.0f, 0.0f, width, height);
47    } else {
48        dirty.intersect(0.0f, 0.0f, width, height);
49        android::Rect r(dirty.left, dirty.top, dirty.right, dirty.bottom);
50        mLayer->region.subtractSelf(r);
51    }
52
53    OpenGLRenderer::prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, opaque);
54#else
55    OpenGLRenderer::prepareDirty(0.0f, 0.0f, width, height, opaque);
56#endif
57}
58
59void LayerRenderer::finish() {
60    OpenGLRenderer::finish();
61
62    generateMesh();
63
64    LAYER_RENDERER_LOGD("Finished rendering into layer, fbo = %d", mLayer->fbo);
65
66    // No need to unbind our FBO, this will be taken care of by the caller
67    // who will invoke OpenGLRenderer::resume()
68}
69
70GLint LayerRenderer::getTargetFbo() {
71    return mLayer->fbo;
72}
73
74///////////////////////////////////////////////////////////////////////////////
75// Dirty region tracking
76///////////////////////////////////////////////////////////////////////////////
77
78bool LayerRenderer::hasLayer() {
79    return true;
80}
81
82Region* LayerRenderer::getRegion() {
83#if RENDER_LAYERS_AS_REGIONS
84    if (getSnapshot()->flags & Snapshot::kFlagFboTarget) {
85        return OpenGLRenderer::getRegion();
86    }
87    return &mLayer->region;
88#else
89    return OpenGLRenderer::getRegion();
90#endif
91}
92
93void LayerRenderer::generateMesh() {
94#if RENDER_LAYERS_AS_REGIONS
95    if (mLayer->region.isRect() || mLayer->region.isEmpty()) {
96        if (mLayer->mesh) {
97            delete mLayer->mesh;
98            delete mLayer->meshIndices;
99
100            mLayer->mesh = NULL;
101            mLayer->meshIndices = NULL;
102            mLayer->meshElementCount = 0;
103        }
104
105        const android::Rect& bounds = mLayer->region.getBounds();
106        mLayer->regionRect.set(bounds.leftTop().x, bounds.leftTop().y,
107                bounds.rightBottom().x, bounds.rightBottom().y);
108
109        const float texX = 1.0f / float(mLayer->width);
110        const float texY = 1.0f / float(mLayer->height);
111        const float height = mLayer->layer.getHeight();
112        mLayer->texCoords.set(
113                mLayer->regionRect.left * texX,
114                (height - mLayer->regionRect.top) * texY,
115                mLayer->regionRect.right * texX,
116                (height - mLayer->regionRect.bottom) * texY);
117
118        return;
119    }
120
121    size_t count;
122    const android::Rect* rects = mLayer->region.getArray(&count);
123
124    GLsizei elementCount = count * 6;
125
126    if (mLayer->mesh && mLayer->meshElementCount < elementCount) {
127        delete mLayer->mesh;
128        delete mLayer->meshIndices;
129
130        mLayer->mesh = NULL;
131        mLayer->meshIndices = NULL;
132    }
133
134    bool rebuildIndices = false;
135    if (!mLayer->mesh) {
136        mLayer->mesh = new TextureVertex[count * 4];
137        mLayer->meshIndices = new uint16_t[elementCount];
138        rebuildIndices = true;
139    }
140    mLayer->meshElementCount = elementCount;
141
142    const float texX = 1.0f / float(mLayer->width);
143    const float texY = 1.0f / float(mLayer->height);
144    const float height = mLayer->layer.getHeight();
145
146    TextureVertex* mesh = mLayer->mesh;
147    uint16_t* indices = mLayer->meshIndices;
148
149    for (size_t i = 0; i < count; i++) {
150        const android::Rect* r = &rects[i];
151
152        const float u1 = r->left * texX;
153        const float v1 = (height - r->top) * texY;
154        const float u2 = r->right * texX;
155        const float v2 = (height - r->bottom) * texY;
156
157        TextureVertex::set(mesh++, r->left, r->top, u1, v1);
158        TextureVertex::set(mesh++, r->right, r->top, u2, v1);
159        TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
160        TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
161
162        if (rebuildIndices) {
163            uint16_t quad = i * 4;
164            int index = i * 6;
165            indices[index    ] = quad;       // top-left
166            indices[index + 1] = quad + 1;   // top-right
167            indices[index + 2] = quad + 2;   // bottom-left
168            indices[index + 3] = quad + 2;   // bottom-left
169            indices[index + 4] = quad + 1;   // top-right
170            indices[index + 5] = quad + 3;   // bottom-right
171        }
172    }
173#endif
174}
175
176///////////////////////////////////////////////////////////////////////////////
177// Layers management
178///////////////////////////////////////////////////////////////////////////////
179
180Layer* LayerRenderer::createLayer(uint32_t width, uint32_t height, bool isOpaque) {
181    LAYER_RENDERER_LOGD("Creating new layer %dx%d", width, height);
182
183    GLuint fbo = Caches::getInstance().fboCache.get();
184    if (!fbo) {
185        LOGW("Could not obtain an FBO");
186        return NULL;
187    }
188
189    glActiveTexture(GL_TEXTURE0);
190    Layer* layer = Caches::getInstance().layerCache.get(width, height);
191    if (!layer) {
192        LOGW("Could not obtain a layer");
193        return NULL;
194    }
195
196    layer->fbo = fbo;
197    layer->layer.set(0.0f, 0.0f, width, height);
198    layer->texCoords.set(0.0f, height / float(layer->height),
199            width / float(layer->width), 0.0f);
200    layer->alpha = 255;
201    layer->mode = SkXfermode::kSrcOver_Mode;
202    layer->blend = !isOpaque;
203    layer->colorFilter = NULL;
204    layer->region.clear();
205
206    GLuint previousFbo;
207    glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*) &previousFbo);
208
209    glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
210    glBindTexture(GL_TEXTURE_2D, layer->texture);
211
212    // Initialize the texture if needed
213    if (layer->empty) {
214        layer->empty = false;
215        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->width, layer->height, 0,
216                GL_RGBA, GL_UNSIGNED_BYTE, NULL);
217
218        if (glGetError() != GL_NO_ERROR) {
219            LOGD("Could not allocate texture");
220            glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
221            glDeleteTextures(1, &layer->texture);
222            Caches::getInstance().fboCache.put(fbo);
223            delete layer;
224            return NULL;
225        }
226    }
227
228    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
229            layer->texture, 0);
230
231    glDisable(GL_SCISSOR_TEST);
232    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
233    glClear(GL_COLOR_BUFFER_BIT);
234    glEnable(GL_SCISSOR_TEST);
235
236    glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
237
238    return layer;
239}
240
241bool LayerRenderer::resizeLayer(Layer* layer, uint32_t width, uint32_t height) {
242    if (layer) {
243        LAYER_RENDERER_LOGD("Resizing layer fbo = %d to %dx%d", layer->fbo, width, height);
244
245        if (Caches::getInstance().layerCache.resize(layer, width, height)) {
246            layer->layer.set(0.0f, 0.0f, width, height);
247            layer->texCoords.set(0.0f, height / float(layer->height),
248                    width / float(layer->width), 0.0f);
249        } else {
250            if (layer->texture) glDeleteTextures(1, &layer->texture);
251            delete layer;
252            return false;
253        }
254    }
255
256    return true;
257}
258
259void LayerRenderer::destroyLayer(Layer* layer) {
260    if (layer) {
261        LAYER_RENDERER_LOGD("Destroying layer, fbo = %d", layer->fbo);
262
263        if (layer->fbo) {
264            Caches::getInstance().fboCache.put(layer->fbo);
265        }
266
267        if (!Caches::getInstance().layerCache.put(layer)) {
268            if (layer->texture) glDeleteTextures(1, &layer->texture);
269            delete layer;
270        } else {
271            layer->region.clear();
272        }
273    }
274}
275
276void LayerRenderer::destroyLayerDeferred(Layer* layer) {
277    if (layer) {
278        LAYER_RENDERER_LOGD("Deferring layer destruction, fbo = %d", layer->fbo);
279
280        Caches::getInstance().deleteLayerDeferred(layer);
281    }
282}
283
284}; // namespace uirenderer
285}; // namespace android
286