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