Merge "Deprecated Layer::TREE_DEPTH_MULTIPLIER and fixed mis-usage." into devel/master
[platform/core/uifw/dali-core.git] / dali / internal / render / gl-resources / bitmap-texture.h
1 #ifndef __DALI_INTERNAL_BITMAP_TEXTURE_H__
2 #define __DALI_INTERNAL_BITMAP_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 // EXTERNAL INCLUDES
22 #include <stdint.h> // for uint32_t
23
24 // INTERNAL INCLUDES
25 #include <dali/internal/common/message.h>
26 #include <dali/integration-api/bitmap.h>
27 #include <dali/integration-api/debug.h>
28 #include <dali/internal/render/gl-resources/texture-cache.h>
29 #include <dali/internal/render/gl-resources/gl-texture.h>
30
31 namespace Dali
32 {
33
34 namespace Internal
35 {
36
37 class BitmapTexture;
38 typedef IntrusivePtr<BitmapTexture> BitmapTexturePointer;
39
40 /**
41  *
42  * Texture class.
43  * If you want to load a file to a BitmapTexture use
44  * TextureManager::GetTexture()
45  *
46  */
47 class BitmapTexture : public Texture
48 {
49 public:
50   /**
51    * Constructor
52    * Creates a new texture object from a Bitmap
53    * @param[in] bitmap The Bitmap
54    * @param[in] bitmapPackedPixelsProfile The Bitmap features related to an addressable array of raw pixel data
55    * @param     context The GL context
56    */
57   BitmapTexture(Integration::Bitmap* const bitmap, const Integration::Bitmap::PackedPixelsProfile * const bitmapPackedPixelsProfile, Context& context, ResourcePolicy::Discardable discardPolicy);
58
59   /**
60    * Constructor
61    * Creates a new texture object
62    * @param[in] width width in pixels
63    * @param[in] height height in pixels
64    * @param[in] pixelFormat pixel format
65    * @param[in] clearPixels True if the pixel data should be cleared before gl texture creation
66    * @param[in] context Dali context
67    */
68   BitmapTexture( unsigned int width, unsigned int height, Pixel::Format pixelFormat,
69                  bool clearPixels, Context& context, ResourcePolicy::Discardable discardPolicy );
70
71   /**
72    * Destructor.
73    */
74   virtual ~BitmapTexture();
75
76   /**
77    * Retrieve the bitmap
78    * @return The bitmap or NULL if already discarded
79    */
80   Integration::Bitmap* GetBitmap() { return mBitmap.Get(); }
81
82 public:
83
84   /**
85    * @copydoc Texture::Init
86    */
87   virtual bool Init();
88
89   /**
90    * @copydoc Texture::GetWidth
91    */
92   virtual unsigned int GetWidth() const;
93
94   /**
95    * @copydoc Texture::GetHeight
96    */
97   virtual unsigned int GetHeight() const;
98
99   /**
100    * @copydoc Texture::HasAlphaChannel
101    */
102   virtual bool HasAlphaChannel() const;
103
104   /**
105    * @copydoc Texture::IsFullyOpaque
106    */
107   virtual bool IsFullyOpaque() const;
108
109   /**
110    * Replace current bitmap with a fresh one, for instance after a Bitmap has
111    * been reloaded.
112    * @param[in] bitmap The new bitmap
113    */
114   virtual void Update( Integration::Bitmap* bitmap );
115
116   /**
117    * Update part of the texture with a different bitmap
118    * @param[in] srcBitmap The bitmap to copy from
119    * @param [in] xOffset Specifies an offset in the x direction within the texture
120    * @param [in] yOffset Specifies an offset in the y direction within the texture
121    */
122   virtual void Update( Integration::Bitmap* srcBitmap, std::size_t xOffset, std::size_t yOffset );
123
124   /**
125    * Update part of the texture with a pixel buffer
126    * @param[in] srcPixelData The pixel data to copy from
127    * @param [in] xOffset Specifies an offset in the x direction within the texture
128    * @param [in] yOffset Specifies an offset in the y direction within the texture
129    */
130   virtual void Update( PixelData* srcPixelData, std::size_t xOffset, std::size_t yOffset );
131
132   /**
133    * Bitmap area has been modified - update the texture appropriately.
134    * @pre The bitmap hasn't been discarded (should be external type)
135    * @param[in] area The updated area
136    */
137   virtual void UpdateArea( const RectArea& area );
138
139   /**
140    * @return Return true if the texture should be updated on GL texture creation.
141    */
142   virtual bool UpdateOnCreate();
143
144 protected:
145   /**
146    * @copydoc Texture::CreateGlTexture
147    */
148   virtual bool CreateGlTexture();
149
150 private:
151
152   /**
153    * Uploads changes to GPU after Bitmap buffer has changed.
154    * @param [in] updateArea area which changed
155    * @param[in] pixels The pixel data
156    */
157   void AreaUpdated( const RectArea& updateArea, const unsigned char* pixels );
158
159   /**
160    * Assigns the bitmap data to an OpenGL texture
161    * Creates a new texture object and copies
162    * the image data held in the pixels parameter
163    * @pre The texture has to have a width/height that is a power of 2.
164    * @param[in] generateTexture True if we should generate a GL texture id
165    * @param[in] pixels The pixel data
166    */
167   void AssignBitmap( bool generateTexture, const unsigned char* pixels );
168
169   /**
170    * If the discard policy is not RETAIN, then discards the bitmap's pixel buffer
171    */
172   void DiscardBitmapBuffer();
173
174   /**
175    * Update part of the texture.
176    * @param [in] pixels The pixel buffer.
177    * @param [in] width  The buffer width in pixels
178    * @param [in] height The buffer height in pixels
179    * @param [in] pixelFormat The pixel format
180    * @param [in] xOffset Specifies an offset in the x direction within the texture
181    * @param [in] yOffset Specifies an offset in the y direction within the texture
182    */
183   void Update( const unsigned char* pixels, std::size_t width, std::size_t height, Pixel::Format pixelFormat, std::size_t xOffset, std::size_t yOffset );
184
185 private:
186   Integration::BitmapPtr      mBitmap;            ///< The Bitmap the Texture was created from (may be NULL)
187   Pixel::Format               mPixelFormat;       ///< Pack pixel format into bitfield
188   ResourcePolicy::Discardable mDiscardPolicy;     ///< The bitmap discard policy
189   bool                        mClearPixels;       ///< true if initial texture should be cleared on creation
190
191   // Changes scope, should be at end of class
192   DALI_LOG_OBJECT_STRING_DECLARATION;
193 };
194
195 }  //namespace Internal
196
197 } //namespace Dali
198
199 #endif //__DALI_INTERNAL_BITMAP_TEXTURE_H__