blob: 923913e864a93efcaf9f4bc5dfe05f0f06cf4bed [file] [log] [blame]
Romain Guyada4d532012-02-02 17:31:16 -08001/*
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
21namespace android {
22namespace uirenderer {
23
24///////////////////////////////////////////////////////////////////////////////
25// Constructors
26///////////////////////////////////////////////////////////////////////////////
27
28Snapshot::Snapshot(): flags(0), previous(NULL), layer(NULL), fbo(0),
Chet Haasedb8c9a62012-03-21 18:54:18 -070029 invisible(false), empty(false), alpha(1.0f) {
Romain Guyada4d532012-02-02 17:31:16 -080030
31 transform = &mTransformRoot;
32 clipRect = &mClipRectRoot;
33 region = NULL;
Romain Guy8ce00302013-01-15 18:51:42 -080034 clipRegion = &mClipRegionRoot;
Romain Guyada4d532012-02-02 17:31:16 -080035}
36
37/**
38 * Copies the specified snapshot/ The specified snapshot is stored as
39 * the previous snapshot.
40 */
41Snapshot::Snapshot(const sp<Snapshot>& s, int saveFlags):
Romain Guy8ce00302013-01-15 18:51:42 -080042 flags(0), previous(s), layer(s->layer), fbo(s->fbo),
Romain Guyada4d532012-02-02 17:31:16 -080043 invisible(s->invisible), empty(false),
Chet Haasedb8c9a62012-03-21 18:54:18 -070044 viewport(s->viewport), height(s->height), alpha(s->alpha) {
Romain Guyada4d532012-02-02 17:31:16 -080045
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 Guy8ce00302013-01-15 18:51:42 -080056 if (!s->clipRegion->isEmpty()) {
Romain Guy0baaac52012-08-31 20:31:01 -070057 mClipRegionRoot.op(*s->clipRegion, SkRegion::kUnion_Op);
Romain Guy967e2bf2012-02-07 17:04:34 -080058 }
Romain Guy8ce00302013-01-15 18:51:42 -080059 clipRegion = &mClipRegionRoot;
Romain Guyada4d532012-02-02 17:31:16 -080060 } else {
61 clipRect = s->clipRect;
Romain Guy967e2bf2012-02-07 17:04:34 -080062 clipRegion = s->clipRegion;
Romain Guyada4d532012-02-02 17:31:16 -080063 }
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 Guy967e2bf2012-02-07 17:04:34 -080077void Snapshot::ensureClipRegion() {
Romain Guy8ce00302013-01-15 18:51:42 -080078 if (clipRegion->isEmpty()) {
Romain Guy0baaac52012-08-31 20:31:01 -070079 clipRegion->setRect(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
Romain Guy967e2bf2012-02-07 17:04:34 -080080 }
Romain Guy967e2bf2012-02-07 17:04:34 -080081}
82
83void Snapshot::copyClipRectFromRegion() {
Romain Guy967e2bf2012-02-07 17:04:34 -080084 if (!clipRegion->isEmpty()) {
Romain Guy0baaac52012-08-31 20:31:01 -070085 const SkIRect& bounds = clipRegion->getBounds();
86 clipRect->set(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom);
Romain Guy967e2bf2012-02-07 17:04:34 -080087
88 if (clipRegion->isRect()) {
Romain Guy0baaac52012-08-31 20:31:01 -070089 clipRegion->setEmpty();
Romain Guy967e2bf2012-02-07 17:04:34 -080090 }
91 } else {
92 clipRect->setEmpty();
Romain Guy967e2bf2012-02-07 17:04:34 -080093 }
Romain Guy967e2bf2012-02-07 17:04:34 -080094}
95
Romain Guy0baaac52012-08-31 20:31:01 -070096bool Snapshot::clipRegionOp(float left, float top, float right, float bottom, SkRegion::Op op) {
Romain Guy0baaac52012-08-31 20:31:01 -070097 SkIRect tmp;
98 tmp.set(left, top, right, bottom);
99 clipRegion->op(tmp, op);
Romain Guy967e2bf2012-02-07 17:04:34 -0800100 copyClipRectFromRegion();
101 return true;
Romain Guy8ce00302013-01-15 18:51:42 -0800102}
103
104bool 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 Guy967e2bf2012-02-07 17:04:34 -0800110}
111
Romain Guyada4d532012-02-02 17:31:16 -0800112bool 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
118bool Snapshot::clipTransformed(const Rect& r, SkRegion::Op op) {
119 bool clipped = false;
120
Romain Guyada4d532012-02-02 17:31:16 -0800121 switch (op) {
Romain Guy967e2bf2012-02-07 17:04:34 -0800122 case SkRegion::kIntersect_Op: {
Romain Guy8ce00302013-01-15 18:51:42 -0800123 if (CC_UNLIKELY(!clipRegion->isEmpty())) {
Romain Guy735738c2012-12-03 12:34:51 -0800124 ensureClipRegion();
Romain Guy0baaac52012-08-31 20:31:01 -0700125 clipped = clipRegionOp(r.left, r.top, r.right, r.bottom, SkRegion::kIntersect_Op);
Romain Guy967e2bf2012-02-07 17:04:34 -0800126 } else {
127 clipped = clipRect->intersect(r);
128 if (!clipped) {
129 clipRect->setEmpty();
130 clipped = true;
131 }
Romain Guyada4d532012-02-02 17:31:16 -0800132 }
133 break;
Romain Guy967e2bf2012-02-07 17:04:34 -0800134 }
Romain Guy967e2bf2012-02-07 17:04:34 -0800135 case SkRegion::kReplace_Op: {
136 setClip(r.left, r.top, r.right, r.bottom);
Romain Guyada4d532012-02-02 17:31:16 -0800137 clipped = true;
138 break;
Romain Guy967e2bf2012-02-07 17:04:34 -0800139 }
Romain Guy0baaac52012-08-31 20:31:01 -0700140 default: {
141 ensureClipRegion();
142 clipped = clipRegionOp(r.left, r.top, r.right, r.bottom, op);
143 break;
144 }
Romain Guyada4d532012-02-02 17:31:16 -0800145 }
146
147 if (clipped) {
148 flags |= Snapshot::kFlagClipSet;
149 }
150
151 return clipped;
152}
153
154void Snapshot::setClip(float left, float top, float right, float bottom) {
155 clipRect->set(left, top, right, bottom);
Romain Guy8ce00302013-01-15 18:51:42 -0800156 if (!clipRegion->isEmpty()) {
Romain Guy0baaac52012-08-31 20:31:01 -0700157 clipRegion->setEmpty();
Romain Guy967e2bf2012-02-07 17:04:34 -0800158 }
Romain Guyada4d532012-02-02 17:31:16 -0800159 flags |= Snapshot::kFlagClipSet;
160}
161
Romain Guya3dc55f2012-09-28 13:55:44 -0700162bool Snapshot::hasPerspectiveTransform() const {
163 return transform->isPerspective();
164}
165
Romain Guyada4d532012-02-02 17:31:16 -0800166const Rect& Snapshot::getLocalClip() {
167 mat4 inverse;
168 inverse.loadInverse(*transform);
169
170 mLocalClip.set(*clipRect);
171 inverse.mapRect(mLocalClip);
172
173 return mLocalClip;
174}
175
176void Snapshot::resetClip(float left, float top, float right, float bottom) {
Romain Guy3bbacf22013-02-06 16:51:04 -0800177 // TODO: This is incorrect, when we start rendering into a new layer,
178 // we may have to modify the previous snapshot's clip rect and clip
179 // region if the previous restore() call did not restore the clip
Romain Guyada4d532012-02-02 17:31:16 -0800180 clipRect = &mClipRectRoot;
Romain Guy3c099c42013-02-06 15:28:04 -0800181 clipRegion = &mClipRegionRoot;
Romain Guy967e2bf2012-02-07 17:04:34 -0800182 setClip(left, top, right, bottom);
Romain Guyada4d532012-02-02 17:31:16 -0800183}
184
185///////////////////////////////////////////////////////////////////////////////
186// Transforms
187///////////////////////////////////////////////////////////////////////////////
188
189void Snapshot::resetTransform(float x, float y, float z) {
190 transform = &mTransformRoot;
191 transform->loadTranslate(x, y, z);
192}
193
194///////////////////////////////////////////////////////////////////////////////
195// Queries
196///////////////////////////////////////////////////////////////////////////////
197
198bool Snapshot::isIgnored() const {
199 return invisible || empty;
200}
201
202}; // namespace uirenderer
203}; // namespace android