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_BASE_RENDER_RENDER_RS_IMAGE_H
17 #define RENDER_SERVICE_BASE_RENDER_RENDER_RS_IMAGE_H
18
19 #include "draw/canvas.h"
20 #include "effect/color_filter.h"
21 #include "image/image.h"
22 #include "render/rs_image_base.h"
23
24 namespace OHOS {
25 namespace Media {
26 class PixelMap;
27 }
28 namespace Rosen {
29 namespace Drawing {
30 struct AdaptiveImageInfo {
31 int32_t fitNum = 0;
32 int32_t repeatNum = 0;
33 Point radius[4];
34 double scale = 0.0;
35 uint32_t uniqueId = 0;
36 int32_t width = 0;
37 int32_t height = 0;
38 uint32_t dynamicRangeMode = 0;
39 int32_t rotateDegree = 0;
40 Rect frameRect = Rect();
41 Drawing::Matrix fitMatrix = Drawing::Matrix();
42 int32_t orientationNum = 0;
43 };
44 }
45
46 class RsImageInfo final {
47 public:
RsImageInfo(int fitNum,int repeatNum,const Drawing::Point * radius,double scale,uint32_t id,int w,int h)48 RsImageInfo(int fitNum, int repeatNum, const Drawing::Point* radius, double scale, uint32_t id, int w, int h)
49 : fitNum_(fitNum), repeatNum_(repeatNum), radius_(radius), scale_(scale),
50 uniqueId_(id), width_(w), height_(h) {};
~RsImageInfo()51 ~RsImageInfo() {}
52 int fitNum_ = 0;
53 int repeatNum_ = 0;
54 const Drawing::Point* radius_;
55 double scale_ = 0.0;
56 uint32_t uniqueId_ = 0;
57 int width_ = 0;
58 int height_ = 0;
59 };
60
61 enum class ImageRepeat {
62 NO_REPEAT = 0,
63 REPEAT_X,
64 REPEAT_Y,
65 REPEAT,
66 };
67
68 enum class ImageFit {
69 FILL,
70 CONTAIN,
71 COVER,
72 FIT_WIDTH,
73 FIT_HEIGHT,
74 NONE,
75 SCALE_DOWN,
76 TOP_LEFT,
77 TOP,
78 TOP_RIGHT,
79 LEFT,
80 CENTER,
81 RIGHT,
82 BOTTOM_LEFT,
83 BOTTOM,
84 BOTTOM_RIGHT,
85 COVER_TOP_LEFT,
86 MATRIX,
87 };
88
89 enum class OrientationFit : int {
90 NONE,
91 VERTICAL_FLIP,
92 HORIZONTAL_FLIP,
93 };
94
95 class RSB_EXPORT RSImage : public RSImageBase {
96 public:
97 RSImage() = default;
98 ~RSImage();
99
100 using RSImageBase::SetCompressData;
101
102 bool IsEqual(const RSImage& other) const;
103 void CanvasDrawImage(Drawing::Canvas& canvas, const Drawing::Rect& rect,
104 const Drawing::SamplingOptions& samplingOptions, bool isBackground = false);
105 void SetImageFit(int fitNum);
106 void SetImageRepeat(int repeatNum);
107 void SetImageRotateDegree(int32_t degree);
108 void SetRadius(const std::vector<Drawing::Point>& radius);
109 void SetScale(double scale);
SetInnerRect(const std::optional<Drawing::RectI> & innerRect)110 void SetInnerRect(const std::optional<Drawing::RectI>& innerRect) { innerRect_ = innerRect;}
111
112 void SetCompressData(const std::shared_ptr<Drawing::Data> data, uint32_t id, int width, int height);
113
114 bool HDRConvert(const Drawing::SamplingOptions& sampling, Drawing::Canvas& canvas);
115 void SetPaint(Drawing::Paint paint);
116 void SetDynamicRangeMode(uint32_t dynamicRangeMode);
117
118 void SetNodeId(NodeId nodeId);
119
120 void ApplyImageFit();
121 ImageFit GetImageFit();
122 Drawing::AdaptiveImageInfo GetAdaptiveImageInfoWithCustomizedFrameRect(const Drawing::Rect& frameRect) const;
123 RectF GetDstRect();
124 void SetFrameRect(RectF frameRect);
125 void SetFitMatrix(const Drawing::Matrix& matrix);
126 Drawing::Matrix GetFitMatrix() const;
127 std::shared_ptr<Drawing::Image> GetImage() const;
128 void SetOrientationFit(int orientationFitNum);
129 OrientationFit GetOrientationFit() const;
130 #ifdef ROSEN_OHOS
131 bool Marshalling(Parcel& parcel) const override;
132 [[nodiscard]] static RSImage* Unmarshalling(Parcel& parcel);
133 #endif
134 std::string PixelSamplingDump() const;
Dump(std::string & desc,uint8_t depth)135 void Dump(std::string &desc, uint8_t depth) const
136 {
137 std::string split(depth, '\t');
138 desc += split + "RSImage:{ ";
139 desc += split + "\timageFit_: " + std::to_string(static_cast<int>(imageFit_)) + " \n";
140 desc += split + "\timageRepeat_: " + std::to_string(static_cast<int>(imageRepeat_)) + " \n";
141 desc += split + "\torientationFit_: " + std::to_string(static_cast<int>(orientationFit_)) + " \n";
142
143 int radiusSize = 4;
144 for (int i = 0; i < radiusSize; i++) {
145 desc += split + "\tPointF:{ \n";
146 desc += split + "\t\t x_: " + std::to_string(radius_[i].GetX()) + " \n";
147 desc += split + "\t\t y_: " + std::to_string(radius_[i].GetY()) + " \n";
148 desc += split + "\t} \n";
149 }
150 desc += split + frameRect_.ToString();
151 desc += split + "\tscale_: " + std::to_string(scale_) + " \n";
152 desc += split + "\tsrc_: " + src_.ToString() + " \n";
153 desc += split + "\tdst_: " + dst_.ToString() + " \n";
154 desc += split + "\tpixel sampling: " + PixelSamplingDump() + " \n";
155 desc += split + "} \n";
156 }
157
158 private:
159 bool HasRadius() const;
160 void ApplyCanvasClip(Drawing::Canvas& canvas);
161 std::pair<float, float> CalculateByDegree(const Drawing::Rect& rect);
162 void DrawImageRect(
163 Drawing::Canvas& canvas, const Drawing::Rect& rect, const Drawing::SamplingOptions& samplingOptions);
164 void DrawImageRepeatRect(const Drawing::SamplingOptions& samplingOptions, Drawing::Canvas& canvas);
165 void CalcRepeatBounds(int& minX, int& maxX, int& minY, int& maxY);
166 void DrawImageOnCanvas(
167 const Drawing::SamplingOptions& samplingOptions, Drawing::Canvas& canvas, const bool hdrImageDraw);
168 bool CanDrawRectWithImageShader(const Drawing::Canvas& canvas) const;
169 std::shared_ptr<Drawing::ShaderEffect> GenerateImageShaderForDrawRect(
170 const Drawing::Canvas& canvas, const Drawing::SamplingOptions& sampling) const;
171 void DrawImageShaderRectOnCanvas(
172 Drawing::Canvas& canvas, const std::shared_ptr<Drawing::ShaderEffect>& imageShader) const;
173 void DrawImageWithFirMatrixRotateOnCanvas(
174 const Drawing::SamplingOptions& samplingOptions, Drawing::Canvas& canvas) const;
175 void ApplyImageOrientation(Drawing::Canvas& canvas);
176 #ifdef ROSEN_OHOS
177 static bool UnmarshalIdSizeAndNodeId(Parcel& parcel, uint64_t& uniqueId, int& width, int& height, NodeId& nodeId);
178 static bool UnmarshalImageProperties(
179 Parcel& parcel, int& fitNum, int& repeatNum, std::vector<Drawing::Point>& radius, double& scale,
180 bool& hasFitMatrix, Drawing::Matrix& fitMatrix, uint32_t& dynamicRangeMode, int32_t& degree,
181 int& orientationFitNum);
182 static void ProcessImageAfterCreation(RSImage* rsImage, const uint64_t uniqueId, const bool useSkImage,
183 const std::shared_ptr<Media::PixelMap>& pixelMap);
184 #endif
185 ImageFit imageFit_ = ImageFit::COVER;
186 ImageRepeat imageRepeat_ = ImageRepeat::NO_REPEAT;
187 std::vector<Drawing::Point> radius_ = std::vector<Drawing::Point>(4);
188 std::optional<Drawing::RectI> innerRect_ = std::nullopt;
189 bool hasRadius_ = false;
190 RectF frameRect_;
191 double scale_ = 1.0;
192 NodeId nodeId_ = 0;
193 int32_t rotateDegree_;
194 Drawing::Paint paint_;
195 uint32_t dynamicRangeMode_ = 0;
196 std::optional<Drawing::Matrix> fitMatrix_ = std::nullopt;
197 bool isFitMatrixValid_ = false;
198 OrientationFit orientationFit_ = OrientationFit::NONE;
199 bool isOrientationValid_ = false;
200 };
201
202 template<>
ROSEN_EQ(const std::shared_ptr<RSImage> & x,const std::shared_ptr<RSImage> & y)203 inline bool ROSEN_EQ(const std::shared_ptr<RSImage>& x, const std::shared_ptr<RSImage>& y)
204 {
205 if (x == y) {
206 return true;
207 }
208 return (x && y) ? x->IsEqual(*y) : false;
209 }
210 } // namespace Rosen
211 } // namespace OHOS
212 #endif // RENDER_SERVICE_BASE_RENDER_RENDER_RS_IMAGE_H
213