-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
GBMBufferSwapchain.cpp
181 lines (154 loc) · 7.59 KB
/
GBMBufferSwapchain.cpp
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
/*
* Copyright (C) 2022 Metrological Group B.V.
* Copyright (C) 2022 Igalia S.L.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "GBMBufferSwapchain.h"
#if USE(GBM)
#include "DMABufColorSpace.h"
#include "GBMDevice.h"
#include <gbm.h>
namespace WebCore {
GBMBufferSwapchain::GBMBufferSwapchain(BufferSwapchainSize size)
{
ASSERT(unsigned(size) <= c_maxBuffers);
m_array.size = unsigned(size);
}
GBMBufferSwapchain::~GBMBufferSwapchain() = default;
RefPtr<GBMBufferSwapchain::Buffer> GBMBufferSwapchain::getBuffer(const BufferDescription& description)
{
auto& device = GBMDevice::singleton();
// If the description of the requested buffers has changed, update the description to the new one and wreck the existing buffers.
// This should handle changes in format or dimension of the buffers.
if (description.format.fourcc != m_array.description.format.fourcc || description.width != m_array.description.width || description.height != m_array.description.height || description.flags != m_array.description.flags) {
m_array.description = description;
m_array.object = { };
}
// Swapchain was asked to provide a buffer. The buffer array is traversed to find one.
for (unsigned i = 0; i < m_array.size; ++i) {
if (!m_array.object[i]) {
// If no buffer was spawned yet at this location, we do that, and return it.
auto buffer = adoptRef(*new Buffer(m_handleGenerator++, description));
m_array.object[i] = buffer.copyRef();
// Fill out the buffer's description and plane information for known and supported formats.
switch (description.format.fourcc) {
case DMABufFormat::FourCC::XRGB8888:
case DMABufFormat::FourCC::XBGR8888:
case DMABufFormat::FourCC::RGBX8888:
case DMABufFormat::FourCC::BGRX8888:
case DMABufFormat::FourCC::ARGB8888:
case DMABufFormat::FourCC::ABGR8888:
case DMABufFormat::FourCC::RGBA8888:
case DMABufFormat::FourCC::BGRA8888:
case DMABufFormat::FourCC::I420:
case DMABufFormat::FourCC::YV12:
case DMABufFormat::FourCC::A420:
case DMABufFormat::FourCC::NV12:
case DMABufFormat::FourCC::NV21:
case DMABufFormat::FourCC::YUY2:
case DMABufFormat::FourCC::YVYU:
case DMABufFormat::FourCC::UYVY:
case DMABufFormat::FourCC::VYUY:
case DMABufFormat::FourCC::VUYA:
case DMABufFormat::FourCC::AYUV:
case DMABufFormat::FourCC::Y444:
case DMABufFormat::FourCC::Y41B:
case DMABufFormat::FourCC::Y42B:
buffer->m_description.format.numPlanes = description.format.numPlanes;
for (unsigned i = 0; i < buffer->m_description.format.numPlanes; ++i) {
buffer->m_planes[i].fourcc = description.format.planes[i].fourcc;
buffer->m_planes[i].width = description.format.planeWidth(i, description.width);
buffer->m_planes[i].height = description.format.planeHeight(i, description.height);
}
break;
default:
return nullptr;
}
uint32_t boFlags = 0;
if (description.flags & BufferDescription::LinearStorage)
boFlags |= GBM_BO_USE_LINEAR;
// For each plane, we spawn a gbm_bo object of the appropriate size and format.
// TODO: GBM_BO_USE_LINEAR will be needed when transferring memory into the bo (e.g. copying
// over the software-decoded video data), but might not be required for backing e.g. ANGLE rendering.
for (unsigned i = 0; i < buffer->m_description.format.numPlanes; ++i) {
auto& plane = buffer->m_planes[i];
plane.bo = gbm_bo_create(device.device(), plane.width, plane.height, uint32_t(plane.fourcc), boFlags);
plane.stride = gbm_bo_get_stride(plane.bo);
}
// Lock the buffer and return it.
buffer->m_state.locked = true;
return buffer;
}
// There is already an existing buffer at this location. If marked as locked, update its state by reading
// the release flag. If still locked after that, we have to continue over to the next candidate.
{
auto& buffer = m_array.object[i];
if (buffer->m_state.locked)
buffer->m_state.locked = !buffer->m_state.releaseFlag.released();
if (buffer->m_state.locked)
continue;
}
// This buffer was unlocked, so it can be used. Lock and return it.
auto buffer = m_array.object[i].copyRef();
buffer->m_state.locked = true;
// Swap out the located buffer to the end of the buffer queue. When the next buffer is requested from this
// swapchain, the previously-used buffers will be traversed and tested for release first, meaning there's a
// higher chance we will be able to reuse them, without the penalty of having to traverse to deep into the
// swapchain array.
for (++i; i < m_array.size && !!m_array.object[i]; ++i)
std::swap(m_array.object[i - 1], m_array.object[i]);
return buffer;
}
return nullptr;
}
GBMBufferSwapchain::Buffer::Buffer(uint32_t handle, const BufferDescription& description)
: m_handle(handle)
, m_description(description)
{
m_state.releaseFlag = DMABufReleaseFlag(DMABufReleaseFlag::Initialize);
}
GBMBufferSwapchain::Buffer::~Buffer() = default;
DMABufObject GBMBufferSwapchain::Buffer::createDMABufObject(uintptr_t handle) const
{
DMABufObject object(handle);
object.format = m_description.format;
object.width = m_description.width;
object.height = m_description.height;
object.releaseFlag = m_state.releaseFlag.dup();
for (unsigned i = 0; i < m_description.format.numPlanes; ++i) {
object.fd[i] = UnixFileDescriptor { gbm_bo_get_fd(m_planes[i].bo), UnixFileDescriptor::Adopt };
object.offset[i] = 0;
object.stride[i] = m_planes[i].stride;
object.modifierPresent[i] = true;
object.modifierValue[i] = gbm_bo_get_modifier(m_planes[i].bo);
}
return object;
}
GBMBufferSwapchain::Buffer::PlaneData::~PlaneData()
{
if (bo)
gbm_bo_destroy(bo);
}
} // namespace WebCore
#endif // USE(GBM)