Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 17 | #define LOG_TAG "OpenGLRenderer" |
| 18 | |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 19 | #include <SkCanvas.h> |
| 20 | |
| 21 | #include "StatefulBaseRenderer.h" |
| 22 | |
Chris Craik | af4d04c | 2014-07-29 12:50:14 -0700 | [diff] [blame] | 23 | #include "utils/MathUtils.h" |
| 24 | |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 25 | namespace android { |
| 26 | namespace uirenderer { |
| 27 | |
Chris Craik | 058fc64 | 2014-07-23 18:19:28 -0700 | [diff] [blame] | 28 | StatefulBaseRenderer::StatefulBaseRenderer() |
| 29 | : mDirtyClip(false) |
| 30 | , mWidth(-1) |
| 31 | , mHeight(-1) |
| 32 | , mSaveCount(1) |
| 33 | , mFirstSnapshot(new Snapshot) |
| 34 | , mSnapshot(mFirstSnapshot) { |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | void StatefulBaseRenderer::initializeSaveStack(float clipLeft, float clipTop, |
Chris Craik | 69e5adf | 2014-08-14 13:34:01 -0700 | [diff] [blame] | 38 | float clipRight, float clipBottom, const Vector3& lightCenter) { |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 39 | mSnapshot = new Snapshot(mFirstSnapshot, |
| 40 | SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag); |
| 41 | mSnapshot->setClip(clipLeft, clipTop, clipRight, clipBottom); |
| 42 | mSnapshot->fbo = getTargetFbo(); |
Chris Craik | 69e5adf | 2014-08-14 13:34:01 -0700 | [diff] [blame] | 43 | mSnapshot->setRelativeLightCenter(lightCenter); |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 44 | mSaveCount = 1; |
| 45 | } |
| 46 | |
Chris Craik | 797b95b2 | 2014-05-20 18:10:25 -0700 | [diff] [blame] | 47 | void StatefulBaseRenderer::setViewport(int width, int height) { |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 48 | mWidth = width; |
| 49 | mHeight = height; |
Chris Craik | a64a2be | 2014-05-14 14:17:01 -0700 | [diff] [blame] | 50 | mFirstSnapshot->initializeViewport(width, height); |
Chris Craik | 797b95b2 | 2014-05-20 18:10:25 -0700 | [diff] [blame] | 51 | onViewportInitialized(); |
| 52 | } |
| 53 | |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 54 | /////////////////////////////////////////////////////////////////////////////// |
| 55 | // Save (layer) |
| 56 | /////////////////////////////////////////////////////////////////////////////// |
| 57 | |
| 58 | /** |
| 59 | * Non-virtual implementation of save, guaranteed to save without side-effects |
| 60 | * |
| 61 | * The approach here and in restoreSnapshot(), allows subclasses to directly manipulate the save |
| 62 | * stack, and ensures restoreToCount() doesn't call back into subclass overrides. |
| 63 | */ |
| 64 | int StatefulBaseRenderer::saveSnapshot(int flags) { |
| 65 | mSnapshot = new Snapshot(mSnapshot, flags); |
| 66 | return mSaveCount++; |
| 67 | } |
| 68 | |
| 69 | int StatefulBaseRenderer::save(int flags) { |
| 70 | return saveSnapshot(flags); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Non-virtual implementation of restore, guaranteed to restore without side-effects. |
| 75 | */ |
| 76 | void StatefulBaseRenderer::restoreSnapshot() { |
| 77 | sp<Snapshot> toRemove = mSnapshot; |
| 78 | sp<Snapshot> toRestore = mSnapshot->previous; |
| 79 | |
| 80 | mSaveCount--; |
| 81 | mSnapshot = toRestore; |
| 82 | |
| 83 | // subclass handles restore implementation |
| 84 | onSnapshotRestored(*toRemove, *toRestore); |
| 85 | } |
| 86 | |
| 87 | void StatefulBaseRenderer::restore() { |
| 88 | if (mSaveCount > 1) { |
| 89 | restoreSnapshot(); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | void StatefulBaseRenderer::restoreToCount(int saveCount) { |
| 94 | if (saveCount < 1) saveCount = 1; |
| 95 | |
| 96 | while (mSaveCount > saveCount) { |
| 97 | restoreSnapshot(); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /////////////////////////////////////////////////////////////////////////////// |
| 102 | // Matrix |
| 103 | /////////////////////////////////////////////////////////////////////////////// |
| 104 | |
| 105 | void StatefulBaseRenderer::getMatrix(SkMatrix* matrix) const { |
| 106 | mSnapshot->transform->copyTo(*matrix); |
| 107 | } |
| 108 | |
| 109 | void StatefulBaseRenderer::translate(float dx, float dy, float dz) { |
| 110 | mSnapshot->transform->translate(dx, dy, dz); |
| 111 | } |
| 112 | |
| 113 | void StatefulBaseRenderer::rotate(float degrees) { |
| 114 | mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f); |
| 115 | } |
| 116 | |
| 117 | void StatefulBaseRenderer::scale(float sx, float sy) { |
| 118 | mSnapshot->transform->scale(sx, sy, 1.0f); |
| 119 | } |
| 120 | |
| 121 | void StatefulBaseRenderer::skew(float sx, float sy) { |
| 122 | mSnapshot->transform->skew(sx, sy); |
| 123 | } |
| 124 | |
Derek Sollenberger | 1390882 | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 125 | void StatefulBaseRenderer::setMatrix(const SkMatrix& matrix) { |
| 126 | mSnapshot->transform->load(matrix); |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | void StatefulBaseRenderer::setMatrix(const Matrix4& matrix) { |
| 130 | mSnapshot->transform->load(matrix); |
| 131 | } |
| 132 | |
Derek Sollenberger | 1390882 | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 133 | void StatefulBaseRenderer::concatMatrix(const SkMatrix& matrix) { |
| 134 | mat4 transform(matrix); |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 135 | mSnapshot->transform->multiply(transform); |
| 136 | } |
| 137 | |
| 138 | void StatefulBaseRenderer::concatMatrix(const Matrix4& matrix) { |
| 139 | mSnapshot->transform->multiply(matrix); |
| 140 | } |
| 141 | |
| 142 | /////////////////////////////////////////////////////////////////////////////// |
| 143 | // Clip |
| 144 | /////////////////////////////////////////////////////////////////////////////// |
| 145 | |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 146 | bool StatefulBaseRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) { |
| 147 | if (CC_LIKELY(currentTransform()->rectToRect())) { |
| 148 | mDirtyClip |= mSnapshot->clip(left, top, right, bottom, op); |
| 149 | return !mSnapshot->clipRect->isEmpty(); |
| 150 | } |
| 151 | |
| 152 | SkPath path; |
| 153 | path.addRect(left, top, right, bottom); |
| 154 | |
| 155 | return StatefulBaseRenderer::clipPath(&path, op); |
| 156 | } |
| 157 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 158 | bool StatefulBaseRenderer::clipPath(const SkPath* path, SkRegion::Op op) { |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 159 | SkMatrix transform; |
| 160 | currentTransform()->copyTo(transform); |
| 161 | |
| 162 | SkPath transformed; |
| 163 | path->transform(transform, &transformed); |
| 164 | |
| 165 | SkRegion clip; |
| 166 | if (!mSnapshot->previous->clipRegion->isEmpty()) { |
| 167 | clip.setRegion(*mSnapshot->previous->clipRegion); |
| 168 | } else { |
| 169 | if (mSnapshot->previous == firstSnapshot()) { |
| 170 | clip.setRect(0, 0, getWidth(), getHeight()); |
| 171 | } else { |
| 172 | Rect* bounds = mSnapshot->previous->clipRect; |
| 173 | clip.setRect(bounds->left, bounds->top, bounds->right, bounds->bottom); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | SkRegion region; |
| 178 | region.setPath(transformed, clip); |
| 179 | |
Chris Craik | 62d307c | 2014-07-29 10:35:13 -0700 | [diff] [blame] | 180 | // region is the transformed input path, masked by the previous clip |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 181 | mDirtyClip |= mSnapshot->clipRegionTransformed(region, op); |
| 182 | return !mSnapshot->clipRect->isEmpty(); |
| 183 | } |
| 184 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 185 | bool StatefulBaseRenderer::clipRegion(const SkRegion* region, SkRegion::Op op) { |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 186 | mDirtyClip |= mSnapshot->clipRegionTransformed(*region, op); |
| 187 | return !mSnapshot->clipRect->isEmpty(); |
| 188 | } |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 189 | |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 190 | void StatefulBaseRenderer::setClippingOutline(LinearAllocator& allocator, const Outline* outline) { |
Chris Craik | af4d04c | 2014-07-29 12:50:14 -0700 | [diff] [blame] | 191 | Rect bounds; |
| 192 | float radius; |
| 193 | if (!outline->getAsRoundRect(&bounds, &radius)) return; // only RR supported |
| 194 | |
Chris Craik | 79d26c7 | 2014-08-21 12:26:16 -0700 | [diff] [blame] | 195 | bool outlineIsRounded = MathUtils::isPositive(radius); |
| 196 | if (!outlineIsRounded || currentTransform()->isSimple()) { |
Chris Craik | af4d04c | 2014-07-29 12:50:14 -0700 | [diff] [blame] | 197 | // TODO: consider storing this rect separately, so that this can't be replaced with clip ops |
| 198 | clipRect(bounds.left, bounds.top, bounds.right, bounds.bottom, SkRegion::kIntersect_Op); |
Chris Craik | af4d04c | 2014-07-29 12:50:14 -0700 | [diff] [blame] | 199 | } |
Chris Craik | 79d26c7 | 2014-08-21 12:26:16 -0700 | [diff] [blame] | 200 | if (outlineIsRounded) { |
Chris Craik | e83cbd4 | 2014-09-03 17:52:24 -0700 | [diff] [blame] | 201 | setClippingRoundRect(allocator, bounds, radius, false); |
Chris Craik | 79d26c7 | 2014-08-21 12:26:16 -0700 | [diff] [blame] | 202 | } |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Chris Craik | af4d04c | 2014-07-29 12:50:14 -0700 | [diff] [blame] | 205 | void StatefulBaseRenderer::setClippingRoundRect(LinearAllocator& allocator, |
Chris Craik | e83cbd4 | 2014-09-03 17:52:24 -0700 | [diff] [blame] | 206 | const Rect& rect, float radius, bool highPriority) { |
| 207 | mSnapshot->setClippingRoundRect(allocator, rect, radius, highPriority); |
Chris Craik | af4d04c | 2014-07-29 12:50:14 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 211 | /////////////////////////////////////////////////////////////////////////////// |
| 212 | // Quick Rejection |
| 213 | /////////////////////////////////////////////////////////////////////////////// |
| 214 | |
| 215 | /** |
| 216 | * Calculates whether content drawn within the passed bounds would be outside of, or intersect with |
| 217 | * the clipRect. Does not modify the scissor. |
| 218 | * |
| 219 | * @param clipRequired if not null, will be set to true if element intersects clip |
| 220 | * (and wasn't rejected) |
| 221 | * |
| 222 | * @param snapOut if set, the geometry will be treated as having an AA ramp. |
| 223 | * See Rect::snapGeometryToPixelBoundaries() |
| 224 | */ |
| 225 | bool StatefulBaseRenderer::calculateQuickRejectForScissor(float left, float top, |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 226 | float right, float bottom, |
| 227 | bool* clipRequired, bool* roundRectClipRequired, |
| 228 | bool snapOut) const { |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 229 | if (mSnapshot->isIgnored() || bottom <= top || right <= left) { |
| 230 | return true; |
| 231 | } |
| 232 | |
| 233 | Rect r(left, top, right, bottom); |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 234 | currentTransform()->mapRect(r); |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 235 | r.snapGeometryToPixelBoundaries(snapOut); |
| 236 | |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 237 | Rect clipRect(*currentClipRect()); |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 238 | clipRect.snapToPixelBoundaries(); |
| 239 | |
| 240 | if (!clipRect.intersects(r)) return true; |
| 241 | |
| 242 | // clip is required if geometry intersects clip rect |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 243 | if (clipRequired) { |
| 244 | *clipRequired = !clipRect.contains(r); |
| 245 | } |
| 246 | |
| 247 | // round rect clip is required if RR clip exists, and geometry intersects its corners |
| 248 | if (roundRectClipRequired) { |
| 249 | *roundRectClipRequired = mSnapshot->roundRectClipState != NULL |
| 250 | && mSnapshot->roundRectClipState->areaRequiresRoundRectClip(r); |
| 251 | } |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 252 | return false; |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Returns false if drawing won't be clipped out. |
| 257 | * |
| 258 | * Makes the decision conservatively, by rounding out the mapped rect before comparing with the |
| 259 | * clipRect. To be used when perfect, pixel accuracy is not possible (esp. with tessellation) but |
| 260 | * rejection is still desired. |
| 261 | * |
| 262 | * This function, unlike quickRejectSetupScissor, should be used where precise geometry information |
| 263 | * isn't known (esp. when geometry adjusts based on scale). Generally, this will be first pass |
| 264 | * rejection where precise rejection isn't important, or precise information isn't available. |
| 265 | */ |
| 266 | bool StatefulBaseRenderer::quickRejectConservative(float left, float top, |
| 267 | float right, float bottom) const { |
| 268 | if (mSnapshot->isIgnored() || bottom <= top || right <= left) { |
| 269 | return true; |
| 270 | } |
| 271 | |
| 272 | Rect r(left, top, right, bottom); |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 273 | currentTransform()->mapRect(r); |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 274 | r.roundOut(); // rounded out to be conservative |
| 275 | |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 276 | Rect clipRect(*currentClipRect()); |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 277 | clipRect.snapToPixelBoundaries(); |
| 278 | |
| 279 | if (!clipRect.intersects(r)) return true; |
| 280 | |
| 281 | return false; |
| 282 | } |
| 283 | |
| 284 | }; // namespace uirenderer |
| 285 | }; // namespace android |