xref: /aosp_15_r20/frameworks/base/libs/hwui/pipeline/skia/BackdropFilterDrawable.cpp (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
1 /*
2  * Copyright (C) 2022 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 "BackdropFilterDrawable.h"
18 
19 #include <SkImage.h>
20 #include <SkSurface.h>
21 
22 #include "RenderNode.h"
23 #include "RenderNodeDrawable.h"
24 #ifdef __ANDROID__
25 #include "include/gpu/ganesh/SkImageGanesh.h"
26 #endif
27 
28 namespace android {
29 namespace uirenderer {
30 namespace skiapipeline {
31 
onDraw(SkCanvas * canvas)32 void BackdropFilterDrawable::onDraw(SkCanvas* canvas) {
33     const RenderProperties& properties = mTargetRenderNode->properties();
34     auto* backdropFilter = properties.layerProperties().getBackdropImageFilter();
35     auto* surface = canvas->getSurface();
36     if (!backdropFilter || !surface) {
37         return;
38     }
39 
40     SkRect srcBounds = SkRect::MakeWH(properties.getWidth(), properties.getHeight());
41 
42     float alphaMultiplier = 1.0f;
43     RenderNodeDrawable::setViewProperties(properties, canvas, &alphaMultiplier, true);
44     SkPaint paint;
45     paint.setAlpha(properties.layerProperties().alpha() * alphaMultiplier);
46 
47     SkRect surfaceSubset;
48     canvas->getTotalMatrix().mapRect(&surfaceSubset, srcBounds);
49     if (!surfaceSubset.intersect(SkRect::MakeWH(surface->width(), surface->height()))) {
50         return;
51     }
52 
53     auto backdropImage = surface->makeImageSnapshot(surfaceSubset.roundOut());
54 
55     SkIRect imageBounds = SkIRect::MakeWH(backdropImage->width(), backdropImage->height());
56     SkIPoint offset;
57     SkIRect imageSubset;
58 
59 #ifdef __ANDROID__
60     if (canvas->recordingContext()) {
61         backdropImage =
62                 SkImages::MakeWithFilter(canvas->recordingContext(), backdropImage, backdropFilter,
63                                          imageBounds, imageBounds, &imageSubset, &offset);
64     } else
65 #endif
66     {
67         backdropImage = SkImages::MakeWithFilter(backdropImage, backdropFilter, imageBounds,
68                                                  imageBounds, &imageSubset, &offset);
69     }
70 
71     canvas->save();
72     canvas->resetMatrix();
73     canvas->drawImageRect(backdropImage, SkRect::Make(imageSubset), surfaceSubset,
74                           SkSamplingOptions(SkFilterMode::kLinear), &paint,
75                           SkCanvas::kFast_SrcRectConstraint);
76     canvas->restore();
77 }
78 
79 }  // namespace skiapipeline
80 }  // namespace uirenderer
81 }  // namespace android
82