Bugfix: Gfx program discarded while DALi ProgramCache still holds to the GL resource
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / render-sampler.h
index 1adf2f9..21a461f 100644 (file)
@@ -2,7 +2,7 @@
 #define DALI_INTERNAL_RENDER_SAMPLER_H
 
 /*
- * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2021 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.
@@ -17,6 +17,7 @@
  * limitations under the License.
  */
 
+#include <dali/graphics-api/graphics-controller.h>
 #include <dali/public-api/actors/sampling.h>
 #include <dali/public-api/rendering/sampler.h>
 
@@ -26,7 +27,12 @@ namespace Internal
 {
 namespace Render
 {
-
+/**
+ * The sampler class holds the min/mag filter and texture wrap modes.
+ * It's graphics counterpart is only created when needed, and also
+ * only created when the filters and wrap modes are not default
+ * values.
+ */
 struct Sampler
 {
   using FilterMode = Dali::FilterMode::Type;
@@ -35,13 +41,7 @@ struct Sampler
   /**
    * Constructor
    */
-  Sampler()
-  :mMinificationFilter(FilterMode::DEFAULT),
-   mMagnificationFilter(FilterMode::DEFAULT),
-   mSWrapMode(WrapMode::DEFAULT),
-   mTWrapMode(WrapMode::DEFAULT),
-   mRWrapMode(WrapMode::DEFAULT)
-  {}
+  Sampler();
 
   /**
    * Destructor
@@ -50,11 +50,11 @@ struct Sampler
 
   bool operator==(const Sampler& rhs) const
   {
-    return ( ( mMinificationFilter == rhs.mMinificationFilter ) &&
-             ( mMagnificationFilter == rhs.mMagnificationFilter ) &&
-             ( mSWrapMode == rhs.mSWrapMode ) &&
-             ( mTWrapMode == rhs.mTWrapMode ) &&
-             ( mRWrapMode == rhs.mRWrapMode ) );
+    return ((mMinificationFilter == rhs.mMinificationFilter) &&
+            (mMagnificationFilter == rhs.mMagnificationFilter) &&
+            (mSWrapMode == rhs.mSWrapMode) &&
+            (mTWrapMode == rhs.mTWrapMode) &&
+            (mRWrapMode == rhs.mRWrapMode));
   }
 
   bool operator!=(const Sampler& rhs) const
@@ -62,11 +62,123 @@ struct Sampler
     return !(*this == rhs);
   }
 
-  FilterMode  mMinificationFilter   : 4;    ///< The minify filter
-  FilterMode  mMagnificationFilter  : 4;    ///< The magnify filter
-  WrapMode    mSWrapMode            : 4;    ///< The horizontal wrap mode
-  WrapMode    mTWrapMode            : 4;    ///< The vertical wrap mode
-  WrapMode    mRWrapMode            : 4;    ///< The vertical wrap mode
+  /**
+   * Returns Graphics API sampler object
+   * @return Pointer to API sampler or nullptr
+   */
+  const Dali::Graphics::Sampler* GetGraphicsObject();
+
+  void DestroyGraphicsObjects();
+
+  inline Graphics::SamplerAddressMode GetGraphicsSamplerAddressMode(WrapMode mode) const
+  {
+    switch(mode)
+    {
+      case WrapMode::REPEAT:
+        return Graphics::SamplerAddressMode::REPEAT;
+      case WrapMode::MIRRORED_REPEAT:
+        return Graphics::SamplerAddressMode::MIRRORED_REPEAT;
+      case WrapMode::CLAMP_TO_EDGE:
+        return Graphics::SamplerAddressMode::CLAMP_TO_EDGE;
+      case WrapMode::DEFAULT:
+        return Graphics::SamplerAddressMode::CLAMP_TO_EDGE;
+    }
+    return {};
+  }
+
+  inline Graphics::SamplerMipmapMode GetGraphicsSamplerMipmapMode(FilterMode mode) const
+  {
+    switch(mode)
+    {
+      case FilterMode::LINEAR_MIPMAP_LINEAR:
+        return Graphics::SamplerMipmapMode::LINEAR;
+      case FilterMode::NEAREST_MIPMAP_LINEAR:
+        return Graphics::SamplerMipmapMode::LINEAR;
+      case FilterMode::NEAREST_MIPMAP_NEAREST:
+        return Graphics::SamplerMipmapMode::NEAREST;
+      case FilterMode::LINEAR_MIPMAP_NEAREST:
+        return Graphics::SamplerMipmapMode::NEAREST;
+      default:
+        return Graphics::SamplerMipmapMode::NONE;
+    }
+    return {};
+  }
+
+  inline Graphics::SamplerFilter GetGraphicsFilter(FilterMode mode) const
+  {
+    switch(mode)
+    {
+      case FilterMode::LINEAR:
+      case FilterMode::LINEAR_MIPMAP_LINEAR:
+      case FilterMode::LINEAR_MIPMAP_NEAREST:
+        return Graphics::SamplerFilter::LINEAR;
+      case FilterMode::NEAREST:
+      case FilterMode::NEAREST_MIPMAP_LINEAR:
+      case FilterMode::NEAREST_MIPMAP_NEAREST:
+        return Graphics::SamplerFilter::NEAREST;
+      case FilterMode::DEFAULT:
+        return Graphics::SamplerFilter::LINEAR;
+      case FilterMode::NONE:
+        return Graphics::SamplerFilter::NEAREST;
+      default:
+        return {};
+    }
+    return {};
+  }
+
+  /**
+   * Sets the filter modes for an existing sampler
+   * @param[in] sampler The sampler
+   * @param[in] minFilterMode The filter to use under minification
+   * @param[in] magFilterMode The filter to use under magnification
+   */
+  inline void SetFilterMode(unsigned int minFilterMode, unsigned int magFilterMode)
+  {
+    mMinificationFilter  = static_cast<Dali::FilterMode::Type>(minFilterMode);
+    mMagnificationFilter = static_cast<Dali::FilterMode::Type>(magFilterMode);
+    mIsDirty             = true;
+  }
+
+  /**
+   * Sets the wrap mode for an existing sampler
+   * @param[in] sampler The sampler
+   * @param[in] rWrapMode Wrapping mode in z direction
+   * @param[in] sWrapMode Wrapping mode in x direction
+   * @param[in] tWrapMode Wrapping mode in y direction
+   */
+  inline void SetWrapMode(unsigned int rWrapMode, unsigned int sWrapMode, unsigned int tWrapMode)
+  {
+    mRWrapMode = static_cast<Dali::WrapMode::Type>(rWrapMode);
+    mSWrapMode = static_cast<Dali::WrapMode::Type>(sWrapMode);
+    mTWrapMode = static_cast<Dali::WrapMode::Type>(tWrapMode);
+    mIsDirty   = true;
+  }
+
+  /**
+   * Check if the sampler has default values
+   */
+  inline bool IsDefaultSampler()
+  {
+    return (mMagnificationFilter == FilterMode::DEFAULT &&
+            mMinificationFilter == FilterMode::DEFAULT &&
+            mSWrapMode == WrapMode::DEFAULT &&
+            mTWrapMode == WrapMode::DEFAULT &&
+            mRWrapMode == WrapMode::DEFAULT);
+  }
+
+  void Initialize(Graphics::Controller& graphicsController);
+
+  Graphics::Sampler* CreateGraphicsObject();
+
+  Graphics::Controller*                  mGraphicsController;
+  Graphics::UniquePtr<Graphics::Sampler> mGraphicsSampler;
+
+  FilterMode mMinificationFilter : 4;  ///< The minify filter
+  FilterMode mMagnificationFilter : 4; ///< The magnify filter
+  WrapMode   mSWrapMode : 4;           ///< The horizontal wrap mode
+  WrapMode   mTWrapMode : 4;           ///< The vertical wrap mode
+  WrapMode   mRWrapMode : 4;           ///< The vertical wrap mode
+  bool       mIsDirty : 1;             ///< If parameters have been set thru API
 };
 
 } // namespace Render
@@ -75,5 +187,4 @@ struct Sampler
 
 } // namespace Dali
 
-
 #endif //  DALI_INTERNAL_RENDER_SAMPLER_H