blob: 9e17b9e6d9853fdb247315eec879516e42c85906 [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>
rnleece9762b2021-05-21 15:40:53 -070019#include <gui/TraceUtils.h>
Stan Iliev021693b2016-10-17 16:26:15 -040020#include "RenderNode.h"
21#include "SkiaDisplayList.h"
rnleece9762b2021-05-21 15:40:53 -070022#include "StretchMask.h"
Nader Jawad2dc632a2021-03-29 18:51:29 -070023#include "TransformCanvas.h"
Stan Iliev021693b2016-10-17 16:26:15 -040024
John Reck5cb290b2021-02-01 13:47:31 -050025#include <include/effects/SkImageFilters.h>
26
Ben Wagner1d155332018-08-20 18:36:05 -040027#include <optional>
28
Stan Iliev021693b2016-10-17 16:26:15 -040029namespace android {
30namespace uirenderer {
31namespace skiapipeline {
32
John Reckd9d7f122018-05-03 14:40:56 -070033RenderNodeDrawable::RenderNodeDrawable(RenderNode* node, SkCanvas* canvas, bool composeLayer,
34 bool inReorderingSection)
35 : mRenderNode(node)
36 , mRecordedTransform(canvas->getTotalMatrix())
37 , mComposeLayer(composeLayer)
38 , mInReorderingSection(inReorderingSection) {}
39
40RenderNodeDrawable::~RenderNodeDrawable() {
41 // Just here to move the destructor into the cpp file where we can access RenderNode.
42
43 // TODO: Detangle the header nightmare.
44}
45
John Reck1bcacfd2017-11-03 10:12:19 -070046void RenderNodeDrawable::drawBackwardsProjectedNodes(SkCanvas* canvas,
47 const SkiaDisplayList& displayList,
Nathaniel Nifong2945bff2019-11-25 09:34:21 -050048 int nestLevel) const {
Stan Ilievdb45a4b2016-11-08 14:18:31 -050049 LOG_ALWAYS_FATAL_IF(0 == nestLevel && !displayList.mProjectionReceiver);
50 for (auto& child : displayList.mChildNodes) {
51 const RenderProperties& childProperties = child.getNodeProperties();
52
John Reck1bcacfd2017-11-03 10:12:19 -070053 // immediate children cannot be projected on their parent
Stan Ilievdb45a4b2016-11-08 14:18:31 -050054 if (childProperties.getProjectBackwards() && nestLevel > 0) {
55 SkAutoCanvasRestore acr2(canvas, true);
John Reck1bcacfd2017-11-03 10:12:19 -070056 // Apply recorded matrix, which is a total matrix saved at recording time to avoid
57 // replaying all DL commands.
Stan Ilievdb45a4b2016-11-08 14:18:31 -050058 canvas->concat(child.getRecordedMatrix());
59 child.drawContent(canvas);
60 }
61
John Reck1bcacfd2017-11-03 10:12:19 -070062 // skip walking sub-nodes if current display list contains a receiver with exception of
63 // level 0, which is a known receiver
Stan Ilievdb45a4b2016-11-08 14:18:31 -050064 if (0 == nestLevel || !displayList.containsProjectionReceiver()) {
65 SkAutoCanvasRestore acr(canvas, true);
66 SkMatrix nodeMatrix;
67 mat4 hwuiMatrix(child.getRecordedMatrix());
John Reckbe671952021-01-13 22:39:32 -050068 const RenderNode* childNode = child.getRenderNode();
Stan Ilievdb45a4b2016-11-08 14:18:31 -050069 childNode->applyViewPropertyTransforms(hwuiMatrix);
70 hwuiMatrix.copyTo(nodeMatrix);
71 canvas->concat(nodeMatrix);
John Reckbe671952021-01-13 22:39:32 -050072 const SkiaDisplayList* childDisplayList = childNode->getDisplayList().asSkiaDl();
Stan Ilievdb45a4b2016-11-08 14:18:31 -050073 if (childDisplayList) {
John Reck1bcacfd2017-11-03 10:12:19 -070074 drawBackwardsProjectedNodes(canvas, *childDisplayList, nestLevel + 1);
Stan Ilievdb45a4b2016-11-08 14:18:31 -050075 }
76 }
77 }
78}
79
Stan Iliev021693b2016-10-17 16:26:15 -040080static void clipOutline(const Outline& outline, SkCanvas* canvas, const SkRect* pendingClip) {
Stan Iliev021693b2016-10-17 16:26:15 -040081 Rect possibleRect;
82 float radius;
Derek Sollenbergerf209c062017-05-26 12:11:34 -040083
84 /* To match the existing HWUI behavior we only supports rectangles or
85 * rounded rectangles; passing in a more complicated outline fails silently.
86 */
87 if (!outline.getAsRoundRect(&possibleRect, &radius)) {
88 if (pendingClip) {
89 canvas->clipRect(*pendingClip);
90 }
Chet Haase33c1ea72021-09-27 20:56:08 +000091 const SkPath* path = outline.getPath();
92 if (path) {
93 canvas->clipPath(*path, SkClipOp::kIntersect, true);
94 }
Derek Sollenbergerf209c062017-05-26 12:11:34 -040095 return;
96 }
97
Stan Iliev021693b2016-10-17 16:26:15 -040098 SkRect rect = possibleRect.toSkRect();
99 if (radius != 0.0f) {
100 if (pendingClip && !pendingClip->contains(rect)) {
101 canvas->clipRect(*pendingClip);
102 }
Mike Reed6c67f1d2016-12-14 10:29:54 -0500103 canvas->clipRRect(SkRRect::MakeRectXY(rect, radius, radius), SkClipOp::kIntersect, true);
Stan Iliev021693b2016-10-17 16:26:15 -0400104 } else {
105 if (pendingClip) {
106 (void)rect.intersect(*pendingClip);
107 }
108 canvas->clipRect(rect);
109 }
110}
111
112const RenderProperties& RenderNodeDrawable::getNodeProperties() const {
113 return mRenderNode->properties();
114}
115
116void RenderNodeDrawable::onDraw(SkCanvas* canvas) {
John Reck1bcacfd2017-11-03 10:12:19 -0700117 // negative and positive Z order are drawn out of order, if this render node drawable is in
118 // a reordering section
Stan Iliev2f06e8a2016-11-02 15:29:03 -0400119 if ((!mInReorderingSection) || MathUtils::isZero(mRenderNode->properties().getZ())) {
Stan Iliev021693b2016-10-17 16:26:15 -0400120 this->forceDraw(canvas);
121 }
122}
123
John Reckf96b2842018-11-29 09:44:10 -0800124class MarkDraw {
125public:
126 explicit MarkDraw(SkCanvas& canvas, RenderNode& node) : mCanvas(canvas), mNode(node) {
127 if (CC_UNLIKELY(Properties::skpCaptureEnabled)) {
128 mNode.markDrawStart(mCanvas);
129 }
130 }
131 ~MarkDraw() {
132 if (CC_UNLIKELY(Properties::skpCaptureEnabled)) {
133 mNode.markDrawEnd(mCanvas);
134 }
135 }
John Reck283bb462018-12-13 16:40:14 -0800136
John Reckf96b2842018-11-29 09:44:10 -0800137private:
138 SkCanvas& mCanvas;
139 RenderNode& mNode;
140};
141
Nathaniel Nifong2945bff2019-11-25 09:34:21 -0500142void RenderNodeDrawable::forceDraw(SkCanvas* canvas) const {
Stan Iliev021693b2016-10-17 16:26:15 -0400143 RenderNode* renderNode = mRenderNode.get();
John Reckf96b2842018-11-29 09:44:10 -0800144 MarkDraw _marker{*canvas, *renderNode};
Stan Iliev021693b2016-10-17 16:26:15 -0400145
146 // We only respect the nothingToDraw check when we are composing a layer. This
147 // ensures that we paint the layer even if it is not currently visible in the
148 // event that the properties change and it becomes visible.
Yuqian Li5ebbc8e2017-11-29 09:53:06 -0500149 if ((mProjectedDisplayList == nullptr && !renderNode->isRenderable()) ||
John Reck283bb462018-12-13 16:40:14 -0800150 (renderNode->nothingToDraw() && mComposeLayer)) {
Stan Iliev021693b2016-10-17 16:26:15 -0400151 return;
152 }
153
John Reckbe671952021-01-13 22:39:32 -0500154 SkiaDisplayList* displayList = renderNode->getDisplayList().asSkiaDl();
Stan Iliev021693b2016-10-17 16:26:15 -0400155
156 SkAutoCanvasRestore acr(canvas, true);
Stan Iliev021693b2016-10-17 16:26:15 -0400157 const RenderProperties& properties = this->getNodeProperties();
John Reck1bcacfd2017-11-03 10:12:19 -0700158 // pass this outline to the children that may clip backward projected nodes
159 displayList->mProjectedOutline =
160 displayList->containsProjectionReceiver() ? &properties.getOutline() : nullptr;
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500161 if (!properties.getProjectBackwards()) {
162 drawContent(canvas);
163 if (mProjectedDisplayList) {
John Reck1bcacfd2017-11-03 10:12:19 -0700164 acr.restore(); // draw projected children using parent matrix
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500165 LOG_ALWAYS_FATAL_IF(!mProjectedDisplayList->mProjectedOutline);
166 const bool shouldClip = mProjectedDisplayList->mProjectedOutline->getPath();
167 SkAutoCanvasRestore acr2(canvas, shouldClip);
Stan Iliev54d70322018-06-14 18:00:10 -0400168 canvas->setMatrix(mProjectedDisplayList->mParentMatrix);
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500169 if (shouldClip) {
Stan Iliev8b7cc512019-02-22 10:16:43 -0500170 canvas->clipPath(*mProjectedDisplayList->mProjectedOutline->getPath());
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500171 }
172 drawBackwardsProjectedNodes(canvas, *mProjectedDisplayList);
Stan Iliev021693b2016-10-17 16:26:15 -0400173 }
Stan Iliev021693b2016-10-17 16:26:15 -0400174 }
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500175 displayList->mProjectedOutline = nullptr;
Stan Iliev021693b2016-10-17 16:26:15 -0400176}
177
Nader Jawad6aff4812021-06-09 10:14:43 -0700178static bool layerNeedsPaint(const LayerProperties& properties, float alphaMultiplier,
179 SkPaint* paint) {
John Reck1bcacfd2017-11-03 10:12:19 -0700180 if (alphaMultiplier < 1.0f || properties.alpha() < 255 ||
Nader Jawad390d6e82020-09-24 21:35:03 -0700181 properties.xferMode() != SkBlendMode::kSrcOver || properties.getColorFilter() != nullptr ||
Nader Jawad6aff4812021-06-09 10:14:43 -0700182 properties.getStretchEffect().requiresLayer()) {
Stan Iliev021693b2016-10-17 16:26:15 -0400183 paint->setAlpha(properties.alpha() * alphaMultiplier);
184 paint->setBlendMode(properties.xferMode());
Ben Wagnerc1a8a462018-07-12 12:41:28 -0400185 paint->setColorFilter(sk_ref_sp(properties.getColorFilter()));
Stan Iliev021693b2016-10-17 16:26:15 -0400186 return true;
187 }
188 return false;
189}
190
Stan Iliev1843ac72017-09-20 18:05:35 -0400191class AlphaFilterCanvas : public SkPaintFilterCanvas {
192public:
193 AlphaFilterCanvas(SkCanvas* canvas, float alpha) : SkPaintFilterCanvas(canvas), mAlpha(alpha) {}
John Reck1bcacfd2017-11-03 10:12:19 -0700194
Stan Iliev1843ac72017-09-20 18:05:35 -0400195protected:
Ben Wagner62b38942019-04-15 11:59:33 -0400196 bool onFilter(SkPaint& paint) const override {
197 paint.setAlpha((uint8_t)paint.getAlpha() * mAlpha);
Stan Iliev1843ac72017-09-20 18:05:35 -0400198 return true;
199 }
200 void onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) override {
201 // We unroll the drawable using "this" canvas, so that draw calls contained inside will
Ben Wagner62b38942019-04-15 11:59:33 -0400202 // get their alpha applied. The default SkPaintFilterCanvas::onDrawDrawable does not unroll.
Stan Iliev1843ac72017-09-20 18:05:35 -0400203 drawable->draw(this, matrix);
204 }
John Reck1bcacfd2017-11-03 10:12:19 -0700205
Stan Iliev1843ac72017-09-20 18:05:35 -0400206private:
207 float mAlpha;
208};
209
Stan Iliev021693b2016-10-17 16:26:15 -0400210void RenderNodeDrawable::drawContent(SkCanvas* canvas) const {
211 RenderNode* renderNode = mRenderNode.get();
212 float alphaMultiplier = 1.0f;
213 const RenderProperties& properties = renderNode->properties();
214
215 // If we are drawing the contents of layer, we don't want to apply any of
216 // the RenderNode's properties during this pass. Those will all be applied
217 // when the layer is composited.
218 if (mComposeLayer) {
219 setViewProperties(properties, canvas, &alphaMultiplier);
220 }
John Reckbe671952021-01-13 22:39:32 -0500221 SkiaDisplayList* displayList = mRenderNode->getDisplayList().asSkiaDl();
Stan Iliev54d70322018-06-14 18:00:10 -0400222 displayList->mParentMatrix = canvas->getTotalMatrix();
Stan Iliev021693b2016-10-17 16:26:15 -0400223
John Reck1bcacfd2017-11-03 10:12:19 -0700224 // TODO should we let the bound of the drawable do this for us?
Stan Iliev021693b2016-10-17 16:26:15 -0400225 const SkRect bounds = SkRect::MakeWH(properties.getWidth(), properties.getHeight());
226 bool quickRejected = properties.getClipToBounds() && canvas->quickReject(bounds);
227 if (!quickRejected) {
Zhao Qin93f99892022-10-14 11:52:44 +0800228 auto clipBounds = canvas->getLocalClipBounds();
229 SkIRect srcBounds = SkIRect::MakeWH(bounds.width(), bounds.height());
230 SkIPoint offset = SkIPoint::Make(0.0f, 0.0f);
John Reckbe671952021-01-13 22:39:32 -0500231 SkiaDisplayList* displayList = renderNode->getDisplayList().asSkiaDl();
Stan Iliev021693b2016-10-17 16:26:15 -0400232 const LayerProperties& layerProperties = properties.layerProperties();
233 // composing a hardware layer
234 if (renderNode->getLayerSurface() && mComposeLayer) {
235 SkASSERT(properties.effectiveLayerType() == LayerType::RenderLayer);
Stan Iliev7e100e92018-01-22 10:36:33 -0500236 SkPaint paint;
Nader Jawad6aff4812021-06-09 10:14:43 -0700237 layerNeedsPaint(layerProperties, alphaMultiplier, &paint);
Nader Jawad4a6b60a2021-09-20 21:22:50 -0700238 sk_sp<SkImage> snapshotImage;
239 auto* imageFilter = layerProperties.getImageFilter();
240 auto recordingContext = canvas->recordingContext();
241 // On some GL vendor implementations, caching the result of
242 // getLayerSurface->makeImageSnapshot() causes a call to
243 // Fence::waitForever without a corresponding signal. This would
244 // lead to ANRs throughout the system.
245 // Instead only cache the SkImage created with the SkImageFilter
246 // for supported devices. Otherwise just create a new SkImage with
247 // the corresponding SkImageFilter each time.
248 // See b/193145089 and b/197263715
249 if (!Properties::enableRenderEffectCache) {
Nader Jawad74eaabe2021-09-27 17:48:15 -0700250 snapshotImage = renderNode->getLayerSurface()->makeImageSnapshot();
Nader Jawad4a6b60a2021-09-20 21:22:50 -0700251 if (imageFilter) {
252 auto subset = SkIRect::MakeWH(srcBounds.width(), srcBounds.height());
253 snapshotImage = snapshotImage->makeWithFilter(recordingContext, imageFilter,
254 subset, clipBounds.roundOut(),
255 &srcBounds, &offset);
Nader Jawad4a6b60a2021-09-20 21:22:50 -0700256 }
257 } else {
258 const auto snapshotResult = renderNode->updateSnapshotIfRequired(
259 recordingContext, layerProperties.getImageFilter(), clipBounds.roundOut());
260 snapshotImage = snapshotResult->snapshot;
261 srcBounds = snapshotResult->outSubset;
262 offset = snapshotResult->outOffset;
263 }
264
Nader Jawad6aff4812021-06-09 10:14:43 -0700265 const auto dstBounds = SkIRect::MakeXYWH(offset.x(),
266 offset.y(),
267 srcBounds.width(),
268 srcBounds.height());
Mike Reed7994a312021-01-28 18:06:26 -0500269 SkSamplingOptions sampling(SkFilterMode::kLinear);
Derek Sollenberger03e6cff72017-12-04 15:07:08 -0500270
271 // surfaces for layers are created on LAYER_SIZE boundaries (which are >= layer size) so
272 // we need to restrict the portion of the surface drawn to the size of the renderNode.
273 SkASSERT(renderNode->getLayerSurface()->width() >= bounds.width());
274 SkASSERT(renderNode->getLayerSurface()->height() >= bounds.height());
Nathaniel Nifong2945bff2019-11-25 09:34:21 -0500275
276 // If SKP recording is active save an annotation that indicates this drawImageRect
277 // could also be rendered with the commands saved at ID associated with this node.
278 if (CC_UNLIKELY(Properties::skpCaptureEnabled)) {
279 canvas->drawAnnotation(bounds, String8::format(
280 "SurfaceID|%" PRId64, renderNode->uniqueId()).c_str(), nullptr);
281 }
Nader Jawad2dc632a2021-03-29 18:51:29 -0700282
Nader Jawad8f5d66b2021-04-07 16:05:13 -0700283 const StretchEffect& stretch = properties.layerProperties().getStretchEffect();
John Reck8ed00dc2021-05-10 13:09:27 -0400284 if (stretch.isEmpty() ||
Nader Jawad93d6e242021-05-17 18:12:29 -0700285 Properties::getStretchEffectBehavior() == StretchEffectBehavior::UniformScale) {
Nader Jawad197743f2021-04-19 19:45:13 -0700286 // If we don't have any stretch effects, issue the filtered
287 // canvas draw calls to make sure we still punch a hole
288 // with the same canvas transformation + clip into the target
289 // canvas then draw the layer on top
290 if (renderNode->hasHolePunches()) {
291 TransformCanvas transformCanvas(canvas, SkBlendMode::kClear);
292 displayList->draw(&transformCanvas);
293 }
Nader Jawad6aff4812021-06-09 10:14:43 -0700294 canvas->drawImageRect(snapshotImage, SkRect::Make(srcBounds),
295 SkRect::Make(dstBounds), sampling, &paint,
Nader Jawad8f5d66b2021-04-07 16:05:13 -0700296 SkCanvas::kStrict_SrcRectConstraint);
297 } else {
Nader Jawad197743f2021-04-19 19:45:13 -0700298 // If we do have stretch effects and have hole punches,
299 // then create a mask and issue the filtered draw calls to
300 // get the corresponding hole punches.
301 // Then apply the stretch to the mask and draw the mask to
302 // the destination
Nader Jawad6aff4812021-06-09 10:14:43 -0700303 // Also if the stretchy container has an ImageFilter applied
304 // to it (i.e. blur) we need to take into account the offset
305 // that will be generated with this result. Ex blurs will "grow"
306 // the source image by the blur radius so we need to translate
307 // the shader by the same amount to render in the same location
308 SkMatrix matrix;
309 matrix.setTranslate(
310 offset.x() - srcBounds.left(),
311 offset.y() - srcBounds.top()
312 );
Nader Jawad197743f2021-04-19 19:45:13 -0700313 if (renderNode->hasHolePunches()) {
314 GrRecordingContext* context = canvas->recordingContext();
315 StretchMask& stretchMask = renderNode->getStretchMask();
316 stretchMask.draw(context,
317 stretch,
318 bounds,
319 displayList,
320 canvas);
321 }
322
Nader Jawad6aff4812021-06-09 10:14:43 -0700323 sk_sp<SkShader> stretchShader =
324 stretch.getShader(bounds.width(), bounds.height(), snapshotImage, &matrix);
Nader Jawad8f5d66b2021-04-07 16:05:13 -0700325 paint.setShader(stretchShader);
Nader Jawad6aff4812021-06-09 10:14:43 -0700326 canvas->drawRect(SkRect::Make(dstBounds), paint);
Nader Jawad8f5d66b2021-04-07 16:05:13 -0700327 }
Matt Sarett79756be2016-11-09 16:13:54 -0500328
Matt Sarettf58cc922016-11-14 18:33:38 -0500329 if (!renderNode->getSkiaLayer()->hasRenderedSinceRepaint) {
Matt Sarett79756be2016-11-09 16:13:54 -0500330 renderNode->getSkiaLayer()->hasRenderedSinceRepaint = true;
Matt Sarettf58cc922016-11-14 18:33:38 -0500331 if (CC_UNLIKELY(Properties::debugLayersUpdates)) {
332 SkPaint layerPaint;
333 layerPaint.setColor(0x7f00ff00);
334 canvas->drawRect(bounds, layerPaint);
335 } else if (CC_UNLIKELY(Properties::debugOverdraw)) {
336 // Render transparent rect to increment overdraw for repaint area.
337 // This can be "else if" because flashing green on layer updates
338 // will also increment the overdraw if it happens to be turned on.
339 SkPaint transparentPaint;
340 transparentPaint.setColor(SK_ColorTRANSPARENT);
341 canvas->drawRect(bounds, transparentPaint);
342 }
Matt Sarett79756be2016-11-09 16:13:54 -0500343 }
Stan Iliev021693b2016-10-17 16:26:15 -0400344 } else {
Stan Iliev1843ac72017-09-20 18:05:35 -0400345 if (alphaMultiplier < 1.0f) {
346 // Non-layer draw for a view with getHasOverlappingRendering=false, will apply
347 // the alpha to the paint of each nested draw.
348 AlphaFilterCanvas alphaCanvas(canvas, alphaMultiplier);
349 displayList->draw(&alphaCanvas);
350 } else {
351 displayList->draw(canvas);
352 }
Stan Iliev021693b2016-10-17 16:26:15 -0400353 }
354 }
355}
356
357void RenderNodeDrawable::setViewProperties(const RenderProperties& properties, SkCanvas* canvas,
John Reck1bcacfd2017-11-03 10:12:19 -0700358 float* alphaMultiplier) {
Stan Iliev021693b2016-10-17 16:26:15 -0400359 if (properties.getLeft() != 0 || properties.getTop() != 0) {
360 canvas->translate(properties.getLeft(), properties.getTop());
361 }
362 if (properties.getStaticMatrix()) {
363 canvas->concat(*properties.getStaticMatrix());
364 } else if (properties.getAnimationMatrix()) {
365 canvas->concat(*properties.getAnimationMatrix());
366 }
367 if (properties.hasTransformMatrix()) {
368 if (properties.isTransformTranslateOnly()) {
369 canvas->translate(properties.getTranslationX(), properties.getTranslationY());
370 } else {
371 canvas->concat(*properties.getTransformMatrix());
372 }
373 }
Nader Jawad93d6e242021-05-17 18:12:29 -0700374 if (Properties::getStretchEffectBehavior() == StretchEffectBehavior::UniformScale) {
John Reck8ed00dc2021-05-10 13:09:27 -0400375 const StretchEffect& stretch = properties.layerProperties().getStretchEffect();
376 if (!stretch.isEmpty()) {
377 canvas->concat(
378 stretch.makeLinearStretch(properties.getWidth(), properties.getHeight()));
379 }
380 }
Stan Iliev021693b2016-10-17 16:26:15 -0400381 const bool isLayer = properties.effectiveLayerType() != LayerType::None;
382 int clipFlags = properties.getClippingFlags();
383 if (properties.getAlpha() < 1) {
384 if (isLayer) {
John Reck1bcacfd2017-11-03 10:12:19 -0700385 clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
Stan Iliev021693b2016-10-17 16:26:15 -0400386 }
387 if (CC_LIKELY(isLayer || !properties.getHasOverlappingRendering())) {
388 *alphaMultiplier = properties.getAlpha();
389 } else {
390 // savelayer needed to create an offscreen buffer
391 Rect layerBounds(0, 0, properties.getWidth(), properties.getHeight());
392 if (clipFlags) {
393 properties.getClippingRectForFlags(clipFlags, &layerBounds);
John Reck1bcacfd2017-11-03 10:12:19 -0700394 clipFlags = 0; // all clipping done by savelayer
Stan Iliev021693b2016-10-17 16:26:15 -0400395 }
John Reck1bcacfd2017-11-03 10:12:19 -0700396 SkRect bounds = SkRect::MakeLTRB(layerBounds.left, layerBounds.top, layerBounds.right,
397 layerBounds.bottom);
398 canvas->saveLayerAlpha(&bounds, (int)(properties.getAlpha() * 255));
Stan Iliev021693b2016-10-17 16:26:15 -0400399 }
400
401 if (CC_UNLIKELY(ATRACE_ENABLED() && properties.promotedToLayer())) {
402 // pretend alpha always causes savelayer to warn about
403 // performance problem affecting old versions
404 ATRACE_FORMAT("alpha caused saveLayer %dx%d", properties.getWidth(),
John Reck1bcacfd2017-11-03 10:12:19 -0700405 properties.getHeight());
Stan Iliev021693b2016-10-17 16:26:15 -0400406 }
407 }
408
409 const SkRect* pendingClip = nullptr;
410 SkRect clipRect;
411
412 if (clipFlags) {
413 Rect tmpRect;
414 properties.getClippingRectForFlags(clipFlags, &tmpRect);
415 clipRect = tmpRect.toSkRect();
416 pendingClip = &clipRect;
417 }
418
419 if (properties.getRevealClip().willClip()) {
Mike Reed6c67f1d2016-12-14 10:29:54 -0500420 canvas->clipPath(*properties.getRevealClip().getPath(), SkClipOp::kIntersect, true);
Stan Iliev021693b2016-10-17 16:26:15 -0400421 } else if (properties.getOutline().willClip()) {
422 clipOutline(properties.getOutline(), canvas, pendingClip);
423 pendingClip = nullptr;
424 }
425
426 if (pendingClip) {
427 canvas->clipRect(*pendingClip);
428 }
429}
430
Chris Blume7b8a8082018-11-30 15:51:58 -0800431} // namespace skiapipeline
432} // namespace uirenderer
433} // namespace android