/*
- * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2022 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 "npatch-visual.h"
// EXTERNAL INCLUDES
-#include <dali/integration-api/platform-abstraction.h>
-#include <dali/public-api/images/buffer-image.h>
-#include <dali/public-api/images/resource-image.h>
-#include <dali/devel-api/images/texture-set-image.h>
+#include <dali/devel-api/adaptor-framework/image-loading.h>
+#include <dali/devel-api/common/stage.h>
+#include <dali/devel-api/rendering/renderer-devel.h>
+#include <dali/integration-api/debug.h>
// INTERNAL INCLUDES
-#include <dali-toolkit/public-api/visuals/image-visual-properties.h>
-#include <dali-toolkit/devel-api/visual-factory/devel-visual-properties.h>
-#include <dali-toolkit/internal/visuals/visual-factory-impl.h>
+#include <dali-toolkit/devel-api/utility/npatch-helper.h>
+#include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
+#include <dali-toolkit/internal/graphics/builtin-shader-extern-gen.h>
+#include <dali-toolkit/internal/visuals/image-visual-shader-factory.h>
+#include <dali-toolkit/internal/visuals/npatch-loader.h>
+#include <dali-toolkit/internal/visuals/rendering-addon.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>
+#include <dali-toolkit/internal/visuals/visual-factory-impl.h>
#include <dali-toolkit/internal/visuals/visual-string-constants.h>
-#include <dali-toolkit/internal/visuals/visual-base-impl.h>
-#include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
-
+#include <dali-toolkit/public-api/visuals/visual-properties.h>
namespace Dali
{
-
namespace Toolkit
{
-
namespace Internal
{
-
namespace
{
-const char * const BORDER_ONLY("borderOnly");
-
-const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
- attribute mediump vec2 aPosition;\n
- varying mediump vec2 vTexCoord;\n
- uniform mediump mat4 uMvpMatrix;\n
- uniform mediump vec3 uSize;\n
- uniform mediump vec2 uNinePatchFactorsX[ FACTOR_SIZE_X ];\n
- uniform mediump vec2 uNinePatchFactorsY[ FACTOR_SIZE_Y ];\n
- \n
-
- //Visual size and offset
- uniform mediump vec2 offset;\n
- uniform mediump vec2 size;\n
- uniform mediump vec4 offsetSizeMode;\n
- uniform mediump vec2 origin;\n
- uniform mediump vec2 anchorPoint;\n
-
- void main()\n
- {\n
- mediump vec2 fixedFactor = vec2( uNinePatchFactorsX[ int( ( aPosition.x + 1.0 ) * 0.5 ) ].x, uNinePatchFactorsY[ int( ( aPosition.y + 1.0 ) * 0.5 ) ].x );\n
- mediump vec2 stretch = vec2( uNinePatchFactorsX[ int( ( aPosition.x ) * 0.5 ) ].y, uNinePatchFactorsY[ int( ( aPosition.y ) * 0.5 ) ].y );\n
- \n
- mediump vec2 fixedTotal = vec2( uNinePatchFactorsX[ FACTOR_SIZE_X - 1 ].x, uNinePatchFactorsY[ FACTOR_SIZE_Y - 1 ].x );\n
- mediump vec2 stretchTotal = vec2( uNinePatchFactorsX[ FACTOR_SIZE_X - 1 ].y, uNinePatchFactorsY[ FACTOR_SIZE_Y - 1 ].y );\n
- \n
-
- vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
- vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
-
- mediump vec4 vertexPosition = vec4( ( fixedFactor + ( visualSize.xy - fixedTotal ) * stretch / stretchTotal ) + anchorPoint*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );\n
- vertexPosition.xy -= visualSize.xy * vec2( 0.5, 0.5 );\n
-
- vertexPosition = uMvpMatrix * vertexPosition;\n
- \n
- vTexCoord = ( fixedFactor + stretch ) / ( fixedTotal + stretchTotal );\n
- \n
- gl_Position = vertexPosition;\n
- }\n
-);
-
-const char* VERTEX_SHADER_3X3 = DALI_COMPOSE_SHADER(
- attribute mediump vec2 aPosition;\n
- varying mediump vec2 vTexCoord;\n
- uniform mediump mat4 uModelMatrix;\n
- uniform mediump mat4 uMvpMatrix;\n
- uniform mediump vec3 uSize;\n
- uniform mediump vec2 uFixed[ 3 ];\n
- uniform mediump vec2 uStretchTotal;\n
- \n
-
- //Visual size and offset
- uniform mediump vec2 offset;\n
- uniform mediump vec2 size;\n
- uniform mediump vec4 offsetSizeMode;\n
- uniform mediump vec2 origin;\n
- uniform mediump vec2 anchorPoint;\n
-
- void main()\n
- {\n
- vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
- vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
-
- mediump vec2 scale = vec2( length( uModelMatrix[ 0 ].xyz ), length( uModelMatrix[ 1 ].xyz ) );\n
- mediump vec2 size = visualSize.xy * scale;\n
- \n
- mediump vec2 fixedFactor = vec2( uFixed[ int( ( aPosition.x + 1.0 ) * 0.5 ) ].x, uFixed[ int( ( aPosition.y + 1.0 ) * 0.5 ) ].y );\n
- mediump vec2 stretch = floor( aPosition * 0.5 );\n
- mediump vec2 fixedTotal = uFixed[ 2 ];\n
- \n
- mediump vec4 vertexPosition = vec4( fixedFactor + ( size - fixedTotal ) * stretch, 0.0, 1.0 );
- vertexPosition.xy -= size * vec2( 0.5, 0.5 );\n
- vertexPosition.xy = vertexPosition.xy / scale + anchorPoint*size + (visualOffset + origin)*uSize.xy;\
- \n
- vertexPosition = uMvpMatrix * vertexPosition;\n
- \n
- vTexCoord = ( fixedFactor + stretch * uStretchTotal ) / ( fixedTotal + uStretchTotal );\n
- \n
- gl_Position = vertexPosition;\n
- }\n
-);
-
-const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
- varying mediump vec2 vTexCoord;\n
- uniform sampler2D sTexture;\n
- uniform lowp vec4 uColor;\n
- \n
- void main()\n
- {\n
- gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n
- }\n
-);
-
-/**
- * @brief Creates the geometry formed from the vertices and indices
- *
- * @param[in] vertices The vertices to generate the geometry from
- * @param[in] indices The indices to generate the geometry from
- * @return The geometry formed from the vertices and indices
- */
-Geometry GenerateGeometry( const Vector< Vector2 >& vertices, const Vector< unsigned short >& indices )
-{
- Property::Map vertexFormat;
- vertexFormat[ "aPosition" ] = Property::VECTOR2;
- PropertyBuffer vertexPropertyBuffer = PropertyBuffer::New( vertexFormat );
- if( vertices.Size() > 0 )
- {
- vertexPropertyBuffer.SetData( &vertices[ 0 ], vertices.Size() );
- }
-
- // Create the geometry object
- Geometry geometry = Geometry::New();
- geometry.AddVertexBuffer( vertexPropertyBuffer );
- if( indices.Size() > 0 )
- {
- geometry.SetIndexBuffer( &indices[ 0 ], indices.Size() );
- }
-
-
- return geometry;
-}
-
-/**
- * @brief Adds the indices to form a quad composed off two triangles where the indices are organised in a grid
- *
- * @param[out] indices The indices to add to
- * @param[in] rowIdx The row index to start the quad
- * @param[in] nextRowIdx The index to the next row
- */
-void AddQuadIndices( Vector< unsigned short >& indices, unsigned int rowIdx, unsigned int nextRowIdx )
-{
- indices.PushBack( rowIdx );
- indices.PushBack( nextRowIdx + 1 );
- indices.PushBack( rowIdx + 1 );
-
- indices.PushBack( rowIdx );
- indices.PushBack( nextRowIdx );
- indices.PushBack( nextRowIdx + 1 );
-}
-
-void AddVertex( Vector< Vector2 >& vertices, unsigned int x, unsigned int y )
-{
- vertices.PushBack( Vector2( x, y ) );
-}
-
-void RegisterStretchProperties( Renderer& renderer, const char * uniformName, const NinePatchImage::StretchRanges& stretchPixels, uint16_t imageExtent)
-{
- uint16_t prevEnd = 0;
- uint16_t prevFix = 0;
- uint16_t prevStretch = 0;
- unsigned int i = 1;
- for( NinePatchImage::StretchRanges::ConstIterator it = stretchPixels.Begin(); it != stretchPixels.End(); ++it, ++i )
- {
- uint16_t start = it->GetX();
- uint16_t end = it->GetY();
-
- uint16_t fix = prevFix + start - prevEnd;
- uint16_t stretch = prevStretch + end - start;
-
- std::stringstream uniform;
- uniform << uniformName << "[" << i << "]";
- renderer.RegisterProperty( uniform.str(), Vector2( fix, stretch ) );
-
- prevEnd = end;
- prevFix = fix;
- prevStretch = stretch;
- }
-
- {
- prevFix += imageExtent - prevEnd;
- std::stringstream uniform;
- uniform << uniformName << "[" << i << "]";
- renderer.RegisterProperty( uniform.str(), Vector2( prevFix, prevStretch ) );
- }
+const int CUSTOM_PROPERTY_COUNT(5); // fixed(3),stretch,aux
}
-} //unnamed namespace
-
/////////////////NPatchVisual////////////////
-NPatchVisualPtr NPatchVisual::New( VisualFactoryCache& factoryCache )
+NPatchVisualPtr NPatchVisual::New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties)
{
- return new NPatchVisual( factoryCache );
-}
-
-NPatchVisualPtr NPatchVisual::New( VisualFactoryCache& factoryCache, const std::string& imageUrl, bool borderOnly )
-{
- NPatchVisual* nPatchVisual = new NPatchVisual( factoryCache, borderOnly );
+ NPatchVisualPtr nPatchVisual(new NPatchVisual(factoryCache, shaderFactory));
nPatchVisual->mImageUrl = imageUrl;
-
- NinePatchImage image = NinePatchImage::New( imageUrl );
- nPatchVisual->InitializeFromImage( image );
-
+ nPatchVisual->SetProperties(properties);
+ nPatchVisual->Initialize();
return nPatchVisual;
}
-NPatchVisualPtr NPatchVisual::New( VisualFactoryCache& factoryCache, NinePatchImage image, bool borderOnly )
+NPatchVisualPtr NPatchVisual::New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl)
{
- NPatchVisual* nPatchVisual = new NPatchVisual( factoryCache, borderOnly );
- nPatchVisual->mImage = image;
-
- nPatchVisual->InitializeFromImage( image );
-
+ NPatchVisualPtr nPatchVisual(new NPatchVisual(factoryCache, shaderFactory));
+ nPatchVisual->mImageUrl = imageUrl;
+ nPatchVisual->Initialize();
return nPatchVisual;
}
-NPatchVisual::NPatchVisual( VisualFactoryCache& factoryCache, bool borderOnly )
-: Visual::Base( factoryCache ),
- mImage(),
- mCroppedImage(),
- mImageUrl(),
- mStretchPixelsX(),
- mStretchPixelsY(),
- mImageSize(),
- mBorderOnly( borderOnly )
+void NPatchVisual::LoadImages()
{
-}
-
-NPatchVisual::~NPatchVisual()
-{
-}
+ TextureManager& textureManager = mFactoryCache.GetTextureManager();
+ bool synchronousLoading = mImpl->mFlags & Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
-void NPatchVisual::DoSetProperties( const Property::Map& propertyMap )
-{
- Property::Value* imageURLValue = propertyMap.Find( Toolkit::ImageVisual::Property::URL, IMAGE_URL_NAME );
- if( imageURLValue )
+ if(mId == NPatchData::INVALID_NPATCH_DATA_ID && (mImageUrl.IsLocalResource() || mImageUrl.IsBufferResource()))
{
- //Read the borderOnly property first since InitialiseFromImage relies on mBorderOnly to be properly set
- Property::Value* borderOnlyValue = propertyMap.Find( Toolkit::ImageVisual::Property::BORDER_ONLY, BORDER_ONLY );
- if( borderOnlyValue )
- {
- borderOnlyValue->Get( mBorderOnly );
- }
+ bool preMultiplyOnLoad = IsPreMultipliedAlphaEnabled() && !mImpl->mCustomShader ? true : false;
+ mId = mLoader.Load(textureManager, this, mImageUrl, mBorder, preMultiplyOnLoad, synchronousLoading);
- if( imageURLValue->Get( mImageUrl ) )
+ const NPatchData* data;
+ if(mLoader.GetNPatchData(mId, data) && data->GetLoadingState() == NPatchData::LoadingState::LOAD_COMPLETE)
{
- NinePatchImage nPatch = NinePatchImage::New( mImageUrl );
- InitializeFromImage( nPatch );
- }
- else
- {
- InitializeFromBrokenImage();
- DALI_LOG_ERROR( "The property '%s' is not a string\n", IMAGE_URL_NAME );
+ EnablePreMultipliedAlpha(data->IsPreMultiplied());
}
}
-}
-void NPatchVisual::GetNaturalSize( Vector2& naturalSize ) const
-{
- if( mImage )
- {
- naturalSize.x = mImage.GetWidth();
- naturalSize.y = mImage.GetHeight();
- }
- else if( !mImageUrl.empty() )
+ if(!mAuxiliaryPixelBuffer && mAuxiliaryUrl.IsValid() && (mAuxiliaryUrl.IsLocalResource() || mAuxiliaryUrl.IsBufferResource()))
{
- ImageDimensions dimentions = ResourceImage::GetImageSize( mImageUrl );
- naturalSize.x = dimentions.GetWidth();
- naturalSize.y = dimentions.GetHeight();
- }
- else
- {
- naturalSize = Vector2::ZERO;
+ // Load the auxiliary image
+ auto preMultiplyOnLoading = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
+ mAuxiliaryPixelBuffer = textureManager.LoadPixelBuffer(mAuxiliaryUrl, Dali::ImageDimensions(), FittingMode::DEFAULT, SamplingMode::BOX_THEN_LINEAR, synchronousLoading, this, true, preMultiplyOnLoading);
}
}
-Geometry NPatchVisual::CreateGeometry()
+void NPatchVisual::GetNaturalSize(Vector2& naturalSize)
{
- Geometry geometry;
- if( mStretchPixelsX.Size() == 1 && mStretchPixelsY.Size() == 1 )
+ naturalSize.x = 0u;
+ naturalSize.y = 0u;
+
+ // load now if not already loaded
+ const NPatchData* data;
+ if(mLoader.GetNPatchData(mId, data) && data->GetLoadingState() != NPatchData::LoadingState::LOADING)
{
- if( !mBorderOnly )
- {
- geometry = mFactoryCache.GetGeometry( VisualFactoryCache::NINE_PATCH_GEOMETRY );
- if( !geometry )
- {
- geometry = CreateGeometry( Uint16Pair( 3, 3 ) );
- mFactoryCache.SaveGeometry( VisualFactoryCache::NINE_PATCH_GEOMETRY, geometry );
- }
- }
- else
+ naturalSize.x = data->GetCroppedWidth();
+ naturalSize.y = data->GetCroppedHeight();
+ }
+ else
+ {
+ if(mImageUrl.IsValid())
{
- geometry = mFactoryCache.GetGeometry( VisualFactoryCache::NINE_PATCH_BORDER_GEOMETRY );
- if( !geometry )
+ ImageDimensions dimensions = Dali::GetOriginalImageSize(mImageUrl.GetUrl());
+ if(dimensions != ImageDimensions(0, 0))
{
- geometry = CreateGeometryBorder( Uint16Pair( 3, 3 ) );
- mFactoryCache.SaveGeometry( VisualFactoryCache::NINE_PATCH_BORDER_GEOMETRY, geometry );
+ naturalSize.x = dimensions.GetWidth();
+ naturalSize.y = dimensions.GetHeight();
}
}
}
- else if( mStretchPixelsX.Size() > 0 || mStretchPixelsY.Size() > 0)
+
+ if(mAuxiliaryPixelBuffer)
{
- Uint16Pair gridSize( 2 * mStretchPixelsX.Size() + 1, 2 * mStretchPixelsY.Size() + 1 );
- geometry = !mBorderOnly ? CreateGeometry( gridSize ) : CreateGeometryBorder( gridSize );
+ naturalSize.x = std::max(naturalSize.x, float(mAuxiliaryPixelBuffer.GetWidth()));
+ naturalSize.y = std::max(naturalSize.y, float(mAuxiliaryPixelBuffer.GetHeight()));
}
-
- return geometry;
}
-Shader NPatchVisual::CreateShader()
+void NPatchVisual::DoSetProperties(const Property::Map& propertyMap)
{
- Shader shader;
- if( !mImpl->mCustomShader )
+ // URL is already passed in via constructor
+
+ Property::Value* borderOnlyValue = propertyMap.Find(Toolkit::ImageVisual::Property::BORDER_ONLY, BORDER_ONLY);
+ if(borderOnlyValue)
{
- if( mStretchPixelsX.Size() == 1 && mStretchPixelsY.Size() == 1 )
- {
- shader = mFactoryCache.GetShader( VisualFactoryCache::NINE_PATCH_SHADER );
- if( !shader )
- {
- shader = Shader::New( VERTEX_SHADER_3X3, FRAGMENT_SHADER );
- mFactoryCache.SaveShader( VisualFactoryCache::NINE_PATCH_SHADER, shader );
- }
- }
- else if( mStretchPixelsX.Size() > 0 || mStretchPixelsY.Size() > 0)
- {
- std::stringstream vertexShader;
- vertexShader << "#define FACTOR_SIZE_X " << mStretchPixelsX.Size() + 2 << "\n"
- << "#define FACTOR_SIZE_Y " << mStretchPixelsY.Size() + 2 << "\n"
- << VERTEX_SHADER;
+ borderOnlyValue->Get(mBorderOnly);
+ }
- shader = Shader::New( vertexShader.str(), FRAGMENT_SHADER );
+ Property::Value* borderValue = propertyMap.Find(Toolkit::ImageVisual::Property::BORDER, BORDER);
+ if(borderValue && !borderValue->Get(mBorder)) // If value exists and is rect, just set mBorder
+ {
+ // Not a rect so try vector4
+ Vector4 border;
+ if(borderValue->Get(border))
+ {
+ mBorder.left = static_cast<int>(border.x);
+ mBorder.right = static_cast<int>(border.y);
+ mBorder.bottom = static_cast<int>(border.z);
+ mBorder.top = static_cast<int>(border.w);
}
}
- else
- {
- const char* fragmentShader = FRAGMENT_SHADER;
- Dali::Shader::Hint::Value hints = Dali::Shader::Hint::NONE;
- if( !mImpl->mCustomShader->mFragmentShader.empty() )
+ Property::Value* auxImage = propertyMap.Find(Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE, AUXILIARY_IMAGE_NAME);
+ if(auxImage)
+ {
+ std::string url;
+ if(auxImage->Get(url))
{
- fragmentShader = mImpl->mCustomShader->mFragmentShader.c_str();
+ mAuxiliaryUrl = url;
}
- hints = mImpl->mCustomShader->mHints;
+ }
- if( mStretchPixelsX.Size() == 1 && mStretchPixelsY.Size() == 1 )
+ Property::Value* auxImageAlpha = propertyMap.Find(Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE_ALPHA, AUXILIARY_IMAGE_ALPHA_NAME);
+ if(auxImageAlpha)
+ {
+ auxImageAlpha->Get(mAuxiliaryImageAlpha);
+ }
+
+ Property::Value* synchronousLoading = propertyMap.Find(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, SYNCHRONOUS_LOADING);
+ if(synchronousLoading)
+ {
+ bool sync = false;
+ synchronousLoading->Get(sync);
+ if(sync)
{
- shader = Shader::New( VERTEX_SHADER_3X3, fragmentShader, hints );
+ mImpl->mFlags |= Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
}
- else if( mStretchPixelsX.Size() > 0 || mStretchPixelsY.Size() > 0)
+ else
{
- std::stringstream vertexShader;
- vertexShader << "#define FACTOR_SIZE_X " << mStretchPixelsX.Size() + 2 << "\n"
- << "#define FACTOR_SIZE_Y " << mStretchPixelsY.Size() + 2 << "\n"
- << VERTEX_SHADER;
-
- shader = Shader::New( vertexShader.str(), fragmentShader, hints );
+ mImpl->mFlags &= ~Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
}
}
- return shader;
+ Property::Value* releasePolicy = propertyMap.Find(Toolkit::ImageVisual::Property::RELEASE_POLICY, RELEASE_POLICY_NAME);
+ if(releasePolicy)
+ {
+ releasePolicy->Get(mReleasePolicy);
+ }
}
-void NPatchVisual::InitializeRenderer()
+void NPatchVisual::DoSetOnScene(Actor& actor)
{
- Geometry geometry = CreateGeometry();
- Shader shader = CreateShader();
+ // load when first go on stage
+ LoadImages();
- if( !geometry || !shader )
+ const NPatchData* data;
+ if(mLoader.GetNPatchData(mId, data))
{
- DALI_LOG_ERROR("The 9 patch image '%s' doesn't have any valid stretch borders and so is not a valid 9 patch image\n", mImageUrl.c_str() );
- InitializeFromBrokenImage();
- }
+ Geometry geometry = CreateGeometry();
+ Shader shader = CreateShader();
- TextureSet textureSet = TextureSet::New();
- mImpl->mRenderer = Renderer::New( geometry, shader );
- mImpl->mRenderer.SetTextures( textureSet );
+ mImpl->mRenderer.SetGeometry(geometry);
+ mImpl->mRenderer.SetShader(shader);
- //Register transform properties
- mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
-}
+ mPlacementActor = actor;
+ if(data->GetLoadingState() != NPatchData::LoadingState::LOADING)
+ {
+ if(RenderingAddOn::Get().IsValid())
+ {
+ RenderingAddOn::Get().SubmitRenderTask(mImpl->mRenderer, data->GetRenderingMap());
+ }
+ ApplyTextureAndUniforms();
+ actor.AddRenderer(mImpl->mRenderer);
+ mPlacementActor.Reset();
-void NPatchVisual::DoSetOnStage( Actor& actor )
-{
- if( !mCroppedImage )
- {
- if( !mImageUrl.empty() )
- {
- NinePatchImage nPatch = NinePatchImage::New( mImageUrl );
- InitializeFromImage( nPatch );
- }
- else if( mImage )
- {
- InitializeFromImage( mImage );
+ // npatch loaded and ready to display
+ ResourceReady(Toolkit::Visual::ResourceStatus::READY);
}
}
+}
- //initialize the renderer after initializing from the image since we need to know the grid size from the image before creating the geometry
- InitializeRenderer();
-
- if( mCroppedImage )
+void NPatchVisual::DoSetOffScene(Actor& actor)
+{
+ if((mId != NPatchData::INVALID_NPATCH_DATA_ID) && mReleasePolicy == Toolkit::ImageVisual::ReleasePolicy::DETACHED)
{
- ApplyImageToSampler();
+ mLoader.Remove(mId, this);
+ mImpl->mResourceStatus = Toolkit::Visual::ResourceStatus::PREPARING;
+ mId = NPatchData::INVALID_NPATCH_DATA_ID;
}
- actor.AddRenderer( mImpl->mRenderer );
+ actor.RemoveRenderer(mImpl->mRenderer);
+ mPlacementActor.Reset();
}
-void NPatchVisual::DoSetOffStage( Actor& actor )
+void NPatchVisual::OnSetTransform()
{
- mCroppedImage.Reset();
- actor.RemoveRenderer( mImpl->mRenderer );
- mImpl->mRenderer.Reset();
+ if(mImpl->mRenderer)
+ {
+ mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
+ }
}
-void NPatchVisual::DoCreatePropertyMap( Property::Map& map ) const
+void NPatchVisual::DoCreatePropertyMap(Property::Map& map) const
{
map.Clear();
- map.Insert( Toolkit::VisualProperty::TYPE, Toolkit::Visual::IMAGE );
- if( !mImageUrl.empty() )
- {
- map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl );
- }
- else if( mImage )
+ bool sync = IsSynchronousLoadingRequired();
+ map.Insert(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, sync);
+ map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::N_PATCH);
+ map.Insert(Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl());
+ map.Insert(Toolkit::ImageVisual::Property::BORDER_ONLY, mBorderOnly);
+ map.Insert(Toolkit::ImageVisual::Property::BORDER, mBorder);
+ map.Insert(Toolkit::ImageVisual::Property::RELEASE_POLICY, mReleasePolicy);
+
+ if(mAuxiliaryUrl.IsValid())
{
- map.Insert( Toolkit::ImageVisual::Property::URL, mImage.GetUrl() );
+ map.Insert(Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE, mAuxiliaryUrl.GetUrl());
+ map.Insert(Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE_ALPHA, mAuxiliaryImageAlpha);
}
- map.Insert( Toolkit::ImageVisual::Property::BORDER_ONLY, mBorderOnly );
}
-void NPatchVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
+void NPatchVisual::DoCreateInstancePropertyMap(Property::Map& map) const
{
- // TODO
+ if(mAuxiliaryUrl.IsValid())
+ {
+ map.Insert(Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE, mAuxiliaryUrl.GetUrl());
+ map.Insert(Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE_ALPHA, mAuxiliaryImageAlpha);
+ }
}
-Dali::Property::Value NPatchVisual::DoGetProperty( Dali::Property::Index index )
+NPatchVisual::NPatchVisual(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory)
+: Visual::Base(factoryCache, Visual::FittingMode::FILL, Toolkit::Visual::N_PATCH),
+ mPlacementActor(),
+ mLoader(factoryCache.GetNPatchLoader()),
+ mImageVisualShaderFactory(shaderFactory),
+ mImageUrl(),
+ mAuxiliaryUrl(),
+ mId(NPatchData::INVALID_NPATCH_DATA_ID),
+ mBorderOnly(false),
+ mBorder(),
+ mAuxiliaryImageAlpha(0.0f),
+ mReleasePolicy(Toolkit::ImageVisual::ReleasePolicy::DETACHED)
{
- // TODO
- return Dali::Property::Value();
+ EnablePreMultipliedAlpha(mFactoryCache.GetPreMultiplyOnLoad());
}
-void NPatchVisual::OnSetTransform()
+NPatchVisual::~NPatchVisual()
{
- if( mImpl->mRenderer )
+ if(Stage::IsInstalled() && (mId != NPatchData::INVALID_NPATCH_DATA_ID) && (mReleasePolicy != Toolkit::ImageVisual::ReleasePolicy::NEVER))
{
- mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
+ mLoader.Remove(mId, this);
+ mId = NPatchData::INVALID_NPATCH_DATA_ID;
}
}
-
-void NPatchVisual::ChangeRenderer( bool oldBorderOnly, size_t oldGridX, size_t oldGridY )
+void NPatchVisual::OnInitialize()
{
- //check to see if the border style has changed
+ // Get basic geometry and shader
+ Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
+ Shader shader = mImageVisualShaderFactory.GetShader(
+ mFactoryCache,
+ ImageVisualShaderFeature::FeatureBuilder());
- bool borderOnlyChanged = oldBorderOnly != mBorderOnly;
- bool gridChanged = oldGridX != mStretchPixelsX.Size() || oldGridY != mStretchPixelsY.Size();
+ mImpl->mRenderer = VisualRenderer::New(geometry, shader);
+ mImpl->mRenderer.ReserveCustomProperties(CUSTOM_PROPERTY_COUNT);
- if( borderOnlyChanged || gridChanged )
+ //Register transform properties
+ mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
+}
+
+Geometry NPatchVisual::CreateGeometry()
+{
+ Geometry geometry;
+ const NPatchData* data;
+ if(mLoader.GetNPatchData(mId, data) && data->GetLoadingState() == NPatchData::LoadingState::LOAD_COMPLETE)
{
- Geometry geometry = CreateGeometry();
- if( geometry )
+ if(data->GetStretchPixelsX().Size() == 1 && data->GetStretchPixelsY().Size() == 1)
{
- mImpl->mRenderer.SetGeometry( geometry );
- }
- else
- {
- InitializeFromBrokenImage();
+ if(DALI_UNLIKELY(mBorderOnly))
+ {
+ geometry = GetNinePatchGeometry(VisualFactoryCache::NINE_PATCH_BORDER_GEOMETRY);
+ }
+ else
+ {
+ if(data->GetRenderingMap())
+ {
+ uint32_t elementCount[2];
+ geometry = RenderingAddOn::Get().CreateGeometryGrid(data->GetRenderingMap(), Uint16Pair(3, 3), elementCount);
+ if(mImpl->mRenderer)
+ {
+ RenderingAddOn::Get().SubmitRenderTask(mImpl->mRenderer, data->GetRenderingMap());
+ }
+ }
+ else
+ {
+ geometry = GetNinePatchGeometry(VisualFactoryCache::NINE_PATCH_GEOMETRY);
+ }
+ }
}
- }
-
- if( gridChanged )
- {
- Shader shader = CreateShader();
- TextureSet textureSet;
- if( shader )
+ else if(data->GetStretchPixelsX().Size() > 0 || data->GetStretchPixelsY().Size() > 0)
{
- textureSet = mImpl->mRenderer.GetTextures();
- if( !textureSet )
+ Uint16Pair gridSize(2 * data->GetStretchPixelsX().Size() + 1, 2 * data->GetStretchPixelsY().Size() + 1);
+ if(!data->GetRenderingMap())
+ {
+ geometry = !mBorderOnly ? NPatchHelper::CreateGridGeometry(gridSize) : NPatchHelper::CreateBorderGeometry(gridSize);
+ }
+ else
{
- InitializeFromBrokenImage();
+ uint32_t elementCount[2];
+ geometry = !mBorderOnly ? RenderingAddOn::Get().CreateGeometryGrid(data->GetRenderingMap(), gridSize, elementCount) : NPatchHelper::CreateBorderGeometry(gridSize);
+ if(mImpl->mRenderer)
+ {
+ RenderingAddOn::Get().SubmitRenderTask(mImpl->mRenderer, data->GetRenderingMap());
+ }
}
- mImpl->mRenderer.SetShader( shader );
}
}
-}
-
-void NPatchVisual::InitializeFromImage( NinePatchImage nPatch )
-{
- mCroppedImage = nPatch.CreateCroppedBufferImage();
- if( !mCroppedImage )
+ else
{
- DALI_LOG_ERROR("'%s' specify a valid 9 patch image\n", mImageUrl.c_str() );
- InitializeFromBrokenImage();
- return;
+ // no N patch data so use default geometry
+ geometry = GetNinePatchGeometry(VisualFactoryCache::NINE_PATCH_GEOMETRY);
}
-
- mImageSize = ImageDimensions( mCroppedImage.GetWidth(), mCroppedImage.GetHeight() );
-
- mStretchPixelsX = nPatch.GetStretchPixelsX();
- mStretchPixelsY = nPatch.GetStretchPixelsY();
+ return geometry;
}
-void NPatchVisual::InitializeFromBrokenImage()
+Shader NPatchVisual::CreateShader()
{
- mCroppedImage = VisualFactoryCache::GetBrokenVisualImage();
- mImageSize = ImageDimensions( mCroppedImage.GetWidth(), mCroppedImage.GetHeight() );
+ Shader shader;
+ const NPatchData* data;
+ // 0 is either no data (load failed?) or no stretch regions on image
+ // for both cases we use the default shader
+ NPatchUtility::StretchRanges::SizeType xStretchCount = 0;
+ NPatchUtility::StretchRanges::SizeType yStretchCount = 0;
+
+ auto fragmentShader = mAuxiliaryPixelBuffer ? SHADER_NPATCH_VISUAL_MASK_SHADER_FRAG
+ : SHADER_NPATCH_VISUAL_SHADER_FRAG;
+ auto shaderType = mAuxiliaryPixelBuffer ? VisualFactoryCache::NINE_PATCH_MASK_SHADER
+ : VisualFactoryCache::NINE_PATCH_SHADER;
+
+ // ask loader for the regions
+ if(mLoader.GetNPatchData(mId, data))
+ {
+ xStretchCount = data->GetStretchPixelsX().Count();
+ yStretchCount = data->GetStretchPixelsY().Count();
+ }
- mStretchPixelsX.Clear();
- mStretchPixelsX.PushBack( Uint16Pair( 0, mImageSize.GetWidth() ) );
- mStretchPixelsY.Clear();
- mStretchPixelsY.PushBack( Uint16Pair( 0, mImageSize.GetHeight() ) );
-}
+ if(DALI_LIKELY(!mImpl->mCustomShader))
+ {
+ if(DALI_LIKELY((xStretchCount == 1 && yStretchCount == 1) ||
+ (xStretchCount == 0 && yStretchCount == 0)))
+ {
+ shader = mFactoryCache.GetShader(shaderType);
+ if(DALI_UNLIKELY(!shader))
+ {
+ shader = Shader::New(SHADER_NPATCH_VISUAL_3X3_SHADER_VERT, fragmentShader);
+ // Only cache vanilla 9 patch shaders
+ mFactoryCache.SaveShader(shaderType, shader);
+ }
+ }
+ else if(xStretchCount > 0 || yStretchCount > 0)
+ {
+ std::stringstream vertexShader;
+ vertexShader << "#define FACTOR_SIZE_X " << xStretchCount + 2 << "\n"
+ << "#define FACTOR_SIZE_Y " << yStretchCount + 2 << "\n"
+ << SHADER_NPATCH_VISUAL_SHADER_VERT;
-void NPatchVisual::ApplyImageToSampler()
-{
- TextureSet textureSet = mImpl->mRenderer.GetTextures();
- if( textureSet )
+ shader = Shader::New(vertexShader.str(), fragmentShader);
+ }
+ }
+ else
{
- TextureSetImage( textureSet, 0u, mCroppedImage );
+ Dali::Shader::Hint::Value hints = Dali::Shader::Hint::NONE;
- if( mStretchPixelsX.Size() == 1 && mStretchPixelsY.Size() == 1 )
+ if(!mImpl->mCustomShader->mFragmentShader.empty())
{
- //special case for 9 patch
- Uint16Pair stretchX = mStretchPixelsX[ 0 ];
- Uint16Pair stretchY = mStretchPixelsY[ 0 ];
+ fragmentShader = mImpl->mCustomShader->mFragmentShader.c_str();
+ }
+ hints = mImpl->mCustomShader->mHints;
- uint16_t stretchWidth = stretchX.GetY() - stretchX.GetX();
- uint16_t stretchHeight = stretchY.GetY() - stretchY.GetX();
+ /* Apply Custom Vertex Shader only if image is 9-patch */
+ if((xStretchCount == 1 && yStretchCount == 1) ||
+ (xStretchCount == 0 && yStretchCount == 0))
+ {
+ const char* vertexShader = SHADER_NPATCH_VISUAL_3X3_SHADER_VERT.data();
- mImpl->mRenderer.RegisterProperty( "uFixed[0]", Vector2::ZERO );
- mImpl->mRenderer.RegisterProperty( "uFixed[1]", Vector2( stretchX.GetX(), stretchY.GetX()) );
- mImpl->mRenderer.RegisterProperty( "uFixed[2]", Vector2( mImageSize.GetWidth() - stretchWidth, mImageSize.GetHeight() - stretchHeight ) );
- mImpl->mRenderer.RegisterProperty( "uStretchTotal", Vector2( stretchWidth, stretchHeight ) );
+ if(!mImpl->mCustomShader->mVertexShader.empty())
+ {
+ vertexShader = mImpl->mCustomShader->mVertexShader.c_str();
+ }
+ shader = Shader::New(vertexShader, fragmentShader, hints);
}
- else
+ else if(xStretchCount > 0 || yStretchCount > 0)
{
- mImpl->mRenderer.RegisterProperty( "uNinePatchFactorsX[0]", Vector2::ZERO );
- mImpl->mRenderer.RegisterProperty( "uNinePatchFactorsY[0]", Vector2::ZERO );
+ std::stringstream vertexShader;
+ vertexShader << "#define FACTOR_SIZE_X " << xStretchCount + 2 << "\n"
+ << "#define FACTOR_SIZE_Y " << yStretchCount + 2 << "\n"
+ << SHADER_NPATCH_VISUAL_SHADER_VERT;
- RegisterStretchProperties( mImpl->mRenderer, "uNinePatchFactorsX", mStretchPixelsX, mImageSize.GetWidth() );
- RegisterStretchProperties( mImpl->mRenderer, "uNinePatchFactorsY", mStretchPixelsY, mImageSize.GetHeight() );
+ shader = Shader::New(vertexShader.str(), fragmentShader, hints);
}
}
+
+ return shader;
}
-Geometry NPatchVisual::CreateGeometry( Uint16Pair gridSize )
+void NPatchVisual::ApplyTextureAndUniforms()
{
- uint16_t gridWidth = gridSize.GetWidth();
- uint16_t gridHeight = gridSize.GetHeight();
-
- // Create vertices
- Vector< Vector2 > vertices;
- vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
+ const NPatchData* data;
+ TextureSet textureSet;
- for( int y = 0; y < gridHeight + 1; ++y )
+ if(mLoader.GetNPatchData(mId, data) && data->GetLoadingState() == NPatchData::LoadingState::LOAD_COMPLETE)
+ {
+ textureSet = data->GetTextures();
+ NPatchHelper::ApplyTextureAndUniforms(mImpl->mRenderer, data);
+ }
+ else
{
- for( int x = 0; x < gridWidth + 1; ++x )
+ DALI_LOG_ERROR("The N patch image '%s' is not a valid N patch image\n", mImageUrl.GetUrl().c_str());
+ textureSet = TextureSet::New();
+
+ Actor actor = mPlacementActor.GetHandle();
+ Vector2 imageSize = Vector2::ZERO;
+ if(actor)
{
- AddVertex( vertices, x, y );
+ imageSize = actor.GetProperty(Actor::Property::SIZE).Get<Vector2>();
}
+ mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize);
}
- // Create indices
- //TODO: compare performance with triangle strip when Geometry supports it
- Vector< unsigned short > indices;
- indices.Reserve( gridWidth * gridHeight * 6 );
-
- unsigned int rowIdx = 0;
- unsigned int nextRowIdx = gridWidth + 1;
- for( int y = 0; y < gridHeight; ++y, ++nextRowIdx, ++rowIdx )
+ if(mAuxiliaryPixelBuffer)
{
- for( int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx )
+ // If the auxiliary image is smaller than the un-stretched NPatch, use CPU resizing to enlarge it to the
+ // same size as the unstretched NPatch. This will give slightly higher quality results than just relying
+ // on GL interpolation alone.
+ if(mAuxiliaryPixelBuffer.GetWidth() < data->GetCroppedWidth() &&
+ mAuxiliaryPixelBuffer.GetHeight() < data->GetCroppedHeight())
{
- AddQuadIndices( indices, rowIdx, nextRowIdx );
+ mAuxiliaryPixelBuffer.Resize(data->GetCroppedWidth(), data->GetCroppedHeight());
}
+
+ // Note, this resets mAuxiliaryPixelBuffer handle
+ auto auxiliaryPixelData = Devel::PixelBuffer::Convert(mAuxiliaryPixelBuffer);
+
+ auto texture = Texture::New(TextureType::TEXTURE_2D,
+ auxiliaryPixelData.GetPixelFormat(),
+ auxiliaryPixelData.GetWidth(),
+ auxiliaryPixelData.GetHeight());
+ texture.Upload(auxiliaryPixelData);
+ textureSet.SetTexture(1, texture);
+ mImpl->mRenderer.RegisterProperty(DevelImageVisual::Property::AUXILIARY_IMAGE_ALPHA,
+ AUXILIARY_IMAGE_ALPHA_NAME,
+ mAuxiliaryImageAlpha);
}
+ mImpl->mRenderer.SetTextures(textureSet);
- return GenerateGeometry( vertices, indices );
+ // Register transform properties
+ mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
}
-Geometry NPatchVisual::CreateGeometryBorder( Uint16Pair gridSize )
+Geometry NPatchVisual::GetNinePatchGeometry(VisualFactoryCache::GeometryType subType)
{
- uint16_t gridWidth = gridSize.GetWidth();
- uint16_t gridHeight = gridSize.GetHeight();
-
- // Create vertices
- Vector< Vector2 > vertices;
- vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
-
- //top
- int y = 0;
- for(; y < 2; ++y)
+ Geometry geometry = mFactoryCache.GetGeometry(subType);
+ if(!geometry)
{
- for( int x = 0; x < gridWidth + 1; ++x )
+ if(DALI_LIKELY(VisualFactoryCache::NINE_PATCH_GEOMETRY == subType))
{
- AddVertex( vertices, x, y );
+ geometry = NPatchHelper::CreateGridGeometry(Uint16Pair(3, 3));
}
+ else if(VisualFactoryCache::NINE_PATCH_BORDER_GEOMETRY == subType)
+ {
+ geometry = NPatchHelper::CreateBorderGeometry(Uint16Pair(3, 3));
+ }
+ mFactoryCache.SaveGeometry(subType, geometry);
}
+ return geometry;
+}
- for(; y < gridHeight - 1; ++y)
+void NPatchVisual::SetResource()
+{
+ const NPatchData* data;
+ if(mImpl->mRenderer && mLoader.GetNPatchData(mId, data))
{
- //left
- AddVertex( vertices, 0, y );
- AddVertex( vertices, 1, y );
+ Geometry geometry = CreateGeometry();
+ Shader shader = CreateShader();
- //right
- AddVertex( vertices, gridWidth - 1, y );
- AddVertex( vertices, gridWidth, y );
- }
+ mImpl->mRenderer.SetGeometry(geometry);
+ mImpl->mRenderer.SetShader(shader);
- //bottom
- for(; y < gridHeight + 1; ++y)
- {
- for( int x = 0; x < gridWidth + 1; ++x )
+ Actor actor = mPlacementActor.GetHandle();
+ if(actor)
{
- AddVertex( vertices, x, y );
- }
- }
-
- // Create indices
- //TODO: compare performance with triangle strip when Geometry supports it
- Vector< unsigned short > indices;
- indices.Reserve( gridWidth * gridHeight * 6 );
+ ApplyTextureAndUniforms();
+ actor.AddRenderer(mImpl->mRenderer);
+ mPlacementActor.Reset();
- //top
- unsigned int rowIdx = 0 ;
- unsigned int nextRowIdx = gridWidth + 1;
- for( int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx )
- {
- AddQuadIndices( indices, rowIdx, nextRowIdx );
+ // npatch loaded and ready to display
+ ResourceReady(Toolkit::Visual::ResourceStatus::READY);
+ }
}
+}
- if(gridHeight > 2)
+void NPatchVisual::LoadComplete(bool loadSuccess, TextureInformation textureInformation)
+{
+ if(textureInformation.returnType == TextureUploadObserver::ReturnType::TEXTURE)
{
- rowIdx = gridWidth + 1;
- nextRowIdx = ( gridWidth + 1 ) * 2;
-
- unsigned increment = gridWidth - 1;
- if(gridHeight > 3)
+ EnablePreMultipliedAlpha(textureInformation.preMultiplied);
+ if(!loadSuccess)
{
- increment = 2;
- //second row left
- AddQuadIndices( indices, rowIdx, nextRowIdx );
-
- rowIdx = gridWidth * 2;
- nextRowIdx = ( gridWidth + 1 ) * 2 + 2;
- //second row right
- AddQuadIndices( indices, rowIdx, nextRowIdx );
-
- //left and right
- rowIdx = nextRowIdx - 2;
- nextRowIdx = rowIdx + 4;
- for(int y = 2; y < 2*(gridHeight - 3); ++y, rowIdx += 2, nextRowIdx += 2)
- {
- AddQuadIndices( indices, rowIdx, nextRowIdx );
- }
+ // Image loaded and ready to display
+ ResourceReady(Toolkit::Visual::ResourceStatus::FAILED);
}
- //second row left
- AddQuadIndices( indices, rowIdx, nextRowIdx );
-
- rowIdx += increment;
- nextRowIdx += gridWidth - 1;
- //second row right
- AddQuadIndices( indices, rowIdx, nextRowIdx );
+ if(mAuxiliaryPixelBuffer || !mAuxiliaryUrl.IsValid())
+ {
+ SetResource();
+ }
}
-
- //bottom
- rowIdx = nextRowIdx - gridWidth + 1;
- nextRowIdx = rowIdx + gridWidth + 1;
- for( int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx )
+ else // for the ReturnType::PIXEL_BUFFER
{
- AddQuadIndices( indices, rowIdx, nextRowIdx );
+ if(loadSuccess && textureInformation.url == mAuxiliaryUrl.GetUrl())
+ {
+ mAuxiliaryPixelBuffer = textureInformation.pixelBuffer;
+ SetResource();
+ }
+ else
+ {
+ // Image loaded and ready to display
+ ResourceReady(Toolkit::Visual::ResourceStatus::FAILED);
+ }
}
-
- return GenerateGeometry( vertices, indices );
}
} // namespace Internal