Add null check for mTextureSet.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / image-loader / image-load-thread.h
1 #ifndef DALI_TOOLKIT_IMAGE_LOAD_THREAD_H
2 #define DALI_TOOLKIT_IMAGE_LOAD_THREAD_H
3
4 /*
5  * Copyright (c) 2020 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 // EXTERNAL INCLUDES
21 #include <dali/public-api/common/dali-vector.h>
22 #include <dali/public-api/object/ref-object.h>
23 #include <dali/public-api/images/image-operations.h>
24 #include <dali/devel-api/threading/conditional-wait.h>
25 #include <dali/devel-api/threading/mutex.h>
26 #include <dali/devel-api/threading/thread.h>
27 #include <dali/devel-api/adaptor-framework/event-thread-callback.h>
28 #include <dali/devel-api/adaptor-framework/pixel-buffer.h>
29 #include <dali-toolkit/internal/visuals/visual-url.h>
30 #include <dali-toolkit/devel-api/image-loader/async-image-loader-devel.h>
31 #include <dali/integration-api/adaptor-framework/log-factory-interface.h>
32
33 namespace Dali
34 {
35
36 namespace Toolkit
37 {
38
39 namespace Internal
40 {
41
42 /**
43  * The task of loading and packing an image into the atlas.
44  */
45 struct LoadingTask
46 {
47   /**
48    * Constructor.
49    * @param [in] id of the task
50    * @param [in] animatedImageLoading The AnimatedImageLoading to load animated image
51    * @param [in] frameIndex The frame index of a frame to be loaded frame
52    */
53   LoadingTask( uint32_t id,
54                Dali::AnimatedImageLoading animatedImageLoading,
55                uint32_t frameIndex );
56
57   /**
58    * Constructor.
59    * @param [in] id of the task
60    * @param [in] url The URL of the image file to load.
61    * @param [in] size The width and height to fit the loaded image to, 0.0 means whole image
62    * @param [in] fittingMode The method used to fit the shape of the image before loading to the shape defined by the size parameter.
63    * @param [in] samplingMode The filtering method used when sampling pixels from the input image while fitting it to desired size.
64    * @param [in] orientationCorrection Reorient the image to respect any orientation metadata in its header.
65    * @param [in] preMultiplyOnLoad ON if the image's color should be multiplied by it's alpha. Set to OFF if there is no alpha or if the image need to be applied alpha mask.
66    */
67   LoadingTask( uint32_t id,
68                const VisualUrl& url,
69                ImageDimensions dimensions,
70                FittingMode::Type fittingMode,
71                SamplingMode::Type samplingMode,
72                bool orientationCorrection,
73                DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad );
74
75   /**
76    * Constructor.
77    * @param [in] id of the task
78    * @param [in] pixelBuffer of the to be masked image
79    * @param [in] maskPixelBuffer of the mask image
80    * @param [in] contentScale The factor to scale the content
81    * @param [in] cropToMask Whether to crop the content to the mask size
82    * @param [in] preMultiplyOnLoad ON if the image's color should be multiplied by it's alpha. Set to OFF if there is no alpha.
83    */
84   LoadingTask( uint32_t id,
85                Devel::PixelBuffer pixelBuffer,
86                Devel::PixelBuffer maskPixelBuffer,
87                float contentScale,
88                bool cropToMask,
89                DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad );
90
91   /**
92    * Load the image
93    */
94   void Load();
95
96   /**
97    * Apply mask
98    */
99   void ApplyMask();
100
101   /**
102    * Multiply alpha
103    */
104   void MultiplyAlpha();
105
106 private:
107
108   // Undefined
109   LoadingTask( const LoadingTask& queue );
110
111   // Undefined
112   LoadingTask& operator=( const LoadingTask& queue );
113
114 public:
115
116   Devel::PixelBuffer pixelBuffer;   ///< pixelBuffer handle after successful load
117                                     ///< or pixelBuffer to be masked image in the mask task
118   VisualUrl          url;           ///< url of the image to load
119   uint32_t           id;            ///< The unique id associated with this task.
120   ImageDimensions    dimensions;    ///< dimensions to load
121   FittingMode::Type  fittingMode;   ///< fitting options
122   SamplingMode::Type samplingMode;  ///< sampling options
123   bool               orientationCorrection:1; ///< if orientation correction is needed
124   DevelAsyncImageLoader::PreMultiplyOnLoad            preMultiplyOnLoad; //< if the image's color should be multiplied by it's alpha
125
126   bool isMaskTask;                  ///< whether this task is for mask or not
127   Devel::PixelBuffer maskPixelBuffer; ///< pixelBuffer of mask image
128   float contentScale;               ///< The factor to scale the content
129   bool cropToMask;                  ///< Whether to crop the content to the mask size
130   Dali::AnimatedImageLoading animatedImageLoading;
131   uint32_t frameIndex;
132 };
133
134
135 /**
136  * The worker thread for image loading.
137  */
138 class ImageLoadThread : public Thread
139 {
140 public:
141
142   /**
143    * Constructor.
144    *
145    * @param[in] mTrigger The trigger to wake up the main thread.
146    */
147   ImageLoadThread( EventThreadCallback* mTrigger );
148
149   /**
150    * Destructor.
151    */
152   ~ImageLoadThread() override;
153
154   /**
155    * Add a task in to the loading queue
156    *
157    * @param[in] task The task added to the queue.
158    *
159    * @note This class takes ownership of the task object
160    */
161   void AddTask( LoadingTask* task );
162
163   /**
164    * Pop the next task out from the completed queue.
165    *
166    * @return The next task to be processed.
167    */
168   LoadingTask* NextCompletedTask();
169
170   /**
171    * Remove the loading task from the waiting queue.
172    */
173   bool CancelTask( uint32_t loadingTaskId );
174
175   /**
176    * Remove all the loading tasks in the waiting queue.
177    */
178   void CancelAll();
179
180 private:
181
182   /**
183    * Pop the next loading task out from the queue to process.
184    *
185    * @return The next task to be processed.
186    */
187   LoadingTask* NextTaskToProcess();
188
189   /**
190    * Add a task in to the loading queue
191    *
192    * @param[in] task The task added to the queue.
193    */
194   void AddCompletedTask( LoadingTask* task );
195
196 protected:
197
198   /**
199    * The entry function of the worker thread.
200    * It fetches loading task from the loadQueue, loads the image and adds to the completeQueue.
201    */
202   void Run() override;
203
204 private:
205
206   // Undefined
207   ImageLoadThread( const ImageLoadThread& thread );
208
209   // Undefined
210   ImageLoadThread& operator=( const ImageLoadThread& thread );
211
212 private:
213
214   Vector< LoadingTask* > mLoadQueue;     ///<The task queue with images for loading.
215   Vector< LoadingTask* > mCompleteQueue; ///<The task queue with images loaded.
216   EventThreadCallback*   mTrigger;
217   const Dali::LogFactoryInterface& mLogFactory; ///< The log factory
218
219   ConditionalWait        mConditionalWait;
220   Dali::Mutex            mMutex;
221 };
222
223 } // namespace Internal
224
225 } // namespace Toolkit
226
227 } // namespace Dali
228
229 #endif // DALI_TOOLKIT_IMAGE_LOAD_THREAD_H