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 | |
Chet Haase | 603f6de | 2012-09-14 15:31:25 -0700 | [diff] [blame] | 31 | Layer::Layer(const uint32_t layerWidth, const uint32_t layerHeight) { |
| 32 | mesh = NULL; |
| 33 | meshIndices = NULL; |
| 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; |
Chet Haase | 603f6de | 2012-09-14 15:31:25 -0700 | [diff] [blame] | 50 | Caches::getInstance().resourceCache.incrementRefcount(this); |
| 51 | } |
| 52 | |
Chet Haase | d15ebf2 | 2012-09-05 11:40:29 -0700 | [diff] [blame] | 53 | Layer::~Layer() { |
Chet Haase | d15ebf2 | 2012-09-05 11:40:29 -0700 | [diff] [blame] | 54 | if (colorFilter) Caches::getInstance().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; |
| 59 | delete[] meshIndices; |
| 60 | delete deferredList; |
Romain Guy | 97dc917 | 2012-09-23 17:46:45 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Romain Guy | 2055aba | 2013-01-18 16:42:51 -0800 | [diff] [blame] | 63 | uint32_t Layer::computeIdealWidth(uint32_t layerWidth) { |
| 64 | return uint32_t(ceilf(layerWidth / float(LAYER_SIZE)) * LAYER_SIZE); |
| 65 | } |
| 66 | |
| 67 | uint32_t Layer::computeIdealHeight(uint32_t layerHeight) { |
| 68 | return uint32_t(ceilf(layerHeight / float(LAYER_SIZE)) * LAYER_SIZE); |
| 69 | } |
| 70 | |
| 71 | bool Layer::resize(const uint32_t width, const uint32_t height) { |
| 72 | uint32_t desiredWidth = computeIdealWidth(width); |
| 73 | uint32_t desiredHeight = computeIdealWidth(height); |
| 74 | |
| 75 | if (desiredWidth <= getWidth() && desiredHeight <= getHeight()) { |
| 76 | return true; |
| 77 | } |
| 78 | |
Romain Guy | ce4a7df | 2013-03-28 11:32:33 -0700 | [diff] [blame] | 79 | const uint32_t maxTextureSize = Caches::getInstance().maxTextureSize; |
| 80 | if (desiredWidth > maxTextureSize || desiredHeight > maxTextureSize) { |
| 81 | ALOGW("Layer exceeds max. dimensions supported by the GPU (%dx%d, max=%dx%d)", |
| 82 | desiredWidth, desiredHeight, maxTextureSize, maxTextureSize); |
| 83 | return false; |
| 84 | } |
| 85 | |
Romain Guy | 2055aba | 2013-01-18 16:42:51 -0800 | [diff] [blame] | 86 | uint32_t oldWidth = getWidth(); |
| 87 | uint32_t oldHeight = getHeight(); |
| 88 | |
| 89 | setSize(desiredWidth, desiredHeight); |
| 90 | |
| 91 | if (fbo) { |
| 92 | Caches::getInstance().activeTexture(0); |
| 93 | bindTexture(); |
Romain Guy | 0908764 | 2013-04-04 12:27:54 -0700 | [diff] [blame] | 94 | allocateTexture(); |
Romain Guy | 2055aba | 2013-01-18 16:42:51 -0800 | [diff] [blame] | 95 | |
| 96 | if (glGetError() != GL_NO_ERROR) { |
| 97 | setSize(oldWidth, oldHeight); |
| 98 | return false; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | if (stencil) { |
Romain Guy | 3bbacf2 | 2013-02-06 16:51:04 -0800 | [diff] [blame] | 103 | stencil->bind(); |
| 104 | stencil->resize(desiredWidth, desiredHeight); |
Romain Guy | 2055aba | 2013-01-18 16:42:51 -0800 | [diff] [blame] | 105 | |
| 106 | if (glGetError() != GL_NO_ERROR) { |
| 107 | setSize(oldWidth, oldHeight); |
| 108 | return false; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | return true; |
| 113 | } |
| 114 | |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 115 | void Layer::removeFbo(bool flush) { |
| 116 | if (stencil) { |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 117 | GLuint previousFbo; |
| 118 | glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*) &previousFbo); |
| 119 | if (fbo != previousFbo) glBindFramebuffer(GL_FRAMEBUFFER, fbo); |
| 120 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, 0); |
| 121 | if (fbo != previousFbo) glBindFramebuffer(GL_FRAMEBUFFER, previousFbo); |
| 122 | |
Romain Guy | 8d4aeb7 | 2013-02-12 16:08:55 -0800 | [diff] [blame] | 123 | Caches::getInstance().renderBufferCache.put(stencil); |
Romain Guy | 3bbacf2 | 2013-02-06 16:51:04 -0800 | [diff] [blame] | 124 | stencil = NULL; |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 125 | } |
| 126 | |
Chet Haase | 98d3a64 | 2012-09-26 10:27:40 -0700 | [diff] [blame] | 127 | if (fbo) { |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 128 | if (flush) LayerRenderer::flushLayer(this); |
| 129 | // If put fails the cache will delete the FBO |
Chet Haase | 98d3a64 | 2012-09-26 10:27:40 -0700 | [diff] [blame] | 130 | Caches::getInstance().fboCache.put(fbo); |
| 131 | fbo = 0; |
Dave Burke | 56257af | 2012-09-25 20:30:09 -0700 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
Chet Haase | d15ebf2 | 2012-09-05 11:40:29 -0700 | [diff] [blame] | 135 | void Layer::setPaint(SkPaint* paint) { |
| 136 | OpenGLRenderer::getAlphaAndModeDirect(paint, &alpha, &mode); |
| 137 | } |
| 138 | |
| 139 | void Layer::setColorFilter(SkiaColorFilter* filter) { |
| 140 | if (colorFilter) { |
| 141 | Caches::getInstance().resourceCache.decrementRefcount(colorFilter); |
| 142 | } |
| 143 | colorFilter = filter; |
| 144 | if (colorFilter) { |
| 145 | Caches::getInstance().resourceCache.incrementRefcount(colorFilter); |
| 146 | } |
| 147 | } |
| 148 | |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 149 | void Layer::defer() { |
| 150 | if (!deferredList) { |
| 151 | deferredList = new DeferredDisplayList; |
| 152 | } |
| 153 | DeferStateStruct deferredState(*deferredList, *renderer, |
| 154 | DisplayList::kReplayFlag_ClipChildren); |
| 155 | |
| 156 | const float width = layer.getWidth(); |
| 157 | const float height = layer.getHeight(); |
| 158 | |
| 159 | if (dirtyRect.isEmpty() || (dirtyRect.left <= 0 && dirtyRect.top <= 0 && |
| 160 | dirtyRect.right >= width && dirtyRect.bottom >= height)) { |
| 161 | dirtyRect.set(0, 0, width, height); |
| 162 | } |
| 163 | |
| 164 | renderer->initViewport(width, height); |
| 165 | renderer->setupFrameState(dirtyRect.left, dirtyRect.top, |
| 166 | dirtyRect.right, dirtyRect.bottom, !isBlend()); |
| 167 | |
| 168 | displayList->defer(deferredState, 0); |
Romain Guy | 02b49b7 | 2013-03-29 12:37:16 -0700 | [diff] [blame] | 169 | |
| 170 | deferredUpdateScheduled = false; |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | void Layer::flush() { |
Romain Guy | 696dcf8 | 2013-03-28 13:06:58 -0700 | [diff] [blame] | 174 | if (deferredList) { |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 175 | renderer->setViewport(layer.getWidth(), layer.getHeight()); |
| 176 | renderer->prepareDirty(dirtyRect.left, dirtyRect.top, dirtyRect.right, dirtyRect.bottom, |
| 177 | !isBlend()); |
| 178 | |
| 179 | deferredList->flush(*renderer, dirtyRect); |
| 180 | |
| 181 | renderer->finish(); |
| 182 | renderer = NULL; |
| 183 | |
| 184 | dirtyRect.setEmpty(); |
Chris Craik | 1206b9b | 2013-04-04 14:46:24 -0700 | [diff] [blame] | 185 | displayList = NULL; |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
Romain Guy | 02b49b7 | 2013-03-29 12:37:16 -0700 | [diff] [blame] | 189 | void Layer::render() { |
| 190 | renderer->setViewport(layer.getWidth(), layer.getHeight()); |
| 191 | renderer->prepareDirty(dirtyRect.left, dirtyRect.top, dirtyRect.right, dirtyRect.bottom, |
| 192 | !isBlend()); |
| 193 | |
| 194 | renderer->drawDisplayList(displayList, dirtyRect, DisplayList::kReplayFlag_ClipChildren); |
| 195 | |
| 196 | renderer->finish(); |
| 197 | renderer = NULL; |
| 198 | |
| 199 | dirtyRect.setEmpty(); |
| 200 | |
| 201 | deferredUpdateScheduled = false; |
| 202 | displayList = NULL; |
| 203 | } |
| 204 | |
Chet Haase | d15ebf2 | 2012-09-05 11:40:29 -0700 | [diff] [blame] | 205 | }; // namespace uirenderer |
| 206 | }; // namespace android |