1 /*
2  * Copyright 2021 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 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
18 
19 #include "GaussianBlurFilter.h"
20 #include <SkCanvas.h>
21 #include <SkPaint.h>
22 #include <SkRRect.h>
23 #include <SkRuntimeEffect.h>
24 #include <SkImageFilters.h>
25 #include <SkSize.h>
26 #include <SkString.h>
27 #include <SkSurface.h>
28 #include "include/gpu/GpuTypes.h" // from Skia
29 #include <log/log.h>
30 #include <utils/Trace.h>
31 
32 namespace android {
33 namespace renderengine {
34 namespace skia {
35 
36 // This constant approximates the scaling done in the software path's
37 // "high quality" mode, in SkBlurMask::Blur() (1 / sqrt(3)).
38 static const float BLUR_SIGMA_SCALE = 0.57735f;
39 
GaussianBlurFilter()40 GaussianBlurFilter::GaussianBlurFilter(): BlurFilter(/* maxCrossFadeRadius= */ 0.0f) {}
41 
generate(GrRecordingContext * context,const uint32_t blurRadius,const sk_sp<SkImage> input,const SkRect & blurRect) const42 sk_sp<SkImage> GaussianBlurFilter::generate(GrRecordingContext* context, const uint32_t blurRadius,
43                                             const sk_sp<SkImage> input, const SkRect& blurRect)
44     const {
45     // Create blur surface with the bit depth and colorspace of the original surface
46     SkImageInfo scaledInfo = input->imageInfo().makeWH(std::ceil(blurRect.width() * kInputScale),
47                                                        std::ceil(blurRect.height() * kInputScale));
48     sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(context,
49                                                            skgpu::Budgeted::kNo, scaledInfo);
50 
51     SkPaint paint;
52     paint.setBlendMode(SkBlendMode::kSrc);
53     paint.setImageFilter(SkImageFilters::Blur(
54                 blurRadius * kInputScale * BLUR_SIGMA_SCALE,
55                 blurRadius * kInputScale * BLUR_SIGMA_SCALE,
56                 SkTileMode::kClamp, nullptr));
57 
58     surface->getCanvas()->drawImageRect(
59             input,
60             blurRect,
61             SkRect::MakeWH(scaledInfo.width(), scaledInfo.height()),
62             SkSamplingOptions{SkFilterMode::kLinear, SkMipmapMode::kNone},
63             &paint,
64             SkCanvas::SrcRectConstraint::kFast_SrcRectConstraint);
65     return surface->makeImageSnapshot();
66 }
67 
68 } // namespace skia
69 } // namespace renderengine
70 } // namespace android
71