blob: b838764a54de5dbab4f929aade7c53af1f46cb54 [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 Guy1fc883b2011-01-12 14:30:59 -080030 LAYER_RENDERER_LOGD("Rendering into layer, fbo = %d", mFbo);
31
Romain Guy6c319ca2011-01-11 14:29:25 -080032 glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*) &mPreviousFbo);
33 glBindFramebuffer(GL_FRAMEBUFFER, mFbo);
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
42 LAYER_RENDERER_LOGD("Finished rendering into layer, fbo = %d", mFbo);
Romain Guy6c319ca2011-01-11 14:29:25 -080043}
44
45///////////////////////////////////////////////////////////////////////////////
46// Static functions
47///////////////////////////////////////////////////////////////////////////////
48
49GLuint LayerRenderer::createLayer(uint32_t width, uint32_t height,
50 uint32_t* layerWidth, uint32_t* layerHeight, GLuint* texture) {
Romain Guy1fc883b2011-01-12 14:30:59 -080051 LAYER_RENDERER_LOGD("Creating new layer %dx%d", width, height);
52
Romain Guy6c319ca2011-01-11 14:29:25 -080053 GLuint previousFbo;
54 glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*) &previousFbo);
55
56 GLuint fbo = 0;
57 glGenFramebuffers(1, &fbo);
58 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
59
Romain Guy1fc883b2011-01-12 14:30:59 -080060 if (glGetError() != GL_NO_ERROR) {
61 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
62 glDeleteBuffers(1, &fbo);
63 return 0;
64 }
65
Romain Guy6c319ca2011-01-11 14:29:25 -080066 glActiveTexture(GL_TEXTURE0);
67 glGenTextures(1, texture);
68 glBindTexture(GL_TEXTURE_2D, *texture);
69
70 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
71
72 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
73 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
74
75 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
76 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
77
78 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
79 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
80
81 if (glGetError() != GL_NO_ERROR) {
Romain Guy1fc883b2011-01-12 14:30:59 -080082 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guy6c319ca2011-01-11 14:29:25 -080083 glDeleteBuffers(1, &fbo);
84 glDeleteTextures(1, texture);
Romain Guy6c319ca2011-01-11 14:29:25 -080085 return 0;
86 }
87
88 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
89 *texture, 0);
90
91 if (glGetError() != GL_NO_ERROR) {
Romain Guy1fc883b2011-01-12 14:30:59 -080092 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guy6c319ca2011-01-11 14:29:25 -080093 glDeleteBuffers(1, &fbo);
94 glDeleteTextures(1, texture);
Romain Guy6c319ca2011-01-11 14:29:25 -080095 return 0;
96 }
97
98 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
99
100 *layerWidth = width;
101 *layerHeight = height;
102
103 return fbo;
104}
105
106void LayerRenderer::resizeLayer(GLuint fbo, GLuint texture, uint32_t width, uint32_t height,
107 uint32_t* layerWidth, uint32_t* layerHeight) {
Romain Guy1fc883b2011-01-12 14:30:59 -0800108 LAYER_RENDERER_LOGD("Resizing layer fbo = %d to %dx%d", fbo, width, height);
109
Romain Guy6c319ca2011-01-11 14:29:25 -0800110 glActiveTexture(GL_TEXTURE0);
111 glBindTexture(GL_TEXTURE_2D, texture);
112
113 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
114 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
115
116 if (glGetError() != GL_NO_ERROR) {
117 glDeleteBuffers(1, &fbo);
Romain Guya9d07112011-01-11 17:58:03 -0800118 glDeleteTextures(1, &texture);
Romain Guy6c319ca2011-01-11 14:29:25 -0800119
120 *layerWidth = 0;
121 *layerHeight = 0;
122
123 return;
124 }
125
126 *layerWidth = width;
127 *layerHeight = height;
128}
129
130void LayerRenderer::destroyLayer(GLuint fbo, GLuint texture) {
Romain Guy1fc883b2011-01-12 14:30:59 -0800131 LAYER_RENDERER_LOGD("Destroying layer, fbo = %d", fbo);
132
Romain Guy6c319ca2011-01-11 14:29:25 -0800133 if (fbo) glDeleteFramebuffers(1, &fbo);
134 if (texture) glDeleteTextures(1, &texture);
135}
136
Romain Guy57066eb2011-01-12 12:53:32 -0800137void LayerRenderer::destroyLayerDeferred(GLuint fbo, GLuint texture) {
Romain Guy1fc883b2011-01-12 14:30:59 -0800138 LAYER_RENDERER_LOGD("Deferring layer destruction, fbo = %d", fbo);
139
Romain Guy57066eb2011-01-12 12:53:32 -0800140 Caches& caches = Caches::getInstance();
141 if (fbo) caches.deleteFboDeferred(fbo);
142 if (texture) caches.deleteTextureDeferred(texture);
143}
144
Romain Guy6c319ca2011-01-11 14:29:25 -0800145}; // namespace uirenderer
146}; // namespace android