blob: e034a868a2b85d796e51a456e5f8427a930700c5 [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 Guyaa6c24c2011-04-28 18:40:04 -070023#include "Matrix.h"
Romain Guy1fc883b2011-01-12 14:30:59 -080024#include "Properties.h"
Romain Guy3a3133d2011-02-01 22:59:58 -080025#include "Rect.h"
Romain Guy6c319ca2011-01-11 14:29:25 -080026
27namespace android {
28namespace uirenderer {
29
30///////////////////////////////////////////////////////////////////////////////
31// Rendering
32///////////////////////////////////////////////////////////////////////////////
33
Romain Guy7d7b5492011-01-24 16:33:45 -080034void LayerRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) {
Romain Guyada830f2011-01-13 12:13:20 -080035 LAYER_RENDERER_LOGD("Rendering into layer, fbo = %d", mLayer->fbo);
Romain Guy1fc883b2011-01-12 14:30:59 -080036
Romain Guy62687ec2011-02-02 15:44:19 -080037 glBindFramebuffer(GL_FRAMEBUFFER, mLayer->fbo);
38
Romain Guy09b7c912011-02-02 20:28:09 -080039 const float width = mLayer->layer.getWidth();
40 const float height = mLayer->layer.getHeight();
41
Romain Guyc88e3572011-01-22 00:32:12 -080042#if RENDER_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -080043 Rect dirty(left, top, right, bottom);
44 if (dirty.isEmpty() || (dirty.left <= 0 && dirty.top <= 0 &&
Romain Guy09b7c912011-02-02 20:28:09 -080045 dirty.right >= width && dirty.bottom >= height)) {
Romain Guy3a3133d2011-02-01 22:59:58 -080046 mLayer->region.clear();
Romain Guy09b7c912011-02-02 20:28:09 -080047 dirty.set(0.0f, 0.0f, width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -080048 } else {
Romain Guy09b7c912011-02-02 20:28:09 -080049 dirty.intersect(0.0f, 0.0f, width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -080050 android::Rect r(dirty.left, dirty.top, dirty.right, dirty.bottom);
51 mLayer->region.subtractSelf(r);
52 }
Romain Guyc88e3572011-01-22 00:32:12 -080053
Romain Guy3a3133d2011-02-01 22:59:58 -080054 OpenGLRenderer::prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, opaque);
55#else
Romain Guy09b7c912011-02-02 20:28:09 -080056 OpenGLRenderer::prepareDirty(0.0f, 0.0f, width, height, opaque);
Romain Guy3a3133d2011-02-01 22:59:58 -080057#endif
Romain Guy6c319ca2011-01-11 14:29:25 -080058}
59
60void LayerRenderer::finish() {
61 OpenGLRenderer::finish();
Romain Guy1fc883b2011-01-12 14:30:59 -080062
Romain Guyf219da52011-01-16 12:54:25 -080063 generateMesh();
64
Chet Haase678e0ad2011-01-25 09:37:18 -080065 LAYER_RENDERER_LOGD("Finished rendering into layer, fbo = %d", mLayer->fbo);
Romain Guy42f3a4b2011-01-19 13:42:26 -080066
67 // No need to unbind our FBO, this will be taken care of by the caller
68 // who will invoke OpenGLRenderer::resume()
69}
70
71GLint LayerRenderer::getTargetFbo() {
72 return mLayer->fbo;
Romain Guy6c319ca2011-01-11 14:29:25 -080073}
74
75///////////////////////////////////////////////////////////////////////////////
Romain Guyf219da52011-01-16 12:54:25 -080076// Dirty region tracking
77///////////////////////////////////////////////////////////////////////////////
78
79bool LayerRenderer::hasLayer() {
80 return true;
81}
82
83Region* LayerRenderer::getRegion() {
84#if RENDER_LAYERS_AS_REGIONS
85 if (getSnapshot()->flags & Snapshot::kFlagFboTarget) {
86 return OpenGLRenderer::getRegion();
87 }
88 return &mLayer->region;
89#else
90 return OpenGLRenderer::getRegion();
91#endif
92}
93
94void LayerRenderer::generateMesh() {
95#if RENDER_LAYERS_AS_REGIONS
96 if (mLayer->region.isRect() || mLayer->region.isEmpty()) {
97 if (mLayer->mesh) {
98 delete mLayer->mesh;
99 delete mLayer->meshIndices;
100
101 mLayer->mesh = NULL;
102 mLayer->meshIndices = NULL;
103 mLayer->meshElementCount = 0;
104 }
Romain Guy40667672011-03-18 14:34:03 -0700105
Romain Guy9fc27812011-04-27 14:21:41 -0700106 mLayer->setRegionAsRect();
Romain Guyf219da52011-01-16 12:54:25 -0800107 return;
108 }
109
110 size_t count;
111 const android::Rect* rects = mLayer->region.getArray(&count);
112
113 GLsizei elementCount = count * 6;
114
115 if (mLayer->mesh && mLayer->meshElementCount < elementCount) {
116 delete mLayer->mesh;
117 delete mLayer->meshIndices;
118
119 mLayer->mesh = NULL;
120 mLayer->meshIndices = NULL;
121 }
122
Romain Guy4f09f542011-01-26 22:41:43 -0800123 bool rebuildIndices = false;
Romain Guyf219da52011-01-16 12:54:25 -0800124 if (!mLayer->mesh) {
125 mLayer->mesh = new TextureVertex[count * 4];
126 mLayer->meshIndices = new uint16_t[elementCount];
Romain Guy4f09f542011-01-26 22:41:43 -0800127 rebuildIndices = true;
Romain Guyf219da52011-01-16 12:54:25 -0800128 }
Romain Guy4f09f542011-01-26 22:41:43 -0800129 mLayer->meshElementCount = elementCount;
Romain Guyf219da52011-01-16 12:54:25 -0800130
131 const float texX = 1.0f / float(mLayer->width);
132 const float texY = 1.0f / float(mLayer->height);
133 const float height = mLayer->layer.getHeight();
134
135 TextureVertex* mesh = mLayer->mesh;
136 uint16_t* indices = mLayer->meshIndices;
137
138 for (size_t i = 0; i < count; i++) {
139 const android::Rect* r = &rects[i];
140
141 const float u1 = r->left * texX;
142 const float v1 = (height - r->top) * texY;
143 const float u2 = r->right * texX;
144 const float v2 = (height - r->bottom) * texY;
145
146 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
147 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
148 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
149 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
150
Romain Guy4f09f542011-01-26 22:41:43 -0800151 if (rebuildIndices) {
152 uint16_t quad = i * 4;
153 int index = i * 6;
154 indices[index ] = quad; // top-left
155 indices[index + 1] = quad + 1; // top-right
156 indices[index + 2] = quad + 2; // bottom-left
157 indices[index + 3] = quad + 2; // bottom-left
158 indices[index + 4] = quad + 1; // top-right
159 indices[index + 5] = quad + 3; // bottom-right
160 }
Romain Guyf219da52011-01-16 12:54:25 -0800161 }
162#endif
163}
164
165///////////////////////////////////////////////////////////////////////////////
166// Layers management
Romain Guy6c319ca2011-01-11 14:29:25 -0800167///////////////////////////////////////////////////////////////////////////////
168
Romain Guyada830f2011-01-13 12:13:20 -0800169Layer* LayerRenderer::createLayer(uint32_t width, uint32_t height, bool isOpaque) {
Romain Guy1fc883b2011-01-12 14:30:59 -0800170 LAYER_RENDERER_LOGD("Creating new layer %dx%d", width, height);
171
Romain Guy09b7c912011-02-02 20:28:09 -0800172 GLuint fbo = Caches::getInstance().fboCache.get();
173 if (!fbo) {
174 LOGW("Could not obtain an FBO");
175 return NULL;
176 }
177
178 glActiveTexture(GL_TEXTURE0);
179 Layer* layer = Caches::getInstance().layerCache.get(width, height);
180 if (!layer) {
181 LOGW("Could not obtain a layer");
182 return NULL;
183 }
184
185 layer->fbo = fbo;
186 layer->layer.set(0.0f, 0.0f, width, height);
187 layer->texCoords.set(0.0f, height / float(layer->height),
188 width / float(layer->width), 0.0f);
189 layer->alpha = 255;
190 layer->mode = SkXfermode::kSrcOver_Mode;
191 layer->blend = !isOpaque;
192 layer->colorFilter = NULL;
193 layer->region.clear();
Romain Guyada830f2011-01-13 12:13:20 -0800194
Romain Guy6c319ca2011-01-11 14:29:25 -0800195 GLuint previousFbo;
196 glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*) &previousFbo);
197
Romain Guyada830f2011-01-13 12:13:20 -0800198 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
Romain Guyada830f2011-01-13 12:13:20 -0800199 glBindTexture(GL_TEXTURE_2D, layer->texture);
Romain Guy6c319ca2011-01-11 14:29:25 -0800200
Romain Guy09b7c912011-02-02 20:28:09 -0800201 // Initialize the texture if needed
202 if (layer->empty) {
203 layer->empty = false;
204 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->width, layer->height, 0,
205 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
Romain Guy6c319ca2011-01-11 14:29:25 -0800206
Romain Guy09b7c912011-02-02 20:28:09 -0800207 if (glGetError() != GL_NO_ERROR) {
208 LOGD("Could not allocate texture");
209 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
210 glDeleteTextures(1, &layer->texture);
211 Caches::getInstance().fboCache.put(fbo);
212 delete layer;
213 return NULL;
214 }
Romain Guy6c319ca2011-01-11 14:29:25 -0800215 }
216
217 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guyada830f2011-01-13 12:13:20 -0800218 layer->texture, 0);
Romain Guy6c319ca2011-01-11 14:29:25 -0800219
Romain Guy40a787f2011-03-02 15:15:42 -0800220 glDisable(GL_SCISSOR_TEST);
221 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
222 glClear(GL_COLOR_BUFFER_BIT);
223 glEnable(GL_SCISSOR_TEST);
224
Romain Guy6c319ca2011-01-11 14:29:25 -0800225 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
226
Romain Guyada830f2011-01-13 12:13:20 -0800227 return layer;
Romain Guy6c319ca2011-01-11 14:29:25 -0800228}
229
Romain Guyada830f2011-01-13 12:13:20 -0800230bool LayerRenderer::resizeLayer(Layer* layer, uint32_t width, uint32_t height) {
231 if (layer) {
232 LAYER_RENDERER_LOGD("Resizing layer fbo = %d to %dx%d", layer->fbo, width, height);
Romain Guy1fc883b2011-01-12 14:30:59 -0800233
Romain Guy09b7c912011-02-02 20:28:09 -0800234 if (Caches::getInstance().layerCache.resize(layer, width, height)) {
235 layer->layer.set(0.0f, 0.0f, width, height);
236 layer->texCoords.set(0.0f, height / float(layer->height),
237 width / float(layer->width), 0.0f);
238 } else {
239 if (layer->texture) glDeleteTextures(1, &layer->texture);
240 delete layer;
Romain Guyada830f2011-01-13 12:13:20 -0800241 return false;
242 }
Romain Guy6c319ca2011-01-11 14:29:25 -0800243 }
Romain Guy09b7c912011-02-02 20:28:09 -0800244
Romain Guyada830f2011-01-13 12:13:20 -0800245 return true;
Romain Guy6c319ca2011-01-11 14:29:25 -0800246}
247
Romain Guy4a5a7152011-06-24 17:53:53 -0700248static void setTextureParameters(Layer* layer) {
249 glBindTexture(layer->renderTarget, layer->texture);
250
251 glTexParameteri(layer->renderTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
252 glTexParameteri(layer->renderTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
253
254 glTexParameteri(layer->renderTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
255 glTexParameteri(layer->renderTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
256}
257
258Layer* LayerRenderer::createTextureLayer(bool isOpaque) {
259 LAYER_RENDERER_LOGD("Creating new texture layer");
260
261 Layer* layer = new Layer(0, 0);
262 layer->isCacheable = false;
263 layer->isTextureLayer = true;
264 layer->blend = !isOpaque;
265 layer->empty = true;
266 layer->fbo = 0;
267 layer->colorFilter = NULL;
268 layer->fbo = 0;
269 layer->layer.set(0.0f, 0.0f, 0.0f, 0.0f);
270 layer->texCoords.set(0.0f, 1.0f, 0.0f, 1.0f);
271 layer->alpha = 255;
272 layer->mode = SkXfermode::kSrcOver_Mode;
273 layer->colorFilter = NULL;
274 layer->region.clear();
275 layer->renderTarget = GL_NONE; // see ::updateTextureLayer()
276
277 glActiveTexture(GL_TEXTURE0);
278 glGenTextures(1, &layer->texture);
279
280 return layer;
281}
282
Romain Guyaa6c24c2011-04-28 18:40:04 -0700283void LayerRenderer::updateTextureLayer(Layer* layer, uint32_t width, uint32_t height,
Romain Guya9489272011-06-22 20:58:11 -0700284 bool isOpaque, GLenum renderTarget, float* transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700285 if (layer) {
Romain Guya9489272011-06-22 20:58:11 -0700286 layer->blend = !isOpaque;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700287 layer->width = width;
288 layer->height = height;
289 layer->layer.set(0.0f, 0.0f, width, height);
290 layer->region.set(width, height);
291 layer->regionRect.set(0.0f, 0.0f, width, height);
292 layer->texTransform.load(transform);
Romain Guy8f0095c2011-05-02 17:24:22 -0700293
Romain Guy4a5a7152011-06-24 17:53:53 -0700294 if (renderTarget != layer->renderTarget) {
295 layer->renderTarget = renderTarget;
296 setTextureParameters(layer);
297 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700298 }
299}
300
Romain Guyada830f2011-01-13 12:13:20 -0800301void LayerRenderer::destroyLayer(Layer* layer) {
302 if (layer) {
303 LAYER_RENDERER_LOGD("Destroying layer, fbo = %d", layer->fbo);
Romain Guy1fc883b2011-01-12 14:30:59 -0800304
Romain Guy09b7c912011-02-02 20:28:09 -0800305 if (layer->fbo) {
306 Caches::getInstance().fboCache.put(layer->fbo);
307 }
Romain Guyada830f2011-01-13 12:13:20 -0800308
Romain Guy09b7c912011-02-02 20:28:09 -0800309 if (!Caches::getInstance().layerCache.put(layer)) {
310 if (layer->texture) glDeleteTextures(1, &layer->texture);
311 delete layer;
312 } else {
313 layer->region.clear();
314 }
Romain Guyada830f2011-01-13 12:13:20 -0800315 }
Romain Guy6c319ca2011-01-11 14:29:25 -0800316}
317
Romain Guyada830f2011-01-13 12:13:20 -0800318void LayerRenderer::destroyLayerDeferred(Layer* layer) {
319 if (layer) {
320 LAYER_RENDERER_LOGD("Deferring layer destruction, fbo = %d", layer->fbo);
Romain Guy1fc883b2011-01-12 14:30:59 -0800321
Romain Guyada830f2011-01-13 12:13:20 -0800322 Caches::getInstance().deleteLayerDeferred(layer);
323 }
Romain Guy57066eb2011-01-12 12:53:32 -0800324}
325
Romain Guy77a81162011-06-14 16:45:55 -0700326bool LayerRenderer::copyLayer(Layer* layer, SkBitmap* bitmap) {
327 Caches& caches = Caches::getInstance();
328 if (layer && layer->isTextureLayer && bitmap->width() <= caches.maxTextureSize &&
329 bitmap->height() <= caches.maxTextureSize) {
330
331 GLuint fbo = caches.fboCache.get();
332 if (!fbo) {
333 LOGW("Could not obtain an FBO");
334 return false;
335 }
336
Romain Guyd6b2a002011-06-17 17:45:59 -0700337 SkAutoLockPixels alp(*bitmap);
338
Romain Guy77a81162011-06-14 16:45:55 -0700339 GLuint texture;
340 GLuint previousFbo;
341
342 GLenum format;
343 GLenum type;
344
Romain Guyd6b2a002011-06-17 17:45:59 -0700345 GLenum error = GL_NO_ERROR;
346 bool status = false;
347
Romain Guy77a81162011-06-14 16:45:55 -0700348 switch (bitmap->config()) {
349 case SkBitmap::kA8_Config:
350 format = GL_ALPHA;
351 type = GL_UNSIGNED_BYTE;
352 break;
353 case SkBitmap::kRGB_565_Config:
354 format = GL_RGB;
355 type = GL_UNSIGNED_SHORT_5_6_5;
356 break;
357 case SkBitmap::kARGB_4444_Config:
358 format = GL_RGBA;
359 type = GL_UNSIGNED_SHORT_4_4_4_4;
360 break;
361 case SkBitmap::kARGB_8888_Config:
362 default:
363 format = GL_RGBA;
364 type = GL_UNSIGNED_BYTE;
365 break;
366 }
367
Romain Guyd6b2a002011-06-17 17:45:59 -0700368 float alpha = layer->alpha;
369 SkXfermode::Mode mode = layer->mode;
370
371 layer->mode = SkXfermode::kSrc_Mode;
372 layer->alpha = 255;
373 layer->fbo = fbo;
374
Romain Guy77a81162011-06-14 16:45:55 -0700375 glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*) &previousFbo);
376 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
377
378 glGenTextures(1, &texture);
Romain Guyd6b2a002011-06-17 17:45:59 -0700379 if ((error = glGetError()) != GL_NO_ERROR) goto error;
Romain Guy77a81162011-06-14 16:45:55 -0700380
381 glActiveTexture(GL_TEXTURE0);
382 glBindTexture(GL_TEXTURE_2D, texture);
383
384 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
385 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
386
387 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
388 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
389
390 glTexImage2D(GL_TEXTURE_2D, 0, format, bitmap->width(), bitmap->height(),
391 0, format, type, NULL);
Romain Guyd6b2a002011-06-17 17:45:59 -0700392 if ((error = glGetError()) != GL_NO_ERROR) goto error;
393
Romain Guy77a81162011-06-14 16:45:55 -0700394 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
395 GL_TEXTURE_2D, texture, 0);
Romain Guyd6b2a002011-06-17 17:45:59 -0700396 if ((error = glGetError()) != GL_NO_ERROR) goto error;
Romain Guy77a81162011-06-14 16:45:55 -0700397
Romain Guyd6b2a002011-06-17 17:45:59 -0700398 {
399 LayerRenderer renderer(layer);
400 renderer.setViewport(bitmap->width(), bitmap->height());
401 renderer.OpenGLRenderer::prepareDirty(0.0f, 0.0f,
402 bitmap->width(), bitmap->height(), !layer->blend);
403 if ((error = glGetError()) != GL_NO_ERROR) goto error;
Romain Guy77a81162011-06-14 16:45:55 -0700404
Romain Guyd6b2a002011-06-17 17:45:59 -0700405 {
406 Rect bounds;
407 bounds.set(0.0f, 0.0f, bitmap->width(), bitmap->height());
408 renderer.drawTextureLayer(layer, bounds);
Romain Guy77a81162011-06-14 16:45:55 -0700409
Romain Guyd6b2a002011-06-17 17:45:59 -0700410 glReadPixels(0, 0, bitmap->width(), bitmap->height(), format,
411 type, bitmap->getPixels());
Romain Guy77a81162011-06-14 16:45:55 -0700412
Romain Guyd6b2a002011-06-17 17:45:59 -0700413 if ((error = glGetError()) != GL_NO_ERROR) goto error;
414 }
Romain Guy77a81162011-06-14 16:45:55 -0700415
Romain Guyd6b2a002011-06-17 17:45:59 -0700416 status = true;
417 }
Romain Guy77a81162011-06-14 16:45:55 -0700418
Romain Guyd6b2a002011-06-17 17:45:59 -0700419error:
420#if DEBUG_OPENGL
421 if (error != GL_NO_ERROR) {
422 LOGD("GL error while copying layer into bitmap = 0x%x", error);
423 }
424#endif
Romain Guy77a81162011-06-14 16:45:55 -0700425
426 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guy77a81162011-06-14 16:45:55 -0700427 layer->mode = mode;
428 layer->alpha = alpha;
429 layer->fbo = 0;
430 glDeleteTextures(1, &texture);
431 caches.fboCache.put(fbo);
432
Romain Guyd6b2a002011-06-17 17:45:59 -0700433 return status;
Romain Guy77a81162011-06-14 16:45:55 -0700434 }
435 return false;
436}
437
Romain Guy6c319ca2011-01-11 14:29:25 -0800438}; // namespace uirenderer
439}; // namespace android