blob: f0fb9ba8b32469f410252fe9be71f6ad77e1427c [file] [log] [blame]
Tom Hudson984162f2014-10-10 13:38:16 -04001/*
2 * Copyright (C) 2014 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#ifndef ANDROID_HWUI_CANVAS_STATE_H
18#define ANDROID_HWUI_CANVAS_STATE_H
19
20#include <SkMatrix.h>
21#include <SkPath.h>
22#include <SkRegion.h>
23
24#include "Snapshot.h"
25
26namespace android {
27namespace uirenderer {
28
29/**
30 * Abstract base class for any class containing CanvasState.
31 * Defines three mandatory callbacks.
32 */
33class CanvasStateClient {
34public:
35 CanvasStateClient() { }
36 virtual ~CanvasStateClient() { }
37
38 /**
39 * Callback allowing embedder to take actions in the middle of a
40 * setViewport() call.
41 */
42 virtual void onViewportInitialized() = 0;
43
44 /**
45 * Callback allowing embedder to take actions in the middle of a
46 * restore() call. May be called several times sequentially.
47 */
48 virtual void onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) = 0;
49
50 /**
51 * Allows subclasses to control what value is stored in snapshot's
52 * fbo field in * initializeSaveStack.
53 */
Chris Craik6b109c72015-02-27 10:55:28 -080054 virtual GLuint getTargetFbo() const = 0;
Tom Hudson984162f2014-10-10 13:38:16 -040055
56}; // class CanvasStateClient
57
58/**
59 * Implements Canvas state methods on behalf of Renderers.
60 *
61 * Manages the Snapshot stack, implementing matrix, save/restore, and clipping methods in the
62 * Renderer interface. Drawing and recording classes that include a CanvasState will have
63 * different use cases:
64 *
Chris Craikdb663fe2015-04-20 13:34:45 -070065 * Drawing code maintaining canvas state (i.e. OpenGLRenderer) can query attributes (such as
66 * transform) or hook into changes (e.g. save/restore) with minimal surface area for manipulating
67 * the stack itself.
Tom Hudson984162f2014-10-10 13:38:16 -040068 *
Chris Craikdb663fe2015-04-20 13:34:45 -070069 * Recording code maintaining canvas state (i.e. DisplayListCanvas) can both record and pass
70 * through state operations to CanvasState, so that not only will querying operations work
71 * (getClip/Matrix), but so that quickRejection can also be used.
Tom Hudson984162f2014-10-10 13:38:16 -040072 */
73
Chris Craik64e445b2015-09-02 14:23:49 -070074class CanvasState {
Tom Hudson984162f2014-10-10 13:38:16 -040075public:
76 CanvasState(CanvasStateClient& renderer);
Tom Hudson984162f2014-10-10 13:38:16 -040077
78 /**
79 * Initializes the first snapshot, computing the projection matrix,
80 * and stores the dimensions of the render target.
81 */
Chris Craik64e445b2015-09-02 14:23:49 -070082 void initializeSaveStack(int viewportWidth, int viewportHeight,
83 float clipLeft, float clipTop, float clipRight, float clipBottom,
Tom Hudson984162f2014-10-10 13:38:16 -040084 const Vector3& lightCenter);
85
Tom Hudson984162f2014-10-10 13:38:16 -040086 bool hasRectToRectTransform() const {
87 return CC_LIKELY(currentTransform()->rectToRect());
88 }
89
90 // Save (layer)
91 int getSaveCount() const { return mSaveCount; }
92 int save(int flags);
93 void restore();
94 void restoreToCount(int saveCount);
95
96 // Save/Restore without side-effects
97 int saveSnapshot(int flags);
98 void restoreSnapshot();
99
100 // Matrix
101 void getMatrix(SkMatrix* outMatrix) const;
102 void translate(float dx, float dy, float dz = 0.0f);
103 void rotate(float degrees);
104 void scale(float sx, float sy);
105 void skew(float sx, float sy);
106
107 void setMatrix(const SkMatrix& matrix);
108 void setMatrix(const Matrix4& matrix); // internal only convenience method
109 void concatMatrix(const SkMatrix& matrix);
110 void concatMatrix(const Matrix4& matrix); // internal only convenience method
111
112 // Clip
113 const Rect& getLocalClipBounds() const { return mSnapshot->getLocalClip(); }
114 const Rect& getRenderTargetClipBounds() const { return mSnapshot->getRenderTargetClip(); }
115
116 bool quickRejectConservative(float left, float top, float right, float bottom) const;
117
118 bool clipRect(float left, float top, float right, float bottom, SkRegion::Op op);
119 bool clipPath(const SkPath* path, SkRegion::Op op);
120 bool clipRegion(const SkRegion* region, SkRegion::Op op);
121
Tom Hudson984162f2014-10-10 13:38:16 -0400122 /**
123 * Sets a "clipping outline", which is independent from the regular clip.
124 * Currently only supports rectangles or rounded rectangles; passing in a
125 * more complicated outline fails silently. Replaces any previous clipping
126 * outline.
127 */
128 void setClippingOutline(LinearAllocator& allocator, const Outline* outline);
129 void setClippingRoundRect(LinearAllocator& allocator,
130 const Rect& rect, float radius, bool highPriority = true);
Chris Craikfca52b752015-04-28 11:45:59 -0700131 void setProjectionPathMask(LinearAllocator& allocator, const SkPath* path);
Tom Hudson984162f2014-10-10 13:38:16 -0400132
133 /**
134 * Returns true if drawing in the rectangle (left, top, right, bottom)
135 * will be clipped out. Is conservative: might return false when subpixel-
136 * perfect tests would return true.
137 */
138 bool calculateQuickRejectForScissor(float left, float top, float right, float bottom,
139 bool* clipRequired, bool* roundRectClipRequired, bool snapOut) const;
140
141 void setDirtyClip(bool opaque) { mDirtyClip = opaque; }
142 bool getDirtyClip() const { return mDirtyClip; }
143
144 void scaleAlpha(float alpha) { mSnapshot->alpha *= alpha; }
145 void setEmpty(bool value) { mSnapshot->empty = value; }
146 void setInvisible(bool value) { mSnapshot->invisible = value; }
147
148 inline const mat4* currentTransform() const { return currentSnapshot()->transform; }
Rob Tsuk487a92c2015-01-06 13:22:54 -0800149 inline const Rect& currentClipRect() const { return currentSnapshot()->getClipRect(); }
Tom Hudson984162f2014-10-10 13:38:16 -0400150 inline Region* currentRegion() const { return currentSnapshot()->region; }
151 inline int currentFlags() const { return currentSnapshot()->flags; }
152 const Vector3& currentLightCenter() const { return currentSnapshot()->getRelativeLightCenter(); }
153 inline bool currentlyIgnored() const { return currentSnapshot()->isIgnored(); }
154 int getViewportWidth() const { return currentSnapshot()->getViewportWidth(); }
155 int getViewportHeight() const { return currentSnapshot()->getViewportHeight(); }
Chris Craika766cb22015-06-08 16:49:43 -0700156 int getWidth() const { return mWidth; }
157 int getHeight() const { return mHeight; }
158 bool clipIsSimple() const { return currentSnapshot()->clipIsSimple(); }
Tom Hudson984162f2014-10-10 13:38:16 -0400159
Chris Craik64e445b2015-09-02 14:23:49 -0700160 inline const Snapshot* currentSnapshot() const { return mSnapshot.get(); }
Tom Hudson984162f2014-10-10 13:38:16 -0400161 inline Snapshot* writableSnapshot() { return mSnapshot.get(); }
162 inline const Snapshot* firstSnapshot() const { return mFirstSnapshot.get(); }
163
164private:
Tom Hudson984162f2014-10-10 13:38:16 -0400165 /// indicates that the clip has been changed since the last time it was consumed
166 bool mDirtyClip;
167
168 /// Dimensions of the drawing surface
169 int mWidth, mHeight;
170
171 /// Number of saved states
172 int mSaveCount;
173
174 /// Base state
175 sp<Snapshot> mFirstSnapshot;
176
177 /// Host providing callbacks
178 CanvasStateClient& mCanvas;
179
180 /// Current state
181 sp<Snapshot> mSnapshot;
182
183}; // class CanvasState
184
185}; // namespace uirenderer
186}; // namespace android
187
188#endif // ANDROID_HWUI_CANVAS_STATE_H