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 9446377..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/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
@@ -41,9 +39,7 @@ 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[] =
   { { "NONE",                     Dali::Shader::Hint::NONE},
@@ -51,14 +47,14 @@ Dali::Scripting::StringEnum ShaderHintsTable[] =
     { "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,70 +89,34 @@ 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
-{
-  return mSceneObject;
-}
-
-SceneGraph::Shader* Shader::GetShaderSceneObject()
-{
-  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)
   {
@@ -164,7 +124,7 @@ 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;
@@ -189,7 +149,7 @@ void Shader::SetDefaultProperty( Property::Index index,
               );
           }
 
-          Initialize(vertex, fragment, hints );
+          SetShader( vertex, fragment, hints );
         }
       }
       else
@@ -201,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;
@@ -232,84 +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 >= 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 ) )
-  {
-    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::Hint::Value 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