blob: 6c390c3fce247a9230ac2dc6017881ca43c8db19 [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
Mike Reedf3338bd2018-10-09 11:54:39 -040019#include "SkiaUtils.h"
20
Stan Iliev021693b2016-10-17 16:26:15 -040021#include <SkCanvas.h>
22#include <SkDrawable.h>
23#include <SkMatrix.h>
24#include <utils/RefBase.h>
25
26namespace android {
27namespace uirenderer {
28
29class RenderNode;
30class RenderProperties;
31
32namespace skiapipeline {
33
Stan Ilievdb45a4b2016-11-08 14:18:31 -050034class SkiaDisplayList;
35
Stan Iliev021693b2016-10-17 16:26:15 -040036/**
37 * This drawable wraps a RenderNode and enables it to be recorded into a list
38 * of Skia drawing commands.
39 */
40class RenderNodeDrawable : public SkDrawable {
41public:
42 /**
Stan Iliev021693b2016-10-17 16:26:15 -040043 * Creates a new RenderNodeDrawable backed by a render node.
44 *
45 * @param node that has to be drawn
46 * @param canvas is a recording canvas used to extract its matrix
47 * @param composeLayer if the node's layer type is RenderLayer this flag determines whether
48 * we should draw into the contents of the layer or compose the existing contents of the
49 * layer into the canvas.
50 */
Stan Iliev2f06e8a2016-11-02 15:29:03 -040051 explicit RenderNodeDrawable(RenderNode* node, SkCanvas* canvas, bool composeLayer = true,
John Reckd9d7f122018-05-03 14:40:56 -070052 bool inReorderingSection = false);
53
54 ~RenderNodeDrawable();
Stan Iliev021693b2016-10-17 16:26:15 -040055
56 /**
57 * Draws into the canvas this render node and its children. If the node is marked as a
58 * projection receiver then all projected children (excluding direct children) will be drawn
59 * last. Any projected node not matching those requirements will not be drawn by this function.
60 */
Nathaniel Nifong2945bff2019-11-25 09:34:21 -050061 void forceDraw(SkCanvas* canvas) const;
Stan Iliev021693b2016-10-17 16:26:15 -040062
63 /**
64 * Returns readonly render properties for this render node.
65 */
66 const RenderProperties& getNodeProperties() const;
67
68 /**
69 * The renderNode (and its properties) that is to be drawn
70 */
71 RenderNode* getRenderNode() const { return mRenderNode.get(); }
72
73 /**
74 * Returns the transform on the canvas at time of recording and is used for
75 * computing total transform without rerunning DL contents.
76 */
77 const SkMatrix& getRecordedMatrix() const { return mRecordedTransform; }
78
Stan Ilievdb45a4b2016-11-08 14:18:31 -050079 /**
80 * Sets a pointer to a display list of the parent render node. The display list is used when
81 * drawing backward projected nodes, when this node is a projection receiver.
82 */
83 void setProjectedDisplayList(SkiaDisplayList* projectedDisplayList) {
84 mProjectedDisplayList = projectedDisplayList;
85 }
86
Stan Iliev021693b2016-10-17 16:26:15 -040087protected:
88 /*
89 * Return the (conservative) bounds of what the drawable will draw.
90 */
91 virtual SkRect onGetBounds() override {
92 // We don't want to enable a record time quick reject because the properties
93 // of the RenderNode may be updated on subsequent frames.
Mike Reedf3338bd2018-10-09 11:54:39 -040094 return SkRectMakeLargest();
Stan Iliev021693b2016-10-17 16:26:15 -040095 }
96 /**
97 * This function draws into a canvas as forceDraw, but does nothing if the render node has a
98 * non-zero elevation.
99 */
100 virtual void onDraw(SkCanvas* canvas) override;
101
102private:
103 /*
104 * Render node that is wrapped by this class.
105 */
106 sp<RenderNode> mRenderNode;
107
108 /**
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500109 * Walks recursively the display list and draws the content of backward projected nodes.
110 *
111 * @param canvas used to draw the backward projected nodes
112 * @param displayList is a display list that contains a projection receiver
113 * @param nestLevel should be always 0. Used to track how far we are from the receiver.
114 */
115 void drawBackwardsProjectedNodes(SkCanvas* canvas, const SkiaDisplayList& displayList,
Nathaniel Nifong2945bff2019-11-25 09:34:21 -0500116 int nestLevel = 0) const;
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500117
118 /**
Stan Iliev021693b2016-10-17 16:26:15 -0400119 * Applies the rendering properties of a view onto a SkCanvas.
120 */
121 static void setViewProperties(const RenderProperties& properties, SkCanvas* canvas,
John Reck1bcacfd2017-11-03 10:12:19 -0700122 float* alphaMultiplier);
Stan Iliev021693b2016-10-17 16:26:15 -0400123
124 /**
125 * Stores transform on the canvas at time of recording and is used for
126 * computing total transform without rerunning DL contents.
127 */
128 const SkMatrix mRecordedTransform;
129
130 /**
131 * If mRenderNode's layer type is RenderLayer this flag determines whether we
132 * should draw into the contents of the layer or compose the existing contents
133 * of the layer into the canvas.
134 */
135 const bool mComposeLayer;
136
Stan Iliev021693b2016-10-17 16:26:15 -0400137 /*
Stan Iliev2f06e8a2016-11-02 15:29:03 -0400138 * True if the render node is in a reordering section
139 */
140 bool mInReorderingSection;
141
142 /*
Stan Iliev021693b2016-10-17 16:26:15 -0400143 * Draw the content into a canvas, depending on the render node layer type and mComposeLayer.
144 */
145 void drawContent(SkCanvas* canvas) const;
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500146
147 /*
148 * display list that is searched for any render nodes with getProjectBackwards==true
149 */
150 SkiaDisplayList* mProjectedDisplayList = nullptr;
Stan Iliev021693b2016-10-17 16:26:15 -0400151};
152
Chris Blume7b8a8082018-11-30 15:51:58 -0800153} // namespace skiapipeline
154} // namespace uirenderer
155} // namespace android