blob: 9ac35ff47dabe92517d06d71d58380f5993dad54 [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
Mike Reed6e49c9f2016-12-02 15:36:59 -050021#include <SkClipOp.h>
Tom Hudson984162f2014-10-10 13:38:16 -040022#include <SkMatrix.h>
23#include <SkPath.h>
24#include <SkRegion.h>
25
Tom Hudson984162f2014-10-10 13:38:16 -040026namespace android {
27namespace uirenderer {
28
29/**
30 * Abstract base class for any class containing CanvasState.
31 * Defines three mandatory callbacks.
32 */
33class CanvasStateClient {
34public:
John Reck1bcacfd2017-11-03 10:12:19 -070035 CanvasStateClient() {}
36 virtual ~CanvasStateClient() {}
Tom Hudson984162f2014-10-10 13:38:16 -040037
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
John Reck1bcacfd2017-11-03 10:12:19 -070056}; // class CanvasStateClient
Tom Hudson984162f2014-10-10 13:38:16 -040057
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 Craik5e00c7c2016-07-06 16:10:09 -070065 * Drawing code maintaining canvas state (e.g. FrameBuilder) can query attributes (such as
Chris Craikdb663fe2015-04-20 13:34:45 -070066 * 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 Craik5e00c7c2016-07-06 16:10:09 -070069 * Recording code maintaining canvas state (e.g. RecordingCanvas) can both record and pass
Chris Craikdb663fe2015-04-20 13:34:45 -070070 * 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:
Chih-Hung Hsiehfaecb782016-07-21 11:23:06 -070076 explicit CanvasState(CanvasStateClient& renderer);
John Reckd9ee5502015-10-06 10:06:37 -070077 ~CanvasState();
Tom Hudson984162f2014-10-10 13:38:16 -040078
79 /**
80 * Initializes the first snapshot, computing the projection matrix,
81 * and stores the dimensions of the render target.
82 */
Chris Craike4db79d2015-12-22 16:32:23 -080083 void initializeRecordingSaveStack(int viewportWidth, int viewportHeight);
84
85 /**
86 * Initializes the first snapshot, computing the projection matrix,
87 * and stores the dimensions of the render target.
88 */
John Reck1bcacfd2017-11-03 10:12:19 -070089 void initializeSaveStack(int viewportWidth, int viewportHeight, float clipLeft, float clipTop,
90 float clipRight, float clipBottom, const Vector3& lightCenter);
Tom Hudson984162f2014-10-10 13:38:16 -040091
John Reck1bcacfd2017-11-03 10:12:19 -070092 bool hasRectToRectTransform() const { return CC_LIKELY(currentTransform()->rectToRect()); }
Tom Hudson984162f2014-10-10 13:38:16 -040093
94 // Save (layer)
95 int getSaveCount() const { return mSaveCount; }
96 int save(int flags);
97 void restore();
98 void restoreToCount(int saveCount);
99
100 // Save/Restore without side-effects
101 int saveSnapshot(int flags);
102 void restoreSnapshot();
103
104 // Matrix
105 void getMatrix(SkMatrix* outMatrix) const;
106 void translate(float dx, float dy, float dz = 0.0f);
107 void rotate(float degrees);
108 void scale(float sx, float sy);
109 void skew(float sx, float sy);
110
111 void setMatrix(const SkMatrix& matrix);
John Reck1bcacfd2017-11-03 10:12:19 -0700112 void setMatrix(const Matrix4& matrix); // internal only convenience method
Tom Hudson984162f2014-10-10 13:38:16 -0400113 void concatMatrix(const SkMatrix& matrix);
John Reck1bcacfd2017-11-03 10:12:19 -0700114 void concatMatrix(const Matrix4& matrix); // internal only convenience method
Tom Hudson984162f2014-10-10 13:38:16 -0400115
116 // Clip
117 const Rect& getLocalClipBounds() const { return mSnapshot->getLocalClip(); }
118 const Rect& getRenderTargetClipBounds() const { return mSnapshot->getRenderTargetClip(); }
119
120 bool quickRejectConservative(float left, float top, float right, float bottom) const;
121
Mike Reed6e49c9f2016-12-02 15:36:59 -0500122 bool clipRect(float left, float top, float right, float bottom, SkClipOp op);
123 bool clipPath(const SkPath* path, SkClipOp op);
Tom Hudson984162f2014-10-10 13:38:16 -0400124
Tom Hudson984162f2014-10-10 13:38:16 -0400125 /**
126 * Sets a "clipping outline", which is independent from the regular clip.
127 * Currently only supports rectangles or rounded rectangles; passing in a
128 * more complicated outline fails silently. Replaces any previous clipping
129 * outline.
130 */
131 void setClippingOutline(LinearAllocator& allocator, const Outline* outline);
John Reck1bcacfd2017-11-03 10:12:19 -0700132 void setClippingRoundRect(LinearAllocator& allocator, const Rect& rect, float radius,
133 bool highPriority = true) {
Chris Craik5e00c7c2016-07-06 16:10:09 -0700134 mSnapshot->setClippingRoundRect(allocator, rect, radius, highPriority);
135 }
John Reck1bcacfd2017-11-03 10:12:19 -0700136 void setProjectionPathMask(const SkPath* path) { mSnapshot->setProjectionPathMask(path); }
Tom Hudson984162f2014-10-10 13:38:16 -0400137
138 /**
139 * Returns true if drawing in the rectangle (left, top, right, bottom)
140 * will be clipped out. Is conservative: might return false when subpixel-
141 * perfect tests would return true.
142 */
143 bool calculateQuickRejectForScissor(float left, float top, float right, float bottom,
John Reck1bcacfd2017-11-03 10:12:19 -0700144 bool* clipRequired, bool* roundRectClipRequired,
145 bool snapOut) const;
Tom Hudson984162f2014-10-10 13:38:16 -0400146
Tom Hudson984162f2014-10-10 13:38:16 -0400147 void scaleAlpha(float alpha) { mSnapshot->alpha *= alpha; }
Tom Hudson984162f2014-10-10 13:38:16 -0400148
149 inline const mat4* currentTransform() const { return currentSnapshot()->transform; }
John Reck1bcacfd2017-11-03 10:12:19 -0700150 inline const Rect& currentRenderTargetClip() const {
151 return currentSnapshot()->getRenderTargetClip();
152 }
Tom Hudson984162f2014-10-10 13:38:16 -0400153 inline int currentFlags() const { return currentSnapshot()->flags; }
John Reck1bcacfd2017-11-03 10:12:19 -0700154 const Vector3& currentLightCenter() const {
155 return currentSnapshot()->getRelativeLightCenter();
156 }
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
John Reck1bcacfd2017-11-03 10:12:19 -0700192}; // class CanvasState
Tom Hudson984162f2014-10-10 13:38:16 -0400193
John Reck1bcacfd2017-11-03 10:12:19 -0700194}; // namespace uirenderer
195}; // namespace android