blob: 3e55fffd93a9d8008f07bea3950b7c010d71320d [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 Guyc3fedaf2013-01-29 17:26:25 -080065 mLayer->clipRect.set(dirty);
Romain Guyc88e3572011-01-22 00:32:12 -080066
Chet Haase44b2fe32012-06-06 19:03:58 -070067 return OpenGLRenderer::prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, opaque);
Romain Guy6c319ca2011-01-11 14:29:25 -080068}
69
Romain Guy7c25aab2012-10-18 15:05:02 -070070status_t LayerRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
71 if (mLayer->isDirty()) {
72 getCaches().disableScissor();
73 glClear(GL_COLOR_BUFFER_BIT);
74
75 getCaches().resetScissor();
76 mLayer->setDirty(false);
77
78 return DrawGlInfo::kStatusDone;
79 }
80
81 return OpenGLRenderer::clear(left, top, right, bottom, opaque);
82}
83
Romain Guy6c319ca2011-01-11 14:29:25 -080084void LayerRenderer::finish() {
85 OpenGLRenderer::finish();
Romain Guy1fc883b2011-01-12 14:30:59 -080086
Romain Guyf219da52011-01-16 12:54:25 -080087 generateMesh();
88
Romain Guy9ace8f52011-07-07 20:50:11 -070089 LAYER_RENDERER_LOGD("Finished rendering into layer, fbo = %d", mLayer->getFbo());
Romain Guy42f3a4b2011-01-19 13:42:26 -080090
91 // No need to unbind our FBO, this will be taken care of by the caller
92 // who will invoke OpenGLRenderer::resume()
93}
94
Romain Guy624234f2013-03-05 16:43:31 -080095GLint LayerRenderer::getTargetFbo() const {
Romain Guy9ace8f52011-07-07 20:50:11 -070096 return mLayer->getFbo();
Romain Guy6c319ca2011-01-11 14:29:25 -080097}
98
Romain Guy624234f2013-03-05 16:43:31 -080099bool LayerRenderer::suppressErrorChecks() const {
Romain Guy11cb6422012-09-21 00:39:43 -0700100 return true;
101}
102
Romain Guy6c319ca2011-01-11 14:29:25 -0800103///////////////////////////////////////////////////////////////////////////////
Romain Guy8ce00302013-01-15 18:51:42 -0800104// Layer support
Romain Guyf219da52011-01-16 12:54:25 -0800105///////////////////////////////////////////////////////////////////////////////
106
Romain Guy624234f2013-03-05 16:43:31 -0800107bool LayerRenderer::hasLayer() const {
Romain Guyf219da52011-01-16 12:54:25 -0800108 return true;
109}
110
Romain Guy8ce00302013-01-15 18:51:42 -0800111void LayerRenderer::ensureStencilBuffer() {
112 attachStencilBufferToLayer(mLayer);
113}
114
115///////////////////////////////////////////////////////////////////////////////
116// Dirty region tracking
117///////////////////////////////////////////////////////////////////////////////
118
Romain Guy624234f2013-03-05 16:43:31 -0800119Region* LayerRenderer::getRegion() const {
Romain Guyf219da52011-01-16 12:54:25 -0800120 if (getSnapshot()->flags & Snapshot::kFlagFboTarget) {
121 return OpenGLRenderer::getRegion();
122 }
123 return &mLayer->region;
Romain Guyf219da52011-01-16 12:54:25 -0800124}
125
Chris Craik6c5b9be2013-02-27 14:03:19 -0800126// TODO: This implementation uses a very simple approach to fixing T-junctions which keeps the
127// results as rectangles, and is thus not necessarily efficient in the geometry
128// produced. Eventually, it may be better to develop triangle-based mechanism.
Romain Guyf219da52011-01-16 12:54:25 -0800129void LayerRenderer::generateMesh() {
Romain Guyf219da52011-01-16 12:54:25 -0800130 if (mLayer->region.isRect() || mLayer->region.isEmpty()) {
131 if (mLayer->mesh) {
Romain Guy96885eb2013-03-26 15:05:58 -0700132 delete[] mLayer->mesh;
133 delete[] mLayer->meshIndices;
Romain Guyf219da52011-01-16 12:54:25 -0800134
135 mLayer->mesh = NULL;
136 mLayer->meshIndices = NULL;
137 mLayer->meshElementCount = 0;
138 }
Romain Guy40667672011-03-18 14:34:03 -0700139
Romain Guy9fc27812011-04-27 14:21:41 -0700140 mLayer->setRegionAsRect();
Romain Guyf219da52011-01-16 12:54:25 -0800141 return;
142 }
143
Chris Craik6c5b9be2013-02-27 14:03:19 -0800144 // avoid T-junctions as they cause artifacts in between the resultant
145 // geometry when complex transforms occur.
146 // TODO: generate the safeRegion only if necessary based on drawing transform (see
147 // OpenGLRenderer::composeLayerRegion())
148 Region safeRegion = Region::createTJunctionFreeRegion(mLayer->region);
149
Romain Guyf219da52011-01-16 12:54:25 -0800150 size_t count;
Chris Craik6c5b9be2013-02-27 14:03:19 -0800151 const android::Rect* rects = safeRegion.getArray(&count);
Romain Guyf219da52011-01-16 12:54:25 -0800152
153 GLsizei elementCount = count * 6;
154
155 if (mLayer->mesh && mLayer->meshElementCount < elementCount) {
Romain Guy96885eb2013-03-26 15:05:58 -0700156 delete[] mLayer->mesh;
157 delete[] mLayer->meshIndices;
Romain Guyf219da52011-01-16 12:54:25 -0800158
159 mLayer->mesh = NULL;
160 mLayer->meshIndices = NULL;
161 }
162
Romain Guy4f09f542011-01-26 22:41:43 -0800163 bool rebuildIndices = false;
Romain Guyf219da52011-01-16 12:54:25 -0800164 if (!mLayer->mesh) {
165 mLayer->mesh = new TextureVertex[count * 4];
166 mLayer->meshIndices = new uint16_t[elementCount];
Romain Guy4f09f542011-01-26 22:41:43 -0800167 rebuildIndices = true;
Romain Guyf219da52011-01-16 12:54:25 -0800168 }
Romain Guy4f09f542011-01-26 22:41:43 -0800169 mLayer->meshElementCount = elementCount;
Romain Guyf219da52011-01-16 12:54:25 -0800170
Romain Guy9ace8f52011-07-07 20:50:11 -0700171 const float texX = 1.0f / float(mLayer->getWidth());
172 const float texY = 1.0f / float(mLayer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800173 const float height = mLayer->layer.getHeight();
174
175 TextureVertex* mesh = mLayer->mesh;
176 uint16_t* indices = mLayer->meshIndices;
177
178 for (size_t i = 0; i < count; i++) {
179 const android::Rect* r = &rects[i];
180
181 const float u1 = r->left * texX;
182 const float v1 = (height - r->top) * texY;
183 const float u2 = r->right * texX;
184 const float v2 = (height - r->bottom) * texY;
185
186 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
187 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
188 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
189 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
190
Romain Guy4f09f542011-01-26 22:41:43 -0800191 if (rebuildIndices) {
192 uint16_t quad = i * 4;
193 int index = i * 6;
194 indices[index ] = quad; // top-left
195 indices[index + 1] = quad + 1; // top-right
196 indices[index + 2] = quad + 2; // bottom-left
197 indices[index + 3] = quad + 2; // bottom-left
198 indices[index + 4] = quad + 1; // top-right
199 indices[index + 5] = quad + 3; // bottom-right
200 }
Romain Guyf219da52011-01-16 12:54:25 -0800201 }
Romain Guyf219da52011-01-16 12:54:25 -0800202}
203
204///////////////////////////////////////////////////////////////////////////////
205// Layers management
Romain Guy6c319ca2011-01-11 14:29:25 -0800206///////////////////////////////////////////////////////////////////////////////
207
Romain Guyada830f2011-01-13 12:13:20 -0800208Layer* LayerRenderer::createLayer(uint32_t width, uint32_t height, bool isOpaque) {
Romain Guyeea60692011-07-26 20:35:55 -0700209 LAYER_RENDERER_LOGD("Requesting new render layer %dx%d", width, height);
Romain Guy1fc883b2011-01-12 14:30:59 -0800210
Romain Guya1d3c912011-12-13 14:55:06 -0800211 Caches& caches = Caches::getInstance();
212 GLuint fbo = caches.fboCache.get();
Romain Guy09b7c912011-02-02 20:28:09 -0800213 if (!fbo) {
Steve Block8564c8d2012-01-05 23:22:43 +0000214 ALOGW("Could not obtain an FBO");
Romain Guy09b7c912011-02-02 20:28:09 -0800215 return NULL;
216 }
217
Romain Guya1d3c912011-12-13 14:55:06 -0800218 caches.activeTexture(0);
219 Layer* layer = caches.layerCache.get(width, height);
Romain Guy09b7c912011-02-02 20:28:09 -0800220 if (!layer) {
Steve Block8564c8d2012-01-05 23:22:43 +0000221 ALOGW("Could not obtain a layer");
Romain Guy09b7c912011-02-02 20:28:09 -0800222 return NULL;
223 }
224
Romain Guyce4a7df2013-03-28 11:32:33 -0700225 // We first obtain a layer before comparing against the max texture size
226 // because layers are not allocated at the exact desired size. They are
227 // always created slighly larger to improve recycling
228 const uint32_t maxTextureSize = caches.maxTextureSize;
229 if (layer->getWidth() > maxTextureSize || layer->getHeight() > maxTextureSize) {
230 ALOGW("Layer exceeds max. dimensions supported by the GPU (%dx%d, max=%dx%d)",
231 width, height, maxTextureSize, maxTextureSize);
232
233 // Creating a new layer always increment its refcount by 1, this allows
234 // us to destroy the layer object if one was created for us
235 Caches::getInstance().resourceCache.decrementRefcount(layer);
236
237 return NULL;
238 }
239
Romain Guy9ace8f52011-07-07 20:50:11 -0700240 layer->setFbo(fbo);
Romain Guy09b7c912011-02-02 20:28:09 -0800241 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);
244 layer->setAlpha(255, SkXfermode::kSrcOver_Mode);
245 layer->setBlend(!isOpaque);
246 layer->setColorFilter(NULL);
Romain Guy7c25aab2012-10-18 15:05:02 -0700247 layer->setDirty(true);
Romain Guy09b7c912011-02-02 20:28:09 -0800248 layer->region.clear();
Romain Guyada830f2011-01-13 12:13:20 -0800249
Romain Guy6c319ca2011-01-11 14:29:25 -0800250 GLuint previousFbo;
251 glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*) &previousFbo);
252
Romain Guy9ace8f52011-07-07 20:50:11 -0700253 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
254 layer->bindTexture();
Romain Guy6c319ca2011-01-11 14:29:25 -0800255
Romain Guy09b7c912011-02-02 20:28:09 -0800256 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700257 if (layer->isEmpty()) {
258 layer->setEmpty(false);
Romain Guy09087642013-04-04 12:27:54 -0700259 layer->allocateTexture();
Romain Guy6c319ca2011-01-11 14:29:25 -0800260
Romain Guyce4a7df2013-03-28 11:32:33 -0700261 // This should only happen if we run out of memory
Romain Guy09b7c912011-02-02 20:28:09 -0800262 if (glGetError() != GL_NO_ERROR) {
Romain Guyce4a7df2013-03-28 11:32:33 -0700263 ALOGE("Could not allocate texture for layer (fbo=%d %dx%d)", fbo, width, height);
Romain Guy09b7c912011-02-02 20:28:09 -0800264 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guyce4a7df2013-03-28 11:32:33 -0700265 caches.resourceCache.decrementRefcount(layer);
Romain Guy09b7c912011-02-02 20:28:09 -0800266 return NULL;
267 }
Romain Guy6c319ca2011-01-11 14:29:25 -0800268 }
269
270 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700271 layer->getTexture(), 0);
Romain Guy6c319ca2011-01-11 14:29:25 -0800272
Romain Guy6c319ca2011-01-11 14:29:25 -0800273 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
274
Romain Guyada830f2011-01-13 12:13:20 -0800275 return layer;
Romain Guy6c319ca2011-01-11 14:29:25 -0800276}
277
Romain Guyada830f2011-01-13 12:13:20 -0800278bool LayerRenderer::resizeLayer(Layer* layer, uint32_t width, uint32_t height) {
279 if (layer) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700280 LAYER_RENDERER_LOGD("Resizing layer fbo = %d to %dx%d", layer->getFbo(), width, height);
Romain Guy1fc883b2011-01-12 14:30:59 -0800281
Romain Guy2055aba2013-01-18 16:42:51 -0800282 if (layer->resize(width, height)) {
Romain Guy09b7c912011-02-02 20:28:09 -0800283 layer->layer.set(0.0f, 0.0f, width, height);
Romain Guy9ace8f52011-07-07 20:50:11 -0700284 layer->texCoords.set(0.0f, height / float(layer->getHeight()),
285 width / float(layer->getWidth()), 0.0f);
Romain Guy09b7c912011-02-02 20:28:09 -0800286 } else {
Romain Guyada830f2011-01-13 12:13:20 -0800287 return false;
288 }
Romain Guy6c319ca2011-01-11 14:29:25 -0800289 }
Romain Guy09b7c912011-02-02 20:28:09 -0800290
Romain Guyada830f2011-01-13 12:13:20 -0800291 return true;
Romain Guy6c319ca2011-01-11 14:29:25 -0800292}
293
Romain Guy4a5a7152011-06-24 17:53:53 -0700294Layer* LayerRenderer::createTextureLayer(bool isOpaque) {
295 LAYER_RENDERER_LOGD("Creating new texture layer");
296
297 Layer* layer = new Layer(0, 0);
Romain Guy9ace8f52011-07-07 20:50:11 -0700298 layer->setCacheable(false);
299 layer->setTextureLayer(true);
300 layer->setBlend(!isOpaque);
301 layer->setEmpty(true);
302 layer->setFbo(0);
303 layer->setAlpha(255, SkXfermode::kSrcOver_Mode);
Romain Guy4a5a7152011-06-24 17:53:53 -0700304 layer->layer.set(0.0f, 0.0f, 0.0f, 0.0f);
Romain Guya9dc86b2011-10-11 14:06:21 -0700305 layer->texCoords.set(0.0f, 1.0f, 1.0f, 0.0f);
Romain Guy4a5a7152011-06-24 17:53:53 -0700306 layer->region.clear();
Romain Guy9ace8f52011-07-07 20:50:11 -0700307 layer->setRenderTarget(GL_NONE); // see ::updateTextureLayer()
Romain Guy4a5a7152011-06-24 17:53:53 -0700308
Romain Guya1d3c912011-12-13 14:55:06 -0800309 Caches::getInstance().activeTexture(0);
Romain Guy9ace8f52011-07-07 20:50:11 -0700310 layer->generateTexture();
Romain Guy4a5a7152011-06-24 17:53:53 -0700311
312 return layer;
313}
314
Romain Guyaa6c24c2011-04-28 18:40:04 -0700315void LayerRenderer::updateTextureLayer(Layer* layer, uint32_t width, uint32_t height,
Romain Guya9489272011-06-22 20:58:11 -0700316 bool isOpaque, GLenum renderTarget, float* transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700317 if (layer) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700318 layer->setBlend(!isOpaque);
319 layer->setSize(width, height);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700320 layer->layer.set(0.0f, 0.0f, width, height);
321 layer->region.set(width, height);
322 layer->regionRect.set(0.0f, 0.0f, width, height);
Romain Guy9ace8f52011-07-07 20:50:11 -0700323 layer->getTexTransform().load(transform);
Romain Guy8f0095c2011-05-02 17:24:22 -0700324
Romain Guy9ace8f52011-07-07 20:50:11 -0700325 if (renderTarget != layer->getRenderTarget()) {
326 layer->setRenderTarget(renderTarget);
327 layer->bindTexture();
Romain Guyd21b6e12011-11-30 20:21:23 -0800328 layer->setFilter(GL_NEAREST, false, true);
329 layer->setWrap(GL_CLAMP_TO_EDGE, false, true);
Romain Guy4a5a7152011-06-24 17:53:53 -0700330 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700331 }
332}
333
Romain Guyada830f2011-01-13 12:13:20 -0800334void LayerRenderer::destroyLayer(Layer* layer) {
335 if (layer) {
Romain Guyeea60692011-07-26 20:35:55 -0700336 LAYER_RENDERER_LOGD("Recycling layer, %dx%d fbo = %d",
337 layer->getWidth(), layer->getHeight(), layer->getFbo());
Romain Guy1fc883b2011-01-12 14:30:59 -0800338
Romain Guy09b7c912011-02-02 20:28:09 -0800339 if (!Caches::getInstance().layerCache.put(layer)) {
Romain Guyeea60692011-07-26 20:35:55 -0700340 LAYER_RENDERER_LOGD(" Destroyed!");
Chet Haase603f6de2012-09-14 15:31:25 -0700341 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy09b7c912011-02-02 20:28:09 -0800342 } else {
Romain Guyeea60692011-07-26 20:35:55 -0700343 LAYER_RENDERER_LOGD(" Cached!");
Romain Guy65b345f2011-07-27 18:51:50 -0700344#if DEBUG_LAYER_RENDERER
Romain Guyeea60692011-07-26 20:35:55 -0700345 Caches::getInstance().layerCache.dump();
346#endif
Chet Haase98d3a642012-09-26 10:27:40 -0700347 layer->removeFbo();
Romain Guy09b7c912011-02-02 20:28:09 -0800348 layer->region.clear();
349 }
Romain Guyada830f2011-01-13 12:13:20 -0800350 }
Romain Guy6c319ca2011-01-11 14:29:25 -0800351}
352
Romain Guyada830f2011-01-13 12:13:20 -0800353void LayerRenderer::destroyLayerDeferred(Layer* layer) {
354 if (layer) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700355 LAYER_RENDERER_LOGD("Deferring layer destruction, fbo = %d", layer->getFbo());
Dave Burke56257af2012-09-25 20:30:09 -0700356
Romain Guyada830f2011-01-13 12:13:20 -0800357 Caches::getInstance().deleteLayerDeferred(layer);
358 }
Romain Guy57066eb2011-01-12 12:53:32 -0800359}
360
Romain Guy9c4b79a2011-11-10 19:23:58 -0800361void LayerRenderer::flushLayer(Layer* layer) {
362#ifdef GL_EXT_discard_framebuffer
363 GLuint fbo = layer->getFbo();
364 if (layer && fbo) {
365 // If possible, discard any enqueud operations on deferred
366 // rendering architectures
Romain Guy3bbacf22013-02-06 16:51:04 -0800367 if (Extensions::getInstance().hasDiscardFramebuffer()) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800368 GLuint previousFbo;
369 glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*) &previousFbo);
Romain Guy9c4b79a2011-11-10 19:23:58 -0800370 if (fbo != previousFbo) glBindFramebuffer(GL_FRAMEBUFFER, fbo);
Romain Guy45e4c3d2012-09-11 17:17:07 -0700371
372 const GLenum attachments[] = { GL_COLOR_ATTACHMENT0 };
373 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
Romain Guy9c4b79a2011-11-10 19:23:58 -0800374
375 if (fbo != previousFbo) glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
376 }
377 }
378#endif
379}
380
Romain Guy77a81162011-06-14 16:45:55 -0700381bool LayerRenderer::copyLayer(Layer* layer, SkBitmap* bitmap) {
382 Caches& caches = Caches::getInstance();
Chris Craik710f46d2012-09-17 17:25:49 -0700383 if (layer && bitmap->width() <= caches.maxTextureSize &&
Romain Guy77a81162011-06-14 16:45:55 -0700384 bitmap->height() <= caches.maxTextureSize) {
385
386 GLuint fbo = caches.fboCache.get();
387 if (!fbo) {
Steve Block8564c8d2012-01-05 23:22:43 +0000388 ALOGW("Could not obtain an FBO");
Romain Guy77a81162011-06-14 16:45:55 -0700389 return false;
390 }
391
Romain Guyd6b2a002011-06-17 17:45:59 -0700392 SkAutoLockPixels alp(*bitmap);
393
Romain Guy77a81162011-06-14 16:45:55 -0700394 GLuint texture;
395 GLuint previousFbo;
Chris Craik710f46d2012-09-17 17:25:49 -0700396 GLuint previousViewport[4];
Romain Guy77a81162011-06-14 16:45:55 -0700397
398 GLenum format;
399 GLenum type;
400
Romain Guyd6b2a002011-06-17 17:45:59 -0700401 GLenum error = GL_NO_ERROR;
402 bool status = false;
403
Romain Guy77a81162011-06-14 16:45:55 -0700404 switch (bitmap->config()) {
405 case SkBitmap::kA8_Config:
406 format = GL_ALPHA;
407 type = GL_UNSIGNED_BYTE;
408 break;
409 case SkBitmap::kRGB_565_Config:
410 format = GL_RGB;
411 type = GL_UNSIGNED_SHORT_5_6_5;
412 break;
413 case SkBitmap::kARGB_4444_Config:
414 format = GL_RGBA;
415 type = GL_UNSIGNED_SHORT_4_4_4_4;
416 break;
417 case SkBitmap::kARGB_8888_Config:
418 default:
419 format = GL_RGBA;
420 type = GL_UNSIGNED_BYTE;
421 break;
422 }
423
Romain Guy9ace8f52011-07-07 20:50:11 -0700424 float alpha = layer->getAlpha();
425 SkXfermode::Mode mode = layer->getMode();
Chris Craik710f46d2012-09-17 17:25:49 -0700426 GLuint previousLayerFbo = layer->getFbo();
Romain Guyd6b2a002011-06-17 17:45:59 -0700427
Romain Guy9ace8f52011-07-07 20:50:11 -0700428 layer->setAlpha(255, SkXfermode::kSrc_Mode);
429 layer->setFbo(fbo);
Romain Guyd6b2a002011-06-17 17:45:59 -0700430
Romain Guy77a81162011-06-14 16:45:55 -0700431 glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*) &previousFbo);
Chris Craik710f46d2012-09-17 17:25:49 -0700432 glGetIntegerv(GL_VIEWPORT, (GLint*) &previousViewport);
Romain Guy77a81162011-06-14 16:45:55 -0700433 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
434
435 glGenTextures(1, &texture);
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 Guya1d3c912011-12-13 14:55:06 -0800438 caches.activeTexture(0);
Romain Guy77a81162011-06-14 16:45:55 -0700439 glBindTexture(GL_TEXTURE_2D, texture);
440
Romain Guye49d7ec2012-09-07 18:42:38 -0700441 glPixelStorei(GL_PACK_ALIGNMENT, bitmap->bytesPerPixel());
442
Romain Guyec19b4a2011-07-07 21:05:04 -0700443 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
444 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
Romain Guy77a81162011-06-14 16:45:55 -0700445
446 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
447 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
448
449 glTexImage2D(GL_TEXTURE_2D, 0, format, bitmap->width(), bitmap->height(),
450 0, format, type, NULL);
Romain Guyd6b2a002011-06-17 17:45:59 -0700451 if ((error = glGetError()) != GL_NO_ERROR) goto error;
452
Romain Guy77a81162011-06-14 16:45:55 -0700453 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
454 GL_TEXTURE_2D, texture, 0);
Romain Guyd6b2a002011-06-17 17:45:59 -0700455 if ((error = glGetError()) != GL_NO_ERROR) goto error;
Romain Guy77a81162011-06-14 16:45:55 -0700456
Romain Guyd6b2a002011-06-17 17:45:59 -0700457 {
458 LayerRenderer renderer(layer);
459 renderer.setViewport(bitmap->width(), bitmap->height());
460 renderer.OpenGLRenderer::prepareDirty(0.0f, 0.0f,
Romain Guy9ace8f52011-07-07 20:50:11 -0700461 bitmap->width(), bitmap->height(), !layer->isBlend());
Romain Guya9dc86b2011-10-11 14:06:21 -0700462
Romain Guy586cae32012-07-13 15:28:31 -0700463 caches.disableScissor();
Romain Guya9dc86b2011-10-11 14:06:21 -0700464 renderer.translate(0.0f, bitmap->height());
465 renderer.scale(1.0f, -1.0f);
466
467 mat4 texTransform(layer->getTexTransform());
468
469 mat4 invert;
470 invert.translate(0.0f, 1.0f, 0.0f);
471 invert.scale(1.0f, -1.0f, 1.0f);
472 layer->getTexTransform().multiply(invert);
473
Romain Guyd6b2a002011-06-17 17:45:59 -0700474 if ((error = glGetError()) != GL_NO_ERROR) goto error;
Romain Guy77a81162011-06-14 16:45:55 -0700475
Romain Guyd6b2a002011-06-17 17:45:59 -0700476 {
477 Rect bounds;
478 bounds.set(0.0f, 0.0f, bitmap->width(), bitmap->height());
479 renderer.drawTextureLayer(layer, bounds);
Romain Guy77a81162011-06-14 16:45:55 -0700480
Romain Guyd6b2a002011-06-17 17:45:59 -0700481 glReadPixels(0, 0, bitmap->width(), bitmap->height(), format,
482 type, bitmap->getPixels());
Romain Guy77a81162011-06-14 16:45:55 -0700483
Romain Guyd6b2a002011-06-17 17:45:59 -0700484 if ((error = glGetError()) != GL_NO_ERROR) goto error;
485 }
Romain Guy77a81162011-06-14 16:45:55 -0700486
Romain Guya9dc86b2011-10-11 14:06:21 -0700487 layer->getTexTransform().load(texTransform);
Romain Guyd6b2a002011-06-17 17:45:59 -0700488 status = true;
489 }
Romain Guy77a81162011-06-14 16:45:55 -0700490
Romain Guyd6b2a002011-06-17 17:45:59 -0700491error:
492#if DEBUG_OPENGL
493 if (error != GL_NO_ERROR) {
Steve Block5baa3a62011-12-20 16:23:08 +0000494 ALOGD("GL error while copying layer into bitmap = 0x%x", error);
Romain Guyd6b2a002011-06-17 17:45:59 -0700495 }
496#endif
Romain Guy77a81162011-06-14 16:45:55 -0700497
498 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guy9ace8f52011-07-07 20:50:11 -0700499 layer->setAlpha(alpha, mode);
Chris Craik710f46d2012-09-17 17:25:49 -0700500 layer->setFbo(previousLayerFbo);
Romain Guy77a81162011-06-14 16:45:55 -0700501 glDeleteTextures(1, &texture);
502 caches.fboCache.put(fbo);
Chris Craik710f46d2012-09-17 17:25:49 -0700503 glViewport(previousViewport[0], previousViewport[1],
504 previousViewport[2], previousViewport[3]);
Romain Guy77a81162011-06-14 16:45:55 -0700505
Romain Guyd6b2a002011-06-17 17:45:59 -0700506 return status;
Romain Guy77a81162011-06-14 16:45:55 -0700507 }
508 return false;
509}
510
Romain Guy6c319ca2011-01-11 14:29:25 -0800511}; // namespace uirenderer
512}; // namespace android