removed reliance on dali-adaptor
[platform/core/uifw/dali-core.git] / automated-tests / src / dali-internal / utc-Dali-Internal-Material.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
20 #include <stdlib.h>
21 #include <dali/public-api/dali-core.h>
22
23 #include <dali-test-suite-utils.h>
24
25 using namespace Dali;
26
27 #include <mesh-builder.h>
28
29 #include <dali/internal/event/common/thread-local-storage.h>
30 #include <dali/internal/update/resources/resource-manager.h>
31 #include <dali/internal/update/manager/update-manager.h>
32 #include <dali/internal/event/resources/resource-client.h>
33 #include <dali/internal/event/resources/resource-ticket.h>
34 #include <dali/internal/event/resources/image-ticket.h>
35 #include <dali/internal/event/resources/resource-ticket-observer.h>
36 #include <dali/internal/event/images/image-impl.h>
37 #include <dali/internal/event/modeling/model-data-impl.h>
38 #include <dali/integration-api/resource-cache.h>
39 #include <dali/internal/event/modeling/material-impl.h>
40 #include <dali/internal/update/modeling/scene-graph-material.h>
41 #include <dali/internal/render/renderers/render-material.h>
42 #include <dali/internal/render/gl-resources/context.h>
43 #include <dali/internal/render/shaders/program.h>
44
45 namespace
46 {
47 Dali::Internal::MaterialProperties TEST_PROPS;
48 }
49
50 // Called only once before first test is run.
51 void utc_dali_material_startup(void)
52 {
53   TEST_PROPS.mOpacity       = 0.4f;
54   TEST_PROPS.mShininess     = 0.27f;
55   TEST_PROPS.mDiffuseColor  = Color::MAGENTA;
56   TEST_PROPS.mAmbientColor  = Color::GREEN;
57   TEST_PROPS.mSpecularColor = Color::BLUE;
58   TEST_PROPS.mEmissiveColor = Color::RED;
59   test_return_value = TET_UNDEF;
60 }
61
62 // Called only once after last test is run
63 void utc_dali_material_cleanup(void)
64 {
65   test_return_value = TET_PASS;
66 }
67
68 namespace
69 {
70
71 class TestProgram
72 {
73 public:
74   TestProgram( TestApplication& application )
75   : app(application)
76   {
77     Internal::Context* testContext = new Internal::Context( application.GetGlAbstraction() );
78     Integration::ShaderDataPtr shaderData = new Integration::ShaderData("123", "132");
79     shaderData->AllocateBuffer(10);
80
81     Integration::ResourceId resourceId = 100;
82     program = Internal::Program::New(resourceId, shaderData.Get(), *testContext, true);
83     programId = app.GetGlAbstraction().GetLastProgramCreated();
84     program->Use();
85   }
86
87   virtual ~TestProgram()
88   {
89   }
90
91   Internal::Program& GetProgram()
92   {
93     return *program;
94   }
95
96   float GetUniformF(std::string uniform)
97   {
98     GLint uniformLoc = program->GetUniformLocation( program->RegisterUniform( uniform.c_str() ) );
99     float value=0.0f;
100     if(app.GetGlAbstraction().GetUniformValue( programId, (GLuint) uniformLoc, value))
101     {
102       return value;
103     }
104     return 0.0f;
105   }
106
107   Vector4 GetUniformV(std::string uniform)
108   {
109     GLint uniformLoc = program->GetUniformLocation( program->RegisterUniform( uniform.c_str() ) );
110     Vector4 value;
111     if(app.GetGlAbstraction().GetUniformValue( programId, (GLuint) uniformLoc, value))
112     {
113       return value;
114     }
115     return Vector4();
116   }
117
118   float GetOpacity()         { return GetUniformF("uMaterial.mOpacity"); }
119   float GetShininess()       { return GetUniformF("uMaterial.mShininess"); }
120   Vector4 GetAmbientColor()  { return GetUniformV("uMaterial.mAmbient"); }
121   Vector4 GetDiffuseColor()  { return GetUniformV("uMaterial.mDiffuse"); }
122   Vector4 GetSpecularColor() { return GetUniformV("uMaterial.mSpecular"); }
123   Vector4 GetEmissiveColor() { return GetUniformV("uMaterial.mEmissive"); }
124
125   TestApplication& app;
126   GLuint programId;
127   Internal::Program* program;
128   Integration::ShaderDataPtr shaderData;
129 };
130
131
132 class TestBoundTextures
133 {
134 public:
135   TestBoundTextures(TestApplication& application)
136   : app(application)
137   {
138     std::vector<GLuint> ids;
139     ids.push_back( 8 ); // 8 = actor1
140     ids.push_back( 9 ); // 9 = actor2
141     ids.push_back( 10 ); // 10 = actor3
142     application.GetGlAbstraction().SetNextTextureIds( ids );
143   }
144
145   std::size_t GetNumBoundTextures()
146   {
147     const std::vector<GLuint>& boundTextures = app.GetGlAbstraction().GetBoundTextures();
148     return boundTextures.size();
149   }
150
151   bool CheckFirstTextureBound( GLuint activeTextureUnit )
152   {
153     bool bound=false;
154     const std::vector<GLuint>& boundTextures = app.GetGlAbstraction().GetBoundTextures( activeTextureUnit );
155
156     if ( boundTextures.size() == 1 )
157     {
158       if( boundTextures[0] == 8u )
159       {
160         bound = true;
161       }
162     }
163     return bound;
164   }
165
166   bool CheckFirstTextureDeleted()
167   {
168     return ( app.GetGlAbstraction().CheckTextureDeleted( 8u ));
169   }
170
171   TestApplication& app;
172 };
173
174
175 Internal::ResourceTicketPtr CheckLoadBitmap(TestApplication& application, const char* name, int w, int h)
176 {
177   Internal::ResourceClient& resourceClient = Internal::ThreadLocalStorage::Get().GetResourceClient();
178   ImageAttributes attr;
179   Integration::BitmapResourceType bitmapRequest(attr);
180   Internal::ResourceTicketPtr ticket = resourceClient.RequestResource( bitmapRequest, name );
181   application.SendNotification(); // Flush update messages
182   application.Render();           // Process resource request
183   Integration::ResourceRequest*   req = application.GetPlatform().GetRequest();
184   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, false );
185   bitmap->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888, w, h, w, h );
186   Integration::ResourcePointer resourcePtr(bitmap); // reference it
187   application.GetPlatform().SetResourceLoaded(req->GetId(), req->GetType()->id, resourcePtr);
188   application.Render();           // Process LoadComplete
189   application.SendNotification(); // Process event messages
190   DALI_TEST_CHECK( ticket->GetLoadingState() == ResourceLoadingSucceeded );
191   application.GetPlatform().DiscardRequest(); // Ensure load request is discarded
192   req=NULL;
193   application.GetPlatform().ResetTrace();
194
195   return ticket;
196 }
197
198 Internal::ImagePtr LoadImage(TestApplication& application, const char* name)
199 {
200   Internal::ImagePtr image = Internal::Image::New(name);
201   application.SendNotification(); // Flush update messages
202   application.Render();           // Process resource request
203   Integration::ResourceRequest* req = application.GetPlatform().GetRequest();
204   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, false );
205   bitmap->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888, 80,80,80,80 );
206   Integration::ResourcePointer resourcePtr(bitmap); // reference it
207
208   application.GetPlatform().SetResourceLoaded(req->GetId(), req->GetType()->id, resourcePtr);
209   application.Render();           // Process LoadComplete
210   application.SendNotification(); // Process event messages
211   application.GetPlatform().DiscardRequest(); // Ensure load request is discarded
212   req=NULL;
213   application.GetPlatform().ResetTrace();
214   return image;
215 }
216
217 } // Anonymous Namespace
218
219 /********************************************************************************/
220 /********************************************************************************/
221 /********************************************************************************/
222
223 // Test new with no parameters sets up default object
224 int UtcDaliMaterialMethodNew01(void)
225 {
226   TestApplication application;
227
228   Internal::SceneGraph::Material* sceneObject = Internal::SceneGraph::Material::New();
229   Internal::MaterialProperties props = sceneObject->GetProperties(); // copy.
230   DALI_TEST_CHECK(props.mOpacity == Dali::Material::DEFAULT_OPACITY);
231   DALI_TEST_CHECK(props.mShininess == Dali::Material::DEFAULT_SHININESS);
232   DALI_TEST_CHECK(props.mAmbientColor == Dali::Material::DEFAULT_AMBIENT_COLOR);
233   DALI_TEST_CHECK(props.mDiffuseColor == Dali::Material::DEFAULT_DIFFUSE_COLOR);
234   DALI_TEST_CHECK(props.mSpecularColor == Dali::Material::DEFAULT_SPECULAR_COLOR);
235   DALI_TEST_CHECK(props.mEmissiveColor == Dali::Material::DEFAULT_EMISSIVE_COLOR);
236
237   Internal::ResourceId textureId = sceneObject->GetDiffuseTextureId();
238   DALI_TEST_CHECK( !textureId );
239   textureId = sceneObject->GetOpacityTextureId();
240   DALI_TEST_CHECK( !textureId );
241   textureId = sceneObject->GetNormalMapId();
242   DALI_TEST_CHECK( !textureId );
243
244   DALI_TEST_CHECK(! sceneObject->HasDiffuseTexture());
245   DALI_TEST_CHECK(! sceneObject->HasOpacityTexture());
246   DALI_TEST_CHECK(! sceneObject->HasNormalMap());
247   END_TEST;
248 }
249
250 // Test new with event object sets up parameters appropriately
251 int UtcDaliMaterialMethodNew02(void)
252 {
253   TestApplication application;
254
255   Internal::Material* material = Internal::Material::New("cloth");
256   DALI_TEST_CHECK(material->GetShininess() == Dali::Material::DEFAULT_SHININESS);
257   DALI_TEST_CHECK(material->GetAmbientColor() == Dali::Material::DEFAULT_AMBIENT_COLOR);
258   material->SetOpacity(0.4f);
259   material->SetDiffuseColor(Color::MAGENTA);
260
261   // Create directly
262   Internal::SceneGraph::Material* sceneObject = Internal::SceneGraph::Material::New(material);
263   Internal::MaterialProperties props = sceneObject->GetProperties(); // copy.
264   DALI_TEST_CHECK(props.mOpacity == 0.4f);
265   DALI_TEST_CHECK(props.mDiffuseColor == Color::MAGENTA);
266   DALI_TEST_CHECK(props.mShininess == Dali::Material::DEFAULT_SHININESS);
267   DALI_TEST_CHECK(props.mAmbientColor == Dali::Material::DEFAULT_AMBIENT_COLOR);
268   DALI_TEST_CHECK(props.mSpecularColor == Dali::Material::DEFAULT_SPECULAR_COLOR);
269   DALI_TEST_CHECK(props.mEmissiveColor == Dali::Material::DEFAULT_EMISSIVE_COLOR);
270
271   Internal::ResourceId textureId = sceneObject->GetDiffuseTextureId();
272   DALI_TEST_CHECK( !textureId );
273   textureId = sceneObject->GetOpacityTextureId();
274   DALI_TEST_CHECK( !textureId );
275   textureId = sceneObject->GetNormalMapId();
276   DALI_TEST_CHECK( !textureId );
277
278   DALI_TEST_CHECK(! sceneObject->HasDiffuseTexture());
279   DALI_TEST_CHECK(! sceneObject->HasOpacityTexture());
280   DALI_TEST_CHECK(! sceneObject->HasNormalMap());
281
282   // Create indirectly
283   const Internal::SceneGraph::Material* sceneObject2 = material->GetSceneObject();
284   DALI_TEST_CHECK( sceneObject2 != NULL );
285   Internal::MaterialProperties props2 = sceneObject2->GetProperties(); // copy.
286   DALI_TEST_CHECK(props2.mOpacity == 0.4f);
287   DALI_TEST_CHECK(props2.mDiffuseColor == Color::MAGENTA);
288   DALI_TEST_CHECK(props2.mShininess == Dali::Material::DEFAULT_SHININESS);
289   DALI_TEST_CHECK(props2.mAmbientColor == Dali::Material::DEFAULT_AMBIENT_COLOR);
290   DALI_TEST_CHECK(props2.mSpecularColor == Dali::Material::DEFAULT_SPECULAR_COLOR);
291   DALI_TEST_CHECK(props2.mEmissiveColor == Dali::Material::DEFAULT_EMISSIVE_COLOR);
292
293   DALI_TEST_CHECK(! sceneObject2->GetDiffuseTextureId());
294   DALI_TEST_CHECK(! sceneObject2->GetOpacityTextureId());
295   DALI_TEST_CHECK(! sceneObject2->GetNormalMapId());
296
297   DALI_TEST_CHECK(! sceneObject2->HasDiffuseTexture());
298   DALI_TEST_CHECK(! sceneObject2->HasOpacityTexture());
299   DALI_TEST_CHECK(! sceneObject2->HasNormalMap());
300   END_TEST;
301 }
302
303 // Test setting ready texture off stage
304 int UtcDaliMaterialReadyTextureOffstage(void)
305 {
306   TestApplication application;
307
308   Internal::ResourceTicketPtr ticket = CheckLoadBitmap(application, "diffuse.png", 80, 80);
309   Internal::SceneGraph::Material* sceneObject = Internal::SceneGraph::Material::New();
310   sceneObject->SetDiffuseTextureId(ticket->GetId());
311
312   DALI_TEST_EQUALS( sceneObject->GetDiffuseTextureId(), ticket->GetId(), TEST_LOCATION );
313   END_TEST;
314 }
315
316 // Test setting unready texture offstage, becoming ready
317 int UtcDaliMaterialUnreadyTextureOffstage(void)
318 {
319   TestApplication application;
320
321   Internal::ResourceClient& resourceClient = Internal::ThreadLocalStorage::Get().GetResourceClient();
322   ImageAttributes attr;
323   Integration::BitmapResourceType bitmapRequest(attr);
324   Internal::ResourceTicketPtr ticket = resourceClient.RequestResource( bitmapRequest, "image.png" );
325   application.SendNotification(); // Flush update messages
326   application.Render();           // Process resource request
327
328   // Ticket is valid, but no resource yet
329   Internal::SceneGraph::Material* sceneObject = Internal::SceneGraph::Material::New();
330   Internal::ResourceId textureId = ticket->GetId();
331   sceneObject->SetDiffuseTextureId(textureId);
332   Internal::ResourceId textureId2 = sceneObject->GetDiffuseTextureId();
333   DALI_TEST_CHECK( textureId == textureId2 );
334
335   Integration::ResourceRequest*   req = application.GetPlatform().GetRequest();
336   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, false );
337   bitmap->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888, 80, 80, 80, 80 );
338   Integration::ResourcePointer resourcePtr(bitmap); // reference it
339   application.GetPlatform().SetResourceLoaded(req->GetId(), req->GetType()->id, resourcePtr);
340   application.Render();           // Process LoadComplete
341   application.SendNotification(); // Process event messages
342   DALI_TEST_CHECK( ticket->GetLoadingState() == ResourceLoadingSucceeded );
343
344   Internal::ResourceId textureId3 = sceneObject->GetDiffuseTextureId();
345   DALI_TEST_CHECK( textureId3 );
346   DALI_TEST_CHECK( textureId3 == textureId );
347   END_TEST;
348 }
349
350 // Test staging creates render material
351 int UtcDaliMaterialStaging01(void)
352 {
353   TestApplication application;
354   TestBoundTextures boundTextures(application);
355   TestProgram testProgram(application);
356
357   // Create object and set some properties
358   Internal::SceneGraph::Material* sceneObject = Internal::SceneGraph::Material::New();
359   DALI_TEST_CHECK( sceneObject != NULL );
360   sceneObject->SetProperties(TEST_PROPS);
361
362   // Stage the object
363   Internal::SceneGraph::UpdateManager& updateManager = Internal::ThreadLocalStorage::Get().GetUpdateManager();
364   AddMaterialMessage( updateManager, sceneObject );
365   application.SendNotification(); // Flush update Q
366
367   application.GetPlatform().IncrementGetTimeResult( 1 );
368   Integration::UpdateStatus status;
369   application.GetCore().Update( status );
370
371   // Check that a render object has been created
372   Internal::SceneGraph::RenderMaterial* renderMaterial = sceneObject->GetRenderMaterial();
373   DALI_TEST_CHECK(renderMaterial != NULL);
374   // Not on render manager, and should have default props
375
376   Internal::SceneGraph::RenderMaterialUniforms materialUniforms;
377   renderMaterial->SetUniforms( materialUniforms, testProgram.GetProgram(), Internal::SHADER_DEFAULT );
378
379   DALI_TEST_EQUALS( testProgram.GetOpacity(),       1.0f, TEST_LOCATION );
380   DALI_TEST_EQUALS( testProgram.GetShininess(),     0.5f, TEST_LOCATION );
381   DALI_TEST_EQUALS( testProgram.GetAmbientColor(),  Vector4(0.2f, 0.2f, 0.2f, 1.0f), TEST_LOCATION);
382   DALI_TEST_EQUALS( testProgram.GetDiffuseColor(),  Vector4(0.8f, 0.8f, 0.8f, 1.0f), TEST_LOCATION);
383   DALI_TEST_EQUALS( testProgram.GetSpecularColor(), Vector4(0.0f, 0.0f, 0.0f, 1.0f), TEST_LOCATION);
384   DALI_TEST_EQUALS( testProgram.GetEmissiveColor(), Vector4(0.0f, 0.0f, 0.0f, 1.0f), TEST_LOCATION);
385
386   application.Render(); //Process render Q stores & processes mat
387
388   renderMaterial->SetUniforms( materialUniforms, testProgram.GetProgram(), Internal::SHADER_DEFAULT );
389   renderMaterial->BindTextures( testProgram.GetProgram() );
390   DALI_TEST_CHECK( boundTextures.GetNumBoundTextures() == 0 );
391
392   DALI_TEST_EQUALS( testProgram.GetOpacity(),       TEST_PROPS.mOpacity,       TEST_LOCATION);
393   DALI_TEST_EQUALS( testProgram.GetShininess(),     TEST_PROPS.mShininess,     TEST_LOCATION );
394   DALI_TEST_EQUALS( testProgram.GetAmbientColor(),  TEST_PROPS.mAmbientColor,  TEST_LOCATION );
395   DALI_TEST_EQUALS( testProgram.GetDiffuseColor(),  TEST_PROPS.mDiffuseColor,  TEST_LOCATION );
396   DALI_TEST_EQUALS( testProgram.GetSpecularColor(), TEST_PROPS.mSpecularColor, TEST_LOCATION );
397   DALI_TEST_EQUALS( testProgram.GetEmissiveColor(), TEST_PROPS.mEmissiveColor, TEST_LOCATION );
398   END_TEST;
399 }
400
401 // Test staging creates render material
402 int UtcDaliMaterialStaging02(void)
403 {
404   TestApplication application;
405   TestBoundTextures boundTextures(application);
406   TestProgram testProgram(application);
407
408   Internal::Material* material = Internal::Material::New("cloth");
409   material->SetOpacity(0.4f);
410   material->SetDiffuseColor(Color::MAGENTA);
411
412   // Create object and set some properties
413   Internal::SceneGraph::Material* sceneObject = Internal::SceneGraph::Material::New(material);
414   DALI_TEST_CHECK( sceneObject != NULL );
415
416   // Stage the object
417   Internal::SceneGraph::UpdateManager& updateManager = Internal::ThreadLocalStorage::Get().GetUpdateManager();
418   AddMaterialMessage( updateManager, sceneObject );
419   application.SendNotification(); // Flush update Q
420
421   application.GetPlatform().IncrementGetTimeResult( 1 );
422   Integration::UpdateStatus status;
423   application.GetCore().Update( status );
424
425   // Check that a render object has been created
426   Internal::SceneGraph::RenderMaterial* renderMaterial = sceneObject->GetRenderMaterial();
427   DALI_TEST_CHECK(renderMaterial != NULL);
428   // Not on render manager, and should have default props
429
430   Internal::SceneGraph::RenderMaterialUniforms materialUniforms;
431   renderMaterial->SetUniforms( materialUniforms, testProgram.GetProgram(), Internal::SHADER_DEFAULT );
432
433   DALI_TEST_EQUALS( testProgram.GetOpacity(),       1.0f, TEST_LOCATION );
434   DALI_TEST_EQUALS( testProgram.GetShininess(),     0.5f, TEST_LOCATION );
435   DALI_TEST_EQUALS( testProgram.GetAmbientColor(),  Vector4(0.2f, 0.2f, 0.2f, 1.0f), TEST_LOCATION);
436   DALI_TEST_EQUALS( testProgram.GetDiffuseColor(),  Vector4(0.8f, 0.8f, 0.8f, 1.0f), TEST_LOCATION);
437   DALI_TEST_EQUALS( testProgram.GetSpecularColor(), Vector4(0.0f, 0.0f, 0.0f, 1.0f), TEST_LOCATION);
438   DALI_TEST_EQUALS( testProgram.GetEmissiveColor(), Vector4(0.0f, 0.0f, 0.0f, 1.0f), TEST_LOCATION);
439
440   application.Render(); //Process render Q stores & processes mat
441
442   renderMaterial->SetUniforms( materialUniforms, testProgram.GetProgram(), Internal::SHADER_DEFAULT );
443   renderMaterial->BindTextures( testProgram.GetProgram() );
444
445   DALI_TEST_CHECK( boundTextures.GetNumBoundTextures() == 0 );
446   DALI_TEST_EQUALS( testProgram.GetOpacity(),       0.4f, TEST_LOCATION);
447   DALI_TEST_EQUALS( testProgram.GetShininess(),     Dali::Material::DEFAULT_SHININESS, TEST_LOCATION );
448   DALI_TEST_EQUALS( testProgram.GetAmbientColor(),  Dali::Material::DEFAULT_AMBIENT_COLOR,  TEST_LOCATION );
449   DALI_TEST_EQUALS( testProgram.GetDiffuseColor(),  Color::MAGENTA, TEST_LOCATION );
450   DALI_TEST_EQUALS( testProgram.GetSpecularColor(), Dali::Material::DEFAULT_SPECULAR_COLOR, TEST_LOCATION );
451   DALI_TEST_EQUALS( testProgram.GetEmissiveColor(), Dali::Material::DEFAULT_EMISSIVE_COLOR, TEST_LOCATION );
452   END_TEST;
453 }
454
455
456
457 // Test setting properties on stage
458 int UtcDaliMaterialSetPropsWhilstStaged(void)
459 {
460   TestApplication application;
461   TestBoundTextures boundTextures(application);
462   TestProgram testProgram(application);
463
464   // Create object with default properties
465   Internal::SceneGraph::Material* sceneObject = Internal::SceneGraph::Material::New();
466   DALI_TEST_CHECK( sceneObject != NULL );
467
468   // Stage the object
469   Internal::SceneGraph::UpdateManager& updateManager = Internal::ThreadLocalStorage::Get().GetUpdateManager();
470   AddMaterialMessage( updateManager, sceneObject );
471   application.SendNotification(); // Flush update Q
472   application.Render(); // Process update message Q then create & post to render Q
473
474   // Check that a render object has been created
475   Internal::SceneGraph::RenderMaterial* renderMaterial = sceneObject->GetRenderMaterial();
476   DALI_TEST_CHECK(renderMaterial != NULL);
477   application.Render(); // Update & Prepare renderMat, Process render Q stores & processes mat
478
479   sceneObject->SetProperties(TEST_PROPS);
480   application.SendNotification(); // Flush update Q
481   application.Render(); // Update & Prepare material
482   application.Render(); // Process render Q
483
484   Internal::SceneGraph::RenderMaterialUniforms materialUniforms;
485   renderMaterial->SetUniforms( materialUniforms, testProgram.GetProgram(), Internal::SHADER_DEFAULT );
486   renderMaterial->BindTextures( testProgram.GetProgram() );
487
488   DALI_TEST_EQUALS( boundTextures.GetNumBoundTextures(), 0u, TEST_LOCATION );
489
490   DALI_TEST_EQUALS( testProgram.GetOpacity(),       TEST_PROPS.mOpacity,       TEST_LOCATION);
491   DALI_TEST_EQUALS( testProgram.GetShininess(),     TEST_PROPS.mShininess,     TEST_LOCATION );
492   DALI_TEST_EQUALS( testProgram.GetAmbientColor(),  TEST_PROPS.mAmbientColor,  TEST_LOCATION );
493   DALI_TEST_EQUALS( testProgram.GetDiffuseColor(),  TEST_PROPS.mDiffuseColor,  TEST_LOCATION );
494   DALI_TEST_EQUALS( testProgram.GetSpecularColor(), TEST_PROPS.mSpecularColor, TEST_LOCATION );
495   DALI_TEST_EQUALS( testProgram.GetEmissiveColor(), TEST_PROPS.mEmissiveColor, TEST_LOCATION );
496   END_TEST;
497 }
498
499 // Test setting ready texture on stage
500 int UtcDaliMaterialSetTextureWhilstStaged(void)
501 {
502   TestApplication application;
503   TestBoundTextures boundTextures(application);
504   TestProgram testProgram(application);
505
506   Internal::ResourceTicketPtr ticket = CheckLoadBitmap(application, "diffuse.png", 80, 80);
507
508   // Create object with default properties
509   Internal::SceneGraph::Material* sceneObject = Internal::SceneGraph::Material::New();
510   DALI_TEST_CHECK( sceneObject != NULL );
511
512   // Stage the object
513   Internal::SceneGraph::UpdateManager& updateManager = Internal::ThreadLocalStorage::Get().GetUpdateManager();
514   AddMaterialMessage( updateManager, sceneObject );
515   application.SendNotification(); // Flush update Q
516   application.Render(); // Process update message Q then create & post to render Q
517
518   // Check that a render object has been created
519   Internal::SceneGraph::RenderMaterial* renderMaterial = sceneObject->GetRenderMaterial();
520   DALI_TEST_CHECK(renderMaterial != NULL);
521   application.Render(); // Update & Prepare renderMat, Process render Q stores & processes mat
522
523   sceneObject->SetDiffuseTextureId(ticket->GetId());
524   application.SendNotification(); // Flush update Q
525   application.Render(); // Update & Prepare material
526   application.Render(); // Process render Q
527
528   renderMaterial->BindTextures( testProgram.GetProgram() );
529   DALI_TEST_CHECK( boundTextures.CheckFirstTextureBound( GL_TEXTURE0 ) );
530   END_TEST;
531 }
532
533 // Test setting unready texture on stage, becoming ready
534 int UtcDaliMaterialSetUnreadyTextureWhilstStaged(void)
535 {
536   TestApplication application;
537   TestBoundTextures boundTextures(application);
538   TestProgram testProgram(application);
539
540   Internal::ResourceClient& resourceClient = Internal::ThreadLocalStorage::Get().GetResourceClient();
541   ImageAttributes attr;
542   Integration::BitmapResourceType bitmapRequest(attr);
543   Internal::ResourceTicketPtr ticket = resourceClient.RequestResource( bitmapRequest, "image.png" );
544   application.SendNotification(); // Flush update messages
545   application.Render();           // Process resource request
546
547
548   // Create object with default properties
549   Internal::SceneGraph::Material* sceneObject = Internal::SceneGraph::Material::New();
550   DALI_TEST_CHECK( sceneObject != NULL );
551
552   // Stage the object
553   Internal::SceneGraph::UpdateManager& updateManager = Internal::ThreadLocalStorage::Get().GetUpdateManager();
554   AddMaterialMessage( updateManager, sceneObject );
555   application.SendNotification(); // Flush update Q
556   application.Render(); // Process update message Q then create & post to render Q
557
558   // Check that a render object has been created
559   Internal::SceneGraph::RenderMaterial* renderMaterial = sceneObject->GetRenderMaterial();
560   DALI_TEST_CHECK(renderMaterial != NULL);
561   application.Render(); // Update & Prepare renderMat, Process render Q stores & processes mat
562
563   sceneObject->SetDiffuseTextureId(ticket->GetId());
564   application.SendNotification(); // Flush update Q
565   application.Render(); // Update & Prepare material
566   application.Render(); // Process render Q
567
568   renderMaterial->BindTextures( testProgram.GetProgram() );
569
570   DALI_TEST_EQUALS( boundTextures.GetNumBoundTextures(), 0u, TEST_LOCATION );
571
572   Integration::ResourceRequest*   req = application.GetPlatform().GetRequest();
573   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, false );
574   bitmap->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888, 80, 80, 80, 80 );
575   Integration::ResourcePointer resourcePtr(bitmap); // reference it
576   application.GetPlatform().SetResourceLoaded(req->GetId(), req->GetType()->id, resourcePtr);
577   application.Render();           // Process LoadComplete
578   application.SendNotification(); // Process event messages
579
580   renderMaterial->BindTextures( testProgram.GetProgram() );
581   DALI_TEST_CHECK( boundTextures.CheckFirstTextureBound( GL_TEXTURE0 ) );
582   END_TEST;
583 }
584
585 // Test IsOpaque without texture, with unready texture, with ready texture
586
587 int UtcDaliMaterialIsOpaqueWithoutTexture(void)
588 {
589   TestApplication application;
590
591   // Create object with default properties
592   Internal::SceneGraph::Material* sceneObject = Internal::SceneGraph::Material::New();
593   DALI_TEST_CHECK( sceneObject != NULL );
594
595   // Stage the object
596   Internal::SceneGraph::UpdateManager& updateManager = Internal::ThreadLocalStorage::Get().GetUpdateManager();
597   AddMaterialMessage( updateManager, sceneObject );
598   application.SendNotification(); // Flush update Q
599   application.Render(); // Process update message Q then create & post to render Q
600
601   // Check that a render object has been created
602   Internal::SceneGraph::RenderMaterial* renderMaterial = sceneObject->GetRenderMaterial();
603   DALI_TEST_CHECK(renderMaterial != NULL);
604   application.Render(); // Update & Prepare renderMat, Process render Q stores & processes mat
605
606   DALI_TEST_CHECK( sceneObject->IsOpaque() );
607   END_TEST;
608 }
609
610 int UtcDaliMaterialIsOpaqueWithTexture(void)
611 {
612   TestApplication application;
613
614   Internal::ResourceTicketPtr ticket = CheckLoadBitmap(application, "diffuse.png", 80, 80);
615
616   // Create object with default properties
617   Internal::SceneGraph::Material* sceneObject = Internal::SceneGraph::Material::New();
618   DALI_TEST_CHECK( sceneObject != NULL );
619
620   // Stage the object
621   Internal::SceneGraph::UpdateManager& updateManager = Internal::ThreadLocalStorage::Get().GetUpdateManager();
622   AddMaterialMessage( updateManager, sceneObject );
623   application.SendNotification(); // Flush update Q
624   application.Render(); // Process update message Q then create & post to render Q
625
626   // Check that a render object has been created
627   Internal::SceneGraph::RenderMaterial* renderMaterial = sceneObject->GetRenderMaterial();
628   DALI_TEST_CHECK(renderMaterial != NULL);
629   application.Render(); // Update & Prepare renderMat, Process render Q stores & processes mat
630
631   DALI_TEST_CHECK( sceneObject->IsOpaque() );
632
633   sceneObject->SetDiffuseTextureId(ticket->GetId());
634   application.SendNotification(); // Flush update Q
635   application.Render(); // Update & Prepare material
636   application.Render(); // Process render Q
637
638   DALI_TEST_CHECK( ! sceneObject->IsOpaque() );
639   END_TEST;
640 }
641
642
643 int UtcDaliMaterialIsOpaqueWithProps(void)
644 {
645   TestApplication application;
646
647   // Create object with default properties
648   Internal::SceneGraph::Material* sceneObject = Internal::SceneGraph::Material::New();
649   DALI_TEST_CHECK( sceneObject != NULL );
650
651   // Stage the object
652   Internal::SceneGraph::UpdateManager& updateManager = Internal::ThreadLocalStorage::Get().GetUpdateManager();
653   AddMaterialMessage( updateManager, sceneObject );
654   application.SendNotification(); // Flush update Q
655   application.Render(); // Process update message Q then create & post to render Q
656
657   // Check that a render object has been created
658   Internal::SceneGraph::RenderMaterial* renderMaterial = sceneObject->GetRenderMaterial();
659   DALI_TEST_CHECK(renderMaterial != NULL);
660   application.Render(); // Update & Prepare renderMat, Process render Q stores & processes mat
661
662   DALI_TEST_CHECK( sceneObject->IsOpaque() );
663
664   sceneObject->SetProperties(TEST_PROPS);
665   application.SendNotification(); // Flush update Q
666   application.Render(); // Update & Prepare material
667   application.Render(); // Process render Q
668
669   DALI_TEST_CHECK( ! sceneObject->IsOpaque() );
670   END_TEST;
671 }
672
673 int UtcDaliMaterialRender(void)
674 {
675   TestApplication application;
676   TestBoundTextures boundTextures(application);
677
678   {
679
680     MeshData meshData;
681     MeshData::VertexContainer    vertices;
682     MeshData::FaceIndices        faces;
683     BoneContainer                bones;
684     ConstructVertices(vertices, 60);
685     ConstructFaces(vertices, faces);
686     Dali::Material               material  = ConstructMaterial();
687
688     Internal::ImagePtr image = LoadImage(application, "texture.png");
689     Image imageHandle(image.Get());
690     material.SetDiffuseTexture(imageHandle);
691     meshData.SetData(vertices, faces, bones, material);
692     Mesh mesh = Mesh::New(meshData);
693
694     MeshActor actor = MeshActor::New(mesh);
695     std::string name = "AMeshActor";
696     actor.SetName(name);
697     actor.SetAffectedByLighting(false);
698     Stage::GetCurrent().Add(actor);
699
700     material.SetOpacity(TEST_PROPS.mOpacity);
701     material.SetShininess(TEST_PROPS.mShininess);
702     material.SetAmbientColor(TEST_PROPS.mAmbientColor);
703     material.SetDiffuseColor(TEST_PROPS.mDiffuseColor);
704     material.SetSpecularColor(TEST_PROPS.mSpecularColor);
705     material.SetEmissiveColor(TEST_PROPS.mEmissiveColor);
706
707     application.SendNotification();
708     application.Render();
709     application.SendNotification();
710     application.Render();
711     application.SendNotification();
712     application.Render();
713     application.SendNotification();
714
715     DALI_TEST_CHECK(application.GetGlAbstraction().CheckUniformValue("uMaterial.mOpacity",   TEST_PROPS.mOpacity ) );
716     DALI_TEST_CHECK(application.GetGlAbstraction().CheckUniformValue("uMaterial.mShininess", TEST_PROPS.mShininess ) );
717     DALI_TEST_CHECK(application.GetGlAbstraction().CheckUniformValue("uMaterial.mAmbient",   TEST_PROPS.mAmbientColor ) );
718     DALI_TEST_CHECK(application.GetGlAbstraction().CheckUniformValue("uMaterial.mDiffuse",   TEST_PROPS.mDiffuseColor ) );
719     DALI_TEST_CHECK(application.GetGlAbstraction().CheckUniformValue("uMaterial.mSpecular",  TEST_PROPS.mSpecularColor ) );
720     DALI_TEST_CHECK(application.GetGlAbstraction().CheckUniformValue("uMaterial.mEmissive",  TEST_PROPS.mEmissiveColor ) );
721
722     DALI_TEST_CHECK(boundTextures.CheckFirstTextureBound( GL_TEXTURE0 ));
723
724     Stage::GetCurrent().Remove(actor);
725     application.SendNotification();
726     application.Render();
727     application.SendNotification();
728     application.Render();
729   }
730   application.SendNotification();
731   application.Render();
732
733   // texture should have been removed:
734   DALI_TEST_CHECK( boundTextures.CheckFirstTextureDeleted() );
735   END_TEST;
736 }