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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
|
/*
* Copyright (C) 2024 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 "GraphicsContextGLTextureMapperGBM.h"
#if ENABLE(WEBGL) && USE(COORDINATED_GRAPHICS) && USE(GBM)
#include "ANGLEHeaders.h"
#include "CoordinatedPlatformLayerBufferDMABuf.h"
#include "DMABufBuffer.h"
#include "DRMDeviceManager.h"
#include "GBMDevice.h"
#include "GBMVersioning.h"
#include "GLFence.h"
#include "Logging.h"
#include "PlatformDisplay.h"
#include "TextureMapperFlags.h"
#include <drm_fourcc.h>
#include <wtf/unix/UnixFileDescriptor.h>
namespace WebCore {
RefPtr<GraphicsContextGLTextureMapperGBM> GraphicsContextGLTextureMapperGBM::create(GraphicsContextGLAttributes&& attributes, RefPtr<GraphicsLayerContentsDisplayDelegate>&& delegate)
{
auto context = adoptRef(new GraphicsContextGLTextureMapperGBM(WTFMove(attributes), WTFMove(delegate)));
if (!context->initialize())
return nullptr;
return context;
}
GraphicsContextGLTextureMapperGBM::GraphicsContextGLTextureMapperGBM(GraphicsContextGLAttributes&& attributes, RefPtr<GraphicsLayerContentsDisplayDelegate>&& delegate)
: GraphicsContextGLTextureMapperANGLE(WTFMove(attributes))
{
m_layerContentsDisplayDelegate = WTFMove(delegate);
}
GraphicsContextGLTextureMapperGBM::~GraphicsContextGLTextureMapperGBM()
{
freeDrawingBuffers();
}
bool GraphicsContextGLTextureMapperGBM::platformInitialize()
{
auto isOpaqueFormat = [](uint32_t fourcc) -> bool {
return fourcc != DRM_FORMAT_ARGB8888
&& fourcc != DRM_FORMAT_RGBA8888
&& fourcc != DRM_FORMAT_ABGR8888
&& fourcc != DRM_FORMAT_BGRA8888
&& fourcc != DRM_FORMAT_ARGB2101010
&& fourcc != DRM_FORMAT_ABGR2101010
&& fourcc != DRM_FORMAT_ARGB16161616F
&& fourcc != DRM_FORMAT_ABGR16161616F;
};
bool isOpaque = !contextAttributes().alpha;
const auto& supportedFormats = PlatformDisplay::sharedDisplay().dmabufFormats();
for (const auto& format : supportedFormats) {
bool matchesOpacity = isOpaqueFormat(format.fourcc) == isOpaque;
if (!matchesOpacity && m_drawingBufferFormat.fourcc)
continue;
m_drawingBufferFormat.fourcc = format.fourcc;
m_drawingBufferFormat.modifiers = format.modifiers;
if (matchesOpacity)
break;
}
if (!m_drawingBufferFormat.fourcc)
return false;
return true;
}
bool GraphicsContextGLTextureMapperGBM::platformInitializeExtensions()
{
if (!enableExtension("GL_OES_EGL_image"_s))
return false;
const auto& eglExtensions = PlatformDisplay::sharedDisplay().eglExtensions();
return eglExtensions.KHR_image_base && eglExtensions.EXT_image_dma_buf_import;
}
GraphicsContextGLTextureMapperGBM::DrawingBuffer GraphicsContextGLTextureMapperGBM::createDrawingBuffer() const
{
auto gbmDevice = DRMDeviceManager::singleton().mainGBMDevice(DRMDeviceManager::NodeType::Render);
if (!gbmDevice)
return { };
const auto size = getInternalFramebufferSize();
struct gbm_bo* bo = nullptr;
bool disableModifiers = m_drawingBufferFormat.modifiers.size() == 1 && m_drawingBufferFormat.modifiers[0] == DRM_FORMAT_MOD_INVALID;
if (!disableModifiers && !m_drawingBufferFormat.modifiers.isEmpty())
bo = gbm_bo_create_with_modifiers2(gbmDevice->device(), size.width(), size.height(), m_drawingBufferFormat.fourcc, m_drawingBufferFormat.modifiers.span().data(), m_drawingBufferFormat.modifiers.size(), GBM_BO_USE_RENDERING);
if (!bo)
bo = gbm_bo_create(gbmDevice->device(), size.width(), size.height(), m_drawingBufferFormat.fourcc, GBM_BO_USE_RENDERING);
if (!bo)
return { };
Vector<UnixFileDescriptor> fds;
Vector<uint32_t> offsets;
Vector<uint32_t> strides;
uint32_t format = gbm_bo_get_format(bo);
int planeCount = gbm_bo_get_plane_count(bo);
uint64_t modifier = disableModifiers ? DRM_FORMAT_MOD_INVALID : gbm_bo_get_modifier(bo);
Vector<EGLint> attributes = {
EGL_WIDTH, size.width(),
EGL_HEIGHT, size.height(),
EGL_LINUX_DRM_FOURCC_EXT, static_cast<EGLint>(format),
};
#define ADD_PLANE_ATTRIBUTES(planeIndex) { \
fds.append(UnixFileDescriptor { gbm_bo_get_fd_for_plane(bo, planeIndex), UnixFileDescriptor::Adopt }); \
offsets.append(gbm_bo_get_offset(bo, planeIndex)); \
strides.append(gbm_bo_get_stride_for_plane(bo, planeIndex)); \
std::array<EGLint, 6> planeAttributes { \
EGL_DMA_BUF_PLANE##planeIndex##_FD_EXT, fds.last().value(), \
EGL_DMA_BUF_PLANE##planeIndex##_OFFSET_EXT, static_cast<EGLint>(offsets.last()), \
EGL_DMA_BUF_PLANE##planeIndex##_PITCH_EXT, static_cast<EGLint>(strides.last()) \
}; \
attributes.append(std::span<const EGLint> { planeAttributes }); \
if (modifier != DRM_FORMAT_MOD_INVALID) { \
std::array<EGLint, 4> modifierAttributes { \
EGL_DMA_BUF_PLANE##planeIndex##_MODIFIER_HI_EXT, static_cast<EGLint>(modifier >> 32), \
EGL_DMA_BUF_PLANE##planeIndex##_MODIFIER_LO_EXT, static_cast<EGLint>(modifier & 0xffffffff) \
}; \
attributes.append(std::span<const EGLint> { modifierAttributes }); \
} \
}
if (planeCount > 0)
ADD_PLANE_ATTRIBUTES(0);
if (planeCount > 1)
ADD_PLANE_ATTRIBUTES(1);
if (planeCount > 2)
ADD_PLANE_ATTRIBUTES(2);
if (planeCount > 3)
ADD_PLANE_ATTRIBUTES(3);
#undef ADD_PLANE_ATTRIBUTES
attributes.append(EGL_NONE);
auto* image = EGL_CreateImageKHR(m_displayObj, EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT, nullptr, attributes.span().data());
gbm_bo_destroy(bo);
if (!image)
return { };
return { DMABufBuffer::create(size, format, WTFMove(fds), WTFMove(offsets), WTFMove(strides), modifier), image };
}
void GraphicsContextGLTextureMapperGBM::freeDrawingBuffers()
{
auto destroyBuffer = [this](DrawingBuffer& buffer) {
if (!buffer.image)
return;
EGL_DestroyImageKHR(m_displayObj, buffer.image);
buffer.image = nullptr;
buffer.dmabuf = nullptr;
};
destroyBuffer(m_drawingBuffer);
destroyBuffer(m_displayBuffer);
}
bool GraphicsContextGLTextureMapperGBM::bindNextDrawingBuffer()
{
std::swap(m_drawingBuffer, m_displayBuffer);
if (!m_drawingBuffer.dmabuf) {
auto buffer = createDrawingBuffer();
if (!buffer.dmabuf)
return false;
m_drawingBuffer = WTFMove(buffer);
}
auto [textureTarget, textureBinding] = drawingBufferTextureBindingPoint();
ScopedRestoreTextureBinding restoreBinding(textureBinding, textureTarget, textureTarget != TEXTURE_RECTANGLE_ARB);
GL_BindTexture(textureTarget, m_texture);
GL_EGLImageTargetTexture2DOES(textureTarget, m_drawingBuffer.image);
return true;
}
bool GraphicsContextGLTextureMapperGBM::reshapeDrawingBuffer()
{
freeDrawingBuffers();
return bindNextDrawingBuffer();
}
UnixFileDescriptor GraphicsContextGLTextureMapperGBM::createExportedFence() const
{
const auto& eglExtensions = PlatformDisplay::sharedDisplay().eglExtensions();
if (!eglExtensions.KHR_fence_sync || !eglExtensions.ANDROID_native_fence_sync)
return { };
auto sync = EGL_CreateSyncKHR(m_displayObj, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
if (sync == EGL_NO_SYNC)
return { };
GL_Flush();
auto fd = UnixFileDescriptor { EGL_DupNativeFenceFDANDROID(m_displayObj, sync), UnixFileDescriptor::Adopt };
EGL_DestroySyncKHR(m_displayObj, sync);
return fd;
}
void GraphicsContextGLTextureMapperGBM::prepareForDisplay()
{
UnixFileDescriptor fenceFD;
std::unique_ptr<GLFence> fence;
prepareForDisplayWithFinishedSignal([this, &fenceFD, &fence] {
fenceFD = createExportedFence();
if (!fenceFD) {
GL_Flush();
fence = GLFence::create(PlatformDisplay::sharedDisplay().glDisplay());
}
});
if (!m_displayBuffer.dmabuf)
return;
RELEASE_ASSERT(m_layerContentsDisplayDelegate);
OptionSet<TextureMapperFlags> flags = TextureMapperFlags::ShouldFlipTexture;
if (contextAttributes().alpha)
flags.add(TextureMapperFlags::ShouldBlend);
std::unique_ptr<CoordinatedPlatformLayerBuffer> buffer;
if (fenceFD)
buffer = CoordinatedPlatformLayerBufferDMABuf::create(Ref { *m_displayBuffer.dmabuf }, flags, WTFMove(fenceFD));
else
buffer = CoordinatedPlatformLayerBufferDMABuf::create(Ref { *m_displayBuffer.dmabuf }, flags, WTFMove(fence));
m_layerContentsDisplayDelegate->setDisplayBuffer(WTFMove(buffer));
}
void GraphicsContextGLTextureMapperGBM::prepareForDisplayWithFinishedSignal(Function<void()>&& finishedSignalCreator)
{
if (!makeContextCurrent())
return;
if (!m_drawingBuffer.dmabuf)
return;
prepareTexture();
finishedSignalCreator();
if (!bindNextDrawingBuffer()) {
forceContextLost();
return;
}
}
#if ENABLE(WEBXR)
GCGLExternalImage GraphicsContextGLTextureMapperGBM::createExternalImage(ExternalImageSource&& source, GCGLenum, GCGLint)
{
GraphicsContextGLExternalImageSource imageSource = WTFMove(source);
Vector<EGLint> attributes = {
EGL_WIDTH, imageSource.size.width(),
EGL_HEIGHT, imageSource.size.height(),
EGL_LINUX_DRM_FOURCC_EXT, static_cast<EGLint>(imageSource.fourcc)
};
#define ADD_PLANE_ATTRIBUTES(planeIndex) { \
std::array<EGLAttrib, 6> planeAttributes { \
EGL_DMA_BUF_PLANE##planeIndex##_FD_EXT, imageSource.fds[planeIndex].value(), \
EGL_DMA_BUF_PLANE##planeIndex##_OFFSET_EXT, static_cast<EGLint>(imageSource.offsets[planeIndex]), \
EGL_DMA_BUF_PLANE##planeIndex##_PITCH_EXT, static_cast<EGLint>(imageSource.strides[planeIndex]) \
}; \
attributes.append(std::span<const EGLAttrib> { planeAttributes }); \
if (imageSource.modifier != DRM_FORMAT_MOD_INVALID && PlatformDisplay::sharedDisplay().eglExtensions().EXT_image_dma_buf_import_modifiers) { \
std::array<EGLint, 4> modifierAttributes { \
EGL_DMA_BUF_PLANE##planeIndex##_MODIFIER_HI_EXT, static_cast<EGLint>(imageSource.modifier >> 32), \
EGL_DMA_BUF_PLANE##planeIndex##_MODIFIER_LO_EXT, static_cast<EGLint>(imageSource.modifier & 0xffffffff) \
}; \
attributes.append(std::span<const EGLint> { modifierAttributes }); \
} \
};
auto planeCount = imageSource.fds.size();
if (planeCount > 0)
ADD_PLANE_ATTRIBUTES(0);
if (planeCount > 1)
ADD_PLANE_ATTRIBUTES(1);
if (planeCount > 2)
ADD_PLANE_ATTRIBUTES(2);
if (planeCount > 3)
ADD_PLANE_ATTRIBUTES(3);
#undef ADD_PLANE_ATTRIBUTES
attributes.append(EGL_NONE);
if (m_displayObj == EGL_NO_DISPLAY) {
addError(GCGLErrorCode::InvalidOperation);
LOG(XR, "invalid display %d", EGL_GetError());
return { };
}
auto eglImage = EGL_CreateImageKHR(m_displayObj, EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT, nullptr, attributes.span().data());
if (!eglImage) {
LOG(XR, "invalid operation importing the image %d", EGL_GetError());
addError(GCGLErrorCode::InvalidOperation);
return { };
}
auto newName = ++m_nextExternalImageName;
m_eglImages.add(newName, eglImage);
return newName;
}
void GraphicsContextGLTextureMapperGBM::bindExternalImage(GCGLenum target, GCGLExternalImage image)
{
if (!makeContextCurrent())
return;
EGLImage eglImage = EGL_NO_IMAGE_KHR;
if (image) {
eglImage = m_eglImages.get(image);
if (!eglImage) {
addError(GCGLErrorCode::InvalidOperation);
return;
}
}
if (target == RENDERBUFFER)
GL_EGLImageTargetRenderbufferStorageOES(RENDERBUFFER, eglImage);
else
GL_EGLImageTargetTexture2DOES(target, eglImage);
}
bool GraphicsContextGLTextureMapperGBM::enableRequiredWebXRExtensions()
{
if (!makeContextCurrent())
return false;
return enableExtension("GL_OES_EGL_image"_s)
&& enableExtension("GL_OES_EGL_image_external"_s)
&& enableExtension("EGL_KHR_image_base"_s)
&& enableExtension("EGL_EXT_image_dma_buf_import"_s)
&& enableExtension("EGL_KHR_surfaceless_context"_s);
}
#endif // ENABLE(WEBXR)
} // namespace WebCore
#endif // ENABLE(WEBGL) && USE(COORDINATED_GRAPHICS) && USE(GBM)
|