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=259b0059ecb70f86dbf0121106cc1978de5d0415;hb=1d71a8f7d7abd7729aa645ad062e530958097214;hpb=2b051f9220d4e28225a57d5dabc98e093357cad6 diff --git a/dali-toolkit/internal/visuals/border/border-visual.cpp b/dali-toolkit/internal/visuals/border/border-visual.cpp index 259b005..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) 2018 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,118 +19,48 @@ #include "border-visual.h" // EXTERNAL INCLUDES +#include #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 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 highp 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 - uniform lowp vec3 mixColor;\n - \n - void main()\n - {\n - gl_FragColor = vec4(mixColor, 1.0)*borderColor*uColor;\n - }\n -); - -const char* VERTEX_SHADER_ANTI_ALIASING = DALI_COMPOSE_SHADER( - attribute mediump vec2 aPosition;\n - attribute mediump vec2 aDrift;\n - uniform highp 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 lowp vec3 mixColor;\n - uniform mediump float borderSize;\n - varying mediump float vAlpha;\n - \n - void main()\n - {\n - gl_FragColor = vec4(mixColor, 1.0)*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 + +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 ) +BorderVisualPtr BorderVisual::New(VisualFactoryCache& factoryCache, const Property::Map& properties) { - BorderVisualPtr borderVisualPtr( new BorderVisual( factoryCache ) ); - borderVisualPtr->SetProperties( properties ); + BorderVisualPtr borderVisualPtr(new BorderVisual(factoryCache)); + borderVisualPtr->SetProperties(properties); + borderVisualPtr->Initialize(); return borderVisualPtr; } -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 ) +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) { } @@ -138,41 +68,41 @@ BorderVisual::~BorderVisual() { } -void BorderVisual::DoSetProperties( const Property::Map& propertyMap ) +void BorderVisual::DoSetProperties(const Property::Map& propertyMap) { - for( Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter ) + for(Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter) { - KeyValuePair keyValue = propertyMap.GetKeyValue( iter ); - if( keyValue.first.type == Property::Key::INDEX ) + KeyValuePair keyValue = propertyMap.GetKeyValue(iter); + if(keyValue.first.type == Property::Key::INDEX) { - DoSetProperty( keyValue.first.indexKey, keyValue.second ); + DoSetProperty(keyValue.first.indexKey, keyValue.second); } else { - if( keyValue.first == COLOR_NAME ) + if(keyValue.first == COLOR_NAME) { - DoSetProperty( Toolkit::BorderVisual::Property::COLOR, keyValue.second ); + DoSetProperty(Toolkit::BorderVisual::Property::COLOR, keyValue.second); } - else if( keyValue.first == SIZE_NAME ) + else if(keyValue.first == SIZE_NAME) { - DoSetProperty( Toolkit::BorderVisual::Property::SIZE, keyValue.second ); + DoSetProperty(Toolkit::BorderVisual::Property::SIZE, keyValue.second); } - else if( keyValue.first == ANTI_ALIASING ) + else if(keyValue.first == ANTI_ALIASING) { - DoSetProperty( Toolkit::BorderVisual::Property::ANTI_ALIASING, keyValue.second ); + DoSetProperty(Toolkit::BorderVisual::Property::ANTI_ALIASING, keyValue.second); } } } } -void BorderVisual::DoSetProperty( Dali::Property::Index index, - const Dali::Property::Value& value ) +void BorderVisual::DoSetProperty(Dali::Property::Index index, + const Dali::Property::Value& value) { - switch( index ) + switch(index) { case Toolkit::BorderVisual::Property::COLOR: { - if( !value.Get( mBorderColor ) ) + if(!value.Get(mBorderColor)) { DALI_LOG_ERROR("BorderVisual: borderColor property has incorrect type\n"); } @@ -180,7 +110,7 @@ void BorderVisual::DoSetProperty( Dali::Property::Index index, } case Toolkit::BorderVisual::Property::SIZE: { - if( !value.Get( mBorderSize ) ) + if(!value.Get(mBorderSize)) { DALI_LOG_ERROR("BorderVisual: borderSize property has incorrect type\n"); } @@ -188,7 +118,7 @@ void BorderVisual::DoSetProperty( Dali::Property::Index index, } case Toolkit::BorderVisual::Property::ANTI_ALIASING: { - if( !value.Get( mAntiAliasing ) ) + if(!value.Get(mAntiAliasing)) { DALI_LOG_ERROR("BorderVisual: antiAliasing property has incorrect type\n"); } @@ -197,80 +127,85 @@ void BorderVisual::DoSetProperty( Dali::Property::Index index, } } -void BorderVisual::DoSetOnStage( Actor& actor ) +void BorderVisual::DoSetOnScene(Actor& actor) { - InitializeRenderer(); - - mBorderColorIndex = DevelHandle::RegisterProperty( mImpl->mRenderer, Toolkit::BorderVisual::Property::COLOR, COLOR_NAME, mBorderColor ); - if( mBorderColor.a < 1.f || mAntiAliasing) + if(mBorderColorIndex == Property::INVALID_INDEX) + { + mBorderColorIndex = mImpl->mRenderer.RegisterUniqueProperty(Toolkit::BorderVisual::Property::COLOR, COLOR_NAME, mBorderColor); + } + if(mBorderColor.a < 1.f || mAntiAliasing) { - mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON ); + 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); } - mBorderSizeIndex = DevelHandle::RegisterProperty( mImpl->mRenderer, Toolkit::BorderVisual::Property::SIZE, SIZE_NAME, mBorderSize ); - actor.AddRenderer( mImpl->mRenderer ); + actor.AddRenderer(mImpl->mRenderer); // Border Visual Generated and ready to display - ResourceReady( Toolkit::Visual::ResourceStatus::READY ); + ResourceReady(Toolkit::Visual::ResourceStatus::READY); } -void BorderVisual::DoCreatePropertyMap( Property::Map& map ) const +void BorderVisual::DoCreatePropertyMap(Property::Map& map) const { 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 ); + 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::DoCreateInstancePropertyMap( Property::Map& map ) const +void BorderVisual::DoCreateInstancePropertyMap(Property::Map& map) const { // Do nothing } void BorderVisual::OnSetTransform() { - if( mImpl->mRenderer ) + if(mImpl->mRenderer) { - mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT ); + mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT); } } -void BorderVisual::InitializeRenderer() +void BorderVisual::OnInitialize() { - Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::BORDER_GEOMETRY ); - if( !geometry ) + Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::BORDER_GEOMETRY); + if(!geometry) { - geometry = CreateBorderGeometry(); - mFactoryCache.SaveGeometry( VisualFactoryCache::BORDER_GEOMETRY, geometry ); + geometry = CreateBorderGeometry(); + mFactoryCache.SaveGeometry(VisualFactoryCache::BORDER_GEOMETRY, geometry); } - Shader shader = GetBorderShader(); - mImpl->mRenderer = Renderer::New( geometry, shader ); + 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 ); + 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); } } @@ -295,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; }