BakedOpRenderer.cpp revision 2de7771740ee08fcaff638ec6b2e460bb72fff04
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/OffscreenBufferPool.h" 23#include "renderstate/RenderState.h" 24#include "utils/GLUtils.h" 25#include "VertexBuffer.h" 26 27#include <algorithm> 28 29namespace android { 30namespace uirenderer { 31 32OffscreenBuffer* BakedOpRenderer::startTemporaryLayer(uint32_t width, uint32_t height) { 33 LOG_ALWAYS_FATAL_IF(mRenderTarget.offscreenBuffer, "already has layer..."); 34 35 OffscreenBuffer* buffer = mRenderState.layerPool().get(mRenderState, width, height); 36 startRepaintLayer(buffer, Rect(width, height)); 37 return buffer; 38} 39 40void BakedOpRenderer::startRepaintLayer(OffscreenBuffer* offscreenBuffer, const Rect& repaintRect) { 41 LOG_ALWAYS_FATAL_IF(mRenderTarget.offscreenBuffer, "already has layer..."); 42 43 mRenderTarget.offscreenBuffer = offscreenBuffer; 44 45 // create and bind framebuffer 46 mRenderTarget.frameBufferId = mRenderState.genFramebuffer(); 47 mRenderState.bindFramebuffer(mRenderTarget.frameBufferId); 48 49 // attach the texture to the FBO 50 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 51 offscreenBuffer->texture.id(), 0); 52 LOG_ALWAYS_FATAL_IF(GLUtils::dumpGLErrors(), "startLayer FAILED"); 53 LOG_ALWAYS_FATAL_IF(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE, 54 "framebuffer incomplete!"); 55 56 // Change the viewport & ortho projection 57 setViewport(offscreenBuffer->viewportWidth, offscreenBuffer->viewportHeight); 58 59 clearColorBuffer(repaintRect); 60} 61 62void BakedOpRenderer::endLayer() { 63 if (mRenderTarget.stencil) { 64 // if stencil was used for clipping, detach it and return it to pool 65 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, 0); 66 LOG_ALWAYS_FATAL_IF(GLUtils::dumpGLErrors(), "glfbrb endlayer failed"); 67 mCaches.renderBufferCache.put(mRenderTarget.stencil); 68 mRenderTarget.stencil = nullptr; 69 } 70 mRenderTarget.lastStencilClip = nullptr; 71 72 mRenderTarget.offscreenBuffer->updateMeshFromRegion(); 73 mRenderTarget.offscreenBuffer = nullptr; // It's in drawLayerOp's hands now. 74 75 // Detach the texture from the FBO 76 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0); 77 LOG_ALWAYS_FATAL_IF(GLUtils::dumpGLErrors(), "endLayer FAILED, bound fbo = %u", 78 mRenderState.getFramebuffer()); 79 mRenderState.deleteFramebuffer(mRenderTarget.frameBufferId); 80 mRenderTarget.frameBufferId = 0; 81} 82 83OffscreenBuffer* BakedOpRenderer::copyToLayer(const Rect& area) { 84 OffscreenBuffer* buffer = mRenderState.layerPool().get(mRenderState, 85 area.getWidth(), area.getHeight()); 86 if (!area.isEmpty()) { 87 mCaches.textureState().activateTexture(0); 88 mCaches.textureState().bindTexture(buffer->texture.id()); 89 90 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 91 area.left, mRenderTarget.viewportHeight - area.bottom, 92 area.getWidth(), area.getHeight()); 93 } 94 return buffer; 95} 96 97void BakedOpRenderer::startFrame(uint32_t width, uint32_t height, const Rect& repaintRect) { 98 LOG_ALWAYS_FATAL_IF(mRenderTarget.frameBufferId != 0, "primary framebufferId must be 0"); 99 mRenderState.bindFramebuffer(0); 100 setViewport(width, height); 101 102 if (!mOpaque) { 103 clearColorBuffer(repaintRect); 104 } 105 106 mRenderState.debugOverdraw(true, true); 107} 108 109void BakedOpRenderer::endFrame(const Rect& repaintRect) { 110 if (CC_UNLIKELY(Properties::debugOverdraw)) { 111 ClipRect overdrawClip(repaintRect); 112 Rect viewportRect(mRenderTarget.viewportWidth, mRenderTarget.viewportHeight); 113 // overdraw visualization 114 for (int i = 1; i <= 4; i++) { 115 if (i < 4) { 116 // nth level of overdraw tests for n+1 draws per pixel 117 mRenderState.stencil().enableDebugTest(i + 1, false); 118 } else { 119 // 4th level tests for 4 or higher draws per pixel 120 mRenderState.stencil().enableDebugTest(4, true); 121 } 122 123 SkPaint paint; 124 paint.setColor(mCaches.getOverdrawColor(i)); 125 Glop glop; 126 GlopBuilder(mRenderState, mCaches, &glop) 127 .setRoundRectClipState(nullptr) 128 .setMeshUnitQuad() 129 .setFillPaint(paint, 1.0f) 130 .setTransform(Matrix4::identity(), TransformFlags::None) 131 .setModelViewMapUnitToRect(viewportRect) 132 .build(); 133 renderGlop(nullptr, &overdrawClip, glop); 134 } 135 mRenderState.stencil().disable(); 136 } 137 138 mCaches.clearGarbage(); 139 mCaches.pathCache.trim(); 140 mCaches.tessellationCache.trim(); 141 142 GL_CHECKPOINT(); 143 144#if DEBUG_MEMORY_USAGE 145 mCaches.dumpMemoryUsage(); 146#else 147 if (Properties::debugLevel & kDebugMemory) { 148 mCaches.dumpMemoryUsage(); 149 } 150#endif 151} 152 153void BakedOpRenderer::setViewport(uint32_t width, uint32_t height) { 154 mRenderTarget.viewportWidth = width; 155 mRenderTarget.viewportHeight = height; 156 mRenderTarget.orthoMatrix.loadOrtho(width, height); 157 158 mRenderState.setViewport(width, height); 159 mRenderState.blend().syncEnabled(); 160} 161 162void BakedOpRenderer::clearColorBuffer(const Rect& rect) { 163 if (rect.contains(Rect(mRenderTarget.viewportWidth, mRenderTarget.viewportHeight))) { 164 // Full viewport is being cleared - disable scissor 165 mRenderState.scissor().setEnabled(false); 166 } else { 167 // Requested rect is subset of viewport - scissor to it to avoid over-clearing 168 mRenderState.scissor().setEnabled(true); 169 mRenderState.scissor().set(rect.left, mRenderTarget.viewportHeight - rect.bottom, 170 rect.getWidth(), rect.getHeight()); 171 } 172 glClear(GL_COLOR_BUFFER_BIT); 173 if (!mRenderTarget.frameBufferId) mHasDrawn = true; 174} 175 176Texture* BakedOpRenderer::getTexture(const SkBitmap* bitmap) { 177 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap->pixelRef()); 178 if (!texture) { 179 return mCaches.textureCache.get(bitmap); 180 } 181 return texture; 182} 183 184// clears and re-fills stencil with provided rendertarget space quads, 185// and then put stencil into test mode 186void BakedOpRenderer::setupStencilQuads(std::vector<Vertex>& quadVertices, 187 int incrementThreshold) { 188 mRenderState.stencil().enableWrite(incrementThreshold); 189 mRenderState.stencil().clear(); 190 Glop glop; 191 GlopBuilder(mRenderState, mCaches, &glop) 192 .setRoundRectClipState(nullptr) 193 .setMeshIndexedQuads(quadVertices.data(), quadVertices.size() / 4) 194 .setFillBlack() 195 .setTransform(Matrix4::identity(), TransformFlags::None) 196 .setModelViewIdentityEmptyBounds() 197 .build(); 198 mRenderState.render(glop, mRenderTarget.orthoMatrix); 199 mRenderState.stencil().enableTest(incrementThreshold); 200} 201 202void BakedOpRenderer::setupStencilRectList(const ClipBase* clip) { 203 LOG_ALWAYS_FATAL_IF(clip->mode != ClipMode::RectangleList, "can't rectlist clip without rectlist"); 204 auto&& rectList = reinterpret_cast<const ClipRectList*>(clip)->rectList; 205 int quadCount = rectList.getTransformedRectanglesCount(); 206 std::vector<Vertex> rectangleVertices; 207 rectangleVertices.reserve(quadCount * 4); 208 for (int i = 0; i < quadCount; i++) { 209 const TransformedRectangle& tr(rectList.getTransformedRectangle(i)); 210 const Matrix4& transform = tr.getTransform(); 211 Rect bounds = tr.getBounds(); 212 if (transform.rectToRect()) { 213 // If rectToRect, can simply map bounds before storing verts 214 transform.mapRect(bounds); 215 bounds.doIntersect(clip->rect); 216 if (bounds.isEmpty()) { 217 continue; // will be outside of scissor, skip 218 } 219 } 220 221 rectangleVertices.push_back(Vertex{bounds.left, bounds.top}); 222 rectangleVertices.push_back(Vertex{bounds.right, bounds.top}); 223 rectangleVertices.push_back(Vertex{bounds.left, bounds.bottom}); 224 rectangleVertices.push_back(Vertex{bounds.right, bounds.bottom}); 225 226 if (!transform.rectToRect()) { 227 // If not rectToRect, must map each point individually 228 for (auto cur = rectangleVertices.end() - 4; cur < rectangleVertices.end(); cur++) { 229 transform.mapPoint(cur->x, cur->y); 230 } 231 } 232 } 233 setupStencilQuads(rectangleVertices, rectList.getTransformedRectanglesCount()); 234} 235 236void BakedOpRenderer::setupStencilRegion(const ClipBase* clip) { 237 LOG_ALWAYS_FATAL_IF(clip->mode != ClipMode::Region, "can't region clip without region"); 238 auto&& region = reinterpret_cast<const ClipRegion*>(clip)->region; 239 240 std::vector<Vertex> regionVertices; 241 SkRegion::Cliperator it(region, clip->rect.toSkIRect()); 242 while (!it.done()) { 243 const SkIRect& r = it.rect(); 244 regionVertices.push_back(Vertex{(float)r.fLeft, (float)r.fTop}); 245 regionVertices.push_back(Vertex{(float)r.fRight, (float)r.fTop}); 246 regionVertices.push_back(Vertex{(float)r.fLeft, (float)r.fBottom}); 247 regionVertices.push_back(Vertex{(float)r.fRight, (float)r.fBottom}); 248 it.next(); 249 } 250 setupStencilQuads(regionVertices, 0); 251} 252 253void BakedOpRenderer::prepareRender(const Rect* dirtyBounds, const ClipBase* clip) { 254 // Prepare scissor (done before stencil, to simplify filling stencil) 255 mRenderState.scissor().setEnabled(clip != nullptr); 256 if (clip) { 257 mRenderState.scissor().set(mRenderTarget.viewportHeight, clip->rect); 258 } 259 260 // If stencil may be used for clipping, enable it, fill it, or disable it as appropriate 261 if (CC_LIKELY(!Properties::debugOverdraw)) { 262 // only modify stencil mode and content when it's not used for overdraw visualization 263 if (CC_UNLIKELY(clip && clip->mode != ClipMode::Rectangle)) { 264 // NOTE: this pointer check is only safe for non-rect clips, 265 // since rect clips may be created on the stack 266 if (mRenderTarget.lastStencilClip != clip) { 267 // Stencil needed, but current stencil isn't up to date 268 mRenderTarget.lastStencilClip = clip; 269 270 if (mRenderTarget.frameBufferId != 0 && !mRenderTarget.stencil) { 271 OffscreenBuffer* layer = mRenderTarget.offscreenBuffer; 272 mRenderTarget.stencil = mCaches.renderBufferCache.get( 273 Stencil::getLayerStencilFormat(), 274 layer->texture.width(), layer->texture.height()); 275 // stencil is bound + allocated - associate it with current FBO 276 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, 277 GL_RENDERBUFFER, mRenderTarget.stencil->getName()); 278 } 279 280 if (clip->mode == ClipMode::RectangleList) { 281 setupStencilRectList(clip); 282 } else { 283 setupStencilRegion(clip); 284 } 285 } else { 286 // stencil is up to date - just need to ensure it's enabled (since an unclipped 287 // or scissor-only clipped op may have been drawn, disabling the stencil) 288 int incrementThreshold = 0; 289 if (CC_LIKELY(clip->mode == ClipMode::RectangleList)) { 290 auto&& rectList = reinterpret_cast<const ClipRectList*>(clip)->rectList; 291 incrementThreshold = rectList.getTransformedRectanglesCount(); 292 } 293 mRenderState.stencil().enableTest(incrementThreshold); 294 } 295 } else { 296 // either scissor or no clip, so disable stencil test 297 mRenderState.stencil().disable(); 298 } 299 } 300 301 // dirty offscreenbuffer 302 if (dirtyBounds && mRenderTarget.offscreenBuffer) { 303 // register layer damage to draw-back region 304 android::Rect dirty(dirtyBounds->left, dirtyBounds->top, 305 dirtyBounds->right, dirtyBounds->bottom); 306 mRenderTarget.offscreenBuffer->region.orSelf(dirty); 307 } 308} 309 310void BakedOpRenderer::renderGlop(const Rect* dirtyBounds, const ClipBase* clip, 311 const Glop& glop) { 312 prepareRender(dirtyBounds, clip); 313 mRenderState.render(glop, mRenderTarget.orthoMatrix); 314 if (!mRenderTarget.frameBufferId) mHasDrawn = true; 315} 316 317void BakedOpRenderer::renderFunctor(const FunctorOp& op, const BakedOpState& state) { 318 prepareRender(&state.computedState.clippedBounds, state.computedState.getClipIfNeeded()); 319 320 DrawGlInfo info; 321 auto&& clip = state.computedState.clipRect(); 322 info.clipLeft = clip.left; 323 info.clipTop = clip.top; 324 info.clipRight = clip.right; 325 info.clipBottom = clip.bottom; 326 info.isLayer = offscreenRenderTarget(); 327 info.width = mRenderTarget.viewportWidth; 328 info.height = mRenderTarget.viewportHeight; 329 state.computedState.transform.copyTo(&info.transform[0]); 330 331 mRenderState.invokeFunctor(op.functor, DrawGlInfo::kModeDraw, &info); 332} 333 334void BakedOpRenderer::dirtyRenderTarget(const Rect& uiDirty) { 335 if (mRenderTarget.offscreenBuffer) { 336 android::Rect dirty(uiDirty.left, uiDirty.top, uiDirty.right, uiDirty.bottom); 337 mRenderTarget.offscreenBuffer->region.orSelf(dirty); 338 } 339} 340 341} // namespace uirenderer 342} // namespace android 343