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