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