e143837aa527d14c4c23cfbb02af8b24845073d9
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-NativeImage.cpp
1 /*
2  * Copyright (c) 2015 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-test-suite-utils.h>
23 #include <test-native-image.h>
24 #include <test-intrusive-ptr.h>
25
26 using namespace Dali;
27
28 void utc_dali_native_image_startup(void)
29 {
30   test_return_value = TET_UNDEF;
31 }
32
33 void utc_dali_native_image_cleanup(void)
34 {
35   test_return_value = TET_PASS;
36 }
37
38 IntrusivePtr<TestNativeImage> Creator()
39 {
40   return TestNativeImage::New(10,10);
41 }
42
43 int UtcDaliIntrusivePtrTestNativeImage(void)
44 {
45   UtcCoverageIntrusivePtr<TestNativeImage> pointer;
46
47   pointer.Check(Creator);
48
49   END_TEST;
50 }
51
52 int UtcDaliNativeImageNew(void)
53 {
54   TestApplication application;
55
56   tet_infoline("UtcDaliNativeImageNew - NativeImage::New(NativeImageInterface&)");
57
58   // invoke default handle constructor
59   NativeImage image;
60   TestNativeImagePointer nativeImage = TestNativeImage::New(16, 16);
61
62   DALI_TEST_CHECK( !image );
63
64   // initialise handle
65   image = NativeImage::New(*(nativeImage.Get()));
66
67   DALI_TEST_CHECK( image );
68   END_TEST;
69 }
70
71 int UtcDaliNativeImageCopyConstructor(void)
72 {
73   TestApplication application;
74
75   tet_infoline("UtcDaliNativeImageCopyConstructor - NativeImage::NativeImage( const NativeImage& )");
76
77   NativeImage image1;
78   DALI_TEST_CHECK( !image1 );
79
80   TestNativeImagePointer nativeImage = TestNativeImage::New(16, 16);
81   image1 = NativeImage::New(*(nativeImage.Get()));
82   NativeImage image2( image1 );
83
84   DALI_TEST_CHECK( image2 );
85   DALI_TEST_EQUALS( image1, image2, TEST_LOCATION );
86
87   END_TEST;
88 }
89
90 int UtcDaliNativeImageDownCast(void)
91 {
92   TestApplication application;
93   tet_infoline("Testing Dali::Image::DownCast()");
94
95   TestNativeImagePointer nativeImage = TestNativeImage::New(16, 16);
96   NativeImage image = NativeImage::New(*(nativeImage.Get()));
97
98   BaseHandle object(image);
99
100   NativeImage image2 = NativeImage::DownCast(object);
101   DALI_TEST_CHECK(image2);
102
103   NativeImage image3 = DownCast< NativeImage >(object);
104   DALI_TEST_CHECK(image3);
105
106   BaseHandle unInitializedObject;
107   NativeImage image4 = NativeImage::DownCast(unInitializedObject);
108   DALI_TEST_CHECK(!image4);
109
110   NativeImage image5 = DownCast< NativeImage >(unInitializedObject);
111   DALI_TEST_CHECK(!image5);
112
113   Image image6 = NativeImage::New(*(nativeImage.Get()));
114   NativeImage image7 = NativeImage::DownCast(image6);
115   DALI_TEST_CHECK(image7);
116   END_TEST;
117 }
118
119 int UtcDaliNativeImageCreateGlTextureN(void)
120 {
121   TestApplication application;
122   tet_infoline( "Testing Dali::NativeImage::GenerateGlTexture()" );
123
124   NativeImage image;
125   try
126   {
127     image.CreateGlTexture();
128     tet_printf( "Assertion test failed - no Exception\n" );
129     tet_result( TET_FAIL );
130   }
131   catch( Dali::DaliException& e )
132   {
133     DALI_TEST_PRINT_ASSERT( e );
134     DALI_TEST_ASSERT( e, "image &&", TEST_LOCATION );
135   }
136   END_TEST;
137 }
138
139 int UtcDaliNativeImageCreateGlTextureP(void)
140 {
141   TestApplication application;
142   tet_infoline( "Testing Dali::NativeImage::GenerateGlTexture()" );
143
144   TestNativeImagePointer imageInterface = TestNativeImage::New( 16, 16 );
145   NativeImage image = NativeImage::New( *(imageInterface.Get()) );
146   DALI_TEST_CHECK( image );
147
148   image.CreateGlTexture();
149
150   application.SendNotification();
151   application.Render(16);
152
153   DALI_TEST_EQUALS( imageInterface->mExtensionCreateCalls, 1, TEST_LOCATION );
154   DALI_TEST_EQUALS( imageInterface->mTargetTextureCalls, 1, TEST_LOCATION );
155
156   END_TEST;
157 }
158
159 int UtcDaliNativeImageContextLoss(void)
160 {
161   TestApplication application;
162   tet_infoline( "Testing Dali::NativeImage behaviour through a context lost/regained cycle." );
163
164   // Build an image that is expected to have a GL texture created for it and
165   // recreated in a GL context recovery:
166   TestNativeImagePointer eagerImageInterface = TestNativeImage::New( 16, 16 );
167   NativeImage eagerImage = NativeImage::New( *(eagerImageInterface.Get()) );
168   DALI_TEST_CHECK( eagerImage );
169
170   // Build a regular lazy-allocated image for comparison:
171   TestNativeImagePointer lazyImageInterface = TestNativeImage::New( 16, 16 );
172   NativeImage lazyImage = NativeImage::New( *(lazyImageInterface.Get()) );
173   DALI_TEST_CHECK( lazyImage );
174
175   eagerImage.CreateGlTexture();
176
177   application.SendNotification();
178   application.Render(16);
179
180   // Cycle through a context loss and regain, asserting that the texture is
181   // not reallocated if it already existed before the cycle and is never allocated
182   // throughout the cycle if of the regular lazy kind:
183
184   // Call render thread context destroyed / created functions:
185   application.ResetContext();
186
187   // Call event thread function:
188   application.GetCore().RecoverFromContextLoss();
189
190   // Run update/render loop
191   application.SendNotification();
192   application.Render(16);
193
194   DALI_TEST_EQUALS( eagerImageInterface->mExtensionCreateCalls, 1, TEST_LOCATION );
195   DALI_TEST_EQUALS( eagerImageInterface->mTargetTextureCalls, 1, TEST_LOCATION );
196
197   DALI_TEST_EQUALS( lazyImageInterface->mExtensionCreateCalls, 0, TEST_LOCATION );
198   DALI_TEST_EQUALS( lazyImageInterface->mTargetTextureCalls, 0, TEST_LOCATION );
199
200   END_TEST;
201 }
202
203 int UtcDaliNativeImageExtensionP(void)
204 {
205   TestApplication application;
206   tet_infoline( "Testing Dali::NativeImage::GenerateGlTexture()" );
207
208   TestNativeImagePointer testNativeImage = TestNativeImage::New( 16, 16 );
209   DALI_TEST_CHECK( testNativeImage );
210   DALI_TEST_CHECK( NULL != testNativeImage->GetExtension() );
211
212   TestNativeImageNoExtPointer testNativeImage2 = TestNativeImageNoExt::New( 16, 16 );
213   DALI_TEST_CHECK( testNativeImage2 );
214   DALI_TEST_CHECK( NULL == testNativeImage2->GetExtension() );
215
216   END_TEST;
217 }
218
219 int UtcDaliNativeImageGetCustomFragmentPreFixP(void)
220 {
221   TestApplication application;
222   TestNativeImagePointer nativeImageInterface = TestNativeImage::New( 16, 16 );
223   NativeImage nativeImage = NativeImage::New( *(nativeImageInterface.Get()) );
224
225   const char* preFix = "#extension GL_OES_EGL_image_external:require\n";
226   DALI_TEST_EQUALS( nativeImage.GetCustomFragmentPreFix(), preFix, TEST_LOCATION );
227   END_TEST;
228 }
229
230 int UtcDaliNativeImageGetCustomSamplerTypenameP(void)
231 {
232   TestApplication application;
233   TestNativeImagePointer nativeImageInterface = TestNativeImage::New( 16, 16 );
234   NativeImage nativeImage = NativeImage::New( *(nativeImageInterface.Get()) );
235
236   const char* samplerTypename = "samplerExternalOES";
237   DALI_TEST_EQUALS( nativeImage.GetCustomSamplerTypename(), samplerTypename, TEST_LOCATION );
238   END_TEST;
239 }
240
241
242
243 int UtcDaliNativeImageTestCreationFailure(void)
244 {
245   TestApplication application;
246   TestNativeImagePointer nativeImageInterface = TestNativeImage::New( 16, 16 );
247   NativeImage nativeImage = NativeImage::New( *(nativeImageInterface.Get()) );
248
249   tet_printf("Test what happens when GlExtensionCreate is called, and returns false to indicate an error\n");
250
251   nativeImageInterface->SetGlExtensionCreateResult( false );
252
253   Actor actor = CreateRenderableActor( nativeImage );
254   actor.SetParentOrigin( ParentOrigin::CENTER );
255   Stage::GetCurrent().Add( actor );
256
257   TestGlAbstraction& gl = application.GetGlAbstraction();
258   TraceCallStack& textureTrace = gl.GetTextureTrace();
259   textureTrace.Reset();
260   textureTrace.Enable(true);
261
262   TraceCallStack& drawTrace = gl.GetDrawTrace();
263   drawTrace.Reset();
264   drawTrace.Enable(true);
265
266   application.SendNotification();
267   application.Render();
268
269   // Test that nothing was rendered
270   DALI_TEST_EQUALS( nativeImageInterface->mExtensionCreateCalls, 1, TEST_LOCATION );
271   DALI_TEST_EQUALS( nativeImageInterface->mTargetTextureCalls, 0, TEST_LOCATION );
272   DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), false, TEST_LOCATION );
273   DALI_TEST_EQUALS( drawTrace.FindMethod("DrawElements") || drawTrace.FindMethod("DrawArrays"), false, TEST_LOCATION );
274
275   textureTrace.Reset();
276   drawTrace.Reset();
277
278   nativeImageInterface->SetGlExtensionCreateResult( true );
279   actor.SetPosition( 0, 0, 1 );
280   application.SendNotification();
281   application.Render();
282
283   // This time around, the bind and draw should occur following the call to nativeImage->GlExtensionCreate.
284   DALI_TEST_EQUALS( nativeImageInterface->mExtensionCreateCalls, 2, TEST_LOCATION );
285   DALI_TEST_EQUALS( nativeImageInterface->mTargetTextureCalls, 1, TEST_LOCATION );
286   DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION );
287   DALI_TEST_EQUALS( drawTrace.FindMethod("DrawElements") || drawTrace.FindMethod("DrawArrays"), true, TEST_LOCATION );
288
289   END_TEST;
290 }