(Vector) Change shader at visual side 42/307642/3
authorEunki, Hong <eunkiki.hong@samsung.com>
Tue, 12 Mar 2024 05:12:22 +0000 (14:12 +0900)
committerEunki, Hong <eunkiki.hong@samsung.com>
Fri, 15 Mar 2024 10:11:00 +0000 (19:11 +0900)
Let we change animated vector visual shader at visual side, instead of dali-extension.
Now we can use cached shader.

Change-Id: Ie530f295227ce55b9d374bc4ddfd1a09361905a4
Signed-off-by: Eunki, Hong <eunkiki.hong@samsung.com>
automated-tests/src/dali-toolkit/dali-toolkit-test-utils/toolkit-vector-animation-renderer.cpp
automated-tests/src/dali-toolkit/dali-toolkit-test-utils/toolkit-vector-animation-renderer.h
automated-tests/src/dali-toolkit/utc-Dali-AnimatedVectorImageVisual.cpp
dali-toolkit/internal/visuals/animated-vector-image/animated-vector-image-visual.cpp
dali-toolkit/internal/visuals/animated-vector-image/animated-vector-image-visual.h

index 4c5fe551d57ac4b0b23df46e2e14496af5375693..aec90add5f888dd23d3db7061d7ba12006325f3b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2023 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2024 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.
@@ -18,6 +18,7 @@
 #include <dali/devel-api/adaptor-framework/pixel-buffer.h>
 #include <dali/devel-api/adaptor-framework/vector-animation-renderer.h>
 #include <dali/devel-api/threading/mutex.h>
+#include <dali/public-api/adaptor-framework/native-image-source.h>
 #include <dali/public-api/object/base-object.h>
 #include <dali/public-api/object/property-array.h>
 #include <toolkit-application.h>
@@ -55,6 +56,7 @@ public:
     mFrameRate(60.0f),
     mTestFrameDrop(false),
     mNeedDroppedFrames(false),
+    mUseNativeImage(false),
     mEventThreadCallback(new EventThreadCallback(MakeCallback(this, &VectorAnimationRenderer::OnTriggered)))
   {
     mCount++;
@@ -229,12 +231,22 @@ public:
       mResourceReady = true;
 
       Dali::TextureSet textureSet = mRenderer.GetTextures();
-      Dali::Texture    texture    = Dali::Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, mWidth, mHeight);
-      textureSet.SetTexture(0, texture);
 
-      Devel::PixelBuffer pixelBuffer = Devel::PixelBuffer::New(mWidth, mHeight, Pixel::RGBA8888);
-      Dali::PixelData    pixelData   = Devel::PixelBuffer::Convert(pixelBuffer);
-      texture.Upload(pixelData);
+      if(mUseNativeImage)
+      {
+        Dali::NativeImageSourcePtr nativeImageSource = Dali::NativeImageSource::New(mWidth, mHeight, Dali::NativeImageSource::COLOR_DEPTH_32);
+        Dali::Texture              texture           = Dali::Texture::New(*nativeImageSource);
+        textureSet.SetTexture(0, texture);
+      }
+      else
+      {
+        Dali::Texture texture = Dali::Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, mWidth, mHeight);
+        textureSet.SetTexture(0, texture);
+
+        Devel::PixelBuffer pixelBuffer = Devel::PixelBuffer::New(mWidth, mHeight, Pixel::RGBA8888);
+        Dali::PixelData    pixelData   = Devel::PixelBuffer::Convert(pixelBuffer);
+        texture.Upload(pixelData);
+      }
 
       mUploadCompletedSignal.Emit();
     }
@@ -263,6 +275,7 @@ public:
   bool     mResourceReady{false};
   bool     mNeedTrigger{true};
   bool     mEnableFixedCache{false};
+  bool     mUseNativeImage{false};
 
   Dali::VectorAnimationRenderer::UploadCompletedSignalType mUploadCompletedSignal;
   std::unique_ptr<EventThreadCallback>                     mEventThreadCallback;
@@ -403,7 +416,6 @@ void VectorAnimationRenderer::KeepRasterizedBuffer()
   Internal::Adaptor::GetImplementation(*this).KeepRasterizedBuffer();
 }
 
-
 VectorAnimationRenderer::UploadCompletedSignalType& VectorAnimationRenderer::UploadCompletedSignal()
 {
   return Internal::Adaptor::GetImplementation(*this).UploadCompletedSignal();
@@ -426,5 +438,10 @@ uint32_t GetDroppedFrames()
   return Dali::Internal::Adaptor::gVectorAnimationRenderer->mDroppedFrames;
 }
 
+void UseNativeImageTexture(bool useNativeImage)
+{
+  Dali::Internal::Adaptor::gVectorAnimationRenderer->mUseNativeImage = useNativeImage;
+}
+
 } // namespace VectorAnimationRenderer
 } // namespace Test
index bc241f120a6d3a7b25c3fc641a79e31659570ecc..9177afd5df6324d952a622728d4386ab38486c6b 100644 (file)
@@ -2,7 +2,7 @@
 #define DALI_TOOLKIT_TEST_VECTOR_ANIMATION_RENDERER_H
 
 /*
- * Copyright (c) 2022 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2024 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.
@@ -32,6 +32,7 @@ namespace VectorAnimationRenderer
 
 void     DelayRendering(uint32_t delay);
 uint32_t GetDroppedFrames();
+void     UseNativeImageTexture(bool useNativeImage);
 
 } // namespace VectorAnimationRenderer
 } // namespace Test
index 81369558364ba17a077984e201b86ae3bfb53c3d..72a496637934e2672c58a951789f2a8af0315896 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2023 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2024 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.
@@ -24,6 +24,7 @@
 #include <toolkit-timer.h>
 #include <toolkit-vector-animation-renderer.h>
 #include "dummy-control.h"
+#include "test-native-image-source.h"
 
 #include <dali-toolkit/dali-toolkit.h>
 
@@ -2500,5 +2501,49 @@ int UtcDaliAnimatedVectorImageVisualFlushAction(void)
 
   dummyControl.Unparent();
 
+  END_TEST;
+}
+
+int UtcDaliAnimatedVectorImageNativeTextureChangeShader(void)
+{
+  ToolkitTestApplication application;
+  tet_infoline("UtcDaliAnimatedVectorImageNativeTextureChangeShader");
+
+  VisualFactory factory = VisualFactory::Get();
+  Visual::Base  visual  = factory.CreateVisual(TEST_VECTOR_IMAGE_FILE_NAME, ImageDimensions());
+  DALI_TEST_CHECK(visual);
+
+  DummyControl      actor     = DummyControl::New(true);
+  DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
+  dummyImpl.RegisterVisual(DummyControl::Property::TEST_VISUAL, visual);
+
+  // Make we use native texture now.
+  Test::VectorAnimationRenderer::UseNativeImageTexture(true);
+
+  application.GetScene().Add(actor);
+
+  application.SendNotification();
+  application.Render();
+
+  // Trigger count is 1 - resource ready
+  DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
+
+  application.SendNotification();
+  application.Render();
+
+  Renderer        renderer = actor.GetRendererAt(0);
+  Shader          shader   = renderer.GetShader();
+  Property::Value value    = shader.GetProperty(Shader::Property::PROGRAM);
+  Property::Map*  map      = value.GetMap();
+  DALI_TEST_CHECK(map);
+
+  std::string      resultFragmentShader, resultVertexShader;
+  Property::Value* fragment = map->Find("fragment"); // fragment key name from shader-impl.cpp
+  fragment->Get(resultFragmentShader);
+  DALI_TEST_CHECK(resultFragmentShader.find(NativeImageSourceTest::GetCustomFragmentPrefix()) != std::string::npos);
+
+  // Reset to make we use normal texture again.
+  Test::VectorAnimationRenderer::UseNativeImageTexture(false);
+
   END_TEST;
 }
\ No newline at end of file
index 5d8a35510265da941d422d0b08d1170e611c728d..f078dc87418340b79d1bd1491d950299f57198c6 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2023 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2024 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.
@@ -101,7 +101,8 @@ AnimatedVectorImageVisual::AnimatedVectorImageVisual(VisualFactoryCache& factory
   mRendererAdded(false),
   mCoreShutdown(false),
   mRedrawInScalingDown(true),
-  mEnableFrameCache(false)
+  mEnableFrameCache(false),
+  mUseNativeImage(false)
 {
   // the rasterized image is with pre-multiplied alpha format
   mImpl->mFlags |= Visual::Base::Impl::IS_PREMULTIPLIED_ALPHA;
@@ -457,6 +458,7 @@ void AnimatedVectorImageVisual::DoSetOnScene(Actor& actor)
     Vector2 imageSize = actor.GetProperty(Actor::Property::SIZE).Get<Vector2>();
     mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize, false);
     actor.AddRenderer(mImpl->mRenderer);
+    mRendererAdded = true;
     ResourceReady(Toolkit::Visual::ResourceStatus::FAILED);
   }
   else
@@ -645,6 +647,29 @@ void AnimatedVectorImageVisual::OnResourceReady(VectorAnimationTask::ResourceSta
   else
   {
     mLoadFailed = status == VectorAnimationTask::ResourceStatus::FAILED ? true : false;
+    if(status == VectorAnimationTask::ResourceStatus::READY)
+    {
+      // Texture was ready. Change the shader if we need.
+      bool useNativeImage = false;
+      if(mImpl->mRenderer)
+      {
+        auto textureSet = mImpl->mRenderer.GetTextures();
+        if(textureSet && textureSet.GetTextureCount() > 0)
+        {
+          auto texture = textureSet.GetTexture(0u);
+          if(texture)
+          {
+            useNativeImage = DevelTexture::IsNative(texture);
+
+            if(mUseNativeImage != useNativeImage)
+            {
+              mUseNativeImage = useNativeImage;
+              UpdateShader();
+            }
+          }
+        }
+      }
+    }
 
     // If weak handle is holding a placement actor, it is the time to add the renderer to actor.
     Actor actor = mPlacementActor.GetHandle();
@@ -850,7 +875,8 @@ Shader AnimatedVectorImageVisual::GenerateShader() const
       mFactoryCache,
       ImageVisualShaderFeatureBuilder()
         .EnableRoundedCorner(IsRoundedCornerRequired())
-        .EnableBorderline(IsBorderlineRequired()));
+        .EnableBorderline(IsBorderlineRequired())
+        .SetTextureForFragmentShaderCheck(mUseNativeImage ? mImpl->mRenderer.GetTextures().GetTexture(0) : Dali::Texture()));
   }
   return shader;
 }
index 5ebd051e35012b31b3e3934634baf307e7d9572f..493e62143d8246ae4724d2c9841e473538c85fb5 100644 (file)
@@ -2,7 +2,7 @@
 #define DALI_TOOLKIT_INTERNAL_ANIMATED_VECTOR_IMAGE_VISUAL_H
 
 /*
- * Copyright (c) 2023 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2024 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.
@@ -249,11 +249,13 @@ private:
   WeakHandle<Actor>                  mPlacementActor;
   DevelImageVisual::PlayState::Type  mPlayState;
   CallbackBase*                      mEventCallback; // Not owned
-  bool                               mLoadFailed;
-  bool                               mRendererAdded;
-  bool                               mCoreShutdown;
-  bool                               mRedrawInScalingDown;
-  bool                               mEnableFrameCache;
+
+  bool mLoadFailed : 1;
+  bool mRendererAdded : 1;
+  bool mCoreShutdown : 1;
+  bool mRedrawInScalingDown : 1;
+  bool mEnableFrameCache : 1;
+  bool mUseNativeImage : 1;
 };
 
 } // namespace Internal