[3.0] Mark Clipping API as being part of the Tizen 3 version
[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   request = platform.GetRequest();
194   if(request)
195   {
196     platform.SetResourceLoaded(request->GetId(), request->GetType()->id, resource);
197   }
198
199   //upload
200   application.Render(16);
201   application.SendNotification();
202   application.Render(16);
203   application.SendNotification();
204   DALI_TEST_CHECK( SignalUploadedFlag == true );
205   END_TEST;
206 }
207
208 int UtcDaliImageDiscard01(void)
209 {
210   TestApplication application;
211   tet_infoline("UtcDaliImageDiscard01 - no actors");
212
213   {
214     Image image = ResourceImage::New(gTestImageFilename);
215
216     // Load image
217     application.SendNotification();
218     application.Render(16);
219     std::vector<GLuint> ids;
220     ids.push_back( 23 );
221     application.GetGlAbstraction().SetNextTextureIds( ids );
222     TestPlatformAbstraction& platform = application.GetPlatform();
223     LoadBitmapResource( platform );
224     application.Render(16);
225     application.SendNotification();
226   } // Drop image handle
227
228   application.SendNotification();
229   application.Render(16);
230   application.Render(16);
231   application.SendNotification();
232
233   // Shouldn't have been sent to GL...
234   const std::vector<GLuint>& texIds = application.GetGlAbstraction().GetNextTextureIds();
235   DALI_TEST_CHECK( texIds.size() == 1 );
236   DALI_TEST_CHECK( texIds[0] == 23 );
237   END_TEST;
238 }
239
240 int UtcDaliImageDiscard02(void)
241 {
242   TestApplication application;
243   application.GetGlAbstraction().EnableTextureCallTrace( true );
244   tet_infoline("UtcDaliImageDiscard02 - one actor, tests TextureCache::DiscardTexture");
245
246   {
247     {
248       Actor actor;
249       {
250         Image image = ResourceImage::New(gTestImageFilename, ImageDimensions( 40, 30 ) );
251         actor = CreateRenderableActor(image);
252         Stage::GetCurrent().Add(actor);
253
254         application.SendNotification();
255         application.Render(16);
256
257         std::vector<GLuint> ids;
258         ids.push_back( 23 );
259         application.GetGlAbstraction().SetNextTextureIds( ids );
260
261         TestPlatformAbstraction& platform = application.GetPlatform();
262         LoadBitmapResource( platform );
263         application.Render(16);
264         application.SendNotification();
265         DALI_TEST_CHECK( application.GetGlAbstraction().GetTextureTrace().FindMethod("BindTexture") );
266       } // lose image handle, actor should still keep one
267       application.SendNotification();
268       application.Render(16);
269
270       Stage::GetCurrent().Remove(actor);
271       application.SendNotification();
272       application.Render(16);
273     } // lose actor
274     application.SendNotification();
275     application.Render(16);
276   }
277
278   // Cleanup
279   application.SendNotification();
280   application.Render(16);
281   application.Render(16);
282   application.SendNotification();
283
284   // texture should have been removed:
285   DALI_TEST_CHECK( application.GetGlAbstraction().CheckTextureDeleted( 23 ));
286   END_TEST;
287 }
288
289 int UtcDaliImageDiscard03(void)
290 {
291   TestApplication application;
292   tet_infoline("UtcDaliImageDiscard03 - one actor, tests TextureCache::RemoveObserver");
293
294   const Vector2 closestImageSize( 1, 1);
295   application.GetPlatform().SetClosestImageSize(closestImageSize);
296
297   Image image = ResourceImage::New(gTestImageFilename);
298   Actor actor = CreateRenderableActor(image);
299   Stage::GetCurrent().Add(actor);
300
301   application.SendNotification();
302   application.Render(16);
303
304   std::vector<GLuint> ids;
305   ids.push_back( 23 );
306   application.GetGlAbstraction().SetNextTextureIds( ids );
307
308   TestPlatformAbstraction& platform = application.GetPlatform();
309   LoadBitmapResource( platform );
310   application.Render(16);
311   application.SendNotification();
312   application.SendNotification();
313   application.Render(16);
314
315   const std::vector<GLuint>& texIds = application.GetGlAbstraction().GetNextTextureIds();
316   DALI_TEST_CHECK( texIds.size() == 0 );
317   const std::vector<GLuint>& boundTexIds = application.GetGlAbstraction().GetBoundTextures();
318   DALI_TEST_CHECK( boundTexIds[0] == 23 );
319
320   Stage::GetCurrent().Remove(actor);
321   application.SendNotification();
322   application.Render(16);
323   application.SendNotification();
324   application.Render(16);
325   application.SendNotification();
326   application.Render(16); // Should remove image renderer
327
328   END_TEST;
329 }
330
331 int UtcDaliImageContextLoss(void)
332 {
333   TestApplication application; // Default config: DALI_DISCARDS_ALL_DATA
334
335   const Vector2 closestImageSize( 80, 80 );
336   TestPlatformAbstraction& platform = application.GetPlatform();
337   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
338
339   platform.SetClosestImageSize(closestImageSize);
340
341   tet_infoline("UtcDaliImageContextLoss - Load image with LoadPolicy::Immediate, ReleasePolicy::Never, bitmap discard. Check that the image is re-requested on context regain\n");
342
343   Image image = ResourceImage::New("image.png", ResourceImage::IMMEDIATE, Image::NEVER);
344
345   DALI_TEST_CHECK( image );
346
347   application.SendNotification();
348   application.Render(16);
349
350   // request file loading immediately
351
352   DALI_TEST_CHECK( platform.WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
353   Actor actor = CreateRenderableActor(image);
354   Stage::GetCurrent().Add(actor);
355
356   application.SendNotification();
357   application.Render(16);
358
359   TraceCallStack& textureTrace = glAbstraction.GetTextureTrace();
360   textureTrace.Enable(true);
361
362   std::vector<GLuint> ids;
363   ids.push_back( 23 );
364   glAbstraction.SetNextTextureIds( ids );
365
366   LoadBitmapResource(platform);
367
368   application.Render(16);
369   application.SendNotification();
370
371   DALI_TEST_CHECK( textureTrace.FindMethod("GenTextures") );
372
373   textureTrace.Reset();
374   textureTrace.Enable(true);
375   platform.ResetTrace();
376   platform.EnableTrace(true);
377
378   // Lose & regain context (in render 'thread')
379   application.ResetContext();
380
381   application.GetCore().RecoverFromContextLoss(); // start the recovery process
382   application.SendNotification();
383
384   // Run update/render loop
385   application.Render(16);
386   application.SendNotification();
387
388   // Expect new load request
389   DALI_TEST_CHECK( platform.WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
390
391   // Finish loading image
392   LoadBitmapResource(platform);
393   ids.clear();
394   ids.push_back( 57 );
395   glAbstraction.SetNextTextureIds(ids);
396
397   // Run update/render loop
398   application.Render(16);
399   application.SendNotification();
400
401   // Expect new GenTextures
402   DALI_TEST_CHECK( textureTrace.FindMethod("GenTextures") );
403
404   END_TEST;
405 }