[Tizen] Detach skybox on setting an empty url
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / internal / controls / scene-view / scene-view-impl.cpp
index a4158e1..0a1e5b0 100644 (file)
@@ -39,6 +39,7 @@
 // INTERNAL INCLUDES
 #include <dali-scene3d/internal/controls/model/model-impl.h>
 #include <dali-scene3d/internal/graphics/builtin-shader-extern-gen.h>
+#include <dali-scene3d/internal/light/light-impl.h>
 
 using namespace Dali;
 
@@ -61,6 +62,7 @@ DALI_TYPE_REGISTRATION_END()
 
 Property::Index   RENDERING_BUFFER    = Dali::Toolkit::Control::CONTROL_PROPERTY_END_INDEX + 1;
 constexpr int32_t DEFAULT_ORIENTATION = 0;
+constexpr int32_t INVALID_INDEX = -1;
 
 static constexpr std::string_view SKYBOX_INTENSITY_STRING = "uIntensity";
 
@@ -273,16 +275,28 @@ void SceneView::SelectCamera(const std::string& name)
   UpdateCamera(GetCamera(name));
 }
 
-void SceneView::RegisterSceneItem(Scene3D::Internal::ImageBasedLightObserver* item)
+void SceneView::RegisterSceneItem(Scene3D::Internal::LightObserver* item)
 {
   if(item)
   {
-    item->NotifyImageBasedLightTexture(mDiffuseTexture, mSpecularTexture, mIblScaleFactor);
+    item->NotifyImageBasedLightTexture(mDiffuseTexture, mSpecularTexture, mIblScaleFactor, mSpecularMipmapLevels);
+
+    if(mActivatedLightCount > 0)
+    {
+      uint32_t maxLightCount = Scene3D::Internal::Light::GetMaximumEnabledLightCount();
+      for(uint32_t i = 0; i < maxLightCount; ++i)
+      {
+        if(mActivatedLights[i])
+        {
+          item->NotifyLightAdded(i, mActivatedLights[i]);
+        }
+      }
+    }
     mItems.push_back(item);
   }
 }
 
-void SceneView::UnregisterSceneItem(Scene3D::Internal::ImageBasedLightObserver* item)
+void SceneView::UnregisterSceneItem(Scene3D::Internal::LightObserver* item)
 {
   if(item)
   {
@@ -290,6 +304,17 @@ void SceneView::UnregisterSceneItem(Scene3D::Internal::ImageBasedLightObserver*
     {
       if(mItems[i] == item)
       {
+        if(mActivatedLightCount > 0)
+        {
+          uint32_t maxLightCount = Scene3D::Internal::Light::GetMaximumEnabledLightCount();
+          for(uint32_t i = 0; i < maxLightCount; ++i)
+          {
+            if(mActivatedLights[i])
+            {
+              item->NotifyLightRemoved(i);
+            }
+          }
+        }
         mItems.erase(mItems.begin() + i);
         break;
       }
@@ -353,6 +378,7 @@ void SceneView::SetImageBasedLightSource(const std::string& diffuseUrl, const st
     mDiffuseTexture.Reset();
     mSpecularTexture.Reset();
 
+    mSpecularMipmapLevels = 1u;
     NotifyImageBasedLightTextureChange();
   }
   else
@@ -411,6 +437,64 @@ float SceneView::GetImageBasedLightScaleFactor() const
   return mIblScaleFactor;
 }
 
+void SceneView::AddLight(Scene3D::Light light)
+{
+  bool enabled = AddLightInternal(light);
+  mLights.push_back(std::make_pair(light, enabled));
+}
+
+void SceneView::RemoveLight(Scene3D::Light light)
+{
+  if(mActivatedLights.empty())
+  {
+    return;
+  }
+
+  bool needToDisable = false;
+  for(uint32_t i = 0; i < mLights.size(); ++i)
+  {
+    if(mLights[i].first == light)
+    {
+      // If mLights[i].second is true, it means the light is currently activated in Scene.
+      // Then it should be removed from mActivatedLights list too.
+      needToDisable = mLights[i].second;
+      mLights.erase(mLights.begin() + i);
+      break;
+    }
+  }
+
+  uint32_t maxNumberOfLight = Scene3D::Internal::Light::GetMaximumEnabledLightCount();
+  if(needToDisable)
+  {
+    uint32_t removedIndex = RemoveLightInternal(light);
+    if(mActivatedLightCount < maxNumberOfLight && mLights.size() >= maxNumberOfLight)
+    {
+      for(auto && lightItem : mLights)
+      {
+        if(lightItem.second == false)
+        {
+          lightItem.second = AddLightInternal(lightItem.first);
+          break;
+        }
+      }
+    }
+
+    // To remove empty entity of mActivatedLights, moves last object to empty position.
+    // Because one Light is removed, mActivatedLights[mActivatedLightCount] is current last object of the list.
+    if(!mActivatedLights[removedIndex] && mActivatedLightCount > 0 && removedIndex < mActivatedLightCount)
+    {
+      Scene3D::Light reorderingLight = mActivatedLights[mActivatedLightCount];
+      RemoveLightInternal(reorderingLight);
+      AddLightInternal(reorderingLight);
+    }
+  }
+}
+
+uint32_t SceneView::GetActivatedLightCount() const
+{
+  return mActivatedLightCount;
+}
+
 void SceneView::UseFramebuffer(bool useFramebuffer)
 {
   if(mUseFrameBuffer != useFramebuffer)
@@ -550,6 +634,7 @@ void SceneView::OnSceneDisconnection()
     if(mRenderTask)
     {
       taskList.RemoveTask(mRenderTask);
+      mRenderTask.Reset();
       mFrameBuffer.Reset();
     }
   }
@@ -667,19 +752,10 @@ void SceneView::UpdateRenderTask()
         Property::Map imagePropertyMap;
         imagePropertyMap.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE);
         imagePropertyMap.Insert(Toolkit::ImageVisual::Property::URL, imageUrl.GetUrl());
-        // To make sure this visual call LoadTexture API immediate.
-        imagePropertyMap.Insert(Toolkit::ImageVisual::Property::LOAD_POLICY, Toolkit::ImageVisual::LoadPolicy::IMMEDIATE);
-        imagePropertyMap.Insert(Toolkit::ImageVisual::Property::RELEASE_POLICY, Toolkit::ImageVisual::ReleasePolicy::DESTROYED);
         // To flip rendered scene without CameraActor::SetInvertYAxis() to avoid backface culling.
         imagePropertyMap.Insert(Toolkit::ImageVisual::Property::PIXEL_AREA, Vector4(0.0f, 1.0f, 1.0f, -1.0f));
         mVisual = Toolkit::VisualFactory::Get().CreateVisual(imagePropertyMap);
 
-        // Use premultiplied alpha when we use FBO
-        if(mVisual)
-        {
-          Toolkit::GetImplementation(mVisual).EnablePreMultipliedAlpha(true);
-        }
-
         Toolkit::DevelControl::RegisterVisual(*this, RENDERING_BUFFER, mVisual);
 
         mRenderTask.SetFrameBuffer(mFrameBuffer);
@@ -745,6 +821,14 @@ void SceneView::UpdateSkybox(const std::string& skyboxUrl, Scene3D::EnvironmentM
       Dali::AsyncTaskManager::Get().RemoveTask(mSkyboxLoadTask);
       mSkyboxLoadTask.Reset();
     }
+
+    if(mSkybox)
+    {
+        mSkybox.Unparent();
+        mSkybox.Reset();
+        mSkyboxTexture.Reset();
+    }
+
     mSkyboxDirty         = false;
     mSkyboxResourceReady = true;
   }
@@ -791,7 +875,7 @@ void SceneView::OnSkyboxLoadComplete()
 
   mSkyboxTexture = mSkyboxLoadTask->GetLoadedTexture();
   Shader skyboxShader;
-  if(mSkyboxEnvironmentMapType == Scene3D::EnvironmentMapType::CUBEMAP)
+  if(mSkyboxLoadTask->GetEnvironmentMapType() == Scene3D::EnvironmentMapType::CUBEMAP)
   {
     skyboxShader = Shader::New(SHADER_SKYBOX_SHADER_VERT.data(), SHADER_SKYBOX_SHADER_FRAG.data());
   }
@@ -826,6 +910,7 @@ void SceneView::OnIblDiffuseLoadComplete()
 void SceneView::OnIblSpecularLoadComplete()
 {
   mSpecularTexture          = mIblSpecularLoadTask->GetLoadedTexture();
+  mSpecularMipmapLevels     = mIblSpecularLoadTask->GetMipmapLevels();
   mIblSpecularResourceReady = true;
   if(mIblDiffuseResourceReady && mIblSpecularResourceReady)
   {
@@ -849,11 +934,71 @@ void SceneView::NotifyImageBasedLightTextureChange()
   {
     if(item)
     {
-      item->NotifyImageBasedLightTexture(mDiffuseTexture, mSpecularTexture, mIblScaleFactor);
+      item->NotifyImageBasedLightTexture(mDiffuseTexture, mSpecularTexture, mIblScaleFactor, mSpecularMipmapLevels);
     }
   }
 }
 
+bool SceneView::AddLightInternal(Scene3D::Light light)
+{
+  uint32_t maxNumberOfLight = Scene3D::Internal::Light::GetMaximumEnabledLightCount();
+  if(mActivatedLightCount == 0)
+  {
+    mActivatedLights.resize(maxNumberOfLight);
+  }
+
+  bool enabled = false;
+  if(mActivatedLightCount < maxNumberOfLight)
+  {
+    uint32_t newLightIndex = 0u;
+    for(; newLightIndex < maxNumberOfLight; ++newLightIndex)
+    {
+      if(!mActivatedLights[newLightIndex])
+      {
+        mActivatedLights[newLightIndex] = light;
+        break;
+      }
+    }
+
+    for(auto&& item : mItems)
+    {
+      if(item)
+      {
+        item->NotifyLightAdded(newLightIndex, light);
+      }
+    }
+
+    mActivatedLightCount++;
+    enabled = true;
+  }
+  return enabled;
+}
+
+int32_t SceneView::RemoveLightInternal(Scene3D::Light light)
+{
+  int32_t removedIndex = INVALID_INDEX;
+  uint32_t maxNumberOfLight = Scene3D::Internal::Light::GetMaximumEnabledLightCount();
+  for(uint32_t i = 0; i < maxNumberOfLight; ++i)
+  {
+    if(mActivatedLights[i] == light)
+    {
+      for(auto&& item : mItems)
+      {
+        if(item)
+        {
+          item->NotifyLightRemoved(i);
+        }
+      }
+      mActivatedLights[i].Reset();
+      mActivatedLightCount--;
+      removedIndex = i;
+      break;
+    }
+  }
+
+  return removedIndex;
+}
+
 } // namespace Internal
 } // namespace Scene3D
 } // namespace Dali