Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [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 | |
Chris Craik | 5e00c7c | 2016-07-06 16:10:09 -0700 | [diff] [blame] | 17 | #pragma once |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 18 | |
John Reck | d9ee550 | 2015-10-06 10:06:37 -0700 | [diff] [blame] | 19 | #include "Snapshot.h" |
| 20 | |
Mike Reed | 6e49c9f | 2016-12-02 15:36:59 -0500 | [diff] [blame] | 21 | #include <SkClipOp.h> |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 22 | #include <SkMatrix.h> |
| 23 | #include <SkPath.h> |
| 24 | #include <SkRegion.h> |
| 25 | |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 26 | namespace android { |
| 27 | namespace uirenderer { |
| 28 | |
| 29 | /** |
| 30 | * Abstract base class for any class containing CanvasState. |
| 31 | * Defines three mandatory callbacks. |
| 32 | */ |
| 33 | class CanvasStateClient { |
| 34 | public: |
| 35 | CanvasStateClient() { } |
| 36 | virtual ~CanvasStateClient() { } |
| 37 | |
| 38 | /** |
| 39 | * Callback allowing embedder to take actions in the middle of a |
| 40 | * setViewport() call. |
| 41 | */ |
| 42 | virtual void onViewportInitialized() = 0; |
| 43 | |
| 44 | /** |
| 45 | * Callback allowing embedder to take actions in the middle of a |
| 46 | * restore() call. May be called several times sequentially. |
| 47 | */ |
| 48 | virtual void onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) = 0; |
| 49 | |
| 50 | /** |
| 51 | * Allows subclasses to control what value is stored in snapshot's |
| 52 | * fbo field in * initializeSaveStack. |
| 53 | */ |
Chris Craik | 6b109c7 | 2015-02-27 10:55:28 -0800 | [diff] [blame] | 54 | virtual GLuint getTargetFbo() const = 0; |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 55 | |
| 56 | }; // class CanvasStateClient |
| 57 | |
| 58 | /** |
| 59 | * Implements Canvas state methods on behalf of Renderers. |
| 60 | * |
| 61 | * Manages the Snapshot stack, implementing matrix, save/restore, and clipping methods in the |
| 62 | * Renderer interface. Drawing and recording classes that include a CanvasState will have |
| 63 | * different use cases: |
| 64 | * |
Chris Craik | 5e00c7c | 2016-07-06 16:10:09 -0700 | [diff] [blame] | 65 | * Drawing code maintaining canvas state (e.g. FrameBuilder) can query attributes (such as |
Chris Craik | db663fe | 2015-04-20 13:34:45 -0700 | [diff] [blame] | 66 | * transform) or hook into changes (e.g. save/restore) with minimal surface area for manipulating |
| 67 | * the stack itself. |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 68 | * |
Chris Craik | 5e00c7c | 2016-07-06 16:10:09 -0700 | [diff] [blame] | 69 | * Recording code maintaining canvas state (e.g. RecordingCanvas) can both record and pass |
Chris Craik | db663fe | 2015-04-20 13:34:45 -0700 | [diff] [blame] | 70 | * through state operations to CanvasState, so that not only will querying operations work |
| 71 | * (getClip/Matrix), but so that quickRejection can also be used. |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 72 | */ |
| 73 | |
Chris Craik | 64e445b | 2015-09-02 14:23:49 -0700 | [diff] [blame] | 74 | class CanvasState { |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 75 | public: |
Chih-Hung Hsieh | faecb78 | 2016-07-21 11:23:06 -0700 | [diff] [blame] | 76 | explicit CanvasState(CanvasStateClient& renderer); |
John Reck | d9ee550 | 2015-10-06 10:06:37 -0700 | [diff] [blame] | 77 | ~CanvasState(); |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 78 | |
| 79 | /** |
| 80 | * Initializes the first snapshot, computing the projection matrix, |
| 81 | * and stores the dimensions of the render target. |
| 82 | */ |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 83 | void initializeRecordingSaveStack(int viewportWidth, int viewportHeight); |
| 84 | |
| 85 | /** |
| 86 | * Initializes the first snapshot, computing the projection matrix, |
| 87 | * and stores the dimensions of the render target. |
| 88 | */ |
Chris Craik | 64e445b | 2015-09-02 14:23:49 -0700 | [diff] [blame] | 89 | void initializeSaveStack(int viewportWidth, int viewportHeight, |
| 90 | float clipLeft, float clipTop, float clipRight, float clipBottom, |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 91 | const Vector3& lightCenter); |
| 92 | |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 93 | bool hasRectToRectTransform() const { |
| 94 | return CC_LIKELY(currentTransform()->rectToRect()); |
| 95 | } |
| 96 | |
| 97 | // Save (layer) |
| 98 | int getSaveCount() const { return mSaveCount; } |
| 99 | int save(int flags); |
| 100 | void restore(); |
| 101 | void restoreToCount(int saveCount); |
| 102 | |
| 103 | // Save/Restore without side-effects |
| 104 | int saveSnapshot(int flags); |
| 105 | void restoreSnapshot(); |
| 106 | |
| 107 | // Matrix |
| 108 | void getMatrix(SkMatrix* outMatrix) const; |
| 109 | void translate(float dx, float dy, float dz = 0.0f); |
| 110 | void rotate(float degrees); |
| 111 | void scale(float sx, float sy); |
| 112 | void skew(float sx, float sy); |
| 113 | |
| 114 | void setMatrix(const SkMatrix& matrix); |
| 115 | void setMatrix(const Matrix4& matrix); // internal only convenience method |
| 116 | void concatMatrix(const SkMatrix& matrix); |
| 117 | void concatMatrix(const Matrix4& matrix); // internal only convenience method |
| 118 | |
| 119 | // Clip |
| 120 | const Rect& getLocalClipBounds() const { return mSnapshot->getLocalClip(); } |
| 121 | const Rect& getRenderTargetClipBounds() const { return mSnapshot->getRenderTargetClip(); } |
| 122 | |
| 123 | bool quickRejectConservative(float left, float top, float right, float bottom) const; |
| 124 | |
Mike Reed | 6e49c9f | 2016-12-02 15:36:59 -0500 | [diff] [blame] | 125 | bool clipRect(float left, float top, float right, float bottom, SkClipOp op); |
| 126 | bool clipPath(const SkPath* path, SkClipOp op); |
| 127 | bool clipRegion(const SkRegion* region, SkClipOp op); |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 128 | |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 129 | /** |
| 130 | * Sets a "clipping outline", which is independent from the regular clip. |
| 131 | * Currently only supports rectangles or rounded rectangles; passing in a |
| 132 | * more complicated outline fails silently. Replaces any previous clipping |
| 133 | * outline. |
| 134 | */ |
| 135 | void setClippingOutline(LinearAllocator& allocator, const Outline* outline); |
| 136 | void setClippingRoundRect(LinearAllocator& allocator, |
Chris Craik | 5e00c7c | 2016-07-06 16:10:09 -0700 | [diff] [blame] | 137 | const Rect& rect, float radius, bool highPriority = true) { |
| 138 | mSnapshot->setClippingRoundRect(allocator, rect, radius, highPriority); |
| 139 | } |
| 140 | void setProjectionPathMask(const SkPath* path) { |
| 141 | mSnapshot->setProjectionPathMask(path); |
| 142 | } |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 143 | |
| 144 | /** |
| 145 | * Returns true if drawing in the rectangle (left, top, right, bottom) |
| 146 | * will be clipped out. Is conservative: might return false when subpixel- |
| 147 | * perfect tests would return true. |
| 148 | */ |
| 149 | bool calculateQuickRejectForScissor(float left, float top, float right, float bottom, |
| 150 | bool* clipRequired, bool* roundRectClipRequired, bool snapOut) const; |
| 151 | |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 152 | void scaleAlpha(float alpha) { mSnapshot->alpha *= alpha; } |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 153 | |
| 154 | inline const mat4* currentTransform() const { return currentSnapshot()->transform; } |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 155 | inline const Rect& currentRenderTargetClip() const { return currentSnapshot()->getRenderTargetClip(); } |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 156 | inline int currentFlags() const { return currentSnapshot()->flags; } |
| 157 | const Vector3& currentLightCenter() const { return currentSnapshot()->getRelativeLightCenter(); } |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 158 | int getViewportWidth() const { return currentSnapshot()->getViewportWidth(); } |
| 159 | int getViewportHeight() const { return currentSnapshot()->getViewportHeight(); } |
Chris Craik | a766cb2 | 2015-06-08 16:49:43 -0700 | [diff] [blame] | 160 | int getWidth() const { return mWidth; } |
| 161 | int getHeight() const { return mHeight; } |
| 162 | bool clipIsSimple() const { return currentSnapshot()->clipIsSimple(); } |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 163 | |
John Reck | d9ee550 | 2015-10-06 10:06:37 -0700 | [diff] [blame] | 164 | inline const Snapshot* currentSnapshot() const { return mSnapshot; } |
| 165 | inline Snapshot* writableSnapshot() { return mSnapshot; } |
| 166 | inline const Snapshot* firstSnapshot() const { return &mFirstSnapshot; } |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 167 | |
| 168 | private: |
John Reck | d9ee550 | 2015-10-06 10:06:37 -0700 | [diff] [blame] | 169 | Snapshot* allocSnapshot(Snapshot* previous, int savecount); |
| 170 | void freeSnapshot(Snapshot* snapshot); |
| 171 | void freeAllSnapshots(); |
| 172 | |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 173 | /// Dimensions of the drawing surface |
| 174 | int mWidth, mHeight; |
| 175 | |
| 176 | /// Number of saved states |
| 177 | int mSaveCount; |
| 178 | |
| 179 | /// Base state |
John Reck | d9ee550 | 2015-10-06 10:06:37 -0700 | [diff] [blame] | 180 | Snapshot mFirstSnapshot; |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 181 | |
| 182 | /// Host providing callbacks |
| 183 | CanvasStateClient& mCanvas; |
| 184 | |
| 185 | /// Current state |
John Reck | d9ee550 | 2015-10-06 10:06:37 -0700 | [diff] [blame] | 186 | Snapshot* mSnapshot; |
| 187 | |
| 188 | // Pool of allocated snapshots to re-use |
| 189 | // NOTE: The dtors have already been invoked! |
| 190 | Snapshot* mSnapshotPool = nullptr; |
| 191 | int mSnapshotPoolCount = 0; |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 192 | |
| 193 | }; // class CanvasState |
| 194 | |
| 195 | }; // namespace uirenderer |
| 196 | }; // namespace android |