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