blob: 6594bd22c0b13e4f7c62ce1e4938c883ac4069cd [file] [log] [blame]
Stan Iliev021693b2016-10-17 16:26:15 -04001/*
2 * Copyright (C) 2016 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
17#pragma once
18
19#include <SkCanvas.h>
20#include <SkDrawable.h>
21#include <SkMatrix.h>
22#include <utils/RefBase.h>
23
24namespace android {
25namespace uirenderer {
26
27class RenderNode;
28class RenderProperties;
29
30namespace skiapipeline {
31
Stan Ilievdb45a4b2016-11-08 14:18:31 -050032class SkiaDisplayList;
33
Stan Iliev021693b2016-10-17 16:26:15 -040034/**
35 * This drawable wraps a RenderNode and enables it to be recorded into a list
36 * of Skia drawing commands.
37 */
38class RenderNodeDrawable : public SkDrawable {
39public:
40 /**
Stan Iliev021693b2016-10-17 16:26:15 -040041 * Creates a new RenderNodeDrawable backed by a render node.
42 *
43 * @param node that has to be drawn
44 * @param canvas is a recording canvas used to extract its matrix
45 * @param composeLayer if the node's layer type is RenderLayer this flag determines whether
46 * we should draw into the contents of the layer or compose the existing contents of the
47 * layer into the canvas.
48 */
Stan Iliev2f06e8a2016-11-02 15:29:03 -040049 explicit RenderNodeDrawable(RenderNode* node, SkCanvas* canvas, bool composeLayer = true,
John Reckd9d7f122018-05-03 14:40:56 -070050 bool inReorderingSection = false);
51
52 ~RenderNodeDrawable();
Stan Iliev021693b2016-10-17 16:26:15 -040053
54 /**
55 * Draws into the canvas this render node and its children. If the node is marked as a
56 * projection receiver then all projected children (excluding direct children) will be drawn
57 * last. Any projected node not matching those requirements will not be drawn by this function.
58 */
59 void forceDraw(SkCanvas* canvas);
60
61 /**
62 * Returns readonly render properties for this render node.
63 */
64 const RenderProperties& getNodeProperties() const;
65
66 /**
67 * The renderNode (and its properties) that is to be drawn
68 */
69 RenderNode* getRenderNode() const { return mRenderNode.get(); }
70
71 /**
72 * Returns the transform on the canvas at time of recording and is used for
73 * computing total transform without rerunning DL contents.
74 */
75 const SkMatrix& getRecordedMatrix() const { return mRecordedTransform; }
76
Stan Ilievdb45a4b2016-11-08 14:18:31 -050077 /**
78 * Sets a pointer to a display list of the parent render node. The display list is used when
79 * drawing backward projected nodes, when this node is a projection receiver.
80 */
81 void setProjectedDisplayList(SkiaDisplayList* projectedDisplayList) {
82 mProjectedDisplayList = projectedDisplayList;
83 }
84
Stan Iliev021693b2016-10-17 16:26:15 -040085protected:
86 /*
87 * Return the (conservative) bounds of what the drawable will draw.
88 */
89 virtual SkRect onGetBounds() override {
90 // We don't want to enable a record time quick reject because the properties
91 // of the RenderNode may be updated on subsequent frames.
92 return SkRect::MakeLargest();
93 }
94 /**
95 * This function draws into a canvas as forceDraw, but does nothing if the render node has a
96 * non-zero elevation.
97 */
98 virtual void onDraw(SkCanvas* canvas) override;
99
100private:
101 /*
102 * Render node that is wrapped by this class.
103 */
104 sp<RenderNode> mRenderNode;
105
106 /**
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500107 * Walks recursively the display list and draws the content of backward projected nodes.
108 *
109 * @param canvas used to draw the backward projected nodes
110 * @param displayList is a display list that contains a projection receiver
111 * @param nestLevel should be always 0. Used to track how far we are from the receiver.
112 */
113 void drawBackwardsProjectedNodes(SkCanvas* canvas, const SkiaDisplayList& displayList,
John Reck1bcacfd2017-11-03 10:12:19 -0700114 int nestLevel = 0);
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500115
116 /**
Stan Iliev021693b2016-10-17 16:26:15 -0400117 * Applies the rendering properties of a view onto a SkCanvas.
118 */
119 static void setViewProperties(const RenderProperties& properties, SkCanvas* canvas,
John Reck1bcacfd2017-11-03 10:12:19 -0700120 float* alphaMultiplier);
Stan Iliev021693b2016-10-17 16:26:15 -0400121
122 /**
123 * Stores transform on the canvas at time of recording and is used for
124 * computing total transform without rerunning DL contents.
125 */
126 const SkMatrix mRecordedTransform;
127
128 /**
129 * If mRenderNode's layer type is RenderLayer this flag determines whether we
130 * should draw into the contents of the layer or compose the existing contents
131 * of the layer into the canvas.
132 */
133 const bool mComposeLayer;
134
Stan Iliev021693b2016-10-17 16:26:15 -0400135 /*
Stan Iliev2f06e8a2016-11-02 15:29:03 -0400136 * True if the render node is in a reordering section
137 */
138 bool mInReorderingSection;
139
140 /*
Stan Iliev021693b2016-10-17 16:26:15 -0400141 * Draw the content into a canvas, depending on the render node layer type and mComposeLayer.
142 */
143 void drawContent(SkCanvas* canvas) const;
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500144
145 /*
146 * display list that is searched for any render nodes with getProjectBackwards==true
147 */
148 SkiaDisplayList* mProjectedDisplayList = nullptr;
Stan Iliev021693b2016-10-17 16:26:15 -0400149};
150
John Reck1bcacfd2017-11-03 10:12:19 -0700151}; // namespace skiapipeline
152}; // namespace uirenderer
153}; // namespace android