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