blob: 6d83b4ca3dd856001103cd6f82afeba74e594665 [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 /**
Chris Craik797b95b22014-05-20 18:10:25 -070051 * Initialize the first snapshot, computing the projection matrix, and stores the dimensions of
52 * the render target.
Chris Craika64a2be2014-05-14 14:17:01 -070053 */
Chris Craik797b95b22014-05-20 18:10:25 -070054 virtual void setViewport(int width, int height);
Chris Craik14e51302013-12-30 15:32:54 -080055 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
Chris Craik05f3d6e2014-06-02 16:27:04 -070071 void getMatrix(Matrix4* outMatrix) const;
Chris Craik14e51302013-12-30 15:32:54 -080072 virtual void getMatrix(SkMatrix* outMatrix) const;
73 virtual void translate(float dx, float dy, float dz = 0.0f);
74 virtual void rotate(float degrees);
75 virtual void scale(float sx, float sy);
76 virtual void skew(float sx, float sy);
77
Derek Sollenberger13908822013-12-10 12:28:58 -050078 virtual void setMatrix(const SkMatrix& matrix);
Chris Craik14e51302013-12-30 15:32:54 -080079 void setMatrix(const Matrix4& matrix); // internal only convenience method
Derek Sollenberger13908822013-12-10 12:28:58 -050080 virtual void concatMatrix(const SkMatrix& matrix);
Chris Craik14e51302013-12-30 15:32:54 -080081 void concatMatrix(const Matrix4& matrix); // internal only convenience method
82
83 // Clip
Chris Craik3f0854292014-04-15 16:18:08 -070084 virtual const Rect& getLocalClipBounds() const { return mSnapshot->getLocalClip(); }
85
Chris Craik14e51302013-12-30 15:32:54 -080086 virtual bool quickRejectConservative(float left, float top, float right, float bottom) const;
87
Chris Craikd6b65f62014-01-01 14:45:21 -080088 virtual bool clipRect(float left, float top, float right, float bottom, SkRegion::Op op);
Chris Craikd218a922014-01-02 17:13:34 -080089 virtual bool clipPath(const SkPath* path, SkRegion::Op op);
90 virtual bool clipRegion(const SkRegion* region, SkRegion::Op op);
Chris Craik14e51302013-12-30 15:32:54 -080091
Chris Craikdeeda3d2014-05-05 19:09:33 -070092 /**
93 * Does not support different clipping Ops (that is, every call to setClippingOutline is
94 * effectively using SkRegion::kReplaceOp)
95 *
96 * The clipping outline is independent from the regular clip.
97 */
98 void setClippingOutline(LinearAllocator& allocator, const Outline* outline);
Chris Craikaf4d04c2014-07-29 12:50:14 -070099 void setClippingRoundRect(LinearAllocator& allocator,
100 const Rect& rect, float radius);
Chris Craikdeeda3d2014-05-05 19:09:33 -0700101
Chris Craik14e51302013-12-30 15:32:54 -0800102protected:
Chris Craik3f0854292014-04-15 16:18:08 -0700103 const Rect& getRenderTargetClipBounds() const { return mSnapshot->getRenderTargetClip(); }
104
Chris Craik14e51302013-12-30 15:32:54 -0800105 int getWidth() { return mWidth; }
106 int getHeight() { return mHeight; }
107
108 // Save
109 int saveSnapshot(int flags);
110 void restoreSnapshot();
111
112 // allows subclasses to control what value is stored in snapshot's fbo field in
113 // initializeSaveStack
114 virtual GLuint getTargetFbo() const {
115 return -1;
116 }
117
118 // Clip
119 bool calculateQuickRejectForScissor(float left, float top, float right, float bottom,
Chris Craikdeeda3d2014-05-05 19:09:33 -0700120 bool* clipRequired, bool* roundRectClipRequired, bool snapOut) const;
Chris Craik14e51302013-12-30 15:32:54 -0800121
122 /**
123 * Called just after a restore has occurred. The 'removed' snapshot popped from the stack,
124 * 'restored' snapshot has become the top/current.
125 *
126 * Subclasses can override this method to handle layer restoration
127 */
128 virtual void onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {};
129
Chris Craik797b95b22014-05-20 18:10:25 -0700130 virtual void onViewportInitialized() {};
131
Chris Craikd6b65f62014-01-01 14:45:21 -0800132 inline const Rect* currentClipRect() const {
133 return mSnapshot->clipRect;
Chris Craik14e51302013-12-30 15:32:54 -0800134 }
135
Chris Craikd6b65f62014-01-01 14:45:21 -0800136 inline const mat4* currentTransform() const {
137 return mSnapshot->transform;
Chris Craik14e51302013-12-30 15:32:54 -0800138 }
139
Chris Craikd6b65f62014-01-01 14:45:21 -0800140 inline const Snapshot* currentSnapshot() const {
141 return mSnapshot != NULL ? mSnapshot.get() : mFirstSnapshot.get();
Chris Craik14e51302013-12-30 15:32:54 -0800142 }
143
Chris Craikd6b65f62014-01-01 14:45:21 -0800144 inline const Snapshot* firstSnapshot() const {
145 return mFirstSnapshot.get();
146 }
147
148 // indicites that the clip has been changed since the last time it was consumed
149 bool mDirtyClip;
150
151private:
152 // Dimensions of the drawing surface
153 int mWidth, mHeight;
Chris Craik14e51302013-12-30 15:32:54 -0800154
155 // Number of saved states
156 int mSaveCount;
157
158 // Base state
159 sp<Snapshot> mFirstSnapshot;
160
Chris Craikd6b65f62014-01-01 14:45:21 -0800161protected:
Chris Craik14e51302013-12-30 15:32:54 -0800162 // Current state
Chris Craikd6b65f62014-01-01 14:45:21 -0800163 // TODO: should become private, once hooks needed by OpenGLRenderer are added
Chris Craik14e51302013-12-30 15:32:54 -0800164 sp<Snapshot> mSnapshot;
Chris Craik14e51302013-12-30 15:32:54 -0800165}; // class StatefulBaseRenderer
166
167}; // namespace uirenderer
168}; // namespace android
169
170#endif // ANDROID_HWUI_STATEFUL_BASE_RENDERER_H