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