Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 1 | /* |
| 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 Lubick | 856848e | 2022-02-24 16:24:09 +0000 | [diff] [blame^] | 18 | #include <SkPaint.h> |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 19 | #include <SkPaintFilterCanvas.h> |
Kevin Lubick | 856848e | 2022-02-24 16:24:09 +0000 | [diff] [blame^] | 20 | #include <SkPoint.h> |
| 21 | #include <SkRRect.h> |
| 22 | #include <SkRect.h> |
rnlee | ce9762b | 2021-05-21 15:40:53 -0700 | [diff] [blame] | 23 | #include <gui/TraceUtils.h> |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 24 | #include "RenderNode.h" |
| 25 | #include "SkiaDisplayList.h" |
rnlee | ce9762b | 2021-05-21 15:40:53 -0700 | [diff] [blame] | 26 | #include "StretchMask.h" |
Nader Jawad | 2dc632a | 2021-03-29 18:51:29 -0700 | [diff] [blame] | 27 | #include "TransformCanvas.h" |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 28 | |
John Reck | 5cb290b | 2021-02-01 13:47:31 -0500 | [diff] [blame] | 29 | #include <include/effects/SkImageFilters.h> |
| 30 | |
Ben Wagner | 1d15533 | 2018-08-20 18:36:05 -0400 | [diff] [blame] | 31 | #include <optional> |
| 32 | |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 33 | namespace android { |
| 34 | namespace uirenderer { |
| 35 | namespace skiapipeline { |
| 36 | |
John Reck | d9d7f12 | 2018-05-03 14:40:56 -0700 | [diff] [blame] | 37 | RenderNodeDrawable::RenderNodeDrawable(RenderNode* node, SkCanvas* canvas, bool composeLayer, |
| 38 | bool inReorderingSection) |
| 39 | : mRenderNode(node) |
| 40 | , mRecordedTransform(canvas->getTotalMatrix()) |
| 41 | , mComposeLayer(composeLayer) |
| 42 | , mInReorderingSection(inReorderingSection) {} |
| 43 | |
| 44 | RenderNodeDrawable::~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 Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 50 | void RenderNodeDrawable::drawBackwardsProjectedNodes(SkCanvas* canvas, |
| 51 | const SkiaDisplayList& displayList, |
Nathaniel Nifong | 2945bff | 2019-11-25 09:34:21 -0500 | [diff] [blame] | 52 | int nestLevel) const { |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 53 | LOG_ALWAYS_FATAL_IF(0 == nestLevel && !displayList.mProjectionReceiver); |
| 54 | for (auto& child : displayList.mChildNodes) { |
| 55 | const RenderProperties& childProperties = child.getNodeProperties(); |
| 56 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 57 | // immediate children cannot be projected on their parent |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 58 | if (childProperties.getProjectBackwards() && nestLevel > 0) { |
| 59 | SkAutoCanvasRestore acr2(canvas, true); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 60 | // Apply recorded matrix, which is a total matrix saved at recording time to avoid |
| 61 | // replaying all DL commands. |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 62 | canvas->concat(child.getRecordedMatrix()); |
| 63 | child.drawContent(canvas); |
| 64 | } |
| 65 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 66 | // skip walking sub-nodes if current display list contains a receiver with exception of |
| 67 | // level 0, which is a known receiver |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 68 | if (0 == nestLevel || !displayList.containsProjectionReceiver()) { |
| 69 | SkAutoCanvasRestore acr(canvas, true); |
| 70 | SkMatrix nodeMatrix; |
| 71 | mat4 hwuiMatrix(child.getRecordedMatrix()); |
John Reck | be67195 | 2021-01-13 22:39:32 -0500 | [diff] [blame] | 72 | const RenderNode* childNode = child.getRenderNode(); |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 73 | childNode->applyViewPropertyTransforms(hwuiMatrix); |
| 74 | hwuiMatrix.copyTo(nodeMatrix); |
| 75 | canvas->concat(nodeMatrix); |
John Reck | be67195 | 2021-01-13 22:39:32 -0500 | [diff] [blame] | 76 | const SkiaDisplayList* childDisplayList = childNode->getDisplayList().asSkiaDl(); |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 77 | if (childDisplayList) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 78 | drawBackwardsProjectedNodes(canvas, *childDisplayList, nestLevel + 1); |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 84 | static void clipOutline(const Outline& outline, SkCanvas* canvas, const SkRect* pendingClip) { |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 85 | Rect possibleRect; |
| 86 | float radius; |
Derek Sollenberger | f209c06 | 2017-05-26 12:11:34 -0400 | [diff] [blame] | 87 | |
| 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 Haase | 33c1ea7 | 2021-09-27 20:56:08 +0000 | [diff] [blame] | 95 | const SkPath* path = outline.getPath(); |
| 96 | if (path) { |
| 97 | canvas->clipPath(*path, SkClipOp::kIntersect, true); |
| 98 | } |
Derek Sollenberger | f209c06 | 2017-05-26 12:11:34 -0400 | [diff] [blame] | 99 | return; |
| 100 | } |
| 101 | |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 102 | SkRect rect = possibleRect.toSkRect(); |
| 103 | if (radius != 0.0f) { |
| 104 | if (pendingClip && !pendingClip->contains(rect)) { |
| 105 | canvas->clipRect(*pendingClip); |
| 106 | } |
Mike Reed | 6c67f1d | 2016-12-14 10:29:54 -0500 | [diff] [blame] | 107 | canvas->clipRRect(SkRRect::MakeRectXY(rect, radius, radius), SkClipOp::kIntersect, true); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 108 | } else { |
| 109 | if (pendingClip) { |
| 110 | (void)rect.intersect(*pendingClip); |
| 111 | } |
| 112 | canvas->clipRect(rect); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | const RenderProperties& RenderNodeDrawable::getNodeProperties() const { |
| 117 | return mRenderNode->properties(); |
| 118 | } |
| 119 | |
| 120 | void RenderNodeDrawable::onDraw(SkCanvas* canvas) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 121 | // negative and positive Z order are drawn out of order, if this render node drawable is in |
| 122 | // a reordering section |
Stan Iliev | 2f06e8a | 2016-11-02 15:29:03 -0400 | [diff] [blame] | 123 | if ((!mInReorderingSection) || MathUtils::isZero(mRenderNode->properties().getZ())) { |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 124 | this->forceDraw(canvas); |
| 125 | } |
| 126 | } |
| 127 | |
John Reck | f96b284 | 2018-11-29 09:44:10 -0800 | [diff] [blame] | 128 | class MarkDraw { |
| 129 | public: |
| 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 Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 140 | |
John Reck | f96b284 | 2018-11-29 09:44:10 -0800 | [diff] [blame] | 141 | private: |
| 142 | SkCanvas& mCanvas; |
| 143 | RenderNode& mNode; |
| 144 | }; |
| 145 | |
Nathaniel Nifong | 2945bff | 2019-11-25 09:34:21 -0500 | [diff] [blame] | 146 | void RenderNodeDrawable::forceDraw(SkCanvas* canvas) const { |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 147 | RenderNode* renderNode = mRenderNode.get(); |
John Reck | f96b284 | 2018-11-29 09:44:10 -0800 | [diff] [blame] | 148 | MarkDraw _marker{*canvas, *renderNode}; |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 149 | |
| 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 Li | 5ebbc8e | 2017-11-29 09:53:06 -0500 | [diff] [blame] | 153 | if ((mProjectedDisplayList == nullptr && !renderNode->isRenderable()) || |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 154 | (renderNode->nothingToDraw() && mComposeLayer)) { |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 155 | return; |
| 156 | } |
| 157 | |
John Reck | be67195 | 2021-01-13 22:39:32 -0500 | [diff] [blame] | 158 | SkiaDisplayList* displayList = renderNode->getDisplayList().asSkiaDl(); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 159 | |
| 160 | SkAutoCanvasRestore acr(canvas, true); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 161 | const RenderProperties& properties = this->getNodeProperties(); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 162 | // pass this outline to the children that may clip backward projected nodes |
| 163 | displayList->mProjectedOutline = |
| 164 | displayList->containsProjectionReceiver() ? &properties.getOutline() : nullptr; |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 165 | if (!properties.getProjectBackwards()) { |
| 166 | drawContent(canvas); |
| 167 | if (mProjectedDisplayList) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 168 | acr.restore(); // draw projected children using parent matrix |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 169 | LOG_ALWAYS_FATAL_IF(!mProjectedDisplayList->mProjectedOutline); |
| 170 | const bool shouldClip = mProjectedDisplayList->mProjectedOutline->getPath(); |
| 171 | SkAutoCanvasRestore acr2(canvas, shouldClip); |
Stan Iliev | 54d7032 | 2018-06-14 18:00:10 -0400 | [diff] [blame] | 172 | canvas->setMatrix(mProjectedDisplayList->mParentMatrix); |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 173 | if (shouldClip) { |
Stan Iliev | 8b7cc51 | 2019-02-22 10:16:43 -0500 | [diff] [blame] | 174 | canvas->clipPath(*mProjectedDisplayList->mProjectedOutline->getPath()); |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 175 | } |
| 176 | drawBackwardsProjectedNodes(canvas, *mProjectedDisplayList); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 177 | } |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 178 | } |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 179 | displayList->mProjectedOutline = nullptr; |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 180 | } |
| 181 | |
Nader Jawad | 6aff481 | 2021-06-09 10:14:43 -0700 | [diff] [blame] | 182 | static bool layerNeedsPaint(const LayerProperties& properties, float alphaMultiplier, |
| 183 | SkPaint* paint) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 184 | if (alphaMultiplier < 1.0f || properties.alpha() < 255 || |
Nader Jawad | 390d6e8 | 2020-09-24 21:35:03 -0700 | [diff] [blame] | 185 | properties.xferMode() != SkBlendMode::kSrcOver || properties.getColorFilter() != nullptr || |
Nader Jawad | 6aff481 | 2021-06-09 10:14:43 -0700 | [diff] [blame] | 186 | properties.getStretchEffect().requiresLayer()) { |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 187 | paint->setAlpha(properties.alpha() * alphaMultiplier); |
| 188 | paint->setBlendMode(properties.xferMode()); |
Ben Wagner | c1a8a46 | 2018-07-12 12:41:28 -0400 | [diff] [blame] | 189 | paint->setColorFilter(sk_ref_sp(properties.getColorFilter())); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 190 | return true; |
| 191 | } |
| 192 | return false; |
| 193 | } |
| 194 | |
Stan Iliev | 1843ac7 | 2017-09-20 18:05:35 -0400 | [diff] [blame] | 195 | class AlphaFilterCanvas : public SkPaintFilterCanvas { |
| 196 | public: |
| 197 | AlphaFilterCanvas(SkCanvas* canvas, float alpha) : SkPaintFilterCanvas(canvas), mAlpha(alpha) {} |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 198 | |
Stan Iliev | 1843ac7 | 2017-09-20 18:05:35 -0400 | [diff] [blame] | 199 | protected: |
Ben Wagner | 62b3894 | 2019-04-15 11:59:33 -0400 | [diff] [blame] | 200 | bool onFilter(SkPaint& paint) const override { |
| 201 | paint.setAlpha((uint8_t)paint.getAlpha() * mAlpha); |
Stan Iliev | 1843ac7 | 2017-09-20 18:05:35 -0400 | [diff] [blame] | 202 | return true; |
| 203 | } |
| 204 | void onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) override { |
| 205 | // We unroll the drawable using "this" canvas, so that draw calls contained inside will |
Ben Wagner | 62b3894 | 2019-04-15 11:59:33 -0400 | [diff] [blame] | 206 | // get their alpha applied. The default SkPaintFilterCanvas::onDrawDrawable does not unroll. |
Stan Iliev | 1843ac7 | 2017-09-20 18:05:35 -0400 | [diff] [blame] | 207 | drawable->draw(this, matrix); |
| 208 | } |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 209 | |
Stan Iliev | 1843ac7 | 2017-09-20 18:05:35 -0400 | [diff] [blame] | 210 | private: |
| 211 | float mAlpha; |
| 212 | }; |
| 213 | |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 214 | void RenderNodeDrawable::drawContent(SkCanvas* canvas) const { |
| 215 | RenderNode* renderNode = mRenderNode.get(); |
| 216 | float alphaMultiplier = 1.0f; |
| 217 | const RenderProperties& properties = renderNode->properties(); |
| 218 | |
| 219 | // If we are drawing the contents of layer, we don't want to apply any of |
| 220 | // the RenderNode's properties during this pass. Those will all be applied |
| 221 | // when the layer is composited. |
| 222 | if (mComposeLayer) { |
| 223 | setViewProperties(properties, canvas, &alphaMultiplier); |
| 224 | } |
John Reck | be67195 | 2021-01-13 22:39:32 -0500 | [diff] [blame] | 225 | SkiaDisplayList* displayList = mRenderNode->getDisplayList().asSkiaDl(); |
Stan Iliev | 54d7032 | 2018-06-14 18:00:10 -0400 | [diff] [blame] | 226 | displayList->mParentMatrix = canvas->getTotalMatrix(); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 227 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 228 | // TODO should we let the bound of the drawable do this for us? |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 229 | const SkRect bounds = SkRect::MakeWH(properties.getWidth(), properties.getHeight()); |
| 230 | bool quickRejected = properties.getClipToBounds() && canvas->quickReject(bounds); |
Nader Jawad | 6aff481 | 2021-06-09 10:14:43 -0700 | [diff] [blame] | 231 | auto clipBounds = canvas->getLocalClipBounds(); |
| 232 | SkIRect srcBounds = SkIRect::MakeWH(bounds.width(), bounds.height()); |
| 233 | SkIPoint offset = SkIPoint::Make(0.0f, 0.0f); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 234 | if (!quickRejected) { |
John Reck | be67195 | 2021-01-13 22:39:32 -0500 | [diff] [blame] | 235 | SkiaDisplayList* displayList = renderNode->getDisplayList().asSkiaDl(); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 236 | const LayerProperties& layerProperties = properties.layerProperties(); |
| 237 | // composing a hardware layer |
| 238 | if (renderNode->getLayerSurface() && mComposeLayer) { |
| 239 | SkASSERT(properties.effectiveLayerType() == LayerType::RenderLayer); |
Stan Iliev | 7e100e9 | 2018-01-22 10:36:33 -0500 | [diff] [blame] | 240 | SkPaint paint; |
Nader Jawad | 6aff481 | 2021-06-09 10:14:43 -0700 | [diff] [blame] | 241 | layerNeedsPaint(layerProperties, alphaMultiplier, &paint); |
Nader Jawad | 4a6b60a | 2021-09-20 21:22:50 -0700 | [diff] [blame] | 242 | sk_sp<SkImage> snapshotImage; |
| 243 | auto* imageFilter = layerProperties.getImageFilter(); |
| 244 | auto recordingContext = canvas->recordingContext(); |
| 245 | // On some GL vendor implementations, caching the result of |
| 246 | // getLayerSurface->makeImageSnapshot() causes a call to |
| 247 | // Fence::waitForever without a corresponding signal. This would |
| 248 | // lead to ANRs throughout the system. |
| 249 | // Instead only cache the SkImage created with the SkImageFilter |
| 250 | // for supported devices. Otherwise just create a new SkImage with |
| 251 | // the corresponding SkImageFilter each time. |
| 252 | // See b/193145089 and b/197263715 |
| 253 | if (!Properties::enableRenderEffectCache) { |
Nader Jawad | 74eaabe | 2021-09-27 17:48:15 -0700 | [diff] [blame] | 254 | snapshotImage = renderNode->getLayerSurface()->makeImageSnapshot(); |
Nader Jawad | 4a6b60a | 2021-09-20 21:22:50 -0700 | [diff] [blame] | 255 | if (imageFilter) { |
| 256 | auto subset = SkIRect::MakeWH(srcBounds.width(), srcBounds.height()); |
| 257 | snapshotImage = snapshotImage->makeWithFilter(recordingContext, imageFilter, |
| 258 | subset, clipBounds.roundOut(), |
| 259 | &srcBounds, &offset); |
Nader Jawad | 4a6b60a | 2021-09-20 21:22:50 -0700 | [diff] [blame] | 260 | } |
| 261 | } else { |
| 262 | const auto snapshotResult = renderNode->updateSnapshotIfRequired( |
| 263 | recordingContext, layerProperties.getImageFilter(), clipBounds.roundOut()); |
| 264 | snapshotImage = snapshotResult->snapshot; |
| 265 | srcBounds = snapshotResult->outSubset; |
| 266 | offset = snapshotResult->outOffset; |
| 267 | } |
| 268 | |
Nader Jawad | 6aff481 | 2021-06-09 10:14:43 -0700 | [diff] [blame] | 269 | const auto dstBounds = SkIRect::MakeXYWH(offset.x(), |
| 270 | offset.y(), |
| 271 | srcBounds.width(), |
| 272 | srcBounds.height()); |
Mike Reed | 7994a31 | 2021-01-28 18:06:26 -0500 | [diff] [blame] | 273 | SkSamplingOptions sampling(SkFilterMode::kLinear); |
Derek Sollenberger | 03e6cff7 | 2017-12-04 15:07:08 -0500 | [diff] [blame] | 274 | |
| 275 | // surfaces for layers are created on LAYER_SIZE boundaries (which are >= layer size) so |
| 276 | // we need to restrict the portion of the surface drawn to the size of the renderNode. |
| 277 | SkASSERT(renderNode->getLayerSurface()->width() >= bounds.width()); |
| 278 | SkASSERT(renderNode->getLayerSurface()->height() >= bounds.height()); |
Nathaniel Nifong | 2945bff | 2019-11-25 09:34:21 -0500 | [diff] [blame] | 279 | |
| 280 | // If SKP recording is active save an annotation that indicates this drawImageRect |
| 281 | // could also be rendered with the commands saved at ID associated with this node. |
| 282 | if (CC_UNLIKELY(Properties::skpCaptureEnabled)) { |
| 283 | canvas->drawAnnotation(bounds, String8::format( |
| 284 | "SurfaceID|%" PRId64, renderNode->uniqueId()).c_str(), nullptr); |
| 285 | } |
Nader Jawad | 2dc632a | 2021-03-29 18:51:29 -0700 | [diff] [blame] | 286 | |
Nader Jawad | 8f5d66b | 2021-04-07 16:05:13 -0700 | [diff] [blame] | 287 | const StretchEffect& stretch = properties.layerProperties().getStretchEffect(); |
John Reck | 8ed00dc | 2021-05-10 13:09:27 -0400 | [diff] [blame] | 288 | if (stretch.isEmpty() || |
Nader Jawad | 93d6e24 | 2021-05-17 18:12:29 -0700 | [diff] [blame] | 289 | Properties::getStretchEffectBehavior() == StretchEffectBehavior::UniformScale) { |
Nader Jawad | 197743f | 2021-04-19 19:45:13 -0700 | [diff] [blame] | 290 | // If we don't have any stretch effects, issue the filtered |
| 291 | // canvas draw calls to make sure we still punch a hole |
| 292 | // with the same canvas transformation + clip into the target |
| 293 | // canvas then draw the layer on top |
| 294 | if (renderNode->hasHolePunches()) { |
| 295 | TransformCanvas transformCanvas(canvas, SkBlendMode::kClear); |
| 296 | displayList->draw(&transformCanvas); |
| 297 | } |
Nader Jawad | 6aff481 | 2021-06-09 10:14:43 -0700 | [diff] [blame] | 298 | canvas->drawImageRect(snapshotImage, SkRect::Make(srcBounds), |
| 299 | SkRect::Make(dstBounds), sampling, &paint, |
Nader Jawad | 8f5d66b | 2021-04-07 16:05:13 -0700 | [diff] [blame] | 300 | SkCanvas::kStrict_SrcRectConstraint); |
| 301 | } else { |
Nader Jawad | 197743f | 2021-04-19 19:45:13 -0700 | [diff] [blame] | 302 | // If we do have stretch effects and have hole punches, |
| 303 | // then create a mask and issue the filtered draw calls to |
| 304 | // get the corresponding hole punches. |
| 305 | // Then apply the stretch to the mask and draw the mask to |
| 306 | // the destination |
Nader Jawad | 6aff481 | 2021-06-09 10:14:43 -0700 | [diff] [blame] | 307 | // Also if the stretchy container has an ImageFilter applied |
| 308 | // to it (i.e. blur) we need to take into account the offset |
| 309 | // that will be generated with this result. Ex blurs will "grow" |
| 310 | // the source image by the blur radius so we need to translate |
| 311 | // the shader by the same amount to render in the same location |
| 312 | SkMatrix matrix; |
| 313 | matrix.setTranslate( |
| 314 | offset.x() - srcBounds.left(), |
| 315 | offset.y() - srcBounds.top() |
| 316 | ); |
Nader Jawad | 197743f | 2021-04-19 19:45:13 -0700 | [diff] [blame] | 317 | if (renderNode->hasHolePunches()) { |
| 318 | GrRecordingContext* context = canvas->recordingContext(); |
| 319 | StretchMask& stretchMask = renderNode->getStretchMask(); |
| 320 | stretchMask.draw(context, |
| 321 | stretch, |
| 322 | bounds, |
| 323 | displayList, |
| 324 | canvas); |
| 325 | } |
| 326 | |
Nader Jawad | 6aff481 | 2021-06-09 10:14:43 -0700 | [diff] [blame] | 327 | sk_sp<SkShader> stretchShader = |
| 328 | stretch.getShader(bounds.width(), bounds.height(), snapshotImage, &matrix); |
Nader Jawad | 8f5d66b | 2021-04-07 16:05:13 -0700 | [diff] [blame] | 329 | paint.setShader(stretchShader); |
Nader Jawad | 6aff481 | 2021-06-09 10:14:43 -0700 | [diff] [blame] | 330 | canvas->drawRect(SkRect::Make(dstBounds), paint); |
Nader Jawad | 8f5d66b | 2021-04-07 16:05:13 -0700 | [diff] [blame] | 331 | } |
Matt Sarett | 79756be | 2016-11-09 16:13:54 -0500 | [diff] [blame] | 332 | |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 333 | if (!renderNode->getSkiaLayer()->hasRenderedSinceRepaint) { |
Matt Sarett | 79756be | 2016-11-09 16:13:54 -0500 | [diff] [blame] | 334 | renderNode->getSkiaLayer()->hasRenderedSinceRepaint = true; |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 335 | if (CC_UNLIKELY(Properties::debugLayersUpdates)) { |
| 336 | SkPaint layerPaint; |
| 337 | layerPaint.setColor(0x7f00ff00); |
| 338 | canvas->drawRect(bounds, layerPaint); |
| 339 | } else if (CC_UNLIKELY(Properties::debugOverdraw)) { |
| 340 | // Render transparent rect to increment overdraw for repaint area. |
| 341 | // This can be "else if" because flashing green on layer updates |
| 342 | // will also increment the overdraw if it happens to be turned on. |
| 343 | SkPaint transparentPaint; |
| 344 | transparentPaint.setColor(SK_ColorTRANSPARENT); |
| 345 | canvas->drawRect(bounds, transparentPaint); |
| 346 | } |
Matt Sarett | 79756be | 2016-11-09 16:13:54 -0500 | [diff] [blame] | 347 | } |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 348 | } else { |
Stan Iliev | 1843ac7 | 2017-09-20 18:05:35 -0400 | [diff] [blame] | 349 | if (alphaMultiplier < 1.0f) { |
| 350 | // Non-layer draw for a view with getHasOverlappingRendering=false, will apply |
| 351 | // the alpha to the paint of each nested draw. |
| 352 | AlphaFilterCanvas alphaCanvas(canvas, alphaMultiplier); |
| 353 | displayList->draw(&alphaCanvas); |
| 354 | } else { |
| 355 | displayList->draw(canvas); |
| 356 | } |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | void RenderNodeDrawable::setViewProperties(const RenderProperties& properties, SkCanvas* canvas, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 362 | float* alphaMultiplier) { |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 363 | if (properties.getLeft() != 0 || properties.getTop() != 0) { |
| 364 | canvas->translate(properties.getLeft(), properties.getTop()); |
| 365 | } |
| 366 | if (properties.getStaticMatrix()) { |
| 367 | canvas->concat(*properties.getStaticMatrix()); |
| 368 | } else if (properties.getAnimationMatrix()) { |
| 369 | canvas->concat(*properties.getAnimationMatrix()); |
| 370 | } |
| 371 | if (properties.hasTransformMatrix()) { |
| 372 | if (properties.isTransformTranslateOnly()) { |
| 373 | canvas->translate(properties.getTranslationX(), properties.getTranslationY()); |
| 374 | } else { |
| 375 | canvas->concat(*properties.getTransformMatrix()); |
| 376 | } |
| 377 | } |
Nader Jawad | 93d6e24 | 2021-05-17 18:12:29 -0700 | [diff] [blame] | 378 | if (Properties::getStretchEffectBehavior() == StretchEffectBehavior::UniformScale) { |
John Reck | 8ed00dc | 2021-05-10 13:09:27 -0400 | [diff] [blame] | 379 | const StretchEffect& stretch = properties.layerProperties().getStretchEffect(); |
| 380 | if (!stretch.isEmpty()) { |
| 381 | canvas->concat( |
| 382 | stretch.makeLinearStretch(properties.getWidth(), properties.getHeight())); |
| 383 | } |
| 384 | } |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 385 | const bool isLayer = properties.effectiveLayerType() != LayerType::None; |
| 386 | int clipFlags = properties.getClippingFlags(); |
| 387 | if (properties.getAlpha() < 1) { |
| 388 | if (isLayer) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 389 | clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 390 | } |
| 391 | if (CC_LIKELY(isLayer || !properties.getHasOverlappingRendering())) { |
| 392 | *alphaMultiplier = properties.getAlpha(); |
| 393 | } else { |
| 394 | // savelayer needed to create an offscreen buffer |
| 395 | Rect layerBounds(0, 0, properties.getWidth(), properties.getHeight()); |
| 396 | if (clipFlags) { |
| 397 | properties.getClippingRectForFlags(clipFlags, &layerBounds); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 398 | clipFlags = 0; // all clipping done by savelayer |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 399 | } |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 400 | SkRect bounds = SkRect::MakeLTRB(layerBounds.left, layerBounds.top, layerBounds.right, |
| 401 | layerBounds.bottom); |
| 402 | canvas->saveLayerAlpha(&bounds, (int)(properties.getAlpha() * 255)); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | if (CC_UNLIKELY(ATRACE_ENABLED() && properties.promotedToLayer())) { |
| 406 | // pretend alpha always causes savelayer to warn about |
| 407 | // performance problem affecting old versions |
| 408 | ATRACE_FORMAT("alpha caused saveLayer %dx%d", properties.getWidth(), |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 409 | properties.getHeight()); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | |
| 413 | const SkRect* pendingClip = nullptr; |
| 414 | SkRect clipRect; |
| 415 | |
| 416 | if (clipFlags) { |
| 417 | Rect tmpRect; |
| 418 | properties.getClippingRectForFlags(clipFlags, &tmpRect); |
| 419 | clipRect = tmpRect.toSkRect(); |
| 420 | pendingClip = &clipRect; |
| 421 | } |
| 422 | |
| 423 | if (properties.getRevealClip().willClip()) { |
Mike Reed | 6c67f1d | 2016-12-14 10:29:54 -0500 | [diff] [blame] | 424 | canvas->clipPath(*properties.getRevealClip().getPath(), SkClipOp::kIntersect, true); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 425 | } else if (properties.getOutline().willClip()) { |
| 426 | clipOutline(properties.getOutline(), canvas, pendingClip); |
| 427 | pendingClip = nullptr; |
| 428 | } |
| 429 | |
| 430 | if (pendingClip) { |
| 431 | canvas->clipRect(*pendingClip); |
| 432 | } |
| 433 | } |
| 434 | |
Chris Blume | 7b8a808 | 2018-11-30 15:51:58 -0800 | [diff] [blame] | 435 | } // namespace skiapipeline |
| 436 | } // namespace uirenderer |
| 437 | } // namespace android |