blob: 2b2e3995d17ef85bfd66a7688294864482de3fbc [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"
Robert Phillips74fe4712023-09-06 15:32:34 -040018
Kevin Lubick856848e2022-02-24 16:24:09 +000019#include <SkPaint.h>
John Reck1bcacfd2017-11-03 10:12:19 -070020#include <SkPaintFilterCanvas.h>
Kevin Lubick856848e2022-02-24 16:24:09 +000021#include <SkPoint.h>
22#include <SkRRect.h>
23#include <SkRect.h>
rnleece9762b2021-05-21 15:40:53 -070024#include <gui/TraceUtils.h>
Robert Phillips74fe4712023-09-06 15:32:34 -040025#include <include/effects/SkImageFilters.h>
26#ifdef __ANDROID__
27#include <include/gpu/ganesh/SkImageGanesh.h>
28#endif
29
30#include <optional>
31
Stan Iliev021693b2016-10-17 16:26:15 -040032#include "RenderNode.h"
33#include "SkiaDisplayList.h"
rnleece9762b2021-05-21 15:40:53 -070034#include "StretchMask.h"
Nader Jawad2dc632a2021-03-29 18:51:29 -070035#include "TransformCanvas.h"
Stan Iliev021693b2016-10-17 16:26:15 -040036
37namespace android {
38namespace uirenderer {
39namespace skiapipeline {
40
John Reckd9d7f122018-05-03 14:40:56 -070041RenderNodeDrawable::RenderNodeDrawable(RenderNode* node, SkCanvas* canvas, bool composeLayer,
42 bool inReorderingSection)
43 : mRenderNode(node)
44 , mRecordedTransform(canvas->getTotalMatrix())
45 , mComposeLayer(composeLayer)
46 , mInReorderingSection(inReorderingSection) {}
47
48RenderNodeDrawable::~RenderNodeDrawable() {
49 // Just here to move the destructor into the cpp file where we can access RenderNode.
50
51 // TODO: Detangle the header nightmare.
52}
53
John Reck1bcacfd2017-11-03 10:12:19 -070054void RenderNodeDrawable::drawBackwardsProjectedNodes(SkCanvas* canvas,
55 const SkiaDisplayList& displayList,
Nathaniel Nifong2945bff2019-11-25 09:34:21 -050056 int nestLevel) const {
Stan Ilievdb45a4b2016-11-08 14:18:31 -050057 LOG_ALWAYS_FATAL_IF(0 == nestLevel && !displayList.mProjectionReceiver);
58 for (auto& child : displayList.mChildNodes) {
59 const RenderProperties& childProperties = child.getNodeProperties();
60
John Reck1bcacfd2017-11-03 10:12:19 -070061 // immediate children cannot be projected on their parent
Stan Ilievdb45a4b2016-11-08 14:18:31 -050062 if (childProperties.getProjectBackwards() && nestLevel > 0) {
63 SkAutoCanvasRestore acr2(canvas, true);
John Reck1bcacfd2017-11-03 10:12:19 -070064 // Apply recorded matrix, which is a total matrix saved at recording time to avoid
65 // replaying all DL commands.
Stan Ilievdb45a4b2016-11-08 14:18:31 -050066 canvas->concat(child.getRecordedMatrix());
67 child.drawContent(canvas);
68 }
69
John Reck1bcacfd2017-11-03 10:12:19 -070070 // skip walking sub-nodes if current display list contains a receiver with exception of
71 // level 0, which is a known receiver
Stan Ilievdb45a4b2016-11-08 14:18:31 -050072 if (0 == nestLevel || !displayList.containsProjectionReceiver()) {
73 SkAutoCanvasRestore acr(canvas, true);
74 SkMatrix nodeMatrix;
75 mat4 hwuiMatrix(child.getRecordedMatrix());
John Reckbe671952021-01-13 22:39:32 -050076 const RenderNode* childNode = child.getRenderNode();
Stan Ilievdb45a4b2016-11-08 14:18:31 -050077 childNode->applyViewPropertyTransforms(hwuiMatrix);
78 hwuiMatrix.copyTo(nodeMatrix);
79 canvas->concat(nodeMatrix);
John Reckbe671952021-01-13 22:39:32 -050080 const SkiaDisplayList* childDisplayList = childNode->getDisplayList().asSkiaDl();
Stan Ilievdb45a4b2016-11-08 14:18:31 -050081 if (childDisplayList) {
John Reck1bcacfd2017-11-03 10:12:19 -070082 drawBackwardsProjectedNodes(canvas, *childDisplayList, nestLevel + 1);
Stan Ilievdb45a4b2016-11-08 14:18:31 -050083 }
84 }
85 }
86}
87
Stan Iliev021693b2016-10-17 16:26:15 -040088static void clipOutline(const Outline& outline, SkCanvas* canvas, const SkRect* pendingClip) {
Stan Iliev021693b2016-10-17 16:26:15 -040089 Rect possibleRect;
90 float radius;
Derek Sollenbergerf209c062017-05-26 12:11:34 -040091
92 /* To match the existing HWUI behavior we only supports rectangles or
93 * rounded rectangles; passing in a more complicated outline fails silently.
94 */
95 if (!outline.getAsRoundRect(&possibleRect, &radius)) {
96 if (pendingClip) {
97 canvas->clipRect(*pendingClip);
98 }
Chet Haase33c1ea72021-09-27 20:56:08 +000099 const SkPath* path = outline.getPath();
100 if (path) {
101 canvas->clipPath(*path, SkClipOp::kIntersect, true);
102 }
Derek Sollenbergerf209c062017-05-26 12:11:34 -0400103 return;
104 }
105
Stan Iliev021693b2016-10-17 16:26:15 -0400106 SkRect rect = possibleRect.toSkRect();
107 if (radius != 0.0f) {
108 if (pendingClip && !pendingClip->contains(rect)) {
109 canvas->clipRect(*pendingClip);
110 }
Mike Reed6c67f1d2016-12-14 10:29:54 -0500111 canvas->clipRRect(SkRRect::MakeRectXY(rect, radius, radius), SkClipOp::kIntersect, true);
Stan Iliev021693b2016-10-17 16:26:15 -0400112 } else {
113 if (pendingClip) {
114 (void)rect.intersect(*pendingClip);
115 }
116 canvas->clipRect(rect);
117 }
118}
119
120const RenderProperties& RenderNodeDrawable::getNodeProperties() const {
121 return mRenderNode->properties();
122}
123
124void RenderNodeDrawable::onDraw(SkCanvas* canvas) {
John Reck1bcacfd2017-11-03 10:12:19 -0700125 // negative and positive Z order are drawn out of order, if this render node drawable is in
126 // a reordering section
Stan Iliev2f06e8a2016-11-02 15:29:03 -0400127 if ((!mInReorderingSection) || MathUtils::isZero(mRenderNode->properties().getZ())) {
Stan Iliev021693b2016-10-17 16:26:15 -0400128 this->forceDraw(canvas);
129 }
130}
131
John Reckf96b2842018-11-29 09:44:10 -0800132class MarkDraw {
133public:
134 explicit MarkDraw(SkCanvas& canvas, RenderNode& node) : mCanvas(canvas), mNode(node) {
135 if (CC_UNLIKELY(Properties::skpCaptureEnabled)) {
136 mNode.markDrawStart(mCanvas);
137 }
138 }
139 ~MarkDraw() {
140 if (CC_UNLIKELY(Properties::skpCaptureEnabled)) {
141 mNode.markDrawEnd(mCanvas);
142 }
143 }
John Reck283bb462018-12-13 16:40:14 -0800144
John Reckf96b2842018-11-29 09:44:10 -0800145private:
146 SkCanvas& mCanvas;
147 RenderNode& mNode;
148};
149
Nathaniel Nifong2945bff2019-11-25 09:34:21 -0500150void RenderNodeDrawable::forceDraw(SkCanvas* canvas) const {
Stan Iliev021693b2016-10-17 16:26:15 -0400151 RenderNode* renderNode = mRenderNode.get();
John Reckf96b2842018-11-29 09:44:10 -0800152 MarkDraw _marker{*canvas, *renderNode};
Stan Iliev021693b2016-10-17 16:26:15 -0400153
154 // We only respect the nothingToDraw check when we are composing a layer. This
155 // ensures that we paint the layer even if it is not currently visible in the
156 // event that the properties change and it becomes visible.
Yuqian Li5ebbc8e2017-11-29 09:53:06 -0500157 if ((mProjectedDisplayList == nullptr && !renderNode->isRenderable()) ||
John Reck283bb462018-12-13 16:40:14 -0800158 (renderNode->nothingToDraw() && mComposeLayer)) {
Stan Iliev021693b2016-10-17 16:26:15 -0400159 return;
160 }
161
John Reckbe671952021-01-13 22:39:32 -0500162 SkiaDisplayList* displayList = renderNode->getDisplayList().asSkiaDl();
Stan Iliev021693b2016-10-17 16:26:15 -0400163
164 SkAutoCanvasRestore acr(canvas, true);
Stan Iliev021693b2016-10-17 16:26:15 -0400165 const RenderProperties& properties = this->getNodeProperties();
John Reck1bcacfd2017-11-03 10:12:19 -0700166 // pass this outline to the children that may clip backward projected nodes
167 displayList->mProjectedOutline =
168 displayList->containsProjectionReceiver() ? &properties.getOutline() : nullptr;
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500169 if (!properties.getProjectBackwards()) {
170 drawContent(canvas);
171 if (mProjectedDisplayList) {
John Reck1bcacfd2017-11-03 10:12:19 -0700172 acr.restore(); // draw projected children using parent matrix
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500173 LOG_ALWAYS_FATAL_IF(!mProjectedDisplayList->mProjectedOutline);
174 const bool shouldClip = mProjectedDisplayList->mProjectedOutline->getPath();
175 SkAutoCanvasRestore acr2(canvas, shouldClip);
Stan Iliev54d70322018-06-14 18:00:10 -0400176 canvas->setMatrix(mProjectedDisplayList->mParentMatrix);
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500177 if (shouldClip) {
Stan Iliev8b7cc512019-02-22 10:16:43 -0500178 canvas->clipPath(*mProjectedDisplayList->mProjectedOutline->getPath());
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500179 }
180 drawBackwardsProjectedNodes(canvas, *mProjectedDisplayList);
Stan Iliev021693b2016-10-17 16:26:15 -0400181 }
Stan Iliev021693b2016-10-17 16:26:15 -0400182 }
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500183 displayList->mProjectedOutline = nullptr;
Stan Iliev021693b2016-10-17 16:26:15 -0400184}
185
Nader Jawad6aff4812021-06-09 10:14:43 -0700186static bool layerNeedsPaint(const LayerProperties& properties, float alphaMultiplier,
187 SkPaint* paint) {
John Reck1bcacfd2017-11-03 10:12:19 -0700188 if (alphaMultiplier < 1.0f || properties.alpha() < 255 ||
Nader Jawad390d6e82020-09-24 21:35:03 -0700189 properties.xferMode() != SkBlendMode::kSrcOver || properties.getColorFilter() != nullptr ||
Nader Jawad6aff4812021-06-09 10:14:43 -0700190 properties.getStretchEffect().requiresLayer()) {
Stan Iliev021693b2016-10-17 16:26:15 -0400191 paint->setAlpha(properties.alpha() * alphaMultiplier);
192 paint->setBlendMode(properties.xferMode());
Ben Wagnerc1a8a462018-07-12 12:41:28 -0400193 paint->setColorFilter(sk_ref_sp(properties.getColorFilter()));
Stan Iliev021693b2016-10-17 16:26:15 -0400194 return true;
195 }
196 return false;
197}
198
Stan Iliev1843ac72017-09-20 18:05:35 -0400199class AlphaFilterCanvas : public SkPaintFilterCanvas {
200public:
201 AlphaFilterCanvas(SkCanvas* canvas, float alpha) : SkPaintFilterCanvas(canvas), mAlpha(alpha) {}
John Reck1bcacfd2017-11-03 10:12:19 -0700202
Stan Iliev1843ac72017-09-20 18:05:35 -0400203protected:
Ben Wagner62b38942019-04-15 11:59:33 -0400204 bool onFilter(SkPaint& paint) const override {
205 paint.setAlpha((uint8_t)paint.getAlpha() * mAlpha);
Stan Iliev1843ac72017-09-20 18:05:35 -0400206 return true;
207 }
Alec Mouri655a5e42022-09-12 17:49:17 +0000208
Stan Iliev1843ac72017-09-20 18:05:35 -0400209 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) {
Zhao Qin93f99892022-10-14 11:52:44 +0800237 auto clipBounds = canvas->getLocalClipBounds();
238 SkIRect srcBounds = SkIRect::MakeWH(bounds.width(), bounds.height());
239 SkIPoint offset = SkIPoint::Make(0.0f, 0.0f);
John Reckbe671952021-01-13 22:39:32 -0500240 SkiaDisplayList* displayList = renderNode->getDisplayList().asSkiaDl();
Stan Iliev021693b2016-10-17 16:26:15 -0400241 const LayerProperties& layerProperties = properties.layerProperties();
242 // composing a hardware layer
243 if (renderNode->getLayerSurface() && mComposeLayer) {
244 SkASSERT(properties.effectiveLayerType() == LayerType::RenderLayer);
Stan Iliev7e100e92018-01-22 10:36:33 -0500245 SkPaint paint;
Nader Jawad6aff4812021-06-09 10:14:43 -0700246 layerNeedsPaint(layerProperties, alphaMultiplier, &paint);
Nader Jawad4a6b60a2021-09-20 21:22:50 -0700247 sk_sp<SkImage> snapshotImage;
248 auto* imageFilter = layerProperties.getImageFilter();
249 auto recordingContext = canvas->recordingContext();
250 // On some GL vendor implementations, caching the result of
251 // getLayerSurface->makeImageSnapshot() causes a call to
252 // Fence::waitForever without a corresponding signal. This would
253 // lead to ANRs throughout the system.
254 // Instead only cache the SkImage created with the SkImageFilter
255 // for supported devices. Otherwise just create a new SkImage with
256 // the corresponding SkImageFilter each time.
257 // See b/193145089 and b/197263715
258 if (!Properties::enableRenderEffectCache) {
Nader Jawad74eaabe2021-09-27 17:48:15 -0700259 snapshotImage = renderNode->getLayerSurface()->makeImageSnapshot();
Nader Jawad4a6b60a2021-09-20 21:22:50 -0700260 if (imageFilter) {
261 auto subset = SkIRect::MakeWH(srcBounds.width(), srcBounds.height());
Robert Phillips74fe4712023-09-06 15:32:34 -0400262
263#ifdef __ANDROID__
264 if (recordingContext) {
265 snapshotImage = SkImages::MakeWithFilter(
266 recordingContext, snapshotImage, imageFilter, subset,
267 clipBounds.roundOut(), &srcBounds, &offset);
268 } else
269#endif
270 {
271 snapshotImage = SkImages::MakeWithFilter(snapshotImage, imageFilter, subset,
272 clipBounds.roundOut(), &srcBounds,
273 &offset);
274 }
Nader Jawad4a6b60a2021-09-20 21:22:50 -0700275 }
276 } else {
277 const auto snapshotResult = renderNode->updateSnapshotIfRequired(
278 recordingContext, layerProperties.getImageFilter(), clipBounds.roundOut());
279 snapshotImage = snapshotResult->snapshot;
280 srcBounds = snapshotResult->outSubset;
281 offset = snapshotResult->outOffset;
282 }
283
Nader Jawad6aff4812021-06-09 10:14:43 -0700284 const auto dstBounds = SkIRect::MakeXYWH(offset.x(),
285 offset.y(),
286 srcBounds.width(),
287 srcBounds.height());
Mike Reed7994a312021-01-28 18:06:26 -0500288 SkSamplingOptions sampling(SkFilterMode::kLinear);
Derek Sollenberger03e6cff72017-12-04 15:07:08 -0500289
290 // surfaces for layers are created on LAYER_SIZE boundaries (which are >= layer size) so
291 // we need to restrict the portion of the surface drawn to the size of the renderNode.
292 SkASSERT(renderNode->getLayerSurface()->width() >= bounds.width());
293 SkASSERT(renderNode->getLayerSurface()->height() >= bounds.height());
Nathaniel Nifong2945bff2019-11-25 09:34:21 -0500294
295 // If SKP recording is active save an annotation that indicates this drawImageRect
296 // could also be rendered with the commands saved at ID associated with this node.
297 if (CC_UNLIKELY(Properties::skpCaptureEnabled)) {
298 canvas->drawAnnotation(bounds, String8::format(
299 "SurfaceID|%" PRId64, renderNode->uniqueId()).c_str(), nullptr);
300 }
Nader Jawad2dc632a2021-03-29 18:51:29 -0700301
Nader Jawad8f5d66b2021-04-07 16:05:13 -0700302 const StretchEffect& stretch = properties.layerProperties().getStretchEffect();
John Reck8ed00dc2021-05-10 13:09:27 -0400303 if (stretch.isEmpty() ||
Nader Jawad93d6e242021-05-17 18:12:29 -0700304 Properties::getStretchEffectBehavior() == StretchEffectBehavior::UniformScale) {
Nader Jawad197743f2021-04-19 19:45:13 -0700305 // If we don't have any stretch effects, issue the filtered
306 // canvas draw calls to make sure we still punch a hole
307 // with the same canvas transformation + clip into the target
308 // canvas then draw the layer on top
309 if (renderNode->hasHolePunches()) {
Nader Jawad10f50632023-04-12 15:53:28 -0700310 canvas->save();
Alec Mouri655a5e42022-09-12 17:49:17 +0000311 TransformCanvas transformCanvas(canvas, SkBlendMode::kDstOut);
Nader Jawad197743f2021-04-19 19:45:13 -0700312 displayList->draw(&transformCanvas);
Nader Jawad10f50632023-04-12 15:53:28 -0700313 canvas->restore();
Nader Jawad197743f2021-04-19 19:45:13 -0700314 }
Nader Jawad6aff4812021-06-09 10:14:43 -0700315 canvas->drawImageRect(snapshotImage, SkRect::Make(srcBounds),
316 SkRect::Make(dstBounds), sampling, &paint,
Nader Jawad8f5d66b2021-04-07 16:05:13 -0700317 SkCanvas::kStrict_SrcRectConstraint);
318 } else {
Nader Jawad197743f2021-04-19 19:45:13 -0700319 // If we do have stretch effects and have hole punches,
320 // then create a mask and issue the filtered draw calls to
321 // get the corresponding hole punches.
322 // Then apply the stretch to the mask and draw the mask to
323 // the destination
Nader Jawad6aff4812021-06-09 10:14:43 -0700324 // Also if the stretchy container has an ImageFilter applied
325 // to it (i.e. blur) we need to take into account the offset
326 // that will be generated with this result. Ex blurs will "grow"
327 // the source image by the blur radius so we need to translate
328 // the shader by the same amount to render in the same location
329 SkMatrix matrix;
330 matrix.setTranslate(
331 offset.x() - srcBounds.left(),
332 offset.y() - srcBounds.top()
333 );
Nader Jawad197743f2021-04-19 19:45:13 -0700334 if (renderNode->hasHolePunches()) {
335 GrRecordingContext* context = canvas->recordingContext();
336 StretchMask& stretchMask = renderNode->getStretchMask();
337 stretchMask.draw(context,
338 stretch,
339 bounds,
340 displayList,
341 canvas);
342 }
343
Nader Jawad6aff4812021-06-09 10:14:43 -0700344 sk_sp<SkShader> stretchShader =
345 stretch.getShader(bounds.width(), bounds.height(), snapshotImage, &matrix);
Nader Jawad8f5d66b2021-04-07 16:05:13 -0700346 paint.setShader(stretchShader);
Nader Jawad6aff4812021-06-09 10:14:43 -0700347 canvas->drawRect(SkRect::Make(dstBounds), paint);
Nader Jawad8f5d66b2021-04-07 16:05:13 -0700348 }
Matt Sarett79756be2016-11-09 16:13:54 -0500349
Matt Sarettf58cc922016-11-14 18:33:38 -0500350 if (!renderNode->getSkiaLayer()->hasRenderedSinceRepaint) {
Matt Sarett79756be2016-11-09 16:13:54 -0500351 renderNode->getSkiaLayer()->hasRenderedSinceRepaint = true;
Matt Sarettf58cc922016-11-14 18:33:38 -0500352 if (CC_UNLIKELY(Properties::debugLayersUpdates)) {
353 SkPaint layerPaint;
354 layerPaint.setColor(0x7f00ff00);
355 canvas->drawRect(bounds, layerPaint);
356 } else if (CC_UNLIKELY(Properties::debugOverdraw)) {
357 // Render transparent rect to increment overdraw for repaint area.
358 // This can be "else if" because flashing green on layer updates
359 // will also increment the overdraw if it happens to be turned on.
360 SkPaint transparentPaint;
361 transparentPaint.setColor(SK_ColorTRANSPARENT);
362 canvas->drawRect(bounds, transparentPaint);
363 }
Matt Sarett79756be2016-11-09 16:13:54 -0500364 }
Stan Iliev021693b2016-10-17 16:26:15 -0400365 } else {
Stan Iliev1843ac72017-09-20 18:05:35 -0400366 if (alphaMultiplier < 1.0f) {
367 // Non-layer draw for a view with getHasOverlappingRendering=false, will apply
368 // the alpha to the paint of each nested draw.
369 AlphaFilterCanvas alphaCanvas(canvas, alphaMultiplier);
370 displayList->draw(&alphaCanvas);
371 } else {
372 displayList->draw(canvas);
373 }
Stan Iliev021693b2016-10-17 16:26:15 -0400374 }
375 }
376}
377
378void RenderNodeDrawable::setViewProperties(const RenderProperties& properties, SkCanvas* canvas,
Dongya Jiange2b99382022-02-28 21:35:57 +0800379 float* alphaMultiplier, bool ignoreLayer) {
Stan Iliev021693b2016-10-17 16:26:15 -0400380 if (properties.getLeft() != 0 || properties.getTop() != 0) {
381 canvas->translate(properties.getLeft(), properties.getTop());
382 }
383 if (properties.getStaticMatrix()) {
384 canvas->concat(*properties.getStaticMatrix());
385 } else if (properties.getAnimationMatrix()) {
386 canvas->concat(*properties.getAnimationMatrix());
387 }
388 if (properties.hasTransformMatrix()) {
389 if (properties.isTransformTranslateOnly()) {
390 canvas->translate(properties.getTranslationX(), properties.getTranslationY());
391 } else {
392 canvas->concat(*properties.getTransformMatrix());
393 }
394 }
Dongya Jiange2b99382022-02-28 21:35:57 +0800395 if (Properties::getStretchEffectBehavior() == StretchEffectBehavior::UniformScale &&
396 !ignoreLayer) {
John Reck8ed00dc2021-05-10 13:09:27 -0400397 const StretchEffect& stretch = properties.layerProperties().getStretchEffect();
398 if (!stretch.isEmpty()) {
399 canvas->concat(
400 stretch.makeLinearStretch(properties.getWidth(), properties.getHeight()));
401 }
402 }
Stan Iliev021693b2016-10-17 16:26:15 -0400403 const bool isLayer = properties.effectiveLayerType() != LayerType::None;
404 int clipFlags = properties.getClippingFlags();
405 if (properties.getAlpha() < 1) {
Dongya Jiange2b99382022-02-28 21:35:57 +0800406 if (isLayer && !ignoreLayer) {
John Reck1bcacfd2017-11-03 10:12:19 -0700407 clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
Stan Iliev021693b2016-10-17 16:26:15 -0400408 }
Dongya Jiange2b99382022-02-28 21:35:57 +0800409 if (CC_LIKELY(isLayer || !properties.getHasOverlappingRendering()) || ignoreLayer) {
Stan Iliev021693b2016-10-17 16:26:15 -0400410 *alphaMultiplier = properties.getAlpha();
411 } else {
412 // savelayer needed to create an offscreen buffer
413 Rect layerBounds(0, 0, properties.getWidth(), properties.getHeight());
414 if (clipFlags) {
415 properties.getClippingRectForFlags(clipFlags, &layerBounds);
John Reck1bcacfd2017-11-03 10:12:19 -0700416 clipFlags = 0; // all clipping done by savelayer
Stan Iliev021693b2016-10-17 16:26:15 -0400417 }
John Reck1bcacfd2017-11-03 10:12:19 -0700418 SkRect bounds = SkRect::MakeLTRB(layerBounds.left, layerBounds.top, layerBounds.right,
419 layerBounds.bottom);
420 canvas->saveLayerAlpha(&bounds, (int)(properties.getAlpha() * 255));
Stan Iliev021693b2016-10-17 16:26:15 -0400421 }
422
423 if (CC_UNLIKELY(ATRACE_ENABLED() && properties.promotedToLayer())) {
424 // pretend alpha always causes savelayer to warn about
425 // performance problem affecting old versions
426 ATRACE_FORMAT("alpha caused saveLayer %dx%d", properties.getWidth(),
John Reck1bcacfd2017-11-03 10:12:19 -0700427 properties.getHeight());
Stan Iliev021693b2016-10-17 16:26:15 -0400428 }
429 }
430
431 const SkRect* pendingClip = nullptr;
432 SkRect clipRect;
433
434 if (clipFlags) {
435 Rect tmpRect;
436 properties.getClippingRectForFlags(clipFlags, &tmpRect);
437 clipRect = tmpRect.toSkRect();
438 pendingClip = &clipRect;
439 }
440
441 if (properties.getRevealClip().willClip()) {
Mike Reed6c67f1d2016-12-14 10:29:54 -0500442 canvas->clipPath(*properties.getRevealClip().getPath(), SkClipOp::kIntersect, true);
Stan Iliev021693b2016-10-17 16:26:15 -0400443 } else if (properties.getOutline().willClip()) {
444 clipOutline(properties.getOutline(), canvas, pendingClip);
445 pendingClip = nullptr;
446 }
447
448 if (pendingClip) {
449 canvas->clipRect(*pendingClip);
450 }
451}
452
Chris Blume7b8a8082018-11-30 15:51:58 -0800453} // namespace skiapipeline
454} // namespace uirenderer
455} // namespace android