(Automated Tests) Use Renderers instead of ImageActor
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-Image.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-test-suite-utils.h>
24 #include <test-native-image.h>
25
26 using namespace Dali;
27
28 void utc_dali_image_startup(void)
29 {
30   test_return_value = TET_UNDEF;
31 }
32
33 void utc_dali_image_cleanup(void)
34 {
35   test_return_value = TET_PASS;
36 }
37
38 static const char* gTestImageFilename = "icon_wrt.png";
39
40 namespace
41 {
42 void LoadBitmapResource(TestPlatformAbstraction& platform)
43 {
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);
48
49   if(request)
50   {
51     platform.SetResourceLoaded(request->GetId(), request->GetType()->id, resource);
52   }
53 }
54
55 }
56
57 int UtcDaliImageDownCast(void)
58 {
59   TestApplication application;
60   tet_infoline("Testing Dali::Image::DownCast()");
61
62   Image image = ResourceImage::New(gTestImageFilename);
63
64   BaseHandle object(image);
65
66   Image image2 = Image::DownCast(object);
67   DALI_TEST_CHECK(image2);
68
69   Image image3 = DownCast< Image >(object);
70   DALI_TEST_CHECK(image3);
71
72   BaseHandle unInitializedObject;
73   Image image4 = Image::DownCast(unInitializedObject);
74   DALI_TEST_CHECK(!image4);
75
76   Image image5 = DownCast< Image >(unInitializedObject);
77   DALI_TEST_CHECK(!image5);
78   END_TEST;
79 }
80
81 int UtcDaliImageGetReleasePolicy(void)
82 {
83   TestApplication application;
84
85   tet_infoline("UtcDaliImageGetReleasePolicy");
86
87   Image image = ResourceImage::New(gTestImageFilename, ResourceImage::IMMEDIATE, Image::UNUSED);
88
89   DALI_TEST_CHECK( image );
90
91   DALI_TEST_CHECK( Image::UNUSED == image.GetReleasePolicy() );
92
93   END_TEST;
94 }
95
96 int UtcDaliImageGetWidthHeight(void)
97 {
98   TestApplication application;
99
100   tet_infoline("UtcDaliImageGetWidthHeight - Image::GetWidth() & Image::GetHeight");
101
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 );
107
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 );
111
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);
115
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);
120
121   END_TEST;
122 }
123
124 static bool SignalUploadedFlag = false;
125
126 static void SignalUploadedHandler(Image image)
127 {
128   tet_infoline("Received image uploaded signal");
129
130   SignalUploadedFlag = true;
131 }
132
133 int UtcDaliImageSignalUploaded(void)
134 {
135   TestApplication application;
136   TestPlatformAbstraction& platform = application.GetPlatform();
137   tet_infoline("UtcDaliImageSignalUploaded - Image::SignalUploaded()");
138
139   // set up image in fake platform abstraction
140   Vector2 testSize(80.0f, 80.0f);
141   platform.SetClosestImageSize(testSize);
142
143   ResourceImage image = ResourceImage::New(gTestImageFilename);
144
145   // Load image
146   application.SendNotification();
147   application.Render(16);
148   std::vector<GLuint> ids;
149   ids.push_back( 23 );
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::OWNED_DISCARD );
153   Integration::ResourcePointer resource(bitmap);
154   bitmap->GetPackedPixelsProfile()->ReserveBuffer(Pixel::RGBA8888, 80, 80, 80, 80);
155
156   if(request)
157   {
158     platform.SetResourceLoaded(request->GetId(), request->GetType()->id, resource);
159   }
160   application.Render(16);
161   application.SendNotification();
162
163   image.UploadedSignal().Connect( SignalUploadedHandler );
164
165   Dali::Actor actor = CreateRenderableActor( image );
166   Stage::GetCurrent().Add( actor );
167   actor.SetSize(80, 80);
168   actor.SetVisible(true);
169
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();
180
181   DALI_TEST_CHECK( SignalUploadedFlag == true );
182   SignalUploadedFlag = false;
183
184   image.Reload();
185   bitmap->GetPackedPixelsProfile()->ReserveBuffer(Pixel::RGBA8888, 160, 160, 160, 160);
186
187   // image loading
188   application.SendNotification();
189   application.Render(16);
190   application.Render(16);
191   application.SendNotification();
192
193   //upload
194   application.Render(16);
195   application.SendNotification();
196   application.Render(16);
197   application.SendNotification();
198   DALI_TEST_CHECK( SignalUploadedFlag == true );
199   END_TEST;
200 }
201
202 int UtcDaliImageDiscard01(void)
203 {
204   TestApplication application;
205   tet_infoline("UtcDaliImageDiscard01 - no actors");
206
207   {
208     Image image = ResourceImage::New(gTestImageFilename);
209
210     // Load image
211     application.SendNotification();
212     application.Render(16);
213     std::vector<GLuint> ids;
214     ids.push_back( 23 );
215     application.GetGlAbstraction().SetNextTextureIds( ids );
216     TestPlatformAbstraction& platform = application.GetPlatform();
217     LoadBitmapResource( platform );
218     application.Render(16);
219     application.SendNotification();
220   } // Drop image handle
221
222   application.SendNotification();
223   application.Render(16);
224   application.Render(16);
225   application.SendNotification();
226
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 );
231   END_TEST;
232 }
233
234 int UtcDaliImageDiscard02(void)
235 {
236   TestApplication application;
237   application.GetGlAbstraction().EnableTextureCallTrace( true );
238   tet_infoline("UtcDaliImageDiscard02 - one actor, tests TextureCache::DiscardTexture");
239
240   {
241     {
242       Actor actor;
243       {
244         Image image = ResourceImage::New(gTestImageFilename, ImageDimensions( 40, 30 ) );
245         actor = CreateRenderableActor(image);
246         Stage::GetCurrent().Add(actor);
247
248         application.SendNotification();
249         application.Render(16);
250
251         std::vector<GLuint> ids;
252         ids.push_back( 23 );
253         application.GetGlAbstraction().SetNextTextureIds( ids );
254
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);
263
264       Stage::GetCurrent().Remove(actor);
265       application.SendNotification();
266       application.Render(16);
267     } // lose actor
268     application.SendNotification();
269     application.Render(16);
270   }
271
272   // Cleanup
273   application.SendNotification();
274   application.Render(16);
275   application.Render(16);
276   application.SendNotification();
277
278   // texture should have been removed:
279   DALI_TEST_CHECK( application.GetGlAbstraction().CheckTextureDeleted( 23 ));
280   END_TEST;
281 }
282
283 int UtcDaliImageDiscard03(void)
284 {
285   TestApplication application;
286   tet_infoline("UtcDaliImageDiscard03 - one actor, tests TextureCache::RemoveObserver");
287
288   const Vector2 closestImageSize( 1, 1);
289   application.GetPlatform().SetClosestImageSize(closestImageSize);
290
291   Image image = ResourceImage::New(gTestImageFilename);
292   Actor actor = CreateRenderableActor(image);
293   Stage::GetCurrent().Add(actor);
294
295   application.SendNotification();
296   application.Render(16);
297
298   std::vector<GLuint> ids;
299   ids.push_back( 23 );
300   application.GetGlAbstraction().SetNextTextureIds( ids );
301
302   TestPlatformAbstraction& platform = application.GetPlatform();
303   LoadBitmapResource( platform );
304   application.Render(16);
305   application.SendNotification();
306   application.SendNotification();
307   application.Render(16);
308
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 );
313
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
321
322   END_TEST;
323 }
324
325 int UtcDaliImageContextLoss(void)
326 {
327   TestApplication application; // Default config: DALI_DISCARDS_ALL_DATA
328
329   const Vector2 closestImageSize( 80, 80 );
330   TestPlatformAbstraction& platform = application.GetPlatform();
331   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
332
333   platform.SetClosestImageSize(closestImageSize);
334
335   tet_infoline("UtcDaliImageContextLoss - Load image with LoadPolicy::Immediate, ReleasePolicy::Never, bitmap discard. Check that the image is re-requested on context regain\n");
336
337   Image image = ResourceImage::New("image.png", ResourceImage::IMMEDIATE, Image::NEVER);
338
339   DALI_TEST_CHECK( image );
340
341   application.SendNotification();
342   application.Render(16);
343
344   // request file loading immediately
345
346   DALI_TEST_CHECK( platform.WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
347   Actor actor = CreateRenderableActor(image);
348   Stage::GetCurrent().Add(actor);
349
350   application.SendNotification();
351   application.Render(16);
352
353   TraceCallStack& textureTrace = glAbstraction.GetTextureTrace();
354   textureTrace.Enable(true);
355
356   std::vector<GLuint> ids;
357   ids.push_back( 23 );
358   glAbstraction.SetNextTextureIds( ids );
359
360   LoadBitmapResource(platform);
361
362   application.Render(16);
363   application.SendNotification();
364
365   DALI_TEST_CHECK( textureTrace.FindMethod("GenTextures") );
366
367   textureTrace.Reset();
368   textureTrace.Enable(true);
369   platform.ResetTrace();
370   platform.EnableTrace(true);
371
372   // Lose & regain context (in render 'thread')
373   application.ResetContext();
374
375   application.GetCore().RecoverFromContextLoss(); // start the recovery process
376   application.SendNotification();
377
378   // Run update/render loop
379   application.Render(16);
380   application.SendNotification();
381
382   // Expect new load request
383   DALI_TEST_CHECK( platform.WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
384
385   // Finish loading image
386   LoadBitmapResource(platform);
387   ids.clear();
388   ids.push_back( 57 );
389   glAbstraction.SetNextTextureIds(ids);
390
391   // Run update/render loop
392   application.Render(16);
393   application.SendNotification();
394
395   // Expect new GenTextures
396   DALI_TEST_CHECK( textureTrace.FindMethod("GenTextures") );
397
398   END_TEST;
399 }