blob: 60ff0bfafdcbb7f8000654e8a829fb65950fee9b [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 Guy62687ec2011-02-02 15:44:19 -080035 glBindFramebuffer(GL_FRAMEBUFFER, mLayer->fbo);
36
Romain Guyc88e3572011-01-22 00:32:12 -080037#if RENDER_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -080038 Rect dirty(left, top, right, bottom);
39 if (dirty.isEmpty() || (dirty.left <= 0 && dirty.top <= 0 &&
40 dirty.right >= mLayer->width && dirty.bottom >= mLayer->height)) {
41 mLayer->region.clear();
42 dirty.set(0.0f, 0.0f, mLayer->width, mLayer->height);
43 } else {
Romain Guybeff8d82011-02-01 23:53:34 -080044 dirty.intersect(0.0f, 0.0f, mLayer->width, mLayer->height);
Romain Guy3a3133d2011-02-01 22:59:58 -080045 android::Rect r(dirty.left, dirty.top, dirty.right, dirty.bottom);
46 mLayer->region.subtractSelf(r);
47 }
Romain Guyc88e3572011-01-22 00:32:12 -080048
Romain Guy3a3133d2011-02-01 22:59:58 -080049 OpenGLRenderer::prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, opaque);
50#else
Romain Guy7d7b5492011-01-24 16:33:45 -080051 OpenGLRenderer::prepareDirty(0.0f, 0.0f, mLayer->width, mLayer->height, opaque);
Romain Guy3a3133d2011-02-01 22:59:58 -080052#endif
Romain Guy6c319ca2011-01-11 14:29:25 -080053}
54
55void LayerRenderer::finish() {
56 OpenGLRenderer::finish();
Romain Guy1fc883b2011-01-12 14:30:59 -080057
Romain Guyf219da52011-01-16 12:54:25 -080058 generateMesh();
59
Chet Haase678e0ad2011-01-25 09:37:18 -080060 LAYER_RENDERER_LOGD("Finished rendering into layer, fbo = %d", mLayer->fbo);
Romain Guy42f3a4b2011-01-19 13:42:26 -080061
62 // No need to unbind our FBO, this will be taken care of by the caller
63 // who will invoke OpenGLRenderer::resume()
64}
65
66GLint LayerRenderer::getTargetFbo() {
67 return mLayer->fbo;
Romain Guy6c319ca2011-01-11 14:29:25 -080068}
69
70///////////////////////////////////////////////////////////////////////////////
Romain Guyf219da52011-01-16 12:54:25 -080071// Dirty region tracking
72///////////////////////////////////////////////////////////////////////////////
73
74bool LayerRenderer::hasLayer() {
75 return true;
76}
77
78Region* LayerRenderer::getRegion() {
79#if RENDER_LAYERS_AS_REGIONS
80 if (getSnapshot()->flags & Snapshot::kFlagFboTarget) {
81 return OpenGLRenderer::getRegion();
82 }
83 return &mLayer->region;
84#else
85 return OpenGLRenderer::getRegion();
86#endif
87}
88
89void LayerRenderer::generateMesh() {
90#if RENDER_LAYERS_AS_REGIONS
91 if (mLayer->region.isRect() || mLayer->region.isEmpty()) {
92 if (mLayer->mesh) {
93 delete mLayer->mesh;
94 delete mLayer->meshIndices;
95
96 mLayer->mesh = NULL;
97 mLayer->meshIndices = NULL;
98 mLayer->meshElementCount = 0;
99 }
100 return;
101 }
102
103 size_t count;
104 const android::Rect* rects = mLayer->region.getArray(&count);
105
106 GLsizei elementCount = count * 6;
107
108 if (mLayer->mesh && mLayer->meshElementCount < elementCount) {
109 delete mLayer->mesh;
110 delete mLayer->meshIndices;
111
112 mLayer->mesh = NULL;
113 mLayer->meshIndices = NULL;
114 }
115
Romain Guy4f09f542011-01-26 22:41:43 -0800116 bool rebuildIndices = false;
Romain Guyf219da52011-01-16 12:54:25 -0800117 if (!mLayer->mesh) {
118 mLayer->mesh = new TextureVertex[count * 4];
119 mLayer->meshIndices = new uint16_t[elementCount];
Romain Guy4f09f542011-01-26 22:41:43 -0800120 rebuildIndices = true;
Romain Guyf219da52011-01-16 12:54:25 -0800121 }
Romain Guy4f09f542011-01-26 22:41:43 -0800122 mLayer->meshElementCount = elementCount;
Romain Guyf219da52011-01-16 12:54:25 -0800123
124 const float texX = 1.0f / float(mLayer->width);
125 const float texY = 1.0f / float(mLayer->height);
126 const float height = mLayer->layer.getHeight();
127
128 TextureVertex* mesh = mLayer->mesh;
129 uint16_t* indices = mLayer->meshIndices;
130
131 for (size_t i = 0; i < count; i++) {
132 const android::Rect* r = &rects[i];
133
134 const float u1 = r->left * texX;
135 const float v1 = (height - r->top) * texY;
136 const float u2 = r->right * texX;
137 const float v2 = (height - r->bottom) * texY;
138
139 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
140 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
141 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
142 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
143
Romain Guy4f09f542011-01-26 22:41:43 -0800144 if (rebuildIndices) {
145 uint16_t quad = i * 4;
146 int index = i * 6;
147 indices[index ] = quad; // top-left
148 indices[index + 1] = quad + 1; // top-right
149 indices[index + 2] = quad + 2; // bottom-left
150 indices[index + 3] = quad + 2; // bottom-left
151 indices[index + 4] = quad + 1; // top-right
152 indices[index + 5] = quad + 3; // bottom-right
153 }
Romain Guyf219da52011-01-16 12:54:25 -0800154 }
155#endif
156}
157
158///////////////////////////////////////////////////////////////////////////////
159// Layers management
Romain Guy6c319ca2011-01-11 14:29:25 -0800160///////////////////////////////////////////////////////////////////////////////
161
Romain Guyada830f2011-01-13 12:13:20 -0800162Layer* LayerRenderer::createLayer(uint32_t width, uint32_t height, bool isOpaque) {
Romain Guy1fc883b2011-01-12 14:30:59 -0800163 LAYER_RENDERER_LOGD("Creating new layer %dx%d", width, height);
164
Romain Guyada830f2011-01-13 12:13:20 -0800165 Layer* layer = new Layer(width, height);
166
Romain Guy6c319ca2011-01-11 14:29:25 -0800167 GLuint previousFbo;
168 glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*) &previousFbo);
169
Romain Guyada830f2011-01-13 12:13:20 -0800170 glGenFramebuffers(1, &layer->fbo);
171 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
Romain Guy6c319ca2011-01-11 14:29:25 -0800172
Romain Guy1fc883b2011-01-12 14:30:59 -0800173 if (glGetError() != GL_NO_ERROR) {
174 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guyada830f2011-01-13 12:13:20 -0800175 glDeleteBuffers(1, &layer->fbo);
Romain Guy1fc883b2011-01-12 14:30:59 -0800176 return 0;
177 }
178
Romain Guy6c319ca2011-01-11 14:29:25 -0800179 glActiveTexture(GL_TEXTURE0);
Romain Guyada830f2011-01-13 12:13:20 -0800180 glGenTextures(1, &layer->texture);
181 glBindTexture(GL_TEXTURE_2D, layer->texture);
Romain Guy6c319ca2011-01-11 14:29:25 -0800182
183 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
184
185 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
186 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
187
188 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
189 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
190
191 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
192 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
193
194 if (glGetError() != GL_NO_ERROR) {
Romain Guy1fc883b2011-01-12 14:30:59 -0800195 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guyada830f2011-01-13 12:13:20 -0800196 glDeleteBuffers(1, &layer->fbo);
197 glDeleteTextures(1, &layer->texture);
198 delete layer;
Romain Guy6c319ca2011-01-11 14:29:25 -0800199 return 0;
200 }
201
202 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guyada830f2011-01-13 12:13:20 -0800203 layer->texture, 0);
Romain Guy6c319ca2011-01-11 14:29:25 -0800204
205 if (glGetError() != GL_NO_ERROR) {
Romain Guy1fc883b2011-01-12 14:30:59 -0800206 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guyada830f2011-01-13 12:13:20 -0800207 glDeleteBuffers(1, &layer->fbo);
208 glDeleteTextures(1, &layer->texture);
209 delete layer;
Romain Guy6c319ca2011-01-11 14:29:25 -0800210 return 0;
211 }
212
213 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
214
Romain Guyada830f2011-01-13 12:13:20 -0800215 layer->layer.set(0.0f, 0.0f, width, height);
216 layer->texCoords.set(0.0f, 1.0f, 1.0f, 0.0f);
217 layer->alpha = 255;
218 layer->mode = SkXfermode::kSrcOver_Mode;
219 layer->blend = !isOpaque;
220 layer->empty = false;
221 layer->colorFilter = NULL;
Romain Guy6c319ca2011-01-11 14:29:25 -0800222
Romain Guyada830f2011-01-13 12:13:20 -0800223 return layer;
Romain Guy6c319ca2011-01-11 14:29:25 -0800224}
225
Romain Guyada830f2011-01-13 12:13:20 -0800226bool LayerRenderer::resizeLayer(Layer* layer, uint32_t width, uint32_t height) {
227 if (layer) {
228 LAYER_RENDERER_LOGD("Resizing layer fbo = %d to %dx%d", layer->fbo, width, height);
Romain Guy1fc883b2011-01-12 14:30:59 -0800229
Romain Guyada830f2011-01-13 12:13:20 -0800230 glActiveTexture(GL_TEXTURE0);
231 glBindTexture(GL_TEXTURE_2D, layer->texture);
Romain Guy6c319ca2011-01-11 14:29:25 -0800232
Romain Guyada830f2011-01-13 12:13:20 -0800233 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
234 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
Romain Guy6c319ca2011-01-11 14:29:25 -0800235
Romain Guyada830f2011-01-13 12:13:20 -0800236 if (glGetError() != GL_NO_ERROR) {
237 glDeleteBuffers(1, &layer->fbo);
238 glDeleteTextures(1, &layer->texture);
Romain Guy6c319ca2011-01-11 14:29:25 -0800239
Romain Guyada830f2011-01-13 12:13:20 -0800240 layer->width = 0;
241 layer->height = 0;
242 layer->fbo = 0;
243 layer->texture = 0;
Romain Guy6c319ca2011-01-11 14:29:25 -0800244
Romain Guyada830f2011-01-13 12:13:20 -0800245 return false;
246 }
247
248 layer->width = width;
249 layer->height = height;
Romain Guy6c319ca2011-01-11 14:29:25 -0800250 }
Romain Guyada830f2011-01-13 12:13:20 -0800251 return true;
Romain Guy6c319ca2011-01-11 14:29:25 -0800252}
253
Romain Guyada830f2011-01-13 12:13:20 -0800254void LayerRenderer::destroyLayer(Layer* layer) {
255 if (layer) {
256 LAYER_RENDERER_LOGD("Destroying layer, fbo = %d", layer->fbo);
Romain Guy1fc883b2011-01-12 14:30:59 -0800257
Romain Guyada830f2011-01-13 12:13:20 -0800258 if (layer->fbo) glDeleteFramebuffers(1, &layer->fbo);
259 if (layer->texture) glDeleteTextures(1, &layer->texture);
260
261 delete layer;
262 }
Romain Guy6c319ca2011-01-11 14:29:25 -0800263}
264
Romain Guyada830f2011-01-13 12:13:20 -0800265void LayerRenderer::destroyLayerDeferred(Layer* layer) {
266 if (layer) {
267 LAYER_RENDERER_LOGD("Deferring layer destruction, fbo = %d", layer->fbo);
Romain Guy1fc883b2011-01-12 14:30:59 -0800268
Romain Guyada830f2011-01-13 12:13:20 -0800269 Caches::getInstance().deleteLayerDeferred(layer);
270 }
Romain Guy57066eb2011-01-12 12:53:32 -0800271}
272
Romain Guy6c319ca2011-01-11 14:29:25 -0800273}; // namespace uirenderer
274}; // namespace android