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-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::OWNED_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 UtcDaliImageGetWidthHeight(void)
83 TestApplication application;
85 tet_infoline("UtcDaliImageGetWidthHeight - Image::GetWidth() & Image::GetHeight");
87 Vector2 testSize(8.0f, 16.0f);
88 application.GetPlatform().SetClosestImageSize(testSize);
89 Image image1 = ResourceImage::New(gTestImageFilename);
90 DALI_TEST_EQUALS( image1.GetWidth(), testSize.width, TEST_LOCATION );
91 DALI_TEST_EQUALS( image1.GetHeight(), testSize.height, TEST_LOCATION );
93 Image image2 = ResourceImage::New( gTestImageFilename, ImageDimensions(128, 256), FittingMode::SCALE_TO_FILL, SamplingMode::DEFAULT );
94 DALI_TEST_EQUALS( image2.GetWidth(), 128u, TEST_LOCATION );
95 DALI_TEST_EQUALS( image2.GetHeight(), 256u, TEST_LOCATION );
97 Image image3 = FrameBufferImage::New(16, 32);
98 DALI_TEST_EQUALS(image3.GetWidth(), 16u, TEST_LOCATION);
99 DALI_TEST_EQUALS(image3.GetHeight(), 32u, TEST_LOCATION);
101 TestNativeImagePointer nativeImage = TestNativeImage::New(32, 64);
102 Image image4 = NativeImage::New(*(nativeImage.Get()));
103 DALI_TEST_EQUALS(image4.GetWidth(), 32u, TEST_LOCATION);
104 DALI_TEST_EQUALS(image4.GetHeight(), 64u, TEST_LOCATION);
109 static bool SignalUploadedFlag = false;
111 static void SignalUploadedHandler(Image image)
113 tet_infoline("Received image uploaded signal");
115 SignalUploadedFlag = true;
118 int UtcDaliImageSignalUploaded(void)
120 TestApplication application;
121 TestPlatformAbstraction& platform = application.GetPlatform();
122 tet_infoline("UtcDaliImageSignalUploaded - Image::SignalUploaded()");
124 // set up image in fake platform abstraction
125 Vector2 testSize(80.0f, 80.0f);
126 platform.SetClosestImageSize(testSize);
128 ResourceImage image = ResourceImage::New(gTestImageFilename);
131 application.SendNotification();
132 application.Render(16);
133 std::vector<GLuint> ids;
135 application.GetGlAbstraction().SetNextTextureIds( ids );
136 Integration::ResourceRequest* request = platform.GetRequest();
137 Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::OWNED_DISCARD );
138 Integration::ResourcePointer resource(bitmap);
139 bitmap->GetPackedPixelsProfile()->ReserveBuffer(Pixel::RGBA8888, 80, 80, 80, 80);
143 platform.SetResourceLoaded(request->GetId(), request->GetType()->id, resource);
145 application.Render(16);
146 application.SendNotification();
148 image.UploadedSignal().Connect( SignalUploadedHandler );
150 Dali::Actor actor = CreateRenderableActor( image );
151 Stage::GetCurrent().Add( actor );
152 actor.SetSize(80, 80);
153 actor.SetVisible(true);
155 application.SendNotification();
156 application.Render(0);
157 application.Render(16);
158 application.SendNotification();
159 application.Render(16);
160 application.SendNotification();
161 application.Render(16);
162 application.SendNotification();
163 application.Render(16);
164 application.SendNotification();
166 DALI_TEST_CHECK( SignalUploadedFlag == true );
167 SignalUploadedFlag = false;
170 bitmap->GetPackedPixelsProfile()->ReserveBuffer(Pixel::RGBA8888, 160, 160, 160, 160);
173 application.SendNotification();
174 application.Render(16);
175 application.Render(16);
176 application.SendNotification();
178 request = platform.GetRequest();
181 platform.SetResourceLoaded(request->GetId(), request->GetType()->id, resource);
185 application.Render(16);
186 application.SendNotification();
187 application.Render(16);
188 application.SendNotification();
189 DALI_TEST_CHECK( SignalUploadedFlag == true );
193 int UtcDaliImageDiscard01(void)
195 TestApplication application;
196 tet_infoline("UtcDaliImageDiscard01 - no actors");
199 Image image = ResourceImage::New(gTestImageFilename);
202 application.SendNotification();
203 application.Render(16);
204 std::vector<GLuint> ids;
206 application.GetGlAbstraction().SetNextTextureIds( ids );
207 TestPlatformAbstraction& platform = application.GetPlatform();
208 LoadBitmapResource( platform );
209 application.Render(16);
210 application.SendNotification();
211 } // Drop image handle
213 application.SendNotification();
214 application.Render(16);
215 application.Render(16);
216 application.SendNotification();
218 // Shouldn't have been sent to GL...
219 const std::vector<GLuint>& texIds = application.GetGlAbstraction().GetNextTextureIds();
220 DALI_TEST_CHECK( texIds.size() == 1 );
221 DALI_TEST_CHECK( texIds[0] == 23 );
225 int UtcDaliImageDiscard02(void)
227 TestApplication application;
228 application.GetGlAbstraction().EnableTextureCallTrace( true );
229 tet_infoline("UtcDaliImageDiscard02 - one actor, tests TextureCache::DiscardTexture");
235 Image image = ResourceImage::New(gTestImageFilename, ImageDimensions( 40, 30 ) );
236 actor = CreateRenderableActor(image);
237 Stage::GetCurrent().Add(actor);
239 application.SendNotification();
240 application.Render(16);
242 std::vector<GLuint> ids;
244 application.GetGlAbstraction().SetNextTextureIds( ids );
246 TestPlatformAbstraction& platform = application.GetPlatform();
247 LoadBitmapResource( platform );
248 application.Render(16);
249 application.SendNotification();
250 DALI_TEST_CHECK( application.GetGlAbstraction().GetTextureTrace().FindMethod("BindTexture") );
251 } // lose image handle, actor should still keep one
252 application.SendNotification();
253 application.Render(16);
255 Stage::GetCurrent().Remove(actor);
256 application.SendNotification();
257 application.Render(16);
259 application.SendNotification();
260 application.Render(16);
264 application.SendNotification();
265 application.Render(16);
266 application.Render(16);
267 application.SendNotification();
269 // texture should have been removed:
270 DALI_TEST_CHECK( application.GetGlAbstraction().CheckTextureDeleted( 23 ));
274 int UtcDaliImageDiscard03(void)
276 TestApplication application;
277 tet_infoline("UtcDaliImageDiscard03 - one actor, tests TextureCache::RemoveObserver");
279 const Vector2 closestImageSize( 1, 1);
280 application.GetPlatform().SetClosestImageSize(closestImageSize);
282 Image image = ResourceImage::New(gTestImageFilename);
283 Actor actor = CreateRenderableActor(image);
284 Stage::GetCurrent().Add(actor);
286 application.SendNotification();
287 application.Render(16);
289 std::vector<GLuint> ids;
291 application.GetGlAbstraction().SetNextTextureIds( ids );
293 TestPlatformAbstraction& platform = application.GetPlatform();
294 LoadBitmapResource( platform );
295 application.Render(16);
296 application.SendNotification();
297 application.SendNotification();
298 application.Render(16);
300 const std::vector<GLuint>& texIds = application.GetGlAbstraction().GetNextTextureIds();
301 DALI_TEST_CHECK( texIds.size() == 0 );
302 const std::vector<GLuint>& boundTexIds = application.GetGlAbstraction().GetBoundTextures();
303 DALI_TEST_CHECK( boundTexIds[0] == 23 );
305 Stage::GetCurrent().Remove(actor);
306 application.SendNotification();
307 application.Render(16);
308 application.SendNotification();
309 application.Render(16);
310 application.SendNotification();
311 application.Render(16); // Should remove image renderer
316 int UtcDaliImageContextLoss(void)
318 TestApplication application; // Default config: DALI_DISCARDS_ALL_DATA
320 const Vector2 closestImageSize( 80, 80 );
321 TestPlatformAbstraction& platform = application.GetPlatform();
322 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
324 platform.SetClosestImageSize(closestImageSize);
326 tet_infoline("UtcDaliImageContextLoss - Load image with LoadPolicy::Immediate, bitmap discard. Check that the image is re-requested on context regain\n");
328 Image image = ResourceImage::New("image.png");
330 DALI_TEST_CHECK( image );
332 application.SendNotification();
333 application.Render(16);
335 // request file loading immediately
337 DALI_TEST_CHECK( platform.WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
338 Actor actor = CreateRenderableActor(image);
339 Stage::GetCurrent().Add(actor);
341 application.SendNotification();
342 application.Render(16);
344 TraceCallStack& textureTrace = glAbstraction.GetTextureTrace();
345 textureTrace.Enable(true);
347 std::vector<GLuint> ids;
349 glAbstraction.SetNextTextureIds( ids );
351 LoadBitmapResource(platform);
353 application.Render(16);
354 application.SendNotification();
356 DALI_TEST_CHECK( textureTrace.FindMethod("GenTextures") );
358 textureTrace.Reset();
359 textureTrace.Enable(true);
360 platform.ResetTrace();
361 platform.EnableTrace(true);
363 // Lose & regain context (in render 'thread')
364 application.ResetContext();
366 application.GetCore().RecoverFromContextLoss(); // start the recovery process
367 application.SendNotification();
369 // Run update/render loop
370 application.Render(16);
371 application.SendNotification();
373 // Expect new load request
374 DALI_TEST_CHECK( platform.WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
376 // Finish loading image
377 LoadBitmapResource(platform);
380 glAbstraction.SetNextTextureIds(ids);
382 // Run update/render loop
383 application.Render(16);
384 application.SendNotification();
386 // Expect new GenTextures
387 DALI_TEST_CHECK( textureTrace.FindMethod("GenTextures") );