• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/lib/ui/io_manager.h"
17 #include "third_party/skia/include/core/SkData.h"
18 #include "third_party/skia/include/core/SkImage.h"
19 #include "third_party/skia/include/core/SkImageInfo.h"
20 #include "third_party/skia/include/core/SkRefCnt.h"
21 #include "third_party/skia/include/core/SkSize.h"
22 
23 namespace flutter {
24 
25 // An object that coordinates image decompression and texture upload across
26 // multiple threads/components in the shell. This object must be created,
27 // accessed and collected on the UI thread (typically the engine or its runtime
28 // controller). None of the expensive operations performed by this component
29 // occur in a frame pipeline.
30 class ImageDecoder {
31  public:
32   ImageDecoder(
33       TaskRunners runners,
34       std::shared_ptr<fml::ConcurrentTaskRunner> concurrent_task_runner,
35       fml::WeakPtr<IOManager> io_manager);
36 
37   ~ImageDecoder();
38 
39   struct ImageInfo {
40     SkImageInfo sk_info = {};
41     size_t row_bytes = 0;
42   };
43 
44   struct ImageDescriptor {
45     sk_sp<SkData> data;
46     std::optional<ImageInfo> decompressed_image_info;
47     std::optional<uint32_t> target_width;
48     std::optional<uint32_t> target_height;
49   };
50 
51   using ImageResult = std::function<void(SkiaGPUObject<SkImage>)>;
52 
53   // Takes an image descriptor and returns a handle to a texture resident on the
54   // GPU. All image decompression and resizes are done on a worker thread
55   // concurrently. Texture upload is done on the IO thread and the result
56   // returned back on the UI thread. On error, the texture is null but the
57   // callback is guaranteed to return on the UI thread.
58   void Decode(ImageDescriptor descriptor, ImageResult result);
59 
60   fml::WeakPtr<ImageDecoder> GetWeakPtr() const;
61 
62  private:
63   TaskRunners runners_;
64   std::shared_ptr<fml::ConcurrentTaskRunner> concurrent_task_runner_;
65   fml::WeakPtr<IOManager> io_manager_;
66   fml::WeakPtrFactory<ImageDecoder> weak_factory_;
67 
68   FML_DISALLOW_COPY_AND_ASSIGN(ImageDecoder);
69 };
70 
71 }  // namespace flutter
72 
73 #endif  // FLUTTER_LIB_UI_PAINTING_IMAGE_DECODER_H_
74