blob: da4f66d45a70b8776f90f6e1e15846a71ca10097 [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"
Kevin Lubick856848e2022-02-24 16:24:09 +000018#include <SkPaint.h>
John Reck1bcacfd2017-11-03 10:12:19 -070019#include <SkPaintFilterCanvas.h>
Kevin Lubick856848e2022-02-24 16:24:09 +000020#include <SkPoint.h>
21#include <SkRRect.h>
22#include <SkRect.h>
rnleece9762b2021-05-21 15:40:53 -070023#include <gui/TraceUtils.h>
Stan Iliev021693b2016-10-17 16:26:15 -040024#include "RenderNode.h"
25#include "SkiaDisplayList.h"
rnleece9762b2021-05-21 15:40:53 -070026#include "StretchMask.h"
Nader Jawad2dc632a2021-03-29 18:51:29 -070027#include "TransformCanvas.h"
Stan Iliev021693b2016-10-17 16:26:15 -040028
John Reck5cb290b2021-02-01 13:47:31 -050029#include <include/effects/SkImageFilters.h>
30
Ben Wagner1d155332018-08-20 18:36:05 -040031#include <optional>
32
Stan Iliev021693b2016-10-17 16:26:15 -040033namespace android {
34namespace uirenderer {
35namespace skiapipeline {
36
John Reckd9d7f122018-05-03 14:40:56 -070037RenderNodeDrawable::RenderNodeDrawable(RenderNode* node, SkCanvas* canvas, bool composeLayer,
38 bool inReorderingSection)
39 : mRenderNode(node)
40 , mRecordedTransform(canvas->getTotalMatrix())
41 , mComposeLayer(composeLayer)
42 , mInReorderingSection(inReorderingSection) {}
43
44RenderNodeDrawable::~RenderNodeDrawable() {
45 // Just here to move the destructor into the cpp file where we can access RenderNode.
46
47 // TODO: Detangle the header nightmare.
48}
49
John Reck1bcacfd2017-11-03 10:12:19 -070050void RenderNodeDrawable::drawBackwardsProjectedNodes(SkCanvas* canvas,
51 const SkiaDisplayList& displayList,
Nathaniel Nifong2945bff2019-11-25 09:34:21 -050052 int nestLevel) const {
Stan Ilievdb45a4b2016-11-08 14:18:31 -050053 LOG_ALWAYS_FATAL_IF(0 == nestLevel && !displayList.mProjectionReceiver);
54 for (auto& child : displayList.mChildNodes) {
55 const RenderProperties& childProperties = child.getNodeProperties();
56
John Reck1bcacfd2017-11-03 10:12:19 -070057 // immediate children cannot be projected on their parent
Stan Ilievdb45a4b2016-11-08 14:18:31 -050058 if (childProperties.getProjectBackwards() && nestLevel > 0) {
59 SkAutoCanvasRestore acr2(canvas, true);
John Reck1bcacfd2017-11-03 10:12:19 -070060 // Apply recorded matrix, which is a total matrix saved at recording time to avoid
61 // replaying all DL commands.
Stan Ilievdb45a4b2016-11-08 14:18:31 -050062 canvas->concat(child.getRecordedMatrix());
63 child.drawContent(canvas);
64 }
65
John Reck1bcacfd2017-11-03 10:12:19 -070066 // skip walking sub-nodes if current display list contains a receiver with exception of
67 // level 0, which is a known receiver
Stan Ilievdb45a4b2016-11-08 14:18:31 -050068 if (0 == nestLevel || !displayList.containsProjectionReceiver()) {
69 SkAutoCanvasRestore acr(canvas, true);
70 SkMatrix nodeMatrix;
71 mat4 hwuiMatrix(child.getRecordedMatrix());
John Reckbe671952021-01-13 22:39:32 -050072 const RenderNode* childNode = child.getRenderNode();
Stan Ilievdb45a4b2016-11-08 14:18:31 -050073 childNode->applyViewPropertyTransforms(hwuiMatrix);
74 hwuiMatrix.copyTo(nodeMatrix);
75 canvas->concat(nodeMatrix);
John Reckbe671952021-01-13 22:39:32 -050076 const SkiaDisplayList* childDisplayList = childNode->getDisplayList().asSkiaDl();
Stan Ilievdb45a4b2016-11-08 14:18:31 -050077 if (childDisplayList) {
John Reck1bcacfd2017-11-03 10:12:19 -070078 drawBackwardsProjectedNodes(canvas, *childDisplayList, nestLevel + 1);
Stan Ilievdb45a4b2016-11-08 14:18:31 -050079 }
80 }
81 }
82}
83
Stan Iliev021693b2016-10-17 16:26:15 -040084static void clipOutline(const Outline& outline, SkCanvas* canvas, const SkRect* pendingClip) {
Stan Iliev021693b2016-10-17 16:26:15 -040085 Rect possibleRect;
86 float radius;
Derek Sollenbergerf209c062017-05-26 12:11:34 -040087
88 /* To match the existing HWUI behavior we only supports rectangles or
89 * rounded rectangles; passing in a more complicated outline fails silently.
90 */
91 if (!outline.getAsRoundRect(&possibleRect, &radius)) {
92 if (pendingClip) {
93 canvas->clipRect(*pendingClip);
94 }
Chet Haase33c1ea72021-09-27 20:56:08 +000095 const SkPath* path = outline.getPath();
96 if (path) {
97 canvas->clipPath(*path, SkClipOp::kIntersect, true);
98 }
Derek Sollenbergerf209c062017-05-26 12:11:34 -040099 return;
100 }
101
Stan Iliev021693b2016-10-17 16:26:15 -0400102 SkRect rect = possibleRect.toSkRect();
103 if (radius != 0.0f) {
104 if (pendingClip && !pendingClip->contains(rect)) {
105 canvas->clipRect(*pendingClip);
106 }
Mike Reed6c67f1d2016-12-14 10:29:54 -0500107 canvas->clipRRect(SkRRect::MakeRectXY(rect, radius, radius), SkClipOp::kIntersect, true);
Stan Iliev021693b2016-10-17 16:26:15 -0400108 } else {
109 if (pendingClip) {
110 (void)rect.intersect(*pendingClip);
111 }
112 canvas->clipRect(rect);
113 }
114}
115
116const RenderProperties& RenderNodeDrawable::getNodeProperties() const {
117 return mRenderNode->properties();
118}
119
120void RenderNodeDrawable::onDraw(SkCanvas* canvas) {
John Reck1bcacfd2017-11-03 10:12:19 -0700121 // negative and positive Z order are drawn out of order, if this render node drawable is in
122 // a reordering section
Stan Iliev2f06e8a2016-11-02 15:29:03 -0400123 if ((!mInReorderingSection) || MathUtils::isZero(mRenderNode->properties().getZ())) {
Stan Iliev021693b2016-10-17 16:26:15 -0400124 this->forceDraw(canvas);
125 }
126}
127
John Reckf96b2842018-11-29 09:44:10 -0800128class MarkDraw {
129public:
130 explicit MarkDraw(SkCanvas& canvas, RenderNode& node) : mCanvas(canvas), mNode(node) {
131 if (CC_UNLIKELY(Properties::skpCaptureEnabled)) {
132 mNode.markDrawStart(mCanvas);
133 }
134 }
135 ~MarkDraw() {
136 if (CC_UNLIKELY(Properties::skpCaptureEnabled)) {
137 mNode.markDrawEnd(mCanvas);
138 }
139 }
John Reck283bb462018-12-13 16:40:14 -0800140
John Reckf96b2842018-11-29 09:44:10 -0800141private:
142 SkCanvas& mCanvas;
143 RenderNode& mNode;
144};
145
Nathaniel Nifong2945bff2019-11-25 09:34:21 -0500146void RenderNodeDrawable::forceDraw(SkCanvas* canvas) const {
Stan Iliev021693b2016-10-17 16:26:15 -0400147 RenderNode* renderNode = mRenderNode.get();
John Reckf96b2842018-11-29 09:44:10 -0800148 MarkDraw _marker{*canvas, *renderNode};
Stan Iliev021693b2016-10-17 16:26:15 -0400149
150 // We only respect the nothingToDraw check when we are composing a layer. This
151 // ensures that we paint the layer even if it is not currently visible in the
152 // event that the properties change and it becomes visible.
Yuqian Li5ebbc8e2017-11-29 09:53:06 -0500153 if ((mProjectedDisplayList == nullptr && !renderNode->isRenderable()) ||
John Reck283bb462018-12-13 16:40:14 -0800154 (renderNode->nothingToDraw() && mComposeLayer)) {
Stan Iliev021693b2016-10-17 16:26:15 -0400155 return;
156 }
157
John Reckbe671952021-01-13 22:39:32 -0500158 SkiaDisplayList* displayList = renderNode->getDisplayList().asSkiaDl();
Stan Iliev021693b2016-10-17 16:26:15 -0400159
160 SkAutoCanvasRestore acr(canvas, true);
Stan Iliev021693b2016-10-17 16:26:15 -0400161 const RenderProperties& properties = this->getNodeProperties();
John Reck1bcacfd2017-11-03 10:12:19 -0700162 // pass this outline to the children that may clip backward projected nodes
163 displayList->mProjectedOutline =
164 displayList->containsProjectionReceiver() ? &properties.getOutline() : nullptr;
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500165 if (!properties.getProjectBackwards()) {
166 drawContent(canvas);
167 if (mProjectedDisplayList) {
John Reck1bcacfd2017-11-03 10:12:19 -0700168 acr.restore(); // draw projected children using parent matrix
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500169 LOG_ALWAYS_FATAL_IF(!mProjectedDisplayList->mProjectedOutline);
170 const bool shouldClip = mProjectedDisplayList->mProjectedOutline->getPath();
171 SkAutoCanvasRestore acr2(canvas, shouldClip);
Stan Iliev54d70322018-06-14 18:00:10 -0400172 canvas->setMatrix(mProjectedDisplayList->mParentMatrix);
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500173 if (shouldClip) {
Stan Iliev8b7cc512019-02-22 10:16:43 -0500174 canvas->clipPath(*mProjectedDisplayList->mProjectedOutline->getPath());
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500175 }
176 drawBackwardsProjectedNodes(canvas, *mProjectedDisplayList);
Stan Iliev021693b2016-10-17 16:26:15 -0400177 }
Stan Iliev021693b2016-10-17 16:26:15 -0400178 }
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500179 displayList->mProjectedOutline = nullptr;
Stan Iliev021693b2016-10-17 16:26:15 -0400180}
181
Nader Jawad6aff4812021-06-09 10:14:43 -0700182static bool layerNeedsPaint(const LayerProperties& properties, float alphaMultiplier,
183 SkPaint* paint) {
John Reck1bcacfd2017-11-03 10:12:19 -0700184 if (alphaMultiplier < 1.0f || properties.alpha() < 255 ||
Nader Jawad390d6e82020-09-24 21:35:03 -0700185 properties.xferMode() != SkBlendMode::kSrcOver || properties.getColorFilter() != nullptr ||
Nader Jawad6aff4812021-06-09 10:14:43 -0700186 properties.getStretchEffect().requiresLayer()) {
Stan Iliev021693b2016-10-17 16:26:15 -0400187 paint->setAlpha(properties.alpha() * alphaMultiplier);
188 paint->setBlendMode(properties.xferMode());
Ben Wagnerc1a8a462018-07-12 12:41:28 -0400189 paint->setColorFilter(sk_ref_sp(properties.getColorFilter()));
Stan Iliev021693b2016-10-17 16:26:15 -0400190 return true;
191 }
192 return false;
193}
194
Stan Iliev1843ac72017-09-20 18:05:35 -0400195class AlphaFilterCanvas : public SkPaintFilterCanvas {
196public:
197 AlphaFilterCanvas(SkCanvas* canvas, float alpha) : SkPaintFilterCanvas(canvas), mAlpha(alpha) {}
John Reck1bcacfd2017-11-03 10:12:19 -0700198
Stan Iliev1843ac72017-09-20 18:05:35 -0400199protected:
Ben Wagner62b38942019-04-15 11:59:33 -0400200 bool onFilter(SkPaint& paint) const override {
201 paint.setAlpha((uint8_t)paint.getAlpha() * mAlpha);
Stan Iliev1843ac72017-09-20 18:05:35 -0400202 return true;
203 }
Alec Mouri655a5e42022-09-12 17:49:17 +0000204
Stan Iliev1843ac72017-09-20 18:05:35 -0400205 void onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) override {
206 // We unroll the drawable using "this" canvas, so that draw calls contained inside will
Ben Wagner62b38942019-04-15 11:59:33 -0400207 // get their alpha applied. The default SkPaintFilterCanvas::onDrawDrawable does not unroll.
Stan Iliev1843ac72017-09-20 18:05:35 -0400208 drawable->draw(this, matrix);
209 }
John Reck1bcacfd2017-11-03 10:12:19 -0700210
Stan Iliev1843ac72017-09-20 18:05:35 -0400211private:
212 float mAlpha;
213};
214
Stan Iliev021693b2016-10-17 16:26:15 -0400215void RenderNodeDrawable::drawContent(SkCanvas* canvas) const {
216 RenderNode* renderNode = mRenderNode.get();
217 float alphaMultiplier = 1.0f;
218 const RenderProperties& properties = renderNode->properties();
219
220 // If we are drawing the contents of layer, we don't want to apply any of
221 // the RenderNode's properties during this pass. Those will all be applied
222 // when the layer is composited.
223 if (mComposeLayer) {
224 setViewProperties(properties, canvas, &alphaMultiplier);
225 }
John Reckbe671952021-01-13 22:39:32 -0500226 SkiaDisplayList* displayList = mRenderNode->getDisplayList().asSkiaDl();
Stan Iliev54d70322018-06-14 18:00:10 -0400227 displayList->mParentMatrix = canvas->getTotalMatrix();
Stan Iliev021693b2016-10-17 16:26:15 -0400228
John Reck1bcacfd2017-11-03 10:12:19 -0700229 // TODO should we let the bound of the drawable do this for us?
Stan Iliev021693b2016-10-17 16:26:15 -0400230 const SkRect bounds = SkRect::MakeWH(properties.getWidth(), properties.getHeight());
231 bool quickRejected = properties.getClipToBounds() && canvas->quickReject(bounds);
232 if (!quickRejected) {
Zhao Qin93f99892022-10-14 11:52:44 +0800233 auto clipBounds = canvas->getLocalClipBounds();
234 SkIRect srcBounds = SkIRect::MakeWH(bounds.width(), bounds.height());
235 SkIPoint offset = SkIPoint::Make(0.0f, 0.0f);
John Reckbe671952021-01-13 22:39:32 -0500236 SkiaDisplayList* displayList = renderNode->getDisplayList().asSkiaDl();
Stan Iliev021693b2016-10-17 16:26:15 -0400237 const LayerProperties& layerProperties = properties.layerProperties();
238 // composing a hardware layer
239 if (renderNode->getLayerSurface() && mComposeLayer) {
240 SkASSERT(properties.effectiveLayerType() == LayerType::RenderLayer);
Stan Iliev7e100e92018-01-22 10:36:33 -0500241 SkPaint paint;
Nader Jawad6aff4812021-06-09 10:14:43 -0700242 layerNeedsPaint(layerProperties, alphaMultiplier, &paint);
Nader Jawad4a6b60a2021-09-20 21:22:50 -0700243 sk_sp<SkImage> snapshotImage;
244 auto* imageFilter = layerProperties.getImageFilter();
245 auto recordingContext = canvas->recordingContext();
246 // On some GL vendor implementations, caching the result of
247 // getLayerSurface->makeImageSnapshot() causes a call to
248 // Fence::waitForever without a corresponding signal. This would
249 // lead to ANRs throughout the system.
250 // Instead only cache the SkImage created with the SkImageFilter
251 // for supported devices. Otherwise just create a new SkImage with
252 // the corresponding SkImageFilter each time.
253 // See b/193145089 and b/197263715
254 if (!Properties::enableRenderEffectCache) {
Nader Jawad74eaabe2021-09-27 17:48:15 -0700255 snapshotImage = renderNode->getLayerSurface()->makeImageSnapshot();
Nader Jawad4a6b60a2021-09-20 21:22:50 -0700256 if (imageFilter) {
257 auto subset = SkIRect::MakeWH(srcBounds.width(), srcBounds.height());
258 snapshotImage = snapshotImage->makeWithFilter(recordingContext, imageFilter,
259 subset, clipBounds.roundOut(),
260 &srcBounds, &offset);
Nader Jawad4a6b60a2021-09-20 21:22:50 -0700261 }
262 } else {
263 const auto snapshotResult = renderNode->updateSnapshotIfRequired(
264 recordingContext, layerProperties.getImageFilter(), clipBounds.roundOut());
265 snapshotImage = snapshotResult->snapshot;
266 srcBounds = snapshotResult->outSubset;
267 offset = snapshotResult->outOffset;
268 }
269
Nader Jawad6aff4812021-06-09 10:14:43 -0700270 const auto dstBounds = SkIRect::MakeXYWH(offset.x(),
271 offset.y(),
272 srcBounds.width(),
273 srcBounds.height());
Mike Reed7994a312021-01-28 18:06:26 -0500274 SkSamplingOptions sampling(SkFilterMode::kLinear);
Derek Sollenberger03e6cff72017-12-04 15:07:08 -0500275
276 // surfaces for layers are created on LAYER_SIZE boundaries (which are >= layer size) so
277 // we need to restrict the portion of the surface drawn to the size of the renderNode.
278 SkASSERT(renderNode->getLayerSurface()->width() >= bounds.width());
279 SkASSERT(renderNode->getLayerSurface()->height() >= bounds.height());
Nathaniel Nifong2945bff2019-11-25 09:34:21 -0500280
281 // If SKP recording is active save an annotation that indicates this drawImageRect
282 // could also be rendered with the commands saved at ID associated with this node.
283 if (CC_UNLIKELY(Properties::skpCaptureEnabled)) {
284 canvas->drawAnnotation(bounds, String8::format(
285 "SurfaceID|%" PRId64, renderNode->uniqueId()).c_str(), nullptr);
286 }
Nader Jawad2dc632a2021-03-29 18:51:29 -0700287
Nader Jawad8f5d66b2021-04-07 16:05:13 -0700288 const StretchEffect& stretch = properties.layerProperties().getStretchEffect();
John Reck8ed00dc2021-05-10 13:09:27 -0400289 if (stretch.isEmpty() ||
Nader Jawad93d6e242021-05-17 18:12:29 -0700290 Properties::getStretchEffectBehavior() == StretchEffectBehavior::UniformScale) {
Nader Jawad197743f2021-04-19 19:45:13 -0700291 // If we don't have any stretch effects, issue the filtered
292 // canvas draw calls to make sure we still punch a hole
293 // with the same canvas transformation + clip into the target
294 // canvas then draw the layer on top
295 if (renderNode->hasHolePunches()) {
Nader Jawad10f50632023-04-12 15:53:28 -0700296 canvas->save();
Alec Mouri655a5e42022-09-12 17:49:17 +0000297 TransformCanvas transformCanvas(canvas, SkBlendMode::kDstOut);
Nader Jawad197743f2021-04-19 19:45:13 -0700298 displayList->draw(&transformCanvas);
Nader Jawad10f50632023-04-12 15:53:28 -0700299 canvas->restore();
Nader Jawad197743f2021-04-19 19:45:13 -0700300 }
Nader Jawad6aff4812021-06-09 10:14:43 -0700301 canvas->drawImageRect(snapshotImage, SkRect::Make(srcBounds),
302 SkRect::Make(dstBounds), sampling, &paint,
Nader Jawad8f5d66b2021-04-07 16:05:13 -0700303 SkCanvas::kStrict_SrcRectConstraint);
304 } else {
Nader Jawad197743f2021-04-19 19:45:13 -0700305 // If we do have stretch effects and have hole punches,
306 // then create a mask and issue the filtered draw calls to
307 // get the corresponding hole punches.
308 // Then apply the stretch to the mask and draw the mask to
309 // the destination
Nader Jawad6aff4812021-06-09 10:14:43 -0700310 // Also if the stretchy container has an ImageFilter applied
311 // to it (i.e. blur) we need to take into account the offset
312 // that will be generated with this result. Ex blurs will "grow"
313 // the source image by the blur radius so we need to translate
314 // the shader by the same amount to render in the same location
315 SkMatrix matrix;
316 matrix.setTranslate(
317 offset.x() - srcBounds.left(),
318 offset.y() - srcBounds.top()
319 );
Nader Jawad197743f2021-04-19 19:45:13 -0700320 if (renderNode->hasHolePunches()) {
321 GrRecordingContext* context = canvas->recordingContext();
322 StretchMask& stretchMask = renderNode->getStretchMask();
323 stretchMask.draw(context,
324 stretch,
325 bounds,
326 displayList,
327 canvas);
328 }
329
Nader Jawad6aff4812021-06-09 10:14:43 -0700330 sk_sp<SkShader> stretchShader =
331 stretch.getShader(bounds.width(), bounds.height(), snapshotImage, &matrix);
Nader Jawad8f5d66b2021-04-07 16:05:13 -0700332 paint.setShader(stretchShader);
Nader Jawad6aff4812021-06-09 10:14:43 -0700333 canvas->drawRect(SkRect::Make(dstBounds), paint);
Nader Jawad8f5d66b2021-04-07 16:05:13 -0700334 }
Matt Sarett79756be2016-11-09 16:13:54 -0500335
Matt Sarettf58cc922016-11-14 18:33:38 -0500336 if (!renderNode->getSkiaLayer()->hasRenderedSinceRepaint) {
Matt Sarett79756be2016-11-09 16:13:54 -0500337 renderNode->getSkiaLayer()->hasRenderedSinceRepaint = true;
Matt Sarettf58cc922016-11-14 18:33:38 -0500338 if (CC_UNLIKELY(Properties::debugLayersUpdates)) {
339 SkPaint layerPaint;
340 layerPaint.setColor(0x7f00ff00);
341 canvas->drawRect(bounds, layerPaint);
342 } else if (CC_UNLIKELY(Properties::debugOverdraw)) {
343 // Render transparent rect to increment overdraw for repaint area.
344 // This can be "else if" because flashing green on layer updates
345 // will also increment the overdraw if it happens to be turned on.
346 SkPaint transparentPaint;
347 transparentPaint.setColor(SK_ColorTRANSPARENT);
348 canvas->drawRect(bounds, transparentPaint);
349 }
Matt Sarett79756be2016-11-09 16:13:54 -0500350 }
Stan Iliev021693b2016-10-17 16:26:15 -0400351 } else {
Stan Iliev1843ac72017-09-20 18:05:35 -0400352 if (alphaMultiplier < 1.0f) {
353 // Non-layer draw for a view with getHasOverlappingRendering=false, will apply
354 // the alpha to the paint of each nested draw.
355 AlphaFilterCanvas alphaCanvas(canvas, alphaMultiplier);
356 displayList->draw(&alphaCanvas);
357 } else {
358 displayList->draw(canvas);
359 }
Stan Iliev021693b2016-10-17 16:26:15 -0400360 }
361 }
362}
363
364void RenderNodeDrawable::setViewProperties(const RenderProperties& properties, SkCanvas* canvas,
John Reck1bcacfd2017-11-03 10:12:19 -0700365 float* alphaMultiplier) {
Stan Iliev021693b2016-10-17 16:26:15 -0400366 if (properties.getLeft() != 0 || properties.getTop() != 0) {
367 canvas->translate(properties.getLeft(), properties.getTop());
368 }
369 if (properties.getStaticMatrix()) {
370 canvas->concat(*properties.getStaticMatrix());
371 } else if (properties.getAnimationMatrix()) {
372 canvas->concat(*properties.getAnimationMatrix());
373 }
374 if (properties.hasTransformMatrix()) {
375 if (properties.isTransformTranslateOnly()) {
376 canvas->translate(properties.getTranslationX(), properties.getTranslationY());
377 } else {
378 canvas->concat(*properties.getTransformMatrix());
379 }
380 }
Nader Jawad93d6e242021-05-17 18:12:29 -0700381 if (Properties::getStretchEffectBehavior() == StretchEffectBehavior::UniformScale) {
John Reck8ed00dc2021-05-10 13:09:27 -0400382 const StretchEffect& stretch = properties.layerProperties().getStretchEffect();
383 if (!stretch.isEmpty()) {
384 canvas->concat(
385 stretch.makeLinearStretch(properties.getWidth(), properties.getHeight()));
386 }
387 }
Stan Iliev021693b2016-10-17 16:26:15 -0400388 const bool isLayer = properties.effectiveLayerType() != LayerType::None;
389 int clipFlags = properties.getClippingFlags();
390 if (properties.getAlpha() < 1) {
391 if (isLayer) {
John Reck1bcacfd2017-11-03 10:12:19 -0700392 clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
Stan Iliev021693b2016-10-17 16:26:15 -0400393 }
394 if (CC_LIKELY(isLayer || !properties.getHasOverlappingRendering())) {
395 *alphaMultiplier = properties.getAlpha();
396 } else {
397 // savelayer needed to create an offscreen buffer
398 Rect layerBounds(0, 0, properties.getWidth(), properties.getHeight());
399 if (clipFlags) {
400 properties.getClippingRectForFlags(clipFlags, &layerBounds);
John Reck1bcacfd2017-11-03 10:12:19 -0700401 clipFlags = 0; // all clipping done by savelayer
Stan Iliev021693b2016-10-17 16:26:15 -0400402 }
John Reck1bcacfd2017-11-03 10:12:19 -0700403 SkRect bounds = SkRect::MakeLTRB(layerBounds.left, layerBounds.top, layerBounds.right,
404 layerBounds.bottom);
405 canvas->saveLayerAlpha(&bounds, (int)(properties.getAlpha() * 255));
Stan Iliev021693b2016-10-17 16:26:15 -0400406 }
407
408 if (CC_UNLIKELY(ATRACE_ENABLED() && properties.promotedToLayer())) {
409 // pretend alpha always causes savelayer to warn about
410 // performance problem affecting old versions
411 ATRACE_FORMAT("alpha caused saveLayer %dx%d", properties.getWidth(),
John Reck1bcacfd2017-11-03 10:12:19 -0700412 properties.getHeight());
Stan Iliev021693b2016-10-17 16:26:15 -0400413 }
414 }
415
416 const SkRect* pendingClip = nullptr;
417 SkRect clipRect;
418
419 if (clipFlags) {
420 Rect tmpRect;
421 properties.getClippingRectForFlags(clipFlags, &tmpRect);
422 clipRect = tmpRect.toSkRect();
423 pendingClip = &clipRect;
424 }
425
426 if (properties.getRevealClip().willClip()) {
Mike Reed6c67f1d2016-12-14 10:29:54 -0500427 canvas->clipPath(*properties.getRevealClip().getPath(), SkClipOp::kIntersect, true);
Stan Iliev021693b2016-10-17 16:26:15 -0400428 } else if (properties.getOutline().willClip()) {
429 clipOutline(properties.getOutline(), canvas, pendingClip);
430 pendingClip = nullptr;
431 }
432
433 if (pendingClip) {
434 canvas->clipRect(*pendingClip);
435 }
436}
437
Chris Blume7b8a8082018-11-30 15:51:58 -0800438} // namespace skiapipeline
439} // namespace uirenderer
440} // namespace android