Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | #include "BakedOpRenderer.h" |
| 18 | |
| 19 | #include "Caches.h" |
| 20 | #include "Glop.h" |
| 21 | #include "GlopBuilder.h" |
| 22 | #include "renderstate/RenderState.h" |
| 23 | #include "utils/GLUtils.h" |
| 24 | |
| 25 | namespace android { |
| 26 | namespace uirenderer { |
| 27 | |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame^] | 28 | //////////////////////////////////////////////////////////////////////////////// |
| 29 | // OffscreenBuffer |
| 30 | //////////////////////////////////////////////////////////////////////////////// |
Chris Craik | 818c9fb | 2015-10-23 14:33:42 -0700 | [diff] [blame] | 31 | |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame^] | 32 | OffscreenBuffer::OffscreenBuffer(Caches& caches, uint32_t textureWidth, uint32_t textureHeight, |
| 33 | uint32_t viewportWidth, uint32_t viewportHeight) |
| 34 | : texture(caches) |
| 35 | , texCoords(0, viewportHeight / float(textureHeight), viewportWidth / float(textureWidth), 0) { |
| 36 | texture.width = textureWidth; |
| 37 | texture.height = textureHeight; |
| 38 | |
| 39 | caches.textureState().activateTexture(0); |
| 40 | glGenTextures(1, &texture.id); |
| 41 | caches.textureState().bindTexture(GL_TEXTURE_2D, texture.id); |
| 42 | |
| 43 | texture.setWrap(GL_CLAMP_TO_EDGE, false, false, GL_TEXTURE_2D); |
| 44 | // not setting filter on texture, since it's set when drawing, based on transform |
| 45 | |
| 46 | glPixelStorei(GL_UNPACK_ALIGNMENT, 4); |
| 47 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture.width, texture.height, 0, |
| 48 | GL_RGBA, GL_UNSIGNED_BYTE, nullptr); |
Chris Craik | 818c9fb | 2015-10-23 14:33:42 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame^] | 51 | //////////////////////////////////////////////////////////////////////////////// |
| 52 | // BakedOpRenderer |
| 53 | //////////////////////////////////////////////////////////////////////////////// |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 54 | |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame^] | 55 | OffscreenBuffer* BakedOpRenderer::startLayer(uint32_t width, uint32_t height) { |
| 56 | LOG_ALWAYS_FATAL_IF(mRenderTarget.offscreenBuffer, "already has layer..."); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 57 | |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame^] | 58 | // TODO: really should be caching these! |
| 59 | OffscreenBuffer* buffer = new OffscreenBuffer(mCaches, width, height, width, height); |
| 60 | mRenderTarget.offscreenBuffer = buffer; |
Chris Craik | 818c9fb | 2015-10-23 14:33:42 -0700 | [diff] [blame] | 61 | |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame^] | 62 | // create and bind framebuffer |
| 63 | mRenderTarget.frameBufferId = mRenderState.genFramebuffer(); |
| 64 | mRenderState.bindFramebuffer(mRenderTarget.frameBufferId); |
Chris Craik | 818c9fb | 2015-10-23 14:33:42 -0700 | [diff] [blame] | 65 | |
| 66 | // attach the texture to the FBO |
| 67 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame^] | 68 | buffer->texture.id, 0); |
Chris Craik | 818c9fb | 2015-10-23 14:33:42 -0700 | [diff] [blame] | 69 | LOG_ALWAYS_FATAL_IF(GLUtils::dumpGLErrors(), "startLayer FAILED"); |
| 70 | LOG_ALWAYS_FATAL_IF(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE, |
| 71 | "framebuffer incomplete!"); |
| 72 | |
| 73 | // Clear the FBO |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame^] | 74 | mRenderState.scissor().setEnabled(false); |
Chris Craik | 818c9fb | 2015-10-23 14:33:42 -0700 | [diff] [blame] | 75 | glClear(GL_COLOR_BUFFER_BIT); |
| 76 | |
| 77 | // Change the viewport & ortho projection |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame^] | 78 | setViewport(width, height); |
| 79 | return buffer; |
Chris Craik | 818c9fb | 2015-10-23 14:33:42 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame^] | 82 | void BakedOpRenderer::endLayer() { |
| 83 | mRenderTarget.offscreenBuffer = nullptr; |
Chris Craik | 818c9fb | 2015-10-23 14:33:42 -0700 | [diff] [blame] | 84 | |
| 85 | // Detach the texture from the FBO |
| 86 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0); |
| 87 | LOG_ALWAYS_FATAL_IF(GLUtils::dumpGLErrors(), "endLayer FAILED"); |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame^] | 88 | mRenderState.deleteFramebuffer(mRenderTarget.frameBufferId); |
| 89 | mRenderTarget.frameBufferId = -1; |
Chris Craik | 818c9fb | 2015-10-23 14:33:42 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame^] | 92 | void BakedOpRenderer::startFrame(uint32_t width, uint32_t height) { |
| 93 | mRenderState.bindFramebuffer(0); |
| 94 | setViewport(width, height); |
| 95 | mCaches.clearGarbage(); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 96 | |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame^] | 97 | if (!mOpaque) { |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 98 | // TODO: partial invalidate! |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame^] | 99 | mRenderState.scissor().setEnabled(false); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 100 | glClear(GL_COLOR_BUFFER_BIT); |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame^] | 101 | mHasDrawn = true; |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 102 | } |
| 103 | } |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame^] | 104 | |
| 105 | void BakedOpRenderer::endFrame() { |
| 106 | mCaches.pathCache.trim(); |
| 107 | mCaches.tessellationCache.trim(); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 108 | |
| 109 | #if DEBUG_OPENGL |
| 110 | GLUtils::dumpGLErrors(); |
| 111 | #endif |
| 112 | |
| 113 | #if DEBUG_MEMORY_USAGE |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame^] | 114 | mCaches.dumpMemoryUsage(); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 115 | #else |
| 116 | if (Properties::debugLevel & kDebugMemory) { |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame^] | 117 | mCaches.dumpMemoryUsage(); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 118 | } |
| 119 | #endif |
| 120 | } |
| 121 | |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame^] | 122 | void BakedOpRenderer::setViewport(uint32_t width, uint32_t height) { |
| 123 | mRenderTarget.viewportWidth = width; |
| 124 | mRenderTarget.viewportHeight = height; |
| 125 | mRenderTarget.orthoMatrix.loadOrtho(width, height); |
| 126 | |
| 127 | mRenderState.setViewport(width, height); |
| 128 | mRenderState.blend().syncEnabled(); |
| 129 | } |
| 130 | |
| 131 | Texture* BakedOpRenderer::getTexture(const SkBitmap* bitmap) { |
| 132 | Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap); |
| 133 | if (!texture) { |
| 134 | return mCaches.textureCache.get(bitmap); |
| 135 | } |
| 136 | return texture; |
| 137 | } |
| 138 | |
| 139 | void BakedOpRenderer::renderGlop(const BakedOpState& state, const Glop& glop) { |
| 140 | bool useScissor = state.computedState.clipSideFlags != OpClipSideFlags::None; |
| 141 | mRenderState.scissor().setEnabled(useScissor); |
| 142 | if (useScissor) { |
| 143 | const Rect& clip = state.computedState.clipRect; |
| 144 | mRenderState.scissor().set(clip.left, mRenderTarget.viewportHeight - clip.bottom, |
| 145 | clip.getWidth(), clip.getHeight()); |
| 146 | } |
| 147 | mRenderState.render(glop, mRenderTarget.orthoMatrix); |
| 148 | mHasDrawn = true; |
| 149 | } |
| 150 | |
| 151 | //////////////////////////////////////////////////////////////////////////////// |
| 152 | // static BakedOpDispatcher methods |
| 153 | //////////////////////////////////////////////////////////////////////////////// |
| 154 | |
| 155 | void BakedOpDispatcher::onRenderNodeOp(BakedOpRenderer&, const RenderNodeOp&, const BakedOpState&) { |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 156 | LOG_ALWAYS_FATAL("unsupported operation"); |
| 157 | } |
| 158 | |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame^] | 159 | void BakedOpDispatcher::onBitmapOp(BakedOpRenderer& renderer, const BitmapOp& op, const BakedOpState& state) { |
| 160 | renderer.caches().textureState().activateTexture(0); // TODO: should this be automatic, and/or elsewhere? |
| 161 | Texture* texture = renderer.getTexture(op.bitmap); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 162 | if (!texture) return; |
| 163 | const AutoTexture autoCleanup(texture); |
| 164 | |
| 165 | const int textureFillFlags = (op.bitmap->colorType() == kAlpha_8_SkColorType) |
| 166 | ? TextureFillFlags::IsAlphaMaskTexture : TextureFillFlags::None; |
| 167 | Glop glop; |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame^] | 168 | GlopBuilder(renderer.renderState(), renderer.caches(), &glop) |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 169 | .setRoundRectClipState(state.roundRectClipState) |
| 170 | .setMeshTexturedUnitQuad(texture->uvMapper) |
| 171 | .setFillTexturePaint(*texture, textureFillFlags, op.paint, state.alpha) |
| 172 | .setTransform(state.computedState.transform, TransformFlags::None) |
| 173 | .setModelViewMapUnitToRectSnap(Rect(0, 0, texture->width, texture->height)) |
| 174 | .build(); |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame^] | 175 | renderer.renderGlop(state, glop); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame^] | 178 | void BakedOpDispatcher::onRectOp(BakedOpRenderer& renderer, const RectOp& op, const BakedOpState& state) { |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 179 | Glop glop; |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame^] | 180 | GlopBuilder(renderer.renderState(), renderer.caches(), &glop) |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 181 | .setRoundRectClipState(state.roundRectClipState) |
| 182 | .setMeshUnitQuad() |
| 183 | .setFillPaint(*op.paint, state.alpha) |
| 184 | .setTransform(state.computedState.transform, TransformFlags::None) |
| 185 | .setModelViewMapUnitToRect(op.unmappedBounds) |
| 186 | .build(); |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame^] | 187 | renderer.renderGlop(state, glop); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame^] | 190 | void BakedOpDispatcher::onSimpleRectsOp(BakedOpRenderer& renderer, const SimpleRectsOp& op, const BakedOpState& state) { |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 191 | Glop glop; |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame^] | 192 | GlopBuilder(renderer.renderState(), renderer.caches(), &glop) |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 193 | .setRoundRectClipState(state.roundRectClipState) |
| 194 | .setMeshIndexedQuads(&op.vertices[0], op.vertexCount / 4) |
| 195 | .setFillPaint(*op.paint, state.alpha) |
| 196 | .setTransform(state.computedState.transform, TransformFlags::None) |
| 197 | .setModelViewOffsetRect(0, 0, op.unmappedBounds) |
| 198 | .build(); |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame^] | 199 | renderer.renderGlop(state, glop); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame^] | 202 | void BakedOpDispatcher::onBeginLayerOp(BakedOpRenderer& renderer, const BeginLayerOp& op, const BakedOpState& state) { |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 203 | LOG_ALWAYS_FATAL("unsupported operation"); |
| 204 | } |
| 205 | |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame^] | 206 | void BakedOpDispatcher::onEndLayerOp(BakedOpRenderer& renderer, const EndLayerOp& op, const BakedOpState& state) { |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 207 | LOG_ALWAYS_FATAL("unsupported operation"); |
| 208 | } |
| 209 | |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame^] | 210 | void BakedOpDispatcher::onLayerOp(BakedOpRenderer& renderer, const LayerOp& op, const BakedOpState& state) { |
| 211 | OffscreenBuffer* buffer = *op.layerHandle; |
Chris Craik | 818c9fb | 2015-10-23 14:33:42 -0700 | [diff] [blame] | 212 | |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame^] | 213 | // TODO: extend this to handle HW layers & paint properties which |
| 214 | // reside in node.properties().layerProperties() |
| 215 | float layerAlpha = (op.paint->getAlpha() / 255.0f) * state.alpha; |
Chris Craik | 818c9fb | 2015-10-23 14:33:42 -0700 | [diff] [blame] | 216 | const bool tryToSnap = state.computedState.transform.isPureTranslate(); |
| 217 | Glop glop; |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame^] | 218 | GlopBuilder(renderer.renderState(), renderer.caches(), &glop) |
Chris Craik | 818c9fb | 2015-10-23 14:33:42 -0700 | [diff] [blame] | 219 | .setRoundRectClipState(state.roundRectClipState) |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame^] | 220 | .setMeshTexturedUvQuad(nullptr, buffer->texCoords) |
| 221 | .setFillLayer(buffer->texture, op.paint->getColorFilter(), layerAlpha, PaintUtils::getXfermodeDirect(op.paint), Blend::ModeOrderSwap::NoSwap) |
Chris Craik | 818c9fb | 2015-10-23 14:33:42 -0700 | [diff] [blame] | 222 | .setTransform(state.computedState.transform, TransformFlags::None) |
| 223 | .setModelViewMapUnitToRectOptionalSnap(tryToSnap, op.unmappedBounds) |
| 224 | .build(); |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame^] | 225 | renderer.renderGlop(state, glop); |
Chris Craik | 818c9fb | 2015-10-23 14:33:42 -0700 | [diff] [blame] | 226 | |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame^] | 227 | // destroy and delete, since each clipped saveLayer is only drawn once. |
| 228 | buffer->texture.deleteTexture(); |
| 229 | |
| 230 | // TODO: return texture/offscreenbuffer to cache! |
| 231 | delete buffer; |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 232 | } |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 233 | |
| 234 | } // namespace uirenderer |
| 235 | } // namespace android |