2 * Copyright (c) 2023 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/public-api/dali-core.h>
22 #include <dali-test-suite-utils.h>
23 #include <mesh-builder.h>
35 Actor CreateActor(SetSampler setSamplerOption)
37 Texture texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
39 Shader shader = CreateShader();
40 TextureSet textureSet = CreateTextureSet();
42 Sampler sampler = Sampler::New();
43 sampler.SetFilterMode(FilterMode::NEAREST, FilterMode::NEAREST);
44 textureSet.SetTexture(0u, texture);
45 if(setSamplerOption == SET_SAMPLER)
47 textureSet.SetSampler(0u, sampler);
50 Geometry geometry = CreateQuadGeometry();
51 Renderer renderer = Renderer::New(geometry, shader);
52 renderer.SetTextures(textureSet);
54 Actor actor = Actor::New();
55 actor.AddRenderer(renderer);
56 actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
57 actor.SetProperty(Actor::Property::SIZE, Vector2(400.0f, 400.0f));
64 void texture_set_test_startup(void)
66 test_return_value = TET_UNDEF;
69 void texture_set_test_cleanup(void)
71 test_return_value = TET_PASS;
74 int UtcDaliTextureSetNew01(void)
76 TestApplication application;
78 TextureSet textureSet = TextureSet::New();
80 DALI_TEST_CHECK(textureSet);
84 int UtcDaliTextureSetNew02(void)
86 TestApplication application;
87 TextureSet textureSet;
88 DALI_TEST_CHECK(!textureSet);
92 int UtcDaliTextureSetCopyConstructor(void)
94 TestApplication application;
96 Texture image = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, 32, 32);
97 TextureSet textureSet = TextureSet::New();
98 textureSet.SetTexture(0u, image);
100 TextureSet textureSetCopy(textureSet);
102 DALI_TEST_CHECK(textureSetCopy);
107 int UtcDaliTextureSetAssignmentOperator(void)
109 TestApplication application;
110 TextureSet textureSet = TextureSet::New();
112 TextureSet textureSet2;
113 DALI_TEST_CHECK(!textureSet2);
115 textureSet2 = textureSet;
116 DALI_TEST_CHECK(textureSet2);
121 int UtcDaliTextureSetMoveConstructor(void)
123 TestApplication application;
125 TextureSet textureSet = TextureSet::New();
126 DALI_TEST_CHECK(textureSet);
127 DALI_TEST_EQUALS(1, textureSet.GetBaseObject().ReferenceCount(), TEST_LOCATION);
129 Texture texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, 32, 32);
130 textureSet.SetTexture(0u, texture);
131 DALI_TEST_EQUALS(textureSet.GetTexture(0u), texture, TEST_LOCATION);
133 TextureSet move = std::move(textureSet);
134 DALI_TEST_CHECK(move);
135 DALI_TEST_EQUALS(1, move.GetBaseObject().ReferenceCount(), TEST_LOCATION);
136 DALI_TEST_EQUALS(move.GetTexture(0u), texture, TEST_LOCATION);
137 DALI_TEST_CHECK(!textureSet);
142 int UtcDaliTextureSetMoveAssignment(void)
144 TestApplication application;
146 TextureSet textureSet = TextureSet::New();
147 DALI_TEST_CHECK(textureSet);
148 DALI_TEST_EQUALS(1, textureSet.GetBaseObject().ReferenceCount(), TEST_LOCATION);
150 Texture texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, 32, 32);
151 textureSet.SetTexture(0u, texture);
152 DALI_TEST_EQUALS(textureSet.GetTexture(0u), texture, TEST_LOCATION);
155 move = std::move(textureSet);
156 DALI_TEST_CHECK(move);
157 DALI_TEST_EQUALS(1, move.GetBaseObject().ReferenceCount(), TEST_LOCATION);
158 DALI_TEST_EQUALS(move.GetTexture(0u), texture, TEST_LOCATION);
159 DALI_TEST_CHECK(!textureSet);
164 int UtcDaliTextureSetDownCast01(void)
166 TestApplication application;
167 TextureSet textureSet = TextureSet::New();
169 BaseHandle handle(textureSet);
170 TextureSet textureSet2 = TextureSet::DownCast(handle);
171 DALI_TEST_CHECK(textureSet2);
176 int UtcDaliTextureSetDownCast02(void)
178 TestApplication application;
180 Handle handle = Handle::New(); // Create a custom object
181 TextureSet textureSet = TextureSet::DownCast(handle);
182 DALI_TEST_CHECK(!textureSet);
186 int UtcDaliTextureSetTexture01(void)
188 TestApplication application;
190 Actor actor = CreateActor(DONT_SET_SAMPLER);
192 application.GetScene().Add(actor);
194 TestGlAbstraction& gl = application.GetGlAbstraction();
196 TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
197 texParameterTrace.Reset();
198 texParameterTrace.Enable(true);
199 application.SendNotification();
200 application.Render();
202 int textureUnit = -1;
203 DALI_TEST_CHECK(gl.GetUniformValue<int>("sTexture", textureUnit));
204 DALI_TEST_EQUALS(textureUnit, 0, TEST_LOCATION);
206 texParameterTrace.Enable(false);
209 // There are four calls to TexParameteri when the texture is first created
210 // as the texture is using default sampling parametrers there shouldn't be any more calls to TexParameteri
211 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 4, TEST_LOCATION);
216 int UtcDaliTextureSetTexture02(void)
218 TestApplication application;
220 Actor actor = CreateActor(SET_SAMPLER);
222 application.GetScene().Add(actor);
224 TestGlAbstraction& gl = application.GetGlAbstraction();
226 TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
227 texParameterTrace.Reset();
228 texParameterTrace.Enable(true);
229 application.SendNotification();
230 application.Render();
232 int textureUnit = -1;
233 DALI_TEST_CHECK(gl.GetUniformValue<int>("sTexture", textureUnit));
234 DALI_TEST_EQUALS(textureUnit, 0, TEST_LOCATION);
236 texParameterTrace.Enable(false);
239 // There are four calls to TexParameteri when the texture is first created
240 // Texture minification and magnification filters are now different than default so
241 //there should have been two extra TexParameteri calls to set the new filter mode
242 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 6, TEST_LOCATION);
247 int UtcDaliTextureSetMultiple(void)
249 TestApplication application;
251 Actor actor1 = CreateActor(SET_SAMPLER);
252 Actor actor2 = CreateActor(SET_SAMPLER);
254 application.GetScene().Add(actor1);
255 application.GetScene().Add(actor2);
257 TestGlAbstraction& gl = application.GetGlAbstraction();
259 TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
260 texParameterTrace.Reset();
261 texParameterTrace.Enable(true);
262 texParameterTrace.EnableLogging(true);
263 application.SendNotification();
264 application.Render();
266 int textureUnit = -1;
267 DALI_TEST_CHECK(gl.GetUniformValue<int>("sTexture", textureUnit));
268 DALI_TEST_EQUALS(textureUnit, 0, TEST_LOCATION);
270 texParameterTrace.Enable(false);
273 // For each actor there are four calls to TexParameteri when the texture is first created
274 // Texture minification and magnification filters are now different than default so
275 // there should have been two extra TexParameteri calls to set the new filter mode
276 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 2 * 6, TEST_LOCATION);
281 int UtcDaliTextureSetSetSampler(void)
283 TestApplication application;
285 Texture image = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
287 Shader shader = CreateShader();
288 TextureSet textureSet = CreateTextureSet(image);
290 Geometry geometry = CreateQuadGeometry();
291 Renderer renderer = Renderer::New(geometry, shader);
292 renderer.SetTextures(textureSet);
294 Actor actor = Actor::New();
295 actor.AddRenderer(renderer);
296 actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
297 actor.SetProperty(Actor::Property::SIZE, Vector2(400.0f, 400.0f));
299 application.GetScene().Add(actor);
301 TestGlAbstraction& gl = application.GetGlAbstraction();
303 TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
304 texParameterTrace.Reset();
305 texParameterTrace.EnableLogging(true);
306 texParameterTrace.Enable(true);
307 application.SendNotification();
308 application.Render();
310 int textureUnit = -1;
311 DALI_TEST_CHECK(gl.GetUniformValue<int>("sTexture", textureUnit));
312 DALI_TEST_EQUALS(textureUnit, 0, TEST_LOCATION);
314 texParameterTrace.Enable(false);
317 // There are 4 calls to TexParameteri when the texture is first created
318 // as the texture is using default sampling parametrers there shouldn't be any more calls to TexParameteri
319 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 4, TEST_LOCATION);
321 texParameterTrace.Reset();
322 texParameterTrace.Enable(true);
324 Sampler sampler = Sampler::New();
325 sampler.SetFilterMode(FilterMode::NEAREST, FilterMode::NEAREST);
326 textureSet.SetSampler(0u, sampler);
328 application.SendNotification();
329 application.Render();
331 texParameterTrace.Enable(false);
334 //There should have been two calls to TexParameteri to set the new filtering mode
335 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 2, TEST_LOCATION);
340 int UtcDaliTextureSetGetTexture(void)
342 TestApplication application;
344 TextureSet textureSet = CreateTextureSet();
345 DALI_TEST_EQUALS(textureSet.GetTexture(0), Texture(), TEST_LOCATION);
346 DALI_TEST_EQUALS(textureSet.GetTexture(1), Texture(), TEST_LOCATION);
347 DALI_TEST_EQUALS(textureSet.GetTexture(2), Texture(), TEST_LOCATION);
349 Texture texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
350 textureSet.SetTexture(0u, texture);
352 DALI_TEST_EQUALS(textureSet.GetTexture(0), texture, TEST_LOCATION);
353 DALI_TEST_EQUALS(textureSet.GetTexture(1), Texture(), TEST_LOCATION);
354 DALI_TEST_EQUALS(textureSet.GetTexture(2), Texture(), TEST_LOCATION);
356 textureSet.SetTexture(2u, texture);
357 DALI_TEST_EQUALS(textureSet.GetTexture(0), texture, TEST_LOCATION);
358 DALI_TEST_EQUALS(textureSet.GetTexture(1), Texture(), TEST_LOCATION);
359 DALI_TEST_EQUALS(textureSet.GetTexture(2), texture, TEST_LOCATION);
361 textureSet.SetTexture(2u, Texture());
362 DALI_TEST_EQUALS(textureSet.GetTexture(0), texture, TEST_LOCATION);
363 DALI_TEST_EQUALS(textureSet.GetTexture(1), Texture(), TEST_LOCATION);
364 DALI_TEST_EQUALS(textureSet.GetTexture(2), Texture(), TEST_LOCATION);
369 int UtcDaliTextureSetGetSampler(void)
371 TestApplication application;
373 TextureSet textureSet = CreateTextureSet();
374 DALI_TEST_EQUALS(textureSet.GetSampler(0), Sampler(), TEST_LOCATION);
375 DALI_TEST_EQUALS(textureSet.GetSampler(1), Sampler(), TEST_LOCATION);
376 DALI_TEST_EQUALS(textureSet.GetSampler(2), Sampler(), TEST_LOCATION);
378 Sampler sampler = Sampler::New();
379 sampler.SetFilterMode(FilterMode::NEAREST, FilterMode::NEAREST);
380 textureSet.SetSampler(0u, sampler);
382 DALI_TEST_EQUALS(textureSet.GetSampler(0), sampler, TEST_LOCATION);
383 DALI_TEST_EQUALS(textureSet.GetSampler(1), Sampler(), TEST_LOCATION);
384 DALI_TEST_EQUALS(textureSet.GetSampler(2), Sampler(), TEST_LOCATION);
386 textureSet.SetSampler(2u, sampler);
387 DALI_TEST_EQUALS(textureSet.GetSampler(0), sampler, TEST_LOCATION);
388 DALI_TEST_EQUALS(textureSet.GetSampler(1), Sampler(), TEST_LOCATION);
389 DALI_TEST_EQUALS(textureSet.GetSampler(2), sampler, TEST_LOCATION);
391 textureSet.SetSampler(2u, Sampler());
392 DALI_TEST_EQUALS(textureSet.GetSampler(0), sampler, TEST_LOCATION);
393 DALI_TEST_EQUALS(textureSet.GetSampler(1), Sampler(), TEST_LOCATION);
394 DALI_TEST_EQUALS(textureSet.GetSampler(2), Sampler(), TEST_LOCATION);
399 int UtcDaliTextureSetGetTextureCount0(void)
401 TestApplication application;
403 TextureSet textureSet = CreateTextureSet();
404 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 0u, TEST_LOCATION);
406 Texture image = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
407 textureSet.SetTexture(0u, image);
408 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 1u, TEST_LOCATION);
410 textureSet.SetTexture(1u, image);
411 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 2u, TEST_LOCATION);
413 Sampler sampler = Sampler::New();
414 sampler.SetFilterMode(FilterMode::NEAREST, FilterMode::NEAREST);
415 textureSet.SetSampler(2u, sampler);
416 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 2u, TEST_LOCATION);
418 textureSet.SetTexture(2u, image);
419 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 3u, TEST_LOCATION);
420 DALI_TEST_EQUALS(textureSet.GetSampler(2u), sampler, TEST_LOCATION);
425 int UtcDaliTextureSetGetTextureCount1(void)
427 TestApplication application;
429 TextureSet textureSet = CreateTextureSet();
430 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 0u, TEST_LOCATION);
432 Texture texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
433 textureSet.SetTexture(0u, texture);
434 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 1u, TEST_LOCATION);
436 textureSet.SetTexture(1u, texture);
437 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 2u, TEST_LOCATION);
439 Sampler sampler = Sampler::New();
440 sampler.SetFilterMode(FilterMode::NEAREST, FilterMode::NEAREST);
441 textureSet.SetSampler(2u, sampler);
442 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 2u, TEST_LOCATION);
444 textureSet.SetTexture(2u, texture);
445 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 3u, TEST_LOCATION);
446 DALI_TEST_EQUALS(textureSet.GetSampler(2u), sampler, TEST_LOCATION);
451 int UtcDaliTextureSetRemoveTextureAndGetTextureCount(void)
453 TestApplication application;
455 TextureSet textureSet = CreateTextureSet();
456 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 0u, TEST_LOCATION);
458 Texture image0 = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
459 Texture image1 = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
460 Texture image2 = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
462 textureSet.SetTexture(0u, image0);
463 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 1u, TEST_LOCATION);
464 DALI_TEST_EQUALS(textureSet.GetTexture(0u), image0, TEST_LOCATION);
466 textureSet.SetTexture(1u, image1);
467 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 2u, TEST_LOCATION);
468 DALI_TEST_EQUALS(textureSet.GetTexture(0u), image0, TEST_LOCATION);
469 DALI_TEST_EQUALS(textureSet.GetTexture(1u), image1, TEST_LOCATION);
471 // Set empty texture so we can remove it.
472 textureSet.SetTexture(1u, Texture());
474 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 2u, TEST_LOCATION);
475 DALI_TEST_EQUALS(textureSet.GetTexture(0u), image0, TEST_LOCATION);
477 application.SendNotification();
478 application.Render();
480 textureSet.SetTexture(2u, image2);
482 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 3u, TEST_LOCATION);
483 DALI_TEST_EQUALS(textureSet.GetTexture(0u), image0, TEST_LOCATION);
484 DALI_TEST_EQUALS(textureSet.GetTexture(1u), Texture(), TEST_LOCATION);
485 DALI_TEST_EQUALS(textureSet.GetTexture(2u), image2, TEST_LOCATION);
487 textureSet.SetTexture(1u, image1);
489 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 3u, TEST_LOCATION);
490 DALI_TEST_EQUALS(textureSet.GetTexture(0u), image0, TEST_LOCATION);
491 DALI_TEST_EQUALS(textureSet.GetTexture(1u), image1, TEST_LOCATION);
492 DALI_TEST_EQUALS(textureSet.GetTexture(2u), image2, TEST_LOCATION);
494 // Set empty texture middle of textureset.
495 textureSet.SetTexture(1u, Texture());
497 application.SendNotification();
498 application.Render();
500 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 3u, TEST_LOCATION);
501 DALI_TEST_EQUALS(textureSet.GetTexture(0u), image0, TEST_LOCATION);
502 DALI_TEST_EQUALS(textureSet.GetTexture(1u), Texture(), TEST_LOCATION);
503 DALI_TEST_EQUALS(textureSet.GetTexture(2u), image2, TEST_LOCATION);
505 // Set empty texture end of textureset.
506 textureSet.SetTexture(2u, Texture());
508 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 3u, TEST_LOCATION);
509 DALI_TEST_EQUALS(textureSet.GetTexture(0u), image0, TEST_LOCATION);
511 application.SendNotification();
512 application.Render();
516 int UtcDaliTextureSetSetSamplerNegative(void)
518 TestApplication application;
519 Dali::TextureSet instance;
522 unsigned long arg1(0u);
524 instance.SetSampler(arg1, arg2);
525 DALI_TEST_CHECK(false); // Should not get here
529 DALI_TEST_CHECK(true); // We expect an assert
534 int UtcDaliTextureSetSetTextureNegative(void)
536 TestApplication application;
537 Dali::TextureSet instance;
540 unsigned long arg1(0u);
542 instance.SetTexture(arg1, arg2);
543 DALI_TEST_CHECK(false); // Should not get here
547 DALI_TEST_CHECK(true); // We expect an assert
552 int UtcDaliTextureSetGetSamplerNegative(void)
554 TestApplication application;
555 Dali::TextureSet instance;
558 unsigned long arg1(0u);
559 instance.GetSampler(arg1);
560 DALI_TEST_CHECK(false); // Should not get here
564 DALI_TEST_CHECK(true); // We expect an assert
569 int UtcDaliTextureSetGetTextureNegative(void)
571 TestApplication application;
572 Dali::TextureSet instance;
575 unsigned long arg1(0u);
576 instance.GetTexture(arg1);
577 DALI_TEST_CHECK(false); // Should not get here
581 DALI_TEST_CHECK(true); // We expect an assert
586 int UtcDaliTextureSetGetTextureCountNegative(void)
588 TestApplication application;
589 Dali::TextureSet instance;
592 instance.GetTextureCount();
593 DALI_TEST_CHECK(false); // Should not get here
597 DALI_TEST_CHECK(true); // We expect an assert
602 int UtcDaliTextureSetMultipleTextures(void)
604 TestApplication application;
606 Shader shader = CreateShader();
607 TextureSet textureSet = CreateTextureSet();
610 Texture texture1 = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
611 textureSet.SetTexture(0u, texture1);
613 Texture texture2 = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
614 textureSet.SetTexture(1u, texture2);
616 Geometry geometry = CreateQuadGeometry();
617 Renderer renderer = Renderer::New(geometry, shader);
618 renderer.SetTextures(textureSet);
620 Actor actor = Actor::New();
621 actor.AddRenderer(renderer);
622 actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
623 actor.SetProperty(Actor::Property::SIZE, Vector2(400.0f, 400.0f));
625 application.GetScene().Add(actor);
627 application.SendNotification();
628 application.Render();
630 const std::vector<GLuint>& boundTextures0 = application.GetGlAbstraction().GetBoundTextures(GL_TEXTURE0);
631 DALI_TEST_CHECK(boundTextures0[boundTextures0.size() - 1] == 1u); // the latest one should be 0.
633 const std::vector<GLuint>& boundTextures1 = application.GetGlAbstraction().GetBoundTextures(GL_TEXTURE1);
634 size_t count = boundTextures1.size();
635 DALI_TEST_CHECK(boundTextures1[count - 1] == 2u); // the latest one should be 1.
637 // Create a new TextureSet
638 textureSet = CreateTextureSet();
641 textureSet.SetTexture(0u, texture1);
643 renderer.SetTextures(textureSet);
645 application.SendNotification();
646 application.Render();
648 TestGlAbstraction& gl = application.GetGlAbstraction();
649 DALI_TEST_CHECK(gl.GetActiveTextureUnit() == GL_TEXTURE0);
651 DALI_TEST_CHECK(boundTextures0[boundTextures0.size() - 1] == 1u);
652 DALI_TEST_CHECK(boundTextures1.size() == count); // The bound texture count of GL_TEXTURE1 should not be changed.