Merge branch 'devel/master (1.2.0)' into tizen
[platform/core/uifw/dali-core.git] / dali / internal / event / resources / resource-ticket.h
1 #ifndef __DALI_INTERNAL_RESOURCE_TICKET_H__
2 #define __DALI_INTERNAL_RESOURCE_TICKET_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/vector-wrapper.h>
23 #include <dali/devel-api/common/map-wrapper.h>
24 #include <dali/public-api/common/loading-state.h>
25 #include <dali/public-api/object/ref-object.h>
26 #include <dali/internal/event/resources/resource-type-path.h>
27 #include <dali/integration-api/resource-cache.h>
28
29 namespace Dali
30 {
31
32 namespace Internal
33 {
34
35 class ResourceTicket;
36 typedef IntrusivePtr<ResourceTicket> ResourceTicketPtr;
37 typedef std::vector<ResourceTicketPtr> ResourceTicketContainer;
38
39 class ResourceTicketObserver;
40 class ResourceTicketLifetimeObserver;
41
42 /**
43  * @brief ResourceTicket records the lifetime of a resource loading request.
44  *
45  * When a resource is requested from ResourceClient, you get a ticket.
46  * The state of the loading operation, can be observed using the ticket.
47  * After the ticket is destroyed, the resource will be discarded.
48  *
49  * Multi-threading note: this class should be used in the main thread only
50  * i.e. not from within Dali::Integration::Core::Render().
51  */
52 class ResourceTicket : public RefObject
53 {
54 public:
55
56   /**
57    * Create a resource request.
58    * This should only be done by the ResourceTicketRegistry.
59    * @param [in] observer The object which observes ticket lifetimes.
60    * @param [in] id A unique ID for this ticket (loading request).
61    * @param [in] typePath The resource Type & Path pair.
62    */
63   ResourceTicket( ResourceTicketLifetimeObserver& observer, unsigned int id, ResourceTypePath& typePath );
64
65   /**
66    * Retrieve the unique ID of the resource request.
67    * This can be shared with Nodes etc. which will require the resource when rendering.
68    * @return The unique ID for this request.
69    */
70   unsigned int GetId() const { return mId; }
71
72   /**
73    * Retrieve the type and path of the resource request.
74    * @return The type and path.
75    */
76   const ResourceTypePath& GetTypePath() const { return mTypePath; }
77
78   /**
79    * Query the state of the resource loading request.
80    * @return The state.
81    */
82   Dali::LoadingState GetLoadingState() const { return mLoadingState; }
83
84   /**
85    * Adds an observer.
86    * @param[in] observer The observer.
87    */
88   void AddObserver(ResourceTicketObserver& observer);
89
90   /**
91    * Removes an observer.
92    * @param[in] observer The observer.
93    */
94   void RemoveObserver(ResourceTicketObserver& observer);
95
96 public: // Used by the ResourceClient
97
98   /**
99    * Called when a resource loads successfully.
100    * The ResourceTicketObservers will be notified.
101    */
102   void LoadingSucceeded();
103
104   /**
105    * Called when a resource fails to load.
106    * The ResourceTicketObservers will be notified.
107    */
108   void LoadingFailed();
109
110   /**
111    * Reset state to ResourceLoading.
112    */
113   void Loading();
114
115   /**
116    * Notification when a resource has been uploaded to GL.
117    * The ResourceTicketObservers will be notified.
118    */
119   void Uploaded();
120
121   /**
122    * Called when the ResourceTicketLifetimeObserver is being destroyed.
123    * This method should only be called during destruction of the Dali core.
124    */
125   void StopLifetimeObservation();
126
127 protected:
128
129   /**
130    * A reference counted object may only be deleted by calling Unreference()
131    */
132   virtual ~ResourceTicket();
133
134 private:
135
136   /**
137    * Undefined copy constructor.
138    */
139   ResourceTicket(const ResourceTicket&);
140
141   /**
142    * Undefined assignment operator.
143    */
144   ResourceTicket& operator=(const ResourceTicket& rhs);
145
146 private:
147
148   ResourceTicketLifetimeObserver* mLifetimeObserver; ///< reference to the lifetime-observer; not owned
149
150   unsigned int mId;
151   ResourceTypePath mTypePath;
152
153   Dali::LoadingState mLoadingState;
154
155   // These ticket observers are not owned by the ticket.
156   // Observers must remove themselves by calling RemoveObserver e.g. during destruction.
157   typedef std::vector<ResourceTicketObserver*> ObserverContainer;
158   typedef ObserverContainer::iterator ObserverIter;
159
160   ObserverContainer mObservers;
161 };
162
163 } // namespace Internal
164
165 } // namespace Dali
166
167 #endif // __DALI_INTERNAL_RESOURCE_MANAGER_H__