Title: [295048] trunk/Source/WebCore/platform
Revision
295048
Author
[email protected]
Date
2022-05-31 06:02:53 -0700 (Tue, 31 May 2022)

Log Message

[Linux] TextureMapperPlatformLayerProxyDMABuf should support different colorspaces
https://bugs.webkit.org/show_bug.cgi?id=240633

Patch by Žan Doberšek <[email protected]> on 2022-05-31
Reviewed by Miguel Gomez and Philippe Normand.

Add the DMABufColorSpace enumeration, covering different colorspaces we
currently can support between decoded GStreamer data and the TextureMapper
DMABuf integration.

DMABufObject gains a DMABufColorSpace member variable, initially of an invalid
value but that can be overridden by whoever is constructing the object.

In MediaPlayerPrivateGStreamer, the desired colorspace can be retrieved from the
GstVideoInfo colorimetry information and set on the DMABufObject.

The colorspace is now respected in TextureMapperPlatformLayerProxyDMABuf,
choosing between different YUV-to-RGB matrices that are to be used when sampling
from the set of plane-assigned textures. The default is the BT.601 conversion
matrix. For RGBA-like formats, no conversion is required, so an assert is placed
to ensure that the colorspace is either unspecified or specified as SRGB.

* Source/WebCore/platform/TextureMapper.cmake:
* Source/WebCore/platform/graphics/gbm/DMABufColorSpace.h: Added.
* Source/WebCore/platform/graphics/gbm/DMABufObject.h:
(WebCore::DMABufObject::encode const):
(WebCore::DMABufObject::encode):
(WebCore::DMABufObject::decode):
* Source/WebCore/platform/graphics/gbm/GBMBufferSwapchain.cpp:
* Source/WebCore/platform/graphics/gstreamer/MediaPlayerPrivateGStreamer.cpp:
(WebCore::colorSpaceForColorimetry):
(WebCore::MediaPlayerPrivateGStreamer::pushDMABufToCompositor):
* Source/WebCore/platform/graphics/texmap/TextureMapperPlatformLayerProxyDMABuf.cpp:
(WebCore::TextureMapperPlatformLayerProxyDMABuf::DMABufLayer::paintToTextureMapper):

Canonical link: https://commits.webkit.org/251143@main

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WebCore/platform/TextureMapper.cmake (295047 => 295048)


--- trunk/Source/WebCore/platform/TextureMapper.cmake	2022-05-31 12:33:00 UTC (rev 295047)
+++ trunk/Source/WebCore/platform/TextureMapper.cmake	2022-05-31 13:02:53 UTC (rev 295048)
@@ -153,6 +153,7 @@
 
 if (USE_LIBGBM)
     list(APPEND WebCore_PRIVATE_FRAMEWORK_HEADERS
+        platform/graphics/gbm/DMABufColorSpace.h
         platform/graphics/gbm/DMABufEGLUtilities.h
         platform/graphics/gbm/DMABufFormat.h
         platform/graphics/gbm/DMABufObject.h

Added: trunk/Source/WebCore/platform/graphics/gbm/DMABufColorSpace.h (0 => 295048)


--- trunk/Source/WebCore/platform/graphics/gbm/DMABufColorSpace.h	                        (rev 0)
+++ trunk/Source/WebCore/platform/graphics/gbm/DMABufColorSpace.h	2022-05-31 13:02:53 UTC (rev 295048)
@@ -0,0 +1,42 @@
+/*
+ * 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.
+ */
+
+#pragma once
+
+#include <cstdint>
+
+namespace WebCore {
+
+enum class DMABufColorSpace : uint32_t {
+    Invalid,
+    SRGB,
+    BT601,
+    BT709,
+    BT2020,
+    SMPTE240M,
+};
+
+} // namespace WebCore

Modified: trunk/Source/WebCore/platform/graphics/gbm/DMABufObject.h (295047 => 295048)


--- trunk/Source/WebCore/platform/graphics/gbm/DMABufObject.h	2022-05-31 12:33:00 UTC (rev 295047)
+++ trunk/Source/WebCore/platform/graphics/gbm/DMABufObject.h	2022-05-31 13:02:53 UTC (rev 295048)
@@ -26,6 +26,7 @@
 
 #pragma once
 
+#include "DMABufColorSpace.h"
 #include "DMABufFormat.h"
 #include "DMABufReleaseFlag.h"
 #include <array>
@@ -56,6 +57,7 @@
 
     uintptr_t handle { 0 };
     DMABufFormat format { };
+    DMABufColorSpace colorSpace { DMABufColorSpace::Invalid };
     uint32_t width { 0 };
     uint32_t height { 0 };
     DMABufReleaseFlag releaseFlag { };
@@ -68,7 +70,7 @@
 template<class Encoder>
 void DMABufObject::encode(Encoder& encoder) const &
 {
-    encoder << handle << uint32_t(format.fourcc) << width << height;
+    encoder << handle << uint32_t(format.fourcc) << uint32_t(colorSpace) << width << height;
     encoder << releaseFlag.fd.duplicate();
 
     for (unsigned i = 0; i < DMABufFormat::c_maxPlanes; ++i) {
@@ -80,7 +82,7 @@
 template<class Encoder>
 void DMABufObject::encode(Encoder& encoder) &&
 {
-    encoder << handle << uint32_t(format.fourcc) << width << height;
+    encoder << handle << uint32_t(format.fourcc) << uint32_t(colorSpace) << width << height;
     encoder << WTFMove(releaseFlag.fd);
 
     for (unsigned i = 0; i < DMABufFormat::c_maxPlanes; ++i) {
@@ -100,6 +102,10 @@
     decoder >> fourcc;
     if (!fourcc)
         return std::nullopt;
+    std::optional<uint32_t> colorSpace;
+    decoder >> colorSpace;
+    if (!colorSpace)
+        return std::nullopt;
     std::optional<uint32_t> width;
     decoder >> width;
     if (!width)
@@ -111,6 +117,7 @@
 
     DMABufObject dmabufObject(*handle);
     dmabufObject.format = DMABufFormat::create(*fourcc);
+    dmabufObject.colorSpace = DMABufColorSpace { *colorSpace };
     dmabufObject.width = *width;
     dmabufObject.height = *height;
 

Modified: trunk/Source/WebCore/platform/graphics/gbm/GBMBufferSwapchain.cpp (295047 => 295048)


--- trunk/Source/WebCore/platform/graphics/gbm/GBMBufferSwapchain.cpp	2022-05-31 12:33:00 UTC (rev 295047)
+++ trunk/Source/WebCore/platform/graphics/gbm/GBMBufferSwapchain.cpp	2022-05-31 13:02:53 UTC (rev 295048)
@@ -29,6 +29,7 @@
 
 #if USE(LIBGBM)
 
+#include "DMABufColorSpace.h"
 #include "GBMDevice.h"
 #include <gbm.h>
 

Modified: trunk/Source/WebCore/platform/graphics/gstreamer/MediaPlayerPrivateGStreamer.cpp (295047 => 295048)


--- trunk/Source/WebCore/platform/graphics/gstreamer/MediaPlayerPrivateGStreamer.cpp	2022-05-31 12:33:00 UTC (rev 295047)
+++ trunk/Source/WebCore/platform/graphics/gstreamer/MediaPlayerPrivateGStreamer.cpp	2022-05-31 13:02:53 UTC (rev 295048)
@@ -3049,6 +3049,21 @@
     return gst_video_format_to_fourcc(format);
 }
 
+static DMABufColorSpace colorSpaceForColorimetry(const GstVideoColorimetry* cinfo)
+{
+    if (gst_video_colorimetry_matches(cinfo, GST_VIDEO_COLORIMETRY_SRGB))
+        return DMABufColorSpace::SRGB;
+    if (gst_video_colorimetry_matches(cinfo, GST_VIDEO_COLORIMETRY_BT601))
+        return DMABufColorSpace::BT601;
+    if (gst_video_colorimetry_matches(cinfo, GST_VIDEO_COLORIMETRY_BT709))
+        return DMABufColorSpace::BT709;
+    if (gst_video_colorimetry_matches(cinfo, GST_VIDEO_COLORIMETRY_BT2020))
+        return DMABufColorSpace::BT2020;
+    if (gst_video_colorimetry_matches(cinfo, GST_VIDEO_COLORIMETRY_SMPTE240M))
+        return DMABufColorSpace::SMPTE240M;
+    return DMABufColorSpace::Invalid;
+}
+
 void MediaPlayerPrivateGStreamer::pushDMABufToCompositor()
 {
     Locker sampleLocker { m_sampleMutex };
@@ -3103,6 +3118,7 @@
             DMABufObject(reinterpret_cast<uintptr_t>(gst_buffer_peek_memory(buffer, 0))),
             [&](auto&& object) {
                 object.format = DMABufFormat::create(fourccValue(GST_VIDEO_INFO_FORMAT(&videoInfo)));
+                object.colorSpace = colorSpaceForColorimetry(&GST_VIDEO_INFO_COLORIMETRY(&videoInfo));
                 object.width = GST_VIDEO_INFO_WIDTH(&videoInfo);
                 object.height = GST_VIDEO_INFO_HEIGHT(&videoInfo);
 
@@ -3248,8 +3264,10 @@
     // given GBMBufferSwapchain::Buffer object.
     downcast<TextureMapperPlatformLayerProxyDMABuf>(proxy).pushDMABuf(
         DMABufObject(reinterpret_cast<uintptr_t>(m_swapchain.get()) + swapchainBuffer->handle()),
-        [&](auto&& object) {
-            return swapchainBuffer->createDMABufObject(object.handle);
+        [&](auto&& initialObject) {
+            auto object = swapchainBuffer->createDMABufObject(initialObject.handle);
+            object.colorSpace = colorSpaceForColorimetry(&GST_VIDEO_INFO_COLORIMETRY(&videoInfo));
+            return object;
         });
 }
 #endif // USE(TEXTURE_MAPPER_DMABUF)

Modified: trunk/Source/WebCore/platform/graphics/texmap/TextureMapperPlatformLayerProxyDMABuf.cpp (295047 => 295048)


--- trunk/Source/WebCore/platform/graphics/texmap/TextureMapperPlatformLayerProxyDMABuf.cpp	2022-05-31 12:33:00 UTC (rev 295047)
+++ trunk/Source/WebCore/platform/graphics/texmap/TextureMapperPlatformLayerProxyDMABuf.cpp	2022-05-31 13:02:53 UTC (rev 295048)
@@ -195,15 +195,56 @@
     if (!m_imageData)
         return;
 
-    // TODO: this is the BT.601 colorspace conversion matrix. The exact desired colorspace should be included
-    // in the DMABufObject, and the relevant matrix decided based on it. BT.601 should remain the default.
-    static constexpr std::array<GLfloat, 16> s_yuvToRGB {
-        1.164383561643836f,  0.0f,                1.596026785714286f, -0.874202217873451f,
-        1.164383561643836f, -0.391762290094914f, -0.812967647237771f,  0.531667823499146f,
-        1.164383561643836f,  2.017232142857143f, -0.0f,               -1.085630789302022f,
-        0.0f,                0.0f,                0.0f,                1.0f,
+    static constexpr std::array<GLfloat, 16> s_bt601ConversionMatrix {
+        1.164383561643836,  0.0,                1.596026785714286, -0.874202217873451,
+        1.164383561643836, -0.391762290094914, -0.812967647237771,  0.531667823499146,
+        1.164383561643836,  2.017232142857143,  0.0,               -1.085630789302022,
+        0.0,                0.0,                0.0,                1.0,
     };
 
+    static constexpr std::array<GLfloat, 16> s_bt709ConversionMatrix {
+        1.164383561643836,  0.0,                1.792741071428571, -0.972945075016308,
+        1.164383561643836, -0.213248614273730, -0.532909328559444,  0.301482665475862,
+        1.164383561643836,  2.112401785714286,  0.0,               -1.133402217873451,
+        0.0,                0.0,                0.0,                1.0,
+    };
+
+    static constexpr std::array<GLfloat, 16> s_bt2020ConversionMatrix {
+        1.164383561643836,  0.0,                1.678674107142857, -0.915687932159165,
+        1.164383561643836, -0.187326104219343, -0.650424318505057,  0.347458498519301,
+        1.164383561643836,  2.141772321428571,  0.0,               -1.148145075016308,
+        0.0,                0.0,                0.0,                1.0,
+    };
+
+    static constexpr std::array<GLfloat, 16> s_smpte240MConversionMatrix {
+        1.164383561643836,  0.0,                1.793651785714286, -0.973402217873451,
+        1.164383561643836, -0.256532845251675, -0.542724809537390,  0.328136638536074,
+        1.164383561643836,  2.07984375,         0.0,               -1.117059360730593,
+        0.0,                0.0,                0.0,                1.0,
+    };
+
+    // Based on the specified colorspace, a YUV-to-RGB matrix is chosen. The default is the BT.601 matrix.
+    // Invalid or SRGB colorspace defaults to that as well, but in case of RGBA-like formats, the matrix
+    // of course goes unused. This is complemented with the below assert that for those formats the specified
+    // colorspace is either invalid or SRGB.
+    const std::array<GLfloat, 16>& yuvToRGB =
+        [&] {
+            switch (m_object.colorSpace) {
+            case DMABufColorSpace::Invalid:
+            case DMABufColorSpace::SRGB:
+                break;
+            case DMABufColorSpace::BT601:
+                return s_bt601ConversionMatrix;
+            case DMABufColorSpace::BT709:
+                return s_bt709ConversionMatrix;
+            case DMABufColorSpace::BT2020:
+                return s_bt2020ConversionMatrix;
+            case DMABufColorSpace::SMPTE240M:
+                return s_smpte240MConversionMatrix;
+            }
+            return s_bt601ConversionMatrix;
+        }();
+
     TextureMapperGL& texmapGL = static_cast<TextureMapperGL&>(textureMapper);
     auto& data = ""
 
@@ -212,6 +253,8 @@
     case DMABufFormat::FourCC::XBGR8888:
     case DMABufFormat::FourCC::ARGB8888:
     case DMABufFormat::FourCC::ABGR8888:
+        // Either no colorspace or the SRGB colorspace was defined for this object. Other options are not meaningful.
+        ASSERT(m_object.colorSpace == DMABufColorSpace::Invalid || m_object.colorSpace == DMABufColorSpace::SRGB);
         texmapGL.drawTexture(data.texture[0], m_flags, IntSize(data.width, data.height), targetRect, modelViewMatrix, opacity);
         break;
     case DMABufFormat::FourCC::I420:
@@ -219,21 +262,21 @@
     case DMABufFormat::FourCC::Y41B:
     case DMABufFormat::FourCC::Y42B:
         texmapGL.drawTexturePlanarYUV(std::array<GLuint, 3> { data.texture[0], data.texture[1], data.texture[2] },
-            s_yuvToRGB, m_flags, IntSize(data.width, data.height), targetRect, modelViewMatrix, opacity, std::nullopt);
+            yuvToRGB, m_flags, IntSize(data.width, data.height), targetRect, modelViewMatrix, opacity, std::nullopt);
         break;
     case DMABufFormat::FourCC::YV12:
         texmapGL.drawTexturePlanarYUV(std::array<GLuint, 3> { data.texture[0], data.texture[2], data.texture[1] },
-            s_yuvToRGB, m_flags, IntSize(data.width, data.height), targetRect, modelViewMatrix, opacity, std::nullopt);
+            yuvToRGB, m_flags, IntSize(data.width, data.height), targetRect, modelViewMatrix, opacity, std::nullopt);
         break;
     case DMABufFormat::FourCC::A420:
         texmapGL.drawTexturePlanarYUV(std::array<GLuint, 3> { data.texture[0], data.texture[1], data.texture[2] },
-            s_yuvToRGB, m_flags, IntSize(data.width, data.height), targetRect, modelViewMatrix, opacity, data.texture[3]);
+            yuvToRGB, m_flags, IntSize(data.width, data.height), targetRect, modelViewMatrix, opacity, data.texture[3]);
         break;
     case DMABufFormat::FourCC::NV12:
     case DMABufFormat::FourCC::NV21:
         texmapGL.drawTextureSemiPlanarYUV(std::array<GLuint, 2> { data.texture[0], data.texture[1] },
             (m_object.format.fourcc == DMABufFormat::FourCC::NV21),
-            s_yuvToRGB, m_flags, IntSize(data.width, data.height), targetRect, modelViewMatrix, opacity);
+            yuvToRGB, m_flags, IntSize(data.width, data.height), targetRect, modelViewMatrix, opacity);
         break;
     case DMABufFormat::FourCC::YUY2:
     case DMABufFormat::FourCC::UYVY:
@@ -240,7 +283,7 @@
     case DMABufFormat::FourCC::VUYA:
     case DMABufFormat::FourCC::YVYU:
         texmapGL.drawTexturePackedYUV(data.texture[0],
-            s_yuvToRGB, m_flags, IntSize(data.width, data.height), targetRect, modelViewMatrix, opacity);
+            yuvToRGB, m_flags, IntSize(data.width, data.height), targetRect, modelViewMatrix, opacity);
         break;
     default:
         break;
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to