blob: 7379b639eed17da5e22011e9dc7683a8178707eb [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 {
Romain Guybeff8d82011-02-01 23:53:34 -080042 dirty.intersect(0.0f, 0.0f, mLayer->width, mLayer->height);
Romain Guy3a3133d2011-02-01 22:59:58 -080043 android::Rect r(dirty.left, dirty.top, dirty.right, dirty.bottom);
44 mLayer->region.subtractSelf(r);
45 }
Romain Guyc88e3572011-01-22 00:32:12 -080046#endif
47
Romain Guyada830f2011-01-13 12:13:20 -080048 glBindFramebuffer(GL_FRAMEBUFFER, mLayer->fbo);
Romain Guy1fc883b2011-01-12 14:30:59 -080049
Romain Guy3a3133d2011-02-01 22:59:58 -080050#if RENDER_LAYERS_AS_REGIONS
51 OpenGLRenderer::prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, opaque);
52#else
Romain Guy7d7b5492011-01-24 16:33:45 -080053 OpenGLRenderer::prepareDirty(0.0f, 0.0f, mLayer->width, mLayer->height, opaque);
Romain Guy3a3133d2011-02-01 22:59:58 -080054#endif
Romain Guy6c319ca2011-01-11 14:29:25 -080055}
56
57void LayerRenderer::finish() {
58 OpenGLRenderer::finish();
Romain Guy1fc883b2011-01-12 14:30:59 -080059
Romain Guyf219da52011-01-16 12:54:25 -080060 generateMesh();
61
Chet Haase678e0ad2011-01-25 09:37:18 -080062 LAYER_RENDERER_LOGD("Finished rendering into layer, fbo = %d", mLayer->fbo);
Romain Guy42f3a4b2011-01-19 13:42:26 -080063
64 // No need to unbind our FBO, this will be taken care of by the caller
65 // who will invoke OpenGLRenderer::resume()
66}
67
68GLint LayerRenderer::getTargetFbo() {
69 return mLayer->fbo;
Romain Guy6c319ca2011-01-11 14:29:25 -080070}
71
72///////////////////////////////////////////////////////////////////////////////
Romain Guyf219da52011-01-16 12:54:25 -080073// Dirty region tracking
74///////////////////////////////////////////////////////////////////////////////
75
76bool LayerRenderer::hasLayer() {
77 return true;
78}
79
80Region* LayerRenderer::getRegion() {
81#if RENDER_LAYERS_AS_REGIONS
82 if (getSnapshot()->flags & Snapshot::kFlagFboTarget) {
83 return OpenGLRenderer::getRegion();
84 }
85 return &mLayer->region;
86#else
87 return OpenGLRenderer::getRegion();
88#endif
89}
90
91void LayerRenderer::generateMesh() {
92#if RENDER_LAYERS_AS_REGIONS
93 if (mLayer->region.isRect() || mLayer->region.isEmpty()) {
94 if (mLayer->mesh) {
95 delete mLayer->mesh;
96 delete mLayer->meshIndices;
97
98 mLayer->mesh = NULL;
99 mLayer->meshIndices = NULL;
100 mLayer->meshElementCount = 0;
101 }
102 return;
103 }
104
105 size_t count;
106 const android::Rect* rects = mLayer->region.getArray(&count);
107
108 GLsizei elementCount = count * 6;
109
110 if (mLayer->mesh && mLayer->meshElementCount < elementCount) {
111 delete mLayer->mesh;
112 delete mLayer->meshIndices;
113
114 mLayer->mesh = NULL;
115 mLayer->meshIndices = NULL;
116 }
117
Romain Guy4f09f542011-01-26 22:41:43 -0800118 bool rebuildIndices = false;
Romain Guyf219da52011-01-16 12:54:25 -0800119 if (!mLayer->mesh) {
120 mLayer->mesh = new TextureVertex[count * 4];
121 mLayer->meshIndices = new uint16_t[elementCount];
Romain Guy4f09f542011-01-26 22:41:43 -0800122 rebuildIndices = true;
Romain Guyf219da52011-01-16 12:54:25 -0800123 }
Romain Guy4f09f542011-01-26 22:41:43 -0800124 mLayer->meshElementCount = elementCount;
Romain Guyf219da52011-01-16 12:54:25 -0800125
126 const float texX = 1.0f / float(mLayer->width);
127 const float texY = 1.0f / float(mLayer->height);
128 const float height = mLayer->layer.getHeight();
129
130 TextureVertex* mesh = mLayer->mesh;
131 uint16_t* indices = mLayer->meshIndices;
132
133 for (size_t i = 0; i < count; i++) {
134 const android::Rect* r = &rects[i];
135
136 const float u1 = r->left * texX;
137 const float v1 = (height - r->top) * texY;
138 const float u2 = r->right * texX;
139 const float v2 = (height - r->bottom) * texY;
140
141 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
142 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
143 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
144 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
145
Romain Guy4f09f542011-01-26 22:41:43 -0800146 if (rebuildIndices) {
147 uint16_t quad = i * 4;
148 int index = i * 6;
149 indices[index ] = quad; // top-left
150 indices[index + 1] = quad + 1; // top-right
151 indices[index + 2] = quad + 2; // bottom-left
152 indices[index + 3] = quad + 2; // bottom-left
153 indices[index + 4] = quad + 1; // top-right
154 indices[index + 5] = quad + 3; // bottom-right
155 }
Romain Guyf219da52011-01-16 12:54:25 -0800156 }
157#endif
158}
159
160///////////////////////////////////////////////////////////////////////////////
161// Layers management
Romain Guy6c319ca2011-01-11 14:29:25 -0800162///////////////////////////////////////////////////////////////////////////////
163
Romain Guyada830f2011-01-13 12:13:20 -0800164Layer* LayerRenderer::createLayer(uint32_t width, uint32_t height, bool isOpaque) {
Romain Guy1fc883b2011-01-12 14:30:59 -0800165 LAYER_RENDERER_LOGD("Creating new layer %dx%d", width, height);
166
Romain Guyada830f2011-01-13 12:13:20 -0800167 Layer* layer = new Layer(width, height);
168
Romain Guy6c319ca2011-01-11 14:29:25 -0800169 GLuint previousFbo;
170 glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*) &previousFbo);
171
Romain Guyada830f2011-01-13 12:13:20 -0800172 glGenFramebuffers(1, &layer->fbo);
173 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
Romain Guy6c319ca2011-01-11 14:29:25 -0800174
Romain Guy1fc883b2011-01-12 14:30:59 -0800175 if (glGetError() != GL_NO_ERROR) {
176 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guyada830f2011-01-13 12:13:20 -0800177 glDeleteBuffers(1, &layer->fbo);
Romain Guy1fc883b2011-01-12 14:30:59 -0800178 return 0;
179 }
180
Romain Guy6c319ca2011-01-11 14:29:25 -0800181 glActiveTexture(GL_TEXTURE0);
Romain Guyada830f2011-01-13 12:13:20 -0800182 glGenTextures(1, &layer->texture);
183 glBindTexture(GL_TEXTURE_2D, layer->texture);
Romain Guy6c319ca2011-01-11 14:29:25 -0800184
185 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
186
187 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
188 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
189
190 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
191 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
192
193 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
194 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
195
196 if (glGetError() != GL_NO_ERROR) {
Romain Guy1fc883b2011-01-12 14:30:59 -0800197 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guyada830f2011-01-13 12:13:20 -0800198 glDeleteBuffers(1, &layer->fbo);
199 glDeleteTextures(1, &layer->texture);
200 delete layer;
Romain Guy6c319ca2011-01-11 14:29:25 -0800201 return 0;
202 }
203
204 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guyada830f2011-01-13 12:13:20 -0800205 layer->texture, 0);
Romain Guy6c319ca2011-01-11 14:29:25 -0800206
207 if (glGetError() != GL_NO_ERROR) {
Romain Guy1fc883b2011-01-12 14:30:59 -0800208 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guyada830f2011-01-13 12:13:20 -0800209 glDeleteBuffers(1, &layer->fbo);
210 glDeleteTextures(1, &layer->texture);
211 delete layer;
Romain Guy6c319ca2011-01-11 14:29:25 -0800212 return 0;
213 }
214
215 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
216
Romain Guyada830f2011-01-13 12:13:20 -0800217 layer->layer.set(0.0f, 0.0f, width, height);
218 layer->texCoords.set(0.0f, 1.0f, 1.0f, 0.0f);
219 layer->alpha = 255;
220 layer->mode = SkXfermode::kSrcOver_Mode;
221 layer->blend = !isOpaque;
222 layer->empty = false;
223 layer->colorFilter = NULL;
Romain Guy6c319ca2011-01-11 14:29:25 -0800224
Romain Guyada830f2011-01-13 12:13:20 -0800225 return layer;
Romain Guy6c319ca2011-01-11 14:29:25 -0800226}
227
Romain Guyada830f2011-01-13 12:13:20 -0800228bool LayerRenderer::resizeLayer(Layer* layer, uint32_t width, uint32_t height) {
229 if (layer) {
230 LAYER_RENDERER_LOGD("Resizing layer fbo = %d to %dx%d", layer->fbo, width, height);
Romain Guy1fc883b2011-01-12 14:30:59 -0800231
Romain Guyada830f2011-01-13 12:13:20 -0800232 glActiveTexture(GL_TEXTURE0);
233 glBindTexture(GL_TEXTURE_2D, layer->texture);
Romain Guy6c319ca2011-01-11 14:29:25 -0800234
Romain Guyada830f2011-01-13 12:13:20 -0800235 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
236 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
Romain Guy6c319ca2011-01-11 14:29:25 -0800237
Romain Guyada830f2011-01-13 12:13:20 -0800238 if (glGetError() != GL_NO_ERROR) {
239 glDeleteBuffers(1, &layer->fbo);
240 glDeleteTextures(1, &layer->texture);
Romain Guy6c319ca2011-01-11 14:29:25 -0800241
Romain Guyada830f2011-01-13 12:13:20 -0800242 layer->width = 0;
243 layer->height = 0;
244 layer->fbo = 0;
245 layer->texture = 0;
Romain Guy6c319ca2011-01-11 14:29:25 -0800246
Romain Guyada830f2011-01-13 12:13:20 -0800247 return false;
248 }
249
250 layer->width = width;
251 layer->height = height;
Romain Guy6c319ca2011-01-11 14:29:25 -0800252 }
Romain Guyada830f2011-01-13 12:13:20 -0800253 return true;
Romain Guy6c319ca2011-01-11 14:29:25 -0800254}
255
Romain Guyada830f2011-01-13 12:13:20 -0800256void LayerRenderer::destroyLayer(Layer* layer) {
257 if (layer) {
258 LAYER_RENDERER_LOGD("Destroying layer, fbo = %d", layer->fbo);
Romain Guy1fc883b2011-01-12 14:30:59 -0800259
Romain Guyada830f2011-01-13 12:13:20 -0800260 if (layer->fbo) glDeleteFramebuffers(1, &layer->fbo);
261 if (layer->texture) glDeleteTextures(1, &layer->texture);
262
263 delete layer;
264 }
Romain Guy6c319ca2011-01-11 14:29:25 -0800265}
266
Romain Guyada830f2011-01-13 12:13:20 -0800267void LayerRenderer::destroyLayerDeferred(Layer* layer) {
268 if (layer) {
269 LAYER_RENDERER_LOGD("Deferring layer destruction, fbo = %d", layer->fbo);
Romain Guy1fc883b2011-01-12 14:30:59 -0800270
Romain Guyada830f2011-01-13 12:13:20 -0800271 Caches::getInstance().deleteLayerDeferred(layer);
272 }
Romain Guy57066eb2011-01-12 12:53:32 -0800273}
274
Romain Guy6c319ca2011-01-11 14:29:25 -0800275}; // namespace uirenderer
276}; // namespace android