Merge "Change PixelData to use the handle/body pattern" into devel/master
[platform/core/uifw/dali-core.git] / dali / internal / event / images / atlas-impl.h
1 #ifndef __DALI_INTERNAL_ATLAS_H__
2 #define __DALI_INTERNAL_ATLAS_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/public-api/common/dali-vector.h>
23 #include <dali/devel-api/images/atlas.h>
24 #include <dali/internal/event/images/context-recovery-interface.h>
25 #include <dali/internal/event/images/image-impl.h>
26 #include <dali/internal/event/images/buffer-image-impl.h>
27
28 namespace Dali
29 {
30
31 namespace Internal
32 {
33
34 class ResourceClient;
35
36 typedef Dali::Atlas::SizeType SizeType;
37
38 /**
39  * Internal class for Dali::Atlas
40  */
41 class Atlas : public Image, public ContextRecoveryInterface
42 {
43 public:
44
45   /**
46    * @brief Create a new Atlas.
47    *
48    * @pre width & height are greater than zero.
49    * The maximum size of the atlas is limited by GL_MAX_TEXTURE_SIZE.
50    * @param [in] width          The atlas width in pixels.
51    * @param [in] height         The atlas height in pixels.
52    * @param [in] pixelFormat    The pixel format.
53    * @param [in] recoverContext Whether re-uploading the resource images automatically when regaining the context
54    * @return A pointer to a new Atlas.
55    */
56   static Atlas* New( SizeType width,
57                      SizeType height,
58                      Pixel::Format pixelFormat,
59                      bool recoverContext);
60
61   /**
62    * @copydoc Dali::Atlas::Clear
63    */
64   void Clear( const Vector4& color  );
65
66   /**
67    * @copydoc Dali::Atlas::Upload( const BufferImage&, uint32_t, uint32_t )
68    */
69   bool Upload( BufferImage& bufferImage,
70                SizeType xOffset,
71                SizeType yOffset );
72
73   /**
74    * @copydoc Dali::Atlas::Upload( const std::string&, uint32_t, uint32_t )
75    */
76   bool Upload( const std::string& url,
77                SizeType xOffset,
78                SizeType yOffset );
79
80   /**
81    * @copydoc Dali::Atlas::Upload( Dali::PixelData, uint32_t, uint32_t )
82    */
83   bool Upload( PixelDataPtr pixelData,
84                SizeType xOffset,
85                SizeType yOffset );
86
87   /**
88    * @copydoc ContextRecoveryInterface::RecoverFromContextLoss
89    */
90   virtual void RecoverFromContextLoss();
91
92 protected:
93
94   /**
95    * @brief Protected constructor.
96    *
97    * @pre width & height are greater than zero.
98    * The maximum size of the atlas is limited by GL_MAX_TEXTURE_SIZE.
99    * @param [in] width          The atlas width in pixels.
100    * @param [in] height         The atlas height in pixels.
101    * @param [in] pixelFormat    The pixel format.
102    * @param [in] recoverContext Whether re-uploading the resource images automatically when regaining the context
103    */
104   Atlas( SizeType width,
105          SizeType height,
106          Pixel::Format pixelFormat,
107          bool recoverContext);
108
109   /**
110    * A reference counted object may only be deleted by calling Unreference()
111    */
112   virtual ~Atlas();
113
114   /**
115    * @copydoc Dali::Internal::Image::Connect
116    */
117   virtual void Connect();
118
119   /**
120    * @copydoc Dali::Internal::Image::Disconnect
121    */
122   virtual void Disconnect();
123
124 private:
125
126   /**
127    * Helper for Upload methods
128    * @return True if the size of the bitmap fits within the atlas at the specified offset
129    */
130   bool IsInside( SizeType x, SizeType y );
131
132   /**
133    * Helper to create the Atlas resource
134    */
135   void AllocateAtlas();
136
137   /**
138    * Helper to release the Atlas resource
139    */
140   void ReleaseAtlas();
141
142   /**
143    * Upload a bitmap with the given color to clear the background.
144    */
145   void ClearBackground( const Vector4& color  );
146
147   /**
148    * Clear all the current tiles and resources of the atlas
149    */
150   void ClearCache();
151
152   /**
153    * Load the bitmap data from the url
154    */
155   Integration::BitmapPtr LoadBitmap( const std::string& url );
156
157 private:
158
159   /**
160    * Record of the url resource in the Atlas.
161    */
162   struct Tile
163   {
164     Tile( SizeType xOffset, SizeType yOffset, const std::string& url )
165     : xOffset( xOffset ), yOffset( yOffset ), url(url)
166     {}
167
168     ~Tile(){};
169
170     SizeType xOffset;   ///< Offset in the x direction within the atlas
171     SizeType yOffset;   ///< Offset in the y direction within the atlas
172     std::string url;    ///< The URL of the resource image file to use
173
174   private:
175     Tile(const Tile& rhs); ///< not defined
176     Tile& operator=(const Tile& rhs); ///< not defined
177   };
178
179 private:
180
181   ResourceClient&          mResourceClient;
182   ImageFactory&            mImageFactory;
183   Vector4                  mClearColor;       ///< The background clear color
184   Vector<Tile*>            mTiles;            ///< The url resources, which would recover automatically when regaining context
185   Pixel::Format            mPixelFormat;      ///< The pixel format (rgba 32 bit by default)
186   bool                     mClear:1;          ///< Clear the backgound or not
187   bool                     mRecoverContext:1; ///< Re-upload the url resources or not when regaining context
188 };
189
190 } // namespace Internal
191
192 /**
193  * Helper methods for public API.
194  */
195 inline Internal::Atlas& GetImplementation(Dali::Atlas& image)
196 {
197   DALI_ASSERT_ALWAYS( image && "Atlas handle is empty" );
198
199   BaseObject& handle = image.GetBaseObject();
200
201   return static_cast<Internal::Atlas&>(handle);
202 }
203
204 inline const Internal::Atlas& GetImplementation(const Dali::Atlas& image)
205 {
206   DALI_ASSERT_ALWAYS( image && "Atlas handle is empty" );
207
208   const BaseObject& handle = image.GetBaseObject();
209
210   return static_cast<const Internal::Atlas&>(handle);
211 }
212
213 } // namespace Dali
214
215 #endif // __DALI_INTERNAL_ATLAS_H__