Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 1 | /* |
Chris Craik | 5ea1724 | 2016-01-11 14:07:59 -0800 | [diff] [blame] | 2 | * Copyright (C) 2016 The Android Open Source Project |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 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 | 5ea1724 | 2016-01-11 14:07:59 -0800 | [diff] [blame] | 17 | #pragma once |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 18 | |
| 19 | #include "BakedOpState.h" |
| 20 | #include "CanvasState.h" |
| 21 | #include "DisplayList.h" |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 22 | #include "LayerBuilder.h" |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 23 | #include "RecordedOp.h" |
John Reck | 9372ac3 | 2016-01-19 11:46:52 -0800 | [diff] [blame] | 24 | #include "utils/GLUtils.h" |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 25 | |
| 26 | #include <vector> |
| 27 | #include <unordered_map> |
| 28 | |
Chris Craik | ddf2215 | 2015-10-14 17:42:47 -0700 | [diff] [blame] | 29 | struct SkRect; |
| 30 | |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 31 | namespace android { |
| 32 | namespace uirenderer { |
| 33 | |
| 34 | class BakedOpState; |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 35 | class LayerUpdateQueue; |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame] | 36 | class OffscreenBuffer; |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 37 | class Rect; |
| 38 | |
Chris Craik | 5ea1724 | 2016-01-11 14:07:59 -0800 | [diff] [blame] | 39 | /** |
| 40 | * Traverses all of the drawing commands from the layers and RenderNodes passed into it, preparing |
| 41 | * them to be rendered. |
| 42 | * |
| 43 | * Resolves final drawing state for each operation (including clip, alpha and matrix), and then |
| 44 | * reorder and merge each op as it is resolved for drawing efficiency. Each layer of content (either |
| 45 | * from the LayerUpdateQueue, or temporary layers created by saveLayer operations in the |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 46 | * draw stream) will create different reorder contexts, each in its own LayerBuilder. |
Chris Craik | 5ea1724 | 2016-01-11 14:07:59 -0800 | [diff] [blame] | 47 | * |
| 48 | * Then the prepared or 'baked' drawing commands can be issued by calling the templated |
| 49 | * replayBakedOps() function, which will dispatch them (including any created merged op collections) |
| 50 | * to a Dispatcher and Renderer. See BakedOpDispatcher for how these baked drawing operations are |
| 51 | * resolved into Glops and rendered via BakedOpRenderer. |
| 52 | * |
| 53 | * This class is also the authoritative source for traversing RenderNodes, both for standard op |
| 54 | * traversal within a DisplayList, and for out of order RenderNode traversal for Z and projection. |
| 55 | */ |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 56 | class FrameBuilder : public CanvasStateClient { |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 57 | public: |
Chris Craik | 6e068c01 | 2016-01-15 16:15:30 -0800 | [diff] [blame] | 58 | struct LightGeometry { |
| 59 | Vector3 center; |
| 60 | float radius; |
| 61 | }; |
| 62 | |
| 63 | // TODO: remove |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 64 | FrameBuilder(const LayerUpdateQueue& layers, const SkRect& clip, |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 65 | uint32_t viewportWidth, uint32_t viewportHeight, |
Chris Craik | 6e068c01 | 2016-01-15 16:15:30 -0800 | [diff] [blame] | 66 | const std::vector< sp<RenderNode> >& nodes, |
| 67 | const LightGeometry& lightGeometry, |
| 68 | Caches* caches) |
| 69 | : FrameBuilder(layers, clip, viewportWidth, viewportHeight, nodes, lightGeometry, Rect(), caches) {} |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 70 | |
Chong Zhang | c3bd568 | 2016-01-25 12:01:12 -0800 | [diff] [blame] | 71 | FrameBuilder(const LayerUpdateQueue& layers, const SkRect& clip, |
| 72 | uint32_t viewportWidth, uint32_t viewportHeight, |
Chris Craik | 6e068c01 | 2016-01-15 16:15:30 -0800 | [diff] [blame] | 73 | const std::vector< sp<RenderNode> >& nodes, |
| 74 | const LightGeometry& lightGeometry, |
| 75 | const Rect &contentDrawBounds, Caches* caches); |
Chong Zhang | c3bd568 | 2016-01-25 12:01:12 -0800 | [diff] [blame] | 76 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 77 | virtual ~FrameBuilder() {} |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 78 | |
| 79 | /** |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 80 | * replayBakedOps() is templated based on what class will receive ops being replayed. |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 81 | * |
| 82 | * It constructs a lookup array of lambdas, which allows a recorded BakeOpState to use |
| 83 | * state->op->opId to lookup a receiver that will be called when the op is replayed. |
| 84 | * |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 85 | */ |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame] | 86 | template <typename StaticDispatcher, typename Renderer> |
| 87 | void replayBakedOps(Renderer& renderer) { |
Chris Craik | 15c3f19 | 2015-12-03 12:16:56 -0800 | [diff] [blame] | 88 | /** |
Chris Craik | 7cbf63d | 2016-01-06 13:46:52 -0800 | [diff] [blame] | 89 | * Defines a LUT of lambdas which allow a recorded BakedOpState to use state->op->opId to |
Chris Craik | 15c3f19 | 2015-12-03 12:16:56 -0800 | [diff] [blame] | 90 | * dispatch the op via a method on a static dispatcher when the op is replayed. |
| 91 | * |
| 92 | * For example a BitmapOp would resolve, via the lambda lookup, to calling: |
| 93 | * |
| 94 | * StaticDispatcher::onBitmapOp(Renderer& renderer, const BitmapOp& op, const BakedOpState& state); |
| 95 | */ |
| 96 | #define X(Type) \ |
| 97 | [](void* renderer, const BakedOpState& state) { \ |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 98 | StaticDispatcher::on##Type(*(static_cast<Renderer*>(renderer)), \ |
| 99 | static_cast<const Type&>(*(state.op)), state); \ |
Chris Craik | 15c3f19 | 2015-12-03 12:16:56 -0800 | [diff] [blame] | 100 | }, |
Chris Craik | 7cbf63d | 2016-01-06 13:46:52 -0800 | [diff] [blame] | 101 | static BakedOpReceiver unmergedReceivers[] = BUILD_RENDERABLE_OP_LUT(X); |
Chris Craik | 15c3f19 | 2015-12-03 12:16:56 -0800 | [diff] [blame] | 102 | #undef X |
| 103 | |
| 104 | /** |
Chris Craik | 7cbf63d | 2016-01-06 13:46:52 -0800 | [diff] [blame] | 105 | * Defines a LUT of lambdas which allow merged arrays of BakedOpState* to be passed to a |
| 106 | * static dispatcher when the group of merged ops is replayed. |
Chris Craik | 15c3f19 | 2015-12-03 12:16:56 -0800 | [diff] [blame] | 107 | */ |
| 108 | #define X(Type) \ |
| 109 | [](void* renderer, const MergedBakedOpList& opList) { \ |
Chris Craik | 15c3f19 | 2015-12-03 12:16:56 -0800 | [diff] [blame] | 110 | StaticDispatcher::onMerged##Type##s(*(static_cast<Renderer*>(renderer)), opList); \ |
| 111 | }, |
Chris Craik | 7cbf63d | 2016-01-06 13:46:52 -0800 | [diff] [blame] | 112 | static MergedOpReceiver mergedReceivers[] = BUILD_MERGEABLE_OP_LUT(X); |
Chris Craik | 15c3f19 | 2015-12-03 12:16:56 -0800 | [diff] [blame] | 113 | #undef X |
Chris Craik | 818c9fb | 2015-10-23 14:33:42 -0700 | [diff] [blame] | 114 | |
| 115 | // Relay through layers in reverse order, since layers |
| 116 | // later in the list will be drawn by earlier ones |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 117 | for (int i = mLayerBuilders.size() - 1; i >= 1; i--) { |
John Reck | 975591a | 2016-01-22 16:28:07 -0800 | [diff] [blame] | 118 | GL_CHECKPOINT(MODERATE); |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 119 | LayerBuilder& layer = *(mLayerBuilders[i]); |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 120 | if (layer.renderNode) { |
| 121 | // cached HW layer - can't skip layer if empty |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 122 | renderer.startRepaintLayer(layer.offscreenBuffer, layer.repaintRect); |
John Reck | 975591a | 2016-01-22 16:28:07 -0800 | [diff] [blame] | 123 | GL_CHECKPOINT(MODERATE); |
Chris Craik | 15c3f19 | 2015-12-03 12:16:56 -0800 | [diff] [blame] | 124 | layer.replayBakedOpsImpl((void*)&renderer, unmergedReceivers, mergedReceivers); |
John Reck | 975591a | 2016-01-22 16:28:07 -0800 | [diff] [blame] | 125 | GL_CHECKPOINT(MODERATE); |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 126 | renderer.endLayer(); |
Chris Craik | c0f3f2f | 2016-02-02 16:10:32 -0800 | [diff] [blame] | 127 | } else if (!layer.empty()) { |
| 128 | // save layer - skip entire layer if empty (in which case, LayerOp has null layer). |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 129 | layer.offscreenBuffer = renderer.startTemporaryLayer(layer.width, layer.height); |
John Reck | 975591a | 2016-01-22 16:28:07 -0800 | [diff] [blame] | 130 | GL_CHECKPOINT(MODERATE); |
Chris Craik | 15c3f19 | 2015-12-03 12:16:56 -0800 | [diff] [blame] | 131 | layer.replayBakedOpsImpl((void*)&renderer, unmergedReceivers, mergedReceivers); |
John Reck | 975591a | 2016-01-22 16:28:07 -0800 | [diff] [blame] | 132 | GL_CHECKPOINT(MODERATE); |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame] | 133 | renderer.endLayer(); |
Chris Craik | 818c9fb | 2015-10-23 14:33:42 -0700 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
John Reck | 975591a | 2016-01-22 16:28:07 -0800 | [diff] [blame] | 137 | GL_CHECKPOINT(MODERATE); |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 138 | const LayerBuilder& fbo0 = *(mLayerBuilders[0]); |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 139 | renderer.startFrame(fbo0.width, fbo0.height, fbo0.repaintRect); |
John Reck | 975591a | 2016-01-22 16:28:07 -0800 | [diff] [blame] | 140 | GL_CHECKPOINT(MODERATE); |
Chris Craik | 15c3f19 | 2015-12-03 12:16:56 -0800 | [diff] [blame] | 141 | fbo0.replayBakedOpsImpl((void*)&renderer, unmergedReceivers, mergedReceivers); |
John Reck | 975591a | 2016-01-22 16:28:07 -0800 | [diff] [blame] | 142 | GL_CHECKPOINT(MODERATE); |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 143 | renderer.endFrame(fbo0.repaintRect); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 144 | } |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 145 | |
| 146 | void dump() const { |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 147 | for (auto&& layer : mLayerBuilders) { |
Chris Craik | 84ad614 | 2016-01-12 12:09:19 -0800 | [diff] [blame] | 148 | layer->dump(); |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
| 152 | /////////////////////////////////////////////////////////////////// |
| 153 | /// CanvasStateClient interface |
| 154 | /////////////////////////////////////////////////////////////////// |
| 155 | virtual void onViewportInitialized() override; |
| 156 | virtual void onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) override; |
| 157 | virtual GLuint getTargetFbo() const override { return 0; } |
| 158 | |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 159 | private: |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 160 | enum class ChildrenSelectMode { |
| 161 | Negative, |
| 162 | Positive |
| 163 | }; |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 164 | void saveForLayer(uint32_t layerWidth, uint32_t layerHeight, |
Chris Craik | 8ecf41c | 2015-11-16 10:27:59 -0800 | [diff] [blame] | 165 | float contentTranslateX, float contentTranslateY, |
| 166 | const Rect& repaintRect, |
| 167 | const Vector3& lightCenter, |
| 168 | const BeginLayerOp* beginLayerOp, RenderNode* renderNode); |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 169 | void restoreForLayer(); |
| 170 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 171 | LayerBuilder& currentLayer() { return *(mLayerBuilders[mLayerStack.back()]); } |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 172 | |
| 173 | BakedOpState* tryBakeOpState(const RecordedOp& recordedOp) { |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 174 | return BakedOpState::tryConstruct(mAllocator, *mCanvasState.writableSnapshot(), recordedOp); |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 175 | } |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 176 | |
Chris Craik | 8d1f212 | 2015-11-24 16:40:09 -0800 | [diff] [blame] | 177 | // should always be surrounded by a save/restore pair, and not called if DisplayList is null |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 178 | void deferNodePropsAndOps(RenderNode& node); |
| 179 | |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 180 | template <typename V> |
| 181 | void defer3dChildren(ChildrenSelectMode mode, const V& zTranslatedNodes); |
| 182 | |
Chris Craik | 8d1f212 | 2015-11-24 16:40:09 -0800 | [diff] [blame] | 183 | void deferShadow(const RenderNodeOp& casterOp); |
| 184 | |
| 185 | void deferProjectedChildren(const RenderNode& renderNode); |
| 186 | |
| 187 | void deferNodeOps(const RenderNode& renderNode); |
| 188 | |
Chris Craik | 268a9c0 | 2015-12-09 18:05:12 -0800 | [diff] [blame] | 189 | void deferRenderNodeOpImpl(const RenderNodeOp& op); |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 190 | |
Chris Craik | 15c3f19 | 2015-12-03 12:16:56 -0800 | [diff] [blame] | 191 | void replayBakedOpsImpl(void* arg, BakedOpReceiver* receivers); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 192 | |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 193 | SkPath* createFrameAllocatedPath() { |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 194 | return mAllocator.create<SkPath>(); |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 195 | } |
Chris Craik | 386aa03 | 2015-12-07 17:08:25 -0800 | [diff] [blame] | 196 | |
Chris Craik | 268a9c0 | 2015-12-09 18:05:12 -0800 | [diff] [blame] | 197 | void deferStrokeableOp(const RecordedOp& op, batchid_t batchId, |
Chris Craik | 386aa03 | 2015-12-07 17:08:25 -0800 | [diff] [blame] | 198 | BakedOpState::StrokeBehavior strokeBehavior = BakedOpState::StrokeBehavior::StyleDefined); |
| 199 | |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 200 | /** |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 201 | * Declares all FrameBuilder::deferXXXXOp() methods for every RecordedOp type. |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 202 | * |
| 203 | * These private methods are called from within deferImpl to defer each individual op |
| 204 | * type differently. |
| 205 | */ |
Chris Craik | 7cbf63d | 2016-01-06 13:46:52 -0800 | [diff] [blame] | 206 | #define X(Type) void defer##Type(const Type& op); |
| 207 | MAP_DEFERRABLE_OPS(X) |
| 208 | #undef X |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 209 | |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 210 | // List of every deferred layer's render state. Replayed in reverse order to render a frame. |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 211 | std::vector<LayerBuilder*> mLayerBuilders; |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 212 | |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 213 | /* |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 214 | * Stack of indices within mLayerBuilders representing currently active layers. If drawing |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 215 | * layerA within a layerB, will contain, in order: |
| 216 | * - 0 (representing FBO 0, always present) |
| 217 | * - layerB's index |
| 218 | * - layerA's index |
| 219 | * |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 220 | * Note that this doesn't vector doesn't always map onto all values of mLayerBuilders. When a |
| 221 | * layer is finished deferring, it will still be represented in mLayerBuilders, but it's index |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 222 | * won't be in mLayerStack. This is because it can be replayed, but can't have any more drawing |
| 223 | * ops added to it. |
| 224 | */ |
| 225 | std::vector<size_t> mLayerStack; |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 226 | |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 227 | CanvasState mCanvasState; |
| 228 | |
Chris Craik | 6e068c01 | 2016-01-15 16:15:30 -0800 | [diff] [blame] | 229 | Caches* mCaches = nullptr; |
| 230 | |
| 231 | float mLightRadius; |
| 232 | |
| 233 | // contains single-frame objects, such as BakedOpStates, LayerBuilders, Batches |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 234 | LinearAllocator mAllocator; |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 235 | }; |
| 236 | |
| 237 | }; // namespace uirenderer |
| 238 | }; // namespace android |