• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "render/rs_skia_filter.h"
17 #ifdef USE_ROSEN_DRAWING
18 #include <memory>
19 #include "draw/blend_mode.h"
20 #endif
21 
22 namespace OHOS {
23 namespace Rosen {
24 #ifndef USE_ROSEN_DRAWING
RSSkiaFilter(sk_sp<SkImageFilter> imageFilter)25 RSSkiaFilter::RSSkiaFilter(sk_sp<SkImageFilter> imageFilter) : RSFilter(), imageFilter_(imageFilter) {}
26 
~RSSkiaFilter()27 RSSkiaFilter::~RSSkiaFilter() {}
28 #else
29 RSDrawingFilter::RSDrawingFilter(std::shared_ptr<Drawing::ImageFilter> imageFilter)
30     : RSFilter(), imageFilter_(imageFilter) {}
31 
32 RSDrawingFilter::~RSDrawingFilter() {}
33 #endif
34 
35 #ifndef USE_ROSEN_DRAWING
GetPaint() const36 SkPaint RSSkiaFilter::GetPaint() const
37 {
38     SkPaint paint;
39     paint.setAntiAlias(true);
40     paint.setImageFilter(imageFilter_);
41     return paint;
42 }
43 #else
GetBrush() const44 Drawing::Brush RSDrawingFilter::GetBrush() const
45 {
46     Drawing::Brush brush;
47     brush.SetAntiAlias(true);
48     brush.SetBlendMode(Drawing::BlendMode::SRC_OVER);
49     Drawing::Filter filter;
50     filter.SetImageFilter(imageFilter_);
51     brush.SetFilter(filter);
52     return brush;
53 }
54 #endif
55 
56 #ifndef USE_ROSEN_DRAWING
GetImageFilter() const57 sk_sp<SkImageFilter> RSSkiaFilter::GetImageFilter() const
58 #else
59 std::shared_ptr<Drawing::ImageFilter> RSDrawingFilter::GetImageFilter() const
60 #endif
61 {
62     return imageFilter_;
63 }
64 
65 #ifndef USE_ROSEN_DRAWING
DrawImageRect(SkCanvas & canvas,const sk_sp<SkImage> & image,const SkRect & src,const SkRect & dst) const66 void RSSkiaFilter::DrawImageRect(
67     SkCanvas& canvas, const sk_sp<SkImage>& image, const SkRect& src, const SkRect& dst) const
68 #else
69 void RSDrawingFilter::DrawImageRect(Drawing::Canvas& canvas, const std::shared_ptr<Drawing::Image>& image,
70     const Drawing::Rect& src, const Drawing::Rect& dst) const
71 #endif
72 {
73 #ifndef USE_ROSEN_DRAWING
74     auto paint = GetPaint();
75 #ifdef NEW_SKIA
76     canvas.drawImageRect(image.get(), src, dst, SkSamplingOptions(), &paint, SkCanvas::kStrict_SrcRectConstraint);
77 #else
78     canvas.drawImageRect(image.get(), src, dst, &paint);
79 #endif
80 #else
81     auto brush = GetBrush();
82     canvas.AttachBrush(brush);
83     canvas.DrawImageRect(*image, src, dst, Drawing::SamplingOptions());
84     canvas.DetachBrush();
85 #endif
86 }
87 } // namespace Rosen
88 } // namespace OHOS
89