1 #ifndef __DALI_INTERNAL_RESOURCE_MANAGER_H__
2 #define __DALI_INTERNAL_RESOURCE_MANAGER_H__
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 #include <dali/public-api/images/image.h>
26 #include <dali/public-api/images/native-image-interface.h>
27 #include <dali/public-api/images/buffer-image.h>
28 #include <dali/public-api/common/ref-counted-dali-vector.h>
30 #include <dali/integration-api/bitmap.h>
31 #include <dali/integration-api/platform-abstraction.h>
32 #include <dali/integration-api/resource-cache.h>
33 #include <dali/integration-api/shader-data.h>
35 #include <dali/internal/common/message.h>
36 #include <dali/internal/event/common/event-thread-services.h>
37 #include <dali/internal/event/common/thread-local-storage.h>
38 #include <dali/internal/event/resources/resource-client-declarations.h>
39 #include <dali/internal/event/effects/shader-factory.h>
40 #include <dali/internal/update/resources/resource-manager-declarations.h>
41 #include <dali/internal/update/resources/bitmap-metadata.h>
46 class NativeImageInterface;
56 class ImageAttributes;
58 // value types used by messages
59 template <> struct ParameterType< Integration::LoadResourcePriority >
60 : public BasicType< Integration::LoadResourcePriority > {};
61 template <> struct ParameterType< Pixel::Format >
62 : public BasicType< Pixel::Format > {};
63 template <> struct ParameterType< Integration::ResourceTypeId >
64 : public BasicType< Integration::ResourceTypeId > {};
70 class TextureCacheDispatcher;
71 class PostProcessResourceDispatcher;
74 class NotificationManager;
76 /** Raw bytes of a resource laid out exactly as it wouldbe in a file, but in memory. */
77 typedef Dali::RefCountedVector<uint8_t> RequestBuffer;
78 /** Counting smart pointer for managing a buffer of raw bytes. */
79 typedef IntrusivePtr<RequestBuffer> RequestBufferPtr;
82 * ResourceManager keeps track of resource loading requests, and caches resources that are loaded.
83 * It uses ResourceTicket objects, to keep track of the lifetime of each request.
84 * If the same resource is required by two client objects, they will share the same ResourceTicket
85 * i.e. only one load will occur using the native filesystem.
87 * Multi-threading notes:
88 * Resources are received from the PlatformAbstraction API during the Core::Render() method, which
89 * may be called from a dedicated rendering thread.
90 * Loading requests must be made from the application's main thread e.g. when Dali::Image is created.
92 class ResourceManager : public Integration::ResourceCache
97 * Create a resource manager.
98 * There should exactly one of these objects per Dali Core.
99 * @param[in] platformAbstraction Used to request resources from the native filesystem.
100 * @param[in] notificationManager Used to send NotifyTickets message.
101 * @param[in] postProcessResourcesQueue Used for performing post processing on resources
102 * @param[in] discardQueue Used to cleanup nodes & resources when no longer in use.
103 * @param[in] renderQueue Used to queue resource updates until the next Render.
105 ResourceManager( Integration::PlatformAbstraction& platformAbstraction,
106 NotificationManager& notificationManager,
107 SceneGraph::TextureCacheDispatcher& textureCacheDispatcher,
108 ResourcePostProcessList& postProcessResourcesQueue,
109 SceneGraph::PostProcessResourceDispatcher& postProcessResourceDispatcher,
110 SceneGraph::DiscardQueue& discardQueue,
111 SceneGraph::RenderQueue& renderQueue );
114 * Virtual destructor.
116 virtual ~ResourceManager();
118 public: // Used by ResourceClient
120 /********************************************************************************
121 ************************ ResourceClient direct interface **********************
122 ********************************************************************************/
125 * Resource client passes itself for secondary intialisation.
126 * (The resource client requires the ResourceManager to be instantiated first).
127 * @param[in] resourceClient The ResourceClient.
129 void SetClient( ResourceClient& resourceClient );
131 /********************************************************************************
132 ************************ UpdateManager direct interface ***********************
133 ********************************************************************************/
136 * Called to update the resource cache before rendering.
137 * New resources will be added to the cache using PlatformAbstraction::FillResourceCache().
138 * Unwanted resources will be added to the DiscardQueue.
139 * @param[in] updateBufferIndex The current update buffer index.
140 * @return true, if a resource load was completed or failed
142 bool UpdateCache( BufferIndex updateBufferIndex );
145 * Iterate through the post process queue, performing requested updates.
146 * @param[in] updateBufferIndex The current update buffer index.
148 void PostProcessResources( BufferIndex updateBufferIndex );
150 /********************************************************************************
151 *************************** CoreImpl direct interface *************************
152 ********************************************************************************/
155 * Returns whether the Resource Manager is still processing any resource requests.
156 * @return true if still processing, false otherwise.
158 bool ResourcesToProcess();
160 /********************************************************************************
161 ********************************* Message handlers *****************************
162 ********************************************************************************/
165 * Request a resource from the native filesystem.
166 * @param[in] id The Id of the requested resource
167 * @param[in] typePath The type & path of requested resource.
168 * @param[in] priority The priority of the request. This is ignored if the resource is already being loaded.
170 void HandleLoadResourceRequest( ResourceId id,
171 const ResourceTypePath& typePath,
172 Integration::LoadResourcePriority priority );
175 * Decode a resource from a memory buffer with the semantics of loading.
176 * Notifications of partial completion, success, and failure will happen via
177 * the same loading notification path used for loading from files: Update()
178 * will retrieve loading events in its main loop and notify listeners to its
179 * own loading events, and forward them, still as loading events, to the event
180 * thread via its update queue.
181 * Resource manager and lower levels make no attempt to detect resource
182 * aliases as is done for multiple requests to load the same resource
183 * file, so the caller is responsible for ensuring that it only requests
184 * the decoding of an in-memory resource once and for doing the sharing of the
185 * resulting object itself. Ultimately this responsibility resides with the
187 * @note ! Only Bitmap resources are supported for decoding from memory !
188 * @param[in] id The Id of the requested resource.
189 * @param[in] typePath The type of the requested resource and a path that is ignored.
190 * @param[in] buffer The raw encoded bytes of the resource as they would appear in a file.
191 * @param[in] priority The priority of the request. This is ignored if the resource is already being loaded.
193 void HandleDecodeResourceRequest( ResourceId id,
194 const ResourceTypePath& typePath,
195 RequestBufferPtr buffer,
196 Integration::LoadResourcePriority priority );
199 * Injects a bitmap resource (does not require loading).
200 * @pre bitmap has to be initialized
201 * @param[in] id The resource id
202 * @param[in] bitmap an initialized bitmap
204 void HandleAddBitmapImageRequest(ResourceId id, Integration::BitmapPtr bitmap);
207 * Add an existing resource to the resource manager.
208 * @param[in] id The resource id
209 * @param [in] resourceData the NativeImageInterface object
210 * @return A ref-counted request object. Keep a copy until the resource is no longer required.
212 void HandleAddNativeImageRequest( ResourceId id, NativeImageInterfacePtr resourceData );
215 * Add an existing resource to the resource manager.
216 * @param[in] id The resource id
217 * @param[in] width width in pixels
218 * @param[in] height height in pixels
219 * @param[in] pixelFormat Pixel format
221 void HandleAddFrameBufferImageRequest( ResourceId id, unsigned int width, unsigned int height, Pixel::Format pixelFormat );
224 * Add an existing resource to the resource manager.
225 * @param[in] id The resource id
226 * @param[in] nativeImage The NativeImage
228 void HandleAddFrameBufferImageRequest( ResourceId id, NativeImageInterfacePtr nativeImage );
231 * Allocate a new empty texture.
232 * @param[in] id The resource id
233 * @param[in] width width in pixels
234 * @param[in] height height in pixels
235 * @param[in] pixelFormat Pixel format
237 void HandleAllocateTextureRequest( ResourceId id, unsigned int width, unsigned int height, Pixel::Format pixelFormat );
241 * Load a shader program from a file
242 * @param[in] id The resource id
243 * @param[in] typePath The type & path of the resource
245 void HandleLoadShaderRequest(ResourceId id, const ResourceTypePath& typePath );
248 * Update bitmap area request
249 * @param[in] textureId The resource ID of a bitmap-texture to remove.
250 * @param[in] area The updated area. Zero width/height indicates the whole bitmap has been updated
252 void HandleUpdateBitmapAreaRequest( ResourceId textureId, const Dali::RectArea& area );
255 * Upload a bitmap to a position within a specified texture
256 * @param[in] destId The destination texture ID
257 * @param[in] bitmap The pointer pointing to the bitmap data to upload
258 * @param [in] xOffset Specifies an offset in the x direction within the texture
259 * @param [in] yOffset Specifies an offset in the y direction within the texture
261 void HandleUploadBitmapRequest( ResourceId destId, Integration::BitmapPtr bitmap, std::size_t xOffset, std::size_t yOffset );
264 * Upload a bitmap to a position within a specified texture
265 * @param[in] destId The destination texture ID
266 * @param[in] srcId The resource ID of the bitmap to upload
267 * @param [in] xOffset Specifies an offset in the x direction within the texture
268 * @param [in] yOffset Specifies an offset in the y direction within the texture
270 void HandleUploadBitmapRequest( ResourceId destId, ResourceId srcId, std::size_t xOffset, std::size_t yOffset );
273 * Request reloading a resource from the native filesystem.
274 * @param[in] id The resource id
275 * @param[in] typePath The type & path of the resource
276 * @param[in] priority The priority of the request. This is ignored if the resource is already being refreshed.
277 * @param[in] resetFinishedStatus True if the finished status of the resource id should be reset
279 void HandleReloadResourceRequest( ResourceId id, const ResourceTypePath& typePath, Integration::LoadResourcePriority priority, bool resetFinishedStatus );
282 * Save a resource to the given url
283 * @param[in] id The resource id
284 * @param[in] typePath The type & path of the resource
286 void HandleSaveResourceRequest( ResourceId id, const ResourceTypePath& typePath );
289 * Resource ticket has been discarded, throw away the actual resource
291 void HandleDiscardResourceRequest( ResourceId id, Integration::ResourceTypeId typeId );
293 /********************************************************************************
294 ******************** Update thread object direct interface ********************
295 ********************************************************************************/
298 * Check if a resource has completed loading.
299 * @param[in] id The ID of a bitmap/texture resource.
300 * @return true if the bitmap or texture has finished loading
302 bool IsResourceLoaded(ResourceId id);
305 * Check if a resource has failed to load, e.g. file not found, etc.
306 * @param[in] id The ID of a bitmap/texture resource.
307 * @return true if the bitmap or texture has failed to load
309 bool IsResourceLoadFailed(ResourceId id);
312 * Get bitmap metadata. This stores meta data about the resource, but
313 * doesn't keep track of the resource
315 BitmapMetadata GetBitmapMetadata(ResourceId id);
318 * Returns the shader resource corresponding to the Id
319 * @param[in] id - the id of a shader binary resource.
320 * @return the shader binary resource data or NULL if it has not been loaded.
322 Integration::ShaderDataPtr GetShaderData(ResourceId id);
324 /********************************************************************************
325 ************************* ResourceCache Implementation ************************
326 ********************************************************************************/
330 * @copydoc Integration::ResourceCache::LoadResponse
332 virtual void LoadResponse(ResourceId id, Integration::ResourceTypeId type, Integration::ResourcePointer resource, Integration::LoadStatus loadStatus);
335 * @copydoc Integration::ResourceCache::SaveComplete
337 virtual void SaveComplete(ResourceId id, Integration::ResourceTypeId type);
340 * @copydoc Integration::ResourceCache::LoadFailed
342 virtual void LoadFailed(ResourceId id, Integration::ResourceFailure failure);
345 * @copydoc Integration::ResourceCache::SaveFailed
347 virtual void SaveFailed(ResourceId id, Integration::ResourceFailure failure);
349 /********************************************************************************
350 ********************************* Private Methods *****************************
351 ********************************************************************************/
354 * Sends notification messages for load sucess & failure,
355 * pushes from newComplete / newFailed into oldComplete / oldFailed respectively
357 void NotifyTickets();
360 * Triggers message to Event thread to update the ticket's image attributes
361 * @pre An Image resource with the given id should exist in the cache.
362 * @param id ID of the image resource
363 * @param attributes Resource image attributes
365 void UpdateImageTicket( ResourceId id, ImageAttributes& attributes );
368 * Send message to ResourceClient in event thread
369 * @param[in] message The message to send
371 void SendToClient( MessageBase* message );
374 * Discard all dead resources.
375 * @param[in] updateBufferIndex The current update buffer index.
377 void DiscardDeadResources( BufferIndex updateBufferIndex );
380 struct ResourceManagerImpl;
381 ResourceManagerImpl* mImpl;
384 // Messages sent to resource manager from other threads:
385 // These functions are run on other threads and insert messages to be
386 // picked-up by the update thread in its main loop and executed on that in
389 inline void RequestLoadResourceMessage( EventThreadServices& eventThreadServices,
390 ResourceManager& manager,
392 const ResourceTypePath& typePath,
393 Integration::LoadResourcePriority priority )
395 typedef MessageValue3< ResourceManager, ResourceId, ResourceTypePath, Integration::LoadResourcePriority > LocalType;
397 // Reserve some memory inside the message queue
398 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ), false );
400 // Construct message in the message queue memory; note that delete should not be called on the return value
401 new (slot) LocalType( &manager, &ResourceManager::HandleLoadResourceRequest, id, typePath, priority );
404 inline void RequestDecodeResourceMessage( EventThreadServices& eventThreadServices,
405 ResourceManager& manager,
407 /// We use typePath instead of the raw type for ownership and to enable copying of a concrete type.
408 const ResourceTypePath& typePath,
409 RequestBufferPtr buffer,
410 Integration::LoadResourcePriority priority )
412 typedef MessageValue4< ResourceManager, ResourceId, ResourceTypePath, RequestBufferPtr, Integration::LoadResourcePriority > LocalType;
414 // Reserve some memory inside the message queue
415 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ), false );
417 // Construct message in the message queue memory; note that delete should not be called on the return value
418 new (slot) LocalType( &manager, &ResourceManager::HandleDecodeResourceRequest, id, typePath, buffer, priority );
421 inline void RequestAddBitmapImageMessage( EventThreadServices& eventThreadServices,
422 ResourceManager& manager,
424 Integration::Bitmap* resourceData )
426 typedef MessageValue2< ResourceManager, ResourceId, Integration::BitmapPtr > LocalType;
428 // Reserve some memory inside the message queue
429 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
431 // Construct message in the message queue memory; note that delete should not be called on the return value
432 new (slot) LocalType( &manager, &ResourceManager::HandleAddBitmapImageRequest, id, resourceData );
435 inline void RequestAddNativeImageMessage( EventThreadServices& eventThreadServices,
436 ResourceManager& manager,
438 NativeImageInterfacePtr resourceData )
440 typedef MessageValue2< ResourceManager, ResourceId, NativeImageInterfacePtr > LocalType;
442 // Reserve some memory inside the message queue
443 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
445 // Construct message in the message queue memory; note that delete should not be called on the return value
446 new (slot) LocalType( &manager, &ResourceManager::HandleAddNativeImageRequest, id, resourceData );
449 inline void RequestAddFrameBufferImageMessage( EventThreadServices& eventThreadServices,
450 ResourceManager& manager,
454 Pixel::Format pixelFormat )
456 typedef MessageValue4< ResourceManager, ResourceId, unsigned int, unsigned int, Pixel::Format > LocalType;
458 // Reserve some memory inside the message queue
459 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
461 // Construct message in the message queue memory; note that delete should not be called on the return value
462 new (slot) LocalType( &manager, &ResourceManager::HandleAddFrameBufferImageRequest, id, width, height, pixelFormat );
465 inline void RequestAddFrameBufferImageMessage( EventThreadServices& eventThreadServices,
466 ResourceManager& manager,
468 NativeImageInterfacePtr resourceData )
470 typedef MessageValue2< ResourceManager, ResourceId, NativeImageInterfacePtr > LocalType;
472 // Reserve some memory inside the message queue
473 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
475 // Construct message in the message queue memory; note that delete should not be called on the return value
476 new (slot) LocalType( &manager, &ResourceManager::HandleAddFrameBufferImageRequest, id, resourceData );
479 inline void RequestAllocateTextureMessage( EventThreadServices& eventThreadServices,
480 ResourceManager& manager,
484 Pixel::Format pixelFormat)
486 typedef MessageValue4< ResourceManager, ResourceId, unsigned int, unsigned int, Pixel::Format > LocalType;
488 // Reserve some memory inside the message queue
489 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
491 // Construct message in the message queue memory; note that delete should not be called on the return value
492 new (slot) LocalType( &manager, &ResourceManager::HandleAllocateTextureRequest, id, width, height, pixelFormat );
496 inline void RequestLoadShaderMessage( EventThreadServices& eventThreadServices,
497 ResourceManager& manager,
499 const ResourceTypePath& typePath )
501 typedef MessageValue2< ResourceManager, ResourceId, ResourceTypePath > LocalType;
503 // Reserve some memory inside the message queue
504 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
506 // Construct message in the message queue memory; note that delete should not be called on the return value
507 new (slot) LocalType( &manager, &ResourceManager::HandleLoadShaderRequest, id, typePath );
510 inline void RequestUpdateBitmapAreaMessage( EventThreadServices& eventThreadServices,
511 ResourceManager& manager,
513 const Dali::RectArea& area )
515 typedef MessageValue2< ResourceManager, ResourceId, Dali::RectArea > LocalType;
517 // Reserve some memory inside the message queue
518 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ), false );
520 // Construct message in the message queue memory; note that delete should not be called on the return value
521 new (slot) LocalType( &manager, &ResourceManager::HandleUpdateBitmapAreaRequest, id, area );
524 inline void RequestUploadBitmapMessage( EventThreadServices& eventThreadServices,
525 ResourceManager& manager,
527 Integration::BitmapPtr bitmap,
529 std::size_t yOffset )
531 typedef MessageValue4< ResourceManager, ResourceId, Integration::BitmapPtr , std::size_t, std::size_t > LocalType;
533 // Reserve some memory inside the message queue
534 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ), false );
536 // Construct message in the message queue memory; note that delete should not be called on the return value
537 new (slot) LocalType( &manager, &ResourceManager::HandleUploadBitmapRequest, destId, bitmap, xOffset, yOffset );
540 inline void RequestUploadBitmapMessage( EventThreadServices& eventThreadServices,
541 ResourceManager& manager,
545 std::size_t yOffset )
547 typedef MessageValue4< ResourceManager, ResourceId, ResourceId, std::size_t, std::size_t > LocalType;
549 // Reserve some memory inside the message queue
550 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ), false );
552 // Construct message in the message queue memory; note that delete should not be called on the return value
553 new (slot) LocalType( &manager, &ResourceManager::HandleUploadBitmapRequest, destId, srcId, xOffset, yOffset );
556 inline void RequestReloadResourceMessage( EventThreadServices& eventThreadServices,
557 ResourceManager& manager,
559 const ResourceTypePath& typePath,
560 Integration::LoadResourcePriority priority,
561 bool resetFinishedStatus )
563 typedef MessageValue4< ResourceManager, ResourceId, ResourceTypePath, Integration::LoadResourcePriority, bool > LocalType;
565 // Reserve some memory inside the message queue
566 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ), false );
568 // Construct message in the message queue memory; note that delete should not be called on the return value
569 new (slot) LocalType( &manager, &ResourceManager::HandleReloadResourceRequest, id, typePath, priority, resetFinishedStatus );
572 inline void RequestSaveResourceMessage( EventThreadServices& eventThreadServices,
573 ResourceManager& manager,
575 const ResourceTypePath& typePath )
577 typedef MessageValue2< ResourceManager, ResourceId, ResourceTypePath > LocalType;
579 // Reserve some memory inside the message queue
580 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
582 // Construct message in the message queue memory; note that delete should not be called on the return value
583 new (slot) LocalType( &manager, &ResourceManager::HandleSaveResourceRequest, id, typePath );
586 inline void RequestDiscardResourceMessage( EventThreadServices& eventThreadServices,
587 ResourceManager& manager,
589 Integration::ResourceTypeId typeId )
591 typedef MessageValue2< ResourceManager, ResourceId, Integration::ResourceTypeId > LocalType;
593 // Reserve some memory inside the message queue
594 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
596 // Construct message in the message queue memory; note that delete should not be called on the return value
597 new (slot) LocalType( &manager, &ResourceManager::HandleDiscardResourceRequest, id, typeId );
600 } // namespace Internal
604 #endif // __DALI_INTERNAL_RESOURCE_MANAGER_H__