1 /*
2 * Copyright (C) 2012 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
19 #include <utils/Log.h>
20
21 #include "Caches.h"
22 #include "DeferredDisplayList.h"
23 #include "RenderState.h"
24 #include "Layer.h"
25 #include "LayerRenderer.h"
26 #include "OpenGLRenderer.h"
27 #include "RenderNode.h"
28
29 namespace android {
30 namespace uirenderer {
31
Layer(Type layerType,RenderState & renderState,const uint32_t layerWidth,const uint32_t layerHeight)32 Layer::Layer(Type layerType, RenderState& renderState, const uint32_t layerWidth, const uint32_t layerHeight)
33 : state(kState_Uncached)
34 , caches(Caches::getInstance())
35 , renderState(renderState)
36 , texture(caches)
37 , type(layerType) {
38 mesh = NULL;
39 meshElementCount = 0;
40 cacheable = true;
41 dirty = false;
42 renderTarget = GL_TEXTURE_2D;
43 texture.width = layerWidth;
44 texture.height = layerHeight;
45 colorFilter = NULL;
46 deferredUpdateScheduled = false;
47 renderer = NULL;
48 renderNode = NULL;
49 fbo = 0;
50 stencil = NULL;
51 debugDrawUpdate = false;
52 hasDrawnSinceUpdate = false;
53 forceFilter = false;
54 deferredList = NULL;
55 convexMask = NULL;
56 caches.resourceCache.incrementRefcount(this);
57 rendererLightPosDirty = true;
58 wasBuildLayered = false;
59 if (!isTextureLayer()) {
60 // track only non-texture layer lifecycles in renderstate,
61 // because texture layers are destroyed via finalizer
62 renderState.registerLayer(this);
63 }
64 }
65
~Layer()66 Layer::~Layer() {
67 if (!isTextureLayer()) {
68 renderState.unregisterLayer(this);
69 }
70 SkSafeUnref(colorFilter);
71 removeFbo();
72 deleteTexture();
73
74 delete[] mesh;
75 delete deferredList;
76 delete renderer;
77 }
78
computeIdealWidth(uint32_t layerWidth)79 uint32_t Layer::computeIdealWidth(uint32_t layerWidth) {
80 return uint32_t(ceilf(layerWidth / float(LAYER_SIZE)) * LAYER_SIZE);
81 }
82
computeIdealHeight(uint32_t layerHeight)83 uint32_t Layer::computeIdealHeight(uint32_t layerHeight) {
84 return uint32_t(ceilf(layerHeight / float(LAYER_SIZE)) * LAYER_SIZE);
85 }
86
requireRenderer()87 void Layer::requireRenderer() {
88 if (!renderer) {
89 renderer = new LayerRenderer(renderState, this);
90 renderer->initProperties();
91 }
92 }
93
updateLightPosFromRenderer(const OpenGLRenderer & rootRenderer)94 void Layer::updateLightPosFromRenderer(const OpenGLRenderer& rootRenderer) {
95 if (renderer && rendererLightPosDirty) {
96 // re-init renderer's light position, based upon last cached location in window
97 Vector3 lightPos = rootRenderer.getLightCenter();
98 cachedInvTransformInWindow.mapPoint3d(lightPos);
99 renderer->initLight(lightPos, rootRenderer.getLightRadius(),
100 rootRenderer.getAmbientShadowAlpha(), rootRenderer.getSpotShadowAlpha());
101 rendererLightPosDirty = false;
102 }
103 }
104
resize(const uint32_t width,const uint32_t height)105 bool Layer::resize(const uint32_t width, const uint32_t height) {
106 uint32_t desiredWidth = computeIdealWidth(width);
107 uint32_t desiredHeight = computeIdealWidth(height);
108
109 if (desiredWidth <= getWidth() && desiredHeight <= getHeight()) {
110 return true;
111 }
112
113 ATRACE_NAME("resizeLayer");
114
115 const uint32_t maxTextureSize = caches.maxTextureSize;
116 if (desiredWidth > maxTextureSize || desiredHeight > maxTextureSize) {
117 ALOGW("Layer exceeds max. dimensions supported by the GPU (%dx%d, max=%dx%d)",
118 desiredWidth, desiredHeight, maxTextureSize, maxTextureSize);
119 return false;
120 }
121
122 uint32_t oldWidth = getWidth();
123 uint32_t oldHeight = getHeight();
124
125 setSize(desiredWidth, desiredHeight);
126
127 if (fbo) {
128 caches.activeTexture(0);
129 bindTexture();
130 allocateTexture();
131
132 if (glGetError() != GL_NO_ERROR) {
133 setSize(oldWidth, oldHeight);
134 return false;
135 }
136 }
137
138 if (stencil) {
139 stencil->bind();
140 stencil->resize(desiredWidth, desiredHeight);
141
142 if (glGetError() != GL_NO_ERROR) {
143 setSize(oldWidth, oldHeight);
144 return false;
145 }
146 }
147
148 return true;
149 }
150
removeFbo(bool flush)151 void Layer::removeFbo(bool flush) {
152 if (stencil) {
153 GLuint previousFbo = renderState.getFramebuffer();
154 renderState.bindFramebuffer(fbo);
155 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, 0);
156 renderState.bindFramebuffer(previousFbo);
157
158 caches.renderBufferCache.put(stencil);
159 stencil = NULL;
160 }
161
162 if (fbo) {
163 if (flush) LayerRenderer::flushLayer(renderState, this);
164 // If put fails the cache will delete the FBO
165 caches.fboCache.put(fbo);
166 fbo = 0;
167 }
168 }
169
updateDeferred(RenderNode * renderNode,int left,int top,int right,int bottom)170 void Layer::updateDeferred(RenderNode* renderNode, int left, int top, int right, int bottom) {
171 requireRenderer();
172 this->renderNode = renderNode;
173 const Rect r(left, top, right, bottom);
174 dirtyRect.unionWith(r);
175 deferredUpdateScheduled = true;
176 }
177
setPaint(const SkPaint * paint)178 void Layer::setPaint(const SkPaint* paint) {
179 OpenGLRenderer::getAlphaAndModeDirect(paint, &alpha, &mode);
180 setColorFilter((paint) ? paint->getColorFilter() : NULL);
181 }
182
setColorFilter(SkColorFilter * filter)183 void Layer::setColorFilter(SkColorFilter* filter) {
184 SkRefCnt_SafeAssign(colorFilter, filter);
185 }
186
bindTexture() const187 void Layer::bindTexture() const {
188 if (texture.id) {
189 caches.bindTexture(renderTarget, texture.id);
190 }
191 }
192
bindStencilRenderBuffer() const193 void Layer::bindStencilRenderBuffer() const {
194 if (stencil) {
195 stencil->bind();
196 }
197 }
198
generateTexture()199 void Layer::generateTexture() {
200 if (!texture.id) {
201 glGenTextures(1, &texture.id);
202 }
203 }
204
deleteTexture()205 void Layer::deleteTexture() {
206 if (texture.id) {
207 texture.deleteTexture();
208 texture.id = 0;
209 }
210 }
211
clearTexture()212 void Layer::clearTexture() {
213 caches.unbindTexture(texture.id);
214 texture.id = 0;
215 }
216
allocateTexture()217 void Layer::allocateTexture() {
218 #if DEBUG_LAYERS
219 ALOGD(" Allocate layer: %dx%d", getWidth(), getHeight());
220 #endif
221 if (texture.id) {
222 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
223 glTexImage2D(renderTarget, 0, GL_RGBA, getWidth(), getHeight(), 0,
224 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
225 }
226 }
227
defer(const OpenGLRenderer & rootRenderer)228 void Layer::defer(const OpenGLRenderer& rootRenderer) {
229 updateLightPosFromRenderer(rootRenderer);
230 const float width = layer.getWidth();
231 const float height = layer.getHeight();
232
233 if (dirtyRect.isEmpty() || (dirtyRect.left <= 0 && dirtyRect.top <= 0 &&
234 dirtyRect.right >= width && dirtyRect.bottom >= height)) {
235 dirtyRect.set(0, 0, width, height);
236 }
237
238 delete deferredList;
239 deferredList = new DeferredDisplayList(dirtyRect);
240
241 DeferStateStruct deferredState(*deferredList, *renderer,
242 RenderNode::kReplayFlag_ClipChildren);
243
244 renderer->setViewport(width, height);
245 renderer->setupFrameState(dirtyRect.left, dirtyRect.top,
246 dirtyRect.right, dirtyRect.bottom, !isBlend());
247
248 renderNode->computeOrdering();
249 renderNode->defer(deferredState, 0);
250
251 deferredUpdateScheduled = false;
252 }
253
cancelDefer()254 void Layer::cancelDefer() {
255 renderNode = NULL;
256 deferredUpdateScheduled = false;
257 if (deferredList) {
258 delete deferredList;
259 deferredList = NULL;
260 }
261 }
262
flush()263 void Layer::flush() {
264 // renderer is checked as layer may be destroyed/put in layer cache with flush scheduled
265 if (deferredList && renderer) {
266 renderer->setViewport(layer.getWidth(), layer.getHeight());
267 renderer->prepareDirty(dirtyRect.left, dirtyRect.top, dirtyRect.right, dirtyRect.bottom,
268 !isBlend());
269
270 deferredList->flush(*renderer, dirtyRect);
271
272 renderer->finish();
273
274 dirtyRect.setEmpty();
275 renderNode = NULL;
276 }
277 }
278
render(const OpenGLRenderer & rootRenderer)279 void Layer::render(const OpenGLRenderer& rootRenderer) {
280 updateLightPosFromRenderer(rootRenderer);
281 renderer->setViewport(layer.getWidth(), layer.getHeight());
282 renderer->prepareDirty(dirtyRect.left, dirtyRect.top, dirtyRect.right, dirtyRect.bottom,
283 !isBlend());
284
285 renderer->drawRenderNode(renderNode.get(), dirtyRect, RenderNode::kReplayFlag_ClipChildren);
286
287 renderer->finish();
288
289 dirtyRect.setEmpty();
290
291 deferredUpdateScheduled = false;
292 renderNode = NULL;
293 }
294
295 }; // namespace uirenderer
296 }; // namespace android
297