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 | /** |
| 29 | * Implementation for Renderer state methods |
| 30 | * |
| 31 | * Eventually, this class should have abstract protected methods |
| 32 | * for allowing subclasses to hook into save/saveLayer and restore |
| 33 | */ |
| 34 | class StatefulBaseRenderer : public Renderer { |
| 35 | public: |
| 36 | StatefulBaseRenderer(); |
| 37 | |
| 38 | virtual status_t prepare(bool opaque) { |
| 39 | return prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque); |
| 40 | } |
| 41 | void initializeViewport(int width, int height); |
| 42 | void initializeSaveStack(float clipLeft, float clipTop, float clipRight, float clipBottom); |
| 43 | |
| 44 | // getters |
| 45 | bool hasRectToRectTransform() const { |
| 46 | return CC_LIKELY(currentTransform().rectToRect()); |
| 47 | } |
| 48 | |
| 49 | // Save (layer) |
| 50 | virtual int getSaveCount() const { return mSaveCount; } |
| 51 | virtual int save(int flags); |
| 52 | virtual void restore(); |
| 53 | virtual void restoreToCount(int saveCount); |
| 54 | //virtual int saveLayer(float left, float top, float right, float bottom, |
| 55 | // int alpha, SkXfermode::Mode mode, int flags); |
| 56 | |
| 57 | // Matrix |
| 58 | virtual void getMatrix(SkMatrix* outMatrix) const; |
| 59 | virtual void translate(float dx, float dy, float dz = 0.0f); |
| 60 | virtual void rotate(float degrees); |
| 61 | virtual void scale(float sx, float sy); |
| 62 | virtual void skew(float sx, float sy); |
| 63 | |
| 64 | virtual void setMatrix(SkMatrix* matrix); |
| 65 | void setMatrix(const Matrix4& matrix); // internal only convenience method |
| 66 | virtual void concatMatrix(SkMatrix* matrix); |
| 67 | void concatMatrix(const Matrix4& matrix); // internal only convenience method |
| 68 | |
| 69 | // Clip |
| 70 | const Rect& getClipBounds() const { return mSnapshot->getLocalClip(); } |
| 71 | virtual bool quickRejectConservative(float left, float top, float right, float bottom) const; |
| 72 | |
| 73 | // TODO: implement these with hooks to enable scissor/stencil usage in OpenGLRenderer |
| 74 | // virtual bool clipRect(float left, float top, float right, float bottom, SkRegion::Op op); |
| 75 | // virtual bool clipPath(SkPath* path, SkRegion::Op op); |
| 76 | // virtual bool clipRegion(SkRegion* region, SkRegion::Op op); |
| 77 | |
| 78 | protected: |
| 79 | int getWidth() { return mWidth; } |
| 80 | int getHeight() { return mHeight; } |
| 81 | |
| 82 | // Save |
| 83 | int saveSnapshot(int flags); |
| 84 | void restoreSnapshot(); |
| 85 | |
| 86 | // allows subclasses to control what value is stored in snapshot's fbo field in |
| 87 | // initializeSaveStack |
| 88 | virtual GLuint getTargetFbo() const { |
| 89 | return -1; |
| 90 | } |
| 91 | |
| 92 | // Clip |
| 93 | bool calculateQuickRejectForScissor(float left, float top, float right, float bottom, |
| 94 | bool* clipRequired, bool snapOut) const; |
| 95 | |
| 96 | /** |
| 97 | * Called just after a restore has occurred. The 'removed' snapshot popped from the stack, |
| 98 | * 'restored' snapshot has become the top/current. |
| 99 | * |
| 100 | * Subclasses can override this method to handle layer restoration |
| 101 | */ |
| 102 | virtual void onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {}; |
| 103 | |
| 104 | inline const Rect& currentClipRect() const { |
| 105 | return *(mSnapshot->clipRect); |
| 106 | } |
| 107 | |
| 108 | inline const mat4& currentTransform() const { |
| 109 | return *(mSnapshot->transform); |
| 110 | } |
| 111 | |
| 112 | inline const Snapshot& currentSnapshot() const { |
| 113 | return mSnapshot != NULL ? *mSnapshot : *mFirstSnapshot; |
| 114 | } |
| 115 | |
| 116 | // TODO: below should be private so that snapshot stack manipulation |
| 117 | // goes though (mostly) public methods |
| 118 | |
| 119 | // Number of saved states |
| 120 | int mSaveCount; |
| 121 | |
| 122 | // Base state |
| 123 | sp<Snapshot> mFirstSnapshot; |
| 124 | |
| 125 | // Current state |
| 126 | sp<Snapshot> mSnapshot; |
| 127 | |
| 128 | private: |
| 129 | // Dimensions of the drawing surface |
| 130 | int mWidth, mHeight; |
| 131 | |
| 132 | }; // class StatefulBaseRenderer |
| 133 | |
| 134 | }; // namespace uirenderer |
| 135 | }; // namespace android |
| 136 | |
| 137 | #endif // ANDROID_HWUI_STATEFUL_BASE_RENDERER_H |