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 | |
| 17 | #include "Snapshot.h" |
| 18 | |
| 19 | #include <SkCanvas.h> |
| 20 | |
| 21 | namespace android { |
| 22 | namespace uirenderer { |
| 23 | |
| 24 | /////////////////////////////////////////////////////////////////////////////// |
| 25 | // Constructors |
| 26 | /////////////////////////////////////////////////////////////////////////////// |
| 27 | |
| 28 | Snapshot::Snapshot(): flags(0), previous(NULL), layer(NULL), fbo(0), |
Chet Haase | db8c9a6 | 2012-03-21 18:54:18 -0700 | [diff] [blame] | 29 | invisible(false), empty(false), alpha(1.0f) { |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 30 | |
| 31 | transform = &mTransformRoot; |
| 32 | clipRect = &mClipRectRoot; |
| 33 | region = NULL; |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 34 | clipRegion = &mClipRegionRoot; |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Copies the specified snapshot/ The specified snapshot is stored as |
| 39 | * the previous snapshot. |
| 40 | */ |
| 41 | Snapshot::Snapshot(const sp<Snapshot>& s, int saveFlags): |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 42 | flags(0), previous(s), layer(s->layer), fbo(s->fbo), |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 43 | invisible(s->invisible), empty(false), |
Chet Haase | db8c9a6 | 2012-03-21 18:54:18 -0700 | [diff] [blame] | 44 | viewport(s->viewport), height(s->height), alpha(s->alpha) { |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 45 | |
| 46 | if (saveFlags & SkCanvas::kMatrix_SaveFlag) { |
| 47 | mTransformRoot.load(*s->transform); |
| 48 | transform = &mTransformRoot; |
| 49 | } else { |
| 50 | transform = s->transform; |
| 51 | } |
| 52 | |
| 53 | if (saveFlags & SkCanvas::kClip_SaveFlag) { |
| 54 | mClipRectRoot.set(*s->clipRect); |
| 55 | clipRect = &mClipRectRoot; |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 56 | if (!s->clipRegion->isEmpty()) { |
Romain Guy | 0baaac5 | 2012-08-31 20:31:01 -0700 | [diff] [blame] | 57 | mClipRegionRoot.op(*s->clipRegion, SkRegion::kUnion_Op); |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 58 | } |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 59 | clipRegion = &mClipRegionRoot; |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 60 | } else { |
| 61 | clipRect = s->clipRect; |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 62 | clipRegion = s->clipRegion; |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | if (s->flags & Snapshot::kFlagFboTarget) { |
| 66 | flags |= Snapshot::kFlagFboTarget; |
| 67 | region = s->region; |
| 68 | } else { |
| 69 | region = NULL; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /////////////////////////////////////////////////////////////////////////////// |
| 74 | // Clipping |
| 75 | /////////////////////////////////////////////////////////////////////////////// |
| 76 | |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 77 | void Snapshot::ensureClipRegion() { |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 78 | if (clipRegion->isEmpty()) { |
Romain Guy | 0baaac5 | 2012-08-31 20:31:01 -0700 | [diff] [blame] | 79 | clipRegion->setRect(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom); |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 80 | } |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | void Snapshot::copyClipRectFromRegion() { |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 84 | if (!clipRegion->isEmpty()) { |
Romain Guy | 0baaac5 | 2012-08-31 20:31:01 -0700 | [diff] [blame] | 85 | const SkIRect& bounds = clipRegion->getBounds(); |
| 86 | clipRect->set(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom); |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 87 | |
| 88 | if (clipRegion->isRect()) { |
Romain Guy | 0baaac5 | 2012-08-31 20:31:01 -0700 | [diff] [blame] | 89 | clipRegion->setEmpty(); |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 90 | } |
| 91 | } else { |
| 92 | clipRect->setEmpty(); |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 93 | } |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Romain Guy | 0baaac5 | 2012-08-31 20:31:01 -0700 | [diff] [blame] | 96 | 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] | 97 | SkIRect tmp; |
| 98 | tmp.set(left, top, right, bottom); |
| 99 | clipRegion->op(tmp, op); |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 100 | copyClipRectFromRegion(); |
| 101 | return true; |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | bool Snapshot::clipRegionTransformed(const SkRegion& region, SkRegion::Op op) { |
| 105 | ensureClipRegion(); |
| 106 | clipRegion->op(region, op); |
| 107 | copyClipRectFromRegion(); |
| 108 | flags |= Snapshot::kFlagClipSet; |
| 109 | return true; |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 112 | bool Snapshot::clip(float left, float top, float right, float bottom, SkRegion::Op op) { |
| 113 | Rect r(left, top, right, bottom); |
| 114 | transform->mapRect(r); |
| 115 | return clipTransformed(r, op); |
| 116 | } |
| 117 | |
| 118 | bool Snapshot::clipTransformed(const Rect& r, SkRegion::Op op) { |
| 119 | bool clipped = false; |
| 120 | |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 121 | switch (op) { |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 122 | case SkRegion::kIntersect_Op: { |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 123 | if (CC_UNLIKELY(!clipRegion->isEmpty())) { |
Romain Guy | 735738c | 2012-12-03 12:34:51 -0800 | [diff] [blame] | 124 | ensureClipRegion(); |
Romain Guy | 0baaac5 | 2012-08-31 20:31:01 -0700 | [diff] [blame] | 125 | 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] | 126 | } else { |
| 127 | clipped = clipRect->intersect(r); |
| 128 | if (!clipped) { |
| 129 | clipRect->setEmpty(); |
| 130 | clipped = true; |
| 131 | } |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 132 | } |
| 133 | break; |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 134 | } |
| 135 | case SkRegion::kUnion_Op: { |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 136 | if (CC_UNLIKELY(!clipRegion->isEmpty())) { |
Romain Guy | 735738c | 2012-12-03 12:34:51 -0800 | [diff] [blame] | 137 | ensureClipRegion(); |
Romain Guy | 0baaac5 | 2012-08-31 20:31:01 -0700 | [diff] [blame] | 138 | clipped = clipRegionOp(r.left, r.top, r.right, r.bottom, SkRegion::kUnion_Op); |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 139 | } else { |
| 140 | clipped = clipRect->unionWith(r); |
| 141 | } |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 142 | break; |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 143 | } |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 144 | case SkRegion::kReplace_Op: { |
| 145 | setClip(r.left, r.top, r.right, r.bottom); |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 146 | clipped = true; |
| 147 | break; |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 148 | } |
Romain Guy | 0baaac5 | 2012-08-31 20:31:01 -0700 | [diff] [blame] | 149 | default: { |
| 150 | ensureClipRegion(); |
| 151 | clipped = clipRegionOp(r.left, r.top, r.right, r.bottom, op); |
| 152 | break; |
| 153 | } |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | if (clipped) { |
| 157 | flags |= Snapshot::kFlagClipSet; |
| 158 | } |
| 159 | |
| 160 | return clipped; |
| 161 | } |
| 162 | |
| 163 | void Snapshot::setClip(float left, float top, float right, float bottom) { |
| 164 | clipRect->set(left, top, right, bottom); |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 165 | if (!clipRegion->isEmpty()) { |
Romain Guy | 0baaac5 | 2012-08-31 20:31:01 -0700 | [diff] [blame] | 166 | clipRegion->setEmpty(); |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 167 | } |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 168 | flags |= Snapshot::kFlagClipSet; |
| 169 | } |
| 170 | |
Romain Guy | a3dc55f | 2012-09-28 13:55:44 -0700 | [diff] [blame] | 171 | bool Snapshot::hasPerspectiveTransform() const { |
| 172 | return transform->isPerspective(); |
| 173 | } |
| 174 | |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 175 | const Rect& Snapshot::getLocalClip() { |
| 176 | mat4 inverse; |
| 177 | inverse.loadInverse(*transform); |
| 178 | |
| 179 | mLocalClip.set(*clipRect); |
| 180 | inverse.mapRect(mLocalClip); |
| 181 | |
| 182 | return mLocalClip; |
| 183 | } |
| 184 | |
| 185 | void Snapshot::resetClip(float left, float top, float right, float bottom) { |
Romain Guy | 3bbacf2 | 2013-02-06 16:51:04 -0800 | [diff] [blame^] | 186 | // TODO: This is incorrect, when we start rendering into a new layer, |
| 187 | // we may have to modify the previous snapshot's clip rect and clip |
| 188 | // region if the previous restore() call did not restore the clip |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 189 | clipRect = &mClipRectRoot; |
Romain Guy | 3c099c4 | 2013-02-06 15:28:04 -0800 | [diff] [blame] | 190 | clipRegion = &mClipRegionRoot; |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 191 | setClip(left, top, right, bottom); |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | /////////////////////////////////////////////////////////////////////////////// |
| 195 | // Transforms |
| 196 | /////////////////////////////////////////////////////////////////////////////// |
| 197 | |
| 198 | void Snapshot::resetTransform(float x, float y, float z) { |
| 199 | transform = &mTransformRoot; |
| 200 | transform->loadTranslate(x, y, z); |
| 201 | } |
| 202 | |
| 203 | /////////////////////////////////////////////////////////////////////////////// |
| 204 | // Queries |
| 205 | /////////////////////////////////////////////////////////////////////////////// |
| 206 | |
| 207 | bool Snapshot::isIgnored() const { |
| 208 | return invisible || empty; |
| 209 | } |
| 210 | |
| 211 | }; // namespace uirenderer |
| 212 | }; // namespace android |