[3.0] Remove/move experimental features
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-Image.cpp
1 /*
2  * Copyright (c) 2016 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 UtcDaliImageGetWidthHeight(void)
82 {
83   TestApplication application;
84
85   tet_infoline("UtcDaliImageGetWidthHeight - Image::GetWidth() & Image::GetHeight");
86
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 );
92
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 );
96
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);
100
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);
105
106   END_TEST;
107 }
108
109 static bool SignalUploadedFlag = false;
110
111 static void SignalUploadedHandler(Image image)
112 {
113   tet_infoline("Received image uploaded signal");
114
115   SignalUploadedFlag = true;
116 }
117
118 int UtcDaliImageSignalUploaded(void)
119 {
120   TestApplication application;
121   TestPlatformAbstraction& platform = application.GetPlatform();
122   tet_infoline("UtcDaliImageSignalUploaded - Image::SignalUploaded()");
123
124   // set up image in fake platform abstraction
125   Vector2 testSize(80.0f, 80.0f);
126   platform.SetClosestImageSize(testSize);
127
128   ResourceImage image = ResourceImage::New(gTestImageFilename);
129
130   // Load image
131   application.SendNotification();
132   application.Render(16);
133   std::vector<GLuint> ids;
134   ids.push_back( 23 );
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);
140
141   if(request)
142   {
143     platform.SetResourceLoaded(request->GetId(), request->GetType()->id, resource);
144   }
145   application.Render(16);
146   application.SendNotification();
147
148   image.UploadedSignal().Connect( SignalUploadedHandler );
149
150   Dali::Actor actor = CreateRenderableActor( image );
151   Stage::GetCurrent().Add( actor );
152   actor.SetSize(80, 80);
153   actor.SetVisible(true);
154
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();
165
166   DALI_TEST_CHECK( SignalUploadedFlag == true );
167   SignalUploadedFlag = false;
168
169   image.Reload();
170   bitmap->GetPackedPixelsProfile()->ReserveBuffer(Pixel::RGBA8888, 160, 160, 160, 160);
171
172   // image loading
173   application.SendNotification();
174   application.Render(16);
175   application.Render(16);
176   application.SendNotification();
177
178   request = platform.GetRequest();
179   if(request)
180   {
181     platform.SetResourceLoaded(request->GetId(), request->GetType()->id, resource);
182   }
183
184   //upload
185   application.Render(16);
186   application.SendNotification();
187   application.Render(16);
188   application.SendNotification();
189   DALI_TEST_CHECK( SignalUploadedFlag == true );
190   END_TEST;
191 }
192
193 int UtcDaliImageDiscard01(void)
194 {
195   TestApplication application;
196   tet_infoline("UtcDaliImageDiscard01 - no actors");
197
198   {
199     Image image = ResourceImage::New(gTestImageFilename);
200
201     // Load image
202     application.SendNotification();
203     application.Render(16);
204     std::vector<GLuint> ids;
205     ids.push_back( 23 );
206     application.GetGlAbstraction().SetNextTextureIds( ids );
207     TestPlatformAbstraction& platform = application.GetPlatform();
208     LoadBitmapResource( platform );
209     application.Render(16);
210     application.SendNotification();
211   } // Drop image handle
212
213   application.SendNotification();
214   application.Render(16);
215   application.Render(16);
216   application.SendNotification();
217
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 );
222   END_TEST;
223 }
224
225 int UtcDaliImageDiscard02(void)
226 {
227   TestApplication application;
228   application.GetGlAbstraction().EnableTextureCallTrace( true );
229   tet_infoline("UtcDaliImageDiscard02 - one actor, tests TextureCache::DiscardTexture");
230
231   {
232     {
233       Actor actor;
234       {
235         Image image = ResourceImage::New(gTestImageFilename, ImageDimensions( 40, 30 ) );
236         actor = CreateRenderableActor(image);
237         Stage::GetCurrent().Add(actor);
238
239         application.SendNotification();
240         application.Render(16);
241
242         std::vector<GLuint> ids;
243         ids.push_back( 23 );
244         application.GetGlAbstraction().SetNextTextureIds( ids );
245
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);
254
255       Stage::GetCurrent().Remove(actor);
256       application.SendNotification();
257       application.Render(16);
258     } // lose actor
259     application.SendNotification();
260     application.Render(16);
261   }
262
263   // Cleanup
264   application.SendNotification();
265   application.Render(16);
266   application.Render(16);
267   application.SendNotification();
268
269   // texture should have been removed:
270   DALI_TEST_CHECK( application.GetGlAbstraction().CheckTextureDeleted( 23 ));
271   END_TEST;
272 }
273
274 int UtcDaliImageDiscard03(void)
275 {
276   TestApplication application;
277   tet_infoline("UtcDaliImageDiscard03 - one actor, tests TextureCache::RemoveObserver");
278
279   const Vector2 closestImageSize( 1, 1);
280   application.GetPlatform().SetClosestImageSize(closestImageSize);
281
282   Image image = ResourceImage::New(gTestImageFilename);
283   Actor actor = CreateRenderableActor(image);
284   Stage::GetCurrent().Add(actor);
285
286   application.SendNotification();
287   application.Render(16);
288
289   std::vector<GLuint> ids;
290   ids.push_back( 23 );
291   application.GetGlAbstraction().SetNextTextureIds( ids );
292
293   TestPlatformAbstraction& platform = application.GetPlatform();
294   LoadBitmapResource( platform );
295   application.Render(16);
296   application.SendNotification();
297   application.SendNotification();
298   application.Render(16);
299
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 );
304
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
312
313   END_TEST;
314 }
315
316 int UtcDaliImageContextLoss(void)
317 {
318   TestApplication application; // Default config: DALI_DISCARDS_ALL_DATA
319
320   const Vector2 closestImageSize( 80, 80 );
321   TestPlatformAbstraction& platform = application.GetPlatform();
322   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
323
324   platform.SetClosestImageSize(closestImageSize);
325
326   tet_infoline("UtcDaliImageContextLoss - Load image with LoadPolicy::Immediate, bitmap discard. Check that the image is re-requested on context regain\n");
327
328   Image image = ResourceImage::New("image.png");
329
330   DALI_TEST_CHECK( image );
331
332   application.SendNotification();
333   application.Render(16);
334
335   // request file loading immediately
336
337   DALI_TEST_CHECK( platform.WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
338   Actor actor = CreateRenderableActor(image);
339   Stage::GetCurrent().Add(actor);
340
341   application.SendNotification();
342   application.Render(16);
343
344   TraceCallStack& textureTrace = glAbstraction.GetTextureTrace();
345   textureTrace.Enable(true);
346
347   std::vector<GLuint> ids;
348   ids.push_back( 23 );
349   glAbstraction.SetNextTextureIds( ids );
350
351   LoadBitmapResource(platform);
352
353   application.Render(16);
354   application.SendNotification();
355
356   DALI_TEST_CHECK( textureTrace.FindMethod("GenTextures") );
357
358   textureTrace.Reset();
359   textureTrace.Enable(true);
360   platform.ResetTrace();
361   platform.EnableTrace(true);
362
363   // Lose & regain context (in render 'thread')
364   application.ResetContext();
365
366   application.GetCore().RecoverFromContextLoss(); // start the recovery process
367   application.SendNotification();
368
369   // Run update/render loop
370   application.Render(16);
371   application.SendNotification();
372
373   // Expect new load request
374   DALI_TEST_CHECK( platform.WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
375
376   // Finish loading image
377   LoadBitmapResource(platform);
378   ids.clear();
379   ids.push_back( 57 );
380   glAbstraction.SetNextTextureIds(ids);
381
382   // Run update/render loop
383   application.Render(16);
384   application.SendNotification();
385
386   // Expect new GenTextures
387   DALI_TEST_CHECK( textureTrace.FindMethod("GenTextures") );
388
389   END_TEST;
390 }