2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "async-image-loader-impl.h"
22 #include <dali/integration-api/adaptor-framework/adaptor.h>
25 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
33 AsyncImageLoader::AsyncImageLoader()
35 mLoadThread(new EventThreadCallback(MakeCallback(this, &AsyncImageLoader::ProcessLoadedImage))),
37 mIsLoadThreadStarted(false)
41 AsyncImageLoader::~AsyncImageLoader()
43 mLoadThread.CancelAll();
46 IntrusivePtr<AsyncImageLoader> AsyncImageLoader::New()
48 IntrusivePtr<AsyncImageLoader> internal = new AsyncImageLoader();
52 uint32_t AsyncImageLoader::LoadAnimatedImage(Dali::AnimatedImageLoading animatedImageLoading,
55 if(!mIsLoadThreadStarted)
58 mIsLoadThreadStarted = true;
60 mLoadThread.AddTask(new LoadingTask(++mLoadTaskId, animatedImageLoading, frameIndex));
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)
72 if(!mIsLoadThreadStarted)
75 mIsLoadThreadStarted = true;
77 if(url.IsBufferResource())
79 auto visualFactory = Toolkit::VisualFactory::Get();
82 // Get EncodedImageBuffer from texturemanager
83 // and make new LoadingTask with buffer
84 auto& textureManager = GetImplementation(visualFactory).GetTextureManager();
86 const EncodedImageBuffer& encodedBuffer = textureManager.GetEncodedImageBuffer(url.GetUrl());
88 mLoadThread.AddTask(new LoadingTask(++mLoadTaskId, encodedBuffer, dimensions, fittingMode, samplingMode, orientationCorrection, preMultiplyOnLoad));
93 mLoadThread.AddTask(new LoadingTask(++mLoadTaskId, url, dimensions, fittingMode, samplingMode, orientationCorrection, preMultiplyOnLoad));
99 uint32_t AsyncImageLoader::ApplyMask(Devel::PixelBuffer pixelBuffer,
100 Devel::PixelBuffer maskPixelBuffer,
103 DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad)
105 if(!mIsLoadThreadStarted)
108 mIsLoadThreadStarted = true;
110 mLoadThread.AddTask(new LoadingTask(++mLoadTaskId, pixelBuffer, maskPixelBuffer, contentScale, cropToMask, preMultiplyOnLoad));
115 Toolkit::AsyncImageLoader::ImageLoadedSignalType& AsyncImageLoader::ImageLoadedSignal()
117 return mLoadedSignal;
120 Toolkit::DevelAsyncImageLoader::PixelBufferLoadedSignalType& AsyncImageLoader::PixelBufferLoadedSignal()
122 return mPixelBufferLoadedSignal;
125 bool AsyncImageLoader::Cancel(uint32_t loadingTaskId)
127 return mLoadThread.CancelTask(loadingTaskId);
130 void AsyncImageLoader::CancelAll()
132 mLoadThread.CancelAll();
135 void AsyncImageLoader::ProcessLoadedImage()
137 while(LoadingTask* next = mLoadThread.NextCompletedTask())
139 if(mPixelBufferLoadedSignal.GetConnectionCount() > 0)
141 mPixelBufferLoadedSignal.Emit(next->id, next->pixelBuffer);
143 else if(mLoadedSignal.GetConnectionCount() > 0)
146 if(next->pixelBuffer)
148 pixelData = Devel::PixelBuffer::Convert(next->pixelBuffer);
150 mLoadedSignal.Emit(next->id, pixelData);
157 } // namespace Internal
159 } // namespace Toolkit