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