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