[dali_2.3.39] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / internal / event / images / bitmap-packed-pixel.h
1 #ifndef DALI_INTERNAL_BITMAP_H
2 #define DALI_INTERNAL_BITMAP_H
3
4 /*
5  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22
23 // INTERNAL INCLUDES
24 #include <dali/integration-api/bitmap.h>
25
26 namespace Dali
27 {
28 namespace Internal
29 {
30 class BitmapPackedPixel;
31 using BitmapPackedPixelPtr = IntrusivePtr<BitmapPackedPixel>;
32
33 /**
34  * BitmapPackedPixel class.
35  * A container for image data that is packed into individual struct-like
36  * pixels in an addressable 2D array, with each pixel occupying a whole
37  * number of bytes.
38  * This is a vanilla Bitmap class, typically used to hold data decompressed
39  * from PNG and JPEG file formats for example.
40  */
41 class BitmapPackedPixel : public Dali::Integration::Bitmap, Dali::Integration::Bitmap::PackedPixelsProfile
42 {
43 public:
44   /**
45    * Constructor
46    * @param[in] discardable Flag to tell the bitmap if it can delete the buffer with the pixel data.
47    */
48   BitmapPackedPixel(ResourcePolicy::Discardable discardable = ResourcePolicy::OWNED_RETAIN, Dali::Integration::PixelBuffer* pixBuf = nullptr);
49
50 public:
51   const Bitmap::PackedPixelsProfile* GetPackedPixelsProfile() const override
52   {
53     return this;
54   }
55   Bitmap::PackedPixelsProfile* GetPackedPixelsProfile() override
56   {
57     return this;
58   }
59
60   /**
61    * (Re-)Allocate pixel buffer for the Bitmap. Any previously allocated pixel buffer is deleted.
62    * Dali has ownership of the buffer, but its contents can be modified.
63    * Bitmap stores given size information about the image.
64    * @pre bufferWidth, bufferHeight have to be power of two
65    * @param[in] pixelFormat   pixel format
66    * @param[in] width         Image width in pixels
67    * @param[in] height        Image height in pixels
68    * @param[in] bufferWidth   Buffer width (stride) in pixels
69    * @param[in] bufferHeight  Buffer height in pixels
70    * @return pixel buffer pointer
71    */
72   Dali::Integration::PixelBuffer* ReserveBuffer(Pixel::Format pixelFormat,
73                                                 uint32_t      width,
74                                                 uint32_t      height,
75                                                 uint32_t      bufferWidth  = 0,
76                                                 uint32_t      bufferHeight = 0) override;
77
78   /**
79    * Assign a pixel buffer. Any previously allocated pixel buffer is deleted.
80    * Dali has ownership of the buffer, but its contents can be modified.
81    * Bitmap stores given size information about the image.
82    * @pre bufferWidth, bufferHeight have to be power of two
83    * @param[in] pixelFormat   pixel format
84    * @param[in] buffer        the pixel buffer
85    * @param[in] bufferSize    size of the pixel buffer
86    * @param[in] width         Image width in pixels
87    * @param[in] height        Image height in pixels
88    * @param[in] bufferWidth   Buffer width (stride) in pixels
89    * @param[in] bufferHeight  Buffer height in pixels
90    */
91   void AssignBuffer(Pixel::Format                   pixelFormat,
92                     Dali::Integration::PixelBuffer* buffer,
93                     uint32_t                        bufferSize,
94                     uint32_t                        width,
95                     uint32_t                        height,
96                     uint32_t                        bufferWidth  = 0,
97                     uint32_t                        bufferHeight = 0) override;
98
99   /**
100    * Get the width of the buffer (stride)
101    * @return The width of the buffer in pixels
102    */
103   unsigned GetBufferWidth() const override
104   {
105     return mBufferWidth;
106   }
107
108   /**
109    * Get the height of the buffer
110    * @return The height of the buffer in pixels
111    */
112   unsigned GetBufferHeight() const override
113   {
114     return mBufferHeight;
115   }
116
117   /**
118    * Get the pixel buffer size in bytes
119    * @return The buffer size in bytes.
120    */
121   uint32_t GetBufferSize() const override
122   {
123     return mBufferWidth * mBytesPerPixel * mBufferHeight;
124   }
125
126   /**
127    * See Dali::Integration::Bitmap::GetReleaseFunction()
128    */
129   ReleaseFunction GetReleaseFunction() override
130   {
131     return FREE;
132   }
133
134   /**
135    * Get the pixel buffer stride.
136    * @return The buffer stride (in bytes).
137    */
138   uint32_t GetBufferStride() const override;
139
140   /**
141    * Get the pixel format
142    * @return The pixel format
143    */
144   Pixel::Format GetPixelFormat() const
145   {
146     return mPixelFormat;
147   }
148
149   /**
150    * Check the bitmap data and test whether it has any transparent pixels.
151    * This property can then be tested for with IsFullyOpaque().
152    */
153   void TestForTransparency() override;
154
155 protected:
156   /**
157    * A reference counted object may only be deleted by calling Unreference()
158    */
159   ~BitmapPackedPixel() override;
160
161 protected:
162   uint32_t mBufferWidth;   ///< Buffer width (stride) in pixels
163   uint32_t mBufferHeight;  ///< Buffer height in pixels
164   uint32_t mBytesPerPixel; ///< Bytes per pixel
165
166 private:
167   /**
168    * Initializes internal class members
169    * @param[in] pixelFormat   pixel format
170    * @param[in] width         Image width in pixels
171    * @param[in] height        Image height in pixels
172    * @param[in] bufferWidth   Buffer width (stride) in pixels
173    * @param[in] bufferHeight  Buffer height in pixels
174    */
175   void Initialize(Pixel::Format pixelFormat,
176                   uint32_t      width,
177                   uint32_t      height,
178                   uint32_t      bufferWidth,
179                   uint32_t      bufferHeight);
180
181   BitmapPackedPixel(const BitmapPackedPixel& other);            ///< defined private to prevent use
182   BitmapPackedPixel& operator=(const BitmapPackedPixel& other); ///< defined private to prevent use
183
184   // Changes scope, should be at end of class
185   DALI_LOG_OBJECT_STRING_DECLARATION;
186 };
187
188 } // namespace Internal
189
190 } // namespace Dali
191
192 #endif // DALI_INTERNAL_BITMAP_H