2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
31 static const char* gTestImageFilename = "icon_wrt.png";
33 void PrepareResourceImage( TestApplication& application, unsigned int imageWidth, unsigned int imageHeight, Pixel::Format pixelFormat )
35 TestPlatformAbstraction& platform = application.GetPlatform();
36 platform.SetClosestImageSize(Vector2( imageWidth, imageHeight));
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);
44 Integration::ResourcePointer resourcePtr(bitmap);
45 platform.SetSynchronouslyLoadedResource( resourcePtr );
48 PixelData CreatePixelData(unsigned int width, unsigned int height, Pixel::Format pixelFormat)
50 unsigned int bufferSize = width*height*Pixel::GetBytesPerPixel( pixelFormat );
51 unsigned char* buffer = new unsigned char [ bufferSize ];
53 return PixelData::New( buffer, bufferSize, width, height, pixelFormat, PixelData::DELETE_ARRAY );
58 void utc_dali_atlas_startup(void)
60 test_return_value = TET_UNDEF;
63 void utc_dali_atlas_cleanup(void)
65 test_return_value = TET_PASS;
68 int UtcDaliAtlasNew(void)
70 TestApplication application;
72 // invoke default handle constructor
75 DALI_TEST_CHECK( !atlas );
78 atlas = Atlas::New( 16, 16 );
80 DALI_TEST_CHECK( atlas );
84 int UtcDaliAtlasCopyConstructor(void)
86 TestApplication application;
88 Atlas atlas = Atlas::New( 16, 16);
89 Atlas atlasCopy(atlas);
92 DALI_TEST_EQUALS( (bool)atlasCopy, true, TEST_LOCATION );
96 int UtcDaliAtlasAssignmentOperator(void)
98 TestApplication application;
100 Atlas atlas = Atlas::New( 16, 16);
103 DALI_TEST_EQUALS( (bool)atlas2, false, TEST_LOCATION );
106 DALI_TEST_EQUALS( (bool)atlas2, true, TEST_LOCATION );
111 int UtcDaliAtlasDownCast(void)
113 TestApplication application;
115 Atlas atlas = Atlas::New( 16, 16);
117 BaseHandle handle(atlas);
118 Atlas atlasDowncast = Atlas::DownCast(handle);
119 DALI_TEST_EQUALS( (bool)atlasDowncast, true, TEST_LOCATION );
121 Handle handle2 = Handle::New(); // Create a custom object
122 Atlas atlas2 = Atlas::DownCast(handle2);
123 DALI_TEST_EQUALS( (bool)atlas2, false, TEST_LOCATION );
128 int UtcDaliAtlasClear(void)
130 TestApplication application;
132 Atlas atlas = Atlas::New( 16, 32, Pixel::RGBA8888 );
133 DALI_TEST_CHECK( atlas );
135 atlas.Clear(Color::TRANSPARENT);
137 TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
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);
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() ) );
156 // Upload resource image, buffer image & pixel data with same pixel format
157 int UtcDaliAtlasUpload01P(void)
159 TestApplication application;
161 Atlas atlas = Atlas::New( 16, 40, Pixel::RGBA8888 );
162 DALI_TEST_CHECK( atlas );
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 ) );
169 PrepareResourceImage( application, 16, 16, Pixel::RGBA8888 );
170 DALI_TEST_CHECK( atlas.Upload( gTestImageFilename, 0, 16 ) );
172 PixelData pixelData = CreatePixelData( 6,8,Pixel::RGBA8888 );
173 DALI_TEST_CHECK( atlas.Upload( pixelData, 2, 32 ) );
175 TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
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);
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() ) );
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() ) );
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() ) );
205 // Upload resource image, buffer image & pixel data with different pixel format
206 int UtcDaliAtlasUpload02P(void)
208 TestApplication application;
210 Atlas atlas = Atlas::New( 20, 20, Pixel::RGBA8888 );
211 DALI_TEST_CHECK( atlas );
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 ) );
218 PrepareResourceImage( application, 12, 12, Pixel::A8 );
219 DALI_TEST_CHECK( atlas.Upload( gTestImageFilename, 6, 6 ) );
221 PixelData pixelData = CreatePixelData( 8,8,Pixel::LA88 );
222 DALI_TEST_CHECK( atlas.Upload( pixelData, 10, 10 ) );
224 TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
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);
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() ) );
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() ) );
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() ) );
254 // Upload resource image, buffer image & pixel data which cannot fit into the atlas with given offset
255 int UtcDaliAtlasUploadN(void)
257 TestApplication application;
259 Atlas atlas = Atlas::New( 16, 16, Pixel::RGBA8888 );
260 DALI_TEST_CHECK( atlas );
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 ) );
267 PrepareResourceImage( application, 16, 16, Pixel::RGBA8888 );
268 DALI_TEST_CHECK( !atlas.Upload( gTestImageFilename, 10, 10 ) );
270 PixelData pixelData = CreatePixelData( 6,6,Pixel::RGBA8888 );
271 DALI_TEST_CHECK( !atlas.Upload( pixelData, 11, 11 ) );
273 TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
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);
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") );