Merge branch 'devel/master' into tizen
[platform/core/uifw/dali-core.git] / dali / internal / event / rendering / texture-impl.h
1 #ifndef DALI_INTERNAL_NEW_TEXTURE_H
2 #define DALI_INTERNAL_NEW_TEXTURE_H
3
4 /*
5  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // INTERNAL INCLUDES
22 #include <dali/internal/event/common/event-thread-services.h>
23 #include <dali/internal/event/images/pixel-data-impl.h>
24 #include <dali/public-api/common/dali-common.h>      // DALI_ASSERT_ALWAYS
25 #include <dali/public-api/common/intrusive-ptr.h>    // Dali::IntrusivePtr
26 #include <dali/public-api/images/image-operations.h> // Dali::ImageDimensions
27 #include <dali/public-api/images/pixel.h>
28 #include <dali/public-api/object/base-object.h>
29 #include <dali/public-api/rendering/texture.h> // Dali::Internal::Render::Texture
30
31 namespace Dali
32 {
33 namespace Internal
34 {
35 namespace Render
36 {
37 class Texture;
38 }
39
40 class Texture;
41 using TexturePtr = IntrusivePtr<Texture>;
42
43 class Texture : public BaseObject
44 {
45 public:
46   /**
47    * @brief Structure used to pass parameters to the Upload method
48    */
49   struct UploadParams
50   {
51     uint32_t dataXOffset; ///< Specifies a pixeldata offset in the x direction within the pixeldata buffer.
52     uint32_t dataYOffset; ///< Specifies a pixeldata offset in the y direction within the pixeldata buffer.
53     uint16_t dataWidth;   ///< Specifies the width of the pixeldata subimage.
54     uint16_t dataHeight;  ///< Specifies the height of the pixeldata subimage.
55     uint16_t layer;       ///< Specifies the layer of a cube map or array texture
56     uint16_t mipmap;      ///< Specifies the level-of-detail number. Level 0 is the base image level. Level n is the nth mipmap reduction image.
57     uint16_t xOffset;     ///< Specifies a texel offset in the x direction within the texture array.
58     uint16_t yOffset;     ///< Specifies a texel offset in the y direction within the texture array.
59     uint16_t width;       ///< Specifies the width of the texture subimage
60     uint16_t height;      ///< Specifies the height of the texture subimage.
61   };
62
63   /**
64    * @brief Create a new Texture.
65    *
66    * @param[in] type The type of the texture
67    * @param[in] format The format of the pixel data
68    * @param[in] width The width of the texture
69    * @param[in] height The height of the texture
70    * @return A smart-pointer to the newly allocated Texture.
71    */
72   static TexturePtr New(TextureType::Type type, Pixel::Format format, uint32_t width, uint32_t height);
73
74   /**
75    * @brief Creates a new Texture from a native image
76    * @param[in] nativeImageInterface The native image
77    * @return A smart-pointer to the newly allocated Texture.
78    */
79   static TexturePtr New(NativeImageInterface& nativeImageInterface);
80
81   /**
82    * @brief Get the texture render object
83    *
84    * @return the texture render object
85    */
86   Render::Texture* GetRenderObject() const;
87
88   /**
89    * @copydoc Dali::Texture::Upload()
90    */
91   bool Upload(PixelDataPtr pixelData);
92
93   /**
94    * @copydoc Dali::Texture::Upload()
95    */
96   bool Upload(PixelDataPtr pixelData,
97               uint32_t     layer,
98               uint32_t     mipmap,
99               uint32_t     xOffset,
100               uint32_t     yOffset,
101               uint32_t     width,
102               uint32_t     height);
103
104   /**
105    * @copydoc Dali::Texture::UploadSubPixelData()
106    */
107   bool UploadSubPixelData(PixelDataPtr pixelData,
108                           uint32_t     dataXOffset,
109                           uint32_t     dataYOffset,
110                           uint32_t     dataWidth,
111                           uint32_t     dataHeight);
112
113   /**
114    * @copydoc Dali::Texture::UploadSubPixelData()
115    */
116   bool UploadSubPixelData(PixelDataPtr pixelData,
117                           uint32_t     dataXOffset,
118                           uint32_t     dataYOffset,
119                           uint32_t     dataWidth,
120                           uint32_t     dataHeight,
121                           uint32_t     layer,
122                           uint32_t     mipmap,
123                           uint32_t     xOffset,
124                           uint32_t     yOffset,
125                           uint32_t     width,
126                           uint32_t     height);
127
128   /**
129    * @copydoc Dali::Texture::GenerateMipmaps()
130    */
131   void GenerateMipmaps();
132
133   /**
134    * @copydoc Dali::Texture::GetWidth()
135    */
136   uint32_t GetWidth() const;
137
138   /**
139    * @copydoc Dali::Texture::GetHeight()
140    */
141   uint32_t GetHeight() const;
142
143   /**
144    * @copydoc Dali::Texture::GetPixelFormat()
145    */
146   Pixel::Format GetPixelFormat() const;
147
148   /**
149    * @brief Determine if the texture is a native image
150    *
151    * @return true if the texture has been initialized with a native image
152    */
153   bool IsNative() const;
154
155   /**
156    * @brief Apply any native texture code to the given fragment shader
157    *
158    * @param[in,out] shader The fragment shader
159    * @return true if the shader has been modified.
160    */
161   bool ApplyNativeFragmentShader(std::string& shader);
162
163 private: // implementation
164   /**
165    * Constructor
166    * @param[in] type The type of the texture
167    * @param[in] format The format of the pixel data
168    * @param[in] size The size of the texture
169    */
170   Texture(TextureType::Type type, Pixel::Format format, ImageDimensions size);
171
172   /**
173    * Constructor from native image
174    * @param[in] nativeImageInterface The native image
175    */
176   Texture(NativeImageInterfacePtr nativeImageInterface);
177
178   /**
179    * Second stage initialization of the Texture
180    */
181   void Initialize();
182
183 protected:
184   /**
185    * A reference counted object may only be deleted by calling Unreference()
186    */
187   ~Texture() override;
188
189 private: // unimplemented methods
190   Texture(const Texture&);
191   Texture& operator=(const Texture&);
192
193 private:                                               // data
194   Internal::EventThreadServices& mEventThreadServices; ///<Used to send messages to the render thread via update thread
195   Internal::Render::Texture*     mRenderObject;        ///<The Render::Texture associated to this texture
196
197   NativeImageInterfacePtr mNativeImage; ///< Pointer to native image
198   ImageDimensions         mSize;        ///< Size of the texture
199   Dali::TextureType::Type mType;        ///< Texture type (cached)
200   Pixel::Format           mFormat;      ///< Pixel format
201 };
202
203 } // namespace Internal
204
205 // Helpers for public-api forwarding methods
206 inline Internal::Texture& GetImplementation(Dali::Texture& handle)
207 {
208   DALI_ASSERT_ALWAYS(handle && "Texture handle is empty");
209
210   BaseObject& object = handle.GetBaseObject();
211
212   return static_cast<Internal::Texture&>(object);
213 }
214
215 inline const Internal::Texture& GetImplementation(const Dali::Texture& handle)
216 {
217   DALI_ASSERT_ALWAYS(handle && "Texture handle is empty");
218
219   const BaseObject& object = handle.GetBaseObject();
220
221   return static_cast<const Internal::Texture&>(object);
222 }
223
224 } // namespace Dali
225
226 #endif // DALI_INTERNAL_TEXTURE_H