1 #ifndef DALI_INTERNAL_BUFFER_IMAGE_H
2 #define DALI_INTERNAL_BUFFER_IMAGE_H
5 * Copyright (c) 2019 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <stdint.h> // for uint32_t
25 #include <dali/integration-api/bitmap.h> // For Integration::BitmapPtr
26 #include <dali/public-api/object/ref-object.h>
27 #include <dali/internal/event/images/image-impl.h>
28 #include <dali/public-api/images/image.h>
29 #include <dali/public-api/images/buffer-image.h>
38 typedef IntrusivePtr<BufferImage> BufferImagePtr;
41 * BufferImage represents an image resource that can be added to actors etc.
42 * Its pixel buffer data is provided by the application developer.
43 * Pixel buffer memory allocation can be handled by dali or application.
45 class BufferImage : public Image
49 * Create a new BufferImage.
50 * Also a pixel buffer for image data is allocated.
51 * Dali has ownership of the buffer.
52 * For better performance and portability use power of two dimensions.
53 * The maximum size of the image is limited by GL_MAX_TEXTURE_SIZE.
54 * @param [in] width image width in pixels
55 * @param [in] height image height in pixels
56 * @param [in] pixelformat the pixel format (rgba 32 bit by default)
58 static BufferImagePtr New( unsigned int width,
60 Pixel::Format pixelformat );
63 * @DEPRECATED_1_1.5. Support for externally owned Pixel Buffers is due to be removed TBA. It is recommended that a BufferImage owned Buffer be used instead.
65 * @brief Create a new BufferImage, which uses external data source.
67 * Pixel buffer has to be allocated by application.
68 * An internal copy is made of the Pixel Buffer, which can then be freed by the Application, unless if there will be a call to Update() later.
69 * The buffer should only be freed when there is no chance of an Update() being called again.
70 * Obtaining the buffer with GetBuffer() and altering the contents, then Update() will not work with externally owned buffers.
71 * For better performance and portability use power of two dimensions.
72 * The maximum size of the image is limited by GL_MAX_TEXTURE_SIZE.
74 * @param [in] pixBuf pixel buffer. has to be allocated by application.
75 * @param [in] width image width in pixels
76 * @param [in] height image height in pixels
77 * @param [in] pixelformat the pixel format (rgba 32 bit by default)
78 * @param [in] stride the internal stride of the pixelbuffer in pixels
80 static BufferImagePtr New( PixelBuffer* pixBuf,
83 Pixel::Format pixelformat,
84 unsigned int stride );
87 * Create a new BufferImage.
88 * Also a pixel buffer for image data is allocated.
89 * Dali has ownership of the buffer.
90 * For better performance use power of two dimensions.
91 * The maximum size of the image is limited by GL_MAX_TEXTURE_SIZE.
92 * @param [in] width image width in pixels
93 * @param [in] height image height in pixels
94 * @param [in] pixelformat the pixel format (rgba 32 bit by default)
96 BufferImage(unsigned int width,
98 Pixel::Format pixelformat );
101 * Create a new BufferImage, which uses external data source.
102 * Pixel buffer has to be allocated by application.
103 * An internal copy is made of the Pixel Buffer, which can then be freed by the Application, unless if there will be a call to Update() later.
104 * The buffer should only be freed when there is no chance of Update() being called again.
105 * Note: obtaining the buffer with GetBuffer(), writing changes, then Update() will cause any changes to be lost.
106 * In this case, the BufferImage will update from the external buffer and so changes should be written there.
107 * For better performance and portability use power of two dimensions.
108 * The maximum size of the image is limited by GL_MAX_TEXTURE_SIZE.
109 * @param [in] pixBuf pixel buffer. has to be allocated by application.
110 * @param [in] width image width in pixels
111 * @param [in] height image height in pixels
112 * @param [in] pixelformat the pixel format (rgba 32 bit by default)
113 * @param [in] stride the internal stride of the pixelbuffer in pixels
115 BufferImage(PixelBuffer* pixBuf,
118 Pixel::Format pixelformat,
119 unsigned int stride );
123 * A reference counted object may only be deleted by calling Unreference()
125 virtual ~BufferImage();
129 * Notify Dali that the contents of the buffer have changed.
130 * @param [in] updateArea area that has changed in buffer. An empty rect means the whole buffer has changed.
132 void Update( const RectArea& updateArea);
135 * @copydoc Dali::BufferImage::IsDataExternal
137 bool IsDataExternal() const;
140 * Returns the pixel buffer of the Image.
141 * The application developer can write to the buffer.
142 * Upload the modified contents with Update().
143 * @return the pixel buffer
145 PixelBuffer* GetBuffer() const
147 return ( mExternalBuffer ? mExternalBuffer : mInternalBuffer );
151 * Returns buffer size in bytes.
152 * @return the buffer size in bytes
154 unsigned int GetBufferSize() const
160 * Returns buffer stride (in bytes).
161 * @return the buffer stride
163 unsigned int GetBufferStride() const
169 * Get the pixel format
170 * @return The pixel format
172 Pixel::Format GetPixelFormat() const
179 void SetupBuffer( unsigned int width,
181 Pixel::Format pixelformat,
182 unsigned int byteStride );
184 void UploadArea( const RectArea& area );
186 void UpdateBufferArea( PixelBuffer* src, PixelBuffer* dest, const RectArea& area );
190 PixelBuffer* mInternalBuffer; ///< NULL if the data is supplied by an external buffer.
191 PixelBuffer* mExternalBuffer; ///< NULL if there is no external pixel data (this is never owned by BufferImage).
192 uint32_t mBufferSize; ///< size of the pixel buffer.
193 uint32_t mByteStride; ///< width of the pixel buffer in bytes.
194 uint32_t mBytesPerPixel; ///< width of a pixel in bytes.
195 uint32_t mBufferWidth; ///< cached pixel width of bitmap used for transport.
196 Pixel::Format mPixelFormat; ///< pixel format of bitmap.
197 ResourcePolicy::Discardable mResourcePolicy; ///< whether to discard the pixel buffer when removed from the stage or to retain the data.
200 } // namespace Internal
203 * Helper methods for public API.
205 inline Internal::BufferImage& GetImplementation(Dali::BufferImage& image)
207 DALI_ASSERT_ALWAYS( image && "BufferImage handle is empty" );
209 BaseObject& handle = image.GetBaseObject();
211 return static_cast<Internal::BufferImage&>(handle);
214 inline const Internal::BufferImage& GetImplementation(const Dali::BufferImage& image)
216 DALI_ASSERT_ALWAYS( image && "BufferImage handle is empty" );
218 const BaseObject& handle = image.GetBaseObject();
220 return static_cast<const Internal::BufferImage&>(handle);
225 #endif // DALI_INTERNAL_BUFFER_IMAGE_H