blob: ffa915ad968c17a2ff6375d6a392869834c081d6 [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) {
John Reckc5982e72023-11-21 10:48:13 -050059 if (!child.getRenderNode()->isRenderable()) continue;
Stan Ilievdb45a4b2016-11-08 14:18:31 -050060 const RenderProperties& childProperties = child.getNodeProperties();
61
John Reck1bcacfd2017-11-03 10:12:19 -070062 // immediate children cannot be projected on their parent
Stan Ilievdb45a4b2016-11-08 14:18:31 -050063 if (childProperties.getProjectBackwards() && nestLevel > 0) {
64 SkAutoCanvasRestore acr2(canvas, true);
John Reck1bcacfd2017-11-03 10:12:19 -070065 // Apply recorded matrix, which is a total matrix saved at recording time to avoid
66 // replaying all DL commands.
Stan Ilievdb45a4b2016-11-08 14:18:31 -050067 canvas->concat(child.getRecordedMatrix());
68 child.drawContent(canvas);
69 }
70
John Reck1bcacfd2017-11-03 10:12:19 -070071 // skip walking sub-nodes if current display list contains a receiver with exception of
72 // level 0, which is a known receiver
Stan Ilievdb45a4b2016-11-08 14:18:31 -050073 if (0 == nestLevel || !displayList.containsProjectionReceiver()) {
74 SkAutoCanvasRestore acr(canvas, true);
75 SkMatrix nodeMatrix;
76 mat4 hwuiMatrix(child.getRecordedMatrix());
John Reckbe671952021-01-13 22:39:32 -050077 const RenderNode* childNode = child.getRenderNode();
Stan Ilievdb45a4b2016-11-08 14:18:31 -050078 childNode->applyViewPropertyTransforms(hwuiMatrix);
79 hwuiMatrix.copyTo(nodeMatrix);
80 canvas->concat(nodeMatrix);
John Reckbe671952021-01-13 22:39:32 -050081 const SkiaDisplayList* childDisplayList = childNode->getDisplayList().asSkiaDl();
Stan Ilievdb45a4b2016-11-08 14:18:31 -050082 if (childDisplayList) {
John Reck1bcacfd2017-11-03 10:12:19 -070083 drawBackwardsProjectedNodes(canvas, *childDisplayList, nestLevel + 1);
Stan Ilievdb45a4b2016-11-08 14:18:31 -050084 }
85 }
86 }
87}
88
Stan Iliev021693b2016-10-17 16:26:15 -040089static void clipOutline(const Outline& outline, SkCanvas* canvas, const SkRect* pendingClip) {
Stan Iliev021693b2016-10-17 16:26:15 -040090 Rect possibleRect;
91 float radius;
Derek Sollenbergerf209c062017-05-26 12:11:34 -040092
93 /* To match the existing HWUI behavior we only supports rectangles or
94 * rounded rectangles; passing in a more complicated outline fails silently.
95 */
96 if (!outline.getAsRoundRect(&possibleRect, &radius)) {
97 if (pendingClip) {
98 canvas->clipRect(*pendingClip);
99 }
Chet Haase33c1ea72021-09-27 20:56:08 +0000100 const SkPath* path = outline.getPath();
101 if (path) {
102 canvas->clipPath(*path, SkClipOp::kIntersect, true);
103 }
Derek Sollenbergerf209c062017-05-26 12:11:34 -0400104 return;
105 }
106
Stan Iliev021693b2016-10-17 16:26:15 -0400107 SkRect rect = possibleRect.toSkRect();
108 if (radius != 0.0f) {
109 if (pendingClip && !pendingClip->contains(rect)) {
110 canvas->clipRect(*pendingClip);
111 }
Mike Reed6c67f1d2016-12-14 10:29:54 -0500112 canvas->clipRRect(SkRRect::MakeRectXY(rect, radius, radius), SkClipOp::kIntersect, true);
Stan Iliev021693b2016-10-17 16:26:15 -0400113 } else {
114 if (pendingClip) {
115 (void)rect.intersect(*pendingClip);
116 }
117 canvas->clipRect(rect);
118 }
119}
120
121const RenderProperties& RenderNodeDrawable::getNodeProperties() const {
122 return mRenderNode->properties();
123}
124
125void RenderNodeDrawable::onDraw(SkCanvas* canvas) {
John Reck1bcacfd2017-11-03 10:12:19 -0700126 // negative and positive Z order are drawn out of order, if this render node drawable is in
127 // a reordering section
Stan Iliev2f06e8a2016-11-02 15:29:03 -0400128 if ((!mInReorderingSection) || MathUtils::isZero(mRenderNode->properties().getZ())) {
Stan Iliev021693b2016-10-17 16:26:15 -0400129 this->forceDraw(canvas);
130 }
131}
132
John Reckf96b2842018-11-29 09:44:10 -0800133class MarkDraw {
134public:
135 explicit MarkDraw(SkCanvas& canvas, RenderNode& node) : mCanvas(canvas), mNode(node) {
136 if (CC_UNLIKELY(Properties::skpCaptureEnabled)) {
137 mNode.markDrawStart(mCanvas);
138 }
139 }
140 ~MarkDraw() {
141 if (CC_UNLIKELY(Properties::skpCaptureEnabled)) {
142 mNode.markDrawEnd(mCanvas);
143 }
144 }
John Reck283bb462018-12-13 16:40:14 -0800145
John Reckf96b2842018-11-29 09:44:10 -0800146private:
147 SkCanvas& mCanvas;
148 RenderNode& mNode;
149};
150
Nathaniel Nifong2945bff2019-11-25 09:34:21 -0500151void RenderNodeDrawable::forceDraw(SkCanvas* canvas) const {
Stan Iliev021693b2016-10-17 16:26:15 -0400152 RenderNode* renderNode = mRenderNode.get();
John Reckf96b2842018-11-29 09:44:10 -0800153 MarkDraw _marker{*canvas, *renderNode};
Stan Iliev021693b2016-10-17 16:26:15 -0400154
155 // We only respect the nothingToDraw check when we are composing a layer. This
156 // ensures that we paint the layer even if it is not currently visible in the
157 // event that the properties change and it becomes visible.
Yuqian Li5ebbc8e2017-11-29 09:53:06 -0500158 if ((mProjectedDisplayList == nullptr && !renderNode->isRenderable()) ||
John Reck283bb462018-12-13 16:40:14 -0800159 (renderNode->nothingToDraw() && mComposeLayer)) {
Stan Iliev021693b2016-10-17 16:26:15 -0400160 return;
161 }
162
John Reckbe671952021-01-13 22:39:32 -0500163 SkiaDisplayList* displayList = renderNode->getDisplayList().asSkiaDl();
Stan Iliev021693b2016-10-17 16:26:15 -0400164
165 SkAutoCanvasRestore acr(canvas, true);
Stan Iliev021693b2016-10-17 16:26:15 -0400166 const RenderProperties& properties = this->getNodeProperties();
John Reck1bcacfd2017-11-03 10:12:19 -0700167 // pass this outline to the children that may clip backward projected nodes
168 displayList->mProjectedOutline =
169 displayList->containsProjectionReceiver() ? &properties.getOutline() : nullptr;
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500170 if (!properties.getProjectBackwards()) {
171 drawContent(canvas);
172 if (mProjectedDisplayList) {
John Reck1bcacfd2017-11-03 10:12:19 -0700173 acr.restore(); // draw projected children using parent matrix
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500174 LOG_ALWAYS_FATAL_IF(!mProjectedDisplayList->mProjectedOutline);
175 const bool shouldClip = mProjectedDisplayList->mProjectedOutline->getPath();
176 SkAutoCanvasRestore acr2(canvas, shouldClip);
Stan Iliev54d70322018-06-14 18:00:10 -0400177 canvas->setMatrix(mProjectedDisplayList->mParentMatrix);
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500178 if (shouldClip) {
Stan Iliev8b7cc512019-02-22 10:16:43 -0500179 canvas->clipPath(*mProjectedDisplayList->mProjectedOutline->getPath());
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500180 }
181 drawBackwardsProjectedNodes(canvas, *mProjectedDisplayList);
Stan Iliev021693b2016-10-17 16:26:15 -0400182 }
Stan Iliev021693b2016-10-17 16:26:15 -0400183 }
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500184 displayList->mProjectedOutline = nullptr;
Stan Iliev021693b2016-10-17 16:26:15 -0400185}
186
Nader Jawad6aff4812021-06-09 10:14:43 -0700187static bool layerNeedsPaint(const LayerProperties& properties, float alphaMultiplier,
188 SkPaint* paint) {
John Reck1bcacfd2017-11-03 10:12:19 -0700189 if (alphaMultiplier < 1.0f || properties.alpha() < 255 ||
Nader Jawad390d6e82020-09-24 21:35:03 -0700190 properties.xferMode() != SkBlendMode::kSrcOver || properties.getColorFilter() != nullptr ||
Nader Jawad6aff4812021-06-09 10:14:43 -0700191 properties.getStretchEffect().requiresLayer()) {
Stan Iliev021693b2016-10-17 16:26:15 -0400192 paint->setAlpha(properties.alpha() * alphaMultiplier);
193 paint->setBlendMode(properties.xferMode());
Ben Wagnerc1a8a462018-07-12 12:41:28 -0400194 paint->setColorFilter(sk_ref_sp(properties.getColorFilter()));
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 }
Alec Mouri655a5e42022-09-12 17:49:17 +0000209
Stan Iliev1843ac72017-09-20 18:05:35 -0400210 void onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) override {
211 // We unroll the drawable using "this" canvas, so that draw calls contained inside will
Ben Wagner62b38942019-04-15 11:59:33 -0400212 // get their alpha applied. The default SkPaintFilterCanvas::onDrawDrawable does not unroll.
Stan Iliev1843ac72017-09-20 18:05:35 -0400213 drawable->draw(this, matrix);
214 }
John Reck1bcacfd2017-11-03 10:12:19 -0700215
Stan Iliev1843ac72017-09-20 18:05:35 -0400216private:
217 float mAlpha;
218};
219
Stan Iliev021693b2016-10-17 16:26:15 -0400220void RenderNodeDrawable::drawContent(SkCanvas* canvas) const {
221 RenderNode* renderNode = mRenderNode.get();
222 float alphaMultiplier = 1.0f;
223 const RenderProperties& properties = renderNode->properties();
224
225 // If we are drawing the contents of layer, we don't want to apply any of
226 // the RenderNode's properties during this pass. Those will all be applied
227 // when the layer is composited.
228 if (mComposeLayer) {
229 setViewProperties(properties, canvas, &alphaMultiplier);
230 }
John Reckbe671952021-01-13 22:39:32 -0500231 SkiaDisplayList* displayList = mRenderNode->getDisplayList().asSkiaDl();
Stan Iliev54d70322018-06-14 18:00:10 -0400232 displayList->mParentMatrix = canvas->getTotalMatrix();
Stan Iliev021693b2016-10-17 16:26:15 -0400233
John Reck1bcacfd2017-11-03 10:12:19 -0700234 // TODO should we let the bound of the drawable do this for us?
Stan Iliev021693b2016-10-17 16:26:15 -0400235 const SkRect bounds = SkRect::MakeWH(properties.getWidth(), properties.getHeight());
236 bool quickRejected = properties.getClipToBounds() && canvas->quickReject(bounds);
237 if (!quickRejected) {
Zhao Qin93f99892022-10-14 11:52:44 +0800238 auto clipBounds = canvas->getLocalClipBounds();
239 SkIRect srcBounds = SkIRect::MakeWH(bounds.width(), bounds.height());
240 SkIPoint offset = SkIPoint::Make(0.0f, 0.0f);
John Reckbe671952021-01-13 22:39:32 -0500241 SkiaDisplayList* displayList = renderNode->getDisplayList().asSkiaDl();
Stan Iliev021693b2016-10-17 16:26:15 -0400242 const LayerProperties& layerProperties = properties.layerProperties();
243 // composing a hardware layer
244 if (renderNode->getLayerSurface() && mComposeLayer) {
245 SkASSERT(properties.effectiveLayerType() == LayerType::RenderLayer);
Stan Iliev7e100e92018-01-22 10:36:33 -0500246 SkPaint paint;
Nader Jawad6aff4812021-06-09 10:14:43 -0700247 layerNeedsPaint(layerProperties, alphaMultiplier, &paint);
Nader Jawad4a6b60a2021-09-20 21:22:50 -0700248 sk_sp<SkImage> snapshotImage;
249 auto* imageFilter = layerProperties.getImageFilter();
250 auto recordingContext = canvas->recordingContext();
251 // On some GL vendor implementations, caching the result of
252 // getLayerSurface->makeImageSnapshot() causes a call to
253 // Fence::waitForever without a corresponding signal. This would
254 // lead to ANRs throughout the system.
255 // Instead only cache the SkImage created with the SkImageFilter
256 // for supported devices. Otherwise just create a new SkImage with
257 // the corresponding SkImageFilter each time.
258 // See b/193145089 and b/197263715
259 if (!Properties::enableRenderEffectCache) {
Nader Jawad74eaabe2021-09-27 17:48:15 -0700260 snapshotImage = renderNode->getLayerSurface()->makeImageSnapshot();
Nader Jawad4a6b60a2021-09-20 21:22:50 -0700261 if (imageFilter) {
262 auto subset = SkIRect::MakeWH(srcBounds.width(), srcBounds.height());
Robert Phillips74fe4712023-09-06 15:32:34 -0400263
264#ifdef __ANDROID__
265 if (recordingContext) {
266 snapshotImage = SkImages::MakeWithFilter(
267 recordingContext, snapshotImage, imageFilter, subset,
268 clipBounds.roundOut(), &srcBounds, &offset);
269 } else
270#endif
271 {
272 snapshotImage = SkImages::MakeWithFilter(snapshotImage, imageFilter, subset,
273 clipBounds.roundOut(), &srcBounds,
274 &offset);
275 }
Nader Jawad4a6b60a2021-09-20 21:22:50 -0700276 }
277 } else {
278 const auto snapshotResult = renderNode->updateSnapshotIfRequired(
279 recordingContext, layerProperties.getImageFilter(), clipBounds.roundOut());
280 snapshotImage = snapshotResult->snapshot;
281 srcBounds = snapshotResult->outSubset;
282 offset = snapshotResult->outOffset;
283 }
284
Nader Jawad6aff4812021-06-09 10:14:43 -0700285 const auto dstBounds = SkIRect::MakeXYWH(offset.x(),
286 offset.y(),
287 srcBounds.width(),
288 srcBounds.height());
Mike Reed7994a312021-01-28 18:06:26 -0500289 SkSamplingOptions sampling(SkFilterMode::kLinear);
Derek Sollenberger03e6cff72017-12-04 15:07:08 -0500290
291 // surfaces for layers are created on LAYER_SIZE boundaries (which are >= layer size) so
292 // we need to restrict the portion of the surface drawn to the size of the renderNode.
293 SkASSERT(renderNode->getLayerSurface()->width() >= bounds.width());
294 SkASSERT(renderNode->getLayerSurface()->height() >= bounds.height());
Nathaniel Nifong2945bff2019-11-25 09:34:21 -0500295
296 // If SKP recording is active save an annotation that indicates this drawImageRect
297 // could also be rendered with the commands saved at ID associated with this node.
298 if (CC_UNLIKELY(Properties::skpCaptureEnabled)) {
299 canvas->drawAnnotation(bounds, String8::format(
300 "SurfaceID|%" PRId64, renderNode->uniqueId()).c_str(), nullptr);
301 }
Nader Jawad2dc632a2021-03-29 18:51:29 -0700302
Nader Jawad8f5d66b2021-04-07 16:05:13 -0700303 const StretchEffect& stretch = properties.layerProperties().getStretchEffect();
John Reck8ed00dc2021-05-10 13:09:27 -0400304 if (stretch.isEmpty() ||
Nader Jawad93d6e242021-05-17 18:12:29 -0700305 Properties::getStretchEffectBehavior() == StretchEffectBehavior::UniformScale) {
Nader Jawad197743f2021-04-19 19:45:13 -0700306 // If we don't have any stretch effects, issue the filtered
307 // canvas draw calls to make sure we still punch a hole
308 // with the same canvas transformation + clip into the target
309 // canvas then draw the layer on top
310 if (renderNode->hasHolePunches()) {
Nader Jawad10f50632023-04-12 15:53:28 -0700311 canvas->save();
Alec Mouri655a5e42022-09-12 17:49:17 +0000312 TransformCanvas transformCanvas(canvas, SkBlendMode::kDstOut);
Nader Jawad197743f2021-04-19 19:45:13 -0700313 displayList->draw(&transformCanvas);
Nader Jawad10f50632023-04-12 15:53:28 -0700314 canvas->restore();
Nader Jawad197743f2021-04-19 19:45:13 -0700315 }
Nader Jawad6aff4812021-06-09 10:14:43 -0700316 canvas->drawImageRect(snapshotImage, SkRect::Make(srcBounds),
317 SkRect::Make(dstBounds), sampling, &paint,
Nader Jawad8f5d66b2021-04-07 16:05:13 -0700318 SkCanvas::kStrict_SrcRectConstraint);
319 } else {
Nader Jawad197743f2021-04-19 19:45:13 -0700320 // If we do have stretch effects and have hole punches,
321 // then create a mask and issue the filtered draw calls to
322 // get the corresponding hole punches.
323 // Then apply the stretch to the mask and draw the mask to
324 // the destination
Nader Jawad6aff4812021-06-09 10:14:43 -0700325 // Also if the stretchy container has an ImageFilter applied
326 // to it (i.e. blur) we need to take into account the offset
327 // that will be generated with this result. Ex blurs will "grow"
328 // the source image by the blur radius so we need to translate
329 // the shader by the same amount to render in the same location
330 SkMatrix matrix;
331 matrix.setTranslate(
332 offset.x() - srcBounds.left(),
333 offset.y() - srcBounds.top()
334 );
Nader Jawad197743f2021-04-19 19:45:13 -0700335 if (renderNode->hasHolePunches()) {
336 GrRecordingContext* context = canvas->recordingContext();
337 StretchMask& stretchMask = renderNode->getStretchMask();
338 stretchMask.draw(context,
339 stretch,
340 bounds,
341 displayList,
342 canvas);
343 }
344
Nader Jawad6aff4812021-06-09 10:14:43 -0700345 sk_sp<SkShader> stretchShader =
346 stretch.getShader(bounds.width(), bounds.height(), snapshotImage, &matrix);
Nader Jawad8f5d66b2021-04-07 16:05:13 -0700347 paint.setShader(stretchShader);
Nader Jawad6aff4812021-06-09 10:14:43 -0700348 canvas->drawRect(SkRect::Make(dstBounds), paint);
Nader Jawad8f5d66b2021-04-07 16:05:13 -0700349 }
Matt Sarett79756be2016-11-09 16:13:54 -0500350
Matt Sarettf58cc922016-11-14 18:33:38 -0500351 if (!renderNode->getSkiaLayer()->hasRenderedSinceRepaint) {
Matt Sarett79756be2016-11-09 16:13:54 -0500352 renderNode->getSkiaLayer()->hasRenderedSinceRepaint = true;
Matt Sarettf58cc922016-11-14 18:33:38 -0500353 if (CC_UNLIKELY(Properties::debugLayersUpdates)) {
354 SkPaint layerPaint;
355 layerPaint.setColor(0x7f00ff00);
356 canvas->drawRect(bounds, layerPaint);
357 } else if (CC_UNLIKELY(Properties::debugOverdraw)) {
358 // Render transparent rect to increment overdraw for repaint area.
359 // This can be "else if" because flashing green on layer updates
360 // will also increment the overdraw if it happens to be turned on.
361 SkPaint transparentPaint;
362 transparentPaint.setColor(SK_ColorTRANSPARENT);
363 canvas->drawRect(bounds, transparentPaint);
364 }
Matt Sarett79756be2016-11-09 16:13:54 -0500365 }
Stan Iliev021693b2016-10-17 16:26:15 -0400366 } else {
Stan Iliev1843ac72017-09-20 18:05:35 -0400367 if (alphaMultiplier < 1.0f) {
368 // Non-layer draw for a view with getHasOverlappingRendering=false, will apply
369 // the alpha to the paint of each nested draw.
370 AlphaFilterCanvas alphaCanvas(canvas, alphaMultiplier);
371 displayList->draw(&alphaCanvas);
372 } else {
373 displayList->draw(canvas);
374 }
Stan Iliev021693b2016-10-17 16:26:15 -0400375 }
376 }
377}
378
379void RenderNodeDrawable::setViewProperties(const RenderProperties& properties, SkCanvas* canvas,
Dongya Jiange2b99382022-02-28 21:35:57 +0800380 float* alphaMultiplier, bool ignoreLayer) {
Stan Iliev021693b2016-10-17 16:26:15 -0400381 if (properties.getLeft() != 0 || properties.getTop() != 0) {
382 canvas->translate(properties.getLeft(), properties.getTop());
383 }
384 if (properties.getStaticMatrix()) {
385 canvas->concat(*properties.getStaticMatrix());
386 } else if (properties.getAnimationMatrix()) {
387 canvas->concat(*properties.getAnimationMatrix());
388 }
389 if (properties.hasTransformMatrix()) {
390 if (properties.isTransformTranslateOnly()) {
391 canvas->translate(properties.getTranslationX(), properties.getTranslationY());
392 } else {
393 canvas->concat(*properties.getTransformMatrix());
394 }
395 }
Dongya Jiange2b99382022-02-28 21:35:57 +0800396 if (Properties::getStretchEffectBehavior() == StretchEffectBehavior::UniformScale &&
397 !ignoreLayer) {
John Reck8ed00dc2021-05-10 13:09:27 -0400398 const StretchEffect& stretch = properties.layerProperties().getStretchEffect();
399 if (!stretch.isEmpty()) {
400 canvas->concat(
401 stretch.makeLinearStretch(properties.getWidth(), properties.getHeight()));
402 }
403 }
Stan Iliev021693b2016-10-17 16:26:15 -0400404 const bool isLayer = properties.effectiveLayerType() != LayerType::None;
405 int clipFlags = properties.getClippingFlags();
406 if (properties.getAlpha() < 1) {
Dongya Jiange2b99382022-02-28 21:35:57 +0800407 if (isLayer && !ignoreLayer) {
John Reck1bcacfd2017-11-03 10:12:19 -0700408 clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
Stan Iliev021693b2016-10-17 16:26:15 -0400409 }
Dongya Jiange2b99382022-02-28 21:35:57 +0800410 if (CC_LIKELY(isLayer || !properties.getHasOverlappingRendering()) || ignoreLayer) {
Stan Iliev021693b2016-10-17 16:26:15 -0400411 *alphaMultiplier = properties.getAlpha();
412 } else {
413 // savelayer needed to create an offscreen buffer
414 Rect layerBounds(0, 0, properties.getWidth(), properties.getHeight());
415 if (clipFlags) {
416 properties.getClippingRectForFlags(clipFlags, &layerBounds);
John Reck1bcacfd2017-11-03 10:12:19 -0700417 clipFlags = 0; // all clipping done by savelayer
Stan Iliev021693b2016-10-17 16:26:15 -0400418 }
John Reck1bcacfd2017-11-03 10:12:19 -0700419 SkRect bounds = SkRect::MakeLTRB(layerBounds.left, layerBounds.top, layerBounds.right,
420 layerBounds.bottom);
421 canvas->saveLayerAlpha(&bounds, (int)(properties.getAlpha() * 255));
Stan Iliev021693b2016-10-17 16:26:15 -0400422 }
423
424 if (CC_UNLIKELY(ATRACE_ENABLED() && properties.promotedToLayer())) {
425 // pretend alpha always causes savelayer to warn about
426 // performance problem affecting old versions
427 ATRACE_FORMAT("alpha caused saveLayer %dx%d", properties.getWidth(),
John Reck1bcacfd2017-11-03 10:12:19 -0700428 properties.getHeight());
Stan Iliev021693b2016-10-17 16:26:15 -0400429 }
430 }
431
432 const SkRect* pendingClip = nullptr;
433 SkRect clipRect;
434
435 if (clipFlags) {
436 Rect tmpRect;
437 properties.getClippingRectForFlags(clipFlags, &tmpRect);
438 clipRect = tmpRect.toSkRect();
439 pendingClip = &clipRect;
440 }
441
442 if (properties.getRevealClip().willClip()) {
Mike Reed6c67f1d2016-12-14 10:29:54 -0500443 canvas->clipPath(*properties.getRevealClip().getPath(), SkClipOp::kIntersect, true);
Stan Iliev021693b2016-10-17 16:26:15 -0400444 } else if (properties.getOutline().willClip()) {
445 clipOutline(properties.getOutline(), canvas, pendingClip);
446 pendingClip = nullptr;
447 }
448
449 if (pendingClip) {
450 canvas->clipRect(*pendingClip);
451 }
452}
453
Chris Blume7b8a8082018-11-30 15:51:58 -0800454} // namespace skiapipeline
455} // namespace uirenderer
456} // namespace android