Stop calling destroy methods of texture and FrameBuffers on RenderManager destructor
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / render-texture.h
1 #ifndef DALI_INTERNAL_RENDER_TEXTURE_H
2 #define DALI_INTERNAL_RENDER_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 // EXTERNAL INCLUDES
21 #include <string>
22
23 // INTERNAL INCLUDES
24 #include <dali/public-api/rendering/sampler.h>
25 #include <dali/public-api/rendering/texture.h>
26 #include <dali/internal/event/rendering/texture-impl.h>
27 #include <dali/internal/render/gl-resources/context.h>
28 #include <dali/internal/render/renderers/render-sampler.h>
29 #include <dali/integration-api/gl-defines.h>
30 #include <dali/integration-api/resource-declarations.h>
31
32 namespace Dali
33 {
34 namespace Internal
35 {
36 namespace Render
37 {
38 struct Sampler;
39
40 /**
41  * This class is the mapping between texture id, sampler and sampler uniform name
42  */
43 class Texture
44 {
45 public:
46
47   /**
48    * Constructor
49    */
50   Texture()
51   : mSampler( 0 ),
52     mTextureId( Integration::InvalidResourceId )
53   {}
54
55   /**
56    * Constructor
57    */
58   Texture( Integration::ResourceId textureId, Render::Sampler* sampler )
59   : mSampler( sampler ),
60     mTextureId( textureId)
61   {}
62
63   /**
64    * Destructor
65    */
66   ~Texture()
67   {}
68
69   /*
70    * Get the Render::Sampler used by the texture
71    * @Return The Render::Sampler being used or 0 if using the default
72    */
73   inline const Render::Sampler* GetSampler() const
74   {
75     return mSampler;
76   }
77
78 public: // called from RenderThread
79
80   /**
81    * Get the texture ID
82    * @return the id of the associated texture
83    */
84   inline Integration::ResourceId GetTextureId() const
85   {
86     return mTextureId;
87   }
88
89 private:
90
91   Render::Sampler*        mSampler;
92   Integration::ResourceId mTextureId;
93 };
94
95
96 //TODO : Remove the old Render::Texture class (see above) once it is no longer needed by Image
97 class NewTexture
98 {
99 public:
100
101   typedef Dali::TextureType::Type Type;
102
103   /**
104    * Constructor
105    * @param[in] type The type of the texture
106    * @param[in] format The format of the pixel data
107    * @param[in] width The width of the texture
108    * @param[in] height The height of the texture
109    */
110   NewTexture( Type type, Pixel::Format format, unsigned int width, unsigned int height );
111
112   /**
113    * Constructor from native image
114    * @param[in] nativeImageInterface The native image
115    */
116   NewTexture( NativeImageInterfacePtr nativeImageInterface );
117
118   /**
119    * Destructor
120    */
121   ~NewTexture();
122
123   /**
124    * Creates the texture in the GPU.
125    * Creates the texture and reserves memory for the first mipmap level
126    * @param[in] context The GL context
127    */
128   void Initialize(Context& context);
129
130   /**
131    * Deletes the texture from the GPU
132    * @param[in] context The GL context
133    */
134   void Destroy( Context& context );
135
136   /**
137    * Called by RenderManager to inform the texture that the context has been destroyed
138    */
139   void GlContextDestroyed();
140
141   /**
142    * Uploads data to the texture.
143    * @param[in] context The GL context
144    * @param[in] pixelData A pixel data object
145    * @param[in] params Upload parameters. See UploadParams
146    */
147   void Upload( Context& context, PixelDataPtr pixelData, const Internal::NewTexture::UploadParams& params );
148
149   /**
150    * Bind the texture to the given texture unit and applies the given sampler
151    * @param[in] context The GL context
152    * @param[in] textureUnit the texture unit
153    * @param[in] sampler The sampler to be used with the texture
154    * @return true if the bind succeeded, false otherwise
155    */
156   bool Bind( Context& context, unsigned int textureUnit, Render::Sampler* sampler );
157
158   /**
159    * Auto generates mipmaps for the texture
160    * @param[in] context The GL context
161    */
162   void GenerateMipmaps( Context& context );
163
164   /**
165    * Retrieve wheter the texture has an alpha channel
166    * @return True if the texture has alpha channel, false otherwise
167    */
168   bool HasAlphaChannel();
169
170   /**
171    * Get the id of the texture
172    * @return Id of the texture
173    */
174   GLuint GetId()
175   {
176     return mId;
177   }
178
179   /**
180    * Get the width of the texture
181    * @return Width of the texture
182    */
183   unsigned int GetWidth() const
184   {
185     return mWidth;
186   }
187
188   /**
189    * Get the height of the texture
190    * @return Height of the texture
191    */
192   unsigned int GetHeight() const
193   {
194     return mHeight;
195   }
196
197   /**
198    * Get the type of the texture
199    * @return Type of the texture
200    */
201   Type GetType() const
202   {
203     return mType;
204   }
205
206 private:
207
208   /**
209    * Helper method to apply a sampler to the texture
210    * @param[in] context The GL context
211    * @param[in] sampler The sampler
212    */
213   void ApplySampler( Context& context, Render::Sampler* sampler );
214
215   GLuint mId;                         ///<Id of the texture
216   Type mType;                         ///<Type of the texture
217   Render::Sampler mSampler;           ///<The current sampler state
218   NativeImageInterfacePtr mNativeImage; ///<Pointer to native image
219   GLenum mInternalFormat;             ///<The format of the pixel data
220   GLenum mPixelDataType;              ///<The data type of the pixel data
221   unsigned int mWidth;                ///<Widht of the texture
222   unsigned int mHeight;               ///<Height of the texture
223   bool mHasAlpha : 1;                 ///<Whether the format has an alpha channel
224   bool mIsCompressed : 1;             ///<Whether the format is compressed
225 };
226
227
228 } // namespace Render
229
230 } // namespace Internal
231
232 } // namespace Dali
233
234
235 #endif //  DALI_INTERNAL_RENDER_TEXTURE_H