2 * Copyright (c) 2024 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/devel-api/threading/thread.h>
20 #include <dali/public-api/dali-core.h>
23 #include <dali-test-suite-utils.h>
24 #include <mesh-builder.h>
36 Actor CreateActor(SetSampler setSamplerOption)
38 Texture texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
40 Shader shader = CreateShader();
41 TextureSet textureSet = CreateTextureSet();
43 Sampler sampler = Sampler::New();
44 sampler.SetFilterMode(FilterMode::NEAREST, FilterMode::NEAREST);
45 textureSet.SetTexture(0u, texture);
46 if(setSamplerOption == SET_SAMPLER)
48 textureSet.SetSampler(0u, sampler);
51 Geometry geometry = CreateQuadGeometry();
52 Renderer renderer = Renderer::New(geometry, shader);
53 renderer.SetTextures(textureSet);
55 Actor actor = Actor::New();
56 actor.AddRenderer(renderer);
57 actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
58 actor.SetProperty(Actor::Property::SIZE, Vector2(400.0f, 400.0f));
65 void texture_set_test_startup(void)
67 test_return_value = TET_UNDEF;
70 void texture_set_test_cleanup(void)
72 test_return_value = TET_PASS;
75 int UtcDaliTextureSetNew01(void)
77 TestApplication application;
79 TextureSet textureSet = TextureSet::New();
81 DALI_TEST_CHECK(textureSet);
85 int UtcDaliTextureSetNew02(void)
87 TestApplication application;
88 TextureSet textureSet;
89 DALI_TEST_CHECK(!textureSet);
93 int UtcDaliTextureSetCopyConstructor(void)
95 TestApplication application;
97 Texture image = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, 32, 32);
98 TextureSet textureSet = TextureSet::New();
99 textureSet.SetTexture(0u, image);
101 TextureSet textureSetCopy(textureSet);
103 DALI_TEST_CHECK(textureSetCopy);
108 int UtcDaliTextureSetAssignmentOperator(void)
110 TestApplication application;
111 TextureSet textureSet = TextureSet::New();
113 TextureSet textureSet2;
114 DALI_TEST_CHECK(!textureSet2);
116 textureSet2 = textureSet;
117 DALI_TEST_CHECK(textureSet2);
122 int UtcDaliTextureSetMoveConstructor(void)
124 TestApplication application;
126 TextureSet textureSet = TextureSet::New();
127 DALI_TEST_CHECK(textureSet);
128 DALI_TEST_EQUALS(1, textureSet.GetBaseObject().ReferenceCount(), TEST_LOCATION);
130 Texture texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, 32, 32);
131 textureSet.SetTexture(0u, texture);
132 DALI_TEST_EQUALS(textureSet.GetTexture(0u), texture, TEST_LOCATION);
134 TextureSet move = std::move(textureSet);
135 DALI_TEST_CHECK(move);
136 DALI_TEST_EQUALS(1, move.GetBaseObject().ReferenceCount(), TEST_LOCATION);
137 DALI_TEST_EQUALS(move.GetTexture(0u), texture, TEST_LOCATION);
138 DALI_TEST_CHECK(!textureSet);
143 int UtcDaliTextureSetMoveAssignment(void)
145 TestApplication application;
147 TextureSet textureSet = TextureSet::New();
148 DALI_TEST_CHECK(textureSet);
149 DALI_TEST_EQUALS(1, textureSet.GetBaseObject().ReferenceCount(), TEST_LOCATION);
151 Texture texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, 32, 32);
152 textureSet.SetTexture(0u, texture);
153 DALI_TEST_EQUALS(textureSet.GetTexture(0u), texture, TEST_LOCATION);
156 move = std::move(textureSet);
157 DALI_TEST_CHECK(move);
158 DALI_TEST_EQUALS(1, move.GetBaseObject().ReferenceCount(), TEST_LOCATION);
159 DALI_TEST_EQUALS(move.GetTexture(0u), texture, TEST_LOCATION);
160 DALI_TEST_CHECK(!textureSet);
165 int UtcDaliTextureSetDownCast01(void)
167 TestApplication application;
168 TextureSet textureSet = TextureSet::New();
170 BaseHandle handle(textureSet);
171 TextureSet textureSet2 = TextureSet::DownCast(handle);
172 DALI_TEST_CHECK(textureSet2);
177 int UtcDaliTextureSetDownCast02(void)
179 TestApplication application;
181 Handle handle = Handle::New(); // Create a custom object
182 TextureSet textureSet = TextureSet::DownCast(handle);
183 DALI_TEST_CHECK(!textureSet);
187 int UtcDaliTextureSetTexture01(void)
189 TestApplication application;
191 Actor actor = CreateActor(DONT_SET_SAMPLER);
193 application.GetScene().Add(actor);
195 TestGlAbstraction& gl = application.GetGlAbstraction();
197 TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
198 texParameterTrace.Reset();
199 texParameterTrace.Enable(true);
200 application.SendNotification();
201 application.Render();
203 int textureUnit = -1;
204 DALI_TEST_CHECK(gl.GetUniformValue<int>("sTexture", textureUnit));
205 DALI_TEST_EQUALS(textureUnit, 0, TEST_LOCATION);
207 texParameterTrace.Enable(false);
210 // There are four calls to TexParameteri when the texture is first created
211 // as the texture is using default sampling parametrers there shouldn't be any more calls to TexParameteri
212 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 4, TEST_LOCATION);
217 int UtcDaliTextureSetTexture02(void)
219 TestApplication application;
221 Actor actor = CreateActor(SET_SAMPLER);
223 application.GetScene().Add(actor);
225 TestGlAbstraction& gl = application.GetGlAbstraction();
227 TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
228 texParameterTrace.Reset();
229 texParameterTrace.Enable(true);
230 application.SendNotification();
231 application.Render();
233 int textureUnit = -1;
234 DALI_TEST_CHECK(gl.GetUniformValue<int>("sTexture", textureUnit));
235 DALI_TEST_EQUALS(textureUnit, 0, TEST_LOCATION);
237 texParameterTrace.Enable(false);
240 // There are four calls to TexParameteri when the texture is first created
241 // Texture minification and magnification filters are now different than default so
242 //there should have been two extra TexParameteri calls to set the new filter mode
243 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 6, TEST_LOCATION);
248 int UtcDaliTextureSetMultiple(void)
250 TestApplication application;
252 Actor actor1 = CreateActor(SET_SAMPLER);
253 Actor actor2 = CreateActor(SET_SAMPLER);
255 application.GetScene().Add(actor1);
256 application.GetScene().Add(actor2);
258 TestGlAbstraction& gl = application.GetGlAbstraction();
260 TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
261 texParameterTrace.Reset();
262 texParameterTrace.Enable(true);
263 texParameterTrace.EnableLogging(true);
264 application.SendNotification();
265 application.Render();
267 int textureUnit = -1;
268 DALI_TEST_CHECK(gl.GetUniformValue<int>("sTexture", textureUnit));
269 DALI_TEST_EQUALS(textureUnit, 0, TEST_LOCATION);
271 texParameterTrace.Enable(false);
274 // For each actor there are four calls to TexParameteri when the texture is first created
275 // Texture minification and magnification filters are now different than default so
276 // there should have been two extra TexParameteri calls to set the new filter mode
277 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 2 * 6, TEST_LOCATION);
282 int UtcDaliTextureSetSetSampler(void)
284 TestApplication application;
286 Texture image = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
288 Shader shader = CreateShader();
289 TextureSet textureSet = CreateTextureSet(image);
291 Geometry geometry = CreateQuadGeometry();
292 Renderer renderer = Renderer::New(geometry, shader);
293 renderer.SetTextures(textureSet);
295 Actor actor = Actor::New();
296 actor.AddRenderer(renderer);
297 actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
298 actor.SetProperty(Actor::Property::SIZE, Vector2(400.0f, 400.0f));
300 application.GetScene().Add(actor);
302 TestGlAbstraction& gl = application.GetGlAbstraction();
304 TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
305 texParameterTrace.Reset();
306 texParameterTrace.EnableLogging(true);
307 texParameterTrace.Enable(true);
308 application.SendNotification();
309 application.Render();
311 int textureUnit = -1;
312 DALI_TEST_CHECK(gl.GetUniformValue<int>("sTexture", textureUnit));
313 DALI_TEST_EQUALS(textureUnit, 0, TEST_LOCATION);
315 texParameterTrace.Enable(false);
318 // There are 4 calls to TexParameteri when the texture is first created
319 // as the texture is using default sampling parametrers there shouldn't be any more calls to TexParameteri
320 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 4, TEST_LOCATION);
322 texParameterTrace.Reset();
323 texParameterTrace.Enable(true);
325 Sampler sampler = Sampler::New();
326 sampler.SetFilterMode(FilterMode::NEAREST, FilterMode::NEAREST);
327 textureSet.SetSampler(0u, sampler);
329 application.SendNotification();
330 application.Render();
332 texParameterTrace.Enable(false);
335 //There should have been two calls to TexParameteri to set the new filtering mode
336 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 2, TEST_LOCATION);
341 int UtcDaliTextureSetGetTexture(void)
343 TestApplication application;
345 TextureSet textureSet = CreateTextureSet();
346 DALI_TEST_EQUALS(textureSet.GetTexture(0), Texture(), TEST_LOCATION);
347 DALI_TEST_EQUALS(textureSet.GetTexture(1), Texture(), TEST_LOCATION);
348 DALI_TEST_EQUALS(textureSet.GetTexture(2), Texture(), TEST_LOCATION);
350 Texture texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
351 textureSet.SetTexture(0u, texture);
353 DALI_TEST_EQUALS(textureSet.GetTexture(0), texture, TEST_LOCATION);
354 DALI_TEST_EQUALS(textureSet.GetTexture(1), Texture(), TEST_LOCATION);
355 DALI_TEST_EQUALS(textureSet.GetTexture(2), Texture(), TEST_LOCATION);
357 textureSet.SetTexture(2u, texture);
358 DALI_TEST_EQUALS(textureSet.GetTexture(0), texture, TEST_LOCATION);
359 DALI_TEST_EQUALS(textureSet.GetTexture(1), Texture(), TEST_LOCATION);
360 DALI_TEST_EQUALS(textureSet.GetTexture(2), texture, TEST_LOCATION);
362 textureSet.SetTexture(2u, Texture());
363 DALI_TEST_EQUALS(textureSet.GetTexture(0), texture, TEST_LOCATION);
364 DALI_TEST_EQUALS(textureSet.GetTexture(1), Texture(), TEST_LOCATION);
365 DALI_TEST_EQUALS(textureSet.GetTexture(2), Texture(), TEST_LOCATION);
370 int UtcDaliTextureSetGetSampler(void)
372 TestApplication application;
374 TextureSet textureSet = CreateTextureSet();
375 DALI_TEST_EQUALS(textureSet.GetSampler(0), Sampler(), TEST_LOCATION);
376 DALI_TEST_EQUALS(textureSet.GetSampler(1), Sampler(), TEST_LOCATION);
377 DALI_TEST_EQUALS(textureSet.GetSampler(2), Sampler(), TEST_LOCATION);
379 Sampler sampler = Sampler::New();
380 sampler.SetFilterMode(FilterMode::NEAREST, FilterMode::NEAREST);
381 textureSet.SetSampler(0u, sampler);
383 DALI_TEST_EQUALS(textureSet.GetSampler(0), sampler, TEST_LOCATION);
384 DALI_TEST_EQUALS(textureSet.GetSampler(1), Sampler(), TEST_LOCATION);
385 DALI_TEST_EQUALS(textureSet.GetSampler(2), Sampler(), TEST_LOCATION);
387 textureSet.SetSampler(2u, sampler);
388 DALI_TEST_EQUALS(textureSet.GetSampler(0), sampler, TEST_LOCATION);
389 DALI_TEST_EQUALS(textureSet.GetSampler(1), Sampler(), TEST_LOCATION);
390 DALI_TEST_EQUALS(textureSet.GetSampler(2), sampler, TEST_LOCATION);
392 textureSet.SetSampler(2u, Sampler());
393 DALI_TEST_EQUALS(textureSet.GetSampler(0), sampler, TEST_LOCATION);
394 DALI_TEST_EQUALS(textureSet.GetSampler(1), Sampler(), TEST_LOCATION);
395 DALI_TEST_EQUALS(textureSet.GetSampler(2), Sampler(), TEST_LOCATION);
400 int UtcDaliTextureSetGetTextureCount0(void)
402 TestApplication application;
404 TextureSet textureSet = CreateTextureSet();
405 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 0u, TEST_LOCATION);
407 Texture image = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
408 textureSet.SetTexture(0u, image);
409 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 1u, TEST_LOCATION);
411 textureSet.SetTexture(1u, image);
412 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 2u, TEST_LOCATION);
414 Sampler sampler = Sampler::New();
415 sampler.SetFilterMode(FilterMode::NEAREST, FilterMode::NEAREST);
416 textureSet.SetSampler(2u, sampler);
417 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 2u, TEST_LOCATION);
419 textureSet.SetTexture(2u, image);
420 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 3u, TEST_LOCATION);
421 DALI_TEST_EQUALS(textureSet.GetSampler(2u), sampler, TEST_LOCATION);
426 int UtcDaliTextureSetGetTextureCount1(void)
428 TestApplication application;
430 TextureSet textureSet = CreateTextureSet();
431 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 0u, TEST_LOCATION);
433 Texture texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
434 textureSet.SetTexture(0u, texture);
435 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 1u, TEST_LOCATION);
437 textureSet.SetTexture(1u, texture);
438 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 2u, TEST_LOCATION);
440 Sampler sampler = Sampler::New();
441 sampler.SetFilterMode(FilterMode::NEAREST, FilterMode::NEAREST);
442 textureSet.SetSampler(2u, sampler);
443 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 2u, TEST_LOCATION);
445 textureSet.SetTexture(2u, texture);
446 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 3u, TEST_LOCATION);
447 DALI_TEST_EQUALS(textureSet.GetSampler(2u), sampler, TEST_LOCATION);
452 int UtcDaliTextureSetRemoveTextureAndGetTextureCount(void)
454 TestApplication application;
456 TextureSet textureSet = CreateTextureSet();
457 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 0u, TEST_LOCATION);
459 Texture image0 = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
460 Texture image1 = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
461 Texture image2 = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
463 textureSet.SetTexture(0u, image0);
464 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 1u, TEST_LOCATION);
465 DALI_TEST_EQUALS(textureSet.GetTexture(0u), image0, TEST_LOCATION);
467 textureSet.SetTexture(1u, image1);
468 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 2u, TEST_LOCATION);
469 DALI_TEST_EQUALS(textureSet.GetTexture(0u), image0, TEST_LOCATION);
470 DALI_TEST_EQUALS(textureSet.GetTexture(1u), image1, TEST_LOCATION);
472 // Set empty texture so we can remove it.
473 textureSet.SetTexture(1u, Texture());
475 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 1u, TEST_LOCATION);
476 DALI_TEST_EQUALS(textureSet.GetTexture(0u), image0, TEST_LOCATION);
478 application.SendNotification();
479 application.Render();
481 textureSet.SetTexture(2u, image2);
483 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 3u, TEST_LOCATION);
484 DALI_TEST_EQUALS(textureSet.GetTexture(0u), image0, TEST_LOCATION);
485 DALI_TEST_EQUALS(textureSet.GetTexture(1u), Texture(), TEST_LOCATION);
486 DALI_TEST_EQUALS(textureSet.GetTexture(2u), image2, TEST_LOCATION);
488 textureSet.SetTexture(1u, image1);
490 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 3u, TEST_LOCATION);
491 DALI_TEST_EQUALS(textureSet.GetTexture(0u), image0, TEST_LOCATION);
492 DALI_TEST_EQUALS(textureSet.GetTexture(1u), image1, TEST_LOCATION);
493 DALI_TEST_EQUALS(textureSet.GetTexture(2u), image2, TEST_LOCATION);
495 // Set empty texture middle of textureset.
496 textureSet.SetTexture(1u, Texture());
498 application.SendNotification();
499 application.Render();
501 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 3u, TEST_LOCATION);
502 DALI_TEST_EQUALS(textureSet.GetTexture(0u), image0, TEST_LOCATION);
503 DALI_TEST_EQUALS(textureSet.GetTexture(1u), Texture(), TEST_LOCATION);
504 DALI_TEST_EQUALS(textureSet.GetTexture(2u), image2, TEST_LOCATION);
506 // Set empty texture end of textureset.
507 textureSet.SetTexture(2u, Texture());
509 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 1u, TEST_LOCATION);
510 DALI_TEST_EQUALS(textureSet.GetTexture(0u), image0, TEST_LOCATION);
512 application.SendNotification();
513 application.Render();
517 int UtcDaliTextureSetSetSamplerNegative(void)
519 TestApplication application;
520 Dali::TextureSet instance;
523 unsigned long arg1(0u);
525 instance.SetSampler(arg1, arg2);
526 DALI_TEST_CHECK(false); // Should not get here
530 DALI_TEST_CHECK(true); // We expect an assert
535 int UtcDaliTextureSetSetTextureNegative(void)
537 TestApplication application;
538 Dali::TextureSet instance;
541 unsigned long arg1(0u);
543 instance.SetTexture(arg1, arg2);
544 DALI_TEST_CHECK(false); // Should not get here
548 DALI_TEST_CHECK(true); // We expect an assert
553 int UtcDaliTextureSetGetSamplerNegative(void)
555 TestApplication application;
556 Dali::TextureSet instance;
559 unsigned long arg1(0u);
560 instance.GetSampler(arg1);
561 DALI_TEST_CHECK(false); // Should not get here
565 DALI_TEST_CHECK(true); // We expect an assert
570 int UtcDaliTextureSetGetTextureNegative(void)
572 TestApplication application;
573 Dali::TextureSet instance;
576 unsigned long arg1(0u);
577 instance.GetTexture(arg1);
578 DALI_TEST_CHECK(false); // Should not get here
582 DALI_TEST_CHECK(true); // We expect an assert
587 int UtcDaliTextureSetGetTextureCountNegative(void)
589 TestApplication application;
590 Dali::TextureSet instance;
593 instance.GetTextureCount();
594 DALI_TEST_CHECK(false); // Should not get here
598 DALI_TEST_CHECK(true); // We expect an assert
603 int UtcDaliTextureSetMultipleTextures(void)
605 TestApplication application;
607 Shader shader = CreateShader();
608 TextureSet textureSet = CreateTextureSet();
611 Texture texture1 = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
612 textureSet.SetTexture(0u, texture1);
614 Texture texture2 = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
615 textureSet.SetTexture(1u, texture2);
617 Geometry geometry = CreateQuadGeometry();
618 Renderer renderer = Renderer::New(geometry, shader);
619 renderer.SetTextures(textureSet);
621 Actor actor = Actor::New();
622 actor.AddRenderer(renderer);
623 actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
624 actor.SetProperty(Actor::Property::SIZE, Vector2(400.0f, 400.0f));
626 application.GetScene().Add(actor);
628 application.SendNotification();
629 application.Render();
631 const std::vector<GLuint>& boundTextures0 = application.GetGlAbstraction().GetBoundTextures(GL_TEXTURE0);
632 DALI_TEST_CHECK(boundTextures0[boundTextures0.size() - 1] == 1u); // the latest one should be 0.
634 const std::vector<GLuint>& boundTextures1 = application.GetGlAbstraction().GetBoundTextures(GL_TEXTURE1);
635 size_t count = boundTextures1.size();
636 DALI_TEST_CHECK(boundTextures1[count - 1] == 2u); // the latest one should be 1.
638 // Create a new TextureSet
639 textureSet = CreateTextureSet();
642 textureSet.SetTexture(0u, texture1);
644 renderer.SetTextures(textureSet);
646 application.SendNotification();
647 application.Render();
649 TestGlAbstraction& gl = application.GetGlAbstraction();
650 DALI_TEST_CHECK(gl.GetActiveTextureUnit() == GL_TEXTURE0);
652 DALI_TEST_CHECK(boundTextures0[boundTextures0.size() - 1] == 1u);
653 DALI_TEST_CHECK(boundTextures1.size() == count); // The bound texture count of GL_TEXTURE1 should not be changed.
658 int UtcDaliTextureSetDestructWorkerThreadN(void)
660 TestApplication application;
661 tet_infoline("UtcDaliTextureSetDestructWorkerThreadN Test, for line coverage");
665 class TestThread : public Thread
670 tet_printf("Run TestThread\n");
671 // Destruct at worker thread.
675 Dali::TextureSet mTextureSet;
679 Dali::TextureSet textureSet = Dali::TextureSet::New();
680 thread.mTextureSet = std::move(textureSet);
692 DALI_TEST_CHECK(true);