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