1 /*
2  * Copyright (c) 2024 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 API_CORE_IMAGE_IIMAGE_CONTAINER_H
17 #define API_CORE_IMAGE_IIMAGE_CONTAINER_H
18 
19 #include <cstdint>
20 
21 #include <base/containers/array_view.h>
22 #include <base/containers/unique_ptr.h>
23 #include <base/namespace.h>
24 #include <base/util/formats.h>
25 #include <core/namespace.h>
26 
CORE_BEGIN_NAMESPACE()27 CORE_BEGIN_NAMESPACE()
28 /** \addtogroup group_image_iimagecontainer
29  *  @{
30  */
31 
32 /**
33  * An interface that is used to represent a collection of bitmap image data.
34  *
35  * The container can be:
36  *    - a single 1D/2D/3D bitmap image (e.g. loaded from a png file)
37  *    - an array of 1D/2D images
38  *    - a cube map
39  *    - an array of cube maps
40  *
41  * All of the above possibly containing mipmaps.
42  */
43 class IImageContainer {
44 public:
45     /** Image flags */
46     enum ImageFlags : uint32_t {
47         /** Bit for cubemap */
48         FLAGS_CUBEMAP_BIT = 0x00000001,
49         /** Bit for packed */
50         FLAGS_PACKED_BIT = 0x00000002,
51         /** Bit for compressed */
52         FLAGS_COMPRESSED_BIT = 0x00000004,
53         /** Bit for request mipmaps */
54         FLAGS_REQUESTING_MIPMAPS_BIT = 0x00000008,
55         /** Image color data has been premultiplied with the alpha value */
56         FLAGS_PREMULTIPLIED_ALPHA_BIT = 0x00000010,
57         /** Image is animated */
58         FLAGS_ANIMATED_BIT = 0x00000020,
59     };
60 
61     /** Image type */
62     enum ImageType : uint32_t {
63         /** 1D */
64         TYPE_1D = 0,
65         /** 2D */
66         TYPE_2D = 1,
67         /** 3D */
68         TYPE_3D = 2,
69         /** Max enumeration */
70         TYPE_MAX_ENUM = 0x7FFFFFFF
71     };
72 
73     /** Image view type */
74     enum ImageViewType : uint32_t {
75         /** 1D */
76         VIEW_TYPE_1D = 0,
77         /** 2D */
78         VIEW_TYPE_2D = 1,
79         /** 3D */
80         VIEW_TYPE_3D = 2,
81         /** Cube */
82         VIEW_TYPE_CUBE = 3,
83         /** 1D array */
84         VIEW_TYPE_1D_ARRAY = 4,
85         /** 2D array */
86         VIEW_TYPE_2D_ARRAY = 5,
87         /** Cube array */
88         VIEW_TYPE_CUBE_ARRAY = 6,
89         /** Max enumeration */
90         VIEW_TYPE_MAX_ENUM = 0x7FFFFFFF
91     };
92 
93     /** Image descriptor */
94     struct ImageDesc {
95         /** Flags for image descriptor */
96         uint32_t imageFlags { 0 };
97 
98         /** Pixel width of block */
99         uint32_t blockPixelWidth { 0 };
100         /** Pixel height of block */
101         uint32_t blockPixelHeight { 0 };
102         /** Pixel depth of block */
103         uint32_t blockPixelDepth { 0 };
104         /** Bits per block */
105         uint32_t bitsPerBlock { 0 };
106 
107         /** Image type */
108         ImageType imageType { ImageType::TYPE_MAX_ENUM };
109         /** Image view type */
110         ImageViewType imageViewType { ImageViewType::VIEW_TYPE_MAX_ENUM };
111         /** Format */
112         BASE_NS::Format format { BASE_NS::Format::BASE_FORMAT_UNDEFINED };
113 
114         /** Width */
115         uint32_t width { 0 };
116         /** Height */
117         uint32_t height { 0 };
118         /** Depth */
119         uint32_t depth { 0 };
120 
121         /** Mip count */
122         uint32_t mipCount { 1 };
123         /** Layer count */
124         uint32_t layerCount { 1 };
125     };
126 
127     /** Descriptor for each subimage (mip level, cube face etc.) */
128     struct SubImageDesc {
129         /** Buffer offset */
130         uint32_t bufferOffset { 0 };
131         /** Buffer row length */
132         uint32_t bufferRowLength { 0 };
133         /** Buffer image height */
134         uint32_t bufferImageHeight { 0 };
135         /** Mip level */
136         uint32_t mipLevel { 0 };
137         /** Layer count */
138         uint32_t layerCount { 0 };
139         /** Width */
140         uint32_t width { 0 };
141         /** Height */
142         uint32_t height { 0 };
143         /** Depth */
144         uint32_t depth { 0 };
145     };
146 
147     /** Preventing copy construction and assign. */
148     IImageContainer(IImageContainer const&) = delete;
149     IImageContainer& operator=(IImageContainer const&) = delete;
150 
151     /** Return descriptor containing information about this image. */
152     virtual const ImageDesc& GetImageDesc() const = 0;
153 
154     /** Return The data buffer that holds the data for all of the image elements of this container. */
155     virtual BASE_NS::array_view<const uint8_t> GetData() const = 0;
156 
157     /** Return Array containing a SubImageDesc for each subimage (mipmap level, cubemap face etc.). */
158     virtual BASE_NS::array_view<const SubImageDesc> GetBufferImageCopies() const = 0;
159 
160     struct Deleter {
161         constexpr Deleter() noexcept = default;
162         void operator()(IImageContainer* ptr) const
163         {
164             ptr->Destroy();
165         }
166     };
167     using Ptr = BASE_NS::unique_ptr<IImageContainer, Deleter>;
168 
169 protected:
170     IImageContainer() = default;
171     virtual ~IImageContainer() = default;
172     virtual void Destroy() = 0;
173 };
174 /** @} */
175 CORE_END_NAMESPACE()
176 
177 #endif // API_CORE_IMAGE_IIMAGE_CONTAINER_H
178