Create Renderer when the Visual is created
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / npatch / npatch-visual.cpp
index 6dcf020..ccee97d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2020 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2021 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.
 
 // INTERNAL INCLUDES
 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
-#include <dali-toolkit/public-api/visuals/visual-properties.h>
+#include <dali-toolkit/internal/graphics/builtin-shader-extern-gen.h>
+#include <dali-toolkit/internal/visuals/image-visual-shader-factory.h>
 #include <dali-toolkit/internal/visuals/npatch-loader.h>
-#include <dali-toolkit/internal/visuals/visual-factory-impl.h>
+#include <dali-toolkit/internal/visuals/rendering-addon.h>
+#include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
+#include <dali-toolkit/internal/visuals/visual-base-impl.h>
 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
+#include <dali-toolkit/internal/visuals/visual-factory-impl.h>
 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
-#include <dali-toolkit/internal/visuals/visual-base-impl.h>
-#include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
-#include <dali-toolkit/internal/visuals/rendering-addon.h>
+#include <dali-toolkit/public-api/visuals/visual-properties.h>
 
 namespace Dali
 {
@@ -45,128 +47,6 @@ namespace Internal
 
 namespace
 {
-const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
-  attribute mediump vec2 aPosition;\n
-  varying mediump vec2 vTexCoord;\n
-  varying mediump vec2 vMaskTexCoord;\n
-  uniform highp   mat4 uMvpMatrix;\n
-  uniform highp   vec3 uSize;\n
-  uniform mediump vec2 uNinePatchFactorsX[ FACTOR_SIZE_X ];\n
-  uniform mediump vec2 uNinePatchFactorsY[ FACTOR_SIZE_Y ];\n
-  \n
-
-  // Visual size and offset
-  uniform mediump vec2 offset;\n
-  uniform highp   vec2 size;\n
-  uniform mediump vec4 offsetSizeMode;\n
-  uniform mediump vec2 origin;\n
-  uniform mediump vec2 anchorPoint;\n
-  uniform mediump vec2 extraSize;\n
-
-  void main()\n
-  {\n
-    mediump vec2 fixedFactor  = vec2( uNinePatchFactorsX[ int( ( aPosition.x + 1.0 ) * 0.5 ) ].x, uNinePatchFactorsY[ int( ( aPosition.y + 1.0 ) * 0.5 ) ].x );\n
-    mediump vec2 stretch      = vec2( uNinePatchFactorsX[ int( ( aPosition.x       ) * 0.5 ) ].y, uNinePatchFactorsY[ int( ( aPosition.y       ) * 0.5 ) ].y );\n
-    \n
-    mediump vec2 fixedTotal   = vec2( uNinePatchFactorsX[ FACTOR_SIZE_X - 1 ].x, uNinePatchFactorsY[ FACTOR_SIZE_Y - 1 ].x );\n
-    mediump vec2 stretchTotal = vec2( uNinePatchFactorsX[ FACTOR_SIZE_X - 1 ].y, uNinePatchFactorsY[ FACTOR_SIZE_Y - 1 ].y );\n
-    \n
-    vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw ) + extraSize;\n
-    vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
-    \n
-    mediump vec4 gridPosition = vec4( fixedFactor + ( visualSize.xy - fixedTotal ) * stretch / stretchTotal, 0.0, 1.0 );\n
-    mediump vec4 vertexPosition = gridPosition;\n
-    vertexPosition.xy -= visualSize.xy * vec2( 0.5, 0.5 );\n
-    vertexPosition.xy += anchorPoint*visualSize + (visualOffset + origin)*uSize.xy;\n
-    vertexPosition = uMvpMatrix * vertexPosition;\n
-    \n
-    vTexCoord = ( fixedFactor + stretch ) / ( fixedTotal + stretchTotal );\n
-    vMaskTexCoord = gridPosition.xy / visualSize;\n
-    \n
-    gl_Position = vertexPosition;\n
-  }\n
-);
-
-const char* VERTEX_SHADER_3X3 = DALI_COMPOSE_SHADER(
-    attribute mediump vec2 aPosition;\n
-    varying mediump vec2 vTexCoord;\n
-    varying mediump vec2 vMaskTexCoord;\n
-    uniform highp   mat4 uMvpMatrix;\n
-    uniform highp   vec3 uSize;\n
-    uniform mediump vec2 uFixed[ 3 ];\n
-    uniform mediump vec2 uStretchTotal;\n
-    \n
-    //Visual size and offset
-    uniform mediump vec2 offset;\n
-    uniform highp   vec2 size;\n
-    uniform mediump vec4 offsetSizeMode;\n
-    uniform mediump vec2 origin;\n
-    uniform mediump vec2 anchorPoint;\n
-    uniform mediump vec2 extraSize;\n
-    \n
-    void main()\n
-    {\n
-      vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw ) + extraSize;\n
-      vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
-      \n
-      mediump vec2 size         = visualSize.xy;\n
-      \n
-      mediump vec2 fixedFactor  = vec2( uFixed[ int( ( aPosition.x + 1.0 ) * 0.5 ) ].x, uFixed[ int( ( aPosition.y  + 1.0 ) * 0.5 ) ].y );\n
-      mediump vec2 stretch      = floor( aPosition * 0.5 );\n
-      mediump vec2 fixedTotal   = uFixed[ 2 ];\n
-      \n
-      mediump vec4 gridPosition = vec4( fixedFactor + ( size - fixedTotal ) * stretch, 0.0, 1.0 );\n
-      mediump vec4 vertexPosition = gridPosition;\n
-      vertexPosition.xy -= size * vec2( 0.5, 0.5 );\n
-      vertexPosition.xy += anchorPoint*size + (visualOffset + origin)*uSize.xy;\n
-      \n
-      vertexPosition = uMvpMatrix * vertexPosition;\n
-      \n
-      vTexCoord = ( fixedFactor + stretch * uStretchTotal ) / ( fixedTotal + uStretchTotal );\n
-      \n
-      vMaskTexCoord = gridPosition.xy / size;\n
-      gl_Position = vertexPosition;\n
-    }\n
-);
-
-const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
-  varying mediump vec2 vTexCoord;\n
-  uniform sampler2D sTexture;\n
-  uniform lowp vec4 uColor;\n
-  uniform lowp vec3 mixColor;\n
-  uniform lowp float preMultipliedAlpha;\n
-  \n
-  void main()\n
-  {\n
-    gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor * vec4( mixColor, 1.0 );\n
-  }\n
-);
-
-const char* FRAGMENT_MASK_SHADER = DALI_COMPOSE_SHADER(
-  varying mediump vec2 vTexCoord;\n
-  varying mediump vec2 vMaskTexCoord;\n
-  uniform sampler2D sTexture;\n
-  uniform sampler2D sMask;\n
-  uniform lowp vec4 uColor;\n
-  uniform lowp vec3 mixColor;\n
-  uniform lowp float preMultipliedAlpha;\n
-  uniform mediump float auxiliaryImageAlpha;\n
-  \n
-  void main()\n
-  {\n
-      // Where mask image is transparent, all of background image must show through.
-      // where mask image is opaque, only mask should be shown
-      // where mask is translucent, less of background should be shown.
-      // auxiliaryImageAlpha controls how much of mask is visible
-
-      mediump vec4 color = texture2D( sTexture, vTexCoord );\n
-      mediump vec4 mask  = texture2D( sMask, vMaskTexCoord );\n
-
-      mediump vec3 mixedColor = color.rgb * mix( 1.0-mask.a, 1.0, 1.0-auxiliaryImageAlpha)
-                                + mask.rgb*mask.a * auxiliaryImageAlpha;\n
-      gl_FragColor = vec4(mixedColor,1.0) * uColor * vec4( mixColor, 1.0 );\n
-  }\n
-);
 
 /**
  * @brief Creates the geometry formed from the vertices and indices
@@ -255,20 +135,20 @@ void RegisterStretchProperties( Renderer& renderer, const char * uniformName, co
 
 /////////////////NPatchVisual////////////////
 
-NPatchVisualPtr NPatchVisual::New( VisualFactoryCache& factoryCache, const VisualUrl& imageUrl, const Property::Map& properties )
+NPatchVisualPtr NPatchVisual::New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties)
 {
-  NPatchVisualPtr nPatchVisual( new NPatchVisual( factoryCache ) );
+  NPatchVisualPtr nPatchVisual(new NPatchVisual(factoryCache, shaderFactory));
   nPatchVisual->mImageUrl = imageUrl;
   nPatchVisual->SetProperties( properties );
-
+  nPatchVisual->Initialize();
   return nPatchVisual;
 }
 
-NPatchVisualPtr NPatchVisual::New( VisualFactoryCache& factoryCache, const VisualUrl& imageUrl )
+NPatchVisualPtr NPatchVisual::New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl)
 {
-  NPatchVisualPtr nPatchVisual( new NPatchVisual( factoryCache ) );
+  NPatchVisualPtr nPatchVisual(new NPatchVisual(factoryCache, shaderFactory));
   nPatchVisual->mImageUrl = imageUrl;
-
+  nPatchVisual->Initialize();
   return nPatchVisual;
 }
 
@@ -404,7 +284,8 @@ void NPatchVisual::DoSetOnScene( Actor& actor )
     Geometry geometry = CreateGeometry();
     Shader shader = CreateShader();
 
-    mImpl->mRenderer = Renderer::New( geometry, shader );
+    mImpl->mRenderer.SetGeometry(geometry);
+    mImpl->mRenderer.SetShader(shader);
 
     mPlacementActor = actor;
     if( data->GetLoadingState() != NPatchData::LoadingState::LOADING )
@@ -434,7 +315,6 @@ void NPatchVisual::DoSetOffScene( Actor& actor )
   }
 
   actor.RemoveRenderer( mImpl->mRenderer );
-  mImpl->mRenderer.Reset();
   mPlacementActor.Reset();
 }
 
@@ -473,17 +353,18 @@ void NPatchVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
   }
 }
 
-NPatchVisual::NPatchVisual( VisualFactoryCache& factoryCache )
-: Visual::Base( factoryCache, Visual::FittingMode::FILL, Toolkit::Visual::N_PATCH ),
+NPatchVisual::NPatchVisual(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory)
+: Visual::Base(factoryCache, Visual::FittingMode::FILL, Toolkit::Visual::N_PATCH),
   mPlacementActor(),
-  mLoader( factoryCache.GetNPatchLoader() ),
+  mLoader(factoryCache.GetNPatchLoader()),
+  mImageVisualShaderFactory(shaderFactory),
   mImageUrl(),
   mAuxiliaryUrl(),
   mId(NPatchData::INVALID_NPATCH_DATA_ID),
-  mBorderOnly( false ),
+  mBorderOnly(false),
   mBorder(),
-  mAuxiliaryImageAlpha( 0.0f ),
-  mReleasePolicy( Toolkit::ImageVisual::ReleasePolicy::DETACHED )
+  mAuxiliaryImageAlpha(0.0f),
+  mReleasePolicy(Toolkit::ImageVisual::ReleasePolicy::DETACHED)
 {
   EnablePreMultipliedAlpha( mFactoryCache.GetPreMultiplyOnLoad() );
 }
@@ -497,6 +378,18 @@ NPatchVisual::~NPatchVisual()
   }
 }
 
+void NPatchVisual::OnInitialize()
+{
+  // Get basic geometry and shader
+  Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
+  Shader   shader   = mImageVisualShaderFactory.GetShader(mFactoryCache, false, true, false);
+
+  mImpl->mRenderer = Renderer::New(geometry, shader);
+
+  //Register transform properties
+  mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
+}
+
 Geometry NPatchVisual::CreateGeometry()
 {
   Geometry geometry;
@@ -562,8 +455,8 @@ Shader NPatchVisual::CreateShader()
   NPatchUtility::StretchRanges::SizeType xStretchCount = 0;
   NPatchUtility::StretchRanges::SizeType yStretchCount = 0;
 
-  auto fragmentShader = mAuxiliaryPixelBuffer ? FRAGMENT_MASK_SHADER
-                                              : FRAGMENT_SHADER;
+  auto fragmentShader = mAuxiliaryPixelBuffer ? SHADER_NPATCH_VISUAL_MASK_SHADER_FRAG
+                                              : SHADER_NPATCH_VISUAL_SHADER_FRAG;
   auto shaderType = mAuxiliaryPixelBuffer ? VisualFactoryCache::NINE_PATCH_MASK_SHADER
                                           : VisualFactoryCache::NINE_PATCH_SHADER;
 
@@ -582,7 +475,7 @@ Shader NPatchVisual::CreateShader()
       shader = mFactoryCache.GetShader( shaderType );
       if( DALI_UNLIKELY( !shader ) )
       {
-        shader = Shader::New( VERTEX_SHADER_3X3, fragmentShader );
+        shader = Shader::New( SHADER_NPATCH_VISUAL_3X3_SHADER_VERT, fragmentShader );
         // Only cache vanilla 9 patch shaders
         mFactoryCache.SaveShader( shaderType, shader );
       }
@@ -592,7 +485,7 @@ Shader NPatchVisual::CreateShader()
       std::stringstream vertexShader;
       vertexShader << "#define FACTOR_SIZE_X " << xStretchCount + 2 << "\n"
                    << "#define FACTOR_SIZE_Y " << yStretchCount + 2 << "\n"
-                   << VERTEX_SHADER;
+                   << SHADER_NPATCH_VISUAL_SHADER_VERT;
 
       shader = Shader::New( vertexShader.str(), fragmentShader );
     }
@@ -611,7 +504,7 @@ Shader NPatchVisual::CreateShader()
     if( ( xStretchCount == 1 && yStretchCount == 1 ) ||
         ( xStretchCount == 0 && yStretchCount == 0 ) )
     {
-      const char* vertexShader = VERTEX_SHADER_3X3;
+      const char* vertexShader = SHADER_NPATCH_VISUAL_3X3_SHADER_VERT.data();
 
       if( !mImpl->mCustomShader->mVertexShader.empty() )
       {
@@ -624,7 +517,7 @@ Shader NPatchVisual::CreateShader()
       std::stringstream vertexShader;
       vertexShader << "#define FACTOR_SIZE_X " << xStretchCount + 2 << "\n"
                    << "#define FACTOR_SIZE_Y " << yStretchCount + 2 << "\n"
-                   << VERTEX_SHADER;
+                   << SHADER_NPATCH_VISUAL_SHADER_VERT;
 
       shader = Shader::New( vertexShader.str(), fragmentShader, hints );
     }