blob: fc3a3b2a277bd7ba24cab4a54450dcaa435805d1 [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"
John Reckec4cefc2014-07-29 09:49:13 -070018#define ATRACE_TAG ATRACE_TAG_VIEW
Romain Guy6c319ca2011-01-11 14:29:25 -080019
Romain Guy3a3133d2011-02-01 22:59:58 -080020#include <ui/Rect.h>
21
Romain Guy7c25aab2012-10-18 15:05:02 -070022#include <private/hwui/DrawGlInfo.h>
23
John Reck3b202512014-06-23 13:13:08 -070024#include "RenderState.h"
Romain Guy09b7c912011-02-02 20:28:09 -080025#include "LayerCache.h"
Romain Guy6c319ca2011-01-11 14:29:25 -080026#include "LayerRenderer.h"
Romain Guyaa6c24c2011-04-28 18:40:04 -070027#include "Matrix.h"
Romain Guy1fc883b2011-01-12 14:30:59 -080028#include "Properties.h"
Romain Guy3a3133d2011-02-01 22:59:58 -080029#include "Rect.h"
Chris Craik70850ea2014-11-18 10:49:23 -080030#include "utils/TraceUtils.h"
Romain Guy6c319ca2011-01-11 14:29:25 -080031
32namespace android {
33namespace uirenderer {
34
35///////////////////////////////////////////////////////////////////////////////
36// Rendering
37///////////////////////////////////////////////////////////////////////////////
38
John Reck3b202512014-06-23 13:13:08 -070039LayerRenderer::LayerRenderer(RenderState& renderState, Layer* layer)
40 : OpenGLRenderer(renderState)
41 , mLayer(layer) {
Romain Guy79537452011-10-12 13:48:51 -070042}
43
44LayerRenderer::~LayerRenderer() {
45}
46
Tom Hudson107843d2014-09-08 11:26:26 -040047void LayerRenderer::prepareDirty(float left, float top, float right, float bottom,
Romain Guy7c25aab2012-10-18 15:05:02 -070048 bool opaque) {
Romain Guy9ace8f52011-07-07 20:50:11 -070049 LAYER_RENDERER_LOGD("Rendering into layer, fbo = %d", mLayer->getFbo());
Romain Guy1fc883b2011-01-12 14:30:59 -080050
John Reck3b202512014-06-23 13:13:08 -070051 renderState().bindFramebuffer(mLayer->getFbo());
Romain Guy62687ec2011-02-02 15:44:19 -080052
Romain Guy09b7c912011-02-02 20:28:09 -080053 const float width = mLayer->layer.getWidth();
54 const float height = mLayer->layer.getHeight();
55
Romain Guy3a3133d2011-02-01 22:59:58 -080056 Rect dirty(left, top, right, bottom);
57 if (dirty.isEmpty() || (dirty.left <= 0 && dirty.top <= 0 &&
Romain Guy09b7c912011-02-02 20:28:09 -080058 dirty.right >= width && dirty.bottom >= height)) {
Romain Guy3a3133d2011-02-01 22:59:58 -080059 mLayer->region.clear();
Romain Guy09b7c912011-02-02 20:28:09 -080060 dirty.set(0.0f, 0.0f, width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -080061 } else {
Romain Guy09b7c912011-02-02 20:28:09 -080062 dirty.intersect(0.0f, 0.0f, width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -080063 android::Rect r(dirty.left, dirty.top, dirty.right, dirty.bottom);
64 mLayer->region.subtractSelf(r);
65 }
Romain Guyc3fedaf2013-01-29 17:26:25 -080066 mLayer->clipRect.set(dirty);
Romain Guyc88e3572011-01-22 00:32:12 -080067
Tom Hudson107843d2014-09-08 11:26:26 -040068 OpenGLRenderer::prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, opaque);
Romain Guy6c319ca2011-01-11 14:29:25 -080069}
70
Tom Hudson107843d2014-09-08 11:26:26 -040071void LayerRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
Romain Guy7c25aab2012-10-18 15:05:02 -070072 if (mLayer->isDirty()) {
73 getCaches().disableScissor();
74 glClear(GL_COLOR_BUFFER_BIT);
75
76 getCaches().resetScissor();
77 mLayer->setDirty(false);
Tom Hudson107843d2014-09-08 11:26:26 -040078 } else {
79 OpenGLRenderer::clear(left, top, right, bottom, opaque);
Romain Guy7c25aab2012-10-18 15:05:02 -070080 }
Romain Guy7c25aab2012-10-18 15:05:02 -070081}
82
Tom Hudson107843d2014-09-08 11:26:26 -040083bool LayerRenderer::finish() {
84 bool retval = 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()
Tom Hudson107843d2014-09-08 11:26:26 -040092 return retval;
Romain Guy42f3a4b2011-01-19 13:42:26 -080093}
94
Tom Hudson984162f2014-10-10 13:38:16 -040095GLuint LayerRenderer::onGetTargetFbo() 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 {
Tom Hudson984162f2014-10-10 13:38:16 -0400120 if (mState.currentFlags() & Snapshot::kFlagFboTarget) {
Romain Guyf219da52011-01-16 12:54:25 -0800121 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;
Romain Guyf219da52011-01-16 12:54:25 -0800133 mLayer->mesh = NULL;
Romain Guyf219da52011-01-16 12:54:25 -0800134 mLayer->meshElementCount = 0;
135 }
Romain Guy40667672011-03-18 14:34:03 -0700136
Romain Guy9fc27812011-04-27 14:21:41 -0700137 mLayer->setRegionAsRect();
Romain Guyf219da52011-01-16 12:54:25 -0800138 return;
139 }
140
Chris Craik6c5b9be2013-02-27 14:03:19 -0800141 // avoid T-junctions as they cause artifacts in between the resultant
142 // geometry when complex transforms occur.
143 // TODO: generate the safeRegion only if necessary based on drawing transform (see
144 // OpenGLRenderer::composeLayerRegion())
145 Region safeRegion = Region::createTJunctionFreeRegion(mLayer->region);
146
Romain Guyf219da52011-01-16 12:54:25 -0800147 size_t count;
Chris Craik6c5b9be2013-02-27 14:03:19 -0800148 const android::Rect* rects = safeRegion.getArray(&count);
Romain Guyf219da52011-01-16 12:54:25 -0800149
150 GLsizei elementCount = count * 6;
151
152 if (mLayer->mesh && mLayer->meshElementCount < elementCount) {
Romain Guy96885eb2013-03-26 15:05:58 -0700153 delete[] mLayer->mesh;
Romain Guyf219da52011-01-16 12:54:25 -0800154 mLayer->mesh = NULL;
Romain Guyf219da52011-01-16 12:54:25 -0800155 }
156
157 if (!mLayer->mesh) {
158 mLayer->mesh = new TextureVertex[count * 4];
Romain Guyf219da52011-01-16 12:54:25 -0800159 }
Romain Guy4f09f542011-01-26 22:41:43 -0800160 mLayer->meshElementCount = elementCount;
Romain Guyf219da52011-01-16 12:54:25 -0800161
Romain Guy9ace8f52011-07-07 20:50:11 -0700162 const float texX = 1.0f / float(mLayer->getWidth());
163 const float texY = 1.0f / float(mLayer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800164 const float height = mLayer->layer.getHeight();
165
166 TextureVertex* mesh = mLayer->mesh;
Romain Guyf219da52011-01-16 12:54:25 -0800167
168 for (size_t i = 0; i < count; i++) {
169 const android::Rect* r = &rects[i];
170
171 const float u1 = r->left * texX;
172 const float v1 = (height - r->top) * texY;
173 const float u2 = r->right * texX;
174 const float v2 = (height - r->bottom) * texY;
175
176 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
177 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
178 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
179 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
Romain Guyf219da52011-01-16 12:54:25 -0800180 }
Romain Guyf219da52011-01-16 12:54:25 -0800181}
182
183///////////////////////////////////////////////////////////////////////////////
184// Layers management
Romain Guy6c319ca2011-01-11 14:29:25 -0800185///////////////////////////////////////////////////////////////////////////////
186
John Reck3b202512014-06-23 13:13:08 -0700187Layer* LayerRenderer::createRenderLayer(RenderState& renderState, uint32_t width, uint32_t height) {
Chris Craik70850ea2014-11-18 10:49:23 -0800188 ATRACE_FORMAT("Allocate %ux%u HW Layer", width, height);
Romain Guyeea60692011-07-26 20:35:55 -0700189 LAYER_RENDERER_LOGD("Requesting new render layer %dx%d", width, height);
Romain Guy1fc883b2011-01-12 14:30:59 -0800190
Romain Guya1d3c912011-12-13 14:55:06 -0800191 Caches& caches = Caches::getInstance();
192 GLuint fbo = caches.fboCache.get();
Romain Guy09b7c912011-02-02 20:28:09 -0800193 if (!fbo) {
Steve Block8564c8d2012-01-05 23:22:43 +0000194 ALOGW("Could not obtain an FBO");
Romain Guy09b7c912011-02-02 20:28:09 -0800195 return NULL;
196 }
197
Romain Guya1d3c912011-12-13 14:55:06 -0800198 caches.activeTexture(0);
John Reck3b202512014-06-23 13:13:08 -0700199 Layer* layer = caches.layerCache.get(renderState, width, height);
Romain Guy09b7c912011-02-02 20:28:09 -0800200 if (!layer) {
Steve Block8564c8d2012-01-05 23:22:43 +0000201 ALOGW("Could not obtain a layer");
Romain Guy09b7c912011-02-02 20:28:09 -0800202 return NULL;
203 }
204
Romain Guyce4a7df2013-03-28 11:32:33 -0700205 // We first obtain a layer before comparing against the max texture size
206 // because layers are not allocated at the exact desired size. They are
207 // always created slighly larger to improve recycling
208 const uint32_t maxTextureSize = caches.maxTextureSize;
209 if (layer->getWidth() > maxTextureSize || layer->getHeight() > maxTextureSize) {
210 ALOGW("Layer exceeds max. dimensions supported by the GPU (%dx%d, max=%dx%d)",
211 width, height, maxTextureSize, maxTextureSize);
212
213 // Creating a new layer always increment its refcount by 1, this allows
214 // us to destroy the layer object if one was created for us
John Reck0e89e2b2014-10-31 14:49:06 -0700215 layer->decStrong(0);
Romain Guyce4a7df2013-03-28 11:32:33 -0700216
217 return NULL;
218 }
219
Romain Guy9ace8f52011-07-07 20:50:11 -0700220 layer->setFbo(fbo);
Romain Guy09b7c912011-02-02 20:28:09 -0800221 layer->layer.set(0.0f, 0.0f, width, height);
Romain Guy9ace8f52011-07-07 20:50:11 -0700222 layer->texCoords.set(0.0f, height / float(layer->getHeight()),
223 width / float(layer->getWidth()), 0.0f);
224 layer->setAlpha(255, SkXfermode::kSrcOver_Mode);
Romain Guy9ace8f52011-07-07 20:50:11 -0700225 layer->setColorFilter(NULL);
Romain Guy7c25aab2012-10-18 15:05:02 -0700226 layer->setDirty(true);
Romain Guy09b7c912011-02-02 20:28:09 -0800227 layer->region.clear();
Romain Guyada830f2011-01-13 12:13:20 -0800228
John Reck3b202512014-06-23 13:13:08 -0700229 GLuint previousFbo = renderState.getFramebuffer();
Romain Guy6c319ca2011-01-11 14:29:25 -0800230
John Reck3b202512014-06-23 13:13:08 -0700231 renderState.bindFramebuffer(layer->getFbo());
Romain Guy9ace8f52011-07-07 20:50:11 -0700232 layer->bindTexture();
Romain Guy6c319ca2011-01-11 14:29:25 -0800233
Romain Guy09b7c912011-02-02 20:28:09 -0800234 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700235 if (layer->isEmpty()) {
236 layer->setEmpty(false);
Romain Guy09087642013-04-04 12:27:54 -0700237 layer->allocateTexture();
Romain Guy6c319ca2011-01-11 14:29:25 -0800238
Romain Guyce4a7df2013-03-28 11:32:33 -0700239 // This should only happen if we run out of memory
Romain Guy09b7c912011-02-02 20:28:09 -0800240 if (glGetError() != GL_NO_ERROR) {
Romain Guyce4a7df2013-03-28 11:32:33 -0700241 ALOGE("Could not allocate texture for layer (fbo=%d %dx%d)", fbo, width, height);
John Reck3b202512014-06-23 13:13:08 -0700242 renderState.bindFramebuffer(previousFbo);
John Reck0e89e2b2014-10-31 14:49:06 -0700243 layer->decStrong(0);
Romain Guy09b7c912011-02-02 20:28:09 -0800244 return NULL;
245 }
Romain Guy6c319ca2011-01-11 14:29:25 -0800246 }
247
248 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700249 layer->getTexture(), 0);
Romain Guy6c319ca2011-01-11 14:29:25 -0800250
John Reck3b202512014-06-23 13:13:08 -0700251 renderState.bindFramebuffer(previousFbo);
Romain Guy6c319ca2011-01-11 14:29:25 -0800252
Romain Guyada830f2011-01-13 12:13:20 -0800253 return layer;
Romain Guy6c319ca2011-01-11 14:29:25 -0800254}
255
Romain Guyada830f2011-01-13 12:13:20 -0800256bool LayerRenderer::resizeLayer(Layer* layer, uint32_t width, uint32_t height) {
257 if (layer) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700258 LAYER_RENDERER_LOGD("Resizing layer fbo = %d to %dx%d", layer->getFbo(), width, height);
Romain Guy1fc883b2011-01-12 14:30:59 -0800259
Romain Guy2055aba2013-01-18 16:42:51 -0800260 if (layer->resize(width, height)) {
Romain Guy09b7c912011-02-02 20:28:09 -0800261 layer->layer.set(0.0f, 0.0f, width, height);
Romain Guy9ace8f52011-07-07 20:50:11 -0700262 layer->texCoords.set(0.0f, height / float(layer->getHeight()),
263 width / float(layer->getWidth()), 0.0f);
Romain Guy09b7c912011-02-02 20:28:09 -0800264 } else {
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
John Reck3b202512014-06-23 13:13:08 -0700272Layer* LayerRenderer::createTextureLayer(RenderState& renderState) {
Romain Guy4a5a7152011-06-24 17:53:53 -0700273 LAYER_RENDERER_LOGD("Creating new texture layer");
274
Chris Craik8a226d22014-09-08 16:40:21 -0700275 Layer* layer = new Layer(Layer::kType_Texture, renderState, 0, 0);
Romain Guy9ace8f52011-07-07 20:50:11 -0700276 layer->setCacheable(false);
Romain Guy9ace8f52011-07-07 20:50:11 -0700277 layer->setEmpty(true);
278 layer->setFbo(0);
279 layer->setAlpha(255, SkXfermode::kSrcOver_Mode);
Romain Guy4a5a7152011-06-24 17:53:53 -0700280 layer->layer.set(0.0f, 0.0f, 0.0f, 0.0f);
Romain Guya9dc86b2011-10-11 14:06:21 -0700281 layer->texCoords.set(0.0f, 1.0f, 1.0f, 0.0f);
Romain Guy4a5a7152011-06-24 17:53:53 -0700282 layer->region.clear();
Romain Guy9ace8f52011-07-07 20:50:11 -0700283 layer->setRenderTarget(GL_NONE); // see ::updateTextureLayer()
Romain Guy4a5a7152011-06-24 17:53:53 -0700284
Romain Guya1d3c912011-12-13 14:55:06 -0800285 Caches::getInstance().activeTexture(0);
Romain Guy9ace8f52011-07-07 20:50:11 -0700286 layer->generateTexture();
Romain Guy4a5a7152011-06-24 17:53:53 -0700287
288 return layer;
289}
290
Romain Guyaa6c24c2011-04-28 18:40:04 -0700291void LayerRenderer::updateTextureLayer(Layer* layer, uint32_t width, uint32_t height,
Chris Craik9757ac02014-02-25 18:50:17 -0800292 bool isOpaque, bool forceFilter, GLenum renderTarget, float* textureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700293 if (layer) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700294 layer->setBlend(!isOpaque);
Chris Craik9757ac02014-02-25 18:50:17 -0800295 layer->setForceFilter(forceFilter);
Romain Guy9ace8f52011-07-07 20:50:11 -0700296 layer->setSize(width, height);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700297 layer->layer.set(0.0f, 0.0f, width, height);
298 layer->region.set(width, height);
299 layer->regionRect.set(0.0f, 0.0f, width, height);
Chris Craik9757ac02014-02-25 18:50:17 -0800300 layer->getTexTransform().load(textureTransform);
Romain Guy8f0095c2011-05-02 17:24:22 -0700301
Romain Guy9ace8f52011-07-07 20:50:11 -0700302 if (renderTarget != layer->getRenderTarget()) {
303 layer->setRenderTarget(renderTarget);
304 layer->bindTexture();
Romain Guyd21b6e12011-11-30 20:21:23 -0800305 layer->setFilter(GL_NEAREST, false, true);
306 layer->setWrap(GL_CLAMP_TO_EDGE, false, true);
Romain Guy4a5a7152011-06-24 17:53:53 -0700307 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700308 }
309}
310
Romain Guyada830f2011-01-13 12:13:20 -0800311void LayerRenderer::destroyLayer(Layer* layer) {
312 if (layer) {
Chris Craik70850ea2014-11-18 10:49:23 -0800313 ATRACE_FORMAT("Destroy %ux%u HW Layer", layer->getWidth(), layer->getHeight());
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!");
John Reck0e89e2b2014-10-31 14:49:06 -0700319 layer->decStrong(0);
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
John Reck3b202512014-06-23 13:13:08 -0700331void LayerRenderer::flushLayer(RenderState& renderState, Layer* layer) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800332#ifdef GL_EXT_discard_framebuffer
Digish Pandya1fa4cef2014-06-12 10:42:54 +0530333 if (!layer) return;
334
Romain Guy9c4b79a2011-11-10 19:23:58 -0800335 GLuint fbo = layer->getFbo();
Digish Pandya1fa4cef2014-06-12 10:42:54 +0530336 if (fbo) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800337 // If possible, discard any enqueud operations on deferred
338 // rendering architectures
Romain Guy3bbacf22013-02-06 16:51:04 -0800339 if (Extensions::getInstance().hasDiscardFramebuffer()) {
John Reck3b202512014-06-23 13:13:08 -0700340 GLuint previousFbo = renderState.getFramebuffer();
341 if (fbo != previousFbo) {
342 renderState.bindFramebuffer(fbo);
343 }
Romain Guy45e4c3d2012-09-11 17:17:07 -0700344
345 const GLenum attachments[] = { GL_COLOR_ATTACHMENT0 };
346 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
Romain Guy9c4b79a2011-11-10 19:23:58 -0800347
John Reck3b202512014-06-23 13:13:08 -0700348 if (fbo != previousFbo) {
349 renderState.bindFramebuffer(previousFbo);
350 }
Romain Guy9c4b79a2011-11-10 19:23:58 -0800351 }
352 }
353#endif
354}
355
John Reck3b202512014-06-23 13:13:08 -0700356bool LayerRenderer::copyLayer(RenderState& renderState, Layer* layer, SkBitmap* bitmap) {
Romain Guy77a81162011-06-14 16:45:55 -0700357 Caches& caches = Caches::getInstance();
Chris Craik710f46d2012-09-17 17:25:49 -0700358 if (layer && bitmap->width() <= caches.maxTextureSize &&
Romain Guy77a81162011-06-14 16:45:55 -0700359 bitmap->height() <= caches.maxTextureSize) {
360
361 GLuint fbo = caches.fboCache.get();
362 if (!fbo) {
Steve Block8564c8d2012-01-05 23:22:43 +0000363 ALOGW("Could not obtain an FBO");
Romain Guy77a81162011-06-14 16:45:55 -0700364 return false;
365 }
366
Romain Guyd6b2a002011-06-17 17:45:59 -0700367 SkAutoLockPixels alp(*bitmap);
368
Romain Guy77a81162011-06-14 16:45:55 -0700369 GLuint texture;
370 GLuint previousFbo;
John Reck3b202512014-06-23 13:13:08 -0700371 GLsizei previousViewportWidth;
372 GLsizei previousViewportHeight;
Romain Guy77a81162011-06-14 16:45:55 -0700373
374 GLenum format;
375 GLenum type;
376
Romain Guyd6b2a002011-06-17 17:45:59 -0700377 GLenum error = GL_NO_ERROR;
378 bool status = false;
379
Mike Reed1103b322014-07-08 12:36:44 -0400380 switch (bitmap->colorType()) {
381 case kAlpha_8_SkColorType:
Romain Guy77a81162011-06-14 16:45:55 -0700382 format = GL_ALPHA;
383 type = GL_UNSIGNED_BYTE;
384 break;
Mike Reed1103b322014-07-08 12:36:44 -0400385 case kRGB_565_SkColorType:
Romain Guy77a81162011-06-14 16:45:55 -0700386 format = GL_RGB;
387 type = GL_UNSIGNED_SHORT_5_6_5;
388 break;
Mike Reed1103b322014-07-08 12:36:44 -0400389 case kARGB_4444_SkColorType:
Romain Guy77a81162011-06-14 16:45:55 -0700390 format = GL_RGBA;
391 type = GL_UNSIGNED_SHORT_4_4_4_4;
392 break;
Mike Reed1103b322014-07-08 12:36:44 -0400393 case kN32_SkColorType:
Romain Guy77a81162011-06-14 16:45:55 -0700394 default:
395 format = GL_RGBA;
396 type = GL_UNSIGNED_BYTE;
397 break;
398 }
399
Romain Guy9ace8f52011-07-07 20:50:11 -0700400 float alpha = layer->getAlpha();
401 SkXfermode::Mode mode = layer->getMode();
Chris Craik710f46d2012-09-17 17:25:49 -0700402 GLuint previousLayerFbo = layer->getFbo();
Romain Guyd6b2a002011-06-17 17:45:59 -0700403
Romain Guy9ace8f52011-07-07 20:50:11 -0700404 layer->setAlpha(255, SkXfermode::kSrc_Mode);
405 layer->setFbo(fbo);
Romain Guyd6b2a002011-06-17 17:45:59 -0700406
John Reck3b202512014-06-23 13:13:08 -0700407 previousFbo = renderState.getFramebuffer();
408 renderState.getViewport(&previousViewportWidth, &previousViewportHeight);
409 renderState.bindFramebuffer(fbo);
Romain Guy77a81162011-06-14 16:45:55 -0700410
411 glGenTextures(1, &texture);
Romain Guyd6b2a002011-06-17 17:45:59 -0700412 if ((error = glGetError()) != GL_NO_ERROR) goto error;
Romain Guy77a81162011-06-14 16:45:55 -0700413
Romain Guya1d3c912011-12-13 14:55:06 -0800414 caches.activeTexture(0);
Romain Guy8aa195d2013-06-04 18:00:09 -0700415 caches.bindTexture(texture);
Romain Guy77a81162011-06-14 16:45:55 -0700416
Romain Guye49d7ec2012-09-07 18:42:38 -0700417 glPixelStorei(GL_PACK_ALIGNMENT, bitmap->bytesPerPixel());
418
Romain Guyec19b4a2011-07-07 21:05:04 -0700419 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
420 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
Romain Guy77a81162011-06-14 16:45:55 -0700421
422 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
423 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
424
425 glTexImage2D(GL_TEXTURE_2D, 0, format, bitmap->width(), bitmap->height(),
426 0, format, type, NULL);
Romain Guyd6b2a002011-06-17 17:45:59 -0700427 if ((error = glGetError()) != GL_NO_ERROR) goto error;
428
Romain Guy77a81162011-06-14 16:45:55 -0700429 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
430 GL_TEXTURE_2D, texture, 0);
Romain Guyd6b2a002011-06-17 17:45:59 -0700431 if ((error = glGetError()) != GL_NO_ERROR) goto error;
Romain Guy77a81162011-06-14 16:45:55 -0700432
Romain Guyd6b2a002011-06-17 17:45:59 -0700433 {
John Reck3b202512014-06-23 13:13:08 -0700434 LayerRenderer renderer(renderState, layer);
Romain Guyd6b2a002011-06-17 17:45:59 -0700435 renderer.setViewport(bitmap->width(), bitmap->height());
436 renderer.OpenGLRenderer::prepareDirty(0.0f, 0.0f,
Romain Guy9ace8f52011-07-07 20:50:11 -0700437 bitmap->width(), bitmap->height(), !layer->isBlend());
Romain Guya9dc86b2011-10-11 14:06:21 -0700438
Romain Guy586cae32012-07-13 15:28:31 -0700439 caches.disableScissor();
Romain Guya9dc86b2011-10-11 14:06:21 -0700440 renderer.translate(0.0f, bitmap->height());
441 renderer.scale(1.0f, -1.0f);
442
443 mat4 texTransform(layer->getTexTransform());
444
445 mat4 invert;
Romain Guy4c2547f2013-06-11 16:19:24 -0700446 invert.translate(0.0f, 1.0f);
Romain Guya9dc86b2011-10-11 14:06:21 -0700447 invert.scale(1.0f, -1.0f, 1.0f);
448 layer->getTexTransform().multiply(invert);
449
Romain Guyd6b2a002011-06-17 17:45:59 -0700450 if ((error = glGetError()) != GL_NO_ERROR) goto error;
Romain Guy77a81162011-06-14 16:45:55 -0700451
Romain Guyd6b2a002011-06-17 17:45:59 -0700452 {
453 Rect bounds;
454 bounds.set(0.0f, 0.0f, bitmap->width(), bitmap->height());
455 renderer.drawTextureLayer(layer, bounds);
Romain Guy77a81162011-06-14 16:45:55 -0700456
Romain Guyd6b2a002011-06-17 17:45:59 -0700457 glReadPixels(0, 0, bitmap->width(), bitmap->height(), format,
458 type, bitmap->getPixels());
Romain Guy77a81162011-06-14 16:45:55 -0700459
Romain Guyd6b2a002011-06-17 17:45:59 -0700460 if ((error = glGetError()) != GL_NO_ERROR) goto error;
461 }
Romain Guy77a81162011-06-14 16:45:55 -0700462
Romain Guya9dc86b2011-10-11 14:06:21 -0700463 layer->getTexTransform().load(texTransform);
Romain Guyd6b2a002011-06-17 17:45:59 -0700464 status = true;
465 }
Romain Guy77a81162011-06-14 16:45:55 -0700466
Romain Guyd6b2a002011-06-17 17:45:59 -0700467error:
468#if DEBUG_OPENGL
469 if (error != GL_NO_ERROR) {
Steve Block5baa3a62011-12-20 16:23:08 +0000470 ALOGD("GL error while copying layer into bitmap = 0x%x", error);
Romain Guyd6b2a002011-06-17 17:45:59 -0700471 }
472#endif
Romain Guy77a81162011-06-14 16:45:55 -0700473
John Reck3b202512014-06-23 13:13:08 -0700474 renderState.bindFramebuffer(previousFbo);
Romain Guy9ace8f52011-07-07 20:50:11 -0700475 layer->setAlpha(alpha, mode);
Chris Craik710f46d2012-09-17 17:25:49 -0700476 layer->setFbo(previousLayerFbo);
Romain Guybe1b1272013-06-06 14:02:54 -0700477 caches.deleteTexture(texture);
Romain Guy77a81162011-06-14 16:45:55 -0700478 caches.fboCache.put(fbo);
John Reck3b202512014-06-23 13:13:08 -0700479 renderState.setViewport(previousViewportWidth, previousViewportHeight);
Romain Guy77a81162011-06-14 16:45:55 -0700480
Romain Guyd6b2a002011-06-17 17:45:59 -0700481 return status;
Romain Guy77a81162011-06-14 16:45:55 -0700482 }
483 return false;
484}
485
Romain Guy6c319ca2011-01-11 14:29:25 -0800486}; // namespace uirenderer
487}; // namespace android