Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 25 | namespace android { |
| 26 | namespace uirenderer { |
| 27 | |
| 28 | /** |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 29 | * Abstract Renderer subclass, which implements Canvas state methods. |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 30 | * |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 31 | * 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 Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 41 | */ |
| 42 | class StatefulBaseRenderer : public Renderer { |
| 43 | public: |
| 44 | StatefulBaseRenderer(); |
| 45 | |
| 46 | virtual status_t prepare(bool opaque) { |
| 47 | return prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque); |
| 48 | } |
Chris Craik | a64a2be | 2014-05-14 14:17:01 -0700 | [diff] [blame] | 49 | |
| 50 | /** |
Chris Craik | 797b95b | 2014-05-20 18:10:25 -0700 | [diff] [blame] | 51 | * Initialize the first snapshot, computing the projection matrix, and stores the dimensions of |
| 52 | * the render target. |
Chris Craik | a64a2be | 2014-05-14 14:17:01 -0700 | [diff] [blame] | 53 | */ |
Chris Craik | 797b95b | 2014-05-20 18:10:25 -0700 | [diff] [blame] | 54 | virtual void setViewport(int width, int height); |
| 55 | virtual void initializeLight(const Vector3& lightCenter, float lightRadius); |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 56 | void initializeSaveStack(float clipLeft, float clipTop, float clipRight, float clipBottom); |
| 57 | |
| 58 | // getters |
| 59 | bool hasRectToRectTransform() const { |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 60 | return CC_LIKELY(currentTransform()->rectToRect()); |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | // Save (layer) |
| 64 | virtual int getSaveCount() const { return mSaveCount; } |
| 65 | virtual int save(int flags); |
| 66 | virtual void restore(); |
| 67 | virtual void restoreToCount(int saveCount); |
| 68 | //virtual int saveLayer(float left, float top, float right, float bottom, |
| 69 | // int alpha, SkXfermode::Mode mode, int flags); |
| 70 | |
| 71 | // Matrix |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 72 | void getMatrix(Matrix4* outMatrix) const; |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 73 | virtual void getMatrix(SkMatrix* outMatrix) const; |
| 74 | virtual void translate(float dx, float dy, float dz = 0.0f); |
| 75 | virtual void rotate(float degrees); |
| 76 | virtual void scale(float sx, float sy); |
| 77 | virtual void skew(float sx, float sy); |
| 78 | |
Derek Sollenberger | 1390882 | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 79 | virtual void setMatrix(const SkMatrix& matrix); |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 80 | void setMatrix(const Matrix4& matrix); // internal only convenience method |
Derek Sollenberger | 1390882 | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 81 | virtual void concatMatrix(const SkMatrix& matrix); |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 82 | void concatMatrix(const Matrix4& matrix); // internal only convenience method |
| 83 | |
| 84 | // Clip |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 85 | virtual const Rect& getLocalClipBounds() const { return mSnapshot->getLocalClip(); } |
| 86 | |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 87 | virtual bool quickRejectConservative(float left, float top, float right, float bottom) const; |
| 88 | |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 89 | virtual bool clipRect(float left, float top, float right, float bottom, SkRegion::Op op); |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 90 | virtual bool clipPath(const SkPath* path, SkRegion::Op op); |
| 91 | virtual bool clipRegion(const SkRegion* region, SkRegion::Op op); |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 92 | |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 93 | /** |
| 94 | * Does not support different clipping Ops (that is, every call to setClippingOutline is |
| 95 | * effectively using SkRegion::kReplaceOp) |
| 96 | * |
| 97 | * The clipping outline is independent from the regular clip. |
| 98 | */ |
| 99 | void setClippingOutline(LinearAllocator& allocator, const Outline* outline); |
| 100 | |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 101 | protected: |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 102 | const Rect& getRenderTargetClipBounds() const { return mSnapshot->getRenderTargetClip(); } |
| 103 | |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 104 | int getWidth() { return mWidth; } |
| 105 | int getHeight() { return mHeight; } |
| 106 | |
| 107 | // Save |
| 108 | int saveSnapshot(int flags); |
| 109 | void restoreSnapshot(); |
| 110 | |
| 111 | // allows subclasses to control what value is stored in snapshot's fbo field in |
| 112 | // initializeSaveStack |
| 113 | virtual GLuint getTargetFbo() const { |
| 114 | return -1; |
| 115 | } |
| 116 | |
| 117 | // Clip |
| 118 | bool calculateQuickRejectForScissor(float left, float top, float right, float bottom, |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 119 | bool* clipRequired, bool* roundRectClipRequired, bool snapOut) const; |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 120 | |
| 121 | /** |
| 122 | * Called just after a restore has occurred. The 'removed' snapshot popped from the stack, |
| 123 | * 'restored' snapshot has become the top/current. |
| 124 | * |
| 125 | * Subclasses can override this method to handle layer restoration |
| 126 | */ |
| 127 | virtual void onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {}; |
| 128 | |
Chris Craik | 797b95b | 2014-05-20 18:10:25 -0700 | [diff] [blame] | 129 | virtual void onViewportInitialized() {}; |
| 130 | |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 131 | inline const Rect* currentClipRect() const { |
| 132 | return mSnapshot->clipRect; |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 135 | inline const mat4* currentTransform() const { |
| 136 | return mSnapshot->transform; |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 137 | } |
| 138 | |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 139 | inline const Snapshot* currentSnapshot() const { |
| 140 | return mSnapshot != NULL ? mSnapshot.get() : mFirstSnapshot.get(); |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 143 | inline const Snapshot* firstSnapshot() const { |
| 144 | return mFirstSnapshot.get(); |
| 145 | } |
| 146 | |
| 147 | // indicites that the clip has been changed since the last time it was consumed |
| 148 | bool mDirtyClip; |
| 149 | |
| 150 | private: |
| 151 | // Dimensions of the drawing surface |
| 152 | int mWidth, mHeight; |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 153 | |
| 154 | // Number of saved states |
| 155 | int mSaveCount; |
| 156 | |
| 157 | // Base state |
| 158 | sp<Snapshot> mFirstSnapshot; |
| 159 | |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 160 | protected: |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 161 | // Current state |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 162 | // TODO: should become private, once hooks needed by OpenGLRenderer are added |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 163 | sp<Snapshot> mSnapshot; |
| 164 | |
Chris Craik | 797b95b | 2014-05-20 18:10:25 -0700 | [diff] [blame] | 165 | Vector3 mLightCenter; |
| 166 | float mLightRadius; |
| 167 | |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 168 | }; // class StatefulBaseRenderer |
| 169 | |
| 170 | }; // namespace uirenderer |
| 171 | }; // namespace android |
| 172 | |
| 173 | #endif // ANDROID_HWUI_STATEFUL_BASE_RENDERER_H |