Flutter Engine
The Flutter Engine
display_list_deferred_image_gpu_skia.h
Go to the documentation of this file.
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_DISPLAY_LIST_DEFERRED_IMAGE_GPU_SKIA_H_
6#define FLUTTER_LIB_UI_PAINTING_DISPLAY_LIST_DEFERRED_IMAGE_GPU_SKIA_H_
7
8#if !SLIMPELLER
9
10#include <memory>
11#include <mutex>
12
13#include "flutter/common/graphics/texture.h"
14#include "flutter/display_list/display_list.h"
15#include "flutter/display_list/image/dl_image.h"
16#include "flutter/flow/layers/layer_tree.h"
17#include "flutter/fml/macros.h"
18#include "flutter/fml/memory/weak_ptr.h"
19#include "flutter/lib/ui/io_manager.h"
20#include "flutter/lib/ui/snapshot_delegate.h"
21
23
24namespace flutter {
25
26class DlDeferredImageGPUSkia final : public DlImage {
27 public:
29 const SkImageInfo& image_info,
30 sk_sp<DisplayList> display_list,
32 const fml::RefPtr<fml::TaskRunner>& raster_task_runner,
33 fml::RefPtr<SkiaUnrefQueue> unref_queue);
34
36 const SkImageInfo& image_info,
37 std::unique_ptr<LayerTree> layer_tree,
39 const fml::RefPtr<fml::TaskRunner>& raster_task_runner,
40 fml::RefPtr<SkiaUnrefQueue> unref_queue);
41
42 // |DlImage|
43 ~DlDeferredImageGPUSkia() override;
44
45 // |DlImage|
46 // This method is only safe to call from the raster thread.
47 // Callers must not hold long term references to this image and
48 // only use it for the immediate painting operation. It must be
49 // collected on the raster task runner.
50 sk_sp<SkImage> skia_image() const override;
51
52 // |DlImage|
53 std::shared_ptr<impeller::Texture> impeller_texture() const override;
54
55 // |DlImage|
56 bool isOpaque() const override;
57
58 // |DlImage|
59 bool isTextureBacked() const override;
60
61 // |DlImage|
62 bool isUIThreadSafe() const override;
63
64 // |DlImage|
65 SkISize dimensions() const override;
66
67 // |DlImage|
68 virtual size_t GetApproximateByteSize() const override;
69
70 // |DlImage|
71 // This method is safe to call from any thread.
72 std::optional<std::string> get_error() const override;
73
74 // |DlImage|
75 OwningContext owning_context() const override {
77 }
78
79 private:
80 class ImageWrapper final : public std::enable_shared_from_this<ImageWrapper>,
81 public ContextListener {
82 public:
83 static std::shared_ptr<ImageWrapper> Make(
84 const SkImageInfo& image_info,
85 sk_sp<DisplayList> display_list,
87 fml::RefPtr<fml::TaskRunner> raster_task_runner,
88 fml::RefPtr<SkiaUnrefQueue> unref_queue);
89
90 static std::shared_ptr<ImageWrapper> MakeFromLayerTree(
91 const SkImageInfo& image_info,
92 std::unique_ptr<LayerTree> layer_tree,
94 fml::RefPtr<fml::TaskRunner> raster_task_runner,
95 fml::RefPtr<SkiaUnrefQueue> unref_queue);
96
97 const SkImageInfo image_info() const { return image_info_; }
98 const GrBackendTexture& texture() const { return texture_; }
99 bool isTextureBacked() const;
100 std::optional<std::string> get_error();
101 sk_sp<SkImage> CreateSkiaImage() const;
102 void Unregister();
103 void DeleteTexture();
104
105 private:
106 const SkImageInfo image_info_;
107 sk_sp<DisplayList> display_list_;
109 fml::RefPtr<fml::TaskRunner> raster_task_runner_;
110 fml::RefPtr<SkiaUnrefQueue> unref_queue_;
111 std::shared_ptr<TextureRegistry> texture_registry_;
112
113 mutable std::mutex error_mutex_;
114 std::optional<std::string> error_;
115
116 GrBackendTexture texture_;
117 sk_sp<GrDirectContext> context_;
118 // May be used if this image is not texture backed.
119 sk_sp<SkImage> image_;
120
121 ImageWrapper(
122 const SkImageInfo& image_info,
123 sk_sp<DisplayList> display_list,
125 fml::RefPtr<fml::TaskRunner> raster_task_runner,
126 fml::RefPtr<SkiaUnrefQueue> unref_queue);
127
128 // If a layer tree is provided, it will be flattened during the raster
129 // thread task spawned by this method. After being flattened into a display
130 // list, the image wrapper will be updated to hold this display list and the
131 // layer tree can be dropped.
132 void SnapshotDisplayList(std::unique_ptr<LayerTree> layer_tree = nullptr);
133
134 // |ContextListener|
135 void OnGrContextCreated() override;
136
137 // |ContextListener|
138 void OnGrContextDestroyed() override;
139 };
140
141 const std::shared_ptr<ImageWrapper> image_wrapper_;
142
143 fml::RefPtr<fml::TaskRunner> raster_task_runner_;
144
145 DlDeferredImageGPUSkia(std::shared_ptr<ImageWrapper> image_wrapper,
146 fml::RefPtr<fml::TaskRunner> raster_task_runner);
147
148 FML_DISALLOW_COPY_AND_ASSIGN(DlDeferredImageGPUSkia);
149};
150
151} // namespace flutter
152
153#endif // !SLIMPELLER
154
155#endif // FLUTTER_LIB_UI_PAINTING_DISPLAY_LIST_DEFERRED_IMAGE_GPU_SKIA_H_
sk_sp< SkImage > skia_image() const override
If this display list image is meant to be used by the Skia backend, an SkImage instance....
std::optional< std::string > get_error() const override
static sk_sp< DlDeferredImageGPUSkia > Make(const SkImageInfo &image_info, sk_sp< DisplayList > display_list, fml::TaskRunnerAffineWeakPtr< SnapshotDelegate > snapshot_delegate, const fml::RefPtr< fml::TaskRunner > &raster_task_runner, fml::RefPtr< SkiaUnrefQueue > unref_queue)
std::shared_ptr< impeller::Texture > impeller_texture() const override
If this display list image is meant to be used by the Impeller backend, an Impeller texture instance....
bool isOpaque() const override
If the pixel format of this image ignores alpha, this returns true. This method might conservatively ...
static sk_sp< DlDeferredImageGPUSkia > MakeFromLayerTree(const SkImageInfo &image_info, std::unique_ptr< LayerTree > layer_tree, fml::TaskRunnerAffineWeakPtr< SnapshotDelegate > snapshot_delegate, const fml::RefPtr< fml::TaskRunner > &raster_task_runner, fml::RefPtr< SkiaUnrefQueue > unref_queue)
bool isUIThreadSafe() const override
If the underlying platform image held by this object has no threading requirements for the release of...
virtual size_t GetApproximateByteSize() const override
Represents an image whose allocation is (usually) resident on device memory.
Definition: dl_image.h:30
fml::TaskRunnerAffineWeakPtr< SnapshotDelegate > snapshot_delegate_
FlTexture * texture
Definition: SkSize.h:16