blob: 223bdf06dca95789b4ef3db41af97a4394812b9f [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 Guy09b7c912011-02-02 20:28:09 -080020#include "LayerCache.h"
Romain Guy6c319ca2011-01-11 14:29:25 -080021#include "LayerRenderer.h"
Romain Guyaa6c24c2011-04-28 18:40:04 -070022#include "Matrix.h"
Romain Guy1fc883b2011-01-12 14:30:59 -080023#include "Properties.h"
Romain Guy3a3133d2011-02-01 22:59:58 -080024#include "Rect.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080025#include "renderstate/RenderState.h"
Chris Craik70850ea2014-11-18 10:49:23 -080026#include "utils/TraceUtils.h"
Romain Guy6c319ca2011-01-11 14:29:25 -080027
Chris Craik65fe5ee2015-01-26 18:06:29 -080028#include <ui/Rect.h>
29
30#include <private/hwui/DrawGlInfo.h>
31
32
Romain Guy6c319ca2011-01-11 14:29:25 -080033namespace android {
34namespace uirenderer {
35
36///////////////////////////////////////////////////////////////////////////////
37// Rendering
38///////////////////////////////////////////////////////////////////////////////
39
John Reck3b202512014-06-23 13:13:08 -070040LayerRenderer::LayerRenderer(RenderState& renderState, Layer* layer)
41 : OpenGLRenderer(renderState)
42 , mLayer(layer) {
Romain Guy79537452011-10-12 13:48:51 -070043}
44
45LayerRenderer::~LayerRenderer() {
46}
47
Tom Hudson107843d2014-09-08 11:26:26 -040048void LayerRenderer::prepareDirty(float left, float top, float right, float bottom,
Romain Guy7c25aab2012-10-18 15:05:02 -070049 bool opaque) {
Romain Guy9ace8f52011-07-07 20:50:11 -070050 LAYER_RENDERER_LOGD("Rendering into layer, fbo = %d", mLayer->getFbo());
Romain Guy1fc883b2011-01-12 14:30:59 -080051
Chris Craik65fe5ee2015-01-26 18:06:29 -080052 mRenderState.bindFramebuffer(mLayer->getFbo());
Romain Guy62687ec2011-02-02 15:44:19 -080053
Romain Guy09b7c912011-02-02 20:28:09 -080054 const float width = mLayer->layer.getWidth();
55 const float height = mLayer->layer.getHeight();
56
Romain Guy3a3133d2011-02-01 22:59:58 -080057 Rect dirty(left, top, right, bottom);
58 if (dirty.isEmpty() || (dirty.left <= 0 && dirty.top <= 0 &&
Romain Guy09b7c912011-02-02 20:28:09 -080059 dirty.right >= width && dirty.bottom >= height)) {
Romain Guy3a3133d2011-02-01 22:59:58 -080060 mLayer->region.clear();
Romain Guy09b7c912011-02-02 20:28:09 -080061 dirty.set(0.0f, 0.0f, width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -080062 } else {
Romain Guy09b7c912011-02-02 20:28:09 -080063 dirty.intersect(0.0f, 0.0f, width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -080064 android::Rect r(dirty.left, dirty.top, dirty.right, dirty.bottom);
65 mLayer->region.subtractSelf(r);
66 }
Romain Guyc3fedaf2013-01-29 17:26:25 -080067 mLayer->clipRect.set(dirty);
Romain Guyc88e3572011-01-22 00:32:12 -080068
Tom Hudson107843d2014-09-08 11:26:26 -040069 OpenGLRenderer::prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, opaque);
Romain Guy6c319ca2011-01-11 14:29:25 -080070}
71
Tom Hudson107843d2014-09-08 11:26:26 -040072void LayerRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
Romain Guy7c25aab2012-10-18 15:05:02 -070073 if (mLayer->isDirty()) {
Chris Craik65fe5ee2015-01-26 18:06:29 -080074 mRenderState.scissor().setEnabled(false);
Romain Guy7c25aab2012-10-18 15:05:02 -070075 glClear(GL_COLOR_BUFFER_BIT);
76
Chris Craik65fe5ee2015-01-26 18:06:29 -080077 mRenderState.scissor().reset();
Romain Guy7c25aab2012-10-18 15:05:02 -070078 mLayer->setDirty(false);
Tom Hudson107843d2014-09-08 11:26:26 -040079 } else {
80 OpenGLRenderer::clear(left, top, right, bottom, opaque);
Romain Guy7c25aab2012-10-18 15:05:02 -070081 }
Romain Guy7c25aab2012-10-18 15:05:02 -070082}
83
Tom Hudson107843d2014-09-08 11:26:26 -040084bool LayerRenderer::finish() {
85 bool retval = 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()
Tom Hudson107843d2014-09-08 11:26:26 -040093 return retval;
Romain Guy42f3a4b2011-01-19 13:42:26 -080094}
95
Chris Craik6b109c72015-02-27 10:55:28 -080096GLuint LayerRenderer::getTargetFbo() const {
Romain Guy9ace8f52011-07-07 20:50:11 -070097 return mLayer->getFbo();
Romain Guy6c319ca2011-01-11 14:29:25 -080098}
99
Romain Guy624234f2013-03-05 16:43:31 -0800100bool LayerRenderer::suppressErrorChecks() const {
Romain Guy11cb6422012-09-21 00:39:43 -0700101 return true;
102}
103
Romain Guy6c319ca2011-01-11 14:29:25 -0800104///////////////////////////////////////////////////////////////////////////////
Romain Guy8ce00302013-01-15 18:51:42 -0800105// Layer support
Romain Guyf219da52011-01-16 12:54:25 -0800106///////////////////////////////////////////////////////////////////////////////
107
Romain Guy624234f2013-03-05 16:43:31 -0800108bool LayerRenderer::hasLayer() const {
Romain Guyf219da52011-01-16 12:54:25 -0800109 return true;
110}
111
Romain Guy8ce00302013-01-15 18:51:42 -0800112void LayerRenderer::ensureStencilBuffer() {
113 attachStencilBufferToLayer(mLayer);
114}
115
116///////////////////////////////////////////////////////////////////////////////
117// Dirty region tracking
118///////////////////////////////////////////////////////////////////////////////
119
Romain Guy624234f2013-03-05 16:43:31 -0800120Region* LayerRenderer::getRegion() const {
Tom Hudson984162f2014-10-10 13:38:16 -0400121 if (mState.currentFlags() & Snapshot::kFlagFboTarget) {
Romain Guyf219da52011-01-16 12:54:25 -0800122 return OpenGLRenderer::getRegion();
123 }
124 return &mLayer->region;
Romain Guyf219da52011-01-16 12:54:25 -0800125}
126
Chris Craik6c5b9be2013-02-27 14:03:19 -0800127// TODO: This implementation uses a very simple approach to fixing T-junctions which keeps the
128// results as rectangles, and is thus not necessarily efficient in the geometry
129// produced. Eventually, it may be better to develop triangle-based mechanism.
Romain Guyf219da52011-01-16 12:54:25 -0800130void LayerRenderer::generateMesh() {
Romain Guyf219da52011-01-16 12:54:25 -0800131 if (mLayer->region.isRect() || mLayer->region.isEmpty()) {
132 if (mLayer->mesh) {
Romain Guy96885eb2013-03-26 15:05:58 -0700133 delete[] mLayer->mesh;
Chris Craikd41c4d82015-01-05 15:51:13 -0800134 mLayer->mesh = nullptr;
Romain Guyf219da52011-01-16 12:54:25 -0800135 mLayer->meshElementCount = 0;
136 }
Romain Guy40667672011-03-18 14:34:03 -0700137
Romain Guy9fc27812011-04-27 14:21:41 -0700138 mLayer->setRegionAsRect();
Romain Guyf219da52011-01-16 12:54:25 -0800139 return;
140 }
141
Chris Craik6c5b9be2013-02-27 14:03:19 -0800142 // avoid T-junctions as they cause artifacts in between the resultant
143 // geometry when complex transforms occur.
144 // TODO: generate the safeRegion only if necessary based on drawing transform (see
145 // OpenGLRenderer::composeLayerRegion())
146 Region safeRegion = Region::createTJunctionFreeRegion(mLayer->region);
147
Romain Guyf219da52011-01-16 12:54:25 -0800148 size_t count;
Chris Craik6c5b9be2013-02-27 14:03:19 -0800149 const android::Rect* rects = safeRegion.getArray(&count);
Romain Guyf219da52011-01-16 12:54:25 -0800150
151 GLsizei elementCount = count * 6;
152
153 if (mLayer->mesh && mLayer->meshElementCount < elementCount) {
Romain Guy96885eb2013-03-26 15:05:58 -0700154 delete[] mLayer->mesh;
Chris Craikd41c4d82015-01-05 15:51:13 -0800155 mLayer->mesh = nullptr;
Romain Guyf219da52011-01-16 12:54:25 -0800156 }
157
158 if (!mLayer->mesh) {
159 mLayer->mesh = new TextureVertex[count * 4];
Romain Guyf219da52011-01-16 12:54:25 -0800160 }
Romain Guy4f09f542011-01-26 22:41:43 -0800161 mLayer->meshElementCount = elementCount;
Romain Guyf219da52011-01-16 12:54:25 -0800162
Romain Guy9ace8f52011-07-07 20:50:11 -0700163 const float texX = 1.0f / float(mLayer->getWidth());
164 const float texY = 1.0f / float(mLayer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800165 const float height = mLayer->layer.getHeight();
166
167 TextureVertex* mesh = mLayer->mesh;
Romain Guyf219da52011-01-16 12:54:25 -0800168
169 for (size_t i = 0; i < count; i++) {
170 const android::Rect* r = &rects[i];
171
172 const float u1 = r->left * texX;
173 const float v1 = (height - r->top) * texY;
174 const float u2 = r->right * texX;
175 const float v2 = (height - r->bottom) * texY;
176
177 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
178 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
179 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
180 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
Romain Guyf219da52011-01-16 12:54:25 -0800181 }
Romain Guyf219da52011-01-16 12:54:25 -0800182}
183
184///////////////////////////////////////////////////////////////////////////////
185// Layers management
Romain Guy6c319ca2011-01-11 14:29:25 -0800186///////////////////////////////////////////////////////////////////////////////
187
John Reck3b202512014-06-23 13:13:08 -0700188Layer* LayerRenderer::createRenderLayer(RenderState& renderState, uint32_t width, uint32_t height) {
Chris Craik70850ea2014-11-18 10:49:23 -0800189 ATRACE_FORMAT("Allocate %ux%u HW Layer", width, height);
Romain Guyeea60692011-07-26 20:35:55 -0700190 LAYER_RENDERER_LOGD("Requesting new render layer %dx%d", width, height);
Romain Guy1fc883b2011-01-12 14:30:59 -0800191
Romain Guya1d3c912011-12-13 14:55:06 -0800192 Caches& caches = Caches::getInstance();
193 GLuint fbo = caches.fboCache.get();
Romain Guy09b7c912011-02-02 20:28:09 -0800194 if (!fbo) {
Steve Block8564c8d2012-01-05 23:22:43 +0000195 ALOGW("Could not obtain an FBO");
Chris Craikd41c4d82015-01-05 15:51:13 -0800196 return nullptr;
Romain Guy09b7c912011-02-02 20:28:09 -0800197 }
198
Chris Craik44eb2c02015-01-29 09:45:09 -0800199 caches.textureState().activateTexture(0);
John Reck3b202512014-06-23 13:13:08 -0700200 Layer* layer = caches.layerCache.get(renderState, width, height);
Romain Guy09b7c912011-02-02 20:28:09 -0800201 if (!layer) {
Steve Block8564c8d2012-01-05 23:22:43 +0000202 ALOGW("Could not obtain a layer");
Chris Craikd41c4d82015-01-05 15:51:13 -0800203 return nullptr;
Romain Guy09b7c912011-02-02 20:28:09 -0800204 }
205
Romain Guyce4a7df2013-03-28 11:32:33 -0700206 // We first obtain a layer before comparing against the max texture size
207 // because layers are not allocated at the exact desired size. They are
208 // always created slighly larger to improve recycling
209 const uint32_t maxTextureSize = caches.maxTextureSize;
210 if (layer->getWidth() > maxTextureSize || layer->getHeight() > maxTextureSize) {
211 ALOGW("Layer exceeds max. dimensions supported by the GPU (%dx%d, max=%dx%d)",
212 width, height, maxTextureSize, maxTextureSize);
213
214 // Creating a new layer always increment its refcount by 1, this allows
215 // us to destroy the layer object if one was created for us
Chris Craikd41c4d82015-01-05 15:51:13 -0800216 layer->decStrong(nullptr);
Romain Guyce4a7df2013-03-28 11:32:33 -0700217
Chris Craikd41c4d82015-01-05 15:51:13 -0800218 return nullptr;
Romain Guyce4a7df2013-03-28 11:32:33 -0700219 }
220
Romain Guy9ace8f52011-07-07 20:50:11 -0700221 layer->setFbo(fbo);
Romain Guy09b7c912011-02-02 20:28:09 -0800222 layer->layer.set(0.0f, 0.0f, width, height);
Romain Guy9ace8f52011-07-07 20:50:11 -0700223 layer->texCoords.set(0.0f, height / float(layer->getHeight()),
224 width / float(layer->getWidth()), 0.0f);
225 layer->setAlpha(255, SkXfermode::kSrcOver_Mode);
Chris Craikd41c4d82015-01-05 15:51:13 -0800226 layer->setColorFilter(nullptr);
Romain Guy7c25aab2012-10-18 15:05:02 -0700227 layer->setDirty(true);
Romain Guy09b7c912011-02-02 20:28:09 -0800228 layer->region.clear();
Romain Guyada830f2011-01-13 12:13:20 -0800229
John Reck3b202512014-06-23 13:13:08 -0700230 GLuint previousFbo = renderState.getFramebuffer();
Romain Guy6c319ca2011-01-11 14:29:25 -0800231
John Reck3b202512014-06-23 13:13:08 -0700232 renderState.bindFramebuffer(layer->getFbo());
Romain Guy9ace8f52011-07-07 20:50:11 -0700233 layer->bindTexture();
Romain Guy6c319ca2011-01-11 14:29:25 -0800234
Romain Guy09b7c912011-02-02 20:28:09 -0800235 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700236 if (layer->isEmpty()) {
237 layer->setEmpty(false);
Romain Guy09087642013-04-04 12:27:54 -0700238 layer->allocateTexture();
Romain Guy6c319ca2011-01-11 14:29:25 -0800239
Romain Guyce4a7df2013-03-28 11:32:33 -0700240 // This should only happen if we run out of memory
Romain Guy09b7c912011-02-02 20:28:09 -0800241 if (glGetError() != GL_NO_ERROR) {
Romain Guyce4a7df2013-03-28 11:32:33 -0700242 ALOGE("Could not allocate texture for layer (fbo=%d %dx%d)", fbo, width, height);
John Reck3b202512014-06-23 13:13:08 -0700243 renderState.bindFramebuffer(previousFbo);
Chris Craikd41c4d82015-01-05 15:51:13 -0800244 layer->decStrong(nullptr);
245 return nullptr;
Romain Guy09b7c912011-02-02 20:28:09 -0800246 }
Romain Guy6c319ca2011-01-11 14:29:25 -0800247 }
248
249 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Chris Craikf27133d2015-02-19 09:51:53 -0800250 layer->getTextureId(), 0);
Romain Guy6c319ca2011-01-11 14:29:25 -0800251
John Reck3b202512014-06-23 13:13:08 -0700252 renderState.bindFramebuffer(previousFbo);
Romain Guy6c319ca2011-01-11 14:29:25 -0800253
Romain Guyada830f2011-01-13 12:13:20 -0800254 return layer;
Romain Guy6c319ca2011-01-11 14:29:25 -0800255}
256
Romain Guyada830f2011-01-13 12:13:20 -0800257bool LayerRenderer::resizeLayer(Layer* layer, uint32_t width, uint32_t height) {
258 if (layer) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700259 LAYER_RENDERER_LOGD("Resizing layer fbo = %d to %dx%d", layer->getFbo(), width, height);
Romain Guy1fc883b2011-01-12 14:30:59 -0800260
Romain Guy2055aba2013-01-18 16:42:51 -0800261 if (layer->resize(width, height)) {
Romain Guy09b7c912011-02-02 20:28:09 -0800262 layer->layer.set(0.0f, 0.0f, width, height);
Romain Guy9ace8f52011-07-07 20:50:11 -0700263 layer->texCoords.set(0.0f, height / float(layer->getHeight()),
264 width / float(layer->getWidth()), 0.0f);
Romain Guy09b7c912011-02-02 20:28:09 -0800265 } else {
Romain Guyada830f2011-01-13 12:13:20 -0800266 return false;
267 }
Romain Guy6c319ca2011-01-11 14:29:25 -0800268 }
Romain Guy09b7c912011-02-02 20:28:09 -0800269
Romain Guyada830f2011-01-13 12:13:20 -0800270 return true;
Romain Guy6c319ca2011-01-11 14:29:25 -0800271}
272
John Reck3b202512014-06-23 13:13:08 -0700273Layer* LayerRenderer::createTextureLayer(RenderState& renderState) {
Romain Guy4a5a7152011-06-24 17:53:53 -0700274 LAYER_RENDERER_LOGD("Creating new texture layer");
275
Chris Craik8a226d22014-09-08 16:40:21 -0700276 Layer* layer = new Layer(Layer::kType_Texture, renderState, 0, 0);
Romain Guy9ace8f52011-07-07 20:50:11 -0700277 layer->setCacheable(false);
Romain Guy4a5a7152011-06-24 17:53:53 -0700278 layer->layer.set(0.0f, 0.0f, 0.0f, 0.0f);
Romain Guya9dc86b2011-10-11 14:06:21 -0700279 layer->texCoords.set(0.0f, 1.0f, 1.0f, 0.0f);
Romain Guy4a5a7152011-06-24 17:53:53 -0700280 layer->region.clear();
Romain Guy9ace8f52011-07-07 20:50:11 -0700281 layer->setRenderTarget(GL_NONE); // see ::updateTextureLayer()
Romain Guy4a5a7152011-06-24 17:53:53 -0700282
Chris Craik44eb2c02015-01-29 09:45:09 -0800283 Caches::getInstance().textureState().activateTexture(0);
Romain Guy9ace8f52011-07-07 20:50:11 -0700284 layer->generateTexture();
Romain Guy4a5a7152011-06-24 17:53:53 -0700285
286 return layer;
287}
288
Romain Guyaa6c24c2011-04-28 18:40:04 -0700289void LayerRenderer::updateTextureLayer(Layer* layer, uint32_t width, uint32_t height,
Chris Craik9757ac02014-02-25 18:50:17 -0800290 bool isOpaque, bool forceFilter, GLenum renderTarget, float* textureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700291 if (layer) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700292 layer->setBlend(!isOpaque);
Chris Craik9757ac02014-02-25 18:50:17 -0800293 layer->setForceFilter(forceFilter);
Romain Guy9ace8f52011-07-07 20:50:11 -0700294 layer->setSize(width, height);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700295 layer->layer.set(0.0f, 0.0f, width, height);
296 layer->region.set(width, height);
297 layer->regionRect.set(0.0f, 0.0f, width, height);
Chris Craik9757ac02014-02-25 18:50:17 -0800298 layer->getTexTransform().load(textureTransform);
Romain Guy8f0095c2011-05-02 17:24:22 -0700299
Romain Guy9ace8f52011-07-07 20:50:11 -0700300 if (renderTarget != layer->getRenderTarget()) {
301 layer->setRenderTarget(renderTarget);
302 layer->bindTexture();
Romain Guyd21b6e12011-11-30 20:21:23 -0800303 layer->setFilter(GL_NEAREST, false, true);
304 layer->setWrap(GL_CLAMP_TO_EDGE, false, true);
Romain Guy4a5a7152011-06-24 17:53:53 -0700305 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700306 }
307}
308
Romain Guyada830f2011-01-13 12:13:20 -0800309void LayerRenderer::destroyLayer(Layer* layer) {
310 if (layer) {
Chris Craik70850ea2014-11-18 10:49:23 -0800311 ATRACE_FORMAT("Destroy %ux%u HW Layer", layer->getWidth(), layer->getHeight());
Romain Guyeea60692011-07-26 20:35:55 -0700312 LAYER_RENDERER_LOGD("Recycling layer, %dx%d fbo = %d",
313 layer->getWidth(), layer->getHeight(), layer->getFbo());
Romain Guy1fc883b2011-01-12 14:30:59 -0800314
Romain Guy09b7c912011-02-02 20:28:09 -0800315 if (!Caches::getInstance().layerCache.put(layer)) {
Romain Guyeea60692011-07-26 20:35:55 -0700316 LAYER_RENDERER_LOGD(" Destroyed!");
Chris Craikd41c4d82015-01-05 15:51:13 -0800317 layer->decStrong(nullptr);
Romain Guy09b7c912011-02-02 20:28:09 -0800318 } else {
Romain Guyeea60692011-07-26 20:35:55 -0700319 LAYER_RENDERER_LOGD(" Cached!");
Romain Guy65b345f2011-07-27 18:51:50 -0700320#if DEBUG_LAYER_RENDERER
Romain Guyeea60692011-07-26 20:35:55 -0700321 Caches::getInstance().layerCache.dump();
322#endif
Chet Haase98d3a642012-09-26 10:27:40 -0700323 layer->removeFbo();
Romain Guy09b7c912011-02-02 20:28:09 -0800324 layer->region.clear();
325 }
Romain Guyada830f2011-01-13 12:13:20 -0800326 }
Romain Guy6c319ca2011-01-11 14:29:25 -0800327}
328
John Reck3b202512014-06-23 13:13:08 -0700329void LayerRenderer::flushLayer(RenderState& renderState, Layer* layer) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800330#ifdef GL_EXT_discard_framebuffer
Digish Pandya1fa4cef2014-06-12 10:42:54 +0530331 if (!layer) return;
332
Romain Guy9c4b79a2011-11-10 19:23:58 -0800333 GLuint fbo = layer->getFbo();
Digish Pandya1fa4cef2014-06-12 10:42:54 +0530334 if (fbo) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800335 // If possible, discard any enqueud operations on deferred
336 // rendering architectures
Chris Craik117bdbc2015-02-05 10:12:38 -0800337 if (Caches::getInstance().extensions().hasDiscardFramebuffer()) {
John Reck3b202512014-06-23 13:13:08 -0700338 GLuint previousFbo = renderState.getFramebuffer();
339 if (fbo != previousFbo) {
340 renderState.bindFramebuffer(fbo);
341 }
Romain Guy45e4c3d2012-09-11 17:17:07 -0700342
343 const GLenum attachments[] = { GL_COLOR_ATTACHMENT0 };
344 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
Romain Guy9c4b79a2011-11-10 19:23:58 -0800345
John Reck3b202512014-06-23 13:13:08 -0700346 if (fbo != previousFbo) {
347 renderState.bindFramebuffer(previousFbo);
348 }
Romain Guy9c4b79a2011-11-10 19:23:58 -0800349 }
350 }
351#endif
352}
353
John Reck3b202512014-06-23 13:13:08 -0700354bool LayerRenderer::copyLayer(RenderState& renderState, Layer* layer, SkBitmap* bitmap) {
Romain Guy77a81162011-06-14 16:45:55 -0700355 Caches& caches = Caches::getInstance();
Chris Craik26bf3422015-02-26 16:28:17 -0800356 if (layer
357 && bitmap->width() <= caches.maxTextureSize
358 && bitmap->height() <= caches.maxTextureSize) {
Romain Guy77a81162011-06-14 16:45:55 -0700359
360 GLuint fbo = caches.fboCache.get();
361 if (!fbo) {
Steve Block8564c8d2012-01-05 23:22:43 +0000362 ALOGW("Could not obtain an FBO");
Romain Guy77a81162011-06-14 16:45:55 -0700363 return false;
364 }
365
Romain Guyd6b2a002011-06-17 17:45:59 -0700366 SkAutoLockPixels alp(*bitmap);
367
Romain Guy77a81162011-06-14 16:45:55 -0700368 GLuint texture;
369 GLuint previousFbo;
John Reck3b202512014-06-23 13:13:08 -0700370 GLsizei previousViewportWidth;
371 GLsizei previousViewportHeight;
Romain Guy77a81162011-06-14 16:45:55 -0700372
373 GLenum format;
374 GLenum type;
375
Romain Guyd6b2a002011-06-17 17:45:59 -0700376 GLenum error = GL_NO_ERROR;
377 bool status = false;
378
Mike Reed1103b322014-07-08 12:36:44 -0400379 switch (bitmap->colorType()) {
380 case kAlpha_8_SkColorType:
Romain Guy77a81162011-06-14 16:45:55 -0700381 format = GL_ALPHA;
382 type = GL_UNSIGNED_BYTE;
383 break;
Mike Reed1103b322014-07-08 12:36:44 -0400384 case kRGB_565_SkColorType:
Romain Guy77a81162011-06-14 16:45:55 -0700385 format = GL_RGB;
386 type = GL_UNSIGNED_SHORT_5_6_5;
387 break;
Mike Reed1103b322014-07-08 12:36:44 -0400388 case kARGB_4444_SkColorType:
Romain Guy77a81162011-06-14 16:45:55 -0700389 format = GL_RGBA;
390 type = GL_UNSIGNED_SHORT_4_4_4_4;
391 break;
Mike Reed1103b322014-07-08 12:36:44 -0400392 case kN32_SkColorType:
Romain Guy77a81162011-06-14 16:45:55 -0700393 default:
394 format = GL_RGBA;
395 type = GL_UNSIGNED_BYTE;
396 break;
397 }
398
Romain Guy9ace8f52011-07-07 20:50:11 -0700399 float alpha = layer->getAlpha();
400 SkXfermode::Mode mode = layer->getMode();
Chris Craik710f46d2012-09-17 17:25:49 -0700401 GLuint previousLayerFbo = layer->getFbo();
Romain Guyd6b2a002011-06-17 17:45:59 -0700402
Romain Guy9ace8f52011-07-07 20:50:11 -0700403 layer->setAlpha(255, SkXfermode::kSrc_Mode);
404 layer->setFbo(fbo);
Romain Guyd6b2a002011-06-17 17:45:59 -0700405
John Reck3b202512014-06-23 13:13:08 -0700406 previousFbo = renderState.getFramebuffer();
407 renderState.getViewport(&previousViewportWidth, &previousViewportHeight);
408 renderState.bindFramebuffer(fbo);
Romain Guy77a81162011-06-14 16:45:55 -0700409
410 glGenTextures(1, &texture);
Romain Guyd6b2a002011-06-17 17:45:59 -0700411 if ((error = glGetError()) != GL_NO_ERROR) goto error;
Romain Guy77a81162011-06-14 16:45:55 -0700412
Chris Craik44eb2c02015-01-29 09:45:09 -0800413 caches.textureState().activateTexture(0);
414 caches.textureState().bindTexture(texture);
Romain Guy77a81162011-06-14 16:45:55 -0700415
Romain Guye49d7ec2012-09-07 18:42:38 -0700416 glPixelStorei(GL_PACK_ALIGNMENT, bitmap->bytesPerPixel());
417
Romain Guyec19b4a2011-07-07 21:05:04 -0700418 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
419 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
Romain Guy77a81162011-06-14 16:45:55 -0700420
421 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
422 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
423
424 glTexImage2D(GL_TEXTURE_2D, 0, format, bitmap->width(), bitmap->height(),
Chris Craikd41c4d82015-01-05 15:51:13 -0800425 0, format, type, nullptr);
Romain Guyd6b2a002011-06-17 17:45:59 -0700426 if ((error = glGetError()) != GL_NO_ERROR) goto error;
427
Romain Guy77a81162011-06-14 16:45:55 -0700428 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
429 GL_TEXTURE_2D, texture, 0);
Romain Guyd6b2a002011-06-17 17:45:59 -0700430 if ((error = glGetError()) != GL_NO_ERROR) goto error;
Romain Guy77a81162011-06-14 16:45:55 -0700431
Romain Guyd6b2a002011-06-17 17:45:59 -0700432 {
John Reck3b202512014-06-23 13:13:08 -0700433 LayerRenderer renderer(renderState, layer);
Romain Guyd6b2a002011-06-17 17:45:59 -0700434 renderer.setViewport(bitmap->width(), bitmap->height());
435 renderer.OpenGLRenderer::prepareDirty(0.0f, 0.0f,
Romain Guy9ace8f52011-07-07 20:50:11 -0700436 bitmap->width(), bitmap->height(), !layer->isBlend());
Romain Guya9dc86b2011-10-11 14:06:21 -0700437
Chris Craik65fe5ee2015-01-26 18:06:29 -0800438 renderState.scissor().setEnabled(false);
Romain Guya9dc86b2011-10-11 14:06:21 -0700439 renderer.translate(0.0f, bitmap->height());
440 renderer.scale(1.0f, -1.0f);
441
442 mat4 texTransform(layer->getTexTransform());
443
444 mat4 invert;
Romain Guy4c2547f2013-06-11 16:19:24 -0700445 invert.translate(0.0f, 1.0f);
Romain Guya9dc86b2011-10-11 14:06:21 -0700446 invert.scale(1.0f, -1.0f, 1.0f);
447 layer->getTexTransform().multiply(invert);
448
Romain Guyd6b2a002011-06-17 17:45:59 -0700449 if ((error = glGetError()) != GL_NO_ERROR) goto error;
Romain Guy77a81162011-06-14 16:45:55 -0700450
Romain Guyd6b2a002011-06-17 17:45:59 -0700451 {
452 Rect bounds;
453 bounds.set(0.0f, 0.0f, bitmap->width(), bitmap->height());
454 renderer.drawTextureLayer(layer, bounds);
Romain Guy77a81162011-06-14 16:45:55 -0700455
Romain Guyd6b2a002011-06-17 17:45:59 -0700456 glReadPixels(0, 0, bitmap->width(), bitmap->height(), format,
457 type, bitmap->getPixels());
Romain Guy77a81162011-06-14 16:45:55 -0700458
Romain Guyd6b2a002011-06-17 17:45:59 -0700459 if ((error = glGetError()) != GL_NO_ERROR) goto error;
460 }
Romain Guy77a81162011-06-14 16:45:55 -0700461
Romain Guya9dc86b2011-10-11 14:06:21 -0700462 layer->getTexTransform().load(texTransform);
Romain Guyd6b2a002011-06-17 17:45:59 -0700463 status = true;
464 }
Romain Guy77a81162011-06-14 16:45:55 -0700465
Romain Guyd6b2a002011-06-17 17:45:59 -0700466error:
467#if DEBUG_OPENGL
468 if (error != GL_NO_ERROR) {
Steve Block5baa3a62011-12-20 16:23:08 +0000469 ALOGD("GL error while copying layer into bitmap = 0x%x", error);
Romain Guyd6b2a002011-06-17 17:45:59 -0700470 }
471#endif
Romain Guy77a81162011-06-14 16:45:55 -0700472
John Reck3b202512014-06-23 13:13:08 -0700473 renderState.bindFramebuffer(previousFbo);
Romain Guy9ace8f52011-07-07 20:50:11 -0700474 layer->setAlpha(alpha, mode);
Chris Craik710f46d2012-09-17 17:25:49 -0700475 layer->setFbo(previousLayerFbo);
Chris Craik44eb2c02015-01-29 09:45:09 -0800476 caches.textureState().deleteTexture(texture);
Romain Guy77a81162011-06-14 16:45:55 -0700477 caches.fboCache.put(fbo);
John Reck3b202512014-06-23 13:13:08 -0700478 renderState.setViewport(previousViewportWidth, previousViewportHeight);
Romain Guy77a81162011-06-14 16:45:55 -0700479
Romain Guyd6b2a002011-06-17 17:45:59 -0700480 return status;
Romain Guy77a81162011-06-14 16:45:55 -0700481 }
482 return false;
483}
484
Romain Guy6c319ca2011-01-11 14:29:25 -0800485}; // namespace uirenderer
486}; // namespace android