blob: aebd2b7c696db5c1da1f3d9ca7a9d58342214beb [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"
18
Romain Guy3a3133d2011-02-01 22:59:58 -080019#include <ui/Rect.h>
20
Romain Guy6c319ca2011-01-11 14:29:25 -080021#include "LayerRenderer.h"
Romain Guy1fc883b2011-01-12 14:30:59 -080022#include "Properties.h"
Romain Guy3a3133d2011-02-01 22:59:58 -080023#include "Rect.h"
Romain Guy6c319ca2011-01-11 14:29:25 -080024
25namespace android {
26namespace uirenderer {
27
28///////////////////////////////////////////////////////////////////////////////
29// Rendering
30///////////////////////////////////////////////////////////////////////////////
31
Romain Guy7d7b5492011-01-24 16:33:45 -080032void LayerRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) {
Romain Guyada830f2011-01-13 12:13:20 -080033 LAYER_RENDERER_LOGD("Rendering into layer, fbo = %d", mLayer->fbo);
Romain Guy1fc883b2011-01-12 14:30:59 -080034
Romain Guyc88e3572011-01-22 00:32:12 -080035#if RENDER_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -080036 Rect dirty(left, top, right, bottom);
37 if (dirty.isEmpty() || (dirty.left <= 0 && dirty.top <= 0 &&
38 dirty.right >= mLayer->width && dirty.bottom >= mLayer->height)) {
39 mLayer->region.clear();
40 dirty.set(0.0f, 0.0f, mLayer->width, mLayer->height);
41 } else {
42 android::Rect r(dirty.left, dirty.top, dirty.right, dirty.bottom);
43 mLayer->region.subtractSelf(r);
44 }
Romain Guyc88e3572011-01-22 00:32:12 -080045#endif
46
Romain Guyada830f2011-01-13 12:13:20 -080047 glBindFramebuffer(GL_FRAMEBUFFER, mLayer->fbo);
Romain Guy1fc883b2011-01-12 14:30:59 -080048
Romain Guy3a3133d2011-02-01 22:59:58 -080049#if RENDER_LAYERS_AS_REGIONS
50 OpenGLRenderer::prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, opaque);
51#else
Romain Guy7d7b5492011-01-24 16:33:45 -080052 OpenGLRenderer::prepareDirty(0.0f, 0.0f, mLayer->width, mLayer->height, opaque);
Romain Guy3a3133d2011-02-01 22:59:58 -080053#endif
Romain Guy6c319ca2011-01-11 14:29:25 -080054}
55
56void LayerRenderer::finish() {
57 OpenGLRenderer::finish();
Romain Guy1fc883b2011-01-12 14:30:59 -080058
Romain Guyf219da52011-01-16 12:54:25 -080059 generateMesh();
60
Chet Haase678e0ad2011-01-25 09:37:18 -080061 LAYER_RENDERER_LOGD("Finished rendering into layer, fbo = %d", mLayer->fbo);
Romain Guy42f3a4b2011-01-19 13:42:26 -080062
63 // No need to unbind our FBO, this will be taken care of by the caller
64 // who will invoke OpenGLRenderer::resume()
65}
66
67GLint LayerRenderer::getTargetFbo() {
68 return mLayer->fbo;
Romain Guy6c319ca2011-01-11 14:29:25 -080069}
70
71///////////////////////////////////////////////////////////////////////////////
Romain Guyf219da52011-01-16 12:54:25 -080072// Dirty region tracking
73///////////////////////////////////////////////////////////////////////////////
74
75bool LayerRenderer::hasLayer() {
76 return true;
77}
78
79Region* LayerRenderer::getRegion() {
80#if RENDER_LAYERS_AS_REGIONS
81 if (getSnapshot()->flags & Snapshot::kFlagFboTarget) {
82 return OpenGLRenderer::getRegion();
83 }
84 return &mLayer->region;
85#else
86 return OpenGLRenderer::getRegion();
87#endif
88}
89
90void LayerRenderer::generateMesh() {
91#if RENDER_LAYERS_AS_REGIONS
92 if (mLayer->region.isRect() || mLayer->region.isEmpty()) {
93 if (mLayer->mesh) {
94 delete mLayer->mesh;
95 delete mLayer->meshIndices;
96
97 mLayer->mesh = NULL;
98 mLayer->meshIndices = NULL;
99 mLayer->meshElementCount = 0;
100 }
101 return;
102 }
103
104 size_t count;
105 const android::Rect* rects = mLayer->region.getArray(&count);
106
107 GLsizei elementCount = count * 6;
108
109 if (mLayer->mesh && mLayer->meshElementCount < elementCount) {
110 delete mLayer->mesh;
111 delete mLayer->meshIndices;
112
113 mLayer->mesh = NULL;
114 mLayer->meshIndices = NULL;
115 }
116
Romain Guy4f09f542011-01-26 22:41:43 -0800117 bool rebuildIndices = false;
Romain Guyf219da52011-01-16 12:54:25 -0800118 if (!mLayer->mesh) {
119 mLayer->mesh = new TextureVertex[count * 4];
120 mLayer->meshIndices = new uint16_t[elementCount];
Romain Guy4f09f542011-01-26 22:41:43 -0800121 rebuildIndices = true;
Romain Guyf219da52011-01-16 12:54:25 -0800122 }
Romain Guy4f09f542011-01-26 22:41:43 -0800123 mLayer->meshElementCount = elementCount;
Romain Guyf219da52011-01-16 12:54:25 -0800124
125 const float texX = 1.0f / float(mLayer->width);
126 const float texY = 1.0f / float(mLayer->height);
127 const float height = mLayer->layer.getHeight();
128
129 TextureVertex* mesh = mLayer->mesh;
130 uint16_t* indices = mLayer->meshIndices;
131
132 for (size_t i = 0; i < count; i++) {
133 const android::Rect* r = &rects[i];
134
135 const float u1 = r->left * texX;
136 const float v1 = (height - r->top) * texY;
137 const float u2 = r->right * texX;
138 const float v2 = (height - r->bottom) * texY;
139
140 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
141 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
142 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
143 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
144
Romain Guy4f09f542011-01-26 22:41:43 -0800145 if (rebuildIndices) {
146 uint16_t quad = i * 4;
147 int index = i * 6;
148 indices[index ] = quad; // top-left
149 indices[index + 1] = quad + 1; // top-right
150 indices[index + 2] = quad + 2; // bottom-left
151 indices[index + 3] = quad + 2; // bottom-left
152 indices[index + 4] = quad + 1; // top-right
153 indices[index + 5] = quad + 3; // bottom-right
154 }
Romain Guyf219da52011-01-16 12:54:25 -0800155 }
156#endif
157}
158
159///////////////////////////////////////////////////////////////////////////////
160// Layers management
Romain Guy6c319ca2011-01-11 14:29:25 -0800161///////////////////////////////////////////////////////////////////////////////
162
Romain Guyada830f2011-01-13 12:13:20 -0800163Layer* LayerRenderer::createLayer(uint32_t width, uint32_t height, bool isOpaque) {
Romain Guy1fc883b2011-01-12 14:30:59 -0800164 LAYER_RENDERER_LOGD("Creating new layer %dx%d", width, height);
165
Romain Guyada830f2011-01-13 12:13:20 -0800166 Layer* layer = new Layer(width, height);
167
Romain Guy6c319ca2011-01-11 14:29:25 -0800168 GLuint previousFbo;
169 glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*) &previousFbo);
170
Romain Guyada830f2011-01-13 12:13:20 -0800171 glGenFramebuffers(1, &layer->fbo);
172 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
Romain Guy6c319ca2011-01-11 14:29:25 -0800173
Romain Guy1fc883b2011-01-12 14:30:59 -0800174 if (glGetError() != GL_NO_ERROR) {
175 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guyada830f2011-01-13 12:13:20 -0800176 glDeleteBuffers(1, &layer->fbo);
Romain Guy1fc883b2011-01-12 14:30:59 -0800177 return 0;
178 }
179
Romain Guy6c319ca2011-01-11 14:29:25 -0800180 glActiveTexture(GL_TEXTURE0);
Romain Guyada830f2011-01-13 12:13:20 -0800181 glGenTextures(1, &layer->texture);
182 glBindTexture(GL_TEXTURE_2D, layer->texture);
Romain Guy6c319ca2011-01-11 14:29:25 -0800183
184 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
185
186 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
187 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
188
189 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
190 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
191
192 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
193 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
194
195 if (glGetError() != GL_NO_ERROR) {
Romain Guy1fc883b2011-01-12 14:30:59 -0800196 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guyada830f2011-01-13 12:13:20 -0800197 glDeleteBuffers(1, &layer->fbo);
198 glDeleteTextures(1, &layer->texture);
199 delete layer;
Romain Guy6c319ca2011-01-11 14:29:25 -0800200 return 0;
201 }
202
203 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guyada830f2011-01-13 12:13:20 -0800204 layer->texture, 0);
Romain Guy6c319ca2011-01-11 14:29:25 -0800205
206 if (glGetError() != GL_NO_ERROR) {
Romain Guy1fc883b2011-01-12 14:30:59 -0800207 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guyada830f2011-01-13 12:13:20 -0800208 glDeleteBuffers(1, &layer->fbo);
209 glDeleteTextures(1, &layer->texture);
210 delete layer;
Romain Guy6c319ca2011-01-11 14:29:25 -0800211 return 0;
212 }
213
214 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
215
Romain Guyada830f2011-01-13 12:13:20 -0800216 layer->layer.set(0.0f, 0.0f, width, height);
217 layer->texCoords.set(0.0f, 1.0f, 1.0f, 0.0f);
218 layer->alpha = 255;
219 layer->mode = SkXfermode::kSrcOver_Mode;
220 layer->blend = !isOpaque;
221 layer->empty = false;
222 layer->colorFilter = NULL;
Romain Guy6c319ca2011-01-11 14:29:25 -0800223
Romain Guyada830f2011-01-13 12:13:20 -0800224 return layer;
Romain Guy6c319ca2011-01-11 14:29:25 -0800225}
226
Romain Guyada830f2011-01-13 12:13:20 -0800227bool LayerRenderer::resizeLayer(Layer* layer, uint32_t width, uint32_t height) {
228 if (layer) {
229 LAYER_RENDERER_LOGD("Resizing layer fbo = %d to %dx%d", layer->fbo, width, height);
Romain Guy1fc883b2011-01-12 14:30:59 -0800230
Romain Guyada830f2011-01-13 12:13:20 -0800231 glActiveTexture(GL_TEXTURE0);
232 glBindTexture(GL_TEXTURE_2D, layer->texture);
Romain Guy6c319ca2011-01-11 14:29:25 -0800233
Romain Guyada830f2011-01-13 12:13:20 -0800234 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
235 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
Romain Guy6c319ca2011-01-11 14:29:25 -0800236
Romain Guyada830f2011-01-13 12:13:20 -0800237 if (glGetError() != GL_NO_ERROR) {
238 glDeleteBuffers(1, &layer->fbo);
239 glDeleteTextures(1, &layer->texture);
Romain Guy6c319ca2011-01-11 14:29:25 -0800240
Romain Guyada830f2011-01-13 12:13:20 -0800241 layer->width = 0;
242 layer->height = 0;
243 layer->fbo = 0;
244 layer->texture = 0;
Romain Guy6c319ca2011-01-11 14:29:25 -0800245
Romain Guyada830f2011-01-13 12:13:20 -0800246 return false;
247 }
248
249 layer->width = width;
250 layer->height = height;
Romain Guy6c319ca2011-01-11 14:29:25 -0800251 }
Romain Guyada830f2011-01-13 12:13:20 -0800252 return true;
Romain Guy6c319ca2011-01-11 14:29:25 -0800253}
254
Romain Guyada830f2011-01-13 12:13:20 -0800255void LayerRenderer::destroyLayer(Layer* layer) {
256 if (layer) {
257 LAYER_RENDERER_LOGD("Destroying layer, fbo = %d", layer->fbo);
Romain Guy1fc883b2011-01-12 14:30:59 -0800258
Romain Guyada830f2011-01-13 12:13:20 -0800259 if (layer->fbo) glDeleteFramebuffers(1, &layer->fbo);
260 if (layer->texture) glDeleteTextures(1, &layer->texture);
261
262 delete layer;
263 }
Romain Guy6c319ca2011-01-11 14:29:25 -0800264}
265
Romain Guyada830f2011-01-13 12:13:20 -0800266void LayerRenderer::destroyLayerDeferred(Layer* layer) {
267 if (layer) {
268 LAYER_RENDERER_LOGD("Deferring layer destruction, fbo = %d", layer->fbo);
Romain Guy1fc883b2011-01-12 14:30:59 -0800269
Romain Guyada830f2011-01-13 12:13:20 -0800270 Caches::getInstance().deleteLayerDeferred(layer);
271 }
Romain Guy57066eb2011-01-12 12:53:32 -0800272}
273
Romain Guy6c319ca2011-01-11 14:29:25 -0800274}; // namespace uirenderer
275}; // namespace android