• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 IMAGE_H
17 #define IMAGE_H
18 
19 #include "drawing/engine_adapter/impl_interface/image_impl.h"
20 
21 namespace OHOS {
22 namespace Rosen {
23 namespace Drawing {
24 enum class BitDepth {
25     KU8,
26     KF16,
27 };
28 
29 enum class CompressedType {
30     ETC1,
31     ASTC,
32 };
33 
34 enum class TextureOrigin {
35     TOP_LEFT,
36     BOTTOM_LEFT,
37 };
38 
39 class TextureInfo {
40 public:
41     /*
42      * @brief  Sets the width value of Texture.
43      */
SetWidth(int width)44     void SetWidth(int width)
45     {
46         width_ = width;
47     }
48 
49     /*
50      * @brief  Sets the height value of Texture.
51      */
SetHeight(int height)52     void SetHeight(int height)
53     {
54         height_ = height;
55     }
56 
57     /*
58      * @brief  Used to say whether a texture has mip levels allocated or not.
59      */
SetIsMipMapped(bool isMipMapped)60     void SetIsMipMapped(bool isMipMapped)
61     {
62         isMipMapped_ = isMipMapped;
63     }
64 
65     /*
66      * @brief         Sets the target type of texture to render.
67      * @param target  The target type of texture.
68      */
SetTarget(unsigned int target)69     void SetTarget(unsigned int target)
70     {
71         target_ = target;
72     }
73 
74     /*
75      * @brief     Sets the Id of texture to render.
76      * @param id  Texture Id value.
77      */
SetID(unsigned int id)78     void SetID(unsigned int id)
79     {
80         id_ = id;
81     }
82 
83     /*
84      * @brief         Set the format of texture.
85      * @param format  The format of texture.
86      */
SetFormat(unsigned int format)87     void SetFormat(unsigned int format)
88     {
89         format_ = format;
90     }
91 
92     /*
93      * @brief  Gets the width of TextureInfo.
94      */
GetWidth()95     int GetWidth() const
96     {
97         return width_;
98     }
99 
100     /*
101      * @brief  Gets the height of TextureInfo.
102      */
GetHeight()103     int GetHeight() const
104     {
105         return height_;
106     }
107 
108     /*
109      * @brief  Gets whether the texture has mip levels allocated.
110      */
GetIsMipMapped()111     bool GetIsMipMapped() const
112     {
113         return isMipMapped_;
114     }
115 
116     /*
117      * @brief   Gets the target type of TextureInfo.
118      * @return  The target type of TextureInfo.
119      */
GetTarget()120     unsigned int GetTarget() const
121     {
122         return target_;
123     }
124 
125     /*
126      * @brief   Gets the Id of TextureInfo.
127      * @return  The Id of TextureInfo.
128      */
GetID()129     unsigned int GetID() const
130     {
131         return id_;
132     }
133 
134     /*
135      * @brief   Gets the format of TextureInfo.
136      * @return  The target format of TextureInfo.
137      */
GetFormat()138     unsigned int GetFormat() const
139     {
140         return format_;
141     }
142 
143 private:
144     int width_ = 0;
145     int height_ = 0;
146     bool isMipMapped_ = false;
147     unsigned int target_ = 0;
148     unsigned int id_ = 0;
149     unsigned int format_ = 0;
150 };
151 
152 class Image {
153 public:
154     Image() noexcept;
155     // constructor adopt a raw image ptr, using for ArkUI, should remove after enable multi-media image decode.
156     explicit Image(void* rawImg) noexcept;
~Image()157     virtual ~Image() {};
158     Image* BuildFromBitmap(const Bitmap& bitmap);
159     Image* BuildFromPicture(const Picture& picture, const SizeI& dimensions, const Matrix& matrix, const Brush& brush,
160         BitDepth bitDepth, std::shared_ptr<ColorSpace> colorSpace);
161 
162 #ifdef ACE_ENABLE_GPU
163     /*
164      * @brief             Create Image from Bitmap. Image is uploaded to GPU back-end using context.
165      * @param gpuContext  GPU context.
166      * @param bitmap      BitmapInfo, pixel address and row bytes.
167      * @return            True if Image is created successed.
168      */
169     bool BuildFromBitmap(GPUContext& gpuContext, const Bitmap& bitmap);
170 
171     /*
172      * @brief             Create a GPU-backed Image from compressed data.
173      * @param gpuContext  GPU context.
174      * @param data        Compressed data to store in Image.
175      * @param width       Width of full Image.
176      * @param height      Height of full Image.
177      * @param type        Type of compression used.
178      * @return            True if Image is created successed.
179      */
180     bool BuildFromCompressed(GPUContext& gpuContext, const std::shared_ptr<Data>& data, int width, int height,
181         CompressedType type);
182 
183     /*
184      * @brief               Create Image from GPU texture associated with context.
185      * @param gpuContext    GPU context.
186      * @param info          Texture info.
187      * @param origin        The origin of the texture space corresponds to the context pixel.
188                             One of TextureOrigin::Top_Left, TextureOrigion::Bottom_Left.
189      * @param bitmapFormat  It contains ColorType and AlphaType.
190      * @param colorSpace    Range of colors, may be nullptr.
191      * @return              True if Image is created successed.
192      */
193     bool BuildFromTexture(GPUContext& gpuContext, const TextureInfo& info, TextureOrigin origin,
194         BitmapFormat bitmapFormat, const std::shared_ptr<ColorSpace>& colorSpace);
195 #endif
196 
197     /*
198      * @brief  Gets the width of Image.
199      */
200     int GetWidth() const;
201 
202     /*
203      * @brief  Gets the height of Image.
204      */
205     int GetHeight() const;
206 
207     /*
208      * @brief  Gets the color type of Image.
209      */
210     ColorType GetColorType() const;
211 
212     /*
213      * @brief  Gets the alpha type of Image.
214      */
215     AlphaType GetAlphaType() const;
216 
217     /*
218      * @brief  Gets the unique Id of Image.
219      */
220     uint32_t GetUniqueID() const;
221 
222     /*
223      * @brief         Copies a Rect of pixels from Image to Bitmap.
224      * @param bitmap  Destination Bitmap.
225      * @param x       Column index whose absolute value is less than Image width.
226      * @param y       Row index whose absolute value is less than Image height.
227      * @return        True of pixels are copied to Bitmap.
228      */
229     bool ReadPixels(Bitmap& bitmap, int x, int y);
230 
231     /*
232      * @brief   Returns true the contents of Image was created on or uploaded to GPU memory,
233                 and is available as a GPU texture.
234      * @return  True if Image is a GPU texture.
235      */
236     bool IsTextureBacked() const;
237 
238     template<typename T>
GetImpl()239     const std::shared_ptr<T> GetImpl() const
240     {
241         return imageImplPtr->DowncastingTo<T>();
242     }
243 
244     // using for recording, should to remove after using shared memory
245     std::shared_ptr<Data> Serialize() const;
246     bool Deserialize(std::shared_ptr<Data> data);
247 
248 private:
249     std::shared_ptr<ImageImpl> imageImplPtr;
250 };
251 } // namespace Drawing
252 } // namespace Rosen
253 } // namespace OHOS
254 #endif
255