blob: c01021221f37f4d9165fe6c8102e00698f55bfa4 [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
John Reck5cb290b2021-02-01 13:47:31 -050023#include <include/effects/SkImageFilters.h>
24
Ben Wagner1d155332018-08-20 18:36:05 -040025#include <optional>
26
Stan Iliev021693b2016-10-17 16:26:15 -040027namespace android {
28namespace uirenderer {
29namespace skiapipeline {
30
John Reckd9d7f122018-05-03 14:40:56 -070031RenderNodeDrawable::RenderNodeDrawable(RenderNode* node, SkCanvas* canvas, bool composeLayer,
32 bool inReorderingSection)
33 : mRenderNode(node)
34 , mRecordedTransform(canvas->getTotalMatrix())
35 , mComposeLayer(composeLayer)
36 , mInReorderingSection(inReorderingSection) {}
37
38RenderNodeDrawable::~RenderNodeDrawable() {
39 // Just here to move the destructor into the cpp file where we can access RenderNode.
40
41 // TODO: Detangle the header nightmare.
42}
43
John Reck1bcacfd2017-11-03 10:12:19 -070044void RenderNodeDrawable::drawBackwardsProjectedNodes(SkCanvas* canvas,
45 const SkiaDisplayList& displayList,
Nathaniel Nifong2945bff2019-11-25 09:34:21 -050046 int nestLevel) const {
Stan Ilievdb45a4b2016-11-08 14:18:31 -050047 LOG_ALWAYS_FATAL_IF(0 == nestLevel && !displayList.mProjectionReceiver);
48 for (auto& child : displayList.mChildNodes) {
49 const RenderProperties& childProperties = child.getNodeProperties();
50
John Reck1bcacfd2017-11-03 10:12:19 -070051 // immediate children cannot be projected on their parent
Stan Ilievdb45a4b2016-11-08 14:18:31 -050052 if (childProperties.getProjectBackwards() && nestLevel > 0) {
53 SkAutoCanvasRestore acr2(canvas, true);
John Reck1bcacfd2017-11-03 10:12:19 -070054 // Apply recorded matrix, which is a total matrix saved at recording time to avoid
55 // replaying all DL commands.
Stan Ilievdb45a4b2016-11-08 14:18:31 -050056 canvas->concat(child.getRecordedMatrix());
57 child.drawContent(canvas);
58 }
59
John Reck1bcacfd2017-11-03 10:12:19 -070060 // skip walking sub-nodes if current display list contains a receiver with exception of
61 // level 0, which is a known receiver
Stan Ilievdb45a4b2016-11-08 14:18:31 -050062 if (0 == nestLevel || !displayList.containsProjectionReceiver()) {
63 SkAutoCanvasRestore acr(canvas, true);
64 SkMatrix nodeMatrix;
65 mat4 hwuiMatrix(child.getRecordedMatrix());
John Reckbe671952021-01-13 22:39:32 -050066 const RenderNode* childNode = child.getRenderNode();
Stan Ilievdb45a4b2016-11-08 14:18:31 -050067 childNode->applyViewPropertyTransforms(hwuiMatrix);
68 hwuiMatrix.copyTo(nodeMatrix);
69 canvas->concat(nodeMatrix);
John Reckbe671952021-01-13 22:39:32 -050070 const SkiaDisplayList* childDisplayList = childNode->getDisplayList().asSkiaDl();
Stan Ilievdb45a4b2016-11-08 14:18:31 -050071 if (childDisplayList) {
John Reck1bcacfd2017-11-03 10:12:19 -070072 drawBackwardsProjectedNodes(canvas, *childDisplayList, nestLevel + 1);
Stan Ilievdb45a4b2016-11-08 14:18:31 -050073 }
74 }
75 }
76}
77
Stan Iliev021693b2016-10-17 16:26:15 -040078static void clipOutline(const Outline& outline, SkCanvas* canvas, const SkRect* pendingClip) {
Stan Iliev021693b2016-10-17 16:26:15 -040079 Rect possibleRect;
80 float radius;
Derek Sollenbergerf209c062017-05-26 12:11:34 -040081
82 /* To match the existing HWUI behavior we only supports rectangles or
83 * rounded rectangles; passing in a more complicated outline fails silently.
84 */
85 if (!outline.getAsRoundRect(&possibleRect, &radius)) {
86 if (pendingClip) {
87 canvas->clipRect(*pendingClip);
88 }
89 return;
90 }
91
Stan Iliev021693b2016-10-17 16:26:15 -040092 SkRect rect = possibleRect.toSkRect();
93 if (radius != 0.0f) {
94 if (pendingClip && !pendingClip->contains(rect)) {
95 canvas->clipRect(*pendingClip);
96 }
Mike Reed6c67f1d2016-12-14 10:29:54 -050097 canvas->clipRRect(SkRRect::MakeRectXY(rect, radius, radius), SkClipOp::kIntersect, true);
Stan Iliev021693b2016-10-17 16:26:15 -040098 } else {
99 if (pendingClip) {
100 (void)rect.intersect(*pendingClip);
101 }
102 canvas->clipRect(rect);
103 }
104}
105
106const RenderProperties& RenderNodeDrawable::getNodeProperties() const {
107 return mRenderNode->properties();
108}
109
110void RenderNodeDrawable::onDraw(SkCanvas* canvas) {
John Reck1bcacfd2017-11-03 10:12:19 -0700111 // negative and positive Z order are drawn out of order, if this render node drawable is in
112 // a reordering section
Stan Iliev2f06e8a2016-11-02 15:29:03 -0400113 if ((!mInReorderingSection) || MathUtils::isZero(mRenderNode->properties().getZ())) {
Stan Iliev021693b2016-10-17 16:26:15 -0400114 this->forceDraw(canvas);
115 }
116}
117
John Reckf96b2842018-11-29 09:44:10 -0800118class MarkDraw {
119public:
120 explicit MarkDraw(SkCanvas& canvas, RenderNode& node) : mCanvas(canvas), mNode(node) {
121 if (CC_UNLIKELY(Properties::skpCaptureEnabled)) {
122 mNode.markDrawStart(mCanvas);
123 }
124 }
125 ~MarkDraw() {
126 if (CC_UNLIKELY(Properties::skpCaptureEnabled)) {
127 mNode.markDrawEnd(mCanvas);
128 }
129 }
John Reck283bb462018-12-13 16:40:14 -0800130
John Reckf96b2842018-11-29 09:44:10 -0800131private:
132 SkCanvas& mCanvas;
133 RenderNode& mNode;
134};
135
Nathaniel Nifong2945bff2019-11-25 09:34:21 -0500136void RenderNodeDrawable::forceDraw(SkCanvas* canvas) const {
Stan Iliev021693b2016-10-17 16:26:15 -0400137 RenderNode* renderNode = mRenderNode.get();
John Reckf96b2842018-11-29 09:44:10 -0800138 MarkDraw _marker{*canvas, *renderNode};
Stan Iliev021693b2016-10-17 16:26:15 -0400139
140 // We only respect the nothingToDraw check when we are composing a layer. This
141 // ensures that we paint the layer even if it is not currently visible in the
142 // event that the properties change and it becomes visible.
Yuqian Li5ebbc8e2017-11-29 09:53:06 -0500143 if ((mProjectedDisplayList == nullptr && !renderNode->isRenderable()) ||
John Reck283bb462018-12-13 16:40:14 -0800144 (renderNode->nothingToDraw() && mComposeLayer)) {
Stan Iliev021693b2016-10-17 16:26:15 -0400145 return;
146 }
147
John Reckbe671952021-01-13 22:39:32 -0500148 SkiaDisplayList* displayList = renderNode->getDisplayList().asSkiaDl();
Stan Iliev021693b2016-10-17 16:26:15 -0400149
150 SkAutoCanvasRestore acr(canvas, true);
Stan Iliev021693b2016-10-17 16:26:15 -0400151 const RenderProperties& properties = this->getNodeProperties();
John Reck1bcacfd2017-11-03 10:12:19 -0700152 // pass this outline to the children that may clip backward projected nodes
153 displayList->mProjectedOutline =
154 displayList->containsProjectionReceiver() ? &properties.getOutline() : nullptr;
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500155 if (!properties.getProjectBackwards()) {
156 drawContent(canvas);
157 if (mProjectedDisplayList) {
John Reck1bcacfd2017-11-03 10:12:19 -0700158 acr.restore(); // draw projected children using parent matrix
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500159 LOG_ALWAYS_FATAL_IF(!mProjectedDisplayList->mProjectedOutline);
160 const bool shouldClip = mProjectedDisplayList->mProjectedOutline->getPath();
161 SkAutoCanvasRestore acr2(canvas, shouldClip);
Stan Iliev54d70322018-06-14 18:00:10 -0400162 canvas->setMatrix(mProjectedDisplayList->mParentMatrix);
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500163 if (shouldClip) {
Stan Iliev8b7cc512019-02-22 10:16:43 -0500164 canvas->clipPath(*mProjectedDisplayList->mProjectedOutline->getPath());
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500165 }
166 drawBackwardsProjectedNodes(canvas, *mProjectedDisplayList);
Stan Iliev021693b2016-10-17 16:26:15 -0400167 }
Stan Iliev021693b2016-10-17 16:26:15 -0400168 }
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500169 displayList->mProjectedOutline = nullptr;
Stan Iliev021693b2016-10-17 16:26:15 -0400170}
171
John Reck1bcacfd2017-11-03 10:12:19 -0700172static bool layerNeedsPaint(const LayerProperties& properties, float alphaMultiplier,
173 SkPaint* paint) {
174 if (alphaMultiplier < 1.0f || properties.alpha() < 255 ||
Nader Jawad390d6e82020-09-24 21:35:03 -0700175 properties.xferMode() != SkBlendMode::kSrcOver || properties.getColorFilter() != nullptr ||
John Reck5cb290b2021-02-01 13:47:31 -0500176 properties.getImageFilter() != nullptr || !properties.getStretchEffect().isEmpty()) {
Stan Iliev021693b2016-10-17 16:26:15 -0400177 paint->setAlpha(properties.alpha() * alphaMultiplier);
178 paint->setBlendMode(properties.xferMode());
Ben Wagnerc1a8a462018-07-12 12:41:28 -0400179 paint->setColorFilter(sk_ref_sp(properties.getColorFilter()));
John Reck5cb290b2021-02-01 13:47:31 -0500180
181 sk_sp<SkImageFilter> imageFilter = sk_ref_sp(properties.getImageFilter());
182 sk_sp<SkImageFilter> stretchFilter = properties.getStretchEffect().getImageFilter();
183 sk_sp<SkImageFilter> filter;
184 if (imageFilter && stretchFilter) {
185 filter = SkImageFilters::Compose(
186 std::move(stretchFilter),
187 std::move(imageFilter)
188 );
189 } else if (stretchFilter) {
190 filter = std::move(stretchFilter);
191 } else {
192 filter = std::move(imageFilter);
193 }
194 paint->setImageFilter(std::move(filter));
Stan Iliev021693b2016-10-17 16:26:15 -0400195 return true;
196 }
197 return false;
198}
199
Stan Iliev1843ac72017-09-20 18:05:35 -0400200class AlphaFilterCanvas : public SkPaintFilterCanvas {
201public:
202 AlphaFilterCanvas(SkCanvas* canvas, float alpha) : SkPaintFilterCanvas(canvas), mAlpha(alpha) {}
John Reck1bcacfd2017-11-03 10:12:19 -0700203
Stan Iliev1843ac72017-09-20 18:05:35 -0400204protected:
Ben Wagner62b38942019-04-15 11:59:33 -0400205 bool onFilter(SkPaint& paint) const override {
206 paint.setAlpha((uint8_t)paint.getAlpha() * mAlpha);
Stan Iliev1843ac72017-09-20 18:05:35 -0400207 return true;
208 }
209 void onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) override {
210 // We unroll the drawable using "this" canvas, so that draw calls contained inside will
Ben Wagner62b38942019-04-15 11:59:33 -0400211 // get their alpha applied. The default SkPaintFilterCanvas::onDrawDrawable does not unroll.
Stan Iliev1843ac72017-09-20 18:05:35 -0400212 drawable->draw(this, matrix);
213 }
John Reck1bcacfd2017-11-03 10:12:19 -0700214
Stan Iliev1843ac72017-09-20 18:05:35 -0400215private:
216 float mAlpha;
217};
218
Stan Iliev021693b2016-10-17 16:26:15 -0400219void RenderNodeDrawable::drawContent(SkCanvas* canvas) const {
220 RenderNode* renderNode = mRenderNode.get();
221 float alphaMultiplier = 1.0f;
222 const RenderProperties& properties = renderNode->properties();
223
224 // If we are drawing the contents of layer, we don't want to apply any of
225 // the RenderNode's properties during this pass. Those will all be applied
226 // when the layer is composited.
227 if (mComposeLayer) {
228 setViewProperties(properties, canvas, &alphaMultiplier);
229 }
John Reckbe671952021-01-13 22:39:32 -0500230 SkiaDisplayList* displayList = mRenderNode->getDisplayList().asSkiaDl();
Stan Iliev54d70322018-06-14 18:00:10 -0400231 displayList->mParentMatrix = canvas->getTotalMatrix();
Stan Iliev021693b2016-10-17 16:26:15 -0400232
John Reck1bcacfd2017-11-03 10:12:19 -0700233 // TODO should we let the bound of the drawable do this for us?
Stan Iliev021693b2016-10-17 16:26:15 -0400234 const SkRect bounds = SkRect::MakeWH(properties.getWidth(), properties.getHeight());
235 bool quickRejected = properties.getClipToBounds() && canvas->quickReject(bounds);
236 if (!quickRejected) {
John Reckbe671952021-01-13 22:39:32 -0500237 SkiaDisplayList* displayList = renderNode->getDisplayList().asSkiaDl();
Stan Iliev021693b2016-10-17 16:26:15 -0400238 const LayerProperties& layerProperties = properties.layerProperties();
239 // composing a hardware layer
240 if (renderNode->getLayerSurface() && mComposeLayer) {
241 SkASSERT(properties.effectiveLayerType() == LayerType::RenderLayer);
Stan Iliev7e100e92018-01-22 10:36:33 -0500242 SkPaint paint;
243 layerNeedsPaint(layerProperties, alphaMultiplier, &paint);
Mike Reed7994a312021-01-28 18:06:26 -0500244 SkSamplingOptions sampling(SkFilterMode::kLinear);
Derek Sollenberger03e6cff72017-12-04 15:07:08 -0500245
246 // surfaces for layers are created on LAYER_SIZE boundaries (which are >= layer size) so
247 // we need to restrict the portion of the surface drawn to the size of the renderNode.
248 SkASSERT(renderNode->getLayerSurface()->width() >= bounds.width());
249 SkASSERT(renderNode->getLayerSurface()->height() >= bounds.height());
Nathaniel Nifong2945bff2019-11-25 09:34:21 -0500250
251 // If SKP recording is active save an annotation that indicates this drawImageRect
252 // could also be rendered with the commands saved at ID associated with this node.
253 if (CC_UNLIKELY(Properties::skpCaptureEnabled)) {
254 canvas->drawAnnotation(bounds, String8::format(
255 "SurfaceID|%" PRId64, renderNode->uniqueId()).c_str(), nullptr);
256 }
257 canvas->drawImageRect(renderNode->getLayerSurface()->makeImageSnapshot(), bounds,
Mike Reed7994a312021-01-28 18:06:26 -0500258 bounds, sampling, &paint, SkCanvas::kStrict_SrcRectConstraint);
Matt Sarett79756be2016-11-09 16:13:54 -0500259
Matt Sarettf58cc922016-11-14 18:33:38 -0500260 if (!renderNode->getSkiaLayer()->hasRenderedSinceRepaint) {
Matt Sarett79756be2016-11-09 16:13:54 -0500261 renderNode->getSkiaLayer()->hasRenderedSinceRepaint = true;
Matt Sarettf58cc922016-11-14 18:33:38 -0500262 if (CC_UNLIKELY(Properties::debugLayersUpdates)) {
263 SkPaint layerPaint;
264 layerPaint.setColor(0x7f00ff00);
265 canvas->drawRect(bounds, layerPaint);
266 } else if (CC_UNLIKELY(Properties::debugOverdraw)) {
267 // Render transparent rect to increment overdraw for repaint area.
268 // This can be "else if" because flashing green on layer updates
269 // will also increment the overdraw if it happens to be turned on.
270 SkPaint transparentPaint;
271 transparentPaint.setColor(SK_ColorTRANSPARENT);
272 canvas->drawRect(bounds, transparentPaint);
273 }
Matt Sarett79756be2016-11-09 16:13:54 -0500274 }
Stan Iliev021693b2016-10-17 16:26:15 -0400275 } else {
Stan Iliev1843ac72017-09-20 18:05:35 -0400276 if (alphaMultiplier < 1.0f) {
277 // Non-layer draw for a view with getHasOverlappingRendering=false, will apply
278 // the alpha to the paint of each nested draw.
279 AlphaFilterCanvas alphaCanvas(canvas, alphaMultiplier);
280 displayList->draw(&alphaCanvas);
281 } else {
282 displayList->draw(canvas);
283 }
Stan Iliev021693b2016-10-17 16:26:15 -0400284 }
285 }
286}
287
288void RenderNodeDrawable::setViewProperties(const RenderProperties& properties, SkCanvas* canvas,
John Reck1bcacfd2017-11-03 10:12:19 -0700289 float* alphaMultiplier) {
Stan Iliev021693b2016-10-17 16:26:15 -0400290 if (properties.getLeft() != 0 || properties.getTop() != 0) {
291 canvas->translate(properties.getLeft(), properties.getTop());
292 }
293 if (properties.getStaticMatrix()) {
294 canvas->concat(*properties.getStaticMatrix());
295 } else if (properties.getAnimationMatrix()) {
296 canvas->concat(*properties.getAnimationMatrix());
297 }
298 if (properties.hasTransformMatrix()) {
299 if (properties.isTransformTranslateOnly()) {
300 canvas->translate(properties.getTranslationX(), properties.getTranslationY());
301 } else {
302 canvas->concat(*properties.getTransformMatrix());
303 }
304 }
305 const bool isLayer = properties.effectiveLayerType() != LayerType::None;
306 int clipFlags = properties.getClippingFlags();
307 if (properties.getAlpha() < 1) {
308 if (isLayer) {
John Reck1bcacfd2017-11-03 10:12:19 -0700309 clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
Stan Iliev021693b2016-10-17 16:26:15 -0400310 }
311 if (CC_LIKELY(isLayer || !properties.getHasOverlappingRendering())) {
312 *alphaMultiplier = properties.getAlpha();
313 } else {
314 // savelayer needed to create an offscreen buffer
315 Rect layerBounds(0, 0, properties.getWidth(), properties.getHeight());
316 if (clipFlags) {
317 properties.getClippingRectForFlags(clipFlags, &layerBounds);
John Reck1bcacfd2017-11-03 10:12:19 -0700318 clipFlags = 0; // all clipping done by savelayer
Stan Iliev021693b2016-10-17 16:26:15 -0400319 }
John Reck1bcacfd2017-11-03 10:12:19 -0700320 SkRect bounds = SkRect::MakeLTRB(layerBounds.left, layerBounds.top, layerBounds.right,
321 layerBounds.bottom);
322 canvas->saveLayerAlpha(&bounds, (int)(properties.getAlpha() * 255));
Stan Iliev021693b2016-10-17 16:26:15 -0400323 }
324
325 if (CC_UNLIKELY(ATRACE_ENABLED() && properties.promotedToLayer())) {
326 // pretend alpha always causes savelayer to warn about
327 // performance problem affecting old versions
328 ATRACE_FORMAT("alpha caused saveLayer %dx%d", properties.getWidth(),
John Reck1bcacfd2017-11-03 10:12:19 -0700329 properties.getHeight());
Stan Iliev021693b2016-10-17 16:26:15 -0400330 }
331 }
332
333 const SkRect* pendingClip = nullptr;
334 SkRect clipRect;
335
336 if (clipFlags) {
337 Rect tmpRect;
338 properties.getClippingRectForFlags(clipFlags, &tmpRect);
339 clipRect = tmpRect.toSkRect();
340 pendingClip = &clipRect;
341 }
342
343 if (properties.getRevealClip().willClip()) {
Mike Reed6c67f1d2016-12-14 10:29:54 -0500344 canvas->clipPath(*properties.getRevealClip().getPath(), SkClipOp::kIntersect, true);
Stan Iliev021693b2016-10-17 16:26:15 -0400345 } else if (properties.getOutline().willClip()) {
346 clipOutline(properties.getOutline(), canvas, pendingClip);
347 pendingClip = nullptr;
348 }
349
350 if (pendingClip) {
351 canvas->clipRect(*pendingClip);
352 }
353}
354
Chris Blume7b8a8082018-11-30 15:51:58 -0800355} // namespace skiapipeline
356} // namespace uirenderer
357} // namespace android