ResourceImage/Image split
[platform/core/uifw/dali-core.git] / dali / internal / event / images / bitmap-image-impl.h
1 #ifndef __DALI_INTERNAL_BITMAP_IMAGE_H__
2 #define __DALI_INTERNAL_BITMAP_IMAGE_H__
3
4 /*
5  * Copyright (c) 2014 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/public-api/object/ref-object.h>
23 #include <dali/internal/event/images/image-impl.h>
24 #include <dali/public-api/images/image.h>
25 #include <dali/public-api/images/bitmap-image.h>
26
27 namespace Dali
28 {
29
30 namespace Internal
31 {
32
33 class BitmapImage;
34 typedef IntrusivePtr<BitmapImage> BitmapImagePtr;
35
36 class ResourceClient;
37 class ResourceManager;
38
39 /**
40  * BitmapImage represents an image resource that can be added to actors etc.
41  * Its pixel buffer data is provided by the application developer.
42  * Pixel buffer memory allocation can be handled by dali or application.
43  */
44 class BitmapImage : public Image
45 {
46 public:
47   /**
48    * Create a new BitmapImage.
49    * Also a pixel buffer for image data is allocated.
50    * Dali has ownership of the buffer.
51    * For better performance and portability use power of two dimensions.
52    * The maximum size of the image is limited by GL_MAX_TEXTURE_SIZE.
53    * @param [in] width       image width in pixels
54    * @param [in] height      image height in pixels
55    * @param [in] pixelformat the pixel format (rgba 32 bit by default)
56    * @param [in] releasePol  optionally relase memory when image is not visible on screen (default: keep image data until Image object is alive).
57    */
58   static BitmapImagePtr New( unsigned int width,
59                              unsigned int height,
60                              Pixel::Format pixelformat,
61                              ReleasePolicy releasePol = IMAGE_RELEASE_POLICY_DEFAULT );
62
63   /**
64    * Create a new BitmapImage, which uses external data source.
65    * Pixel buffer has to be allocated by application.
66    * Application holds ownership of the buffer.
67    * For better performance and portability use power of two dimensions.
68    * The maximum size of the image is limited by GL_MAX_TEXTURE_SIZE.
69    * @note  in case releasePol is "OffStage", application has to call Image::BufferUpdated() whenever image is re-added to the stage
70    * @param [in] pixBuf      pixel buffer. has to be allocated by application.
71    * @param [in] width       image width in pixels
72    * @param [in] height      image height in pixels
73    * @param [in] pixelformat the pixel format (rgba 32 bit by default)
74    * @param [in] stride      the internal stride of the pixelbuffer in pixels
75    * @param [in] releasePol  optionally relase memory when image is not visible on screen (default: keep image data until Image object is alive).
76    */
77   static BitmapImagePtr New( PixelBuffer* pixBuf,
78                              unsigned int width,
79                              unsigned int height,
80                              Pixel::Format pixelformat,
81                              unsigned int stride,
82                              ReleasePolicy releasePol = IMAGE_RELEASE_POLICY_DEFAULT );
83
84   /**
85    * Create a new BitmapImage.
86    * Also a pixel buffer for image data is allocated.
87    * Dali has ownership of the buffer.
88    * For better performance use power of two dimensions.
89    * The maximum size of the image is limited by GL_MAX_TEXTURE_SIZE.
90    * @param [in] width image width in pixels
91    * @param [in] height image height in pixels
92    * @param [in] pixelformat the pixel format (rgba 32 bit by default)
93    * @param [in] releasePol optionally release memory when image is not visible on screen (default: keep image data until Image object is alive).
94    */
95   BitmapImage(unsigned int width,
96               unsigned int height,
97               Pixel::Format pixelformat,
98               ReleasePolicy releasePol = IMAGE_RELEASE_POLICY_DEFAULT);
99
100   /**
101    * Create a new BitmapImage, which uses external data source.
102    * Pixel buffer has to be allocated by application.
103    * Application holds ownership of the buffer.
104    * For better performance and portability use power of two dimensions.
105    * The maximum size of the image is limited by GL_MAX_TEXTURE_SIZE.
106    * @note  in case releasePol is "OffStage", application has to call Image::BufferUpdated() whenever image is re-added to the stage
107    * @param [in] pixBuf      pixel buffer. has to be allocated by application.
108    * @param [in] width       image width in pixels
109    * @param [in] height      image height in pixels
110    * @param [in] pixelformat the pixel format (rgba 32 bit by default)
111    * @param [in] stride      the internal stride of the pixelbuffer in pixels
112    * @param [in] releasePol  optionally relase memory when image is not visible on screen (default: keep image data until Image object is alive).
113    */
114   BitmapImage(PixelBuffer* pixBuf,
115               unsigned int width,
116               unsigned int height,
117               Pixel::Format pixelformat,
118               unsigned int stride,
119               ReleasePolicy releasePol = IMAGE_RELEASE_POLICY_DEFAULT);
120
121 protected:
122   /**
123    * A reference counted object may only be deleted by calling Unreference()
124    */
125   virtual ~BitmapImage();
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::BitmapImage::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 BufferUpdated.
143    * @return the pixel buffer
144    */
145   PixelBuffer* GetBuffer();
146
147   /**
148    * Returns buffer size in bytes.
149    * @return the buffer size in bytes
150    */
151   unsigned int GetBufferSize() const;
152
153   /**
154    * Returns buffer stride (in bytes).
155    * @return the buffer stride
156    */
157   unsigned int GetBufferStride() const;
158
159   /**
160    * Get the pixel format
161    * @return The pixel format
162    */
163   Pixel::Format GetPixelFormat() const;
164
165 protected: // From Resource
166   /**
167    * @copydoc Dali::Internal::Image::Connect
168    */
169   virtual void Connect();
170
171   /**
172    * @copydoc Dali::Internal::Image::Disconnect
173    */
174   virtual void Disconnect();
175
176   /**
177    * Get the bitmap from local cache or ticket.
178    **/
179   Integration::Bitmap * GetBitmap() const;
180
181 private:
182   bool mIsDataExternal; ///< whether application holds ownership of pixel buffer or not
183
184   ResourceClient*            mResourceClient;
185
186 protected:
187   Integration::BitmapPtr     mBitmapCached;
188 };
189
190 } // namespace Internal
191
192 /**
193  * Helper methods for public API.
194  */
195 inline Internal::BitmapImage& GetImplementation(Dali::BitmapImage& image)
196 {
197   DALI_ASSERT_ALWAYS( image && "BitmapImage handle is empty" );
198
199   BaseObject& handle = image.GetBaseObject();
200
201   return static_cast<Internal::BitmapImage&>(handle);
202 }
203
204 inline const Internal::BitmapImage& GetImplementation(const Dali::BitmapImage& image)
205 {
206   DALI_ASSERT_ALWAYS( image && "BitmapImage handle is empty" );
207
208   const BaseObject& handle = image.GetBaseObject();
209
210   return static_cast<const Internal::BitmapImage&>(handle);
211 }
212
213 } // namespace Dali
214
215 #endif // __DALI_INTERNAL_BITMAP_IMAGE_H__