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