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 | } |
| 49 | void initializeViewport(int width, int height); |
| 50 | void initializeSaveStack(float clipLeft, float clipTop, float clipRight, float clipBottom); |
| 51 | |
| 52 | // getters |
| 53 | bool hasRectToRectTransform() const { |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 54 | return CC_LIKELY(currentTransform()->rectToRect()); |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | // Save (layer) |
| 58 | virtual int getSaveCount() const { return mSaveCount; } |
| 59 | virtual int save(int flags); |
| 60 | virtual void restore(); |
| 61 | virtual void restoreToCount(int saveCount); |
| 62 | //virtual int saveLayer(float left, float top, float right, float bottom, |
| 63 | // int alpha, SkXfermode::Mode mode, int flags); |
| 64 | |
| 65 | // Matrix |
| 66 | virtual void getMatrix(SkMatrix* outMatrix) const; |
| 67 | virtual void translate(float dx, float dy, float dz = 0.0f); |
| 68 | virtual void rotate(float degrees); |
| 69 | virtual void scale(float sx, float sy); |
| 70 | virtual void skew(float sx, float sy); |
| 71 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 72 | virtual void setMatrix(const SkMatrix* matrix); |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 73 | void setMatrix(const Matrix4& matrix); // internal only convenience method |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 74 | virtual void concatMatrix(const SkMatrix* matrix); |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 75 | void concatMatrix(const Matrix4& matrix); // internal only convenience method |
| 76 | |
| 77 | // Clip |
| 78 | const Rect& getClipBounds() const { return mSnapshot->getLocalClip(); } |
| 79 | virtual bool quickRejectConservative(float left, float top, float right, float bottom) const; |
| 80 | |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 81 | 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] | 82 | virtual bool clipPath(const SkPath* path, SkRegion::Op op); |
| 83 | virtual bool clipRegion(const SkRegion* region, SkRegion::Op op); |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 84 | |
| 85 | protected: |
| 86 | int getWidth() { return mWidth; } |
| 87 | int getHeight() { return mHeight; } |
| 88 | |
| 89 | // Save |
| 90 | int saveSnapshot(int flags); |
| 91 | void restoreSnapshot(); |
| 92 | |
| 93 | // allows subclasses to control what value is stored in snapshot's fbo field in |
| 94 | // initializeSaveStack |
| 95 | virtual GLuint getTargetFbo() const { |
| 96 | return -1; |
| 97 | } |
| 98 | |
| 99 | // Clip |
| 100 | bool calculateQuickRejectForScissor(float left, float top, float right, float bottom, |
| 101 | bool* clipRequired, bool snapOut) const; |
| 102 | |
| 103 | /** |
| 104 | * Called just after a restore has occurred. The 'removed' snapshot popped from the stack, |
| 105 | * 'restored' snapshot has become the top/current. |
| 106 | * |
| 107 | * Subclasses can override this method to handle layer restoration |
| 108 | */ |
| 109 | virtual void onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {}; |
| 110 | |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 111 | inline const Rect* currentClipRect() const { |
| 112 | return mSnapshot->clipRect; |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 113 | } |
| 114 | |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 115 | inline const mat4* currentTransform() const { |
| 116 | return mSnapshot->transform; |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 117 | } |
| 118 | |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 119 | inline const Snapshot* currentSnapshot() const { |
| 120 | return mSnapshot != NULL ? mSnapshot.get() : mFirstSnapshot.get(); |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 123 | inline const Snapshot* firstSnapshot() const { |
| 124 | return mFirstSnapshot.get(); |
| 125 | } |
| 126 | |
| 127 | // indicites that the clip has been changed since the last time it was consumed |
| 128 | bool mDirtyClip; |
| 129 | |
| 130 | private: |
| 131 | // Dimensions of the drawing surface |
| 132 | int mWidth, mHeight; |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 133 | |
| 134 | // Number of saved states |
| 135 | int mSaveCount; |
| 136 | |
| 137 | // Base state |
| 138 | sp<Snapshot> mFirstSnapshot; |
| 139 | |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 140 | protected: |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 141 | // Current state |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 142 | // TODO: should become private, once hooks needed by OpenGLRenderer are added |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 143 | sp<Snapshot> mSnapshot; |
| 144 | |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 145 | }; // class StatefulBaseRenderer |
| 146 | |
| 147 | }; // namespace uirenderer |
| 148 | }; // namespace android |
| 149 | |
| 150 | #endif // ANDROID_HWUI_STATEFUL_BASE_RENDERER_H |