2 * Copyright (c) 2021 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 UtcDaliTextureSetSetSamplerNegative(void)
453 TestApplication application;
454 Dali::TextureSet instance;
457 unsigned long arg1(0u);
459 instance.SetSampler(arg1, arg2);
460 DALI_TEST_CHECK(false); // Should not get here
464 DALI_TEST_CHECK(true); // We expect an assert
469 int UtcDaliTextureSetSetTextureNegative(void)
471 TestApplication application;
472 Dali::TextureSet instance;
475 unsigned long arg1(0u);
477 instance.SetTexture(arg1, arg2);
478 DALI_TEST_CHECK(false); // Should not get here
482 DALI_TEST_CHECK(true); // We expect an assert
487 int UtcDaliTextureSetGetSamplerNegative(void)
489 TestApplication application;
490 Dali::TextureSet instance;
493 unsigned long arg1(0u);
494 instance.GetSampler(arg1);
495 DALI_TEST_CHECK(false); // Should not get here
499 DALI_TEST_CHECK(true); // We expect an assert
504 int UtcDaliTextureSetGetTextureNegative(void)
506 TestApplication application;
507 Dali::TextureSet instance;
510 unsigned long arg1(0u);
511 instance.GetTexture(arg1);
512 DALI_TEST_CHECK(false); // Should not get here
516 DALI_TEST_CHECK(true); // We expect an assert
521 int UtcDaliTextureSetGetTextureCountNegative(void)
523 TestApplication application;
524 Dali::TextureSet instance;
527 instance.GetTextureCount();
528 DALI_TEST_CHECK(false); // Should not get here
532 DALI_TEST_CHECK(true); // We expect an assert
537 int UtcDaliTextureSetMultipleTextures(void)
539 TestApplication application;
541 Shader shader = CreateShader();
542 TextureSet textureSet = CreateTextureSet();
545 Texture texture1 = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
546 textureSet.SetTexture(0u, texture1);
548 Texture texture2 = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
549 textureSet.SetTexture(1u, texture2);
551 Geometry geometry = CreateQuadGeometry();
552 Renderer renderer = Renderer::New(geometry, shader);
553 renderer.SetTextures(textureSet);
555 Actor actor = Actor::New();
556 actor.AddRenderer(renderer);
557 actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
558 actor.SetProperty(Actor::Property::SIZE, Vector2(400.0f, 400.0f));
560 application.GetScene().Add(actor);
562 application.SendNotification();
563 application.Render();
565 const std::vector<GLuint>& boundTextures0 = application.GetGlAbstraction().GetBoundTextures(GL_TEXTURE0);
566 DALI_TEST_CHECK(boundTextures0[boundTextures0.size() - 1] == 1u); // the latest one should be 0.
568 const std::vector<GLuint>& boundTextures1 = application.GetGlAbstraction().GetBoundTextures(GL_TEXTURE1);
569 size_t count = boundTextures1.size();
570 DALI_TEST_CHECK(boundTextures1[count - 1] == 2u); // the latest one should be 1.
572 // Create a new TextureSet
573 textureSet = CreateTextureSet();
576 textureSet.SetTexture(0u, texture1);
578 renderer.SetTextures(textureSet);
580 application.SendNotification();
581 application.Render();
583 TestGlAbstraction& gl = application.GetGlAbstraction();
584 DALI_TEST_CHECK(gl.GetActiveTextureUnit() == GL_TEXTURE0);
586 DALI_TEST_CHECK(boundTextures0[boundTextures0.size() - 1] == 1u);
587 DALI_TEST_CHECK(boundTextures1.size() == count); // The bound texture count of GL_TEXTURE1 should not be changed.