[Tizen] Fix build warning
[platform/core/uifw/dali-adaptor.git] / dali / internal / graphics / gles / gl-implementation.h
index e60fb8f..39217ce 100644 (file)
@@ -2,7 +2,7 @@
 #define DALI_INTERNAL_GL_IMPLEMENTATION_H
 
 /*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2023 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.
 #include <dali/devel-api/threading/conditional-wait.h>
 #include <dali/integration-api/gl-abstraction.h>
 #include <dali/internal/graphics/common/egl-include.h>
+#include <dali/public-api/common/vector-wrapper.h>
 #include <cstdlib>
 #include <cstring>
 #include <memory>
 
 // INTERNAL INCLUDES
+#include <dali/devel-api/adaptor-framework/environment-variable.h>
+#include <dali/internal/graphics/gles/gl-extensions-support.h>
 #include <dali/internal/graphics/gles/gles-abstraction.h>
 #include <dali/internal/graphics/gles/gles2-implementation.h>
 #include <dali/internal/graphics/gles/gles3-implementation.h>
+#include <dali/internal/system/common/time-service.h>
 
 namespace Dali
 {
@@ -39,30 +43,6 @@ namespace Internal
 {
 namespace Adaptor
 {
-namespace
-{
-static constexpr int32_t     INITIAL_GLES_VERSION                         = 30;
-static constexpr int32_t     GLES_VERSION_SUPPORT_BLEND_EQUATION_ADVANCED = 32;
-static constexpr const char* LEGACY_SHADING_LANGUAGE_VERSION              = "100";
-static constexpr const char* KHR_BLEND_EQUATION_ADVANCED                  = "GL_KHR_blend_equation_advanced";
-
-static constexpr const char* DEFAULT_SAMPLER_TYPE = "sampler2D";
-
-static constexpr const char* FRAGMENT_SHADER_ADVANCED_BLEND_EQUATION_PREFIX =
-  "#extension GL_KHR_blend_equation_advanced : enable\n"
-
-  "#if GL_KHR_blend_equation_advanced==1 || __VERSION__>=320\n"
-  "  layout(blend_support_all_equations) out;\n"
-  "#endif\n";
-
-static constexpr const char* FRAGMENT_SHADER_OUTPUT_COLOR_STRING =
-  "out mediump vec4 fragColor;\n";
-
-static constexpr const char* OES_EGL_IMAGE_EXTERNAL_STRING = "#extension GL_OES_EGL_image_external:require\n";
-
-static constexpr const char* OES_EGL_IMAGE_EXTERNAL_STRING_ESSL3 = "#extension GL_OES_EGL_image_external_essl3:require\n";
-} // namespace
-
 /**
  * GlImplementation is a concrete implementation for GlAbstraction.
  * The class provides an OpenGL-ES 2.0 or 3.0 implementation.
@@ -71,20 +51,7 @@ static constexpr const char* OES_EGL_IMAGE_EXTERNAL_STRING_ESSL3 = "#extension G
 class GlImplementation : public Dali::Integration::GlAbstraction
 {
 public:
-  GlImplementation()
-  : mContextCreatedWaitCondition(),
-    mMaxTextureSize(0),
-    mVertexShaderPrefix(""),
-    mGlesVersion(INITIAL_GLES_VERSION),
-    mShadingLanguageVersion(100),
-    mShadingLanguageVersionCached(false),
-    mIsSurfacelessContextSupported(false),
-    mIsAdvancedBlendEquationSupportedCached(false),
-    mIsAdvancedBlendEquationSupported(false),
-    mIsContextCreated(false)
-  {
-    mImpl.reset(new Gles3Implementation());
-  }
+  GlImplementation();
 
   virtual ~GlImplementation()
   {
@@ -100,68 +67,7 @@ public:
     /* Do nothing in main implementation */
   }
 
-  void ContextCreated()
-  {
-    glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
-
-    // Only change gles version for the device that support above gles 3.0.
-    if(mGlesVersion >= INITIAL_GLES_VERSION)
-    {
-      GLint majorVersion, minorVersion;
-      glGetIntegerv(GL_MAJOR_VERSION, &majorVersion);
-      glGetIntegerv(GL_MINOR_VERSION, &minorVersion);
-      mGlesVersion = majorVersion * 10 + minorVersion;
-    }
-
-    if(mGlesVersion >= GLES_VERSION_SUPPORT_BLEND_EQUATION_ADVANCED)
-    {
-      mIsAdvancedBlendEquationSupported = true;
-    }
-    else
-    {
-      // when mIsAdvancedBlendEquationSupported is cached, we don't need to check all the extensions.
-      if(!mIsAdvancedBlendEquationSupportedCached)
-      {
-        const char* const  extensionStr = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
-        std::istringstream stream(extensionStr);
-        std::string        currentExtension;
-        while(std::getline(stream, currentExtension, ' '))
-        {
-          if(currentExtension == KHR_BLEND_EQUATION_ADVANCED)
-          {
-            mIsAdvancedBlendEquationSupported = true;
-            break;
-          }
-        }
-      }
-    }
-
-    if(!mShadingLanguageVersionCached)
-    {
-      std::istringstream shadingLanguageVersionStream(reinterpret_cast<const char*>(glGetString(GL_SHADING_LANGUAGE_VERSION)));
-      std::string        token;
-      uint32_t           tokenCount = 0;
-      while(std::getline(shadingLanguageVersionStream, token, ' '))
-      {
-        if(tokenCount == 3 && token == "ES")
-        {
-          std::getline(shadingLanguageVersionStream, token, '.');
-          mShadingLanguageVersion = std::atoi(token.c_str());
-          mShadingLanguageVersion *= 100;
-          std::getline(shadingLanguageVersionStream, token, '.');
-          mShadingLanguageVersion += std::atoi(token.c_str());
-          break;
-        }
-        tokenCount++;
-      }
-    }
-
-    {
-      ConditionalWait::ScopedLock lock(mContextCreatedWaitCondition);
-      mIsContextCreated = true;
-      mContextCreatedWaitCondition.Notify(lock);
-    }
-  }
+  void ContextCreated();
 
   void SetGlesVersion(const int32_t glesVersion)
   {
@@ -191,21 +97,39 @@ public:
 
   void SetIsAdvancedBlendEquationSupported(const bool isSupported)
   {
-    mIsAdvancedBlendEquationSupported       = isSupported;
-    mIsAdvancedBlendEquationSupportedCached = true;
+    mGlExtensionSupportedCacheList.SetSupported(GlExtensionCache::GlExtensionCheckerType::BLEND_EQUATION_ADVANCED, isSupported);
+  }
+
+  bool IsAdvancedBlendEquationSupported() override
+  {
+    ConditionalWait::ScopedLock lock(mContextCreatedWaitCondition);
+
+    const auto type = GlExtensionCache::GlExtensionCheckerType::BLEND_EQUATION_ADVANCED;
+    if(!mIsContextCreated && !mGlExtensionSupportedCacheList.GetCached(type))
+    {
+      mContextCreatedWaitCondition.Wait(lock);
+    }
+    return mGlExtensionSupportedCacheList.GetSupported(type);
+  }
+
+  void SetIsMultisampledRenderToTextureSupported(const bool isSupported)
+  {
+    mGlExtensionSupportedCacheList.SetSupported(GlExtensionCache::GlExtensionCheckerType::MULTISAMPLED_RENDER_TO_TEXTURE, isSupported);
   }
 
-  bool IsAdvancedBlendEquationSupported()
+  bool IsMultisampledRenderToTextureSupported() override
   {
     ConditionalWait::ScopedLock lock(mContextCreatedWaitCondition);
-    if(!mIsContextCreated && !mIsAdvancedBlendEquationSupportedCached)
+
+    const auto type = GlExtensionCache::GlExtensionCheckerType::MULTISAMPLED_RENDER_TO_TEXTURE;
+    if(!mIsContextCreated && !mGlExtensionSupportedCacheList.GetCached(type))
     {
       mContextCreatedWaitCondition.Wait(lock);
     }
-    return mIsAdvancedBlendEquationSupported;
+    return mGlExtensionSupportedCacheList.GetSupported(type);
   }
 
-  bool IsBlendEquationSupported(DevelBlendEquation::Type blendEquation)
+  bool IsBlendEquationSupported(DevelBlendEquation::Type blendEquation) override
   {
     switch(blendEquation)
     {
@@ -248,7 +172,7 @@ public:
     return false;
   }
 
-  std::string GetShaderVersionPrefix()
+  std::string GetShaderVersionPrefix() override
   {
     if(mShaderVersionPrefix == "")
     {
@@ -265,7 +189,7 @@ public:
     return mShaderVersionPrefix;
   }
 
-  std::string GetVertexShaderPrefix()
+  std::string GetVertexShaderPrefix() override
   {
     if(mVertexShaderPrefix == "")
     {
@@ -285,34 +209,7 @@ public:
     return mVertexShaderPrefix;
   }
 
-  std::string GetFragmentShaderPrefix()
-  {
-    if(mFragmentShaderPrefix == "")
-    {
-      mFragmentShaderPrefix = GetShaderVersionPrefix();
-
-      if(GetShadingLanguageVersion() < 300)
-      {
-        mFragmentShaderPrefix += "#define INPUT varying\n";
-        mFragmentShaderPrefix += "#define OUT_COLOR gl_FragColor\n";
-        mFragmentShaderPrefix += "#define TEXTURE texture2D\n";
-      }
-      else
-      {
-        mFragmentShaderPrefix += "#define INPUT in\n";
-        mFragmentShaderPrefix += "#define OUT_COLOR fragColor\n";
-        mFragmentShaderPrefix += "#define TEXTURE texture\n";
-
-        if(IsAdvancedBlendEquationSupported())
-        {
-          mFragmentShaderPrefix += FRAGMENT_SHADER_ADVANCED_BLEND_EQUATION_PREFIX;
-        }
-
-        mFragmentShaderPrefix += FRAGMENT_SHADER_OUTPUT_COLOR_STRING;
-      }
-    }
-    return mFragmentShaderPrefix;
-  }
+  std::string GetFragmentShaderPrefix() override;
 
   bool TextureRequiresConverting(const GLenum imageGlFormat, const GLenum textureGlFormat, const bool isSubImage) const override
   {
@@ -335,7 +232,17 @@ public:
     return mMaxTextureSize;
   }
 
-  int GetGlesVersion()
+  int GetMaxTextureSamples()
+  {
+    ConditionalWait::ScopedLock lock(mContextCreatedWaitCondition);
+    if(!mIsContextCreated)
+    {
+      mContextCreatedWaitCondition.Wait(lock);
+    }
+    return mMaxTextureSamples;
+  }
+
+  int32_t GetGlesVersion()
   {
     ConditionalWait::ScopedLock lock(mContextCreatedWaitCondition);
     if(!mIsContextCreated)
@@ -361,53 +268,7 @@ public:
     return mShadingLanguageVersion;
   }
 
-  bool ApplyNativeFragmentShader(std::string& shader, const char* customSamplerType)
-  {
-    bool        modified        = false;
-    std::string versionString   = "#version";
-    size_t      versionPosition = shader.find(versionString);
-    if(versionPosition != std::string::npos)
-    {
-      std::string extensionString;
-      size_t shadingLanguageVersionPosition = shader.find_first_not_of(" \t", versionPosition + versionString.length());
-      if(shadingLanguageVersionPosition != std::string::npos &&
-         shader.substr(shadingLanguageVersionPosition, 3) == LEGACY_SHADING_LANGUAGE_VERSION)
-      {
-        extensionString = OES_EGL_IMAGE_EXTERNAL_STRING;
-      }
-      else
-      {
-        extensionString = OES_EGL_IMAGE_EXTERNAL_STRING_ESSL3;
-      }
-
-      if(shader.find(extensionString) == std::string::npos)
-      {
-        modified                 = true;
-        size_t extensionPosition = shader.find_first_of("\n", versionPosition) + 1;
-        shader.insert(extensionPosition, extensionString);
-      }
-    }
-    else
-    {
-      if(shader.find(OES_EGL_IMAGE_EXTERNAL_STRING) == std::string::npos)
-      {
-        modified = true;
-        shader   = OES_EGL_IMAGE_EXTERNAL_STRING + shader;
-      }
-    }
-
-    if(shader.find(customSamplerType) == std::string::npos)
-    {
-      size_t pos = shader.find(DEFAULT_SAMPLER_TYPE);
-      if(pos != std::string::npos)
-      {
-        modified = true;
-        shader.replace(pos, strlen(DEFAULT_SAMPLER_TYPE), customSamplerType);
-      }
-    }
-
-    return modified;
-  }
+  bool ApplyNativeFragmentShader(std::string& shader, const char* customSamplerType);
 
   /* OpenGL ES 2.0 */
 
@@ -488,7 +349,22 @@ public:
 
   void Clear(GLbitfield mask) override
   {
+    uint32_t startTime = 0, endTime = 0;
+    if(mLogEnabled)
+    {
+      startTime = TimeService::GetMilliSeconds();
+    }
+
     glClear(mask);
+
+    if(mLogEnabled)
+    {
+      endTime = TimeService::GetMilliSeconds();
+      if(endTime - startTime > mLogThreshold)
+      {
+        DALI_LOG_DEBUG_INFO("glClear takes long time! [%u ms]\n", endTime - startTime);
+      }
+    }
   }
 
   void ClearColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha) override
@@ -1248,6 +1124,11 @@ public:
     mImpl->RenderbufferStorageMultisample(target, samples, internalformat, width, height);
   }
 
+  void FramebufferTexture2DMultisample(GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level, GLsizei samples) override
+  {
+    mImpl->FramebufferTexture2DMultisample(target, attachment, textarget, texture, level, samples);
+  }
+
   void FramebufferTextureLayer(GLenum target, GLenum attachment, GLuint texture, GLint level, GLint layer) override
   {
     mImpl->FramebufferTextureLayer(target, attachment, texture, level, layer);
@@ -1645,7 +1526,7 @@ public:
 
   void BlendBarrier(void)
   {
-    if(mIsAdvancedBlendEquationSupported)
+    if(mGlExtensionSupportedCacheList.GetSupported(GlExtensionCache::GlExtensionCheckerType::BLEND_EQUATION_ADVANCED))
     {
       mImpl->BlendBarrier();
     }
@@ -1654,18 +1535,21 @@ public:
 private:
   std::unique_ptr<GlesAbstraction> mImpl;
 
+  GlExtensionCache::GlExtensionSupportedCacheList mGlExtensionSupportedCacheList;
+
   ConditionalWait mContextCreatedWaitCondition;
   GLint           mMaxTextureSize;
+  GLint           mMaxTextureSamples;
   std::string     mShaderVersionPrefix;
   std::string     mVertexShaderPrefix;
   std::string     mFragmentShaderPrefix;
   int32_t         mGlesVersion;
   int32_t         mShadingLanguageVersion;
+  uint32_t        mLogThreshold{0};
   bool            mShadingLanguageVersionCached;
   bool            mIsSurfacelessContextSupported;
-  bool            mIsAdvancedBlendEquationSupportedCached;
-  bool            mIsAdvancedBlendEquationSupported;
   bool            mIsContextCreated;
+  bool            mLogEnabled{false};
 };
 
 } // namespace Adaptor