File: shared_image_manager.h

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (197 lines) | stat: -rw-r--r-- 7,294 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef GPU_COMMAND_BUFFER_SERVICE_SHARED_IMAGE_SHARED_IMAGE_MANAGER_H_
#define GPU_COMMAND_BUFFER_SERVICE_SHARED_IMAGE_SHARED_IMAGE_MANAGER_H_

#include <optional>

#include "base/memory/scoped_refptr.h"
#include "base/synchronization/lock.h"
#include "base/threading/thread_checker.h"
#include "base/trace_event/memory_dump_provider.h"
#include "build/build_config.h"
#include "gpu/command_buffer/common/mailbox.h"
#include "gpu/command_buffer/common/shared_image_usage.h"
#include "gpu/command_buffer/service/shared_image/shared_image_backing.h"
#include "gpu/gpu_gles2_export.h"
#include "gpu/vulkan/buildflags.h"
#include "third_party/abseil-cpp/absl/container/flat_hash_map.h"

#if BUILDFLAG(IS_WIN)
namespace gfx {
class D3DSharedFence;
}
#endif

namespace gpu {
class DXGISharedHandleManager;
class SharedImageRepresentationFactoryRef;

class GPU_GLES2_EXPORT SharedImageManager
    : public base::trace_event::MemoryDumpProvider {
 public:
  // If |thread_safe| is set, the manager itself can be safely accessed from
  // other threads but the backings themselves may not be thread-safe so
  // representations should not be created on other threads. When
  // |display_context_on_another_thread| is set, we make sure that all
  // SharedImages that will be used in the display context have thread-safe
  // backings and therefore it is safe to create representations on the thread
  // that holds the display context.
  explicit SharedImageManager(bool thread_safe = false,
                              bool display_context_on_another_thread = false);

  SharedImageManager(const SharedImageManager&) = delete;
  SharedImageManager& operator=(const SharedImageManager&) = delete;

  ~SharedImageManager() override;

#if BUILDFLAG(IS_OZONE)
  void SetSupportsOverlays(bool supports_overlays) {
    supports_overlays_on_ozone_ = supports_overlays;
  }
#endif

  // base::trace_event::MemoryDumpProvider implementation:
  bool OnMemoryDump(const base::trace_event::MemoryDumpArgs& args,
                    base::trace_event::ProcessMemoryDump* pmd) override;

  // Registers a SharedImageBacking with the manager and returns a
  // SharedImageRepresentationFactoryRef which holds a ref on the SharedImage.
  // The factory should delete this object to release the ref.
  std::unique_ptr<SharedImageRepresentationFactoryRef> Register(
      std::unique_ptr<SharedImageBacking> backing,
      MemoryTypeTracker* ref);

  std::unique_ptr<SharedImageRepresentationFactoryRef> AddSecondaryReference(
      const Mailbox& mailbox,
      MemoryTypeTracker* tracker);

  // Accessors which return a SharedImageRepresentation. Representations also
  // take a ref on the mailbox, releasing it when the representation is
  // destroyed.
  std::unique_ptr<GLTextureImageRepresentation> ProduceGLTexture(
      const Mailbox& mailbox,
      MemoryTypeTracker* ref);
  std::unique_ptr<GLTexturePassthroughImageRepresentation>
  ProduceGLTexturePassthrough(const Mailbox& mailbox, MemoryTypeTracker* ref);
  std::unique_ptr<SkiaImageRepresentation> ProduceSkia(
      const Mailbox& mailbox,
      MemoryTypeTracker* ref,
      scoped_refptr<SharedContextState> context_state);

  // ProduceDawn must also be called using same |device| if
  // using the same |mailbox|. This is because the underlying shared image
  // compatibility also depends on the WGPUAdapter which ProduceDawn does not
  // associate with the representation.
  // TODO(crbug.com/40730564): Revisit this in the future for WebGPU
  // multi-adapter support.
  std::unique_ptr<DawnImageRepresentation> ProduceDawn(
      const Mailbox& mailbox,
      MemoryTypeTracker* ref,
      const wgpu::Device& device,
      wgpu::BackendType backend_type,
      std::vector<wgpu::TextureFormat> view_formats,
      scoped_refptr<SharedContextState> context_state);
  std::unique_ptr<DawnBufferRepresentation> ProduceDawnBuffer(
      const Mailbox& mailbox,
      MemoryTypeTracker* ref,
      const wgpu::Device& device,
      wgpu::BackendType backend_type);
  std::unique_ptr<OverlayImageRepresentation> ProduceOverlay(
      const Mailbox& mailbox,
      MemoryTypeTracker* ref);
  std::unique_ptr<MemoryImageRepresentation> ProduceMemory(
      const Mailbox& mailbox,
      MemoryTypeTracker* ref);
  std::unique_ptr<RasterImageRepresentation> ProduceRaster(
      const Mailbox& mailbox,
      MemoryTypeTracker* ref);
  std::unique_ptr<VideoImageRepresentation> ProduceVideo(
      VideoDevice device,
      const Mailbox& mailbox,
      MemoryTypeTracker* ref);

#if BUILDFLAG(ENABLE_VULKAN)
  std::unique_ptr<VulkanImageRepresentation> ProduceVulkan(
      const Mailbox& mailbox,
      MemoryTypeTracker* ref,
      gpu::VulkanDeviceQueue* vulkan_device_queue,
      gpu::VulkanImplementation& vulkan_impl,
      bool needs_detiling);
#endif

#if BUILDFLAG(IS_ANDROID)
  std::unique_ptr<LegacyOverlayImageRepresentation> ProduceLegacyOverlay(
      const Mailbox& mailbox,
      MemoryTypeTracker* ref);
#endif

#if BUILDFLAG(IS_WIN)
  void UpdateExternalFence(const Mailbox& mailbox,
                           scoped_refptr<gfx::D3DSharedFence> external_fence);
#endif

  // Provides the usage flags supported by the given |mailbox|. Returns nullopt
  // if no backing is registered for `mailbox`.
  std::optional<SharedImageUsageSet> GetUsageForMailbox(const Mailbox& mailbox);

  // Called by SharedImageRepresentation in the destructor.
  void OnRepresentationDestroyed(const Mailbox& mailbox,
                                 SharedImageRepresentation* representation);

  void SetPurgeable(const Mailbox& mailbox, bool purgeable);

  bool is_thread_safe() const { return !!lock_; }

  bool display_context_on_another_thread() const {
    return display_context_on_another_thread_;
  }

  bool SupportsScanoutImages();

  // Returns the NativePixmap backing |mailbox|. Returns null if the SharedImage
  // doesn't exist or is not backed by a NativePixmap. The caller is not
  // expected to read from or write into the provided NativePixmap because it
  // can be modified by the client at any time. The primary purpose of this
  // method is to facilitate pageflip testing on the viz thread.
  scoped_refptr<gfx::NativePixmap> GetNativePixmap(const gpu::Mailbox& mailbox);

#if BUILDFLAG(IS_WIN)
  const scoped_refptr<DXGISharedHandleManager>& dxgi_shared_handle_manager()
      const {
    return dxgi_shared_handle_manager_;
  }
#endif

 private:
  class AutoLock;

  SharedImageBacking* GetBacking(const gpu::Mailbox& mailbox) const
      EXCLUSIVE_LOCKS_REQUIRED(lock_);

  // The lock for protecting |images_|.
  std::optional<base::Lock> lock_;

  absl::flat_hash_map<gpu::Mailbox, std::unique_ptr<SharedImageBacking>> images_
      GUARDED_BY(lock_);

  const bool display_context_on_another_thread_;

  bool is_registered_as_memory_dump_provider_ = false;

#if BUILDFLAG(IS_WIN)
  scoped_refptr<DXGISharedHandleManager> dxgi_shared_handle_manager_;
#endif

#if BUILDFLAG(IS_OZONE)
  bool supports_overlays_on_ozone_ = false;
#endif

  THREAD_CHECKER(thread_checker_);
};

}  // namespace gpu

#endif  // GPU_COMMAND_BUFFER_SERVICE_SHARED_IMAGE_SHARED_IMAGE_MANAGER_H_