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