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=e25b6b672c7d923290104145a5dd47e1c22e2338;hp=6d980f685e75628058dcce5cb801cc5674124ceb;hb=c211a7d84885dca19c90b4656055002865e6fce2;hpb=e6757fc2f8c05d6da32f329a874b0ae129abf3d4 diff --git a/dali-toolkit/internal/visuals/image/image-visual.cpp b/dali-toolkit/internal/visuals/image/image-visual.cpp index 6d980f6..e25b6b6 100644 --- a/dali-toolkit/internal/visuals/image/image-visual.cpp +++ b/dali-toolkit/internal/visuals/image/image-visual.cpp @@ -134,7 +134,7 @@ const char* FRAGMENT_SHADER_ATLAS_CLAMP = DALI_COMPOSE_SHADER( \n void main()\n {\n - mediump vec2 texCoord = mix( uAtlasRect.xy, uAtlasRect.zw, clamp( vTexCoord, 0.0, 1.0 ) );\n + mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );\n gl_FragColor = texture2D( sTexture, texCoord ) * uColor;\n }\n ); @@ -147,17 +147,20 @@ const char* FRAGMENT_SHADER_ATLAS_VARIOUS_WRAP = DALI_COMPOSE_SHADER( uniform lowp vec2 wrapMode;\n uniform lowp vec4 uColor;\n \n - mediump float wrapCoordinate( mediump float coordinate, lowp float wrap )\n + mediump float wrapCoordinate( mediump vec2 range, mediump float coordinate, lowp float wrap )\n {\n - if( abs(wrap-2.0) < 0.5 )\n // REFLECT - return 1.0-abs(fract(coordinate*0.5)*2.0 - 1.0);\n + mediump float coord;\n + if( wrap > 1.5 )\n // REFLECT + coord = 1.0-abs(fract(coordinate*0.5)*2.0 - 1.0);\n else \n// warp == 0 or 1 - return mix( clamp( coordinate, 0.0, 1.0 ), fract( coordinate ), wrap);\n + coord = mix(coordinate, fract( coordinate ), wrap);\n + return clamp( mix(range.x, range.y, coord), range.x, range.y ); }\n + \n void main()\n {\n - mediump vec2 texCoord = mix( uAtlasRect.xy, uAtlasRect.zw, - vec2( wrapCoordinate( vTexCoord.x, wrapMode.x ), wrapCoordinate( vTexCoord.y, wrapMode.y ) ) );\n + mediump vec2 texCoord = vec2( wrapCoordinate( uAtlasRect.xz, vTexCoord.x, wrapMode.x ), + wrapCoordinate( uAtlasRect.yw, vTexCoord.y, wrapMode.y ) );\n gl_FragColor = texture2D( sTexture, texCoord ) * uColor;\n }\n ); @@ -183,19 +186,90 @@ Geometry CreateGeometry( VisualFactoryCache& factoryCache, ImageDimensions gridS return geometry; } -} //unnamed namespace +} // unnamed namespace + +ImageVisualPtr ImageVisual::New( VisualFactoryCache& factoryCache ) +{ + return new ImageVisual( factoryCache ); +} + +ImageVisualPtr ImageVisual::New( VisualFactoryCache& factoryCache, + const std::string& imageUrl, + ImageDimensions size, + FittingMode::Type fittingMode, + Dali::SamplingMode::Type samplingMode ) +{ + return new ImageVisual( factoryCache, imageUrl, size, fittingMode, samplingMode ); +} + +ImageVisualPtr ImageVisual::New( VisualFactoryCache& factoryCache, const Image& image ) +{ + return new ImageVisual( factoryCache, image ); +} ImageVisual::ImageVisual( VisualFactoryCache& factoryCache ) : Visual::Base( factoryCache ), + mImage(), + mPixels(), + mPixelArea( FULL_TEXTURE_RECT ), + mPlacementActor(), + mImageUrl(), + mDesiredSize(), + mFittingMode( FittingMode::DEFAULT ), + mSamplingMode( SamplingMode::DEFAULT ), + mWrapModeU( WrapMode::DEFAULT ), + mWrapModeV( WrapMode::DEFAULT ), + mNativeFragmentShaderCode(), + mNativeImageFlag( false ) +{ +} + +ImageVisual::ImageVisual( VisualFactoryCache& factoryCache, + const std::string& imageUrl, + ImageDimensions size, + FittingMode::Type fittingMode, + Dali::SamplingMode::Type samplingMode ) +: Visual::Base( factoryCache ), + mImage(), + mPixels(), mPixelArea( FULL_TEXTURE_RECT ), + mPlacementActor(), + mImageUrl( imageUrl ), + mDesiredSize( size ), + mFittingMode( fittingMode ), + mSamplingMode( samplingMode ), + mWrapModeU( WrapMode::DEFAULT ), + mWrapModeV( WrapMode::DEFAULT ), + mNativeFragmentShaderCode(), + mNativeImageFlag( false ) +{ +} + +ImageVisual::ImageVisual( VisualFactoryCache& factoryCache, const Image& image ) +: Visual::Base( factoryCache ), + mImage( image ), + mPixels(), + mPixelArea( FULL_TEXTURE_RECT ), + mPlacementActor(), + mImageUrl(), mDesiredSize(), mFittingMode( FittingMode::DEFAULT ), mSamplingMode( SamplingMode::DEFAULT ), mWrapModeU( WrapMode::DEFAULT ), mWrapModeV( WrapMode::DEFAULT ), - mNativeFragmentShaderCode( ), + mNativeFragmentShaderCode(), mNativeImageFlag( false ) { + NativeImage newNativeImage = NativeImage::DownCast( image ); + if( newNativeImage ) + { + SetNativeFragmentShaderCode( newNativeImage ); + mNativeImageFlag = true; + } + else + { + mNativeImageFlag = false; + } } ImageVisual::~ImageVisual() @@ -279,22 +353,9 @@ void ImageVisual::DoInitialize( Actor& actor, const Property::Map& propertyMap ) // if sync loading is required, the loading should start immediately when new image url is set or the actor is off stage // ( for on-stage actor with image url unchanged, resource loading is already finished) - if( ( !mImpl->mRenderer || imageURLValue) && IsSynchronousResourceLoading() ) + if( imageURLValue && IsSynchronousResourceLoading() ) { - DoSynchronousResourceLoading(); - } - - // remove old renderer if exit - if( mImpl->mRenderer ) - { - if( actor ) //remove old renderer from actor - { - actor.RemoveRenderer( mImpl->mRenderer ); - } - if( !oldImageUrl.empty() ) //clean old renderer from cache - { - CleanCache( oldImageUrl ); - } + LoadResourceSynchronously(); } NativeImage nativeImage = NativeImage::DownCast( mImage ); @@ -416,7 +477,7 @@ bool ImageVisual::IsSynchronousResourceLoading() const return mImpl->mFlags & Impl::IS_SYNCHRONOUS_RESOURCE_LOADING; } -void ImageVisual::DoSynchronousResourceLoading() +void ImageVisual::LoadResourceSynchronously() { if( !mImageUrl.empty() ) { @@ -475,7 +536,7 @@ TextureSet ImageVisual::CreateTextureSet( Vector4& textureRect, const std::strin } else { - textureSet = mFactoryCache.GetAtlasManager()->Add(textureRect, url, mDesiredSize, mFittingMode, mSamplingMode ); + textureSet = mFactoryCache.GetAtlasManager()->Add(textureRect, url, mDesiredSize, mFittingMode, true, this ); mImpl->mFlags |= Impl::IS_ATLASING_APPLIED; if( !textureSet ) // big image, no atlasing { @@ -506,6 +567,7 @@ void ImageVisual::InitializeRenderer( const std::string& imageUrl ) mImageUrl = imageUrl; mImpl->mRenderer.Reset(); + mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED; if( !mImpl->mCustomShader && ( strncasecmp( imageUrl.c_str(), HTTP_URL, sizeof(HTTP_URL) -1 ) != 0 ) && // ignore remote images @@ -570,8 +632,22 @@ void ImageVisual::InitializeRenderer( const Image& image ) } } +void ImageVisual::UploadCompleted() +{ + // Resource image is loaded. If weak handle is holding a placement actor, it is the time to add the renderer to actor. + Actor actor = mPlacementActor.GetHandle(); + if( actor ) + { + actor.AddRenderer( mImpl->mRenderer ); + // reset the weak handle so that the renderer only get added to actor once + mPlacementActor.Reset(); + } +} + void ImageVisual::DoSetOnStage( Actor& actor ) { + mPlacementActor = actor; + if( !mImageUrl.empty() ) { InitializeRenderer( mImageUrl ); @@ -585,6 +661,12 @@ void ImageVisual::DoSetOnStage( Actor& actor ) { mImpl->mRenderer.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, mPixelArea ); } + + if( IsSynchronousResourceLoading() || !(mImpl->mFlags & Impl::IS_ATLASING_APPLIED) ) + { + actor.AddRenderer( mImpl->mRenderer ); + mPlacementActor.Reset(); + } } void ImageVisual::DoSetOffStage( Actor& actor ) @@ -601,6 +683,7 @@ void ImageVisual::DoSetOffStage( Actor& actor ) actor.RemoveRenderer( mImpl->mRenderer ); mImpl->mRenderer.Reset(); } + mPlacementActor.Reset(); } void ImageVisual::DoCreatePropertyMap( Property::Map& map ) const @@ -636,6 +719,17 @@ void ImageVisual::DoCreatePropertyMap( Property::Map& map ) const map.Insert( Toolkit::ImageVisual::Property::WRAP_MODE_V, mWrapModeV ); } +void ImageVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue ) +{ + // TODO +} + +Dali::Property::Value ImageVisual::DoGetProperty( Dali::Property::Index index ) +{ + // TODO + return Dali::Property::Value(); +} + Shader ImageVisual::GetImageShader( VisualFactoryCache& factoryCache, bool atlasing, bool defaultTextureWrapping ) { Shader shader; @@ -673,131 +767,6 @@ Shader ImageVisual::GetImageShader( VisualFactoryCache& factoryCache, bool atlas return shader; } -void ImageVisual::SetImage( Actor& actor, const std::string& imageUrl, ImageDimensions size, Dali::FittingMode::Type fittingMode, Dali::SamplingMode::Type samplingMode ) -{ - if( mImageUrl != imageUrl ) - { - std::string oldImageUrl = mImageUrl; - mImageUrl = imageUrl; - mDesiredSize = size; - mFittingMode = fittingMode; - mSamplingMode = samplingMode; - mImage.Reset(); - - if( IsSynchronousResourceLoading() ) - { - DoSynchronousResourceLoading(); - } - - if( mImpl->mRenderer ) - { - if( GetIsFromCache() ) // if renderer is from cache, remove the old one - { - //remove old renderer - if( actor ) - { - actor.RemoveRenderer( mImpl->mRenderer ); - } - - //clean the cache - if( !oldImageUrl.empty() ) - { - CleanCache(oldImageUrl); - } - - if( actor && actor.OnStage() ) // if actor on stage, create a new renderer and apply to actor - { - SetOnStage(actor); - } - } - else // if renderer is not from cache, reuse the same renderer and only change the texture - { - Image image = LoadImage( imageUrl, IsSynchronousResourceLoading() ); - ApplyImageToSampler( image ); - } - } - } -} - -void ImageVisual::SetImage( Actor& actor, const Image& image ) -{ - if( mImage != image ) - { - NativeImage newNativeImage = NativeImage::DownCast( image ); - bool newRendererFlag = true; - - if( newNativeImage && !mNativeImageFlag ) - { - SetNativeFragmentShaderCode( newNativeImage ); - } - - if( ( newNativeImage && mNativeImageFlag ) || ( !newNativeImage && !mNativeImageFlag ) ) - { - newRendererFlag = false; - } - - if( newNativeImage ) - { - mNativeImageFlag = true; - } - else - { - mNativeFragmentShaderCode.clear(); - mNativeImageFlag = false; - } - - mImage = image; - - if( mImpl->mRenderer ) - { - // if renderer is from cache, remove the old one, and create new renderer - if( GetIsFromCache() ) - { - //remove old renderer - if( actor ) - { - actor.RemoveRenderer( mImpl->mRenderer ); - } - - //clean the cache - if( !mImageUrl.empty() ) - { - CleanCache(mImageUrl); - } - mImageUrl.clear(); - - if( actor && actor.OnStage() ) // if actor on stage, create a new renderer and apply to actor - { - SetOnStage(actor); - } - } - // if input image is nativeImage and mImage is regular image or the reverse, remove the old one, and create new renderer - else if( newRendererFlag ) - { - //remove old renderer - if( actor ) - { - actor.RemoveRenderer( mImpl->mRenderer ); - } - - if( actor && actor.OnStage() ) // if actor on stage, create a new renderer and apply to actor - { - SetOnStage(actor); - } - } - else // if renderer is not from cache, reuse the same renderer and only change the texture - { - ApplyImageToSampler( image ); - } - } - - mImageUrl.clear(); - mDesiredSize = ImageDimensions(); - mFittingMode = FittingMode::DEFAULT; - mSamplingMode = SamplingMode::DEFAULT; - } -} - void ImageVisual::ApplyImageToSampler( const Image& image ) { if( image ) @@ -829,20 +798,23 @@ void ImageVisual::OnImageLoaded( ResourceImage image ) void ImageVisual::CleanCache(const std::string& url) { - TextureSet textureSet = mImpl->mRenderer.GetTextures(); - - 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 ) + if( IsFromCache() ) { - 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 ); + 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 ); + } + + mImpl->mRenderer.Reset(); + if( mFactoryCache.CleanRendererCache( url ) && index != Property::INVALID_INDEX ) + { + mFactoryCache.GetAtlasManager()->Remove( textureSet, atlasRect ); + } } }