2 * Copyright (c) 2022 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/visuals/image/image-visual.h>
22 #include <dali/devel-api/adaptor-framework/image-loading.h>
23 #include <dali/devel-api/common/stage.h>
24 #include <dali/devel-api/rendering/renderer-devel.h>
25 #include <dali/devel-api/rendering/texture-devel.h>
26 #include <dali/devel-api/scripting/enum-helper.h>
27 #include <dali/devel-api/scripting/scripting.h>
28 #include <dali/integration-api/debug.h>
29 #include <dali/public-api/actors/layer.h>
30 #include <dali/public-api/rendering/decorated-visual-renderer.h>
31 #include <cstring> // for strlen()
34 #include <dali-toolkit/devel-api/visuals/image-visual-actions-devel.h>
35 #include <dali-toolkit/internal/texture-manager/texture-manager-impl.h>
36 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
37 #include <dali-toolkit/internal/visuals/image-visual-shader-factory.h>
38 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
39 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
40 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
41 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
42 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
43 #include <dali-toolkit/internal/visuals/visual-url.h>
44 #include <dali-toolkit/public-api/visuals/image-visual-properties.h>
45 #include <dali-toolkit/public-api/visuals/visual-properties.h>
55 const int CUSTOM_PROPERTY_COUNT(7); // ltr, wrap, pixel area, atlas, pixalign, crop to mask, mask texture ratio
58 DALI_ENUM_TO_STRING_TABLE_BEGIN(FITTING_MODE)
59 DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::FittingMode, SHRINK_TO_FIT)
60 DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::FittingMode, SCALE_TO_FILL)
61 DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::FittingMode, FIT_WIDTH)
62 DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::FittingMode, FIT_HEIGHT)
63 DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::FittingMode, DEFAULT)
64 DALI_ENUM_TO_STRING_TABLE_END(FITTING_MODE)
67 DALI_ENUM_TO_STRING_TABLE_BEGIN(SAMPLING_MODE)
68 DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::SamplingMode, BOX)
69 DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::SamplingMode, NEAREST)
70 DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::SamplingMode, LINEAR)
71 DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::SamplingMode, BOX_THEN_NEAREST)
72 DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::SamplingMode, BOX_THEN_LINEAR)
73 DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::SamplingMode, NO_FILTER)
74 DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::SamplingMode, DONT_CARE)
75 DALI_ENUM_TO_STRING_TABLE_END(SAMPLING_MODE)
78 DALI_ENUM_TO_STRING_TABLE_BEGIN(WRAP_MODE)
79 DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::WrapMode, DEFAULT)
80 DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::WrapMode, CLAMP_TO_EDGE)
81 DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::WrapMode, REPEAT)
82 DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::WrapMode, MIRRORED_REPEAT)
83 DALI_ENUM_TO_STRING_TABLE_END(WRAP_MODE)
86 DALI_ENUM_TO_STRING_TABLE_BEGIN(LOAD_POLICY)
87 DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::Toolkit::ImageVisual::LoadPolicy, IMMEDIATE)
88 DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::Toolkit::ImageVisual::LoadPolicy, ATTACHED)
89 DALI_ENUM_TO_STRING_TABLE_END(LOAD_POLICY)
92 DALI_ENUM_TO_STRING_TABLE_BEGIN(RELEASE_POLICY)
93 DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::Toolkit::ImageVisual::ReleasePolicy, DETACHED)
94 DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::Toolkit::ImageVisual::ReleasePolicy, DESTROYED)
95 DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::Toolkit::ImageVisual::ReleasePolicy, NEVER)
96 DALI_ENUM_TO_STRING_TABLE_END(RELEASE_POLICY)
98 const Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
100 const float PIXEL_ALIGN_ON = 1.0f;
101 const float PIXEL_ALIGN_OFF = 0.0f;
103 constexpr uint32_t TEXTURE_COUNT_FOR_GPU_ALPHA_MASK = 2u;
105 Geometry CreateGeometry(VisualFactoryCache& factoryCache, ImageDimensions gridSize)
109 if(gridSize == ImageDimensions(1, 1))
111 geometry = factoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
115 geometry = VisualFactoryCache::CreateGridGeometry(gridSize);
121 } // unnamed namespace
123 ImageVisualPtr ImageVisual::New(VisualFactoryCache& factoryCache,
124 ImageVisualShaderFactory& shaderFactory,
125 const VisualUrl& imageUrl,
126 const Property::Map& properties,
127 ImageDimensions size,
128 FittingMode::Type fittingMode,
129 Dali::SamplingMode::Type samplingMode)
131 ImageVisualPtr imageVisualPtr(new ImageVisual(factoryCache, shaderFactory, imageUrl, size, fittingMode, samplingMode));
132 imageVisualPtr->SetProperties(properties);
133 imageVisualPtr->Initialize();
134 return imageVisualPtr;
137 ImageVisualPtr ImageVisual::New(VisualFactoryCache& factoryCache,
138 ImageVisualShaderFactory& shaderFactory,
139 const VisualUrl& imageUrl,
140 ImageDimensions size,
141 FittingMode::Type fittingMode,
142 Dali::SamplingMode::Type samplingMode)
144 ImageVisualPtr imageVisualPtr(new ImageVisual(factoryCache, shaderFactory, imageUrl, size, fittingMode, samplingMode));
145 imageVisualPtr->Initialize();
146 return imageVisualPtr;
149 ImageVisual::ImageVisual(VisualFactoryCache& factoryCache,
150 ImageVisualShaderFactory& shaderFactory,
151 const VisualUrl& imageUrl,
152 ImageDimensions size,
153 FittingMode::Type fittingMode,
154 Dali::SamplingMode::Type samplingMode)
155 : Visual::Base(factoryCache, Visual::FittingMode::FILL, Toolkit::Visual::IMAGE),
156 mPixelArea(FULL_TEXTURE_RECT),
161 mTextureId(TextureManager::INVALID_TEXTURE_ID),
163 mImageVisualShaderFactory(shaderFactory),
164 mFittingMode(fittingMode),
165 mSamplingMode(samplingMode),
166 mWrapModeU(WrapMode::DEFAULT),
167 mWrapModeV(WrapMode::DEFAULT),
168 mLoadPolicy(Toolkit::ImageVisual::LoadPolicy::ATTACHED),
169 mReleasePolicy(Toolkit::ImageVisual::ReleasePolicy::DETACHED),
170 mAtlasRect(0.0f, 0.0f, 0.0f, 0.0f),
171 mAtlasRectSize(0, 0),
172 mLoadState(TextureManager::LoadState::NOT_STARTED),
173 mAttemptAtlasing(false),
174 mOrientationCorrection(true)
176 EnablePreMultipliedAlpha(mFactoryCache.GetPreMultiplyOnLoad());
179 ImageVisual::~ImageVisual()
181 if(Stage::IsInstalled())
185 // TextureManager could have been deleted before the actor that contains this
186 // ImageVisual is destroyed (e.g. due to stage shutdown). Ensure the stage
187 // is still valid before accessing texture manager.
188 if(mMaskingData->mAlphaMaskId != TextureManager::INVALID_TEXTURE_ID)
190 TextureManager& textureManager = mFactoryCache.GetTextureManager();
191 textureManager.Remove(mMaskingData->mAlphaMaskId, this);
195 if(mImageUrl.IsValid())
197 // Decrease reference count of External Resources :
198 // EncodedImageBuffer or ExternalTextures.
199 // Ensure the stage is still valid before accessing texture manager.
200 if(mImageUrl.GetProtocolType() == VisualUrl::TEXTURE)
202 TextureManager& textureManager = mFactoryCache.GetTextureManager();
203 textureManager.RemoveExternalTexture(mImageUrl.GetUrl());
205 else if(mImageUrl.IsBufferResource())
207 TextureManager& textureManager = mFactoryCache.GetTextureManager();
208 textureManager.RemoveEncodedImageBuffer(mImageUrl.GetUrl());
212 // ImageVisual destroyed so remove texture unless ReleasePolicy is set to never release
213 if((mTextureId != TextureManager::INVALID_TEXTURE_ID) && (mReleasePolicy != Toolkit::ImageVisual::ReleasePolicy::NEVER))
220 void ImageVisual::DoSetProperties(const Property::Map& propertyMap)
222 // Url is already received in constructor
223 for(Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter)
225 KeyValuePair keyValue = propertyMap.GetKeyValue(iter);
226 if(keyValue.first.type == Property::Key::INDEX)
228 DoSetProperty(keyValue.first.indexKey, keyValue.second);
232 if(keyValue.first == IMAGE_FITTING_MODE)
234 DoSetProperty(Toolkit::ImageVisual::Property::FITTING_MODE, keyValue.second);
236 else if(keyValue.first == IMAGE_SAMPLING_MODE)
238 DoSetProperty(Toolkit::ImageVisual::Property::SAMPLING_MODE, keyValue.second);
240 else if(keyValue.first == IMAGE_DESIRED_WIDTH)
242 DoSetProperty(Toolkit::ImageVisual::Property::DESIRED_WIDTH, keyValue.second);
244 else if(keyValue.first == IMAGE_DESIRED_HEIGHT)
246 DoSetProperty(Toolkit::ImageVisual::Property::DESIRED_HEIGHT, keyValue.second);
248 else if(keyValue.first == PIXEL_AREA_UNIFORM_NAME)
250 DoSetProperty(Toolkit::ImageVisual::Property::PIXEL_AREA, keyValue.second);
252 else if(keyValue.first == IMAGE_WRAP_MODE_U)
254 DoSetProperty(Toolkit::ImageVisual::Property::WRAP_MODE_U, keyValue.second);
256 else if(keyValue.first == IMAGE_WRAP_MODE_V)
258 DoSetProperty(Toolkit::ImageVisual::Property::WRAP_MODE_V, keyValue.second);
260 else if(keyValue.first == SYNCHRONOUS_LOADING)
262 DoSetProperty(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, keyValue.second);
264 else if(keyValue.first == IMAGE_ATLASING)
266 DoSetProperty(Toolkit::ImageVisual::Property::ATLASING, keyValue.second);
268 else if(keyValue.first == ALPHA_MASK_URL)
270 DoSetProperty(Toolkit::ImageVisual::Property::ALPHA_MASK_URL, keyValue.second);
272 else if(keyValue.first == MASK_CONTENT_SCALE_NAME)
274 DoSetProperty(Toolkit::ImageVisual::Property::MASK_CONTENT_SCALE, keyValue.second);
276 else if(keyValue.first == CROP_TO_MASK_NAME)
278 DoSetProperty(Toolkit::ImageVisual::Property::CROP_TO_MASK, keyValue.second);
280 else if(keyValue.first == MASKING_TYPE_NAME)
282 DoSetProperty(Toolkit::DevelImageVisual::Property::MASKING_TYPE, keyValue.second);
284 else if(keyValue.first == LOAD_POLICY_NAME)
286 DoSetProperty(Toolkit::ImageVisual::Property::LOAD_POLICY, keyValue.second);
288 else if(keyValue.first == RELEASE_POLICY_NAME)
290 DoSetProperty(Toolkit::ImageVisual::Property::RELEASE_POLICY, keyValue.second);
292 else if(keyValue.first == ORIENTATION_CORRECTION_NAME)
294 DoSetProperty(Toolkit::ImageVisual::Property::ORIENTATION_CORRECTION, keyValue.second);
298 // Load image immediately if LOAD_POLICY requires it
299 if(mLoadPolicy == Toolkit::ImageVisual::LoadPolicy::IMMEDIATE)
301 auto attemptAtlasing = AttemptAtlasing();
302 LoadTexture(attemptAtlasing, mAtlasRect, mTextures, mOrientationCorrection, TextureManager::ReloadPolicy::CACHED);
306 void ImageVisual::DoSetProperty(Property::Index index, const Property::Value& value)
310 case Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING:
317 mImpl->mFlags |= Visual::Base::Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
321 mImpl->mFlags &= ~Visual::Base::Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
326 DALI_LOG_ERROR("ImageVisual: synchronousLoading property has incorrect type\n");
331 case Toolkit::ImageVisual::Property::DESIRED_WIDTH:
333 float desiredWidth = 0.0f;
334 if(value.Get(desiredWidth))
336 mDesiredSize.SetWidth(desiredWidth);
340 DALI_LOG_ERROR("ImageVisual: desiredWidth property has incorrect type\n");
345 case Toolkit::ImageVisual::Property::DESIRED_HEIGHT:
347 float desiredHeight = 0.0f;
348 if(value.Get(desiredHeight))
350 mDesiredSize.SetHeight(desiredHeight);
354 DALI_LOG_ERROR("ImageVisual: desiredHeight property has incorrect type\n");
359 case Toolkit::ImageVisual::Property::FITTING_MODE:
362 Scripting::GetEnumerationProperty(value, FITTING_MODE_TABLE, FITTING_MODE_TABLE_COUNT, fittingMode);
363 mFittingMode = Dali::FittingMode::Type(fittingMode);
367 case Toolkit::ImageVisual::Property::SAMPLING_MODE:
369 int samplingMode = 0;
370 Scripting::GetEnumerationProperty(value, SAMPLING_MODE_TABLE, SAMPLING_MODE_TABLE_COUNT, samplingMode);
371 mSamplingMode = Dali::SamplingMode::Type(samplingMode);
375 case Toolkit::ImageVisual::Property::PIXEL_AREA:
377 value.Get(mPixelArea);
381 case Toolkit::ImageVisual::Property::WRAP_MODE_U:
384 Scripting::GetEnumerationProperty(value, WRAP_MODE_TABLE, WRAP_MODE_TABLE_COUNT, wrapMode);
385 mWrapModeU = Dali::WrapMode::Type(wrapMode);
389 case Toolkit::ImageVisual::Property::WRAP_MODE_V:
392 Scripting::GetEnumerationProperty(value, WRAP_MODE_TABLE, WRAP_MODE_TABLE_COUNT, wrapMode);
393 mWrapModeV = Dali::WrapMode::Type(wrapMode);
397 case Toolkit::ImageVisual::Property::ATLASING:
399 value.Get(mAttemptAtlasing);
403 case Toolkit::ImageVisual::Property::ALPHA_MASK_URL:
405 std::string alphaUrl = "";
406 if(value.Get(alphaUrl))
409 mMaskingData->mAlphaMaskUrl = alphaUrl;
414 case Toolkit::ImageVisual::Property::MASK_CONTENT_SCALE:
420 mMaskingData->mContentScaleFactor = scale;
425 case Toolkit::ImageVisual::Property::CROP_TO_MASK:
431 mMaskingData->mCropToMask = crop;
436 case Toolkit::DevelImageVisual::Property::MASKING_TYPE:
439 if(value.Get(maskingType))
442 mMaskingData->mPreappliedMasking = Toolkit::DevelImageVisual::MaskingType::Type(maskingType) == Toolkit::DevelImageVisual::MaskingType::MASKING_ON_LOADING ? true : false;
447 case Toolkit::ImageVisual::Property::RELEASE_POLICY:
449 int releasePolicy = 0;
450 Scripting::GetEnumerationProperty(value, RELEASE_POLICY_TABLE, RELEASE_POLICY_TABLE_COUNT, releasePolicy);
451 mReleasePolicy = Toolkit::ImageVisual::ReleasePolicy::Type(releasePolicy);
455 case Toolkit::ImageVisual::Property::LOAD_POLICY:
458 Scripting::GetEnumerationProperty(value, LOAD_POLICY_TABLE, LOAD_POLICY_TABLE_COUNT, loadPolicy);
459 mLoadPolicy = Toolkit::ImageVisual::LoadPolicy::Type(loadPolicy);
462 case Toolkit::ImageVisual::Property::ORIENTATION_CORRECTION:
464 bool orientationCorrection(mOrientationCorrection);
465 if(value.Get(orientationCorrection))
467 mOrientationCorrection = orientationCorrection;
474 void ImageVisual::AllocateMaskData()
478 mMaskingData.reset(new TextureManager::MaskingData());
482 void ImageVisual::GetNaturalSize(Vector2& naturalSize)
484 if(mDesiredSize.GetWidth() > 0 && mDesiredSize.GetHeight() > 0)
486 naturalSize.x = mDesiredSize.GetWidth();
487 naturalSize.y = mDesiredSize.GetHeight();
490 else if(mImpl->mRenderer) // Check if we have a loaded image
492 if(mImpl->mFlags & Visual::Base::Impl::IS_ATLASING_APPLIED)
494 naturalSize.x = mAtlasRectSize.GetWidth();
495 naturalSize.y = mAtlasRectSize.GetHeight();
499 auto textureSet = mImpl->mRenderer.GetTextures();
500 if(textureSet && textureSet.GetTextureCount())
502 if(mTextureSize != Vector2::ZERO)
504 naturalSize = mTextureSize;
510 if(mMaskingData != NULL && mMaskingData->mAlphaMaskUrl.IsValid() &&
511 mMaskingData->mCropToMask)
513 ImageDimensions dimensions = Dali::GetClosestImageSize(mMaskingData->mAlphaMaskUrl.GetUrl());
514 if(dimensions != ImageDimensions(0, 0))
516 naturalSize.x = dimensions.GetWidth();
517 naturalSize.y = dimensions.GetHeight();
521 else if(mImageUrl.IsValid())
523 if(mImageUrl.GetProtocolType() == VisualUrl::LOCAL)
525 ImageDimensions dimensions = Dali::GetClosestImageSize(mImageUrl.GetUrl());
527 if(dimensions != ImageDimensions(0, 0))
529 naturalSize.x = dimensions.GetWidth();
530 naturalSize.y = dimensions.GetHeight();
534 Actor actor = mPlacementActor.GetHandle();
535 Vector2 imageSize = Vector2::ZERO;
538 imageSize = actor.GetProperty(Actor::Property::SIZE).Get<Vector2>();
540 mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize);
541 Texture brokenImage = mImpl->mRenderer.GetTextures().GetTexture(0);
542 naturalSize.x = brokenImage.GetWidth();
543 naturalSize.y = brokenImage.GetWidth();
548 naturalSize = Vector2::ZERO;
551 void ImageVisual::OnInitialize()
556 if(mImpl->mCustomShader)
558 geometry = CreateGeometry(mFactoryCache, mImpl->mCustomShader->mGridSize);
560 else // Get any geometry associated with the texture
562 TextureManager& textureManager = mFactoryCache.GetTextureManager();
564 uint32_t firstElementCount{0u};
565 uint32_t secondElementCount{0u};
566 geometry = textureManager.GetRenderGeometry(mTextureId, firstElementCount, secondElementCount);
568 if(!firstElementCount && !secondElementCount) // Otherwise use quad
570 geometry = CreateGeometry(mFactoryCache, ImageDimensions(1, 1));
574 // Increase reference count of External Resources :
575 // EncodedImageBuffer or ExternalTextures.
576 // Reference count will be decreased at destructor of the visual.
577 if(mImageUrl.IsValid() && (mImageUrl.IsBufferResource() || mImageUrl.GetProtocolType() == VisualUrl::TEXTURE))
579 TextureManager& textureManager = mFactoryCache.GetTextureManager();
580 textureManager.UseExternalResource(mImageUrl.GetUrl());
583 Shader shader = GenerateShader();
585 // Create the renderer
586 mImpl->mRenderer = DecoratedVisualRenderer::New(geometry, shader);
587 mImpl->mRenderer.ReserveCustomProperties(CUSTOM_PROPERTY_COUNT);
589 //Register transform properties
590 mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
592 EnablePreMultipliedAlpha(IsPreMultipliedAlphaEnabled());
596 mImpl->mRenderer.RegisterProperty(CROP_TO_MASK_NAME, static_cast<float>(mMaskingData->mCropToMask));
600 void ImageVisual::LoadTexture(bool& atlasing, Vector4& atlasRect, TextureSet& textures, bool orientationCorrection, TextureManager::ReloadPolicy forceReload)
602 TextureManager& textureManager = mFactoryCache.GetTextureManager();
604 ImageAtlasManagerPtr atlasManager = nullptr;
605 AtlasUploadObserver* atlasUploadObserver = nullptr;
606 auto textureObserver = this;
610 atlasManager = mFactoryCache.GetAtlasManager();
611 atlasUploadObserver = this;
614 auto preMultiplyOnLoad = IsPreMultipliedAlphaEnabled() && !mImpl->mCustomShader
615 ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD
616 : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
618 bool synchronousLoading = IsSynchronousLoadingRequired();
621 textures = textureManager.LoadTexture(mImageUrl, mDesiredSize, mFittingMode, mSamplingMode, mMaskingData, synchronousLoading, mTextureId, atlasRect, mAtlasRectSize, atlasing, loadingStatus, mWrapModeU, mWrapModeV, textureObserver, atlasUploadObserver, atlasManager, mOrientationCorrection, forceReload, preMultiplyOnLoad);
627 mLoadState = TextureManager::LoadState::LOADING;
631 mLoadState = TextureManager::LoadState::LOAD_FINISHED;
634 EnablePreMultipliedAlpha(preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD);
636 else if(synchronousLoading)
638 // Synchronous loading is failed
639 mLoadState = TextureManager::LoadState::LOAD_FAILED;
642 if(atlasing) // Flag needs to be set before creating renderer
644 mImpl->mFlags |= Visual::Base::Impl::IS_ATLASING_APPLIED;
648 mImpl->mFlags &= ~Visual::Base::Impl::IS_ATLASING_APPLIED;
652 bool ImageVisual::AttemptAtlasing()
654 return (!mImpl->mCustomShader && (mImageUrl.IsLocalResource() || mImageUrl.IsBufferResource()) && mAttemptAtlasing);
657 void ImageVisual::InitializeRenderer()
659 auto attemptAtlasing = AttemptAtlasing();
661 // Load Texture if mTextures is empty.
662 // mTextures is already set, the mTexture can be used to create Renderer.
663 // There are two cases mTextures is empty.
664 // 1. mTextureId == TextureManager::INVALID_TEXTURE_ID
665 // - Visual is on stage with LoadPolicy::ATTACHED
666 // 2. mTextureId != TextureManager::INVALID_TEXTURE_ID
667 // - If ReleasePolicy is DESTROYED, InitializeRenderer called every on stage called.
668 // - Then every resources those contained in Visual are Reset but mTextureId is remained when the Off stage time,
669 // - So, mTextures needed to be get from texture manager to created resources like mImpl->mRenderer.
672 if(mTextureId == TextureManager::INVALID_TEXTURE_ID)
674 LoadTexture(attemptAtlasing, mAtlasRect, mTextures, mOrientationCorrection, TextureManager::ReloadPolicy::CACHED);
678 mTextures = mFactoryCache.GetTextureManager().GetTextureSet(mTextureId);
684 mImpl->mRenderer.SetTextures(mTextures);
685 ComputeTextureSize();
687 if(DevelTexture::IsNative(mTextures.GetTexture(0)))
691 mTextures.Reset(); // Visual should not keep a handle to the texture after this point.
694 if(attemptAtlasing) // the texture is packed inside atlas
696 mImpl->mRenderer.RegisterProperty(ATLAS_RECT_UNIFORM_NAME, mAtlasRect);
698 bool defaultWrapMode = mWrapModeU <= WrapMode::CLAMP_TO_EDGE && mWrapModeV <= WrapMode::CLAMP_TO_EDGE;
700 if(!defaultWrapMode) // custom wrap mode
702 Vector2 wrapMode(mWrapModeU - WrapMode::CLAMP_TO_EDGE, mWrapModeV - WrapMode::CLAMP_TO_EDGE);
703 wrapMode.Clamp(Vector2::ZERO, Vector2(2.f, 2.f));
704 mImpl->mRenderer.RegisterProperty(WRAP_MODE_UNIFORM_NAME, wrapMode);
709 void ImageVisual::DoSetOnScene(Actor& actor)
711 if(mImageUrl.IsValid())
713 InitializeRenderer();
716 if(!mImpl->mRenderer)
721 mPlacementActor = actor;
722 // Search the Actor tree to find if Layer UI behaviour set.
723 Layer layer = actor.GetLayer();
724 if(layer && layer.GetProperty<Layer::Behavior>(Layer::Property::BEHAVIOR) == Layer::LAYER_3D)
726 // Layer 3D set, do not align pixels
727 mImpl->mRenderer.RegisterProperty(PIXEL_ALIGNED_UNIFORM_NAME, PIXEL_ALIGN_OFF);
730 if(mPixelArea != FULL_TEXTURE_RECT)
732 mImpl->mRenderer.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, mPixelArea);
735 if(mLoadState == TextureManager::LoadState::LOAD_FINISHED)
737 actor.AddRenderer(mImpl->mRenderer);
738 mPlacementActor.Reset();
740 // Image loaded and ready to display
741 ResourceReady(Toolkit::Visual::ResourceStatus::READY);
743 else if(mLoadState == TextureManager::LoadState::LOAD_FAILED)
745 Vector2 imageSize = Vector2::ZERO;
748 imageSize = actor.GetProperty(Actor::Property::SIZE).Get<Vector2>();
750 mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize);
751 actor.AddRenderer(mImpl->mRenderer);
752 mPlacementActor.Reset();
754 ResourceReady(Toolkit::Visual::ResourceStatus::FAILED);
758 void ImageVisual::DoSetOffScene(Actor& actor)
760 // Visual::Base::SetOffScene only calls DoSetOffScene if mRenderer exists (is on onstage)
762 // Image release is dependent on the ReleasePolicy, renderer is removed.
763 actor.RemoveRenderer(mImpl->mRenderer);
764 if(mReleasePolicy == Toolkit::ImageVisual::ReleasePolicy::DETACHED)
766 RemoveTexture(); // If INVALID_TEXTURE_ID then removal will be attempted on atlas
767 mImpl->mResourceStatus = Toolkit::Visual::ResourceStatus::PREPARING;
769 TextureSet textureSet = TextureSet::New();
770 mImpl->mRenderer.SetTextures(textureSet);
771 ComputeTextureSize();
773 mLoadState = TextureManager::LoadState::NOT_STARTED;
776 mPlacementActor.Reset();
779 void ImageVisual::DoCreatePropertyMap(Property::Map& map) const
782 map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE);
784 bool sync = IsSynchronousLoadingRequired();
785 map.Insert(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, sync);
786 if(mImageUrl.IsValid())
788 map.Insert(Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl());
789 map.Insert(Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth());
790 map.Insert(Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight());
793 map.Insert(Toolkit::ImageVisual::Property::FITTING_MODE, mFittingMode);
794 map.Insert(Toolkit::ImageVisual::Property::SAMPLING_MODE, mSamplingMode);
796 map.Insert(Toolkit::ImageVisual::Property::PIXEL_AREA, mPixelArea);
797 map.Insert(Toolkit::ImageVisual::Property::WRAP_MODE_U, mWrapModeU);
798 map.Insert(Toolkit::ImageVisual::Property::WRAP_MODE_V, mWrapModeV);
800 map.Insert(Toolkit::ImageVisual::Property::ATLASING, mAttemptAtlasing);
802 if(mMaskingData != NULL)
804 map.Insert(Toolkit::ImageVisual::Property::ALPHA_MASK_URL, mMaskingData->mAlphaMaskUrl.GetUrl());
805 map.Insert(Toolkit::ImageVisual::Property::MASK_CONTENT_SCALE, mMaskingData->mContentScaleFactor);
806 map.Insert(Toolkit::ImageVisual::Property::CROP_TO_MASK, mMaskingData->mCropToMask);
807 map.Insert(Toolkit::DevelImageVisual::Property::MASKING_TYPE, mMaskingData->mPreappliedMasking ? DevelImageVisual::MaskingType::MASKING_ON_LOADING : DevelImageVisual::MaskingType::MASKING_ON_RENDERING);
810 map.Insert(Toolkit::ImageVisual::Property::LOAD_POLICY, mLoadPolicy);
811 map.Insert(Toolkit::ImageVisual::Property::RELEASE_POLICY, mReleasePolicy);
812 map.Insert(Toolkit::ImageVisual::Property::ORIENTATION_CORRECTION, mOrientationCorrection);
815 void ImageVisual::DoCreateInstancePropertyMap(Property::Map& map) const
818 map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE);
819 if(mImageUrl.IsValid())
821 map.Insert(Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth());
822 map.Insert(Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight());
826 void ImageVisual::OnDoAction(const Dali::Property::Index actionId, const Dali::Property::Value& attributes)
828 // Check if action is valid for this visual type and perform action if possible
832 case DevelImageVisual::Action::RELOAD:
834 auto attemptAtlasing = AttemptAtlasing();
835 LoadTexture(attemptAtlasing, mAtlasRect, mTextures, mOrientationCorrection, TextureManager::ReloadPolicy::FORCED);
841 void ImageVisual::OnSetTransform()
845 mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
849 void ImageVisual::UpdateShader()
853 Shader shader = GenerateShader();
854 mImpl->mRenderer.SetShader(shader);
858 // From existing atlas manager
859 void ImageVisual::UploadCompleted()
861 // Texture has been uploaded. If weak handle is holding a placement actor,
862 // it is the time to add the renderer to actor.
863 Actor actor = mPlacementActor.GetHandle();
866 mImpl->mRenderer.RegisterProperty(ATLAS_RECT_UNIFORM_NAME, mAtlasRect);
867 actor.AddRenderer(mImpl->mRenderer);
868 // reset the weak handle so that the renderer only get added to actor once
869 mPlacementActor.Reset();
873 ResourceReady(Toolkit::Visual::ResourceStatus::READY);
874 mLoadState = TextureManager::LoadState::LOAD_FINISHED;
877 // From Texture Manager
878 void ImageVisual::LoadComplete(bool loadingSuccess, TextureInformation textureInformation)
880 Toolkit::Visual::ResourceStatus resourceStatus;
883 if(textureInformation.useAtlasing)
885 mImpl->mRenderer.RegisterProperty(ATLAS_RECT_UNIFORM_NAME, mAtlasRect);
888 EnablePreMultipliedAlpha(textureInformation.preMultiplied);
890 Actor actor = mPlacementActor.GetHandle();
893 Vector2 imageSize = Vector2::ZERO;
896 imageSize = actor.GetProperty(Actor::Property::SIZE).Get<Vector2>();
898 mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize);
899 textureInformation.textureSet = mImpl->mRenderer.GetTextures();
903 Sampler sampler = Sampler::New();
904 sampler.SetWrapMode(mWrapModeU, mWrapModeV);
905 textureInformation.textureSet.SetSampler(0u, sampler);
906 mImpl->mRenderer.SetTextures(textureInformation.textureSet);
907 ComputeTextureSize();
913 actor.AddRenderer(mImpl->mRenderer);
914 // reset the weak handle so that the renderer only get added to actor once
915 mPlacementActor.Reset();
919 // Storing TextureSet needed when renderer staged.
920 if(!mImpl->mRenderer)
922 mTextures = textureInformation.textureSet;
925 // Image loaded, set status regardless of staged status.
928 resourceStatus = Toolkit::Visual::ResourceStatus::READY;
929 mLoadState = TextureManager::LoadState::LOAD_FINISHED;
933 resourceStatus = Toolkit::Visual::ResourceStatus::FAILED;
934 mLoadState = TextureManager::LoadState::LOAD_FAILED;
937 // use geometry if needed
940 uint32_t firstElementCount{0u};
941 uint32_t secondElementCount{0u};
942 auto geometry = mFactoryCache.GetTextureManager().GetRenderGeometry(mTextureId, firstElementCount, secondElementCount);
943 if(mImpl->mRenderer && geometry)
945 mImpl->mRenderer.SetGeometry(geometry);
946 Dali::DevelRenderer::DrawCommand drawCommand{};
947 drawCommand.drawType = DevelRenderer::DrawType::INDEXED;
949 if(firstElementCount)
951 drawCommand.firstIndex = 0;
952 drawCommand.elementCount = firstElementCount;
953 drawCommand.queue = DevelRenderer::RENDER_QUEUE_OPAQUE;
954 DevelRenderer::AddDrawCommand(mImpl->mRenderer, drawCommand);
957 if(secondElementCount)
959 drawCommand.firstIndex = firstElementCount;
960 drawCommand.elementCount = secondElementCount;
961 drawCommand.queue = DevelRenderer::RENDER_QUEUE_TRANSPARENT;
962 DevelRenderer::AddDrawCommand(mImpl->mRenderer, drawCommand);
967 // Signal to observers ( control ) that resources are ready. Must be all resources.
968 ResourceReady(resourceStatus);
971 void ImageVisual::RemoveTexture()
973 if(mTextureId != TextureManager::INVALID_TEXTURE_ID)
975 mFactoryCache.GetTextureManager().Remove(mTextureId, this);
976 mTextureId = TextureManager::INVALID_TEXTURE_ID;
980 Vector4 atlasRect(0.f, 0.f, 1.f, 1.f);
981 Property::Index index = mImpl->mRenderer.GetPropertyIndex(ATLAS_RECT_UNIFORM_NAME);
982 if(index != Property::INVALID_INDEX)
984 Property::Value atlasRectValue = mImpl->mRenderer.GetProperty(index);
985 atlasRectValue.Get(atlasRect);
988 TextureSet textureSet = mImpl->mRenderer.GetTextures();
990 if(index != Property::INVALID_INDEX)
992 mFactoryCache.GetAtlasManager()->Remove(textureSet, atlasRect);
997 void ImageVisual::ComputeTextureSize()
1001 auto textureSet = mImpl->mRenderer.GetTextures();
1002 if(textureSet && textureSet.GetTextureCount())
1004 auto texture = textureSet.GetTexture(0);
1007 mTextureSize.x = texture.GetWidth();
1008 mTextureSize.y = texture.GetHeight();
1009 if(textureSet.GetTextureCount() > 1u && mMaskingData && !mMaskingData->mPreappliedMasking && mMaskingData->mCropToMask)
1011 Texture maskTexture = textureSet.GetTexture(1);
1014 mTextureSize.x = std::min(static_cast<uint32_t>(mTextureSize.x * mMaskingData->mContentScaleFactor), maskTexture.GetWidth());
1015 mTextureSize.y = std::min(static_cast<uint32_t>(mTextureSize.y * mMaskingData->mContentScaleFactor), maskTexture.GetHeight());
1023 Vector2 ImageVisual::ComputeMaskTextureRatio()
1025 Vector2 maskTextureRatio;
1026 if(mImpl->mRenderer)
1028 auto textureSet = mImpl->mRenderer.GetTextures();
1029 if(textureSet && textureSet.GetTextureCount())
1031 auto texture = textureSet.GetTexture(0);
1034 if(textureSet.GetTextureCount() > 1u && mMaskingData && !mMaskingData->mPreappliedMasking && mMaskingData->mCropToMask)
1036 Texture maskTexture = textureSet.GetTexture(1);
1039 float textureWidth = std::max(static_cast<float>(texture.GetWidth() * mMaskingData->mContentScaleFactor), Dali::Math::MACHINE_EPSILON_1);
1040 float textureHeight = std::max(static_cast<float>(texture.GetHeight() * mMaskingData->mContentScaleFactor), Dali::Math::MACHINE_EPSILON_1);
1041 maskTextureRatio = Vector2(std::min(static_cast<float>(maskTexture.GetWidth()), textureWidth) / textureWidth,
1042 std::min(static_cast<float>(maskTexture.GetHeight()), textureHeight) / textureHeight);
1048 return maskTextureRatio;
1051 Shader ImageVisual::GenerateShader() const
1055 bool usesWholeTexture = true;
1056 const bool useStandardShader = !mImpl->mCustomShader;
1057 const bool useNativeImage = (mTextures && DevelTexture::IsNative(mTextures.GetTexture(0)));
1059 if(useStandardShader)
1061 bool requiredAlphaMaskingOnRendering = (mMaskingData && !mMaskingData->mMaskImageLoadingFailed) ? !mMaskingData->mPreappliedMasking : false;
1062 // Create and cache the standard shader
1063 shader = mImageVisualShaderFactory.GetShader(
1065 ImageVisualShaderFeature::FeatureBuilder()
1066 .EnableTextureAtlas(mImpl->mFlags & Visual::Base::Impl::IS_ATLASING_APPLIED && !useNativeImage)
1067 .ApplyDefaultTextureWrapMode(mWrapModeU <= WrapMode::CLAMP_TO_EDGE && mWrapModeV <= WrapMode::CLAMP_TO_EDGE)
1068 .EnableRoundedCorner(IsRoundedCornerRequired())
1069 .EnableBorderline(IsBorderlineRequired())
1070 .SetTextureForFragmentShaderCheck(useNativeImage ? mTextures.GetTexture(0) : Dali::Texture())
1071 .EnableAlphaMaskingOnRendering(requiredAlphaMaskingOnRendering));
1075 std::string_view vertexShaderView;
1076 std::string_view fragmentShaderView;
1078 if(mImpl->mCustomShader && !mImpl->mCustomShader->mVertexShader.empty())
1080 vertexShaderView = mImpl->mCustomShader->mVertexShader;
1081 usesWholeTexture = false; // Impossible to tell.
1085 vertexShaderView = mImageVisualShaderFactory.GetVertexShaderSource();
1088 if(mImpl->mCustomShader && !mImpl->mCustomShader->mFragmentShader.empty())
1090 fragmentShaderView = mImpl->mCustomShader->mFragmentShader;
1094 fragmentShaderView = mImageVisualShaderFactory.GetFragmentShaderSource();
1097 // If the texture is native, we may need to change prefix and sampler in
1098 // the fragment shader
1101 bool modifiedFragmentShader = false;
1102 Texture nativeTexture = mTextures.GetTexture(0);
1103 std::string fragmentShaderString = std::string(fragmentShaderView);
1105 modifiedFragmentShader = DevelTexture::ApplyNativeFragmentShader(nativeTexture, fragmentShaderString);
1106 if(modifiedFragmentShader)
1108 fragmentShaderView = fragmentShaderString;
1111 // Create shader here cause fragmentShaderString scope issue
1112 shader = Shader::New(vertexShaderView, fragmentShaderView, mImpl->mCustomShader->mHints);
1116 shader = Shader::New(vertexShaderView, fragmentShaderView, mImpl->mCustomShader->mHints);
1120 if(usesWholeTexture)
1122 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
1125 // Set pixel align off as default.
1126 // ToDo: Pixel align causes issues such as rattling image animation.
1127 // We should trun it off until issues are resolved
1128 shader.RegisterProperty(PIXEL_ALIGNED_UNIFORM_NAME, PIXEL_ALIGN_OFF);
1133 void ImageVisual::CheckMaskTexture()
1135 if(mMaskingData && !mMaskingData->mPreappliedMasking)
1137 bool maskLoadFailed = true;
1138 TextureSet textures = mImpl->mRenderer.GetTextures();
1139 if(textures && textures.GetTextureCount() >= TEXTURE_COUNT_FOR_GPU_ALPHA_MASK)
1141 if(mMaskingData->mCropToMask)
1143 mImpl->mRenderer.RegisterProperty(MASK_TEXTURE_RATIO_NAME, ComputeMaskTextureRatio());
1145 maskLoadFailed = false;
1148 if(mMaskingData->mMaskImageLoadingFailed != maskLoadFailed)
1150 mMaskingData->mMaskImageLoadingFailed = maskLoadFailed;
1156 } // namespace Internal
1158 } // namespace Toolkit