FrameBuffer::Format changed to bit-mask Attachment: Core
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-Atlas.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <iostream>
19 #include <algorithm>
20 #include <stdlib.h>
21 #include <dali/public-api/dali-core.h>
22 #include <dali/integration-api/bitmap.h>
23 #include <dali/devel-api/images/atlas.h>
24 #include <dali-test-suite-utils.h>
25 #include <test-native-image.h>
26
27 using namespace Dali;
28
29 namespace
30 {
31 static const char* gTestImageFilename = "icon_wrt.png";
32
33 void PrepareResourceImage( TestApplication& application, unsigned int imageWidth, unsigned int imageHeight, Pixel::Format pixelFormat )
34 {
35   TestPlatformAbstraction& platform = application.GetPlatform();
36   platform.SetClosestImageSize(Vector2( imageWidth, imageHeight));
37
38   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::OWNED_RETAIN );
39   Integration::PixelBuffer* pixbuffer = bitmap->GetPackedPixelsProfile()->ReserveBuffer( pixelFormat, imageWidth, imageHeight, imageWidth, imageHeight );
40   unsigned int bytesPerPixel = GetBytesPerPixel(  pixelFormat );
41   unsigned int initialColor = 0xFF;
42   memset( pixbuffer, initialColor, imageHeight*imageWidth*bytesPerPixel);
43
44   Integration::ResourcePointer resourcePtr(bitmap);
45   platform.SetSynchronouslyLoadedResource( resourcePtr );
46 }
47
48 PixelData CreatePixelData(unsigned int width, unsigned int height, Pixel::Format pixelFormat)
49 {
50   unsigned int bufferSize = width*height*Pixel::GetBytesPerPixel( pixelFormat );
51   unsigned char* buffer = new unsigned char [ bufferSize ];
52
53   return PixelData::New( buffer, bufferSize, width, height, pixelFormat, PixelData::DELETE_ARRAY );
54 }
55
56 }
57
58 void utc_dali_atlas_startup(void)
59 {
60   test_return_value = TET_UNDEF;
61 }
62
63 void utc_dali_atlas_cleanup(void)
64 {
65   test_return_value = TET_PASS;
66 }
67
68 int UtcDaliAtlasNew(void)
69 {
70   TestApplication application;
71
72   // invoke default handle constructor
73   Atlas atlas;
74
75   DALI_TEST_CHECK( !atlas );
76
77   // initialise handle
78   atlas = Atlas::New( 16, 16 );
79
80   DALI_TEST_CHECK( atlas );
81   END_TEST;
82 }
83
84 int UtcDaliAtlasCopyConstructor(void)
85 {
86   TestApplication application;
87
88   Atlas atlas = Atlas::New( 16, 16);
89   Atlas atlasCopy(atlas);
90
91
92   DALI_TEST_EQUALS( (bool)atlasCopy, true, TEST_LOCATION );
93   END_TEST;
94 }
95
96 int UtcDaliAtlasAssignmentOperator(void)
97 {
98   TestApplication application;
99
100   Atlas atlas = Atlas::New( 16, 16);
101
102   Atlas atlas2;
103   DALI_TEST_EQUALS( (bool)atlas2, false, TEST_LOCATION );
104
105   atlas2 = atlas;
106   DALI_TEST_EQUALS( (bool)atlas2, true, TEST_LOCATION );
107
108   END_TEST;
109 }
110
111 int UtcDaliAtlasDownCast(void)
112 {
113   TestApplication application;
114
115   Atlas atlas = Atlas::New( 16, 16);
116
117   BaseHandle handle(atlas);
118   Atlas atlasDowncast = Atlas::DownCast(handle);
119   DALI_TEST_EQUALS( (bool)atlasDowncast, true, TEST_LOCATION );
120
121   Handle handle2 = Handle::New(); // Create a custom object
122   Atlas atlas2 = Atlas::DownCast(handle2);
123   DALI_TEST_EQUALS( (bool)atlas2, false, TEST_LOCATION );
124
125   END_TEST;
126 }
127
128 int UtcDaliAtlasClear(void)
129 {
130   TestApplication application;
131
132   Atlas atlas = Atlas::New( 16, 32, Pixel::RGBA8888 );
133   DALI_TEST_CHECK( atlas );
134
135   atlas.Clear(Color::TRANSPARENT);
136
137   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
138   callStack.Reset();
139   callStack.Enable(true);
140   application.SendNotification();
141   application.Render(16);
142   application.Render(16);
143   application.SendNotification();
144   application.Render(16);
145   application.SendNotification();
146   callStack.Enable(false);
147
148   std::stringstream out;
149   out << GL_TEXTURE_2D <<", "<< 0u << ", " << 0u << ", " << 0u << ", " << 16u <<", "<< 32u;
150   DALI_TEST_CHECK( callStack.FindMethodAndParams("TexSubImage2D", out.str().c_str() ) );
151
152   END_TEST;
153 }
154
155
156 // Upload resource image, buffer image & pixel data with same pixel format
157 int UtcDaliAtlasUpload01P(void)
158 {
159   TestApplication application;
160
161   Atlas atlas = Atlas::New( 16, 40, Pixel::RGBA8888 );
162   DALI_TEST_CHECK( atlas );
163
164   // Using same pixel format
165   PixelBuffer* buffer = new PixelBuffer[16 * 16 * 4];
166   BufferImage image = BufferImage::New( buffer, 16, 16, Pixel::RGBA8888 );
167   DALI_TEST_CHECK( atlas.Upload( image, 0, 0 ) );
168
169   PrepareResourceImage( application, 16, 16, Pixel::RGBA8888 );
170   DALI_TEST_CHECK( atlas.Upload( gTestImageFilename, 0, 16 ) );
171
172   PixelData pixelData = CreatePixelData( 6,8,Pixel::RGBA8888 );
173   DALI_TEST_CHECK( atlas.Upload( pixelData, 2, 32 ) );
174
175   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
176   callStack.Reset();
177   callStack.Enable(true);
178   application.SendNotification();
179   application.Render(16);
180   application.Render(16);
181   application.SendNotification();
182   application.Render(16);
183   application.SendNotification();
184   callStack.Enable(false);
185
186   {
187     std::stringstream out;
188     out << GL_TEXTURE_2D <<", "<< 0u << ", " << 0u << ", " << 0u << ", " << 16u <<", "<< 16u;
189     DALI_TEST_CHECK( callStack.FindMethodAndParams("TexSubImage2D", out.str().c_str() ) );
190   }
191   {
192     std::stringstream out;
193     out << GL_TEXTURE_2D <<", "<< 0u << ", " << 0u << ", " << 16u << ", " << 16u <<", "<< 16u;
194     DALI_TEST_CHECK( callStack.FindMethodAndParams("TexSubImage2D", out.str().c_str() ) );
195   }
196   {
197     std::stringstream out;
198     out << GL_TEXTURE_2D <<", "<< 0u << ", " << 2u << ", " << 32u << ", " << 6u <<", "<< 8u;
199     DALI_TEST_CHECK( callStack.FindMethodAndParams("TexSubImage2D", out.str().c_str() ) );
200   }
201
202   END_TEST;
203 }
204
205 // Upload resource image, buffer image & pixel data with different pixel format
206 int UtcDaliAtlasUpload02P(void)
207 {
208   TestApplication application;
209
210   Atlas atlas = Atlas::New( 20, 20, Pixel::RGBA8888 );
211   DALI_TEST_CHECK( atlas );
212
213   // Using different pixel format
214   PixelBuffer* buffer = new PixelBuffer[16 * 16 * 3];
215   BufferImage image = BufferImage::New( buffer, 16, 16, Pixel::RGB888 );
216   DALI_TEST_CHECK( atlas.Upload( image, 0, 0 ) );
217
218   PrepareResourceImage( application, 12, 12, Pixel::A8 );
219   DALI_TEST_CHECK( atlas.Upload( gTestImageFilename, 6, 6 ) );
220
221   PixelData pixelData = CreatePixelData( 8,8,Pixel::LA88 );
222   DALI_TEST_CHECK( atlas.Upload( pixelData, 10, 10 ) );
223
224   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
225   callStack.Reset();
226   callStack.Enable(true);
227   application.SendNotification();
228   application.Render(16);
229   application.Render(16);
230   application.SendNotification();
231   application.Render(16);
232   application.SendNotification();
233   callStack.Enable(false);
234
235   {
236     std::stringstream out;
237     out << GL_TEXTURE_2D <<", "<< 0u << ", " << 0u << ", " << 0u << ", " << 16u <<", "<< 16u;
238     DALI_TEST_CHECK( callStack.FindMethodAndParams("TexSubImage2D", out.str().c_str() ) );
239   }
240   {
241     std::stringstream out;
242     out << GL_TEXTURE_2D <<", "<< 0u << ", " << 6u << ", " << 6u << ", " << 12u <<", "<< 12u;
243     DALI_TEST_CHECK( callStack.FindMethodAndParams("TexSubImage2D", out.str().c_str() ) );
244   }
245   {
246     std::stringstream out;
247     out << GL_TEXTURE_2D <<", "<< 0u << ", " << 10u << ", " << 10u << ", " << 8u <<", "<< 8u;
248     DALI_TEST_CHECK( callStack.FindMethodAndParams("TexSubImage2D", out.str().c_str() ) );
249   }
250
251   END_TEST;
252 }
253
254 // Upload resource image, buffer image & pixel data which cannot fit into the atlas with given offset
255 int UtcDaliAtlasUploadN(void)
256 {
257   TestApplication application;
258
259   Atlas atlas = Atlas::New( 16, 16, Pixel::RGBA8888 );
260   DALI_TEST_CHECK( atlas );
261
262   // Using image cannot fit into atlas at the given offsets
263   PixelBuffer* buffer = new PixelBuffer[24 * 24 * 4];
264   BufferImage image = BufferImage::New( buffer, 24, 24, Pixel::RGBA8888 );
265   DALI_TEST_CHECK( !atlas.Upload( image, 0, 0 ) );
266
267   PrepareResourceImage( application, 16, 16, Pixel::RGBA8888 );
268   DALI_TEST_CHECK( !atlas.Upload( gTestImageFilename, 10, 10 ) );
269
270   PixelData pixelData = CreatePixelData( 6,6,Pixel::RGBA8888 );
271   DALI_TEST_CHECK( !atlas.Upload( pixelData, 11, 11 ) );
272
273   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
274   callStack.Reset();
275   callStack.Enable(true);
276   application.SendNotification();
277   application.Render(16);
278   application.Render(16);
279   application.SendNotification();
280   application.Render(16);
281   application.SendNotification();
282   callStack.Enable(false);
283
284   // none of these three upload() call sends texture to GPU
285   DALI_TEST_CHECK( ! callStack.FindMethodAndParams("TexSubImage2D", "0, 0, 24, 24") );
286   DALI_TEST_CHECK( ! callStack.FindMethodAndParams("TexSubImage2D", "10, 10, 16, 16") );
287   DALI_TEST_CHECK( ! callStack.FindMethodAndParams("TexSubImage2D", "11, 11, 6, 6") );
288
289   END_TEST;
290 }
291
292
293
294