File: mappable_buffer_dxgi.h

package info (click to toggle)
chromium 144.0.7559.109-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 5,915,868 kB
  • sloc: cpp: 35,866,215; ansic: 7,599,035; javascript: 3,623,761; python: 1,639,407; xml: 833,084; asm: 716,173; pascal: 185,323; sh: 88,763; perl: 88,699; objc: 79,984; sql: 58,217; cs: 42,430; fortran: 24,101; makefile: 20,747; tcl: 15,277; php: 14,022; yacc: 9,059; ruby: 7,553; awk: 3,720; lisp: 3,233; lex: 1,330; ada: 727; jsp: 228; sed: 36
file content (154 lines) | stat: -rw-r--r-- 6,030 bytes parent folder | download | duplicates (7)
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
// Copyright 2017 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_CLIENT_INTERNAL_MAPPABLE_BUFFER_DXGI_H_
#define GPU_COMMAND_BUFFER_CLIENT_INTERNAL_MAPPABLE_BUFFER_DXGI_H_

#include <stddef.h>
#include <stdint.h>

#include <memory>
#include <optional>

#include "base/containers/span.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/raw_span.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/unsafe_shared_memory_pool.h"
#include "base/memory/unsafe_shared_memory_region.h"
#include "base/unguessable_token.h"
#include "base/win/scoped_handle.h"
#include "components/viz/common/resources/shared_image_format.h"
#include "gpu/command_buffer/client/gpu_command_buffer_client_export.h"
#include "gpu/command_buffer/client/internal/mappable_buffer.h"
#include "ui/gfx/color_space.h"

namespace gpu {

class ClientSharedImage;

// Implementation of MappableBuffer based on dxgi textures.
class GPU_COMMAND_BUFFER_CLIENT_EXPORT MappableBufferDXGI
    : public MappableBuffer {
 public:
  MappableBufferDXGI(const MappableBufferDXGI&) = delete;
  MappableBufferDXGI& operator=(const MappableBufferDXGI&) = delete;

  ~MappableBufferDXGI() override;

  static constexpr gfx::GpuMemoryBufferType kBufferType =
      gfx::DXGI_SHARED_HANDLE;

  static std::unique_ptr<MappableBufferDXGI> CreateFromHandleForTesting(
      gfx::GpuMemoryBufferHandle handle,
      const gfx::Size& size,
      viz::SharedImageFormat format) {
    return CreateFromHandle(std::move(handle), size, format);
  }

  static base::OnceClosure AllocateForTesting(
      const gfx::Size& size,
      viz::SharedImageFormat format,
      gfx::BufferUsage usage,
      gfx::GpuMemoryBufferHandle* handle);

  bool Map() override;
  void MapAsync(base::OnceCallback<void(bool)> result_cb) override;
  bool AsyncMappingIsNonBlocking() const override;
  void* memory(size_t plane) override;
  void Unmap() override;
  int stride(size_t plane) const override;
  gfx::GpuMemoryBufferType GetType() const override;
  gfx::GpuMemoryBufferHandle CloneHandle() const override;

  // This method allows clients to explicitly specify that it wants to use the
  // |premapped_memory_| which is internally created by this class from the GMB
  // handle shared memory |region_| instead of client providing it.
  // This method is also used when the clients wants to specify that it no
  // longer should be using the pre mapped memory.
  // Windows media capture code will need to use this api in order to transition
  // to MappableSI. It is currently not used until the media capture code is
  // converted to MappableSI.
  void SetUsePreMappedMemory(bool use_premapped_memory) override;

  gfx::GpuMemoryBufferHandle CloneHandleWithRegion(
      base::UnsafeSharedMemoryRegion region) const;

  HANDLE GetHandle() const;
  const gfx::DXGIHandleToken& GetToken() const;

 private:
  friend ClientSharedImage;

  static std::unique_ptr<MappableBufferDXGI> CreateFromHandle(
      gfx::GpuMemoryBufferHandle handle,
      const gfx::Size& size,
      viz::SharedImageFormat format,
      CopyNativeBufferToShMemCallback copy_native_buffer_to_shmem_callback =
          CopyNativeBufferToShMemCallback(),
      scoped_refptr<base::UnsafeSharedMemoryPool> pool = nullptr);

  MappableBufferDXGI(
      const gfx::Size& size,
      viz::SharedImageFormat format,
      gfx::DXGIHandle dxgi_handle,
      CopyNativeBufferToShMemCallback copy_native_buffer_to_shmem_callback,
      scoped_refptr<base::UnsafeSharedMemoryPool> pool);

  // Returns callback for reporting early result.
  // `DoMapAsync` can't invoke it directly as it holds a mapping lock.
  std::optional<base::OnceCallback<void(void)>> DoMapAsync(
      base::OnceCallback<void(bool)>);
  void CheckAsyncMapResult(bool result);
  void AssertMapped();

  const gfx::Size size_;
  const viz::SharedImageFormat format_;

  // This is currently always set to false until media capture code is converted
  // to use MappableSI.
  bool use_premapped_memory_ = false;

  // The DXGIHandle's |region_| is not used currently. It will be eventually be
  // used to pre-map the |region_| internally in this class instead of clients
  // doing the pre-map.
  gfx::DXGIHandle dxgi_handle_;

  // It is created when |region_| is mapped via Map(). It is to keep the
  // |premapped_memory_| alive till its being used. Destroying this makes the
  // |premapped_memory_| invalid in cases where |premapped_memory_| is created
  // from it.
  base::WritableSharedMemoryMapping region_mapping_;

  CopyNativeBufferToShMemCallback copy_native_buffer_to_shmem_callback_;

  // Note: This lock must be held throughout the entirety of the Map() and
  // Unmap() operations to avoid corrupt mutation across multiple threads.
  base::Lock map_lock_;
  uint32_t map_count_ GUARDED_BY(map_lock_) = 0u;

  std::vector<base::OnceCallback<void(bool)>> map_callbacks_
      GUARDED_BY(map_lock_);

  // Used to create and store shared memory for data, copied via request to
  // gpu process.
  scoped_refptr<base::UnsafeSharedMemoryPool> shared_memory_pool_;
  std::unique_ptr<base::UnsafeSharedMemoryPool::Handle> shared_memory_handle_;

  // When |use_premapped_memory_| is false, the caller(which is the video
  // capturer in the render process) maps a memory region and
  // |premapped_memory_| is a view of that region owned by the caller.
  // When |use_premapped_memory_| is true, caller provides |region_| which is a
  // handle to a memory region. This region is mapped via |region_|.Map()
  // which maps the memory region and returns the handle called
  // |region_mapping_| to that mapping. |premapped_memory_| is now the view of
  // |region_mapping_| in this case.
  base::raw_span<uint8_t> premapped_memory_;
  bool async_mapping_in_progress_ GUARDED_BY(map_lock_) = false;
};

}  // namespace gpu

#endif  // GPU_COMMAND_BUFFER_CLIENT_INTERNAL_MAPPABLE_BUFFER_DXGI_H_