blob: 5d59a4cc3a1acfac134d8c6f0b194dbb98bc89fd [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 Guy35643dd2012-09-18 15:40:58 -070040void LayerRenderer::setViewport(int width, int height) {
41 initViewport(width, height);
42}
43
Chet Haase44b2fe32012-06-06 19:03:58 -070044int LayerRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) {
Romain Guy9ace8f52011-07-07 20:50:11 -070045 LAYER_RENDERER_LOGD("Rendering into layer, fbo = %d", mLayer->getFbo());
Romain Guy1fc883b2011-01-12 14:30:59 -080046
Romain Guy9ace8f52011-07-07 20:50:11 -070047 glBindFramebuffer(GL_FRAMEBUFFER, mLayer->getFbo());
Romain Guy62687ec2011-02-02 15:44:19 -080048
Romain Guy09b7c912011-02-02 20:28:09 -080049 const float width = mLayer->layer.getWidth();
50 const float height = mLayer->layer.getHeight();
51
Romain Guy3a3133d2011-02-01 22:59:58 -080052 Rect dirty(left, top, right, bottom);
53 if (dirty.isEmpty() || (dirty.left <= 0 && dirty.top <= 0 &&
Romain Guy09b7c912011-02-02 20:28:09 -080054 dirty.right >= width && dirty.bottom >= height)) {
Romain Guy3a3133d2011-02-01 22:59:58 -080055 mLayer->region.clear();
Romain Guy09b7c912011-02-02 20:28:09 -080056 dirty.set(0.0f, 0.0f, width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -080057 } else {
Romain Guy09b7c912011-02-02 20:28:09 -080058 dirty.intersect(0.0f, 0.0f, width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -080059 android::Rect r(dirty.left, dirty.top, dirty.right, dirty.bottom);
60 mLayer->region.subtractSelf(r);
61 }
Romain Guyc88e3572011-01-22 00:32:12 -080062
Chet Haase44b2fe32012-06-06 19:03:58 -070063 return OpenGLRenderer::prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, opaque);
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() {
Romain Guyf219da52011-01-16 12:54:25 -080090 if (getSnapshot()->flags & Snapshot::kFlagFboTarget) {
91 return OpenGLRenderer::getRegion();
92 }
93 return &mLayer->region;
Romain Guyf219da52011-01-16 12:54:25 -080094}
95
Romain Guy8a3957d2011-09-07 17:55:15 -070096// TODO: This implementation is flawed and can generate T-junctions
97// in the mesh, which will in turn produce cracks when the
98// mesh is rotated/skewed. The easiest way to fix this would
99// be, for each row, to add new vertices shared with the previous
100// row when the two rows share an edge.
101// In practice, T-junctions do not appear often so this has yet
102// to be fixed.
Romain Guyf219da52011-01-16 12:54:25 -0800103void LayerRenderer::generateMesh() {
Romain Guyf219da52011-01-16 12:54:25 -0800104 if (mLayer->region.isRect() || mLayer->region.isEmpty()) {
105 if (mLayer->mesh) {
106 delete mLayer->mesh;
107 delete mLayer->meshIndices;
108
109 mLayer->mesh = NULL;
110 mLayer->meshIndices = NULL;
111 mLayer->meshElementCount = 0;
112 }
Romain Guy40667672011-03-18 14:34:03 -0700113
Romain Guy9fc27812011-04-27 14:21:41 -0700114 mLayer->setRegionAsRect();
Romain Guyf219da52011-01-16 12:54:25 -0800115 return;
116 }
117
118 size_t count;
119 const android::Rect* rects = mLayer->region.getArray(&count);
120
121 GLsizei elementCount = count * 6;
122
123 if (mLayer->mesh && mLayer->meshElementCount < elementCount) {
124 delete mLayer->mesh;
125 delete mLayer->meshIndices;
126
127 mLayer->mesh = NULL;
128 mLayer->meshIndices = NULL;
129 }
130
Romain Guy4f09f542011-01-26 22:41:43 -0800131 bool rebuildIndices = false;
Romain Guyf219da52011-01-16 12:54:25 -0800132 if (!mLayer->mesh) {
133 mLayer->mesh = new TextureVertex[count * 4];
134 mLayer->meshIndices = new uint16_t[elementCount];
Romain Guy4f09f542011-01-26 22:41:43 -0800135 rebuildIndices = true;
Romain Guyf219da52011-01-16 12:54:25 -0800136 }
Romain Guy4f09f542011-01-26 22:41:43 -0800137 mLayer->meshElementCount = elementCount;
Romain Guyf219da52011-01-16 12:54:25 -0800138
Romain Guy9ace8f52011-07-07 20:50:11 -0700139 const float texX = 1.0f / float(mLayer->getWidth());
140 const float texY = 1.0f / float(mLayer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800141 const float height = mLayer->layer.getHeight();
142
143 TextureVertex* mesh = mLayer->mesh;
144 uint16_t* indices = mLayer->meshIndices;
145
146 for (size_t i = 0; i < count; i++) {
147 const android::Rect* r = &rects[i];
148
149 const float u1 = r->left * texX;
150 const float v1 = (height - r->top) * texY;
151 const float u2 = r->right * texX;
152 const float v2 = (height - r->bottom) * texY;
153
154 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
155 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
156 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
157 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
158
Romain Guy4f09f542011-01-26 22:41:43 -0800159 if (rebuildIndices) {
160 uint16_t quad = i * 4;
161 int index = i * 6;
162 indices[index ] = quad; // top-left
163 indices[index + 1] = quad + 1; // top-right
164 indices[index + 2] = quad + 2; // bottom-left
165 indices[index + 3] = quad + 2; // bottom-left
166 indices[index + 4] = quad + 1; // top-right
167 indices[index + 5] = quad + 3; // bottom-right
168 }
Romain Guyf219da52011-01-16 12:54:25 -0800169 }
Romain Guyf219da52011-01-16 12:54:25 -0800170}
171
172///////////////////////////////////////////////////////////////////////////////
173// Layers management
Romain Guy6c319ca2011-01-11 14:29:25 -0800174///////////////////////////////////////////////////////////////////////////////
175
Romain Guyada830f2011-01-13 12:13:20 -0800176Layer* LayerRenderer::createLayer(uint32_t width, uint32_t height, bool isOpaque) {
Romain Guyeea60692011-07-26 20:35:55 -0700177 LAYER_RENDERER_LOGD("Requesting new render layer %dx%d", width, height);
Romain Guy1fc883b2011-01-12 14:30:59 -0800178
Romain Guya1d3c912011-12-13 14:55:06 -0800179 Caches& caches = Caches::getInstance();
180 GLuint fbo = caches.fboCache.get();
Romain Guy09b7c912011-02-02 20:28:09 -0800181 if (!fbo) {
Steve Block8564c8d2012-01-05 23:22:43 +0000182 ALOGW("Could not obtain an FBO");
Romain Guy09b7c912011-02-02 20:28:09 -0800183 return NULL;
184 }
185
Romain Guya1d3c912011-12-13 14:55:06 -0800186 caches.activeTexture(0);
187 Layer* layer = caches.layerCache.get(width, height);
Romain Guy09b7c912011-02-02 20:28:09 -0800188 if (!layer) {
Steve Block8564c8d2012-01-05 23:22:43 +0000189 ALOGW("Could not obtain a layer");
Romain Guy09b7c912011-02-02 20:28:09 -0800190 return NULL;
191 }
192
Romain Guy9ace8f52011-07-07 20:50:11 -0700193 layer->setFbo(fbo);
Romain Guy09b7c912011-02-02 20:28:09 -0800194 layer->layer.set(0.0f, 0.0f, width, height);
Romain Guy9ace8f52011-07-07 20:50:11 -0700195 layer->texCoords.set(0.0f, height / float(layer->getHeight()),
196 width / float(layer->getWidth()), 0.0f);
197 layer->setAlpha(255, SkXfermode::kSrcOver_Mode);
198 layer->setBlend(!isOpaque);
199 layer->setColorFilter(NULL);
Romain Guy09b7c912011-02-02 20:28:09 -0800200 layer->region.clear();
Romain Guyada830f2011-01-13 12:13:20 -0800201
Romain Guy6c319ca2011-01-11 14:29:25 -0800202 GLuint previousFbo;
203 glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*) &previousFbo);
204
Romain Guy9ace8f52011-07-07 20:50:11 -0700205 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
206 layer->bindTexture();
Romain Guy6c319ca2011-01-11 14:29:25 -0800207
Romain Guy09b7c912011-02-02 20:28:09 -0800208 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700209 if (layer->isEmpty()) {
210 layer->setEmpty(false);
211 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
Romain Guy6c319ca2011-01-11 14:29:25 -0800212
Romain Guy09b7c912011-02-02 20:28:09 -0800213 if (glGetError() != GL_NO_ERROR) {
Steve Block5baa3a62011-12-20 16:23:08 +0000214 ALOGD("Could not allocate texture for layer (fbo=%d %dx%d)",
Romain Guy5cd5c3f2011-10-17 17:10:02 -0700215 fbo, width, height);
Romain Guy9ace8f52011-07-07 20:50:11 -0700216
Romain Guy09b7c912011-02-02 20:28:09 -0800217 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guy9ace8f52011-07-07 20:50:11 -0700218
Chet Haase603f6de2012-09-14 15:31:25 -0700219 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy9ace8f52011-07-07 20:50:11 -0700220
Romain Guy09b7c912011-02-02 20:28:09 -0800221 return NULL;
222 }
Romain Guy6c319ca2011-01-11 14:29:25 -0800223 }
224
225 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700226 layer->getTexture(), 0);
Romain Guy6c319ca2011-01-11 14:29:25 -0800227
Romain Guy586cae32012-07-13 15:28:31 -0700228 caches.disableScissor();
Romain Guy40a787f2011-03-02 15:15:42 -0800229 glClear(GL_COLOR_BUFFER_BIT);
Romain Guy40a787f2011-03-02 15:15:42 -0800230
Romain Guy6c319ca2011-01-11 14:29:25 -0800231 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
232
Romain Guyada830f2011-01-13 12:13:20 -0800233 return layer;
Romain Guy6c319ca2011-01-11 14:29:25 -0800234}
235
Romain Guyada830f2011-01-13 12:13:20 -0800236bool LayerRenderer::resizeLayer(Layer* layer, uint32_t width, uint32_t height) {
237 if (layer) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700238 LAYER_RENDERER_LOGD("Resizing layer fbo = %d to %dx%d", layer->getFbo(), width, height);
Romain Guy1fc883b2011-01-12 14:30:59 -0800239
Romain Guy09b7c912011-02-02 20:28:09 -0800240 if (Caches::getInstance().layerCache.resize(layer, width, height)) {
241 layer->layer.set(0.0f, 0.0f, width, height);
Romain Guy9ace8f52011-07-07 20:50:11 -0700242 layer->texCoords.set(0.0f, height / float(layer->getHeight()),
243 width / float(layer->getWidth()), 0.0f);
Romain Guy09b7c912011-02-02 20:28:09 -0800244 } else {
Chet Haase603f6de2012-09-14 15:31:25 -0700245 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guyada830f2011-01-13 12:13:20 -0800246 return false;
247 }
Romain Guy6c319ca2011-01-11 14:29:25 -0800248 }
Romain Guy09b7c912011-02-02 20:28:09 -0800249
Romain Guyada830f2011-01-13 12:13:20 -0800250 return true;
Romain Guy6c319ca2011-01-11 14:29:25 -0800251}
252
Romain Guy4a5a7152011-06-24 17:53:53 -0700253Layer* LayerRenderer::createTextureLayer(bool isOpaque) {
254 LAYER_RENDERER_LOGD("Creating new texture layer");
255
256 Layer* layer = new Layer(0, 0);
Romain Guy9ace8f52011-07-07 20:50:11 -0700257 layer->setCacheable(false);
258 layer->setTextureLayer(true);
259 layer->setBlend(!isOpaque);
260 layer->setEmpty(true);
261 layer->setFbo(0);
262 layer->setAlpha(255, SkXfermode::kSrcOver_Mode);
Romain Guy4a5a7152011-06-24 17:53:53 -0700263 layer->layer.set(0.0f, 0.0f, 0.0f, 0.0f);
Romain Guya9dc86b2011-10-11 14:06:21 -0700264 layer->texCoords.set(0.0f, 1.0f, 1.0f, 0.0f);
Romain Guy4a5a7152011-06-24 17:53:53 -0700265 layer->region.clear();
Romain Guy9ace8f52011-07-07 20:50:11 -0700266 layer->setRenderTarget(GL_NONE); // see ::updateTextureLayer()
Romain Guy4a5a7152011-06-24 17:53:53 -0700267
Romain Guya1d3c912011-12-13 14:55:06 -0800268 Caches::getInstance().activeTexture(0);
Romain Guy9ace8f52011-07-07 20:50:11 -0700269 layer->generateTexture();
Romain Guy4a5a7152011-06-24 17:53:53 -0700270
271 return layer;
272}
273
Romain Guyaa6c24c2011-04-28 18:40:04 -0700274void LayerRenderer::updateTextureLayer(Layer* layer, uint32_t width, uint32_t height,
Romain Guya9489272011-06-22 20:58:11 -0700275 bool isOpaque, GLenum renderTarget, float* transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700276 if (layer) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700277 layer->setBlend(!isOpaque);
278 layer->setSize(width, height);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700279 layer->layer.set(0.0f, 0.0f, width, height);
280 layer->region.set(width, height);
281 layer->regionRect.set(0.0f, 0.0f, width, height);
Romain Guy9ace8f52011-07-07 20:50:11 -0700282 layer->getTexTransform().load(transform);
Romain Guy8f0095c2011-05-02 17:24:22 -0700283
Romain Guy9ace8f52011-07-07 20:50:11 -0700284 if (renderTarget != layer->getRenderTarget()) {
285 layer->setRenderTarget(renderTarget);
286 layer->bindTexture();
Romain Guyd21b6e12011-11-30 20:21:23 -0800287 layer->setFilter(GL_NEAREST, false, true);
288 layer->setWrap(GL_CLAMP_TO_EDGE, false, true);
Romain Guy4a5a7152011-06-24 17:53:53 -0700289 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700290 }
291}
292
Romain Guyada830f2011-01-13 12:13:20 -0800293void LayerRenderer::destroyLayer(Layer* layer) {
294 if (layer) {
Romain Guyeea60692011-07-26 20:35:55 -0700295 LAYER_RENDERER_LOGD("Recycling layer, %dx%d fbo = %d",
296 layer->getWidth(), layer->getHeight(), layer->getFbo());
Romain Guy1fc883b2011-01-12 14:30:59 -0800297
Romain Guy9c4b79a2011-11-10 19:23:58 -0800298 GLuint fbo = layer->getFbo();
299 if (fbo) {
300 flushLayer(layer);
301 Caches::getInstance().fboCache.put(fbo);
Romain Guy5c88fc72012-04-02 17:43:05 -0700302 layer->setFbo(0);
Romain Guy09b7c912011-02-02 20:28:09 -0800303 }
Romain Guyada830f2011-01-13 12:13:20 -0800304
Romain Guy09b7c912011-02-02 20:28:09 -0800305 if (!Caches::getInstance().layerCache.put(layer)) {
Romain Guyeea60692011-07-26 20:35:55 -0700306 LAYER_RENDERER_LOGD(" Destroyed!");
Chet Haase603f6de2012-09-14 15:31:25 -0700307 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy09b7c912011-02-02 20:28:09 -0800308 } else {
Romain Guyeea60692011-07-26 20:35:55 -0700309 LAYER_RENDERER_LOGD(" Cached!");
Romain Guy65b345f2011-07-27 18:51:50 -0700310#if DEBUG_LAYER_RENDERER
Romain Guyeea60692011-07-26 20:35:55 -0700311 Caches::getInstance().layerCache.dump();
312#endif
Romain Guy09b7c912011-02-02 20:28:09 -0800313 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) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700320 LAYER_RENDERER_LOGD("Deferring layer destruction, fbo = %d", layer->getFbo());
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 Guy9c4b79a2011-11-10 19:23:58 -0800326void LayerRenderer::flushLayer(Layer* layer) {
327#ifdef GL_EXT_discard_framebuffer
328 GLuint fbo = layer->getFbo();
329 if (layer && fbo) {
330 // If possible, discard any enqueud operations on deferred
331 // rendering architectures
332 if (Caches::getInstance().extensions.hasDiscardFramebuffer()) {
333 GLuint previousFbo;
334 glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*) &previousFbo);
Romain Guy9c4b79a2011-11-10 19:23:58 -0800335 if (fbo != previousFbo) glBindFramebuffer(GL_FRAMEBUFFER, fbo);
Romain Guy45e4c3d2012-09-11 17:17:07 -0700336
337 const GLenum attachments[] = { GL_COLOR_ATTACHMENT0 };
338 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
Romain Guy9c4b79a2011-11-10 19:23:58 -0800339
340 if (fbo != previousFbo) glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
341 }
342 }
343#endif
344}
345
Romain Guy77a81162011-06-14 16:45:55 -0700346bool LayerRenderer::copyLayer(Layer* layer, SkBitmap* bitmap) {
347 Caches& caches = Caches::getInstance();
Romain Guy9ace8f52011-07-07 20:50:11 -0700348 if (layer && layer->isTextureLayer() && bitmap->width() <= caches.maxTextureSize &&
Romain Guy77a81162011-06-14 16:45:55 -0700349 bitmap->height() <= caches.maxTextureSize) {
350
351 GLuint fbo = caches.fboCache.get();
352 if (!fbo) {
Steve Block8564c8d2012-01-05 23:22:43 +0000353 ALOGW("Could not obtain an FBO");
Romain Guy77a81162011-06-14 16:45:55 -0700354 return false;
355 }
356
Romain Guyd6b2a002011-06-17 17:45:59 -0700357 SkAutoLockPixels alp(*bitmap);
358
Romain Guy77a81162011-06-14 16:45:55 -0700359 GLuint texture;
360 GLuint previousFbo;
361
362 GLenum format;
363 GLenum type;
364
Romain Guyd6b2a002011-06-17 17:45:59 -0700365 GLenum error = GL_NO_ERROR;
366 bool status = false;
367
Romain Guy77a81162011-06-14 16:45:55 -0700368 switch (bitmap->config()) {
369 case SkBitmap::kA8_Config:
370 format = GL_ALPHA;
371 type = GL_UNSIGNED_BYTE;
372 break;
373 case SkBitmap::kRGB_565_Config:
374 format = GL_RGB;
375 type = GL_UNSIGNED_SHORT_5_6_5;
376 break;
377 case SkBitmap::kARGB_4444_Config:
378 format = GL_RGBA;
379 type = GL_UNSIGNED_SHORT_4_4_4_4;
380 break;
381 case SkBitmap::kARGB_8888_Config:
382 default:
383 format = GL_RGBA;
384 type = GL_UNSIGNED_BYTE;
385 break;
386 }
387
Romain Guy9ace8f52011-07-07 20:50:11 -0700388 float alpha = layer->getAlpha();
389 SkXfermode::Mode mode = layer->getMode();
Romain Guyd6b2a002011-06-17 17:45:59 -0700390
Romain Guy9ace8f52011-07-07 20:50:11 -0700391 layer->setAlpha(255, SkXfermode::kSrc_Mode);
392 layer->setFbo(fbo);
Romain Guyd6b2a002011-06-17 17:45:59 -0700393
Romain Guy77a81162011-06-14 16:45:55 -0700394 glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*) &previousFbo);
395 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
396
397 glGenTextures(1, &texture);
Romain Guyd6b2a002011-06-17 17:45:59 -0700398 if ((error = glGetError()) != GL_NO_ERROR) goto error;
Romain Guy77a81162011-06-14 16:45:55 -0700399
Romain Guya1d3c912011-12-13 14:55:06 -0800400 caches.activeTexture(0);
Romain Guy77a81162011-06-14 16:45:55 -0700401 glBindTexture(GL_TEXTURE_2D, texture);
402
Romain Guye49d7ec2012-09-07 18:42:38 -0700403 glPixelStorei(GL_PACK_ALIGNMENT, bitmap->bytesPerPixel());
404
Romain Guyec19b4a2011-07-07 21:05:04 -0700405 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
406 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
Romain Guy77a81162011-06-14 16:45:55 -0700407
408 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
409 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
410
411 glTexImage2D(GL_TEXTURE_2D, 0, format, bitmap->width(), bitmap->height(),
412 0, format, type, NULL);
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 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
416 GL_TEXTURE_2D, texture, 0);
Romain Guyd6b2a002011-06-17 17:45:59 -0700417 if ((error = glGetError()) != GL_NO_ERROR) goto error;
Romain Guy77a81162011-06-14 16:45:55 -0700418
Romain Guyd6b2a002011-06-17 17:45:59 -0700419 {
420 LayerRenderer renderer(layer);
421 renderer.setViewport(bitmap->width(), bitmap->height());
422 renderer.OpenGLRenderer::prepareDirty(0.0f, 0.0f,
Romain Guy9ace8f52011-07-07 20:50:11 -0700423 bitmap->width(), bitmap->height(), !layer->isBlend());
Romain Guya9dc86b2011-10-11 14:06:21 -0700424
Romain Guy586cae32012-07-13 15:28:31 -0700425 caches.disableScissor();
Romain Guya9dc86b2011-10-11 14:06:21 -0700426 renderer.translate(0.0f, bitmap->height());
427 renderer.scale(1.0f, -1.0f);
428
429 mat4 texTransform(layer->getTexTransform());
430
431 mat4 invert;
432 invert.translate(0.0f, 1.0f, 0.0f);
433 invert.scale(1.0f, -1.0f, 1.0f);
434 layer->getTexTransform().multiply(invert);
435
Romain Guyd6b2a002011-06-17 17:45:59 -0700436 if ((error = glGetError()) != GL_NO_ERROR) goto error;
Romain Guy77a81162011-06-14 16:45:55 -0700437
Romain Guyd6b2a002011-06-17 17:45:59 -0700438 {
439 Rect bounds;
440 bounds.set(0.0f, 0.0f, bitmap->width(), bitmap->height());
441 renderer.drawTextureLayer(layer, bounds);
Romain Guy77a81162011-06-14 16:45:55 -0700442
Romain Guyd6b2a002011-06-17 17:45:59 -0700443 glReadPixels(0, 0, bitmap->width(), bitmap->height(), format,
444 type, bitmap->getPixels());
Romain Guy77a81162011-06-14 16:45:55 -0700445
Romain Guyd6b2a002011-06-17 17:45:59 -0700446 if ((error = glGetError()) != GL_NO_ERROR) goto error;
447 }
Romain Guy77a81162011-06-14 16:45:55 -0700448
Romain Guya9dc86b2011-10-11 14:06:21 -0700449 layer->getTexTransform().load(texTransform);
Romain Guyd6b2a002011-06-17 17:45:59 -0700450 status = true;
451 }
Romain Guy77a81162011-06-14 16:45:55 -0700452
Romain Guyd6b2a002011-06-17 17:45:59 -0700453error:
454#if DEBUG_OPENGL
455 if (error != GL_NO_ERROR) {
Steve Block5baa3a62011-12-20 16:23:08 +0000456 ALOGD("GL error while copying layer into bitmap = 0x%x", error);
Romain Guyd6b2a002011-06-17 17:45:59 -0700457 }
458#endif
Romain Guy77a81162011-06-14 16:45:55 -0700459
460 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guy9ace8f52011-07-07 20:50:11 -0700461 layer->setAlpha(alpha, mode);
462 layer->setFbo(0);
Romain Guy77a81162011-06-14 16:45:55 -0700463 glDeleteTextures(1, &texture);
464 caches.fboCache.put(fbo);
465
Romain Guyd6b2a002011-06-17 17:45:59 -0700466 return status;
Romain Guy77a81162011-06-14 16:45:55 -0700467 }
468 return false;
469}
470
Romain Guy6c319ca2011-01-11 14:29:25 -0800471}; // namespace uirenderer
472}; // namespace android