imageVisual.GetNaturalSize(naturalSize);
DALI_TEST_EQUALS( naturalSize, Vector2(100.f, 200.f), TEST_LOCATION );
- // n patch visual
- TestPlatformAbstraction& platform = application.GetPlatform();
- Vector2 testSize(80.f, 160.f);
- platform.SetClosestImageSize(testSize);
- image = ResourceImage::New(TEST_NPATCH_FILE_NAME);
- Visual::Base nPatchVisual = factory.CreateVisual( image );
- nPatchVisual.SetSize( visualSize );
- DALI_TEST_EQUALS( nPatchVisual.GetSize(), visualSize, TEST_LOCATION );
- nPatchVisual.GetNaturalSize(naturalSize);
- DALI_TEST_EQUALS( naturalSize, testSize, TEST_LOCATION );
+ // n patch visual is tested in the utc-Dali-VisualFactory.cpp
// border visual
float borderSize = 5.f;
DALI_TEST_EQUALS( naturalSize, Vector2(100.f, 100.f), TEST_LOCATION ); // Natural size should still be 100, 100
// Batch Image visual
+ TestPlatformAbstraction& platform = application.GetPlatform();
+ Vector2 testSize(80.f, 160.f);
+ platform.SetClosestImageSize(testSize);
propertyMap.Clear();
propertyMap.Insert( Visual::Property::TYPE, Visual::IMAGE );
propertyMap.Insert( ImageVisual::Property::URL, TEST_IMAGE_FILE_NAME );
DALI_TEST_CHECK( value );
DALI_TEST_CHECK( value->Get<bool>() == true );
- // Test the properties..
- Property::Value imageValue = imageVisual.GetProperty( ImageVisual::Property::URL );
- DALI_TEST_CHECK( imageValue.Get<std::string>() == TEST_IMAGE_FILE_NAME );
-
- // Other Properties for Image Visual can be tested here once implemented in the Visual
-
- tet_infoline( "UtcDaliVisualGetPropertyMap5 Ensuring Property without a Getter is not problematic" );
- imageValue = imageVisual.GetProperty( ImageVisual::Property::DESIRED_HEIGHT );
- DALI_TEST_CHECK( imageValue.Get<int>() != 30 );
-
-
- // Rest of test uses Image not an url ( legacy )
-
// Get an image visual with an image handle, and test the default property values
Image image = ResourceImage::New(TEST_IMAGE_FILE_NAME, ImageDimensions(100, 200));
imageVisual = factory.CreateVisual(image);
ToolkitTestApplication application;
tet_infoline( "UtcDaliVisualGetPropertyMap6: NPatchVisual" );
- bool BORDER_ONLY_SETTING = true;
-
VisualFactory factory = VisualFactory::Get();
Property::Map propertyMap;
propertyMap.Insert( Visual::Property::TYPE, Visual::IMAGE );
propertyMap.Insert( ImageVisual::Property::URL, TEST_NPATCH_FILE_NAME );
- propertyMap.Insert( ImageVisual::Property::BORDER_ONLY, BORDER_ONLY_SETTING );
+ propertyMap.Insert( ImageVisual::Property::BORDER_ONLY, true );
Visual::Base nPatchVisual = factory.CreateVisual( propertyMap );
Property::Map resultMap;
DALI_TEST_CHECK( value );
DALI_TEST_CHECK( value->Get<bool>() );
- // Test the properties via Index.
- nPatchVisual.SetProperty( ImageVisual::Property::URL, TEST_NPATCH_FILE_NAME ); // May not able to change URL in future using SetProperty
- Property::Value nPatchValue = nPatchVisual.GetProperty( ImageVisual::Property::URL );
- DALI_TEST_CHECK( nPatchValue.Get<std::string>() == TEST_NPATCH_FILE_NAME );
-
- nPatchValue = nPatchVisual.GetProperty( ImageVisual::Property::BORDER_ONLY );
- DALI_TEST_CHECK( nPatchValue.Get<bool>() == BORDER_ONLY_SETTING );
-
- // Other Properties for N-Patch Visual can be tested here once implemented in the Visual
-
- tet_infoline( "UtcDaliVisualGetPropertyMap6 Ensuring Property without a Getter is not problematic" );
- nPatchValue = nPatchVisual.GetProperty( ImageVisual::Property::DESIRED_HEIGHT );
- DALI_TEST_CHECK( nPatchValue.Get<int>() != 30 );
END_TEST;
}
END_TEST;
}
+int UtcDaliNPatchVisualCustomShader(void)
+{
+ ToolkitTestApplication application;
+ tet_infoline( "NPatchVisual with custom shader" );
+
+ VisualFactory factory = VisualFactory::Get();
+ Property::Map properties;
+ Property::Map shader;
+ const std::string fragmentShader = "Foobar";
+ shader[Dali::Toolkit::Visual::Shader::Property::FRAGMENT_SHADER] = fragmentShader;
+ properties[Dali::Toolkit::Visual::Property::TYPE] = Dali::Toolkit::Visual::IMAGE;
+ properties[Dali::Toolkit::Visual::Property::SHADER]=shader;
+ properties[Dali::Toolkit::ImageVisual::Property::URL] = TEST_NPATCH_FILE_NAME;
+
+ Visual::Base visual = factory.CreateVisual( properties );
+ Actor dummy = Actor::New();
+ // trigger creation through setting on stage
+ visual.SetOnStage( dummy );
+
+ Renderer renderer = dummy.GetRendererAt( 0 );
+ Shader shader2 = renderer.GetShader();
+ Property::Value value = shader2.GetProperty( Shader::Property::PROGRAM );
+ Property::Map* map = value.GetMap();
+ DALI_TEST_CHECK( map );
+
+ Property::Value* fragment = map->Find( "fragment" ); // fragment key name from shader-impl.cpp
+ // *map["vertex"]; is default here so not verifying it
+
+ DALI_TEST_EQUALS( fragmentShader, fragment->Get<std::string>(), TEST_LOCATION );
+
+ END_TEST;
+}
+
DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION );
+
+ ResourceImage image = ResourceImage::New(TEST_NPATCH_FILE_NAME);
+ Visual::Base nPatchVisual = factory.CreateVisual( image );
+ Vector2 visualSize( 20.f, 30.f ), naturalSize(0,0);
+ nPatchVisual.SetSize( visualSize );
+ DALI_TEST_EQUALS( nPatchVisual.GetSize(), visualSize, TEST_LOCATION );
+ nPatchVisual.GetNaturalSize( naturalSize );
+ DALI_TEST_EQUALS( naturalSize, Vector2( ninePatchImageWidth-2, ninePatchImageHeight-2 ), TEST_LOCATION );
+
END_TEST;
}
return GetImplementation( *this ).GetHeightForWidth( width );
}
-void Visual::Base::GetNaturalSize(Vector2& naturalSize ) const
+void Visual::Base::GetNaturalSize(Vector2& naturalSize )
{
GetImplementation( *this ).GetNaturalSize( naturalSize );
}
*
* @param[out] naturalSize The visual's natural size
*/
- void GetNaturalSize( Vector2& naturalSize ) const;
+ void GetNaturalSize( Vector2& naturalSize );
/**
* @brief Set the depth index of this visual.
$(toolkit_src_dir)/visuals/visual-base-impl.cpp \
$(toolkit_src_dir)/visuals/visual-base-data-impl.cpp \
$(toolkit_src_dir)/visuals/image-atlas-manager.cpp \
+ $(toolkit_src_dir)/visuals/npatch-loader.cpp \
$(toolkit_src_dir)/visuals/visual-factory-cache.cpp \
$(toolkit_src_dir)/visuals/visual-factory-impl.cpp \
$(toolkit_src_dir)/visuals/visual-string-constants.cpp \
Visual::Base::SetSize( size );
}
-void BatchImageVisual::GetNaturalSize( Vector2& naturalSize ) const
+void BatchImageVisual::GetNaturalSize( Vector2& naturalSize )
{
if( mDesiredSize.GetWidth() > 0 && mDesiredSize.GetHeight() > 0 )
{
/**
* @copydoc Visual::Base::GetNaturalSize
*/
- virtual void GetNaturalSize( Vector2& naturalSize ) const;
+ virtual void GetNaturalSize( Vector2& naturalSize );
/**
* @copydoc Visual::Base::CreatePropertyMap
}
}
-void ImageVisual::GetNaturalSize( Vector2& naturalSize ) const
+void ImageVisual::GetNaturalSize( Vector2& naturalSize )
{
if(mImage)
{
void ImageVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
{
- // This is where specific Properties can be set.
+ // TODO
}
Dali::Property::Value ImageVisual::DoGetProperty( Dali::Property::Index index )
{
- Dali::Property::Value value;
-
- switch( index )
- {
- case Toolkit::ImageVisual::Property::URL:
- {
- value = mImageUrl;
- break;
- }
- default:
- {
- break;
- }
- }
-
- return value;
+ // TODO
+ return Dali::Property::Value();
}
void ImageVisual::OnSetTransform()
/**
* @copydoc Visual::Base::GetNaturalSize
*/
- virtual void GetNaturalSize( Vector2& naturalSize ) const;
+ virtual void GetNaturalSize( Vector2& naturalSize );
/**
* @copydoc Visual::Base::CreatePropertyMap
--- /dev/null
+ /*
+ * Copyright (c) 2016 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+// CLASS HEADER
+#include <dali-toolkit/internal/visuals/npatch-loader.h>
+
+// EXTERNAL HEADER
+#include <dali/devel-api/common/hash.h>
+#include <dali/devel-api/images/texture-set-image.h>
+
+namespace Dali
+{
+
+namespace Toolkit
+{
+
+namespace Internal
+{
+
+NPatchLoader::NPatchLoader()
+{
+}
+
+NPatchLoader::~NPatchLoader()
+{
+}
+
+std::size_t NPatchLoader::Load( const std::string& url )
+{
+ std::size_t hash = CalculateHash( url );
+ OwnerContainer< Data* >::SizeType index = UNINITIALIZED_ID;
+ const OwnerContainer< Data* >::SizeType count = mCache.Count();
+ for( ; index < count; ++index )
+ {
+ if( mCache[ index ]->hash == hash )
+ {
+ // hash match, check url as well in case of hash collision
+ if( mCache[ index ]->url == url )
+ {
+ return index+1u; // valid indices are from 1 onwards
+ }
+ }
+ }
+ // got to the end so no match, decode N patch and append new item to cache
+ NinePatchImage ninePatch = NinePatchImage::New( url );
+ if( ninePatch )
+ {
+ Data* data = new Data();
+ data->hash = hash;
+ data->url = url;
+ BufferImage croppedImage = ninePatch.CreateCroppedBufferImage();
+ if( croppedImage )
+ {
+ data->textureSet = TextureSet::New();
+ TextureSetImage( data->textureSet, 0u, croppedImage );
+ data->croppedWidth = croppedImage.GetWidth();
+ data->croppedHeight = croppedImage.GetHeight();
+ data->stretchPixelsX = ninePatch.GetStretchPixelsX();
+ data->stretchPixelsY = ninePatch.GetStretchPixelsY();
+ mCache.PushBack( data );
+
+ return mCache.Count(); // valid ids start from 1u
+ }
+ }
+ return 0u;
+}
+
+bool NPatchLoader::GetNPatchData( std::size_t id, const Data*& data )
+{
+ if( ( id > UNINITIALIZED_ID )&&( id <= mCache.Count() ) )
+ {
+ data = mCache[ id - 1u ]; // id's start from 1u
+ return true;
+ }
+ data = NULL;
+ return false;
+}
+
+} // namespace Internal
+
+} // namespace Toolkit
+
+} // namespace Dali
--- /dev/null
+#ifndef DALI_TOOLKIT_NPATCH_LOADER_H
+#define DALI_TOOLKIT_NPATCH_LOADER_H
+
+/*
+ * Copyright (c) 2016 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// EXTERNAL INCLUDES
+#include <string>
+#include <dali/public-api/rendering/texture-set.h>
+#include <dali/devel-api/common/owner-container.h>
+#include <dali/devel-api/images/nine-patch-image.h>
+
+// INTERNAL INCLUDES
+#include <dali-toolkit/devel-api/image-loader/image-atlas.h>
+
+namespace Dali
+{
+
+namespace Toolkit
+{
+
+namespace Internal
+{
+
+/**
+ * The manager for loading Npatch textures.
+ * It caches them internally for better performance; i.e. to avoid loading and
+ * parsing the files over and over.
+ *
+ * Cache is not cleaned during app lifecycle as N patches take considerably
+ * small space and there's not usually a lot of them. Usually N patches are specified in
+ * toolkit default style and there is 1-2 per control that are shared across the whole application.
+ */
+class NPatchLoader
+{
+public:
+
+ enum
+ {
+ UNINITIALIZED_ID = 0 ///< uninitialised id, use to initialize ids
+ };
+
+ struct Data
+ {
+ std::string url; ///< Url of the N-Patch
+ TextureSet textureSet; ///< Texture containing the cropped image
+ NinePatchImage::StretchRanges stretchPixelsX; ///< X stretch pixels
+ NinePatchImage::StretchRanges stretchPixelsY; ///< Y stretch pixels
+ std::size_t hash; ///< Hash code for the Url
+ uint32_t croppedWidth; ///< Width of the cropped middle part of N-patch
+ uint32_t croppedHeight; ///< Height of the cropped middle part of N-patch
+ };
+
+public:
+
+ /**
+ * Constructor
+ */
+ NPatchLoader();
+
+ /**
+ * Destructor, non-virtual as not a base class
+ */
+ ~NPatchLoader();
+
+ /**
+ * @brief Retrieve a texture matching the n-patch url.
+ *
+ * @param [in] url to retrieve
+ * @return id of the texture.
+ */
+ std::size_t Load( const std::string& url );
+
+ /**
+ * @brief Retrieve N patch data matching to an id
+ * @param [in] id of data
+ * @param [out] data const pointer to the data
+ * @return true if data matching to id was really found
+ */
+ bool GetNPatchData( std::size_t id, const Data*& data );
+
+protected:
+
+ /**
+ * Undefined copy constructor.
+ */
+ NPatchLoader(const NPatchLoader&);
+
+ /**
+ * Undefined assignment operator.
+ */
+ NPatchLoader& operator=(const NPatchLoader& rhs);
+
+private:
+
+ OwnerContainer< Data* > mCache;
+
+};
+
+} // name Internal
+
+} // namespace Toolkit
+
+} // namespace Dali
+
+#endif // DALI_TOOLKIT_NPATCH_LOADER_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/npatch-loader.h>
#include <dali-toolkit/internal/visuals/visual-factory-impl.h>
#include <dali-toolkit/internal/visuals/visual-factory-cache.h>
#include <dali-toolkit/internal/visuals/visual-string-constants.h>
NPatchVisual* nPatchVisual = new NPatchVisual( factoryCache );
nPatchVisual->mImageUrl = imageUrl;
- NinePatchImage image = NinePatchImage::New( imageUrl );
- nPatchVisual->InitializeFromImage( image );
-
return nPatchVisual;
}
NPatchVisualPtr NPatchVisual::New( VisualFactoryCache& factoryCache, NinePatchImage image )
{
NPatchVisual* nPatchVisual = new NPatchVisual( factoryCache );
- nPatchVisual->mImage = image;
-
- nPatchVisual->InitializeFromImage( image );
+ nPatchVisual->mImageUrl = image.GetUrl();
return nPatchVisual;
}
-NPatchVisual::NPatchVisual( VisualFactoryCache& factoryCache )
-: Visual::Base( factoryCache ),
- mImage(),
- mCroppedImage(),
- mImageUrl(),
- mStretchPixelsX(),
- mStretchPixelsY(),
- mImageSize(),
- mBorderOnly( false )
-{
-}
-
-NPatchVisual::~NPatchVisual()
+void NPatchVisual::GetNaturalSize( Vector2& naturalSize )
{
+ naturalSize.x = 0u;
+ naturalSize.y = 0u;
+ // load now if not already loaded
+ if( NPatchLoader::UNINITIALIZED_ID == mId )
+ {
+ mId = mLoader.Load( mImageUrl );
+ }
+ const NPatchLoader::Data* data;
+ if( mLoader.GetNPatchData( mId, data ) )
+ {
+ naturalSize.x = data->croppedWidth;
+ naturalSize.y = data->croppedHeight;
+ }
}
void NPatchVisual::DoSetProperties( const Property::Map& propertyMap )
{
// URL is already passed in via constructor
- //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 )
{
}
}
-void NPatchVisual::GetNaturalSize( Vector2& naturalSize ) const
+void NPatchVisual::DoSetOnStage( Actor& actor )
{
- if( mImage )
- {
- naturalSize.x = mImage.GetWidth();
- naturalSize.y = mImage.GetHeight();
- }
- else if( !mImageUrl.empty() )
+ // load when first go on stage
+ if( NPatchLoader::UNINITIALIZED_ID == mId )
{
- ImageDimensions dimentions = ResourceImage::GetImageSize( mImageUrl );
- naturalSize.x = dimentions.GetWidth();
- naturalSize.y = dimentions.GetHeight();
+ mId = mLoader.Load( mImageUrl );
}
- else
+
+ Geometry geometry = CreateGeometry();
+ Shader shader = CreateShader();
+ mImpl->mRenderer = Renderer::New( geometry, shader );
+
+ ApplyTextureAndUniforms();
+
+ actor.AddRenderer( mImpl->mRenderer );
+}
+
+void NPatchVisual::DoSetOffStage( Actor& actor )
+{
+ actor.RemoveRenderer( mImpl->mRenderer );
+ mImpl->mRenderer.Reset();
+}
+
+void NPatchVisual::OnSetTransform()
+{
+ if( mImpl->mRenderer )
{
- naturalSize = Vector2::ZERO;
+ mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
}
}
+void NPatchVisual::DoCreatePropertyMap( Property::Map& map ) const
+{
+ map.Clear();
+ map.Insert( Toolkit::VisualProperty::TYPE, Toolkit::Visual::IMAGE );
+ map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl );
+ map.Insert( Toolkit::ImageVisual::Property::BORDER_ONLY, mBorderOnly );
+}
+
+void NPatchVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
+{
+ // TODO
+}
+
+Dali::Property::Value NPatchVisual::DoGetProperty( Dali::Property::Index index )
+{
+ // TODO
+ return Dali::Property::Value();
+}
+
+NPatchVisual::NPatchVisual( VisualFactoryCache& factoryCache )
+: Visual::Base( factoryCache ),
+ mLoader( factoryCache.GetNPatchLoader() ),
+ mImageUrl(),
+ mId( NPatchLoader::UNINITIALIZED_ID ),
+ mBorderOnly( false )
+{
+}
+
+NPatchVisual::~NPatchVisual()
+{
+}
+
Geometry NPatchVisual::CreateGeometry()
{
Geometry geometry;
- if( mStretchPixelsX.Size() == 1 && mStretchPixelsY.Size() == 1 )
+ const NPatchLoader::Data* data;
+ if( mLoader.GetNPatchData( mId, data ) )
{
- if( !mBorderOnly )
+ if( data->stretchPixelsX.Size() == 1 && data->stretchPixelsY.Size() == 1 )
{
- geometry = mFactoryCache.GetGeometry( VisualFactoryCache::NINE_PATCH_GEOMETRY );
- if( !geometry )
+ if( DALI_UNLIKELY( mBorderOnly ) )
{
- geometry = CreateGeometry( Uint16Pair( 3, 3 ) );
- mFactoryCache.SaveGeometry( VisualFactoryCache::NINE_PATCH_GEOMETRY, geometry );
+ geometry = GetNinePatchGeometry( VisualFactoryCache::NINE_PATCH_BORDER_GEOMETRY );
}
- }
- else
- {
- geometry = mFactoryCache.GetGeometry( VisualFactoryCache::NINE_PATCH_BORDER_GEOMETRY );
- if( !geometry )
+ else
{
- geometry = CreateGeometryBorder( Uint16Pair( 3, 3 ) );
- mFactoryCache.SaveGeometry( VisualFactoryCache::NINE_PATCH_BORDER_GEOMETRY, geometry );
+ geometry = GetNinePatchGeometry( VisualFactoryCache::NINE_PATCH_GEOMETRY );
}
}
+ else if( data->stretchPixelsX.Size() > 0 || data->stretchPixelsY.Size() > 0)
+ {
+ Uint16Pair gridSize( 2 * data->stretchPixelsX.Size() + 1, 2 * data->stretchPixelsY.Size() + 1 );
+ geometry = !mBorderOnly ? CreateGridGeometry( gridSize ) : CreateBorderGeometry( gridSize );
+ }
}
- else if( mStretchPixelsX.Size() > 0 || mStretchPixelsY.Size() > 0)
+ else
{
- Uint16Pair gridSize( 2 * mStretchPixelsX.Size() + 1, 2 * mStretchPixelsY.Size() + 1 );
- geometry = !mBorderOnly ? CreateGeometry( gridSize ) : CreateGeometryBorder( gridSize );
+ // no N patch data so use default geometry
+ geometry = GetNinePatchGeometry( VisualFactoryCache::NINE_PATCH_GEOMETRY );
}
-
return geometry;
}
Shader NPatchVisual::CreateShader()
{
Shader shader;
- if( !mImpl->mCustomShader )
+ const NPatchLoader::Data* data;
+ // 0 is either no data (load failed?) or no stretch regions on image
+ // for both cases we use the default shader
+ NinePatchImage::StretchRanges::SizeType xStretchCount = 0;
+ NinePatchImage::StretchRanges::SizeType yStretchCount = 0;
+
+ // ask loader for the regions
+ if( mLoader.GetNPatchData( mId, data ) )
{
- if( mStretchPixelsX.Size() == 1 && mStretchPixelsY.Size() == 1 )
+ xStretchCount = data->stretchPixelsX.Count();
+ yStretchCount = data->stretchPixelsY.Count();
+ }
+
+ if( DALI_LIKELY( !mImpl->mCustomShader ) )
+ {
+ if( DALI_LIKELY( ( xStretchCount == 1 && yStretchCount == 1 ) ||
+ ( xStretchCount == 0 && yStretchCount == 0 ) ) )
{
shader = mFactoryCache.GetShader( VisualFactoryCache::NINE_PATCH_SHADER );
- if( !shader )
+ if( DALI_UNLIKELY( !shader ) )
{
shader = Shader::New( VERTEX_SHADER_3X3, FRAGMENT_SHADER );
mFactoryCache.SaveShader( VisualFactoryCache::NINE_PATCH_SHADER, shader );
}
}
- else if( mStretchPixelsX.Size() > 0 || mStretchPixelsY.Size() > 0)
+ else if( xStretchCount > 0 || yStretchCount > 0)
{
std::stringstream vertexShader;
- vertexShader << "#define FACTOR_SIZE_X " << mStretchPixelsX.Size() + 2 << "\n"
- << "#define FACTOR_SIZE_Y " << mStretchPixelsY.Size() + 2 << "\n"
+ vertexShader << "#define FACTOR_SIZE_X " << xStretchCount + 2 << "\n"
+ << "#define FACTOR_SIZE_Y " << yStretchCount + 2 << "\n"
<< VERTEX_SHADER;
shader = Shader::New( vertexShader.str(), FRAGMENT_SHADER );
}
hints = mImpl->mCustomShader->mHints;
- if( mStretchPixelsX.Size() == 1 && mStretchPixelsY.Size() == 1 )
+ if( ( xStretchCount == 1 && yStretchCount == 1 ) ||
+ ( xStretchCount == 0 && yStretchCount == 0 ) )
{
shader = Shader::New( VERTEX_SHADER_3X3, fragmentShader, hints );
}
- else if( mStretchPixelsX.Size() > 0 || mStretchPixelsY.Size() > 0)
+ else if( xStretchCount > 0 || yStretchCount > 0)
{
std::stringstream vertexShader;
- vertexShader << "#define FACTOR_SIZE_X " << mStretchPixelsX.Size() + 2 << "\n"
- << "#define FACTOR_SIZE_Y " << mStretchPixelsY.Size() + 2 << "\n"
+ vertexShader << "#define FACTOR_SIZE_X " << xStretchCount + 2 << "\n"
+ << "#define FACTOR_SIZE_Y " << yStretchCount + 2 << "\n"
<< VERTEX_SHADER;
shader = Shader::New( vertexShader.str(), fragmentShader, hints );
return shader;
}
-void NPatchVisual::InitializeRenderer()
+void NPatchVisual::ApplyTextureAndUniforms()
{
- Geometry geometry = CreateGeometry();
- Shader shader = CreateShader();
-
- if( !geometry || !shader )
+ const NPatchLoader::Data* 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();
- }
-
- TextureSet textureSet = TextureSet::New();
- mImpl->mRenderer = Renderer::New( geometry, shader );
- mImpl->mRenderer.SetTextures( textureSet );
-
- //Register transform properties
- mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
-}
-
-
-void NPatchVisual::DoSetOnStage( Actor& actor )
-{
- if( !mCroppedImage )
- {
- if( !mImageUrl.empty() )
+ TextureSet textures( data->textureSet );
+ mImpl->mRenderer.SetTextures( textures );
+ if( data->stretchPixelsX.Size() == 1 && data->stretchPixelsY.Size() == 1 )
{
- NinePatchImage nPatch = NinePatchImage::New( mImageUrl );
- InitializeFromImage( nPatch );
- }
- else if( mImage )
- {
- InitializeFromImage( mImage );
- }
- }
-
- //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 )
- {
- ApplyImageToSampler();
- }
-
- actor.AddRenderer( mImpl->mRenderer );
-}
-
-void NPatchVisual::DoSetOffStage( Actor& actor )
-{
- mCroppedImage.Reset();
- actor.RemoveRenderer( mImpl->mRenderer );
- mImpl->mRenderer.Reset();
-}
-
-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 )
- {
- map.Insert( Toolkit::ImageVisual::Property::URL, mImage.GetUrl() );
- }
- map.Insert( Toolkit::ImageVisual::Property::BORDER_ONLY, mBorderOnly );
-}
-
-void NPatchVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
-{
- // This is where specific Properties can be set.
-}
-
-Dali::Property::Value NPatchVisual::DoGetProperty( Dali::Property::Index index )
-{
- Dali::Property::Value value;
-
- switch( index )
- {
- case Toolkit::ImageVisual::Property::URL:
- {
- value = mImageUrl;
- break;
- }
- case Toolkit::ImageVisual::Property::BORDER_ONLY:
- {
- value = mBorderOnly;
- break;
- }
- default:
- {
- break;
- }
- }
-
- return value;
-}
-
-void NPatchVisual::OnSetTransform()
-{
- if( mImpl->mRenderer )
- {
- mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
- }
-}
-
-
-void NPatchVisual::ChangeRenderer( bool oldBorderOnly, size_t oldGridX, size_t oldGridY )
-{
- //check to see if the border style has changed
+ //special case for 9 patch
+ Uint16Pair stretchX = data->stretchPixelsX[ 0 ];
+ Uint16Pair stretchY = data->stretchPixelsY[ 0 ];
- bool borderOnlyChanged = oldBorderOnly != mBorderOnly;
- bool gridChanged = oldGridX != mStretchPixelsX.Size() || oldGridY != mStretchPixelsY.Size();
+ uint16_t stretchWidth = stretchX.GetY() - stretchX.GetX();
+ uint16_t stretchHeight = stretchY.GetY() - stretchY.GetX();
- if( borderOnlyChanged || gridChanged )
- {
- Geometry geometry = CreateGeometry();
- if( geometry )
- {
- mImpl->mRenderer.SetGeometry( geometry );
+ mImpl->mRenderer.RegisterProperty( "uFixed[0]", Vector2::ZERO );
+ mImpl->mRenderer.RegisterProperty( "uFixed[1]", Vector2( stretchX.GetX(), stretchY.GetX()) );
+ mImpl->mRenderer.RegisterProperty( "uFixed[2]", Vector2( data->croppedWidth - stretchWidth, data->croppedHeight - stretchHeight ) );
+ mImpl->mRenderer.RegisterProperty( "uStretchTotal", Vector2( stretchWidth, stretchHeight ) );
}
else
{
- InitializeFromBrokenImage();
- }
- }
+ mImpl->mRenderer.RegisterProperty( "uNinePatchFactorsX[0]", Vector2::ZERO );
+ mImpl->mRenderer.RegisterProperty( "uNinePatchFactorsY[0]", Vector2::ZERO );
- if( gridChanged )
- {
- Shader shader = CreateShader();
- TextureSet textureSet;
- if( shader )
- {
- textureSet = mImpl->mRenderer.GetTextures();
- if( !textureSet )
- {
- InitializeFromBrokenImage();
- }
- mImpl->mRenderer.SetShader( shader );
+ RegisterStretchProperties( mImpl->mRenderer, "uNinePatchFactorsX", data->stretchPixelsX, data->croppedWidth );
+ RegisterStretchProperties( mImpl->mRenderer, "uNinePatchFactorsY", data->stretchPixelsY, data->croppedHeight );
}
}
-}
-
-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;
+ DALI_LOG_ERROR("The N patch image '%s' is not a valid N patch image\n", mImageUrl.c_str() );
+ TextureSet textureSet = TextureSet::New();
+ mImpl->mRenderer.SetTextures( textureSet );
+ Image croppedImage = VisualFactoryCache::GetBrokenVisualImage();
+ TextureSetImage( textureSet, 0u, croppedImage );
+ mImpl->mRenderer.RegisterProperty( "uFixed[0]", Vector2::ZERO );
+ mImpl->mRenderer.RegisterProperty( "uFixed[1]", Vector2::ZERO );
+ mImpl->mRenderer.RegisterProperty( "uFixed[2]", Vector2::ZERO );
+ mImpl->mRenderer.RegisterProperty( "uStretchTotal", Vector2( croppedImage.GetWidth(), croppedImage.GetHeight() ) );
}
- mImageSize = ImageDimensions( mCroppedImage.GetWidth(), mCroppedImage.GetHeight() );
-
- mStretchPixelsX = nPatch.GetStretchPixelsX();
- mStretchPixelsY = nPatch.GetStretchPixelsY();
-}
-
-void NPatchVisual::InitializeFromBrokenImage()
-{
- mCroppedImage = VisualFactoryCache::GetBrokenVisualImage();
- mImageSize = ImageDimensions( mCroppedImage.GetWidth(), mCroppedImage.GetHeight() );
-
- mStretchPixelsX.Clear();
- mStretchPixelsX.PushBack( Uint16Pair( 0, mImageSize.GetWidth() ) );
- mStretchPixelsY.Clear();
- mStretchPixelsY.PushBack( Uint16Pair( 0, mImageSize.GetHeight() ) );
+ //Register transform properties
+ mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
}
-void NPatchVisual::ApplyImageToSampler()
+Geometry NPatchVisual::GetNinePatchGeometry( VisualFactoryCache::GeometryType subType )
{
- TextureSet textureSet = mImpl->mRenderer.GetTextures();
- if( textureSet )
+ Geometry geometry = mFactoryCache.GetGeometry( subType );
+ if( !geometry )
{
- TextureSetImage( textureSet, 0u, mCroppedImage );
-
- if( mStretchPixelsX.Size() == 1 && mStretchPixelsY.Size() == 1 )
+ if( DALI_LIKELY( VisualFactoryCache::NINE_PATCH_GEOMETRY == subType ) )
{
- //special case for 9 patch
- Uint16Pair stretchX = mStretchPixelsX[ 0 ];
- Uint16Pair stretchY = mStretchPixelsY[ 0 ];
-
- uint16_t stretchWidth = stretchX.GetY() - stretchX.GetX();
- uint16_t stretchHeight = stretchY.GetY() - stretchY.GetX();
-
- 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 ) );
+ geometry = CreateGridGeometry( Uint16Pair( 3, 3 ) );
}
- else
+ else if( VisualFactoryCache::NINE_PATCH_BORDER_GEOMETRY == subType )
{
- mImpl->mRenderer.RegisterProperty( "uNinePatchFactorsX[0]", Vector2::ZERO );
- mImpl->mRenderer.RegisterProperty( "uNinePatchFactorsY[0]", Vector2::ZERO );
-
- RegisterStretchProperties( mImpl->mRenderer, "uNinePatchFactorsX", mStretchPixelsX, mImageSize.GetWidth() );
- RegisterStretchProperties( mImpl->mRenderer, "uNinePatchFactorsY", mStretchPixelsY, mImageSize.GetHeight() );
+ geometry = CreateBorderGeometry( Uint16Pair( 3, 3 ) );
}
+ mFactoryCache.SaveGeometry( subType, geometry );
}
+ return geometry;
}
-Geometry NPatchVisual::CreateGeometry( Uint16Pair gridSize )
+Geometry NPatchVisual::CreateGridGeometry( Uint16Pair gridSize )
{
uint16_t gridWidth = gridSize.GetWidth();
uint16_t gridHeight = gridSize.GetHeight();
}
// Create indices
- //TODO: compare performance with triangle strip when Geometry supports it
Vector< unsigned short > indices;
indices.Reserve( gridWidth * gridHeight * 6 );
return GenerateGeometry( vertices, indices );
}
-Geometry NPatchVisual::CreateGeometryBorder( Uint16Pair gridSize )
+Geometry NPatchVisual::CreateBorderGeometry( Uint16Pair gridSize )
{
uint16_t gridWidth = gridSize.GetWidth();
uint16_t gridHeight = gridSize.GetHeight();
}
// Create indices
- //TODO: compare performance with triangle strip when Geometry supports it
Vector< unsigned short > indices;
indices.Reserve( gridWidth * gridHeight * 6 );
/**
* @copydoc Visual::Base::GetNaturalSize
*/
- virtual void GetNaturalSize( Vector2& naturalSize ) const;
+ virtual void GetNaturalSize( Vector2& naturalSize );
/**
* @copydoc Visual::Base::CreatePropertyMap
private:
/**
- * @brief Initialize the renderer with the geometry and shader from the cache, if not available, create and save to the cache for sharing.
- */
- void InitializeRenderer();
-
- /**
* @brief Creates a geometry for this renderer's grid size
*
* @return Returns the created geometry for this renderer's grid size
Shader CreateShader();
/**
+ * @brief Applies texture and related uniforms
+ */
+ void ApplyTextureAndUniforms();
+
+ /**
+ * Helper method to get the default Nine patch geometry from cache or create and store it there
+ * @param subType to use
+ * @return the geometry
+ */
+ Geometry GetNinePatchGeometry( VisualFactoryCache::GeometryType subType );
+
+ /**
* @brief Creates a geometry for the grid size to be used by this visuals' shaders
*
* @param[in] gridSize The grid size of the solid geometry to create
* @return Returns the created geometry for the grid size
*/
- Geometry CreateGeometry( Uint16Pair gridSize );
+ Geometry CreateGridGeometry( Uint16Pair gridSize );
/**
* @brief Creates a geometry with the border only for the grid size to be used by this visuals' shaders
* @param[in] gridSize The grid size of the solid geometry to create
* @return Returns the created geometry for the grid size
*/
- Geometry CreateGeometryBorder( Uint16Pair gridSize );
-
- /**
- * @brief Creates Image from the image url and parses the image for the stretch borders. Will create a error image if the n patch image is invalid
- *
- * @param[in] nPatchImage The NinePatchImage to base our cropped images and stretch borders from
- */
- void InitializeFromImage( NinePatchImage nPatchImage );
-
- /**
- * @brief Creates an error Image to indicate that there was an error in either the image url or the parsing of the image
- *
- */
- void InitializeFromBrokenImage();
-
- /**
- * @brief Applies this renderer's image to the sampler to the texture set used for this renderer
- */
- void ApplyImageToSampler();
-
- /**
- * @brief Changes the current renderer if the n-patch meta data has changed
- *
- * @param[in] oldBorderOnly The old flag indicating if the image should omit the centre of the n-patch and only render the border
- * @param[in] oldGridX The old horizontal grid size of the solid geometry
- * @param[in] oldGridY The old vertical grid size of the solid geometry
- */
- void ChangeRenderer( bool oldBorderOnly, size_t oldGridX, size_t oldGridY );
+ Geometry CreateBorderGeometry( Uint16Pair gridSize );
private:
- NinePatchImage mImage; ///< The image to render if the visual was set from an NinePatchImage, empty otherwise
- Image mCroppedImage;
+ NPatchLoader& mLoader; ///< reference to N patch loader for fast access
+ std::string mImageUrl; ///< The url to the N patch to load
+ std::size_t mId; ///< id of the N patch (from loader/cache)
+ bool mBorderOnly; ///< if only border is desired
- std::string mImageUrl; ///< The url to the image resource to render if the visual was set from an image resource url, empty otherwise
- NinePatchImage::StretchRanges mStretchPixelsX;
- NinePatchImage::StretchRanges mStretchPixelsY;
- ImageDimensions mImageSize;
- bool mBorderOnly;
};
} // namespace Internal
// ToDo: renderer responds to the size change
}
-void PrimitiveVisual::GetNaturalSize( Vector2& naturalSize ) const
+void PrimitiveVisual::GetNaturalSize( Vector2& naturalSize )
{
naturalSize.x = mObjectDimensions.x;
naturalSize.y = mObjectDimensions.y;
/**
* @copydoc Visual::Base::GetNaturalSize
*/
- virtual void GetNaturalSize( Vector2& naturalSize ) const;
+ virtual void GetNaturalSize( Vector2& naturalSize );
/**
* @copydoc Visual::Base::CreatePropertyMap
mPlacementActor.Reset();
}
-void SvgVisual::GetNaturalSize( Vector2& naturalSize ) const
+void SvgVisual::GetNaturalSize( Vector2& naturalSize )
{
if( mParsedImage )
{
/**
* @copydoc Visual::Base::GetNaturalSize
*/
- virtual void GetNaturalSize( Vector2& naturalSize ) const;
+ virtual void GetNaturalSize( Vector2& naturalSize );
/**
* @copydoc Visual::Base::SetSize
return mController->GetHeightForWidth( width );
}
-void TextVisual::GetNaturalSize( Vector2& naturalSize ) const
+void TextVisual::GetNaturalSize( Vector2& naturalSize )
{
naturalSize = mController->GetNaturalSize().GetVectorXY();
}
/**
* @copydoc Visual::Base::GetNaturalSize()
*/
- virtual void GetNaturalSize( Vector2& naturalSize ) const;
+ virtual void GetNaturalSize( Vector2& naturalSize );
/**
* @copydoc Visual::Base::CreatePropertyMap()
return 0.f;
}
-void Visual::Base::GetNaturalSize( Vector2& naturalSize ) const
+void Visual::Base::GetNaturalSize( Vector2& naturalSize )
{
naturalSize = Vector2::ZERO;
}
void Visual::Base::DoSetOffStage( Actor& actor )
{
- actor.RemoveRenderer( mImpl->mRenderer );
- mImpl->mRenderer.Reset();
+ actor.RemoveRenderer( mImpl->mRenderer );
+ mImpl->mRenderer.Reset();
}
bool Visual::Base::IsOnStage() const
/**
* @copydoc Toolkit::Visual::Base::GetNaturalSize
*/
- virtual void GetNaturalSize( Vector2& naturalSize ) const;
+ virtual void GetNaturalSize( Vector2& naturalSize );
/**
* @copydoc Toolkit::Visual::Base::SetDepthIndex
// CLASS HEADER
#include "visual-factory-cache.h"
-// EXTERNAL HEADER
+// EXTERNAL INCLUDES
#include <dali/devel-api/common/hash.h>
#include <dali/public-api/images/resource-image.h>
-// INTERNAL HEADER
+// INTERNAL INCLUDES
#include <dali-toolkit/internal/visuals/color/color-visual.h>
#include <dali-toolkit/internal/visuals/svg/svg-visual.h>
#include <dali-toolkit/internal/visuals/image-atlas-manager.h>
return mAtlasManager;
}
+NPatchLoader& VisualFactoryCache::GetNPatchLoader()
+{
+ return mNPatchLoader;
+}
+
SvgRasterizeThread* VisualFactoryCache::GetSVGRasterizationThread()
{
if( !mSvgRasterizeThread )
* limitations under the License.
*/
-// INTERNAL INCLUDES
-#include "svg/svg-rasterize-thread.h"
-
// EXTERNAL INCLUDES
#include <dali/public-api/math/uint-16-pair.h>
#include <dali/public-api/object/ref-object.h>
#include <dali/devel-api/common/owner-container.h>
#include <dali/devel-api/object/weak-handle.h>
+// INTERNAL INCLUDES
+#include <dali-toolkit/internal/visuals/npatch-loader.h>
+#include <dali-toolkit/internal/visuals/svg/svg-rasterize-thread.h>
namespace Dali
{
class ImageAtlasManager;
typedef IntrusivePtr<ImageAtlasManager> ImageAtlasManagerPtr;
+class NPatchLoader;
+
/**
* Caches shaders and geometries. Owned by VisualFactory.
*/
/**
* Get the image atlas manager.
- * @return A pointer pointing to the atlas manager
+ * @return A pointer to the atlas manager
*/
ImageAtlasManagerPtr GetAtlasManager();
/**
+ * Get the N-Patch texture cache.
+ * @return A reference to the N patch loader
+ */
+ NPatchLoader& GetNPatchLoader();
+
+ /**
* Get the SVG rasterization thread.
- * @return A pointer pointing to the SVG rasterization thread.
+ * @return A raw pointer pointing to the SVG rasterization thread.
*/
SvgRasterizeThread* GetSVGRasterizationThread();
Renderer mWireframeRenderer;
ImageAtlasManagerPtr mAtlasManager;
+ NPatchLoader mNPatchLoader;
+
SvgRasterizeThread* mSvgRasterizeThread;
};
const unsigned int TOOLKIT_MAJOR_VERSION = 1;
const unsigned int TOOLKIT_MINOR_VERSION = 2;
-const unsigned int TOOLKIT_MICRO_VERSION = 12;
+const unsigned int TOOLKIT_MICRO_VERSION = 13;
const char * const TOOLKIT_BUILD_DATE = __DATE__ " " __TIME__;
#ifdef DEBUG_ENABLED
Name: dali-addon
Summary: DALi module for Node.JS
-Version: 1.2.12
+Version: 1.2.13
Release: 1
Group: Development/Libraries
License: Apache License, Version 2.0
Name: NDalic
Summary: dali wrapper
-Version: 1.2.12
+Version: 1.2.13
Release: 1
Group: uifw/graphic
License: TO_BE_FILLED_IN
Name: dali-toolkit
Summary: The OpenGLES Canvas Core Library Toolkit
-Version: 1.2.12
+Version: 1.2.13
Release: 1
Group: System/Libraries
License: Apache-2.0 and BSD-2-Clause and MIT