1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef FLUTTER_LIB_UI_PAINTING_IMAGE_DECODER_H_
6#define FLUTTER_LIB_UI_PAINTING_IMAGE_DECODER_H_
7
8#include <memory>
9#include <optional>
10
11#include "flutter/common/task_runners.h"
12#include "flutter/flow/skia_gpu_object.h"
13#include "flutter/fml/concurrent_message_loop.h"
14#include "flutter/fml/macros.h"
15#include "flutter/fml/mapping.h"
16#include "flutter/fml/trace_event.h"
17#include "flutter/lib/ui/io_manager.h"
18#include "flutter/lib/ui/painting/image_descriptor.h"
19#include "third_party/skia/include/core/SkData.h"
20#include "third_party/skia/include/core/SkImage.h"
21#include "third_party/skia/include/core/SkImageInfo.h"
22#include "third_party/skia/include/core/SkRefCnt.h"
23#include "third_party/skia/include/core/SkSize.h"
24
25namespace flutter {
26
27// An object that coordinates image decompression and texture upload across
28// multiple threads/components in the shell. This object must be created,
29// accessed and collected on the UI thread (typically the engine or its runtime
30// controller). None of the expensive operations performed by this component
31// occur in a frame pipeline.
32class ImageDecoder {
33 public:
34 ImageDecoder(
35 TaskRunners runners,
36 std::shared_ptr<fml::ConcurrentTaskRunner> concurrent_task_runner,
37 fml::WeakPtr<IOManager> io_manager);
38
39 ~ImageDecoder();
40
41 using ImageResult = std::function<void(SkiaGPUObject<SkImage>)>;
42
43 // Takes an image descriptor and returns a handle to a texture resident on the
44 // GPU. All image decompression and resizes are done on a worker thread
45 // concurrently. Texture upload is done on the IO thread and the result
46 // returned back on the UI thread. On error, the texture is null but the
47 // callback is guaranteed to return on the UI thread.
48 void Decode(fml::RefPtr<ImageDescriptor> descriptor,
49 uint32_t target_width,
50 uint32_t target_height,
51 const ImageResult& result);
52
53 fml::WeakPtr<ImageDecoder> GetWeakPtr() const;
54
55 private:
56 TaskRunners runners_;
57 std::shared_ptr<fml::ConcurrentTaskRunner> concurrent_task_runner_;
58 fml::WeakPtr<IOManager> io_manager_;
59 fml::WeakPtrFactory<ImageDecoder> weak_factory_;
60
61 FML_DISALLOW_COPY_AND_ASSIGN(ImageDecoder);
62};
63
64sk_sp<SkImage> ImageFromCompressedData(fml::RefPtr<ImageDescriptor> descriptor,
65 uint32_t target_width,
66 uint32_t target_height,
67 const fml::tracing::TraceFlow& flow);
68
69} // namespace flutter
70
71#endif // FLUTTER_LIB_UI_PAINTING_IMAGE_DECODER_H_
72