Ensure synchronous buffer decode when using encoded buffer image.
[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&, unsigned int, unsigned int )
68    */
69   bool Upload( BufferImage& bufferImage,
70                SizeType xOffset,
71                SizeType yOffset );
72
73   /**
74    * @copydoc Dali::Atlas::Upload( const std::string&, unsigned int, unsigned int )
75    */
76   bool Upload( const std::string& url,
77                SizeType xOffset,
78                SizeType yOffset );
79
80   /**
81    * @copydoc ContextRecoveryInterface::RecoverFromContextLoss
82    */
83   virtual void RecoverFromContextLoss();
84
85 protected:
86
87   /**
88    * @brief Protected constructor.
89    *
90    * @pre width & height are greater than zero.
91    * The maximum size of the atlas is limited by GL_MAX_TEXTURE_SIZE.
92    * @param [in] width          The atlas width in pixels.
93    * @param [in] height         The atlas height in pixels.
94    * @param [in] pixelFormat    The pixel format.
95    * @param [in] recoverContext Whether re-uploading the resource images automatically when regaining the context
96    */
97   Atlas( SizeType width,
98          SizeType height,
99          Pixel::Format pixelFormat,
100          bool recoverContext);
101
102   /**
103    * A reference counted object may only be deleted by calling Unreference()
104    */
105   virtual ~Atlas();
106
107   /**
108    * @copydoc Dali::Internal::Image::Connect
109    */
110   virtual void Connect();
111
112   /**
113    * @copydoc Dali::Internal::Image::Disconnect
114    */
115   virtual void Disconnect();
116
117 private:
118
119   /**
120    * Helper for Upload methods
121    * @return True if the bitmap has the same pixel format and its size fits within the atlas at the specified offset
122    */
123   bool Compatible( Pixel::Format pixelFormat,
124                    SizeType x,
125                    SizeType y );
126   /**
127    * Helper to create the Atlas resource
128    */
129   void AllocateAtlas();
130
131   /**
132    * Helper to release the Atlas resource
133    */
134   void ReleaseAtlas();
135
136   /**
137    * Upload a bitmap with the given color to clear the background.
138    */
139   void ClearBackground( const Vector4& color  );
140
141   /**
142    * Clear all the current tiles and resources of the atlas
143    */
144   void ClearCache();
145
146   /**
147    * Load the bitmap data from the url
148    */
149   Integration::BitmapPtr LoadBitmap( const std::string& url );
150
151 private:
152
153   /**
154    * Record of the url resource in the Atlas.
155    */
156   struct Tile
157   {
158     Tile( SizeType xOffset, SizeType yOffset, const std::string& url )
159     : xOffset( xOffset ), yOffset( yOffset ), url(url)
160     {}
161
162     ~Tile(){};
163
164     SizeType xOffset;   ///< Offset in the x direction within the atlas
165     SizeType yOffset;   ///< Offset in the y direction within the atlas
166     std::string url;    ///< The URL of the resource image file to use
167
168   private:
169     Tile(const Tile& rhs); ///< not defined
170     Tile& operator=(const Tile& rhs); ///< not defined
171   };
172
173 private:
174
175   ResourceClient&          mResourceClient;
176   ImageFactory&            mImageFactory;
177   Vector4                  mClearColor;       ///< The background clear color
178   Vector<Tile*>            mTiles;            ///< The url resources, which would recover automatically when regaining context
179   Pixel::Format            mPixelFormat;      ///< The pixel format (rgba 32 bit by default)
180   bool                     mClear:1;          ///< Clear the backgound or not
181   bool                     mRecoverContext:1; ///< Re-upload the url resources or not when regaining context
182 };
183
184 } // namespace Internal
185
186 /**
187  * Helper methods for public API.
188  */
189 inline Internal::Atlas& GetImplementation(Dali::Atlas& image)
190 {
191   DALI_ASSERT_ALWAYS( image && "Atlas handle is empty" );
192
193   BaseObject& handle = image.GetBaseObject();
194
195   return static_cast<Internal::Atlas&>(handle);
196 }
197
198 inline const Internal::Atlas& GetImplementation(const Dali::Atlas& image)
199 {
200   DALI_ASSERT_ALWAYS( image && "Atlas handle is empty" );
201
202   const BaseObject& handle = image.GetBaseObject();
203
204   return static_cast<const Internal::Atlas&>(handle);
205 }
206
207 } // namespace Dali
208
209 #endif // __DALI_INTERNAL_ATLAS_H__