/*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2023 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include <dali/devel-api/scripting/scripting.h>
#include <dali/integration-api/debug.h>
#include <dali/public-api/actors/layer.h>
+#include <dali/public-api/adaptor-framework/async-task-manager.h>
+#include <dali/public-api/rendering/decorated-visual-renderer.h>
#include <cstring> // for strlen()
// INTERNAL HEADERS
#include <dali-toolkit/devel-api/visuals/image-visual-actions-devel.h>
+#include <dali-toolkit/internal/texture-manager/texture-manager-impl.h>
#include <dali-toolkit/internal/visuals/image-atlas-manager.h>
#include <dali-toolkit/internal/visuals/image-visual-shader-factory.h>
-#include <dali-toolkit/internal/visuals/texture-manager-impl.h>
+#include <dali-toolkit/internal/visuals/image-visual-shader-feature-builder.h>
#include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
#include <dali-toolkit/internal/visuals/visual-base-impl.h>
#include <dali-toolkit/internal/visuals/visual-factory-cache.h>
{
namespace
{
+const int CUSTOM_PROPERTY_COUNT(7); // ltr, wrap, pixel area, atlas, pixalign, crop to mask, mask texture ratio
+
// fitting modes
DALI_ENUM_TO_STRING_TABLE_BEGIN(FITTING_MODE)
DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::FittingMode, SHRINK_TO_FIT)
const Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
-const float PIXEL_ALIGN_ON = 1.0f;
-const float PIXEL_ALIGN_OFF = 0.0f;
+constexpr uint32_t TEXTURE_COUNT_FOR_GPU_ALPHA_MASK = 2u;
+
+struct NameIndexMatch
+{
+ const char* const name;
+ Property::Index index;
+};
+
+const NameIndexMatch NAME_INDEX_MATCH_TABLE[] =
+ {
+ {IMAGE_FITTING_MODE, Toolkit::ImageVisual::Property::FITTING_MODE},
+ {IMAGE_SAMPLING_MODE, Toolkit::ImageVisual::Property::SAMPLING_MODE},
+ {IMAGE_DESIRED_WIDTH, Toolkit::ImageVisual::Property::DESIRED_WIDTH},
+ {IMAGE_DESIRED_HEIGHT, Toolkit::ImageVisual::Property::DESIRED_HEIGHT},
+ {PIXEL_AREA_UNIFORM_NAME, Toolkit::ImageVisual::Property::PIXEL_AREA},
+ {IMAGE_WRAP_MODE_U, Toolkit::ImageVisual::Property::WRAP_MODE_U},
+ {IMAGE_WRAP_MODE_V, Toolkit::ImageVisual::Property::WRAP_MODE_V},
+ {SYNCHRONOUS_LOADING, Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING},
+ {IMAGE_ATLASING, Toolkit::ImageVisual::Property::ATLASING},
+ {ALPHA_MASK_URL, Toolkit::ImageVisual::Property::ALPHA_MASK_URL},
+ {MASK_CONTENT_SCALE_NAME, Toolkit::ImageVisual::Property::MASK_CONTENT_SCALE},
+ {CROP_TO_MASK_NAME, Toolkit::ImageVisual::Property::CROP_TO_MASK},
+ {MASKING_TYPE_NAME, Toolkit::DevelImageVisual::Property::MASKING_TYPE},
+ {ENABLE_BROKEN_IMAGE, Toolkit::DevelImageVisual::Property::ENABLE_BROKEN_IMAGE},
+ {LOAD_POLICY_NAME, Toolkit::ImageVisual::Property::LOAD_POLICY},
+ {RELEASE_POLICY_NAME, Toolkit::ImageVisual::Property::RELEASE_POLICY},
+ {ORIENTATION_CORRECTION_NAME, Toolkit::ImageVisual::Property::ORIENTATION_CORRECTION},
+ {FAST_TRACK_UPLOADING_NAME, Toolkit::DevelImageVisual::Property::FAST_TRACK_UPLOADING},
+};
+const int NAME_INDEX_MATCH_TABLE_SIZE = sizeof(NAME_INDEX_MATCH_TABLE) / sizeof(NAME_INDEX_MATCH_TABLE[0]);
Geometry CreateGeometry(VisualFactoryCache& factoryCache, ImageDimensions gridSize)
{
Dali::SamplingMode::Type samplingMode)
: Visual::Base(factoryCache, Visual::FittingMode::FILL, Toolkit::Visual::IMAGE),
mPixelArea(FULL_TEXTURE_RECT),
+ mPixelAreaIndex(Property::INVALID_INDEX),
mPlacementActor(),
mImageUrl(imageUrl),
mMaskingData(),
mAtlasRectSize(0, 0),
mLoadState(TextureManager::LoadState::NOT_STARTED),
mAttemptAtlasing(false),
- mOrientationCorrection(true)
+ mOrientationCorrection(true),
+ mEnableBrokenImage(true)
{
EnablePreMultipliedAlpha(mFactoryCache.GetPreMultiplyOnLoad());
}
{
if(Stage::IsInstalled())
{
- if(mMaskingData)
+ if(mImageUrl.IsValid())
{
- // TextureManager could have been deleted before the actor that contains this
- // ImageVisual is destroyed (e.g. due to stage shutdown). Ensure the stage
- // is still valid before accessing texture manager.
- if(mMaskingData->mAlphaMaskId != TextureManager::INVALID_TEXTURE_ID)
+ // Decrease reference count of External Resources :
+ // EncodedImageBuffer or ExternalTextures.
+ // Ensure the stage is still valid before accessing texture manager.
+ if(mImageUrl.GetProtocolType() == VisualUrl::TEXTURE)
{
TextureManager& textureManager = mFactoryCache.GetTextureManager();
- textureManager.Remove(mMaskingData->mAlphaMaskId, this);
+ textureManager.RemoveExternalTexture(mImageUrl.GetUrl());
+ }
+ else if(mImageUrl.IsBufferResource())
+ {
+ TextureManager& textureManager = mFactoryCache.GetTextureManager();
+ textureManager.RemoveEncodedImageBuffer(mImageUrl.GetUrl());
}
}
{
RemoveTexture();
}
+
+ ResetFastTrackLoadingTask();
}
}
}
else
{
- if(keyValue.first == IMAGE_FITTING_MODE)
- {
- DoSetProperty(Toolkit::ImageVisual::Property::FITTING_MODE, keyValue.second);
- }
- else if(keyValue.first == IMAGE_SAMPLING_MODE)
- {
- DoSetProperty(Toolkit::ImageVisual::Property::SAMPLING_MODE, keyValue.second);
- }
- else if(keyValue.first == IMAGE_DESIRED_WIDTH)
- {
- DoSetProperty(Toolkit::ImageVisual::Property::DESIRED_WIDTH, keyValue.second);
- }
- else if(keyValue.first == IMAGE_DESIRED_HEIGHT)
- {
- DoSetProperty(Toolkit::ImageVisual::Property::DESIRED_HEIGHT, keyValue.second);
- }
- else if(keyValue.first == PIXEL_AREA_UNIFORM_NAME)
- {
- DoSetProperty(Toolkit::ImageVisual::Property::PIXEL_AREA, keyValue.second);
- }
- else if(keyValue.first == IMAGE_WRAP_MODE_U)
- {
- DoSetProperty(Toolkit::ImageVisual::Property::WRAP_MODE_U, keyValue.second);
- }
- else if(keyValue.first == IMAGE_WRAP_MODE_V)
+ for(int i = 0; i < NAME_INDEX_MATCH_TABLE_SIZE; ++i)
{
- DoSetProperty(Toolkit::ImageVisual::Property::WRAP_MODE_V, keyValue.second);
- }
- else if(keyValue.first == SYNCHRONOUS_LOADING)
- {
- DoSetProperty(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, keyValue.second);
- }
- else if(keyValue.first == IMAGE_ATLASING)
- {
- DoSetProperty(Toolkit::ImageVisual::Property::ATLASING, keyValue.second);
- }
- else if(keyValue.first == ALPHA_MASK_URL)
- {
- DoSetProperty(Toolkit::ImageVisual::Property::ALPHA_MASK_URL, keyValue.second);
- }
- else if(keyValue.first == MASK_CONTENT_SCALE_NAME)
- {
- DoSetProperty(Toolkit::ImageVisual::Property::MASK_CONTENT_SCALE, keyValue.second);
- }
- else if(keyValue.first == CROP_TO_MASK_NAME)
- {
- DoSetProperty(Toolkit::ImageVisual::Property::CROP_TO_MASK, keyValue.second);
- }
- else if(keyValue.first == LOAD_POLICY_NAME)
- {
- DoSetProperty(Toolkit::ImageVisual::Property::LOAD_POLICY, keyValue.second);
- }
- else if(keyValue.first == RELEASE_POLICY_NAME)
- {
- DoSetProperty(Toolkit::ImageVisual::Property::RELEASE_POLICY, keyValue.second);
- }
- else if(keyValue.first == ORIENTATION_CORRECTION_NAME)
- {
- DoSetProperty(Toolkit::ImageVisual::Property::ORIENTATION_CORRECTION, keyValue.second);
+ if(keyValue.first == NAME_INDEX_MATCH_TABLE[i].name)
+ {
+ DoSetProperty(NAME_INDEX_MATCH_TABLE[i].index, keyValue.second);
+ break;
+ }
}
}
}
{
if(sync)
{
- mImpl->mFlags |= Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
+ mImpl->mFlags |= Visual::Base::Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
}
else
{
- mImpl->mFlags &= ~Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
+ mImpl->mFlags &= ~Visual::Base::Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
}
}
else
case Toolkit::ImageVisual::Property::DESIRED_WIDTH:
{
- float desiredWidth = 0.0f;
+ int32_t desiredWidth = 0;
if(value.Get(desiredWidth))
{
mDesiredSize.SetWidth(desiredWidth);
case Toolkit::ImageVisual::Property::DESIRED_HEIGHT:
{
- float desiredHeight = 0.0f;
+ int32_t desiredHeight = 0;
if(value.Get(desiredHeight))
{
mDesiredSize.SetHeight(desiredHeight);
case Toolkit::ImageVisual::Property::PIXEL_AREA:
{
value.Get(mPixelArea);
+
+ if(DALI_UNLIKELY(mImpl->mRenderer))
+ {
+ // Unusual case. SetProperty called after OnInitialize().
+ // Assume that DoAction call UPDATE_PROPERTY.
+ mPixelAreaIndex = mImpl->mRenderer.RegisterProperty(mPixelAreaIndex, PIXEL_AREA_UNIFORM_NAME, mPixelArea);
+ }
break;
}
break;
}
+ case Toolkit::DevelImageVisual::Property::MASKING_TYPE:
+ {
+ int maskingType = 0;
+ if(value.Get(maskingType))
+ {
+ AllocateMaskData();
+ if(mImageUrl.IsValid() && mImageUrl.GetProtocolType() == VisualUrl::TEXTURE)
+ {
+ // For external textures, only gpu masking is available.
+ // Therefore, MASKING_TYPE is set to MASKING_ON_RENDERING forcelly.
+ mMaskingData->mPreappliedMasking = false;
+ }
+ else
+ {
+ mMaskingData->mPreappliedMasking = (Toolkit::DevelImageVisual::MaskingType::Type(maskingType) == Toolkit::DevelImageVisual::MaskingType::MASKING_ON_LOADING);
+ }
+ }
+ break;
+ }
+
+ case Toolkit::DevelImageVisual::Property::ENABLE_BROKEN_IMAGE:
+ {
+ value.Get(mEnableBrokenImage);
+ break;
+ }
+
case Toolkit::ImageVisual::Property::RELEASE_POLICY:
{
int releasePolicy = 0;
}
case Toolkit::ImageVisual::Property::ORIENTATION_CORRECTION:
{
- bool orientationCorrection(mOrientationCorrection);
- if(value.Get(orientationCorrection))
- {
- mOrientationCorrection = orientationCorrection;
- }
+ value.Get(mOrientationCorrection);
+ break;
+ }
+
+ case Toolkit::DevelImageVisual::Property::FAST_TRACK_UPLOADING:
+ {
+ value.Get(mUseFastTrackUploading);
break;
}
}
if(!mMaskingData)
{
mMaskingData.reset(new TextureManager::MaskingData());
+ if(mImageUrl.IsValid() && mImageUrl.GetProtocolType() == VisualUrl::TEXTURE)
+ {
+ mMaskingData->mPreappliedMasking = false;
+ }
}
}
}
else if(mImpl->mRenderer) // Check if we have a loaded image
{
- if(mImpl->mFlags & Impl::IS_ATLASING_APPLIED)
+ if(mImpl->mFlags & Visual::Base::Impl::IS_ATLASING_APPLIED)
{
naturalSize.x = mAtlasRectSize.GetWidth();
naturalSize.y = mAtlasRectSize.GetHeight();
}
auto textureSet = mImpl->mRenderer.GetTextures();
- if(textureSet)
+ if(textureSet && textureSet.GetTextureCount())
{
- auto texture = textureSet.GetTexture(0);
- if(texture)
+ if(mTextureSize != Vector2::ZERO)
{
- naturalSize.x = texture.GetWidth();
- naturalSize.y = texture.GetHeight();
+ naturalSize = mTextureSize;
return;
}
}
}
else
{
- Texture brokenImage = mFactoryCache.GetBrokenVisualImage();
+ Actor actor = mPlacementActor.GetHandle();
+ Vector2 imageSize = Vector2::ZERO;
+ if(actor)
+ {
+ imageSize = actor.GetProperty(Actor::Property::SIZE).Get<Vector2>();
+ }
+ else
+ {
+ imageSize = mPlacementActorSize;
+ }
- naturalSize.x = brokenImage.GetWidth();
- naturalSize.y = brokenImage.GetWidth();
+ mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize);
+ Texture brokenImage = mImpl->mRenderer.GetTextures().GetTexture(0);
+ naturalSize.x = brokenImage.GetWidth();
+ naturalSize.y = brokenImage.GetWidth();
}
return;
}
}
}
- Shader shader = GetShader();
+ // Increase reference count of External Resources :
+ // EncodedImageBuffer or ExternalTextures.
+ // Reference count will be decreased at destructor of the visual.
+ if(mImageUrl.IsValid() && (mImageUrl.IsBufferResource() || mImageUrl.GetProtocolType() == VisualUrl::TEXTURE))
+ {
+ TextureManager& textureManager = mFactoryCache.GetTextureManager();
+ textureManager.UseExternalResource(mImageUrl.GetUrl());
+ }
+
+ Shader shader = GenerateShader();
// Create the renderer
- mImpl->mRenderer = Renderer::New(geometry, shader);
+ mImpl->mRenderer = DecoratedVisualRenderer::New(geometry, shader);
+ mImpl->mRenderer.ReserveCustomProperties(CUSTOM_PROPERTY_COUNT);
//Register transform properties
- mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
+ mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
EnablePreMultipliedAlpha(IsPreMultipliedAlphaEnabled());
+
+ if(mMaskingData)
+ {
+ mImpl->mRenderer.RegisterProperty(CROP_TO_MASK_NAME, static_cast<float>(mMaskingData->mCropToMask));
+ }
}
void ImageVisual::LoadTexture(bool& atlasing, Vector4& atlasRect, TextureSet& textures, bool orientationCorrection, TextureManager::ReloadPolicy forceReload)
: TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
bool synchronousLoading = IsSynchronousLoadingRequired();
- bool loadingStatus;
+ bool loadingStatus = false;
+
+ // Remove previous loading task.
+ ResetFastTrackLoadingTask();
- textures = textureManager.LoadTexture(mImageUrl, mDesiredSize, mFittingMode, mSamplingMode, mMaskingData, synchronousLoading, mTextureId, atlasRect, mAtlasRectSize, atlasing, loadingStatus, mWrapModeU, mWrapModeV, textureObserver, atlasUploadObserver, atlasManager, mOrientationCorrection, forceReload, preMultiplyOnLoad);
+ // Rare case. If someone call LoadTexture during fast track loading task running, (Ex : Action::RELOAD)
+ // we should remove previously added renderer now.
+ if(mRendererAdded)
+ {
+ Actor actor = mPlacementActor.GetHandle();
+ if(actor)
+ {
+ actor.RemoveRenderer(mImpl->mRenderer);
+ mRendererAdded = false;
+ }
+ }
+
+ /**
+ * @brief Check whether FastTrackUploading is avaliable or not.
+ * @return True if we can use fast track uploading feature. False otherwise.
+ */
+ auto IsFastTrackUploadingAvailable = [&]() {
+ if(mUseFastTrackUploading &&
+ mLoadPolicy == Toolkit::ImageVisual::LoadPolicy::ATTACHED &&
+ mReleasePolicy == Toolkit::ImageVisual::ReleasePolicy::DETACHED &&
+ forceReload == TextureManager::ReloadPolicy::CACHED &&
+ (mImageUrl.GetProtocolType() == VisualUrl::LOCAL || mImageUrl.GetProtocolType() == VisualUrl::REMOTE) &&
+ !synchronousLoading &&
+ !atlasing &&
+ !mImpl->mCustomShader &&
+ !(mMaskingData && mMaskingData->mAlphaMaskUrl.IsValid()))
+ {
+ return true;
+ }
+ else if(mUseFastTrackUploading)
+ {
+ DALI_LOG_DEBUG_INFO("FastTrack : Fail to load fast track. mUrl : [%s]%s%s%s%s%s%s%s%s\n",
+ mImageUrl.GetUrl().c_str(),
+ (mLoadPolicy != Toolkit::ImageVisual::LoadPolicy::ATTACHED) ? "/ mLoadPolicy != ATTACHED" : "",
+ (mReleasePolicy != Toolkit::ImageVisual::ReleasePolicy::DETACHED) ? "/ mReleasePolicy != DETACHED" : "",
+ (forceReload != TextureManager::ReloadPolicy::CACHED) ? "/ forceReload != CACHED" : "",
+ (!(mImageUrl.GetProtocolType() == VisualUrl::LOCAL || mImageUrl.GetProtocolType() == VisualUrl::REMOTE)) ? "/ url is not image" : "",
+ (synchronousLoading) ? "/ synchronousLoading" : "",
+ (atlasing) ? "/ atlasing" : "",
+ (mImpl->mCustomShader) ? "/ use customs shader" : "",
+ (mMaskingData && mMaskingData->mAlphaMaskUrl.IsValid()) ? "/ use masking url" : "");
+ }
+ return false;
+ };
+
+ if(IsFastTrackUploadingAvailable())
+ {
+ // Enable PremultipliedAlpha first.
+ EnablePreMultipliedAlpha(preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD);
+
+ // Set new TextureSet with fast track loading task
+ mFastTrackLoadingTask = new FastTrackLoadingTask(mImageUrl, mDesiredSize, mFittingMode, mSamplingMode, mOrientationCorrection, preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD ? DevelAsyncImageLoader::PreMultiplyOnLoad::ON : DevelAsyncImageLoader::PreMultiplyOnLoad::OFF, mFactoryCache.GetLoadYuvPlanes(), MakeCallback(this, &ImageVisual::FastLoadComplete));
+
+ TextureSet textureSet = TextureSet::New();
+ if(!mFastTrackLoadingTask->mLoadPlanesAvaliable)
+ {
+ DALI_ASSERT_ALWAYS(mFastTrackLoadingTask->mTextures.size() >= 1u);
+ textureSet.SetTexture(0u, mFastTrackLoadingTask->mTextures[0]);
+ }
+ else
+ {
+ DALI_ASSERT_ALWAYS(mFastTrackLoadingTask->mTextures.size() >= 3u);
+ textureSet.SetTexture(2u, mFastTrackLoadingTask->mTextures[2]);
+ textureSet.SetTexture(1u, mFastTrackLoadingTask->mTextures[1]);
+ textureSet.SetTexture(0u, mFastTrackLoadingTask->mTextures[0]);
+
+ // We cannot determine what kind of shader will be used.
+ // Just use unified shader, and then change shader after load completed.
+ mNeedUnifiedYuvAndRgb = true;
+ UpdateShader();
+ }
+ mImpl->mRenderer.SetTextures(textureSet);
+
+ Dali::AsyncTaskManager::Get().AddTask(mFastTrackLoadingTask);
+
+ mLoadState = TextureManager::LoadState::LOADING;
+ }
+ else
+ {
+ textures = textureManager.LoadTexture(mImageUrl, mDesiredSize, mFittingMode, mSamplingMode, mMaskingData, synchronousLoading, mTextureId, atlasRect, mAtlasRectSize, atlasing, loadingStatus, textureObserver, atlasUploadObserver, atlasManager, mOrientationCorrection, forceReload, preMultiplyOnLoad);
+ }
if(textures)
{
}
EnablePreMultipliedAlpha(preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD);
+ if(!atlasing)
+ {
+ Sampler sampler = Sampler::New();
+ sampler.SetWrapMode(mWrapModeU, mWrapModeV);
+ textures.SetSampler(0u, sampler);
+ }
}
else if(synchronousLoading)
{
if(atlasing) // Flag needs to be set before creating renderer
{
- mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
+ mImpl->mFlags |= Visual::Base::Impl::IS_ATLASING_APPLIED;
}
else
{
- mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
+ mImpl->mFlags &= ~Visual::Base::Impl::IS_ATLASING_APPLIED;
}
}
bool ImageVisual::AttemptAtlasing()
{
- return (!mImpl->mCustomShader && mImageUrl.GetProtocolType() == VisualUrl::LOCAL && mAttemptAtlasing);
+ return (!mImpl->mCustomShader && (mImageUrl.IsLocalResource() || mImageUrl.IsBufferResource()) && mAttemptAtlasing);
}
void ImageVisual::InitializeRenderer()
else
{
mTextures = mFactoryCache.GetTextureManager().GetTextureSet(mTextureId);
+ if(!(mImpl->mFlags & Visual::Base::Impl::IS_ATLASING_APPLIED) && mTextures)
+ {
+ Sampler sampler = Sampler::New();
+ sampler.SetWrapMode(mWrapModeU, mWrapModeV);
+ mTextures.SetSampler(0u, sampler);
+ }
}
}
if(mTextures)
{
mImpl->mRenderer.SetTextures(mTextures);
+ ComputeTextureSize();
+ CheckMaskTexture();
+
+ bool needToUpdateShader = DevelTexture::IsNative(mTextures.GetTexture(0));
+
+ if(mTextures.GetTextureCount() == 3)
+ {
+ if(mTextures.GetTexture(0).GetPixelFormat() == Pixel::L8 && mTextures.GetTexture(1).GetPixelFormat() == Pixel::CHROMINANCE_U && mTextures.GetTexture(2).GetPixelFormat() == Pixel::CHROMINANCE_V)
+ {
+ mNeedYuvToRgb = true;
+ needToUpdateShader = true;
+ }
+ }
+
+ if(needToUpdateShader)
+ {
+ UpdateShader();
+ }
mTextures.Reset(); // Visual should not keep a handle to the texture after this point.
}
}
mPlacementActor = actor;
- // Search the Actor tree to find if Layer UI behaviour set.
- Layer layer = actor.GetLayer();
- if(layer && layer.GetProperty<Layer::Behavior>(Layer::Property::BEHAVIOR) == Layer::LAYER_3D)
- {
- // Layer 3D set, do not align pixels
- mImpl->mRenderer.RegisterProperty(PIXEL_ALIGNED_UNIFORM_NAME, PIXEL_ALIGN_OFF);
- }
if(mPixelArea != FULL_TEXTURE_RECT)
{
- mImpl->mRenderer.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, mPixelArea);
+ mPixelAreaIndex = mImpl->mRenderer.RegisterProperty(mPixelAreaIndex, PIXEL_AREA_UNIFORM_NAME, mPixelArea);
}
if(mLoadState == TextureManager::LoadState::LOAD_FINISHED)
{
actor.AddRenderer(mImpl->mRenderer);
+ mRendererAdded = true;
mPlacementActor.Reset();
// Image loaded and ready to display
}
else if(mLoadState == TextureManager::LoadState::LOAD_FAILED)
{
- Texture brokenImage = mFactoryCache.GetBrokenVisualImage();
-
- mTextures = TextureSet::New();
- mTextures.SetTexture(0u, brokenImage);
- mImpl->mRenderer.SetTextures(mTextures);
-
- actor.AddRenderer(mImpl->mRenderer);
- mPlacementActor.Reset();
-
+ ShowBrokenImage();
ResourceReady(Toolkit::Visual::ResourceStatus::FAILED);
}
+ else
+ {
+ if(mFastTrackLoadingTask)
+ {
+ actor.AddRenderer(mImpl->mRenderer);
+ mRendererAdded = true;
+ }
+ }
}
void ImageVisual::DoSetOffScene(Actor& actor)
// Image release is dependent on the ReleasePolicy, renderer is removed.
actor.RemoveRenderer(mImpl->mRenderer);
+ mRendererAdded = false;
+
if(mReleasePolicy == Toolkit::ImageVisual::ReleasePolicy::DETACHED)
{
- RemoveTexture(); // If INVALID_TEXTURE_ID then removal will be attempted on atlas
- mImpl->mResourceStatus = Toolkit::Visual::ResourceStatus::PREPARING;
-
- TextureSet textureSet = TextureSet::New();
- mImpl->mRenderer.SetTextures(textureSet);
-
- mLoadState = TextureManager::LoadState::NOT_STARTED;
+ ResetRenderer();
}
mPlacementActor.Reset();
map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE);
bool sync = IsSynchronousLoadingRequired();
- map.Insert(SYNCHRONOUS_LOADING, sync);
+ map.Insert(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, sync);
if(mImageUrl.IsValid())
{
map.Insert(Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl());
map.Insert(Toolkit::ImageVisual::Property::FITTING_MODE, mFittingMode);
map.Insert(Toolkit::ImageVisual::Property::SAMPLING_MODE, mSamplingMode);
- map.Insert(Toolkit::ImageVisual::Property::PIXEL_AREA, mPixelArea);
+ if(mImpl->mRenderer && mPixelAreaIndex != Property::INVALID_INDEX)
+ {
+ // Update values from Renderer
+ Vector4 pixelArea = mImpl->mRenderer.GetProperty<Vector4>(mPixelAreaIndex);
+ map.Insert(Toolkit::ImageVisual::Property::PIXEL_AREA, pixelArea);
+ }
+ else
+ {
+ map.Insert(Toolkit::ImageVisual::Property::PIXEL_AREA, mPixelArea);
+ }
+
map.Insert(Toolkit::ImageVisual::Property::WRAP_MODE_U, mWrapModeU);
map.Insert(Toolkit::ImageVisual::Property::WRAP_MODE_V, mWrapModeV);
map.Insert(Toolkit::ImageVisual::Property::ALPHA_MASK_URL, mMaskingData->mAlphaMaskUrl.GetUrl());
map.Insert(Toolkit::ImageVisual::Property::MASK_CONTENT_SCALE, mMaskingData->mContentScaleFactor);
map.Insert(Toolkit::ImageVisual::Property::CROP_TO_MASK, mMaskingData->mCropToMask);
+ map.Insert(Toolkit::DevelImageVisual::Property::MASKING_TYPE, mMaskingData->mPreappliedMasking ? DevelImageVisual::MaskingType::MASKING_ON_LOADING : DevelImageVisual::MaskingType::MASKING_ON_RENDERING);
}
map.Insert(Toolkit::ImageVisual::Property::LOAD_POLICY, mLoadPolicy);
map.Insert(Toolkit::ImageVisual::Property::RELEASE_POLICY, mReleasePolicy);
map.Insert(Toolkit::ImageVisual::Property::ORIENTATION_CORRECTION, mOrientationCorrection);
+
+ map.Insert(Toolkit::DevelImageVisual::Property::FAST_TRACK_UPLOADING, mUseFastTrackUploading);
}
void ImageVisual::DoCreateInstancePropertyMap(Property::Map& map) const
}
}
-void ImageVisual::OnDoAction(const Dali::Property::Index actionName, const Dali::Property::Value& attributes)
+void ImageVisual::OnDoAction(const Dali::Property::Index actionId, const Dali::Property::Value& attributes)
{
// Check if action is valid for this visual type and perform action if possible
- switch(actionName)
+ switch(actionId)
{
case DevelImageVisual::Action::RELOAD:
{
{
if(mImpl->mRenderer)
{
- mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
+ mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
}
}
-bool ImageVisual::IsResourceReady() const
-{
- return (mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::READY ||
- mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::FAILED);
-}
-
void ImageVisual::UpdateShader()
{
if(mImpl->mRenderer)
{
- Shader shader = GetShader();
+ Shader shader = GenerateShader();
mImpl->mRenderer.SetShader(shader);
}
}
{
mImpl->mRenderer.RegisterProperty(ATLAS_RECT_UNIFORM_NAME, mAtlasRect);
actor.AddRenderer(mImpl->mRenderer);
+ mRendererAdded = true;
// reset the weak handle so that the renderer only get added to actor once
mPlacementActor.Reset();
}
mLoadState = TextureManager::LoadState::LOAD_FINISHED;
}
-// From Texture Manager
-void ImageVisual::UploadComplete(bool loadingSuccess, int32_t textureId, TextureSet textureSet, bool usingAtlas, const Vector4& atlasRectangle, bool preMultiplied)
+// From FastTrackLoadingTask
+void ImageVisual::FastLoadComplete(FastTrackLoadingTaskPtr task)
{
Toolkit::Visual::ResourceStatus resourceStatus;
- if(mImpl->mRenderer)
+
+ DALI_ASSERT_ALWAYS(mFastTrackLoadingTask == task && "Task was not canceled successfully!");
+ DALI_ASSERT_ALWAYS(mRendererAdded && "Some FastTrack logic missed!");
+
+ Actor actor = mPlacementActor.GetHandle();
+
+ if(mFastTrackLoadingTask && mFastTrackLoadingTask->mLoadSuccess)
{
- if(usingAtlas)
+ resourceStatus = Toolkit::Visual::ResourceStatus::READY;
+ mLoadState = TextureManager::LoadState::LOAD_FINISHED;
+
+ // Change premultiplied alpha flag after change renderer.
+ EnablePreMultipliedAlpha(mFastTrackLoadingTask->mPremultiplied);
+
+ if(mFastTrackLoadingTask->mLoadPlanesAvaliable)
{
- mImpl->mRenderer.RegisterProperty(ATLAS_RECT_UNIFORM_NAME, mAtlasRect);
+ if(mFastTrackLoadingTask->mPlanesLoaded)
+ {
+ // Let we use regular yuv cases.
+ mNeedYuvToRgb = true;
+ }
+ else
+ {
+ // Let we use regular image cases.
+ mNeedYuvToRgb = false;
+
+ auto textureSet = mImpl->mRenderer.GetTextures();
+ DALI_ASSERT_ALWAYS(textureSet && textureSet.GetTextureCount() > 0u && "Previous texture set must exist!");
+
+ Dali::TextureSet newTextureSet = TextureSet::New();
+ newTextureSet.SetTexture(0u, textureSet.GetTexture(0u));
+ mImpl->mRenderer.SetTextures(newTextureSet);
+ }
+
+ // We can specify what kind of shader we need to use now. Update shader.
+ mNeedUnifiedYuvAndRgb = false;
+ UpdateShader();
}
+ }
+ else
+ {
+ resourceStatus = Toolkit::Visual::ResourceStatus::FAILED;
+ mLoadState = TextureManager::LoadState::LOAD_FAILED;
+
+ // Change renderer as broken.
+ ShowBrokenImage();
+ }
+
+ mFastTrackLoadingTask.Reset();
- EnablePreMultipliedAlpha(preMultiplied);
+ // Signal to observers ( control ) that resources are ready. Must be all resources.
+ ResourceReady(resourceStatus);
+}
+
+// From Texture Manager
+void ImageVisual::LoadComplete(bool loadingSuccess, TextureInformation textureInformation)
+{
+ Toolkit::Visual::ResourceStatus resourceStatus;
+ if(DALI_UNLIKELY(mImpl == nullptr))
+ {
+ DALI_LOG_ERROR("Fatal error!! already destroyed object callback called! ImageVisual : %p, url : %s\n", this, mImageUrl.GetUrl().c_str());
+ return;
+ }
+ if(mImpl->mRenderer)
+ {
+ EnablePreMultipliedAlpha(textureInformation.preMultiplied);
Actor actor = mPlacementActor.GetHandle();
- if(actor)
+ if(!loadingSuccess)
{
- actor.AddRenderer(mImpl->mRenderer);
- // reset the weak handle so that the renderer only get added to actor once
- mPlacementActor.Reset();
+ ShowBrokenImage();
+ textureInformation.textureSet = mImpl->mRenderer.GetTextures();
}
-
- if(!loadingSuccess)
+ else
{
- Texture brokenImage = mFactoryCache.GetBrokenVisualImage();
+ Sampler sampler = Sampler::New();
+ sampler.SetWrapMode(mWrapModeU, mWrapModeV);
+ textureInformation.textureSet.SetSampler(0u, sampler);
- textureSet = TextureSet::New();
- textureSet.SetTexture(0u, brokenImage);
- mImpl->mRenderer.SetTextures(textureSet);
- }
+ mImpl->mRenderer.SetTextures(textureInformation.textureSet);
+ ComputeTextureSize();
+ CheckMaskTexture();
- Sampler sampler = Sampler::New();
- sampler.SetWrapMode(mWrapModeU, mWrapModeV);
- textureSet.SetSampler(0u, sampler);
- mImpl->mRenderer.SetTextures(textureSet);
+ if(textureInformation.textureSet.GetTextureCount() == 3)
+ {
+ if(textureInformation.textureSet.GetTexture(0).GetPixelFormat() == Pixel::L8 && textureInformation.textureSet.GetTexture(1).GetPixelFormat() == Pixel::CHROMINANCE_U && textureInformation.textureSet.GetTexture(2).GetPixelFormat() == Pixel::CHROMINANCE_V)
+ {
+ mNeedYuvToRgb = true;
+ UpdateShader();
+ }
+ }
+
+ if(actor)
+ {
+ actor.AddRenderer(mImpl->mRenderer);
+ mRendererAdded = true;
+ // reset the weak handle so that the renderer only get added to actor once
+ mPlacementActor.Reset();
+ }
+ }
}
// Storing TextureSet needed when renderer staged.
if(!mImpl->mRenderer)
{
- mTextures = textureSet;
+ mTextures = textureInformation.textureSet;
}
// Image loaded, set status regardless of staged status.
{
if(mTextureId != TextureManager::INVALID_TEXTURE_ID)
{
- mFactoryCache.GetTextureManager().Remove(mTextureId, this);
+ mFactoryCache.GetTextureManager().RequestRemove(mTextureId, this);
mTextureId = TextureManager::INVALID_TEXTURE_ID;
}
else
{
+ ResetFastTrackLoadingTask();
+
Vector4 atlasRect(0.f, 0.f, 1.f, 1.f);
Property::Index index = mImpl->mRenderer.GetPropertyIndex(ATLAS_RECT_UNIFORM_NAME);
if(index != Property::INVALID_INDEX)
}
}
-Shader ImageVisual::GetShader()
+void ImageVisual::ComputeTextureSize()
{
- Shader shader;
-
- std::string_view vertexShaderView;
- bool usesWholeTexture = true;
- if(mImpl->mCustomShader && !mImpl->mCustomShader->mVertexShader.empty())
+ if(mImpl->mRenderer)
{
- vertexShaderView = mImpl->mCustomShader->mVertexShader;
- usesWholeTexture = false; // Impossible to tell.
+ auto textureSet = mImpl->mRenderer.GetTextures();
+ if(textureSet && textureSet.GetTextureCount())
+ {
+ auto texture = textureSet.GetTexture(0);
+ if(texture)
+ {
+ mTextureSize.x = texture.GetWidth();
+ mTextureSize.y = texture.GetHeight();
+ if(textureSet.GetTextureCount() > 1u && mMaskingData && !mMaskingData->mPreappliedMasking && mMaskingData->mCropToMask)
+ {
+ Texture maskTexture = textureSet.GetTexture(1);
+ if(maskTexture)
+ {
+ mTextureSize.x = std::min(static_cast<uint32_t>(mTextureSize.x * mMaskingData->mContentScaleFactor), maskTexture.GetWidth());
+ mTextureSize.y = std::min(static_cast<uint32_t>(mTextureSize.y * mMaskingData->mContentScaleFactor), maskTexture.GetHeight());
+ }
+ }
+ }
+ }
}
- else
+}
+
+Vector2 ImageVisual::ComputeMaskTextureRatio()
+{
+ Vector2 maskTextureRatio;
+ if(mImpl->mRenderer)
{
- vertexShaderView = mImageVisualShaderFactory.GetVertexShaderSource();
+ auto textureSet = mImpl->mRenderer.GetTextures();
+ if(textureSet && textureSet.GetTextureCount())
+ {
+ auto texture = textureSet.GetTexture(0);
+ if(texture)
+ {
+ if(textureSet.GetTextureCount() > 1u && mMaskingData && !mMaskingData->mPreappliedMasking && mMaskingData->mCropToMask)
+ {
+ Texture maskTexture = textureSet.GetTexture(1);
+ if(maskTexture)
+ {
+ float textureWidth = std::max(static_cast<float>(texture.GetWidth() * mMaskingData->mContentScaleFactor), Dali::Math::MACHINE_EPSILON_1);
+ float textureHeight = std::max(static_cast<float>(texture.GetHeight() * mMaskingData->mContentScaleFactor), Dali::Math::MACHINE_EPSILON_1);
+ maskTextureRatio = Vector2(std::min(static_cast<float>(maskTexture.GetWidth()), textureWidth) / textureWidth,
+ std::min(static_cast<float>(maskTexture.GetHeight()), textureHeight) / textureHeight);
+ }
+ }
+ }
+ }
}
+ return maskTextureRatio;
+}
+
+Shader ImageVisual::GenerateShader() const
+{
+ Shader shader;
- std::string_view fragmentShaderView;
- if(mImpl->mCustomShader && !mImpl->mCustomShader->mFragmentShader.empty())
+ const bool useStandardShader = !mImpl->mCustomShader;
+ const bool useNativeImage = (mTextures && DevelTexture::IsNative(mTextures.GetTexture(0)));
+
+ if(useStandardShader)
{
- fragmentShaderView = mImpl->mCustomShader->mFragmentShader;
+ bool requiredAlphaMaskingOnRendering = (mMaskingData && !mMaskingData->mMaskImageLoadingFailed) ? !mMaskingData->mPreappliedMasking : false;
+ // Create and cache the standard shader
+ shader = mImageVisualShaderFactory.GetShader(
+ mFactoryCache,
+ ImageVisualShaderFeatureBuilder()
+ .EnableTextureAtlas(mImpl->mFlags & Visual::Base::Impl::IS_ATLASING_APPLIED && !useNativeImage)
+ .ApplyDefaultTextureWrapMode(mWrapModeU <= WrapMode::CLAMP_TO_EDGE && mWrapModeV <= WrapMode::CLAMP_TO_EDGE)
+ .EnableRoundedCorner(IsRoundedCornerRequired())
+ .EnableBorderline(IsBorderlineRequired())
+ .SetTextureForFragmentShaderCheck(useNativeImage ? mTextures.GetTexture(0) : Dali::Texture())
+ .EnableAlphaMaskingOnRendering(requiredAlphaMaskingOnRendering)
+ .EnableYuvToRgb(mNeedYuvToRgb, mNeedUnifiedYuvAndRgb));
}
else
{
- fragmentShaderView = mImageVisualShaderFactory.GetFragmentShaderSource();
+ bool usesWholeTexture = true;
+ std::string_view vertexShaderView;
+ std::string_view fragmentShaderView;
+
+ if(mImpl->mCustomShader && !mImpl->mCustomShader->mVertexShader.empty())
+ {
+ vertexShaderView = mImpl->mCustomShader->mVertexShader;
+ usesWholeTexture = false; // Impossible to tell.
+ }
+ else
+ {
+ vertexShaderView = mImageVisualShaderFactory.GetVertexShaderSource();
+ }
+
+ if(mImpl->mCustomShader && !mImpl->mCustomShader->mFragmentShader.empty())
+ {
+ fragmentShaderView = mImpl->mCustomShader->mFragmentShader;
+ }
+ else
+ {
+ fragmentShaderView = mImageVisualShaderFactory.GetFragmentShaderSource();
+ }
+
+ // If the texture is native, we may need to change prefix and sampler in
+ // the fragment shader
+ if(useNativeImage)
+ {
+ bool modifiedFragmentShader = false;
+ Texture nativeTexture = mTextures.GetTexture(0);
+ std::string fragmentShaderString = std::string(fragmentShaderView);
+
+ modifiedFragmentShader = DevelTexture::ApplyNativeFragmentShader(nativeTexture, fragmentShaderString);
+ if(modifiedFragmentShader)
+ {
+ fragmentShaderView = fragmentShaderString;
+ }
+
+ // Create shader here cause fragmentShaderString scope issue
+ shader = Shader::New(vertexShaderView, fragmentShaderView, mImpl->mCustomShader->mHints);
+ }
+ else
+ {
+ shader = Shader::New(vertexShaderView, fragmentShaderView, mImpl->mCustomShader->mHints);
+ }
+
+ if(usesWholeTexture)
+ {
+ shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
+ }
}
- // If the texture is native, we may need to change prefix and sampler in
- // the fragment shader
- bool modifiedFragmentShader = false;
- std::string fragmentShaderString;
- if(mTextures && DevelTexture::IsNative(mTextures.GetTexture(0)))
+ return shader;
+}
+
+Dali::Property ImageVisual::OnGetPropertyObject(Dali::Property::Key key)
+{
+ if((key.type == Property::Key::INDEX && key.indexKey == Toolkit::ImageVisual::Property::PIXEL_AREA) || (key.type == Property::Key::STRING && key.stringKey == PIXEL_AREA_UNIFORM_NAME))
{
- Texture nativeTexture = mTextures.GetTexture(0);
- fragmentShaderString = std::string(fragmentShaderView);
- modifiedFragmentShader = DevelTexture::ApplyNativeFragmentShader(nativeTexture, fragmentShaderString);
- fragmentShaderView = fragmentShaderString;
+ if(DALI_LIKELY(mImpl->mRenderer))
+ {
+ if(mPixelAreaIndex == Property::INVALID_INDEX)
+ {
+ mPixelAreaIndex = mImpl->mRenderer.RegisterProperty(mPixelAreaIndex, PIXEL_AREA_UNIFORM_NAME, mPixelArea);
+ }
+ return Dali::Property(mImpl->mRenderer, mPixelAreaIndex);
+ }
}
- const bool useStandardShader = !mImpl->mCustomShader && !modifiedFragmentShader;
- if(useStandardShader)
+ Handle handle;
+ return Dali::Property(handle, Property::INVALID_INDEX);
+}
+
+void ImageVisual::CheckMaskTexture()
+{
+ if(mMaskingData && !mMaskingData->mPreappliedMasking)
{
- // Create and cache the standard shader
- shader = mImageVisualShaderFactory.GetShader(
- mFactoryCache,
- mImpl->mFlags & Impl::IS_ATLASING_APPLIED,
- mWrapModeU <= WrapMode::CLAMP_TO_EDGE && mWrapModeV <= WrapMode::CLAMP_TO_EDGE,
- IsRoundedCornerRequired());
+ bool maskLoadFailed = true;
+ TextureSet textures = mImpl->mRenderer.GetTextures();
+ if(textures && textures.GetTextureCount() >= TEXTURE_COUNT_FOR_GPU_ALPHA_MASK)
+ {
+ if(mMaskingData->mCropToMask)
+ {
+ mImpl->mRenderer.RegisterProperty(MASK_TEXTURE_RATIO_NAME, ComputeMaskTextureRatio());
+ }
+ else
+ {
+ mImpl->mRenderer.RegisterProperty(MASK_TEXTURE_RATIO_NAME, Vector2::ONE);
+ }
+ maskLoadFailed = false;
+ }
+
+ if(mMaskingData->mMaskImageLoadingFailed != maskLoadFailed)
+ {
+ mMaskingData->mMaskImageLoadingFailed = maskLoadFailed;
+ UpdateShader();
+ }
}
- else if(mImpl->mCustomShader)
+}
+
+void ImageVisual::ResetRenderer()
+{
+ RemoveTexture(); // If INVALID_TEXTURE_ID then removal will be attempted on atlas
+ mImpl->mResourceStatus = Toolkit::Visual::ResourceStatus::PREPARING;
+
+ TextureSet textureSet = TextureSet::New();
+ mImpl->mRenderer.SetTextures(textureSet);
+ ComputeTextureSize();
+
+ mLoadState = TextureManager::LoadState::NOT_STARTED;
+}
+
+void ImageVisual::ShowBrokenImage()
+{
+ if(mEnableBrokenImage)
{
- shader = Shader::New(vertexShaderView, fragmentShaderView, mImpl->mCustomShader->mHints);
+ Actor actor = mPlacementActor.GetHandle();
+
+ Vector2 imageSize = Vector2::ZERO;
+ if(actor)
+ {
+ imageSize = actor.GetProperty(Actor::Property::SIZE).Get<Vector2>();
+ mPlacementActorSize = imageSize;
+
+ if(mRendererAdded)
+ {
+ actor.RemoveRenderer(mImpl->mRenderer);
+ mRendererAdded = false;
+ }
+ }
+
+ mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize);
+ if(actor)
+ {
+ actor.AddRenderer(mImpl->mRenderer);
+ mRendererAdded = true;
+ mPlacementActor.Reset();
+ }
}
else
{
- shader = Shader::New(vertexShaderView, fragmentShaderView);
+ if(mRendererAdded)
+ {
+ Actor actor = mPlacementActor.GetHandle();
+ if(actor)
+ {
+ actor.RemoveRenderer(mImpl->mRenderer);
+ mRendererAdded = false;
+ }
+ }
+ ResetRenderer();
}
+}
- if(usesWholeTexture)
+void ImageVisual::ResetFastTrackLoadingTask()
+{
+ if(mFastTrackLoadingTask)
{
- shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
+ Dali::AsyncTaskManager::Get().RemoveTask(mFastTrackLoadingTask);
+ mFastTrackLoadingTask.Reset();
}
-
- // Set pixel align off as default.
- // ToDo: Pixel align causes issues such as rattling image animation.
- // We should trun it off until issues are resolved
- shader.RegisterProperty(PIXEL_ALIGNED_UNIFORM_NAME, PIXEL_ALIGN_OFF);
-
- return shader;
}
} // namespace Internal