[SRUK] Initial copy from Tizen 2.2 version
[platform/core/uifw/dali-core.git] / dali / internal / event / text / resource / glyph-resource-manager.h
1 #ifndef __DALI_INTERNAL_GLYPH_RESOURCE_MANAGER_H__
2 #define __DALI_INTERNAL_GLYPH_RESOURCE_MANAGER_H__
3
4 //
5 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
6 //
7 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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
22 // INTERNAL INCLUDES
23 #include <dali/public-api/common/map-wrapper.h>
24 #include <dali/internal/event/text/glyph-status/glyph-status.h>
25 #include <dali/internal/event/text/resource/glyph-resource-observer.h>
26 #include <dali/internal/event/text/resource/glyph-resource-request.h>
27 #include <dali/internal/event/text/resource/glyph-load-observer.h>
28 #include <dali/internal/event/text/resource/font-lookup-interface.h>
29 #include <dali/internal/event/text/resource/glyph-texture-observer.h>
30 #include <dali/internal/event/text/text-observer.h>
31 #include <dali/internal/event/resources/resource-ticket.h>
32 #include <dali/integration-api/resource-cache.h>
33
34 // EXTERNAL INCLUDES
35 #include <set>
36
37 namespace Dali
38 {
39
40 namespace Internal
41 {
42
43 class ResourceClient;
44
45 /**
46  * Responsible for managing the resources used by a GlyphAtlas.
47  * This includes
48  * - Texture for holding the atlas
49  * - GlyphSets for uploading glyphs to a Texture.
50  *
51  * It implements the GlyphLoadObserver interface, so that resource-client
52  * can inform it when glyphs are loaded.
53  */
54 class GlyphResourceManager : public GlyphLoadObserver
55 {
56
57 public:
58
59   /**
60    * Constructor
61    * @param[in] fontLookup font lookup interface
62    */
63   GlyphResourceManager( const FontLookupInterface& fontLookup );
64
65   /**
66    * Destructor
67    */
68   virtual ~GlyphResourceManager();
69
70   /**
71    * Create a texture which can be used to upload character bitmaps to.
72    * @param[in] size the width and height of the square texture
73    * @return texture resource id
74    */
75  unsigned int CreateTexture(unsigned int size );
76
77   /**
78    * Add a glyph resource observer
79    * @param[in] observer The observer to add.
80    */
81   void AddObserver( GlyphResourceObserver& observer);
82
83   /**
84    * Remove a glyph resource observer.
85    * @param[in] observer The observer to remove.
86    */
87   void RemoveObserver( GlyphResourceObserver& observer);
88
89   /**
90    * Adds a text observer.
91    * The observer will get a call back whenever new text is loaded.
92    * The observer is responsible for calling RemoveObserver() when
93    * all it's text is loaded or before destruction
94    * @param[in] observer The observer to add.
95    */
96   void AddTextObserver( TextObserver& observer );
97
98   /**
99    * Removes a text observer.
100    * @param[in] observer The observer to remove.
101    */
102   void RemoveTextObserver( TextObserver& observer );
103
104   /**
105    * Adds a texture observer, to detect atlas resize / split changes
106    * @param[in] observer The observer to add.
107    */
108   void AddTextureObserver( GlyphTextureObserver& observer);
109
110   /**
111    * Removes a texture observer
112    * @param[in] observer The texture observer to remove.
113    */
114   void RemoveTextureObserver( GlyphTextureObserver& observer);
115
116   /**
117    * Add a list of requests to load glyphs
118    * @param[in] request list glyph request list
119    * @param[in] observer the observer that sent the request
120    * @param[in] atlasTextureId the texture ID of the atlas
121    */
122   void AddRequests( const GlyphRequestList& requestList, GlyphResourceObserver& observer,
123                     Integration::ResourceId atlasTextureId );
124
125 public: // for GlyphLoadObserver
126
127   /**
128    * @copydoc GlyphLoadObserver::GlyphsLoaded()
129    */
130   virtual void GlyphsLoaded( Integration::ResourceId id, const Integration::GlyphSet& glyphSet, Integration::LoadStatus loadStatus  );
131
132 private:
133
134   /**
135    * Send resource requests to load glyphs.
136    * @param[in]request glyph resource request
137    * @param[in] observer glyph resource observer
138    * @param[in] atlasTextureId the texture ID of the atlas
139    */
140   void SendRequests( const GlyphResourceRequest& request, GlyphResourceObserver& observer,
141                      Integration::ResourceId atlasTextureId );
142   /**
143    * Upload a glyphset to a texture
144    * @param[in] observer glyph resource observer
145    * @param[in] fontId font id
146    * @param[in] glyphSet glyph set (contains the bitmaps to upload)
147    */
148   void UploadGlyphsToTexture( GlyphResourceObserver* observer,
149                               FontId fontId,
150                               const Integration::GlyphSet& glyphSet  );
151
152   /**
153    * Notify text observers that some characters have been loaded
154    */
155   void NotifyTextObservers();
156
157   /**
158    * notify texture observers, that a texture has been replaced
159    * @param[in] oldTextureIds list of old texture id's that have been replaced by the new texture id
160    * @paran[in] newTextureId new texture id
161    */
162   void NotifyTextureReplaced( TextureIdList& oldTextureIds, unsigned int newTextureId);
163
164   /**
165    * New atlas texture has been updated - delete old textures.
166    * @param[in] observer glyph resource observer
167    */
168   void DeleteOldTextures( GlyphResourceObserver* observer );
169
170   /**
171    * glyph update type
172    */
173   enum GlyphUpdateType
174   {
175     GLYPH_LOADED_FROM_FILE, ///< loaded from file
176     GLYPH_UPLOADED_TO_GL,   ///< uploaded to gl
177   };
178
179   /**
180    * update a glyph observer to say whether either the glyphs have been
181    * loaded from file, or have been uploaded to gl.
182    * @param[in] observer glyph resource observer
183    * @param[in] fontId font id
184    * @param[in] glyphSet glyph set
185    * @param[in] updateType glyph update type
186    */
187   void UpdateObserver( GlyphResourceObserver* observer,
188                        FontId fontId,
189                        const Integration::GlyphSet& glyphSet,
190                        GlyphUpdateType updateType);
191
192
193   /**
194    * Given a resource id, return the observer watching that resource
195    * @param[in] id resource id
196    * @return glyph resource observer
197    */
198   GlyphResourceObserver* GetObserver( Integration::ResourceId id );
199
200   /**
201    * Delete a texture ticket
202    * @param[in] id texture id
203    */
204   void DeleteTextureTicket(unsigned int id );
205
206   /**
207    * Convert between GlyphQuality enum and integration TextQuality
208    * @param[in] quality GlyphQuality
209    * @return integration TextQuality
210    */
211   Integration::TextResourceType::TextQuality GetQuality( GlyphResourceRequest::GlyphQuality quality );
212
213
214   // Undefined copy constructor.
215   GlyphResourceManager( const GlyphResourceManager& );
216
217   // Undefined assignment operator.
218   GlyphResourceManager& operator=( const GlyphResourceManager& );
219
220
221   typedef std::pair< ResourceTicketPtr, GlyphResourceObserver* > ObserverTicketPair;  ///<  ticket & observer pair
222   typedef std::map< Integration::ResourceId, ObserverTicketPair > TicketList;         ///<  key = resource id = key, data = ticket+observer
223   typedef std::list< ResourceTicketPtr > TextureTickets;                              ///< list of texture tickets
224   typedef std::set<GlyphResourceObserver*> ObserverList;                              ///< Observer list typedef
225   typedef std::set<TextObserver*> TextObserverList;
226
227   ObserverList            mObservers;             ///< unique set of glyph observers
228   TextObserverList        mTextObservers;         ///< unique set of text observers
229   TicketList              mGlyphLoadTickets;      ///< list of tickets for glyphset load requests
230   TextureTickets          mTextureTickets;        ///< list of tickets for texture requests
231   const FontLookupInterface&  mFontLookup;        ///< font lookup
232   ResourceClient&         mResourceClient;        ///< resource client
233 };
234
235 } // namespace Internal
236
237 } // namespace Dali
238
239 #endif // __DALI_INTERNAL_GLYPH_RESOURCE_MANAGER_H__