X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git;a=blobdiff_plain;f=dali-toolkit%2Finternal%2Fvisuals%2Fborder%2Fborder-visual.cpp;h=b60cc276b093437cd18b360b1b355300ba7c3782;hp=eecaa302a16f162cad2b004dcf112559365d4cf7;hb=1d71a8f7d7abd7729aa645ad062e530958097214;hpb=ddf213d6be29c945105fdeba076ba7ce98acd9ba diff --git a/dali-toolkit/internal/visuals/border/border-visual.cpp b/dali-toolkit/internal/visuals/border/border-visual.cpp index eecaa30..b60cc27 100644 --- a/dali-toolkit/internal/visuals/border/border-visual.cpp +++ b/dali-toolkit/internal/visuals/border/border-visual.cpp @@ -1,5 +1,5 @@ /* - * 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. @@ -19,117 +19,48 @@ #include "border-visual.h" // EXTERNAL INCLUDES +#include #include // INTERNAL INCLUDES -#include -#include -#include +#include +#include #include +#include #include -#include +#include +#include namespace Dali { - namespace Toolkit { - namespace Internal { - namespace { -const char * const COLOR_NAME("borderColor"); -const char * const SIZE_NAME("borderSize"); -const char * const ANTI_ALIASING("antiAliasing"); - -const char * const POSITION_ATTRIBUTE_NAME("aPosition"); -const char * const DRIFT_ATTRIBUTE_NAME("aDrift"); -const char * const INDEX_NAME("indices"); - - -const char* VERTEX_SHADER = DALI_COMPOSE_SHADER( - attribute mediump vec2 aPosition;\n - attribute mediump vec2 aDrift;\n - uniform mediump mat4 uMvpMatrix;\n - uniform mediump vec3 uSize;\n - uniform mediump float borderSize;\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 - - vec2 ComputeVertexPosition()\n - {\n - vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n - vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n - return (aPosition + anchorPoint)*visualSize + (visualOffset + origin)*uSize.xy;\n - }\n - - void main()\n - {\n - vec2 position = ComputeVertexPosition() + aDrift*borderSize;\n - gl_Position = uMvpMatrix * vec4(position, 0.0, 1.0);\n - }\n -); - -const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER( - uniform lowp vec4 uColor;\n - uniform lowp vec4 borderColor;\n - \n - void main()\n - {\n - gl_FragColor = borderColor*uColor;\n - }\n -); - -const char* VERTEX_SHADER_ANTI_ALIASING = DALI_COMPOSE_SHADER( - attribute mediump vec2 aPosition;\n - attribute mediump vec2 aDrift;\n - uniform mediump mat4 uMvpMatrix;\n - uniform mediump vec3 uSize;\n - uniform mediump float borderSize;\n - varying mediump float vAlpha;\n - \n - void main()\n - {\n - vec2 position = aPosition*(uSize.xy+vec2(0.75)) + aDrift*(borderSize+1.5);\n - gl_Position = uMvpMatrix * vec4(position, 0.0, 1.0);\n - vAlpha = min( abs(aDrift.x), abs(aDrift.y) )*(borderSize+1.5); - }\n -); - -const char* FRAGMENT_SHADER_ANTI_ALIASING = DALI_COMPOSE_SHADER( - uniform lowp vec4 uColor;\n - uniform lowp vec4 borderColor;\n - uniform mediump float borderSize;\n - varying mediump float vAlpha;\n - \n - void main()\n - {\n - gl_FragColor = borderColor*uColor;\n - gl_FragColor.a *= smoothstep(0.0, 1.5, vAlpha)*smoothstep( borderSize+1.5, borderSize, vAlpha );\n - }\n -); -} +const int CUSTOM_PROPERTY_COUNT(7); // 5 transform properties + color,size -BorderVisualPtr BorderVisual::New( VisualFactoryCache& factoryCache ) +const char* const POSITION_ATTRIBUTE_NAME("aPosition"); +const char* const DRIFT_ATTRIBUTE_NAME("aDrift"); +const char* const INDEX_NAME("indices"); +} // namespace + +BorderVisualPtr BorderVisual::New(VisualFactoryCache& factoryCache, const Property::Map& properties) { - return new BorderVisual( factoryCache ); + BorderVisualPtr borderVisualPtr(new BorderVisual(factoryCache)); + borderVisualPtr->SetProperties(properties); + borderVisualPtr->Initialize(); + return borderVisualPtr; } -BorderVisual::BorderVisual( VisualFactoryCache& factoryCache ) -: Visual::Base( factoryCache ), - mBorderColor( Color::TRANSPARENT ), - mBorderSize( 0.f ), - mBorderColorIndex( Property::INVALID_INDEX ), - mBorderSizeIndex( Property::INVALID_INDEX ), - mAntiAliasing( false ) +BorderVisual::BorderVisual(VisualFactoryCache& factoryCache) +: Visual::Base(factoryCache, Visual::FittingMode::FILL, Toolkit::Visual::BORDER), + mBorderColor(Color::TRANSPARENT), + mBorderSize(0.f), + mBorderColorIndex(Property::INVALID_INDEX), + mBorderSizeIndex(Property::INVALID_INDEX), + mAntiAliasing(false) { } @@ -137,136 +68,144 @@ BorderVisual::~BorderVisual() { } -void BorderVisual::DoSetProperties( const Property::Map& propertyMap ) +void BorderVisual::DoSetProperties(const Property::Map& propertyMap) { - Property::Value* color = propertyMap.Find( Toolkit::BorderVisual::Property::COLOR, COLOR_NAME ); - if( !( color && color->Get(mBorderColor) ) ) + for(Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter) { - DALI_LOG_ERROR( "Fail to provide a border color to the BorderVisual object\n" ); - } - - Property::Value* size = propertyMap.Find( Toolkit::BorderVisual::Property::SIZE, SIZE_NAME ); - if( !( size && size->Get(mBorderSize) ) ) - { - DALI_LOG_ERROR( "Fail to provide a border size to the BorderVisual object\n" ); - } - - Property::Value* antiAliasing = propertyMap.Find( Toolkit::BorderVisual::Property::ANTI_ALIASING, ANTI_ALIASING ); - if( antiAliasing ) - { - antiAliasing->Get( mAntiAliasing ); + KeyValuePair keyValue = propertyMap.GetKeyValue(iter); + if(keyValue.first.type == Property::Key::INDEX) + { + DoSetProperty(keyValue.first.indexKey, keyValue.second); + } + else + { + if(keyValue.first == COLOR_NAME) + { + DoSetProperty(Toolkit::BorderVisual::Property::COLOR, keyValue.second); + } + else if(keyValue.first == SIZE_NAME) + { + DoSetProperty(Toolkit::BorderVisual::Property::SIZE, keyValue.second); + } + else if(keyValue.first == ANTI_ALIASING) + { + DoSetProperty(Toolkit::BorderVisual::Property::ANTI_ALIASING, keyValue.second); + } + } } } -void BorderVisual::DoSetOnStage( Actor& actor ) +void BorderVisual::DoSetProperty(Dali::Property::Index index, + const Dali::Property::Value& value) { - InitializeRenderer(); - - mBorderColorIndex = (mImpl->mRenderer).RegisterProperty( Toolkit::BorderVisual::Property::COLOR, COLOR_NAME, mBorderColor ); - if( mBorderColor.a < 1.f || mAntiAliasing) + switch(index) { - mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON ); + case Toolkit::BorderVisual::Property::COLOR: + { + if(!value.Get(mBorderColor)) + { + DALI_LOG_ERROR("BorderVisual: borderColor property has incorrect type\n"); + } + break; + } + case Toolkit::BorderVisual::Property::SIZE: + { + if(!value.Get(mBorderSize)) + { + DALI_LOG_ERROR("BorderVisual: borderSize property has incorrect type\n"); + } + break; + } + case Toolkit::BorderVisual::Property::ANTI_ALIASING: + { + if(!value.Get(mAntiAliasing)) + { + DALI_LOG_ERROR("BorderVisual: antiAliasing property has incorrect type\n"); + } + break; + } } - mBorderSizeIndex = (mImpl->mRenderer).RegisterProperty( Toolkit::BorderVisual::Property::SIZE, SIZE_NAME, mBorderSize ); - - actor.AddRenderer( mImpl->mRenderer ); } -void BorderVisual::DoCreatePropertyMap( Property::Map& map ) const -{ - map.Clear(); - map.Insert( VisualProperty::TYPE, Toolkit::Visual::BORDER ); - map.Insert( Toolkit::BorderVisual::Property::COLOR, mBorderColor ); - map.Insert( Toolkit::BorderVisual::Property::SIZE, mBorderSize ); - map.Insert( Toolkit::BorderVisual::Property::ANTI_ALIASING, mAntiAliasing ); -} - -void BorderVisual::OnSetTransform() +void BorderVisual::DoSetOnScene(Actor& actor) { - if( mImpl->mRenderer ) + if(mBorderColorIndex == Property::INVALID_INDEX) { - mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT ); + mBorderColorIndex = mImpl->mRenderer.RegisterUniqueProperty(Toolkit::BorderVisual::Property::COLOR, COLOR_NAME, mBorderColor); } -} - -void BorderVisual::InitializeRenderer() -{ - Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::BORDER_GEOMETRY ); - if( !geometry ) + if(mBorderColor.a < 1.f || mAntiAliasing) { - geometry = CreateBorderGeometry(); - mFactoryCache.SaveGeometry( VisualFactoryCache::BORDER_GEOMETRY, geometry ); + mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON); + } + if(mBorderSizeIndex == Property::INVALID_INDEX) + { + mBorderSizeIndex = mImpl->mRenderer.RegisterUniqueProperty(Toolkit::BorderVisual::Property::SIZE, SIZE_NAME, mBorderSize); } + actor.AddRenderer(mImpl->mRenderer); - Shader shader = GetBorderShader(); - mImpl->mRenderer = Renderer::New( geometry, shader ); - - //Register transform properties - mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT ); - + // Border Visual Generated and ready to display + ResourceReady(Toolkit::Visual::ResourceStatus::READY); } -void BorderVisual::SetBorderColor(const Vector4& color) +void BorderVisual::DoCreatePropertyMap(Property::Map& map) const { - mBorderColor = color; - - if( mImpl->mRenderer ) - { - (mImpl->mRenderer).SetProperty( mBorderColorIndex, color ); - if( color.a < 1.f ) - { - mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON ); - } - } + map.Clear(); + map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::BORDER); + map.Insert(Toolkit::BorderVisual::Property::COLOR, mBorderColor); + map.Insert(Toolkit::BorderVisual::Property::SIZE, mBorderSize); + map.Insert(Toolkit::BorderVisual::Property::ANTI_ALIASING, mAntiAliasing); } -void BorderVisual::SetBorderSize( float size ) +void BorderVisual::DoCreateInstancePropertyMap(Property::Map& map) const { - mBorderSize = size; + // Do nothing +} - if( mImpl->mRenderer ) +void BorderVisual::OnSetTransform() +{ + if(mImpl->mRenderer) { - (mImpl->mRenderer).SetProperty( mBorderSizeIndex, size ); + mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT); } } -void BorderVisual::RequireAntiAliasing( bool antiAliasing ) +void BorderVisual::OnInitialize() { - if( mAntiAliasing != antiAliasing ) + Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::BORDER_GEOMETRY); + if(!geometry) { - mAntiAliasing = antiAliasing; - if( mImpl->mRenderer ) - { - Shader borderShader( GetBorderShader() ); - mImpl->mRenderer.SetShader( borderShader ); - if( mAntiAliasing ) - { - mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON ); - } - } + geometry = CreateBorderGeometry(); + mFactoryCache.SaveGeometry(VisualFactoryCache::BORDER_GEOMETRY, geometry); } + + Shader shader = GetBorderShader(); + mImpl->mRenderer = Renderer::New(geometry, shader); + mImpl->mRenderer.ReserveCustomProperties(CUSTOM_PROPERTY_COUNT); + + //Register transform properties + mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT); } Shader BorderVisual::GetBorderShader() { Shader shader; - if( mAntiAliasing ) + if(mAntiAliasing) { - shader = mFactoryCache.GetShader( VisualFactoryCache::BORDER_SHADER_ANTI_ALIASING ); - if( !shader ) + shader = mFactoryCache.GetShader(VisualFactoryCache::BORDER_SHADER_ANTI_ALIASING); + if(!shader) { - shader = Shader::New( VERTEX_SHADER_ANTI_ALIASING, FRAGMENT_SHADER_ANTI_ALIASING ); - mFactoryCache.SaveShader( VisualFactoryCache::BORDER_SHADER_ANTI_ALIASING, shader ); + shader = Shader::New(Dali::Shader::GetVertexShaderPrefix() + SHADER_BORDER_VISUAL_ANTI_ALIASING_SHADER_VERT.data(), Dali::Shader::GetFragmentShaderPrefix() + SHADER_BORDER_VISUAL_ANTI_ALIASING_SHADER_FRAG.data()); + mFactoryCache.SaveShader(VisualFactoryCache::BORDER_SHADER_ANTI_ALIASING, shader); } } else { - shader = mFactoryCache.GetShader( VisualFactoryCache::BORDER_SHADER ); - if( !shader ) + shader = mFactoryCache.GetShader(VisualFactoryCache::BORDER_SHADER); + if(!shader) { - shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER ); - mFactoryCache.SaveShader( VisualFactoryCache::BORDER_SHADER, shader ); + shader = Shader::New(Dali::Shader::GetVertexShaderPrefix() + SHADER_BORDER_VISUAL_SHADER_VERT.data(), Dali::Shader::GetFragmentShaderPrefix() + SHADER_BORDER_VISUAL_SHADER_FRAG.data()); + mFactoryCache.SaveShader(VisualFactoryCache::BORDER_SHADER, shader); } } @@ -291,46 +230,50 @@ Shader BorderVisual::GetBorderShader() */ Geometry BorderVisual::CreateBorderGeometry() { - const float halfWidth = 0.5f; + const float halfWidth = 0.5f; const float halfHeight = 0.5f; - struct BorderVertex { Vector2 position; Vector2 drift;}; - BorderVertex borderVertexData[16] = + struct BorderVertex { - { Vector2(-halfWidth, -halfHeight), Vector2(0.f, 0.f) }, - { Vector2(-halfWidth, -halfHeight), Vector2(1.f, 0.f) }, - { Vector2(halfWidth, -halfHeight), Vector2(-1.f, 0.f) }, - { Vector2(halfWidth, -halfHeight), Vector2(0.f, 0.f) }, - - { Vector2(-halfWidth, -halfHeight), Vector2(0.f, 1.f) }, - { Vector2(-halfWidth, -halfHeight), Vector2(1.f, 1.f) }, - { Vector2(halfWidth, -halfHeight), Vector2(-1.f, 1.f) }, - { Vector2(halfWidth, -halfHeight), Vector2(0.f, 1.f) }, - - { Vector2(-halfWidth, halfHeight), Vector2(0.f, -1.f) }, - { Vector2(-halfWidth, halfHeight), Vector2(1.f, -1.f) }, - { Vector2(halfWidth, halfHeight), Vector2(-1.f, -1.f) }, - { Vector2(halfWidth, halfHeight), Vector2(0.f, -1.f) }, - - { Vector2(-halfWidth, halfHeight), Vector2(0.f, 0.f) }, - { Vector2(-halfWidth, halfHeight), Vector2(1.f, 0.f) }, - { Vector2(halfWidth, halfHeight), Vector2(-1.f, 0.f) }, - { Vector2(halfWidth, halfHeight), Vector2(0.f, 0.f) }, + Vector2 position; + Vector2 drift; }; + BorderVertex borderVertexData[16] = + { + {Vector2(-halfWidth, -halfHeight), Vector2(0.f, 0.f)}, + {Vector2(-halfWidth, -halfHeight), Vector2(1.f, 0.f)}, + {Vector2(halfWidth, -halfHeight), Vector2(-1.f, 0.f)}, + {Vector2(halfWidth, -halfHeight), Vector2(0.f, 0.f)}, + + {Vector2(-halfWidth, -halfHeight), Vector2(0.f, 1.f)}, + {Vector2(-halfWidth, -halfHeight), Vector2(1.f, 1.f)}, + {Vector2(halfWidth, -halfHeight), Vector2(-1.f, 1.f)}, + {Vector2(halfWidth, -halfHeight), Vector2(0.f, 1.f)}, + + {Vector2(-halfWidth, halfHeight), Vector2(0.f, -1.f)}, + {Vector2(-halfWidth, halfHeight), Vector2(1.f, -1.f)}, + {Vector2(halfWidth, halfHeight), Vector2(-1.f, -1.f)}, + {Vector2(halfWidth, halfHeight), Vector2(0.f, -1.f)}, + + {Vector2(-halfWidth, halfHeight), Vector2(0.f, 0.f)}, + {Vector2(-halfWidth, halfHeight), Vector2(1.f, 0.f)}, + {Vector2(halfWidth, halfHeight), Vector2(-1.f, 0.f)}, + {Vector2(halfWidth, halfHeight), Vector2(0.f, 0.f)}, + }; Property::Map borderVertexFormat; borderVertexFormat[POSITION_ATTRIBUTE_NAME] = Property::VECTOR2; - borderVertexFormat[DRIFT_ATTRIBUTE_NAME] = Property::VECTOR2; - PropertyBuffer borderVertices = PropertyBuffer::New( borderVertexFormat ); - borderVertices.SetData( borderVertexData, 16 ); + borderVertexFormat[DRIFT_ATTRIBUTE_NAME] = Property::VECTOR2; + VertexBuffer borderVertices = VertexBuffer::New(borderVertexFormat); + borderVertices.SetData(borderVertexData, 16); // Create indices - unsigned short indexData[24] = { 1,5,2,6,3,7,7,6,11,10,15,14,14,10,13,9,12,8,8,9,4,5,0,1}; + unsigned short indexData[24] = {1, 5, 2, 6, 3, 7, 7, 6, 11, 10, 15, 14, 14, 10, 13, 9, 12, 8, 8, 9, 4, 5, 0, 1}; // Create the geometry object Geometry geometry = Geometry::New(); - geometry.AddVertexBuffer( borderVertices ); - geometry.SetIndexBuffer( indexData, sizeof(indexData)/sizeof(indexData[0]) ); - geometry.SetType( Geometry::TRIANGLE_STRIP ); + geometry.AddVertexBuffer(borderVertices); + geometry.SetIndexBuffer(indexData, sizeof(indexData) / sizeof(indexData[0])); + geometry.SetType(Geometry::TRIANGLE_STRIP); return geometry; }