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