summaryrefslogtreecommitdiff
path: root/chromium/ui/ozone/platform/wayland/gpu/gl_surface_egl_readback_wayland.cc
blob: 700cb969edf171378ff31254a75c937556647dbc (plain)
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
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/ozone/platform/wayland/gpu/gl_surface_egl_readback_wayland.h"

#include "base/memory/shared_memory_mapping.h"
#include "base/memory/unsafe_shared_memory_region.h"
#include "base/numerics/checked_math.h"
#include "ui/ozone/platform/wayland/gpu/wayland_buffer_manager_gpu.h"

namespace ui {

namespace {

constexpr size_t kMaxBuffers = 2;

constexpr size_t kBytesPerPixelBGRA = 4;

}  // namespace

GLSurfaceEglReadbackWayland::PixelBuffer::PixelBuffer(
    base::WritableSharedMemoryMapping shm_mapping,
    uint32_t buffer_id)
    : shm_mapping_(std::move(shm_mapping)), buffer_id_(buffer_id) {}

GLSurfaceEglReadbackWayland::PixelBuffer::~PixelBuffer() = default;

GLSurfaceEglReadbackWayland::GLSurfaceEglReadbackWayland(
    gfx::AcceleratedWidget widget,
    WaylandBufferManagerGpu* buffer_manager)
    : PbufferGLSurfaceEGL(gfx::Size(1, 1)),
      widget_(widget),
      buffer_manager_(buffer_manager) {
  buffer_manager_->RegisterSurface(widget_, this);
}

void GLSurfaceEglReadbackWayland::Destroy() {
  DestroyBuffers();
  buffer_manager_->UnregisterSurface(widget_);

  PbufferGLSurfaceEGL::Destroy();
}

bool GLSurfaceEglReadbackWayland::Resize(const gfx::Size& size,
                                         float scale_factor,
                                         const gfx::ColorSpace& color_space,
                                         bool has_alpha) {
  DestroyBuffers();

  pending_frames_ = 0;

  if (!PbufferGLSurfaceEGL::Resize(size, scale_factor, color_space, has_alpha))
    return false;

  for (size_t i = 0; i < kMaxBuffers; ++i) {
    base::CheckedNumeric<size_t> checked_length(size.width());
    checked_length *= size.height();
    checked_length *= kBytesPerPixelBGRA;
    if (!checked_length.IsValid())
      return false;

    base::UnsafeSharedMemoryRegion shm_region =
        base::UnsafeSharedMemoryRegion::Create(checked_length.ValueOrDie());
    if (!shm_region.IsValid())
      return false;

    auto shm_mapping = shm_region.Map();
    if (!shm_mapping.IsValid())
      return false;

    base::subtle::PlatformSharedMemoryRegion platform_shm =
        base::UnsafeSharedMemoryRegion::TakeHandleForSerialization(
            std::move(shm_region));
    base::subtle::ScopedFDPair fd_pair = platform_shm.PassPlatformHandle();

    auto buffer_id = buffer_manager_->AllocateBufferID();
    available_buffers_.push_back(
        std::make_unique<PixelBuffer>(std::move(shm_mapping), buffer_id));

    buffer_manager_->CreateShmBasedBuffer(
        std::move(fd_pair.fd), checked_length.ValueOrDie(), size, buffer_id);
  }

  return true;
}

bool GLSurfaceEglReadbackWayland::IsOffscreen() {
  return false;
}

bool GLSurfaceEglReadbackWayland::SupportsAsyncSwap() {
  return true;
}

gfx::SwapResult GLSurfaceEglReadbackWayland::SwapBuffers(
    PresentationCallback callback) {
  NOTREACHED();
  return gfx::SwapResult::SWAP_FAILED;
}

void GLSurfaceEglReadbackWayland::SwapBuffersAsync(
    SwapCompletionCallback completion_callback,
    PresentationCallback presentation_callback) {
  DCHECK(pending_frames_ < kMaxBuffers);

  // Increase pending frames number.
  ++pending_frames_;

  completion_callbacks_.push_back(std::move(completion_callback));
  presentation_callbacks_.push_back(std::move(presentation_callback));

  DCHECK(!available_buffers_.empty());
  in_flight_pixel_buffers_.push_back(std::move(available_buffers_.front()));
  auto* next_buffer = in_flight_pixel_buffers_.back().get();
  available_buffers_.erase(available_buffers_.begin());

  const gfx::Size size = GetSize();
  CHECK(next_buffer->shm_mapping_.memory());
  glReadPixels(0, 0, size.width(), size.height(), GL_BGRA, GL_UNSIGNED_BYTE,
               next_buffer->shm_mapping_.memory());

  buffer_manager_->CommitBuffer(widget_, next_buffer->buffer_id_,
                                {{0, 0}, size});
}

gfx::SurfaceOrigin GLSurfaceEglReadbackWayland::GetOrigin() const {
  // GLSurfaceEglReadbackWayland's y-axis is flipped compare to GL - (0,0) is at
  // top left corner.
  return gfx::SurfaceOrigin::kTopLeft;
}

GLSurfaceEglReadbackWayland::~GLSurfaceEglReadbackWayland() {
  Destroy();
}

void GLSurfaceEglReadbackWayland::OnSubmission(
    uint32_t buffer_id,
    const gfx::SwapResult& swap_result) {
  --pending_frames_;

  if (in_flight_pixel_buffers_.front()) {
    if (displayed_buffer_)
      available_buffers_.push_back(std::move(displayed_buffer_));
    displayed_buffer_ = std::move(in_flight_pixel_buffers_.front());
    DCHECK_EQ(displayed_buffer_->buffer_id_, buffer_id);
  }

  in_flight_pixel_buffers_.pop_front();

  DCHECK(!completion_callbacks_.empty());
  std::move(completion_callbacks_.front()).Run(swap_result, nullptr);
  completion_callbacks_.erase(completion_callbacks_.begin());
}

void GLSurfaceEglReadbackWayland::OnPresentation(
    uint32_t buffer_id,
    const gfx::PresentationFeedback& feedback) {
  DCHECK(!presentation_callbacks_.empty());
  std::move(presentation_callbacks_.front()).Run(feedback);
  presentation_callbacks_.erase(presentation_callbacks_.begin());
}

void GLSurfaceEglReadbackWayland::DestroyBuffers() {
  for (const auto& pixel_buffer : available_buffers_)
    buffer_manager_->DestroyBuffer(widget_, pixel_buffer->buffer_id_);
  for (const auto& pixel_buffer : in_flight_pixel_buffers_)
    buffer_manager_->DestroyBuffer(widget_, pixel_buffer->buffer_id_);

  if (displayed_buffer_)
    buffer_manager_->DestroyBuffer(widget_, displayed_buffer_->buffer_id_);

  available_buffers_.clear();
  in_flight_pixel_buffers_.clear();
  displayed_buffer_.reset();
}

}  // namespace ui