X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git;a=blobdiff_plain;f=dali-toolkit%2Finternal%2Fvisuals%2Fimage%2Fimage-visual.cpp;h=5c8a00482684047ae5c5933b3bfb760a94503523;hp=9ce3a928db26c91ed45e1f5fd9bc08a01e12bef1;hb=1fbb87959f02b4526fd66d9b8b39e4f8241c23ee;hpb=45e2dd4d4023bd68a494e062114eacf130defeb2 diff --git a/dali-toolkit/internal/visuals/image/image-visual.cpp b/dali-toolkit/internal/visuals/image/image-visual.cpp index 9ce3a92..5c8a004 100644 --- a/dali-toolkit/internal/visuals/image/image-visual.cpp +++ b/dali-toolkit/internal/visuals/image/image-visual.cpp @@ -18,19 +18,20 @@ // CLASS HEADER #include -// EXTERNAL HEADER -#include // for strncasecmp +// EXTERNAL HEADERS +#include // for strlen() #include #include -#include #include #include +#include #include #include #include -// INTERNAL HEADER +// INTERNAL HEADERS #include +#include #include #include #include @@ -38,6 +39,7 @@ #include #include #include +#include namespace Dali { @@ -50,8 +52,6 @@ namespace Internal namespace { -const char HTTP_URL[] = "http://"; -const char HTTPS_URL[] = "https://"; // property names const char * const IMAGE_FITTING_MODE( "fittingMode" ); @@ -59,7 +59,7 @@ const char * const IMAGE_SAMPLING_MODE( "samplingMode" ); const char * const IMAGE_DESIRED_WIDTH( "desiredWidth" ); const char * const IMAGE_DESIRED_HEIGHT( "desiredHeight" ); const char * const SYNCHRONOUS_LOADING( "synchronousLoading" ); -const char * const BATCHING_ENABLED( "batchingEnabled" ); +const char * const IMAGE_ATLASING("atlasing"); // fitting modes DALI_ENUM_TO_STRING_TABLE_BEGIN( FITTING_MODE ) @@ -95,7 +95,9 @@ const char* DEFAULT_SAMPLER_TYPENAME = "sampler2D"; const char* VERTEX_SHADER = DALI_COMPOSE_SHADER( attribute mediump vec2 aPosition;\n - uniform mediump mat4 uMvpMatrix;\n + uniform mediump mat4 uModelMatrix;\n + uniform mediump mat4 uViewMatrix;\n + uniform mediump mat4 uProjection;\n uniform mediump vec3 uSize;\n uniform mediump vec4 pixelArea; varying mediump vec2 vTexCoord;\n @@ -117,7 +119,9 @@ const char* VERTEX_SHADER = DALI_COMPOSE_SHADER( void main()\n {\n - mediump vec4 vertexPosition = uMvpMatrix * ComputeVertexPosition();\n + mediump vec4 vertexPosition = uViewMatrix * uModelMatrix * ComputeVertexPosition();\n + vertexPosition.xy = floor ( vertexPosition.xy );\n // Pixel alignment + vertexPosition = uProjection * vertexPosition;\n vTexCoord = pixelArea.xy+pixelArea.zw*(aPosition + vec2(0.5) );\n gl_Position = vertexPosition;\n }\n @@ -127,12 +131,13 @@ const char* FRAGMENT_SHADER_NO_ATLAS = DALI_COMPOSE_SHADER( varying mediump vec2 vTexCoord;\n uniform sampler2D sTexture;\n uniform lowp vec4 uColor;\n - uniform lowp vec4 mixColor;\n + uniform lowp vec3 mixColor;\n + uniform lowp float opacity;\n uniform lowp float preMultipliedAlpha;\n \n lowp vec4 visualMixColor()\n {\n - return vec4( mixColor.rgb * mix( 1.0, mixColor.a, preMultipliedAlpha ), mixColor.a );\n + return vec4( mixColor * mix( 1.0, opacity, preMultipliedAlpha ), opacity );\n }\n void main()\n {\n @@ -145,12 +150,13 @@ const char* FRAGMENT_SHADER_ATLAS_CLAMP = DALI_COMPOSE_SHADER( uniform sampler2D sTexture;\n uniform mediump vec4 uAtlasRect;\n uniform lowp vec4 uColor;\n - uniform lowp vec4 mixColor;\n + uniform lowp vec3 mixColor;\n + uniform lowp float opacity;\n uniform lowp float preMultipliedAlpha;\n \n lowp vec4 visualMixColor()\n {\n - return vec4( mixColor.rgb * mix( 1.0, mixColor.a, preMultipliedAlpha ), mixColor.a );\n + return vec4( mixColor * mix( 1.0, opacity, preMultipliedAlpha ), opacity );\n }\n \n void main()\n @@ -167,7 +173,8 @@ const char* FRAGMENT_SHADER_ATLAS_VARIOUS_WRAP = DALI_COMPOSE_SHADER( // WrapMode -- 0: CLAMP; 1: REPEAT; 2: REFLECT; uniform lowp vec2 wrapMode;\n uniform lowp vec4 uColor;\n - uniform lowp vec4 mixColor;\n + uniform lowp vec3 mixColor;\n + uniform lowp float opacity;\n uniform lowp float preMultipliedAlpha;\n \n mediump float wrapCoordinate( mediump vec2 range, mediump float coordinate, lowp float wrap )\n @@ -182,7 +189,7 @@ const char* FRAGMENT_SHADER_ATLAS_VARIOUS_WRAP = DALI_COMPOSE_SHADER( \n lowp vec4 visualMixColor()\n {\n - return vec4( mixColor.rgb * mix( 1.0, mixColor.a, preMultipliedAlpha ), mixColor.a );\n + return vec4( mixColor * mix( 1.0, opacity, preMultipliedAlpha ), opacity );\n }\n \n void main()\n @@ -212,7 +219,7 @@ Geometry CreateGeometry( VisualFactoryCache& factoryCache, ImageDimensions gridS } // unnamed namespace ImageVisualPtr ImageVisual::New( VisualFactoryCache& factoryCache, - const std::string& imageUrl, + const VisualUrl& imageUrl, const Property::Map& properties, ImageDimensions size, FittingMode::Type fittingMode, @@ -224,7 +231,7 @@ ImageVisualPtr ImageVisual::New( VisualFactoryCache& factoryCache, } ImageVisualPtr ImageVisual::New( VisualFactoryCache& factoryCache, - const std::string& imageUrl, + const VisualUrl& imageUrl, ImageDimensions size, FittingMode::Type fittingMode, Dali::SamplingMode::Type samplingMode ) @@ -238,7 +245,7 @@ ImageVisualPtr ImageVisual::New( VisualFactoryCache& factoryCache, const Image& } ImageVisual::ImageVisual( VisualFactoryCache& factoryCache, - const std::string& imageUrl, + const VisualUrl& imageUrl, ImageDimensions size, FittingMode::Type fittingMode, Dali::SamplingMode::Type samplingMode ) @@ -252,7 +259,8 @@ ImageVisual::ImageVisual( VisualFactoryCache& factoryCache, mFittingMode( fittingMode ), mSamplingMode( samplingMode ), mWrapModeU( WrapMode::DEFAULT ), - mWrapModeV( WrapMode::DEFAULT ) + mWrapModeV( WrapMode::DEFAULT ), + mAttemptAtlasing( false ) { } @@ -267,7 +275,8 @@ ImageVisual::ImageVisual( VisualFactoryCache& factoryCache, const Image& image ) mFittingMode( FittingMode::DEFAULT ), mSamplingMode( SamplingMode::DEFAULT ), mWrapModeU( WrapMode::DEFAULT ), - mWrapModeV( WrapMode::DEFAULT ) + mWrapModeV( WrapMode::DEFAULT ), + mAttemptAtlasing( false ) { } @@ -319,10 +328,14 @@ void ImageVisual::DoSetProperties( const Property::Map& propertyMap ) { DoSetProperty( Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, keyValue.second ); } + else if ( keyValue.first == IMAGE_ATLASING ) + { + DoSetProperty( Toolkit::DevelImageVisual::Property::ATLASING, keyValue.second ); + } } } - if( mImpl->mFlags & Impl::IS_SYNCHRONOUS_RESOURCE_LOADING && mImageUrl.size() > 0u ) + if( ( mImpl->mFlags & Impl::IS_SYNCHRONOUS_RESOURCE_LOADING ) && mImageUrl.IsValid() ) { // if sync loading is required, the loading should start // immediately when new image url is set or the actor is off stage @@ -422,6 +435,12 @@ void ImageVisual::DoSetProperty( Property::Index index, const Property::Value& v mWrapModeV = Dali::WrapMode::Type( wrapMode ); break; } + + case Toolkit::DevelImageVisual::Property::ATLASING: + { + bool atlasing = false; + mAttemptAtlasing = value.Get( atlasing ); + } } } @@ -439,9 +458,9 @@ void ImageVisual::GetNaturalSize( Vector2& naturalSize ) naturalSize.y = mDesiredSize.GetHeight(); return; } - else if( !mImageUrl.empty() ) + else if( mImageUrl.IsValid() && mImageUrl.GetLocation() == VisualUrl::LOCAL ) { - ImageDimensions dimentions = ResourceImage::GetImageSize( mImageUrl ); + ImageDimensions dimentions = Dali::GetClosestImageSize( mImageUrl.GetUrl() ); naturalSize.x = dimentions.GetWidth(); naturalSize.y = dimentions.GetHeight(); return; @@ -498,11 +517,13 @@ void ImageVisual::CreateNativeImageRenderer( NativeImage& nativeImage ) std::string fragmentShader; const char* fragmentPreFix = nativeImage.GetCustomFragmentPreFix(); const char* customSamplerTypename = nativeImage.GetCustomSamplerTypename(); + if( fragmentPreFix ) { fragmentShader = fragmentPreFix; fragmentShader += "\n"; } + if( mImpl->mCustomShader && !mImpl->mCustomShader->mFragmentShader.empty() ) { fragmentShader += mImpl->mCustomShader->mFragmentShader; @@ -511,6 +532,7 @@ void ImageVisual::CreateNativeImageRenderer( NativeImage& nativeImage ) { fragmentShader += FRAGMENT_SHADER_NO_ATLAS; } + if( customSamplerTypename ) { fragmentShader.replace( fragmentShader.find( DEFAULT_SAMPLER_TYPENAME ), strlen( DEFAULT_SAMPLER_TYPENAME ), customSamplerTypename ); @@ -549,15 +571,15 @@ bool ImageVisual::IsSynchronousResourceLoading() const void ImageVisual::LoadResourceSynchronously() { - if( !mImageUrl.empty() ) + if( mImageUrl.IsValid() ) { - BitmapLoader loader = BitmapLoader::New( mImageUrl, mDesiredSize, mFittingMode, mSamplingMode ); + BitmapLoader loader = BitmapLoader::New( mImageUrl.GetUrl(), mDesiredSize, mFittingMode, mSamplingMode ); loader.Load(); mPixels = loader.GetPixelData(); } } -TextureSet ImageVisual::CreateTextureSet( Vector4& textureRect, const std::string& url, bool synchronousLoading, bool attemptAtlasing ) +TextureSet ImageVisual::CreateTextureSet( Vector4& textureRect, bool synchronousLoading, bool attemptAtlasing ) { TextureSet textureSet; textureRect = FULL_TEXTURE_RECT; @@ -591,13 +613,13 @@ TextureSet ImageVisual::CreateTextureSet( Vector4& textureRect, const std::strin { if( attemptAtlasing ) { - textureSet = mFactoryCache.GetAtlasManager()->Add(textureRect, url, mDesiredSize, mFittingMode, true, this ); + textureSet = mFactoryCache.GetAtlasManager()->Add( textureRect, mImageUrl.GetUrl(), mDesiredSize, mFittingMode, true, this ); mImpl->mFlags |= Impl::IS_ATLASING_APPLIED; } if( !textureSet ) // big image, no atlasing or atlasing failed { mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED; - ResourceImage resourceImage = Dali::ResourceImage::New( url, mDesiredSize, mFittingMode, mSamplingMode ); + ResourceImage resourceImage = Dali::ResourceImage::New( mImageUrl.GetUrl(), mDesiredSize, mFittingMode, mSamplingMode ); resourceImage.LoadingFinishedSignal().Connect( this, &ImageVisual::OnImageLoaded ); textureSet = TextureSet::New(); TextureSetImage( textureSet, 0u, resourceImage ); @@ -613,74 +635,45 @@ TextureSet ImageVisual::CreateTextureSet( Vector4& textureRect, const std::strin return textureSet; } -void ImageVisual::InitializeRenderer( const std::string& imageUrl ) +void ImageVisual::InitializeRenderer() { - mImageUrl = imageUrl; mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED; - if( !mImpl->mCustomShader && - ( strncasecmp( imageUrl.c_str(), HTTP_URL, sizeof(HTTP_URL) -1 ) != 0 ) && // dont atlas remote images - ( strncasecmp( imageUrl.c_str(), HTTPS_URL, sizeof(HTTPS_URL) -1 ) != 0 ) ) + if( !mImpl->mCustomShader && mImageUrl.GetLocation() == VisualUrl::LOCAL ) { bool defaultWrapMode = mWrapModeU <= WrapMode::CLAMP_TO_EDGE && mWrapModeV <= WrapMode::CLAMP_TO_EDGE; - bool defaultTransform = mImpl->mTransform.mSize == Vector2::ONE && - mImpl->mTransform.mOffset == Vector2::ZERO && - mImpl->mTransform.mOffsetSizeMode == Vector4::ZERO && - mImpl->mTransform.mOrigin == Toolkit::Align::CENTER && - mImpl->mTransform.mAnchorPoint == Toolkit::Align::CENTER; - - bool cacheable = defaultWrapMode && defaultTransform && mPixelArea == FULL_TEXTURE_RECT; + Vector4 atlasRect; + // texture set has to be created first as we need to know if atlasing succeeded or not + // when selecting the shader + TextureSet textures = CreateTextureSet( atlasRect, IsSynchronousResourceLoading(), mAttemptAtlasing ); + CreateRenderer( textures ); - mImpl->mFlags &= ~Impl::IS_FROM_CACHE; - if( cacheable ) // fetch the renderer from cache if exist - { - mImpl->mRenderer = mFactoryCache.GetRenderer( imageUrl ); - mImpl->mFlags |= Impl::IS_FROM_CACHE; - } - - if( !mImpl->mRenderer ) // new renderer is needed + if( mImpl->mFlags & Impl::IS_ATLASING_APPLIED ) // the texture is packed inside atlas { - Vector4 atlasRect; - // texture set has to be created first as we need to know if atlasing succeeded or not - // when selecting the shader - TextureSet textures = CreateTextureSet( atlasRect, imageUrl, IsSynchronousResourceLoading(), true ); - CreateRenderer( textures ); - - if( mImpl->mFlags & Impl::IS_ATLASING_APPLIED ) // the texture is packed inside atlas + mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, atlasRect ); + if( !defaultWrapMode ) // custom wrap mode, renderer is not cached. { - mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, atlasRect ); - if( !defaultWrapMode ) // custom wrap mode, renderer is not cached. - { - Vector2 wrapMode(mWrapModeU-WrapMode::CLAMP_TO_EDGE, mWrapModeV-WrapMode::CLAMP_TO_EDGE); - wrapMode.Clamp( Vector2::ZERO, Vector2( 2.f, 2.f ) ); - mImpl->mRenderer.RegisterProperty( WRAP_MODE_UNIFORM_NAME, wrapMode ); - } - } - - // save the renderer to cache only when default wrap mode and default pixel area is used - if( cacheable ) - { - mFactoryCache.SaveRenderer( imageUrl, mImpl->mRenderer ); + Vector2 wrapMode(mWrapModeU-WrapMode::CLAMP_TO_EDGE, mWrapModeV-WrapMode::CLAMP_TO_EDGE); + wrapMode.Clamp( Vector2::ZERO, Vector2( 2.f, 2.f ) ); + mImpl->mRenderer.RegisterProperty( WRAP_MODE_UNIFORM_NAME, wrapMode ); } } } else { - // for custom shader or remote image, renderer is not cached and atlas is not applied - mImpl->mFlags &= ~Impl::IS_FROM_CACHE; + // for custom shader or remote image, atlas is not applied Vector4 atlasRect; // ignored in this case - TextureSet textures = CreateTextureSet( atlasRect, imageUrl, IsSynchronousResourceLoading(), false ); + TextureSet textures = CreateTextureSet( atlasRect, IsSynchronousResourceLoading(), false ); CreateRenderer( textures ); } } void ImageVisual::InitializeRenderer( const Image& image ) { - mImpl->mFlags &= ~Impl::IS_FROM_CACHE; - // don't reuse CreateTextureSet TextureSet textures = TextureSet::New(); + // Renderer can't be shared if mImage is NativeImage NativeImage nativeImage = NativeImage::DownCast( image ); if( nativeImage ) @@ -711,9 +704,9 @@ void ImageVisual::UploadCompleted() void ImageVisual::DoSetOnStage( Actor& actor ) { - if( !mImageUrl.empty() ) + if( mImageUrl.IsValid() ) { - InitializeRenderer( mImageUrl ); + InitializeRenderer(); } else if ( mImage ) { @@ -745,9 +738,9 @@ void ImageVisual::DoSetOffStage( Actor& actor ) //If we own the image then make sure we release it when we go off stage actor.RemoveRenderer( mImpl->mRenderer); - if( !mImageUrl.empty() ) + if( mImageUrl.IsValid() ) { - CleanCache(mImageUrl); + RemoveFromAtlas( mImageUrl.GetUrl() ); mImage.Reset(); } @@ -762,9 +755,9 @@ void ImageVisual::DoCreatePropertyMap( Property::Map& map ) const bool sync = IsSynchronousResourceLoading(); map.Insert( SYNCHRONOUS_LOADING, sync ); - if( !mImageUrl.empty() ) + if( mImageUrl.IsValid() ) { - map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl ); + map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl() ); map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth() ); map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight() ); } @@ -786,6 +779,24 @@ void ImageVisual::DoCreatePropertyMap( Property::Map& map ) const 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::DevelImageVisual::Property::ATLASING, mAttemptAtlasing ); +} + +void ImageVisual::DoCreateInstancePropertyMap( Property::Map& map ) const +{ + map.Clear(); + map.Insert( Toolkit::DevelVisual::Property::TYPE, Toolkit::Visual::IMAGE ); + if( mImageUrl.IsValid() ) + { + map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth() ); + map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight() ); + } + else if( mImage ) + { + map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, static_cast(mImage.GetWidth()) ); + map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, static_cast(mImage.GetHeight()) ); + } } void ImageVisual::OnSetTransform() @@ -862,24 +873,22 @@ void ImageVisual::OnImageLoaded( ResourceImage image ) } } -void ImageVisual::CleanCache(const std::string& url) +void ImageVisual::RemoveFromAtlas(const std::string& url) { - if( IsFromCache() ) + 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 ) { - 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 ) - { - Property::Value atlasRectValue = mImpl->mRenderer.GetProperty( index ); - atlasRectValue.Get( atlasRect ); - } + Property::Value atlasRectValue = mImpl->mRenderer.GetProperty( index ); + atlasRectValue.Get( atlasRect ); + } - TextureSet textureSet = mImpl->mRenderer.GetTextures(); - mImpl->mRenderer.Reset(); - if( mFactoryCache.CleanRendererCache( url ) && index != Property::INVALID_INDEX ) - { - mFactoryCache.GetAtlasManager()->Remove( textureSet, atlasRect ); - } + TextureSet textureSet = mImpl->mRenderer.GetTextures(); + mImpl->mRenderer.Reset(); + + if( index != Property::INVALID_INDEX ) + { + mFactoryCache.GetAtlasManager()->Remove( textureSet, atlasRect ); } }