496eaacf634f63ad233b7818f576aea23d2ec587
[platform/core/uifw/dali-core.git] / dali / internal / event / images / buffer-image-impl.h
1 #ifndef __DALI_INTERNAL_BUFFER_IMAGE_H__
2 #define __DALI_INTERNAL_BUFFER_IMAGE_H__
3
4 /*
5  * Copyright (c) 2015 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 // INTERNAL INCLUDES
22 #include <dali/integration-api/bitmap.h> // For Integration::BitmapPtr
23 #include <dali/public-api/object/ref-object.h>
24 #include <dali/internal/event/images/image-impl.h>
25 #include <dali/public-api/images/image.h>
26 #include <dali/public-api/images/buffer-image.h>
27
28 namespace Dali
29 {
30
31 namespace Internal
32 {
33
34 class BufferImage;
35 typedef IntrusivePtr<BufferImage> BufferImagePtr;
36
37 class ResourceClient;
38 class ResourceManager;
39
40 /**
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.
44  */
45 class BufferImage : public Image
46 {
47 public:
48   /**
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)
57    */
58   static BufferImagePtr New( unsigned int width,
59                              unsigned int height,
60                              Pixel::Format pixelformat );
61
62   /**
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.
64    *
65    * @brief Create a new BufferImage, which uses external data source.
66    *
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.
73    *
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
79    */
80   static BufferImagePtr New( PixelBuffer* pixBuf,
81                              unsigned int width,
82                              unsigned int height,
83                              Pixel::Format pixelformat,
84                              unsigned int stride );
85
86   /**
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)
95    */
96   BufferImage(unsigned int width,
97               unsigned int height,
98               Pixel::Format pixelformat );
99
100   /**
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
114    */
115   BufferImage(PixelBuffer* pixBuf,
116               unsigned int width,
117               unsigned int height,
118               Pixel::Format pixelformat,
119               unsigned int stride );
120
121 protected:
122   /**
123    * A reference counted object may only be deleted by calling Unreference()
124    */
125   virtual ~BufferImage();
126
127 public:
128   /**
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.
131    */
132   void Update (RectArea& updateArea);
133
134   /**
135    * @copydoc Dali::BufferImage::IsDataExternal
136    */
137   bool IsDataExternal() const;
138
139   /**
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
144    */
145   PixelBuffer* GetBuffer() const
146   {
147     return ( mExternalBuffer ? mExternalBuffer : mInternalBuffer );
148   }
149
150   /**
151    * Returns buffer size in bytes.
152    * @return the buffer size in bytes
153    */
154   unsigned int GetBufferSize() const
155   {
156     return mBufferSize;
157   }
158
159   /**
160    * Returns buffer stride (in bytes).
161    * @return the buffer stride
162    */
163   unsigned int GetBufferStride() const
164   {
165     return mByteStride;
166   }
167
168   /**
169    * Get the pixel format
170    * @return The pixel format
171    */
172   Pixel::Format GetPixelFormat() const
173   {
174     return mPixelFormat;
175   }
176
177   /**
178    * @brief Upload pixel data to another resource at an offset
179    *
180    * @param destId ResourceId of the destination
181    * @param xOffset x offset in the destination
182    * @param yOffset y offset in the destination
183    */
184   void UploadBitmap( ResourceId destId, std::size_t xOffset, std::size_t yOffset );
185
186 protected: // From Image
187   /**
188    * @copydoc Dali::Internal::Image::Connect
189    */
190   virtual void Connect();
191
192   /**
193    * @copydoc Dali::Internal::Image::Disconnect
194    */
195   virtual void Disconnect();
196
197 private:
198
199   void SetupBuffer( unsigned int width,
200                     unsigned int height,
201                     Pixel::Format pixelformat,
202                     unsigned int byteStride );
203
204   void CreateHostBitmap();
205
206   void UploadArea( ResourceId destId, const RectArea& area );
207
208   void UpdateBufferArea( PixelBuffer* src, PixelBuffer* dest, const RectArea& area );
209
210 private:
211
212   PixelBuffer*                 mInternalBuffer;       ///< NULL if the data is supplied by an external buffer.
213   PixelBuffer*                 mExternalBuffer;       ///< NULL if there is no external pixel data (this is never owned by BufferImage).
214   ResourceClient*              mResourceClient;       ///< pointer to the resource client.
215   uint32_t                     mBufferSize;           ///< size of the pixel buffer.
216   uint32_t                     mByteStride;           ///< width of the pixel buffer in bytes.
217   uint32_t                     mBytesPerPixel;        ///< width of a pixel in bytes.
218   uint32_t                     mBufferWidth;          ///< cached pixel width of bitmap used for transport.
219   Pixel::Format                mPixelFormat;          ///< pixel format of bitmap.
220   ResourcePolicy::Discardable  mResourcePolicy;       ///< whether to discard the pixel buffer when removed from the stage or to retain the data.
221 };
222
223 } // namespace Internal
224
225 /**
226  * Helper methods for public API.
227  */
228 inline Internal::BufferImage& GetImplementation(Dali::BufferImage& image)
229 {
230   DALI_ASSERT_ALWAYS( image && "BufferImage handle is empty" );
231
232   BaseObject& handle = image.GetBaseObject();
233
234   return static_cast<Internal::BufferImage&>(handle);
235 }
236
237 inline const Internal::BufferImage& GetImplementation(const Dali::BufferImage& image)
238 {
239   DALI_ASSERT_ALWAYS( image && "BufferImage handle is empty" );
240
241   const BaseObject& handle = image.GetBaseObject();
242
243   return static_cast<const Internal::BufferImage&>(handle);
244 }
245
246 } // namespace Dali
247
248 #endif // __DALI_INTERNAL_BUFFER_IMAGE_H__