blob: c30923f9f16c5e44f455d16f34b40231242dcfec [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 Guy79537452011-10-12 13:48:51 -070034LayerRenderer::LayerRenderer(Layer* layer): mLayer(layer) {
35}
36
37LayerRenderer::~LayerRenderer() {
38}
39
Romain Guy7d7b5492011-01-24 16:33:45 -080040void LayerRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) {
Romain Guy9ace8f52011-07-07 20:50:11 -070041 LAYER_RENDERER_LOGD("Rendering into layer, fbo = %d", mLayer->getFbo());
Romain Guy1fc883b2011-01-12 14:30:59 -080042
Romain Guy9ace8f52011-07-07 20:50:11 -070043 glBindFramebuffer(GL_FRAMEBUFFER, mLayer->getFbo());
Romain Guy62687ec2011-02-02 15:44:19 -080044
Romain Guy09b7c912011-02-02 20:28:09 -080045 const float width = mLayer->layer.getWidth();
46 const float height = mLayer->layer.getHeight();
47
Romain Guyc88e3572011-01-22 00:32:12 -080048#if RENDER_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -080049 Rect dirty(left, top, right, bottom);
50 if (dirty.isEmpty() || (dirty.left <= 0 && dirty.top <= 0 &&
Romain Guy09b7c912011-02-02 20:28:09 -080051 dirty.right >= width && dirty.bottom >= height)) {
Romain Guy3a3133d2011-02-01 22:59:58 -080052 mLayer->region.clear();
Romain Guy09b7c912011-02-02 20:28:09 -080053 dirty.set(0.0f, 0.0f, width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -080054 } else {
Romain Guy09b7c912011-02-02 20:28:09 -080055 dirty.intersect(0.0f, 0.0f, width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -080056 android::Rect r(dirty.left, dirty.top, dirty.right, dirty.bottom);
57 mLayer->region.subtractSelf(r);
58 }
Romain Guyc88e3572011-01-22 00:32:12 -080059
Romain Guy3a3133d2011-02-01 22:59:58 -080060 OpenGLRenderer::prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, opaque);
61#else
Romain Guy09b7c912011-02-02 20:28:09 -080062 OpenGLRenderer::prepareDirty(0.0f, 0.0f, width, height, opaque);
Romain Guy3a3133d2011-02-01 22:59:58 -080063#endif
Romain Guy6c319ca2011-01-11 14:29:25 -080064}
65
66void LayerRenderer::finish() {
67 OpenGLRenderer::finish();
Romain Guy1fc883b2011-01-12 14:30:59 -080068
Romain Guyf219da52011-01-16 12:54:25 -080069 generateMesh();
70
Romain Guy9ace8f52011-07-07 20:50:11 -070071 LAYER_RENDERER_LOGD("Finished rendering into layer, fbo = %d", mLayer->getFbo());
Romain Guy42f3a4b2011-01-19 13:42:26 -080072
73 // No need to unbind our FBO, this will be taken care of by the caller
74 // who will invoke OpenGLRenderer::resume()
75}
76
77GLint LayerRenderer::getTargetFbo() {
Romain Guy9ace8f52011-07-07 20:50:11 -070078 return mLayer->getFbo();
Romain Guy6c319ca2011-01-11 14:29:25 -080079}
80
81///////////////////////////////////////////////////////////////////////////////
Romain Guyf219da52011-01-16 12:54:25 -080082// Dirty region tracking
83///////////////////////////////////////////////////////////////////////////////
84
85bool LayerRenderer::hasLayer() {
86 return true;
87}
88
89Region* LayerRenderer::getRegion() {
90#if RENDER_LAYERS_AS_REGIONS
91 if (getSnapshot()->flags & Snapshot::kFlagFboTarget) {
92 return OpenGLRenderer::getRegion();
93 }
94 return &mLayer->region;
95#else
96 return OpenGLRenderer::getRegion();
97#endif
98}
99
Romain Guy8a3957d2011-09-07 17:55:15 -0700100// TODO: This implementation is flawed and can generate T-junctions
101// in the mesh, which will in turn produce cracks when the
102// mesh is rotated/skewed. The easiest way to fix this would
103// be, for each row, to add new vertices shared with the previous
104// row when the two rows share an edge.
105// In practice, T-junctions do not appear often so this has yet
106// to be fixed.
Romain Guyf219da52011-01-16 12:54:25 -0800107void LayerRenderer::generateMesh() {
108#if RENDER_LAYERS_AS_REGIONS
109 if (mLayer->region.isRect() || mLayer->region.isEmpty()) {
110 if (mLayer->mesh) {
111 delete mLayer->mesh;
112 delete mLayer->meshIndices;
113
114 mLayer->mesh = NULL;
115 mLayer->meshIndices = NULL;
116 mLayer->meshElementCount = 0;
117 }
Romain Guy40667672011-03-18 14:34:03 -0700118
Romain Guy9fc27812011-04-27 14:21:41 -0700119 mLayer->setRegionAsRect();
Romain Guyf219da52011-01-16 12:54:25 -0800120 return;
121 }
122
123 size_t count;
124 const android::Rect* rects = mLayer->region.getArray(&count);
125
126 GLsizei elementCount = count * 6;
127
128 if (mLayer->mesh && mLayer->meshElementCount < elementCount) {
129 delete mLayer->mesh;
130 delete mLayer->meshIndices;
131
132 mLayer->mesh = NULL;
133 mLayer->meshIndices = NULL;
134 }
135
Romain Guy4f09f542011-01-26 22:41:43 -0800136 bool rebuildIndices = false;
Romain Guyf219da52011-01-16 12:54:25 -0800137 if (!mLayer->mesh) {
138 mLayer->mesh = new TextureVertex[count * 4];
139 mLayer->meshIndices = new uint16_t[elementCount];
Romain Guy4f09f542011-01-26 22:41:43 -0800140 rebuildIndices = true;
Romain Guyf219da52011-01-16 12:54:25 -0800141 }
Romain Guy4f09f542011-01-26 22:41:43 -0800142 mLayer->meshElementCount = elementCount;
Romain Guyf219da52011-01-16 12:54:25 -0800143
Romain Guy9ace8f52011-07-07 20:50:11 -0700144 const float texX = 1.0f / float(mLayer->getWidth());
145 const float texY = 1.0f / float(mLayer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800146 const float height = mLayer->layer.getHeight();
147
148 TextureVertex* mesh = mLayer->mesh;
149 uint16_t* indices = mLayer->meshIndices;
150
151 for (size_t i = 0; i < count; i++) {
152 const android::Rect* r = &rects[i];
153
154 const float u1 = r->left * texX;
155 const float v1 = (height - r->top) * texY;
156 const float u2 = r->right * texX;
157 const float v2 = (height - r->bottom) * texY;
158
159 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
160 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
161 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
162 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
163
Romain Guy4f09f542011-01-26 22:41:43 -0800164 if (rebuildIndices) {
165 uint16_t quad = i * 4;
166 int index = i * 6;
167 indices[index ] = quad; // top-left
168 indices[index + 1] = quad + 1; // top-right
169 indices[index + 2] = quad + 2; // bottom-left
170 indices[index + 3] = quad + 2; // bottom-left
171 indices[index + 4] = quad + 1; // top-right
172 indices[index + 5] = quad + 3; // bottom-right
173 }
Romain Guyf219da52011-01-16 12:54:25 -0800174 }
175#endif
176}
177
178///////////////////////////////////////////////////////////////////////////////
179// Layers management
Romain Guy6c319ca2011-01-11 14:29:25 -0800180///////////////////////////////////////////////////////////////////////////////
181
Romain Guyada830f2011-01-13 12:13:20 -0800182Layer* LayerRenderer::createLayer(uint32_t width, uint32_t height, bool isOpaque) {
Romain Guyeea60692011-07-26 20:35:55 -0700183 LAYER_RENDERER_LOGD("Requesting new render layer %dx%d", width, height);
Romain Guy1fc883b2011-01-12 14:30:59 -0800184
Romain Guy09b7c912011-02-02 20:28:09 -0800185 GLuint fbo = Caches::getInstance().fboCache.get();
186 if (!fbo) {
187 LOGW("Could not obtain an FBO");
188 return NULL;
189 }
190
191 glActiveTexture(GL_TEXTURE0);
192 Layer* layer = Caches::getInstance().layerCache.get(width, height);
193 if (!layer) {
194 LOGW("Could not obtain a layer");
195 return NULL;
196 }
197
Romain Guy9ace8f52011-07-07 20:50:11 -0700198 layer->setFbo(fbo);
Romain Guy09b7c912011-02-02 20:28:09 -0800199 layer->layer.set(0.0f, 0.0f, width, height);
Romain Guy9ace8f52011-07-07 20:50:11 -0700200 layer->texCoords.set(0.0f, height / float(layer->getHeight()),
201 width / float(layer->getWidth()), 0.0f);
202 layer->setAlpha(255, SkXfermode::kSrcOver_Mode);
203 layer->setBlend(!isOpaque);
204 layer->setColorFilter(NULL);
Romain Guy09b7c912011-02-02 20:28:09 -0800205 layer->region.clear();
Romain Guyada830f2011-01-13 12:13:20 -0800206
Romain Guy6c319ca2011-01-11 14:29:25 -0800207 GLuint previousFbo;
208 glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*) &previousFbo);
209
Romain Guy9ace8f52011-07-07 20:50:11 -0700210 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
211 layer->bindTexture();
Romain Guy6c319ca2011-01-11 14:29:25 -0800212
Romain Guy09b7c912011-02-02 20:28:09 -0800213 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700214 if (layer->isEmpty()) {
215 layer->setEmpty(false);
216 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
Romain Guy6c319ca2011-01-11 14:29:25 -0800217
Romain Guy09b7c912011-02-02 20:28:09 -0800218 if (glGetError() != GL_NO_ERROR) {
Romain Guy5cd5c3f2011-10-17 17:10:02 -0700219 LOGD("Could not allocate texture for layer (fbo=%d %dx%d)",
220 fbo, width, height);
Romain Guy9ace8f52011-07-07 20:50:11 -0700221
Romain Guy09b7c912011-02-02 20:28:09 -0800222 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guy09b7c912011-02-02 20:28:09 -0800223 Caches::getInstance().fboCache.put(fbo);
Romain Guy9ace8f52011-07-07 20:50:11 -0700224
225 layer->deleteTexture();
Romain Guy09b7c912011-02-02 20:28:09 -0800226 delete layer;
Romain Guy9ace8f52011-07-07 20:50:11 -0700227
Romain Guy09b7c912011-02-02 20:28:09 -0800228 return NULL;
229 }
Romain Guy6c319ca2011-01-11 14:29:25 -0800230 }
231
232 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700233 layer->getTexture(), 0);
Romain Guy6c319ca2011-01-11 14:29:25 -0800234
Romain Guy40a787f2011-03-02 15:15:42 -0800235 glDisable(GL_SCISSOR_TEST);
Romain Guy40a787f2011-03-02 15:15:42 -0800236 glClear(GL_COLOR_BUFFER_BIT);
237 glEnable(GL_SCISSOR_TEST);
238
Romain Guy6c319ca2011-01-11 14:29:25 -0800239 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
240
Romain Guyada830f2011-01-13 12:13:20 -0800241 return layer;
Romain Guy6c319ca2011-01-11 14:29:25 -0800242}
243
Romain Guyada830f2011-01-13 12:13:20 -0800244bool LayerRenderer::resizeLayer(Layer* layer, uint32_t width, uint32_t height) {
245 if (layer) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700246 LAYER_RENDERER_LOGD("Resizing layer fbo = %d to %dx%d", layer->getFbo(), width, height);
Romain Guy1fc883b2011-01-12 14:30:59 -0800247
Romain Guy09b7c912011-02-02 20:28:09 -0800248 if (Caches::getInstance().layerCache.resize(layer, width, height)) {
249 layer->layer.set(0.0f, 0.0f, width, height);
Romain Guy9ace8f52011-07-07 20:50:11 -0700250 layer->texCoords.set(0.0f, height / float(layer->getHeight()),
251 width / float(layer->getWidth()), 0.0f);
Romain Guy09b7c912011-02-02 20:28:09 -0800252 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700253 layer->deleteTexture();
Romain Guy09b7c912011-02-02 20:28:09 -0800254 delete layer;
Romain Guyada830f2011-01-13 12:13:20 -0800255 return false;
256 }
Romain Guy6c319ca2011-01-11 14:29:25 -0800257 }
Romain Guy09b7c912011-02-02 20:28:09 -0800258
Romain Guyada830f2011-01-13 12:13:20 -0800259 return true;
Romain Guy6c319ca2011-01-11 14:29:25 -0800260}
261
Romain Guy4a5a7152011-06-24 17:53:53 -0700262Layer* LayerRenderer::createTextureLayer(bool isOpaque) {
263 LAYER_RENDERER_LOGD("Creating new texture layer");
264
265 Layer* layer = new Layer(0, 0);
Romain Guy9ace8f52011-07-07 20:50:11 -0700266 layer->setCacheable(false);
267 layer->setTextureLayer(true);
268 layer->setBlend(!isOpaque);
269 layer->setEmpty(true);
270 layer->setFbo(0);
271 layer->setAlpha(255, SkXfermode::kSrcOver_Mode);
Romain Guy4a5a7152011-06-24 17:53:53 -0700272 layer->layer.set(0.0f, 0.0f, 0.0f, 0.0f);
Romain Guya9dc86b2011-10-11 14:06:21 -0700273 layer->texCoords.set(0.0f, 1.0f, 1.0f, 0.0f);
Romain Guy4a5a7152011-06-24 17:53:53 -0700274 layer->region.clear();
Romain Guy9ace8f52011-07-07 20:50:11 -0700275 layer->setRenderTarget(GL_NONE); // see ::updateTextureLayer()
Romain Guy4a5a7152011-06-24 17:53:53 -0700276
277 glActiveTexture(GL_TEXTURE0);
Romain Guy9ace8f52011-07-07 20:50:11 -0700278 layer->generateTexture();
Romain Guy4a5a7152011-06-24 17:53:53 -0700279
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 Guy9ace8f52011-07-07 20:50:11 -0700286 layer->setBlend(!isOpaque);
287 layer->setSize(width, height);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700288 layer->layer.set(0.0f, 0.0f, width, height);
289 layer->region.set(width, height);
290 layer->regionRect.set(0.0f, 0.0f, width, height);
Romain Guy9ace8f52011-07-07 20:50:11 -0700291 layer->getTexTransform().load(transform);
Romain Guy8f0095c2011-05-02 17:24:22 -0700292
Romain Guy9ace8f52011-07-07 20:50:11 -0700293 if (renderTarget != layer->getRenderTarget()) {
294 layer->setRenderTarget(renderTarget);
295 layer->bindTexture();
Romain Guyd21b6e12011-11-30 20:21:23 -0800296 layer->setFilter(GL_NEAREST, false, true);
297 layer->setWrap(GL_CLAMP_TO_EDGE, false, true);
Romain Guy4a5a7152011-06-24 17:53:53 -0700298 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700299 }
300}
301
Romain Guyada830f2011-01-13 12:13:20 -0800302void LayerRenderer::destroyLayer(Layer* layer) {
303 if (layer) {
Romain Guyeea60692011-07-26 20:35:55 -0700304 LAYER_RENDERER_LOGD("Recycling layer, %dx%d fbo = %d",
305 layer->getWidth(), layer->getHeight(), layer->getFbo());
Romain Guy1fc883b2011-01-12 14:30:59 -0800306
Romain Guy9c4b79a2011-11-10 19:23:58 -0800307 GLuint fbo = layer->getFbo();
308 if (fbo) {
309 flushLayer(layer);
310 Caches::getInstance().fboCache.put(fbo);
Romain Guy09b7c912011-02-02 20:28:09 -0800311 }
Romain Guyada830f2011-01-13 12:13:20 -0800312
Romain Guy09b7c912011-02-02 20:28:09 -0800313 if (!Caches::getInstance().layerCache.put(layer)) {
Romain Guyeea60692011-07-26 20:35:55 -0700314 LAYER_RENDERER_LOGD(" Destroyed!");
Romain Guy9ace8f52011-07-07 20:50:11 -0700315 layer->deleteTexture();
Romain Guy09b7c912011-02-02 20:28:09 -0800316 delete layer;
317 } else {
Romain Guyeea60692011-07-26 20:35:55 -0700318 LAYER_RENDERER_LOGD(" Cached!");
Romain Guy65b345f2011-07-27 18:51:50 -0700319#if DEBUG_LAYER_RENDERER
Romain Guyeea60692011-07-26 20:35:55 -0700320 Caches::getInstance().layerCache.dump();
321#endif
Romain Guy09b7c912011-02-02 20:28:09 -0800322 layer->region.clear();
323 }
Romain Guyada830f2011-01-13 12:13:20 -0800324 }
Romain Guy6c319ca2011-01-11 14:29:25 -0800325}
326
Romain Guyada830f2011-01-13 12:13:20 -0800327void LayerRenderer::destroyLayerDeferred(Layer* layer) {
328 if (layer) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700329 LAYER_RENDERER_LOGD("Deferring layer destruction, fbo = %d", layer->getFbo());
Romain Guy1fc883b2011-01-12 14:30:59 -0800330
Romain Guyada830f2011-01-13 12:13:20 -0800331 Caches::getInstance().deleteLayerDeferred(layer);
332 }
Romain Guy57066eb2011-01-12 12:53:32 -0800333}
334
Romain Guy9c4b79a2011-11-10 19:23:58 -0800335void LayerRenderer::flushLayer(Layer* layer) {
336#ifdef GL_EXT_discard_framebuffer
337 GLuint fbo = layer->getFbo();
338 if (layer && fbo) {
339 // If possible, discard any enqueud operations on deferred
340 // rendering architectures
341 if (Caches::getInstance().extensions.hasDiscardFramebuffer()) {
342 GLuint previousFbo;
343 glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*) &previousFbo);
344
345 GLenum attachments = GL_COLOR_ATTACHMENT0;
346 if (fbo != previousFbo) glBindFramebuffer(GL_FRAMEBUFFER, fbo);
347 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, &attachments);
348
349 if (fbo != previousFbo) glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
350 }
351 }
352#endif
353}
354
Romain Guy77a81162011-06-14 16:45:55 -0700355bool LayerRenderer::copyLayer(Layer* layer, SkBitmap* bitmap) {
356 Caches& caches = Caches::getInstance();
Romain Guy9ace8f52011-07-07 20:50:11 -0700357 if (layer && layer->isTextureLayer() && bitmap->width() <= caches.maxTextureSize &&
Romain Guy77a81162011-06-14 16:45:55 -0700358 bitmap->height() <= caches.maxTextureSize) {
359
360 GLuint fbo = caches.fboCache.get();
361 if (!fbo) {
362 LOGW("Could not obtain an FBO");
363 return false;
364 }
365
Romain Guyd6b2a002011-06-17 17:45:59 -0700366 SkAutoLockPixels alp(*bitmap);
367
Romain Guy77a81162011-06-14 16:45:55 -0700368 GLuint texture;
369 GLuint previousFbo;
370
371 GLenum format;
372 GLenum type;
373
Romain Guyd6b2a002011-06-17 17:45:59 -0700374 GLenum error = GL_NO_ERROR;
375 bool status = false;
376
Romain Guy77a81162011-06-14 16:45:55 -0700377 switch (bitmap->config()) {
378 case SkBitmap::kA8_Config:
379 format = GL_ALPHA;
380 type = GL_UNSIGNED_BYTE;
381 break;
382 case SkBitmap::kRGB_565_Config:
383 format = GL_RGB;
384 type = GL_UNSIGNED_SHORT_5_6_5;
385 break;
386 case SkBitmap::kARGB_4444_Config:
387 format = GL_RGBA;
388 type = GL_UNSIGNED_SHORT_4_4_4_4;
389 break;
390 case SkBitmap::kARGB_8888_Config:
391 default:
392 format = GL_RGBA;
393 type = GL_UNSIGNED_BYTE;
394 break;
395 }
396
Romain Guy9ace8f52011-07-07 20:50:11 -0700397 float alpha = layer->getAlpha();
398 SkXfermode::Mode mode = layer->getMode();
Romain Guyd6b2a002011-06-17 17:45:59 -0700399
Romain Guy9ace8f52011-07-07 20:50:11 -0700400 layer->setAlpha(255, SkXfermode::kSrc_Mode);
401 layer->setFbo(fbo);
Romain Guyd6b2a002011-06-17 17:45:59 -0700402
Romain Guy77a81162011-06-14 16:45:55 -0700403 glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*) &previousFbo);
404 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
405
406 glGenTextures(1, &texture);
Romain Guyd6b2a002011-06-17 17:45:59 -0700407 if ((error = glGetError()) != GL_NO_ERROR) goto error;
Romain Guy77a81162011-06-14 16:45:55 -0700408
409 glActiveTexture(GL_TEXTURE0);
410 glBindTexture(GL_TEXTURE_2D, texture);
411
Romain Guyec19b4a2011-07-07 21:05:04 -0700412 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
413 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
Romain Guy77a81162011-06-14 16:45:55 -0700414
415 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
416 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
417
418 glTexImage2D(GL_TEXTURE_2D, 0, format, bitmap->width(), bitmap->height(),
419 0, format, type, NULL);
Romain Guyd6b2a002011-06-17 17:45:59 -0700420 if ((error = glGetError()) != GL_NO_ERROR) goto error;
421
Romain Guy77a81162011-06-14 16:45:55 -0700422 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
423 GL_TEXTURE_2D, texture, 0);
Romain Guyd6b2a002011-06-17 17:45:59 -0700424 if ((error = glGetError()) != GL_NO_ERROR) goto error;
Romain Guy77a81162011-06-14 16:45:55 -0700425
Romain Guyd6b2a002011-06-17 17:45:59 -0700426 {
427 LayerRenderer renderer(layer);
428 renderer.setViewport(bitmap->width(), bitmap->height());
429 renderer.OpenGLRenderer::prepareDirty(0.0f, 0.0f,
Romain Guy9ace8f52011-07-07 20:50:11 -0700430 bitmap->width(), bitmap->height(), !layer->isBlend());
Romain Guya9dc86b2011-10-11 14:06:21 -0700431
432 glDisable(GL_SCISSOR_TEST);
433 renderer.translate(0.0f, bitmap->height());
434 renderer.scale(1.0f, -1.0f);
435
436 mat4 texTransform(layer->getTexTransform());
437
438 mat4 invert;
439 invert.translate(0.0f, 1.0f, 0.0f);
440 invert.scale(1.0f, -1.0f, 1.0f);
441 layer->getTexTransform().multiply(invert);
442
Romain Guyd6b2a002011-06-17 17:45:59 -0700443 if ((error = glGetError()) != GL_NO_ERROR) goto error;
Romain Guy77a81162011-06-14 16:45:55 -0700444
Romain Guyd6b2a002011-06-17 17:45:59 -0700445 {
446 Rect bounds;
447 bounds.set(0.0f, 0.0f, bitmap->width(), bitmap->height());
448 renderer.drawTextureLayer(layer, bounds);
Romain Guy77a81162011-06-14 16:45:55 -0700449
Romain Guyd6b2a002011-06-17 17:45:59 -0700450 glReadPixels(0, 0, bitmap->width(), bitmap->height(), format,
451 type, bitmap->getPixels());
Romain Guy77a81162011-06-14 16:45:55 -0700452
Romain Guyd6b2a002011-06-17 17:45:59 -0700453 if ((error = glGetError()) != GL_NO_ERROR) goto error;
454 }
Romain Guy77a81162011-06-14 16:45:55 -0700455
Romain Guya9dc86b2011-10-11 14:06:21 -0700456 layer->getTexTransform().load(texTransform);
Romain Guyd6b2a002011-06-17 17:45:59 -0700457 status = true;
458 }
Romain Guy77a81162011-06-14 16:45:55 -0700459
Romain Guyd6b2a002011-06-17 17:45:59 -0700460error:
Romain Guy39d252a2011-12-12 18:14:06 -0800461 glEnable(GL_SCISSOR_TEST);
462
Romain Guyd6b2a002011-06-17 17:45:59 -0700463#if DEBUG_OPENGL
464 if (error != GL_NO_ERROR) {
465 LOGD("GL error while copying layer into bitmap = 0x%x", error);
466 }
467#endif
Romain Guy77a81162011-06-14 16:45:55 -0700468
469 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guy9ace8f52011-07-07 20:50:11 -0700470 layer->setAlpha(alpha, mode);
471 layer->setFbo(0);
Romain Guy77a81162011-06-14 16:45:55 -0700472 glDeleteTextures(1, &texture);
473 caches.fboCache.put(fbo);
474
Romain Guyd6b2a002011-06-17 17:45:59 -0700475 return status;
Romain Guy77a81162011-06-14 16:45:55 -0700476 }
477 return false;
478}
479
Romain Guy6c319ca2011-01-11 14:29:25 -0800480}; // namespace uirenderer
481}; // namespace android