blob: f2e7f66ac50363919a83a65af4f28301ae2a770b [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
Romain Guy11cb6422012-09-21 00:39:43 -070081bool LayerRenderer::suppressErrorChecks() {
82 return true;
83}
84
Romain Guy6c319ca2011-01-11 14:29:25 -080085///////////////////////////////////////////////////////////////////////////////
Romain Guyf219da52011-01-16 12:54:25 -080086// Dirty region tracking
87///////////////////////////////////////////////////////////////////////////////
88
89bool LayerRenderer::hasLayer() {
90 return true;
91}
92
93Region* LayerRenderer::getRegion() {
Romain Guyf219da52011-01-16 12:54:25 -080094 if (getSnapshot()->flags & Snapshot::kFlagFboTarget) {
95 return OpenGLRenderer::getRegion();
96 }
97 return &mLayer->region;
Romain Guyf219da52011-01-16 12:54:25 -080098}
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() {
Romain Guyf219da52011-01-16 12:54:25 -0800108 if (mLayer->region.isRect() || mLayer->region.isEmpty()) {
109 if (mLayer->mesh) {
110 delete mLayer->mesh;
111 delete mLayer->meshIndices;
112
113 mLayer->mesh = NULL;
114 mLayer->meshIndices = NULL;
115 mLayer->meshElementCount = 0;
116 }
Romain Guy40667672011-03-18 14:34:03 -0700117
Romain Guy9fc27812011-04-27 14:21:41 -0700118 mLayer->setRegionAsRect();
Romain Guyf219da52011-01-16 12:54:25 -0800119 return;
120 }
121
122 size_t count;
123 const android::Rect* rects = mLayer->region.getArray(&count);
124
125 GLsizei elementCount = count * 6;
126
127 if (mLayer->mesh && mLayer->meshElementCount < elementCount) {
128 delete mLayer->mesh;
129 delete mLayer->meshIndices;
130
131 mLayer->mesh = NULL;
132 mLayer->meshIndices = NULL;
133 }
134
Romain Guy4f09f542011-01-26 22:41:43 -0800135 bool rebuildIndices = false;
Romain Guyf219da52011-01-16 12:54:25 -0800136 if (!mLayer->mesh) {
137 mLayer->mesh = new TextureVertex[count * 4];
138 mLayer->meshIndices = new uint16_t[elementCount];
Romain Guy4f09f542011-01-26 22:41:43 -0800139 rebuildIndices = true;
Romain Guyf219da52011-01-16 12:54:25 -0800140 }
Romain Guy4f09f542011-01-26 22:41:43 -0800141 mLayer->meshElementCount = elementCount;
Romain Guyf219da52011-01-16 12:54:25 -0800142
Romain Guy9ace8f52011-07-07 20:50:11 -0700143 const float texX = 1.0f / float(mLayer->getWidth());
144 const float texY = 1.0f / float(mLayer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800145 const float height = mLayer->layer.getHeight();
146
147 TextureVertex* mesh = mLayer->mesh;
148 uint16_t* indices = mLayer->meshIndices;
149
150 for (size_t i = 0; i < count; i++) {
151 const android::Rect* r = &rects[i];
152
153 const float u1 = r->left * texX;
154 const float v1 = (height - r->top) * texY;
155 const float u2 = r->right * texX;
156 const float v2 = (height - r->bottom) * texY;
157
158 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
159 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
160 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
161 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
162
Romain Guy4f09f542011-01-26 22:41:43 -0800163 if (rebuildIndices) {
164 uint16_t quad = i * 4;
165 int index = i * 6;
166 indices[index ] = quad; // top-left
167 indices[index + 1] = quad + 1; // top-right
168 indices[index + 2] = quad + 2; // bottom-left
169 indices[index + 3] = quad + 2; // bottom-left
170 indices[index + 4] = quad + 1; // top-right
171 indices[index + 5] = quad + 3; // bottom-right
172 }
Romain Guyf219da52011-01-16 12:54:25 -0800173 }
Romain Guyf219da52011-01-16 12:54:25 -0800174}
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 Guya1d3c912011-12-13 14:55:06 -0800183 Caches& caches = Caches::getInstance();
184 GLuint fbo = caches.fboCache.get();
Romain Guy09b7c912011-02-02 20:28:09 -0800185 if (!fbo) {
Steve Block8564c8d2012-01-05 23:22:43 +0000186 ALOGW("Could not obtain an FBO");
Romain Guy09b7c912011-02-02 20:28:09 -0800187 return NULL;
188 }
189
Romain Guya1d3c912011-12-13 14:55:06 -0800190 caches.activeTexture(0);
191 Layer* layer = caches.layerCache.get(width, height);
Romain Guy09b7c912011-02-02 20:28:09 -0800192 if (!layer) {
Steve Block8564c8d2012-01-05 23:22:43 +0000193 ALOGW("Could not obtain a layer");
Romain Guy09b7c912011-02-02 20:28:09 -0800194 return NULL;
195 }
196
Romain Guy9ace8f52011-07-07 20:50:11 -0700197 layer->setFbo(fbo);
Romain Guy09b7c912011-02-02 20:28:09 -0800198 layer->layer.set(0.0f, 0.0f, width, height);
Romain Guy9ace8f52011-07-07 20:50:11 -0700199 layer->texCoords.set(0.0f, height / float(layer->getHeight()),
200 width / float(layer->getWidth()), 0.0f);
201 layer->setAlpha(255, SkXfermode::kSrcOver_Mode);
202 layer->setBlend(!isOpaque);
203 layer->setColorFilter(NULL);
Romain Guy09b7c912011-02-02 20:28:09 -0800204 layer->region.clear();
Romain Guyada830f2011-01-13 12:13:20 -0800205
Romain Guy6c319ca2011-01-11 14:29:25 -0800206 GLuint previousFbo;
207 glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*) &previousFbo);
208
Romain Guy9ace8f52011-07-07 20:50:11 -0700209 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
210 layer->bindTexture();
Romain Guy6c319ca2011-01-11 14:29:25 -0800211
Romain Guy09b7c912011-02-02 20:28:09 -0800212 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700213 if (layer->isEmpty()) {
214 layer->setEmpty(false);
215 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
Romain Guy6c319ca2011-01-11 14:29:25 -0800216
Romain Guy09b7c912011-02-02 20:28:09 -0800217 if (glGetError() != GL_NO_ERROR) {
Steve Block5baa3a62011-12-20 16:23:08 +0000218 ALOGD("Could not allocate texture for layer (fbo=%d %dx%d)",
Romain Guy5cd5c3f2011-10-17 17:10:02 -0700219 fbo, width, height);
Romain Guy9ace8f52011-07-07 20:50:11 -0700220
Romain Guy09b7c912011-02-02 20:28:09 -0800221 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guy9ace8f52011-07-07 20:50:11 -0700222
Chet Haase603f6de2012-09-14 15:31:25 -0700223 Caches::getInstance().resourceCache.decrementRefcount(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 Guy586cae32012-07-13 15:28:31 -0700232 caches.disableScissor();
Romain Guy40a787f2011-03-02 15:15:42 -0800233 glClear(GL_COLOR_BUFFER_BIT);
Romain Guy40a787f2011-03-02 15:15:42 -0800234
Romain Guy6c319ca2011-01-11 14:29:25 -0800235 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
236
Romain Guyada830f2011-01-13 12:13:20 -0800237 return layer;
Romain Guy6c319ca2011-01-11 14:29:25 -0800238}
239
Romain Guyada830f2011-01-13 12:13:20 -0800240bool LayerRenderer::resizeLayer(Layer* layer, uint32_t width, uint32_t height) {
241 if (layer) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700242 LAYER_RENDERER_LOGD("Resizing layer fbo = %d to %dx%d", layer->getFbo(), width, height);
Romain Guy1fc883b2011-01-12 14:30:59 -0800243
Romain Guy09b7c912011-02-02 20:28:09 -0800244 if (Caches::getInstance().layerCache.resize(layer, width, height)) {
245 layer->layer.set(0.0f, 0.0f, width, height);
Romain Guy9ace8f52011-07-07 20:50:11 -0700246 layer->texCoords.set(0.0f, height / float(layer->getHeight()),
247 width / float(layer->getWidth()), 0.0f);
Romain Guy09b7c912011-02-02 20:28:09 -0800248 } else {
Chet Haase603f6de2012-09-14 15:31:25 -0700249 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guyada830f2011-01-13 12:13:20 -0800250 return false;
251 }
Romain Guy6c319ca2011-01-11 14:29:25 -0800252 }
Romain Guy09b7c912011-02-02 20:28:09 -0800253
Romain Guyada830f2011-01-13 12:13:20 -0800254 return true;
Romain Guy6c319ca2011-01-11 14:29:25 -0800255}
256
Romain Guy4a5a7152011-06-24 17:53:53 -0700257Layer* LayerRenderer::createTextureLayer(bool isOpaque) {
258 LAYER_RENDERER_LOGD("Creating new texture layer");
259
260 Layer* layer = new Layer(0, 0);
Romain Guy9ace8f52011-07-07 20:50:11 -0700261 layer->setCacheable(false);
262 layer->setTextureLayer(true);
263 layer->setBlend(!isOpaque);
264 layer->setEmpty(true);
265 layer->setFbo(0);
266 layer->setAlpha(255, SkXfermode::kSrcOver_Mode);
Romain Guy4a5a7152011-06-24 17:53:53 -0700267 layer->layer.set(0.0f, 0.0f, 0.0f, 0.0f);
Romain Guya9dc86b2011-10-11 14:06:21 -0700268 layer->texCoords.set(0.0f, 1.0f, 1.0f, 0.0f);
Romain Guy4a5a7152011-06-24 17:53:53 -0700269 layer->region.clear();
Romain Guy9ace8f52011-07-07 20:50:11 -0700270 layer->setRenderTarget(GL_NONE); // see ::updateTextureLayer()
Romain Guy4a5a7152011-06-24 17:53:53 -0700271
Romain Guya1d3c912011-12-13 14:55:06 -0800272 Caches::getInstance().activeTexture(0);
Romain Guy9ace8f52011-07-07 20:50:11 -0700273 layer->generateTexture();
Romain Guy4a5a7152011-06-24 17:53:53 -0700274
275 return layer;
276}
277
Romain Guyaa6c24c2011-04-28 18:40:04 -0700278void LayerRenderer::updateTextureLayer(Layer* layer, uint32_t width, uint32_t height,
Romain Guya9489272011-06-22 20:58:11 -0700279 bool isOpaque, GLenum renderTarget, float* transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700280 if (layer) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700281 layer->setBlend(!isOpaque);
282 layer->setSize(width, height);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700283 layer->layer.set(0.0f, 0.0f, width, height);
284 layer->region.set(width, height);
285 layer->regionRect.set(0.0f, 0.0f, width, height);
Romain Guy9ace8f52011-07-07 20:50:11 -0700286 layer->getTexTransform().load(transform);
Romain Guy8f0095c2011-05-02 17:24:22 -0700287
Romain Guy9ace8f52011-07-07 20:50:11 -0700288 if (renderTarget != layer->getRenderTarget()) {
289 layer->setRenderTarget(renderTarget);
290 layer->bindTexture();
Romain Guyd21b6e12011-11-30 20:21:23 -0800291 layer->setFilter(GL_NEAREST, false, true);
292 layer->setWrap(GL_CLAMP_TO_EDGE, false, true);
Romain Guy4a5a7152011-06-24 17:53:53 -0700293 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700294 }
295}
296
Romain Guyada830f2011-01-13 12:13:20 -0800297void LayerRenderer::destroyLayer(Layer* layer) {
298 if (layer) {
Romain Guyeea60692011-07-26 20:35:55 -0700299 LAYER_RENDERER_LOGD("Recycling layer, %dx%d fbo = %d",
300 layer->getWidth(), layer->getHeight(), layer->getFbo());
Romain Guy1fc883b2011-01-12 14:30:59 -0800301
Romain Guy09b7c912011-02-02 20:28:09 -0800302 if (!Caches::getInstance().layerCache.put(layer)) {
Romain Guyeea60692011-07-26 20:35:55 -0700303 LAYER_RENDERER_LOGD(" Destroyed!");
Chet Haase603f6de2012-09-14 15:31:25 -0700304 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy09b7c912011-02-02 20:28:09 -0800305 } else {
Romain Guyeea60692011-07-26 20:35:55 -0700306 LAYER_RENDERER_LOGD(" Cached!");
Romain Guy65b345f2011-07-27 18:51:50 -0700307#if DEBUG_LAYER_RENDERER
Romain Guyeea60692011-07-26 20:35:55 -0700308 Caches::getInstance().layerCache.dump();
309#endif
Chet Haase98d3a642012-09-26 10:27:40 -0700310 layer->removeFbo();
Romain Guy09b7c912011-02-02 20:28:09 -0800311 layer->region.clear();
312 }
Romain Guyada830f2011-01-13 12:13:20 -0800313 }
Romain Guy6c319ca2011-01-11 14:29:25 -0800314}
315
Romain Guyada830f2011-01-13 12:13:20 -0800316void LayerRenderer::destroyLayerDeferred(Layer* layer) {
317 if (layer) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700318 LAYER_RENDERER_LOGD("Deferring layer destruction, fbo = %d", layer->getFbo());
Dave Burke56257af2012-09-25 20:30:09 -0700319
Romain Guyada830f2011-01-13 12:13:20 -0800320 Caches::getInstance().deleteLayerDeferred(layer);
321 }
Romain Guy57066eb2011-01-12 12:53:32 -0800322}
323
Romain Guy9c4b79a2011-11-10 19:23:58 -0800324void LayerRenderer::flushLayer(Layer* layer) {
325#ifdef GL_EXT_discard_framebuffer
326 GLuint fbo = layer->getFbo();
327 if (layer && fbo) {
328 // If possible, discard any enqueud operations on deferred
329 // rendering architectures
330 if (Caches::getInstance().extensions.hasDiscardFramebuffer()) {
331 GLuint previousFbo;
332 glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*) &previousFbo);
Romain Guy9c4b79a2011-11-10 19:23:58 -0800333 if (fbo != previousFbo) glBindFramebuffer(GL_FRAMEBUFFER, fbo);
Romain Guy45e4c3d2012-09-11 17:17:07 -0700334
335 const GLenum attachments[] = { GL_COLOR_ATTACHMENT0 };
336 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
Romain Guy9c4b79a2011-11-10 19:23:58 -0800337
338 if (fbo != previousFbo) glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
339 }
340 }
341#endif
342}
343
Romain Guy77a81162011-06-14 16:45:55 -0700344bool LayerRenderer::copyLayer(Layer* layer, SkBitmap* bitmap) {
345 Caches& caches = Caches::getInstance();
Chris Craik710f46d2012-09-17 17:25:49 -0700346 if (layer && bitmap->width() <= caches.maxTextureSize &&
Romain Guy77a81162011-06-14 16:45:55 -0700347 bitmap->height() <= caches.maxTextureSize) {
348
349 GLuint fbo = caches.fboCache.get();
350 if (!fbo) {
Steve Block8564c8d2012-01-05 23:22:43 +0000351 ALOGW("Could not obtain an FBO");
Romain Guy77a81162011-06-14 16:45:55 -0700352 return false;
353 }
354
Romain Guyd6b2a002011-06-17 17:45:59 -0700355 SkAutoLockPixels alp(*bitmap);
356
Romain Guy77a81162011-06-14 16:45:55 -0700357 GLuint texture;
358 GLuint previousFbo;
Chris Craik710f46d2012-09-17 17:25:49 -0700359 GLuint previousViewport[4];
Romain Guy77a81162011-06-14 16:45:55 -0700360
361 GLenum format;
362 GLenum type;
363
Romain Guyd6b2a002011-06-17 17:45:59 -0700364 GLenum error = GL_NO_ERROR;
365 bool status = false;
366
Romain Guy77a81162011-06-14 16:45:55 -0700367 switch (bitmap->config()) {
368 case SkBitmap::kA8_Config:
369 format = GL_ALPHA;
370 type = GL_UNSIGNED_BYTE;
371 break;
372 case SkBitmap::kRGB_565_Config:
373 format = GL_RGB;
374 type = GL_UNSIGNED_SHORT_5_6_5;
375 break;
376 case SkBitmap::kARGB_4444_Config:
377 format = GL_RGBA;
378 type = GL_UNSIGNED_SHORT_4_4_4_4;
379 break;
380 case SkBitmap::kARGB_8888_Config:
381 default:
382 format = GL_RGBA;
383 type = GL_UNSIGNED_BYTE;
384 break;
385 }
386
Romain Guy9ace8f52011-07-07 20:50:11 -0700387 float alpha = layer->getAlpha();
388 SkXfermode::Mode mode = layer->getMode();
Chris Craik710f46d2012-09-17 17:25:49 -0700389 GLuint previousLayerFbo = layer->getFbo();
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);
Chris Craik710f46d2012-09-17 17:25:49 -0700395 glGetIntegerv(GL_VIEWPORT, (GLint*) &previousViewport);
Romain Guy77a81162011-06-14 16:45:55 -0700396 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
397
398 glGenTextures(1, &texture);
Romain Guyd6b2a002011-06-17 17:45:59 -0700399 if ((error = glGetError()) != GL_NO_ERROR) goto error;
Romain Guy77a81162011-06-14 16:45:55 -0700400
Romain Guya1d3c912011-12-13 14:55:06 -0800401 caches.activeTexture(0);
Romain Guy77a81162011-06-14 16:45:55 -0700402 glBindTexture(GL_TEXTURE_2D, texture);
403
Romain Guye49d7ec2012-09-07 18:42:38 -0700404 glPixelStorei(GL_PACK_ALIGNMENT, bitmap->bytesPerPixel());
405
Romain Guyec19b4a2011-07-07 21:05:04 -0700406 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
407 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
Romain Guy77a81162011-06-14 16:45:55 -0700408
409 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
410 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
411
412 glTexImage2D(GL_TEXTURE_2D, 0, format, bitmap->width(), bitmap->height(),
413 0, format, type, NULL);
Romain Guyd6b2a002011-06-17 17:45:59 -0700414 if ((error = glGetError()) != GL_NO_ERROR) goto error;
415
Romain Guy77a81162011-06-14 16:45:55 -0700416 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
417 GL_TEXTURE_2D, texture, 0);
Romain Guyd6b2a002011-06-17 17:45:59 -0700418 if ((error = glGetError()) != GL_NO_ERROR) goto error;
Romain Guy77a81162011-06-14 16:45:55 -0700419
Romain Guyd6b2a002011-06-17 17:45:59 -0700420 {
421 LayerRenderer renderer(layer);
422 renderer.setViewport(bitmap->width(), bitmap->height());
423 renderer.OpenGLRenderer::prepareDirty(0.0f, 0.0f,
Romain Guy9ace8f52011-07-07 20:50:11 -0700424 bitmap->width(), bitmap->height(), !layer->isBlend());
Romain Guya9dc86b2011-10-11 14:06:21 -0700425
Romain Guy586cae32012-07-13 15:28:31 -0700426 caches.disableScissor();
Romain Guya9dc86b2011-10-11 14:06:21 -0700427 renderer.translate(0.0f, bitmap->height());
428 renderer.scale(1.0f, -1.0f);
429
430 mat4 texTransform(layer->getTexTransform());
431
432 mat4 invert;
433 invert.translate(0.0f, 1.0f, 0.0f);
434 invert.scale(1.0f, -1.0f, 1.0f);
435 layer->getTexTransform().multiply(invert);
436
Romain Guyd6b2a002011-06-17 17:45:59 -0700437 if ((error = glGetError()) != GL_NO_ERROR) goto error;
Romain Guy77a81162011-06-14 16:45:55 -0700438
Romain Guyd6b2a002011-06-17 17:45:59 -0700439 {
440 Rect bounds;
441 bounds.set(0.0f, 0.0f, bitmap->width(), bitmap->height());
442 renderer.drawTextureLayer(layer, bounds);
Romain Guy77a81162011-06-14 16:45:55 -0700443
Romain Guyd6b2a002011-06-17 17:45:59 -0700444 glReadPixels(0, 0, bitmap->width(), bitmap->height(), format,
445 type, bitmap->getPixels());
Romain Guy77a81162011-06-14 16:45:55 -0700446
Romain Guyd6b2a002011-06-17 17:45:59 -0700447 if ((error = glGetError()) != GL_NO_ERROR) goto error;
448 }
Romain Guy77a81162011-06-14 16:45:55 -0700449
Romain Guya9dc86b2011-10-11 14:06:21 -0700450 layer->getTexTransform().load(texTransform);
Romain Guyd6b2a002011-06-17 17:45:59 -0700451 status = true;
452 }
Romain Guy77a81162011-06-14 16:45:55 -0700453
Romain Guyd6b2a002011-06-17 17:45:59 -0700454error:
455#if DEBUG_OPENGL
456 if (error != GL_NO_ERROR) {
Steve Block5baa3a62011-12-20 16:23:08 +0000457 ALOGD("GL error while copying layer into bitmap = 0x%x", error);
Romain Guyd6b2a002011-06-17 17:45:59 -0700458 }
459#endif
Romain Guy77a81162011-06-14 16:45:55 -0700460
461 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guy9ace8f52011-07-07 20:50:11 -0700462 layer->setAlpha(alpha, mode);
Chris Craik710f46d2012-09-17 17:25:49 -0700463 layer->setFbo(previousLayerFbo);
Romain Guy77a81162011-06-14 16:45:55 -0700464 glDeleteTextures(1, &texture);
465 caches.fboCache.put(fbo);
Chris Craik710f46d2012-09-17 17:25:49 -0700466 glViewport(previousViewport[0], previousViewport[1],
467 previousViewport[2], previousViewport[3]);
Romain Guy77a81162011-06-14 16:45:55 -0700468
Romain Guyd6b2a002011-06-17 17:45:59 -0700469 return status;
Romain Guy77a81162011-06-14 16:45:55 -0700470 }
471 return false;
472}
473
Romain Guy6c319ca2011-01-11 14:29:25 -0800474}; // namespace uirenderer
475}; // namespace android