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