[Tizen] Fixed actor relayout dimension dependencies
[platform/core/uifw/dali-core.git] / dali / internal / event / rendering / shader-impl.cpp
index 9446377..74a8ab9 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016 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.
 #include <dali/internal/event/rendering/shader-impl.h> // Dali::Internal::Shader
 
 // INTERNAL INCLUDES
-#include <dali/public-api/object/type-registry.h>
 #include <dali/devel-api/scripting/scripting.h>
-#include <dali/internal/event/common/object-impl-helper.h> // Dali::Internal::ObjectHelper
 #include <dali/internal/event/common/property-helper.h> // DALI_PROPERTY_TABLE_BEGIN, DALI_PROPERTY, DALI_PROPERTY_TABLE_END
 #include <dali/internal/event/common/thread-local-storage.h>
 #include <dali/internal/event/effects/shader-factory.h>
-#include <dali/internal/event/resources/resource-ticket.h>
 #include <dali/internal/update/manager/update-manager.h>
+#include <dali/public-api/object/type-registry.h>
 
 namespace Dali
 {
 namespace Internal
 {
-
 namespace
 {
-
 /**
  *            |name             |type    |writable|animatable|constraint-input|enum for index-checking|
  */
 DALI_PROPERTY_TABLE_BEGIN
-DALI_PROPERTY( "program",       MAP,     true,     false,     false,  Dali::Shader::Property::PROGRAM )
-DALI_PROPERTY_TABLE_END( DEFAULT_ACTOR_PROPERTY_START_INDEX )
-
-const ObjectImplHelper<DEFAULT_PROPERTY_COUNT> SHADER_IMPL = { DEFAULT_PROPERTY_DETAILS };
+DALI_PROPERTY("program", MAP, true, false, false, Dali::Shader::Property::PROGRAM)
+DALI_PROPERTY_TABLE_END(DEFAULT_ACTOR_PROPERTY_START_INDEX, ShaderDefaultProperties)
 
 Dali::Scripting::StringEnum ShaderHintsTable[] =
-  { { "NONE",                     Dali::Shader::Hint::NONE},
-    { "OUTPUT_IS_TRANSPARENT",    Dali::Shader::Hint::OUTPUT_IS_TRANSPARENT},
-    { "MODIFIES_GEOMETRY",        Dali::Shader::Hint::MODIFIES_GEOMETRY}
-  };
+  {{"NONE", Dali::Shader::Hint::NONE},
+   {"OUTPUT_IS_TRANSPARENT", Dali::Shader::Hint::OUTPUT_IS_TRANSPARENT},
+   {"MODIFIES_GEOMETRY", Dali::Shader::Hint::MODIFIES_GEOMETRY}};
 
-const unsigned int ShaderHintsTableSize = sizeof( ShaderHintsTable ) / sizeof( ShaderHintsTable[0] );
+const uint32_t ShaderHintsTableSize = static_cast<uint32_t>(sizeof(ShaderHintsTable) / sizeof(ShaderHintsTable[0]));
 
 BaseHandle Create()
 {
   return Dali::BaseHandle();
 }
 
-TypeRegistration mType( typeid( Dali::Shader ), typeid( Dali::Handle ), Create );
+TypeRegistration mType(typeid(Dali::Shader), typeid(Dali::Handle), Create, ShaderDefaultProperties);
 
 #define TOKEN_STRING(x) (#x)
 
@@ -93,123 +86,80 @@ Property::Value HintString(const Dali::Shader::Hint::Value& hints)
   return Property::Value(s);
 }
 
-
 } // unnamed namespace
 
-ShaderPtr Shader::New( const std::string& vertexShader,
-                       const std::string& fragmentShader,
-                       Dali::Shader::Hint::Value hints )
-{
-  ShaderPtr shader( new Shader() );
-  shader->Initialize( vertexShader, fragmentShader, hints );
-  return shader;
-}
-
-const SceneGraph::Shader* Shader::GetShaderSceneObject() const
+ShaderPtr Shader::New(std::string_view          vertexShader,
+                      std::string_view          fragmentShader,
+                      Dali::Shader::Hint::Value hints)
 {
-  return mSceneObject;
-}
+  // create scene object first so it's guaranteed to exist for the event side
+  auto                             sceneObject = new SceneGraph::Shader(hints);
+  OwnerPointer<SceneGraph::Shader> transferOwnership(sceneObject);
+  // pass the pointer to base for message passing
+  ShaderPtr shader(new Shader(sceneObject));
+  // transfer scene object ownership to update manager
+  auto&&                     services      = shader->GetEventThreadServices();
+  SceneGraph::UpdateManager& updateManager = services.GetUpdateManager();
+  AddShaderMessage(updateManager, transferOwnership);
 
-SceneGraph::Shader* Shader::GetShaderSceneObject()
-{
-  return mSceneObject;
-}
+  services.RegisterObject(shader.Get());
+  shader->SetShader(vertexShader, fragmentShader, hints);
 
-unsigned int Shader::GetDefaultPropertyCount() const
-{
-  return SHADER_IMPL.GetDefaultPropertyCount();
-}
-
-void Shader::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
-{
-  SHADER_IMPL.GetDefaultPropertyIndices( indices );
-}
-
-const char* Shader::GetDefaultPropertyName(Property::Index index) const
-{
-  return SHADER_IMPL.GetDefaultPropertyName( index );
-}
-
-Property::Index Shader::GetDefaultPropertyIndex( const std::string& name ) const
-{
-  return SHADER_IMPL.GetDefaultPropertyIndex( name );
-}
-
-bool Shader::IsDefaultPropertyWritable( Property::Index index ) const
-{
-  return SHADER_IMPL.IsDefaultPropertyWritable( index );
-}
-
-bool Shader::IsDefaultPropertyAnimatable( Property::Index index ) const
-{
-  return SHADER_IMPL.IsDefaultPropertyAnimatable( index );
-}
-
-bool Shader::IsDefaultPropertyAConstraintInput( Property::Index index ) const
-{
-  return SHADER_IMPL.IsDefaultPropertyAConstraintInput( index );
+  return shader;
 }
 
-Property::Type Shader::GetDefaultPropertyType( Property::Index index ) const
+const SceneGraph::Shader& Shader::GetShaderSceneObject() const
 {
-  return SHADER_IMPL.GetDefaultPropertyType( index );
+  return static_cast<const SceneGraph::Shader&>(GetSceneObject());
 }
 
-void Shader::SetDefaultProperty( Property::Index index,
-                                 const Property::Value& propertyValue )
+void Shader::SetDefaultProperty(Property::Index index, const Property::Value& propertyValue)
 {
   switch(index)
   {
     case Dali::Shader::Property::PROGRAM:
     {
-      if( propertyValue.GetType() == Property::MAP )
+      if(propertyValue.GetType() == Property::MAP)
       {
-        Dali::Property::Map* map = propertyValue.GetMap();
-        if( map )
+        const Dali::Property::Map* map = propertyValue.GetMap();
+        if(map)
         {
-          std::string vertex;
-          std::string fragment;
+          std::string               vertex;
+          std::string               fragment;
           Dali::Shader::Hint::Value hints(Dali::Shader::Hint::NONE);
 
-          if( Property::Value* value = map->Find("vertex") )
+          if(Property::Value* value = map->Find("vertex"))
           {
             vertex = value->Get<std::string>();
           }
 
-          if( Property::Value* value = map->Find("fragment") )
+          if(Property::Value* value = map->Find("fragment"))
           {
             fragment = value->Get<std::string>();
           }
 
-          if( Property::Value* value = map->Find("hints") )
+          if(Property::Value* value = map->Find("hints"))
           {
             static_cast<void>( // ignore return
-              Scripting::GetEnumeration< Dali::Shader::Hint::Value >(value->Get<std::string>().c_str(),
-                                                                     ShaderHintsTable, ShaderHintsTableSize, hints)
-              );
+              Scripting::GetEnumeration<Dali::Shader::Hint::Value>(value->Get<std::string>().c_str(),
+                                                                   ShaderHintsTable,
+                                                                   ShaderHintsTableSize,
+                                                                   hints));
           }
 
-          Initialize(vertex, fragment, hints );
+          SetShader(vertex, fragment, hints);
         }
       }
       else
       {
-        DALI_LOG_WARNING( "Shader program property should be a map\n" );
+        DALI_LOG_WARNING("Shader program property should be a map\n");
       }
       break;
     }
   }
 }
 
-void Shader::SetSceneGraphProperty( Property::Index index,
-                                    const PropertyMetadata& entry,
-                                    const Property::Value& value )
-{
-  SHADER_IMPL.SetSceneGraphProperty( GetEventThreadServices(), this, index, entry, value );
-  OnPropertySet(index, value);
-}
-
-Property::Value Shader::GetDefaultProperty( Property::Index index ) const
+Property::Value Shader::GetDefaultProperty(Property::Index index) const
 {
   Property::Value value;
 
@@ -218,11 +168,11 @@ Property::Value Shader::GetDefaultProperty( Property::Index index ) const
     case Dali::Shader::Property::PROGRAM:
     {
       Dali::Property::Map map;
-      if( mShaderData )
+      if(mShaderData)
       {
-        map["vertex"] = Property::Value(mShaderData->GetVertexShader());
+        map["vertex"]   = Property::Value(mShaderData->GetVertexShader());
         map["fragment"] = Property::Value(mShaderData->GetFragmentShader());
-        map["hints"] = HintString(mShaderData->GetHints());
+        map["hints"]    = HintString(mShaderData->GetHints());
       }
       value = map;
       break;
@@ -232,98 +182,61 @@ Property::Value Shader::GetDefaultProperty( Property::Index index ) const
   return value;
 }
 
-const SceneGraph::PropertyOwner* Shader::GetPropertyOwner() const
+Property::Value Shader::GetDefaultPropertyCurrentValue(Property::Index index) const
 {
-  return mSceneObject;
+  return GetDefaultProperty(index); // Event-side only properties
 }
 
-const SceneGraph::PropertyOwner* Shader::GetSceneObject() const
+Shader::Shader(const SceneGraph::Shader* sceneObject)
+: Object(sceneObject),
+  mShaderData(nullptr)
 {
-  return mSceneObject;
 }
 
-const SceneGraph::PropertyBase* Shader::GetSceneObjectAnimatableProperty( Property::Index index ) const
+void Shader::SetShader(std::string_view          vertexSource,
+                       std::string_view          fragmentSource,
+                       Dali::Shader::Hint::Value hints)
 {
-  DALI_ASSERT_ALWAYS( IsPropertyAnimatable( index ) && "Property is not animatable" );
-  const SceneGraph::PropertyBase* property = NULL;
-
-  property = SHADER_IMPL.GetRegisteredSceneGraphProperty( this,
-                                                          &Shader::FindAnimatableProperty,
-                                                          &Shader::FindCustomProperty,
-                                                          index );
+  // Try to load a pre-compiled shader binary for the source pair:
+  ThreadLocalStorage& tls           = ThreadLocalStorage::Get();
+  ShaderFactory&      shaderFactory = tls.GetShaderFactory();
+  size_t              shaderHash;
+  mShaderData = shaderFactory.Load(vertexSource, fragmentSource, hints, shaderHash);
 
-  if( property == NULL && index < DEFAULT_PROPERTY_MAX_COUNT )
-  {
-    DALI_ASSERT_ALWAYS( 0 && "Property is not animatable" );
-  }
+  // Add shader data to scene-object
 
-  return property;
+  SceneGraph::SetShaderDataMessage(GetEventThreadServices(), GetShaderSceneObject(), mShaderData);
 }
 
-const PropertyInputImpl* Shader::GetSceneObjectInputProperty( Property::Index index ) const
+Shader::~Shader()
 {
-  PropertyMetadata* property = NULL;
-
-  if( ( index >= CHILD_PROPERTY_REGISTRATION_START_INDEX ) && // Child properties are also stored as custom properties
-      ( index <= PROPERTY_CUSTOM_MAX_INDEX ) )
-  {
-    property = FindCustomProperty( index );
-  }
-  else if( ( index >= ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX ) && ( index <= ANIMATABLE_PROPERTY_REGISTRATION_MAX_INDEX ) )
+  if(EventThreadServices::IsCoreRunning())
   {
-    property = FindAnimatableProperty( index );
-  }
+    EventThreadServices&       eventThreadServices = GetEventThreadServices();
+    SceneGraph::UpdateManager& updateManager       = eventThreadServices.GetUpdateManager();
+    RemoveShaderMessage(updateManager, &GetShaderSceneObject());
 
-  DALI_ASSERT_ALWAYS( property && "property index is invalid" );
-  return property->GetSceneGraphProperty();
-}
-
-int Shader::GetPropertyComponentIndex( Property::Index index ) const
-{
-  return Property::INVALID_COMPONENT_INDEX;
+    eventThreadServices.UnregisterObject(this);
+  }
 }
 
-Shader::Shader()
-  : mSceneObject( NULL ),
-    mShaderData( NULL )
+std::string Shader::GetShaderVersionPrefix()
 {
+  Dali::Internal::ThreadLocalStorage& tls = Dali::Internal::ThreadLocalStorage::Get();
+  return tls.GetShaderVersionPrefix();
 }
 
-void Shader::Initialize(
-  const std::string& vertexSource,
-  const std::string& fragmentSource,
-  Dali::Shader::Hint::Value hints )
+std::string Shader::GetVertexShaderPrefix()
 {
-  EventThreadServices& eventThreadServices = GetEventThreadServices();
-  SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
-  mSceneObject = new SceneGraph::Shader( hints );
-
-  // Add to update manager
-  AddShaderMessage( updateManager, *mSceneObject );
-
-  // Try to load a precompiled shader binary for the source pair:
-  ThreadLocalStorage& tls = ThreadLocalStorage::Get();
-  ShaderFactory& shaderFactory = tls.GetShaderFactory();
-  size_t shaderHash;
-  mShaderData = shaderFactory.Load( vertexSource, fragmentSource, hints, shaderHash );
-
-  // Add shader program to scene-object using a message to the UpdateManager
-  SetShaderProgramMessage( updateManager, *mSceneObject, mShaderData, (hints & Dali::Shader::Hint::MODIFIES_GEOMETRY) != 0x0 );
-  eventThreadServices.RegisterObject( this );
+  Dali::Internal::ThreadLocalStorage& tls = Dali::Internal::ThreadLocalStorage::Get();
+  return tls.GetVertexShaderPrefix();
 }
 
-Shader::~Shader()
+std::string Shader::GetFragmentShaderPrefix()
 {
-  if( EventThreadServices::IsCoreRunning() )
-  {
-    EventThreadServices& eventThreadServices = GetEventThreadServices();
-    SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
-    RemoveShaderMessage( updateManager, *mSceneObject);
-
-    eventThreadServices.UnregisterObject( this );
-  }
+  Dali::Internal::ThreadLocalStorage& tls = Dali::Internal::ThreadLocalStorage::Get();
+  return tls.GetFragmentShaderPrefix();
 }
 
-
 } // namespace Internal
 } // namespace Dali