Programming guide of DebugRenderer
[platform/core/uifw/dali-toolkit.git] / docs / content / shared-javascript-and-cpp-documentation / texture-atlas.md
1 <!--
2 /**-->
3
4 # Texture Atlases {#textureatlases}
5
6 ## Example demo application
7
8 ![ ](../assets/img/texture-atlas/image-wall.jpg)
9 ![ ](image-wall.jpg)
10   
11
12 Application above is running slow as there are many small individual images displayed (50)
13   
14 | Metric | Result | Explanation |
15 |--------|--------|-------------|
16 | Launch time | Slow | Has to perform: 50 file open requests and multiple reads for each image |
17 | Memory consumption|  High | Has to create:50 Dali::Image objects,50 OpenGL Textures|
18 | Rendering | Slow | Has to perform: 50 glBindTexture calls per frame ( each OpenGL calls takes time) 50 a frame = 3000 calls per second @60 FPS.Texture switching is a major state change in the GPU|
19   
20
21
22 ## Solutions to problem: Use a Texture Atlas
23
24 A texture atlas is simply one larger image that contains smaller images. A texture atlas is sometimes called a
25 sprite sheet, bitmap sheet or texture pack.
26
27 ![ ](../assets/img/texture-atlas/atlas.jpg)
28 ![ ](atlas.jpg)
29   
30 Dali::ImageActor has the ability to display a portion of an image using ImageActor::PixelArea setting.
31 For example to display the first 3 image in the atlas
32   
33 ![ ](../assets/img/texture-atlas/example-javascript-code.jpg)
34 ![ ](example-code.jpg)
35
36 ### Result of using an Atlas
37
38 | Metric | Result | Explanation |
39 |--------|--------|-------------|
40 | Launch time | Fast | Has to perform: - 1 file open request  |
41 | Memory consumption|  Low | Has to create: 1 Dali::Image objects 1 OpenGL Textures|
42 | Rendering | Fast | HHas to perform: 1 glBindTexture calls per frame ( each OpenGL calls takes time) 1 a frame = 6- calls per second @60 FPS.|
43
44   
45 ## Atlas creation guide
46
47 Many tools exist for creating texture atlases.
48 In the following example we are using a tool called TexturePacker as DALi has an exporter script for it.
49 The exporter automatically generates a source file that has the ImageActor::PixelArea pre-defined.
50   
51 - Download http://www.codeandweb.com/texturepacker
52 - Launch TexturePacker
53 - Go to the menu File -> Preferences
54 - Set the "Exporter directory" to be the location of dali-toolkit/texture-atlas-exporter
55   
56 ![ ](../assets/img/texture-atlas/texture-packer-preferences.jpg)
57 ![ ](texture-packer-preferences.jpg)
58   
59 - Restart the application!
60 - Select DALi 3D framework for new project
61   
62 ![ ](../assets/img/texture-atlas/texture-packer-startup.jpg)
63 ![ ](texture-packer-startup.jpg)
64   
65 - Create the atlas
66 ![ ](../assets/img/texture-atlas/texture-packer-add-sprites.jpg)
67 ![ ](texture-packer-add-sprites.jpg)
68 - Click publish to produce the files
69 ![ ](../assets/img/texture-atlas/texture-packer-publish.jpg)
70 ![ ](texture-packer-publish.jpg)
71
72
73
74 ## Using the generated cpp ( contains JavaScript code as well)
75
76 The generated cpp file contains 3 different ways of describing the atlas.
77 Copy and paste the section that best suits your application.
78 -  Lookup table. Includes code for storing the table in a std::map for fast lookup.
79 - constants.
80 - JavaScript property map
81
82 ### Using the JavaScript generated property map
83
84 The property map should be cut and paste in to your application. It just looks like
85   
86 ~~~{.js}
87 var ATLAS_IMAGE_LIST : [
88     { name: "add_user_usericon_bg", x: 2, y:109, w:105, h:105,  blendMode:dali.BLENDING_ON  },
89     { name: "app_background", x: 180, y:183, w:1, h:1,  blendMode:dali.BLENDING_OFF  },
90     { name: "btn_arrow_left", x: 141, y:183, w:11, h:20,  blendMode:dali.BLENDING_ON  },
91     { name: "btn_arrow_right", x: 154, y:183, w:11, h:20,  blendMode:dali.BLENDING_ON  },
92     { name: "icn_app_foc", x: 194, y:2, w:72, h:72,  blendMode:dali.BLENDING_ON  },
93     { name: "icn_app_nor", x: 109, y:109, w:72, h:72, blendMode:dali.BLENDING_ON  }
94     ]
95 var atlas = new dali.ResourceImage( { url: "atlas_filename.png" });
96
97 // display the user icon using the size / position data in the ATLAS_IMAGE_LIST
98 var userIconData = ATLAS_IMAGE_LIST[0];
99 var userIconRect = [ userIconData.x, userIconData.y,userIconData.w,userIconData.h];
100
101 var btnArrowLeft = new dali.ImageActor( atlas, userIconRect );
102 btnArrowLeft.setBlendMode(userIconData.blendMode);
103
104 ~~~
105
106 ![ ](example-javascript-code.jpg)
107
108
109 ### Using the lookup table in C++
110
111 Cut and paste the lookup table code into your application.
112
113 ~~~{.cpp}
114
115 // The following code is automatically generated when TexturePacker publishes to a cpp file.
116 const char* ATLAS_FILE_NAME( "my_first_atlas.png" );  ///< Atlas image filename
117
118 // Structure to hold image name and position within the atlas.
119 struct ImageInfo
120 {
121   const char* name;
122   unsigned int x,y,w,h;
123   Dali::BlendingMode::Type blendMode;  // only enable blending if image has alpha
124 };
125
126
127 // lookup table
128 const ImageInfo ImageAtlas[]=
129 {
130  { "blocks-ball", 2, 198, 51, 51, BlendingMode::ON },
131  { "bubble-ball", 288, 74, 32, 32, BlendingMode::ON },
132  { "gallery-small-52", 2, 2, 128, 128, BlendingMode::OFF },
133  { "icon-change", 219, 2, 37, 34, BlendingMode::ON },
134  { "icon-cluster-carousel", 180, 2, 37, 34, BlendingMode::ON }
135 };
136
137 const ImageInfo* GetImageInfo(const char* name)
138 {
139   typedef std::map< const char*, const ImageInfo* > LookupMap;
140   static LookupMap lookup;
141   if( lookup.empty() )
142   {
143     for( unsigned int i = 0; i < ATLAS_IMAGE_COUNT; ++i)
144     {
145       lookup[ ImageAtlas[i].name ] =  &ImageAtlas[i];
146     }
147   }
148   LookupMap::const_iterator iter = lookup.find(name);
149   if( iter != lookup.end() )
150   {
151    return (*iter).second;
152   }
153   DALI_ASSERT_ALWAYS(0 && "image name not found in atlas");
154   return NULL;
155 }
156
157 ~~~
158
159 To use the lookup table you can do something like this:
160
161 ~~~{.cpp}
162 // Example function on how to get an image info from the table
163
164 std::string fileName = std::string( DALI_IMAGE_DIR ) + ATLAS_FILE_NAME;
165 Image imageImage = Image::New( fileName );
166
167 const ImageInfo* info(NULL);
168
169 info = GetImageInfo("blocks-ball");
170 if( info)
171 {
172   ImageActor ballActor = ImageActor::New( imageAtlas, ImageActor::PixelArea( info->x, info->y, info->w, info->h) );
173   ballActor->SetBlendMode( info->blendMode );
174 }
175 info = GetImageInfo("bubble-ball");
176 if( info)
177 {
178   ImageActor bubbleActor = ImageActor::New( imageAtlas, ImageActor::PixelArea( info->x, info->y, info->w, info->h) );
179   bubbleActor->SetBlendMode( info->blendMode );
180 }
181
182 ~~~
183
184 ### Using the constant definitions (C++)
185
186 1. Cut and paste the constant definition code into your application.
187
188 You'll notice the code below won't compile because C++ variables can't have a dash character.
189 E.g. BLOCKS-BALL, BUBBLE-BALL will cause errors. Do a search and replace for - and replace with underscores.
190 This is one reason why using lookup table which holds the filename as a string maybe easier to use.
191   
192 ~~~{.cpp}
193
194 // The following code is automatically generated when TexturePacker publishes to a cpp file.
195 const char* ATLAS_FILE_NAME( "my_first_atlas.png" );
196
197
198 // Structure to hold position / blend mode within the atlas.
199 struct ImageInfo
200 {
201   ImageInfo(unsigned int x,unsigned int y,unsigned int w,unsigned int h,  Dali::BlendingMode::Type mode)
202   :pixelArea(x,y,w,h),blendMode(mode)
203   {}
204   ImageActor::PixelArea pixelArea;
205   Dali::BlendingMode::Type blendMode;  // only enable blending if image has alpha
206 };
207
208
209 const ImageInfo BLOCKS-BALL( 2, 198, 51, 51 ,BlendingMode::ON );
210 const ImageInfo BUBBLE-BALL( 288, 74, 32, 32 ,BlendingMode::ON );
211 const ImageInfo GALLERY-SMALL-52( 2, 2, 128, 128 ,BlendingMode::OFF );
212 ~~~
213   
214   2. To use it, you can copy example code from the generated cpp file which looks
215 like this
216
217 ~~~{.cpp}
218 void LoadAtlasImages()
219 {
220   std::string fileName = std::string(DALI_IMAGE_DIR) + ATLAS_FILE_NAME;
221   Image atlasImage = Image::New( fileName );
222   ImageActor Blocksball = ImageActor::New( atlasImage,  BLOCKS_BALL.pixelArea);
223   Blocksball.SetBlendMode( BLOCKS_BALL.blendMode );
224
225   ImageActor Bubbleball = ImageActor::New( atlasImage,  BUBBLE_BALL.pixelArea);
226   Bubbleball.SetBlendMode( BUBBLE_BALL.blendMode );
227   ...
228 ~~~
229
230
231 ## Atlas creation tips
232
233 - Compress the atlas  - \link texturecompression Compressing Textures \endlink
234 - Avoid adding large images to the Atlas.
235 - E.g. don't add background images to it. Medium to large images should be kept seperate.
236   
237 ![ ](../assets/img/texture-atlas/atlas-size.jpg)
238 ![ ](atlas-size.jpg)
239   
240
241 - Try to ensure the atlas contains only images that are frequently used.  There's no point in having images taking up GPU texture memory if they're not displayed.
242 - Avoid very large atlases.   Try to create multiple atlases based on how they are used within your application.
243 Alternatively Texture packer has the option to split atlases ( search help for Multipack)
244
245
246
247 @class _Guide_TextureAtlases
248
249 */