1 /*
2 * Copyright (c) 2021-2023 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 #ifndef RENDER_SERVICE_CLIENT_CORE_RENDER_RS_IMAGE_H
17 #define RENDER_SERVICE_CLIENT_CORE_RENDER_RS_IMAGE_H
18
19 #include <cstdint>
20 #include <mutex>
21 #include "common/rs_macros.h"
22 #include "common/rs_rect.h"
23 #include "include/core/SkCanvas.h"
24 #include "include/core/SkColorFilter.h"
25 #include "include/core/SkImage.h"
26 #include "transaction/rs_marshalling_helper.h"
27
28 namespace OHOS {
29 namespace Media {
30 class PixelMap;
31 }
32 namespace Rosen {
33 class RsImageInfo final {
34 public:
RsImageInfo(int fitNum,int repeatNum,const SkVector * radius,double scale,uint32_t id,int w,int h)35 RsImageInfo(int fitNum, int repeatNum, const SkVector* radius, double scale, uint32_t id, int w, int h)
36 : fitNum_(fitNum), repeatNum_(repeatNum), radius_(radius), scale_(scale),
37 uniqueId_(id), width_(w), height_(h) {};
~RsImageInfo()38 ~RsImageInfo() {}
39 int fitNum_ = 0;
40 int repeatNum_ = 0;
41 const SkVector* radius_;
42 double scale_ = 0.0;
43 uint32_t uniqueId_ = 0;
44 int width_ = 0;
45 int height_ = 0;
46 };
47
48 enum class ImageRepeat {
49 NO_REPEAT = 0,
50 REPEAT_X,
51 REPEAT_Y,
52 REPEAT,
53 };
54
55 enum class ImageFit {
56 FILL,
57 CONTAIN,
58 COVER,
59 FIT_WIDTH,
60 FIT_HEIGHT,
61 NONE,
62 SCALE_DOWN,
63 TOP_LEFT,
64 };
65
66 class RSB_EXPORT RSImage {
67 public:
68 RSImage() = default;
69 ~RSImage();
70
71 bool IsEqual(const RSImage& other) const;
72 void CanvasDrawImage(SkCanvas& canvas, const SkRect& rect, const SkPaint& paint, bool isBackground = false);
73 void SetImage(const sk_sp<SkImage> image);
74 void SetPixelMap(const std::shared_ptr<Media::PixelMap>& pixelmap);
75 void SetDstRect(const RectF& dstRect);
76 void SetImageFit(int fitNum);
77 void SetImageRepeat(int repeatNum);
78 void SetRadius(const SkVector radius[]);
79 void SetScale(double scale);
80 void SetCompressData(const sk_sp<SkData> data, uint32_t id, int width, int height);
81 #ifdef ROSEN_OHOS
82 bool Marshalling(Parcel& parcel) const;
83 static RSImage* Unmarshalling(Parcel& parcel);
84 #endif
85
86 private:
87 void ApplyImageFit();
88 void ApplyCanvasClip(SkCanvas& canvas);
89 void DrawImageRepeatRect(const SkPaint& paint, SkCanvas& canvas);
90 void UploadGpu(SkCanvas& canvas);
91
92 mutable std::mutex mutex_;
93 sk_sp<SkImage> image_;
94 sk_sp<SkData> compressData_;
95 std::shared_ptr<Media::PixelMap> pixelmap_;
96
97 ImageFit imageFit_ = ImageFit::COVER;
98 ImageRepeat imageRepeat_ = ImageRepeat::NO_REPEAT;
99 SkVector radius_[4];
100 RectF srcRect_;
101 RectF dstRect_;
102 RectF frameRect_;
103 double scale_ = 1.0;
104 uint64_t uniqueId_;
105 };
106
107 template<>
ROSEN_EQ(const std::shared_ptr<RSImage> & x,const std::shared_ptr<RSImage> & y)108 inline bool ROSEN_EQ(const std::shared_ptr<RSImage>& x, const std::shared_ptr<RSImage>& y)
109 {
110 if (x == y) {
111 return true;
112 }
113 return (x && y) ? x->IsEqual(*y) : false;
114 }
115 } // namespace Rosen
116 } // namespace OHOS
117 #endif // RENDER_SERVICE_CLIENT_CORE_RENDER_RS_IMAGE_H
118