1 #ifndef __DALI_INTERNAL_IMAGE_FACTORY_H__
2 #define __DALI_INTERNAL_IMAGE_FACTORY_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.
22 #include <dali/internal/event/resources/resource-type-path-id-map.h>
23 #include <dali/internal/event/resources/resource-ticket.h>
24 #include <dali/internal/event/images/image-factory-cache.h>
33 namespace ImageFactoryCache
39 * ImageFactory is an object that manages Image resource load requests.
40 * It utilises an internal caching system where previous requests and associated
41 * resources are stored to avoid accessing the file system when not neccessary.
43 class ImageFactory : public ImageFactoryCache::RequestLifetimeObserver
50 ImageFactory( ResourceClient& resourceClient );
55 virtual ~ImageFactory();
58 * Registers a request for an image resource if not yet available, but does not start loading yet.
59 * Use Load( req ) to issue load request.
60 * If image was already requested, an existing request is returned.
61 * @param [in] filename path of requested image resource
62 * @param [in] attributes pointer to the ImageAttributes of the request. If NULL, default attributes are used.
63 * @return request pointer
65 ImageFactoryCache::Request* RegisterRequest( const std::string& filename, const ImageAttributes *attributes );
68 * Issue a request which has already been registered with ImageFactory.
69 * If the associated Ticket is no longer alive ImageFactory issues a resource load request.
70 * @param [in] req pointer to request
71 * @return intrusive pointer to image ticket. If Load fails, returned pointer is invalid. (!ret)
73 ResourceTicketPtr Load( ImageFactoryCache::Request* req );
76 * Tells ResourceManager to reload image from filesystem.
77 * Also sends message to render thread.
78 * This operation uses the originally requested attributes when reloading the image.
79 * @pre req must be registered with ImageFactory
80 * @note if image is still loading, no new load request will be issued
81 * @param [in] req Request pointer
82 * @return[out] the ResourceTicket mapped to the request
84 ResourceTicketPtr Reload( ImageFactoryCache::Request* req );
87 * Get resource path used in request.
88 * @param [in] request of the image
89 * @return resource path
91 const std::string& GetRequestPath( const ImageFactoryCache::RequestPtr& request ) const;
94 * Get ImageAttributes for an already requested image resource.
95 * @pre id should mark an existing Resource (Ticket is alive)
96 * @param [in] ticket of the image
97 * @return ImageAttributes used for request.
98 * @throws Throws exception if id is not valid.
100 const ImageAttributes& GetActualAttributes( const ResourceTicketPtr& ticket ) const;
103 * Get ImageAttributes used for request.
104 * @pre req must point to a Request registered with ImageFactory
105 * @param [in] request of the image
106 * @return ImageAttributes used for request.
108 const ImageAttributes& GetRequestAttributes( const ImageFactoryCache::RequestPtr& request ) const;
111 * Retrieve the size of an image. This is either the application requested size or
112 * the actual (full size) that is or will be loaded.
113 * @param[in] request of the image
114 * @param[in] ticket of the image
115 * @param[out] size of the image
117 void GetImageSize( const ImageFactoryCache::RequestPtr& request, const ResourceTicketPtr& ticket, Size& size );
120 * Prevents releasing and reloading image resources in the same frame
121 * @param [in] ticket the resource ticket to queue for releasing
123 void ReleaseTicket( ResourceTicket* ticket );
126 * Flush the queue of resource tickets that were about to be relased.
127 * This discards the kept ticket handles at the end of each frame, and this way prevents
128 * releasing and reloading image resources in the same frame.
130 void FlushReleaseQueue();
132 public: // From RequestLifetimeObserver
135 * Finds request by id in mRequestCache and mUrlCache and removes relevant entries.
136 * @param [in] id request id
138 virtual void RequestDiscarded( const ImageFactoryCache::Request& request );
143 ImageFactory( const ImageFactory& );
146 ImageFactory& operator=( const ImageFactory& rhs );
149 * Checks if the previously loaded image's attributes are compatible with a new request
150 * @param [in] requested The requested attributes
151 * @param [in] actual The actual attributes
152 * @return True if the attributes are compatible
154 bool CompareAttributes( const Dali::ImageAttributes& requested,
155 const Dali::ImageAttributes& actual ) const;
158 * Inserts a new request to the request cache and url cache.
159 * @note this method increases the current request Id counter (mReqIdCurrent)
160 * @param [in] resourceId Ticket id to insert.
161 * @param [in] url The requested url to insert.
162 * @param [in] urlHash Calculated hash value for the url.
163 * @param [in] attr Pointer to the requested attributes, NULL if default values are used.
164 * @return pointer to Request
166 ImageFactoryCache::Request* InsertNewRequest( ResourceId resourceId, const std::string& url, std::size_t urlHash, const ImageAttributes* attr );
169 * Searches request cache for exact match.
170 * @param [in] filename The url of the image resource.
171 * @param [in] hash Hash value for the filename.
172 * @param [in] attributes Pointer to ImageAttributes used for the request or NULL if default attributes were used.
173 * @return pointer to the found request or NULL if no exact match is found.
175 ImageFactoryCache::Request* FindRequest( const std::string& filename, size_t hash, const ImageAttributes *attributes );
178 * Searches through tickets to find a compatible resource.
179 * @param [in] filename The url of the image resource.
180 * @param [in] hash Hash value for the filename.
181 * @param [in] attributes Pointer to ImageAttributes used for the request or NULL if default attributes were used.
182 * @return A ticket pointer to the found resource or an unitialized pointer if no compatible one is found.
184 ResourceTicketPtr FindCompatibleResource( const std::string& filename, size_t hash, const ImageAttributes* attributes );
187 * Helper function that requests the image resource from platform abstraction.
188 * @param [in] filename The url of the image resource.
189 * @param [in] attributes Pointer to ImageAttributes to be used for the request or NULL if default attributes are used.
190 * @return intrusive pointer to Ticket
192 ResourceTicketPtr IssueLoadRequest( const std::string& filename, const ImageAttributes* attributes );
195 ResourceClient& mResourceClient;
196 ImageFactoryCache::RequestPathHashMap mUrlCache; ///< A multimap of url hashes and request IDs
197 ImageFactoryCache::RequestIdMap mRequestCache; ///< A map of request IDs and request information.
198 ResourceTicketContainer mTicketsToRelease; ///< List of ticket handles
199 float mMaxScale; ///< Defines maximum size difference between compatible resources
200 ImageFactoryCache::RequestId mReqIdCurrent; ///< Internal counter for Request IDs
203 } // namespace Internal
207 #endif // __DALI_INTERNAL_IMAGE_FACTORY_H__