Allow new texture to be created from native images
[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) 2016 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/public-api/common/dali-common.h> // DALI_ASSERT_ALWAYS
23 #include <dali/public-api/common/intrusive-ptr.h> // Dali::IntrusivePtr
24 #include <dali/public-api/object/base-object.h>
25 #include <dali/public-api/images/pixel.h>
26 #include <dali/devel-api/rendering/texture.h> // Dali::Internal::Render::Texture
27 #include <dali/internal/event/common/event-thread-services.h>
28
29 namespace Dali
30 {
31 namespace Internal
32 {
33 namespace Render
34 {
35 class NewTexture;
36 }
37
38 class NewTexture;
39 typedef IntrusivePtr<NewTexture> NewTexturePtr;
40
41 class NewTexture : public BaseObject
42 {
43 public:
44
45   /**
46    * @brief Structure used to pass parameters to the Upload method
47    */
48   struct UploadParams
49   {
50     unsigned int layer;    ///< Specifies the layer of a cube map or array texture
51     unsigned int mipmap;   ///< Specifies the level-of-detail number. Level 0 is the base image level. Level n is the nth mipmap reduction image.
52     unsigned int xOffset;  ///< Specifies a texel offset in the x direction within the texture array.
53     unsigned int yOffset;  ///< Specifies a texel offset in the y direction within the texture array.
54     unsigned int width;    ///< Specifies the width of the texture subimage
55     unsigned int height;   ///< Specifies the height of the texture subimage.
56   };
57
58   /**
59    * @brief Create a new Texture.
60    *
61    * @param[in] type The type of the texture
62    * @param[in] format The format of the pixel data
63    * @param[in] width The width of the texture
64    * @param[in] height The height of the texture
65    * @return A smart-pointer to the newly allocated Texture.
66    */
67   static NewTexturePtr New(TextureType::Type type, Pixel::Format format, unsigned int width, unsigned int height);
68
69   /**
70    * @brief Creates a new Texture from a native image
71    * @param[in] nativeImageInterface The native image
72    * @return A smart-pointer to the newly allocated Texture.
73    */
74   static NewTexturePtr New( NativeImageInterface& nativeImageInterface );
75
76   /**
77    * @brief Get the texture render object
78    *
79    * @return the texture render object
80    */
81   Render::NewTexture* GetRenderObject() const;
82
83   /**
84    * @copydoc Dali::Texture::Upload()
85    */
86   void Upload( Vector<unsigned char>& buffer );
87
88   /**
89    * @copydoc Dali::Texture::Upload()
90    */
91   void Upload( Vector<unsigned char>& buffer,
92                unsigned int layer, unsigned int mipmap,
93                unsigned int xOffset, unsigned int yOffset,
94                unsigned int width, unsigned int height );
95
96   /**
97    * @copydoc Dali::Texture::GenerateMipmaps()
98    */
99   void GenerateMipmaps();
100
101   /**
102    * @copydoc Dali::Texture::GetWidth()
103    */
104   unsigned int GetWidth() const;
105
106   /**
107    * @copydoc Dali::Texture::GetHeight()
108    */
109   unsigned int GetHeight() const;
110
111
112 private: // implementation
113
114   /**
115    * Constructor
116    * @param[in] type The type of the texture
117    * @param[in] format The format of the pixel data
118    * @param[in] width The width of the texture
119    * @param[in] height The height of the texture
120    */
121   NewTexture(TextureType::Type type, Pixel::Format format, unsigned int width, unsigned int height );
122
123   /**
124    * Constructor from native image
125    * @param[in] nativeImageInterface The native image
126    */
127   NewTexture( NativeImageInterfacePtr nativeImageInterface );
128
129   /**
130    * Second stage initialization of the Texture
131    */
132   void Initialize();
133
134 protected:
135
136   /**
137    * A reference counted object may only be deleted by calling Unreference()
138    */
139   virtual ~NewTexture();
140
141 private: // unimplemented methods
142   NewTexture( const NewTexture& );
143   NewTexture& operator=( const NewTexture& );
144
145 private: // data
146
147   /**
148    * @brief Helper method to check that upload parameters are correct
149    * @param[in] buffer A vector with the data to be uploaded
150    * @param[in] parameters The uploading parameters
151    */
152   bool CheckUploadParametres( const Vector<unsigned char>& buffer, const UploadParams& parameters) const;
153
154   Internal::EventThreadServices& mEventThreadServices;    ///<Used to send messages to the render thread via update thread
155   Internal::Render::NewTexture* mRenderObject;            ///<The Render::Texture associated to this texture
156
157   NativeImageInterfacePtr mNativeImage; ///< Pointer to native image
158   Dali::TextureType::Type mType;      ///< Texture type (cached)
159   Pixel::Format mFormat;              ///< Pixel format
160   unsigned int mWidth;                ///< Width of the texture
161   unsigned int mHeight;               ///< Height of the texture
162 };
163
164 } // namespace Internal
165
166 // Helpers for public-api forwarding methods
167 inline Internal::NewTexture& GetImplementation(Dali::Texture& handle)
168 {
169   DALI_ASSERT_ALWAYS(handle && "Texture handle is empty");
170
171   BaseObject& object = handle.GetBaseObject();
172
173   return static_cast<Internal::NewTexture&>(object);
174 }
175
176 inline const Internal::NewTexture& GetImplementation(const Dali::Texture& handle)
177 {
178   DALI_ASSERT_ALWAYS(handle && "Texture handle is empty");
179
180   const BaseObject& object = handle.GetBaseObject();
181
182   return static_cast<const Internal::NewTexture&>(object);
183 }
184
185 } // namespace Dali
186
187 #endif // DALI_INTERNAL_TEXTURE_H