blob: 9fbf2ca821b7d7b344f7d1d6e60f0e2501dbe1c0 [file] [log] [blame]
Chris Craik14e51302013-12-30 15:32:54 -08001/*
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_STATEFUL_BASE_RENDERER_H
18#define ANDROID_HWUI_STATEFUL_BASE_RENDERER_H
19
20#include <utils/RefBase.h>
21
22#include "Renderer.h"
23#include "Snapshot.h"
24
25namespace android {
26namespace uirenderer {
27
28/**
Chris Craikd6b65f62014-01-01 14:45:21 -080029 * Abstract Renderer subclass, which implements Canvas state methods.
Chris Craik14e51302013-12-30 15:32:54 -080030 *
Chris Craikd6b65f62014-01-01 14:45:21 -080031 * Manages the Snapshot stack, implementing matrix, save/restore, and clipping methods in the
32 * Renderer interface. Drawing and recording classes that extend StatefulBaseRenderer will have
33 * different use cases:
34 *
35 * Drawing subclasses (i.e. OpenGLRenderer) can query attributes (such as transform) or hook into
36 * changes (e.g. save/restore) with minimal surface area for manipulating the stack itself.
37 *
38 * Recording subclasses (i.e. DisplayListRenderer) can both record and pass through state operations
39 * to StatefulBaseRenderer, so that not only will querying operations work (getClip/Matrix), but so
40 * that quickRejection can also be used.
Chris Craik14e51302013-12-30 15:32:54 -080041 */
42class StatefulBaseRenderer : public Renderer {
43public:
44 StatefulBaseRenderer();
45
46 virtual status_t prepare(bool opaque) {
47 return prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
48 }
Chris Craika64a2be2014-05-14 14:17:01 -070049
50 /**
51 * Initialize the first snapshot, computing the projection matrix,
52 * and stores the dimensions of the render target.
53 */
Chris Craik14e51302013-12-30 15:32:54 -080054 void initializeViewport(int width, int height);
55 void initializeSaveStack(float clipLeft, float clipTop, float clipRight, float clipBottom);
56
57 // getters
58 bool hasRectToRectTransform() const {
Chris Craikd6b65f62014-01-01 14:45:21 -080059 return CC_LIKELY(currentTransform()->rectToRect());
Chris Craik14e51302013-12-30 15:32:54 -080060 }
61
62 // Save (layer)
63 virtual int getSaveCount() const { return mSaveCount; }
64 virtual int save(int flags);
65 virtual void restore();
66 virtual void restoreToCount(int saveCount);
67 //virtual int saveLayer(float left, float top, float right, float bottom,
68 // int alpha, SkXfermode::Mode mode, int flags);
69
70 // Matrix
71 virtual void getMatrix(SkMatrix* outMatrix) const;
72 virtual void translate(float dx, float dy, float dz = 0.0f);
73 virtual void rotate(float degrees);
74 virtual void scale(float sx, float sy);
75 virtual void skew(float sx, float sy);
76
Chris Craikd218a922014-01-02 17:13:34 -080077 virtual void setMatrix(const SkMatrix* matrix);
Chris Craik14e51302013-12-30 15:32:54 -080078 void setMatrix(const Matrix4& matrix); // internal only convenience method
Chris Craikd218a922014-01-02 17:13:34 -080079 virtual void concatMatrix(const SkMatrix* matrix);
Chris Craik14e51302013-12-30 15:32:54 -080080 void concatMatrix(const Matrix4& matrix); // internal only convenience method
81
82 // Clip
Chris Craik3f0854292014-04-15 16:18:08 -070083 virtual const Rect& getLocalClipBounds() const { return mSnapshot->getLocalClip(); }
84
Chris Craik14e51302013-12-30 15:32:54 -080085 virtual bool quickRejectConservative(float left, float top, float right, float bottom) const;
86
Chris Craikd6b65f62014-01-01 14:45:21 -080087 virtual bool clipRect(float left, float top, float right, float bottom, SkRegion::Op op);
Chris Craikd218a922014-01-02 17:13:34 -080088 virtual bool clipPath(const SkPath* path, SkRegion::Op op);
89 virtual bool clipRegion(const SkRegion* region, SkRegion::Op op);
Chris Craik14e51302013-12-30 15:32:54 -080090
91protected:
Chris Craik3f0854292014-04-15 16:18:08 -070092 const Rect& getRenderTargetClipBounds() const { return mSnapshot->getRenderTargetClip(); }
93
Chris Craik14e51302013-12-30 15:32:54 -080094 int getWidth() { return mWidth; }
95 int getHeight() { return mHeight; }
96
97 // Save
98 int saveSnapshot(int flags);
99 void restoreSnapshot();
100
101 // allows subclasses to control what value is stored in snapshot's fbo field in
102 // initializeSaveStack
103 virtual GLuint getTargetFbo() const {
104 return -1;
105 }
106
107 // Clip
108 bool calculateQuickRejectForScissor(float left, float top, float right, float bottom,
109 bool* clipRequired, bool snapOut) const;
110
111 /**
112 * Called just after a restore has occurred. The 'removed' snapshot popped from the stack,
113 * 'restored' snapshot has become the top/current.
114 *
115 * Subclasses can override this method to handle layer restoration
116 */
117 virtual void onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {};
118
Chris Craikd6b65f62014-01-01 14:45:21 -0800119 inline const Rect* currentClipRect() const {
120 return mSnapshot->clipRect;
Chris Craik14e51302013-12-30 15:32:54 -0800121 }
122
Chris Craikd6b65f62014-01-01 14:45:21 -0800123 inline const mat4* currentTransform() const {
124 return mSnapshot->transform;
Chris Craik14e51302013-12-30 15:32:54 -0800125 }
126
Chris Craikd6b65f62014-01-01 14:45:21 -0800127 inline const Snapshot* currentSnapshot() const {
128 return mSnapshot != NULL ? mSnapshot.get() : mFirstSnapshot.get();
Chris Craik14e51302013-12-30 15:32:54 -0800129 }
130
Chris Craikd6b65f62014-01-01 14:45:21 -0800131 inline const Snapshot* firstSnapshot() const {
132 return mFirstSnapshot.get();
133 }
134
135 // indicites that the clip has been changed since the last time it was consumed
136 bool mDirtyClip;
137
138private:
139 // Dimensions of the drawing surface
140 int mWidth, mHeight;
Chris Craik14e51302013-12-30 15:32:54 -0800141
142 // Number of saved states
143 int mSaveCount;
144
145 // Base state
146 sp<Snapshot> mFirstSnapshot;
147
Chris Craikd6b65f62014-01-01 14:45:21 -0800148protected:
Chris Craik14e51302013-12-30 15:32:54 -0800149 // Current state
Chris Craikd6b65f62014-01-01 14:45:21 -0800150 // TODO: should become private, once hooks needed by OpenGLRenderer are added
Chris Craik14e51302013-12-30 15:32:54 -0800151 sp<Snapshot> mSnapshot;
152
Chris Craik14e51302013-12-30 15:32:54 -0800153}; // class StatefulBaseRenderer
154
155}; // namespace uirenderer
156}; // namespace android
157
158#endif // ANDROID_HWUI_STATEFUL_BASE_RENDERER_H