blob: e6bea786dfdc526f083556c88fb90ba73698e935 [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
19#include "LayerRenderer.h"
Romain Guy1fc883b2011-01-12 14:30:59 -080020#include "Properties.h"
Romain Guy6c319ca2011-01-11 14:29:25 -080021
22namespace android {
23namespace uirenderer {
24
25///////////////////////////////////////////////////////////////////////////////
26// Rendering
27///////////////////////////////////////////////////////////////////////////////
28
29void LayerRenderer::prepare(bool opaque) {
Romain Guyada830f2011-01-13 12:13:20 -080030 LAYER_RENDERER_LOGD("Rendering into layer, fbo = %d", mLayer->fbo);
Romain Guy1fc883b2011-01-12 14:30:59 -080031
Romain Guy6c319ca2011-01-11 14:29:25 -080032 glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*) &mPreviousFbo);
Romain Guyada830f2011-01-13 12:13:20 -080033 glBindFramebuffer(GL_FRAMEBUFFER, mLayer->fbo);
Romain Guy1fc883b2011-01-12 14:30:59 -080034
Romain Guy6c319ca2011-01-11 14:29:25 -080035 OpenGLRenderer::prepare(opaque);
36}
37
38void LayerRenderer::finish() {
39 OpenGLRenderer::finish();
40 glBindFramebuffer(GL_FRAMEBUFFER, mPreviousFbo);
Romain Guy1fc883b2011-01-12 14:30:59 -080041
Romain Guyf219da52011-01-16 12:54:25 -080042 generateMesh();
43
Romain Guyada830f2011-01-13 12:13:20 -080044 LAYER_RENDERER_LOGD("Finished rendering into layer, fbo = %d", mLayer->mFbo);
Romain Guy6c319ca2011-01-11 14:29:25 -080045}
46
47///////////////////////////////////////////////////////////////////////////////
Romain Guyf219da52011-01-16 12:54:25 -080048// Dirty region tracking
49///////////////////////////////////////////////////////////////////////////////
50
51bool LayerRenderer::hasLayer() {
52 return true;
53}
54
55Region* LayerRenderer::getRegion() {
56#if RENDER_LAYERS_AS_REGIONS
57 if (getSnapshot()->flags & Snapshot::kFlagFboTarget) {
58 return OpenGLRenderer::getRegion();
59 }
60 return &mLayer->region;
61#else
62 return OpenGLRenderer::getRegion();
63#endif
64}
65
66void LayerRenderer::generateMesh() {
67#if RENDER_LAYERS_AS_REGIONS
68 if (mLayer->region.isRect() || mLayer->region.isEmpty()) {
69 if (mLayer->mesh) {
70 delete mLayer->mesh;
71 delete mLayer->meshIndices;
72
73 mLayer->mesh = NULL;
74 mLayer->meshIndices = NULL;
75 mLayer->meshElementCount = 0;
76 }
Romain Guyfb13abd2011-01-16 15:16:38 -080077 mLayer->region.clear();
Romain Guyf219da52011-01-16 12:54:25 -080078 return;
79 }
80
81 size_t count;
82 const android::Rect* rects = mLayer->region.getArray(&count);
83
84 GLsizei elementCount = count * 6;
85
86 if (mLayer->mesh && mLayer->meshElementCount < elementCount) {
87 delete mLayer->mesh;
88 delete mLayer->meshIndices;
89
90 mLayer->mesh = NULL;
91 mLayer->meshIndices = NULL;
92 }
93
94 if (!mLayer->mesh) {
95 mLayer->mesh = new TextureVertex[count * 4];
96 mLayer->meshIndices = new uint16_t[elementCount];
97 mLayer->meshElementCount = elementCount;
98 }
99
100 const float texX = 1.0f / float(mLayer->width);
101 const float texY = 1.0f / float(mLayer->height);
102 const float height = mLayer->layer.getHeight();
103
104 TextureVertex* mesh = mLayer->mesh;
105 uint16_t* indices = mLayer->meshIndices;
106
107 for (size_t i = 0; i < count; i++) {
108 const android::Rect* r = &rects[i];
109
110 const float u1 = r->left * texX;
111 const float v1 = (height - r->top) * texY;
112 const float u2 = r->right * texX;
113 const float v2 = (height - r->bottom) * texY;
114
115 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
116 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
117 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
118 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
119
120 uint16_t quad = i * 4;
121 int index = i * 6;
122 indices[index ] = quad; // top-left
123 indices[index + 1] = quad + 1; // top-right
124 indices[index + 2] = quad + 2; // bottom-left
125 indices[index + 3] = quad + 2; // bottom-left
126 indices[index + 4] = quad + 1; // top-right
127 indices[index + 5] = quad + 3; // bottom-right
128 }
Romain Guyfb13abd2011-01-16 15:16:38 -0800129
130 mLayer->region.clear();
Romain Guyf219da52011-01-16 12:54:25 -0800131#endif
132}
133
134///////////////////////////////////////////////////////////////////////////////
135// Layers management
Romain Guy6c319ca2011-01-11 14:29:25 -0800136///////////////////////////////////////////////////////////////////////////////
137
Romain Guyada830f2011-01-13 12:13:20 -0800138Layer* LayerRenderer::createLayer(uint32_t width, uint32_t height, bool isOpaque) {
Romain Guy1fc883b2011-01-12 14:30:59 -0800139 LAYER_RENDERER_LOGD("Creating new layer %dx%d", width, height);
140
Romain Guyada830f2011-01-13 12:13:20 -0800141 Layer* layer = new Layer(width, height);
142
Romain Guy6c319ca2011-01-11 14:29:25 -0800143 GLuint previousFbo;
144 glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*) &previousFbo);
145
Romain Guyada830f2011-01-13 12:13:20 -0800146 glGenFramebuffers(1, &layer->fbo);
147 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
Romain Guy6c319ca2011-01-11 14:29:25 -0800148
Romain Guy1fc883b2011-01-12 14:30:59 -0800149 if (glGetError() != GL_NO_ERROR) {
150 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guyada830f2011-01-13 12:13:20 -0800151 glDeleteBuffers(1, &layer->fbo);
Romain Guy1fc883b2011-01-12 14:30:59 -0800152 return 0;
153 }
154
Romain Guy6c319ca2011-01-11 14:29:25 -0800155 glActiveTexture(GL_TEXTURE0);
Romain Guyada830f2011-01-13 12:13:20 -0800156 glGenTextures(1, &layer->texture);
157 glBindTexture(GL_TEXTURE_2D, layer->texture);
Romain Guy6c319ca2011-01-11 14:29:25 -0800158
159 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
160
161 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
162 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
163
164 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
165 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
166
167 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
168 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
169
170 if (glGetError() != GL_NO_ERROR) {
Romain Guy1fc883b2011-01-12 14:30:59 -0800171 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guyada830f2011-01-13 12:13:20 -0800172 glDeleteBuffers(1, &layer->fbo);
173 glDeleteTextures(1, &layer->texture);
174 delete layer;
Romain Guy6c319ca2011-01-11 14:29:25 -0800175 return 0;
176 }
177
178 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guyada830f2011-01-13 12:13:20 -0800179 layer->texture, 0);
Romain Guy6c319ca2011-01-11 14:29:25 -0800180
181 if (glGetError() != GL_NO_ERROR) {
Romain Guy1fc883b2011-01-12 14:30:59 -0800182 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guyada830f2011-01-13 12:13:20 -0800183 glDeleteBuffers(1, &layer->fbo);
184 glDeleteTextures(1, &layer->texture);
185 delete layer;
Romain Guy6c319ca2011-01-11 14:29:25 -0800186 return 0;
187 }
188
189 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
190
Romain Guyada830f2011-01-13 12:13:20 -0800191 layer->layer.set(0.0f, 0.0f, width, height);
192 layer->texCoords.set(0.0f, 1.0f, 1.0f, 0.0f);
193 layer->alpha = 255;
194 layer->mode = SkXfermode::kSrcOver_Mode;
195 layer->blend = !isOpaque;
196 layer->empty = false;
197 layer->colorFilter = NULL;
Romain Guy6c319ca2011-01-11 14:29:25 -0800198
Romain Guyada830f2011-01-13 12:13:20 -0800199 return layer;
Romain Guy6c319ca2011-01-11 14:29:25 -0800200}
201
Romain Guyada830f2011-01-13 12:13:20 -0800202bool LayerRenderer::resizeLayer(Layer* layer, uint32_t width, uint32_t height) {
203 if (layer) {
204 LAYER_RENDERER_LOGD("Resizing layer fbo = %d to %dx%d", layer->fbo, width, height);
Romain Guy1fc883b2011-01-12 14:30:59 -0800205
Romain Guyada830f2011-01-13 12:13:20 -0800206 glActiveTexture(GL_TEXTURE0);
207 glBindTexture(GL_TEXTURE_2D, layer->texture);
Romain Guy6c319ca2011-01-11 14:29:25 -0800208
Romain Guyada830f2011-01-13 12:13:20 -0800209 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
210 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
Romain Guy6c319ca2011-01-11 14:29:25 -0800211
Romain Guyada830f2011-01-13 12:13:20 -0800212 if (glGetError() != GL_NO_ERROR) {
213 glDeleteBuffers(1, &layer->fbo);
214 glDeleteTextures(1, &layer->texture);
Romain Guy6c319ca2011-01-11 14:29:25 -0800215
Romain Guyada830f2011-01-13 12:13:20 -0800216 layer->width = 0;
217 layer->height = 0;
218 layer->fbo = 0;
219 layer->texture = 0;
Romain Guy6c319ca2011-01-11 14:29:25 -0800220
Romain Guyada830f2011-01-13 12:13:20 -0800221 return false;
222 }
223
224 layer->width = width;
225 layer->height = height;
Romain Guy6c319ca2011-01-11 14:29:25 -0800226 }
Romain Guyada830f2011-01-13 12:13:20 -0800227 return true;
Romain Guy6c319ca2011-01-11 14:29:25 -0800228}
229
Romain Guyada830f2011-01-13 12:13:20 -0800230void LayerRenderer::destroyLayer(Layer* layer) {
231 if (layer) {
232 LAYER_RENDERER_LOGD("Destroying layer, fbo = %d", layer->fbo);
Romain Guy1fc883b2011-01-12 14:30:59 -0800233
Romain Guyada830f2011-01-13 12:13:20 -0800234 if (layer->fbo) glDeleteFramebuffers(1, &layer->fbo);
235 if (layer->texture) glDeleteTextures(1, &layer->texture);
236
237 delete layer;
238 }
Romain Guy6c319ca2011-01-11 14:29:25 -0800239}
240
Romain Guyada830f2011-01-13 12:13:20 -0800241void LayerRenderer::destroyLayerDeferred(Layer* layer) {
242 if (layer) {
243 LAYER_RENDERER_LOGD("Deferring layer destruction, fbo = %d", layer->fbo);
Romain Guy1fc883b2011-01-12 14:30:59 -0800244
Romain Guyada830f2011-01-13 12:13:20 -0800245 Caches::getInstance().deleteLayerDeferred(layer);
246 }
Romain Guy57066eb2011-01-12 12:53:32 -0800247}
248
Romain Guy6c319ca2011-01-11 14:29:25 -0800249}; // namespace uirenderer
250}; // namespace android