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 Texture CreateTexture(TextureType::Type type, Pixel::Format format, int width, int height)
37 Texture texture = Texture::New(type, format, width, height);
39 int bufferSize = width * height * 2;
40 uint8_t* buffer = reinterpret_cast<uint8_t*>(malloc(bufferSize));
41 PixelData pixelData = PixelData::New(buffer, bufferSize, width, height, format, PixelData::FREE);
42 texture.Upload(pixelData, 0u, 0u, 0u, 0u, width, height);
46 Actor CreateActor(SetSampler setSamplerOption)
48 Texture texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
50 Shader shader = CreateShader();
51 TextureSet textureSet = CreateTextureSet();
53 Sampler sampler = Sampler::New();
54 sampler.SetFilterMode(FilterMode::NEAREST, FilterMode::NEAREST);
55 textureSet.SetTexture(0u, texture);
56 if(setSamplerOption == SET_SAMPLER)
58 textureSet.SetSampler(0u, sampler);
61 Geometry geometry = CreateQuadGeometry();
62 Renderer renderer = Renderer::New(geometry, shader);
63 renderer.SetTextures(textureSet);
65 Actor actor = Actor::New();
66 actor.AddRenderer(renderer);
67 actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
68 actor.SetProperty(Actor::Property::SIZE, Vector2(400.0f, 400.0f));
75 void texture_set_test_startup(void)
77 test_return_value = TET_UNDEF;
80 void texture_set_test_cleanup(void)
82 test_return_value = TET_PASS;
85 int UtcDaliTextureSetNew01(void)
87 TestApplication application;
89 TextureSet textureSet = TextureSet::New();
91 DALI_TEST_CHECK(textureSet);
95 int UtcDaliTextureSetNew02(void)
97 TestApplication application;
98 TextureSet textureSet;
99 DALI_TEST_CHECK(!textureSet);
103 int UtcDaliTextureSetCopyConstructor(void)
105 TestApplication application;
107 Texture image = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, 32, 32);
108 TextureSet textureSet = TextureSet::New();
109 textureSet.SetTexture(0u, image);
111 TextureSet textureSetCopy(textureSet);
113 DALI_TEST_CHECK(textureSetCopy);
118 int UtcDaliTextureSetAssignmentOperator(void)
120 TestApplication application;
121 TextureSet textureSet = TextureSet::New();
123 TextureSet textureSet2;
124 DALI_TEST_CHECK(!textureSet2);
126 textureSet2 = textureSet;
127 DALI_TEST_CHECK(textureSet2);
132 int UtcDaliTextureSetMoveConstructor(void)
134 TestApplication application;
136 TextureSet textureSet = TextureSet::New();
137 DALI_TEST_CHECK(textureSet);
138 DALI_TEST_EQUALS(1, textureSet.GetBaseObject().ReferenceCount(), TEST_LOCATION);
140 Texture texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, 32, 32);
141 textureSet.SetTexture(0u, texture);
142 DALI_TEST_EQUALS(textureSet.GetTexture(0u), texture, TEST_LOCATION);
144 TextureSet move = std::move(textureSet);
145 DALI_TEST_CHECK(move);
146 DALI_TEST_EQUALS(1, move.GetBaseObject().ReferenceCount(), TEST_LOCATION);
147 DALI_TEST_EQUALS(move.GetTexture(0u), texture, TEST_LOCATION);
148 DALI_TEST_CHECK(!textureSet);
153 int UtcDaliTextureSetMoveAssignment(void)
155 TestApplication application;
157 TextureSet textureSet = TextureSet::New();
158 DALI_TEST_CHECK(textureSet);
159 DALI_TEST_EQUALS(1, textureSet.GetBaseObject().ReferenceCount(), TEST_LOCATION);
161 Texture texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, 32, 32);
162 textureSet.SetTexture(0u, texture);
163 DALI_TEST_EQUALS(textureSet.GetTexture(0u), texture, TEST_LOCATION);
166 move = std::move(textureSet);
167 DALI_TEST_CHECK(move);
168 DALI_TEST_EQUALS(1, move.GetBaseObject().ReferenceCount(), TEST_LOCATION);
169 DALI_TEST_EQUALS(move.GetTexture(0u), texture, TEST_LOCATION);
170 DALI_TEST_CHECK(!textureSet);
175 int UtcDaliTextureSetDownCast01(void)
177 TestApplication application;
178 TextureSet textureSet = TextureSet::New();
180 BaseHandle handle(textureSet);
181 TextureSet textureSet2 = TextureSet::DownCast(handle);
182 DALI_TEST_CHECK(textureSet2);
187 int UtcDaliTextureSetDownCast02(void)
189 TestApplication application;
191 Handle handle = Handle::New(); // Create a custom object
192 TextureSet textureSet = TextureSet::DownCast(handle);
193 DALI_TEST_CHECK(!textureSet);
197 int UtcDaliTextureSetTexture01(void)
199 TestApplication application;
201 Actor actor = CreateActor(DONT_SET_SAMPLER);
203 application.GetScene().Add(actor);
205 TestGlAbstraction& gl = application.GetGlAbstraction();
207 TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
208 texParameterTrace.Reset();
209 texParameterTrace.Enable(true);
210 application.SendNotification();
211 application.Render();
213 int textureUnit = -1;
214 DALI_TEST_CHECK(gl.GetUniformValue<int>("sTexture", textureUnit));
215 DALI_TEST_EQUALS(textureUnit, 0, TEST_LOCATION);
217 texParameterTrace.Enable(false);
220 // There are four calls to TexParameteri when the texture is first created
221 // as the texture is using default sampling parametrers there shouldn't be any more calls to TexParameteri
222 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 4, TEST_LOCATION);
227 int UtcDaliTextureSetTexture02(void)
229 TestApplication application;
231 Actor actor = CreateActor(SET_SAMPLER);
233 application.GetScene().Add(actor);
235 TestGlAbstraction& gl = application.GetGlAbstraction();
237 TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
238 texParameterTrace.Reset();
239 texParameterTrace.Enable(true);
240 application.SendNotification();
241 application.Render();
243 int textureUnit = -1;
244 DALI_TEST_CHECK(gl.GetUniformValue<int>("sTexture", textureUnit));
245 DALI_TEST_EQUALS(textureUnit, 0, TEST_LOCATION);
247 texParameterTrace.Enable(false);
250 // There are four calls to TexParameteri when the texture is first created
251 // Texture minification and magnification filters are now different than default so
252 //there should have been two extra TexParameteri calls to set the new filter mode
253 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 6, TEST_LOCATION);
258 int UtcDaliTextureSetMultiple(void)
260 TestApplication application;
262 Actor actor1 = CreateActor(SET_SAMPLER);
263 Actor actor2 = CreateActor(SET_SAMPLER);
265 application.GetScene().Add(actor1);
266 application.GetScene().Add(actor2);
268 TestGlAbstraction& gl = application.GetGlAbstraction();
270 TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
271 texParameterTrace.Reset();
272 texParameterTrace.Enable(true);
273 texParameterTrace.EnableLogging(true);
274 application.SendNotification();
275 application.Render();
277 int textureUnit = -1;
278 DALI_TEST_CHECK(gl.GetUniformValue<int>("sTexture", textureUnit));
279 DALI_TEST_EQUALS(textureUnit, 0, TEST_LOCATION);
281 texParameterTrace.Enable(false);
284 // For each actor there are four calls to TexParameteri when the texture is first created
285 // Texture minification and magnification filters are now different than default so
286 // there should have been two extra TexParameteri calls to set the new filter mode
287 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 2 * 6, TEST_LOCATION);
292 int UtcDaliTextureSetSetSampler(void)
294 TestApplication application;
296 Texture image = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
298 Shader shader = CreateShader();
299 TextureSet textureSet = CreateTextureSet(image);
301 Geometry geometry = CreateQuadGeometry();
302 Renderer renderer = Renderer::New(geometry, shader);
303 renderer.SetTextures(textureSet);
305 Actor actor = Actor::New();
306 actor.AddRenderer(renderer);
307 actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
308 actor.SetProperty(Actor::Property::SIZE, Vector2(400.0f, 400.0f));
310 application.GetScene().Add(actor);
312 TestGlAbstraction& gl = application.GetGlAbstraction();
314 TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
315 texParameterTrace.Reset();
316 texParameterTrace.EnableLogging(true);
317 texParameterTrace.Enable(true);
318 application.SendNotification();
319 application.Render();
321 int textureUnit = -1;
322 DALI_TEST_CHECK(gl.GetUniformValue<int>("sTexture", textureUnit));
323 DALI_TEST_EQUALS(textureUnit, 0, TEST_LOCATION);
325 texParameterTrace.Enable(false);
328 // There are 4 calls to TexParameteri when the texture is first created
329 // as the texture is using default sampling parametrers there shouldn't be any more calls to TexParameteri
330 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 4, TEST_LOCATION);
332 texParameterTrace.Reset();
333 texParameterTrace.Enable(true);
335 Sampler sampler = Sampler::New();
336 sampler.SetFilterMode(FilterMode::NEAREST, FilterMode::NEAREST);
337 textureSet.SetSampler(0u, sampler);
339 application.SendNotification();
340 application.Render();
342 texParameterTrace.Enable(false);
345 //There should have been two calls to TexParameteri to set the new filtering mode
346 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 2, TEST_LOCATION);
351 int UtcDaliTextureSetGetTexture(void)
353 TestApplication application;
355 TextureSet textureSet = CreateTextureSet();
356 DALI_TEST_EQUALS(textureSet.GetTexture(0), Texture(), TEST_LOCATION);
357 DALI_TEST_EQUALS(textureSet.GetTexture(1), Texture(), TEST_LOCATION);
358 DALI_TEST_EQUALS(textureSet.GetTexture(2), Texture(), TEST_LOCATION);
360 Texture texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
361 textureSet.SetTexture(0u, 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);
367 textureSet.SetTexture(2u, texture);
368 DALI_TEST_EQUALS(textureSet.GetTexture(0), texture, TEST_LOCATION);
369 DALI_TEST_EQUALS(textureSet.GetTexture(1), Texture(), TEST_LOCATION);
370 DALI_TEST_EQUALS(textureSet.GetTexture(2), texture, TEST_LOCATION);
372 textureSet.SetTexture(2u, Texture());
373 DALI_TEST_EQUALS(textureSet.GetTexture(0), texture, TEST_LOCATION);
374 DALI_TEST_EQUALS(textureSet.GetTexture(1), Texture(), TEST_LOCATION);
375 DALI_TEST_EQUALS(textureSet.GetTexture(2), Texture(), TEST_LOCATION);
380 int UtcDaliTextureSetGetSampler(void)
382 TestApplication application;
384 TextureSet textureSet = CreateTextureSet();
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 Sampler sampler = Sampler::New();
390 sampler.SetFilterMode(FilterMode::NEAREST, FilterMode::NEAREST);
391 textureSet.SetSampler(0u, 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);
397 textureSet.SetSampler(2u, sampler);
398 DALI_TEST_EQUALS(textureSet.GetSampler(0), sampler, TEST_LOCATION);
399 DALI_TEST_EQUALS(textureSet.GetSampler(1), Sampler(), TEST_LOCATION);
400 DALI_TEST_EQUALS(textureSet.GetSampler(2), sampler, TEST_LOCATION);
402 textureSet.SetSampler(2u, Sampler());
403 DALI_TEST_EQUALS(textureSet.GetSampler(0), sampler, TEST_LOCATION);
404 DALI_TEST_EQUALS(textureSet.GetSampler(1), Sampler(), TEST_LOCATION);
405 DALI_TEST_EQUALS(textureSet.GetSampler(2), Sampler(), TEST_LOCATION);
410 int UtcDaliTextureSetGetTextureCount0(void)
412 TestApplication application;
414 TextureSet textureSet = CreateTextureSet();
415 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 0u, TEST_LOCATION);
417 Texture image = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
418 textureSet.SetTexture(0u, image);
419 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 1u, TEST_LOCATION);
421 textureSet.SetTexture(1u, image);
422 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 2u, TEST_LOCATION);
424 Sampler sampler = Sampler::New();
425 sampler.SetFilterMode(FilterMode::NEAREST, FilterMode::NEAREST);
426 textureSet.SetSampler(2u, sampler);
427 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 2u, TEST_LOCATION);
429 textureSet.SetTexture(2u, image);
430 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 3u, TEST_LOCATION);
431 DALI_TEST_EQUALS(textureSet.GetSampler(2u), sampler, TEST_LOCATION);
436 int UtcDaliTextureSetGetTextureCount1(void)
438 TestApplication application;
440 TextureSet textureSet = CreateTextureSet();
441 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 0u, TEST_LOCATION);
443 Texture texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
444 textureSet.SetTexture(0u, texture);
445 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 1u, TEST_LOCATION);
447 textureSet.SetTexture(1u, texture);
448 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 2u, TEST_LOCATION);
450 Sampler sampler = Sampler::New();
451 sampler.SetFilterMode(FilterMode::NEAREST, FilterMode::NEAREST);
452 textureSet.SetSampler(2u, sampler);
453 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 2u, TEST_LOCATION);
455 textureSet.SetTexture(2u, texture);
456 DALI_TEST_EQUALS(textureSet.GetTextureCount(), 3u, TEST_LOCATION);
457 DALI_TEST_EQUALS(textureSet.GetSampler(2u), sampler, TEST_LOCATION);
462 int UtcDaliTextureSetSetSamplerNegative(void)
464 TestApplication application;
465 Dali::TextureSet instance;
468 unsigned long arg1(0u);
470 instance.SetSampler(arg1, arg2);
471 DALI_TEST_CHECK(false); // Should not get here
475 DALI_TEST_CHECK(true); // We expect an assert
480 int UtcDaliTextureSetSetTextureNegative(void)
482 TestApplication application;
483 Dali::TextureSet instance;
486 unsigned long arg1(0u);
488 instance.SetTexture(arg1, arg2);
489 DALI_TEST_CHECK(false); // Should not get here
493 DALI_TEST_CHECK(true); // We expect an assert
498 int UtcDaliTextureSetGetSamplerNegative(void)
500 TestApplication application;
501 Dali::TextureSet instance;
504 unsigned long arg1(0u);
505 instance.GetSampler(arg1);
506 DALI_TEST_CHECK(false); // Should not get here
510 DALI_TEST_CHECK(true); // We expect an assert
515 int UtcDaliTextureSetGetTextureNegative(void)
517 TestApplication application;
518 Dali::TextureSet instance;
521 unsigned long arg1(0u);
522 instance.GetTexture(arg1);
523 DALI_TEST_CHECK(false); // Should not get here
527 DALI_TEST_CHECK(true); // We expect an assert
532 int UtcDaliTextureSetGetTextureCountNegative(void)
534 TestApplication application;
535 Dali::TextureSet instance;
538 instance.GetTextureCount();
539 DALI_TEST_CHECK(false); // Should not get here
543 DALI_TEST_CHECK(true); // We expect an assert
548 int UtcDaliTextureSetMultipleTextures(void)
550 TestApplication application;
552 Shader shader = CreateShader();
553 TextureSet textureSet = CreateTextureSet();
556 Texture texture1 = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
557 textureSet.SetTexture(0u, texture1);
559 Texture texture2 = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
560 textureSet.SetTexture(1u, texture2);
562 Geometry geometry = CreateQuadGeometry();
563 Renderer renderer = Renderer::New(geometry, shader);
564 renderer.SetTextures(textureSet);
566 Actor actor = Actor::New();
567 actor.AddRenderer(renderer);
568 actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
569 actor.SetProperty(Actor::Property::SIZE, Vector2(400.0f, 400.0f));
571 application.GetScene().Add(actor);
573 application.SendNotification();
574 application.Render();
576 const std::vector<GLuint>& boundTextures0 = application.GetGlAbstraction().GetBoundTextures(GL_TEXTURE0);
577 DALI_TEST_CHECK(boundTextures0[boundTextures0.size() - 1] == 1u); // the latest one should be 0.
579 const std::vector<GLuint>& boundTextures1 = application.GetGlAbstraction().GetBoundTextures(GL_TEXTURE1);
580 size_t count = boundTextures1.size();
581 DALI_TEST_CHECK(boundTextures1[count - 1] == 2u); // the latest one should be 1.
583 // Create a new TextureSet
584 textureSet = CreateTextureSet();
587 textureSet.SetTexture(0u, texture1);
589 renderer.SetTextures(textureSet);
591 application.SendNotification();
592 application.Render();
594 TestGlAbstraction& gl = application.GetGlAbstraction();
595 DALI_TEST_CHECK(gl.GetActiveTextureUnit() == GL_TEXTURE0);
597 DALI_TEST_CHECK(boundTextures0[boundTextures0.size() - 1] == 1u);
598 DALI_TEST_CHECK(boundTextures1.size() == count); // The bound texture count of GL_TEXTURE1 should not be changed.