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/integration-api/trace.h>
27 #include <dali/public-api/rendering/geometry.h>
30 #include <dali-toolkit/internal/texture-manager/texture-async-loading-helper.h>
31 #include <dali-toolkit/internal/texture-manager/texture-cache-manager.h>
32 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
33 #include <dali-toolkit/internal/visuals/rendering-addon.h>
37 constexpr auto INITIAL_HASH_NUMBER = size_t{0u};
39 constexpr auto TEXTURE_INDEX = 0u; ///< The Index for texture
40 constexpr auto MASK_TEXTURE_INDEX = 1u; ///< The Index for mask texture
42 DALI_INIT_TRACE_FILTER(gTraceFilter, DALI_TRACE_IMAGE_PERFORMANCE_MARKER, false);
52 // Define logfilter Internal namespace level so other files (e.g. texture-cache-manager.cpp) can also use this filter.
53 Debug::Filter* gTextureManagerLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_TEXTURE_MANAGER");
56 #define GET_LOAD_STATE_STRING(loadState) \
57 loadState == TextureManagerType::LoadState::NOT_STARTED ? "NOT_STARTED" : \
58 loadState == TextureManagerType::LoadState::LOADING ? "LOADING" : \
59 loadState == TextureManagerType::LoadState::LOAD_FINISHED ? "LOAD_FINISHED" : \
60 loadState == TextureManagerType::LoadState::WAITING_FOR_MASK ? "WAITING_FOR_MASK" : \
61 loadState == TextureManagerType::LoadState::MASK_APPLYING ? "MASK_APPLYING" : \
62 loadState == TextureManagerType::LoadState::MASK_APPLIED ? "MASK_APPLIED" : \
63 loadState == TextureManagerType::LoadState::UPLOADED ? "UPLOADED" : \
64 loadState == TextureManagerType::LoadState::CANCELLED ? "CANCELLED" : \
65 loadState == TextureManagerType::LoadState::MASK_CANCELLED ? "MASK_CANCELLED" : \
66 loadState == TextureManagerType::LoadState::LOAD_FAILED ? "LOAD_FAILED" : \
73 const Vector4 FULL_ATLAS_RECT(0.0f, 0.0f, 1.0f, 1.0f); ///< UV Rectangle that covers the full Texture
75 void PreMultiply(Devel::PixelBuffer pixelBuffer, TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
77 if(Pixel::HasAlpha(pixelBuffer.GetPixelFormat()))
79 if(preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD)
81 pixelBuffer.MultiplyColorByAlpha();
86 preMultiplyOnLoad = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
90 } // Anonymous namespace
92 TextureManager::MaskingData::MaskingData()
94 mAlphaMaskId(INVALID_TEXTURE_ID),
95 mContentScaleFactor(1.0f),
97 mPreappliedMasking(true),
98 mMaskImageLoadingFailed(false)
102 TextureManager::TextureManager(bool loadYuvPlanes)
103 : mTextureCacheManager(),
104 mAsyncLoader(std::unique_ptr<TextureAsyncLoadingHelper>(new TextureAsyncLoadingHelper(*this))),
105 mLifecycleObservers(),
107 mLoadingQueueTextureId(INVALID_TEXTURE_ID),
109 mLoadYuvPlanes(loadYuvPlanes),
110 mRemoveProcessorRegistered(false)
112 // Initialize the AddOn
113 RenderingAddOn::Get();
116 TextureManager::~TextureManager()
118 if(mRemoveProcessorRegistered && Adaptor::IsAvailable())
120 Adaptor::Get().UnregisterProcessor(*this, true);
121 mRemoveProcessorRegistered = false;
124 for(auto iter = mLifecycleObservers.Begin(), endIter = mLifecycleObservers.End(); iter != endIter; ++iter)
126 (*iter)->TextureManagerDestroyed();
130 TextureSet TextureManager::LoadAnimatedImageTexture(
131 const VisualUrl& url,
132 Dali::AnimatedImageLoading animatedImageLoading,
133 const uint32_t frameIndex,
134 TextureManager::TextureId& textureId,
135 MaskingDataPointer& maskInfo,
136 const Dali::ImageDimensions& desiredSize,
137 const Dali::FittingMode::Type fittingMode,
138 const Dali::SamplingMode::Type samplingMode,
139 const bool synchronousLoading,
140 TextureUploadObserver* textureObserver,
141 TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
143 TextureSet textureSet;
145 if(synchronousLoading)
147 Devel::PixelBuffer pixelBuffer;
148 if(animatedImageLoading)
150 pixelBuffer = animatedImageLoading.LoadFrame(frameIndex, desiredSize, fittingMode, samplingMode);
154 DALI_LOG_ERROR("TextureManager::LoadAnimatedImageTexture: Synchronous loading is failed\n");
159 if(maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
161 Devel::PixelBuffer maskPixelBuffer = LoadImageFromFile(maskInfo->mAlphaMaskUrl.GetUrl(), desiredSize, fittingMode, samplingMode, true);
164 if(!maskInfo->mPreappliedMasking)
166 PixelData maskPixelData = Devel::PixelBuffer::Convert(maskPixelBuffer); // takes ownership of buffer
167 maskTexture = Texture::New(Dali::TextureType::TEXTURE_2D, maskPixelData.GetPixelFormat(), maskPixelData.GetWidth(), maskPixelData.GetHeight());
168 maskTexture.Upload(maskPixelData);
172 pixelBuffer.ApplyMask(maskPixelBuffer, maskInfo->mContentScaleFactor, maskInfo->mCropToMask);
177 DALI_LOG_ERROR("TextureManager::LoadAnimatedImageTexture: Synchronous mask image loading is failed\n");
181 if(preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD)
183 PreMultiply(pixelBuffer, preMultiplyOnLoad);
186 PixelData pixelData = Devel::PixelBuffer::Convert(pixelBuffer); // takes ownership of buffer
189 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D, pixelData.GetPixelFormat(), pixelData.GetWidth(), pixelData.GetHeight());
190 texture.Upload(pixelData);
191 textureSet = TextureSet::New();
192 textureSet.SetTexture(TEXTURE_INDEX, texture);
195 textureSet.SetTexture(MASK_TEXTURE_INDEX, maskTexture);
202 TextureId alphaMaskId = INVALID_TEXTURE_ID;
203 float contentScaleFactor = 1.0f;
204 bool cropToMask = false;
205 if(maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
207 maskInfo->mAlphaMaskId = RequestMaskLoad(maskInfo->mAlphaMaskUrl, maskInfo->mPreappliedMasking ? TextureManager::StorageType::KEEP_PIXEL_BUFFER : TextureManager::StorageType::KEEP_TEXTURE);
208 alphaMaskId = maskInfo->mAlphaMaskId;
209 if(maskInfo->mPreappliedMasking)
211 contentScaleFactor = maskInfo->mContentScaleFactor;
212 cropToMask = maskInfo->mCropToMask;
216 textureId = RequestLoadInternal(url, alphaMaskId, textureId, contentScaleFactor, desiredSize, fittingMode, samplingMode, cropToMask, TextureManager::StorageType::UPLOAD_TO_TEXTURE, textureObserver, true, TextureManager::ReloadPolicy::CACHED, preMultiplyOnLoad, animatedImageLoading, frameIndex, false);
218 TextureManager::LoadState loadState = mTextureCacheManager.GetTextureStateInternal(textureId);
219 if(loadState == TextureManager::LoadState::UPLOADED)
221 // LoadComplete has already been called - keep the same texture set
222 textureSet = GetTextureSet(textureId);
229 Devel::PixelBuffer TextureManager::LoadPixelBuffer(
230 const VisualUrl& url,
231 const Dali::ImageDimensions& desiredSize,
232 const Dali::FittingMode::Type fittingMode,
233 const Dali::SamplingMode::Type samplingMode,
234 const bool synchronousLoading,
235 TextureUploadObserver* textureObserver,
236 const bool orientationCorrection,
237 TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
239 Devel::PixelBuffer pixelBuffer;
240 if(synchronousLoading)
244 if(url.IsBufferResource())
246 const EncodedImageBuffer& encodedImageBuffer = mTextureCacheManager.GetEncodedImageBuffer(url);
247 if(encodedImageBuffer)
249 pixelBuffer = LoadImageFromBuffer(encodedImageBuffer.GetRawBuffer(), desiredSize, fittingMode, samplingMode, orientationCorrection);
254 pixelBuffer = LoadImageFromFile(url.GetUrl(), desiredSize, fittingMode, samplingMode, orientationCorrection);
256 if(pixelBuffer && preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD)
258 PreMultiply(pixelBuffer, preMultiplyOnLoad);
264 RequestLoadInternal(url, INVALID_TEXTURE_ID, INVALID_TEXTURE_ID, 1.0f, desiredSize, fittingMode, samplingMode, false, TextureManager::StorageType::RETURN_PIXEL_BUFFER, textureObserver, orientationCorrection, TextureManager::ReloadPolicy::FORCED, preMultiplyOnLoad, Dali::AnimatedImageLoading(), 0u, false);
270 TextureSet TextureManager::LoadTexture(
271 const VisualUrl& url,
272 const Dali::ImageDimensions& desiredSize,
273 const Dali::FittingMode::Type fittingMode,
274 const Dali::SamplingMode::Type samplingMode,
275 MaskingDataPointer& maskInfo,
276 const bool synchronousLoading,
277 TextureManager::TextureId& textureId,
278 Vector4& textureRect,
279 Dali::ImageDimensions& textureRectSize,
280 bool& atlasingStatus,
282 TextureUploadObserver* textureObserver,
283 AtlasUploadObserver* atlasObserver,
284 ImageAtlasManagerPtr imageAtlasManager,
285 const bool orientationCorrection,
286 const TextureManager::ReloadPolicy reloadPolicy,
287 TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
289 TextureSet textureSet;
291 loadingStatus = false;
292 textureRect = FULL_ATLAS_RECT;
294 if(VisualUrl::TEXTURE == url.GetProtocolType())
296 std::string location = url.GetLocation();
297 if(location.size() > 0u)
299 TextureId id = std::stoi(location);
300 auto externalTextureInfo = mTextureCacheManager.GetExternalTextureInfo(id);
301 if(externalTextureInfo.textureSet)
303 if(preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD)
305 // Change preMultiplyOnLoad value so make caller determine to preMultiplyAlpha or not.
306 // TODO : Should we seperate input and output value?
307 preMultiplyOnLoad = externalTextureInfo.preMultiplied ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
310 TextureId alphaMaskId = INVALID_TEXTURE_ID;
311 if(maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
313 maskInfo->mAlphaMaskId = RequestMaskLoad(maskInfo->mAlphaMaskUrl, TextureManager::StorageType::KEEP_TEXTURE, synchronousLoading);
314 alphaMaskId = maskInfo->mAlphaMaskId;
316 // Create new textureId. this textureId is not same as location
317 textureId = RequestLoad(url, alphaMaskId, textureId, 1.0f, desiredSize, fittingMode, samplingMode, false, textureObserver, orientationCorrection, reloadPolicy, preMultiplyOnLoad, synchronousLoading);
319 TextureManager::LoadState loadState = mTextureCacheManager.GetTextureStateInternal(textureId);
320 if(loadState == TextureManager::LoadState::UPLOADED)
322 textureSet = GetTextureSet(textureId);
327 // TextureId is same as location
330 textureSet = TextureSet::New();
331 textureSet.SetTexture(TEXTURE_INDEX, externalTextureInfo.textureSet.GetTexture(TEXTURE_INDEX));
339 if(synchronousLoading && atlasingStatus)
341 const bool synchronousAtlasAvaliable = (desiredSize != ImageDimensions() || url.IsLocalResource()) ? imageAtlasManager->CheckAtlasAvailable(url, desiredSize)
343 if(synchronousAtlasAvaliable)
345 std::vector<Devel::PixelBuffer> pixelBuffers;
346 LoadImageSynchronously(url, desiredSize, fittingMode, samplingMode, orientationCorrection, false, pixelBuffers);
348 if(!pixelBuffers.empty() && maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
350 std::vector<Devel::PixelBuffer> maskPixelBuffers;
351 LoadImageSynchronously(maskInfo->mAlphaMaskUrl, ImageDimensions(), FittingMode::SCALE_TO_FILL, SamplingMode::NO_FILTER, true, false, maskPixelBuffers);
352 if(!maskPixelBuffers.empty())
354 pixelBuffers[0].ApplyMask(maskPixelBuffers[0], maskInfo->mContentScaleFactor, maskInfo->mCropToMask);
359 if(!pixelBuffers.empty())
361 PreMultiply(pixelBuffers[0], preMultiplyOnLoad);
362 data = Devel::PixelBuffer::Convert(pixelBuffers[0]); // takes ownership of buffer
366 textureSet = imageAtlasManager->Add(textureRect, data);
369 textureRectSize.SetWidth(data.GetWidth());
370 textureRectSize.SetHeight(data.GetHeight());
375 DALI_LOG_ERROR("TextureManager::LoadTexture: Synchronous Texture loading with atlasing is failed.\n");
380 atlasingStatus = false;
387 loadingStatus = true;
388 // Atlas manager can chage desired size when it is set by 0,0.
389 // We should store into textureRectSize only if atlasing successed.
390 // So copy inputed desiredSize, and replace value into textureRectSize only if atlasing success.
391 Dali::ImageDimensions atlasDesiredSize = desiredSize;
394 if(url.IsBufferResource())
396 const EncodedImageBuffer& encodedImageBuffer = GetEncodedImageBuffer(url.GetUrl());
397 if(encodedImageBuffer)
399 textureSet = imageAtlasManager->Add(textureRect, encodedImageBuffer, desiredSize, fittingMode, true, atlasObserver);
404 textureSet = imageAtlasManager->Add(textureRect, url, atlasDesiredSize, fittingMode, true, atlasObserver);
407 if(!textureSet) // big image, no atlasing or atlasing failed
409 atlasingStatus = false;
411 TextureId alphaMaskId = INVALID_TEXTURE_ID;
412 float contentScaleFactor = 1.0f;
413 bool cropToMask = false;
414 if(maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
416 maskInfo->mAlphaMaskId = RequestMaskLoad(maskInfo->mAlphaMaskUrl, maskInfo->mPreappliedMasking ? TextureManager::StorageType::KEEP_PIXEL_BUFFER : TextureManager::StorageType::KEEP_TEXTURE, synchronousLoading);
417 alphaMaskId = maskInfo->mAlphaMaskId;
418 if(maskInfo && maskInfo->mPreappliedMasking)
420 contentScaleFactor = maskInfo->mContentScaleFactor;
421 cropToMask = maskInfo->mCropToMask;
425 textureId = RequestLoad(
435 orientationCorrection,
440 TextureManager::LoadState loadState = mTextureCacheManager.GetTextureStateInternal(textureId);
441 if(loadState == TextureManager::LoadState::UPLOADED)
443 // LoadComplete has already been called - keep the same texture set
444 textureSet = GetTextureSet(textureId);
447 // If we are loading the texture, or waiting for the ready signal handler to complete, inform
448 // caller that they need to wait.
449 loadingStatus = (loadState == TextureManager::LoadState::LOADING ||
450 loadState == TextureManager::LoadState::WAITING_FOR_MASK ||
451 loadState == TextureManager::LoadState::MASK_APPLYING ||
452 loadState == TextureManager::LoadState::MASK_APPLIED ||
453 loadState == TextureManager::LoadState::NOT_STARTED ||
454 mLoadingQueueTextureId != INVALID_TEXTURE_ID);
458 textureRectSize = atlasDesiredSize;
463 if(synchronousLoading)
465 loadingStatus = false;
471 TextureManager::TextureId TextureManager::RequestLoad(
472 const VisualUrl& url,
473 const ImageDimensions& desiredSize,
474 const Dali::FittingMode::Type fittingMode,
475 const Dali::SamplingMode::Type samplingMode,
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, false, TextureManager::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 bool cropToMask,
494 TextureUploadObserver* observer,
495 const bool orientationCorrection,
496 const TextureManager::ReloadPolicy reloadPolicy,
497 TextureManager::MultiplyOnLoad& preMultiplyOnLoad,
498 const bool synchronousLoading)
500 return RequestLoadInternal(url, maskTextureId, previousTextureId, contentScale, desiredSize, fittingMode, samplingMode, cropToMask, TextureManager::StorageType::UPLOAD_TO_TEXTURE, observer, orientationCorrection, reloadPolicy, preMultiplyOnLoad, Dali::AnimatedImageLoading(), 0u, synchronousLoading);
503 TextureManager::TextureId TextureManager::RequestMaskLoad(
504 const VisualUrl& maskUrl,
505 const TextureManager::StorageType storageType,
506 const bool synchronousLoading)
508 // Use the normal load procedure to get the alpha mask.
509 auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
510 return RequestLoadInternal(maskUrl, INVALID_TEXTURE_ID, INVALID_TEXTURE_ID, 1.0f, ImageDimensions(), Dali::FittingMode::SCALE_TO_FILL, Dali::SamplingMode::NO_FILTER, false, storageType, NULL, true, TextureManager::ReloadPolicy::CACHED, preMultiply, Dali::AnimatedImageLoading(), 0u, synchronousLoading);
513 TextureManager::TextureId TextureManager::RequestLoadInternal(
514 const VisualUrl& url,
515 const TextureManager::TextureId maskTextureId,
516 const TextureManager::TextureId previousTextureId,
517 const float contentScale,
518 const Dali::ImageDimensions& desiredSize,
519 const Dali::FittingMode::Type fittingMode,
520 const Dali::SamplingMode::Type samplingMode,
521 const bool cropToMask,
522 const TextureManager::StorageType storageType,
523 TextureUploadObserver* observer,
524 const bool orientationCorrection,
525 const TextureManager::ReloadPolicy reloadPolicy,
526 TextureManager::MultiplyOnLoad& preMultiplyOnLoad,
527 Dali::AnimatedImageLoading animatedImageLoading,
528 const uint32_t frameIndex,
529 const bool synchronousLoading)
531 TextureHash textureHash = INITIAL_HASH_NUMBER;
532 TextureCacheIndex cacheIndex = INVALID_CACHE_INDEX;
533 bool loadYuvPlanes = (mLoadYuvPlanes && maskTextureId == INVALID_TEXTURE_ID && storageType == TextureManager::StorageType::UPLOAD_TO_TEXTURE);
535 if(storageType != TextureManager::StorageType::RETURN_PIXEL_BUFFER)
537 textureHash = mTextureCacheManager.GenerateHash(url, desiredSize, fittingMode, samplingMode, maskTextureId, cropToMask, frameIndex);
539 // Look up the texture by hash. Note: The extra parameters are used in case of a hash collision.
540 cacheIndex = mTextureCacheManager.FindCachedTexture(textureHash, url, desiredSize, fittingMode, samplingMode, storageType, maskTextureId, cropToMask, preMultiplyOnLoad, (animatedImageLoading) ? true : false, frameIndex);
543 TextureManager::TextureId textureId = INVALID_TEXTURE_ID;
544 // Check if the requested Texture exists in the cache.
545 if(cacheIndex != INVALID_CACHE_INDEX)
547 if(TextureManager::ReloadPolicy::CACHED == reloadPolicy || TextureManager::INVALID_TEXTURE_ID == previousTextureId)
549 // Mark this texture being used by another client resource, or Reload forced without request load before.
550 // Forced reload which have current texture before, would replace the current texture.
551 // without the need for incrementing the reference count.
552 ++(mTextureCacheManager[cacheIndex].referenceCount);
554 textureId = mTextureCacheManager[cacheIndex].textureId;
556 // Update preMultiplyOnLoad value. It should be changed according to preMultiplied value of the cached info.
557 preMultiplyOnLoad = mTextureCacheManager[cacheIndex].preMultiplied ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
558 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));
561 if(textureId == INVALID_TEXTURE_ID) // There was no caching, or caching not required
563 textureId = mTextureCacheManager.GenerateTextureId();
565 bool preMultiply = (preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD);
567 // Cache new texutre, and get cacheIndex.
568 cacheIndex = mTextureCacheManager.AppendCache(TextureInfo(textureId, maskTextureId, url, desiredSize, contentScale, fittingMode, samplingMode, false, cropToMask, textureHash, orientationCorrection, preMultiply, animatedImageLoading, frameIndex, loadYuvPlanes));
569 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);
572 // The below code path is common whether we are using the cache or not.
573 // The textureInfoIndex now refers to either a pre-existing cached TextureInfo,
574 // or a new TextureInfo just created.
575 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
576 textureInfo.maskTextureId = maskTextureId;
577 textureInfo.storageType = storageType;
578 textureInfo.orientationCorrection = orientationCorrection;
580 // the case using external texture has already been loaded texture, so change its status to WAITING_FOR_MASK.
581 if(url.GetProtocolType() == VisualUrl::TEXTURE)
583 if(textureInfo.loadState != TextureManager::LoadState::UPLOADED)
585 textureInfo.preMultiplied = (preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD);
586 textureInfo.loadState = TextureManager::LoadState::WAITING_FOR_MASK;
590 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureInfo loadState:%s\n", GET_LOAD_STATE_STRING(textureInfo.loadState));
592 // Force reloading of texture by setting loadState unless already loading or cancelled.
593 if(TextureManager::ReloadPolicy::FORCED == reloadPolicy &&
594 TextureManager::LoadState::LOADING != textureInfo.loadState &&
595 TextureManager::LoadState::WAITING_FOR_MASK != textureInfo.loadState &&
596 TextureManager::LoadState::MASK_APPLYING != textureInfo.loadState &&
597 TextureManager::LoadState::MASK_APPLIED != textureInfo.loadState &&
598 TextureManager::LoadState::CANCELLED != textureInfo.loadState &&
599 TextureManager::LoadState::MASK_CANCELLED != textureInfo.loadState)
601 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);
602 textureInfo.loadState = TextureManager::LoadState::NOT_STARTED;
605 if(!synchronousLoading)
607 // Check if we should add the observer.
608 // Only do this if we have not loaded yet and it will not have loaded by the end of this method.
609 switch(textureInfo.loadState)
611 case TextureManager::LoadState::LOAD_FAILED: // Failed notifies observer which then stops observing.
612 case TextureManager::LoadState::NOT_STARTED:
614 LoadOrQueueTexture(textureInfo, observer); // If called inside NotifyObservers, queues until afterwards
617 case TextureManager::LoadState::LOADING:
618 case TextureManager::LoadState::WAITING_FOR_MASK:
619 case TextureManager::LoadState::MASK_APPLYING:
620 case TextureManager::LoadState::MASK_APPLIED:
622 // Do not observe even we reload forced when texture is already loading state.
623 if(TextureManager::ReloadPolicy::CACHED == reloadPolicy || TextureManager::INVALID_TEXTURE_ID == previousTextureId)
625 ObserveTexture(textureInfo, observer);
629 case TextureManager::LoadState::UPLOADED:
633 LoadOrQueueTexture(textureInfo, observer);
637 case TextureManager::LoadState::CANCELLED:
639 // A cancelled texture hasn't finished loading yet. Treat as a loading texture
640 // (it's ref count has already been incremented, above)
641 textureInfo.loadState = TextureManager::LoadState::LOADING;
642 ObserveTexture(textureInfo, observer);
645 case TextureManager::LoadState::MASK_CANCELLED:
647 // A cancelled texture hasn't finished mask applying yet. Treat as a mask applying texture
648 // (it's ref count has already been incremented, above)
649 textureInfo.loadState = TextureManager::LoadState::MASK_APPLYING;
650 ObserveTexture(textureInfo, observer);
653 case TextureManager::LoadState::LOAD_FINISHED:
655 // Loading has already completed.
656 if(observer && textureInfo.storageType == TextureManager::StorageType::RETURN_PIXEL_BUFFER)
658 LoadOrQueueTexture(textureInfo, observer);
666 // If the image is already finished to load, use cached texture.
667 // We don't need to consider Observer because this is synchronous loading.
668 if(!(textureInfo.loadState == TextureManager::LoadState::UPLOADED ||
669 textureInfo.loadState == TextureManager::LoadState::LOAD_FINISHED))
671 if(url.GetProtocolType() == VisualUrl::TEXTURE)
673 // Get external textureSet from cacheManager.
674 std::string location = textureInfo.url.GetLocation();
675 if(!location.empty())
677 TextureId id = std::stoi(location);
678 auto externalTextureInfo = mTextureCacheManager.GetExternalTextureInfo(id);
679 textureInfo.textures.push_back(externalTextureInfo.textureSet.GetTexture(0));
680 textureInfo.loadState = TextureManager::LoadState::UPLOADED;
685 std::vector<Devel::PixelBuffer> pixelBuffers;
686 LoadImageSynchronously(url, desiredSize, fittingMode, samplingMode, orientationCorrection, loadYuvPlanes, pixelBuffers);
688 if(pixelBuffers.empty())
690 // If pixelBuffer loading is failed in synchronously, call RequestRemove() method.
691 RequestRemove(textureId, nullptr);
692 return INVALID_TEXTURE_ID;
695 if(storageType == TextureManager::StorageType::KEEP_PIXEL_BUFFER) // For the mask image loading.
697 textureInfo.pixelBuffer = pixelBuffers[0]; // Store the pixel data
698 textureInfo.loadState = TextureManager::LoadState::LOAD_FINISHED;
700 else // For the image loading.
703 if(maskTextureId != INVALID_TEXTURE_ID)
705 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(maskTextureId);
706 if(maskCacheIndex != INVALID_CACHE_INDEX)
708 if(mTextureCacheManager[maskCacheIndex].storageType == TextureManager::StorageType::KEEP_PIXEL_BUFFER)
710 Devel::PixelBuffer maskPixelBuffer = mTextureCacheManager[maskCacheIndex].pixelBuffer;
713 pixelBuffers[0].ApplyMask(maskPixelBuffer, contentScale, cropToMask);
717 DALI_LOG_ERROR("Mask image cached invalid pixel buffer!\n");
723 DALI_LOG_ERROR("Mask image is not stored in cache.\n");
726 PreMultiply(pixelBuffers[0], preMultiplyOnLoad);
729 UploadTextures(pixelBuffers, textureInfo);
738 void TextureManager::RequestRemove(const TextureManager::TextureId textureId, TextureUploadObserver* observer)
740 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureManager::RequestRemove( textureId=%d observer=%p )\n", textureId, observer);
743 if(textureId != INVALID_TEXTURE_ID)
745 TextureCacheIndex textureCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
746 if(textureCacheIndex != INVALID_CACHE_INDEX)
750 // Remove observer from cached texture info
751 TextureInfo& textureInfo(mTextureCacheManager[textureCacheIndex]);
752 RemoveTextureObserver(textureInfo, observer);
755 mRemoveQueue.PushBack(textureId);
757 if(!mRemoveProcessorRegistered && Adaptor::IsAvailable())
759 mRemoveProcessorRegistered = true;
760 Adaptor::Get().RegisterProcessor(*this, true);
766 void TextureManager::Remove(const TextureManager::TextureId textureId)
768 if(textureId != INVALID_TEXTURE_ID)
770 TextureCacheIndex textureCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
771 if(textureCacheIndex != INVALID_CACHE_INDEX)
773 TextureManager::TextureId maskTextureId = INVALID_TEXTURE_ID;
774 TextureInfo& textureInfo(mTextureCacheManager[textureCacheIndex]);
775 // We only need to consider maskTextureId when texture's loadState is not cancelled. Because it is already deleted.
776 if(textureInfo.loadState != TextureManager::LoadState::CANCELLED && textureInfo.loadState != TextureManager::LoadState::MASK_CANCELLED)
778 if(textureInfo.maskTextureId != INVALID_TEXTURE_ID)
780 maskTextureId = textureInfo.maskTextureId;
784 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));
786 // Remove textureId in CacheManager. Now, textureInfo is invalidate.
787 mTextureCacheManager.RemoveCache(textureInfo);
789 // Remove maskTextureId in CacheManager
790 if(maskTextureId != INVALID_TEXTURE_ID)
792 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(maskTextureId);
793 if(maskCacheIndex != INVALID_CACHE_INDEX)
795 TextureInfo& maskTextureInfo(mTextureCacheManager[maskCacheIndex]);
797 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));
799 mTextureCacheManager.RemoveCache(maskTextureInfo);
806 void TextureManager::ProcessRemoveQueue()
808 DALI_TRACE_BEGIN_WITH_MESSAGE_GENERATOR(gTraceFilter, "DALI_TEXTURE_MANAGER_PROCESS_REMOVE_QUEUE", [&](std::ostringstream& oss) {
809 oss << "[" << mRemoveQueue.Count() << "]";
812 // Note that RemoveQueue is not be changed during Remove().
813 for(auto&& textureId : mRemoveQueue)
815 if(textureId != INVALID_TEXTURE_ID)
821 mRemoveQueue.Clear();
823 DALI_TRACE_END(gTraceFilter, "DALI_TEXTURE_MANAGER_PROCESS_REMOVE_QUEUE");
826 void TextureManager::Process(bool postProcessor)
828 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureManager::Process()\n");
830 ProcessRemoveQueue();
832 if(Adaptor::IsAvailable())
834 Adaptor::Get().UnregisterProcessor(*this, true);
835 mRemoveProcessorRegistered = false;
839 void TextureManager::LoadImageSynchronously(
840 const VisualUrl& url,
841 const Dali::ImageDimensions& desiredSize,
842 const Dali::FittingMode::Type fittingMode,
843 const Dali::SamplingMode::Type samplingMode,
844 const bool orientationCorrection,
845 const bool loadYuvPlanes,
846 std::vector<Devel::PixelBuffer>& pixelBuffers)
848 Devel::PixelBuffer pixelBuffer;
849 if(url.IsBufferResource())
851 const EncodedImageBuffer& encodedImageBuffer = mTextureCacheManager.GetEncodedImageBuffer(url);
852 if(encodedImageBuffer)
854 pixelBuffer = LoadImageFromBuffer(encodedImageBuffer.GetRawBuffer(), desiredSize, fittingMode, samplingMode, orientationCorrection);
861 Dali::LoadImagePlanesFromFile(url.GetUrl(), pixelBuffers, desiredSize, fittingMode, samplingMode, orientationCorrection);
865 pixelBuffer = Dali::LoadImageFromFile(url.GetUrl(), desiredSize, fittingMode, samplingMode, orientationCorrection);
871 pixelBuffers.push_back(pixelBuffer);
875 void TextureManager::AddObserver(TextureManager::LifecycleObserver& observer)
877 // make sure an observer doesn't observe the same object twice
878 // otherwise it will get multiple calls to ObjectDestroyed()
879 DALI_ASSERT_DEBUG(mLifecycleObservers.End() == std::find(mLifecycleObservers.Begin(), mLifecycleObservers.End(), &observer));
880 mLifecycleObservers.PushBack(&observer);
883 void TextureManager::RemoveObserver(TextureManager::LifecycleObserver& observer)
885 // Find the observer...
886 auto endIter = mLifecycleObservers.End();
887 for(auto iter = mLifecycleObservers.Begin(); iter != endIter; ++iter)
889 if((*iter) == &observer)
891 mLifecycleObservers.Erase(iter);
895 DALI_ASSERT_DEBUG(endIter != mLifecycleObservers.End());
898 void TextureManager::LoadOrQueueTexture(TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
900 switch(textureInfo.loadState)
902 case TextureManager::LoadState::NOT_STARTED:
903 case TextureManager::LoadState::LOAD_FAILED:
905 if(mLoadingQueueTextureId != INVALID_TEXTURE_ID)
907 QueueLoadTexture(textureInfo, observer);
911 LoadTexture(textureInfo, observer);
915 case TextureManager::LoadState::UPLOADED:
917 if(mLoadingQueueTextureId != INVALID_TEXTURE_ID)
919 QueueLoadTexture(textureInfo, observer);
923 // The Texture has already loaded. The other observers have already been notified.
924 // We need to send a "late" loaded notification for this observer.
927 EmitLoadComplete(observer, textureInfo, true);
932 case TextureManager::LoadState::LOADING:
933 case TextureManager::LoadState::CANCELLED:
934 case TextureManager::LoadState::MASK_CANCELLED:
935 case TextureManager::LoadState::LOAD_FINISHED:
936 case TextureManager::LoadState::WAITING_FOR_MASK:
937 case TextureManager::LoadState::MASK_APPLYING:
938 case TextureManager::LoadState::MASK_APPLIED:
945 void TextureManager::QueueLoadTexture(const TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
947 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Verbose, "Add observer to observer queue (textureId:%d, observer:%p)\n", textureInfo.textureId, observer);
949 const auto& textureId = textureInfo.textureId;
950 mLoadQueue.PushBack(QueueElement(textureId, observer));
954 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Verbose, " Connect DestructionSignal to observer:%p\n", observer);
955 observer->DestructionSignal().Connect(this, &TextureManager::ObserverDestroyed);
959 void TextureManager::LoadTexture(TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
961 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::LoadTexture(): url:%s sync:%s\n", textureInfo.url.GetUrl().c_str(), textureInfo.loadSynchronously ? "T" : "F");
962 textureInfo.loadState = TextureManager::LoadState::LOADING;
963 if(!textureInfo.loadSynchronously)
965 auto premultiplyOnLoad = (textureInfo.preMultiplyOnLoad && textureInfo.maskTextureId == INVALID_TEXTURE_ID) ? DevelAsyncImageLoader::PreMultiplyOnLoad::ON : DevelAsyncImageLoader::PreMultiplyOnLoad::OFF;
966 if(textureInfo.animatedImageLoading)
968 mAsyncLoader->LoadAnimatedImage(textureInfo.textureId, textureInfo.animatedImageLoading, textureInfo.frameIndex, textureInfo.desiredSize, textureInfo.fittingMode, textureInfo.samplingMode, premultiplyOnLoad);
972 mAsyncLoader->Load(textureInfo.textureId, textureInfo.url, textureInfo.desiredSize, textureInfo.fittingMode, textureInfo.samplingMode, textureInfo.orientationCorrection, premultiplyOnLoad, textureInfo.loadYuvPlanes);
975 ObserveTexture(textureInfo, observer);
978 void TextureManager::ProcessLoadQueue()
980 for(auto&& element : mLoadQueue)
982 if(element.mTextureId == INVALID_TEXTURE_ID)
987 TextureCacheIndex cacheIndex = mTextureCacheManager.GetCacheIndexFromId(element.mTextureId);
988 if(cacheIndex != INVALID_CACHE_INDEX)
990 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
992 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));
994 if((textureInfo.loadState == TextureManager::LoadState::UPLOADED) ||
995 (textureInfo.loadState == TextureManager::LoadState::LOAD_FINISHED &&
996 textureInfo.storageType == TextureManager::StorageType::RETURN_PIXEL_BUFFER))
998 if(element.mObserver)
1000 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Verbose, " Disconnect DestructionSignal to observer:%p\n", element.mObserver);
1001 element.mObserver->DestructionSignal().Disconnect(this, &TextureManager::ObserverDestroyed);
1003 EmitLoadComplete(element.mObserver, textureInfo, true);
1006 else if(textureInfo.loadState == TextureManager::LoadState::LOADING)
1008 // Note : LOADING state texture cannot be queue.
1009 // This case be occured when same texture id are queue in mLoadQueue.
1010 ObserveTexture(textureInfo, element.mObserver);
1014 LoadTexture(textureInfo, element.mObserver);
1022 void TextureManager::ObserveTexture(TextureManager::TextureInfo& textureInfo,
1023 TextureUploadObserver* observer)
1025 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::ObserveTexture(): url:%s observer:%p\n", textureInfo.url.GetUrl().c_str(), observer);
1029 textureInfo.observerList.PushBack(observer);
1031 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Verbose, " Connect DestructionSignal to observer:%p\n", observer);
1032 observer->DestructionSignal().Connect(this, &TextureManager::ObserverDestroyed);
1036 void TextureManager::AsyncLoadComplete(const TextureManager::TextureId textureId, std::vector<Devel::PixelBuffer>& pixelBuffers)
1038 TextureCacheIndex cacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
1039 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::AsyncLoadComplete( textureId:%d CacheIndex:%d )\n", textureId, cacheIndex.GetIndex());
1040 if(cacheIndex != INVALID_CACHE_INDEX)
1042 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
1044 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));
1045 if(textureInfo.loadState != TextureManager::LoadState::CANCELLED && textureInfo.loadState != TextureManager::LoadState::MASK_CANCELLED)
1047 // textureInfo can be invalidated after this call (as the mTextureInfoContainer may be modified)
1048 PostLoad(textureInfo, pixelBuffers);
1052 Remove(textureInfo.textureId);
1057 void TextureManager::PostLoad(TextureManager::TextureInfo& textureInfo, std::vector<Devel::PixelBuffer>& pixelBuffers)
1059 if(!pixelBuffers.empty()) ///< Load success
1061 if(pixelBuffers.size() == 1)
1063 Devel::PixelBuffer pixelBuffer = pixelBuffers[0];
1064 if(pixelBuffer && (pixelBuffer.GetWidth() != 0) && (pixelBuffer.GetHeight() != 0))
1066 textureInfo.preMultiplied = pixelBuffer.IsAlphaPreMultiplied();
1068 if(textureInfo.storageType == TextureManager::StorageType::UPLOAD_TO_TEXTURE)
1070 // If there is a mask texture ID associated with this texture, then apply the mask
1071 // if it's already loaded. If it hasn't, and the mask is still loading,
1072 // wait for the mask to finish loading.
1073 // note, If the texture is already uploaded synchronously during loading,
1074 // we don't need to apply mask.
1075 if(textureInfo.loadState != TextureManager::LoadState::UPLOADED &&
1076 textureInfo.maskTextureId != INVALID_TEXTURE_ID)
1078 if(textureInfo.loadState == TextureManager::LoadState::MASK_APPLYING)
1080 textureInfo.loadState = TextureManager::LoadState::MASK_APPLIED;
1081 UploadTextures(pixelBuffers, textureInfo);
1082 NotifyObservers(textureInfo, true);
1086 LoadState maskLoadState = mTextureCacheManager.GetTextureStateInternal(textureInfo.maskTextureId);
1087 textureInfo.pixelBuffer = pixelBuffer; // Store the pixel buffer temporarily
1088 if(maskLoadState == TextureManager::LoadState::LOADING)
1090 textureInfo.loadState = TextureManager::LoadState::WAITING_FOR_MASK;
1092 else if(maskLoadState == TextureManager::LoadState::LOAD_FINISHED || maskLoadState == TextureManager::LoadState::UPLOADED)
1094 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureInfo.maskTextureId);
1095 if(maskCacheIndex != INVALID_CACHE_INDEX)
1097 TextureInfo& maskTextureInfo(mTextureCacheManager[maskCacheIndex]);
1098 if(maskTextureInfo.storageType == TextureManager::StorageType::KEEP_PIXEL_BUFFER)
1100 ApplyMask(textureInfo, textureInfo.maskTextureId);
1102 else if(maskTextureInfo.storageType == TextureManager::StorageType::KEEP_TEXTURE)
1104 // Upload image texture. textureInfo.loadState will be UPLOADED.
1105 UploadTextures(pixelBuffers, textureInfo);
1107 // notify mask texture set.
1108 NotifyObservers(textureInfo, true);
1112 else // maskLoadState == TextureManager::LoadState::LOAD_FAILED
1114 // Url texture load success, But alpha mask texture load failed. Run as normal image upload.
1115 DALI_LOG_ERROR("Alpha mask image loading failed! Image will not be masked\n");
1116 UploadTextures(pixelBuffers, textureInfo);
1117 NotifyObservers(textureInfo, true);
1123 UploadTextures(pixelBuffers, textureInfo);
1124 NotifyObservers(textureInfo, true);
1129 textureInfo.pixelBuffer = pixelBuffer; // Store the pixel data
1130 textureInfo.loadState = TextureManager::LoadState::LOAD_FINISHED;
1132 if(textureInfo.storageType == TextureManager::StorageType::RETURN_PIXEL_BUFFER)
1134 NotifyObservers(textureInfo, true);
1136 else // for the TextureManager::StorageType::KEEP_PIXEL_BUFFER and TextureManager::StorageType::KEEP_TEXTURE
1138 // Check if there was another texture waiting for this load to complete
1139 // (e.g. if this was an image mask, and its load is on a different thread)
1140 CheckForWaitingTexture(textureInfo);
1148 textureInfo.preMultiplied = false;
1150 UploadTextures(pixelBuffers, textureInfo);
1151 NotifyObservers(textureInfo, true);
1156 textureInfo.loadState = TextureManager::LoadState::LOAD_FAILED;
1157 if(textureInfo.storageType == TextureManager::StorageType::KEEP_PIXEL_BUFFER || textureInfo.storageType == TextureManager::StorageType::KEEP_TEXTURE)
1159 // Check if there was another texture waiting for this load to complete
1160 // (e.g. if this was an image mask, and its load is on a different thread)
1161 CheckForWaitingTexture(textureInfo);
1165 NotifyObservers(textureInfo, false);
1170 void TextureManager::CheckForWaitingTexture(TextureManager::TextureInfo& maskTextureInfo)
1172 if(maskTextureInfo.loadState == TextureManager::LoadState::LOAD_FINISHED &&
1173 maskTextureInfo.storageType == TextureManager::StorageType::KEEP_TEXTURE)
1175 // Upload mask texture. textureInfo.loadState will be UPLOADED.
1176 std::vector<Devel::PixelBuffer> pixelBuffers;
1177 pixelBuffers.push_back(maskTextureInfo.pixelBuffer);
1178 UploadTextures(pixelBuffers, maskTextureInfo);
1181 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::CheckForWaitingTexture(): maskTextureId=%d, maskTextureUrl=%s\n", maskTextureInfo.textureId, maskTextureInfo.url.GetUrl().c_str());
1183 // Search the cache, checking if any texture has this texture id as a maskTextureId
1184 const size_t size = mTextureCacheManager.size();
1186 // Keep notify observer required textureIds.
1187 // Note : NotifyObservers can change mTextureCacheManager cache struct. We should check id's validation before notify.
1188 std::vector<TextureId> notifyRequiredTextureIds;
1190 // TODO : Refactorize here to not iterate whole cached image.
1191 for(TextureCacheIndex cacheIndex = TextureCacheIndex(TextureManagerType::TEXTURE_CACHE_INDEX_TYPE_LOCAL, 0u); cacheIndex.GetIndex() < size; ++cacheIndex.detailValue.index)
1193 if(mTextureCacheManager[cacheIndex].maskTextureId == maskTextureInfo.textureId &&
1194 mTextureCacheManager[cacheIndex].loadState == TextureManager::LoadState::WAITING_FOR_MASK)
1196 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
1198 if(maskTextureInfo.loadState == TextureManager::LoadState::LOAD_FINISHED)
1200 if(maskTextureInfo.storageType == TextureManager::StorageType::KEEP_PIXEL_BUFFER)
1202 ApplyMask(textureInfo, maskTextureInfo.textureId);
1205 else if(maskTextureInfo.loadState == TextureManager::LoadState::UPLOADED)
1207 if(maskTextureInfo.storageType == TextureManager::StorageType::KEEP_TEXTURE)
1209 if(textureInfo.url.GetProtocolType() == VisualUrl::TEXTURE)
1211 // Get external textureSet from cacheManager.
1212 std::string location = textureInfo.url.GetLocation();
1213 if(!location.empty())
1215 TextureId id = std::stoi(location);
1216 auto externalTextureInfo = mTextureCacheManager.GetExternalTextureInfo(id);
1217 textureInfo.textures.push_back(externalTextureInfo.textureSet.GetTexture(0));
1218 textureInfo.loadState = TextureManager::LoadState::UPLOADED;
1223 // Upload image texture. textureInfo.loadState will be UPLOADED.
1224 std::vector<Devel::PixelBuffer> pixelBuffers;
1225 pixelBuffers.push_back(textureInfo.pixelBuffer);
1226 UploadTextures(pixelBuffers, textureInfo);
1229 // Increase reference counts for notify required textureId.
1230 // Now we can assume that we don't remove & re-assign this textureId
1231 // during NotifyObserver signal emit.
1232 maskTextureInfo.referenceCount++;
1233 textureInfo.referenceCount++;
1235 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::CheckForWaitingTexture(): Ready to notify textureId=%d\n", textureInfo.textureId);
1237 notifyRequiredTextureIds.push_back(textureInfo.textureId);
1240 else // maskTextureInfo.loadState == TextureManager::LoadState::LOAD_FAILED
1242 // Url texture load success, But alpha mask texture load failed. Run as normal image upload.
1243 DALI_LOG_ERROR("Alpha mask image loading failed! Image will not be masked\n");
1244 std::vector<Devel::PixelBuffer> pixelBuffers;
1245 pixelBuffers.push_back(textureInfo.pixelBuffer);
1246 UploadTextures(pixelBuffers, textureInfo);
1248 // Increase reference counts for notify required textureId.
1249 // Now we can assume that we don't remove & re-assign this textureId
1250 // during NotifyObserver signal emit.
1251 maskTextureInfo.referenceCount++;
1252 textureInfo.referenceCount++;
1254 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::CheckForWaitingTexture(): Ready to notify textureId=%d\n", textureInfo.textureId);
1256 notifyRequiredTextureIds.push_back(textureInfo.textureId);
1261 // Notify textures are masked
1262 for(const auto textureId : notifyRequiredTextureIds)
1264 TextureCacheIndex textureCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
1265 if(textureCacheIndex != INVALID_CACHE_INDEX)
1267 TextureInfo& textureInfo(mTextureCacheManager[textureCacheIndex]);
1268 NotifyObservers(textureInfo, true);
1272 // Decrease reference count
1273 for(const auto textureId : notifyRequiredTextureIds)
1275 RequestRemove(textureId, nullptr);
1279 void TextureManager::ApplyMask(TextureManager::TextureInfo& textureInfo, const TextureManager::TextureId maskTextureId)
1281 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(maskTextureId);
1282 if(maskCacheIndex != INVALID_CACHE_INDEX)
1284 Devel::PixelBuffer maskPixelBuffer = mTextureCacheManager[maskCacheIndex].pixelBuffer;
1285 Devel::PixelBuffer pixelBuffer = textureInfo.pixelBuffer;
1286 textureInfo.pixelBuffer.Reset();
1288 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::ApplyMask(): url:%s sync:%s\n", textureInfo.url.GetUrl().c_str(), textureInfo.loadSynchronously ? "T" : "F");
1290 textureInfo.loadState = TextureManager::LoadState::MASK_APPLYING;
1291 auto premultiplyOnLoad = textureInfo.preMultiplyOnLoad ? DevelAsyncImageLoader::PreMultiplyOnLoad::ON : DevelAsyncImageLoader::PreMultiplyOnLoad::OFF;
1292 mAsyncLoader->ApplyMask(textureInfo.textureId, pixelBuffer, maskPixelBuffer, textureInfo.scaleFactor, textureInfo.cropToMask, premultiplyOnLoad);
1296 void TextureManager::UploadTextures(std::vector<Devel::PixelBuffer>& pixelBuffers, TextureManager::TextureInfo& textureInfo)
1298 if(!pixelBuffers.empty() && textureInfo.loadState != TextureManager::LoadState::UPLOADED)
1300 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, " TextureManager::UploadTextures() New Texture for textureId:%d\n", textureInfo.textureId);
1302 // Check if this pixelBuffer is premultiplied
1303 textureInfo.preMultiplied = pixelBuffers[0].IsAlphaPreMultiplied();
1305 auto& renderingAddOn = RenderingAddOn::Get();
1306 if(renderingAddOn.IsValid())
1308 renderingAddOn.CreateGeometry(textureInfo.textureId, pixelBuffers[0]);
1311 // Remove previous textures and insert new textures
1312 textureInfo.textures.clear();
1314 for(auto&& pixelBuffer : pixelBuffers)
1316 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D, pixelBuffer.GetPixelFormat(), pixelBuffer.GetWidth(), pixelBuffer.GetHeight());
1317 PixelData pixelData = Devel::PixelBuffer::Convert(pixelBuffer);
1318 texture.Upload(pixelData);
1319 textureInfo.textures.push_back(texture);
1323 // Update the load state.
1324 // Note: This is regardless of success as we care about whether a
1325 // load attempt is in progress or not. If unsuccessful, a broken
1326 // image is still loaded.
1327 textureInfo.loadState = TextureManager::LoadState::UPLOADED;
1330 void TextureManager::NotifyObservers(TextureManager::TextureInfo& textureInfo, const bool success)
1332 TextureId textureId = textureInfo.textureId;
1334 // If there is an observer: Notify the load is complete, whether successful or not,
1335 // and erase it from the list
1336 TextureInfo* info = &textureInfo;
1338 if(info->animatedImageLoading)
1340 // If loading failed, we don't need to get frameCount and frameInterval.
1343 info->frameCount = info->animatedImageLoading.GetImageCount();
1344 info->frameInterval = info->animatedImageLoading.GetFrameInterval(info->frameIndex);
1346 info->animatedImageLoading.Reset();
1349 mLoadingQueueTextureId = textureId;
1351 // Reverse observer list that we can pop_back the observer.
1352 std::reverse(info->observerList.Begin(), info->observerList.End());
1354 while(info->observerList.Count())
1356 TextureUploadObserver* observer = *(info->observerList.End() - 1u);
1358 // During LoadComplete() a Control ResourceReady() signal is emitted.
1359 // During that signal the app may add remove /add Textures (e.g. via
1361 // It is possible for observers to be removed from the observer list,
1362 // and it is also possible for the mTextureInfoContainer to be modified,
1363 // invalidating the reference to the textureInfo struct.
1364 // Texture load requests for the same URL are deferred until the end of this
1366 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));
1367 // It is possible for the observer to be deleted.
1368 // Disconnect and remove the observer first.
1369 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Verbose, " Disconnect DestructionSignal to observer:%p\n", observer);
1370 observer->DestructionSignal().Disconnect(this, &TextureManager::ObserverDestroyed);
1372 info->observerList.Erase(info->observerList.End() - 1u);
1374 EmitLoadComplete(observer, *info, success);
1376 // Get the textureInfo from the container again as it may have been invalidated.
1377 TextureCacheIndex textureInfoIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
1378 if(textureInfoIndex == INVALID_CACHE_INDEX)
1380 break; // texture has been removed - can stop.
1382 info = &mTextureCacheManager[textureInfoIndex];
1385 mLoadingQueueTextureId = INVALID_TEXTURE_ID;
1388 if(info->storageType == TextureManager::StorageType::RETURN_PIXEL_BUFFER && info->observerList.Count() == 0)
1390 RequestRemove(info->textureId, nullptr);
1394 void TextureManager::ObserverDestroyed(TextureUploadObserver* observer)
1396 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Verbose, "TextureManager::ObserverDestroyed() observer:%p\n", observer);
1398 const std::size_t size = mTextureCacheManager.size();
1399 for(TextureCacheIndex cacheIndex = TextureCacheIndex(TextureManagerType::TEXTURE_CACHE_INDEX_TYPE_LOCAL, 0u); cacheIndex.GetIndex() < size; ++cacheIndex.detailValue.index)
1401 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
1402 for(TextureInfo::ObserverListType::Iterator j = textureInfo.observerList.Begin();
1403 j != textureInfo.observerList.End();)
1407 j = textureInfo.observerList.Erase(j);
1416 // Remove element from the LoadQueue
1417 for(auto&& element : mLoadQueue)
1419 if(element.mObserver == observer)
1421 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Verbose, "Remove observer from observer queue (textureId:%d, observer:%p)\n", element.mTextureId, element.mObserver);
1422 element.mTextureId = INVALID_TEXTURE_ID;
1423 element.mObserver = nullptr;
1428 Dali::Geometry TextureManager::GetRenderGeometry(const TextureManager::TextureId textureId, uint32_t& frontElements, uint32_t& backElements)
1430 return RenderingAddOn::Get().IsValid() ? RenderingAddOn::Get().GetGeometry(textureId, frontElements, backElements) : Geometry();
1433 void TextureManager::EmitLoadComplete(TextureUploadObserver* observer, TextureManager::TextureInfo& textureInfo, const bool success)
1435 if(textureInfo.storageType == TextureManager::StorageType::RETURN_PIXEL_BUFFER)
1437 observer->LoadComplete(success, TextureUploadObserver::TextureInformation(TextureUploadObserver::ReturnType::PIXEL_BUFFER, textureInfo.pixelBuffer, textureInfo.url.GetUrl(), textureInfo.preMultiplied));
1441 TextureSet textureSet = GetTextureSet(textureInfo);
1442 if(textureInfo.isAnimatedImageFormat)
1444 observer->LoadComplete(success, TextureUploadObserver::TextureInformation(TextureUploadObserver::ReturnType::ANIMATED_IMAGE_TEXTURE, textureInfo.textureId, textureSet, textureInfo.frameCount, textureInfo.frameInterval));
1448 observer->LoadComplete(success, TextureUploadObserver::TextureInformation(TextureUploadObserver::ReturnType::TEXTURE, textureInfo.textureId, textureSet, textureInfo.preMultiplied));
1453 TextureSet TextureManager::GetTextureSet(const TextureManager::TextureId textureId)
1455 TextureSet textureSet;
1456 TextureManager::LoadState loadState = mTextureCacheManager.GetTextureStateInternal(textureId);
1457 if(loadState == TextureManager::LoadState::UPLOADED)
1459 TextureCacheIndex textureCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
1460 if(textureCacheIndex != INVALID_CACHE_INDEX)
1462 TextureInfo& textureInfo(mTextureCacheManager[textureCacheIndex]);
1463 textureSet = GetTextureSet(textureInfo);
1468 DALI_LOG_ERROR("GetTextureSet is failed. texture is not uploaded \n");
1473 TextureSet TextureManager::GetTextureSet(const TextureManager::TextureInfo& textureInfo)
1475 TextureSet textureSet;
1477 // Always create new TextureSet here, so we don't share same TextureSets for multiple visuals.
1478 textureSet = TextureSet::New();
1480 if(!textureInfo.textures.empty())
1482 if(textureInfo.textures.size() > 1) // For YUV case
1484 uint32_t index = 0u;
1485 for(auto&& texture : textureInfo.textures)
1487 textureSet.SetTexture(index++, texture);
1492 textureSet.SetTexture(TEXTURE_INDEX, textureInfo.textures[0]);
1493 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureInfo.maskTextureId);
1494 if(maskCacheIndex != INVALID_CACHE_INDEX)
1496 TextureInfo& maskTextureInfo(mTextureCacheManager[maskCacheIndex]);
1497 if(maskTextureInfo.storageType == TextureManager::StorageType::UPLOAD_TO_TEXTURE || maskTextureInfo.storageType == TextureManager::StorageType::KEEP_TEXTURE)
1499 if(!maskTextureInfo.textures.empty())
1501 textureSet.SetTexture(MASK_TEXTURE_INDEX, maskTextureInfo.textures[0]);
1510 void TextureManager::RemoveTextureObserver(TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
1514 const auto iterEnd = textureInfo.observerList.End();
1515 const auto iter = std::find(textureInfo.observerList.Begin(), iterEnd, observer);
1518 // Disconnect and remove the observer.
1519 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Verbose, " Disconnect DestructionSignal to observer:%p\n", observer);
1520 observer->DestructionSignal().Disconnect(this, &TextureManager::ObserverDestroyed);
1521 textureInfo.observerList.Erase(iter);
1525 // Given textureId might exist at load queue.
1526 // Remove observer from the LoadQueue
1527 for(auto&& element : mLoadQueue)
1529 if(element.mTextureId == textureInfo.textureId && element.mObserver == observer)
1531 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Verbose, "Remove observer from observer queue (textureId:%d, observer:%p)\n", element.mTextureId, element.mObserver);
1532 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Verbose, " Disconnect DestructionSignal to observer:%p\n", observer);
1533 observer->DestructionSignal().Disconnect(this, &TextureManager::ObserverDestroyed);
1534 element.mObserver = nullptr;
1542 } // namespace Internal
1544 } // namespace Toolkit