Fix memory leak
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / image-loader / image-load-thread.cpp
index c1b4a91..eba2eef 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -20,6 +20,9 @@
 
 // EXTERNAL INCLUDES
 #include <dali/devel-api/adaptor-framework/image-loading.h>
+#include <dali/devel-api/adaptor-framework/thread-settings.h>
+#include <dali/integration-api/adaptor-framework/adaptor.h>
+#include <dali/integration-api/debug.h>
 
 namespace Dali
 {
@@ -30,21 +33,69 @@ namespace Toolkit
 namespace Internal
 {
 
+LoadingTask::LoadingTask( uint32_t id, Dali::AnimatedImageLoading animatedImageLoading, uint32_t frameIndex )
+: pixelBuffer(),
+  url(),
+  id( id ),
+  dimensions(),
+  fittingMode(),
+  samplingMode(),
+  orientationCorrection(),
+  preMultiplyOnLoad( DevelAsyncImageLoader::PreMultiplyOnLoad::OFF ),
+  isMaskTask( false ),
+  maskPixelBuffer(),
+  contentScale( 1.0f ),
+  cropToMask( false ),
+  animatedImageLoading( animatedImageLoading ),
+  frameIndex( frameIndex )
+{
+}
+
 LoadingTask::LoadingTask( uint32_t id, const VisualUrl& url, ImageDimensions dimensions,
-                          FittingMode::Type fittingMode, SamplingMode::Type samplingMode, bool orientationCorrection )
+                          FittingMode::Type fittingMode, SamplingMode::Type samplingMode, bool orientationCorrection, DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad )
 : pixelBuffer(),
   url( url ),
   id( id ),
   dimensions( dimensions ),
   fittingMode( fittingMode ),
   samplingMode( samplingMode ),
-  orientationCorrection( orientationCorrection )
+  orientationCorrection( orientationCorrection ),
+  preMultiplyOnLoad( preMultiplyOnLoad ),
+  isMaskTask( false ),
+  maskPixelBuffer(),
+  contentScale( 1.0f ),
+  cropToMask( false ),
+  animatedImageLoading(),
+  frameIndex( 0u )
 {
 }
 
-void LoadingTask::Load()
+LoadingTask::LoadingTask( uint32_t id, Devel::PixelBuffer pixelBuffer, Devel::PixelBuffer maskPixelBuffer, float contentScale, bool cropToMask,
+                          DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad )
+: pixelBuffer( pixelBuffer ),
+  url( "" ),
+  id( id ),
+  dimensions(),
+  fittingMode(),
+  samplingMode(),
+  orientationCorrection(),
+  preMultiplyOnLoad( preMultiplyOnLoad ),
+  isMaskTask( true ),
+  maskPixelBuffer( maskPixelBuffer ),
+  contentScale( contentScale ),
+  cropToMask( cropToMask ),
+  animatedImageLoading(),
+  frameIndex( 0u )
 {
-  if( url.IsLocal() )
+}
+
+void LoadingTask::Load()
+{;
+  if( animatedImageLoading )
+  {
+    pixelBuffer = animatedImageLoading.LoadFrame( frameIndex );
+  }
+  else if( url.IsLocalResource() )
   {
     pixelBuffer = Dali::LoadImageFromFile( url.GetUrl(), dimensions, fittingMode, samplingMode, orientationCorrection );
   }
@@ -52,11 +103,32 @@ void LoadingTask::Load()
   {
     pixelBuffer = Dali::DownloadImageSynchronously ( url.GetUrl(), dimensions, fittingMode, samplingMode, orientationCorrection );
   }
+
+  if( !pixelBuffer )
+  {
+    DALI_LOG_ERROR( "LoadingTask::Load: Loading is failed: %s\n", url.GetUrl().c_str() );
+  }
 }
 
+void LoadingTask::ApplyMask()
+{
+  pixelBuffer.ApplyMask( maskPixelBuffer, contentScale, cropToMask );
+}
+
+void LoadingTask::MultiplyAlpha()
+{
+  if( pixelBuffer && Pixel::HasAlpha( pixelBuffer.GetPixelFormat() ) )
+  {
+    if( preMultiplyOnLoad == DevelAsyncImageLoader::PreMultiplyOnLoad::ON )
+    {
+      pixelBuffer.MultiplyColorByAlpha();
+    }
+  }
+}
 
 ImageLoadThread::ImageLoadThread( EventThreadCallback* trigger )
-: mTrigger( trigger )
+: mTrigger( trigger ),
+  mLogFactory( Dali::Adaptor::Get().GetLogFactory() )
 {
 }
 
@@ -68,13 +140,37 @@ ImageLoadThread::~ImageLoadThread()
   Join();
 
   delete mTrigger;
+
+  for( auto&& iter : mLoadQueue )
+  {
+    delete iter;
+  }
+  mLoadQueue.Clear();
+
+  for( auto&& iter : mCompleteQueue )
+  {
+    delete iter;
+  }
+  mCompleteQueue.Clear();
 }
 
 void ImageLoadThread::Run()
 {
+  SetThreadName( "ImageLoadThread" );
+  mLogFactory.InstallLogFunction();
+
   while( LoadingTask* task = NextTaskToProcess() )
   {
-    task->Load();
+    if( !task->isMaskTask )
+    {
+      task->Load();
+    }
+    else
+    {
+      task->ApplyMask();
+    }
+    task->MultiplyAlpha();
+
     AddCompletedTask( task );
   }
 }
@@ -82,7 +178,6 @@ void ImageLoadThread::Run()
 void ImageLoadThread::AddTask( LoadingTask* task )
 {
   bool wasEmpty = false;
-
   {
     // Lock while adding task to the queue
     ConditionalWait::ScopedLock lock( mConditionalWait );