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 <cstring> // for strlen()
33 #include <dali-toolkit/devel-api/visuals/image-visual-actions-devel.h>
34 #include <dali-toolkit/internal/texture-manager/texture-manager-impl.h>
35 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
36 #include <dali-toolkit/internal/visuals/image-visual-shader-factory.h>
37 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
38 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
39 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
40 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
41 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
42 #include <dali-toolkit/internal/visuals/visual-url.h>
43 #include <dali-toolkit/public-api/visuals/image-visual-properties.h>
44 #include <dali-toolkit/public-api/visuals/visual-properties.h>
54 const int CUSTOM_PROPERTY_COUNT(9); // wrap, pixel area, atlas, pixalign, + border/corner
57 DALI_ENUM_TO_STRING_TABLE_BEGIN(FITTING_MODE)
58 DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::FittingMode, SHRINK_TO_FIT)
59 DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::FittingMode, SCALE_TO_FILL)
60 DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::FittingMode, FIT_WIDTH)
61 DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::FittingMode, FIT_HEIGHT)
62 DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::FittingMode, DEFAULT)
63 DALI_ENUM_TO_STRING_TABLE_END(FITTING_MODE)
66 DALI_ENUM_TO_STRING_TABLE_BEGIN(SAMPLING_MODE)
67 DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::SamplingMode, BOX)
68 DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::SamplingMode, NEAREST)
69 DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::SamplingMode, LINEAR)
70 DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::SamplingMode, BOX_THEN_NEAREST)
71 DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::SamplingMode, BOX_THEN_LINEAR)
72 DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::SamplingMode, NO_FILTER)
73 DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::SamplingMode, DONT_CARE)
74 DALI_ENUM_TO_STRING_TABLE_END(SAMPLING_MODE)
77 DALI_ENUM_TO_STRING_TABLE_BEGIN(WRAP_MODE)
78 DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::WrapMode, DEFAULT)
79 DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::WrapMode, CLAMP_TO_EDGE)
80 DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::WrapMode, REPEAT)
81 DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::WrapMode, MIRRORED_REPEAT)
82 DALI_ENUM_TO_STRING_TABLE_END(WRAP_MODE)
85 DALI_ENUM_TO_STRING_TABLE_BEGIN(LOAD_POLICY)
86 DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::Toolkit::ImageVisual::LoadPolicy, IMMEDIATE)
87 DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::Toolkit::ImageVisual::LoadPolicy, ATTACHED)
88 DALI_ENUM_TO_STRING_TABLE_END(LOAD_POLICY)
91 DALI_ENUM_TO_STRING_TABLE_BEGIN(RELEASE_POLICY)
92 DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::Toolkit::ImageVisual::ReleasePolicy, DETACHED)
93 DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::Toolkit::ImageVisual::ReleasePolicy, DESTROYED)
94 DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::Toolkit::ImageVisual::ReleasePolicy, NEVER)
95 DALI_ENUM_TO_STRING_TABLE_END(RELEASE_POLICY)
97 const Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
99 const float PIXEL_ALIGN_ON = 1.0f;
100 const float PIXEL_ALIGN_OFF = 0.0f;
102 Geometry CreateGeometry(VisualFactoryCache& factoryCache, ImageDimensions gridSize)
106 if(gridSize == ImageDimensions(1, 1))
108 geometry = factoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
112 geometry = VisualFactoryCache::CreateGridGeometry(gridSize);
118 } // unnamed namespace
120 ImageVisualPtr ImageVisual::New(VisualFactoryCache& factoryCache,
121 ImageVisualShaderFactory& shaderFactory,
122 const VisualUrl& imageUrl,
123 const Property::Map& properties,
124 ImageDimensions size,
125 FittingMode::Type fittingMode,
126 Dali::SamplingMode::Type samplingMode)
128 ImageVisualPtr imageVisualPtr(new ImageVisual(factoryCache, shaderFactory, imageUrl, size, fittingMode, samplingMode));
129 imageVisualPtr->SetProperties(properties);
130 imageVisualPtr->Initialize();
131 return imageVisualPtr;
134 ImageVisualPtr ImageVisual::New(VisualFactoryCache& factoryCache,
135 ImageVisualShaderFactory& shaderFactory,
136 const VisualUrl& imageUrl,
137 ImageDimensions size,
138 FittingMode::Type fittingMode,
139 Dali::SamplingMode::Type samplingMode)
141 ImageVisualPtr imageVisualPtr(new ImageVisual(factoryCache, shaderFactory, imageUrl, size, fittingMode, samplingMode));
142 imageVisualPtr->Initialize();
143 return imageVisualPtr;
146 ImageVisual::ImageVisual(VisualFactoryCache& factoryCache,
147 ImageVisualShaderFactory& shaderFactory,
148 const VisualUrl& imageUrl,
149 ImageDimensions size,
150 FittingMode::Type fittingMode,
151 Dali::SamplingMode::Type samplingMode)
152 : Visual::Base(factoryCache, Visual::FittingMode::FILL, Toolkit::Visual::IMAGE),
153 mPixelArea(FULL_TEXTURE_RECT),
158 mTextureId(TextureManager::INVALID_TEXTURE_ID),
160 mImageVisualShaderFactory(shaderFactory),
161 mFittingMode(fittingMode),
162 mSamplingMode(samplingMode),
163 mWrapModeU(WrapMode::DEFAULT),
164 mWrapModeV(WrapMode::DEFAULT),
165 mLoadPolicy(Toolkit::ImageVisual::LoadPolicy::ATTACHED),
166 mReleasePolicy(Toolkit::ImageVisual::ReleasePolicy::DETACHED),
167 mAtlasRect(0.0f, 0.0f, 0.0f, 0.0f),
168 mAtlasRectSize(0, 0),
169 mLoadState(TextureManager::LoadState::NOT_STARTED),
170 mAttemptAtlasing(false),
171 mOrientationCorrection(true)
173 EnablePreMultipliedAlpha(mFactoryCache.GetPreMultiplyOnLoad());
176 ImageVisual::~ImageVisual()
178 if(Stage::IsInstalled())
182 // TextureManager could have been deleted before the actor that contains this
183 // ImageVisual is destroyed (e.g. due to stage shutdown). Ensure the stage
184 // is still valid before accessing texture manager.
185 if(mMaskingData->mAlphaMaskId != TextureManager::INVALID_TEXTURE_ID)
187 TextureManager& textureManager = mFactoryCache.GetTextureManager();
188 textureManager.Remove(mMaskingData->mAlphaMaskId, this);
192 if(mImageUrl.IsValid())
194 // Decrease reference count of External Resources :
195 // EncodedImageBuffer or ExternalTextures.
196 // Ensure the stage is still valid before accessing texture manager.
197 if(mImageUrl.GetProtocolType() == VisualUrl::TEXTURE)
199 TextureManager& textureManager = mFactoryCache.GetTextureManager();
200 textureManager.RemoveExternalTexture(mImageUrl.GetUrl());
202 else if(mImageUrl.IsBufferResource())
204 TextureManager& textureManager = mFactoryCache.GetTextureManager();
205 textureManager.RemoveExternalEncodedImageBuffer(mImageUrl.GetUrl());
209 // ImageVisual destroyed so remove texture unless ReleasePolicy is set to never release
210 if((mTextureId != TextureManager::INVALID_TEXTURE_ID) && (mReleasePolicy != Toolkit::ImageVisual::ReleasePolicy::NEVER))
217 void ImageVisual::DoSetProperties(const Property::Map& propertyMap)
219 // Url is already received in constructor
220 for(Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter)
222 KeyValuePair keyValue = propertyMap.GetKeyValue(iter);
223 if(keyValue.first.type == Property::Key::INDEX)
225 DoSetProperty(keyValue.first.indexKey, keyValue.second);
229 if(keyValue.first == IMAGE_FITTING_MODE)
231 DoSetProperty(Toolkit::ImageVisual::Property::FITTING_MODE, keyValue.second);
233 else if(keyValue.first == IMAGE_SAMPLING_MODE)
235 DoSetProperty(Toolkit::ImageVisual::Property::SAMPLING_MODE, keyValue.second);
237 else if(keyValue.first == IMAGE_DESIRED_WIDTH)
239 DoSetProperty(Toolkit::ImageVisual::Property::DESIRED_WIDTH, keyValue.second);
241 else if(keyValue.first == IMAGE_DESIRED_HEIGHT)
243 DoSetProperty(Toolkit::ImageVisual::Property::DESIRED_HEIGHT, keyValue.second);
245 else if(keyValue.first == PIXEL_AREA_UNIFORM_NAME)
247 DoSetProperty(Toolkit::ImageVisual::Property::PIXEL_AREA, keyValue.second);
249 else if(keyValue.first == IMAGE_WRAP_MODE_U)
251 DoSetProperty(Toolkit::ImageVisual::Property::WRAP_MODE_U, keyValue.second);
253 else if(keyValue.first == IMAGE_WRAP_MODE_V)
255 DoSetProperty(Toolkit::ImageVisual::Property::WRAP_MODE_V, keyValue.second);
257 else if(keyValue.first == SYNCHRONOUS_LOADING)
259 DoSetProperty(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, keyValue.second);
261 else if(keyValue.first == IMAGE_ATLASING)
263 DoSetProperty(Toolkit::ImageVisual::Property::ATLASING, keyValue.second);
265 else if(keyValue.first == ALPHA_MASK_URL)
267 DoSetProperty(Toolkit::ImageVisual::Property::ALPHA_MASK_URL, keyValue.second);
269 else if(keyValue.first == MASK_CONTENT_SCALE_NAME)
271 DoSetProperty(Toolkit::ImageVisual::Property::MASK_CONTENT_SCALE, keyValue.second);
273 else if(keyValue.first == CROP_TO_MASK_NAME)
275 DoSetProperty(Toolkit::ImageVisual::Property::CROP_TO_MASK, keyValue.second);
277 else if(keyValue.first == LOAD_POLICY_NAME)
279 DoSetProperty(Toolkit::ImageVisual::Property::LOAD_POLICY, keyValue.second);
281 else if(keyValue.first == RELEASE_POLICY_NAME)
283 DoSetProperty(Toolkit::ImageVisual::Property::RELEASE_POLICY, keyValue.second);
285 else if(keyValue.first == ORIENTATION_CORRECTION_NAME)
287 DoSetProperty(Toolkit::ImageVisual::Property::ORIENTATION_CORRECTION, keyValue.second);
291 // Load image immediately if LOAD_POLICY requires it
292 if(mLoadPolicy == Toolkit::ImageVisual::LoadPolicy::IMMEDIATE)
294 auto attemptAtlasing = AttemptAtlasing();
295 LoadTexture(attemptAtlasing, mAtlasRect, mTextures, mOrientationCorrection, TextureManager::ReloadPolicy::CACHED);
299 void ImageVisual::DoSetProperty(Property::Index index, const Property::Value& value)
303 case Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING:
310 mImpl->mFlags |= Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
314 mImpl->mFlags &= ~Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
319 DALI_LOG_ERROR("ImageVisual: synchronousLoading property has incorrect type\n");
324 case Toolkit::ImageVisual::Property::DESIRED_WIDTH:
326 float desiredWidth = 0.0f;
327 if(value.Get(desiredWidth))
329 mDesiredSize.SetWidth(desiredWidth);
333 DALI_LOG_ERROR("ImageVisual: desiredWidth property has incorrect type\n");
338 case Toolkit::ImageVisual::Property::DESIRED_HEIGHT:
340 float desiredHeight = 0.0f;
341 if(value.Get(desiredHeight))
343 mDesiredSize.SetHeight(desiredHeight);
347 DALI_LOG_ERROR("ImageVisual: desiredHeight property has incorrect type\n");
352 case Toolkit::ImageVisual::Property::FITTING_MODE:
355 Scripting::GetEnumerationProperty(value, FITTING_MODE_TABLE, FITTING_MODE_TABLE_COUNT, fittingMode);
356 mFittingMode = Dali::FittingMode::Type(fittingMode);
360 case Toolkit::ImageVisual::Property::SAMPLING_MODE:
362 int samplingMode = 0;
363 Scripting::GetEnumerationProperty(value, SAMPLING_MODE_TABLE, SAMPLING_MODE_TABLE_COUNT, samplingMode);
364 mSamplingMode = Dali::SamplingMode::Type(samplingMode);
368 case Toolkit::ImageVisual::Property::PIXEL_AREA:
370 value.Get(mPixelArea);
374 case Toolkit::ImageVisual::Property::WRAP_MODE_U:
377 Scripting::GetEnumerationProperty(value, WRAP_MODE_TABLE, WRAP_MODE_TABLE_COUNT, wrapMode);
378 mWrapModeU = Dali::WrapMode::Type(wrapMode);
382 case Toolkit::ImageVisual::Property::WRAP_MODE_V:
385 Scripting::GetEnumerationProperty(value, WRAP_MODE_TABLE, WRAP_MODE_TABLE_COUNT, wrapMode);
386 mWrapModeV = Dali::WrapMode::Type(wrapMode);
390 case Toolkit::ImageVisual::Property::ATLASING:
392 value.Get(mAttemptAtlasing);
396 case Toolkit::ImageVisual::Property::ALPHA_MASK_URL:
398 std::string alphaUrl = "";
399 if(value.Get(alphaUrl))
402 mMaskingData->mAlphaMaskUrl = alphaUrl;
407 case Toolkit::ImageVisual::Property::MASK_CONTENT_SCALE:
413 mMaskingData->mContentScaleFactor = scale;
418 case Toolkit::ImageVisual::Property::CROP_TO_MASK:
424 mMaskingData->mCropToMask = crop;
429 case Toolkit::ImageVisual::Property::RELEASE_POLICY:
431 int releasePolicy = 0;
432 Scripting::GetEnumerationProperty(value, RELEASE_POLICY_TABLE, RELEASE_POLICY_TABLE_COUNT, releasePolicy);
433 mReleasePolicy = Toolkit::ImageVisual::ReleasePolicy::Type(releasePolicy);
437 case Toolkit::ImageVisual::Property::LOAD_POLICY:
440 Scripting::GetEnumerationProperty(value, LOAD_POLICY_TABLE, LOAD_POLICY_TABLE_COUNT, loadPolicy);
441 mLoadPolicy = Toolkit::ImageVisual::LoadPolicy::Type(loadPolicy);
444 case Toolkit::ImageVisual::Property::ORIENTATION_CORRECTION:
446 bool orientationCorrection(mOrientationCorrection);
447 if(value.Get(orientationCorrection))
449 mOrientationCorrection = orientationCorrection;
456 void ImageVisual::AllocateMaskData()
460 mMaskingData.reset(new TextureManager::MaskingData());
464 void ImageVisual::GetNaturalSize(Vector2& naturalSize)
466 if(mDesiredSize.GetWidth() > 0 && mDesiredSize.GetHeight() > 0)
468 naturalSize.x = mDesiredSize.GetWidth();
469 naturalSize.y = mDesiredSize.GetHeight();
472 else if(mImpl->mRenderer) // Check if we have a loaded image
474 if(mImpl->mFlags & Impl::IS_ATLASING_APPLIED)
476 naturalSize.x = mAtlasRectSize.GetWidth();
477 naturalSize.y = mAtlasRectSize.GetHeight();
481 auto textureSet = mImpl->mRenderer.GetTextures();
482 if(textureSet && textureSet.GetTextureCount())
484 auto texture = textureSet.GetTexture(0);
487 naturalSize.x = texture.GetWidth();
488 naturalSize.y = texture.GetHeight();
494 if(mMaskingData != NULL && mMaskingData->mAlphaMaskUrl.IsValid() &&
495 mMaskingData->mCropToMask)
497 ImageDimensions dimensions = Dali::GetClosestImageSize(mMaskingData->mAlphaMaskUrl.GetUrl());
498 if(dimensions != ImageDimensions(0, 0))
500 naturalSize.x = dimensions.GetWidth();
501 naturalSize.y = dimensions.GetHeight();
505 else if(mImageUrl.IsValid())
507 if(mImageUrl.GetProtocolType() == VisualUrl::LOCAL)
509 ImageDimensions dimensions = Dali::GetClosestImageSize(mImageUrl.GetUrl());
511 if(dimensions != ImageDimensions(0, 0))
513 naturalSize.x = dimensions.GetWidth();
514 naturalSize.y = dimensions.GetHeight();
518 Actor actor = mPlacementActor.GetHandle();
519 Vector2 imageSize = Vector2::ZERO;
522 imageSize = actor.GetProperty(Actor::Property::SIZE).Get<Vector2>();
524 mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize);
525 Texture brokenImage = mImpl->mRenderer.GetTextures().GetTexture(0);
526 naturalSize.x = brokenImage.GetWidth();
527 naturalSize.y = brokenImage.GetWidth();
532 naturalSize = Vector2::ZERO;
535 void ImageVisual::OnInitialize()
540 if(mImpl->mCustomShader)
542 geometry = CreateGeometry(mFactoryCache, mImpl->mCustomShader->mGridSize);
544 else // Get any geometry associated with the texture
546 TextureManager& textureManager = mFactoryCache.GetTextureManager();
548 uint32_t firstElementCount{0u};
549 uint32_t secondElementCount{0u};
550 geometry = textureManager.GetRenderGeometry(mTextureId, firstElementCount, secondElementCount);
552 if(!firstElementCount && !secondElementCount) // Otherwise use quad
554 geometry = CreateGeometry(mFactoryCache, ImageDimensions(1, 1));
558 // Increase reference count of External Resources :
559 // EncodedImageBuffer or ExternalTextures.
560 // Reference count will be decreased at destructor of the visual.
561 if(mImageUrl.IsValid() && (mImageUrl.IsBufferResource() || mImageUrl.GetProtocolType() == VisualUrl::TEXTURE))
563 TextureManager& textureManager = mFactoryCache.GetTextureManager();
564 textureManager.UseExternalResource(mImageUrl.GetUrl());
567 Shader shader = GenerateShader();
569 // Create the renderer
570 mImpl->mRenderer = VisualRenderer::New(geometry, shader);
571 mImpl->mRenderer.ReserveCustomProperties(CUSTOM_PROPERTY_COUNT);
573 //Register transform properties
574 mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
576 EnablePreMultipliedAlpha(IsPreMultipliedAlphaEnabled());
579 void ImageVisual::LoadTexture(bool& atlasing, Vector4& atlasRect, TextureSet& textures, bool orientationCorrection, TextureManager::ReloadPolicy forceReload)
581 TextureManager& textureManager = mFactoryCache.GetTextureManager();
583 ImageAtlasManagerPtr atlasManager = nullptr;
584 AtlasUploadObserver* atlasUploadObserver = nullptr;
585 auto textureObserver = this;
589 atlasManager = mFactoryCache.GetAtlasManager();
590 atlasUploadObserver = this;
593 auto preMultiplyOnLoad = IsPreMultipliedAlphaEnabled() && !mImpl->mCustomShader
594 ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD
595 : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
597 bool synchronousLoading = IsSynchronousLoadingRequired();
600 textures = textureManager.LoadTexture(mImageUrl, mDesiredSize, mFittingMode, mSamplingMode, mMaskingData, synchronousLoading, mTextureId, atlasRect, mAtlasRectSize, atlasing, loadingStatus, mWrapModeU, mWrapModeV, textureObserver, atlasUploadObserver, atlasManager, mOrientationCorrection, forceReload, preMultiplyOnLoad);
606 mLoadState = TextureManager::LoadState::LOADING;
610 mLoadState = TextureManager::LoadState::LOAD_FINISHED;
613 EnablePreMultipliedAlpha(preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD);
615 else if(synchronousLoading)
617 // Synchronous loading is failed
618 mLoadState = TextureManager::LoadState::LOAD_FAILED;
621 if(atlasing) // Flag needs to be set before creating renderer
623 mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
627 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
631 bool ImageVisual::AttemptAtlasing()
633 return (!mImpl->mCustomShader && (mImageUrl.IsLocalResource() || mImageUrl.IsBufferResource()) && mAttemptAtlasing);
636 void ImageVisual::InitializeRenderer()
638 auto attemptAtlasing = AttemptAtlasing();
640 // Load Texture if mTextures is empty.
641 // mTextures is already set, the mTexture can be used to create Renderer.
642 // There are two cases mTextures is empty.
643 // 1. mTextureId == TextureManager::INVALID_TEXTURE_ID
644 // - Visual is on stage with LoadPolicy::ATTACHED
645 // 2. mTextureId != TextureManager::INVALID_TEXTURE_ID
646 // - If ReleasePolicy is DESTROYED, InitializeRenderer called every on stage called.
647 // - Then every resources those contained in Visual are Reset but mTextureId is remained when the Off stage time,
648 // - So, mTextures needed to be get from texture manager to created resources like mImpl->mRenderer.
651 if(mTextureId == TextureManager::INVALID_TEXTURE_ID)
653 LoadTexture(attemptAtlasing, mAtlasRect, mTextures, mOrientationCorrection, TextureManager::ReloadPolicy::CACHED);
657 mTextures = mFactoryCache.GetTextureManager().GetTextureSet(mTextureId);
663 mImpl->mRenderer.SetTextures(mTextures);
664 if(DevelTexture::IsNative(mTextures.GetTexture(0)))
668 mTextures.Reset(); // Visual should not keep a handle to the texture after this point.
671 if(attemptAtlasing) // the texture is packed inside atlas
673 mImpl->mRenderer.RegisterProperty(ATLAS_RECT_UNIFORM_NAME, mAtlasRect);
675 bool defaultWrapMode = mWrapModeU <= WrapMode::CLAMP_TO_EDGE && mWrapModeV <= WrapMode::CLAMP_TO_EDGE;
677 if(!defaultWrapMode) // custom wrap mode
679 Vector2 wrapMode(mWrapModeU - WrapMode::CLAMP_TO_EDGE, mWrapModeV - WrapMode::CLAMP_TO_EDGE);
680 wrapMode.Clamp(Vector2::ZERO, Vector2(2.f, 2.f));
681 mImpl->mRenderer.RegisterProperty(WRAP_MODE_UNIFORM_NAME, wrapMode);
686 void ImageVisual::DoSetOnScene(Actor& actor)
688 if(mImageUrl.IsValid())
690 InitializeRenderer();
693 if(!mImpl->mRenderer)
698 mPlacementActor = actor;
699 // Search the Actor tree to find if Layer UI behaviour set.
700 Layer layer = actor.GetLayer();
701 if(layer && layer.GetProperty<Layer::Behavior>(Layer::Property::BEHAVIOR) == Layer::LAYER_3D)
703 // Layer 3D set, do not align pixels
704 mImpl->mRenderer.RegisterProperty(PIXEL_ALIGNED_UNIFORM_NAME, PIXEL_ALIGN_OFF);
707 if(mPixelArea != FULL_TEXTURE_RECT)
709 mImpl->mRenderer.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, mPixelArea);
712 if(mLoadState == TextureManager::LoadState::LOAD_FINISHED)
714 actor.AddRenderer(mImpl->mRenderer);
715 mPlacementActor.Reset();
717 // Image loaded and ready to display
718 ResourceReady(Toolkit::Visual::ResourceStatus::READY);
720 else if(mLoadState == TextureManager::LoadState::LOAD_FAILED)
722 Vector2 imageSize = Vector2::ZERO;
725 imageSize = actor.GetProperty(Actor::Property::SIZE).Get<Vector2>();
727 mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize);
728 actor.AddRenderer(mImpl->mRenderer);
729 mPlacementActor.Reset();
731 ResourceReady(Toolkit::Visual::ResourceStatus::FAILED);
735 void ImageVisual::DoSetOffScene(Actor& actor)
737 // Visual::Base::SetOffScene only calls DoSetOffScene if mRenderer exists (is on onstage)
739 // Image release is dependent on the ReleasePolicy, renderer is removed.
740 actor.RemoveRenderer(mImpl->mRenderer);
741 if(mReleasePolicy == Toolkit::ImageVisual::ReleasePolicy::DETACHED)
743 RemoveTexture(); // If INVALID_TEXTURE_ID then removal will be attempted on atlas
744 mImpl->mResourceStatus = Toolkit::Visual::ResourceStatus::PREPARING;
746 TextureSet textureSet = TextureSet::New();
747 mImpl->mRenderer.SetTextures(textureSet);
749 mLoadState = TextureManager::LoadState::NOT_STARTED;
752 mPlacementActor.Reset();
755 void ImageVisual::DoCreatePropertyMap(Property::Map& map) const
758 map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE);
760 bool sync = IsSynchronousLoadingRequired();
761 map.Insert(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, sync);
762 if(mImageUrl.IsValid())
764 map.Insert(Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl());
765 map.Insert(Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth());
766 map.Insert(Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight());
769 map.Insert(Toolkit::ImageVisual::Property::FITTING_MODE, mFittingMode);
770 map.Insert(Toolkit::ImageVisual::Property::SAMPLING_MODE, mSamplingMode);
772 map.Insert(Toolkit::ImageVisual::Property::PIXEL_AREA, mPixelArea);
773 map.Insert(Toolkit::ImageVisual::Property::WRAP_MODE_U, mWrapModeU);
774 map.Insert(Toolkit::ImageVisual::Property::WRAP_MODE_V, mWrapModeV);
776 map.Insert(Toolkit::ImageVisual::Property::ATLASING, mAttemptAtlasing);
778 if(mMaskingData != NULL)
780 map.Insert(Toolkit::ImageVisual::Property::ALPHA_MASK_URL, mMaskingData->mAlphaMaskUrl.GetUrl());
781 map.Insert(Toolkit::ImageVisual::Property::MASK_CONTENT_SCALE, mMaskingData->mContentScaleFactor);
782 map.Insert(Toolkit::ImageVisual::Property::CROP_TO_MASK, mMaskingData->mCropToMask);
785 map.Insert(Toolkit::ImageVisual::Property::LOAD_POLICY, mLoadPolicy);
786 map.Insert(Toolkit::ImageVisual::Property::RELEASE_POLICY, mReleasePolicy);
787 map.Insert(Toolkit::ImageVisual::Property::ORIENTATION_CORRECTION, mOrientationCorrection);
790 void ImageVisual::DoCreateInstancePropertyMap(Property::Map& map) const
793 map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE);
794 if(mImageUrl.IsValid())
796 map.Insert(Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth());
797 map.Insert(Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight());
801 void ImageVisual::OnDoAction(const Dali::Property::Index actionId, const Dali::Property::Value& attributes)
803 // Check if action is valid for this visual type and perform action if possible
807 case DevelImageVisual::Action::RELOAD:
809 auto attemptAtlasing = AttemptAtlasing();
810 LoadTexture(attemptAtlasing, mAtlasRect, mTextures, mOrientationCorrection, TextureManager::ReloadPolicy::FORCED);
816 void ImageVisual::OnSetTransform()
820 mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
824 bool ImageVisual::IsResourceReady() const
826 return (mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::READY ||
827 mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::FAILED);
830 void ImageVisual::UpdateShader()
834 Shader shader = GenerateShader();
835 mImpl->mRenderer.SetShader(shader);
839 // From existing atlas manager
840 void ImageVisual::UploadCompleted()
842 // Texture has been uploaded. If weak handle is holding a placement actor,
843 // it is the time to add the renderer to actor.
844 Actor actor = mPlacementActor.GetHandle();
847 mImpl->mRenderer.RegisterProperty(ATLAS_RECT_UNIFORM_NAME, mAtlasRect);
848 actor.AddRenderer(mImpl->mRenderer);
849 // reset the weak handle so that the renderer only get added to actor once
850 mPlacementActor.Reset();
854 ResourceReady(Toolkit::Visual::ResourceStatus::READY);
855 mLoadState = TextureManager::LoadState::LOAD_FINISHED;
858 // From Texture Manager
859 void ImageVisual::LoadComplete(bool loadingSuccess, TextureInformation textureInformation)
861 Toolkit::Visual::ResourceStatus resourceStatus;
864 if(textureInformation.useAtlasing)
866 mImpl->mRenderer.RegisterProperty(ATLAS_RECT_UNIFORM_NAME, mAtlasRect);
869 EnablePreMultipliedAlpha(textureInformation.preMultiplied);
871 Actor actor = mPlacementActor.GetHandle();
874 Vector2 imageSize = Vector2::ZERO;
877 imageSize = actor.GetProperty(Actor::Property::SIZE).Get<Vector2>();
879 mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize);
880 textureInformation.textureSet = mImpl->mRenderer.GetTextures();
884 Sampler sampler = Sampler::New();
885 sampler.SetWrapMode(mWrapModeU, mWrapModeV);
886 textureInformation.textureSet.SetSampler(0u, sampler);
887 mImpl->mRenderer.SetTextures(textureInformation.textureSet);
892 actor.AddRenderer(mImpl->mRenderer);
893 // reset the weak handle so that the renderer only get added to actor once
894 mPlacementActor.Reset();
898 // Storing TextureSet needed when renderer staged.
899 if(!mImpl->mRenderer)
901 mTextures = textureInformation.textureSet;
904 // Image loaded, set status regardless of staged status.
907 resourceStatus = Toolkit::Visual::ResourceStatus::READY;
908 mLoadState = TextureManager::LoadState::LOAD_FINISHED;
912 resourceStatus = Toolkit::Visual::ResourceStatus::FAILED;
913 mLoadState = TextureManager::LoadState::LOAD_FAILED;
916 // use geometry if needed
919 uint32_t firstElementCount{0u};
920 uint32_t secondElementCount{0u};
921 auto geometry = mFactoryCache.GetTextureManager().GetRenderGeometry(mTextureId, firstElementCount, secondElementCount);
922 if(mImpl->mRenderer && geometry)
924 mImpl->mRenderer.SetGeometry(geometry);
925 Dali::DevelRenderer::DrawCommand drawCommand{};
926 drawCommand.drawType = DevelRenderer::DrawType::INDEXED;
928 if(firstElementCount)
930 drawCommand.firstIndex = 0;
931 drawCommand.elementCount = firstElementCount;
932 drawCommand.queue = DevelRenderer::RENDER_QUEUE_OPAQUE;
933 DevelRenderer::AddDrawCommand(mImpl->mRenderer, drawCommand);
936 if(secondElementCount)
938 drawCommand.firstIndex = firstElementCount;
939 drawCommand.elementCount = secondElementCount;
940 drawCommand.queue = DevelRenderer::RENDER_QUEUE_TRANSPARENT;
941 DevelRenderer::AddDrawCommand(mImpl->mRenderer, drawCommand);
946 // Signal to observers ( control ) that resources are ready. Must be all resources.
947 ResourceReady(resourceStatus);
950 void ImageVisual::RemoveTexture()
952 if(mTextureId != TextureManager::INVALID_TEXTURE_ID)
954 mFactoryCache.GetTextureManager().Remove(mTextureId, this);
955 mTextureId = TextureManager::INVALID_TEXTURE_ID;
959 Vector4 atlasRect(0.f, 0.f, 1.f, 1.f);
960 Property::Index index = mImpl->mRenderer.GetPropertyIndex(ATLAS_RECT_UNIFORM_NAME);
961 if(index != Property::INVALID_INDEX)
963 Property::Value atlasRectValue = mImpl->mRenderer.GetProperty(index);
964 atlasRectValue.Get(atlasRect);
967 TextureSet textureSet = mImpl->mRenderer.GetTextures();
969 if(index != Property::INVALID_INDEX)
971 mFactoryCache.GetAtlasManager()->Remove(textureSet, atlasRect);
976 Shader ImageVisual::GenerateShader() const
980 bool usesWholeTexture = true;
981 const bool useStandardShader = !mImpl->mCustomShader;
982 const bool useNativeImage = (mTextures && DevelTexture::IsNative(mTextures.GetTexture(0)));
984 if(useStandardShader)
986 // Create and cache the standard shader
987 shader = mImageVisualShaderFactory.GetShader(
989 ImageVisualShaderFeature::FeatureBuilder()
990 .EnableTextureAtlas(mImpl->mFlags & Impl::IS_ATLASING_APPLIED && !useNativeImage)
991 .ApplyDefaultTextureWrapMode(mWrapModeU <= WrapMode::CLAMP_TO_EDGE && mWrapModeV <= WrapMode::CLAMP_TO_EDGE)
992 .EnableRoundedCorner(IsRoundedCornerRequired())
993 .EnableBorderline(IsBorderlineRequired())
994 .SetTextureForFragmentShaderCheck(useNativeImage ? mTextures.GetTexture(0) : Dali::Texture()));
998 std::string_view vertexShaderView;
999 std::string_view fragmentShaderView;
1001 if(mImpl->mCustomShader && !mImpl->mCustomShader->mVertexShader.empty())
1003 vertexShaderView = mImpl->mCustomShader->mVertexShader;
1004 usesWholeTexture = false; // Impossible to tell.
1008 vertexShaderView = mImageVisualShaderFactory.GetVertexShaderSource();
1011 if(mImpl->mCustomShader && !mImpl->mCustomShader->mFragmentShader.empty())
1013 fragmentShaderView = mImpl->mCustomShader->mFragmentShader;
1017 fragmentShaderView = mImageVisualShaderFactory.GetFragmentShaderSource();
1020 // If the texture is native, we may need to change prefix and sampler in
1021 // the fragment shader
1024 bool modifiedFragmentShader = false;
1025 Texture nativeTexture = mTextures.GetTexture(0);
1026 std::string fragmentShaderString = std::string(fragmentShaderView);
1028 modifiedFragmentShader = DevelTexture::ApplyNativeFragmentShader(nativeTexture, fragmentShaderString);
1029 if(modifiedFragmentShader)
1031 fragmentShaderView = fragmentShaderString;
1034 // Create shader here cause fragmentShaderString scope issue
1035 shader = Shader::New(vertexShaderView, fragmentShaderView, mImpl->mCustomShader->mHints);
1039 shader = Shader::New(vertexShaderView, fragmentShaderView, mImpl->mCustomShader->mHints);
1043 if(usesWholeTexture)
1045 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
1048 // Set pixel align off as default.
1049 // ToDo: Pixel align causes issues such as rattling image animation.
1050 // We should trun it off until issues are resolved
1051 shader.RegisterProperty(PIXEL_ALIGNED_UNIFORM_NAME, PIXEL_ALIGN_OFF);
1056 } // namespace Internal
1058 } // namespace Toolkit