[Tizen] Add a mutex lock to a method of NativeImageSource
[platform/core/uifw/dali-adaptor.git] / dali / internal / imaging / tizen / native-image-source-impl-tizen.cpp
index f3b526d..153848f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2022 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.
@@ -38,8 +38,7 @@ namespace Adaptor
 {
 namespace
 {
-const char* FRAGMENT_PREFIX = "#extension GL_OES_EGL_image_external:require\n";
-const char* SAMPLER_TYPE    = "samplerExternalOES";
+const char* SAMPLER_TYPE = "samplerExternalOES";
 
 // clang-format off
 tbm_format FORMATS_BLENDING_REQUIRED[] = {
@@ -87,7 +86,9 @@ NativeImageSourceTizen::NativeImageSourceTizen(uint32_t width, uint32_t height,
   mEglImageExtensions(NULL),
   mSetSource(false),
   mMutex(),
-  mIsBufferAcquired(false)
+  mIsBufferAcquired(false),
+  mResourceDestructionCallback()
+
 {
   DALI_ASSERT_ALWAYS(Adaptor::IsAvailable());
 
@@ -401,6 +402,12 @@ bool NativeImageSourceTizen::IsColorDepthSupported(Dali::NativeImageSource::Colo
 
 bool NativeImageSourceTizen::CreateResource()
 {
+  // If an EGL image exists, use it as it is without creating it.
+  if(mEglImageKHR != NULL)
+  {
+    return true;
+  }
+
   // casting from an unsigned int to a void *, which should then be cast back
   // to an unsigned int in the driver.
   EGLClientBuffer eglBuffer = reinterpret_cast<EGLClientBuffer>(mTbmSurface);
@@ -426,6 +433,11 @@ void NativeImageSourceTizen::DestroyResource()
 
     mEglImageKHR = NULL;
   }
+
+  if(mResourceDestructionCallback)
+  {
+    mResourceDestructionCallback->Trigger();
+  }
 }
 
 uint32_t NativeImageSourceTizen::TargetTexture()
@@ -440,22 +452,23 @@ void NativeImageSourceTizen::PrepareTexture()
   Dali::Mutex::ScopedLock lock(mMutex);
   if(mSetSource)
   {
-    void* eglImage = mEglImageKHR;
+    // Destroy previous eglImage because use for new one.
+    // if mEglImageKHR is not to be NULL here, it will not be updated with a new eglImage.
+    mEglImageExtensions->DestroyImageKHR(mEglImageKHR);
+    mEglImageKHR = NULL;
 
     if(CreateResource())
     {
       TargetTexture();
     }
 
-    mEglImageExtensions->DestroyImageKHR(eglImage);
-
     mSetSource = false;
   }
 }
 
-const char* NativeImageSourceTizen::GetCustomFragmentPrefix() const
+bool NativeImageSourceTizen::ApplyNativeFragmentShader(std::string& shader)
 {
-  return FRAGMENT_PREFIX;
+  return mEglGraphics->ApplyNativeFragmentShader(shader, SAMPLER_TYPE);
 }
 
 const char* NativeImageSourceTizen::GetCustomSamplerTypename() const
@@ -503,7 +516,7 @@ uint8_t* NativeImageSourceTizen::AcquireBuffer(uint16_t& width, uint16_t& height
   {
     tbm_surface_info_s info;
 
-    if(tbm_surface_map(mTbmSurface, TBM_SURF_OPTION_READ, &info) != TBM_SURFACE_ERROR_NONE)
+    if(tbm_surface_map(mTbmSurface, TBM_SURF_OPTION_READ | TBM_SURF_OPTION_WRITE, &info) != TBM_SURFACE_ERROR_NONE)
     {
       DALI_LOG_ERROR("Fail to map tbm_surface\n");
 
@@ -541,6 +554,12 @@ bool NativeImageSourceTizen::ReleaseBuffer()
   return ret;
 }
 
+void NativeImageSourceTizen::SetResourceDestructionCallback(EventThreadCallback* callback)
+{
+  Dali::Mutex::ScopedLock lock(mMutex);
+  mResourceDestructionCallback = std::unique_ptr<EventThreadCallback>(callback);
+}
+
 } // namespace Adaptor
 
 } // namespace Internal