blob: 070a765cf7ca354c477ee5a094fbf4716542ee91 [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#include "RenderNodeDrawable.h"
John Reck1bcacfd2017-11-03 10:12:19 -070018#include <SkPaintFilterCanvas.h>
Stan Iliev021693b2016-10-17 16:26:15 -040019#include "RenderNode.h"
20#include "SkiaDisplayList.h"
Stan Iliev021693b2016-10-17 16:26:15 -040021#include "utils/TraceUtils.h"
22
Ben Wagner1d155332018-08-20 18:36:05 -040023#include <optional>
24
Stan Iliev021693b2016-10-17 16:26:15 -040025namespace android {
26namespace uirenderer {
27namespace skiapipeline {
28
John Reckd9d7f122018-05-03 14:40:56 -070029RenderNodeDrawable::RenderNodeDrawable(RenderNode* node, SkCanvas* canvas, bool composeLayer,
30 bool inReorderingSection)
31 : mRenderNode(node)
32 , mRecordedTransform(canvas->getTotalMatrix())
33 , mComposeLayer(composeLayer)
34 , mInReorderingSection(inReorderingSection) {}
35
36RenderNodeDrawable::~RenderNodeDrawable() {
37 // Just here to move the destructor into the cpp file where we can access RenderNode.
38
39 // TODO: Detangle the header nightmare.
40}
41
John Reck1bcacfd2017-11-03 10:12:19 -070042void RenderNodeDrawable::drawBackwardsProjectedNodes(SkCanvas* canvas,
43 const SkiaDisplayList& displayList,
Nathaniel Nifong2945bff2019-11-25 09:34:21 -050044 int nestLevel) const {
Stan Ilievdb45a4b2016-11-08 14:18:31 -050045 LOG_ALWAYS_FATAL_IF(0 == nestLevel && !displayList.mProjectionReceiver);
46 for (auto& child : displayList.mChildNodes) {
47 const RenderProperties& childProperties = child.getNodeProperties();
48
John Reck1bcacfd2017-11-03 10:12:19 -070049 // immediate children cannot be projected on their parent
Stan Ilievdb45a4b2016-11-08 14:18:31 -050050 if (childProperties.getProjectBackwards() && nestLevel > 0) {
51 SkAutoCanvasRestore acr2(canvas, true);
John Reck1bcacfd2017-11-03 10:12:19 -070052 // Apply recorded matrix, which is a total matrix saved at recording time to avoid
53 // replaying all DL commands.
Stan Ilievdb45a4b2016-11-08 14:18:31 -050054 canvas->concat(child.getRecordedMatrix());
55 child.drawContent(canvas);
56 }
57
John Reck1bcacfd2017-11-03 10:12:19 -070058 // skip walking sub-nodes if current display list contains a receiver with exception of
59 // level 0, which is a known receiver
Stan Ilievdb45a4b2016-11-08 14:18:31 -050060 if (0 == nestLevel || !displayList.containsProjectionReceiver()) {
61 SkAutoCanvasRestore acr(canvas, true);
62 SkMatrix nodeMatrix;
63 mat4 hwuiMatrix(child.getRecordedMatrix());
John Reckbe671952021-01-13 22:39:32 -050064 const RenderNode* childNode = child.getRenderNode();
Stan Ilievdb45a4b2016-11-08 14:18:31 -050065 childNode->applyViewPropertyTransforms(hwuiMatrix);
66 hwuiMatrix.copyTo(nodeMatrix);
67 canvas->concat(nodeMatrix);
John Reckbe671952021-01-13 22:39:32 -050068 const SkiaDisplayList* childDisplayList = childNode->getDisplayList().asSkiaDl();
Stan Ilievdb45a4b2016-11-08 14:18:31 -050069 if (childDisplayList) {
John Reck1bcacfd2017-11-03 10:12:19 -070070 drawBackwardsProjectedNodes(canvas, *childDisplayList, nestLevel + 1);
Stan Ilievdb45a4b2016-11-08 14:18:31 -050071 }
72 }
73 }
74}
75
Stan Iliev021693b2016-10-17 16:26:15 -040076static void clipOutline(const Outline& outline, SkCanvas* canvas, const SkRect* pendingClip) {
Stan Iliev021693b2016-10-17 16:26:15 -040077 Rect possibleRect;
78 float radius;
Derek Sollenbergerf209c062017-05-26 12:11:34 -040079
80 /* To match the existing HWUI behavior we only supports rectangles or
81 * rounded rectangles; passing in a more complicated outline fails silently.
82 */
83 if (!outline.getAsRoundRect(&possibleRect, &radius)) {
84 if (pendingClip) {
85 canvas->clipRect(*pendingClip);
86 }
87 return;
88 }
89
Stan Iliev021693b2016-10-17 16:26:15 -040090 SkRect rect = possibleRect.toSkRect();
91 if (radius != 0.0f) {
92 if (pendingClip && !pendingClip->contains(rect)) {
93 canvas->clipRect(*pendingClip);
94 }
Mike Reed6c67f1d2016-12-14 10:29:54 -050095 canvas->clipRRect(SkRRect::MakeRectXY(rect, radius, radius), SkClipOp::kIntersect, true);
Stan Iliev021693b2016-10-17 16:26:15 -040096 } else {
97 if (pendingClip) {
98 (void)rect.intersect(*pendingClip);
99 }
100 canvas->clipRect(rect);
101 }
102}
103
104const RenderProperties& RenderNodeDrawable::getNodeProperties() const {
105 return mRenderNode->properties();
106}
107
108void RenderNodeDrawable::onDraw(SkCanvas* canvas) {
John Reck1bcacfd2017-11-03 10:12:19 -0700109 // negative and positive Z order are drawn out of order, if this render node drawable is in
110 // a reordering section
Stan Iliev2f06e8a2016-11-02 15:29:03 -0400111 if ((!mInReorderingSection) || MathUtils::isZero(mRenderNode->properties().getZ())) {
Stan Iliev021693b2016-10-17 16:26:15 -0400112 this->forceDraw(canvas);
113 }
114}
115
John Reckf96b2842018-11-29 09:44:10 -0800116class MarkDraw {
117public:
118 explicit MarkDraw(SkCanvas& canvas, RenderNode& node) : mCanvas(canvas), mNode(node) {
119 if (CC_UNLIKELY(Properties::skpCaptureEnabled)) {
120 mNode.markDrawStart(mCanvas);
121 }
122 }
123 ~MarkDraw() {
124 if (CC_UNLIKELY(Properties::skpCaptureEnabled)) {
125 mNode.markDrawEnd(mCanvas);
126 }
127 }
John Reck283bb462018-12-13 16:40:14 -0800128
John Reckf96b2842018-11-29 09:44:10 -0800129private:
130 SkCanvas& mCanvas;
131 RenderNode& mNode;
132};
133
Nathaniel Nifong2945bff2019-11-25 09:34:21 -0500134void RenderNodeDrawable::forceDraw(SkCanvas* canvas) const {
Stan Iliev021693b2016-10-17 16:26:15 -0400135 RenderNode* renderNode = mRenderNode.get();
John Reckf96b2842018-11-29 09:44:10 -0800136 MarkDraw _marker{*canvas, *renderNode};
Stan Iliev021693b2016-10-17 16:26:15 -0400137
138 // We only respect the nothingToDraw check when we are composing a layer. This
139 // ensures that we paint the layer even if it is not currently visible in the
140 // event that the properties change and it becomes visible.
Yuqian Li5ebbc8e2017-11-29 09:53:06 -0500141 if ((mProjectedDisplayList == nullptr && !renderNode->isRenderable()) ||
John Reck283bb462018-12-13 16:40:14 -0800142 (renderNode->nothingToDraw() && mComposeLayer)) {
Stan Iliev021693b2016-10-17 16:26:15 -0400143 return;
144 }
145
John Reckbe671952021-01-13 22:39:32 -0500146 SkiaDisplayList* displayList = renderNode->getDisplayList().asSkiaDl();
Stan Iliev021693b2016-10-17 16:26:15 -0400147
148 SkAutoCanvasRestore acr(canvas, true);
Stan Iliev021693b2016-10-17 16:26:15 -0400149 const RenderProperties& properties = this->getNodeProperties();
John Reck1bcacfd2017-11-03 10:12:19 -0700150 // pass this outline to the children that may clip backward projected nodes
151 displayList->mProjectedOutline =
152 displayList->containsProjectionReceiver() ? &properties.getOutline() : nullptr;
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500153 if (!properties.getProjectBackwards()) {
154 drawContent(canvas);
155 if (mProjectedDisplayList) {
John Reck1bcacfd2017-11-03 10:12:19 -0700156 acr.restore(); // draw projected children using parent matrix
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500157 LOG_ALWAYS_FATAL_IF(!mProjectedDisplayList->mProjectedOutline);
158 const bool shouldClip = mProjectedDisplayList->mProjectedOutline->getPath();
159 SkAutoCanvasRestore acr2(canvas, shouldClip);
Stan Iliev54d70322018-06-14 18:00:10 -0400160 canvas->setMatrix(mProjectedDisplayList->mParentMatrix);
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500161 if (shouldClip) {
Stan Iliev8b7cc512019-02-22 10:16:43 -0500162 canvas->clipPath(*mProjectedDisplayList->mProjectedOutline->getPath());
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500163 }
164 drawBackwardsProjectedNodes(canvas, *mProjectedDisplayList);
Stan Iliev021693b2016-10-17 16:26:15 -0400165 }
Stan Iliev021693b2016-10-17 16:26:15 -0400166 }
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500167 displayList->mProjectedOutline = nullptr;
Stan Iliev021693b2016-10-17 16:26:15 -0400168}
169
John Reck1bcacfd2017-11-03 10:12:19 -0700170static bool layerNeedsPaint(const LayerProperties& properties, float alphaMultiplier,
171 SkPaint* paint) {
Stan Iliev7e100e92018-01-22 10:36:33 -0500172 paint->setFilterQuality(kLow_SkFilterQuality);
John Reck1bcacfd2017-11-03 10:12:19 -0700173 if (alphaMultiplier < 1.0f || properties.alpha() < 255 ||
Nader Jawad390d6e82020-09-24 21:35:03 -0700174 properties.xferMode() != SkBlendMode::kSrcOver || properties.getColorFilter() != nullptr ||
175 properties.getImageFilter() != nullptr) {
Stan Iliev021693b2016-10-17 16:26:15 -0400176 paint->setAlpha(properties.alpha() * alphaMultiplier);
177 paint->setBlendMode(properties.xferMode());
Ben Wagnerc1a8a462018-07-12 12:41:28 -0400178 paint->setColorFilter(sk_ref_sp(properties.getColorFilter()));
Nader Jawad390d6e82020-09-24 21:35:03 -0700179 paint->setImageFilter(sk_ref_sp(properties.getImageFilter()));
Stan Iliev021693b2016-10-17 16:26:15 -0400180 return true;
181 }
182 return false;
183}
184
Stan Iliev1843ac72017-09-20 18:05:35 -0400185class AlphaFilterCanvas : public SkPaintFilterCanvas {
186public:
187 AlphaFilterCanvas(SkCanvas* canvas, float alpha) : SkPaintFilterCanvas(canvas), mAlpha(alpha) {}
John Reck1bcacfd2017-11-03 10:12:19 -0700188
Stan Iliev1843ac72017-09-20 18:05:35 -0400189protected:
Ben Wagner62b38942019-04-15 11:59:33 -0400190 bool onFilter(SkPaint& paint) const override {
191 paint.setAlpha((uint8_t)paint.getAlpha() * mAlpha);
Stan Iliev1843ac72017-09-20 18:05:35 -0400192 return true;
193 }
194 void onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) override {
195 // We unroll the drawable using "this" canvas, so that draw calls contained inside will
Ben Wagner62b38942019-04-15 11:59:33 -0400196 // get their alpha applied. The default SkPaintFilterCanvas::onDrawDrawable does not unroll.
Stan Iliev1843ac72017-09-20 18:05:35 -0400197 drawable->draw(this, matrix);
198 }
John Reck1bcacfd2017-11-03 10:12:19 -0700199
Stan Iliev1843ac72017-09-20 18:05:35 -0400200private:
201 float mAlpha;
202};
203
Stan Iliev021693b2016-10-17 16:26:15 -0400204void RenderNodeDrawable::drawContent(SkCanvas* canvas) const {
205 RenderNode* renderNode = mRenderNode.get();
206 float alphaMultiplier = 1.0f;
207 const RenderProperties& properties = renderNode->properties();
208
209 // If we are drawing the contents of layer, we don't want to apply any of
210 // the RenderNode's properties during this pass. Those will all be applied
211 // when the layer is composited.
212 if (mComposeLayer) {
213 setViewProperties(properties, canvas, &alphaMultiplier);
214 }
John Reckbe671952021-01-13 22:39:32 -0500215 SkiaDisplayList* displayList = mRenderNode->getDisplayList().asSkiaDl();
Stan Iliev54d70322018-06-14 18:00:10 -0400216 displayList->mParentMatrix = canvas->getTotalMatrix();
Stan Iliev021693b2016-10-17 16:26:15 -0400217
John Reck1bcacfd2017-11-03 10:12:19 -0700218 // TODO should we let the bound of the drawable do this for us?
Stan Iliev021693b2016-10-17 16:26:15 -0400219 const SkRect bounds = SkRect::MakeWH(properties.getWidth(), properties.getHeight());
220 bool quickRejected = properties.getClipToBounds() && canvas->quickReject(bounds);
221 if (!quickRejected) {
John Reckbe671952021-01-13 22:39:32 -0500222 SkiaDisplayList* displayList = renderNode->getDisplayList().asSkiaDl();
Stan Iliev021693b2016-10-17 16:26:15 -0400223 const LayerProperties& layerProperties = properties.layerProperties();
224 // composing a hardware layer
225 if (renderNode->getLayerSurface() && mComposeLayer) {
226 SkASSERT(properties.effectiveLayerType() == LayerType::RenderLayer);
Stan Iliev7e100e92018-01-22 10:36:33 -0500227 SkPaint paint;
228 layerNeedsPaint(layerProperties, alphaMultiplier, &paint);
Derek Sollenberger03e6cff72017-12-04 15:07:08 -0500229
230 // surfaces for layers are created on LAYER_SIZE boundaries (which are >= layer size) so
231 // we need to restrict the portion of the surface drawn to the size of the renderNode.
232 SkASSERT(renderNode->getLayerSurface()->width() >= bounds.width());
233 SkASSERT(renderNode->getLayerSurface()->height() >= bounds.height());
Nathaniel Nifong2945bff2019-11-25 09:34:21 -0500234
235 // If SKP recording is active save an annotation that indicates this drawImageRect
236 // could also be rendered with the commands saved at ID associated with this node.
237 if (CC_UNLIKELY(Properties::skpCaptureEnabled)) {
238 canvas->drawAnnotation(bounds, String8::format(
239 "SurfaceID|%" PRId64, renderNode->uniqueId()).c_str(), nullptr);
240 }
241 canvas->drawImageRect(renderNode->getLayerSurface()->makeImageSnapshot(), bounds,
John Reck283bb462018-12-13 16:40:14 -0800242 bounds, &paint);
Matt Sarett79756be2016-11-09 16:13:54 -0500243
Matt Sarettf58cc922016-11-14 18:33:38 -0500244 if (!renderNode->getSkiaLayer()->hasRenderedSinceRepaint) {
Matt Sarett79756be2016-11-09 16:13:54 -0500245 renderNode->getSkiaLayer()->hasRenderedSinceRepaint = true;
Matt Sarettf58cc922016-11-14 18:33:38 -0500246 if (CC_UNLIKELY(Properties::debugLayersUpdates)) {
247 SkPaint layerPaint;
248 layerPaint.setColor(0x7f00ff00);
249 canvas->drawRect(bounds, layerPaint);
250 } else if (CC_UNLIKELY(Properties::debugOverdraw)) {
251 // Render transparent rect to increment overdraw for repaint area.
252 // This can be "else if" because flashing green on layer updates
253 // will also increment the overdraw if it happens to be turned on.
254 SkPaint transparentPaint;
255 transparentPaint.setColor(SK_ColorTRANSPARENT);
256 canvas->drawRect(bounds, transparentPaint);
257 }
Matt Sarett79756be2016-11-09 16:13:54 -0500258 }
Stan Iliev021693b2016-10-17 16:26:15 -0400259 } else {
Stan Iliev1843ac72017-09-20 18:05:35 -0400260 if (alphaMultiplier < 1.0f) {
261 // Non-layer draw for a view with getHasOverlappingRendering=false, will apply
262 // the alpha to the paint of each nested draw.
263 AlphaFilterCanvas alphaCanvas(canvas, alphaMultiplier);
264 displayList->draw(&alphaCanvas);
265 } else {
266 displayList->draw(canvas);
267 }
Stan Iliev021693b2016-10-17 16:26:15 -0400268 }
269 }
270}
271
272void RenderNodeDrawable::setViewProperties(const RenderProperties& properties, SkCanvas* canvas,
John Reck1bcacfd2017-11-03 10:12:19 -0700273 float* alphaMultiplier) {
Stan Iliev021693b2016-10-17 16:26:15 -0400274 if (properties.getLeft() != 0 || properties.getTop() != 0) {
275 canvas->translate(properties.getLeft(), properties.getTop());
276 }
277 if (properties.getStaticMatrix()) {
278 canvas->concat(*properties.getStaticMatrix());
279 } else if (properties.getAnimationMatrix()) {
280 canvas->concat(*properties.getAnimationMatrix());
281 }
282 if (properties.hasTransformMatrix()) {
283 if (properties.isTransformTranslateOnly()) {
284 canvas->translate(properties.getTranslationX(), properties.getTranslationY());
285 } else {
286 canvas->concat(*properties.getTransformMatrix());
287 }
288 }
289 const bool isLayer = properties.effectiveLayerType() != LayerType::None;
290 int clipFlags = properties.getClippingFlags();
291 if (properties.getAlpha() < 1) {
292 if (isLayer) {
John Reck1bcacfd2017-11-03 10:12:19 -0700293 clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
Stan Iliev021693b2016-10-17 16:26:15 -0400294 }
295 if (CC_LIKELY(isLayer || !properties.getHasOverlappingRendering())) {
296 *alphaMultiplier = properties.getAlpha();
297 } else {
298 // savelayer needed to create an offscreen buffer
299 Rect layerBounds(0, 0, properties.getWidth(), properties.getHeight());
300 if (clipFlags) {
301 properties.getClippingRectForFlags(clipFlags, &layerBounds);
John Reck1bcacfd2017-11-03 10:12:19 -0700302 clipFlags = 0; // all clipping done by savelayer
Stan Iliev021693b2016-10-17 16:26:15 -0400303 }
John Reck1bcacfd2017-11-03 10:12:19 -0700304 SkRect bounds = SkRect::MakeLTRB(layerBounds.left, layerBounds.top, layerBounds.right,
305 layerBounds.bottom);
306 canvas->saveLayerAlpha(&bounds, (int)(properties.getAlpha() * 255));
Stan Iliev021693b2016-10-17 16:26:15 -0400307 }
308
309 if (CC_UNLIKELY(ATRACE_ENABLED() && properties.promotedToLayer())) {
310 // pretend alpha always causes savelayer to warn about
311 // performance problem affecting old versions
312 ATRACE_FORMAT("alpha caused saveLayer %dx%d", properties.getWidth(),
John Reck1bcacfd2017-11-03 10:12:19 -0700313 properties.getHeight());
Stan Iliev021693b2016-10-17 16:26:15 -0400314 }
315 }
316
317 const SkRect* pendingClip = nullptr;
318 SkRect clipRect;
319
320 if (clipFlags) {
321 Rect tmpRect;
322 properties.getClippingRectForFlags(clipFlags, &tmpRect);
323 clipRect = tmpRect.toSkRect();
324 pendingClip = &clipRect;
325 }
326
327 if (properties.getRevealClip().willClip()) {
Mike Reed6c67f1d2016-12-14 10:29:54 -0500328 canvas->clipPath(*properties.getRevealClip().getPath(), SkClipOp::kIntersect, true);
Stan Iliev021693b2016-10-17 16:26:15 -0400329 } else if (properties.getOutline().willClip()) {
330 clipOutline(properties.getOutline(), canvas, pendingClip);
331 pendingClip = nullptr;
332 }
333
334 if (pendingClip) {
335 canvas->clipRect(*pendingClip);
336 }
337}
338
Chris Blume7b8a8082018-11-30 15:51:58 -0800339} // namespace skiapipeline
340} // namespace uirenderer
341} // namespace android