File: fake_gpu_memory_buffer.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 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 (162 lines) | stat: -rw-r--r-- 4,990 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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "gpu/command_buffer/client/fake_gpu_memory_buffer.h"

#include "base/atomic_sequence_num.h"
#include "build/build_config.h"
#include "media/base/format_utils.h"
#include "media/base/video_frame.h"

#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#endif

#if BUILDFLAG(IS_FUCHSIA)
#include <lib/zx/eventpair.h>
#include <lib/zx/object.h>
#endif

namespace gpu {

namespace {

#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
base::ScopedFD GetDummyFD() {
  base::ScopedFD fd(open("/dev/zero", O_RDWR));
  DCHECK(fd.is_valid());
  return fd;
}
#endif

static base::AtomicSequenceNumber buffer_id_generator;

}  // namespace

#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
gfx::GpuMemoryBufferHandle CreatePixmapHandleForTesting(
    const gfx::Size& size,
    gfx::BufferFormat format,
    uint64_t modifier) {
  std::optional<media::VideoPixelFormat> video_pixel_format =
      media::GfxBufferFormatToVideoPixelFormat(format);
  CHECK(video_pixel_format);

  gfx::NativePixmapHandle native_pixmap_handle;
  for (size_t i = 0; i < media::VideoFrame::NumPlanes(*video_pixel_format);
       i++) {
    const gfx::Size plane_size_in_bytes =
        media::VideoFrame::PlaneSize(*video_pixel_format, i, size);
    native_pixmap_handle.planes.emplace_back(plane_size_in_bytes.width(), 0,
                                             plane_size_in_bytes.GetArea(),
                                             GetDummyFD());
  }
  native_pixmap_handle.modifier = modifier;

  gfx::GpuMemoryBufferHandle handle(std::move(native_pixmap_handle));
  handle.id = gfx::GpuMemoryBufferId(buffer_id_generator.GetNext());
  return handle;
}
#endif

FakeGpuMemoryBuffer::FakeGpuMemoryBuffer(const gfx::Size& size,
                                         gfx::BufferFormat format)
    : FakeGpuMemoryBuffer(size, format, gfx::NativePixmapHandle::kNoModifier) {}

FakeGpuMemoryBuffer::FakeGpuMemoryBuffer(const gfx::Size& size,
                                         gfx::BufferFormat format,
                                         bool premapped,
                                         MapCallbackController* controller)
    : FakeGpuMemoryBuffer(size, format, gfx::NativePixmapHandle::kNoModifier) {
  premapped_ = premapped;
  map_callback_controller_ = controller;
}

FakeGpuMemoryBuffer::FakeGpuMemoryBuffer(const gfx::Size& size,
                                         gfx::BufferFormat format,
                                         uint64_t modifier)
    : size_(size), format_(format) {
  std::optional<media::VideoPixelFormat> video_pixel_format =
      media::GfxBufferFormatToVideoPixelFormat(format);
  CHECK(video_pixel_format);
  video_pixel_format_ = *video_pixel_format;

  const size_t allocation_size =
      media::VideoFrame::AllocationSize(video_pixel_format_, size_);
  data_ = std::vector<uint8_t>(allocation_size);

  handle_.type = gfx::SHARED_MEMORY_BUFFER;
  handle_.id = gfx::GpuMemoryBufferId(buffer_id_generator.GetNext());
}

FakeGpuMemoryBuffer::~FakeGpuMemoryBuffer() = default;

bool FakeGpuMemoryBuffer::Map() {
  return true;
}

void FakeGpuMemoryBuffer::MapAsync(base::OnceCallback<void(bool)> result_cb) {
  if (premapped_) {
    std::move(result_cb).Run(true);
    return;
  }
  map_callback_controller_->RegisterCallback(std::move(result_cb));
}

bool FakeGpuMemoryBuffer::AsyncMappingIsNonBlocking() const {
  return true;
}

void* FakeGpuMemoryBuffer::memory(size_t plane) {
  DCHECK_LT(plane, media::VideoFrame::NumPlanes(video_pixel_format_));
  auto* data_ptr = data_.data();
  for (size_t i = 1; i <= plane; i++) {
    data_ptr += media::VideoFrame::PlaneSize(video_pixel_format_, i - 1, size_)
                    .GetArea();
  }
  return data_ptr;
}

void FakeGpuMemoryBuffer::Unmap() {}

gfx::Size FakeGpuMemoryBuffer::GetSize() const {
  return size_;
}

gfx::BufferFormat FakeGpuMemoryBuffer::GetFormat() const {
  return format_;
}

int FakeGpuMemoryBuffer::stride(size_t plane) const {
  DCHECK_LT(plane, media::VideoFrame::NumPlanes(video_pixel_format_));
  return media::VideoFrame::PlaneSize(video_pixel_format_, plane, size_)
      .width();
}

gfx::GpuMemoryBufferId FakeGpuMemoryBuffer::GetId() const {
  return handle_.id;
}

gfx::GpuMemoryBufferType FakeGpuMemoryBuffer::GetType() const {
  return gfx::SHARED_MEMORY_BUFFER;
}

gfx::GpuMemoryBufferHandle FakeGpuMemoryBuffer::CloneHandle() const {
  return handle_.Clone();
}

void FakeGpuMemoryBuffer::OnMemoryDump(
    base::trace_event::ProcessMemoryDump* pmd,
    const base::trace_event::MemoryAllocatorDumpGuid& buffer_dump_guid,
    uint64_t tracing_process_id,
    int importance) const {}

}  // namespace gpu