blob: d8e6392bd07e0af712f6b005366ac2956c4c37ff [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
Romain Guy09b7c912011-02-02 20:28:09 -080017#include "LayerCache.h"
Romain Guy6c319ca2011-01-11 14:29:25 -080018#include "LayerRenderer.h"
Romain Guyaa6c24c2011-04-28 18:40:04 -070019#include "Matrix.h"
Romain Guy1fc883b2011-01-12 14:30:59 -080020#include "Properties.h"
Romain Guy3a3133d2011-02-01 22:59:58 -080021#include "Rect.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080022#include "renderstate/RenderState.h"
Chris Craik5686bae2015-06-23 10:33:46 -070023#include "utils/GLUtils.h"
Chris Craik70850ea2014-11-18 10:49:23 -080024#include "utils/TraceUtils.h"
Romain Guy6c319ca2011-01-11 14:29:25 -080025
Chris Craik65fe5ee2015-01-26 18:06:29 -080026#include <ui/Rect.h>
27
28#include <private/hwui/DrawGlInfo.h>
29
30
Romain Guy6c319ca2011-01-11 14:29:25 -080031namespace android {
32namespace uirenderer {
33
34///////////////////////////////////////////////////////////////////////////////
35// Rendering
36///////////////////////////////////////////////////////////////////////////////
37
John Reck3b202512014-06-23 13:13:08 -070038LayerRenderer::LayerRenderer(RenderState& renderState, Layer* layer)
39 : OpenGLRenderer(renderState)
40 , mLayer(layer) {
Romain Guy79537452011-10-12 13:48:51 -070041}
42
43LayerRenderer::~LayerRenderer() {
44}
45
Tom Hudson107843d2014-09-08 11:26:26 -040046void LayerRenderer::prepareDirty(float left, float top, float right, float bottom,
Romain Guy7c25aab2012-10-18 15:05:02 -070047 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
Chris Craik65fe5ee2015-01-26 18:06:29 -080050 mRenderState.bindFramebuffer(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
Tom Hudson107843d2014-09-08 11:26:26 -040067 OpenGLRenderer::prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, opaque);
Romain Guy6c319ca2011-01-11 14:29:25 -080068}
69
Tom Hudson107843d2014-09-08 11:26:26 -040070void LayerRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
Romain Guy7c25aab2012-10-18 15:05:02 -070071 if (mLayer->isDirty()) {
Chris Craik65fe5ee2015-01-26 18:06:29 -080072 mRenderState.scissor().setEnabled(false);
Romain Guy7c25aab2012-10-18 15:05:02 -070073 glClear(GL_COLOR_BUFFER_BIT);
74
Chris Craik65fe5ee2015-01-26 18:06:29 -080075 mRenderState.scissor().reset();
Romain Guy7c25aab2012-10-18 15:05:02 -070076 mLayer->setDirty(false);
Tom Hudson107843d2014-09-08 11:26:26 -040077 } else {
78 OpenGLRenderer::clear(left, top, right, bottom, opaque);
Romain Guy7c25aab2012-10-18 15:05:02 -070079 }
Romain Guy7c25aab2012-10-18 15:05:02 -070080}
81
Tom Hudson107843d2014-09-08 11:26:26 -040082bool LayerRenderer::finish() {
83 bool retval = OpenGLRenderer::finish();
Romain Guy1fc883b2011-01-12 14:30:59 -080084
Romain Guyf219da52011-01-16 12:54:25 -080085 generateMesh();
86
Romain Guy9ace8f52011-07-07 20:50:11 -070087 LAYER_RENDERER_LOGD("Finished rendering into layer, fbo = %d", mLayer->getFbo());
Romain Guy42f3a4b2011-01-19 13:42:26 -080088
89 // No need to unbind our FBO, this will be taken care of by the caller
90 // who will invoke OpenGLRenderer::resume()
Tom Hudson107843d2014-09-08 11:26:26 -040091 return retval;
Romain Guy42f3a4b2011-01-19 13:42:26 -080092}
93
Chris Craik6b109c72015-02-27 10:55:28 -080094GLuint LayerRenderer::getTargetFbo() const {
Romain Guy9ace8f52011-07-07 20:50:11 -070095 return mLayer->getFbo();
Romain Guy6c319ca2011-01-11 14:29:25 -080096}
97
Romain Guy624234f2013-03-05 16:43:31 -080098bool LayerRenderer::suppressErrorChecks() const {
Romain Guy11cb6422012-09-21 00:39:43 -070099 return true;
100}
101
Romain Guy6c319ca2011-01-11 14:29:25 -0800102///////////////////////////////////////////////////////////////////////////////
Romain Guy8ce00302013-01-15 18:51:42 -0800103// Layer support
Romain Guyf219da52011-01-16 12:54:25 -0800104///////////////////////////////////////////////////////////////////////////////
105
Romain Guy624234f2013-03-05 16:43:31 -0800106bool LayerRenderer::hasLayer() const {
Romain Guyf219da52011-01-16 12:54:25 -0800107 return true;
108}
109
Romain Guy8ce00302013-01-15 18:51:42 -0800110void LayerRenderer::ensureStencilBuffer() {
111 attachStencilBufferToLayer(mLayer);
112}
113
114///////////////////////////////////////////////////////////////////////////////
115// Dirty region tracking
116///////////////////////////////////////////////////////////////////////////////
117
Romain Guy624234f2013-03-05 16:43:31 -0800118Region* LayerRenderer::getRegion() const {
Tom Hudson984162f2014-10-10 13:38:16 -0400119 if (mState.currentFlags() & Snapshot::kFlagFboTarget) {
Romain Guyf219da52011-01-16 12:54:25 -0800120 return OpenGLRenderer::getRegion();
121 }
122 return &mLayer->region;
Romain Guyf219da52011-01-16 12:54:25 -0800123}
124
Chris Craik6c5b9be2013-02-27 14:03:19 -0800125// TODO: This implementation uses a very simple approach to fixing T-junctions which keeps the
126// results as rectangles, and is thus not necessarily efficient in the geometry
127// produced. Eventually, it may be better to develop triangle-based mechanism.
Romain Guyf219da52011-01-16 12:54:25 -0800128void LayerRenderer::generateMesh() {
Romain Guyf219da52011-01-16 12:54:25 -0800129 if (mLayer->region.isRect() || mLayer->region.isEmpty()) {
130 if (mLayer->mesh) {
Romain Guy96885eb2013-03-26 15:05:58 -0700131 delete[] mLayer->mesh;
Chris Craikd41c4d82015-01-05 15:51:13 -0800132 mLayer->mesh = nullptr;
Romain Guyf219da52011-01-16 12:54:25 -0800133 mLayer->meshElementCount = 0;
134 }
Romain Guy40667672011-03-18 14:34:03 -0700135
Romain Guy9fc27812011-04-27 14:21:41 -0700136 mLayer->setRegionAsRect();
Romain Guyf219da52011-01-16 12:54:25 -0800137 return;
138 }
139
Chris Craik6c5b9be2013-02-27 14:03:19 -0800140 // avoid T-junctions as they cause artifacts in between the resultant
141 // geometry when complex transforms occur.
142 // TODO: generate the safeRegion only if necessary based on drawing transform (see
143 // OpenGLRenderer::composeLayerRegion())
144 Region safeRegion = Region::createTJunctionFreeRegion(mLayer->region);
145
Romain Guyf219da52011-01-16 12:54:25 -0800146 size_t count;
Chris Craik6c5b9be2013-02-27 14:03:19 -0800147 const android::Rect* rects = safeRegion.getArray(&count);
Romain Guyf219da52011-01-16 12:54:25 -0800148
149 GLsizei elementCount = count * 6;
150
151 if (mLayer->mesh && mLayer->meshElementCount < elementCount) {
Romain Guy96885eb2013-03-26 15:05:58 -0700152 delete[] mLayer->mesh;
Chris Craikd41c4d82015-01-05 15:51:13 -0800153 mLayer->mesh = nullptr;
Romain Guyf219da52011-01-16 12:54:25 -0800154 }
155
156 if (!mLayer->mesh) {
157 mLayer->mesh = new TextureVertex[count * 4];
Romain Guyf219da52011-01-16 12:54:25 -0800158 }
Romain Guy4f09f542011-01-26 22:41:43 -0800159 mLayer->meshElementCount = elementCount;
Romain Guyf219da52011-01-16 12:54:25 -0800160
Romain Guy9ace8f52011-07-07 20:50:11 -0700161 const float texX = 1.0f / float(mLayer->getWidth());
162 const float texY = 1.0f / float(mLayer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800163 const float height = mLayer->layer.getHeight();
164
165 TextureVertex* mesh = mLayer->mesh;
Romain Guyf219da52011-01-16 12:54:25 -0800166
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);
Romain Guyf219da52011-01-16 12:54:25 -0800179 }
Romain Guyf219da52011-01-16 12:54:25 -0800180}
181
182///////////////////////////////////////////////////////////////////////////////
183// Layers management
Romain Guy6c319ca2011-01-11 14:29:25 -0800184///////////////////////////////////////////////////////////////////////////////
185
John Reck3b202512014-06-23 13:13:08 -0700186Layer* LayerRenderer::createRenderLayer(RenderState& renderState, uint32_t width, uint32_t height) {
Chris Craik70850ea2014-11-18 10:49:23 -0800187 ATRACE_FORMAT("Allocate %ux%u HW Layer", width, height);
Romain Guyeea60692011-07-26 20:35:55 -0700188 LAYER_RENDERER_LOGD("Requesting new render layer %dx%d", width, height);
Romain Guy1fc883b2011-01-12 14:30:59 -0800189
Romain Guya1d3c912011-12-13 14:55:06 -0800190 Caches& caches = Caches::getInstance();
191 GLuint fbo = caches.fboCache.get();
Romain Guy09b7c912011-02-02 20:28:09 -0800192 if (!fbo) {
Steve Block8564c8d2012-01-05 23:22:43 +0000193 ALOGW("Could not obtain an FBO");
Chris Craikd41c4d82015-01-05 15:51:13 -0800194 return nullptr;
Romain Guy09b7c912011-02-02 20:28:09 -0800195 }
196
Chris Craik44eb2c02015-01-29 09:45:09 -0800197 caches.textureState().activateTexture(0);
John Reck3b202512014-06-23 13:13:08 -0700198 Layer* layer = caches.layerCache.get(renderState, width, height);
Romain Guy09b7c912011-02-02 20:28:09 -0800199 if (!layer) {
Steve Block8564c8d2012-01-05 23:22:43 +0000200 ALOGW("Could not obtain a layer");
Chris Craikd41c4d82015-01-05 15:51:13 -0800201 return nullptr;
Romain Guy09b7c912011-02-02 20:28:09 -0800202 }
203
Romain Guyce4a7df2013-03-28 11:32:33 -0700204 // We first obtain a layer before comparing against the max texture size
205 // because layers are not allocated at the exact desired size. They are
206 // always created slighly larger to improve recycling
207 const uint32_t maxTextureSize = caches.maxTextureSize;
208 if (layer->getWidth() > maxTextureSize || layer->getHeight() > maxTextureSize) {
209 ALOGW("Layer exceeds max. dimensions supported by the GPU (%dx%d, max=%dx%d)",
210 width, height, maxTextureSize, maxTextureSize);
211
212 // Creating a new layer always increment its refcount by 1, this allows
213 // us to destroy the layer object if one was created for us
Chris Craikd41c4d82015-01-05 15:51:13 -0800214 layer->decStrong(nullptr);
Romain Guyce4a7df2013-03-28 11:32:33 -0700215
Chris Craikd41c4d82015-01-05 15:51:13 -0800216 return nullptr;
Romain Guyce4a7df2013-03-28 11:32:33 -0700217 }
218
Romain Guy9ace8f52011-07-07 20:50:11 -0700219 layer->setFbo(fbo);
Romain Guy09b7c912011-02-02 20:28:09 -0800220 layer->layer.set(0.0f, 0.0f, width, height);
Romain Guy9ace8f52011-07-07 20:50:11 -0700221 layer->texCoords.set(0.0f, height / float(layer->getHeight()),
222 width / float(layer->getWidth()), 0.0f);
223 layer->setAlpha(255, SkXfermode::kSrcOver_Mode);
Chris Craikd41c4d82015-01-05 15:51:13 -0800224 layer->setColorFilter(nullptr);
Romain Guy7c25aab2012-10-18 15:05:02 -0700225 layer->setDirty(true);
Romain Guy09b7c912011-02-02 20:28:09 -0800226 layer->region.clear();
Romain Guyada830f2011-01-13 12:13:20 -0800227
John Reck3b202512014-06-23 13:13:08 -0700228 GLuint previousFbo = renderState.getFramebuffer();
Romain Guy6c319ca2011-01-11 14:29:25 -0800229
John Reck3b202512014-06-23 13:13:08 -0700230 renderState.bindFramebuffer(layer->getFbo());
Romain Guy9ace8f52011-07-07 20:50:11 -0700231 layer->bindTexture();
Romain Guy6c319ca2011-01-11 14:29:25 -0800232
Romain Guy09b7c912011-02-02 20:28:09 -0800233 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700234 if (layer->isEmpty()) {
235 layer->setEmpty(false);
Romain Guy09087642013-04-04 12:27:54 -0700236 layer->allocateTexture();
Romain Guy6c319ca2011-01-11 14:29:25 -0800237
Romain Guyce4a7df2013-03-28 11:32:33 -0700238 // This should only happen if we run out of memory
Chris Craik5686bae2015-06-23 10:33:46 -0700239 if (CC_UNLIKELY(GLUtils::dumpGLErrors())) {
240 LOG_ALWAYS_FATAL("Could not allocate texture for layer (fbo=%d %dx%d)",
241 fbo, width, height);
John Reck3b202512014-06-23 13:13:08 -0700242 renderState.bindFramebuffer(previousFbo);
Chris Craikd41c4d82015-01-05 15:51:13 -0800243 layer->decStrong(nullptr);
244 return nullptr;
Romain Guy09b7c912011-02-02 20:28:09 -0800245 }
Romain Guy6c319ca2011-01-11 14:29:25 -0800246 }
247
248 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Chris Craikf27133d2015-02-19 09:51:53 -0800249 layer->getTextureId(), 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 Craikb9ce116d2015-08-20 15:14:06 -0700275 Layer* layer = new Layer(Layer::Type::Texture, renderState, 0, 0);
Romain Guy9ace8f52011-07-07 20:50:11 -0700276 layer->setCacheable(false);
Romain Guy4a5a7152011-06-24 17:53:53 -0700277 layer->layer.set(0.0f, 0.0f, 0.0f, 0.0f);
Romain Guya9dc86b2011-10-11 14:06:21 -0700278 layer->texCoords.set(0.0f, 1.0f, 1.0f, 0.0f);
Romain Guy4a5a7152011-06-24 17:53:53 -0700279 layer->region.clear();
Romain Guy9ace8f52011-07-07 20:50:11 -0700280 layer->setRenderTarget(GL_NONE); // see ::updateTextureLayer()
Romain Guy4a5a7152011-06-24 17:53:53 -0700281
Chris Craik44eb2c02015-01-29 09:45:09 -0800282 Caches::getInstance().textureState().activateTexture(0);
Romain Guy9ace8f52011-07-07 20:50:11 -0700283 layer->generateTexture();
Romain Guy4a5a7152011-06-24 17:53:53 -0700284
285 return layer;
286}
287
Romain Guyaa6c24c2011-04-28 18:40:04 -0700288void LayerRenderer::updateTextureLayer(Layer* layer, uint32_t width, uint32_t height,
Chris Craik9757ac02014-02-25 18:50:17 -0800289 bool isOpaque, bool forceFilter, GLenum renderTarget, float* textureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700290 if (layer) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700291 layer->setBlend(!isOpaque);
Chris Craik9757ac02014-02-25 18:50:17 -0800292 layer->setForceFilter(forceFilter);
Romain Guy9ace8f52011-07-07 20:50:11 -0700293 layer->setSize(width, height);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700294 layer->layer.set(0.0f, 0.0f, width, height);
295 layer->region.set(width, height);
296 layer->regionRect.set(0.0f, 0.0f, width, height);
Chris Craik9757ac02014-02-25 18:50:17 -0800297 layer->getTexTransform().load(textureTransform);
Romain Guy8f0095c2011-05-02 17:24:22 -0700298
Romain Guy9ace8f52011-07-07 20:50:11 -0700299 if (renderTarget != layer->getRenderTarget()) {
300 layer->setRenderTarget(renderTarget);
301 layer->bindTexture();
Romain Guyd21b6e12011-11-30 20:21:23 -0800302 layer->setFilter(GL_NEAREST, false, true);
303 layer->setWrap(GL_CLAMP_TO_EDGE, false, true);
Romain Guy4a5a7152011-06-24 17:53:53 -0700304 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700305 }
306}
307
Romain Guyada830f2011-01-13 12:13:20 -0800308void LayerRenderer::destroyLayer(Layer* layer) {
309 if (layer) {
Chris Craik70850ea2014-11-18 10:49:23 -0800310 ATRACE_FORMAT("Destroy %ux%u HW Layer", layer->getWidth(), layer->getHeight());
Romain Guyeea60692011-07-26 20:35:55 -0700311 LAYER_RENDERER_LOGD("Recycling layer, %dx%d fbo = %d",
312 layer->getWidth(), layer->getHeight(), layer->getFbo());
Romain Guy1fc883b2011-01-12 14:30:59 -0800313
Romain Guy09b7c912011-02-02 20:28:09 -0800314 if (!Caches::getInstance().layerCache.put(layer)) {
Romain Guyeea60692011-07-26 20:35:55 -0700315 LAYER_RENDERER_LOGD(" Destroyed!");
Chris Craikd41c4d82015-01-05 15:51:13 -0800316 layer->decStrong(nullptr);
Romain Guy09b7c912011-02-02 20:28:09 -0800317 } else {
Romain Guyeea60692011-07-26 20:35:55 -0700318 LAYER_RENDERER_LOGD(" Cached!");
Romain Guy65b345f2011-07-27 18:51:50 -0700319#if DEBUG_LAYER_RENDERER
Romain Guyeea60692011-07-26 20:35:55 -0700320 Caches::getInstance().layerCache.dump();
321#endif
Chet Haase98d3a642012-09-26 10:27:40 -0700322 layer->removeFbo();
Romain Guy09b7c912011-02-02 20:28:09 -0800323 layer->region.clear();
324 }
Romain Guyada830f2011-01-13 12:13:20 -0800325 }
Romain Guy6c319ca2011-01-11 14:29:25 -0800326}
327
John Reck3b202512014-06-23 13:13:08 -0700328void LayerRenderer::flushLayer(RenderState& renderState, Layer* layer) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800329#ifdef GL_EXT_discard_framebuffer
Digish Pandya1fa4cef2014-06-12 10:42:54 +0530330 if (!layer) return;
331
Romain Guy9c4b79a2011-11-10 19:23:58 -0800332 GLuint fbo = layer->getFbo();
Digish Pandya1fa4cef2014-06-12 10:42:54 +0530333 if (fbo) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800334 // If possible, discard any enqueud operations on deferred
335 // rendering architectures
Chris Craik117bdbc2015-02-05 10:12:38 -0800336 if (Caches::getInstance().extensions().hasDiscardFramebuffer()) {
John Reck3b202512014-06-23 13:13:08 -0700337 GLuint previousFbo = renderState.getFramebuffer();
338 if (fbo != previousFbo) {
339 renderState.bindFramebuffer(fbo);
340 }
Romain Guy45e4c3d2012-09-11 17:17:07 -0700341
342 const GLenum attachments[] = { GL_COLOR_ATTACHMENT0 };
343 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
Romain Guy9c4b79a2011-11-10 19:23:58 -0800344
John Reck3b202512014-06-23 13:13:08 -0700345 if (fbo != previousFbo) {
346 renderState.bindFramebuffer(previousFbo);
347 }
Romain Guy9c4b79a2011-11-10 19:23:58 -0800348 }
349 }
350#endif
351}
352
John Reck3b202512014-06-23 13:13:08 -0700353bool LayerRenderer::copyLayer(RenderState& renderState, Layer* layer, SkBitmap* bitmap) {
Romain Guy77a81162011-06-14 16:45:55 -0700354 Caches& caches = Caches::getInstance();
Chris Craik26bf3422015-02-26 16:28:17 -0800355 if (layer
356 && bitmap->width() <= caches.maxTextureSize
357 && bitmap->height() <= caches.maxTextureSize) {
Romain Guy77a81162011-06-14 16:45:55 -0700358
359 GLuint fbo = caches.fboCache.get();
360 if (!fbo) {
Steve Block8564c8d2012-01-05 23:22:43 +0000361 ALOGW("Could not obtain an FBO");
Romain Guy77a81162011-06-14 16:45:55 -0700362 return false;
363 }
364
Romain Guyd6b2a002011-06-17 17:45:59 -0700365 SkAutoLockPixels alp(*bitmap);
366
Romain Guy77a81162011-06-14 16:45:55 -0700367 GLuint texture;
368 GLuint previousFbo;
John Reck3b202512014-06-23 13:13:08 -0700369 GLsizei previousViewportWidth;
370 GLsizei previousViewportHeight;
Romain Guy77a81162011-06-14 16:45:55 -0700371
372 GLenum format;
373 GLenum type;
374
Romain Guyd6b2a002011-06-17 17:45:59 -0700375 GLenum error = GL_NO_ERROR;
376 bool status = false;
377
Mike Reed1103b322014-07-08 12:36:44 -0400378 switch (bitmap->colorType()) {
379 case kAlpha_8_SkColorType:
Romain Guy77a81162011-06-14 16:45:55 -0700380 format = GL_ALPHA;
381 type = GL_UNSIGNED_BYTE;
382 break;
Mike Reed1103b322014-07-08 12:36:44 -0400383 case kRGB_565_SkColorType:
Romain Guy77a81162011-06-14 16:45:55 -0700384 format = GL_RGB;
385 type = GL_UNSIGNED_SHORT_5_6_5;
386 break;
Mike Reed1103b322014-07-08 12:36:44 -0400387 case kARGB_4444_SkColorType:
Romain Guy77a81162011-06-14 16:45:55 -0700388 format = GL_RGBA;
389 type = GL_UNSIGNED_SHORT_4_4_4_4;
390 break;
Mike Reed1103b322014-07-08 12:36:44 -0400391 case kN32_SkColorType:
Romain Guy77a81162011-06-14 16:45:55 -0700392 default:
393 format = GL_RGBA;
394 type = GL_UNSIGNED_BYTE;
395 break;
396 }
397
Romain Guy9ace8f52011-07-07 20:50:11 -0700398 float alpha = layer->getAlpha();
399 SkXfermode::Mode mode = layer->getMode();
Chris Craik710f46d2012-09-17 17:25:49 -0700400 GLuint previousLayerFbo = layer->getFbo();
Romain Guyd6b2a002011-06-17 17:45:59 -0700401
Romain Guy9ace8f52011-07-07 20:50:11 -0700402 layer->setAlpha(255, SkXfermode::kSrc_Mode);
403 layer->setFbo(fbo);
Romain Guyd6b2a002011-06-17 17:45:59 -0700404
John Reck3b202512014-06-23 13:13:08 -0700405 previousFbo = renderState.getFramebuffer();
406 renderState.getViewport(&previousViewportWidth, &previousViewportHeight);
407 renderState.bindFramebuffer(fbo);
Romain Guy77a81162011-06-14 16:45:55 -0700408
409 glGenTextures(1, &texture);
Romain Guyd6b2a002011-06-17 17:45:59 -0700410 if ((error = glGetError()) != GL_NO_ERROR) goto error;
Romain Guy77a81162011-06-14 16:45:55 -0700411
Chris Craik44eb2c02015-01-29 09:45:09 -0800412 caches.textureState().activateTexture(0);
413 caches.textureState().bindTexture(texture);
Romain Guy77a81162011-06-14 16:45:55 -0700414
Romain Guye49d7ec2012-09-07 18:42:38 -0700415 glPixelStorei(GL_PACK_ALIGNMENT, bitmap->bytesPerPixel());
416
Romain Guyec19b4a2011-07-07 21:05:04 -0700417 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
418 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
Romain Guy77a81162011-06-14 16:45:55 -0700419
420 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
421 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
422
423 glTexImage2D(GL_TEXTURE_2D, 0, format, bitmap->width(), bitmap->height(),
Chris Craikd41c4d82015-01-05 15:51:13 -0800424 0, format, type, nullptr);
Romain Guyd6b2a002011-06-17 17:45:59 -0700425 if ((error = glGetError()) != GL_NO_ERROR) goto error;
426
Romain Guy77a81162011-06-14 16:45:55 -0700427 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
428 GL_TEXTURE_2D, texture, 0);
Romain Guyd6b2a002011-06-17 17:45:59 -0700429 if ((error = glGetError()) != GL_NO_ERROR) goto error;
Romain Guy77a81162011-06-14 16:45:55 -0700430
Romain Guyd6b2a002011-06-17 17:45:59 -0700431 {
John Reck3b202512014-06-23 13:13:08 -0700432 LayerRenderer renderer(renderState, layer);
Romain Guyd6b2a002011-06-17 17:45:59 -0700433 renderer.setViewport(bitmap->width(), bitmap->height());
434 renderer.OpenGLRenderer::prepareDirty(0.0f, 0.0f,
Romain Guy9ace8f52011-07-07 20:50:11 -0700435 bitmap->width(), bitmap->height(), !layer->isBlend());
Romain Guya9dc86b2011-10-11 14:06:21 -0700436
Chris Craik65fe5ee2015-01-26 18:06:29 -0800437 renderState.scissor().setEnabled(false);
Romain Guya9dc86b2011-10-11 14:06:21 -0700438 renderer.translate(0.0f, bitmap->height());
439 renderer.scale(1.0f, -1.0f);
440
Romain Guyd6b2a002011-06-17 17:45:59 -0700441 if ((error = glGetError()) != GL_NO_ERROR) goto error;
Romain Guy77a81162011-06-14 16:45:55 -0700442
Romain Guyd6b2a002011-06-17 17:45:59 -0700443 {
444 Rect bounds;
445 bounds.set(0.0f, 0.0f, bitmap->width(), bitmap->height());
446 renderer.drawTextureLayer(layer, bounds);
Romain Guy77a81162011-06-14 16:45:55 -0700447
Romain Guyd6b2a002011-06-17 17:45:59 -0700448 glReadPixels(0, 0, bitmap->width(), bitmap->height(), format,
449 type, bitmap->getPixels());
Romain Guy77a81162011-06-14 16:45:55 -0700450
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
Romain Guyd6b2a002011-06-17 17:45:59 -0700454 status = true;
455 }
Romain Guy77a81162011-06-14 16:45:55 -0700456
Romain Guyd6b2a002011-06-17 17:45:59 -0700457error:
458#if DEBUG_OPENGL
459 if (error != GL_NO_ERROR) {
Steve Block5baa3a62011-12-20 16:23:08 +0000460 ALOGD("GL error while copying layer into bitmap = 0x%x", error);
Romain Guyd6b2a002011-06-17 17:45:59 -0700461 }
462#endif
Romain Guy77a81162011-06-14 16:45:55 -0700463
John Reck3b202512014-06-23 13:13:08 -0700464 renderState.bindFramebuffer(previousFbo);
Romain Guy9ace8f52011-07-07 20:50:11 -0700465 layer->setAlpha(alpha, mode);
Chris Craik710f46d2012-09-17 17:25:49 -0700466 layer->setFbo(previousLayerFbo);
Chris Craik44eb2c02015-01-29 09:45:09 -0800467 caches.textureState().deleteTexture(texture);
Romain Guy77a81162011-06-14 16:45:55 -0700468 caches.fboCache.put(fbo);
John Reck3b202512014-06-23 13:13:08 -0700469 renderState.setViewport(previousViewportWidth, previousViewportHeight);
Romain Guy77a81162011-06-14 16:45:55 -0700470
Romain Guyd6b2a002011-06-17 17:45:59 -0700471 return status;
Romain Guy77a81162011-06-14 16:45:55 -0700472 }
473 return false;
474}
475
Romain Guy6c319ca2011-01-11 14:29:25 -0800476}; // namespace uirenderer
477}; // namespace android