blob: a3986733a64efce738a1f9d9707d98a0d1d0ed21 [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
Mathias Agopiana5f7b3c2011-09-16 16:59:52 -070034Rect* gHackDontCorruptRegisters;
35
Romain Guy7d7b5492011-01-24 16:33:45 -080036void LayerRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) {
Romain Guy9ace8f52011-07-07 20:50:11 -070037 LAYER_RENDERER_LOGD("Rendering into layer, fbo = %d", mLayer->getFbo());
Romain Guy1fc883b2011-01-12 14:30:59 -080038
Mathias Agopiana5f7b3c2011-09-16 16:59:52 -070039 Rect dirty(left, top, right, bottom);
40 gHackDontCorruptRegisters = &dirty;
41
Romain Guy9ace8f52011-07-07 20:50:11 -070042 glBindFramebuffer(GL_FRAMEBUFFER, mLayer->getFbo());
Romain Guy62687ec2011-02-02 15:44:19 -080043
Romain Guy09b7c912011-02-02 20:28:09 -080044 const float width = mLayer->layer.getWidth();
45 const float height = mLayer->layer.getHeight();
46
Romain Guyc88e3572011-01-22 00:32:12 -080047#if RENDER_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -080048 if (dirty.isEmpty() || (dirty.left <= 0 && dirty.top <= 0 &&
Romain Guy09b7c912011-02-02 20:28:09 -080049 dirty.right >= width && dirty.bottom >= height)) {
Romain Guy3a3133d2011-02-01 22:59:58 -080050 mLayer->region.clear();
Romain Guy09b7c912011-02-02 20:28:09 -080051 dirty.set(0.0f, 0.0f, width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -080052 } else {
Romain Guy09b7c912011-02-02 20:28:09 -080053 dirty.intersect(0.0f, 0.0f, width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -080054 android::Rect r(dirty.left, dirty.top, dirty.right, dirty.bottom);
55 mLayer->region.subtractSelf(r);
56 }
Romain Guyc88e3572011-01-22 00:32:12 -080057
Romain Guy3a3133d2011-02-01 22:59:58 -080058 OpenGLRenderer::prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, opaque);
59#else
Romain Guy09b7c912011-02-02 20:28:09 -080060 OpenGLRenderer::prepareDirty(0.0f, 0.0f, width, height, opaque);
Romain Guy3a3133d2011-02-01 22:59:58 -080061#endif
Romain Guy6c319ca2011-01-11 14:29:25 -080062}
63
64void LayerRenderer::finish() {
65 OpenGLRenderer::finish();
Romain Guy1fc883b2011-01-12 14:30:59 -080066
Romain Guyf219da52011-01-16 12:54:25 -080067 generateMesh();
68
Romain Guy9ace8f52011-07-07 20:50:11 -070069 LAYER_RENDERER_LOGD("Finished rendering into layer, fbo = %d", mLayer->getFbo());
Romain Guy42f3a4b2011-01-19 13:42:26 -080070
71 // No need to unbind our FBO, this will be taken care of by the caller
72 // who will invoke OpenGLRenderer::resume()
73}
74
75GLint LayerRenderer::getTargetFbo() {
Romain Guy9ace8f52011-07-07 20:50:11 -070076 return mLayer->getFbo();
Romain Guy6c319ca2011-01-11 14:29:25 -080077}
78
79///////////////////////////////////////////////////////////////////////////////
Romain Guyf219da52011-01-16 12:54:25 -080080// Dirty region tracking
81///////////////////////////////////////////////////////////////////////////////
82
83bool LayerRenderer::hasLayer() {
84 return true;
85}
86
87Region* LayerRenderer::getRegion() {
88#if RENDER_LAYERS_AS_REGIONS
89 if (getSnapshot()->flags & Snapshot::kFlagFboTarget) {
90 return OpenGLRenderer::getRegion();
91 }
92 return &mLayer->region;
93#else
94 return OpenGLRenderer::getRegion();
95#endif
96}
97
Romain Guy8a3957d2011-09-07 17:55:15 -070098// TODO: This implementation is flawed and can generate T-junctions
99// in the mesh, which will in turn produce cracks when the
100// mesh is rotated/skewed. The easiest way to fix this would
101// be, for each row, to add new vertices shared with the previous
102// row when the two rows share an edge.
103// In practice, T-junctions do not appear often so this has yet
104// to be fixed.
Romain Guyf219da52011-01-16 12:54:25 -0800105void LayerRenderer::generateMesh() {
106#if RENDER_LAYERS_AS_REGIONS
107 if (mLayer->region.isRect() || mLayer->region.isEmpty()) {
108 if (mLayer->mesh) {
109 delete mLayer->mesh;
110 delete mLayer->meshIndices;
111
112 mLayer->mesh = NULL;
113 mLayer->meshIndices = NULL;
114 mLayer->meshElementCount = 0;
115 }
Romain Guy40667672011-03-18 14:34:03 -0700116
Romain Guy9fc27812011-04-27 14:21:41 -0700117 mLayer->setRegionAsRect();
Romain Guyf219da52011-01-16 12:54:25 -0800118 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
Romain Guy4f09f542011-01-26 22:41:43 -0800134 bool rebuildIndices = false;
Romain Guyf219da52011-01-16 12:54:25 -0800135 if (!mLayer->mesh) {
136 mLayer->mesh = new TextureVertex[count * 4];
137 mLayer->meshIndices = new uint16_t[elementCount];
Romain Guy4f09f542011-01-26 22:41:43 -0800138 rebuildIndices = true;
Romain Guyf219da52011-01-16 12:54:25 -0800139 }
Romain Guy4f09f542011-01-26 22:41:43 -0800140 mLayer->meshElementCount = elementCount;
Romain Guyf219da52011-01-16 12:54:25 -0800141
Romain Guy9ace8f52011-07-07 20:50:11 -0700142 const float texX = 1.0f / float(mLayer->getWidth());
143 const float texY = 1.0f / float(mLayer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800144 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
Romain Guy4f09f542011-01-26 22:41:43 -0800162 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 }
Romain Guyf219da52011-01-16 12:54:25 -0800172 }
173#endif
174}
175
176///////////////////////////////////////////////////////////////////////////////
177// Layers management
Romain Guy6c319ca2011-01-11 14:29:25 -0800178///////////////////////////////////////////////////////////////////////////////
179
Romain Guyada830f2011-01-13 12:13:20 -0800180Layer* LayerRenderer::createLayer(uint32_t width, uint32_t height, bool isOpaque) {
Romain Guyeea60692011-07-26 20:35:55 -0700181 LAYER_RENDERER_LOGD("Requesting new render layer %dx%d", width, height);
Romain Guy1fc883b2011-01-12 14:30:59 -0800182
Romain Guy09b7c912011-02-02 20:28:09 -0800183 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
Romain Guy9ace8f52011-07-07 20:50:11 -0700196 layer->setFbo(fbo);
Romain Guy09b7c912011-02-02 20:28:09 -0800197 layer->layer.set(0.0f, 0.0f, width, height);
Romain Guy9ace8f52011-07-07 20:50:11 -0700198 layer->texCoords.set(0.0f, height / float(layer->getHeight()),
199 width / float(layer->getWidth()), 0.0f);
200 layer->setAlpha(255, SkXfermode::kSrcOver_Mode);
201 layer->setBlend(!isOpaque);
202 layer->setColorFilter(NULL);
Romain Guy09b7c912011-02-02 20:28:09 -0800203 layer->region.clear();
Romain Guyada830f2011-01-13 12:13:20 -0800204
Romain Guy6c319ca2011-01-11 14:29:25 -0800205 GLuint previousFbo;
206 glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*) &previousFbo);
207
Romain Guy9ace8f52011-07-07 20:50:11 -0700208 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
209 layer->bindTexture();
Romain Guy6c319ca2011-01-11 14:29:25 -0800210
Romain Guy09b7c912011-02-02 20:28:09 -0800211 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700212 if (layer->isEmpty()) {
213 layer->setEmpty(false);
214 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
Romain Guy6c319ca2011-01-11 14:29:25 -0800215
Romain Guy09b7c912011-02-02 20:28:09 -0800216 if (glGetError() != GL_NO_ERROR) {
217 LOGD("Could not allocate texture");
Romain Guy9ace8f52011-07-07 20:50:11 -0700218
Romain Guy09b7c912011-02-02 20:28:09 -0800219 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guy09b7c912011-02-02 20:28:09 -0800220 Caches::getInstance().fboCache.put(fbo);
Romain Guy9ace8f52011-07-07 20:50:11 -0700221
222 layer->deleteTexture();
Romain Guy09b7c912011-02-02 20:28:09 -0800223 delete layer;
Romain Guy9ace8f52011-07-07 20:50:11 -0700224
Romain Guy09b7c912011-02-02 20:28:09 -0800225 return NULL;
226 }
Romain Guy6c319ca2011-01-11 14:29:25 -0800227 }
228
229 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700230 layer->getTexture(), 0);
Romain Guy6c319ca2011-01-11 14:29:25 -0800231
Romain Guy40a787f2011-03-02 15:15:42 -0800232 glDisable(GL_SCISSOR_TEST);
233 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
234 glClear(GL_COLOR_BUFFER_BIT);
235 glEnable(GL_SCISSOR_TEST);
236
Romain Guy6c319ca2011-01-11 14:29:25 -0800237 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
238
Romain Guyada830f2011-01-13 12:13:20 -0800239 return layer;
Romain Guy6c319ca2011-01-11 14:29:25 -0800240}
241
Romain Guyada830f2011-01-13 12:13:20 -0800242bool LayerRenderer::resizeLayer(Layer* layer, uint32_t width, uint32_t height) {
243 if (layer) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700244 LAYER_RENDERER_LOGD("Resizing layer fbo = %d to %dx%d", layer->getFbo(), width, height);
Romain Guy1fc883b2011-01-12 14:30:59 -0800245
Romain Guy09b7c912011-02-02 20:28:09 -0800246 if (Caches::getInstance().layerCache.resize(layer, width, height)) {
247 layer->layer.set(0.0f, 0.0f, width, height);
Romain Guy9ace8f52011-07-07 20:50:11 -0700248 layer->texCoords.set(0.0f, height / float(layer->getHeight()),
249 width / float(layer->getWidth()), 0.0f);
Romain Guy09b7c912011-02-02 20:28:09 -0800250 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700251 layer->deleteTexture();
Romain Guy09b7c912011-02-02 20:28:09 -0800252 delete layer;
Romain Guyada830f2011-01-13 12:13:20 -0800253 return false;
254 }
Romain Guy6c319ca2011-01-11 14:29:25 -0800255 }
Romain Guy09b7c912011-02-02 20:28:09 -0800256
Romain Guyada830f2011-01-13 12:13:20 -0800257 return true;
Romain Guy6c319ca2011-01-11 14:29:25 -0800258}
259
Romain Guy4a5a7152011-06-24 17:53:53 -0700260Layer* LayerRenderer::createTextureLayer(bool isOpaque) {
261 LAYER_RENDERER_LOGD("Creating new texture layer");
262
263 Layer* layer = new Layer(0, 0);
Romain Guy9ace8f52011-07-07 20:50:11 -0700264 layer->setCacheable(false);
265 layer->setTextureLayer(true);
266 layer->setBlend(!isOpaque);
267 layer->setEmpty(true);
268 layer->setFbo(0);
269 layer->setAlpha(255, SkXfermode::kSrcOver_Mode);
Romain Guy4a5a7152011-06-24 17:53:53 -0700270 layer->layer.set(0.0f, 0.0f, 0.0f, 0.0f);
271 layer->texCoords.set(0.0f, 1.0f, 0.0f, 1.0f);
Romain Guy4a5a7152011-06-24 17:53:53 -0700272 layer->region.clear();
Romain Guy9ace8f52011-07-07 20:50:11 -0700273 layer->setRenderTarget(GL_NONE); // see ::updateTextureLayer()
Romain Guy4a5a7152011-06-24 17:53:53 -0700274
275 glActiveTexture(GL_TEXTURE0);
Romain Guy9ace8f52011-07-07 20:50:11 -0700276 layer->generateTexture();
Romain Guy4a5a7152011-06-24 17:53:53 -0700277
278 return layer;
279}
280
Romain Guyaa6c24c2011-04-28 18:40:04 -0700281void LayerRenderer::updateTextureLayer(Layer* layer, uint32_t width, uint32_t height,
Romain Guya9489272011-06-22 20:58:11 -0700282 bool isOpaque, GLenum renderTarget, float* transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700283 if (layer) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700284 layer->setBlend(!isOpaque);
285 layer->setSize(width, height);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700286 layer->layer.set(0.0f, 0.0f, width, height);
287 layer->region.set(width, height);
288 layer->regionRect.set(0.0f, 0.0f, width, height);
Romain Guy9ace8f52011-07-07 20:50:11 -0700289 layer->getTexTransform().load(transform);
Romain Guy8f0095c2011-05-02 17:24:22 -0700290
Romain Guy9ace8f52011-07-07 20:50:11 -0700291 if (renderTarget != layer->getRenderTarget()) {
292 layer->setRenderTarget(renderTarget);
293 layer->bindTexture();
294 layer->setFilter(GL_NEAREST, GL_NEAREST, false, true);
295 layer->setWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, false, true);
Romain Guy4a5a7152011-06-24 17:53:53 -0700296 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700297 }
298}
299
Romain Guyada830f2011-01-13 12:13:20 -0800300void LayerRenderer::destroyLayer(Layer* layer) {
301 if (layer) {
Romain Guyeea60692011-07-26 20:35:55 -0700302 LAYER_RENDERER_LOGD("Recycling layer, %dx%d fbo = %d",
303 layer->getWidth(), layer->getHeight(), layer->getFbo());
Romain Guy1fc883b2011-01-12 14:30:59 -0800304
Romain Guy9ace8f52011-07-07 20:50:11 -0700305 if (layer->getFbo()) {
306 Caches::getInstance().fboCache.put(layer->getFbo());
Romain Guy09b7c912011-02-02 20:28:09 -0800307 }
Romain Guyada830f2011-01-13 12:13:20 -0800308
Romain Guy09b7c912011-02-02 20:28:09 -0800309 if (!Caches::getInstance().layerCache.put(layer)) {
Romain Guyeea60692011-07-26 20:35:55 -0700310 LAYER_RENDERER_LOGD(" Destroyed!");
Romain Guy9ace8f52011-07-07 20:50:11 -0700311 layer->deleteTexture();
Romain Guy09b7c912011-02-02 20:28:09 -0800312 delete layer;
313 } else {
Romain Guyeea60692011-07-26 20:35:55 -0700314 LAYER_RENDERER_LOGD(" Cached!");
Romain Guy65b345f2011-07-27 18:51:50 -0700315#if DEBUG_LAYER_RENDERER
Romain Guyeea60692011-07-26 20:35:55 -0700316 Caches::getInstance().layerCache.dump();
317#endif
Romain Guy09b7c912011-02-02 20:28:09 -0800318 layer->region.clear();
319 }
Romain Guyada830f2011-01-13 12:13:20 -0800320 }
Romain Guy6c319ca2011-01-11 14:29:25 -0800321}
322
Romain Guyada830f2011-01-13 12:13:20 -0800323void LayerRenderer::destroyLayerDeferred(Layer* layer) {
324 if (layer) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700325 LAYER_RENDERER_LOGD("Deferring layer destruction, fbo = %d", layer->getFbo());
Romain Guy1fc883b2011-01-12 14:30:59 -0800326
Romain Guyada830f2011-01-13 12:13:20 -0800327 Caches::getInstance().deleteLayerDeferred(layer);
328 }
Romain Guy57066eb2011-01-12 12:53:32 -0800329}
330
Romain Guy77a81162011-06-14 16:45:55 -0700331bool LayerRenderer::copyLayer(Layer* layer, SkBitmap* bitmap) {
332 Caches& caches = Caches::getInstance();
Romain Guy9ace8f52011-07-07 20:50:11 -0700333 if (layer && layer->isTextureLayer() && bitmap->width() <= caches.maxTextureSize &&
Romain Guy77a81162011-06-14 16:45:55 -0700334 bitmap->height() <= caches.maxTextureSize) {
335
336 GLuint fbo = caches.fboCache.get();
337 if (!fbo) {
338 LOGW("Could not obtain an FBO");
339 return false;
340 }
341
Romain Guyd6b2a002011-06-17 17:45:59 -0700342 SkAutoLockPixels alp(*bitmap);
343
Romain Guy77a81162011-06-14 16:45:55 -0700344 GLuint texture;
345 GLuint previousFbo;
346
347 GLenum format;
348 GLenum type;
349
Romain Guyd6b2a002011-06-17 17:45:59 -0700350 GLenum error = GL_NO_ERROR;
351 bool status = false;
352
Romain Guy77a81162011-06-14 16:45:55 -0700353 switch (bitmap->config()) {
354 case SkBitmap::kA8_Config:
355 format = GL_ALPHA;
356 type = GL_UNSIGNED_BYTE;
357 break;
358 case SkBitmap::kRGB_565_Config:
359 format = GL_RGB;
360 type = GL_UNSIGNED_SHORT_5_6_5;
361 break;
362 case SkBitmap::kARGB_4444_Config:
363 format = GL_RGBA;
364 type = GL_UNSIGNED_SHORT_4_4_4_4;
365 break;
366 case SkBitmap::kARGB_8888_Config:
367 default:
368 format = GL_RGBA;
369 type = GL_UNSIGNED_BYTE;
370 break;
371 }
372
Romain Guy9ace8f52011-07-07 20:50:11 -0700373 float alpha = layer->getAlpha();
374 SkXfermode::Mode mode = layer->getMode();
Romain Guyd6b2a002011-06-17 17:45:59 -0700375
Romain Guy9ace8f52011-07-07 20:50:11 -0700376 layer->setAlpha(255, SkXfermode::kSrc_Mode);
377 layer->setFbo(fbo);
Romain Guyd6b2a002011-06-17 17:45:59 -0700378
Romain Guy77a81162011-06-14 16:45:55 -0700379 glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*) &previousFbo);
380 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
381
382 glGenTextures(1, &texture);
Romain Guyd6b2a002011-06-17 17:45:59 -0700383 if ((error = glGetError()) != GL_NO_ERROR) goto error;
Romain Guy77a81162011-06-14 16:45:55 -0700384
385 glActiveTexture(GL_TEXTURE0);
386 glBindTexture(GL_TEXTURE_2D, texture);
387
Romain Guyec19b4a2011-07-07 21:05:04 -0700388 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
389 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
Romain Guy77a81162011-06-14 16:45:55 -0700390
391 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
392 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
393
394 glTexImage2D(GL_TEXTURE_2D, 0, format, bitmap->width(), bitmap->height(),
395 0, format, type, NULL);
Romain Guyd6b2a002011-06-17 17:45:59 -0700396 if ((error = glGetError()) != GL_NO_ERROR) goto error;
397
Romain Guy77a81162011-06-14 16:45:55 -0700398 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
399 GL_TEXTURE_2D, texture, 0);
Romain Guyd6b2a002011-06-17 17:45:59 -0700400 if ((error = glGetError()) != GL_NO_ERROR) goto error;
Romain Guy77a81162011-06-14 16:45:55 -0700401
Romain Guyd6b2a002011-06-17 17:45:59 -0700402 {
403 LayerRenderer renderer(layer);
404 renderer.setViewport(bitmap->width(), bitmap->height());
405 renderer.OpenGLRenderer::prepareDirty(0.0f, 0.0f,
Romain Guy9ace8f52011-07-07 20:50:11 -0700406 bitmap->width(), bitmap->height(), !layer->isBlend());
Romain Guyd6b2a002011-06-17 17:45:59 -0700407 if ((error = glGetError()) != GL_NO_ERROR) goto error;
Romain Guy77a81162011-06-14 16:45:55 -0700408
Romain Guyd6b2a002011-06-17 17:45:59 -0700409 {
410 Rect bounds;
411 bounds.set(0.0f, 0.0f, bitmap->width(), bitmap->height());
412 renderer.drawTextureLayer(layer, bounds);
Romain Guy77a81162011-06-14 16:45:55 -0700413
Romain Guyd6b2a002011-06-17 17:45:59 -0700414 glReadPixels(0, 0, bitmap->width(), bitmap->height(), format,
415 type, bitmap->getPixels());
Romain Guy77a81162011-06-14 16:45:55 -0700416
Romain Guyd6b2a002011-06-17 17:45:59 -0700417 if ((error = glGetError()) != GL_NO_ERROR) goto error;
418 }
Romain Guy77a81162011-06-14 16:45:55 -0700419
Romain Guyd6b2a002011-06-17 17:45:59 -0700420 status = true;
421 }
Romain Guy77a81162011-06-14 16:45:55 -0700422
Romain Guyd6b2a002011-06-17 17:45:59 -0700423error:
424#if DEBUG_OPENGL
425 if (error != GL_NO_ERROR) {
426 LOGD("GL error while copying layer into bitmap = 0x%x", error);
427 }
428#endif
Romain Guy77a81162011-06-14 16:45:55 -0700429
430 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guy9ace8f52011-07-07 20:50:11 -0700431 layer->setAlpha(alpha, mode);
432 layer->setFbo(0);
Romain Guy77a81162011-06-14 16:45:55 -0700433 glDeleteTextures(1, &texture);
434 caches.fboCache.put(fbo);
435
Romain Guyd6b2a002011-06-17 17:45:59 -0700436 return status;
Romain Guy77a81162011-06-14 16:45:55 -0700437 }
438 return false;
439}
440
Romain Guy6c319ca2011-01-11 14:29:25 -0800441}; // namespace uirenderer
442}; // namespace android