blob: 1c89577207c6032807399dfe2794799ba3e9df40 [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 Guyc88e3572011-01-22 00:32:12 -080032#if RENDER_LAYERS_AS_REGIONS
33 mLayer->region.clear();
34#endif
35
Romain Guyada830f2011-01-13 12:13:20 -080036 glBindFramebuffer(GL_FRAMEBUFFER, mLayer->fbo);
Romain Guy1fc883b2011-01-12 14:30:59 -080037
Romain Guy6c319ca2011-01-11 14:29:25 -080038 OpenGLRenderer::prepare(opaque);
39}
40
41void LayerRenderer::finish() {
42 OpenGLRenderer::finish();
Romain Guy1fc883b2011-01-12 14:30:59 -080043
Romain Guyf219da52011-01-16 12:54:25 -080044 generateMesh();
45
Romain Guyada830f2011-01-13 12:13:20 -080046 LAYER_RENDERER_LOGD("Finished rendering into layer, fbo = %d", mLayer->mFbo);
Romain Guy42f3a4b2011-01-19 13:42:26 -080047
48 // No need to unbind our FBO, this will be taken care of by the caller
49 // who will invoke OpenGLRenderer::resume()
50}
51
52GLint LayerRenderer::getTargetFbo() {
53 return mLayer->fbo;
Romain Guy6c319ca2011-01-11 14:29:25 -080054}
55
56///////////////////////////////////////////////////////////////////////////////
Romain Guyf219da52011-01-16 12:54:25 -080057// Dirty region tracking
58///////////////////////////////////////////////////////////////////////////////
59
60bool LayerRenderer::hasLayer() {
61 return true;
62}
63
64Region* LayerRenderer::getRegion() {
65#if RENDER_LAYERS_AS_REGIONS
66 if (getSnapshot()->flags & Snapshot::kFlagFboTarget) {
67 return OpenGLRenderer::getRegion();
68 }
69 return &mLayer->region;
70#else
71 return OpenGLRenderer::getRegion();
72#endif
73}
74
75void LayerRenderer::generateMesh() {
76#if RENDER_LAYERS_AS_REGIONS
77 if (mLayer->region.isRect() || mLayer->region.isEmpty()) {
78 if (mLayer->mesh) {
79 delete mLayer->mesh;
80 delete mLayer->meshIndices;
81
82 mLayer->mesh = NULL;
83 mLayer->meshIndices = NULL;
84 mLayer->meshElementCount = 0;
85 }
86 return;
87 }
88
89 size_t count;
90 const android::Rect* rects = mLayer->region.getArray(&count);
91
92 GLsizei elementCount = count * 6;
93
94 if (mLayer->mesh && mLayer->meshElementCount < elementCount) {
95 delete mLayer->mesh;
96 delete mLayer->meshIndices;
97
98 mLayer->mesh = NULL;
99 mLayer->meshIndices = NULL;
100 }
101
102 if (!mLayer->mesh) {
103 mLayer->mesh = new TextureVertex[count * 4];
104 mLayer->meshIndices = new uint16_t[elementCount];
105 mLayer->meshElementCount = elementCount;
106 }
107
108 const float texX = 1.0f / float(mLayer->width);
109 const float texY = 1.0f / float(mLayer->height);
110 const float height = mLayer->layer.getHeight();
111
112 TextureVertex* mesh = mLayer->mesh;
113 uint16_t* indices = mLayer->meshIndices;
114
115 for (size_t i = 0; i < count; i++) {
116 const android::Rect* r = &rects[i];
117
118 const float u1 = r->left * texX;
119 const float v1 = (height - r->top) * texY;
120 const float u2 = r->right * texX;
121 const float v2 = (height - r->bottom) * texY;
122
123 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
124 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
125 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
126 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
127
128 uint16_t quad = i * 4;
129 int index = i * 6;
130 indices[index ] = quad; // top-left
131 indices[index + 1] = quad + 1; // top-right
132 indices[index + 2] = quad + 2; // bottom-left
133 indices[index + 3] = quad + 2; // bottom-left
134 indices[index + 4] = quad + 1; // top-right
135 indices[index + 5] = quad + 3; // bottom-right
136 }
137#endif
138}
139
140///////////////////////////////////////////////////////////////////////////////
141// Layers management
Romain Guy6c319ca2011-01-11 14:29:25 -0800142///////////////////////////////////////////////////////////////////////////////
143
Romain Guyada830f2011-01-13 12:13:20 -0800144Layer* LayerRenderer::createLayer(uint32_t width, uint32_t height, bool isOpaque) {
Romain Guy1fc883b2011-01-12 14:30:59 -0800145 LAYER_RENDERER_LOGD("Creating new layer %dx%d", width, height);
146
Romain Guyada830f2011-01-13 12:13:20 -0800147 Layer* layer = new Layer(width, height);
148
Romain Guy6c319ca2011-01-11 14:29:25 -0800149 GLuint previousFbo;
150 glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*) &previousFbo);
151
Romain Guyada830f2011-01-13 12:13:20 -0800152 glGenFramebuffers(1, &layer->fbo);
153 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
Romain Guy6c319ca2011-01-11 14:29:25 -0800154
Romain Guy1fc883b2011-01-12 14:30:59 -0800155 if (glGetError() != GL_NO_ERROR) {
156 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guyada830f2011-01-13 12:13:20 -0800157 glDeleteBuffers(1, &layer->fbo);
Romain Guy1fc883b2011-01-12 14:30:59 -0800158 return 0;
159 }
160
Romain Guy6c319ca2011-01-11 14:29:25 -0800161 glActiveTexture(GL_TEXTURE0);
Romain Guyada830f2011-01-13 12:13:20 -0800162 glGenTextures(1, &layer->texture);
163 glBindTexture(GL_TEXTURE_2D, layer->texture);
Romain Guy6c319ca2011-01-11 14:29:25 -0800164
165 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
166
167 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
168 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
169
170 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
171 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
172
173 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
174 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
175
176 if (glGetError() != GL_NO_ERROR) {
Romain Guy1fc883b2011-01-12 14:30:59 -0800177 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guyada830f2011-01-13 12:13:20 -0800178 glDeleteBuffers(1, &layer->fbo);
179 glDeleteTextures(1, &layer->texture);
180 delete layer;
Romain Guy6c319ca2011-01-11 14:29:25 -0800181 return 0;
182 }
183
184 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guyada830f2011-01-13 12:13:20 -0800185 layer->texture, 0);
Romain Guy6c319ca2011-01-11 14:29:25 -0800186
187 if (glGetError() != GL_NO_ERROR) {
Romain Guy1fc883b2011-01-12 14:30:59 -0800188 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guyada830f2011-01-13 12:13:20 -0800189 glDeleteBuffers(1, &layer->fbo);
190 glDeleteTextures(1, &layer->texture);
191 delete layer;
Romain Guy6c319ca2011-01-11 14:29:25 -0800192 return 0;
193 }
194
195 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
196
Romain Guyada830f2011-01-13 12:13:20 -0800197 layer->layer.set(0.0f, 0.0f, width, height);
198 layer->texCoords.set(0.0f, 1.0f, 1.0f, 0.0f);
199 layer->alpha = 255;
200 layer->mode = SkXfermode::kSrcOver_Mode;
201 layer->blend = !isOpaque;
202 layer->empty = false;
203 layer->colorFilter = NULL;
Romain Guy6c319ca2011-01-11 14:29:25 -0800204
Romain Guyada830f2011-01-13 12:13:20 -0800205 return layer;
Romain Guy6c319ca2011-01-11 14:29:25 -0800206}
207
Romain Guyada830f2011-01-13 12:13:20 -0800208bool LayerRenderer::resizeLayer(Layer* layer, uint32_t width, uint32_t height) {
209 if (layer) {
210 LAYER_RENDERER_LOGD("Resizing layer fbo = %d to %dx%d", layer->fbo, width, height);
Romain Guy1fc883b2011-01-12 14:30:59 -0800211
Romain Guyada830f2011-01-13 12:13:20 -0800212 glActiveTexture(GL_TEXTURE0);
213 glBindTexture(GL_TEXTURE_2D, layer->texture);
Romain Guy6c319ca2011-01-11 14:29:25 -0800214
Romain Guyada830f2011-01-13 12:13:20 -0800215 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
216 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
Romain Guy6c319ca2011-01-11 14:29:25 -0800217
Romain Guyada830f2011-01-13 12:13:20 -0800218 if (glGetError() != GL_NO_ERROR) {
219 glDeleteBuffers(1, &layer->fbo);
220 glDeleteTextures(1, &layer->texture);
Romain Guy6c319ca2011-01-11 14:29:25 -0800221
Romain Guyada830f2011-01-13 12:13:20 -0800222 layer->width = 0;
223 layer->height = 0;
224 layer->fbo = 0;
225 layer->texture = 0;
Romain Guy6c319ca2011-01-11 14:29:25 -0800226
Romain Guyada830f2011-01-13 12:13:20 -0800227 return false;
228 }
229
230 layer->width = width;
231 layer->height = height;
Romain Guy6c319ca2011-01-11 14:29:25 -0800232 }
Romain Guyada830f2011-01-13 12:13:20 -0800233 return true;
Romain Guy6c319ca2011-01-11 14:29:25 -0800234}
235
Romain Guyada830f2011-01-13 12:13:20 -0800236void LayerRenderer::destroyLayer(Layer* layer) {
237 if (layer) {
238 LAYER_RENDERER_LOGD("Destroying layer, fbo = %d", layer->fbo);
Romain Guy1fc883b2011-01-12 14:30:59 -0800239
Romain Guyada830f2011-01-13 12:13:20 -0800240 if (layer->fbo) glDeleteFramebuffers(1, &layer->fbo);
241 if (layer->texture) glDeleteTextures(1, &layer->texture);
242
243 delete layer;
244 }
Romain Guy6c319ca2011-01-11 14:29:25 -0800245}
246
Romain Guyada830f2011-01-13 12:13:20 -0800247void LayerRenderer::destroyLayerDeferred(Layer* layer) {
248 if (layer) {
249 LAYER_RENDERER_LOGD("Deferring layer destruction, fbo = %d", layer->fbo);
Romain Guy1fc883b2011-01-12 14:30:59 -0800250
Romain Guyada830f2011-01-13 12:13:20 -0800251 Caches::getInstance().deleteLayerDeferred(layer);
252 }
Romain Guy57066eb2011-01-12 12:53:32 -0800253}
254
Romain Guy6c319ca2011-01-11 14:29:25 -0800255}; // namespace uirenderer
256}; // namespace android