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 void CanvasDrawImage(RSPaintFilterCanvas& canvas, const SkRect& rect, const SkSamplingOptions& samplingOptions,
85 const SkPaint& paint, bool isBackground = false);
86 #else
87 void CanvasDrawImage(Drawing::Canvas& canvas, const Drawing::Rect& rect,
88 const Drawing::SamplingOptions& samplingOptions, bool isBackground = false);
89 #endif
90 void SetImageFit(int fitNum);
91 void SetImageRepeat(int repeatNum);
92 #ifndef USE_ROSEN_DRAWING
93 void SetRadius(const SkVector radius[]);
94 #else
95 void SetRadius(const std::vector<Drawing::Point>& radius);
96 #endif
97 void SetScale(double scale);
98
99 #ifndef USE_ROSEN_DRAWING
100 void SetCompressData(const sk_sp<SkData> data, uint32_t id, int width, int height);
101 #else
102 void SetCompressData(const std::shared_ptr<Drawing::Data> data, uint32_t id, int width, int height);
103 #endif
104 #ifndef USE_ROSEN_DRAWING
105 #if defined(ROSEN_OHOS) && (defined(RS_ENABLE_GL) || defined (RS_ENABLE_VK))
106 void SetCompressData(const sk_sp<SkData> compressData);
107 #endif
108 #else
109 void SetCompressData(const std::shared_ptr<Drawing::Data> compressData);
110 #endif
111
112 void SetNodeId(NodeId nodeId);
113 #ifdef ROSEN_OHOS
114 bool Marshalling(Parcel& parcel) const override;
115 [[nodiscard]] static RSImage* Unmarshalling(Parcel& parcel);
116 #endif
dump(std::string & desc,int depth)117 void dump(std::string &desc, int depth) const
118 {
119 std::string split(depth, '\t');
120 desc += split + "RSImage:{";
121 desc += split + "\timageFit_: " + std::to_string(static_cast<int>(imageFit_)) + "\n";
122 desc += split + "\timageRepeat_: " + std::to_string(static_cast<int>(imageRepeat_)) + "\n";
123 int radiusSize = 4;
124 for (int i = 0; i < radiusSize; i++) {
125 #ifndef USE_ROSEN_DRAWING
126 radius_[i].dump(desc, depth + 1);
127 #else
128 desc += split + "\tPointF:{ \n";
129 desc += split + "\t\t x_: " + std::to_string(radius_[i].GetX()) + "\n";
130 desc += split + "\t\t y_: " + std::to_string(radius_[i].GetY()) + "\n";
131 desc += split + "\t}\n";
132 #endif
133 }
134 desc += split + frameRect_.ToString();
135 desc += split + "\tscale_: " + std::to_string(scale_) + "\n";
136 desc += split + "}\n";
137 }
138
139 private:
140 bool HasRadius() const;
141 void ApplyImageFit();
142 #ifndef USE_ROSEN_DRAWING
143 void ApplyCanvasClip(RSPaintFilterCanvas& canvas);
144 void UploadGpu(RSPaintFilterCanvas& canvas);
145 void DrawImageRepeatRect(const SkSamplingOptions& samplingOptions, const SkPaint& paint,
146 RSPaintFilterCanvas& canvas);
147 #else
148 void ApplyCanvasClip(Drawing::Canvas& canvas);
149 void UploadGpu(Drawing::Canvas& canvas);
150 void DrawImageRepeatRect(const Drawing::SamplingOptions& samplingOptions, Drawing::Canvas& canvas);
151 #endif
152
153 #ifndef USE_ROSEN_DRAWING
154 sk_sp<SkData> compressData_;
155 #else
156 std::shared_ptr<Drawing::Data> compressData_;
157 #endif
158 ImageFit imageFit_ = ImageFit::COVER;
159 ImageRepeat imageRepeat_ = ImageRepeat::NO_REPEAT;
160 #ifndef USE_ROSEN_DRAWING
161 SkVector radius_[4];
162 #else
163 std::vector<Drawing::Point> radius_ = std::vector<Drawing::Point>(4);
164 #endif
165 bool hasRadius_ = false;
166 RectF frameRect_;
167 double scale_ = 1.0;
168 NodeId nodeId_ = 0;
169 };
170
171 template<>
ROSEN_EQ(const std::shared_ptr<RSImage> & x,const std::shared_ptr<RSImage> & y)172 inline bool ROSEN_EQ(const std::shared_ptr<RSImage>& x, const std::shared_ptr<RSImage>& y)
173 {
174 if (x == y) {
175 return true;
176 }
177 return (x && y) ? x->IsEqual(*y) : false;
178 }
179 } // namespace Rosen
180 } // namespace OHOS
181 #endif // RENDER_SERVICE_CLIENT_CORE_RENDER_RS_IMAGE_H
182