2 * Copyright (c) 2020 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 = Texture::New(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 = Texture::New(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 = Texture::New(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 = Texture::New(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 application.SendNotification();
263 application.Render();
265 int textureUnit = -1;
266 DALI_TEST_CHECK(gl.GetUniformValue<int>("sTexture", textureUnit));
267 DALI_TEST_EQUALS(textureUnit, 0, TEST_LOCATION);
269 texParameterTrace.Enable(false);
272 // For each actor there are four calls to TexParameteri when the texture is first created
273 // Texture minification and magnification filters are now different than default so
274 //there should have been two extra TexParameteri calls to set the new filter mode
275 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 2 * 6, TEST_LOCATION);
280 int UtcDaliTextureSetSetSampler(void)
282 TestApplication application;
284 Texture image = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
286 Shader shader = CreateShader();
287 TextureSet textureSet = CreateTextureSet(image);
289 Geometry geometry = CreateQuadGeometry();
290 Renderer renderer = Renderer::New(geometry, shader);
291 renderer.SetTextures(textureSet);
293 Actor actor = Actor::New();
294 actor.AddRenderer(renderer);
295 actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
296 actor.SetProperty(Actor::Property::SIZE, Vector2(400.0f, 400.0f));
298 application.GetScene().Add(actor);
300 TestGlAbstraction& gl = application.GetGlAbstraction();
302 TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
303 texParameterTrace.Reset();
304 texParameterTrace.Enable(true);
305 application.SendNotification();
306 application.Render();
308 int textureUnit = -1;
309 DALI_TEST_CHECK(gl.GetUniformValue<int>("sTexture", textureUnit));
310 DALI_TEST_EQUALS(textureUnit, 0, TEST_LOCATION);
312 texParameterTrace.Enable(false);
315 // There are 4 calls to TexParameteri when the texture is first created
316 // as the texture is using default sampling parametrers there shouldn't be any more calls to TexParameteri
317 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 4, TEST_LOCATION);
319 texParameterTrace.Reset();
320 texParameterTrace.Enable(true);
322 Sampler sampler = Sampler::New();
323 sampler.SetFilterMode(FilterMode::NEAREST, FilterMode::NEAREST);
324 textureSet.SetSampler(0u, sampler);
326 application.SendNotification();
327 application.Render();
329 texParameterTrace.Enable(false);
332 //There should have been two calls to TexParameteri to set the new filtering mode
333 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 2, TEST_LOCATION);
338 int UtcDaliTextureSetGetTexture(void)
340 TestApplication application;
342 TextureSet textureSet = CreateTextureSet();
343 DALI_TEST_EQUALS(textureSet.GetTexture(0), Texture(), TEST_LOCATION);
344 DALI_TEST_EQUALS(textureSet.GetTexture(1), Texture(), TEST_LOCATION);
345 DALI_TEST_EQUALS(textureSet.GetTexture(2), Texture(), TEST_LOCATION);
347 Texture texture = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
348 textureSet.SetTexture(0u, texture);
350 DALI_TEST_EQUALS(textureSet.GetTexture(0), texture, TEST_LOCATION);
351 DALI_TEST_EQUALS(textureSet.GetTexture(1), Texture(), TEST_LOCATION);
352 DALI_TEST_EQUALS(textureSet.GetTexture(2), Texture(), TEST_LOCATION);
354 textureSet.SetTexture(2u, texture);
355 DALI_TEST_EQUALS(textureSet.GetTexture(0), texture, TEST_LOCATION);
356 DALI_TEST_EQUALS(textureSet.GetTexture(1), Texture(), TEST_LOCATION);
357 DALI_TEST_EQUALS(textureSet.GetTexture(2), texture, TEST_LOCATION);
359 textureSet.SetTexture(2u, Texture());
360 DALI_TEST_EQUALS(textureSet.GetTexture(0), texture, TEST_LOCATION);
361 DALI_TEST_EQUALS(textureSet.GetTexture(1), Texture(), TEST_LOCATION);
362 DALI_TEST_EQUALS(textureSet.GetTexture(2), Texture(), TEST_LOCATION);
367 int UtcDaliTextureSetGetSampler(void)
369 TestApplication application;
371 TextureSet textureSet = CreateTextureSet();
372 DALI_TEST_EQUALS(textureSet.GetSampler(0), Sampler(), TEST_LOCATION);
373 DALI_TEST_EQUALS(textureSet.GetSampler(1), Sampler(), TEST_LOCATION);
374 DALI_TEST_EQUALS(textureSet.GetSampler(2), Sampler(), TEST_LOCATION);
376 Sampler sampler = Sampler::New();
377 sampler.SetFilterMode(FilterMode::NEAREST, FilterMode::NEAREST);
378 textureSet.SetSampler(0u, sampler);
380 DALI_TEST_EQUALS(textureSet.GetSampler(0), sampler, TEST_LOCATION);
381 DALI_TEST_EQUALS(textureSet.GetSampler(1), Sampler(), TEST_LOCATION);
382 DALI_TEST_EQUALS(textureSet.GetSampler(2), Sampler(), TEST_LOCATION);
384 textureSet.SetSampler(2u, sampler);
385 DALI_TEST_EQUALS(textureSet.GetSampler(0), sampler, TEST_LOCATION);
386 DALI_TEST_EQUALS(textureSet.GetSampler(1), Sampler(), TEST_LOCATION);
387 DALI_TEST_EQUALS(textureSet.GetSampler(2), sampler, TEST_LOCATION);
389 textureSet.SetSampler(2u, Sampler());
390 DALI_TEST_EQUALS(textureSet.GetSampler(0), sampler, TEST_LOCATION);
391 DALI_TEST_EQUALS(textureSet.GetSampler(1), Sampler(), TEST_LOCATION);
392 DALI_TEST_EQUALS(textureSet.GetSampler(2), Sampler(), TEST_LOCATION);
397 int UtcDaliTextureSetGetTextureCount0(void)
399 TestApplication application;
401 TextureSet textureSet = CreateTextureSet();
402 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 0u, TEST_LOCATION);
404 Texture image = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
405 textureSet.SetTexture(0u, image);
406 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 1u, TEST_LOCATION);
408 textureSet.SetTexture(1u, image);
409 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 2u, TEST_LOCATION);
411 Sampler sampler = Sampler::New();
412 sampler.SetFilterMode(FilterMode::NEAREST, FilterMode::NEAREST);
413 textureSet.SetSampler(2u, sampler);
414 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 2u, TEST_LOCATION);
416 textureSet.SetTexture(2u, image);
417 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 3u, TEST_LOCATION);
418 DALI_TEST_EQUALS(textureSet.GetSampler(2u), sampler, TEST_LOCATION);
423 int UtcDaliTextureSetGetTextureCount1(void)
425 TestApplication application;
427 TextureSet textureSet = CreateTextureSet();
428 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 0u, TEST_LOCATION);
430 Texture texture = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
431 textureSet.SetTexture(0u, texture);
432 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 1u, TEST_LOCATION);
434 textureSet.SetTexture(1u, texture);
435 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 2u, TEST_LOCATION);
437 Sampler sampler = Sampler::New();
438 sampler.SetFilterMode(FilterMode::NEAREST, FilterMode::NEAREST);
439 textureSet.SetSampler(2u, sampler);
440 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 2u, TEST_LOCATION);
442 textureSet.SetTexture(2u, texture);
443 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 3u, TEST_LOCATION);
444 DALI_TEST_EQUALS(textureSet.GetSampler(2u), sampler, TEST_LOCATION);
449 int UtcDaliTextureSetSetSamplerNegative(void)
451 TestApplication application;
452 Dali::TextureSet instance;
455 unsigned long arg1(0u);
457 instance.SetSampler(arg1, arg2);
458 DALI_TEST_CHECK(false); // Should not get here
462 DALI_TEST_CHECK(true); // We expect an assert
467 int UtcDaliTextureSetSetTextureNegative(void)
469 TestApplication application;
470 Dali::TextureSet instance;
473 unsigned long arg1(0u);
475 instance.SetTexture(arg1, arg2);
476 DALI_TEST_CHECK(false); // Should not get here
480 DALI_TEST_CHECK(true); // We expect an assert
485 int UtcDaliTextureSetGetSamplerNegative(void)
487 TestApplication application;
488 Dali::TextureSet instance;
491 unsigned long arg1(0u);
492 instance.GetSampler(arg1);
493 DALI_TEST_CHECK(false); // Should not get here
497 DALI_TEST_CHECK(true); // We expect an assert
502 int UtcDaliTextureSetGetTextureNegative(void)
504 TestApplication application;
505 Dali::TextureSet instance;
508 unsigned long arg1(0u);
509 instance.GetTexture(arg1);
510 DALI_TEST_CHECK(false); // Should not get here
514 DALI_TEST_CHECK(true); // We expect an assert
519 int UtcDaliTextureSetGetTextureCountNegative(void)
521 TestApplication application;
522 Dali::TextureSet instance;
525 instance.GetTextureCount();
526 DALI_TEST_CHECK(false); // Should not get here
530 DALI_TEST_CHECK(true); // We expect an assert
535 int UtcDaliTextureSetMultipleTextures(void)
537 TestApplication application;
539 Shader shader = CreateShader();
540 TextureSet textureSet = CreateTextureSet();
543 Texture texture1 = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
544 textureSet.SetTexture(0u, texture1);
546 Texture texture2 = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
547 textureSet.SetTexture(1u, texture2);
549 Geometry geometry = CreateQuadGeometry();
550 Renderer renderer = Renderer::New(geometry, shader);
551 renderer.SetTextures(textureSet);
553 Actor actor = Actor::New();
554 actor.AddRenderer(renderer);
555 actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
556 actor.SetProperty(Actor::Property::SIZE, Vector2(400.0f, 400.0f));
558 application.GetScene().Add(actor);
560 application.SendNotification();
561 application.Render();
563 const std::vector<GLuint>& boundTextures0 = application.GetGlAbstraction().GetBoundTextures(GL_TEXTURE0);
564 DALI_TEST_CHECK(boundTextures0[boundTextures0.size() - 1] == 1u); // the latest one should be 0.
566 const std::vector<GLuint>& boundTextures1 = application.GetGlAbstraction().GetBoundTextures(GL_TEXTURE1);
567 size_t count = boundTextures1.size();
568 DALI_TEST_CHECK(boundTextures1[count - 1] == 2u); // the latest one should be 1.
570 // Create a new TextureSet
571 textureSet = CreateTextureSet();
574 textureSet.SetTexture(0u, texture1);
576 renderer.SetTextures(textureSet);
578 application.SendNotification();
579 application.Render();
581 TestGlAbstraction& gl = application.GetGlAbstraction();
582 DALI_TEST_CHECK(gl.GetActiveTextureUnit() == GL_TEXTURE0);
584 DALI_TEST_CHECK(boundTextures0[boundTextures0.size() - 1] == 1u);
585 DALI_TEST_CHECK(boundTextures1.size() == count); // The bound texture count of GL_TEXTURE1 should not be changed.