blob: 22c7e8a96cc8d6ed983424c0d8a7977d969cbc58 [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
Chris Craik5e00c7c2016-07-06 16:10:09 -070017#pragma once
Tom Hudson984162f2014-10-10 13:38:16 -040018
John Reckd9ee5502015-10-06 10:06:37 -070019#include "Snapshot.h"
20
Tom Hudson984162f2014-10-10 13:38:16 -040021#include <SkMatrix.h>
22#include <SkPath.h>
23#include <SkRegion.h>
24
Tom Hudson984162f2014-10-10 13:38:16 -040025namespace android {
26namespace uirenderer {
27
28/**
29 * Abstract base class for any class containing CanvasState.
30 * Defines three mandatory callbacks.
31 */
32class CanvasStateClient {
33public:
34 CanvasStateClient() { }
35 virtual ~CanvasStateClient() { }
36
37 /**
38 * Callback allowing embedder to take actions in the middle of a
39 * setViewport() call.
40 */
41 virtual void onViewportInitialized() = 0;
42
43 /**
44 * Callback allowing embedder to take actions in the middle of a
45 * restore() call. May be called several times sequentially.
46 */
47 virtual void onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) = 0;
48
49 /**
50 * Allows subclasses to control what value is stored in snapshot's
51 * fbo field in * initializeSaveStack.
52 */
Chris Craik6b109c72015-02-27 10:55:28 -080053 virtual GLuint getTargetFbo() const = 0;
Tom Hudson984162f2014-10-10 13:38:16 -040054
55}; // class CanvasStateClient
56
57/**
58 * Implements Canvas state methods on behalf of Renderers.
59 *
60 * Manages the Snapshot stack, implementing matrix, save/restore, and clipping methods in the
61 * Renderer interface. Drawing and recording classes that include a CanvasState will have
62 * different use cases:
63 *
Chris Craik5e00c7c2016-07-06 16:10:09 -070064 * Drawing code maintaining canvas state (e.g. FrameBuilder) can query attributes (such as
Chris Craikdb663fe2015-04-20 13:34:45 -070065 * transform) or hook into changes (e.g. save/restore) with minimal surface area for manipulating
66 * the stack itself.
Tom Hudson984162f2014-10-10 13:38:16 -040067 *
Chris Craik5e00c7c2016-07-06 16:10:09 -070068 * Recording code maintaining canvas state (e.g. RecordingCanvas) can both record and pass
Chris Craikdb663fe2015-04-20 13:34:45 -070069 * through state operations to CanvasState, so that not only will querying operations work
70 * (getClip/Matrix), but so that quickRejection can also be used.
Tom Hudson984162f2014-10-10 13:38:16 -040071 */
72
Chris Craik64e445b2015-09-02 14:23:49 -070073class CanvasState {
Tom Hudson984162f2014-10-10 13:38:16 -040074public:
Chih-Hung Hsiehfaecb782016-07-21 11:23:06 -070075 explicit CanvasState(CanvasStateClient& renderer);
John Reckd9ee5502015-10-06 10:06:37 -070076 ~CanvasState();
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 Craike4db79d2015-12-22 16:32:23 -080082 void initializeRecordingSaveStack(int viewportWidth, int viewportHeight);
83
84 /**
85 * Initializes the first snapshot, computing the projection matrix,
86 * and stores the dimensions of the render target.
87 */
Chris Craik64e445b2015-09-02 14:23:49 -070088 void initializeSaveStack(int viewportWidth, int viewportHeight,
89 float clipLeft, float clipTop, float clipRight, float clipBottom,
Tom Hudson984162f2014-10-10 13:38:16 -040090 const Vector3& lightCenter);
91
Tom Hudson984162f2014-10-10 13:38:16 -040092 bool hasRectToRectTransform() const {
93 return CC_LIKELY(currentTransform()->rectToRect());
94 }
95
96 // Save (layer)
97 int getSaveCount() const { return mSaveCount; }
98 int save(int flags);
99 void restore();
100 void restoreToCount(int saveCount);
101
102 // Save/Restore without side-effects
103 int saveSnapshot(int flags);
104 void restoreSnapshot();
105
106 // Matrix
107 void getMatrix(SkMatrix* outMatrix) const;
108 void translate(float dx, float dy, float dz = 0.0f);
109 void rotate(float degrees);
110 void scale(float sx, float sy);
111 void skew(float sx, float sy);
112
113 void setMatrix(const SkMatrix& matrix);
114 void setMatrix(const Matrix4& matrix); // internal only convenience method
115 void concatMatrix(const SkMatrix& matrix);
116 void concatMatrix(const Matrix4& matrix); // internal only convenience method
117
118 // Clip
119 const Rect& getLocalClipBounds() const { return mSnapshot->getLocalClip(); }
120 const Rect& getRenderTargetClipBounds() const { return mSnapshot->getRenderTargetClip(); }
121
122 bool quickRejectConservative(float left, float top, float right, float bottom) const;
123
124 bool clipRect(float left, float top, float right, float bottom, SkRegion::Op op);
125 bool clipPath(const SkPath* path, SkRegion::Op op);
126 bool clipRegion(const SkRegion* region, SkRegion::Op op);
127
Tom Hudson984162f2014-10-10 13:38:16 -0400128 /**
129 * Sets a "clipping outline", which is independent from the regular clip.
130 * Currently only supports rectangles or rounded rectangles; passing in a
131 * more complicated outline fails silently. Replaces any previous clipping
132 * outline.
133 */
134 void setClippingOutline(LinearAllocator& allocator, const Outline* outline);
135 void setClippingRoundRect(LinearAllocator& allocator,
Chris Craik5e00c7c2016-07-06 16:10:09 -0700136 const Rect& rect, float radius, bool highPriority = true) {
137 mSnapshot->setClippingRoundRect(allocator, rect, radius, highPriority);
138 }
139 void setProjectionPathMask(const SkPath* path) {
140 mSnapshot->setProjectionPathMask(path);
141 }
Tom Hudson984162f2014-10-10 13:38:16 -0400142
143 /**
144 * Returns true if drawing in the rectangle (left, top, right, bottom)
145 * will be clipped out. Is conservative: might return false when subpixel-
146 * perfect tests would return true.
147 */
148 bool calculateQuickRejectForScissor(float left, float top, float right, float bottom,
149 bool* clipRequired, bool* roundRectClipRequired, bool snapOut) const;
150
Tom Hudson984162f2014-10-10 13:38:16 -0400151 void scaleAlpha(float alpha) { mSnapshot->alpha *= alpha; }
Tom Hudson984162f2014-10-10 13:38:16 -0400152
153 inline const mat4* currentTransform() const { return currentSnapshot()->transform; }
Chris Craik6fe991e52015-10-20 09:39:42 -0700154 inline const Rect& currentRenderTargetClip() const { return currentSnapshot()->getRenderTargetClip(); }
Tom Hudson984162f2014-10-10 13:38:16 -0400155 inline int currentFlags() const { return currentSnapshot()->flags; }
156 const Vector3& currentLightCenter() const { return currentSnapshot()->getRelativeLightCenter(); }
Tom Hudson984162f2014-10-10 13:38:16 -0400157 int getViewportWidth() const { return currentSnapshot()->getViewportWidth(); }
158 int getViewportHeight() const { return currentSnapshot()->getViewportHeight(); }
Chris Craika766cb22015-06-08 16:49:43 -0700159 int getWidth() const { return mWidth; }
160 int getHeight() const { return mHeight; }
161 bool clipIsSimple() const { return currentSnapshot()->clipIsSimple(); }
Tom Hudson984162f2014-10-10 13:38:16 -0400162
John Reckd9ee5502015-10-06 10:06:37 -0700163 inline const Snapshot* currentSnapshot() const { return mSnapshot; }
164 inline Snapshot* writableSnapshot() { return mSnapshot; }
165 inline const Snapshot* firstSnapshot() const { return &mFirstSnapshot; }
Tom Hudson984162f2014-10-10 13:38:16 -0400166
167private:
John Reckd9ee5502015-10-06 10:06:37 -0700168 Snapshot* allocSnapshot(Snapshot* previous, int savecount);
169 void freeSnapshot(Snapshot* snapshot);
170 void freeAllSnapshots();
171
Tom Hudson984162f2014-10-10 13:38:16 -0400172 /// Dimensions of the drawing surface
173 int mWidth, mHeight;
174
175 /// Number of saved states
176 int mSaveCount;
177
178 /// Base state
John Reckd9ee5502015-10-06 10:06:37 -0700179 Snapshot mFirstSnapshot;
Tom Hudson984162f2014-10-10 13:38:16 -0400180
181 /// Host providing callbacks
182 CanvasStateClient& mCanvas;
183
184 /// Current state
John Reckd9ee5502015-10-06 10:06:37 -0700185 Snapshot* mSnapshot;
186
187 // Pool of allocated snapshots to re-use
188 // NOTE: The dtors have already been invoked!
189 Snapshot* mSnapshotPool = nullptr;
190 int mSnapshotPoolCount = 0;
Tom Hudson984162f2014-10-10 13:38:16 -0400191
192}; // class CanvasState
193
194}; // namespace uirenderer
195}; // namespace android