blob: 3484d411c130391e2258fdbb6008b7351ae02fc1 [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 Guy7c25aab2012-10-18 15:05:02 -070021#include <private/hwui/DrawGlInfo.h>
22
Romain Guy09b7c912011-02-02 20:28:09 -080023#include "LayerCache.h"
Romain Guy6c319ca2011-01-11 14:29:25 -080024#include "LayerRenderer.h"
Romain Guyaa6c24c2011-04-28 18:40:04 -070025#include "Matrix.h"
Romain Guy1fc883b2011-01-12 14:30:59 -080026#include "Properties.h"
Romain Guy3a3133d2011-02-01 22:59:58 -080027#include "Rect.h"
Romain Guy6c319ca2011-01-11 14:29:25 -080028
29namespace android {
30namespace uirenderer {
31
32///////////////////////////////////////////////////////////////////////////////
33// Rendering
34///////////////////////////////////////////////////////////////////////////////
35
Romain Guy79537452011-10-12 13:48:51 -070036LayerRenderer::LayerRenderer(Layer* layer): mLayer(layer) {
37}
38
39LayerRenderer::~LayerRenderer() {
40}
41
Romain Guy35643dd2012-09-18 15:40:58 -070042void LayerRenderer::setViewport(int width, int height) {
43 initViewport(width, height);
44}
45
Romain Guy7c25aab2012-10-18 15:05:02 -070046status_t LayerRenderer::prepareDirty(float left, float top, float right, float bottom,
47 bool opaque) {
Romain Guy9ace8f52011-07-07 20:50:11 -070048 LAYER_RENDERER_LOGD("Rendering into layer, fbo = %d", mLayer->getFbo());
Romain Guy1fc883b2011-01-12 14:30:59 -080049
Romain Guy9ace8f52011-07-07 20:50:11 -070050 glBindFramebuffer(GL_FRAMEBUFFER, mLayer->getFbo());
Romain Guy62687ec2011-02-02 15:44:19 -080051
Romain Guy09b7c912011-02-02 20:28:09 -080052 const float width = mLayer->layer.getWidth();
53 const float height = mLayer->layer.getHeight();
54
Romain Guy3a3133d2011-02-01 22:59:58 -080055 Rect dirty(left, top, right, bottom);
56 if (dirty.isEmpty() || (dirty.left <= 0 && dirty.top <= 0 &&
Romain Guy09b7c912011-02-02 20:28:09 -080057 dirty.right >= width && dirty.bottom >= height)) {
Romain Guy3a3133d2011-02-01 22:59:58 -080058 mLayer->region.clear();
Romain Guy09b7c912011-02-02 20:28:09 -080059 dirty.set(0.0f, 0.0f, width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -080060 } else {
Romain Guy09b7c912011-02-02 20:28:09 -080061 dirty.intersect(0.0f, 0.0f, width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -080062 android::Rect r(dirty.left, dirty.top, dirty.right, dirty.bottom);
63 mLayer->region.subtractSelf(r);
64 }
Romain Guyc88e3572011-01-22 00:32:12 -080065
Chet Haase44b2fe32012-06-06 19:03:58 -070066 return OpenGLRenderer::prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, opaque);
Romain Guy6c319ca2011-01-11 14:29:25 -080067}
68
Romain Guy7c25aab2012-10-18 15:05:02 -070069status_t LayerRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
70 if (mLayer->isDirty()) {
71 getCaches().disableScissor();
72 glClear(GL_COLOR_BUFFER_BIT);
73
74 getCaches().resetScissor();
75 mLayer->setDirty(false);
76
77 return DrawGlInfo::kStatusDone;
78 }
79
80 return OpenGLRenderer::clear(left, top, right, bottom, opaque);
81}
82
Romain Guy6c319ca2011-01-11 14:29:25 -080083void LayerRenderer::finish() {
84 OpenGLRenderer::finish();
Romain Guy1fc883b2011-01-12 14:30:59 -080085
Romain Guyf219da52011-01-16 12:54:25 -080086 generateMesh();
87
Romain Guy9ace8f52011-07-07 20:50:11 -070088 LAYER_RENDERER_LOGD("Finished rendering into layer, fbo = %d", mLayer->getFbo());
Romain Guy42f3a4b2011-01-19 13:42:26 -080089
90 // No need to unbind our FBO, this will be taken care of by the caller
91 // who will invoke OpenGLRenderer::resume()
92}
93
94GLint LayerRenderer::getTargetFbo() {
Romain Guy9ace8f52011-07-07 20:50:11 -070095 return mLayer->getFbo();
Romain Guy6c319ca2011-01-11 14:29:25 -080096}
97
Romain Guy11cb6422012-09-21 00:39:43 -070098bool LayerRenderer::suppressErrorChecks() {
99 return true;
100}
101
Romain Guy6c319ca2011-01-11 14:29:25 -0800102///////////////////////////////////////////////////////////////////////////////
Romain Guyf219da52011-01-16 12:54:25 -0800103// Dirty region tracking
104///////////////////////////////////////////////////////////////////////////////
105
106bool LayerRenderer::hasLayer() {
107 return true;
108}
109
110Region* LayerRenderer::getRegion() {
Romain Guyf219da52011-01-16 12:54:25 -0800111 if (getSnapshot()->flags & Snapshot::kFlagFboTarget) {
112 return OpenGLRenderer::getRegion();
113 }
114 return &mLayer->region;
Romain Guyf219da52011-01-16 12:54:25 -0800115}
116
Romain Guy8a3957d2011-09-07 17:55:15 -0700117// TODO: This implementation is flawed and can generate T-junctions
118// in the mesh, which will in turn produce cracks when the
119// mesh is rotated/skewed. The easiest way to fix this would
120// be, for each row, to add new vertices shared with the previous
121// row when the two rows share an edge.
122// In practice, T-junctions do not appear often so this has yet
123// to be fixed.
Romain Guyf219da52011-01-16 12:54:25 -0800124void LayerRenderer::generateMesh() {
Romain Guyf219da52011-01-16 12:54:25 -0800125 if (mLayer->region.isRect() || mLayer->region.isEmpty()) {
126 if (mLayer->mesh) {
127 delete mLayer->mesh;
128 delete mLayer->meshIndices;
129
130 mLayer->mesh = NULL;
131 mLayer->meshIndices = NULL;
132 mLayer->meshElementCount = 0;
133 }
Romain Guy40667672011-03-18 14:34:03 -0700134
Romain Guy9fc27812011-04-27 14:21:41 -0700135 mLayer->setRegionAsRect();
Romain Guyf219da52011-01-16 12:54:25 -0800136 return;
137 }
138
139 size_t count;
140 const android::Rect* rects = mLayer->region.getArray(&count);
141
142 GLsizei elementCount = count * 6;
143
144 if (mLayer->mesh && mLayer->meshElementCount < elementCount) {
145 delete mLayer->mesh;
146 delete mLayer->meshIndices;
147
148 mLayer->mesh = NULL;
149 mLayer->meshIndices = NULL;
150 }
151
Romain Guy4f09f542011-01-26 22:41:43 -0800152 bool rebuildIndices = false;
Romain Guyf219da52011-01-16 12:54:25 -0800153 if (!mLayer->mesh) {
154 mLayer->mesh = new TextureVertex[count * 4];
155 mLayer->meshIndices = new uint16_t[elementCount];
Romain Guy4f09f542011-01-26 22:41:43 -0800156 rebuildIndices = true;
Romain Guyf219da52011-01-16 12:54:25 -0800157 }
Romain Guy4f09f542011-01-26 22:41:43 -0800158 mLayer->meshElementCount = elementCount;
Romain Guyf219da52011-01-16 12:54:25 -0800159
Romain Guy9ace8f52011-07-07 20:50:11 -0700160 const float texX = 1.0f / float(mLayer->getWidth());
161 const float texY = 1.0f / float(mLayer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800162 const float height = mLayer->layer.getHeight();
163
164 TextureVertex* mesh = mLayer->mesh;
165 uint16_t* indices = mLayer->meshIndices;
166
167 for (size_t i = 0; i < count; i++) {
168 const android::Rect* r = &rects[i];
169
170 const float u1 = r->left * texX;
171 const float v1 = (height - r->top) * texY;
172 const float u2 = r->right * texX;
173 const float v2 = (height - r->bottom) * texY;
174
175 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
176 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
177 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
178 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
179
Romain Guy4f09f542011-01-26 22:41:43 -0800180 if (rebuildIndices) {
181 uint16_t quad = i * 4;
182 int index = i * 6;
183 indices[index ] = quad; // top-left
184 indices[index + 1] = quad + 1; // top-right
185 indices[index + 2] = quad + 2; // bottom-left
186 indices[index + 3] = quad + 2; // bottom-left
187 indices[index + 4] = quad + 1; // top-right
188 indices[index + 5] = quad + 3; // bottom-right
189 }
Romain Guyf219da52011-01-16 12:54:25 -0800190 }
Romain Guyf219da52011-01-16 12:54:25 -0800191}
192
193///////////////////////////////////////////////////////////////////////////////
194// Layers management
Romain Guy6c319ca2011-01-11 14:29:25 -0800195///////////////////////////////////////////////////////////////////////////////
196
Romain Guyada830f2011-01-13 12:13:20 -0800197Layer* LayerRenderer::createLayer(uint32_t width, uint32_t height, bool isOpaque) {
Romain Guyeea60692011-07-26 20:35:55 -0700198 LAYER_RENDERER_LOGD("Requesting new render layer %dx%d", width, height);
Romain Guy1fc883b2011-01-12 14:30:59 -0800199
Romain Guya1d3c912011-12-13 14:55:06 -0800200 Caches& caches = Caches::getInstance();
201 GLuint fbo = caches.fboCache.get();
Romain Guy09b7c912011-02-02 20:28:09 -0800202 if (!fbo) {
Steve Block8564c8d2012-01-05 23:22:43 +0000203 ALOGW("Could not obtain an FBO");
Romain Guy09b7c912011-02-02 20:28:09 -0800204 return NULL;
205 }
206
Romain Guya1d3c912011-12-13 14:55:06 -0800207 caches.activeTexture(0);
208 Layer* layer = caches.layerCache.get(width, height);
Romain Guy09b7c912011-02-02 20:28:09 -0800209 if (!layer) {
Steve Block8564c8d2012-01-05 23:22:43 +0000210 ALOGW("Could not obtain a layer");
Romain Guy09b7c912011-02-02 20:28:09 -0800211 return NULL;
212 }
213
Romain Guy9ace8f52011-07-07 20:50:11 -0700214 layer->setFbo(fbo);
Romain Guy09b7c912011-02-02 20:28:09 -0800215 layer->layer.set(0.0f, 0.0f, width, height);
Romain Guy9ace8f52011-07-07 20:50:11 -0700216 layer->texCoords.set(0.0f, height / float(layer->getHeight()),
217 width / float(layer->getWidth()), 0.0f);
218 layer->setAlpha(255, SkXfermode::kSrcOver_Mode);
219 layer->setBlend(!isOpaque);
220 layer->setColorFilter(NULL);
Romain Guy7c25aab2012-10-18 15:05:02 -0700221 layer->setDirty(true);
Romain Guy09b7c912011-02-02 20:28:09 -0800222 layer->region.clear();
Romain Guyada830f2011-01-13 12:13:20 -0800223
Romain Guy6c319ca2011-01-11 14:29:25 -0800224 GLuint previousFbo;
225 glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*) &previousFbo);
226
Romain Guy9ace8f52011-07-07 20:50:11 -0700227 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
228 layer->bindTexture();
Romain Guy6c319ca2011-01-11 14:29:25 -0800229
Romain Guy09b7c912011-02-02 20:28:09 -0800230 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700231 if (layer->isEmpty()) {
232 layer->setEmpty(false);
233 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
Romain Guy6c319ca2011-01-11 14:29:25 -0800234
Romain Guy09b7c912011-02-02 20:28:09 -0800235 if (glGetError() != GL_NO_ERROR) {
Steve Block5baa3a62011-12-20 16:23:08 +0000236 ALOGD("Could not allocate texture for layer (fbo=%d %dx%d)",
Romain Guy5cd5c3f2011-10-17 17:10:02 -0700237 fbo, width, height);
Romain Guy9ace8f52011-07-07 20:50:11 -0700238
Romain Guy09b7c912011-02-02 20:28:09 -0800239 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guy9ace8f52011-07-07 20:50:11 -0700240
Chet Haase603f6de2012-09-14 15:31:25 -0700241 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy9ace8f52011-07-07 20:50:11 -0700242
Romain Guy09b7c912011-02-02 20:28:09 -0800243 return NULL;
244 }
Romain Guy6c319ca2011-01-11 14:29:25 -0800245 }
246
247 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700248 layer->getTexture(), 0);
Romain Guy6c319ca2011-01-11 14:29:25 -0800249
Romain Guy6c319ca2011-01-11 14:29:25 -0800250 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
251
Romain Guyada830f2011-01-13 12:13:20 -0800252 return layer;
Romain Guy6c319ca2011-01-11 14:29:25 -0800253}
254
Romain Guyada830f2011-01-13 12:13:20 -0800255bool LayerRenderer::resizeLayer(Layer* layer, uint32_t width, uint32_t height) {
256 if (layer) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700257 LAYER_RENDERER_LOGD("Resizing layer fbo = %d to %dx%d", layer->getFbo(), width, height);
Romain Guy1fc883b2011-01-12 14:30:59 -0800258
Romain Guy09b7c912011-02-02 20:28:09 -0800259 if (Caches::getInstance().layerCache.resize(layer, width, height)) {
260 layer->layer.set(0.0f, 0.0f, width, height);
Romain Guy9ace8f52011-07-07 20:50:11 -0700261 layer->texCoords.set(0.0f, height / float(layer->getHeight()),
262 width / float(layer->getWidth()), 0.0f);
Romain Guy09b7c912011-02-02 20:28:09 -0800263 } else {
Chet Haase603f6de2012-09-14 15:31:25 -0700264 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guyada830f2011-01-13 12:13:20 -0800265 return false;
266 }
Romain Guy6c319ca2011-01-11 14:29:25 -0800267 }
Romain Guy09b7c912011-02-02 20:28:09 -0800268
Romain Guyada830f2011-01-13 12:13:20 -0800269 return true;
Romain Guy6c319ca2011-01-11 14:29:25 -0800270}
271
Romain Guy4a5a7152011-06-24 17:53:53 -0700272Layer* LayerRenderer::createTextureLayer(bool isOpaque) {
273 LAYER_RENDERER_LOGD("Creating new texture layer");
274
275 Layer* layer = new Layer(0, 0);
Romain Guy9ace8f52011-07-07 20:50:11 -0700276 layer->setCacheable(false);
277 layer->setTextureLayer(true);
278 layer->setBlend(!isOpaque);
279 layer->setEmpty(true);
280 layer->setFbo(0);
281 layer->setAlpha(255, SkXfermode::kSrcOver_Mode);
Romain Guy4a5a7152011-06-24 17:53:53 -0700282 layer->layer.set(0.0f, 0.0f, 0.0f, 0.0f);
Romain Guya9dc86b2011-10-11 14:06:21 -0700283 layer->texCoords.set(0.0f, 1.0f, 1.0f, 0.0f);
Romain Guy4a5a7152011-06-24 17:53:53 -0700284 layer->region.clear();
Romain Guy9ace8f52011-07-07 20:50:11 -0700285 layer->setRenderTarget(GL_NONE); // see ::updateTextureLayer()
Romain Guy4a5a7152011-06-24 17:53:53 -0700286
Romain Guya1d3c912011-12-13 14:55:06 -0800287 Caches::getInstance().activeTexture(0);
Romain Guy9ace8f52011-07-07 20:50:11 -0700288 layer->generateTexture();
Romain Guy4a5a7152011-06-24 17:53:53 -0700289
290 return layer;
291}
292
Romain Guyaa6c24c2011-04-28 18:40:04 -0700293void LayerRenderer::updateTextureLayer(Layer* layer, uint32_t width, uint32_t height,
Romain Guya9489272011-06-22 20:58:11 -0700294 bool isOpaque, GLenum renderTarget, float* transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700295 if (layer) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700296 layer->setBlend(!isOpaque);
297 layer->setSize(width, height);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700298 layer->layer.set(0.0f, 0.0f, width, height);
299 layer->region.set(width, height);
300 layer->regionRect.set(0.0f, 0.0f, width, height);
Romain Guy9ace8f52011-07-07 20:50:11 -0700301 layer->getTexTransform().load(transform);
Romain Guy8f0095c2011-05-02 17:24:22 -0700302
Romain Guy9ace8f52011-07-07 20:50:11 -0700303 if (renderTarget != layer->getRenderTarget()) {
304 layer->setRenderTarget(renderTarget);
305 layer->bindTexture();
Romain Guyd21b6e12011-11-30 20:21:23 -0800306 layer->setFilter(GL_NEAREST, false, true);
307 layer->setWrap(GL_CLAMP_TO_EDGE, false, true);
Romain Guy4a5a7152011-06-24 17:53:53 -0700308 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700309 }
310}
311
Romain Guyada830f2011-01-13 12:13:20 -0800312void LayerRenderer::destroyLayer(Layer* layer) {
313 if (layer) {
Romain Guyeea60692011-07-26 20:35:55 -0700314 LAYER_RENDERER_LOGD("Recycling layer, %dx%d fbo = %d",
315 layer->getWidth(), layer->getHeight(), layer->getFbo());
Romain Guy1fc883b2011-01-12 14:30:59 -0800316
Romain Guy09b7c912011-02-02 20:28:09 -0800317 if (!Caches::getInstance().layerCache.put(layer)) {
Romain Guyeea60692011-07-26 20:35:55 -0700318 LAYER_RENDERER_LOGD(" Destroyed!");
Chet Haase603f6de2012-09-14 15:31:25 -0700319 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy09b7c912011-02-02 20:28:09 -0800320 } else {
Romain Guyeea60692011-07-26 20:35:55 -0700321 LAYER_RENDERER_LOGD(" Cached!");
Romain Guy65b345f2011-07-27 18:51:50 -0700322#if DEBUG_LAYER_RENDERER
Romain Guyeea60692011-07-26 20:35:55 -0700323 Caches::getInstance().layerCache.dump();
324#endif
Chet Haase98d3a642012-09-26 10:27:40 -0700325 layer->removeFbo();
Romain Guy09b7c912011-02-02 20:28:09 -0800326 layer->region.clear();
327 }
Romain Guyada830f2011-01-13 12:13:20 -0800328 }
Romain Guy6c319ca2011-01-11 14:29:25 -0800329}
330
Romain Guyada830f2011-01-13 12:13:20 -0800331void LayerRenderer::destroyLayerDeferred(Layer* layer) {
332 if (layer) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700333 LAYER_RENDERER_LOGD("Deferring layer destruction, fbo = %d", layer->getFbo());
Dave Burke56257af2012-09-25 20:30:09 -0700334
Romain Guyada830f2011-01-13 12:13:20 -0800335 Caches::getInstance().deleteLayerDeferred(layer);
336 }
Romain Guy57066eb2011-01-12 12:53:32 -0800337}
338
Romain Guy9c4b79a2011-11-10 19:23:58 -0800339void LayerRenderer::flushLayer(Layer* layer) {
340#ifdef GL_EXT_discard_framebuffer
341 GLuint fbo = layer->getFbo();
342 if (layer && fbo) {
343 // If possible, discard any enqueud operations on deferred
344 // rendering architectures
345 if (Caches::getInstance().extensions.hasDiscardFramebuffer()) {
346 GLuint previousFbo;
347 glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*) &previousFbo);
Romain Guy9c4b79a2011-11-10 19:23:58 -0800348 if (fbo != previousFbo) glBindFramebuffer(GL_FRAMEBUFFER, fbo);
Romain Guy45e4c3d2012-09-11 17:17:07 -0700349
350 const GLenum attachments[] = { GL_COLOR_ATTACHMENT0 };
351 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
Romain Guy9c4b79a2011-11-10 19:23:58 -0800352
353 if (fbo != previousFbo) glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
354 }
355 }
356#endif
357}
358
Romain Guy77a81162011-06-14 16:45:55 -0700359bool LayerRenderer::copyLayer(Layer* layer, SkBitmap* bitmap) {
360 Caches& caches = Caches::getInstance();
Chris Craik710f46d2012-09-17 17:25:49 -0700361 if (layer && bitmap->width() <= caches.maxTextureSize &&
Romain Guy77a81162011-06-14 16:45:55 -0700362 bitmap->height() <= caches.maxTextureSize) {
363
364 GLuint fbo = caches.fboCache.get();
365 if (!fbo) {
Steve Block8564c8d2012-01-05 23:22:43 +0000366 ALOGW("Could not obtain an FBO");
Romain Guy77a81162011-06-14 16:45:55 -0700367 return false;
368 }
369
Romain Guyd6b2a002011-06-17 17:45:59 -0700370 SkAutoLockPixels alp(*bitmap);
371
Romain Guy77a81162011-06-14 16:45:55 -0700372 GLuint texture;
373 GLuint previousFbo;
Chris Craik710f46d2012-09-17 17:25:49 -0700374 GLuint previousViewport[4];
Romain Guy77a81162011-06-14 16:45:55 -0700375
376 GLenum format;
377 GLenum type;
378
Romain Guyd6b2a002011-06-17 17:45:59 -0700379 GLenum error = GL_NO_ERROR;
380 bool status = false;
381
Romain Guy77a81162011-06-14 16:45:55 -0700382 switch (bitmap->config()) {
383 case SkBitmap::kA8_Config:
384 format = GL_ALPHA;
385 type = GL_UNSIGNED_BYTE;
386 break;
387 case SkBitmap::kRGB_565_Config:
388 format = GL_RGB;
389 type = GL_UNSIGNED_SHORT_5_6_5;
390 break;
391 case SkBitmap::kARGB_4444_Config:
392 format = GL_RGBA;
393 type = GL_UNSIGNED_SHORT_4_4_4_4;
394 break;
395 case SkBitmap::kARGB_8888_Config:
396 default:
397 format = GL_RGBA;
398 type = GL_UNSIGNED_BYTE;
399 break;
400 }
401
Romain Guy9ace8f52011-07-07 20:50:11 -0700402 float alpha = layer->getAlpha();
403 SkXfermode::Mode mode = layer->getMode();
Chris Craik710f46d2012-09-17 17:25:49 -0700404 GLuint previousLayerFbo = layer->getFbo();
Romain Guyd6b2a002011-06-17 17:45:59 -0700405
Romain Guy9ace8f52011-07-07 20:50:11 -0700406 layer->setAlpha(255, SkXfermode::kSrc_Mode);
407 layer->setFbo(fbo);
Romain Guyd6b2a002011-06-17 17:45:59 -0700408
Romain Guy77a81162011-06-14 16:45:55 -0700409 glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*) &previousFbo);
Chris Craik710f46d2012-09-17 17:25:49 -0700410 glGetIntegerv(GL_VIEWPORT, (GLint*) &previousViewport);
Romain Guy77a81162011-06-14 16:45:55 -0700411 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
412
413 glGenTextures(1, &texture);
Romain Guyd6b2a002011-06-17 17:45:59 -0700414 if ((error = glGetError()) != GL_NO_ERROR) goto error;
Romain Guy77a81162011-06-14 16:45:55 -0700415
Romain Guya1d3c912011-12-13 14:55:06 -0800416 caches.activeTexture(0);
Romain Guy77a81162011-06-14 16:45:55 -0700417 glBindTexture(GL_TEXTURE_2D, texture);
418
Romain Guye49d7ec2012-09-07 18:42:38 -0700419 glPixelStorei(GL_PACK_ALIGNMENT, bitmap->bytesPerPixel());
420
Romain Guyec19b4a2011-07-07 21:05:04 -0700421 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
422 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
Romain Guy77a81162011-06-14 16:45:55 -0700423
424 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
425 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
426
427 glTexImage2D(GL_TEXTURE_2D, 0, format, bitmap->width(), bitmap->height(),
428 0, format, type, NULL);
Romain Guyd6b2a002011-06-17 17:45:59 -0700429 if ((error = glGetError()) != GL_NO_ERROR) goto error;
430
Romain Guy77a81162011-06-14 16:45:55 -0700431 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
432 GL_TEXTURE_2D, texture, 0);
Romain Guyd6b2a002011-06-17 17:45:59 -0700433 if ((error = glGetError()) != GL_NO_ERROR) goto error;
Romain Guy77a81162011-06-14 16:45:55 -0700434
Romain Guyd6b2a002011-06-17 17:45:59 -0700435 {
436 LayerRenderer renderer(layer);
437 renderer.setViewport(bitmap->width(), bitmap->height());
438 renderer.OpenGLRenderer::prepareDirty(0.0f, 0.0f,
Romain Guy9ace8f52011-07-07 20:50:11 -0700439 bitmap->width(), bitmap->height(), !layer->isBlend());
Romain Guya9dc86b2011-10-11 14:06:21 -0700440
Romain Guy586cae32012-07-13 15:28:31 -0700441 caches.disableScissor();
Romain Guya9dc86b2011-10-11 14:06:21 -0700442 renderer.translate(0.0f, bitmap->height());
443 renderer.scale(1.0f, -1.0f);
444
445 mat4 texTransform(layer->getTexTransform());
446
447 mat4 invert;
448 invert.translate(0.0f, 1.0f, 0.0f);
449 invert.scale(1.0f, -1.0f, 1.0f);
450 layer->getTexTransform().multiply(invert);
451
Romain Guyd6b2a002011-06-17 17:45:59 -0700452 if ((error = glGetError()) != GL_NO_ERROR) goto error;
Romain Guy77a81162011-06-14 16:45:55 -0700453
Romain Guyd6b2a002011-06-17 17:45:59 -0700454 {
455 Rect bounds;
456 bounds.set(0.0f, 0.0f, bitmap->width(), bitmap->height());
457 renderer.drawTextureLayer(layer, bounds);
Romain Guy77a81162011-06-14 16:45:55 -0700458
Romain Guyd6b2a002011-06-17 17:45:59 -0700459 glReadPixels(0, 0, bitmap->width(), bitmap->height(), format,
460 type, bitmap->getPixels());
Romain Guy77a81162011-06-14 16:45:55 -0700461
Romain Guyd6b2a002011-06-17 17:45:59 -0700462 if ((error = glGetError()) != GL_NO_ERROR) goto error;
463 }
Romain Guy77a81162011-06-14 16:45:55 -0700464
Romain Guya9dc86b2011-10-11 14:06:21 -0700465 layer->getTexTransform().load(texTransform);
Romain Guyd6b2a002011-06-17 17:45:59 -0700466 status = true;
467 }
Romain Guy77a81162011-06-14 16:45:55 -0700468
Romain Guyd6b2a002011-06-17 17:45:59 -0700469error:
470#if DEBUG_OPENGL
471 if (error != GL_NO_ERROR) {
Steve Block5baa3a62011-12-20 16:23:08 +0000472 ALOGD("GL error while copying layer into bitmap = 0x%x", error);
Romain Guyd6b2a002011-06-17 17:45:59 -0700473 }
474#endif
Romain Guy77a81162011-06-14 16:45:55 -0700475
476 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guy9ace8f52011-07-07 20:50:11 -0700477 layer->setAlpha(alpha, mode);
Chris Craik710f46d2012-09-17 17:25:49 -0700478 layer->setFbo(previousLayerFbo);
Romain Guy77a81162011-06-14 16:45:55 -0700479 glDeleteTextures(1, &texture);
480 caches.fboCache.put(fbo);
Chris Craik710f46d2012-09-17 17:25:49 -0700481 glViewport(previousViewport[0], previousViewport[1],
482 previousViewport[2], previousViewport[3]);
Romain Guy77a81162011-06-14 16:45:55 -0700483
Romain Guyd6b2a002011-06-17 17:45:59 -0700484 return status;
Romain Guy77a81162011-06-14 16:45:55 -0700485 }
486 return false;
487}
488
Romain Guy6c319ca2011-01-11 14:29:25 -0800489}; // namespace uirenderer
490}; // namespace android