blob: 3bb862620ba299f1dbbb8dd02caf8c1e4bd9f2bc [file] [log] [blame]
Chris Craikb565df12015-10-05 13:00:52 -07001/*
Chris Craik5ea17242016-01-11 14:07:59 -08002 * Copyright (C) 2016 The Android Open Source Project
Chris Craikb565df12015-10-05 13:00:52 -07003 *
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 Craik5ea17242016-01-11 14:07:59 -080017#pragma once
Chris Craikb565df12015-10-05 13:00:52 -070018
19#include "BakedOpState.h"
20#include "CanvasState.h"
21#include "DisplayList.h"
Chris Craik5ea17242016-01-11 14:07:59 -080022#include "LayerReorderer.h"
Chris Craikb565df12015-10-05 13:00:52 -070023#include "RecordedOp.h"
24
25#include <vector>
26#include <unordered_map>
27
Chris Craikddf22152015-10-14 17:42:47 -070028struct SkRect;
29
Chris Craikb565df12015-10-05 13:00:52 -070030namespace android {
31namespace uirenderer {
32
33class BakedOpState;
Chris Craik0b7e8242015-10-28 16:50:44 -070034class LayerUpdateQueue;
Chris Craik5854b342015-10-26 15:49:56 -070035class OffscreenBuffer;
Chris Craikb565df12015-10-05 13:00:52 -070036class Rect;
37
Chris Craik5ea17242016-01-11 14:07:59 -080038/**
39 * Traverses all of the drawing commands from the layers and RenderNodes passed into it, preparing
40 * them to be rendered.
41 *
42 * Resolves final drawing state for each operation (including clip, alpha and matrix), and then
43 * reorder and merge each op as it is resolved for drawing efficiency. Each layer of content (either
44 * from the LayerUpdateQueue, or temporary layers created by saveLayer operations in the
45 * draw stream) will create different reorder contexts, each in its own LayerReorderer.
46 *
47 * Then the prepared or 'baked' drawing commands can be issued by calling the templated
48 * replayBakedOps() function, which will dispatch them (including any created merged op collections)
49 * to a Dispatcher and Renderer. See BakedOpDispatcher for how these baked drawing operations are
50 * resolved into Glops and rendered via BakedOpRenderer.
51 *
52 * This class is also the authoritative source for traversing RenderNodes, both for standard op
53 * traversal within a DisplayList, and for out of order RenderNode traversal for Z and projection.
54 */
55class FrameReorderer : public CanvasStateClient {
Chris Craikb565df12015-10-05 13:00:52 -070056public:
Chris Craik5ea17242016-01-11 14:07:59 -080057 FrameReorderer(const LayerUpdateQueue& layers, const SkRect& clip,
Chris Craik0b7e8242015-10-28 16:50:44 -070058 uint32_t viewportWidth, uint32_t viewportHeight,
Chris Craik98787e62015-11-13 10:55:30 -080059 const std::vector< sp<RenderNode> >& nodes, const Vector3& lightCenter);
Chris Craikb565df12015-10-05 13:00:52 -070060
Chris Craik5ea17242016-01-11 14:07:59 -080061 virtual ~FrameReorderer() {}
Chris Craikb565df12015-10-05 13:00:52 -070062
63 /**
Chris Craik6fe991e52015-10-20 09:39:42 -070064 * replayBakedOps() is templated based on what class will receive ops being replayed.
Chris Craikb565df12015-10-05 13:00:52 -070065 *
66 * It constructs a lookup array of lambdas, which allows a recorded BakeOpState to use
67 * state->op->opId to lookup a receiver that will be called when the op is replayed.
68 *
Chris Craikb565df12015-10-05 13:00:52 -070069 */
Chris Craik5854b342015-10-26 15:49:56 -070070 template <typename StaticDispatcher, typename Renderer>
71 void replayBakedOps(Renderer& renderer) {
Chris Craik15c3f192015-12-03 12:16:56 -080072 /**
Chris Craik7cbf63d2016-01-06 13:46:52 -080073 * Defines a LUT of lambdas which allow a recorded BakedOpState to use state->op->opId to
Chris Craik15c3f192015-12-03 12:16:56 -080074 * dispatch the op via a method on a static dispatcher when the op is replayed.
75 *
76 * For example a BitmapOp would resolve, via the lambda lookup, to calling:
77 *
78 * StaticDispatcher::onBitmapOp(Renderer& renderer, const BitmapOp& op, const BakedOpState& state);
79 */
80 #define X(Type) \
81 [](void* renderer, const BakedOpState& state) { \
Chris Craikb87eadd2016-01-06 09:16:05 -080082 StaticDispatcher::on##Type(*(static_cast<Renderer*>(renderer)), \
83 static_cast<const Type&>(*(state.op)), state); \
Chris Craik15c3f192015-12-03 12:16:56 -080084 },
Chris Craik7cbf63d2016-01-06 13:46:52 -080085 static BakedOpReceiver unmergedReceivers[] = BUILD_RENDERABLE_OP_LUT(X);
Chris Craik15c3f192015-12-03 12:16:56 -080086 #undef X
87
88 /**
Chris Craik7cbf63d2016-01-06 13:46:52 -080089 * Defines a LUT of lambdas which allow merged arrays of BakedOpState* to be passed to a
90 * static dispatcher when the group of merged ops is replayed.
Chris Craik15c3f192015-12-03 12:16:56 -080091 */
92 #define X(Type) \
93 [](void* renderer, const MergedBakedOpList& opList) { \
Chris Craik15c3f192015-12-03 12:16:56 -080094 StaticDispatcher::onMerged##Type##s(*(static_cast<Renderer*>(renderer)), opList); \
95 },
Chris Craik7cbf63d2016-01-06 13:46:52 -080096 static MergedOpReceiver mergedReceivers[] = BUILD_MERGEABLE_OP_LUT(X);
Chris Craik15c3f192015-12-03 12:16:56 -080097 #undef X
Chris Craik818c9fb2015-10-23 14:33:42 -070098
99 // Relay through layers in reverse order, since layers
100 // later in the list will be drawn by earlier ones
101 for (int i = mLayerReorderers.size() - 1; i >= 1; i--) {
Chris Craik84ad6142016-01-12 12:09:19 -0800102 LayerReorderer& layer = *(mLayerReorderers[i]);
Chris Craik0b7e8242015-10-28 16:50:44 -0700103 if (layer.renderNode) {
104 // cached HW layer - can't skip layer if empty
Chris Craik98787e62015-11-13 10:55:30 -0800105 renderer.startRepaintLayer(layer.offscreenBuffer, layer.repaintRect);
Chris Craik15c3f192015-12-03 12:16:56 -0800106 layer.replayBakedOpsImpl((void*)&renderer, unmergedReceivers, mergedReceivers);
Chris Craik0b7e8242015-10-28 16:50:44 -0700107 renderer.endLayer();
108 } else if (!layer.empty()) { // save layer - skip entire layer if empty
Chris Craikd3daa312015-11-06 10:59:56 -0800109 layer.offscreenBuffer = renderer.startTemporaryLayer(layer.width, layer.height);
Chris Craik15c3f192015-12-03 12:16:56 -0800110 layer.replayBakedOpsImpl((void*)&renderer, unmergedReceivers, mergedReceivers);
Chris Craik5854b342015-10-26 15:49:56 -0700111 renderer.endLayer();
Chris Craik818c9fb2015-10-23 14:33:42 -0700112 }
113 }
114
Chris Craik84ad6142016-01-12 12:09:19 -0800115 const LayerReorderer& fbo0 = *(mLayerReorderers[0]);
Chris Craik98787e62015-11-13 10:55:30 -0800116 renderer.startFrame(fbo0.width, fbo0.height, fbo0.repaintRect);
Chris Craik15c3f192015-12-03 12:16:56 -0800117 fbo0.replayBakedOpsImpl((void*)&renderer, unmergedReceivers, mergedReceivers);
Chris Craike4db79d2015-12-22 16:32:23 -0800118 renderer.endFrame(fbo0.repaintRect);
Chris Craikb565df12015-10-05 13:00:52 -0700119 }
Chris Craik6fe991e52015-10-20 09:39:42 -0700120
121 void dump() const {
122 for (auto&& layer : mLayerReorderers) {
Chris Craik84ad6142016-01-12 12:09:19 -0800123 layer->dump();
Chris Craik6fe991e52015-10-20 09:39:42 -0700124 }
125 }
126
127 ///////////////////////////////////////////////////////////////////
128 /// CanvasStateClient interface
129 ///////////////////////////////////////////////////////////////////
130 virtual void onViewportInitialized() override;
131 virtual void onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) override;
132 virtual GLuint getTargetFbo() const override { return 0; }
133
Chris Craikb565df12015-10-05 13:00:52 -0700134private:
Chris Craik161f54b2015-11-05 11:08:52 -0800135 enum class ChildrenSelectMode {
136 Negative,
137 Positive
138 };
Chris Craik0b7e8242015-10-28 16:50:44 -0700139 void saveForLayer(uint32_t layerWidth, uint32_t layerHeight,
Chris Craik8ecf41c2015-11-16 10:27:59 -0800140 float contentTranslateX, float contentTranslateY,
141 const Rect& repaintRect,
142 const Vector3& lightCenter,
143 const BeginLayerOp* beginLayerOp, RenderNode* renderNode);
Chris Craik0b7e8242015-10-28 16:50:44 -0700144 void restoreForLayer();
145
Chris Craik84ad6142016-01-12 12:09:19 -0800146 LayerReorderer& currentLayer() { return *(mLayerReorderers[mLayerStack.back()]); }
Chris Craik6fe991e52015-10-20 09:39:42 -0700147
148 BakedOpState* tryBakeOpState(const RecordedOp& recordedOp) {
Chris Craike4db79d2015-12-22 16:32:23 -0800149 return BakedOpState::tryConstruct(mAllocator, *mCanvasState.writableSnapshot(), recordedOp);
Chris Craik6fe991e52015-10-20 09:39:42 -0700150 }
Chris Craikb565df12015-10-05 13:00:52 -0700151
Chris Craik8d1f2122015-11-24 16:40:09 -0800152 // should always be surrounded by a save/restore pair, and not called if DisplayList is null
Chris Craik0b7e8242015-10-28 16:50:44 -0700153 void deferNodePropsAndOps(RenderNode& node);
154
Chris Craik161f54b2015-11-05 11:08:52 -0800155 template <typename V>
156 void defer3dChildren(ChildrenSelectMode mode, const V& zTranslatedNodes);
157
Chris Craik8d1f2122015-11-24 16:40:09 -0800158 void deferShadow(const RenderNodeOp& casterOp);
159
160 void deferProjectedChildren(const RenderNode& renderNode);
161
162 void deferNodeOps(const RenderNode& renderNode);
163
Chris Craik268a9c02015-12-09 18:05:12 -0800164 void deferRenderNodeOpImpl(const RenderNodeOp& op);
Chris Craik161f54b2015-11-05 11:08:52 -0800165
Chris Craik15c3f192015-12-03 12:16:56 -0800166 void replayBakedOpsImpl(void* arg, BakedOpReceiver* receivers);
Chris Craikb565df12015-10-05 13:00:52 -0700167
Chris Craikd3daa312015-11-06 10:59:56 -0800168 SkPath* createFrameAllocatedPath() {
Chris Craikb87eadd2016-01-06 09:16:05 -0800169 return mAllocator.create<SkPath>();
Chris Craikd3daa312015-11-06 10:59:56 -0800170 }
Chris Craik386aa032015-12-07 17:08:25 -0800171
Chris Craik268a9c02015-12-09 18:05:12 -0800172 void deferStrokeableOp(const RecordedOp& op, batchid_t batchId,
Chris Craik386aa032015-12-07 17:08:25 -0800173 BakedOpState::StrokeBehavior strokeBehavior = BakedOpState::StrokeBehavior::StyleDefined);
174
Chris Craikb565df12015-10-05 13:00:52 -0700175 /**
Chris Craik5ea17242016-01-11 14:07:59 -0800176 * Declares all FrameReorderer::deferXXXXOp() methods for every RecordedOp type.
Chris Craikb565df12015-10-05 13:00:52 -0700177 *
178 * These private methods are called from within deferImpl to defer each individual op
179 * type differently.
180 */
Chris Craik7cbf63d2016-01-06 13:46:52 -0800181#define X(Type) void defer##Type(const Type& op);
182 MAP_DEFERRABLE_OPS(X)
183#undef X
Chris Craikb565df12015-10-05 13:00:52 -0700184
Chris Craik6fe991e52015-10-20 09:39:42 -0700185 // List of every deferred layer's render state. Replayed in reverse order to render a frame.
Chris Craik84ad6142016-01-12 12:09:19 -0800186 std::vector<LayerReorderer*> mLayerReorderers;
Chris Craikb565df12015-10-05 13:00:52 -0700187
Chris Craik6fe991e52015-10-20 09:39:42 -0700188 /*
189 * Stack of indices within mLayerReorderers representing currently active layers. If drawing
190 * layerA within a layerB, will contain, in order:
191 * - 0 (representing FBO 0, always present)
192 * - layerB's index
193 * - layerA's index
194 *
195 * Note that this doesn't vector doesn't always map onto all values of mLayerReorderers. When a
196 * layer is finished deferring, it will still be represented in mLayerReorderers, but it's index
197 * won't be in mLayerStack. This is because it can be replayed, but can't have any more drawing
198 * ops added to it.
199 */
200 std::vector<size_t> mLayerStack;
Chris Craikb565df12015-10-05 13:00:52 -0700201
Chris Craikb565df12015-10-05 13:00:52 -0700202 CanvasState mCanvasState;
203
204 // contains ResolvedOps and Batches
205 LinearAllocator mAllocator;
Chris Craikb565df12015-10-05 13:00:52 -0700206};
207
208}; // namespace uirenderer
209}; // namespace android