File: gpu_memory_buffer_support.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 (175 lines) | stat: -rw-r--r-- 6,320 bytes parent folder | download | duplicates (3)
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
// Copyright 2016 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_IPC_COMMON_GPU_MEMORY_BUFFER_SUPPORT_H_
#define GPU_IPC_COMMON_GPU_MEMORY_BUFFER_SUPPORT_H_

#include <memory>
#include <unordered_set>

#include "base/containers/span.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/hash/hash.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/unsafe_shared_memory_pool.h"
#include "base/unguessable_token.h"
#include "build/build_config.h"
#include "gpu/ipc/common/gpu_ipc_common_export.h"
#include "gpu/ipc/common/gpu_memory_buffer_impl.h"
#include "ui/gfx/buffer_types.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/gpu_memory_buffer_handle.h"

#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_OZONE)
namespace gfx {
class ClientNativePixmapFactory;
}  // namespace gfx
#endif

namespace gpu {
using GpuMemoryBufferConfigurationKey = gfx::BufferUsageAndFormat;
using GpuMemoryBufferConfigurationSet =
    std::unordered_set<GpuMemoryBufferConfigurationKey>;
}  // namespace gpu

namespace std {
template <>
struct hash<gpu::GpuMemoryBufferConfigurationKey> {
  size_t operator()(const gpu::GpuMemoryBufferConfigurationKey& key) const {
    return base::HashInts(static_cast<int>(key.format),
                          static_cast<int>(key.usage));
  }
};
}  // namespace std

namespace arc {
class GpuArcVideoEncodeAccelerator;
}

namespace media {
class VaapiJpegEncodeAccelerator;
class V4L2JpegEncodeAccelerator;
}  // namespace media

namespace gpu {
class ClientSharedImage;
class GpuMemoryBufferManager;

// Provides a common factory for GPU memory buffer implementations.
class GPU_IPC_COMMON_EXPORT GpuMemoryBufferSupport {
 public:
  GpuMemoryBufferSupport();

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

  virtual ~GpuMemoryBufferSupport();

  // Returns the set of supported configurations.
  static GpuMemoryBufferConfigurationSet
  GetNativeGpuMemoryBufferConfigurations();

  // Returns whether the provided buffer format is supported.
  static bool IsNativeGpuMemoryBufferConfigurationSupportedForTesting(
      gfx::BufferFormat format,
      gfx::BufferUsage usage) {
    return IsNativeGpuMemoryBufferConfigurationSupported(format, usage);
  }

  // Returns whether the provided buffer format is supported.
  bool IsConfigurationSupportedForTest(gfx::GpuMemoryBufferType type,
                                       gfx::BufferFormat format,
                                       gfx::BufferUsage usage);

  // Creates a GpuMemoryBufferImpl from the given |handle| for VideoFrames.
  // |size| and |format| should match what was used to allocate the |handle|.
  // NOTE: DO NOT ADD ANY USAGES OF THIS METHOD.
  // TODO(crbug.com/40263579): Remove this method once all usages are
  // eliminated.
  std::unique_ptr<GpuMemoryBufferImpl>
  CreateGpuMemoryBufferImplFromHandleForVideoFrame(
      gfx::GpuMemoryBufferHandle handle,
      const gfx::Size& size,
      gfx::BufferFormat format,
      gfx::BufferUsage usage) {
    return CreateGpuMemoryBufferImplFromHandle(std::move(handle), size, format,
                                               usage, base::NullCallback());
  }

  std::unique_ptr<GpuMemoryBufferImpl>
  CreateGpuMemoryBufferImplFromHandleForTesting(
      gfx::GpuMemoryBufferHandle handle,
      const gfx::Size& size,
      gfx::BufferFormat format,
      gfx::BufferUsage usage,
      GpuMemoryBufferImpl::DestructionCallback callback) {
    return CreateGpuMemoryBufferImplFromHandle(std::move(handle), size, format,
                                               usage, std::move(callback));
  }

 private:
  // TODO(crbug.com/404905709): Eliminate these class' creation of GMBs and
  // remove this friending.
  friend class arc::GpuArcVideoEncodeAccelerator;
  friend class media::VaapiJpegEncodeAccelerator;
  friend class media::V4L2JpegEncodeAccelerator;

  // ClientSharedImage is the only entity that should be creating GMBs via
  // GpuMemoryBufferSupport.
  friend class ClientSharedImage;

  // Creates a GpuMemoryBufferImpl from the given |handle|. |size| and |format|
  // should match what was used to allocate the |handle|. |callback|, if
  // non-null, is called when instance is deleted, which is not necessarily on
  // the same thread as this function was called on and instance was created on.
  // |gpu_memory_buffer_manager| and |pool| are only needed if the created
  // buffer is a windows DXGI buffer and it needs to be mapped at the consumer.
  virtual std::unique_ptr<GpuMemoryBufferImpl>
  CreateGpuMemoryBufferImplFromHandle(
      gfx::GpuMemoryBufferHandle handle,
      const gfx::Size& size,
      gfx::BufferFormat format,
      gfx::BufferUsage usage,
      GpuMemoryBufferImpl::DestructionCallback callback,
      gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager = nullptr,
      scoped_refptr<base::UnsafeSharedMemoryPool> pool = nullptr);

  // Returns whether the provided buffer format is supported.
  static bool IsNativeGpuMemoryBufferConfigurationSupported(
      gfx::BufferFormat format,
      gfx::BufferUsage usage);

#if BUILDFLAG(IS_OZONE)
  std::unique_ptr<gfx::ClientNativePixmapFactory> client_native_pixmap_factory_;
#endif
};

// Helper class to manage allocated GMB info and to provide interface to dump
// the memory consumed by that GMB.
class GPU_IPC_COMMON_EXPORT AllocatedBufferInfo {
 public:
  AllocatedBufferInfo(const gfx::GpuMemoryBufferHandle& handle,
                      const gfx::Size& size,
                      gfx::BufferFormat format);
  ~AllocatedBufferInfo();

  gfx::GpuMemoryBufferType type() const { return type_; }

  // Add a memory dump for this buffer to |pmd|. Returns false if adding the
  // dump failed.
  bool OnMemoryDump(base::trace_event::ProcessMemoryDump* pmd,
                    int client_id,
                    uint64_t client_tracing_process_id) const;

 private:
  gfx::GpuMemoryBufferId buffer_id_;
  gfx::GpuMemoryBufferType type_;
  size_t size_in_bytes_;
  base::UnguessableToken shared_memory_guid_;
};

}  // namespace gpu

#endif  // GPU_IPC_COMMON_GPU_MEMORY_BUFFER_SUPPORT_H_