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.
22 #include <dali-toolkit-test-suite-utils.h>
23 #include <toolkit-timer.h>
24 #include <toolkit-event-thread-callback.h>
25 #include <dali-toolkit/internal/visuals/texture-manager-impl.h>
26 #include <dali-toolkit/internal/visuals/texture-upload-observer.h>
27 #include <dali/devel-api/adaptor-framework/pixel-buffer.h>
28 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
29 #include <dali-toolkit/internal/visuals/visual-factory-impl.h> ///< For VisualFactory's member TextureManager.
31 #include <test-encoded-image-buffer.h>
33 #if defined(ELDBUS_ENABLED)
34 #include <automated-tests/src/dali-toolkit-internal/dali-toolkit-test-utils/dbus-wrapper.h>
37 using namespace Dali::Toolkit::Internal;
39 void utc_dali_toolkit_texture_manager_startup(void)
41 setenv( "LOG_TEXTURE_MANAGER", "3", 1 );
42 test_return_value = TET_UNDEF;
43 #if defined(ELDBUS_ENABLED)
44 DBusWrapper::Install(std::unique_ptr<DBusWrapper>(new TestDBusWrapper));
48 void utc_dali_toolkit_texture_manager_cleanup(void)
50 test_return_value = TET_PASS;
56 const char* TEST_IMAGE_FILE_NAME = TEST_RESOURCE_DIR "/gallery-small-1.jpg";
60 class TestObserver : public Dali::Toolkit::TextureUploadObserver
63 enum class CompleteType
72 : mCompleteType( CompleteType::NOT_COMPLETED ),
74 mObserverCalled(false)
78 virtual void UploadComplete( bool loadSuccess, int32_t textureId, TextureSet textureSet,
79 bool useAtlasing, const Vector4& atlasRect, bool preMultiplied ) override
81 mCompleteType = CompleteType::UPLOAD_COMPLETE;
82 mLoaded = loadSuccess;
83 mObserverCalled = true;
86 virtual void LoadComplete( bool loadSuccess, Devel::PixelBuffer pixelBuffer, const VisualUrl& url, bool preMultiplied ) override
88 mCompleteType = CompleteType::LOAD_COMPLETE;
89 mLoaded = loadSuccess;
90 mObserverCalled = true;
93 CompleteType mCompleteType;
99 int UtcTextureManagerRequestLoad(void)
101 ToolkitTestApplication application;
103 TextureManager textureManager; // Create new texture manager
105 TestObserver observer;
106 std::string filename("image.png");
107 auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
108 TextureManager::TextureId textureId = textureManager.RequestLoad(
111 FittingMode::SCALE_TO_FILL,
112 SamplingMode::BOX_THEN_LINEAR,
113 TextureManager::NO_ATLAS,
116 TextureManager::ReloadPolicy::CACHED,
119 VisualUrl url = textureManager.GetVisualUrl( textureId );
121 DALI_TEST_EQUALS( url.GetUrl().compare( filename ), 0, TEST_LOCATION );
126 int UtcTextureManagerGenerateHash(void)
128 ToolkitTestApplication application;
130 TextureManager textureManager; // Create new texture manager
132 TestObserver observer;
133 std::string filename( "image.png" );
134 auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
135 TextureManager::TextureId textureId = textureManager.RequestLoad(
138 FittingMode::SCALE_TO_FILL,
139 SamplingMode::BOX_THEN_LINEAR,
140 TextureManager::USE_ATLAS,
143 TextureManager::ReloadPolicy::CACHED,
146 VisualUrl url = textureManager.GetVisualUrl( textureId );
148 DALI_TEST_EQUALS( url.GetUrl().compare( filename ), 0, TEST_LOCATION );
153 int UtcTextureManagerEncodedImageBuffer(void)
155 ToolkitTestApplication application;
156 tet_infoline( "UtcTextureManagerEncodedImageBuffer" );
158 auto visualFactory = Toolkit::VisualFactory::Get();
159 auto& textureManager = GetImplementation(visualFactory).GetTextureManager(); // Use VisualFactory's texture manager
161 // Get encoded raw-buffer image and generate url
162 EncodedImageBuffer buffer1 = ConvertFileToEncodedImageBuffer(TEST_IMAGE_FILE_NAME);
163 EncodedImageBuffer buffer2 = ConvertFileToEncodedImageBuffer(TEST_IMAGE_FILE_NAME);
165 std::string url1 = textureManager.AddExternalEncodedImageBuffer(buffer1);
166 std::string url2 = textureManager.AddExternalEncodedImageBuffer(buffer1);
167 std::string url3 = VisualUrl::CreateBufferUrl(""); ///< Impossible Buffer URL. for coverage
169 // Check if same EncodedImageBuffer get same url
170 DALI_TEST_CHECK(url1 == url2);
171 // Reduce reference count
172 textureManager.RemoveExternalEncodedImageBuffer(url1);
173 // Check whethere url1 still valid
174 DALI_TEST_CHECK(textureManager.GetEncodedImageBuffer(url1));
176 url2 = textureManager.AddExternalEncodedImageBuffer(buffer2);
177 // Check if difference EncodedImageBuffer get difference url
178 DALI_TEST_CHECK(url1 != url2);
180 auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
182 TestObserver observer1;
183 textureManager.RequestLoad(
186 FittingMode::SCALE_TO_FILL,
187 SamplingMode::BOX_THEN_LINEAR,
188 TextureManager::NO_ATLAS,
190 true, ///< orientationCorrection
191 TextureManager::ReloadPolicy::CACHED,
194 DALI_TEST_EQUALS( observer1.mLoaded, false, TEST_LOCATION );
195 DALI_TEST_EQUALS( observer1.mObserverCalled, false, TEST_LOCATION );
197 application.SendNotification();
198 application.Render();
200 DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
202 application.SendNotification();
203 application.Render();
205 DALI_TEST_EQUALS( observer1.mLoaded, true, TEST_LOCATION );
206 DALI_TEST_EQUALS( observer1.mObserverCalled, true, TEST_LOCATION );
207 DALI_TEST_EQUALS( observer1.mCompleteType, TestObserver::CompleteType::UPLOAD_COMPLETE, TEST_LOCATION );
209 TestObserver observer2;
211 Devel::PixelBuffer pixelBuffer = textureManager.LoadPixelBuffer(
214 FittingMode::SCALE_TO_FILL,
215 SamplingMode::BOX_THEN_LINEAR,
216 true, ///< synchronousLoading
218 true, ///< orientationCorrection
221 DALI_TEST_CHECK( pixelBuffer );
222 DALI_TEST_EQUALS( observer2.mLoaded, false, TEST_LOCATION );
223 DALI_TEST_EQUALS( observer2.mObserverCalled, false, TEST_LOCATION );
226 pixelBuffer = textureManager.LoadPixelBuffer(
229 FittingMode::SCALE_TO_FILL,
230 SamplingMode::BOX_THEN_LINEAR,
231 false, ///< synchronousLoading
233 true, ///< orientationCorrection
236 DALI_TEST_EQUALS( observer2.mLoaded, false, TEST_LOCATION );
237 DALI_TEST_EQUALS( observer2.mObserverCalled, false, TEST_LOCATION );
239 application.SendNotification();
240 application.Render();
242 DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
244 application.SendNotification();
245 application.Render();
247 DALI_TEST_EQUALS( observer2.mLoaded, true, TEST_LOCATION );
248 DALI_TEST_EQUALS( observer2.mObserverCalled, true, TEST_LOCATION );
249 DALI_TEST_EQUALS( observer2.mCompleteType, TestObserver::CompleteType::LOAD_COMPLETE, TEST_LOCATION );
251 textureManager.RemoveExternalEncodedImageBuffer(url1);
252 textureManager.RemoveExternalEncodedImageBuffer(url2);
254 // Now url1 and url2 is invalid type. mLoaded will return false
256 TestObserver observer3;
257 textureManager.RequestLoad(
260 FittingMode::SCALE_TO_FILL,
261 SamplingMode::BOX_THEN_LINEAR,
262 TextureManager::NO_ATLAS,
264 true, ///< orientationCorrection
265 TextureManager::ReloadPolicy::CACHED,
268 // Load will be success because url1 is cached
269 DALI_TEST_EQUALS( observer3.mLoaded, true, TEST_LOCATION );
270 DALI_TEST_EQUALS( observer3.mObserverCalled, true, TEST_LOCATION );
271 DALI_TEST_EQUALS( observer3.mCompleteType, TestObserver::CompleteType::UPLOAD_COMPLETE, TEST_LOCATION );
273 TestObserver observer4;
274 textureManager.RequestLoad(
277 FittingMode::SCALE_TO_FILL,
278 SamplingMode::BOX_THEN_LINEAR,
279 TextureManager::NO_ATLAS,
281 true, ///< orientationCorrection
282 TextureManager::ReloadPolicy::FORCED,
285 DALI_TEST_EQUALS( observer4.mLoaded, false, TEST_LOCATION );
286 DALI_TEST_EQUALS( observer4.mObserverCalled, false, TEST_LOCATION );
287 application.SendNotification();
288 application.Render();
290 DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
292 application.SendNotification();
293 application.Render();
295 // Load will be failed becuase reloadpolicy is forced
296 DALI_TEST_EQUALS( observer4.mLoaded, false, TEST_LOCATION );
297 DALI_TEST_EQUALS( observer4.mObserverCalled, true, TEST_LOCATION );
298 DALI_TEST_EQUALS( observer4.mCompleteType, TestObserver::CompleteType::UPLOAD_COMPLETE, TEST_LOCATION );
300 TestObserver observer5;
301 pixelBuffer = textureManager.LoadPixelBuffer(
304 FittingMode::SCALE_TO_FILL,
305 SamplingMode::BOX_THEN_LINEAR,
306 true, ///< synchronousLoading
308 true, ///< orientationCorrection
311 // Load will be faild because synchronousLoading doesn't use cached texture
312 DALI_TEST_CHECK( !pixelBuffer );
313 DALI_TEST_EQUALS( observer5.mLoaded, false, TEST_LOCATION );
314 DALI_TEST_EQUALS( observer5.mObserverCalled, false, TEST_LOCATION );
316 TestObserver observer6;
317 pixelBuffer = textureManager.LoadPixelBuffer(
320 FittingMode::SCALE_TO_FILL,
321 SamplingMode::BOX_THEN_LINEAR,
322 false, ///< synchronousLoading
324 true, ///< orientationCorrection
327 DALI_TEST_EQUALS( observer6.mLoaded, false, TEST_LOCATION );
328 DALI_TEST_EQUALS( observer6.mObserverCalled, false, TEST_LOCATION );
330 application.SendNotification();
331 application.Render();
333 DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
335 application.SendNotification();
336 application.Render();
338 // Load will be failed because url3 is invalid URL
339 DALI_TEST_EQUALS( observer6.mLoaded, false, TEST_LOCATION );
340 DALI_TEST_EQUALS( observer6.mObserverCalled, true, TEST_LOCATION );
341 DALI_TEST_EQUALS( observer6.mCompleteType, TestObserver::CompleteType::LOAD_COMPLETE, TEST_LOCATION );
346 int UtcTextureManagerEncodedImageBufferReferenceCount(void)
348 ToolkitTestApplication application;
349 tet_infoline( "UtcTextureManagerEncodedImageBuffer check reference count works well" );
351 auto visualFactory = Toolkit::VisualFactory::Get();
352 auto& textureManager = GetImplementation(visualFactory).GetTextureManager(); // Use VisualFactory's texture manager
354 // Get encoded raw-buffer image and generate url
355 EncodedImageBuffer buffer1 = ConvertFileToEncodedImageBuffer(TEST_IMAGE_FILE_NAME);
356 EncodedImageBuffer buffer2 = ConvertFileToEncodedImageBuffer(TEST_IMAGE_FILE_NAME);
358 std::string url1 = textureManager.AddExternalEncodedImageBuffer(buffer1);
359 std::string url2 = textureManager.AddExternalEncodedImageBuffer(buffer1);
361 // Check if same EncodedImageBuffer get same url
362 DALI_TEST_CHECK(url1 == url2);
364 // Reduce reference count
365 textureManager.RemoveExternalEncodedImageBuffer(url1);
366 // Check whethere url1 still valid
367 DALI_TEST_CHECK(textureManager.GetEncodedImageBuffer(url1));
369 // Reduce reference count
370 textureManager.RemoveExternalEncodedImageBuffer(url1);
371 // Check whethere url1 is not valid anymore
372 DALI_TEST_CHECK(!textureManager.GetEncodedImageBuffer(url1));
374 // UseExternalTexture doesn't create new buffer.
375 // So, reference count is still zero.
376 textureManager.UseExternalResource(url1);
377 DALI_TEST_CHECK(!textureManager.GetEncodedImageBuffer(url1));
379 url1 = textureManager.AddExternalEncodedImageBuffer(buffer1);
380 // Check if difference EncodedImageBuffer get difference url
381 // Previous EncodedImageBuffer was deleted, so we get new url even same buffer.
382 DALI_TEST_CHECK(url1 != url2);
384 url2 = textureManager.AddExternalEncodedImageBuffer(buffer2);
385 // Check if difference EncodedImageBuffer get difference url
386 DALI_TEST_CHECK(url1 != url2);
388 auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
390 // url1 load image by cache
391 TestObserver observer1;
392 textureManager.RequestLoad(
395 FittingMode::SCALE_TO_FILL,
396 SamplingMode::BOX_THEN_LINEAR,
397 TextureManager::NO_ATLAS,
399 true, ///< orientationCorrection
400 TextureManager::ReloadPolicy::CACHED,
403 DALI_TEST_EQUALS( observer1.mLoaded, false, TEST_LOCATION );
404 DALI_TEST_EQUALS( observer1.mObserverCalled, false, TEST_LOCATION );
406 application.SendNotification();
407 application.Render();
409 DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
411 application.SendNotification();
412 application.Render();
414 DALI_TEST_EQUALS( observer1.mLoaded, true, TEST_LOCATION );
415 DALI_TEST_EQUALS( observer1.mObserverCalled, true, TEST_LOCATION );
416 DALI_TEST_EQUALS( observer1.mCompleteType, TestObserver::CompleteType::UPLOAD_COMPLETE, TEST_LOCATION );
418 // LoadPixelBuffer doen't use cache. url2 will not be cached
419 TestObserver observer2;
420 Devel::PixelBuffer pixelBuffer = textureManager.LoadPixelBuffer(
423 FittingMode::SCALE_TO_FILL,
424 SamplingMode::BOX_THEN_LINEAR,
425 false, ///< synchronousLoading
427 true, ///< orientationCorrection
430 DALI_TEST_EQUALS( observer2.mLoaded, false, TEST_LOCATION );
431 DALI_TEST_EQUALS( observer2.mObserverCalled, false, TEST_LOCATION );
433 application.SendNotification();
434 application.Render();
436 DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
438 application.SendNotification();
439 application.Render();
441 DALI_TEST_EQUALS( observer2.mLoaded, true, TEST_LOCATION );
442 DALI_TEST_EQUALS( observer2.mObserverCalled, true, TEST_LOCATION );
443 DALI_TEST_EQUALS( observer2.mCompleteType, TestObserver::CompleteType::LOAD_COMPLETE, TEST_LOCATION );
445 // Decrease each url's reference count.
446 textureManager.RemoveExternalEncodedImageBuffer(url1);
447 textureManager.RemoveExternalEncodedImageBuffer(url2);
449 // url1 buffer is still have 1 reference count because it is cached.
450 // But url2 not valid because it is not cached.
451 DALI_TEST_CHECK(textureManager.GetEncodedImageBuffer(url1));
452 DALI_TEST_CHECK(!textureManager.GetEncodedImageBuffer(url2));
454 // Check url1 buffer have 1 reference count because it is cached.
455 textureManager.RemoveExternalEncodedImageBuffer(url1);
456 DALI_TEST_CHECK(!textureManager.GetEncodedImageBuffer(url1));
462 int UtcTextureManagerCachingForDifferentLoadingType(void)
464 ToolkitTestApplication application;
465 tet_infoline( "UtcTextureManagerCachingForDifferentLoadingType" );
467 TextureManager textureManager; // Create new texture manager
469 TestObserver observer1;
470 std::string filename( TEST_IMAGE_FILE_NAME );
471 auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
472 textureManager.RequestLoad(
475 FittingMode::SCALE_TO_FILL,
476 SamplingMode::BOX_THEN_LINEAR,
477 TextureManager::NO_ATLAS,
480 TextureManager::ReloadPolicy::CACHED,
483 DALI_TEST_EQUALS( observer1.mLoaded, false, TEST_LOCATION );
484 DALI_TEST_EQUALS( observer1.mObserverCalled, false, TEST_LOCATION );
486 application.SendNotification();
487 application.Render();
489 DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
491 application.SendNotification();
492 application.Render();
494 DALI_TEST_EQUALS( observer1.mLoaded, true, TEST_LOCATION );
495 DALI_TEST_EQUALS( observer1.mObserverCalled, true, TEST_LOCATION );
496 DALI_TEST_EQUALS( observer1.mCompleteType, TestObserver::CompleteType::UPLOAD_COMPLETE, TEST_LOCATION );
498 TestObserver observer2;
499 Devel::PixelBuffer pixelBuffer = textureManager.LoadPixelBuffer(
502 FittingMode::SCALE_TO_FILL,
503 SamplingMode::BOX_THEN_LINEAR,
509 DALI_TEST_EQUALS( observer2.mLoaded, false, TEST_LOCATION );
510 DALI_TEST_EQUALS( observer2.mObserverCalled, false, TEST_LOCATION );
512 application.SendNotification();
513 application.Render();
515 DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
517 application.SendNotification();
518 application.Render();
520 DALI_TEST_EQUALS( observer2.mLoaded, true, TEST_LOCATION );
521 DALI_TEST_EQUALS( observer2.mObserverCalled, true, TEST_LOCATION );
522 DALI_TEST_EQUALS( observer2.mCompleteType, TestObserver::CompleteType::LOAD_COMPLETE, TEST_LOCATION );
527 int UtcTextureManagerUseInvalidMask(void)
529 ToolkitTestApplication application;
530 tet_infoline( "UtcTextureManagerUseInvalidMask" );
532 TextureManager textureManager; // Create new texture manager
534 TestObserver observer;
535 std::string filename( TEST_IMAGE_FILE_NAME );
536 std::string maskname("");
537 TextureManager::MaskingDataPointer maskInfo = nullptr;
538 maskInfo.reset(new TextureManager::MaskingData());
539 maskInfo->mAlphaMaskUrl = maskname;
540 maskInfo->mAlphaMaskId = TextureManager::INVALID_TEXTURE_ID;
541 maskInfo->mCropToMask = true;
542 maskInfo->mContentScaleFactor = 1.0f;
544 auto textureId( TextureManager::INVALID_TEXTURE_ID );
545 Vector4 atlasRect( 0.f, 0.f, 1.f, 1.f );
546 Dali::ImageDimensions atlasRectSize( 0,0 );
547 bool synchronousLoading(false);
548 bool atlasingStatus(false);
549 bool loadingStatus(false);
550 auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
551 ImageAtlasManagerPtr atlasManager = nullptr;
552 Toolkit::AtlasUploadObserver* atlasUploadObserver = nullptr;
554 textureManager.LoadTexture(
557 FittingMode::SCALE_TO_FILL,
558 SamplingMode::BOX_THEN_LINEAR,
572 TextureManager::ReloadPolicy::CACHED,
576 DALI_TEST_EQUALS( observer.mLoaded, false, TEST_LOCATION );
577 DALI_TEST_EQUALS( observer.mObserverCalled, false, TEST_LOCATION );
579 application.SendNotification();
580 application.Render();
582 DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
584 application.SendNotification();
585 application.Render();
587 DALI_TEST_EQUALS( observer.mLoaded, true, TEST_LOCATION );
588 DALI_TEST_EQUALS( observer.mObserverCalled, true, TEST_LOCATION );
589 DALI_TEST_EQUALS( observer.mCompleteType, TestObserver::CompleteType::UPLOAD_COMPLETE, TEST_LOCATION );