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 "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 Rect frameRect;
40 };
41 }
42
43 class RsImageInfo final {
44 public:
RsImageInfo(int fitNum,int repeatNum,const Drawing::Point * radius,double scale,uint32_t id,int w,int h)45 RsImageInfo(int fitNum, int repeatNum, const Drawing::Point* radius, double scale, uint32_t id, int w, int h)
46 : fitNum_(fitNum), repeatNum_(repeatNum), radius_(radius), scale_(scale),
47 uniqueId_(id), width_(w), height_(h) {};
~RsImageInfo()48 ~RsImageInfo() {}
49 int fitNum_ = 0;
50 int repeatNum_ = 0;
51 const Drawing::Point* radius_;
52 double scale_ = 0.0;
53 uint32_t uniqueId_ = 0;
54 int width_ = 0;
55 int height_ = 0;
56 };
57
58 enum class ImageRepeat {
59 NO_REPEAT = 0,
60 REPEAT_X,
61 REPEAT_Y,
62 REPEAT,
63 };
64
65 enum class ImageFit {
66 FILL,
67 CONTAIN,
68 COVER,
69 FIT_WIDTH,
70 FIT_HEIGHT,
71 NONE,
72 SCALE_DOWN,
73 TOP_LEFT,
74 TOP,
75 TOP_RIGHT,
76 LEFT,
77 CENTER,
78 RIGHT,
79 BOTTOM_LEFT,
80 BOTTOM,
81 BOTTOM_RIGHT,
82 COVER_TOP_LEFT,
83 };
84
85 class RSB_EXPORT RSImage : public RSImageBase {
86 public:
87 RSImage() = default;
88 ~RSImage();
89
90 bool IsEqual(const RSImage& other) const;
91 void CanvasDrawImage(Drawing::Canvas& canvas, const Drawing::Rect& rect,
92 const Drawing::SamplingOptions& samplingOptions, bool isBackground = false);
93 void SetImageFit(int fitNum);
94 void SetImageRepeat(int repeatNum);
95 void SetRadius(const std::vector<Drawing::Point>& radius);
96 void SetScale(double scale);
SetInnerRect(const std::optional<Drawing::RectI> & innerRect)97 void SetInnerRect(const std::optional<Drawing::RectI>& innerRect) { innerRect_ = innerRect;}
98
99 void SetCompressData(const std::shared_ptr<Drawing::Data> data, uint32_t id, int width, int height);
100 void SetCompressData(const std::shared_ptr<Drawing::Data> compressData);
101
102 bool HDRConvert(const Drawing::SamplingOptions& sampling, Drawing::Canvas& canvas);
103 void SetPaint(Drawing::Paint paint);
104 void SetDyamicRangeMode(uint32_t dynamicRangeMode);
105
106 void SetNodeId(NodeId nodeId);
107
108 void ApplyImageFit();
109 ImageFit GetImageFit();
110 Drawing::AdaptiveImageInfo GetAdaptiveImageInfoWithCustomizedFrameRect(const Drawing::Rect& frameRect) const;
111 RectF GetDstRect();
112 void SetFrameRect(RectF frameRect);
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 desc += split + "\tPointF:{ \n";
126 desc += split + "\t\t x_: " + std::to_string(radius_[i].GetX()) + "\n";
127 desc += split + "\t\t y_: " + std::to_string(radius_[i].GetY()) + "\n";
128 desc += split + "\t}\n";
129 }
130 desc += split + frameRect_.ToString();
131 desc += split + "\tscale_: " + std::to_string(scale_) + "\n";
132 desc += split + "}\n";
133 }
134
135 private:
136 bool HasRadius() const;
137 void ApplyCanvasClip(Drawing::Canvas& canvas);
138 void UploadGpu(Drawing::Canvas& canvas);
139 void DrawImageRepeatRect(const Drawing::SamplingOptions& samplingOptions, Drawing::Canvas& canvas);
140 void CalcRepeatBounds(int& minX, int& maxX, int& minY, int& maxY);
141 void DrawImageOnCanvas(
142 const Drawing::SamplingOptions& samplingOptions, Drawing::Canvas& canvas, const bool hdrImageDraw);
143 #ifdef ROSEN_OHOS
144 static bool UnmarshalIdSizeAndNodeId(Parcel& parcel, uint64_t& uniqueId, int& width, int& height, NodeId& nodeId);
145 static bool UnmarshalImageProperties(
146 Parcel& parcel, int& fitNum, int& repeatNum, std::vector<Drawing::Point>& radius, double& scale);
147 static void ProcessImageAfterCreation(RSImage* rsImage, const uint64_t uniqueId, const bool useSkImage,
148 const std::shared_ptr<Media::PixelMap>& pixelMap);
149 #endif
150 std::shared_ptr<Drawing::Data> compressData_;
151 ImageFit imageFit_ = ImageFit::COVER;
152 ImageRepeat imageRepeat_ = ImageRepeat::NO_REPEAT;
153 std::vector<Drawing::Point> radius_ = std::vector<Drawing::Point>(4);
154 std::optional<Drawing::RectI> innerRect_ = std::nullopt;
155 bool hasRadius_ = false;
156 RectF frameRect_;
157 double scale_ = 1.0;
158 NodeId nodeId_ = 0;
159 Drawing::Paint paint_;
160 uint32_t dynamicRangeMode_ = 0;
161 };
162
163 template<>
ROSEN_EQ(const std::shared_ptr<RSImage> & x,const std::shared_ptr<RSImage> & y)164 inline bool ROSEN_EQ(const std::shared_ptr<RSImage>& x, const std::shared_ptr<RSImage>& y)
165 {
166 if (x == y) {
167 return true;
168 }
169 return (x && y) ? x->IsEqual(*y) : false;
170 }
171 } // namespace Rosen
172 } // namespace OHOS
173 #endif // RENDER_SERVICE_CLIENT_CORE_RENDER_RS_IMAGE_H
174