blob: d80cb6d1ab7055c503839946730a2e9dda99a527 [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>
Stan Iliev021693b2016-10-17 16:26:15 -040019#include "RenderNode.h"
20#include "SkiaDisplayList.h"
Stan Iliev500a0c32016-10-26 10:30:09 -040021#include "SkiaPipeline.h"
Stan Iliev021693b2016-10-17 16:26:15 -040022#include "utils/TraceUtils.h"
23
Ben Wagner1d155332018-08-20 18:36:05 -040024#include <optional>
25
Stan Iliev021693b2016-10-17 16:26:15 -040026namespace android {
27namespace uirenderer {
28namespace skiapipeline {
29
John Reckd9d7f122018-05-03 14:40:56 -070030RenderNodeDrawable::RenderNodeDrawable(RenderNode* node, SkCanvas* canvas, bool composeLayer,
31 bool inReorderingSection)
32 : mRenderNode(node)
33 , mRecordedTransform(canvas->getTotalMatrix())
34 , mComposeLayer(composeLayer)
35 , mInReorderingSection(inReorderingSection) {}
36
37RenderNodeDrawable::~RenderNodeDrawable() {
38 // Just here to move the destructor into the cpp file where we can access RenderNode.
39
40 // TODO: Detangle the header nightmare.
41}
42
John Reck1bcacfd2017-11-03 10:12:19 -070043void RenderNodeDrawable::drawBackwardsProjectedNodes(SkCanvas* canvas,
44 const SkiaDisplayList& displayList,
45 int nestLevel) {
Stan Ilievdb45a4b2016-11-08 14:18:31 -050046 LOG_ALWAYS_FATAL_IF(0 == nestLevel && !displayList.mProjectionReceiver);
47 for (auto& child : displayList.mChildNodes) {
48 const RenderProperties& childProperties = child.getNodeProperties();
49
John Reck1bcacfd2017-11-03 10:12:19 -070050 // immediate children cannot be projected on their parent
Stan Ilievdb45a4b2016-11-08 14:18:31 -050051 if (childProperties.getProjectBackwards() && nestLevel > 0) {
52 SkAutoCanvasRestore acr2(canvas, true);
John Reck1bcacfd2017-11-03 10:12:19 -070053 // Apply recorded matrix, which is a total matrix saved at recording time to avoid
54 // replaying all DL commands.
Stan Ilievdb45a4b2016-11-08 14:18:31 -050055 canvas->concat(child.getRecordedMatrix());
56 child.drawContent(canvas);
57 }
58
John Reck1bcacfd2017-11-03 10:12:19 -070059 // skip walking sub-nodes if current display list contains a receiver with exception of
60 // level 0, which is a known receiver
Stan Ilievdb45a4b2016-11-08 14:18:31 -050061 if (0 == nestLevel || !displayList.containsProjectionReceiver()) {
62 SkAutoCanvasRestore acr(canvas, true);
63 SkMatrix nodeMatrix;
64 mat4 hwuiMatrix(child.getRecordedMatrix());
65 auto childNode = child.getRenderNode();
66 childNode->applyViewPropertyTransforms(hwuiMatrix);
67 hwuiMatrix.copyTo(nodeMatrix);
68 canvas->concat(nodeMatrix);
69 SkiaDisplayList* childDisplayList = static_cast<SkiaDisplayList*>(
John Reck1bcacfd2017-11-03 10:12:19 -070070 (const_cast<DisplayList*>(childNode->getDisplayList())));
Stan Ilievdb45a4b2016-11-08 14:18:31 -050071 if (childDisplayList) {
John Reck1bcacfd2017-11-03 10:12:19 -070072 drawBackwardsProjectedNodes(canvas, *childDisplayList, nestLevel + 1);
Stan Ilievdb45a4b2016-11-08 14:18:31 -050073 }
74 }
75 }
76}
77
Stan Iliev021693b2016-10-17 16:26:15 -040078static void clipOutline(const Outline& outline, SkCanvas* canvas, const SkRect* pendingClip) {
Stan Iliev021693b2016-10-17 16:26:15 -040079 Rect possibleRect;
80 float radius;
Derek Sollenbergerf209c062017-05-26 12:11:34 -040081
82 /* To match the existing HWUI behavior we only supports rectangles or
83 * rounded rectangles; passing in a more complicated outline fails silently.
84 */
85 if (!outline.getAsRoundRect(&possibleRect, &radius)) {
86 if (pendingClip) {
87 canvas->clipRect(*pendingClip);
88 }
89 return;
90 }
91
Stan Iliev021693b2016-10-17 16:26:15 -040092 SkRect rect = possibleRect.toSkRect();
93 if (radius != 0.0f) {
94 if (pendingClip && !pendingClip->contains(rect)) {
95 canvas->clipRect(*pendingClip);
96 }
Mike Reed6c67f1d2016-12-14 10:29:54 -050097 canvas->clipRRect(SkRRect::MakeRectXY(rect, radius, radius), SkClipOp::kIntersect, true);
Stan Iliev021693b2016-10-17 16:26:15 -040098 } else {
99 if (pendingClip) {
100 (void)rect.intersect(*pendingClip);
101 }
102 canvas->clipRect(rect);
103 }
104}
105
106const RenderProperties& RenderNodeDrawable::getNodeProperties() const {
107 return mRenderNode->properties();
108}
109
110void RenderNodeDrawable::onDraw(SkCanvas* canvas) {
John Reck1bcacfd2017-11-03 10:12:19 -0700111 // negative and positive Z order are drawn out of order, if this render node drawable is in
112 // a reordering section
Stan Iliev2f06e8a2016-11-02 15:29:03 -0400113 if ((!mInReorderingSection) || MathUtils::isZero(mRenderNode->properties().getZ())) {
Stan Iliev021693b2016-10-17 16:26:15 -0400114 this->forceDraw(canvas);
115 }
116}
117
John Reckf96b2842018-11-29 09:44:10 -0800118class MarkDraw {
119public:
120 explicit MarkDraw(SkCanvas& canvas, RenderNode& node) : mCanvas(canvas), mNode(node) {
121 if (CC_UNLIKELY(Properties::skpCaptureEnabled)) {
122 mNode.markDrawStart(mCanvas);
123 }
124 }
125 ~MarkDraw() {
126 if (CC_UNLIKELY(Properties::skpCaptureEnabled)) {
127 mNode.markDrawEnd(mCanvas);
128 }
129 }
130private:
131 SkCanvas& mCanvas;
132 RenderNode& mNode;
133};
134
Stan Iliev021693b2016-10-17 16:26:15 -0400135void RenderNodeDrawable::forceDraw(SkCanvas* canvas) {
136 RenderNode* renderNode = mRenderNode.get();
John Reckf96b2842018-11-29 09:44:10 -0800137 MarkDraw _marker{*canvas, *renderNode};
Stan Iliev021693b2016-10-17 16:26:15 -0400138
139 // We only respect the nothingToDraw check when we are composing a layer. This
140 // ensures that we paint the layer even if it is not currently visible in the
141 // event that the properties change and it becomes visible.
Yuqian Li5ebbc8e2017-11-29 09:53:06 -0500142 if ((mProjectedDisplayList == nullptr && !renderNode->isRenderable()) ||
143 (renderNode->nothingToDraw() && mComposeLayer)) {
Stan Iliev021693b2016-10-17 16:26:15 -0400144 return;
145 }
146
Stan Iliev021693b2016-10-17 16:26:15 -0400147 SkiaDisplayList* displayList = (SkiaDisplayList*)renderNode->getDisplayList();
148
149 SkAutoCanvasRestore acr(canvas, true);
Stan Iliev021693b2016-10-17 16:26:15 -0400150 const RenderProperties& properties = this->getNodeProperties();
John Reck1bcacfd2017-11-03 10:12:19 -0700151 // pass this outline to the children that may clip backward projected nodes
152 displayList->mProjectedOutline =
153 displayList->containsProjectionReceiver() ? &properties.getOutline() : nullptr;
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500154 if (!properties.getProjectBackwards()) {
155 drawContent(canvas);
156 if (mProjectedDisplayList) {
John Reck1bcacfd2017-11-03 10:12:19 -0700157 acr.restore(); // draw projected children using parent matrix
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500158 LOG_ALWAYS_FATAL_IF(!mProjectedDisplayList->mProjectedOutline);
159 const bool shouldClip = mProjectedDisplayList->mProjectedOutline->getPath();
160 SkAutoCanvasRestore acr2(canvas, shouldClip);
Stan Iliev54d70322018-06-14 18:00:10 -0400161 canvas->setMatrix(mProjectedDisplayList->mParentMatrix);
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500162 if (shouldClip) {
163 clipOutline(*mProjectedDisplayList->mProjectedOutline, canvas, nullptr);
164 }
165 drawBackwardsProjectedNodes(canvas, *mProjectedDisplayList);
Stan Iliev021693b2016-10-17 16:26:15 -0400166 }
Stan Iliev021693b2016-10-17 16:26:15 -0400167 }
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500168 displayList->mProjectedOutline = nullptr;
Stan Iliev021693b2016-10-17 16:26:15 -0400169}
170
John Reck1bcacfd2017-11-03 10:12:19 -0700171static bool layerNeedsPaint(const LayerProperties& properties, float alphaMultiplier,
172 SkPaint* paint) {
Stan Iliev7e100e92018-01-22 10:36:33 -0500173 paint->setFilterQuality(kLow_SkFilterQuality);
John Reck1bcacfd2017-11-03 10:12:19 -0700174 if (alphaMultiplier < 1.0f || properties.alpha() < 255 ||
Ben Wagnerc1a8a462018-07-12 12:41:28 -0400175 properties.xferMode() != SkBlendMode::kSrcOver || properties.getColorFilter() != nullptr) {
Stan Iliev021693b2016-10-17 16:26:15 -0400176 paint->setAlpha(properties.alpha() * alphaMultiplier);
177 paint->setBlendMode(properties.xferMode());
Ben Wagnerc1a8a462018-07-12 12:41:28 -0400178 paint->setColorFilter(sk_ref_sp(properties.getColorFilter()));
Stan Iliev021693b2016-10-17 16:26:15 -0400179 return true;
180 }
181 return false;
182}
183
Stan Iliev1843ac72017-09-20 18:05:35 -0400184class AlphaFilterCanvas : public SkPaintFilterCanvas {
185public:
186 AlphaFilterCanvas(SkCanvas* canvas, float alpha) : SkPaintFilterCanvas(canvas), mAlpha(alpha) {}
John Reck1bcacfd2017-11-03 10:12:19 -0700187
Stan Iliev1843ac72017-09-20 18:05:35 -0400188protected:
189 bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type t) const override {
Ben Wagner1d155332018-08-20 18:36:05 -0400190 std::optional<SkPaint> defaultPaint;
Stan Iliev1843ac72017-09-20 18:05:35 -0400191 if (!*paint) {
Ben Wagner1d155332018-08-20 18:36:05 -0400192 paint->init(defaultPaint.emplace());
Stan Iliev1843ac72017-09-20 18:05:35 -0400193 }
John Reck1bcacfd2017-11-03 10:12:19 -0700194 paint->writable()->setAlpha((uint8_t)(*paint)->getAlpha() * mAlpha);
Stan Iliev1843ac72017-09-20 18:05:35 -0400195 return true;
196 }
197 void onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) override {
198 // We unroll the drawable using "this" canvas, so that draw calls contained inside will
199 // get their alpha applied. THe default SkPaintFilterCanvas::onDrawDrawable does not unroll.
200 drawable->draw(this, matrix);
201 }
John Reck1bcacfd2017-11-03 10:12:19 -0700202
Stan Iliev1843ac72017-09-20 18:05:35 -0400203private:
204 float mAlpha;
205};
206
Stan Iliev021693b2016-10-17 16:26:15 -0400207void RenderNodeDrawable::drawContent(SkCanvas* canvas) const {
208 RenderNode* renderNode = mRenderNode.get();
209 float alphaMultiplier = 1.0f;
210 const RenderProperties& properties = renderNode->properties();
211
212 // If we are drawing the contents of layer, we don't want to apply any of
213 // the RenderNode's properties during this pass. Those will all be applied
214 // when the layer is composited.
215 if (mComposeLayer) {
216 setViewProperties(properties, canvas, &alphaMultiplier);
217 }
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500218 SkiaDisplayList* displayList = (SkiaDisplayList*)mRenderNode->getDisplayList();
Stan Iliev54d70322018-06-14 18:00:10 -0400219 displayList->mParentMatrix = canvas->getTotalMatrix();
Stan Iliev021693b2016-10-17 16:26:15 -0400220
John Reck1bcacfd2017-11-03 10:12:19 -0700221 // TODO should we let the bound of the drawable do this for us?
Stan Iliev021693b2016-10-17 16:26:15 -0400222 const SkRect bounds = SkRect::MakeWH(properties.getWidth(), properties.getHeight());
223 bool quickRejected = properties.getClipToBounds() && canvas->quickReject(bounds);
224 if (!quickRejected) {
225 SkiaDisplayList* displayList = (SkiaDisplayList*)renderNode->getDisplayList();
226 const LayerProperties& layerProperties = properties.layerProperties();
227 // composing a hardware layer
228 if (renderNode->getLayerSurface() && mComposeLayer) {
229 SkASSERT(properties.effectiveLayerType() == LayerType::RenderLayer);
Stan Iliev7e100e92018-01-22 10:36:33 -0500230 SkPaint paint;
231 layerNeedsPaint(layerProperties, alphaMultiplier, &paint);
Derek Sollenberger03e6cff72017-12-04 15:07:08 -0500232
233 // surfaces for layers are created on LAYER_SIZE boundaries (which are >= layer size) so
234 // we need to restrict the portion of the surface drawn to the size of the renderNode.
235 SkASSERT(renderNode->getLayerSurface()->width() >= bounds.width());
236 SkASSERT(renderNode->getLayerSurface()->height() >= bounds.height());
237 canvas->drawImageRect(renderNode->getLayerSurface()->makeImageSnapshot().get(),
Stan Iliev7e100e92018-01-22 10:36:33 -0500238 bounds, bounds, &paint);
Matt Sarett79756be2016-11-09 16:13:54 -0500239
Matt Sarettf58cc922016-11-14 18:33:38 -0500240 if (!renderNode->getSkiaLayer()->hasRenderedSinceRepaint) {
Matt Sarett79756be2016-11-09 16:13:54 -0500241 renderNode->getSkiaLayer()->hasRenderedSinceRepaint = true;
Matt Sarettf58cc922016-11-14 18:33:38 -0500242 if (CC_UNLIKELY(Properties::debugLayersUpdates)) {
243 SkPaint layerPaint;
244 layerPaint.setColor(0x7f00ff00);
245 canvas->drawRect(bounds, layerPaint);
246 } else if (CC_UNLIKELY(Properties::debugOverdraw)) {
247 // Render transparent rect to increment overdraw for repaint area.
248 // This can be "else if" because flashing green on layer updates
249 // will also increment the overdraw if it happens to be turned on.
250 SkPaint transparentPaint;
251 transparentPaint.setColor(SK_ColorTRANSPARENT);
252 canvas->drawRect(bounds, transparentPaint);
253 }
Matt Sarett79756be2016-11-09 16:13:54 -0500254 }
Stan Iliev021693b2016-10-17 16:26:15 -0400255 } else {
Stan Iliev1843ac72017-09-20 18:05:35 -0400256 if (alphaMultiplier < 1.0f) {
257 // Non-layer draw for a view with getHasOverlappingRendering=false, will apply
258 // the alpha to the paint of each nested draw.
259 AlphaFilterCanvas alphaCanvas(canvas, alphaMultiplier);
260 displayList->draw(&alphaCanvas);
261 } else {
262 displayList->draw(canvas);
263 }
Stan Iliev021693b2016-10-17 16:26:15 -0400264 }
265 }
266}
267
268void RenderNodeDrawable::setViewProperties(const RenderProperties& properties, SkCanvas* canvas,
John Reck1bcacfd2017-11-03 10:12:19 -0700269 float* alphaMultiplier) {
Stan Iliev021693b2016-10-17 16:26:15 -0400270 if (properties.getLeft() != 0 || properties.getTop() != 0) {
271 canvas->translate(properties.getLeft(), properties.getTop());
272 }
273 if (properties.getStaticMatrix()) {
274 canvas->concat(*properties.getStaticMatrix());
275 } else if (properties.getAnimationMatrix()) {
276 canvas->concat(*properties.getAnimationMatrix());
277 }
278 if (properties.hasTransformMatrix()) {
279 if (properties.isTransformTranslateOnly()) {
280 canvas->translate(properties.getTranslationX(), properties.getTranslationY());
281 } else {
282 canvas->concat(*properties.getTransformMatrix());
283 }
284 }
285 const bool isLayer = properties.effectiveLayerType() != LayerType::None;
286 int clipFlags = properties.getClippingFlags();
287 if (properties.getAlpha() < 1) {
288 if (isLayer) {
John Reck1bcacfd2017-11-03 10:12:19 -0700289 clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
Stan Iliev021693b2016-10-17 16:26:15 -0400290 }
291 if (CC_LIKELY(isLayer || !properties.getHasOverlappingRendering())) {
292 *alphaMultiplier = properties.getAlpha();
293 } else {
294 // savelayer needed to create an offscreen buffer
295 Rect layerBounds(0, 0, properties.getWidth(), properties.getHeight());
296 if (clipFlags) {
297 properties.getClippingRectForFlags(clipFlags, &layerBounds);
John Reck1bcacfd2017-11-03 10:12:19 -0700298 clipFlags = 0; // all clipping done by savelayer
Stan Iliev021693b2016-10-17 16:26:15 -0400299 }
John Reck1bcacfd2017-11-03 10:12:19 -0700300 SkRect bounds = SkRect::MakeLTRB(layerBounds.left, layerBounds.top, layerBounds.right,
301 layerBounds.bottom);
302 canvas->saveLayerAlpha(&bounds, (int)(properties.getAlpha() * 255));
Stan Iliev021693b2016-10-17 16:26:15 -0400303 }
304
305 if (CC_UNLIKELY(ATRACE_ENABLED() && properties.promotedToLayer())) {
306 // pretend alpha always causes savelayer to warn about
307 // performance problem affecting old versions
308 ATRACE_FORMAT("alpha caused saveLayer %dx%d", properties.getWidth(),
John Reck1bcacfd2017-11-03 10:12:19 -0700309 properties.getHeight());
Stan Iliev021693b2016-10-17 16:26:15 -0400310 }
311 }
312
313 const SkRect* pendingClip = nullptr;
314 SkRect clipRect;
315
316 if (clipFlags) {
317 Rect tmpRect;
318 properties.getClippingRectForFlags(clipFlags, &tmpRect);
319 clipRect = tmpRect.toSkRect();
320 pendingClip = &clipRect;
321 }
322
323 if (properties.getRevealClip().willClip()) {
Mike Reed6c67f1d2016-12-14 10:29:54 -0500324 canvas->clipPath(*properties.getRevealClip().getPath(), SkClipOp::kIntersect, true);
Stan Iliev021693b2016-10-17 16:26:15 -0400325 } else if (properties.getOutline().willClip()) {
326 clipOutline(properties.getOutline(), canvas, pendingClip);
327 pendingClip = nullptr;
328 }
329
330 if (pendingClip) {
331 canvas->clipRect(*pendingClip);
332 }
333}
334
John Reck1bcacfd2017-11-03 10:12:19 -0700335}; // namespace skiapipeline
336}; // namespace uirenderer
337}; // namespace android