[AT-SPI] enhance SHOWING state decision logic
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / image-loader / async-image-loader-impl.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include "async-image-loader-impl.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/adaptor-framework/adaptor.h>
23
24 // INTERNAL INCLUDES
25 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
26
27 namespace Dali
28 {
29 namespace Toolkit
30 {
31 namespace Internal
32 {
33 AsyncImageLoader::AsyncImageLoader()
34 : mLoadedSignal(),
35   mLoadThread(new EventThreadCallback(MakeCallback(this, &AsyncImageLoader::ProcessLoadedImage))),
36   mLoadTaskId(0u),
37   mIsLoadThreadStarted(false)
38 {
39 }
40
41 AsyncImageLoader::~AsyncImageLoader()
42 {
43   mLoadThread.CancelAll();
44 }
45
46 IntrusivePtr<AsyncImageLoader> AsyncImageLoader::New()
47 {
48   IntrusivePtr<AsyncImageLoader> internal = new AsyncImageLoader();
49   return internal;
50 }
51
52 uint32_t AsyncImageLoader::LoadAnimatedImage(Dali::AnimatedImageLoading animatedImageLoading,
53                                              uint32_t                   frameIndex)
54 {
55   if(!mIsLoadThreadStarted)
56   {
57     mLoadThread.Start();
58     mIsLoadThreadStarted = true;
59   }
60   mLoadThread.AddTask(new LoadingTask(++mLoadTaskId, animatedImageLoading, frameIndex));
61
62   return mLoadTaskId;
63 }
64
65 uint32_t AsyncImageLoader::Load(const VisualUrl&                         url,
66                                 ImageDimensions                          dimensions,
67                                 FittingMode::Type                        fittingMode,
68                                 SamplingMode::Type                       samplingMode,
69                                 bool                                     orientationCorrection,
70                                 DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad)
71 {
72   if(!mIsLoadThreadStarted)
73   {
74     mLoadThread.Start();
75     mIsLoadThreadStarted = true;
76   }
77   if(url.IsBufferResource())
78   {
79     auto visualFactory = Toolkit::VisualFactory::Get();
80     if(visualFactory)
81     {
82       // Get EncodedImageBuffer from texturemanager
83       // and make new LoadingTask with buffer
84       auto& textureManager = GetImplementation(visualFactory).GetTextureManager();
85
86       const EncodedImageBuffer& encodedBuffer = textureManager.GetEncodedImageBuffer(url.GetUrl());
87
88       mLoadThread.AddTask(new LoadingTask(++mLoadTaskId, encodedBuffer, dimensions, fittingMode, samplingMode, orientationCorrection, preMultiplyOnLoad));
89     }
90   }
91   else
92   {
93     mLoadThread.AddTask(new LoadingTask(++mLoadTaskId, url, dimensions, fittingMode, samplingMode, orientationCorrection, preMultiplyOnLoad));
94   }
95
96   return mLoadTaskId;
97 }
98
99 uint32_t AsyncImageLoader::ApplyMask(Devel::PixelBuffer                       pixelBuffer,
100                                      Devel::PixelBuffer                       maskPixelBuffer,
101                                      float                                    contentScale,
102                                      bool                                     cropToMask,
103                                      DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad)
104 {
105   if(!mIsLoadThreadStarted)
106   {
107     mLoadThread.Start();
108     mIsLoadThreadStarted = true;
109   }
110   mLoadThread.AddTask(new LoadingTask(++mLoadTaskId, pixelBuffer, maskPixelBuffer, contentScale, cropToMask, preMultiplyOnLoad));
111
112   return mLoadTaskId;
113 }
114
115 Toolkit::AsyncImageLoader::ImageLoadedSignalType& AsyncImageLoader::ImageLoadedSignal()
116 {
117   return mLoadedSignal;
118 }
119
120 Toolkit::DevelAsyncImageLoader::PixelBufferLoadedSignalType& AsyncImageLoader::PixelBufferLoadedSignal()
121 {
122   return mPixelBufferLoadedSignal;
123 }
124
125 bool AsyncImageLoader::Cancel(uint32_t loadingTaskId)
126 {
127   return mLoadThread.CancelTask(loadingTaskId);
128 }
129
130 void AsyncImageLoader::CancelAll()
131 {
132   mLoadThread.CancelAll();
133 }
134
135 void AsyncImageLoader::ProcessLoadedImage()
136 {
137   while(LoadingTask* next = mLoadThread.NextCompletedTask())
138   {
139     if(mPixelBufferLoadedSignal.GetConnectionCount() > 0)
140     {
141       mPixelBufferLoadedSignal.Emit(next->id, next->pixelBuffer);
142     }
143     else if(mLoadedSignal.GetConnectionCount() > 0)
144     {
145       PixelData pixelData;
146       if(next->pixelBuffer)
147       {
148         pixelData = Devel::PixelBuffer::Convert(next->pixelBuffer);
149       }
150       mLoadedSignal.Emit(next->id, pixelData);
151     }
152
153     delete next;
154   }
155 }
156
157 } // namespace Internal
158
159 } // namespace Toolkit
160
161 } // namespace Dali