Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 1 | /* |
| 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 Guy | 1fc883b | 2011-01-12 14:30:59 -0800 | [diff] [blame] | 20 | #include "Properties.h" |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 21 | |
| 22 | namespace android { |
| 23 | namespace uirenderer { |
| 24 | |
| 25 | /////////////////////////////////////////////////////////////////////////////// |
| 26 | // Rendering |
| 27 | /////////////////////////////////////////////////////////////////////////////// |
| 28 | |
| 29 | void LayerRenderer::prepare(bool opaque) { |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame^] | 30 | LAYER_RENDERER_LOGD("Rendering into layer, fbo = %d", mLayer->fbo); |
Romain Guy | 1fc883b | 2011-01-12 14:30:59 -0800 | [diff] [blame] | 31 | |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 32 | glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*) &mPreviousFbo); |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame^] | 33 | glBindFramebuffer(GL_FRAMEBUFFER, mLayer->fbo); |
Romain Guy | 1fc883b | 2011-01-12 14:30:59 -0800 | [diff] [blame] | 34 | |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 35 | OpenGLRenderer::prepare(opaque); |
| 36 | } |
| 37 | |
| 38 | void LayerRenderer::finish() { |
| 39 | OpenGLRenderer::finish(); |
| 40 | glBindFramebuffer(GL_FRAMEBUFFER, mPreviousFbo); |
Romain Guy | 1fc883b | 2011-01-12 14:30:59 -0800 | [diff] [blame] | 41 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame^] | 42 | LAYER_RENDERER_LOGD("Finished rendering into layer, fbo = %d", mLayer->mFbo); |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | /////////////////////////////////////////////////////////////////////////////// |
| 46 | // Static functions |
| 47 | /////////////////////////////////////////////////////////////////////////////// |
| 48 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame^] | 49 | Layer* LayerRenderer::createLayer(uint32_t width, uint32_t height, bool isOpaque) { |
Romain Guy | 1fc883b | 2011-01-12 14:30:59 -0800 | [diff] [blame] | 50 | LAYER_RENDERER_LOGD("Creating new layer %dx%d", width, height); |
| 51 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame^] | 52 | Layer* layer = new Layer(width, height); |
| 53 | |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 54 | GLuint previousFbo; |
| 55 | glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*) &previousFbo); |
| 56 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame^] | 57 | glGenFramebuffers(1, &layer->fbo); |
| 58 | glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo); |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 59 | |
Romain Guy | 1fc883b | 2011-01-12 14:30:59 -0800 | [diff] [blame] | 60 | if (glGetError() != GL_NO_ERROR) { |
| 61 | glBindFramebuffer(GL_FRAMEBUFFER, previousFbo); |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame^] | 62 | glDeleteBuffers(1, &layer->fbo); |
Romain Guy | 1fc883b | 2011-01-12 14:30:59 -0800 | [diff] [blame] | 63 | return 0; |
| 64 | } |
| 65 | |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 66 | glActiveTexture(GL_TEXTURE0); |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame^] | 67 | glGenTextures(1, &layer->texture); |
| 68 | glBindTexture(GL_TEXTURE_2D, layer->texture); |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 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 Guy | 1fc883b | 2011-01-12 14:30:59 -0800 | [diff] [blame] | 82 | glBindFramebuffer(GL_FRAMEBUFFER, previousFbo); |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame^] | 83 | glDeleteBuffers(1, &layer->fbo); |
| 84 | glDeleteTextures(1, &layer->texture); |
| 85 | delete layer; |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame^] | 90 | layer->texture, 0); |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 91 | |
| 92 | if (glGetError() != GL_NO_ERROR) { |
Romain Guy | 1fc883b | 2011-01-12 14:30:59 -0800 | [diff] [blame] | 93 | glBindFramebuffer(GL_FRAMEBUFFER, previousFbo); |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame^] | 94 | glDeleteBuffers(1, &layer->fbo); |
| 95 | glDeleteTextures(1, &layer->texture); |
| 96 | delete layer; |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | glBindFramebuffer(GL_FRAMEBUFFER, previousFbo); |
| 101 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame^] | 102 | layer->layer.set(0.0f, 0.0f, width, height); |
| 103 | layer->texCoords.set(0.0f, 1.0f, 1.0f, 0.0f); |
| 104 | layer->alpha = 255; |
| 105 | layer->mode = SkXfermode::kSrcOver_Mode; |
| 106 | layer->blend = !isOpaque; |
| 107 | layer->empty = false; |
| 108 | layer->colorFilter = NULL; |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 109 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame^] | 110 | return layer; |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame^] | 113 | bool LayerRenderer::resizeLayer(Layer* layer, uint32_t width, uint32_t height) { |
| 114 | if (layer) { |
| 115 | LAYER_RENDERER_LOGD("Resizing layer fbo = %d to %dx%d", layer->fbo, width, height); |
Romain Guy | 1fc883b | 2011-01-12 14:30:59 -0800 | [diff] [blame] | 116 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame^] | 117 | glActiveTexture(GL_TEXTURE0); |
| 118 | glBindTexture(GL_TEXTURE_2D, layer->texture); |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 119 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame^] | 120 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, |
| 121 | GL_RGBA, GL_UNSIGNED_BYTE, NULL); |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 122 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame^] | 123 | if (glGetError() != GL_NO_ERROR) { |
| 124 | glDeleteBuffers(1, &layer->fbo); |
| 125 | glDeleteTextures(1, &layer->texture); |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 126 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame^] | 127 | layer->width = 0; |
| 128 | layer->height = 0; |
| 129 | layer->fbo = 0; |
| 130 | layer->texture = 0; |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 131 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame^] | 132 | return false; |
| 133 | } |
| 134 | |
| 135 | layer->width = width; |
| 136 | layer->height = height; |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 137 | } |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame^] | 138 | return true; |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame^] | 141 | void LayerRenderer::destroyLayer(Layer* layer) { |
| 142 | if (layer) { |
| 143 | LAYER_RENDERER_LOGD("Destroying layer, fbo = %d", layer->fbo); |
Romain Guy | 1fc883b | 2011-01-12 14:30:59 -0800 | [diff] [blame] | 144 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame^] | 145 | if (layer->fbo) glDeleteFramebuffers(1, &layer->fbo); |
| 146 | if (layer->texture) glDeleteTextures(1, &layer->texture); |
| 147 | |
| 148 | delete layer; |
| 149 | } |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 150 | } |
| 151 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame^] | 152 | void LayerRenderer::destroyLayerDeferred(Layer* layer) { |
| 153 | if (layer) { |
| 154 | LAYER_RENDERER_LOGD("Deferring layer destruction, fbo = %d", layer->fbo); |
Romain Guy | 1fc883b | 2011-01-12 14:30:59 -0800 | [diff] [blame] | 155 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame^] | 156 | Caches::getInstance().deleteLayerDeferred(layer); |
| 157 | } |
Romain Guy | 57066eb | 2011-01-12 12:53:32 -0800 | [diff] [blame] | 158 | } |
| 159 | |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 160 | }; // namespace uirenderer |
| 161 | }; // namespace android |