Merge "Clean up the code to build successfully on macOS" into devel/master
[platform/core/uifw/dali-core.git] / dali / internal / event / rendering / shader-impl.cpp
index e44a983..aa85ac7 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2018 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/public-api/object/type-registry.h>
-#include <dali/public-api/shader-effects/shader-effect.h> // Dali::ShaderEffect::GeometryHints // TODO: MESH_REWORK REMOVE
 #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>
 
 namespace Dali
@@ -42,24 +39,22 @@ namespace
  */
 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_TABLE_END( DEFAULT_ACTOR_PROPERTY_START_INDEX, ShaderDefaultProperties )
 
 Dali::Scripting::StringEnum ShaderHintsTable[] =
-  { { "HINT_NONE",                     Dali::Shader::HINT_NONE},
-    { "HINT_OUTPUT_IS_TRANSPARENT",    Dali::Shader::HINT_OUTPUT_IS_TRANSPARENT},
-    { "HINT_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)
 
@@ -72,92 +67,56 @@ void AppendString(std::string& to, const std::string& append)
   to += append;
 }
 
-Property::Value HintString(const Dali::Shader::ShaderHints& hints)
+Property::Value HintString(const Dali::Shader::Hint::Value& hints)
 {
   std::string s;
 
-  if(hints == Dali::Shader::HINT_NONE)
+  if(hints == Dali::Shader::Hint::NONE)
   {
-    s = "HINT_NONE";
+    s = "NONE";
   }
 
-  if(hints & Dali::Shader::HINT_OUTPUT_IS_TRANSPARENT)
+  if(hints & Dali::Shader::Hint::OUTPUT_IS_TRANSPARENT)
   {
-    AppendString(s, "HINT_OUTPUT_IS_TRANSPARENT");
+    AppendString(s, "OUTPUT_IS_TRANSPARENT");
   }
 
-  if(hints & Dali::Shader::HINT_MODIFIES_GEOMETRY)
+  if(hints & Dali::Shader::Hint::MODIFIES_GEOMETRY)
   {
-    AppendString(s, "HINT_MODIFIES_GEOMETRY");
+    AppendString(s, "MODIFIES_GEOMETRY");
   }
 
   return Property::Value(s);
 }
 
-
 } // unnamed namespace
 
 ShaderPtr Shader::New( const std::string& vertexShader,
                        const std::string& fragmentShader,
-                       Dali::Shader::ShaderHints hints )
-{
-  ShaderPtr shader( new Shader() );
-  shader->Initialize( vertexShader, fragmentShader, hints );
-  return shader;
-}
-
-const SceneGraph::Shader* Shader::GetShaderSceneObject() const
-{
-  return mSceneObject;
-}
-
-SceneGraph::Shader* Shader::GetShaderSceneObject()
+                       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 );
 
-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 );
-}
+  services.RegisterObject( shader.Get() );
+  shader->SetShader( vertexShader, fragmentShader, hints );
 
-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)
   {
@@ -165,12 +124,12 @@ void Shader::SetDefaultProperty( Property::Index index,
     {
       if( propertyValue.GetType() == Property::MAP )
       {
-        Dali::Property::Map* map = propertyValue.GetMap();
+        const Dali::Property::Map* map = propertyValue.GetMap();
         if( map )
         {
           std::string vertex;
           std::string fragment;
-          Dali::Shader::ShaderHints hints(Dali::Shader::HINT_NONE);
+          Dali::Shader::Hint::Value hints(Dali::Shader::Hint::NONE);
 
           if( Property::Value* value = map->Find("vertex") )
           {
@@ -185,12 +144,12 @@ void Shader::SetDefaultProperty( Property::Index index,
           if( Property::Value* value = map->Find("hints") )
           {
             static_cast<void>( // ignore return
-              Scripting::GetEnumeration< Dali::Shader::ShaderHints >(value->Get<std::string>().c_str(),
+              Scripting::GetEnumeration< Dali::Shader::Hint::Value >(value->Get<std::string>().c_str(),
                                                                      ShaderHintsTable, ShaderHintsTableSize, hints)
               );
           }
 
-          Initialize(vertex, fragment, hints );
+          SetShader( vertex, fragment, hints );
         }
       }
       else
@@ -202,14 +161,6 @@ void Shader::SetDefaultProperty( Property::Index index,
   }
 }
 
-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 value;
@@ -233,83 +184,31 @@ 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( const std::string& vertexSource,
+                        const std::string& 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 );
-
-  if( property == NULL && index < DEFAULT_PROPERTY_MAX_COUNT )
-  {
-    DALI_ASSERT_ALWAYS( 0 && "Property is not animatable" );
-  }
-
-  return property;
-}
-
-const PropertyInputImpl* Shader::GetSceneObjectInputProperty( Property::Index index ) const
-{
-  PropertyMetadata* property = NULL;
-
-  if(index >= PROPERTY_CUSTOM_START_INDEX )
-  {
-    property = FindCustomProperty( index );
-  }
-  else
-  {
-    property = FindAnimatableProperty( index );
-  }
-
-  DALI_ASSERT_ALWAYS( property && "property index is invalid" );
-  return property->GetSceneGraphProperty();
-}
-
-int Shader::GetPropertyComponentIndex( Property::Index index ) const
-{
-  return Property::INVALID_COMPONENT_INDEX;
-}
-
-Shader::Shader()
-  : mSceneObject( NULL ),
-    mShaderData( NULL )
-{
-}
-
-void Shader::Initialize(
-  const std::string& vertexSource,
-  const std::string& fragmentSource,
-  Dali::Shader::ShaderHints hints )
-{
-  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:
+  // 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 );
 
   // 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 );
+  EventThreadServices& eventThreadServices = GetEventThreadServices();
+  SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
+  SetShaderProgramMessage( updateManager, GetShaderSceneObject(), mShaderData, (hints & Dali::Shader::Hint::MODIFIES_GEOMETRY) != 0x0 );
 }
 
 Shader::~Shader()
@@ -318,12 +217,29 @@ Shader::~Shader()
   {
     EventThreadServices& eventThreadServices = GetEventThreadServices();
     SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
-    RemoveShaderMessage( updateManager, *mSceneObject);
+    RemoveShaderMessage( updateManager, &GetShaderSceneObject() );
 
     eventThreadServices.UnregisterObject( this );
   }
 }
 
+std::string Shader::GetShaderVersionPrefix()
+{
+  Dali::Internal::ThreadLocalStorage& tls = Dali::Internal::ThreadLocalStorage::Get();
+  return tls.GetShaderVersionPrefix();
+}
+
+std::string Shader::GetVertexShaderPrefix()
+{
+  Dali::Internal::ThreadLocalStorage& tls = Dali::Internal::ThreadLocalStorage::Get();
+  return tls.GetVertexShaderPrefix();
+}
+
+std::string Shader::GetFragmentShaderPrefix()
+{
+  Dali::Internal::ThreadLocalStorage& tls = Dali::Internal::ThreadLocalStorage::Get();
+  return tls.GetFragmentShaderPrefix();
+}
 
 } // namespace Internal
 } // namespace Dali