blob: f92e20b841f9ab4d723f8fe9cfbf32138828efb8 [file] [log] [blame]
Romain Guy6c319ca2011-01-11 14:29:25 -08001/*
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
Romain Guy3a3133d2011-02-01 22:59:58 -080019#include <ui/Rect.h>
20
Romain Guy09b7c912011-02-02 20:28:09 -080021#include "LayerCache.h"
Romain Guy6c319ca2011-01-11 14:29:25 -080022#include "LayerRenderer.h"
Romain Guy1fc883b2011-01-12 14:30:59 -080023#include "Properties.h"
Romain Guy3a3133d2011-02-01 22:59:58 -080024#include "Rect.h"
Romain Guy6c319ca2011-01-11 14:29:25 -080025
26namespace android {
27namespace uirenderer {
28
29///////////////////////////////////////////////////////////////////////////////
30// Rendering
31///////////////////////////////////////////////////////////////////////////////
32
Romain Guy7d7b5492011-01-24 16:33:45 -080033void LayerRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) {
Romain Guyada830f2011-01-13 12:13:20 -080034 LAYER_RENDERER_LOGD("Rendering into layer, fbo = %d", mLayer->fbo);
Romain Guy1fc883b2011-01-12 14:30:59 -080035
Romain Guy62687ec2011-02-02 15:44:19 -080036 glBindFramebuffer(GL_FRAMEBUFFER, mLayer->fbo);
37
Romain Guy09b7c912011-02-02 20:28:09 -080038 const float width = mLayer->layer.getWidth();
39 const float height = mLayer->layer.getHeight();
40
Romain Guyc88e3572011-01-22 00:32:12 -080041#if RENDER_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -080042 Rect dirty(left, top, right, bottom);
43 if (dirty.isEmpty() || (dirty.left <= 0 && dirty.top <= 0 &&
Romain Guy09b7c912011-02-02 20:28:09 -080044 dirty.right >= width && dirty.bottom >= height)) {
Romain Guy3a3133d2011-02-01 22:59:58 -080045 mLayer->region.clear();
Romain Guy09b7c912011-02-02 20:28:09 -080046 dirty.set(0.0f, 0.0f, width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -080047 } else {
Romain Guy09b7c912011-02-02 20:28:09 -080048 dirty.intersect(0.0f, 0.0f, width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -080049 android::Rect r(dirty.left, dirty.top, dirty.right, dirty.bottom);
50 mLayer->region.subtractSelf(r);
51 }
Romain Guyc88e3572011-01-22 00:32:12 -080052
Romain Guy3a3133d2011-02-01 22:59:58 -080053 OpenGLRenderer::prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, opaque);
54#else
Romain Guy09b7c912011-02-02 20:28:09 -080055 OpenGLRenderer::prepareDirty(0.0f, 0.0f, width, height, opaque);
Romain Guy3a3133d2011-02-01 22:59:58 -080056#endif
Romain Guy6c319ca2011-01-11 14:29:25 -080057}
58
59void LayerRenderer::finish() {
60 OpenGLRenderer::finish();
Romain Guy1fc883b2011-01-12 14:30:59 -080061
Romain Guyf219da52011-01-16 12:54:25 -080062 generateMesh();
63
Chet Haase678e0ad2011-01-25 09:37:18 -080064 LAYER_RENDERER_LOGD("Finished rendering into layer, fbo = %d", mLayer->fbo);
Romain Guy42f3a4b2011-01-19 13:42:26 -080065
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;
Romain Guy6c319ca2011-01-11 14:29:25 -080072}
73
74///////////////////////////////////////////////////////////////////////////////
Romain Guyf219da52011-01-16 12:54:25 -080075// 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
Romain Guy6217a712011-03-15 16:32:28 -070095#if RENDER_LAYERS_RECT_AS_RECT
Romain Guyf219da52011-01-16 12:54:25 -080096 if (mLayer->region.isRect() || mLayer->region.isEmpty()) {
Romain Guy6217a712011-03-15 16:32:28 -070097#else
98 if (mLayer->region.isEmpty()) {
99#endif
Romain Guyf219da52011-01-16 12:54:25 -0800100 if (mLayer->mesh) {
101 delete mLayer->mesh;
102 delete mLayer->meshIndices;
103
104 mLayer->mesh = NULL;
105 mLayer->meshIndices = NULL;
106 mLayer->meshElementCount = 0;
107 }
108 return;
109 }
110
111 size_t count;
112 const android::Rect* rects = mLayer->region.getArray(&count);
113
114 GLsizei elementCount = count * 6;
115
116 if (mLayer->mesh && mLayer->meshElementCount < elementCount) {
117 delete mLayer->mesh;
118 delete mLayer->meshIndices;
119
120 mLayer->mesh = NULL;
121 mLayer->meshIndices = NULL;
122 }
123
Romain Guy4f09f542011-01-26 22:41:43 -0800124 bool rebuildIndices = false;
Romain Guyf219da52011-01-16 12:54:25 -0800125 if (!mLayer->mesh) {
126 mLayer->mesh = new TextureVertex[count * 4];
127 mLayer->meshIndices = new uint16_t[elementCount];
Romain Guy4f09f542011-01-26 22:41:43 -0800128 rebuildIndices = true;
Romain Guyf219da52011-01-16 12:54:25 -0800129 }
Romain Guy4f09f542011-01-26 22:41:43 -0800130 mLayer->meshElementCount = elementCount;
Romain Guyf219da52011-01-16 12:54:25 -0800131
132 const float texX = 1.0f / float(mLayer->width);
133 const float texY = 1.0f / float(mLayer->height);
134 const float height = mLayer->layer.getHeight();
135
136 TextureVertex* mesh = mLayer->mesh;
137 uint16_t* indices = mLayer->meshIndices;
138
139 for (size_t i = 0; i < count; i++) {
140 const android::Rect* r = &rects[i];
141
142 const float u1 = r->left * texX;
143 const float v1 = (height - r->top) * texY;
144 const float u2 = r->right * texX;
145 const float v2 = (height - r->bottom) * texY;
146
147 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
148 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
149 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
150 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
151
Romain Guy4f09f542011-01-26 22:41:43 -0800152 if (rebuildIndices) {
153 uint16_t quad = i * 4;
154 int index = i * 6;
155 indices[index ] = quad; // top-left
156 indices[index + 1] = quad + 1; // top-right
157 indices[index + 2] = quad + 2; // bottom-left
158 indices[index + 3] = quad + 2; // bottom-left
159 indices[index + 4] = quad + 1; // top-right
160 indices[index + 5] = quad + 3; // bottom-right
161 }
Romain Guyf219da52011-01-16 12:54:25 -0800162 }
163#endif
164}
165
166///////////////////////////////////////////////////////////////////////////////
167// Layers management
Romain Guy6c319ca2011-01-11 14:29:25 -0800168///////////////////////////////////////////////////////////////////////////////
169
Romain Guyada830f2011-01-13 12:13:20 -0800170Layer* LayerRenderer::createLayer(uint32_t width, uint32_t height, bool isOpaque) {
Romain Guy1fc883b2011-01-12 14:30:59 -0800171 LAYER_RENDERER_LOGD("Creating new layer %dx%d", width, height);
172
Romain Guy09b7c912011-02-02 20:28:09 -0800173 GLuint fbo = Caches::getInstance().fboCache.get();
174 if (!fbo) {
175 LOGW("Could not obtain an FBO");
176 return NULL;
177 }
178
179 glActiveTexture(GL_TEXTURE0);
180 Layer* layer = Caches::getInstance().layerCache.get(width, height);
181 if (!layer) {
182 LOGW("Could not obtain a layer");
183 return NULL;
184 }
185
186 layer->fbo = fbo;
187 layer->layer.set(0.0f, 0.0f, width, height);
188 layer->texCoords.set(0.0f, height / float(layer->height),
189 width / float(layer->width), 0.0f);
190 layer->alpha = 255;
191 layer->mode = SkXfermode::kSrcOver_Mode;
192 layer->blend = !isOpaque;
193 layer->colorFilter = NULL;
194 layer->region.clear();
Romain Guyada830f2011-01-13 12:13:20 -0800195
Romain Guy6c319ca2011-01-11 14:29:25 -0800196 GLuint previousFbo;
197 glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*) &previousFbo);
198
Romain Guyada830f2011-01-13 12:13:20 -0800199 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
Romain Guyada830f2011-01-13 12:13:20 -0800200 glBindTexture(GL_TEXTURE_2D, layer->texture);
Romain Guy6c319ca2011-01-11 14:29:25 -0800201
Romain Guy09b7c912011-02-02 20:28:09 -0800202 // Initialize the texture if needed
203 if (layer->empty) {
204 layer->empty = false;
205 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->width, layer->height, 0,
206 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
Romain Guy6c319ca2011-01-11 14:29:25 -0800207
Romain Guy09b7c912011-02-02 20:28:09 -0800208 if (glGetError() != GL_NO_ERROR) {
209 LOGD("Could not allocate texture");
210 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
211 glDeleteTextures(1, &layer->texture);
212 Caches::getInstance().fboCache.put(fbo);
213 delete layer;
214 return NULL;
215 }
Romain Guy6c319ca2011-01-11 14:29:25 -0800216 }
217
218 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guyada830f2011-01-13 12:13:20 -0800219 layer->texture, 0);
Romain Guy6c319ca2011-01-11 14:29:25 -0800220
Romain Guy40a787f2011-03-02 15:15:42 -0800221 glDisable(GL_SCISSOR_TEST);
222 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
223 glClear(GL_COLOR_BUFFER_BIT);
224 glEnable(GL_SCISSOR_TEST);
225
Romain Guy6c319ca2011-01-11 14:29:25 -0800226 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
227
Romain Guyada830f2011-01-13 12:13:20 -0800228 return layer;
Romain Guy6c319ca2011-01-11 14:29:25 -0800229}
230
Romain Guyada830f2011-01-13 12:13:20 -0800231bool LayerRenderer::resizeLayer(Layer* layer, uint32_t width, uint32_t height) {
232 if (layer) {
233 LAYER_RENDERER_LOGD("Resizing layer fbo = %d to %dx%d", layer->fbo, width, height);
Romain Guy1fc883b2011-01-12 14:30:59 -0800234
Romain Guy09b7c912011-02-02 20:28:09 -0800235 if (Caches::getInstance().layerCache.resize(layer, width, height)) {
236 layer->layer.set(0.0f, 0.0f, width, height);
237 layer->texCoords.set(0.0f, height / float(layer->height),
238 width / float(layer->width), 0.0f);
239 } else {
240 if (layer->texture) glDeleteTextures(1, &layer->texture);
241 delete layer;
Romain Guyada830f2011-01-13 12:13:20 -0800242 return false;
243 }
Romain Guy6c319ca2011-01-11 14:29:25 -0800244 }
Romain Guy09b7c912011-02-02 20:28:09 -0800245
Romain Guyada830f2011-01-13 12:13:20 -0800246 return true;
Romain Guy6c319ca2011-01-11 14:29:25 -0800247}
248
Romain Guyada830f2011-01-13 12:13:20 -0800249void LayerRenderer::destroyLayer(Layer* layer) {
250 if (layer) {
251 LAYER_RENDERER_LOGD("Destroying layer, fbo = %d", layer->fbo);
Romain Guy1fc883b2011-01-12 14:30:59 -0800252
Romain Guy09b7c912011-02-02 20:28:09 -0800253 if (layer->fbo) {
254 Caches::getInstance().fboCache.put(layer->fbo);
255 }
Romain Guyada830f2011-01-13 12:13:20 -0800256
Romain Guy09b7c912011-02-02 20:28:09 -0800257 if (!Caches::getInstance().layerCache.put(layer)) {
258 if (layer->texture) glDeleteTextures(1, &layer->texture);
259 delete layer;
260 } else {
261 layer->region.clear();
262 }
Romain Guyada830f2011-01-13 12:13:20 -0800263 }
Romain Guy6c319ca2011-01-11 14:29:25 -0800264}
265
Romain Guyada830f2011-01-13 12:13:20 -0800266void LayerRenderer::destroyLayerDeferred(Layer* layer) {
267 if (layer) {
268 LAYER_RENDERER_LOGD("Deferring layer destruction, fbo = %d", layer->fbo);
Romain Guy1fc883b2011-01-12 14:30:59 -0800269
Romain Guyada830f2011-01-13 12:13:20 -0800270 Caches::getInstance().deleteLayerDeferred(layer);
271 }
Romain Guy57066eb2011-01-12 12:53:32 -0800272}
273
Romain Guy6c319ca2011-01-11 14:29:25 -0800274}; // namespace uirenderer
275}; // namespace android