[Tizen] Create Texture by ResourceId + Set Texture size and format internally.
[platform/core/uifw/dali-core.git] / dali / internal / event / rendering / texture-impl.h
index 852e952..4d3a133 100644 (file)
@@ -2,7 +2,7 @@
 #define DALI_INTERNAL_NEW_TEXTURE_H
 
 /*
- * Copyright (c) 2016 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.
  */
 
 // INTERNAL INCLUDES
-#include <dali/public-api/common/dali-common.h> // DALI_ASSERT_ALWAYS
-#include <dali/public-api/common/intrusive-ptr.h> // Dali::IntrusivePtr
-#include <dali/public-api/object/base-object.h>
-#include <dali/public-api/images/pixel.h>
-#include <dali/devel-api/rendering/texture.h> // Dali::Internal::Render::Texture
 #include <dali/internal/event/common/event-thread-services.h>
+#include <dali/internal/event/images/pixel-data-impl.h>
+#include <dali/internal/render/renderers/render-texture-key.h>
+#include <dali/public-api/common/dali-common.h>      // DALI_ASSERT_ALWAYS
+#include <dali/public-api/common/intrusive-ptr.h>    // Dali::IntrusivePtr
+#include <dali/public-api/images/image-operations.h> // Dali::ImageDimensions
+#include <dali/public-api/images/pixel.h>
+#include <dali/public-api/object/base-object.h>
+#include <dali/public-api/rendering/texture.h> // Dali::Internal::Render::Texture
 
 namespace Dali
 {
 namespace Internal
 {
-namespace Render
-{
-class NewTexture;
-}
+class Texture;
+using TexturePtr = IntrusivePtr<Texture>;
 
-class NewTexture;
-typedef IntrusivePtr<NewTexture> NewTexturePtr;
-
-class NewTexture : public BaseObject
+class Texture : public BaseObject
 {
 public:
-
-  /**
-   * @brief Structure used to pass parameters to the Upload method
-   */
-  struct UploadParams
-  {
-    unsigned int layer;    ///< Specifies the layer of a cube map or array texture
-    unsigned int mipmap;   ///< Specifies the level-of-detail number. Level 0 is the base image level. Level n is the nth mipmap reduction image.
-    unsigned int xOffset;  ///< Specifies a texel offset in the x direction within the texture array.
-    unsigned int yOffset;  ///< Specifies a texel offset in the y direction within the texture array.
-    unsigned int width;    ///< Specifies the width of the texture subimage
-    unsigned int height;   ///< Specifies the height of the texture subimage.
-  };
-
   /**
    * @brief Create a new Texture.
    *
@@ -64,34 +48,70 @@ public:
    * @param[in] height The height of the texture
    * @return A smart-pointer to the newly allocated Texture.
    */
-  static NewTexturePtr New(TextureType::Type type, Pixel::Format format, unsigned int width, unsigned int height);
+  static TexturePtr New(TextureType::Type type, Pixel::Format format, uint32_t width, uint32_t height);
 
   /**
    * @brief Creates a new Texture from a native image
    * @param[in] nativeImageInterface The native image
    * @return A smart-pointer to the newly allocated Texture.
    */
-  static NewTexturePtr New( NativeImageInterface& nativeImageInterface );
+  static TexturePtr New(NativeImageInterface& nativeImageInterface);
+
+  /**
+   * @brief Create a new Texture with resourceId.
+   *
+   * @param[in] type The type of the texture
+   * @param[in] resourceId The unique id of this texture combind with TextureUploadManager
+   * @return A smart-pointer to the newly allocated Texture.
+   */
+  static TexturePtr New(TextureType::Type type, uint32_t resourceId);
 
   /**
    * @brief Get the texture render object
    *
    * @return the texture render object
    */
-  Render::NewTexture* GetRenderObject() const;
+  Render::TextureKey GetRenderTextureKey() const;
 
   /**
    * @copydoc Dali::Texture::Upload()
    */
-  void Upload( Vector<unsigned char>& buffer );
+  bool Upload(PixelDataPtr pixelData);
 
   /**
    * @copydoc Dali::Texture::Upload()
    */
-  void Upload( Vector<unsigned char>& buffer,
-               unsigned int layer, unsigned int mipmap,
-               unsigned int xOffset, unsigned int yOffset,
-               unsigned int width, unsigned int height );
+  bool Upload(PixelDataPtr pixelData,
+              uint32_t     layer,
+              uint32_t     mipmap,
+              uint32_t     xOffset,
+              uint32_t     yOffset,
+              uint32_t     width,
+              uint32_t     height);
+
+  /**
+   * @copydoc Dali::Texture::UploadSubPixelData()
+   */
+  bool UploadSubPixelData(PixelDataPtr pixelData,
+                          uint32_t     dataXOffset,
+                          uint32_t     dataYOffset,
+                          uint32_t     dataWidth,
+                          uint32_t     dataHeight);
+
+  /**
+   * @copydoc Dali::Texture::UploadSubPixelData()
+   */
+  bool UploadSubPixelData(PixelDataPtr pixelData,
+                          uint32_t     dataXOffset,
+                          uint32_t     dataYOffset,
+                          uint32_t     dataWidth,
+                          uint32_t     dataHeight,
+                          uint32_t     layer,
+                          uint32_t     mipmap,
+                          uint32_t     xOffset,
+                          uint32_t     yOffset,
+                          uint32_t     width,
+                          uint32_t     height);
 
   /**
    * @copydoc Dali::Texture::GenerateMipmaps()
@@ -101,30 +121,74 @@ public:
   /**
    * @copydoc Dali::Texture::GetWidth()
    */
-  unsigned int GetWidth() const;
+  uint32_t GetWidth() const;
 
   /**
    * @copydoc Dali::Texture::GetHeight()
    */
-  unsigned int GetHeight() const;
+  uint32_t GetHeight() const;
 
+  /**
+   * @copydoc Dali::Texture::GetPixelFormat()
+   */
+  Pixel::Format GetPixelFormat() const;
 
-private: // implementation
+  /**
+   * @copydoc Dali::Integration::GetTextureResourceId()
+   */
+  uint32_t GetResourceId() const;
 
   /**
+   * @copydoc Dali::Integration::GetTextureType()
+   */
+  Dali::TextureType::Type GetTextureType() const;
+
+  /**
+   * @copydoc Dali::Integration::SetTextureSize()
+   */
+  void SetSize(const ImageDimensions& size);
+
+  /**
+   * @copydoc Dali::Integration::SetTexturePixelFormat()
+   */
+  void SetPixelFormat(Pixel::Format format);
+
+  /**
+   * @brief Determine if the texture is a native image
+   *
+   * @return true if the texture has been initialized with a native image
+   */
+  bool IsNative() const;
+
+  /**
+   * @brief Apply any native texture code to the given fragment shader
+   *
+   * @param[in,out] shader The fragment shader
+   * @return true if the shader has been modified.
+   */
+  bool ApplyNativeFragmentShader(std::string& shader);
+
+private: // implementation
+  /**
    * Constructor
    * @param[in] type The type of the texture
    * @param[in] format The format of the pixel data
-   * @param[in] width The width of the texture
-   * @param[in] height The height of the texture
+   * @param[in] size The size of the texture
    */
-  NewTexture(TextureType::Type type, Pixel::Format format, unsigned int width, unsigned int height );
+  Texture(TextureType::Type type, Pixel::Format format, ImageDimensions size);
 
   /**
    * Constructor from native image
    * @param[in] nativeImageInterface The native image
    */
-  NewTexture( NativeImageInterfacePtr nativeImageInterface );
+  Texture(NativeImageInterfacePtr nativeImageInterface);
+
+  /**
+   * Constructor from resource id
+   * @param[in] type The type of the texture
+   * @param[in] resourceId The resouce id for texture upload manager using
+   */
+  Texture(TextureType::Type type, uint32_t resourceId);
 
   /**
    * Second stage initialization of the Texture
@@ -132,54 +196,48 @@ private: // implementation
   void Initialize();
 
 protected:
-
   /**
    * A reference counted object may only be deleted by calling Unreference()
    */
-  virtual ~NewTexture();
+  ~Texture() override;
 
 private: // unimplemented methods
-  NewTexture( const NewTexture& );
-  NewTexture& operator=( const NewTexture& );
+  Texture(const Texture&);
+  Texture& operator=(const Texture&);
 
-private: // data
+private:                                               // data
+  Internal::EventThreadServices& mEventThreadServices; ///<Used to send messages to the render thread via update thread
+  Internal::Render::TextureKey   mTextureKey;          ///<The Render::Texture associated to this texture
 
-  /**
-   * @brief Helper method to check that upload parameters are correct
-   * @param[in] buffer A vector with the data to be uploaded
-   * @param[in] parameters The uploading parameters
-   */
-  bool CheckUploadParametres( const Vector<unsigned char>& buffer, const UploadParams& parameters) const;
+  NativeImageInterfacePtr mNativeImage; ///< Pointer to native image
+  ImageDimensions         mSize;        ///< Size of the texture
+  Dali::TextureType::Type mType;        ///< Texture type (cached)
+  Pixel::Format           mFormat;      ///< Pixel format
 
-  Internal::EventThreadServices& mEventThreadServices;    ///<Used to send messages to the render thread via update thread
-  Internal::Render::NewTexture* mRenderObject;            ///<The Render::Texture associated to this texture
+  uint32_t mResourceId;
 
-  NativeImageInterfacePtr mNativeImage; ///< Pointer to native image
-  Dali::TextureType::Type mType;      ///< Texture type (cached)
-  Pixel::Format mFormat;              ///< Pixel format
-  unsigned int mWidth;                ///< Width of the texture
-  unsigned int mHeight;               ///< Height of the texture
+  bool mUseUploadedParameter : 1; ///< Whether ths texture size and format depend on uploaded image or not.
 };
 
 } // namespace Internal
 
 // Helpers for public-api forwarding methods
-inline Internal::NewTexture& GetImplementation(Dali::Texture& handle)
+inline Internal::Texture& GetImplementation(Dali::Texture& handle)
 {
   DALI_ASSERT_ALWAYS(handle && "Texture handle is empty");
 
   BaseObject& object = handle.GetBaseObject();
 
-  return static_cast<Internal::NewTexture&>(object);
+  return static_cast<Internal::Texture&>(object);
 }
 
-inline const Internal::NewTexture& GetImplementation(const Dali::Texture& handle)
+inline const Internal::Texture& GetImplementation(const Dali::Texture& handle)
 {
   DALI_ASSERT_ALWAYS(handle && "Texture handle is empty");
 
   const BaseObject& object = handle.GetBaseObject();
 
-  return static_cast<const Internal::NewTexture&>(object);
+  return static_cast<const Internal::Texture&>(object);
 }
 
 } // namespace Dali