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 #ifndef USE_ROSEN_DRAWING
20 #include "include/core/SkColorFilter.h"
21 #include "include/core/SkImage.h"
22 #else
23 #include "draw/canvas.h"
24 #include "effect/color_filter.h"
25 #include "image/image.h"
26 #endif
27 #include "render/rs_image_base.h"
28
29 namespace OHOS {
30 namespace Media {
31 class PixelMap;
32 }
33 namespace Rosen {
34 class RsImageInfo final {
35 public:
36 #ifndef USE_ROSEN_DRAWING
RsImageInfo(int fitNum,int repeatNum,const SkVector * radius,double scale,uint32_t id,int w,int h)37 RsImageInfo(int fitNum, int repeatNum, const SkVector* radius, double scale, uint32_t id, int w, int h)
38 : fitNum_(fitNum), repeatNum_(repeatNum), radius_(radius), scale_(scale),
39 uniqueId_(id), width_(w), height_(h) {};
40 #else
41 RsImageInfo(int fitNum, int repeatNum, const Drawing::Point* radius, double scale, uint32_t id, int w, int h)
42 : fitNum_(fitNum), repeatNum_(repeatNum), radius_(radius), scale_(scale),
43 uniqueId_(id), width_(w), height_(h) {};
44 #endif
~RsImageInfo()45 ~RsImageInfo() {}
46 int fitNum_ = 0;
47 int repeatNum_ = 0;
48 #ifndef USE_ROSEN_DRAWING
49 const SkVector* radius_;
50 #else
51 const Drawing::Point* radius_;
52 #endif
53 double scale_ = 0.0;
54 uint32_t uniqueId_ = 0;
55 int width_ = 0;
56 int height_ = 0;
57 };
58
59 enum class ImageRepeat {
60 NO_REPEAT = 0,
61 REPEAT_X,
62 REPEAT_Y,
63 REPEAT,
64 };
65
66 enum class ImageFit {
67 FILL,
68 CONTAIN,
69 COVER,
70 FIT_WIDTH,
71 FIT_HEIGHT,
72 NONE,
73 SCALE_DOWN,
74 TOP_LEFT,
75 };
76
77 class RSB_EXPORT RSImage : public RSImageBase {
78 public:
79 RSImage() = default;
80 ~RSImage();
81
82 bool IsEqual(const RSImage& other) const;
83 #ifndef USE_ROSEN_DRAWING
84 #ifdef NEW_SKIA
85 void CanvasDrawImage(SkCanvas& canvas, const SkRect& rect, const SkSamplingOptions& samplingOptions,
86 const SkPaint& paint, bool isBackground = false);
87 #else
88 void CanvasDrawImage(SkCanvas& canvas, const SkRect& rect, const SkPaint& paint, bool isBackground = false);
89 #endif
90 #else
91 void CanvasDrawImage(
92 Drawing::Canvas& canvas, const Drawing::Rect& rect, bool isBackground = false);
93 #endif
94 void SetImageFit(int fitNum);
95 void SetImageRepeat(int repeatNum);
96 #ifndef USE_ROSEN_DRAWING
97 void SetRadius(const SkVector radius[]);
98 #else
99 void SetRadius(const std::vector<Drawing::Point>& radius);
100 #endif
101 void SetScale(double scale);
102 #ifndef USE_ROSEN_DRAWING
103 void SetCompressData(const sk_sp<SkData> data, uint32_t id, int width, int height);
104 #else
105 void SetCompressData(const std::shared_ptr<Drawing::Data> data, uint32_t id, int width, int height);
106 #endif
107 void SetNodeId(NodeId nodeId);
108 #ifdef ROSEN_OHOS
109 bool Marshalling(Parcel& parcel) const override;
110 [[nodiscard]] static RSImage* Unmarshalling(Parcel& parcel);
111 #endif
dump(std::string & desc,int depth)112 void dump(std::string &desc, int depth) const
113 {
114 std::string split(depth, '\t');
115 desc += split + "RSImage:{";
116 desc += split + "\timageFit_: " + std::to_string(static_cast<int>(imageFit_)) + "\n";
117 desc += split + "\timageRepeat_: " + std::to_string(static_cast<int>(imageRepeat_)) + "\n";
118 int radiusSize = 4;
119 for (int i = 0; i < radiusSize; i++) {
120 #ifndef USE_ROSEN_DRAWING
121 radius_[i].dump(desc, depth + 1);
122 #else
123 desc += split + "\tPointF:{ \n";
124 desc += split + "\t\t x_: " + std::to_string(radius_[i].GetX()) + "\n";
125 desc += split + "\t\t y_: " + std::to_string(radius_[i].GetY()) + "\n";
126 desc += split + "\t}\n";
127 #endif
128 }
129 desc += split + frameRect_.ToString();
130 desc += split + "\tscale_: " + std::to_string(scale_) + "\n";
131 desc += split + "}\n";
132 }
133 private:
134 bool HasRadius() const;
135 void ApplyImageFit();
136 #ifndef USE_ROSEN_DRAWING
137 void ApplyCanvasClip(SkCanvas& canvas);
138 void UploadGpu(SkCanvas& canvas);
139 #ifdef NEW_SKIA
140 void DrawImageRepeatRect(const SkSamplingOptions& samplingOptions, const SkPaint& paint, SkCanvas& canvas);
141 #else
142 void DrawImageRepeatRect(const SkPaint& paint, SkCanvas& canvas);
143 #endif
144 #else
145 void ApplyCanvasClip(Drawing::Canvas& canvas);
146 void UploadGpu(Drawing::Canvas& canvas);
147 void DrawImageRepeatRect(Drawing::Canvas& canvas);
148 #endif
149
150 #ifndef USE_ROSEN_DRAWING
151 sk_sp<SkData> compressData_;
152 #else
153 std::shared_ptr<Drawing::Data> compressData_;
154 #endif
155 ImageFit imageFit_ = ImageFit::COVER;
156 ImageRepeat imageRepeat_ = ImageRepeat::NO_REPEAT;
157 #ifndef USE_ROSEN_DRAWING
158 SkVector radius_[4];
159 #else
160 std::vector<Drawing::Point> radius_ = std::vector<Drawing::Point>(4);
161 #endif
162 bool hasRadius_ = false;
163 RectF frameRect_;
164 double scale_ = 1.0;
165 NodeId nodeId_ = 0;
166 };
167
168 template<>
ROSEN_EQ(const std::shared_ptr<RSImage> & x,const std::shared_ptr<RSImage> & y)169 inline bool ROSEN_EQ(const std::shared_ptr<RSImage>& x, const std::shared_ptr<RSImage>& y)
170 {
171 if (x == y) {
172 return true;
173 }
174 return (x && y) ? x->IsEqual(*y) : false;
175 }
176 } // namespace Rosen
177 } // namespace OHOS
178 #endif // RENDER_SERVICE_CLIENT_CORE_RENDER_RS_IMAGE_H
179