Chet Haase | d15ebf2 | 2012-09-05 11:40:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 <utils/Log.h> |
| 20 | |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 21 | #include "DisplayList.h" |
| 22 | #include "DeferredDisplayList.h" |
Chet Haase | d15ebf2 | 2012-09-05 11:40:29 -0700 | [diff] [blame] | 23 | #include "Layer.h" |
Chet Haase | 98d3a64 | 2012-09-26 10:27:40 -0700 | [diff] [blame] | 24 | #include "LayerRenderer.h" |
Chet Haase | d15ebf2 | 2012-09-05 11:40:29 -0700 | [diff] [blame] | 25 | #include "OpenGLRenderer.h" |
| 26 | #include "Caches.h" |
| 27 | |
| 28 | namespace android { |
| 29 | namespace uirenderer { |
| 30 | |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 31 | Layer::Layer(const uint32_t layerWidth, const uint32_t layerHeight): |
| 32 | caches(Caches::getInstance()), texture(caches) { |
Chet Haase | 603f6de | 2012-09-14 15:31:25 -0700 | [diff] [blame] | 33 | mesh = NULL; |
Chet Haase | 603f6de | 2012-09-14 15:31:25 -0700 | [diff] [blame] | 34 | meshElementCount = 0; |
| 35 | cacheable = true; |
Romain Guy | 7c25aab | 2012-10-18 15:05:02 -0700 | [diff] [blame] | 36 | dirty = false; |
Chet Haase | 603f6de | 2012-09-14 15:31:25 -0700 | [diff] [blame] | 37 | textureLayer = false; |
| 38 | renderTarget = GL_TEXTURE_2D; |
| 39 | texture.width = layerWidth; |
| 40 | texture.height = layerHeight; |
| 41 | colorFilter = NULL; |
| 42 | deferredUpdateScheduled = false; |
| 43 | renderer = NULL; |
| 44 | displayList = NULL; |
| 45 | fbo = 0; |
Romain Guy | 3bbacf2 | 2013-02-06 16:51:04 -0800 | [diff] [blame] | 46 | stencil = NULL; |
Romain Guy | 5bb3c73 | 2012-11-29 17:52:58 -0800 | [diff] [blame] | 47 | debugDrawUpdate = false; |
Chris Craik | 34416ea | 2013-04-15 16:08:28 -0700 | [diff] [blame] | 48 | hasDrawnSinceUpdate = false; |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 49 | deferredList = NULL; |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 50 | caches.resourceCache.incrementRefcount(this); |
Chet Haase | 603f6de | 2012-09-14 15:31:25 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Chet Haase | d15ebf2 | 2012-09-05 11:40:29 -0700 | [diff] [blame] | 53 | Layer::~Layer() { |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 54 | if (colorFilter) caches.resourceCache.decrementRefcount(colorFilter); |
Chet Haase | 98d3a64 | 2012-09-26 10:27:40 -0700 | [diff] [blame] | 55 | removeFbo(); |
Romain Guy | 8a13749 | 2012-09-25 15:49:03 -0700 | [diff] [blame] | 56 | deleteTexture(); |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 57 | |
| 58 | delete[] mesh; |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 59 | delete deferredList; |
Romain Guy | 97dc917 | 2012-09-23 17:46:45 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Romain Guy | 2055aba | 2013-01-18 16:42:51 -0800 | [diff] [blame] | 62 | uint32_t Layer::computeIdealWidth(uint32_t layerWidth) { |
| 63 | return uint32_t(ceilf(layerWidth / float(LAYER_SIZE)) * LAYER_SIZE); |
| 64 | } |
| 65 | |
| 66 | uint32_t Layer::computeIdealHeight(uint32_t layerHeight) { |
| 67 | return uint32_t(ceilf(layerHeight / float(LAYER_SIZE)) * LAYER_SIZE); |
| 68 | } |
| 69 | |
| 70 | bool Layer::resize(const uint32_t width, const uint32_t height) { |
| 71 | uint32_t desiredWidth = computeIdealWidth(width); |
| 72 | uint32_t desiredHeight = computeIdealWidth(height); |
| 73 | |
| 74 | if (desiredWidth <= getWidth() && desiredHeight <= getHeight()) { |
| 75 | return true; |
| 76 | } |
| 77 | |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 78 | const uint32_t maxTextureSize = caches.maxTextureSize; |
Romain Guy | ce4a7df | 2013-03-28 11:32:33 -0700 | [diff] [blame] | 79 | if (desiredWidth > maxTextureSize || desiredHeight > maxTextureSize) { |
| 80 | ALOGW("Layer exceeds max. dimensions supported by the GPU (%dx%d, max=%dx%d)", |
| 81 | desiredWidth, desiredHeight, maxTextureSize, maxTextureSize); |
| 82 | return false; |
| 83 | } |
| 84 | |
Romain Guy | 2055aba | 2013-01-18 16:42:51 -0800 | [diff] [blame] | 85 | uint32_t oldWidth = getWidth(); |
| 86 | uint32_t oldHeight = getHeight(); |
| 87 | |
| 88 | setSize(desiredWidth, desiredHeight); |
| 89 | |
| 90 | if (fbo) { |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 91 | caches.activeTexture(0); |
Romain Guy | 2055aba | 2013-01-18 16:42:51 -0800 | [diff] [blame] | 92 | bindTexture(); |
Romain Guy | 0908764 | 2013-04-04 12:27:54 -0700 | [diff] [blame] | 93 | allocateTexture(); |
Romain Guy | 2055aba | 2013-01-18 16:42:51 -0800 | [diff] [blame] | 94 | |
| 95 | if (glGetError() != GL_NO_ERROR) { |
| 96 | setSize(oldWidth, oldHeight); |
| 97 | return false; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | if (stencil) { |
Romain Guy | 3bbacf2 | 2013-02-06 16:51:04 -0800 | [diff] [blame] | 102 | stencil->bind(); |
| 103 | stencil->resize(desiredWidth, desiredHeight); |
Romain Guy | 2055aba | 2013-01-18 16:42:51 -0800 | [diff] [blame] | 104 | |
| 105 | if (glGetError() != GL_NO_ERROR) { |
| 106 | setSize(oldWidth, oldHeight); |
| 107 | return false; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | return true; |
| 112 | } |
| 113 | |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 114 | void Layer::removeFbo(bool flush) { |
| 115 | if (stencil) { |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 116 | GLuint previousFbo; |
| 117 | glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*) &previousFbo); |
| 118 | if (fbo != previousFbo) glBindFramebuffer(GL_FRAMEBUFFER, fbo); |
| 119 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, 0); |
| 120 | if (fbo != previousFbo) glBindFramebuffer(GL_FRAMEBUFFER, previousFbo); |
| 121 | |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 122 | caches.renderBufferCache.put(stencil); |
Romain Guy | 3bbacf2 | 2013-02-06 16:51:04 -0800 | [diff] [blame] | 123 | stencil = NULL; |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Chet Haase | 98d3a64 | 2012-09-26 10:27:40 -0700 | [diff] [blame] | 126 | if (fbo) { |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 127 | if (flush) LayerRenderer::flushLayer(this); |
| 128 | // If put fails the cache will delete the FBO |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 129 | caches.fboCache.put(fbo); |
Chet Haase | 98d3a64 | 2012-09-26 10:27:40 -0700 | [diff] [blame] | 130 | fbo = 0; |
Dave Burke | 56257af | 2012-09-25 20:30:09 -0700 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
Chet Haase | d15ebf2 | 2012-09-05 11:40:29 -0700 | [diff] [blame] | 134 | void Layer::setPaint(SkPaint* paint) { |
| 135 | OpenGLRenderer::getAlphaAndModeDirect(paint, &alpha, &mode); |
| 136 | } |
| 137 | |
| 138 | void Layer::setColorFilter(SkiaColorFilter* filter) { |
| 139 | if (colorFilter) { |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 140 | caches.resourceCache.decrementRefcount(colorFilter); |
Chet Haase | d15ebf2 | 2012-09-05 11:40:29 -0700 | [diff] [blame] | 141 | } |
| 142 | colorFilter = filter; |
| 143 | if (colorFilter) { |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 144 | caches.resourceCache.incrementRefcount(colorFilter); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | void Layer::bindTexture() const { |
| 149 | if (texture.id) { |
| 150 | caches.bindTexture(renderTarget, texture.id); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | void Layer::bindStencilRenderBuffer() const { |
| 155 | if (stencil) { |
| 156 | stencil->bind(); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | void Layer::generateTexture() { |
| 161 | if (!texture.id) { |
| 162 | glGenTextures(1, &texture.id); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | void Layer::deleteTexture() { |
| 167 | if (texture.id) { |
Romain Guy | be1b127 | 2013-06-06 14:02:54 -0700 | [diff] [blame] | 168 | texture.deleteTexture(); |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 169 | texture.id = 0; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | void Layer::clearTexture() { |
| 174 | texture.id = 0; |
| 175 | } |
| 176 | |
| 177 | void Layer::allocateTexture() { |
| 178 | #if DEBUG_LAYERS |
| 179 | ALOGD(" Allocate layer: %dx%d", getWidth(), getHeight()); |
| 180 | #endif |
| 181 | if (texture.id) { |
| 182 | glPixelStorei(GL_UNPACK_ALIGNMENT, 4); |
| 183 | glTexImage2D(renderTarget, 0, GL_RGBA, getWidth(), getHeight(), 0, |
| 184 | GL_RGBA, GL_UNSIGNED_BYTE, NULL); |
Chet Haase | d15ebf2 | 2012-09-05 11:40:29 -0700 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 188 | void Layer::defer() { |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 189 | const float width = layer.getWidth(); |
| 190 | const float height = layer.getHeight(); |
| 191 | |
| 192 | if (dirtyRect.isEmpty() || (dirtyRect.left <= 0 && dirtyRect.top <= 0 && |
| 193 | dirtyRect.right >= width && dirtyRect.bottom >= height)) { |
| 194 | dirtyRect.set(0, 0, width, height); |
| 195 | } |
| 196 | |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 197 | if (deferredList) { |
| 198 | deferredList->reset(dirtyRect); |
| 199 | } else { |
| 200 | deferredList = new DeferredDisplayList(dirtyRect); |
| 201 | } |
| 202 | DeferStateStruct deferredState(*deferredList, *renderer, |
| 203 | DisplayList::kReplayFlag_ClipChildren); |
| 204 | |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 205 | renderer->initViewport(width, height); |
| 206 | renderer->setupFrameState(dirtyRect.left, dirtyRect.top, |
| 207 | dirtyRect.right, dirtyRect.bottom, !isBlend()); |
| 208 | |
| 209 | displayList->defer(deferredState, 0); |
Romain Guy | 02b49b7 | 2013-03-29 12:37:16 -0700 | [diff] [blame] | 210 | |
| 211 | deferredUpdateScheduled = false; |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Romain Guy | e93482f | 2013-06-17 13:14:51 -0700 | [diff] [blame] | 214 | void Layer::cancelDefer() { |
| 215 | renderer = NULL; |
| 216 | displayList = NULL; |
| 217 | deferredUpdateScheduled = false; |
| 218 | if (deferredList) { |
| 219 | delete deferredList; |
| 220 | deferredList = NULL; |
| 221 | } |
| 222 | } |
| 223 | |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 224 | void Layer::flush() { |
Chris Craik | 8c6e17c | 2013-06-17 13:02:12 -0700 | [diff] [blame] | 225 | // renderer is checked as layer may be destroyed/put in layer cache with flush scheduled |
| 226 | if (deferredList && renderer) { |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 227 | renderer->setViewport(layer.getWidth(), layer.getHeight()); |
| 228 | renderer->prepareDirty(dirtyRect.left, dirtyRect.top, dirtyRect.right, dirtyRect.bottom, |
| 229 | !isBlend()); |
| 230 | |
| 231 | deferredList->flush(*renderer, dirtyRect); |
| 232 | |
| 233 | renderer->finish(); |
| 234 | renderer = NULL; |
| 235 | |
| 236 | dirtyRect.setEmpty(); |
Chris Craik | 1206b9b | 2013-04-04 14:46:24 -0700 | [diff] [blame] | 237 | displayList = NULL; |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
Romain Guy | 02b49b7 | 2013-03-29 12:37:16 -0700 | [diff] [blame] | 241 | void Layer::render() { |
| 242 | renderer->setViewport(layer.getWidth(), layer.getHeight()); |
| 243 | renderer->prepareDirty(dirtyRect.left, dirtyRect.top, dirtyRect.right, dirtyRect.bottom, |
| 244 | !isBlend()); |
| 245 | |
| 246 | renderer->drawDisplayList(displayList, dirtyRect, DisplayList::kReplayFlag_ClipChildren); |
| 247 | |
| 248 | renderer->finish(); |
| 249 | renderer = NULL; |
| 250 | |
| 251 | dirtyRect.setEmpty(); |
| 252 | |
| 253 | deferredUpdateScheduled = false; |
| 254 | displayList = NULL; |
| 255 | } |
| 256 | |
Chet Haase | d15ebf2 | 2012-09-05 11:40:29 -0700 | [diff] [blame] | 257 | }; // namespace uirenderer |
| 258 | }; // namespace android |