John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [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 | #ifndef RENDERNODE_H |
| 17 | #define RENDERNODE_H |
| 18 | |
| 19 | #ifndef LOG_TAG |
| 20 | #define LOG_TAG "OpenGLRenderer" |
| 21 | #endif |
| 22 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 23 | #include <set> |
| 24 | #include <vector> |
| 25 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 26 | #include <SkCamera.h> |
| 27 | #include <SkMatrix.h> |
| 28 | |
| 29 | #include <private/hwui/DrawGlInfo.h> |
| 30 | |
| 31 | #include <utils/KeyedVector.h> |
| 32 | #include <utils/LinearAllocator.h> |
| 33 | #include <utils/RefBase.h> |
| 34 | #include <utils/SortedVector.h> |
| 35 | #include <utils/String8.h> |
| 36 | #include <utils/Vector.h> |
| 37 | |
| 38 | #include <cutils/compiler.h> |
| 39 | |
| 40 | #include <androidfw/ResourceTypes.h> |
| 41 | |
| 42 | #include "Debug.h" |
| 43 | #include "Matrix.h" |
| 44 | #include "DeferredDisplayList.h" |
| 45 | #include "DisplayList.h" |
| 46 | #include "RenderProperties.h" |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 47 | #include "TreeInfo.h" |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 48 | |
| 49 | class SkBitmap; |
| 50 | class SkPaint; |
| 51 | class SkPath; |
| 52 | class SkRegion; |
| 53 | |
| 54 | namespace android { |
| 55 | namespace uirenderer { |
| 56 | |
| 57 | class DeferredDisplayList; |
| 58 | class DisplayListOp; |
| 59 | class DisplayListRenderer; |
| 60 | class OpenGLRenderer; |
| 61 | class Rect; |
| 62 | class Layer; |
| 63 | class SkiaShader; |
| 64 | |
| 65 | class ClipRectOp; |
| 66 | class SaveLayerOp; |
| 67 | class SaveOp; |
| 68 | class RestoreToCountOp; |
| 69 | class DrawDisplayListOp; |
| 70 | |
| 71 | /** |
| 72 | * Primary class for storing recorded canvas commands, as well as per-View/ViewGroup display properties. |
| 73 | * |
| 74 | * Recording of canvas commands is somewhat similar to SkPicture, except the canvas-recording |
| 75 | * functionality is split between DisplayListRenderer (which manages the recording), DisplayListData |
| 76 | * (which holds the actual data), and DisplayList (which holds properties and performs playback onto |
| 77 | * a renderer). |
| 78 | * |
| 79 | * Note that DisplayListData is swapped out from beneath an individual DisplayList when a view's |
| 80 | * recorded stream of canvas operations is refreshed. The DisplayList (and its properties) stay |
| 81 | * attached. |
| 82 | */ |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame] | 83 | class RenderNode : public VirtualLightRefBase { |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 84 | public: |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 85 | enum DirtyPropertyMask { |
| 86 | GENERIC = 1 << 1, |
| 87 | TRANSLATION_X = 1 << 2, |
| 88 | TRANSLATION_Y = 1 << 3, |
| 89 | TRANSLATION_Z = 1 << 4, |
| 90 | SCALE_X = 1 << 5, |
| 91 | SCALE_Y = 1 << 6, |
| 92 | ROTATION = 1 << 7, |
| 93 | ROTATION_X = 1 << 8, |
| 94 | ROTATION_Y = 1 << 9, |
| 95 | X = 1 << 10, |
| 96 | Y = 1 << 11, |
| 97 | Z = 1 << 12, |
| 98 | ALPHA = 1 << 13, |
| 99 | }; |
| 100 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 101 | ANDROID_API RenderNode(); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 102 | ANDROID_API virtual ~RenderNode(); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 103 | |
| 104 | // See flags defined in DisplayList.java |
| 105 | enum ReplayFlag { |
| 106 | kReplayFlag_ClipChildren = 0x1 |
| 107 | }; |
| 108 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 109 | ANDROID_API static void outputLogBuffer(int fd); |
| 110 | |
John Reck | 8de65a8 | 2014-04-09 15:23:38 -0700 | [diff] [blame] | 111 | ANDROID_API void setStagingDisplayList(DisplayListData* newData); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 112 | |
| 113 | void computeOrdering(); |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 114 | |
| 115 | void deferNodeTree(DeferStateStruct& deferStruct); |
| 116 | void deferNodeInParent(DeferStateStruct& deferStruct, const int level); |
| 117 | |
| 118 | void replayNodeTree(ReplayStateStruct& replayStruct); |
| 119 | void replayNodeInParent(ReplayStateStruct& replayStruct, const int level); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 120 | |
| 121 | ANDROID_API void output(uint32_t level = 1); |
| 122 | |
| 123 | bool isRenderable() const { |
| 124 | return mDisplayListData && mDisplayListData->hasDrawOps; |
| 125 | } |
| 126 | |
Chris Craik | defb7f3 | 2014-04-08 18:17:07 -0700 | [diff] [blame] | 127 | const char* getName() const { |
| 128 | return mName.string(); |
| 129 | } |
| 130 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 131 | void setName(const char* name) { |
| 132 | if (name) { |
| 133 | char* lastPeriod = strrchr(name, '.'); |
| 134 | if (lastPeriod) { |
| 135 | mName.setTo(lastPeriod + 1); |
| 136 | } else { |
| 137 | mName.setTo(name); |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 142 | bool isPropertyFieldDirty(DirtyPropertyMask field) const { |
| 143 | return mDirtyPropertyFields & field; |
| 144 | } |
| 145 | |
| 146 | void setPropertyFieldsDirty(uint32_t fields) { |
| 147 | mDirtyPropertyFields |= fields; |
| 148 | } |
| 149 | |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 150 | const RenderProperties& properties() { |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 151 | return mProperties; |
| 152 | } |
| 153 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 154 | RenderProperties& animatorProperties() { |
| 155 | return mProperties; |
| 156 | } |
| 157 | |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 158 | const RenderProperties& stagingProperties() { |
| 159 | return mStagingProperties; |
| 160 | } |
| 161 | |
| 162 | RenderProperties& mutateStagingProperties() { |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 163 | return mStagingProperties; |
| 164 | } |
| 165 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 166 | int getWidth() { |
| 167 | return properties().getWidth(); |
| 168 | } |
| 169 | |
| 170 | int getHeight() { |
| 171 | return properties().getHeight(); |
| 172 | } |
| 173 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 174 | ANDROID_API virtual void prepareTree(TreeInfo& info); |
| 175 | |
| 176 | // UI thread only! |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 177 | ANDROID_API void addAnimator(const sp<BaseRenderNodeAnimator>& animator) { |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 178 | animator->onAttached(this); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 179 | mStagingAnimators.insert(animator); |
| 180 | mNeedsAnimatorsSync = true; |
| 181 | } |
| 182 | |
| 183 | // UI thread only! |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 184 | ANDROID_API void removeAnimator(const sp<BaseRenderNodeAnimator>& animator) { |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 185 | mStagingAnimators.erase(animator); |
| 186 | mNeedsAnimatorsSync = true; |
| 187 | } |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 188 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 189 | private: |
| 190 | typedef key_value_pair_t<float, DrawDisplayListOp*> ZDrawDisplayListOpPair; |
| 191 | |
| 192 | static size_t findNonNegativeIndex(const Vector<ZDrawDisplayListOpPair>& nodes) { |
| 193 | for (size_t i = 0; i < nodes.size(); i++) { |
| 194 | if (nodes[i].key >= 0.0f) return i; |
| 195 | } |
| 196 | return nodes.size(); |
| 197 | } |
| 198 | |
| 199 | enum ChildrenSelectMode { |
| 200 | kNegativeZChildren, |
| 201 | kPositiveZChildren |
| 202 | }; |
| 203 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 204 | void applyViewPropertyTransforms(mat4& matrix, bool true3dTransform = false); |
| 205 | |
| 206 | void computeOrderingImpl(DrawDisplayListOp* opState, |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 207 | const SkPath* outlineOfProjectionSurface, |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 208 | Vector<DrawDisplayListOp*>* compositedChildrenOfProjectionSurface, |
| 209 | const mat4* transformFromProjectionSurface); |
| 210 | |
| 211 | template <class T> |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 212 | inline void setViewProperties(OpenGLRenderer& renderer, T& handler); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 213 | |
| 214 | void buildZSortedChildList(Vector<ZDrawDisplayListOpPair>& zTranslatedNodes); |
| 215 | |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 216 | template<class T> |
| 217 | inline void issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler); |
| 218 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 219 | template <class T> |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 220 | inline void issueOperationsOf3dChildren(const Vector<ZDrawDisplayListOpPair>& zTranslatedNodes, |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 221 | ChildrenSelectMode mode, OpenGLRenderer& renderer, T& handler); |
| 222 | |
| 223 | template <class T> |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 224 | inline void issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 225 | |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 226 | /** |
| 227 | * Issue the RenderNode's operations into a handler, recursing for subtrees through |
| 228 | * DrawDisplayListOp's defer() or replay() methods |
| 229 | */ |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 230 | template <class T> |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 231 | inline void issueOperations(OpenGLRenderer& renderer, T& handler); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 232 | |
| 233 | class TextContainer { |
| 234 | public: |
| 235 | size_t length() const { |
| 236 | return mByteLength; |
| 237 | } |
| 238 | |
| 239 | const char* text() const { |
| 240 | return (const char*) mText; |
| 241 | } |
| 242 | |
| 243 | size_t mByteLength; |
| 244 | const char* mText; |
| 245 | }; |
| 246 | |
John Reck | f4198b7 | 2014-04-09 17:00:04 -0700 | [diff] [blame] | 247 | void prepareTreeImpl(TreeInfo& info); |
| 248 | void pushStagingChanges(TreeInfo& info); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 249 | void evaluateAnimations(TreeInfo& info); |
John Reck | f4198b7 | 2014-04-09 17:00:04 -0700 | [diff] [blame] | 250 | void prepareSubTree(TreeInfo& info, DisplayListData* subtree); |
John Reck | 8de65a8 | 2014-04-09 15:23:38 -0700 | [diff] [blame] | 251 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 252 | String8 mName; |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 253 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 254 | uint32_t mDirtyPropertyFields; |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 255 | RenderProperties mProperties; |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 256 | RenderProperties mStagingProperties; |
| 257 | |
John Reck | 8de65a8 | 2014-04-09 15:23:38 -0700 | [diff] [blame] | 258 | bool mNeedsDisplayListDataSync; |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 259 | DisplayListData* mDisplayListData; |
John Reck | 8de65a8 | 2014-04-09 15:23:38 -0700 | [diff] [blame] | 260 | DisplayListData* mStagingDisplayListData; |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 261 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 262 | bool mNeedsAnimatorsSync; |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 263 | std::set< sp<BaseRenderNodeAnimator> > mStagingAnimators; |
| 264 | std::vector< sp<BaseRenderNodeAnimator> > mAnimators; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 265 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 266 | /** |
| 267 | * Draw time state - these properties are only set and used during rendering |
| 268 | */ |
| 269 | |
| 270 | // for projection surfaces, contains a list of all children items |
| 271 | Vector<DrawDisplayListOp*> mProjectedNodes; |
| 272 | }; // class RenderNode |
| 273 | |
| 274 | } /* namespace uirenderer */ |
| 275 | } /* namespace android */ |
| 276 | |
| 277 | #endif /* RENDERNODE_H */ |