blob: 5627a7e27232fdd6e69260ad357d6f0326826382 [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"
Nader Jawad2dc632a2021-03-29 18:51:29 -070021#include "TransformCanvas.h"
Stan Iliev021693b2016-10-17 16:26:15 -040022#include "utils/TraceUtils.h"
23
John Reck5cb290b2021-02-01 13:47:31 -050024#include <include/effects/SkImageFilters.h>
25
Ben Wagner1d155332018-08-20 18:36:05 -040026#include <optional>
27
Stan Iliev021693b2016-10-17 16:26:15 -040028namespace android {
29namespace uirenderer {
30namespace skiapipeline {
31
John Reckd9d7f122018-05-03 14:40:56 -070032RenderNodeDrawable::RenderNodeDrawable(RenderNode* node, SkCanvas* canvas, bool composeLayer,
33 bool inReorderingSection)
34 : mRenderNode(node)
35 , mRecordedTransform(canvas->getTotalMatrix())
36 , mComposeLayer(composeLayer)
37 , mInReorderingSection(inReorderingSection) {}
38
39RenderNodeDrawable::~RenderNodeDrawable() {
40 // Just here to move the destructor into the cpp file where we can access RenderNode.
41
42 // TODO: Detangle the header nightmare.
43}
44
John Reck1bcacfd2017-11-03 10:12:19 -070045void RenderNodeDrawable::drawBackwardsProjectedNodes(SkCanvas* canvas,
46 const SkiaDisplayList& displayList,
Nathaniel Nifong2945bff2019-11-25 09:34:21 -050047 int nestLevel) const {
Stan Ilievdb45a4b2016-11-08 14:18:31 -050048 LOG_ALWAYS_FATAL_IF(0 == nestLevel && !displayList.mProjectionReceiver);
49 for (auto& child : displayList.mChildNodes) {
50 const RenderProperties& childProperties = child.getNodeProperties();
51
John Reck1bcacfd2017-11-03 10:12:19 -070052 // immediate children cannot be projected on their parent
Stan Ilievdb45a4b2016-11-08 14:18:31 -050053 if (childProperties.getProjectBackwards() && nestLevel > 0) {
54 SkAutoCanvasRestore acr2(canvas, true);
John Reck1bcacfd2017-11-03 10:12:19 -070055 // Apply recorded matrix, which is a total matrix saved at recording time to avoid
56 // replaying all DL commands.
Stan Ilievdb45a4b2016-11-08 14:18:31 -050057 canvas->concat(child.getRecordedMatrix());
58 child.drawContent(canvas);
59 }
60
John Reck1bcacfd2017-11-03 10:12:19 -070061 // skip walking sub-nodes if current display list contains a receiver with exception of
62 // level 0, which is a known receiver
Stan Ilievdb45a4b2016-11-08 14:18:31 -050063 if (0 == nestLevel || !displayList.containsProjectionReceiver()) {
64 SkAutoCanvasRestore acr(canvas, true);
65 SkMatrix nodeMatrix;
66 mat4 hwuiMatrix(child.getRecordedMatrix());
John Reckbe671952021-01-13 22:39:32 -050067 const RenderNode* childNode = child.getRenderNode();
Stan Ilievdb45a4b2016-11-08 14:18:31 -050068 childNode->applyViewPropertyTransforms(hwuiMatrix);
69 hwuiMatrix.copyTo(nodeMatrix);
70 canvas->concat(nodeMatrix);
John Reckbe671952021-01-13 22:39:32 -050071 const SkiaDisplayList* childDisplayList = childNode->getDisplayList().asSkiaDl();
Stan Ilievdb45a4b2016-11-08 14:18:31 -050072 if (childDisplayList) {
John Reck1bcacfd2017-11-03 10:12:19 -070073 drawBackwardsProjectedNodes(canvas, *childDisplayList, nestLevel + 1);
Stan Ilievdb45a4b2016-11-08 14:18:31 -050074 }
75 }
76 }
77}
78
Stan Iliev021693b2016-10-17 16:26:15 -040079static void clipOutline(const Outline& outline, SkCanvas* canvas, const SkRect* pendingClip) {
Stan Iliev021693b2016-10-17 16:26:15 -040080 Rect possibleRect;
81 float radius;
Derek Sollenbergerf209c062017-05-26 12:11:34 -040082
83 /* To match the existing HWUI behavior we only supports rectangles or
84 * rounded rectangles; passing in a more complicated outline fails silently.
85 */
86 if (!outline.getAsRoundRect(&possibleRect, &radius)) {
87 if (pendingClip) {
88 canvas->clipRect(*pendingClip);
89 }
90 return;
91 }
92
Stan Iliev021693b2016-10-17 16:26:15 -040093 SkRect rect = possibleRect.toSkRect();
94 if (radius != 0.0f) {
95 if (pendingClip && !pendingClip->contains(rect)) {
96 canvas->clipRect(*pendingClip);
97 }
Mike Reed6c67f1d2016-12-14 10:29:54 -050098 canvas->clipRRect(SkRRect::MakeRectXY(rect, radius, radius), SkClipOp::kIntersect, true);
Stan Iliev021693b2016-10-17 16:26:15 -040099 } else {
100 if (pendingClip) {
101 (void)rect.intersect(*pendingClip);
102 }
103 canvas->clipRect(rect);
104 }
105}
106
107const RenderProperties& RenderNodeDrawable::getNodeProperties() const {
108 return mRenderNode->properties();
109}
110
111void RenderNodeDrawable::onDraw(SkCanvas* canvas) {
John Reck1bcacfd2017-11-03 10:12:19 -0700112 // negative and positive Z order are drawn out of order, if this render node drawable is in
113 // a reordering section
Stan Iliev2f06e8a2016-11-02 15:29:03 -0400114 if ((!mInReorderingSection) || MathUtils::isZero(mRenderNode->properties().getZ())) {
Stan Iliev021693b2016-10-17 16:26:15 -0400115 this->forceDraw(canvas);
116 }
117}
118
John Reckf96b2842018-11-29 09:44:10 -0800119class MarkDraw {
120public:
121 explicit MarkDraw(SkCanvas& canvas, RenderNode& node) : mCanvas(canvas), mNode(node) {
122 if (CC_UNLIKELY(Properties::skpCaptureEnabled)) {
123 mNode.markDrawStart(mCanvas);
124 }
125 }
126 ~MarkDraw() {
127 if (CC_UNLIKELY(Properties::skpCaptureEnabled)) {
128 mNode.markDrawEnd(mCanvas);
129 }
130 }
John Reck283bb462018-12-13 16:40:14 -0800131
John Reckf96b2842018-11-29 09:44:10 -0800132private:
133 SkCanvas& mCanvas;
134 RenderNode& mNode;
135};
136
Nathaniel Nifong2945bff2019-11-25 09:34:21 -0500137void RenderNodeDrawable::forceDraw(SkCanvas* canvas) const {
Stan Iliev021693b2016-10-17 16:26:15 -0400138 RenderNode* renderNode = mRenderNode.get();
John Reckf96b2842018-11-29 09:44:10 -0800139 MarkDraw _marker{*canvas, *renderNode};
Stan Iliev021693b2016-10-17 16:26:15 -0400140
141 // We only respect the nothingToDraw check when we are composing a layer. This
142 // ensures that we paint the layer even if it is not currently visible in the
143 // event that the properties change and it becomes visible.
Yuqian Li5ebbc8e2017-11-29 09:53:06 -0500144 if ((mProjectedDisplayList == nullptr && !renderNode->isRenderable()) ||
John Reck283bb462018-12-13 16:40:14 -0800145 (renderNode->nothingToDraw() && mComposeLayer)) {
Stan Iliev021693b2016-10-17 16:26:15 -0400146 return;
147 }
148
John Reckbe671952021-01-13 22:39:32 -0500149 SkiaDisplayList* displayList = renderNode->getDisplayList().asSkiaDl();
Stan Iliev021693b2016-10-17 16:26:15 -0400150
151 SkAutoCanvasRestore acr(canvas, true);
Stan Iliev021693b2016-10-17 16:26:15 -0400152 const RenderProperties& properties = this->getNodeProperties();
John Reck1bcacfd2017-11-03 10:12:19 -0700153 // pass this outline to the children that may clip backward projected nodes
154 displayList->mProjectedOutline =
155 displayList->containsProjectionReceiver() ? &properties.getOutline() : nullptr;
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500156 if (!properties.getProjectBackwards()) {
157 drawContent(canvas);
158 if (mProjectedDisplayList) {
John Reck1bcacfd2017-11-03 10:12:19 -0700159 acr.restore(); // draw projected children using parent matrix
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500160 LOG_ALWAYS_FATAL_IF(!mProjectedDisplayList->mProjectedOutline);
161 const bool shouldClip = mProjectedDisplayList->mProjectedOutline->getPath();
162 SkAutoCanvasRestore acr2(canvas, shouldClip);
Stan Iliev54d70322018-06-14 18:00:10 -0400163 canvas->setMatrix(mProjectedDisplayList->mParentMatrix);
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500164 if (shouldClip) {
Stan Iliev8b7cc512019-02-22 10:16:43 -0500165 canvas->clipPath(*mProjectedDisplayList->mProjectedOutline->getPath());
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500166 }
167 drawBackwardsProjectedNodes(canvas, *mProjectedDisplayList);
Stan Iliev021693b2016-10-17 16:26:15 -0400168 }
Stan Iliev021693b2016-10-17 16:26:15 -0400169 }
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500170 displayList->mProjectedOutline = nullptr;
Stan Iliev021693b2016-10-17 16:26:15 -0400171}
172
Nader Jawad6701a602021-02-23 18:14:22 -0800173static bool layerNeedsPaint(const sk_sp<SkImage>& snapshotImage, const LayerProperties& properties,
174 float alphaMultiplier, SkPaint* paint) {
John Reck1bcacfd2017-11-03 10:12:19 -0700175 if (alphaMultiplier < 1.0f || properties.alpha() < 255 ||
Nader Jawad390d6e82020-09-24 21:35:03 -0700176 properties.xferMode() != SkBlendMode::kSrcOver || properties.getColorFilter() != nullptr ||
John Reck5cb290b2021-02-01 13:47:31 -0500177 properties.getImageFilter() != nullptr || !properties.getStretchEffect().isEmpty()) {
Stan Iliev021693b2016-10-17 16:26:15 -0400178 paint->setAlpha(properties.alpha() * alphaMultiplier);
179 paint->setBlendMode(properties.xferMode());
Ben Wagnerc1a8a462018-07-12 12:41:28 -0400180 paint->setColorFilter(sk_ref_sp(properties.getColorFilter()));
John Reck5cb290b2021-02-01 13:47:31 -0500181
182 sk_sp<SkImageFilter> imageFilter = sk_ref_sp(properties.getImageFilter());
Nader Jawad6701a602021-02-23 18:14:22 -0800183 sk_sp<SkImageFilter> stretchFilter =
184 properties.getStretchEffect().getImageFilter(snapshotImage);
John Reck5cb290b2021-02-01 13:47:31 -0500185 sk_sp<SkImageFilter> filter;
186 if (imageFilter && stretchFilter) {
187 filter = SkImageFilters::Compose(
188 std::move(stretchFilter),
189 std::move(imageFilter)
190 );
191 } else if (stretchFilter) {
192 filter = std::move(stretchFilter);
193 } else {
194 filter = std::move(imageFilter);
195 }
196 paint->setImageFilter(std::move(filter));
Stan Iliev021693b2016-10-17 16:26:15 -0400197 return true;
198 }
199 return false;
200}
201
Stan Iliev1843ac72017-09-20 18:05:35 -0400202class AlphaFilterCanvas : public SkPaintFilterCanvas {
203public:
204 AlphaFilterCanvas(SkCanvas* canvas, float alpha) : SkPaintFilterCanvas(canvas), mAlpha(alpha) {}
John Reck1bcacfd2017-11-03 10:12:19 -0700205
Stan Iliev1843ac72017-09-20 18:05:35 -0400206protected:
Ben Wagner62b38942019-04-15 11:59:33 -0400207 bool onFilter(SkPaint& paint) const override {
208 paint.setAlpha((uint8_t)paint.getAlpha() * mAlpha);
Stan Iliev1843ac72017-09-20 18:05:35 -0400209 return true;
210 }
211 void onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) override {
212 // We unroll the drawable using "this" canvas, so that draw calls contained inside will
Ben Wagner62b38942019-04-15 11:59:33 -0400213 // get their alpha applied. The default SkPaintFilterCanvas::onDrawDrawable does not unroll.
Stan Iliev1843ac72017-09-20 18:05:35 -0400214 drawable->draw(this, matrix);
215 }
John Reck1bcacfd2017-11-03 10:12:19 -0700216
Stan Iliev1843ac72017-09-20 18:05:35 -0400217private:
218 float mAlpha;
219};
220
Stan Iliev021693b2016-10-17 16:26:15 -0400221void RenderNodeDrawable::drawContent(SkCanvas* canvas) const {
222 RenderNode* renderNode = mRenderNode.get();
223 float alphaMultiplier = 1.0f;
224 const RenderProperties& properties = renderNode->properties();
225
226 // If we are drawing the contents of layer, we don't want to apply any of
227 // the RenderNode's properties during this pass. Those will all be applied
228 // when the layer is composited.
229 if (mComposeLayer) {
230 setViewProperties(properties, canvas, &alphaMultiplier);
231 }
John Reckbe671952021-01-13 22:39:32 -0500232 SkiaDisplayList* displayList = mRenderNode->getDisplayList().asSkiaDl();
Stan Iliev54d70322018-06-14 18:00:10 -0400233 displayList->mParentMatrix = canvas->getTotalMatrix();
Stan Iliev021693b2016-10-17 16:26:15 -0400234
John Reck1bcacfd2017-11-03 10:12:19 -0700235 // TODO should we let the bound of the drawable do this for us?
Stan Iliev021693b2016-10-17 16:26:15 -0400236 const SkRect bounds = SkRect::MakeWH(properties.getWidth(), properties.getHeight());
237 bool quickRejected = properties.getClipToBounds() && canvas->quickReject(bounds);
238 if (!quickRejected) {
John Reckbe671952021-01-13 22:39:32 -0500239 SkiaDisplayList* displayList = renderNode->getDisplayList().asSkiaDl();
Stan Iliev021693b2016-10-17 16:26:15 -0400240 const LayerProperties& layerProperties = properties.layerProperties();
241 // composing a hardware layer
242 if (renderNode->getLayerSurface() && mComposeLayer) {
243 SkASSERT(properties.effectiveLayerType() == LayerType::RenderLayer);
Stan Iliev7e100e92018-01-22 10:36:33 -0500244 SkPaint paint;
Nader Jawad6701a602021-02-23 18:14:22 -0800245 sk_sp<SkImage> snapshotImage = renderNode->getLayerSurface()->makeImageSnapshot();
246 layerNeedsPaint(snapshotImage, layerProperties, alphaMultiplier, &paint);
Mike Reed7994a312021-01-28 18:06:26 -0500247 SkSamplingOptions sampling(SkFilterMode::kLinear);
Derek Sollenberger03e6cff72017-12-04 15:07:08 -0500248
249 // surfaces for layers are created on LAYER_SIZE boundaries (which are >= layer size) so
250 // we need to restrict the portion of the surface drawn to the size of the renderNode.
251 SkASSERT(renderNode->getLayerSurface()->width() >= bounds.width());
252 SkASSERT(renderNode->getLayerSurface()->height() >= bounds.height());
Nathaniel Nifong2945bff2019-11-25 09:34:21 -0500253
254 // If SKP recording is active save an annotation that indicates this drawImageRect
255 // could also be rendered with the commands saved at ID associated with this node.
256 if (CC_UNLIKELY(Properties::skpCaptureEnabled)) {
257 canvas->drawAnnotation(bounds, String8::format(
258 "SurfaceID|%" PRId64, renderNode->uniqueId()).c_str(), nullptr);
259 }
Nader Jawad2dc632a2021-03-29 18:51:29 -0700260
261 if (renderNode->hasHolePunches()) {
262 TransformCanvas transformCanvas(canvas);
263 displayList->draw(&transformCanvas);
264 }
Nader Jawad6701a602021-02-23 18:14:22 -0800265 canvas->drawImageRect(snapshotImage, bounds, bounds, sampling, &paint,
266 SkCanvas::kStrict_SrcRectConstraint);
Matt Sarett79756be2016-11-09 16:13:54 -0500267
Matt Sarettf58cc922016-11-14 18:33:38 -0500268 if (!renderNode->getSkiaLayer()->hasRenderedSinceRepaint) {
Matt Sarett79756be2016-11-09 16:13:54 -0500269 renderNode->getSkiaLayer()->hasRenderedSinceRepaint = true;
Matt Sarettf58cc922016-11-14 18:33:38 -0500270 if (CC_UNLIKELY(Properties::debugLayersUpdates)) {
271 SkPaint layerPaint;
272 layerPaint.setColor(0x7f00ff00);
273 canvas->drawRect(bounds, layerPaint);
274 } else if (CC_UNLIKELY(Properties::debugOverdraw)) {
275 // Render transparent rect to increment overdraw for repaint area.
276 // This can be "else if" because flashing green on layer updates
277 // will also increment the overdraw if it happens to be turned on.
278 SkPaint transparentPaint;
279 transparentPaint.setColor(SK_ColorTRANSPARENT);
280 canvas->drawRect(bounds, transparentPaint);
281 }
Matt Sarett79756be2016-11-09 16:13:54 -0500282 }
Stan Iliev021693b2016-10-17 16:26:15 -0400283 } else {
Stan Iliev1843ac72017-09-20 18:05:35 -0400284 if (alphaMultiplier < 1.0f) {
285 // Non-layer draw for a view with getHasOverlappingRendering=false, will apply
286 // the alpha to the paint of each nested draw.
287 AlphaFilterCanvas alphaCanvas(canvas, alphaMultiplier);
288 displayList->draw(&alphaCanvas);
289 } else {
290 displayList->draw(canvas);
291 }
Stan Iliev021693b2016-10-17 16:26:15 -0400292 }
293 }
294}
295
296void RenderNodeDrawable::setViewProperties(const RenderProperties& properties, SkCanvas* canvas,
John Reck1bcacfd2017-11-03 10:12:19 -0700297 float* alphaMultiplier) {
Stan Iliev021693b2016-10-17 16:26:15 -0400298 if (properties.getLeft() != 0 || properties.getTop() != 0) {
299 canvas->translate(properties.getLeft(), properties.getTop());
300 }
301 if (properties.getStaticMatrix()) {
302 canvas->concat(*properties.getStaticMatrix());
303 } else if (properties.getAnimationMatrix()) {
304 canvas->concat(*properties.getAnimationMatrix());
305 }
306 if (properties.hasTransformMatrix()) {
307 if (properties.isTransformTranslateOnly()) {
308 canvas->translate(properties.getTranslationX(), properties.getTranslationY());
309 } else {
310 canvas->concat(*properties.getTransformMatrix());
311 }
312 }
313 const bool isLayer = properties.effectiveLayerType() != LayerType::None;
314 int clipFlags = properties.getClippingFlags();
315 if (properties.getAlpha() < 1) {
316 if (isLayer) {
John Reck1bcacfd2017-11-03 10:12:19 -0700317 clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
Stan Iliev021693b2016-10-17 16:26:15 -0400318 }
319 if (CC_LIKELY(isLayer || !properties.getHasOverlappingRendering())) {
320 *alphaMultiplier = properties.getAlpha();
321 } else {
322 // savelayer needed to create an offscreen buffer
323 Rect layerBounds(0, 0, properties.getWidth(), properties.getHeight());
324 if (clipFlags) {
325 properties.getClippingRectForFlags(clipFlags, &layerBounds);
John Reck1bcacfd2017-11-03 10:12:19 -0700326 clipFlags = 0; // all clipping done by savelayer
Stan Iliev021693b2016-10-17 16:26:15 -0400327 }
John Reck1bcacfd2017-11-03 10:12:19 -0700328 SkRect bounds = SkRect::MakeLTRB(layerBounds.left, layerBounds.top, layerBounds.right,
329 layerBounds.bottom);
330 canvas->saveLayerAlpha(&bounds, (int)(properties.getAlpha() * 255));
Stan Iliev021693b2016-10-17 16:26:15 -0400331 }
332
333 if (CC_UNLIKELY(ATRACE_ENABLED() && properties.promotedToLayer())) {
334 // pretend alpha always causes savelayer to warn about
335 // performance problem affecting old versions
336 ATRACE_FORMAT("alpha caused saveLayer %dx%d", properties.getWidth(),
John Reck1bcacfd2017-11-03 10:12:19 -0700337 properties.getHeight());
Stan Iliev021693b2016-10-17 16:26:15 -0400338 }
339 }
340
341 const SkRect* pendingClip = nullptr;
342 SkRect clipRect;
343
344 if (clipFlags) {
345 Rect tmpRect;
346 properties.getClippingRectForFlags(clipFlags, &tmpRect);
347 clipRect = tmpRect.toSkRect();
348 pendingClip = &clipRect;
349 }
350
351 if (properties.getRevealClip().willClip()) {
Mike Reed6c67f1d2016-12-14 10:29:54 -0500352 canvas->clipPath(*properties.getRevealClip().getPath(), SkClipOp::kIntersect, true);
Stan Iliev021693b2016-10-17 16:26:15 -0400353 } else if (properties.getOutline().willClip()) {
354 clipOutline(properties.getOutline(), canvas, pendingClip);
355 pendingClip = nullptr;
356 }
357
358 if (pendingClip) {
359 canvas->clipRect(*pendingClip);
360 }
361}
362
Chris Blume7b8a8082018-11-30 15:51:58 -0800363} // namespace skiapipeline
364} // namespace uirenderer
365} // namespace android