xref: /aosp_15_r20/frameworks/base/libs/hwui/tests/common/scenes/WindowBlurSkia.cpp (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
1 /*
2  * Copyright (C) 2024 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 <SkBitmap.h>
18 #include <SkBlendMode.h>
19 #include <SkCanvas.h>
20 #include <SkPaint.h>
21 #include <SkRefCnt.h>
22 #include <SkRuntimeEffect.h>
23 #include <SkSurface.h>
24 #include <include/gpu/ganesh/SkSurfaceGanesh.h>
25 #include <math.h>
26 
27 #include "SkImageFilters.h"
28 #include "TestSceneBase.h"
29 #include "include/gpu/GpuTypes.h"  // from Skia
30 #include "tests/common/BitmapAllocationTestUtils.h"
31 #include "utils/Color.h"
32 
33 class WindowBlurSkia;
34 
35 static TestScene::Registrar _WindowBlurSkia(TestScene::Info{
36         "windowblurskia", "Draws window Skia blur", TestScene::simpleCreateScene<WindowBlurSkia>});
37 
38 /**
39  * Simulates the Skia window blur in
40  * frameworks/native/libs/renderengine/skia/filters/GaussianBlurFilter.cpp
41  */
42 class WindowBlurSkia : public TestScene {
43 private:
44     // Keep in sync with frameworks/native/libs/renderengine/skia/filters/BlurFilter.h
45     static constexpr float kInputScale = 0.25f;
46 
47     static constexpr uint32_t kLoopLength = 500;
48     static constexpr uint32_t kMaxBlurRadius = 300;
49 
50     sp<RenderNode> card;
51     sp<RenderNode> contentNode;
52 
53 public:
createContent(int width,int height,Canvas & canvas)54     void createContent(int width, int height, Canvas& canvas) override {
55         contentNode = TestUtils::createNode(
56                 0, 0, width, height, [width, height](RenderProperties& props, Canvas& canvas) {
57                     canvas.drawColor(Color::White, SkBlendMode::kSrcOver);
58                     Paint paint;
59                     paint.setColor(Color::Red_500);
60                     canvas.drawRect(0, 0, width / 2, height / 2, paint);
61                     paint.setColor(Color::Blue_500);
62                     canvas.drawRect(width / 2, height / 2, width, height, paint);
63                 });
64 
65         card = TestUtils::createNode(
66                 0, 0, width, height,
67                 [this](RenderProperties& props, Canvas& canvas) { blurFrame(canvas, 0); });
68         canvas.drawRenderNode(card.get());
69     }
70 
doFrame(int frameNr)71     void doFrame(int frameNr) override {
72         int curFrame = frameNr % kLoopLength;
73         float blurRadius =
74                 (sin((float)curFrame / kLoopLength * M_PI * 2) + 1) * 0.5 * kMaxBlurRadius;
75         TestUtils::recordNode(
76                 *card, [this, blurRadius](Canvas& canvas) { blurFrame(canvas, blurRadius); });
77     }
78 
blurFrame(Canvas & canvas,float blurRadius)79     void blurFrame(Canvas& canvas, float blurRadius) {
80         if (blurRadius == 0) {
81             canvas.drawRenderNode(contentNode.get());
82             return;
83         }
84 
85         int width = canvas.width();
86         int height = canvas.height();
87 
88         // Downsample and blur the image with the Skia blur filter.
89         sp<RenderNode> node = contentNode;
90         sk_sp<SkImageFilter> blurFilter =
91                 SkImageFilters::Blur(blurRadius, blurRadius, SkTileMode::kClamp, nullptr, nullptr);
92         node = TestUtils::createNode(
93                 0, 0, width * kInputScale, height * kInputScale,
94                 [node, blurFilter](RenderProperties& props, Canvas& canvas) {
95                     props.mutateLayerProperties().setImageFilter(blurFilter.get());
96                     canvas.scale(kInputScale, kInputScale);
97                     canvas.drawRenderNode(node.get());
98                 });
99 
100         // Upsample the image to its original size.
101         canvas.scale(1 / kInputScale, 1 / kInputScale);
102         canvas.drawRenderNode(node.get());
103     }
104 };
105