Merge "Change PixelData to use the handle/body pattern" into devel/master
[platform/core/uifw/dali-core.git] / automated-tests / src / dali-devel / utc-Dali-Atlas.cpp
1 /*
2  * Copyright (c) 2014 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/devel-api/images/pixel-data.h>
25 #include <dali-test-suite-utils.h>
26 #include <test-native-image.h>
27
28 using namespace Dali;
29
30 namespace
31 {
32 static const char* gTestImageFilename = "icon_wrt.png";
33
34 void PrepareResourceImage( TestApplication& application, unsigned int imageWidth, unsigned int imageHeight, Pixel::Format pixelFormat )
35 {
36   TestPlatformAbstraction& platform = application.GetPlatform();
37   platform.SetClosestImageSize(Vector2( imageWidth, imageHeight));
38
39   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::OWNED_RETAIN );
40   Integration::PixelBuffer* pixbuffer = bitmap->GetPackedPixelsProfile()->ReserveBuffer( pixelFormat, imageWidth, imageHeight, imageWidth, imageHeight );
41   unsigned int bytesPerPixel = GetBytesPerPixel(  pixelFormat );
42   unsigned int initialColor = 0xFF;
43   memset( pixbuffer, initialColor, imageHeight*imageWidth*bytesPerPixel);
44
45   Integration::ResourcePointer resourcePtr(bitmap);
46   platform.SetSynchronouslyLoadedResource( resourcePtr );
47 }
48
49 PixelData CreatePixelData(unsigned int width, unsigned int height, Pixel::Format pixelFormat)
50 {
51   unsigned int bufferSize = width*height*Pixel::GetBytesPerPixel( pixelFormat );
52   unsigned char* buffer = new unsigned char [ bufferSize ];
53
54   return PixelData::New( buffer, width, height, pixelFormat, PixelData::DELETE_ARRAY );
55 }
56
57 }
58
59 void utc_dali_atlas_startup(void)
60 {
61   test_return_value = TET_UNDEF;
62 }
63
64 void utc_dali_atlas_cleanup(void)
65 {
66   test_return_value = TET_PASS;
67 }
68
69 int UtcDaliAtlasNew(void)
70 {
71   TestApplication application;
72
73   // invoke default handle constructor
74   Atlas atlas;
75
76   DALI_TEST_CHECK( !atlas );
77
78   // initialise handle
79   atlas = Atlas::New( 16, 16 );
80
81   DALI_TEST_CHECK( atlas );
82   END_TEST;
83 }
84
85 int UtcDaliAtlasCopyConstructor(void)
86 {
87   TestApplication application;
88
89   Atlas atlas = Atlas::New( 16, 16);
90   Atlas atlasCopy(atlas);
91
92
93   DALI_TEST_EQUALS( (bool)atlasCopy, true, TEST_LOCATION );
94   END_TEST;
95 }
96
97 int UtcDaliAtlasAssignmentOperator(void)
98 {
99   TestApplication application;
100
101   Atlas atlas = Atlas::New( 16, 16);
102
103   Atlas atlas2;
104   DALI_TEST_EQUALS( (bool)atlas2, false, TEST_LOCATION );
105
106   atlas2 = atlas;
107   DALI_TEST_EQUALS( (bool)atlas2, true, TEST_LOCATION );
108
109   END_TEST;
110 }
111
112 int UtcDaliAtlasDownCast(void)
113 {
114   TestApplication application;
115
116   Atlas atlas = Atlas::New( 16, 16);
117
118   BaseHandle handle(atlas);
119   Atlas atlasDowncast = Atlas::DownCast(handle);
120   DALI_TEST_EQUALS( (bool)atlasDowncast, true, TEST_LOCATION );
121
122   Handle handle2 = Handle::New(); // Create a custom object
123   Atlas atlas2 = Atlas::DownCast(handle2);
124   DALI_TEST_EQUALS( (bool)atlas2, false, TEST_LOCATION );
125
126   END_TEST;
127 }
128
129 int UtcDaliAtlasClear(void)
130 {
131   TestApplication application;
132
133   Atlas atlas = Atlas::New( 16, 32, Pixel::RGBA8888 );
134   DALI_TEST_CHECK( atlas );
135
136   atlas.Clear(Color::TRANSPARENT);
137
138   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
139   callStack.Reset();
140   callStack.Enable(true);
141   application.SendNotification();
142   application.Render(16);
143   application.Render(16);
144   application.SendNotification();
145   application.Render(16);
146   application.SendNotification();
147   callStack.Enable(false);
148
149   DALI_TEST_CHECK( callStack.FindMethodAndParams("TexSubImage2D", "0, 0, 16, 32") );
150
151   END_TEST;
152 }
153
154
155 // Upload resource image, buffer image & pixel data with same pixel format
156 int UtcDaliAtlasUpload01P(void)
157 {
158   TestApplication application;
159
160   Atlas atlas = Atlas::New( 16, 40, Pixel::RGBA8888 );
161   DALI_TEST_CHECK( atlas );
162
163   // Using same pixel format
164   PixelBuffer* buffer = new PixelBuffer[16 * 16 * 4];
165   BufferImage image = BufferImage::New( buffer, 16, 16, Pixel::RGBA8888 );
166   DALI_TEST_CHECK( atlas.Upload( image, 0, 0 ) );
167
168   PrepareResourceImage( application, 16, 16, Pixel::RGBA8888 );
169   DALI_TEST_CHECK( atlas.Upload( gTestImageFilename, 0, 16 ) );
170
171   PixelData pixelData = CreatePixelData( 6,8,Pixel::RGBA8888 );
172   DALI_TEST_CHECK( atlas.Upload( pixelData, 2, 32 ) );
173
174   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
175   callStack.Reset();
176   callStack.Enable(true);
177   application.SendNotification();
178   application.Render(16);
179   application.Render(16);
180   application.SendNotification();
181   application.Render(16);
182   application.SendNotification();
183   callStack.Enable(false);
184
185   DALI_TEST_CHECK( callStack.FindMethodAndParams("TexSubImage2D", "0, 0, 16, 16") );
186   DALI_TEST_CHECK( callStack.FindMethodAndParams("TexSubImage2D", "0, 16, 16, 16") );
187   DALI_TEST_CHECK( callStack.FindMethodAndParams("TexSubImage2D", "2, 32, 6, 8") );
188
189   END_TEST;
190 }
191
192 // Upload resource image, buffer image & pixel data with different pixel format
193 int UtcDaliAtlasUpload02P(void)
194 {
195   TestApplication application;
196
197   Atlas atlas = Atlas::New( 20, 20, Pixel::RGBA8888 );
198   DALI_TEST_CHECK( atlas );
199
200   // Using different pixel format
201   PixelBuffer* buffer = new PixelBuffer[16 * 16 * 3];
202   BufferImage image = BufferImage::New( buffer, 16, 16, Pixel::RGB888 );
203   DALI_TEST_CHECK( atlas.Upload( image, 0, 0 ) );
204
205   PrepareResourceImage( application, 12, 12, Pixel::A8 );
206   DALI_TEST_CHECK( atlas.Upload( gTestImageFilename, 6, 6 ) );
207
208   PixelData pixelData = CreatePixelData( 8,8,Pixel::LA88 );
209   DALI_TEST_CHECK( atlas.Upload( pixelData, 10, 10 ) );
210
211   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
212   callStack.Reset();
213   callStack.Enable(true);
214   application.SendNotification();
215   application.Render(16);
216   application.Render(16);
217   application.SendNotification();
218   application.Render(16);
219   application.SendNotification();
220   callStack.Enable(false);
221
222   DALI_TEST_CHECK( callStack.FindMethodAndParams("TexSubImage2D", "0, 0, 16, 16") );
223   DALI_TEST_CHECK( callStack.FindMethodAndParams("TexSubImage2D", "6, 6, 12, 12") );
224   DALI_TEST_CHECK( callStack.FindMethodAndParams("TexSubImage2D", "10, 10, 8, 8") );
225
226   END_TEST;
227 }
228
229 // Upload resource image, buffer image & pixel data which cannot fit into the atlas with given offset
230 int UtcDaliAtlasUploadN(void)
231 {
232   TestApplication application;
233
234   Atlas atlas = Atlas::New( 16, 16, Pixel::RGBA8888 );
235   DALI_TEST_CHECK( atlas );
236
237   // Using image cannot fit into atlas at the given offsets
238   PixelBuffer* buffer = new PixelBuffer[24 * 24 * 4];
239   BufferImage image = BufferImage::New( buffer, 24, 24, Pixel::RGBA8888 );
240   DALI_TEST_CHECK( !atlas.Upload( image, 0, 0 ) );
241
242   PrepareResourceImage( application, 16, 16, Pixel::RGBA8888 );
243   DALI_TEST_CHECK( !atlas.Upload( gTestImageFilename, 10, 10 ) );
244
245   PixelData pixelData = CreatePixelData( 6,6,Pixel::RGBA8888 );
246   DALI_TEST_CHECK( !atlas.Upload( pixelData, 11, 11 ) );
247
248   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
249   callStack.Reset();
250   callStack.Enable(true);
251   application.SendNotification();
252   application.Render(16);
253   application.Render(16);
254   application.SendNotification();
255   application.Render(16);
256   application.SendNotification();
257   callStack.Enable(false);
258
259   // none of these three upload() call sends texture to GPU
260   DALI_TEST_CHECK( ! callStack.FindMethodAndParams("TexSubImage2D", "0, 0, 24, 24") );
261   DALI_TEST_CHECK( ! callStack.FindMethodAndParams("TexSubImage2D", "10, 10, 16, 16") );
262   DALI_TEST_CHECK( ! callStack.FindMethodAndParams("TexSubImage2D", "11, 11, 6, 6") );
263
264   END_TEST;
265 }
266
267
268
269