2 * Copyright (c) 2014 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-test-suite-utils.h>
24 #include <test-native-image.h>
28 void utc_dali_image_startup(void)
30 test_return_value = TET_UNDEF;
33 void utc_dali_image_cleanup(void)
35 test_return_value = TET_PASS;
38 static const char* gTestImageFilename = "icon_wrt.png";
42 void LoadBitmapResource(TestPlatformAbstraction& platform)
44 Integration::ResourceRequest* request = platform.GetRequest();
45 Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::DISCARD );
46 Integration::ResourcePointer resource(bitmap);
47 bitmap->GetPackedPixelsProfile()->ReserveBuffer(Pixel::RGBA8888, 80, 80, 80, 80);
51 platform.SetResourceLoaded(request->GetId(), request->GetType()->id, resource);
57 int UtcDaliImageDownCast(void)
59 TestApplication application;
60 tet_infoline("Testing Dali::Image::DownCast()");
62 Image image = ResourceImage::New(gTestImageFilename);
64 BaseHandle object(image);
66 Image image2 = Image::DownCast(object);
67 DALI_TEST_CHECK(image2);
69 Image image3 = DownCast< Image >(object);
70 DALI_TEST_CHECK(image3);
72 BaseHandle unInitializedObject;
73 Image image4 = Image::DownCast(unInitializedObject);
74 DALI_TEST_CHECK(!image4);
76 Image image5 = DownCast< Image >(unInitializedObject);
77 DALI_TEST_CHECK(!image5);
81 int UtcDaliImageGetReleasePolicy(void)
83 TestApplication application;
85 tet_infoline("UtcDaliImageGetReleasePolicy");
87 Image image = ResourceImage::New(gTestImageFilename, ResourceImage::IMMEDIATE, Image::UNUSED);
89 DALI_TEST_CHECK( image );
91 DALI_TEST_CHECK( Image::UNUSED == image.GetReleasePolicy() );
96 int UtcDaliImageGetWidthHeight(void)
98 TestApplication application;
100 tet_infoline("UtcDaliImageGetWidthHeight - Image::GetWidth() & Image::GetHeight");
102 Vector2 testSize(8.0f, 16.0f);
103 application.GetPlatform().SetClosestImageSize(testSize);
104 Image image1 = ResourceImage::New(gTestImageFilename);
105 DALI_TEST_EQUALS( image1.GetWidth(), testSize.width, TEST_LOCATION );
106 DALI_TEST_EQUALS( image1.GetHeight(), testSize.height, TEST_LOCATION );
108 Image image2 = ResourceImage::New( gTestImageFilename, ImageDimensions(128, 256), FittingMode::SCALE_TO_FILL, SamplingMode::DEFAULT );
109 DALI_TEST_EQUALS( image2.GetWidth(), 128u, TEST_LOCATION );
110 DALI_TEST_EQUALS( image2.GetHeight(), 256u, TEST_LOCATION );
112 Image image3 = FrameBufferImage::New(16, 32);
113 DALI_TEST_EQUALS(image3.GetWidth(), 16u, TEST_LOCATION);
114 DALI_TEST_EQUALS(image3.GetHeight(), 32u, TEST_LOCATION);
116 TestNativeImagePointer nativeImage = TestNativeImage::New(32, 64);
117 Image image4 = NativeImage::New(*(nativeImage.Get()));
118 DALI_TEST_EQUALS(image4.GetWidth(), 32u, TEST_LOCATION);
119 DALI_TEST_EQUALS(image4.GetHeight(), 64u, TEST_LOCATION);
124 static bool SignalUploadedFlag = false;
126 static void SignalUploadedHandler(Image image)
128 tet_infoline("Received image uploaded signal");
130 SignalUploadedFlag = true;
133 int UtcDaliImageSignalUploaded(void)
135 TestApplication application;
136 TestPlatformAbstraction& platform = application.GetPlatform();
137 tet_infoline("UtcDaliImageSignalUploaded - Image::SignalUploaded()");
139 // set up image in fake platform abstraction
140 Vector2 testSize(80.0f, 80.0f);
141 platform.SetClosestImageSize(testSize);
143 ResourceImage image = ResourceImage::New(gTestImageFilename);
146 application.SendNotification();
147 application.Render(16);
148 std::vector<GLuint> ids;
150 application.GetGlAbstraction().SetNextTextureIds( ids );
151 Integration::ResourceRequest* request = platform.GetRequest();
152 Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::DISCARD );
153 Integration::ResourcePointer resource(bitmap);
154 bitmap->GetPackedPixelsProfile()->ReserveBuffer(Pixel::RGBA8888, 80, 80, 80, 80);
158 platform.SetResourceLoaded(request->GetId(), request->GetType()->id, resource);
160 application.Render(16);
161 application.SendNotification();
163 image.UploadedSignal().Connect( SignalUploadedHandler );
165 Dali::ImageActor imageActor = ImageActor::New(image);
166 Stage::GetCurrent().Add(imageActor);
167 imageActor.SetSize(80, 80);
168 imageActor.SetVisible(true);
170 application.SendNotification();
171 application.Render(0);
172 application.Render(16);
173 application.SendNotification();
174 application.Render(16);
175 application.SendNotification();
176 application.Render(16);
177 application.SendNotification();
178 application.Render(16);
179 application.SendNotification();
181 DALI_TEST_CHECK( SignalUploadedFlag == true );
182 SignalUploadedFlag = false;
185 bitmap->GetPackedPixelsProfile()->ReserveBuffer(Pixel::RGBA8888, 160, 160, 160, 160);
188 application.SendNotification();
189 application.Render(16);
190 application.Render(16);
191 application.SendNotification();
194 application.Render(16);
195 application.SendNotification();
196 application.Render(16);
197 application.SendNotification();
198 DALI_TEST_CHECK( SignalUploadedFlag == true );
202 int UtcDaliImageDiscard01(void)
204 TestApplication application;
205 tet_infoline("UtcDaliImageDiscard01 - no actors");
208 Image image = ResourceImage::New(gTestImageFilename);
211 application.SendNotification();
212 application.Render(16);
213 std::vector<GLuint> ids;
215 application.GetGlAbstraction().SetNextTextureIds( ids );
216 TestPlatformAbstraction& platform = application.GetPlatform();
217 LoadBitmapResource( platform );
218 application.Render(16);
219 application.SendNotification();
220 } // Drop image handle
222 application.SendNotification();
223 application.Render(16);
224 application.Render(16);
225 application.SendNotification();
227 // Shouldn't have been sent to GL...
228 const std::vector<GLuint>& texIds = application.GetGlAbstraction().GetNextTextureIds();
229 DALI_TEST_CHECK( texIds.size() == 1 );
230 DALI_TEST_CHECK( texIds[0] == 23 );
234 int UtcDaliImageDiscard02(void)
236 TestApplication application;
237 application.GetGlAbstraction().EnableTextureCallTrace( true );
238 tet_infoline("UtcDaliImageDiscard02 - one actor, tests TextureCache::DiscardTexture");
244 Image image = ResourceImage::New(gTestImageFilename, ImageDimensions( 40, 30 ) );
245 actor = ImageActor::New(image);
246 Stage::GetCurrent().Add(actor);
248 application.SendNotification();
249 application.Render(16);
251 std::vector<GLuint> ids;
253 application.GetGlAbstraction().SetNextTextureIds( ids );
255 TestPlatformAbstraction& platform = application.GetPlatform();
256 LoadBitmapResource( platform );
257 application.Render(16);
258 application.SendNotification();
259 DALI_TEST_CHECK( application.GetGlAbstraction().GetTextureTrace().FindMethod("BindTexture") );
260 } // lose image handle, actor should still keep one
261 application.SendNotification();
262 application.Render(16);
264 Stage::GetCurrent().Remove(actor);
265 application.SendNotification();
266 application.Render(16);
268 application.SendNotification();
269 application.Render(16);
273 application.SendNotification();
274 application.Render(16);
275 application.Render(16);
276 application.SendNotification();
278 // texture should have been removed:
279 DALI_TEST_CHECK( application.GetGlAbstraction().CheckTextureDeleted( 23 ));
283 int UtcDaliImageDiscard03(void)
285 TestApplication application;
286 tet_infoline("UtcDaliImageDiscard03 - one actor, tests TextureCache::RemoveObserver");
288 const Vector2 closestImageSize( 1, 1);
289 application.GetPlatform().SetClosestImageSize(closestImageSize);
291 Image image = ResourceImage::New(gTestImageFilename);
292 ImageActor actor = ImageActor::New(image);
293 Stage::GetCurrent().Add(actor);
295 application.SendNotification();
296 application.Render(16);
298 std::vector<GLuint> ids;
300 application.GetGlAbstraction().SetNextTextureIds( ids );
302 TestPlatformAbstraction& platform = application.GetPlatform();
303 LoadBitmapResource( platform );
304 application.Render(16);
305 application.SendNotification();
306 application.SendNotification();
307 application.Render(16);
309 const std::vector<GLuint>& texIds = application.GetGlAbstraction().GetNextTextureIds();
310 DALI_TEST_CHECK( texIds.size() == 0 );
311 const std::vector<GLuint>& boundTexIds = application.GetGlAbstraction().GetBoundTextures();
312 DALI_TEST_CHECK( boundTexIds[0] == 23 );
314 Stage::GetCurrent().Remove(actor);
315 application.SendNotification();
316 application.Render(16);
317 application.SendNotification();
318 application.Render(16);
319 application.SendNotification();
320 application.Render(16); // Should remove image renderer
325 int UtcDaliImageContextLoss(void)
327 TestApplication application; // Default config: DALI_DISCARDS_ALL_DATA
329 const Vector2 closestImageSize( 80, 80 );
330 TestPlatformAbstraction& platform = application.GetPlatform();
331 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
333 platform.SetClosestImageSize(closestImageSize);
335 tet_infoline("UtcDaliImageContextLoss - Load image with LoadPolicy::Immediate, ReleasePolicy::Never, bitmap discard. Check that the image is re-requested on context regain\n");
337 Image image = ResourceImage::New("image.png", ResourceImage::IMMEDIATE, Image::NEVER);
339 DALI_TEST_CHECK( image );
341 application.SendNotification();
342 application.Render(16);
344 // request file loading immediately
346 DALI_TEST_CHECK( platform.WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
347 ImageActor actor = ImageActor::New(image);
348 Stage::GetCurrent().Add(actor);
350 application.SendNotification();
351 application.Render(16);
353 TraceCallStack& textureTrace = glAbstraction.GetTextureTrace();
354 textureTrace.Enable(true);
356 std::vector<GLuint> ids;
358 glAbstraction.SetNextTextureIds( ids );
360 LoadBitmapResource(platform);
362 application.Render(16);
363 application.SendNotification();
365 DALI_TEST_CHECK( textureTrace.FindMethod("GenTextures") );
367 textureTrace.Reset();
368 textureTrace.Enable(true);
369 platform.ResetTrace();
370 platform.EnableTrace(true);
372 // Lose & regain context (in render 'thread')
373 application.ResetContext();
375 application.GetCore().RecoverFromContextLoss(); // start the recovery process
376 application.SendNotification();
378 // Run update/render loop
379 application.Render(16);
380 application.SendNotification();
382 // Expect new load request
383 DALI_TEST_CHECK( platform.WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
385 // Finish loading image
386 LoadBitmapResource(platform);
389 glAbstraction.SetNextTextureIds(ids);
391 // Run update/render loop
392 application.Render(16);
393 application.SendNotification();
395 // Expect new GenTextures
396 DALI_TEST_CHECK( textureTrace.FindMethod("GenTextures") );