Upload package dali_0.9.11.
[platform/core/uifw/dali-core.git] / automated-tests / TET / dali-test-suite / effects / utc-Dali-ShaderEffect.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 #include <iostream>
18
19 #include <stdlib.h>
20 #include <tet_api.h>
21
22 #include <dali/public-api/dali-core.h>
23 #include <dali/internal/event/images/image-impl.h>
24 #include <dali-test-suite-utils.h>
25 #include "../../../build/slp/dali-core/dali-shaders.h"
26
27 using namespace Dali;
28
29 static void Startup();
30 static void Cleanup();
31
32 extern "C" {
33   void (*tet_startup)() = Startup;
34   void (*tet_cleanup)() = Cleanup;
35 }
36
37 static const char* VertexSource =
38 "void main()\n"
39 "{\n"
40 "  gl_Position = uProjection * uModelView * vec4(aPosition, 1.0);\n"
41 "  vTexCoord = aTexCoord;\n"
42 "}\n";
43
44 static const char* FragmentSource =
45 "void main()\n"
46 "{\n"
47 "  gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n"
48 "}\n";
49
50 static const char* FragmentSourceUsingExtensions =
51 "void main()\n"
52 "{\n"
53 "  float floatValue = 0.5f;\n"
54 "  float test = fwidth(floatValue);\n"
55 "  gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n"
56 "  gl_FragColor.a *= test;\n"
57 "}\n";
58
59 const int GETSOURCE_BUFFER_SIZE = 0x10000;
60
61 enum {
62   POSITIVE_TC_IDX = 0x01,
63   NEGATIVE_TC_IDX,
64 };
65
66 #define MAX_NUMBER_OF_TESTS 10000
67 extern "C" {
68   struct tet_testlist tet_testlist[MAX_NUMBER_OF_TESTS];
69 }
70
71 static const char* TestImageFilename = "icon_wrt.png";
72
73 // Add test functionality for all APIs in the class (Positive and Negative)
74 TEST_FUNCTION( UtcDaliShaderEffectMethodNew01, POSITIVE_TC_IDX );
75 TEST_FUNCTION( UtcDaliShaderEffectMethodNew02, NEGATIVE_TC_IDX );
76 TEST_FUNCTION( UtcDaliShaderEffectMethodNew03, POSITIVE_TC_IDX );
77 TEST_FUNCTION( UtcDaliShaderEffectMethodNew04, POSITIVE_TC_IDX );
78 TEST_FUNCTION( UtcDaliShaderEffectMethodNew05, POSITIVE_TC_IDX );
79 TEST_FUNCTION( UtcDaliShaderEffectMethodNew06, POSITIVE_TC_IDX );
80 TEST_FUNCTION( UtcDaliShaderEffectMethodDelete01, POSITIVE_TC_IDX );
81 TEST_FUNCTION( UtcDaliShaderEffectMethodDownCast, POSITIVE_TC_IDX );
82 TEST_FUNCTION( UtcDaliShaderEffectMethodSetUniformFloat, POSITIVE_TC_IDX );
83 TEST_FUNCTION( UtcDaliShaderEffectMethodSetUniformVector2, POSITIVE_TC_IDX );
84 TEST_FUNCTION( UtcDaliShaderEffectMethodSetUniformVector3, POSITIVE_TC_IDX );
85 TEST_FUNCTION( UtcDaliShaderEffectMethodSetUniformVector4, POSITIVE_TC_IDX );
86 TEST_FUNCTION( UtcDaliShaderEffectMethodSetUniformMatrix, POSITIVE_TC_IDX );
87
88 TEST_FUNCTION( UtcDaliShaderEffectMethodSetUniformMatrix3, POSITIVE_TC_IDX );
89 TEST_FUNCTION( UtcDaliShaderEffectMethodSetUniformViewport, POSITIVE_TC_IDX );
90 TEST_FUNCTION( UtcDaliShaderEffectMethodSetEffectImage, POSITIVE_TC_IDX );
91 TEST_FUNCTION( UtcDaliShaderEffectMethodSetEffectImageAndDelete, POSITIVE_TC_IDX );
92 TEST_FUNCTION( UtcDaliShaderEffectMethodApplyConstraint, POSITIVE_TC_IDX );
93 TEST_FUNCTION( UtcDaliShaderEffectMethodApplyConstraintFromActor, POSITIVE_TC_IDX );
94 TEST_FUNCTION( UtcDaliShaderEffectMethodApplyConstraintFromActor2, POSITIVE_TC_IDX );
95 TEST_FUNCTION( UtcDaliShaderEffectMethodApplyConstraintCallback, POSITIVE_TC_IDX );
96 TEST_FUNCTION( UtcDaliShaderEffectMethodRemoveConstraints, POSITIVE_TC_IDX );
97 TEST_FUNCTION( UtcDaliShaderEffectMethodRemoveConstraints2, POSITIVE_TC_IDX );
98
99 TEST_FUNCTION( UtcDaliShaderEffectMethodCreateExtension, POSITIVE_TC_IDX );
100 TEST_FUNCTION( UtcDaliShaderEffectMethodCreateExtension2, POSITIVE_TC_IDX );
101 TEST_FUNCTION( UtcDaliShaderEffectMethodNoExtension, NEGATIVE_TC_IDX );
102 TEST_FUNCTION( UtcDaliShaderEffectFromProperties01, POSITIVE_TC_IDX );
103 TEST_FUNCTION( UtcDaliShaderEffectFromProperties02, NEGATIVE_TC_IDX );
104 TEST_FUNCTION( UtcDaliShaderEffectFromProperties03, NEGATIVE_TC_IDX );
105
106 TEST_FUNCTION( UtcDaliShaderEffectPropertyIndices, POSITIVE_TC_IDX );
107
108 // Called only once before first test is run.
109 static void Startup()
110 {
111 }
112
113 // Called only once after last test is run
114 static void Cleanup()
115 {
116 }
117
118 // Create bitmap image
119 BitmapImage CreateBitmapImage()
120 {
121   BitmapImage image = BitmapImage::New(4,4,Pixel::RGBA8888);
122
123   PixelBuffer* pixbuf = image.GetBuffer();
124
125   // Using a 4x4 image gives a better blend with the GL implementation
126   // than a 3x3 image
127   for(size_t i=0; i<16; i++)
128   {
129     pixbuf[i*4+0] = 0xFF;
130     pixbuf[i*4+1] = 0xFF;
131     pixbuf[i*4+2] = 0xFF;
132     pixbuf[i*4+3] = 0xFF;
133   }
134
135   return image;
136 }
137
138 struct TestConstraintToVector3
139 {
140   TestConstraintToVector3(Vector3 target)
141   : mTarget(target)
142   {
143   }
144
145   Vector3 operator()(const Vector3& current)
146   {
147     return mTarget;
148   }
149
150   Vector3 mTarget;
151 };
152
153 struct TestConstraintFromPositionToVector3
154 {
155   TestConstraintFromPositionToVector3()
156   {
157   }
158
159   Vector3 operator()(const Vector3& current, const PropertyInput& position)
160   {
161
162     return position.GetVector3();
163   }
164 };
165
166 struct TestConstraintToVector3Double
167 {
168   TestConstraintToVector3Double(Vector3 target)
169   : mTarget(target)
170   {
171   }
172
173   Vector3 operator()(const Vector3& current)
174   {
175     return mTarget * 2.0f;
176   }
177
178   Vector3 mTarget;
179 };
180
181 static void UtcDaliShaderEffectMethodNew01()
182 {
183   TestApplication application;
184
185   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
186   DALI_TEST_CHECK(effect);
187 }
188
189 static void UtcDaliShaderEffectMethodNew02()
190 {
191   TestApplication application;
192
193   ShaderEffect effect;
194
195   try
196   {
197     // New must be called to create a ShaderEffect or it wont be valid.
198     effect.SetUniform( "uUniform", 0 );
199     DALI_TEST_CHECK( false );
200   }
201   catch (Dali::DaliException& e)
202   {
203     // Tests that a negative test of an assertion succeeds
204     tet_printf("Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
205     DALI_TEST_CHECK( !effect );
206   }
207 }
208
209 static void UtcDaliShaderEffectMethodNew03()
210 {
211   TestApplication application;
212
213   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource, VertexSource, FragmentSource, ShaderEffect::HINT_NONE );
214   DALI_TEST_CHECK(effect);
215 }
216
217 static void UtcDaliShaderEffectMethodNew04()
218 {
219   TestApplication application;
220   tet_infoline("Testing prefixed version of Dali::ShaderEffect::New()");
221
222   std::string fragmentShaderPrefix = "#define TEST_FS 1\n#extension GL_OES_standard_derivatives : enable";
223   std::string vertexShaderPrefix = "#define TEST_VS 1";
224
225   try
226   {
227     // Call render to compile default shaders.
228     application.SendNotification();
229     application.Render();
230     application.Render();
231     application.Render();
232
233     GLuint lastShaderCompiledBefore = application.GetGlAbstraction().GetLastShaderCompiled();
234     ShaderEffect effect = ShaderEffect::NewWithPrefix( vertexShaderPrefix, VertexSource,
235                                                        fragmentShaderPrefix, FragmentSourceUsingExtensions,
236                                                        GEOMETRY_TYPE_IMAGE, ShaderEffect::HINT_NONE );
237
238     BitmapImage image = CreateBitmapImage();
239     ImageActor actor = ImageActor::New( image );
240     actor.SetSize( 100.0f, 100.0f );
241     actor.SetName("TestImageFilenameActor");
242     actor.SetShaderEffect(effect);
243     Stage::GetCurrent().Add(actor);
244
245     application.SendNotification();
246     application.Render();
247     GLuint lastShaderCompiledAfter = application.GetGlAbstraction().GetLastShaderCompiled();
248     bool testResult = false;
249
250     // we should have compiled 4 shaders.
251     if (lastShaderCompiledAfter - lastShaderCompiledBefore == 4)
252     {
253       char testVertexSourceResult[GETSOURCE_BUFFER_SIZE];
254       char testFragmentSourceResult[GETSOURCE_BUFFER_SIZE];
255
256       // we are interested in the first two.
257       GLuint vertexShaderId = lastShaderCompiledBefore + 1;
258       GLuint fragmentShaderId = lastShaderCompiledBefore + 2;
259
260       GLsizei lengthVertexResult;
261       GLsizei lengthFragmentResult;
262
263       application.GetGlAbstraction().GetShaderSource(vertexShaderId, GETSOURCE_BUFFER_SIZE, &lengthVertexResult, testVertexSourceResult);
264       application.GetGlAbstraction().GetShaderSource(fragmentShaderId, GETSOURCE_BUFFER_SIZE, &lengthFragmentResult, testFragmentSourceResult);
265
266       int vertexShaderHasPrefix = strncmp(testVertexSourceResult, "#define ", strlen("#define "));
267       int fragmentShaderHasPrefix = strncmp(testFragmentSourceResult, "#define ", strlen("#define "));
268       testResult = (vertexShaderHasPrefix == 0) && (fragmentShaderHasPrefix == 0);
269     }
270
271     DALI_TEST_CHECK(testResult);
272   }
273   catch(Dali::DaliException& e)
274   {
275     tet_printf("Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
276     tet_result( TET_FAIL );
277   }
278 }
279
280 class ShaderEffectExtension : public ShaderEffect::Extension {};
281 static void UtcDaliShaderEffectMethodNew05()
282 {
283   TestApplication application;
284
285   // heap constructor / destructor
286   DefaultFunctionCoverage<ShaderEffect> shaderEffect;
287   DefaultFunctionCoverage<ShaderEffectExtension> shaderEffectExtension;
288
289 }
290
291 static void UtcDaliShaderEffectMethodNew06()
292 {
293   TestApplication application;
294   tet_infoline("Testing Dali::ShaderEffect::New() with shader sources for different geometry types");
295
296   ShaderEffect effect = ShaderEffect::New( "imageVertexShader", "imageFragmentShader",
297                                            "textVertexShader", "textFragmentShader",
298                                            "texturedMeshVertexShader", "texturedMeshFragmentShader",
299                                            "meshVertexShader", "meshFragmentShader",
300                                            ShaderEffect::HINT_NONE );
301   DALI_TEST_CHECK(effect);
302 }
303
304
305 static void UtcDaliShaderEffectMethodDownCast()
306 {
307   TestApplication application;
308   tet_infoline("Testing Dali::ShaderEffect::DownCast()");
309
310   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
311
312   BaseHandle object(effect);
313
314   ShaderEffect effect2 = ShaderEffect::DownCast(object);
315   DALI_TEST_CHECK(effect2);
316
317   ShaderEffect effect3 = DownCast< ShaderEffect >(object);
318   DALI_TEST_CHECK(effect3);
319
320   BaseHandle unInitializedObject;
321   ShaderEffect effect4 = ShaderEffect::DownCast(unInitializedObject);
322   DALI_TEST_CHECK(!effect4);
323
324   ShaderEffect effect5 = DownCast< ShaderEffect >(unInitializedObject);
325   DALI_TEST_CHECK(!effect5);
326 }
327
328 static void UtcDaliShaderEffectMethodDelete01()
329 {
330    TestApplication application;
331
332    // get the default shaders built, this is not required but makes it
333    // easier to debug the TET case and isolate the custom shader compilation.
334    application.SendNotification();
335    application.Render();
336
337    application.SendNotification();
338    application.Render();
339
340    // create a new shader effect
341    // the vertex and fragment shader will be cached in the ShaderFactory
342    ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource);
343
344    // destroy the shader effect
345    effect.Reset();
346
347    // Create the same shader effect again, this should now use the cached version
348    // held in the shader factory
349    effect= ShaderEffect::New( VertexSource, FragmentSource );
350
351    // Compile the shader effect
352    application.SendNotification();
353    application.Render();
354
355    GLuint lastShaderCompiled = application.GetGlAbstraction().GetLastShaderCompiled();
356
357    // get the vertex shader (compiled before fragment shader).
358    // this last shaders compiled is for text.
359    GLuint vertexShaderId = lastShaderCompiled-1;
360    GLsizei lengthVertexResult;
361
362    char testVertexSourceResult[GETSOURCE_BUFFER_SIZE];
363    application.GetGlAbstraction().GetShaderSource(vertexShaderId, GETSOURCE_BUFFER_SIZE, &lengthVertexResult, testVertexSourceResult);
364
365    // compare the first 10 bytes of the vertex shader sent to be compiled, with
366    // the shader string that ended up being compiled (in the render task)
367    // this is to confirm the string hasn't been deleted / corrupted.
368    int testPassed = strncmp( testVertexSourceResult , Dali::Internal::CustomFontPrefixVertex.c_str(), 10 ) ;
369
370    DALI_TEST_CHECK( testPassed == 0 );
371 }
372
373 static void UtcDaliShaderEffectMethodSetUniformFloat()
374 {
375   TestApplication application;
376
377   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
378   DALI_TEST_CHECK( effect );
379
380   BitmapImage image = CreateBitmapImage();
381
382   effect.SetUniform( "uFloat", 1.0f );
383
384   ImageActor actor = ImageActor::New( image );
385   actor.SetSize( 100.0f, 100.0f );
386   actor.SetName("TestImageFilenameActor");
387   actor.SetShaderEffect(effect);
388   Stage::GetCurrent().Add(actor);
389
390   application.SendNotification();
391   application.Render();
392
393   DALI_TEST_CHECK(
394       application.GetGlAbstraction().CheckUniformValue(
395           "uFloat", 1.0f ) );
396 }
397
398 static void UtcDaliShaderEffectMethodSetUniformVector2()
399 {
400   TestApplication application;
401
402   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
403   DALI_TEST_CHECK( effect );
404
405   BitmapImage image = CreateBitmapImage();
406
407   effect.SetUniform( "uVec2", Vector2( 2.0f, 3.0f ) );
408
409   ImageActor actor = ImageActor::New( image );
410   actor.SetSize( 100.0f, 100.0f );
411   actor.SetName("TestImageFilenameActor");
412   actor.SetShaderEffect(effect);
413   Stage::GetCurrent().Add(actor);
414
415   application.SendNotification();
416   application.Render();
417
418   DALI_TEST_CHECK(
419       application.GetGlAbstraction().CheckUniformValue(
420           "uVec2", Vector2( 2.0f, 3.0f ) ) );
421 }
422
423 static void UtcDaliShaderEffectMethodSetUniformVector3()
424 {
425   TestApplication application;
426
427   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
428   DALI_TEST_CHECK( effect );
429
430   BitmapImage image = CreateBitmapImage();
431
432   effect.SetUniform( "uVec3", Vector3( 4.0f, 5.0f, 6.0f ) );
433
434   ImageActor actor = ImageActor::New( image );
435   actor.SetSize( 100.0f, 100.0f );
436   actor.SetName("TestImageFilenameActor");
437   actor.SetShaderEffect(effect);
438   Stage::GetCurrent().Add(actor);
439
440   application.SendNotification();
441   application.Render();
442
443   DALI_TEST_CHECK(
444       application.GetGlAbstraction().CheckUniformValue(
445           "uVec3", Vector3( 4.0f, 5.0f, 6.0f ) ) );
446 }
447
448 static void UtcDaliShaderEffectMethodSetUniformVector4()
449 {
450   TestApplication application;
451
452   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
453   DALI_TEST_CHECK( effect );
454
455   BitmapImage image = CreateBitmapImage();
456
457   effect.SetUniform( "uVec4", Vector4( 7.0f, 8.0f, 9.0f, 10.0f ) );
458
459   ImageActor actor = ImageActor::New( image );
460   actor.SetSize( 100.0f, 100.0f );
461   actor.SetName("TestImageFilenameActor");
462   actor.SetShaderEffect(effect);
463   Stage::GetCurrent().Add(actor);
464
465   application.SendNotification();
466   application.Render();
467
468   DALI_TEST_CHECK(
469       application.GetGlAbstraction().CheckUniformValue(
470           "uVec4", Vector4( 7.0f, 8.0f, 9.0f, 10.0f ) ) );
471 }
472
473 static void UtcDaliShaderEffectMethodSetUniformMatrix()
474 {
475   TestApplication application;
476
477   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
478   DALI_TEST_CHECK( effect );
479
480   BitmapImage image = CreateBitmapImage();
481
482   effect.SetUniform( "uModelView", Matrix::IDENTITY );
483
484   ImageActor actor = ImageActor::New( image );
485   actor.SetSize( 100.0f, 100.0f );
486   actor.SetName("TestImageFilenameActor");
487   actor.SetShaderEffect(effect);
488   Stage::GetCurrent().Add(actor);
489
490   application.SendNotification();
491   application.Render();
492
493   DALI_TEST_CHECK(
494       application.GetGlAbstraction().CheckUniformValue(
495           "uModelView", Matrix::IDENTITY ) );
496 }
497
498 static void UtcDaliShaderEffectMethodSetUniformMatrix3()
499 {
500   TestApplication application;
501
502   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
503   DALI_TEST_CHECK( effect );
504
505   BitmapImage image = CreateBitmapImage();
506
507   Matrix3 matIdentity(1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f);
508   effect.SetUniform( "uMatrix3", matIdentity );
509
510   ImageActor actor = ImageActor::New( image );
511   actor.SetSize( 100.0f, 100.0f );
512   actor.SetName("TestImageFilenameActor");
513   actor.SetShaderEffect(effect);
514   Stage::GetCurrent().Add(actor);
515
516   application.SendNotification();
517   application.Render();
518
519   DALI_TEST_CHECK( application.GetGlAbstraction().CheckUniformValue("uMatrix3", matIdentity) );
520 }
521
522 static void UtcDaliShaderEffectMethodSetUniformViewport()
523 {
524   TestApplication application;
525
526   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
527   DALI_TEST_CHECK( effect );
528
529   BitmapImage image = CreateBitmapImage();
530
531   ImageActor actor = ImageActor::New( image );
532   actor.SetSize( 100.0f, 100.0f );
533   actor.SetName("TestImageFilenameActor");
534   actor.SetShaderEffect(effect);
535   Stage::GetCurrent().Add(actor);
536
537   effect.SetUniform( "uVec2", Vector2( 0.0f, 0.0f ), ShaderEffect::COORDINATE_TYPE_VIEWPORT_POSITION );
538   effect.SetUniform( "uVec2Dir", Vector2( 1.0f, 2.0f ), ShaderEffect::COORDINATE_TYPE_VIEWPORT_DIRECTION );
539
540   application.SendNotification();
541   application.Render();
542
543   const Vector2& stageSize(Stage::GetCurrent().GetSize());
544
545   DALI_TEST_CHECK(
546       application.GetGlAbstraction().CheckUniformValue(
547           "uVec2", Vector2( stageSize.x/2, -stageSize.y/2 ) ) );
548   DALI_TEST_CHECK(
549       application.GetGlAbstraction().CheckUniformValue(
550           "uVec2Dir", Vector2( -1.0f, 2.0f ) ) );
551 }
552
553 static void UtcDaliShaderEffectMethodSetEffectImage()
554 {
555   TestApplication application;
556
557   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
558   DALI_TEST_CHECK( effect );
559
560   BitmapImage image = CreateBitmapImage();
561
562   effect.SetEffectImage(image);
563
564   ImageActor actor = ImageActor::New( image );
565   actor.SetSize( 100.0f, 100.0f );
566   actor.SetName("TestImageFilenameActor");
567   actor.SetShaderEffect(effect);
568   Stage::GetCurrent().Add(actor);
569
570   application.SendNotification();
571   application.Render(16);
572   application.SendNotification();
573   application.Render(16);
574   application.SendNotification();
575
576   DALI_TEST_CHECK( application.GetGlAbstraction().CheckUniformValue( "sEffect", 1 ) );
577 }
578
579 static void UtcDaliShaderEffectMethodSetEffectImageAndDelete()
580 {
581   TestApplication application;
582
583   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
584
585   BitmapImage effectImage = CreateBitmapImage();
586   effect.SetEffectImage(effectImage);
587
588   Integration::ResourceId imageId = GetImplementation(effectImage).GetResourceId();
589   tet_printf("Effect Image id %d \n ",imageId);
590
591
592   ImageActor actor = ImageActor::New();
593
594   actor.SetShaderEffect(effect);
595   effect.Reset();
596
597   Stage::GetCurrent().Add(actor);
598
599   // do an update / render cycle
600   application.SendNotification();
601   application.Render(16);
602   application.SendNotification();
603   application.Render(16);
604   application.SendNotification();
605   application.Render(16);
606
607   printf("removing image actor from stage and Reseting handle\n");
608   Stage::GetCurrent().Remove(actor);
609   actor.Reset();
610
611   tet_printf("### Update & Render  \n");
612
613   application.SendNotification();
614   application.Render(16);
615
616   tet_printf("#### Update Only  \n");
617
618   tet_printf("effectImage.Reset \n");
619
620   // this releases the effect texture resource,
621   // Update will send a DispatchDiscardTexture message to render
622   effectImage.Reset();
623   application.SendNotification();
624   application.UpdateOnly(16);
625
626   tet_printf("#### Update Only \n");
627
628   // at this point shader is deleted, during clear discard queue
629   // and it sends a Shader:: DispatchRemoveObserver message to render thread
630   application.UpdateOnly(16);
631
632   tet_printf("#### Render Only  \n");
633   // This is where it used to crash, there is a message in the queue to perform DispatchDiscardTexture
634   // which tries to call observer->TextureDiscarded, where observer == shader that was deleted
635   // in previous update.
636   application.RenderOnly();
637
638
639   // process the discard texture message
640   application.RenderOnly();
641   application.SendNotification();
642   application.Render(16);
643
644   tet_result(TET_PASS);
645
646 }
647 static void UtcDaliShaderEffectMethodApplyConstraint()
648 {
649   // Test whether Shader's uniform can be constrained to a stationary constraint.
650   TestApplication application;
651
652   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
653   DALI_TEST_CHECK( effect );
654
655   BitmapImage image = CreateBitmapImage();
656
657   effect.SetUniform( "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) );
658
659   ImageActor actor = ImageActor::New( image );
660   actor.SetSize( 100.0f, 100.0f );
661   actor.SetName("TestImageFilenameActor");
662   actor.SetShaderEffect(effect);
663   Stage::GetCurrent().Add(actor);
664
665   Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
666
667   application.SendNotification();
668   application.Render();
669
670   // Test effects of SetUniform...
671   DALI_TEST_CHECK(
672       application.GetGlAbstraction().CheckUniformValue(
673           "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
674
675   Constraint constraint = Constraint::New<Vector3>( uVecProperty,
676                                                     TestConstraintToVector3(Vector3(4.0f, 9.0f, 16.0f)) );
677
678   effect.ApplyConstraint(constraint);
679
680   application.SendNotification();
681   application.Render();
682
683   // Test effects of Constraint.
684   DALI_TEST_CHECK(
685       application.GetGlAbstraction().CheckUniformValue(
686           "uVec3", Vector3( 4.0f, 9.0f, 16.0f ) ) );
687 }
688
689
690 static void UtcDaliShaderEffectMethodApplyConstraintFromActor()
691 {
692   // Test whether Shader's uniform can be constrained to Actor's position.
693   TestApplication application;
694
695   const Vector3 targetPosition = Vector3( 100.0f, 70.0f, 20.0f);
696
697   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
698   DALI_TEST_CHECK( effect );
699
700   BitmapImage image = CreateBitmapImage();
701
702   effect.SetUniform( "uVec3", Vector3( 50.0f, 25.0f, 0.0f ) );
703
704   ImageActor actor = ImageActor::New( image );
705   actor.SetPosition(targetPosition);
706   actor.SetSize( 100.0f, 100.0f );
707   actor.SetName("TestImageFilenameActor");
708   actor.SetShaderEffect(effect);
709   Stage::GetCurrent().Add(actor);
710
711   Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
712
713   Constraint constraint = Constraint::New<Vector3>( uVecProperty,
714                                                     Source(actor, Actor::POSITION),
715                                                     TestConstraintFromPositionToVector3() );
716
717   effect.ApplyConstraint(constraint);
718
719   application.SendNotification();
720   application.Render();
721
722   // Test effects of Constraint.
723   DALI_TEST_CHECK(
724       application.GetGlAbstraction().CheckUniformValue(
725           "uVec3", targetPosition ) );
726 }
727
728 static void UtcDaliShaderEffectMethodApplyConstraintFromActor2()
729 {
730   // Test whether Shader's uniform can be constrained to Actor's position.
731   // While Actor's position is constrained to another point * 2.0f
732   TestApplication application;
733
734   const Vector3 targetPosition = Vector3( 25.0f, 36.0f, 49.0f );
735
736   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
737   DALI_TEST_CHECK( effect );
738
739   BitmapImage image = CreateBitmapImage();
740
741   effect.SetUniform( "uVec3", Vector3( 50.0f, 25.0f, 0.0f ) );
742
743   ImageActor actor = ImageActor::New( image );
744   actor.SetPosition(Vector3( 100.0f, 70.0f, 20.0f));
745   actor.SetSize( 100.0f, 100.0f );
746   actor.SetName("TestImageFilenameActor");
747   actor.SetShaderEffect(effect);
748   Stage::GetCurrent().Add(actor);
749
750   Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
751
752   Constraint shaderConstraint = Constraint::New<Vector3>( uVecProperty,
753                                                     Source(actor, Actor::POSITION),
754                                                     TestConstraintFromPositionToVector3() );
755
756   effect.ApplyConstraint(shaderConstraint);
757
758   Constraint actorConstraint = Constraint::New<Vector3>( Actor::POSITION,
759                                                          TestConstraintToVector3Double(targetPosition) );
760
761   actor.ApplyConstraint(actorConstraint);
762
763   application.SendNotification();
764   application.Render();
765
766   // Test effects of Constraint.
767   DALI_TEST_CHECK(
768       application.GetGlAbstraction().CheckUniformValue(
769           "uVec3", targetPosition * 2.0f ) );
770 }
771
772 static void UtcDaliShaderEffectMethodApplyConstraintCallback()
773 {
774   // Test whether Shader's uniform can be constrained to a stationary constraint.
775   TestApplication application;
776
777   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
778   DALI_TEST_CHECK( effect );
779
780   BitmapImage image = CreateBitmapImage();
781
782   effect.SetUniform( "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) );
783
784   ImageActor actor = ImageActor::New( image );
785   actor.SetSize( 100.0f, 100.0f );
786   actor.SetName("TestImageFilenameActor");
787   actor.SetShaderEffect(effect);
788   Stage::GetCurrent().Add(actor);
789
790   Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
791
792   application.SendNotification();
793   application.Render();
794
795   // Test effects of SetUniform...
796   DALI_TEST_CHECK(
797       application.GetGlAbstraction().CheckUniformValue(
798           "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
799
800   Constraint constraint = Constraint::New<Vector3>( uVecProperty,
801                                                     TestConstraintToVector3(Vector3(4.0f, 9.0f, 16.0f)) );
802
803   constraint.SetApplyTime( 10.0f );
804
805   bool constraintCheck( false );
806   ConstraintAppliedCheck appliedCheck( constraintCheck );
807
808   // We should receive the "Applied" signal after 10 seconds
809   ActiveConstraint active = effect.ApplyConstraint(constraint);
810   active.AppliedSignal().Connect( &application, appliedCheck );
811
812   application.SendNotification();
813   application.Render(static_cast<unsigned int>(1000.0f)); // 1 elapsed second
814
815   // Check signal has not fired
816   application.SendNotification();
817   appliedCheck.CheckSignalNotReceived();
818
819   application.Render(static_cast<unsigned int>(4000.0f)); // 5 elapsed seconds
820
821   // Check signal has not fired
822   application.SendNotification();
823   appliedCheck.CheckSignalNotReceived();
824
825   application.Render(static_cast<unsigned int>(5000.0f - 1.0f)); // <10 elapsed seconds
826
827   // Check signal has not fired
828   application.SendNotification();
829   appliedCheck.CheckSignalNotReceived();
830
831   application.Render(static_cast<unsigned int>(2.0f)); // >10 elapsed seconds
832
833   // Signal should have fired
834   application.SendNotification();
835   appliedCheck.CheckSignalReceived();
836
837   // Test effects of Constraint.
838   DALI_TEST_CHECK(
839       application.GetGlAbstraction().CheckUniformValue(
840           "uVec3", Vector3( 4.0f, 9.0f, 16.0f ) ) );
841 }
842
843 static void UtcDaliShaderEffectMethodRemoveConstraints()
844 {
845   // Test if constrains can be removed before they are ever applyed.
846   TestApplication application;
847
848   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
849   DALI_TEST_CHECK( effect );
850
851   BitmapImage image = CreateBitmapImage();
852
853   effect.SetUniform( "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) );
854
855   ImageActor actor = ImageActor::New( image );
856   actor.SetSize( 100.0f, 100.0f );
857   actor.SetName("TestImageFilenameActor");
858   actor.SetShaderEffect(effect);
859   Stage::GetCurrent().Add(actor);
860
861   Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
862
863   application.SendNotification();
864   application.Render();
865
866   // Test effects of SetUniform...
867   DALI_TEST_CHECK(
868       application.GetGlAbstraction().CheckUniformValue(
869           "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
870
871   Constraint constraint = Constraint::New<Vector3>( uVecProperty,
872                                                     TestConstraintToVector3(Vector3(4.0f, 9.0f, 16.0f)) );
873
874   effect.ApplyConstraint(constraint);
875
876   // Remove the constraints
877   effect.RemoveConstraints();
878
879   application.SendNotification();
880   application.Render();
881
882   // Test effects of Constraint.
883   DALI_TEST_CHECK(
884       application.GetGlAbstraction().CheckUniformValue(
885           "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
886 }
887
888 static void UtcDaliShaderEffectMethodRemoveConstraints2()
889 {
890   // Test whether Shader's uniform constrains can be removed after they are applyed.
891   TestApplication application;
892
893   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
894   DALI_TEST_CHECK( effect );
895
896   BitmapImage image = CreateBitmapImage();
897
898   effect.SetUniform( "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) );
899
900   ImageActor actor = ImageActor::New( image );
901   actor.SetSize( 100.0f, 100.0f );
902   actor.SetName("TestImageFilenameActor");
903   actor.SetShaderEffect(effect);
904   Stage::GetCurrent().Add(actor);
905
906   Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
907
908   application.SendNotification();
909   application.Render();
910
911   // Test effects of SetUniform...
912   DALI_TEST_CHECK(
913       application.GetGlAbstraction().CheckUniformValue(
914           "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
915
916   Constraint constraint = Constraint::New<Vector3>( uVecProperty,
917                                                     TestConstraintToVector3(Vector3(4.0f, 9.0f, 16.0f)) );
918
919   effect.ApplyConstraint(constraint);
920
921   application.SendNotification();
922   application.Render();
923
924   // Reset the value and remove the constraints
925   effect.SetUniform( "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) );
926   effect.RemoveConstraints();
927
928   application.SendNotification();
929   application.Render();
930
931   // Test effects of Constraint.
932   DALI_TEST_CHECK(
933       application.GetGlAbstraction().CheckUniformValue(
934           "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
935 }
936
937
938 class TestExtension : public ShaderEffect::Extension
939 {
940 public:
941   TestExtension( bool& deleted )
942   : mDeleted(deleted)
943   {
944     mDeleted = false;
945   }
946   ~TestExtension()
947   {
948     mDeleted = true;
949   }
950   bool IsAlive() const
951   {
952     return !mDeleted;
953   }
954 private:
955   bool& mDeleted;
956 };
957
958 static void UtcDaliShaderEffectMethodCreateExtension()
959 {
960   // Test creation of a shader extension
961   TestApplication aplication;
962
963   bool deleted;
964   {
965     ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
966     DALI_TEST_CHECK( effect );
967
968     TestExtension* extension = new TestExtension ( deleted );
969
970     effect.AttachExtension( extension );
971
972     DALI_TEST_CHECK( static_cast<TestExtension&>(effect.GetExtension()).IsAlive() );
973   }
974
975   DALI_TEST_CHECK( deleted );
976 }
977
978 static void UtcDaliShaderEffectMethodCreateExtension2()
979 {
980   // Test creation of a shader extension
981   bool deleted;
982   {
983     TestApplication application;
984
985     ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
986     DALI_TEST_CHECK( effect );
987
988     BitmapImage image = CreateBitmapImage();
989
990     effect.SetUniform( "uFloat", 1.0f );
991
992     ImageActor actor = ImageActor::New( image );
993     actor.SetSize( 100.0f, 100.0f );
994     actor.SetName("TestImageFilenameActor");
995     actor.SetShaderEffect(effect);
996     Stage::GetCurrent().Add(actor);
997
998     application.SendNotification();
999     application.Render();
1000
1001     TestExtension* extension = new TestExtension ( deleted );
1002
1003     effect.AttachExtension( extension );
1004
1005     const ShaderEffect& constEffect(effect);
1006     const TestExtension& ext( static_cast<const TestExtension&>(constEffect.GetExtension()) );
1007
1008     DALI_TEST_CHECK( ext.IsAlive() );
1009   }
1010
1011   DALI_TEST_CHECK( deleted );
1012 }
1013
1014 static void UtcDaliShaderEffectMethodNoExtension()
1015 {
1016   TestApplication application;
1017
1018   ShaderEffect effect;
1019
1020   try
1021   {
1022     ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
1023     DALI_TEST_CHECK( effect );
1024
1025     // Don't attach extension
1026     ShaderEffect::Extension& extension = effect.GetExtension();
1027     (void) extension;
1028
1029     DALI_TEST_CHECK( false );
1030   }
1031   catch (Dali::DaliException& e)
1032   {
1033     // Tests that a negative test of an assertion succeeds
1034     tet_printf("Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
1035     DALI_TEST_CHECK( !effect );
1036   }
1037 }
1038
1039 static void UtcDaliShaderEffectFromProperties01()
1040 {
1041   TestApplication application;
1042   tet_infoline("UtcDaliShaderEffectFromProperties01()");
1043
1044   std::string fragmentShaderPrefix = "#define TEST_FS 1\n#extension GL_OES_standard_derivatives : enable";
1045   std::string vertexShaderPrefix = "#define TEST_VS 1";
1046
1047   // Call render to compile default shaders.
1048   application.SendNotification();
1049   application.Render();
1050   application.Render();
1051   application.Render();
1052
1053   GLuint lastShaderCompiledBefore = application.GetGlAbstraction().GetLastShaderCompiled();
1054
1055   // create from type registry
1056
1057   TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( "ShaderEffect" );
1058   DALI_TEST_CHECK( typeInfo );
1059   ShaderEffect effect = ShaderEffect::DownCast( typeInfo.CreateInstance() );
1060   DALI_TEST_CHECK( effect );
1061
1062   Property::Value programMap = Property::Value(Property::MAP);
1063
1064   programMap.SetValue("vertex", std::string(VertexSource));
1065   programMap.SetValue("fragment", std::string(FragmentSource));
1066
1067   programMap.SetValue("vertex-prefix", std::string(fragmentShaderPrefix));
1068   programMap.SetValue("fragment-prefix", std::string(vertexShaderPrefix));
1069
1070   programMap.SetValue("geometry-type", "GEOMETRY_TYPE_IMAGE");
1071
1072   effect.SetProperty(effect.GetPropertyIndex("program"), programMap);
1073
1074   Property::Value imageMap = Property::Value(Property::MAP);
1075   imageMap.SetValue("filename", Property::Value(TestImageFilename));
1076
1077   effect.SetProperty(effect.GetPropertyIndex("image"), imageMap);
1078
1079   BitmapImage image = CreateBitmapImage();
1080   ImageActor actor = ImageActor::New( image );
1081   actor.SetSize( 100.0f, 100.0f );
1082   actor.SetName("TestImageFilenameActor");
1083   actor.SetShaderEffect(effect);
1084   Stage::GetCurrent().Add(actor);
1085
1086   application.SendNotification();
1087   application.Render();
1088   GLuint lastShaderCompiledAfter = application.GetGlAbstraction().GetLastShaderCompiled();
1089   bool testResult = false;
1090
1091   // we should have compiled 4 shaders.
1092   DALI_TEST_CHECK(lastShaderCompiledAfter - lastShaderCompiledBefore == 4);
1093   if (lastShaderCompiledAfter - lastShaderCompiledBefore == 4)
1094   {
1095     char testVertexSourceResult[GETSOURCE_BUFFER_SIZE];
1096     char testFragmentSourceResult[GETSOURCE_BUFFER_SIZE];
1097
1098     // we are interested in the first two.
1099     GLuint vertexShaderId = lastShaderCompiledBefore + 1;
1100     GLuint fragmentShaderId = lastShaderCompiledBefore + 2;
1101
1102     GLsizei lengthVertexResult;
1103     GLsizei lengthFragmentResult;
1104
1105     application.GetGlAbstraction().GetShaderSource(vertexShaderId, GETSOURCE_BUFFER_SIZE, &lengthVertexResult, testVertexSourceResult);
1106     application.GetGlAbstraction().GetShaderSource(fragmentShaderId, GETSOURCE_BUFFER_SIZE, &lengthFragmentResult, testFragmentSourceResult);
1107
1108     int vertexShaderHasPrefix = strncmp(testVertexSourceResult, "#define ", strlen("#define "));
1109     int fragmentShaderHasPrefix = strncmp(testFragmentSourceResult, "#define ", strlen("#define "));
1110     testResult = (vertexShaderHasPrefix == 0) && (fragmentShaderHasPrefix == 0);
1111   }
1112   DALI_TEST_CHECK(testResult);
1113
1114 }
1115
1116 static void UtcDaliShaderEffectFromProperties02()
1117 {
1118   try
1119   {
1120     TestApplication application;
1121     tet_infoline("UtcDaliShaderEffectFromProperties02()");
1122
1123     // Call render to compile default shaders.
1124     application.SendNotification();
1125     application.Render();
1126     application.Render();
1127     application.Render();
1128
1129     // create from type registry (currently only way to get ShaderEffect with no shader setup in constructor
1130     TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( "ShaderEffect" );
1131     DALI_TEST_CHECK( typeInfo );
1132     ShaderEffect effect = ShaderEffect::DownCast( typeInfo.CreateInstance() );
1133     DALI_TEST_CHECK( effect );
1134
1135     Property::Value programMap = Property::Value(Property::MAP);
1136
1137     programMap.SetValue("vertex",   std::string(VertexSource));
1138     programMap.SetValue("fragment", std::string(FragmentSource));
1139
1140     // programMap.SetValue("geometry-type", "GEOMETRY_TYPE_IMAGE");
1141     // dont set by value
1142     programMap.SetValue("geometry-type", GeometryType( GEOMETRY_TYPE_IMAGE ));
1143
1144     effect.SetProperty(effect.GetPropertyIndex("program"), programMap);
1145
1146     tet_result( TET_FAIL );
1147   }
1148   catch(Dali::DaliException& e)
1149   {
1150     tet_printf("Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
1151   }
1152 }
1153
1154 static void UtcDaliShaderEffectFromProperties03()
1155 {
1156   try
1157   {
1158     TestApplication application;
1159     tet_infoline("UtcDaliShaderEffectFromProperties03()");
1160
1161     // Call render to compile default shaders.
1162     application.SendNotification();
1163     application.Render();
1164     application.Render();
1165     application.Render();
1166
1167     // create from type registry (currently only way to get ShaderEffect with no shader setup in constructor
1168     TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( "ShaderEffect" );
1169     DALI_TEST_CHECK( typeInfo );
1170     ShaderEffect effect = ShaderEffect::DownCast( typeInfo.CreateInstance() );
1171     DALI_TEST_CHECK( effect );
1172
1173     // dont set unknown
1174     effect.SetProperty( effect.GetPropertyIndex("geometry-hints"), "HINT_2" );
1175
1176     tet_result( TET_FAIL );
1177   }
1178   catch(Dali::DaliException& e)
1179   {
1180     tet_printf("Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
1181   }
1182 }
1183
1184 void UtcDaliShaderEffectPropertyIndices()
1185 {
1186   TestApplication application;
1187   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
1188
1189   Property::IndexContainer indices;
1190   effect.GetPropertyIndices( indices );
1191   DALI_TEST_CHECK( ! indices.empty() );
1192   DALI_TEST_EQUALS( indices.size(), effect.GetPropertyCount(), TEST_LOCATION );
1193 }