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