2 * Copyright (c) 2023 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 <dali-toolkit/internal/texture-manager/texture-manager-impl.h>
22 #include <dali/devel-api/adaptor-framework/image-loading.h>
23 #include <dali/devel-api/adaptor-framework/pixel-buffer.h>
24 #include <dali/integration-api/adaptor-framework/adaptor.h>
25 #include <dali/integration-api/debug.h>
26 #include <dali/public-api/rendering/geometry.h>
29 #include <dali-toolkit/internal/texture-manager/texture-async-loading-helper.h>
30 #include <dali-toolkit/internal/texture-manager/texture-cache-manager.h>
31 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
32 #include <dali-toolkit/internal/visuals/rendering-addon.h>
36 constexpr auto INITIAL_HASH_NUMBER = size_t{0u};
38 constexpr auto TEXTURE_INDEX = 0u; ///< The Index for texture
39 constexpr auto MASK_TEXTURE_INDEX = 1u; ///< The Index for mask texture
49 // Define logfilter Internal namespace level so other files (e.g. texture-cache-manager.cpp) can also use this filter.
50 Debug::Filter* gTextureManagerLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_TEXTURE_MANAGER");
53 #define GET_LOAD_STATE_STRING(loadState) \
54 loadState == TextureManagerType::LoadState::NOT_STARTED ? "NOT_STARTED" : \
55 loadState == TextureManagerType::LoadState::LOADING ? "LOADING" : \
56 loadState == TextureManagerType::LoadState::LOAD_FINISHED ? "LOAD_FINISHED" : \
57 loadState == TextureManagerType::LoadState::WAITING_FOR_MASK ? "WAITING_FOR_MASK" : \
58 loadState == TextureManagerType::LoadState::MASK_APPLYING ? "MASK_APPLYING" : \
59 loadState == TextureManagerType::LoadState::MASK_APPLIED ? "MASK_APPLIED" : \
60 loadState == TextureManagerType::LoadState::UPLOADED ? "UPLOADED" : \
61 loadState == TextureManagerType::LoadState::CANCELLED ? "CANCELLED" : \
62 loadState == TextureManagerType::LoadState::MASK_CANCELLED ? "MASK_CANCELLED" : \
63 loadState == TextureManagerType::LoadState::LOAD_FAILED ? "LOAD_FAILED" : \
70 const uint32_t DEFAULT_ATLAS_SIZE(1024u); ///< This size can fit 8 by 8 images of average size 128 * 128
71 const Vector4 FULL_ATLAS_RECT(0.0f, 0.0f, 1.0f, 1.0f); ///< UV Rectangle that covers the full Texture
73 void PreMultiply(Devel::PixelBuffer pixelBuffer, TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
75 if(Pixel::HasAlpha(pixelBuffer.GetPixelFormat()))
77 if(preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD)
79 pixelBuffer.MultiplyColorByAlpha();
84 preMultiplyOnLoad = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
88 } // Anonymous namespace
90 TextureManager::MaskingData::MaskingData()
92 mAlphaMaskId(INVALID_TEXTURE_ID),
93 mContentScaleFactor(1.0f),
95 mPreappliedMasking(true),
96 mMaskImageLoadingFailed(false)
100 TextureManager::TextureManager(bool loadYuvPlanes)
101 : mTextureCacheManager(),
102 mAsyncLoader(std::unique_ptr<TextureAsyncLoadingHelper>(new TextureAsyncLoadingHelper(*this))),
103 mLifecycleObservers(),
105 mLoadingQueueTextureId(INVALID_TEXTURE_ID),
107 mLoadYuvPlanes(loadYuvPlanes),
108 mRemoveProcessorRegistered(false)
110 // Initialize the AddOn
111 RenderingAddOn::Get();
114 TextureManager::~TextureManager()
116 if(mRemoveProcessorRegistered && Adaptor::IsAvailable())
118 Adaptor::Get().UnregisterProcessor(*this, true);
119 mRemoveProcessorRegistered = false;
122 for(auto iter = mLifecycleObservers.Begin(), endIter = mLifecycleObservers.End(); iter != endIter; ++iter)
124 (*iter)->TextureManagerDestroyed();
128 TextureSet TextureManager::LoadAnimatedImageTexture(
129 const VisualUrl& url,
130 Dali::AnimatedImageLoading animatedImageLoading,
131 const uint32_t& frameIndex,
132 TextureManager::TextureId& textureId,
133 MaskingDataPointer& maskInfo,
134 const Dali::ImageDimensions& desiredSize,
135 const Dali::FittingMode::Type& fittingMode,
136 const Dali::SamplingMode::Type& samplingMode,
137 const bool& synchronousLoading,
138 TextureUploadObserver* textureObserver,
139 TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
141 TextureSet textureSet;
143 if(synchronousLoading)
145 Devel::PixelBuffer pixelBuffer;
146 if(animatedImageLoading)
148 pixelBuffer = animatedImageLoading.LoadFrame(frameIndex, desiredSize, fittingMode, samplingMode);
152 DALI_LOG_ERROR("TextureManager::LoadAnimatedImageTexture: Synchronous loading is failed\n");
157 if(maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
159 Devel::PixelBuffer maskPixelBuffer = LoadImageFromFile(maskInfo->mAlphaMaskUrl.GetUrl(), desiredSize, fittingMode, samplingMode, true);
162 if(!maskInfo->mPreappliedMasking)
164 PixelData maskPixelData = Devel::PixelBuffer::Convert(maskPixelBuffer); // takes ownership of buffer
165 maskTexture = Texture::New(Dali::TextureType::TEXTURE_2D, maskPixelData.GetPixelFormat(), maskPixelData.GetWidth(), maskPixelData.GetHeight());
166 maskTexture.Upload(maskPixelData);
170 pixelBuffer.ApplyMask(maskPixelBuffer, maskInfo->mContentScaleFactor, maskInfo->mCropToMask);
175 DALI_LOG_ERROR("TextureManager::LoadAnimatedImageTexture: Synchronous mask image loading is failed\n");
179 if(preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD)
181 PreMultiply(pixelBuffer, preMultiplyOnLoad);
184 PixelData pixelData = Devel::PixelBuffer::Convert(pixelBuffer); // takes ownership of buffer
187 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D, pixelData.GetPixelFormat(), pixelData.GetWidth(), pixelData.GetHeight());
188 texture.Upload(pixelData);
189 textureSet = TextureSet::New();
190 textureSet.SetTexture(TEXTURE_INDEX, texture);
193 textureSet.SetTexture(MASK_TEXTURE_INDEX, maskTexture);
200 TextureId alphaMaskId = INVALID_TEXTURE_ID;
201 float contentScaleFactor = 1.0f;
202 bool cropToMask = false;
203 if(maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
205 maskInfo->mAlphaMaskId = RequestMaskLoad(maskInfo->mAlphaMaskUrl, maskInfo->mPreappliedMasking ? StorageType::KEEP_PIXEL_BUFFER : StorageType::KEEP_TEXTURE);
206 alphaMaskId = maskInfo->mAlphaMaskId;
207 if(maskInfo->mPreappliedMasking)
209 contentScaleFactor = maskInfo->mContentScaleFactor;
210 cropToMask = maskInfo->mCropToMask;
214 textureId = RequestLoadInternal(url, alphaMaskId, textureId, contentScaleFactor, desiredSize, fittingMode, samplingMode, UseAtlas::NO_ATLAS, cropToMask, StorageType::UPLOAD_TO_TEXTURE, textureObserver, true, TextureManager::ReloadPolicy::CACHED, preMultiplyOnLoad, animatedImageLoading, frameIndex, false);
216 TextureManager::LoadState loadState = mTextureCacheManager.GetTextureStateInternal(textureId);
217 if(loadState == TextureManager::LoadState::UPLOADED)
219 // LoadComplete has already been called - keep the same texture set
220 textureSet = GetTextureSet(textureId);
227 Devel::PixelBuffer TextureManager::LoadPixelBuffer(
228 const VisualUrl& url,
229 const Dali::ImageDimensions& desiredSize,
230 const Dali::FittingMode::Type& fittingMode,
231 const Dali::SamplingMode::Type& samplingMode,
232 const bool& synchronousLoading,
233 TextureUploadObserver* textureObserver,
234 const bool& orientationCorrection,
235 TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
237 Devel::PixelBuffer pixelBuffer;
238 if(synchronousLoading)
242 if(url.IsBufferResource())
244 const EncodedImageBuffer& encodedImageBuffer = mTextureCacheManager.GetEncodedImageBuffer(url);
245 if(encodedImageBuffer)
247 pixelBuffer = LoadImageFromBuffer(encodedImageBuffer.GetRawBuffer(), desiredSize, fittingMode, samplingMode, orientationCorrection);
252 pixelBuffer = LoadImageFromFile(url.GetUrl(), desiredSize, fittingMode, samplingMode, orientationCorrection);
254 if(pixelBuffer && preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD)
256 PreMultiply(pixelBuffer, preMultiplyOnLoad);
262 RequestLoadInternal(url, INVALID_TEXTURE_ID, INVALID_TEXTURE_ID, 1.0f, desiredSize, fittingMode, samplingMode, UseAtlas::NO_ATLAS, false, StorageType::RETURN_PIXEL_BUFFER, textureObserver, orientationCorrection, TextureManager::ReloadPolicy::FORCED, preMultiplyOnLoad, Dali::AnimatedImageLoading(), 0u, false);
268 TextureSet TextureManager::LoadTexture(
269 const VisualUrl& url,
270 const Dali::ImageDimensions& desiredSize,
271 const Dali::FittingMode::Type& fittingMode,
272 const Dali::SamplingMode::Type& samplingMode,
273 MaskingDataPointer& maskInfo,
274 const bool& synchronousLoading,
275 TextureManager::TextureId& textureId,
276 Vector4& textureRect,
277 Dali::ImageDimensions& textureRectSize,
278 bool& atlasingStatus,
280 TextureUploadObserver* textureObserver,
281 AtlasUploadObserver* atlasObserver,
282 ImageAtlasManagerPtr imageAtlasManager,
283 const bool& orientationCorrection,
284 const TextureManager::ReloadPolicy& reloadPolicy,
285 TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
287 TextureSet textureSet;
289 loadingStatus = false;
290 textureRect = FULL_ATLAS_RECT;
292 if(VisualUrl::TEXTURE == url.GetProtocolType())
294 std::string location = url.GetLocation();
295 if(location.size() > 0u)
297 TextureId id = std::stoi(location);
298 auto externalTextureInfo = mTextureCacheManager.GetExternalTextureInfo(id);
299 if(externalTextureInfo.textureSet)
301 if(preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD)
303 // Change preMultiplyOnLoad value so make caller determine to preMultiplyAlpha or not.
304 // TODO : Should we seperate input and output value?
305 preMultiplyOnLoad = externalTextureInfo.preMultiplied ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
308 TextureId alphaMaskId = INVALID_TEXTURE_ID;
309 if(maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
311 maskInfo->mAlphaMaskId = RequestMaskLoad(maskInfo->mAlphaMaskUrl, StorageType::KEEP_TEXTURE, synchronousLoading);
312 alphaMaskId = maskInfo->mAlphaMaskId;
314 // Create new textureId. this textureId is not same as location
315 textureId = RequestLoad(url, alphaMaskId, textureId, 1.0f, desiredSize, fittingMode, samplingMode, UseAtlas::NO_ATLAS, false, textureObserver, orientationCorrection, reloadPolicy, preMultiplyOnLoad, synchronousLoading);
317 TextureManager::LoadState loadState = mTextureCacheManager.GetTextureStateInternal(textureId);
318 if(loadState == TextureManager::LoadState::UPLOADED)
320 textureSet = GetTextureSet(textureId);
325 // TextureId is same as location
328 textureSet = TextureSet::New();
329 textureSet.SetTexture(TEXTURE_INDEX, externalTextureInfo.textureSet.GetTexture(TEXTURE_INDEX));
337 if(synchronousLoading && atlasingStatus)
339 const bool synchronousAtlasAvaliable = (desiredSize != ImageDimensions() || url.IsLocalResource()) ? imageAtlasManager->CheckAtlasAvailable(url, desiredSize)
341 if(synchronousAtlasAvaliable)
343 std::vector<Devel::PixelBuffer> pixelBuffers;
344 LoadImageSynchronously(url, desiredSize, fittingMode, samplingMode, orientationCorrection, false, pixelBuffers);
346 if(!pixelBuffers.empty() && maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
348 std::vector<Devel::PixelBuffer> maskPixelBuffers;
349 LoadImageSynchronously(maskInfo->mAlphaMaskUrl, ImageDimensions(), FittingMode::SCALE_TO_FILL, SamplingMode::NO_FILTER, true, false, maskPixelBuffers);
350 if(!maskPixelBuffers.empty())
352 pixelBuffers[0].ApplyMask(maskPixelBuffers[0], maskInfo->mContentScaleFactor, maskInfo->mCropToMask);
357 if(!pixelBuffers.empty())
359 PreMultiply(pixelBuffers[0], preMultiplyOnLoad);
360 data = Devel::PixelBuffer::Convert(pixelBuffers[0]); // takes ownership of buffer
364 textureSet = imageAtlasManager->Add(textureRect, data);
367 textureRectSize.SetWidth(data.GetWidth());
368 textureRectSize.SetHeight(data.GetHeight());
373 DALI_LOG_ERROR("TextureManager::LoadTexture: Synchronous Texture loading with atlasing is failed.\n");
378 atlasingStatus = false;
385 loadingStatus = true;
386 // Atlas manager can chage desired size when it is set by 0,0.
387 // We should store into textureRectSize only if atlasing successed.
388 // So copy inputed desiredSize, and replace value into textureRectSize only if atlasing success.
389 Dali::ImageDimensions atlasDesiredSize = desiredSize;
392 if(url.IsBufferResource())
394 const EncodedImageBuffer& encodedImageBuffer = GetEncodedImageBuffer(url.GetUrl());
395 if(encodedImageBuffer)
397 textureSet = imageAtlasManager->Add(textureRect, encodedImageBuffer, desiredSize, fittingMode, true, atlasObserver);
402 textureSet = imageAtlasManager->Add(textureRect, url, atlasDesiredSize, fittingMode, true, atlasObserver);
405 if(!textureSet) // big image, no atlasing or atlasing failed
407 atlasingStatus = false;
409 TextureId alphaMaskId = INVALID_TEXTURE_ID;
410 float contentScaleFactor = 1.0f;
411 bool cropToMask = false;
412 if(maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
414 maskInfo->mAlphaMaskId = RequestMaskLoad(maskInfo->mAlphaMaskUrl, maskInfo->mPreappliedMasking ? StorageType::KEEP_PIXEL_BUFFER : StorageType::KEEP_TEXTURE, synchronousLoading);
415 alphaMaskId = maskInfo->mAlphaMaskId;
416 if(maskInfo && maskInfo->mPreappliedMasking)
418 contentScaleFactor = maskInfo->mContentScaleFactor;
419 cropToMask = maskInfo->mCropToMask;
423 textureId = RequestLoad(
434 orientationCorrection,
439 TextureManager::LoadState loadState = mTextureCacheManager.GetTextureStateInternal(textureId);
440 if(loadState == TextureManager::LoadState::UPLOADED)
442 // LoadComplete has already been called - keep the same texture set
443 textureSet = GetTextureSet(textureId);
446 // If we are loading the texture, or waiting for the ready signal handler to complete, inform
447 // caller that they need to wait.
448 loadingStatus = (loadState == TextureManager::LoadState::LOADING ||
449 loadState == TextureManager::LoadState::WAITING_FOR_MASK ||
450 loadState == TextureManager::LoadState::MASK_APPLYING ||
451 loadState == TextureManager::LoadState::MASK_APPLIED ||
452 loadState == TextureManager::LoadState::NOT_STARTED ||
453 mLoadingQueueTextureId != INVALID_TEXTURE_ID);
457 textureRectSize = atlasDesiredSize;
462 if(synchronousLoading)
464 loadingStatus = false;
470 TextureManager::TextureId TextureManager::RequestLoad(
471 const VisualUrl& url,
472 const ImageDimensions& desiredSize,
473 const Dali::FittingMode::Type& fittingMode,
474 const Dali::SamplingMode::Type& samplingMode,
475 const UseAtlas& useAtlas,
476 TextureUploadObserver* observer,
477 const bool& orientationCorrection,
478 const TextureManager::ReloadPolicy& reloadPolicy,
479 TextureManager::MultiplyOnLoad& preMultiplyOnLoad,
480 const bool& synchronousLoading)
482 return RequestLoadInternal(url, INVALID_TEXTURE_ID, INVALID_TEXTURE_ID, 1.0f, desiredSize, fittingMode, samplingMode, useAtlas, false, StorageType::UPLOAD_TO_TEXTURE, observer, orientationCorrection, reloadPolicy, preMultiplyOnLoad, Dali::AnimatedImageLoading(), 0u, synchronousLoading);
485 TextureManager::TextureId TextureManager::RequestLoad(
486 const VisualUrl& url,
487 const TextureManager::TextureId& maskTextureId,
488 const TextureManager::TextureId& previousTextureId,
489 const float& contentScale,
490 const Dali::ImageDimensions& desiredSize,
491 const Dali::FittingMode::Type& fittingMode,
492 const Dali::SamplingMode::Type& samplingMode,
493 const TextureManager::UseAtlas& useAtlas,
494 const bool& cropToMask,
495 TextureUploadObserver* observer,
496 const bool& orientationCorrection,
497 const TextureManager::ReloadPolicy& reloadPolicy,
498 TextureManager::MultiplyOnLoad& preMultiplyOnLoad,
499 const bool& synchronousLoading)
501 return RequestLoadInternal(url, maskTextureId, previousTextureId, contentScale, desiredSize, fittingMode, samplingMode, useAtlas, cropToMask, StorageType::UPLOAD_TO_TEXTURE, observer, orientationCorrection, reloadPolicy, preMultiplyOnLoad, Dali::AnimatedImageLoading(), 0u, synchronousLoading);
504 TextureManager::TextureId TextureManager::RequestMaskLoad(
505 const VisualUrl& maskUrl,
506 StorageType storageType,
507 const bool& synchronousLoading)
509 // Use the normal load procedure to get the alpha mask.
510 auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
511 return RequestLoadInternal(maskUrl, INVALID_TEXTURE_ID, INVALID_TEXTURE_ID, 1.0f, ImageDimensions(), FittingMode::SCALE_TO_FILL, SamplingMode::NO_FILTER, UseAtlas::NO_ATLAS, false, storageType, NULL, true, TextureManager::ReloadPolicy::CACHED, preMultiply, Dali::AnimatedImageLoading(), 0u, synchronousLoading);
514 TextureManager::TextureId TextureManager::RequestLoadInternal(
515 const VisualUrl& url,
516 const TextureManager::TextureId& maskTextureId,
517 const TextureManager::TextureId& previousTextureId,
518 const float& contentScale,
519 const Dali::ImageDimensions& desiredSize,
520 const Dali::FittingMode::Type& fittingMode,
521 const Dali::SamplingMode::Type& samplingMode,
522 const TextureManager::UseAtlas& useAtlas,
523 const bool& cropToMask,
524 const TextureManager::StorageType& storageType,
525 TextureUploadObserver* observer,
526 const bool& orientationCorrection,
527 const TextureManager::ReloadPolicy& reloadPolicy,
528 TextureManager::MultiplyOnLoad& preMultiplyOnLoad,
529 Dali::AnimatedImageLoading animatedImageLoading,
530 const std::uint32_t& frameIndex,
531 const bool& synchronousLoading)
533 TextureHash textureHash = INITIAL_HASH_NUMBER;
534 TextureCacheIndex cacheIndex = INVALID_CACHE_INDEX;
535 bool loadYuvPlanes = (mLoadYuvPlanes && maskTextureId == INVALID_TEXTURE_ID && storageType == StorageType::UPLOAD_TO_TEXTURE);
537 if(storageType != StorageType::RETURN_PIXEL_BUFFER)
539 textureHash = mTextureCacheManager.GenerateHash(url, desiredSize, fittingMode, samplingMode, useAtlas, maskTextureId, cropToMask, frameIndex);
541 // Look up the texture by hash. Note: The extra parameters are used in case of a hash collision.
542 cacheIndex = mTextureCacheManager.FindCachedTexture(textureHash, url, desiredSize, fittingMode, samplingMode, useAtlas, storageType, maskTextureId, cropToMask, preMultiplyOnLoad, (animatedImageLoading) ? true : false, frameIndex);
545 TextureManager::TextureId textureId = INVALID_TEXTURE_ID;
546 // Check if the requested Texture exists in the cache.
547 if(cacheIndex != INVALID_CACHE_INDEX)
549 if(TextureManager::ReloadPolicy::CACHED == reloadPolicy || TextureManager::INVALID_TEXTURE_ID == previousTextureId)
551 // Mark this texture being used by another client resource, or Reload forced without request load before.
552 // Forced reload which have current texture before, would replace the current texture.
553 // without the need for incrementing the reference count.
554 ++(mTextureCacheManager[cacheIndex].referenceCount);
556 textureId = mTextureCacheManager[cacheIndex].textureId;
558 // Update preMultiplyOnLoad value. It should be changed according to preMultiplied value of the cached info.
559 preMultiplyOnLoad = mTextureCacheManager[cacheIndex].preMultiplied ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
560 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureManager::RequestLoad( url=%s observer=%p ) Using cached texture id@%d, textureId=%d, maskTextureId=%d, prevTextureId=%d, frameindex=%d, premultiplied=%d, refCount=%d\n", url.GetUrl().c_str(), observer, cacheIndex.GetIndex(), textureId, maskTextureId, previousTextureId, frameIndex, mTextureCacheManager[cacheIndex].preMultiplied ? 1 : 0, static_cast<int>(mTextureCacheManager[cacheIndex].referenceCount));
563 if(textureId == INVALID_TEXTURE_ID) // There was no caching, or caching not required
565 textureId = mTextureCacheManager.GenerateTextureId();
567 bool preMultiply = (preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD);
569 // Cache new texutre, and get cacheIndex.
570 cacheIndex = mTextureCacheManager.AppendCache(TextureInfo(textureId, maskTextureId, url, desiredSize, contentScale, fittingMode, samplingMode, false, cropToMask, useAtlas, textureHash, orientationCorrection, preMultiply, animatedImageLoading, frameIndex, loadYuvPlanes));
571 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureManager::RequestLoad( url=%s observer=%p ) New texture, cacheIndex:%d, textureId=%d, maskTextureId=%d, frameindex=%d premultiply=%d\n", url.GetUrl().c_str(), observer, cacheIndex.GetIndex(), textureId, maskTextureId, frameIndex, preMultiply);
574 // The below code path is common whether we are using the cache or not.
575 // The textureInfoIndex now refers to either a pre-existing cached TextureInfo,
576 // or a new TextureInfo just created.
577 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
578 textureInfo.maskTextureId = maskTextureId;
579 textureInfo.storageType = storageType;
580 textureInfo.orientationCorrection = orientationCorrection;
582 // the case using external texture has already been loaded texture, so change its status to WAITING_FOR_MASK.
583 if(url.GetProtocolType() == VisualUrl::TEXTURE)
585 if(textureInfo.loadState != LoadState::UPLOADED)
587 textureInfo.loadState = TextureManager::LoadState::WAITING_FOR_MASK;
591 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureInfo loadState:%s\n", GET_LOAD_STATE_STRING(textureInfo.loadState));
593 // Force reloading of texture by setting loadState unless already loading or cancelled.
594 if(TextureManager::ReloadPolicy::FORCED == reloadPolicy &&
595 TextureManager::LoadState::LOADING != textureInfo.loadState &&
596 TextureManager::LoadState::WAITING_FOR_MASK != textureInfo.loadState &&
597 TextureManager::LoadState::MASK_APPLYING != textureInfo.loadState &&
598 TextureManager::LoadState::MASK_APPLIED != textureInfo.loadState &&
599 TextureManager::LoadState::CANCELLED != textureInfo.loadState &&
600 TextureManager::LoadState::MASK_CANCELLED != textureInfo.loadState)
602 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Verbose, "TextureManager::RequestLoad( url=%s observer=%p ) ForcedReload cacheIndex:%d, textureId=%d, maskTextureId=%d, prevTextureId=%d\n", url.GetUrl().c_str(), observer, cacheIndex.GetIndex(), textureId, maskTextureId, previousTextureId);
603 textureInfo.loadState = TextureManager::LoadState::NOT_STARTED;
606 if(!synchronousLoading)
608 // Check if we should add the observer.
609 // Only do this if we have not loaded yet and it will not have loaded by the end of this method.
610 switch(textureInfo.loadState)
612 case TextureManager::LoadState::LOAD_FAILED: // Failed notifies observer which then stops observing.
613 case TextureManager::LoadState::NOT_STARTED:
615 LoadOrQueueTexture(textureInfo, observer); // If called inside NotifyObservers, queues until afterwards
618 case TextureManager::LoadState::LOADING:
619 case TextureManager::LoadState::WAITING_FOR_MASK:
620 case TextureManager::LoadState::MASK_APPLYING:
621 case TextureManager::LoadState::MASK_APPLIED:
623 // Do not observe even we reload forced when texture is already loading state.
624 if(TextureManager::ReloadPolicy::CACHED == reloadPolicy || TextureManager::INVALID_TEXTURE_ID == previousTextureId)
626 ObserveTexture(textureInfo, observer);
630 case TextureManager::LoadState::UPLOADED:
634 LoadOrQueueTexture(textureInfo, observer);
638 case TextureManager::LoadState::CANCELLED:
640 // A cancelled texture hasn't finished loading yet. Treat as a loading texture
641 // (it's ref count has already been incremented, above)
642 textureInfo.loadState = TextureManager::LoadState::LOADING;
643 ObserveTexture(textureInfo, observer);
646 case TextureManager::LoadState::MASK_CANCELLED:
648 // A cancelled texture hasn't finished mask applying yet. Treat as a mask applying texture
649 // (it's ref count has already been incremented, above)
650 textureInfo.loadState = TextureManager::LoadState::MASK_APPLYING;
651 ObserveTexture(textureInfo, observer);
654 case TextureManager::LoadState::LOAD_FINISHED:
656 // Loading has already completed.
657 if(observer && textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER)
659 LoadOrQueueTexture(textureInfo, observer);
667 // If the image is already finished to load, use cached texture.
668 // We don't need to consider Observer because this is synchronous loading.
669 if(!(textureInfo.loadState == TextureManager::LoadState::UPLOADED ||
670 textureInfo.loadState == TextureManager::LoadState::LOAD_FINISHED))
672 if(url.GetProtocolType() == VisualUrl::TEXTURE)
674 // Get external textureSet from cacheManager.
675 std::string location = textureInfo.url.GetLocation();
676 if(!location.empty())
678 TextureId id = std::stoi(location);
679 auto externalTextureInfo = mTextureCacheManager.GetExternalTextureInfo(id);
680 textureInfo.textures.push_back(externalTextureInfo.textureSet.GetTexture(0));
681 textureInfo.loadState = LoadState::UPLOADED;
686 std::vector<Devel::PixelBuffer> pixelBuffers;
687 LoadImageSynchronously(url, desiredSize, fittingMode, samplingMode, orientationCorrection, loadYuvPlanes, pixelBuffers);
689 if(pixelBuffers.empty())
691 // If pixelBuffer loading is failed in synchronously, call RequestRemove() method.
692 RequestRemove(textureId, nullptr);
693 return INVALID_TEXTURE_ID;
696 if(storageType == StorageType::KEEP_PIXEL_BUFFER) // For the mask image loading.
698 textureInfo.pixelBuffer = pixelBuffers[0]; // Store the pixel data
699 textureInfo.loadState = LoadState::LOAD_FINISHED;
701 else // For the image loading.
704 if(maskTextureId != INVALID_TEXTURE_ID)
706 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(maskTextureId);
707 if(maskCacheIndex != INVALID_CACHE_INDEX)
709 if(mTextureCacheManager[maskCacheIndex].storageType == StorageType::KEEP_PIXEL_BUFFER)
711 Devel::PixelBuffer maskPixelBuffer = mTextureCacheManager[maskCacheIndex].pixelBuffer;
714 pixelBuffers[0].ApplyMask(maskPixelBuffer, contentScale, cropToMask);
718 DALI_LOG_ERROR("Mask image cached invalid pixel buffer!\n");
724 DALI_LOG_ERROR("Mask image is not stored in cache.\n");
727 PreMultiply(pixelBuffers[0], preMultiplyOnLoad);
730 UploadTextures(pixelBuffers, textureInfo);
739 void TextureManager::RequestRemove(const TextureManager::TextureId& textureId, TextureUploadObserver* observer)
741 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureManager::RequestRemove( textureId=%d observer=%p )\n", textureId, observer);
744 if(textureId != INVALID_TEXTURE_ID)
746 TextureCacheIndex textureCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
747 if(textureCacheIndex != INVALID_CACHE_INDEX)
751 // Remove observer from cached texture info
752 TextureInfo& textureInfo(mTextureCacheManager[textureCacheIndex]);
753 RemoveTextureObserver(textureInfo, observer);
756 mRemoveQueue.PushBack(textureId);
758 if(!mRemoveProcessorRegistered && Adaptor::IsAvailable())
760 mRemoveProcessorRegistered = true;
761 Adaptor::Get().RegisterProcessor(*this, true);
767 void TextureManager::Remove(const TextureManager::TextureId& textureId)
769 if(textureId != INVALID_TEXTURE_ID)
771 TextureCacheIndex textureCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
772 if(textureCacheIndex != INVALID_CACHE_INDEX)
774 TextureManager::TextureId maskTextureId = INVALID_TEXTURE_ID;
775 TextureInfo& textureInfo(mTextureCacheManager[textureCacheIndex]);
776 // We only need to consider maskTextureId when texture's loadState is not cancelled. Because it is already deleted.
777 if(textureInfo.loadState != LoadState::CANCELLED && textureInfo.loadState != LoadState::MASK_CANCELLED)
779 if(textureInfo.maskTextureId != INVALID_TEXTURE_ID)
781 maskTextureId = textureInfo.maskTextureId;
785 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureManager::Remove( textureId=%d ) cacheIndex:%d removal maskTextureId=%d, loadState=%s\n", textureId, textureCacheIndex.GetIndex(), maskTextureId, GET_LOAD_STATE_STRING(textureInfo.loadState));
787 // Remove textureId in CacheManager. Now, textureInfo is invalidate.
788 mTextureCacheManager.RemoveCache(textureInfo);
790 // Remove maskTextureId in CacheManager
791 if(maskTextureId != INVALID_TEXTURE_ID)
793 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(maskTextureId);
794 if(maskCacheIndex != INVALID_CACHE_INDEX)
796 TextureInfo& maskTextureInfo(mTextureCacheManager[maskCacheIndex]);
798 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureManager::Remove mask texture( maskTextureId=%d ) cacheIndex:%d, loadState=%s\n", maskTextureId, maskCacheIndex.GetIndex(), GET_LOAD_STATE_STRING(maskTextureInfo.loadState));
800 mTextureCacheManager.RemoveCache(maskTextureInfo);
807 void TextureManager::ProcessRemoveQueue()
809 // Note that RemoveQueue is not be changed during Remove().
810 for(auto&& textureId : mRemoveQueue)
812 if(textureId != INVALID_TEXTURE_ID)
817 mRemoveQueue.Clear();
820 void TextureManager::Process(bool postProcessor)
822 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureManager::Process()\n");
824 ProcessRemoveQueue();
826 if(Adaptor::IsAvailable())
828 Adaptor::Get().UnregisterProcessor(*this, true);
829 mRemoveProcessorRegistered = false;
833 void TextureManager::LoadImageSynchronously(
834 const VisualUrl& url,
835 const Dali::ImageDimensions& desiredSize,
836 const Dali::FittingMode::Type& fittingMode,
837 const Dali::SamplingMode::Type& samplingMode,
838 const bool& orientationCorrection,
839 const bool& loadYuvPlanes,
840 std::vector<Devel::PixelBuffer>& pixelBuffers)
842 Devel::PixelBuffer pixelBuffer;
843 if(url.IsBufferResource())
845 const EncodedImageBuffer& encodedImageBuffer = mTextureCacheManager.GetEncodedImageBuffer(url);
846 if(encodedImageBuffer)
848 pixelBuffer = LoadImageFromBuffer(encodedImageBuffer.GetRawBuffer(), desiredSize, fittingMode, samplingMode, orientationCorrection);
855 Dali::LoadImagePlanesFromFile(url.GetUrl(), pixelBuffers, desiredSize, fittingMode, samplingMode, orientationCorrection);
859 pixelBuffer = Dali::LoadImageFromFile(url.GetUrl(), desiredSize, fittingMode, samplingMode, orientationCorrection);
865 pixelBuffers.push_back(pixelBuffer);
869 void TextureManager::AddObserver(TextureManager::LifecycleObserver& observer)
871 // make sure an observer doesn't observe the same object twice
872 // otherwise it will get multiple calls to ObjectDestroyed()
873 DALI_ASSERT_DEBUG(mLifecycleObservers.End() == std::find(mLifecycleObservers.Begin(), mLifecycleObservers.End(), &observer));
874 mLifecycleObservers.PushBack(&observer);
877 void TextureManager::RemoveObserver(TextureManager::LifecycleObserver& observer)
879 // Find the observer...
880 auto endIter = mLifecycleObservers.End();
881 for(auto iter = mLifecycleObservers.Begin(); iter != endIter; ++iter)
883 if((*iter) == &observer)
885 mLifecycleObservers.Erase(iter);
889 DALI_ASSERT_DEBUG(endIter != mLifecycleObservers.End());
892 void TextureManager::LoadOrQueueTexture(TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
894 switch(textureInfo.loadState)
896 case LoadState::NOT_STARTED:
897 case LoadState::LOAD_FAILED:
899 if(mLoadingQueueTextureId != INVALID_TEXTURE_ID)
901 QueueLoadTexture(textureInfo, observer);
905 LoadTexture(textureInfo, observer);
909 case LoadState::UPLOADED:
911 if(mLoadingQueueTextureId != INVALID_TEXTURE_ID)
913 QueueLoadTexture(textureInfo, observer);
917 // The Texture has already loaded. The other observers have already been notified.
918 // We need to send a "late" loaded notification for this observer.
921 EmitLoadComplete(observer, textureInfo, true);
926 case LoadState::LOADING:
927 case LoadState::CANCELLED:
928 case LoadState::MASK_CANCELLED:
929 case LoadState::LOAD_FINISHED:
930 case LoadState::WAITING_FOR_MASK:
931 case LoadState::MASK_APPLYING:
932 case LoadState::MASK_APPLIED:
939 void TextureManager::QueueLoadTexture(const TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
941 const auto& textureId = textureInfo.textureId;
942 mLoadQueue.PushBack(QueueElement(textureId, observer));
946 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Verbose, " Connect DestructionSignal to observer:%p\n", observer);
947 observer->DestructionSignal().Connect(this, &TextureManager::ObserverDestroyed);
951 void TextureManager::LoadTexture(TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
953 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::LoadTexture(): url:%s sync:%s\n", textureInfo.url.GetUrl().c_str(), textureInfo.loadSynchronously ? "T" : "F");
954 textureInfo.loadState = LoadState::LOADING;
955 if(!textureInfo.loadSynchronously)
957 auto premultiplyOnLoad = (textureInfo.preMultiplyOnLoad && textureInfo.maskTextureId == INVALID_TEXTURE_ID) ? DevelAsyncImageLoader::PreMultiplyOnLoad::ON : DevelAsyncImageLoader::PreMultiplyOnLoad::OFF;
958 if(textureInfo.animatedImageLoading)
960 mAsyncLoader->LoadAnimatedImage(textureInfo.textureId, textureInfo.animatedImageLoading, textureInfo.frameIndex, textureInfo.desiredSize, textureInfo.fittingMode, textureInfo.samplingMode, premultiplyOnLoad);
964 mAsyncLoader->Load(textureInfo.textureId, textureInfo.url, textureInfo.desiredSize, textureInfo.fittingMode, textureInfo.samplingMode, textureInfo.orientationCorrection, premultiplyOnLoad, textureInfo.loadYuvPlanes);
967 ObserveTexture(textureInfo, observer);
970 void TextureManager::ProcessLoadQueue()
972 for(auto&& element : mLoadQueue)
974 if(element.mTextureId == INVALID_TEXTURE_ID)
979 TextureCacheIndex cacheIndex = mTextureCacheManager.GetCacheIndexFromId(element.mTextureId);
980 if(cacheIndex != INVALID_CACHE_INDEX)
982 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
984 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureManager::ProcessLoadQueue() textureId=%d, observer=%p, cacheIndex=@%d, loadState:%s\n", element.mTextureId, element.mObserver, cacheIndex.GetIndex(), GET_LOAD_STATE_STRING(textureInfo.loadState));
986 if((textureInfo.loadState == LoadState::UPLOADED) || (textureInfo.loadState == LoadState::LOAD_FINISHED && textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER))
988 if(element.mObserver)
990 EmitLoadComplete(element.mObserver, textureInfo, true);
993 else if(textureInfo.loadState == LoadState::LOADING)
995 // Note : LOADING state texture cannot be queue.
996 // This case be occured when same texture id are queue in mLoadQueue.
997 ObserveTexture(textureInfo, element.mObserver);
1001 LoadTexture(textureInfo, element.mObserver);
1008 void TextureManager::ObserveTexture(TextureManager::TextureInfo& textureInfo,
1009 TextureUploadObserver* observer)
1011 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::ObserveTexture(): url:%s observer:%p\n", textureInfo.url.GetUrl().c_str(), observer);
1015 textureInfo.observerList.PushBack(observer);
1017 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Verbose, " Connect DestructionSignal to observer:%p\n", observer);
1018 observer->DestructionSignal().Connect(this, &TextureManager::ObserverDestroyed);
1022 void TextureManager::AsyncLoadComplete(const TextureManager::TextureId& textureId, std::vector<Devel::PixelBuffer>& pixelBuffers)
1024 TextureCacheIndex cacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
1025 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::AsyncLoadComplete( textureId:%d CacheIndex:%d )\n", textureId, cacheIndex.GetIndex());
1026 if(cacheIndex != INVALID_CACHE_INDEX)
1028 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
1030 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, " textureId:%d Url:%s CacheIndex:%d LoadState: %s\n", textureInfo.textureId, textureInfo.url.GetUrl().c_str(), cacheIndex.GetIndex(), GET_LOAD_STATE_STRING(textureInfo.loadState));
1031 if(textureInfo.loadState != LoadState::CANCELLED && textureInfo.loadState != LoadState::MASK_CANCELLED)
1033 // textureInfo can be invalidated after this call (as the mTextureInfoContainer may be modified)
1034 PostLoad(textureInfo, pixelBuffers);
1038 Remove(textureInfo.textureId);
1043 void TextureManager::PostLoad(TextureManager::TextureInfo& textureInfo, std::vector<Devel::PixelBuffer>& pixelBuffers)
1045 // Was the load successful?
1046 if(!pixelBuffers.empty())
1048 if(pixelBuffers.size() == 1)
1050 Devel::PixelBuffer pixelBuffer = pixelBuffers[0];
1051 if(pixelBuffer && (pixelBuffer.GetWidth() != 0) && (pixelBuffer.GetHeight() != 0))
1053 // No atlas support for now
1054 textureInfo.useAtlas = UseAtlas::NO_ATLAS;
1055 textureInfo.preMultiplied = pixelBuffer.IsAlphaPreMultiplied();
1057 if(textureInfo.storageType == StorageType::UPLOAD_TO_TEXTURE)
1059 // If there is a mask texture ID associated with this texture, then apply the mask
1060 // if it's already loaded. If it hasn't, and the mask is still loading,
1061 // wait for the mask to finish loading.
1062 // note, If the texture is already uploaded synchronously during loading,
1063 // we don't need to apply mask.
1064 if(textureInfo.loadState != LoadState::UPLOADED &&
1065 textureInfo.maskTextureId != INVALID_TEXTURE_ID)
1067 if(textureInfo.loadState == LoadState::MASK_APPLYING)
1069 textureInfo.loadState = LoadState::MASK_APPLIED;
1070 UploadTextures(pixelBuffers, textureInfo);
1071 NotifyObservers(textureInfo, true);
1075 LoadState maskLoadState = mTextureCacheManager.GetTextureStateInternal(textureInfo.maskTextureId);
1076 textureInfo.pixelBuffer = pixelBuffer; // Store the pixel buffer temporarily
1077 if(maskLoadState == LoadState::LOADING)
1079 textureInfo.loadState = LoadState::WAITING_FOR_MASK;
1081 else if(maskLoadState == LoadState::LOAD_FINISHED || maskLoadState == LoadState::UPLOADED)
1083 // Send New Task to Thread
1084 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureInfo.maskTextureId);
1085 if(maskCacheIndex != INVALID_CACHE_INDEX)
1087 TextureInfo& maskTextureInfo(mTextureCacheManager[maskCacheIndex]);
1088 if(maskTextureInfo.storageType == StorageType::KEEP_PIXEL_BUFFER)
1090 // Send New Task to Thread
1091 ApplyMask(textureInfo, textureInfo.maskTextureId);
1093 else if(maskTextureInfo.storageType == StorageType::KEEP_TEXTURE)
1095 // Upload image texture. textureInfo.loadState will be UPLOADED.
1096 UploadTextures(pixelBuffers, textureInfo);
1098 // notify mask texture set.
1099 NotifyObservers(textureInfo, true);
1103 else // maskLoadState == LoadState::LOAD_FAILED
1105 // Url texture load success, But alpha mask texture load failed. Run as normal image upload.
1106 DALI_LOG_ERROR("Alpha mask image loading failed! Image will not be masked\n");
1107 UploadTextures(pixelBuffers, textureInfo);
1108 NotifyObservers(textureInfo, true);
1114 UploadTextures(pixelBuffers, textureInfo);
1115 NotifyObservers(textureInfo, true);
1120 textureInfo.pixelBuffer = pixelBuffer; // Store the pixel data
1121 textureInfo.loadState = LoadState::LOAD_FINISHED;
1123 if(textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER)
1125 NotifyObservers(textureInfo, true);
1127 else // for the StorageType::KEEP_PIXEL_BUFFER and StorageType::KEEP_TEXTURE
1129 // Check if there was another texture waiting for this load to complete
1130 // (e.g. if this was an image mask, and its load is on a different thread)
1131 CheckForWaitingTexture(textureInfo);
1139 // No atlas support for now
1140 textureInfo.useAtlas = UseAtlas::NO_ATLAS;
1141 textureInfo.preMultiplied = false;
1143 UploadTextures(pixelBuffers, textureInfo);
1144 NotifyObservers(textureInfo, true);
1149 textureInfo.loadState = LoadState::LOAD_FAILED;
1150 if(textureInfo.storageType == StorageType::KEEP_PIXEL_BUFFER || textureInfo.storageType == StorageType::KEEP_TEXTURE)
1152 // Check if there was another texture waiting for this load to complete
1153 // (e.g. if this was an image mask, and its load is on a different thread)
1154 CheckForWaitingTexture(textureInfo);
1158 NotifyObservers(textureInfo, false);
1163 void TextureManager::CheckForWaitingTexture(TextureManager::TextureInfo& maskTextureInfo)
1165 if(maskTextureInfo.loadState == LoadState::LOAD_FINISHED &&
1166 maskTextureInfo.storageType == StorageType::KEEP_TEXTURE)
1168 // Upload mask texture. textureInfo.loadState will be UPLOADED.
1169 std::vector<Devel::PixelBuffer> pixelBuffers;
1170 pixelBuffers.push_back(maskTextureInfo.pixelBuffer);
1171 UploadTextures(pixelBuffers, maskTextureInfo);
1174 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::CheckForWaitingTexture(): maskTextureId=%d, maskTextureUrl=%s\n", maskTextureInfo.textureId, maskTextureInfo.url.GetUrl().c_str());
1176 // Search the cache, checking if any texture has this texture id as a maskTextureId
1177 const std::size_t size = mTextureCacheManager.size();
1179 // Keep notify observer required textureIds.
1180 // Note : NotifyObservers can change mTextureCacheManager cache struct. We should check id's validation before notify.
1181 std::vector<TextureId> notifyRequiredTextureIds;
1183 // TODO : Refactorize here to not iterate whole cached image.
1184 for(TextureCacheIndex cacheIndex = TextureCacheIndex(TextureManagerType::TEXTURE_CACHE_INDEX_TYPE_LOCAL, 0u); cacheIndex.GetIndex() < size; ++cacheIndex.detailValue.index)
1186 if(mTextureCacheManager[cacheIndex].maskTextureId == maskTextureInfo.textureId &&
1187 mTextureCacheManager[cacheIndex].loadState == LoadState::WAITING_FOR_MASK)
1189 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
1191 if(maskTextureInfo.loadState == LoadState::LOAD_FINISHED)
1193 if(maskTextureInfo.storageType == StorageType::KEEP_PIXEL_BUFFER)
1195 // Send New Task to Thread
1196 ApplyMask(textureInfo, maskTextureInfo.textureId);
1199 else if(maskTextureInfo.loadState == LoadState::UPLOADED)
1201 if(maskTextureInfo.storageType == StorageType::KEEP_TEXTURE)
1203 if(textureInfo.url.GetProtocolType() == VisualUrl::TEXTURE)
1205 // Get external textureSet from cacheManager.
1206 std::string location = textureInfo.url.GetLocation();
1207 if(!location.empty())
1209 TextureId id = std::stoi(location);
1210 auto externalTextureInfo = mTextureCacheManager.GetExternalTextureInfo(id);
1211 textureInfo.textures.push_back(externalTextureInfo.textureSet.GetTexture(0));
1212 textureInfo.loadState = LoadState::UPLOADED;
1217 // Upload image texture. textureInfo.loadState will be UPLOADED.
1218 std::vector<Devel::PixelBuffer> pixelBuffers;
1219 pixelBuffers.push_back(textureInfo.pixelBuffer);
1220 UploadTextures(pixelBuffers, textureInfo);
1223 // Increase reference counts for notify required textureId.
1224 // Now we can assume that we don't remove & re-assign this textureId
1225 // during NotifyObserver signal emit.
1226 maskTextureInfo.referenceCount++;
1227 textureInfo.referenceCount++;
1229 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::CheckForWaitingTexture(): Ready to notify textureId=%d\n", textureInfo.textureId);
1231 notifyRequiredTextureIds.push_back(textureInfo.textureId);
1234 else // maskTextureInfo.loadState == LoadState::LOAD_FAILED
1236 // Url texture load success, But alpha mask texture load failed. Run as normal image upload.
1237 DALI_LOG_ERROR("Alpha mask image loading failed! Image will not be masked\n");
1238 std::vector<Devel::PixelBuffer> pixelBuffers;
1239 pixelBuffers.push_back(textureInfo.pixelBuffer);
1240 UploadTextures(pixelBuffers, textureInfo);
1242 // Increase reference counts for notify required textureId.
1243 // Now we can assume that we don't remove & re-assign this textureId
1244 // during NotifyObserver signal emit.
1245 maskTextureInfo.referenceCount++;
1246 textureInfo.referenceCount++;
1248 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::CheckForWaitingTexture(): Ready to notify textureId=%d\n", textureInfo.textureId);
1250 notifyRequiredTextureIds.push_back(textureInfo.textureId);
1255 // Notify textures are masked
1256 for(const auto textureId : notifyRequiredTextureIds)
1258 TextureCacheIndex textureCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
1259 if(textureCacheIndex != INVALID_CACHE_INDEX)
1261 TextureInfo& textureInfo(mTextureCacheManager[textureCacheIndex]);
1262 NotifyObservers(textureInfo, true);
1266 // Decrease reference count
1267 for(const auto textureId : notifyRequiredTextureIds)
1269 RequestRemove(textureId, nullptr);
1273 void TextureManager::ApplyMask(TextureManager::TextureInfo& textureInfo, const TextureManager::TextureId& maskTextureId)
1275 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(maskTextureId);
1276 if(maskCacheIndex != INVALID_CACHE_INDEX)
1278 Devel::PixelBuffer maskPixelBuffer = mTextureCacheManager[maskCacheIndex].pixelBuffer;
1279 Devel::PixelBuffer pixelBuffer = textureInfo.pixelBuffer;
1280 textureInfo.pixelBuffer.Reset();
1282 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::ApplyMask(): url:%s sync:%s\n", textureInfo.url.GetUrl().c_str(), textureInfo.loadSynchronously ? "T" : "F");
1284 textureInfo.loadState = LoadState::MASK_APPLYING;
1285 auto premultiplyOnLoad = textureInfo.preMultiplyOnLoad ? DevelAsyncImageLoader::PreMultiplyOnLoad::ON : DevelAsyncImageLoader::PreMultiplyOnLoad::OFF;
1286 mAsyncLoader->ApplyMask(textureInfo.textureId, pixelBuffer, maskPixelBuffer, textureInfo.scaleFactor, textureInfo.cropToMask, premultiplyOnLoad);
1290 void TextureManager::UploadTextures(std::vector<Devel::PixelBuffer>& pixelBuffers, TextureManager::TextureInfo& textureInfo)
1292 if(!pixelBuffers.empty() && textureInfo.loadState != LoadState::UPLOADED && textureInfo.useAtlas != UseAtlas::USE_ATLAS)
1294 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, " TextureManager::UploadTextures() New Texture for textureId:%d\n", textureInfo.textureId);
1296 // Check if this pixelBuffer is premultiplied
1297 textureInfo.preMultiplied = pixelBuffers[0].IsAlphaPreMultiplied();
1299 auto& renderingAddOn = RenderingAddOn::Get();
1300 if(renderingAddOn.IsValid())
1302 renderingAddOn.CreateGeometry(textureInfo.textureId, pixelBuffers[0]);
1305 // Remove previous textures and insert new textures
1306 textureInfo.textures.clear();
1308 for(auto&& pixelBuffer : pixelBuffers)
1310 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D, pixelBuffer.GetPixelFormat(), pixelBuffer.GetWidth(), pixelBuffer.GetHeight());
1311 PixelData pixelData = Devel::PixelBuffer::Convert(pixelBuffer);
1312 texture.Upload(pixelData);
1313 textureInfo.textures.push_back(texture);
1317 // Update the load state.
1318 // Note: This is regardless of success as we care about whether a
1319 // load attempt is in progress or not. If unsuccessful, a broken
1320 // image is still loaded.
1321 textureInfo.loadState = LoadState::UPLOADED;
1324 void TextureManager::NotifyObservers(TextureManager::TextureInfo& textureInfo, const bool& success)
1326 TextureId textureId = textureInfo.textureId;
1328 // If there is an observer: Notify the load is complete, whether successful or not,
1329 // and erase it from the list
1330 TextureInfo* info = &textureInfo;
1332 if(info->animatedImageLoading)
1334 // If loading failed, we don't need to get frameCount and frameInterval.
1337 info->frameCount = info->animatedImageLoading.GetImageCount();
1338 info->frameInterval = info->animatedImageLoading.GetFrameInterval(info->frameIndex);
1340 info->animatedImageLoading.Reset();
1343 mLoadingQueueTextureId = textureId;
1345 // Reverse observer list that we can pop_back the observer.
1346 std::reverse(info->observerList.Begin(), info->observerList.End());
1348 while(info->observerList.Count())
1350 TextureUploadObserver* observer = *(info->observerList.End() - 1u);
1352 // During LoadComplete() a Control ResourceReady() signal is emitted.
1353 // During that signal the app may add remove /add Textures (e.g. via
1355 // It is possible for observers to be removed from the observer list,
1356 // and it is also possible for the mTextureInfoContainer to be modified,
1357 // invalidating the reference to the textureInfo struct.
1358 // Texture load requests for the same URL are deferred until the end of this
1360 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::NotifyObservers() observer:%p textureId:%d url:%s loadState:%s\n", observer, textureId, info->url.GetUrl().c_str(), GET_LOAD_STATE_STRING(info->loadState));
1361 // It is possible for the observer to be deleted.
1362 // Disconnect and remove the observer first.
1363 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Verbose, " Disconnect DestructionSignal to observer:%p\n", observer);
1364 observer->DestructionSignal().Disconnect(this, &TextureManager::ObserverDestroyed);
1366 info->observerList.Erase(info->observerList.End() - 1u);
1368 EmitLoadComplete(observer, *info, success);
1370 // Get the textureInfo from the container again as it may have been invalidated.
1371 TextureCacheIndex textureInfoIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
1372 if(textureInfoIndex == INVALID_CACHE_INDEX)
1374 break; // texture has been removed - can stop.
1376 info = &mTextureCacheManager[textureInfoIndex];
1379 mLoadingQueueTextureId = INVALID_TEXTURE_ID;
1382 if(info->storageType == StorageType::RETURN_PIXEL_BUFFER && info->observerList.Count() == 0)
1384 RequestRemove(info->textureId, nullptr);
1388 void TextureManager::ObserverDestroyed(TextureUploadObserver* observer)
1390 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Verbose, "TextureManager::ObserverDestroyed() observer:%p\n", observer);
1392 const std::size_t size = mTextureCacheManager.size();
1393 for(TextureCacheIndex cacheIndex = TextureCacheIndex(TextureManagerType::TEXTURE_CACHE_INDEX_TYPE_LOCAL, 0u); cacheIndex.GetIndex() < size; ++cacheIndex.detailValue.index)
1395 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
1396 for(TextureInfo::ObserverListType::Iterator j = textureInfo.observerList.Begin();
1397 j != textureInfo.observerList.End();)
1401 j = textureInfo.observerList.Erase(j);
1410 // Remove element from the LoadQueue
1411 for(auto&& element : mLoadQueue)
1413 if(element.mObserver == observer)
1415 element.mTextureId = INVALID_TEXTURE_ID;
1416 element.mObserver = nullptr;
1421 Dali::Geometry TextureManager::GetRenderGeometry(const TextureManager::TextureId& textureId, std::uint32_t& frontElements, std::uint32_t& backElements)
1423 return RenderingAddOn::Get().IsValid() ? RenderingAddOn::Get().GetGeometry(textureId, frontElements, backElements) : Geometry();
1426 void TextureManager::EmitLoadComplete(TextureUploadObserver* observer, TextureManager::TextureInfo& textureInfo, const bool& success)
1428 if(textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER)
1430 observer->LoadComplete(success, TextureUploadObserver::TextureInformation(TextureUploadObserver::ReturnType::PIXEL_BUFFER, textureInfo.pixelBuffer, textureInfo.url.GetUrl(), textureInfo.preMultiplied));
1434 TextureSet textureSet = GetTextureSet(textureInfo);
1435 if(textureInfo.isAnimatedImageFormat)
1437 observer->LoadComplete(success, TextureUploadObserver::TextureInformation(TextureUploadObserver::ReturnType::ANIMATED_IMAGE_TEXTURE, textureInfo.textureId, textureSet, textureInfo.frameCount, textureInfo.frameInterval));
1441 observer->LoadComplete(success, TextureUploadObserver::TextureInformation(TextureUploadObserver::ReturnType::TEXTURE, textureInfo.textureId, textureSet, (textureInfo.useAtlas == UseAtlas::USE_ATLAS) ? true : false, textureInfo.atlasRect, textureInfo.preMultiplied));
1446 TextureSet TextureManager::GetTextureSet(const TextureManager::TextureId& textureId)
1448 TextureSet textureSet;
1449 TextureManager::LoadState loadState = mTextureCacheManager.GetTextureStateInternal(textureId);
1450 if(loadState == TextureManager::LoadState::UPLOADED)
1452 // LoadComplete has already been called - keep the same texture set
1453 TextureCacheIndex textureCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
1454 if(textureCacheIndex != INVALID_CACHE_INDEX)
1456 TextureInfo& textureInfo(mTextureCacheManager[textureCacheIndex]);
1457 textureSet = GetTextureSet(textureInfo);
1462 DALI_LOG_ERROR("GetTextureSet is failed. texture is not uploaded \n");
1467 TextureSet TextureManager::GetTextureSet(const TextureManager::TextureInfo& textureInfo)
1469 TextureSet textureSet;
1471 // LoadComplete has already been called - keep the same texture set
1472 textureSet = TextureSet::New();
1473 if(!textureInfo.textures.empty())
1475 if(textureInfo.textures.size() > 1) // For YUV case
1477 uint32_t index = 0u;
1478 for(auto&& texture : textureInfo.textures)
1480 textureSet.SetTexture(index++, texture);
1485 textureSet.SetTexture(TEXTURE_INDEX, textureInfo.textures[0]);
1486 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureInfo.maskTextureId);
1487 if(maskCacheIndex != INVALID_CACHE_INDEX)
1489 TextureInfo& maskTextureInfo(mTextureCacheManager[maskCacheIndex]);
1490 if(maskTextureInfo.storageType == StorageType::UPLOAD_TO_TEXTURE || maskTextureInfo.storageType == StorageType::KEEP_TEXTURE)
1492 if(!maskTextureInfo.textures.empty())
1494 textureSet.SetTexture(MASK_TEXTURE_INDEX, maskTextureInfo.textures[0]);
1503 void TextureManager::RemoveTextureObserver(TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
1505 // Remove its observer
1508 const auto iterEnd = textureInfo.observerList.End();
1509 const auto iter = std::find(textureInfo.observerList.Begin(), iterEnd, observer);
1512 // Disconnect and remove the observer.
1513 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Verbose, " Disconnect DestructionSignal to observer:%p\n", observer);
1514 observer->DestructionSignal().Disconnect(this, &TextureManager::ObserverDestroyed);
1515 textureInfo.observerList.Erase(iter);
1520 } // namespace Internal
1522 } // namespace Toolkit