Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | 5f80362 | 2013-03-21 14:39:04 -0700 | [diff] [blame] | 17 | #define LOG_TAG "OpenGLRenderer" |
| 18 | |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 19 | #include "Snapshot.h" |
| 20 | |
| 21 | #include <SkCanvas.h> |
| 22 | |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame^] | 23 | #include "utils/MathUtils.h" |
| 24 | |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 25 | namespace android { |
| 26 | namespace uirenderer { |
| 27 | |
| 28 | /////////////////////////////////////////////////////////////////////////////// |
| 29 | // Constructors |
| 30 | /////////////////////////////////////////////////////////////////////////////// |
| 31 | |
Chris Craik | e4aa95e | 2014-05-08 13:57:05 -0700 | [diff] [blame] | 32 | Snapshot::Snapshot() |
| 33 | : flags(0) |
| 34 | , previous(NULL) |
| 35 | , layer(NULL) |
| 36 | , fbo(0) |
| 37 | , invisible(false) |
| 38 | , empty(false) |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame^] | 39 | , alpha(1.0f) |
| 40 | , roundRectClipState(NULL) { |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 41 | transform = &mTransformRoot; |
| 42 | clipRect = &mClipRectRoot; |
| 43 | region = NULL; |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 44 | clipRegion = &mClipRegionRoot; |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Copies the specified snapshot/ The specified snapshot is stored as |
| 49 | * the previous snapshot. |
| 50 | */ |
Chris Craik | e4aa95e | 2014-05-08 13:57:05 -0700 | [diff] [blame] | 51 | Snapshot::Snapshot(const sp<Snapshot>& s, int saveFlags) |
| 52 | : flags(0) |
| 53 | , previous(s) |
| 54 | , layer(s->layer) |
| 55 | , fbo(s->fbo) |
| 56 | , invisible(s->invisible) |
| 57 | , empty(false) |
Chris Craik | a64a2be | 2014-05-14 14:17:01 -0700 | [diff] [blame] | 58 | , alpha(s->alpha) |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame^] | 59 | , roundRectClipState(s->roundRectClipState) |
Chris Craik | a64a2be | 2014-05-14 14:17:01 -0700 | [diff] [blame] | 60 | , mViewportData(s->mViewportData) { |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 61 | if (saveFlags & SkCanvas::kMatrix_SaveFlag) { |
| 62 | mTransformRoot.load(*s->transform); |
| 63 | transform = &mTransformRoot; |
| 64 | } else { |
| 65 | transform = s->transform; |
| 66 | } |
| 67 | |
| 68 | if (saveFlags & SkCanvas::kClip_SaveFlag) { |
| 69 | mClipRectRoot.set(*s->clipRect); |
| 70 | clipRect = &mClipRectRoot; |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 71 | if (!s->clipRegion->isEmpty()) { |
Romain Guy | 0baaac5 | 2012-08-31 20:31:01 -0700 | [diff] [blame] | 72 | mClipRegionRoot.op(*s->clipRegion, SkRegion::kUnion_Op); |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 73 | } |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 74 | clipRegion = &mClipRegionRoot; |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 75 | } else { |
| 76 | clipRect = s->clipRect; |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 77 | clipRegion = s->clipRegion; |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | if (s->flags & Snapshot::kFlagFboTarget) { |
| 81 | flags |= Snapshot::kFlagFboTarget; |
| 82 | region = s->region; |
| 83 | } else { |
| 84 | region = NULL; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /////////////////////////////////////////////////////////////////////////////// |
| 89 | // Clipping |
| 90 | /////////////////////////////////////////////////////////////////////////////// |
| 91 | |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 92 | void Snapshot::ensureClipRegion() { |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 93 | if (clipRegion->isEmpty()) { |
Romain Guy | 0baaac5 | 2012-08-31 20:31:01 -0700 | [diff] [blame] | 94 | clipRegion->setRect(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom); |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 95 | } |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void Snapshot::copyClipRectFromRegion() { |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 99 | if (!clipRegion->isEmpty()) { |
Romain Guy | 0baaac5 | 2012-08-31 20:31:01 -0700 | [diff] [blame] | 100 | const SkIRect& bounds = clipRegion->getBounds(); |
| 101 | clipRect->set(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom); |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 102 | |
| 103 | if (clipRegion->isRect()) { |
Romain Guy | 0baaac5 | 2012-08-31 20:31:01 -0700 | [diff] [blame] | 104 | clipRegion->setEmpty(); |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 105 | } |
| 106 | } else { |
| 107 | clipRect->setEmpty(); |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 108 | } |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Romain Guy | 0baaac5 | 2012-08-31 20:31:01 -0700 | [diff] [blame] | 111 | bool Snapshot::clipRegionOp(float left, float top, float right, float bottom, SkRegion::Op op) { |
Romain Guy | 0baaac5 | 2012-08-31 20:31:01 -0700 | [diff] [blame] | 112 | SkIRect tmp; |
| 113 | tmp.set(left, top, right, bottom); |
| 114 | clipRegion->op(tmp, op); |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 115 | copyClipRectFromRegion(); |
| 116 | return true; |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | bool Snapshot::clipRegionTransformed(const SkRegion& region, SkRegion::Op op) { |
| 120 | ensureClipRegion(); |
| 121 | clipRegion->op(region, op); |
| 122 | copyClipRectFromRegion(); |
| 123 | flags |= Snapshot::kFlagClipSet; |
| 124 | return true; |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 125 | } |
| 126 | |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 127 | bool Snapshot::clip(float left, float top, float right, float bottom, SkRegion::Op op) { |
| 128 | Rect r(left, top, right, bottom); |
| 129 | transform->mapRect(r); |
| 130 | return clipTransformed(r, op); |
| 131 | } |
| 132 | |
| 133 | bool Snapshot::clipTransformed(const Rect& r, SkRegion::Op op) { |
| 134 | bool clipped = false; |
| 135 | |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 136 | switch (op) { |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 137 | case SkRegion::kIntersect_Op: { |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 138 | if (CC_UNLIKELY(!clipRegion->isEmpty())) { |
Romain Guy | 735738c | 2012-12-03 12:34:51 -0800 | [diff] [blame] | 139 | ensureClipRegion(); |
Romain Guy | 0baaac5 | 2012-08-31 20:31:01 -0700 | [diff] [blame] | 140 | clipped = clipRegionOp(r.left, r.top, r.right, r.bottom, SkRegion::kIntersect_Op); |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 141 | } else { |
| 142 | clipped = clipRect->intersect(r); |
| 143 | if (!clipped) { |
| 144 | clipRect->setEmpty(); |
| 145 | clipped = true; |
| 146 | } |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 147 | } |
| 148 | break; |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 149 | } |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 150 | case SkRegion::kReplace_Op: { |
| 151 | setClip(r.left, r.top, r.right, r.bottom); |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 152 | clipped = true; |
| 153 | break; |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 154 | } |
Romain Guy | 0baaac5 | 2012-08-31 20:31:01 -0700 | [diff] [blame] | 155 | default: { |
| 156 | ensureClipRegion(); |
| 157 | clipped = clipRegionOp(r.left, r.top, r.right, r.bottom, op); |
| 158 | break; |
| 159 | } |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | if (clipped) { |
| 163 | flags |= Snapshot::kFlagClipSet; |
| 164 | } |
| 165 | |
| 166 | return clipped; |
| 167 | } |
| 168 | |
| 169 | void Snapshot::setClip(float left, float top, float right, float bottom) { |
| 170 | clipRect->set(left, top, right, bottom); |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 171 | if (!clipRegion->isEmpty()) { |
Romain Guy | 0baaac5 | 2012-08-31 20:31:01 -0700 | [diff] [blame] | 172 | clipRegion->setEmpty(); |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 173 | } |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 174 | flags |= Snapshot::kFlagClipSet; |
| 175 | } |
| 176 | |
Romain Guy | a3dc55f | 2012-09-28 13:55:44 -0700 | [diff] [blame] | 177 | bool Snapshot::hasPerspectiveTransform() const { |
| 178 | return transform->isPerspective(); |
| 179 | } |
| 180 | |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 181 | const Rect& Snapshot::getLocalClip() { |
| 182 | mat4 inverse; |
| 183 | inverse.loadInverse(*transform); |
| 184 | |
| 185 | mLocalClip.set(*clipRect); |
| 186 | inverse.mapRect(mLocalClip); |
| 187 | |
| 188 | return mLocalClip; |
| 189 | } |
| 190 | |
| 191 | void Snapshot::resetClip(float left, float top, float right, float bottom) { |
Romain Guy | 3bbacf2 | 2013-02-06 16:51:04 -0800 | [diff] [blame] | 192 | // TODO: This is incorrect, when we start rendering into a new layer, |
| 193 | // we may have to modify the previous snapshot's clip rect and clip |
| 194 | // region if the previous restore() call did not restore the clip |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 195 | clipRect = &mClipRectRoot; |
Romain Guy | 3c099c4 | 2013-02-06 15:28:04 -0800 | [diff] [blame] | 196 | clipRegion = &mClipRegionRoot; |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 197 | setClip(left, top, right, bottom); |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /////////////////////////////////////////////////////////////////////////////// |
| 201 | // Transforms |
| 202 | /////////////////////////////////////////////////////////////////////////////// |
| 203 | |
| 204 | void Snapshot::resetTransform(float x, float y, float z) { |
| 205 | transform = &mTransformRoot; |
| 206 | transform->loadTranslate(x, y, z); |
| 207 | } |
| 208 | |
| 209 | /////////////////////////////////////////////////////////////////////////////// |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame^] | 210 | // Clipping outline |
| 211 | /////////////////////////////////////////////////////////////////////////////// |
| 212 | |
| 213 | void Snapshot::setClippingOutline(LinearAllocator& allocator, const Outline* outline) { |
| 214 | Rect bounds; |
| 215 | float radius; |
| 216 | if (!outline->getAsRoundRect(&bounds, &radius)) return; // only RR supported |
| 217 | |
| 218 | if (!MathUtils::isPositive(radius)) return; // leave clipping up to rect clipping |
| 219 | |
| 220 | RoundRectClipState* state = new (allocator) RoundRectClipState; |
| 221 | |
| 222 | // store the inverse drawing matrix |
| 223 | Matrix4 outlineDrawingMatrix; |
| 224 | outlineDrawingMatrix.load(getOrthoMatrix()); |
| 225 | outlineDrawingMatrix.multiply(*transform); |
| 226 | state->matrix.loadInverse(outlineDrawingMatrix); |
| 227 | |
| 228 | // compute area under rounded corners - only draws overlapping these rects need to be clipped |
| 229 | for (int i = 0 ; i < 4; i++) { |
| 230 | state->dangerRects[i] = bounds; |
| 231 | } |
| 232 | state->dangerRects[0].bottom = state->dangerRects[1].bottom = bounds.top + radius; |
| 233 | state->dangerRects[0].right = state->dangerRects[2].right = bounds.left + radius; |
| 234 | state->dangerRects[1].left = state->dangerRects[3].left = bounds.right - radius; |
| 235 | state->dangerRects[2].top = state->dangerRects[3].top = bounds.bottom - radius; |
| 236 | for (int i = 0; i < 4; i++) { |
| 237 | transform->mapRect(state->dangerRects[i]); |
| 238 | |
| 239 | // round danger rects out as though they are AA geometry (since they essentially are) |
| 240 | state->dangerRects[i].snapGeometryToPixelBoundaries(true); |
| 241 | } |
| 242 | |
| 243 | // store RR area |
| 244 | bounds.inset(radius); |
| 245 | state->outlineInnerRect = bounds; |
| 246 | state->outlineRadius = radius; |
| 247 | |
| 248 | // store as immutable so, for this frame, pointer uniquely identifies this bundle of shader info |
| 249 | roundRectClipState = state; |
| 250 | } |
| 251 | |
| 252 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 253 | // Queries |
| 254 | /////////////////////////////////////////////////////////////////////////////// |
| 255 | |
| 256 | bool Snapshot::isIgnored() const { |
| 257 | return invisible || empty; |
| 258 | } |
| 259 | |
Chris Craik | 5f80362 | 2013-03-21 14:39:04 -0700 | [diff] [blame] | 260 | void Snapshot::dump() const { |
| 261 | ALOGD("Snapshot %p, flags %x, prev %p, height %d, ignored %d, hasComplexClip %d", |
Chris Craik | a64a2be | 2014-05-14 14:17:01 -0700 | [diff] [blame] | 262 | this, flags, previous.get(), getViewportHeight(), isIgnored(), clipRegion && !clipRegion->isEmpty()); |
Chris Craik | 5f80362 | 2013-03-21 14:39:04 -0700 | [diff] [blame] | 263 | ALOGD(" ClipRect (at %p) %.1f %.1f %.1f %.1f", |
| 264 | clipRect, clipRect->left, clipRect->top, clipRect->right, clipRect->bottom); |
| 265 | ALOGD(" Transform (at %p):", transform); |
| 266 | transform->dump(); |
| 267 | } |
| 268 | |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 269 | }; // namespace uirenderer |
| 270 | }; // namespace android |