blob: 46048f7125ce55a98eeee0b498d21c819559e4c2 [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 Craikf158b492016-01-12 14:45:08 -080022#include "LayerBuilder.h"
Chris Craikb565df12015-10-05 13:00:52 -070023#include "RecordedOp.h"
John Reck9372ac32016-01-19 11:46:52 -080024#include "utils/GLUtils.h"
Chris Craikb565df12015-10-05 13:00:52 -070025
26#include <vector>
27#include <unordered_map>
28
Chris Craikddf22152015-10-14 17:42:47 -070029struct SkRect;
30
Chris Craikb565df12015-10-05 13:00:52 -070031namespace android {
32namespace uirenderer {
33
34class BakedOpState;
Chris Craik0b7e8242015-10-28 16:50:44 -070035class LayerUpdateQueue;
Chris Craik5854b342015-10-26 15:49:56 -070036class OffscreenBuffer;
Chris Craikb565df12015-10-05 13:00:52 -070037class Rect;
38
Chris Craik5ea17242016-01-11 14:07:59 -080039/**
Chris Craik9cd1bbe2016-04-14 16:08:25 -070040 * Processes, optimizes, and stores rendering commands from RenderNodes and
41 * LayerUpdateQueue, building content needed to render a frame.
Chris Craik5ea17242016-01-11 14:07:59 -080042 *
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 Craikf158b492016-01-12 14:45:08 -080046 * draw stream) will create different reorder contexts, each in its own LayerBuilder.
Chris Craik5ea17242016-01-11 14:07:59 -080047 *
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 Craikf158b492016-01-12 14:45:08 -080056class FrameBuilder : public CanvasStateClient {
Chris Craikb565df12015-10-05 13:00:52 -070057public:
Chris Craik6e068c012016-01-15 16:15:30 -080058 struct LightGeometry {
59 Vector3 center;
60 float radius;
61 };
62
Chris Craik9cd1bbe2016-04-14 16:08:25 -070063 FrameBuilder(const SkRect& clip,
Chris Craik0b7e8242015-10-28 16:50:44 -070064 uint32_t viewportWidth, uint32_t viewportHeight,
Chris Craik9cd1bbe2016-04-14 16:08:25 -070065 const LightGeometry& lightGeometry, Caches& caches);
Chris Craikb565df12015-10-05 13:00:52 -070066
Chris Craik9cd1bbe2016-04-14 16:08:25 -070067 FrameBuilder(const LayerUpdateQueue& layerUpdateQueue,
68 const LightGeometry& lightGeometry, Caches& caches);
69
70 void deferLayers(const LayerUpdateQueue& layers);
71
72 void deferRenderNode(RenderNode& renderNode);
73
74 void deferRenderNode(float tx, float ty, Rect clipRect, RenderNode& renderNode);
75
76 void deferRenderNodeScene(const std::vector< sp<RenderNode> >& nodes,
77 const Rect& contentDrawBounds);
Chong Zhangc3bd5682016-01-25 12:01:12 -080078
Chris Craikf158b492016-01-12 14:45:08 -080079 virtual ~FrameBuilder() {}
Chris Craikb565df12015-10-05 13:00:52 -070080
81 /**
Chris Craik6fe991e52015-10-20 09:39:42 -070082 * replayBakedOps() is templated based on what class will receive ops being replayed.
Chris Craikb565df12015-10-05 13:00:52 -070083 *
84 * It constructs a lookup array of lambdas, which allows a recorded BakeOpState to use
85 * state->op->opId to lookup a receiver that will be called when the op is replayed.
Chris Craikb565df12015-10-05 13:00:52 -070086 */
Chris Craik5854b342015-10-26 15:49:56 -070087 template <typename StaticDispatcher, typename Renderer>
88 void replayBakedOps(Renderer& renderer) {
Chris Craik74af6e22016-04-05 13:18:56 -070089 std::vector<OffscreenBuffer*> temporaryLayers;
Chris Craik3a5811b2016-03-22 15:03:08 -070090 finishDefer();
Chris Craik15c3f192015-12-03 12:16:56 -080091 /**
Chris Craik7cbf63d2016-01-06 13:46:52 -080092 * Defines a LUT of lambdas which allow a recorded BakedOpState to use state->op->opId to
Chris Craik15c3f192015-12-03 12:16:56 -080093 * dispatch the op via a method on a static dispatcher when the op is replayed.
94 *
95 * For example a BitmapOp would resolve, via the lambda lookup, to calling:
96 *
97 * StaticDispatcher::onBitmapOp(Renderer& renderer, const BitmapOp& op, const BakedOpState& state);
98 */
99 #define X(Type) \
100 [](void* renderer, const BakedOpState& state) { \
Chris Craikb87eadd2016-01-06 09:16:05 -0800101 StaticDispatcher::on##Type(*(static_cast<Renderer*>(renderer)), \
102 static_cast<const Type&>(*(state.op)), state); \
Chris Craik15c3f192015-12-03 12:16:56 -0800103 },
Chris Craik7cbf63d2016-01-06 13:46:52 -0800104 static BakedOpReceiver unmergedReceivers[] = BUILD_RENDERABLE_OP_LUT(X);
Chris Craik15c3f192015-12-03 12:16:56 -0800105 #undef X
106
107 /**
Chris Craik7cbf63d2016-01-06 13:46:52 -0800108 * Defines a LUT of lambdas which allow merged arrays of BakedOpState* to be passed to a
109 * static dispatcher when the group of merged ops is replayed.
Chris Craik15c3f192015-12-03 12:16:56 -0800110 */
111 #define X(Type) \
112 [](void* renderer, const MergedBakedOpList& opList) { \
Chris Craik15c3f192015-12-03 12:16:56 -0800113 StaticDispatcher::onMerged##Type##s(*(static_cast<Renderer*>(renderer)), opList); \
114 },
Chris Craik7cbf63d2016-01-06 13:46:52 -0800115 static MergedOpReceiver mergedReceivers[] = BUILD_MERGEABLE_OP_LUT(X);
Chris Craik15c3f192015-12-03 12:16:56 -0800116 #undef X
Chris Craik818c9fb2015-10-23 14:33:42 -0700117
118 // Relay through layers in reverse order, since layers
119 // later in the list will be drawn by earlier ones
Chris Craikf158b492016-01-12 14:45:08 -0800120 for (int i = mLayerBuilders.size() - 1; i >= 1; i--) {
John Reck975591a2016-01-22 16:28:07 -0800121 GL_CHECKPOINT(MODERATE);
Chris Craikf158b492016-01-12 14:45:08 -0800122 LayerBuilder& layer = *(mLayerBuilders[i]);
Chris Craik0b7e8242015-10-28 16:50:44 -0700123 if (layer.renderNode) {
124 // cached HW layer - can't skip layer if empty
Chris Craik98787e62015-11-13 10:55:30 -0800125 renderer.startRepaintLayer(layer.offscreenBuffer, layer.repaintRect);
John Reck975591a2016-01-22 16:28:07 -0800126 GL_CHECKPOINT(MODERATE);
Chris Craik15c3f192015-12-03 12:16:56 -0800127 layer.replayBakedOpsImpl((void*)&renderer, unmergedReceivers, mergedReceivers);
John Reck975591a2016-01-22 16:28:07 -0800128 GL_CHECKPOINT(MODERATE);
Chris Craik0b7e8242015-10-28 16:50:44 -0700129 renderer.endLayer();
Chris Craikc0f3f2f2016-02-02 16:10:32 -0800130 } else if (!layer.empty()) {
131 // save layer - skip entire layer if empty (in which case, LayerOp has null layer).
Chris Craikd3daa312015-11-06 10:59:56 -0800132 layer.offscreenBuffer = renderer.startTemporaryLayer(layer.width, layer.height);
Chris Craik74af6e22016-04-05 13:18:56 -0700133 temporaryLayers.push_back(layer.offscreenBuffer);
John Reck975591a2016-01-22 16:28:07 -0800134 GL_CHECKPOINT(MODERATE);
Chris Craik15c3f192015-12-03 12:16:56 -0800135 layer.replayBakedOpsImpl((void*)&renderer, unmergedReceivers, mergedReceivers);
John Reck975591a2016-01-22 16:28:07 -0800136 GL_CHECKPOINT(MODERATE);
Chris Craik5854b342015-10-26 15:49:56 -0700137 renderer.endLayer();
Chris Craik818c9fb2015-10-23 14:33:42 -0700138 }
139 }
140
John Reck975591a2016-01-22 16:28:07 -0800141 GL_CHECKPOINT(MODERATE);
Chris Craik6246d2782016-03-29 15:01:41 -0700142 if (CC_LIKELY(mDrawFbo0)) {
143 const LayerBuilder& fbo0 = *(mLayerBuilders[0]);
144 renderer.startFrame(fbo0.width, fbo0.height, fbo0.repaintRect);
145 GL_CHECKPOINT(MODERATE);
146 fbo0.replayBakedOpsImpl((void*)&renderer, unmergedReceivers, mergedReceivers);
147 GL_CHECKPOINT(MODERATE);
148 renderer.endFrame(fbo0.repaintRect);
149 }
Chris Craik74af6e22016-04-05 13:18:56 -0700150
151 for (auto& temporaryLayer : temporaryLayers) {
152 renderer.recycleTemporaryLayer(temporaryLayer);
153 }
Chris Craikb565df12015-10-05 13:00:52 -0700154 }
Chris Craik6fe991e52015-10-20 09:39:42 -0700155
156 void dump() const {
Chris Craikf158b492016-01-12 14:45:08 -0800157 for (auto&& layer : mLayerBuilders) {
Chris Craik84ad6142016-01-12 12:09:19 -0800158 layer->dump();
Chris Craik6fe991e52015-10-20 09:39:42 -0700159 }
160 }
161
162 ///////////////////////////////////////////////////////////////////
163 /// CanvasStateClient interface
164 ///////////////////////////////////////////////////////////////////
165 virtual void onViewportInitialized() override;
166 virtual void onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) override;
167 virtual GLuint getTargetFbo() const override { return 0; }
168
Chris Craikb565df12015-10-05 13:00:52 -0700169private:
Chris Craik3a5811b2016-03-22 15:03:08 -0700170 void finishDefer();
Chris Craik161f54b2015-11-05 11:08:52 -0800171 enum class ChildrenSelectMode {
172 Negative,
173 Positive
174 };
Chris Craik0b7e8242015-10-28 16:50:44 -0700175 void saveForLayer(uint32_t layerWidth, uint32_t layerHeight,
Chris Craik8ecf41c2015-11-16 10:27:59 -0800176 float contentTranslateX, float contentTranslateY,
177 const Rect& repaintRect,
178 const Vector3& lightCenter,
179 const BeginLayerOp* beginLayerOp, RenderNode* renderNode);
Chris Craik0b7e8242015-10-28 16:50:44 -0700180 void restoreForLayer();
181
Chris Craikf158b492016-01-12 14:45:08 -0800182 LayerBuilder& currentLayer() { return *(mLayerBuilders[mLayerStack.back()]); }
Chris Craik6fe991e52015-10-20 09:39:42 -0700183
184 BakedOpState* tryBakeOpState(const RecordedOp& recordedOp) {
Chris Craike4db79d2015-12-22 16:32:23 -0800185 return BakedOpState::tryConstruct(mAllocator, *mCanvasState.writableSnapshot(), recordedOp);
Chris Craik6fe991e52015-10-20 09:39:42 -0700186 }
Chris Craik4c3980b2016-03-15 14:20:18 -0700187 BakedOpState* tryBakeUnboundedOpState(const RecordedOp& recordedOp) {
188 return BakedOpState::tryConstructUnbounded(mAllocator, *mCanvasState.writableSnapshot(), recordedOp);
189 }
190
Chris Craikb565df12015-10-05 13:00:52 -0700191
Chris Craik8d1f2122015-11-24 16:40:09 -0800192 // should always be surrounded by a save/restore pair, and not called if DisplayList is null
Chris Craik0b7e8242015-10-28 16:50:44 -0700193 void deferNodePropsAndOps(RenderNode& node);
194
Chris Craik161f54b2015-11-05 11:08:52 -0800195 template <typename V>
Chris Craikd6456402016-04-11 12:24:23 -0700196 void defer3dChildren(const ClipBase* reorderClip, ChildrenSelectMode mode,
197 const V& zTranslatedNodes);
Chris Craik161f54b2015-11-05 11:08:52 -0800198
Chris Craikd6456402016-04-11 12:24:23 -0700199 void deferShadow(const ClipBase* reorderClip, const RenderNodeOp& casterOp);
Chris Craik8d1f2122015-11-24 16:40:09 -0800200
201 void deferProjectedChildren(const RenderNode& renderNode);
202
203 void deferNodeOps(const RenderNode& renderNode);
204
Chris Craik268a9c02015-12-09 18:05:12 -0800205 void deferRenderNodeOpImpl(const RenderNodeOp& op);
Chris Craik161f54b2015-11-05 11:08:52 -0800206
Chris Craik15c3f192015-12-03 12:16:56 -0800207 void replayBakedOpsImpl(void* arg, BakedOpReceiver* receivers);
Chris Craikb565df12015-10-05 13:00:52 -0700208
Chris Craikd3daa312015-11-06 10:59:56 -0800209 SkPath* createFrameAllocatedPath() {
Chris Craikb87eadd2016-01-06 09:16:05 -0800210 return mAllocator.create<SkPath>();
Chris Craikd3daa312015-11-06 10:59:56 -0800211 }
Chris Craik386aa032015-12-07 17:08:25 -0800212
Chris Craik80d2ade2016-03-28 12:54:07 -0700213 BakedOpState* deferStrokeableOp(const RecordedOp& op, batchid_t batchId,
Chris Craik49b403d2017-03-06 13:51:43 -0800214 BakedOpState::StrokeBehavior strokeBehavior = BakedOpState::StrokeBehavior::StyleDefined,
215 bool expandForPathTexture = false);
Chris Craik386aa032015-12-07 17:08:25 -0800216
Chris Craikb565df12015-10-05 13:00:52 -0700217 /**
Chris Craikf158b492016-01-12 14:45:08 -0800218 * Declares all FrameBuilder::deferXXXXOp() methods for every RecordedOp type.
Chris Craikb565df12015-10-05 13:00:52 -0700219 *
220 * These private methods are called from within deferImpl to defer each individual op
221 * type differently.
222 */
Chris Craik7cbf63d2016-01-06 13:46:52 -0800223#define X(Type) void defer##Type(const Type& op);
224 MAP_DEFERRABLE_OPS(X)
225#undef X
Chris Craikb565df12015-10-05 13:00:52 -0700226
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700227 // contains single-frame objects, such as BakedOpStates, LayerBuilders, Batches
228 LinearAllocator mAllocator;
229 LinearStdAllocator<void*> mStdAllocator;
230
Chris Craik6fe991e52015-10-20 09:39:42 -0700231 // List of every deferred layer's render state. Replayed in reverse order to render a frame.
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700232 LsaVector<LayerBuilder*> mLayerBuilders;
Chris Craikb565df12015-10-05 13:00:52 -0700233
Chris Craik6fe991e52015-10-20 09:39:42 -0700234 /*
Chris Craikf158b492016-01-12 14:45:08 -0800235 * Stack of indices within mLayerBuilders representing currently active layers. If drawing
Chris Craik6fe991e52015-10-20 09:39:42 -0700236 * layerA within a layerB, will contain, in order:
237 * - 0 (representing FBO 0, always present)
238 * - layerB's index
239 * - layerA's index
240 *
Chris Craikf158b492016-01-12 14:45:08 -0800241 * Note that this doesn't vector doesn't always map onto all values of mLayerBuilders. When a
242 * layer is finished deferring, it will still be represented in mLayerBuilders, but it's index
Chris Craik6fe991e52015-10-20 09:39:42 -0700243 * won't be in mLayerStack. This is because it can be replayed, but can't have any more drawing
244 * ops added to it.
245 */
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700246 LsaVector<size_t> mLayerStack;
Chris Craikb565df12015-10-05 13:00:52 -0700247
Chris Craikb565df12015-10-05 13:00:52 -0700248 CanvasState mCanvasState;
249
Chris Craik3a5811b2016-03-22 15:03:08 -0700250 Caches& mCaches;
Chris Craik6e068c012016-01-15 16:15:30 -0800251
252 float mLightRadius;
253
Chris Craik6246d2782016-03-29 15:01:41 -0700254 const bool mDrawFbo0;
Chris Craikb565df12015-10-05 13:00:52 -0700255};
256
257}; // namespace uirenderer
258}; // namespace android