• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
19 #include <ui/Rect.h>
20 
21 #include <private/hwui/DrawGlInfo.h>
22 
23 #include "LayerCache.h"
24 #include "LayerRenderer.h"
25 #include "Matrix.h"
26 #include "Properties.h"
27 #include "Rect.h"
28 
29 namespace android {
30 namespace uirenderer {
31 
32 ///////////////////////////////////////////////////////////////////////////////
33 // Rendering
34 ///////////////////////////////////////////////////////////////////////////////
35 
LayerRenderer(Layer * layer)36 LayerRenderer::LayerRenderer(Layer* layer): mLayer(layer) {
37 }
38 
~LayerRenderer()39 LayerRenderer::~LayerRenderer() {
40 }
41 
setViewport(int width,int height)42 void LayerRenderer::setViewport(int width, int height) {
43     initViewport(width, height);
44 }
45 
prepareDirty(float left,float top,float right,float bottom,bool opaque)46 status_t LayerRenderer::prepareDirty(float left, float top, float right, float bottom,
47         bool opaque) {
48     LAYER_RENDERER_LOGD("Rendering into layer, fbo = %d", mLayer->getFbo());
49 
50     glBindFramebuffer(GL_FRAMEBUFFER, mLayer->getFbo());
51 
52     const float width = mLayer->layer.getWidth();
53     const float height = mLayer->layer.getHeight();
54 
55     Rect dirty(left, top, right, bottom);
56     if (dirty.isEmpty() || (dirty.left <= 0 && dirty.top <= 0 &&
57             dirty.right >= width && dirty.bottom >= height)) {
58         mLayer->region.clear();
59         dirty.set(0.0f, 0.0f, width, height);
60     } else {
61         dirty.intersect(0.0f, 0.0f, width, height);
62         android::Rect r(dirty.left, dirty.top, dirty.right, dirty.bottom);
63         mLayer->region.subtractSelf(r);
64     }
65 
66     return OpenGLRenderer::prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, opaque);
67 }
68 
clear(float left,float top,float right,float bottom,bool opaque)69 status_t LayerRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
70     if (mLayer->isDirty()) {
71         getCaches().disableScissor();
72         glClear(GL_COLOR_BUFFER_BIT);
73 
74         getCaches().resetScissor();
75         mLayer->setDirty(false);
76 
77         return DrawGlInfo::kStatusDone;
78     }
79 
80     return OpenGLRenderer::clear(left, top, right, bottom, opaque);
81 }
82 
finish()83 void LayerRenderer::finish() {
84     OpenGLRenderer::finish();
85 
86     generateMesh();
87 
88     LAYER_RENDERER_LOGD("Finished rendering into layer, fbo = %d", mLayer->getFbo());
89 
90     // No need to unbind our FBO, this will be taken care of by the caller
91     // who will invoke OpenGLRenderer::resume()
92 }
93 
getTargetFbo()94 GLint LayerRenderer::getTargetFbo() {
95     return mLayer->getFbo();
96 }
97 
suppressErrorChecks()98 bool LayerRenderer::suppressErrorChecks() {
99     return true;
100 }
101 
102 ///////////////////////////////////////////////////////////////////////////////
103 // Dirty region tracking
104 ///////////////////////////////////////////////////////////////////////////////
105 
hasLayer()106 bool LayerRenderer::hasLayer() {
107     return true;
108 }
109 
getRegion()110 Region* LayerRenderer::getRegion() {
111     if (getSnapshot()->flags & Snapshot::kFlagFboTarget) {
112         return OpenGLRenderer::getRegion();
113     }
114     return &mLayer->region;
115 }
116 
117 // TODO: This implementation is flawed and can generate T-junctions
118 //       in the mesh, which will in turn produce cracks when the
119 //       mesh is rotated/skewed. The easiest way to fix this would
120 //       be, for each row, to add new vertices shared with the previous
121 //       row when the two rows share an edge.
122 //       In practice, T-junctions do not appear often so this has yet
123 //       to be fixed.
generateMesh()124 void LayerRenderer::generateMesh() {
125     if (mLayer->region.isRect() || mLayer->region.isEmpty()) {
126         if (mLayer->mesh) {
127             delete mLayer->mesh;
128             delete mLayer->meshIndices;
129 
130             mLayer->mesh = NULL;
131             mLayer->meshIndices = NULL;
132             mLayer->meshElementCount = 0;
133         }
134 
135         mLayer->setRegionAsRect();
136         return;
137     }
138 
139     size_t count;
140     const android::Rect* rects = mLayer->region.getArray(&count);
141 
142     GLsizei elementCount = count * 6;
143 
144     if (mLayer->mesh && mLayer->meshElementCount < elementCount) {
145         delete mLayer->mesh;
146         delete mLayer->meshIndices;
147 
148         mLayer->mesh = NULL;
149         mLayer->meshIndices = NULL;
150     }
151 
152     bool rebuildIndices = false;
153     if (!mLayer->mesh) {
154         mLayer->mesh = new TextureVertex[count * 4];
155         mLayer->meshIndices = new uint16_t[elementCount];
156         rebuildIndices = true;
157     }
158     mLayer->meshElementCount = elementCount;
159 
160     const float texX = 1.0f / float(mLayer->getWidth());
161     const float texY = 1.0f / float(mLayer->getHeight());
162     const float height = mLayer->layer.getHeight();
163 
164     TextureVertex* mesh = mLayer->mesh;
165     uint16_t* indices = mLayer->meshIndices;
166 
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);
179 
180         if (rebuildIndices) {
181             uint16_t quad = i * 4;
182             int index = i * 6;
183             indices[index    ] = quad;       // top-left
184             indices[index + 1] = quad + 1;   // top-right
185             indices[index + 2] = quad + 2;   // bottom-left
186             indices[index + 3] = quad + 2;   // bottom-left
187             indices[index + 4] = quad + 1;   // top-right
188             indices[index + 5] = quad + 3;   // bottom-right
189         }
190     }
191 }
192 
193 ///////////////////////////////////////////////////////////////////////////////
194 // Layers management
195 ///////////////////////////////////////////////////////////////////////////////
196 
createLayer(uint32_t width,uint32_t height,bool isOpaque)197 Layer* LayerRenderer::createLayer(uint32_t width, uint32_t height, bool isOpaque) {
198     LAYER_RENDERER_LOGD("Requesting new render layer %dx%d", width, height);
199 
200     Caches& caches = Caches::getInstance();
201     GLuint fbo = caches.fboCache.get();
202     if (!fbo) {
203         ALOGW("Could not obtain an FBO");
204         return NULL;
205     }
206 
207     caches.activeTexture(0);
208     Layer* layer = caches.layerCache.get(width, height);
209     if (!layer) {
210         ALOGW("Could not obtain a layer");
211         return NULL;
212     }
213 
214     layer->setFbo(fbo);
215     layer->layer.set(0.0f, 0.0f, width, height);
216     layer->texCoords.set(0.0f, height / float(layer->getHeight()),
217             width / float(layer->getWidth()), 0.0f);
218     layer->setAlpha(255, SkXfermode::kSrcOver_Mode);
219     layer->setBlend(!isOpaque);
220     layer->setColorFilter(NULL);
221     layer->setDirty(true);
222     layer->region.clear();
223 
224     GLuint previousFbo;
225     glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*) &previousFbo);
226 
227     glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
228     layer->bindTexture();
229 
230     // Initialize the texture if needed
231     if (layer->isEmpty()) {
232         layer->setEmpty(false);
233         layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
234 
235         if (glGetError() != GL_NO_ERROR) {
236             ALOGD("Could not allocate texture for layer (fbo=%d %dx%d)",
237                     fbo, width, height);
238 
239             glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
240 
241             Caches::getInstance().resourceCache.decrementRefcount(layer);
242 
243             return NULL;
244         }
245     }
246 
247     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
248             layer->getTexture(), 0);
249 
250     glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
251 
252     return layer;
253 }
254 
resizeLayer(Layer * layer,uint32_t width,uint32_t height)255 bool LayerRenderer::resizeLayer(Layer* layer, uint32_t width, uint32_t height) {
256     if (layer) {
257         LAYER_RENDERER_LOGD("Resizing layer fbo = %d to %dx%d", layer->getFbo(), width, height);
258 
259         if (Caches::getInstance().layerCache.resize(layer, width, height)) {
260             layer->layer.set(0.0f, 0.0f, width, height);
261             layer->texCoords.set(0.0f, height / float(layer->getHeight()),
262                     width / float(layer->getWidth()), 0.0f);
263         } else {
264             Caches::getInstance().resourceCache.decrementRefcount(layer);
265             return false;
266         }
267     }
268 
269     return true;
270 }
271 
createTextureLayer(bool isOpaque)272 Layer* LayerRenderer::createTextureLayer(bool isOpaque) {
273     LAYER_RENDERER_LOGD("Creating new texture layer");
274 
275     Layer* layer = new Layer(0, 0);
276     layer->setCacheable(false);
277     layer->setTextureLayer(true);
278     layer->setBlend(!isOpaque);
279     layer->setEmpty(true);
280     layer->setFbo(0);
281     layer->setAlpha(255, SkXfermode::kSrcOver_Mode);
282     layer->layer.set(0.0f, 0.0f, 0.0f, 0.0f);
283     layer->texCoords.set(0.0f, 1.0f, 1.0f, 0.0f);
284     layer->region.clear();
285     layer->setRenderTarget(GL_NONE); // see ::updateTextureLayer()
286 
287     Caches::getInstance().activeTexture(0);
288     layer->generateTexture();
289 
290     return layer;
291 }
292 
updateTextureLayer(Layer * layer,uint32_t width,uint32_t height,bool isOpaque,GLenum renderTarget,float * transform)293 void LayerRenderer::updateTextureLayer(Layer* layer, uint32_t width, uint32_t height,
294         bool isOpaque, GLenum renderTarget, float* transform) {
295     if (layer) {
296         layer->setBlend(!isOpaque);
297         layer->setSize(width, height);
298         layer->layer.set(0.0f, 0.0f, width, height);
299         layer->region.set(width, height);
300         layer->regionRect.set(0.0f, 0.0f, width, height);
301         layer->getTexTransform().load(transform);
302 
303         if (renderTarget != layer->getRenderTarget()) {
304             layer->setRenderTarget(renderTarget);
305             layer->bindTexture();
306             layer->setFilter(GL_NEAREST, false, true);
307             layer->setWrap(GL_CLAMP_TO_EDGE, false, true);
308         }
309     }
310 }
311 
destroyLayer(Layer * layer)312 void LayerRenderer::destroyLayer(Layer* layer) {
313     if (layer) {
314         LAYER_RENDERER_LOGD("Recycling layer, %dx%d fbo = %d",
315                 layer->getWidth(), layer->getHeight(), layer->getFbo());
316 
317         if (!Caches::getInstance().layerCache.put(layer)) {
318             LAYER_RENDERER_LOGD("  Destroyed!");
319             Caches::getInstance().resourceCache.decrementRefcount(layer);
320         } else {
321             LAYER_RENDERER_LOGD("  Cached!");
322 #if DEBUG_LAYER_RENDERER
323             Caches::getInstance().layerCache.dump();
324 #endif
325             layer->removeFbo();
326             layer->region.clear();
327         }
328     }
329 }
330 
destroyLayerDeferred(Layer * layer)331 void LayerRenderer::destroyLayerDeferred(Layer* layer) {
332     if (layer) {
333         LAYER_RENDERER_LOGD("Deferring layer destruction, fbo = %d", layer->getFbo());
334 
335         Caches::getInstance().deleteLayerDeferred(layer);
336     }
337 }
338 
flushLayer(Layer * layer)339 void LayerRenderer::flushLayer(Layer* layer) {
340 #ifdef GL_EXT_discard_framebuffer
341     GLuint fbo = layer->getFbo();
342     if (layer && fbo) {
343         // If possible, discard any enqueud operations on deferred
344         // rendering architectures
345         if (Caches::getInstance().extensions.hasDiscardFramebuffer()) {
346             GLuint previousFbo;
347             glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*) &previousFbo);
348             if (fbo != previousFbo) glBindFramebuffer(GL_FRAMEBUFFER, fbo);
349 
350             const GLenum attachments[] = { GL_COLOR_ATTACHMENT0 };
351             glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
352 
353             if (fbo != previousFbo) glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
354         }
355     }
356 #endif
357 }
358 
copyLayer(Layer * layer,SkBitmap * bitmap)359 bool LayerRenderer::copyLayer(Layer* layer, SkBitmap* bitmap) {
360     Caches& caches = Caches::getInstance();
361     if (layer && bitmap->width() <= caches.maxTextureSize &&
362             bitmap->height() <= caches.maxTextureSize) {
363 
364         GLuint fbo = caches.fboCache.get();
365         if (!fbo) {
366             ALOGW("Could not obtain an FBO");
367             return false;
368         }
369 
370         SkAutoLockPixels alp(*bitmap);
371 
372         GLuint texture;
373         GLuint previousFbo;
374         GLuint previousViewport[4];
375 
376         GLenum format;
377         GLenum type;
378 
379         GLenum error = GL_NO_ERROR;
380         bool status = false;
381 
382         switch (bitmap->config()) {
383             case SkBitmap::kA8_Config:
384                 format = GL_ALPHA;
385                 type = GL_UNSIGNED_BYTE;
386                 break;
387             case SkBitmap::kRGB_565_Config:
388                 format = GL_RGB;
389                 type = GL_UNSIGNED_SHORT_5_6_5;
390                 break;
391             case SkBitmap::kARGB_4444_Config:
392                 format = GL_RGBA;
393                 type = GL_UNSIGNED_SHORT_4_4_4_4;
394                 break;
395             case SkBitmap::kARGB_8888_Config:
396             default:
397                 format = GL_RGBA;
398                 type = GL_UNSIGNED_BYTE;
399                 break;
400         }
401 
402         float alpha = layer->getAlpha();
403         SkXfermode::Mode mode = layer->getMode();
404         GLuint previousLayerFbo = layer->getFbo();
405 
406         layer->setAlpha(255, SkXfermode::kSrc_Mode);
407         layer->setFbo(fbo);
408 
409         glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*) &previousFbo);
410         glGetIntegerv(GL_VIEWPORT, (GLint*) &previousViewport);
411         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
412 
413         glGenTextures(1, &texture);
414         if ((error = glGetError()) != GL_NO_ERROR) goto error;
415 
416         caches.activeTexture(0);
417         glBindTexture(GL_TEXTURE_2D, texture);
418 
419         glPixelStorei(GL_PACK_ALIGNMENT, bitmap->bytesPerPixel());
420 
421         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
422         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
423 
424         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
425         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
426 
427         glTexImage2D(GL_TEXTURE_2D, 0, format, bitmap->width(), bitmap->height(),
428                 0, format, type, NULL);
429         if ((error = glGetError()) != GL_NO_ERROR) goto error;
430 
431         glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
432                 GL_TEXTURE_2D, texture, 0);
433         if ((error = glGetError()) != GL_NO_ERROR) goto error;
434 
435         {
436             LayerRenderer renderer(layer);
437             renderer.setViewport(bitmap->width(), bitmap->height());
438             renderer.OpenGLRenderer::prepareDirty(0.0f, 0.0f,
439                     bitmap->width(), bitmap->height(), !layer->isBlend());
440 
441             caches.disableScissor();
442             renderer.translate(0.0f, bitmap->height());
443             renderer.scale(1.0f, -1.0f);
444 
445             mat4 texTransform(layer->getTexTransform());
446 
447             mat4 invert;
448             invert.translate(0.0f, 1.0f, 0.0f);
449             invert.scale(1.0f, -1.0f, 1.0f);
450             layer->getTexTransform().multiply(invert);
451 
452             if ((error = glGetError()) != GL_NO_ERROR) goto error;
453 
454             {
455                 Rect bounds;
456                 bounds.set(0.0f, 0.0f, bitmap->width(), bitmap->height());
457                 renderer.drawTextureLayer(layer, bounds);
458 
459                 glReadPixels(0, 0, bitmap->width(), bitmap->height(), format,
460                         type, bitmap->getPixels());
461 
462                 if ((error = glGetError()) != GL_NO_ERROR) goto error;
463             }
464 
465             layer->getTexTransform().load(texTransform);
466             status = true;
467         }
468 
469 error:
470 #if DEBUG_OPENGL
471         if (error != GL_NO_ERROR) {
472             ALOGD("GL error while copying layer into bitmap = 0x%x", error);
473         }
474 #endif
475 
476         glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
477         layer->setAlpha(alpha, mode);
478         layer->setFbo(previousLayerFbo);
479         glDeleteTextures(1, &texture);
480         caches.fboCache.put(fbo);
481         glViewport(previousViewport[0], previousViewport[1],
482                 previousViewport[2], previousViewport[3]);
483 
484         return status;
485     }
486     return false;
487 }
488 
489 }; // namespace uirenderer
490 }; // namespace android
491