Flutter Engine
flutter::ImageDecoder Class Reference

#include <image_decoder.h>

Public Types

using ImageResult = std::function< void(SkiaGPUObject< SkImage >)>
 

Public Member Functions

 ImageDecoder (TaskRunners runners, std::shared_ptr< fml::ConcurrentTaskRunner > concurrent_task_runner, fml::WeakPtr< IOManager > io_manager)
 
 ~ImageDecoder ()
 
void Decode (fml::RefPtr< ImageDescriptor > descriptor, uint32_t target_width, uint32_t target_height, const ImageResult &result)
 
fml::WeakPtr< ImageDecoderGetWeakPtr () const
 

Detailed Description

Definition at line 32 of file image_decoder.h.

Member Typedef Documentation

◆ ImageResult

Definition at line 41 of file image_decoder.h.

Constructor & Destructor Documentation

◆ ImageDecoder()

flutter::ImageDecoder::ImageDecoder ( TaskRunners  runners,
std::shared_ptr< fml::ConcurrentTaskRunner concurrent_task_runner,
fml::WeakPtr< IOManager io_manager 
)

Definition at line 14 of file image_decoder.cc.

References FML_DCHECK, flutter::TaskRunners::GetUITaskRunner(), flutter::TaskRunners::IsValid(), fml::TaskRunner::RunsTasksOnCurrentThread(), and ~ImageDecoder().

18  : runners_(std::move(runners)),
19  concurrent_task_runner_(std::move(concurrent_task_runner)),
20  io_manager_(std::move(io_manager)),
21  weak_factory_(this) {
22  FML_DCHECK(runners_.IsValid());
24  << "The image decoder must be created & collected on the UI thread.";
25 }
bool IsValid() const
Definition: task_runners.cc:46
#define FML_DCHECK(condition)
Definition: logging.h:86
fml::RefPtr< fml::TaskRunner > GetUITaskRunner() const
Definition: task_runners.cc:34
virtual bool RunsTasksOnCurrentThread()
Definition: task_runner.cc:43

◆ ~ImageDecoder()

flutter::ImageDecoder::~ImageDecoder ( )
default

Referenced by ImageDecoder().

Member Function Documentation

◆ Decode()

void flutter::ImageDecoder::Decode ( fml::RefPtr< ImageDescriptor descriptor,
uint32_t  target_width,
uint32_t  target_height,
const ImageResult result 
)

Definition at line 217 of file image_decoder.cc.

References fml::tracing::TraceFlow::End(), FML_DCHECK, FML_LOG, flutter::TaskRunners::GetIOTaskRunner(), flutter::TaskRunners::GetUITaskRunner(), flutter::ImageFromCompressedData(), flutter::ImageFromDecompressedData(), fml::MakeCopyable(), fml::TaskRunner::RunsTasksOnCurrentThread(), TRACE_EVENT0, and flutter::UploadRasterImage().

220  {
221  TRACE_EVENT0("flutter", __FUNCTION__);
222  fml::tracing::TraceFlow flow(__FUNCTION__);
223 
224  FML_DCHECK(callback);
226 
227  // Always service the callback on the UI thread.
228  auto result = [callback, ui_runner = runners_.GetUITaskRunner()](
229  SkiaGPUObject<SkImage> image,
231  ui_runner->PostTask(fml::MakeCopyable(
232  [callback, image = std::move(image), flow = std::move(flow)]() mutable {
233  // We are going to terminate the trace flow here. Flows cannot
234  // terminate without a base trace. Add one explicitly.
235  TRACE_EVENT0("flutter", "ImageDecodeCallback");
236  flow.End();
237  callback(std::move(image));
238  }));
239  };
240 
241  if (!descriptor->data() || descriptor->data()->size() == 0) {
242  result({}, std::move(flow));
243  return;
244  }
245 
246  concurrent_task_runner_->PostTask(
247  fml::MakeCopyable([descriptor, //
248  io_manager = io_manager_, //
249  io_runner = runners_.GetIOTaskRunner(), //
250  result, //
251  target_width = target_width, //
252  target_height = target_height, //
253  flow = std::move(flow) //
254  ]() mutable {
255  // Step 1: Decompress the image.
256  // On Worker.
257 
258  auto decompressed =
259  descriptor->is_compressed()
260  ? ImageFromCompressedData(std::move(descriptor), //
261  target_width, //
262  target_height, //
263  flow)
264  : ImageFromDecompressedData(std::move(descriptor), //
265  target_width, //
266  target_height, //
267  flow);
268 
269  if (!decompressed) {
270  FML_LOG(ERROR) << "Could not decompress image.";
271  result({}, std::move(flow));
272  return;
273  }
274 
275  // Step 2: Update the image to the GPU.
276  // On IO Thread.
277 
278  io_runner->PostTask(fml::MakeCopyable([io_manager, decompressed, result,
279  flow =
280  std::move(flow)]() mutable {
281  if (!io_manager) {
282  FML_LOG(ERROR) << "Could not acquire IO manager.";
283  return result({}, std::move(flow));
284  }
285 
286  // If the IO manager does not have a resource context, the caller
287  // might not have set one or a software backend could be in use.
288  // Either way, just return the image as-is.
289  if (!io_manager->GetResourceContext()) {
290  result({std::move(decompressed), io_manager->GetSkiaUnrefQueue()},
291  std::move(flow));
292  return;
293  }
294 
295  auto uploaded =
296  UploadRasterImage(std::move(decompressed), io_manager, flow);
297 
298  if (!uploaded.get()) {
299  FML_LOG(ERROR) << "Could not upload image to the GPU.";
300  result({}, std::move(flow));
301  return;
302  }
303 
304  // Finally, all done.
305  result(std::move(uploaded), std::move(flow));
306  }));
307  }));
308 }
static SkiaGPUObject< SkImage > UploadRasterImage(sk_sp< SkImage > image, fml::WeakPtr< IOManager > io_manager, const fml::tracing::TraceFlow &flow)
#define TRACE_EVENT0(category_group, name)
Definition: trace_event.h:75
#define FML_DCHECK(condition)
Definition: logging.h:86
Definition: ref_ptr.h:252
#define FML_LOG(severity)
Definition: logging.h:65
internal::CopyableLambda< T > MakeCopyable(T lambda)
Definition: make_copyable.h:57
static sk_sp< SkImage > ImageFromDecompressedData(fml::RefPtr< ImageDescriptor > descriptor, uint32_t target_width, uint32_t target_height, const fml::tracing::TraceFlow &flow)
fml::RefPtr< fml::TaskRunner > GetIOTaskRunner() const
Definition: task_runners.cc:38
fml::RefPtr< fml::TaskRunner > GetUITaskRunner() const
Definition: task_runners.cc:34
virtual bool RunsTasksOnCurrentThread()
Definition: task_runner.cc:43
sk_sp< SkImage > ImageFromCompressedData(fml::RefPtr< ImageDescriptor > descriptor, uint32_t target_width, uint32_t target_height, const fml::tracing::TraceFlow &flow)

◆ GetWeakPtr()

fml::WeakPtr< ImageDecoder > flutter::ImageDecoder::GetWeakPtr ( ) const

Definition at line 310 of file image_decoder.cc.

Referenced by flutter::Engine::Engine().

310  {
311  return weak_factory_.GetWeakPtr();
312 }

The documentation for this class was generated from the following files: