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