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=3c82d758a296fe049a9d1a8f414e182796a44d82;hp=a0197cd6afcca4dca1377b944333d1b6e9beb2bc;hb=c46c71ad6367e90da584952677f44a97723156f0;hpb=8e7cfd0c114bf778287cc6e67d0f42f3c866e205 diff --git a/dali-toolkit/internal/visuals/image/image-visual.cpp b/dali-toolkit/internal/visuals/image/image-visual.cpp index a0197cd..3c82d75 100644 --- a/dali-toolkit/internal/visuals/image/image-visual.cpp +++ b/dali-toolkit/internal/visuals/image/image-visual.cpp @@ -18,18 +18,21 @@ // 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 @@ -37,6 +40,7 @@ #include #include #include +#include namespace Dali { @@ -49,8 +53,6 @@ namespace Internal namespace { -const char HTTP_URL[] = "http://"; -const char HTTPS_URL[] = "https://"; // property names const char * const IMAGE_FITTING_MODE( "fittingMode" ); @@ -58,6 +60,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 IMAGE_ATLASING("atlasing"); // fitting modes DALI_ENUM_TO_STRING_TABLE_BEGIN( FITTING_MODE ) @@ -91,31 +94,39 @@ const Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f); const char* DEFAULT_SAMPLER_TYPENAME = "sampler2D"; +const float PIXEL_ALIGN_ON = 1.0f; +const float PIXEL_ALIGN_OFF = 0.0f; + 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 + uniform lowp float uPixelAligned;\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 - +\n vec4 ComputeVertexPosition()\n {\n vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n return vec4( (aPosition + anchorPoint)*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );\n }\n - +\n void main()\n {\n - mediump vec4 vertexPosition = uMvpMatrix * ComputeVertexPosition();\n + mediump vec4 vertexPosition = uViewMatrix * uModelMatrix * ComputeVertexPosition();\n + vec4 alignedVertexPosition = vertexPosition;\n + alignedVertexPosition.xy = floor ( vertexPosition.xy );\n // Pixel alignment + vertexPosition = uProjection * mix( vertexPosition, alignedVertexPosition, uPixelAligned );\n vTexCoord = pixelArea.xy+pixelArea.zw*(aPosition + vec2(0.5) );\n gl_Position = vertexPosition;\n }\n @@ -125,12 +136,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 @@ -143,12 +155,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 @@ -165,7 +178,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 @@ -180,7 +194,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 @@ -210,7 +224,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, @@ -222,7 +236,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 ) @@ -236,7 +250,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 ) @@ -250,7 +264,8 @@ ImageVisual::ImageVisual( VisualFactoryCache& factoryCache, mFittingMode( fittingMode ), mSamplingMode( samplingMode ), mWrapModeU( WrapMode::DEFAULT ), - mWrapModeV( WrapMode::DEFAULT ) + mWrapModeV( WrapMode::DEFAULT ), + mAttemptAtlasing( false ) { } @@ -265,7 +280,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 ) { } @@ -317,10 +333,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 @@ -420,6 +440,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 ); + } } } @@ -437,9 +463,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; @@ -480,6 +506,8 @@ void ImageVisual::CreateRenderer( TextureSet& textures ) } } + shader.RegisterProperty( PIXEL_ALIGNED_UNIFORM_NAME, PIXEL_ALIGN_ON ); // Set default to align + mImpl->mRenderer = Renderer::New( geometry, shader ); DALI_ASSERT_DEBUG( textures ); mImpl->mRenderer.SetTextures( textures ); @@ -496,11 +524,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; @@ -509,6 +539,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 ); @@ -547,15 +578,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; @@ -589,13 +620,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 ); @@ -611,74 +642,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 ) @@ -709,9 +711,9 @@ void ImageVisual::UploadCompleted() void ImageVisual::DoSetOnStage( Actor& actor ) { - if( !mImageUrl.empty() ) + if( mImageUrl.IsValid() ) { - InitializeRenderer( mImageUrl ); + InitializeRenderer(); } else if ( mImage ) { @@ -725,6 +727,14 @@ void ImageVisual::DoSetOnStage( Actor& actor ) mPlacementActor = actor; + // Search the Actor tree to find if Layer UI behaviour set. + Layer layer = actor.GetLayer(); + if ( layer && layer.GetBehavior() == Layer::LAYER_3D ) + { + // Layer 3D set, do not align pixels + mImpl->mRenderer.RegisterProperty( PIXEL_ALIGNED_UNIFORM_NAME, PIXEL_ALIGN_OFF ); + } + if( mPixelArea != FULL_TEXTURE_RECT ) { mImpl->mRenderer.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, mPixelArea ); @@ -743,9 +753,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(); } @@ -760,9 +770,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() ); } @@ -784,6 +794,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() @@ -860,24 +888,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 ); } }