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 <SkData.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 <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, SkBudgeted::kNo, scaledInfo);
49
50 SkPaint paint;
51 paint.setBlendMode(SkBlendMode::kSrc);
52 paint.setImageFilter(SkImageFilters::Blur(
53 blurRadius * kInputScale * BLUR_SIGMA_SCALE,
54 blurRadius * kInputScale * BLUR_SIGMA_SCALE,
55 SkTileMode::kClamp, nullptr));
56
57 surface->getCanvas()->drawImageRect(
58 input,
59 blurRect,
60 SkRect::MakeWH(scaledInfo.width(), scaledInfo.height()),
61 SkSamplingOptions{SkFilterMode::kLinear, SkMipmapMode::kNone},
62 &paint,
63 SkCanvas::SrcRectConstraint::kFast_SrcRectConstraint);
64 return surface->makeImageSnapshot();
65 }
66
67 } // namespace skia
68 } // namespace renderengine
69 } // namespace android
70