From 79320b37769d659c7ec243dc4d2e4dc037f74ab0 Mon Sep 17 00:00:00 2001 From: Peter Varga Date: Thu, 21 Dec 2023 09:08:01 +0100 Subject: Get rid of native_skia_output_device_mac2.mm This source file was used to workaround redefinition build errors when including Qt headers in native_skia_output_device_mac.mm. Introduce another workaround what doesn't redefine Q_FORWARD_DECLARE_OBJC_CLASS macro in native_skia_output_device_mac.mm thus the two source files can be merged into one. Pick-to: 6.7 Change-Id: I640c42e435514f5df0fcb8b69d0d728d9420e761 Reviewed-by: Allan Sandfeld Jensen --- src/core/CMakeLists.txt | 1 - .../compositor/native_skia_output_device_mac.mm | 83 ++++++++++++++++---- .../compositor/native_skia_output_device_mac2.mm | 89 ---------------------- 3 files changed, 70 insertions(+), 103 deletions(-) delete mode 100644 src/core/compositor/native_skia_output_device_mac2.mm diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 058ea4405..67f821589 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -230,7 +230,6 @@ foreach(arch ${archs}) SOURCES native_web_keyboard_event_qt_mac.mm compositor/native_skia_output_device_mac.mm - compositor/native_skia_output_device_mac2.mm ) extend_gn_target(${buildGn} CONDITION QT_FEATURE_webengine_pepper_plugins diff --git a/src/core/compositor/native_skia_output_device_mac.mm b/src/core/compositor/native_skia_output_device_mac.mm index ef400272c..8a23ef7c3 100644 --- a/src/core/compositor/native_skia_output_device_mac.mm +++ b/src/core/compositor/native_skia_output_device_mac.mm @@ -1,38 +1,95 @@ // Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only +// This is a workaround to be able to include Qt headers without +// "redefinition of 'NSString' as different kind of symbol" errors. +// TODO: Remove this when namespace ambiguity issues are fixed properly, +// see get_forward_declaration_macro() in cmake/Functions.cmake +#undef Q_FORWARD_DECLARE_OBJC_CLASS + #import #import #import #include +#include +#include +#include -QT_BEGIN_NAMESPACE -class QSGTexture; -class QQuickWindow; -QT_END_NAMESPACE - -@protocol MTLDevice; +#if QT_CONFIG(opengl) +#include +#include +#include +#include +#include +#endif namespace QtWebEngineCore { -QSGTexture *makeMetalTexture2(QQuickWindow *win, id mtlTexture, int width, int height, uint32_t textureOptions); -id getRhiDev(QQuickWindow *win); QSGTexture *makeMetalTexture(QQuickWindow *win, IOSurfaceRef io_surface, uint io_surface_plane, int width, int height, uint32_t textureOptions) { auto desc = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatBGRA8Unorm width:width height:height mipmapped:false]; - auto device = getRhiDev(win); + QSGRendererInterface *ri = win->rendererInterface(); + auto device = (__bridge id)(ri->getResource(win, QSGRendererInterface::DeviceResource)); auto texture = [device newTextureWithDescriptor:desc iosurface:io_surface plane:io_surface_plane]; - return makeMetalTexture2(win, texture, width, height, textureOptions); + QQuickWindow::CreateTextureOptions texOpts(textureOptions); + return QNativeInterface::QSGMetalTexture::fromNative(texture, win, {width, height}, texOpts); } #if QT_CONFIG(opengl) -CGLContextObj getCGLContext(NSOpenGLContext *context) +void releaseGlTexture(uint32_t glTexture) { - return [context CGLContextObj]; + auto *glContext = QOpenGLContext::currentContext(); + if (!glContext) + return; + auto glFun = glContext->functions(); + glFun->glDeleteTextures(1, &glTexture); } -#endif + +QSGTexture *makeCGLTexture(QQuickWindow *win, IOSurfaceRef io_surface, int width, int height, uint32_t textureOptions, uint32_t *heldTexture) +{ + auto glContext = QOpenGLContext::currentContext(); + auto glFun = glContext->extraFunctions(); + auto nscontext = glContext->nativeInterface()->nativeContext(); + CGLContextObj cglContext = [nscontext CGLContextObj]; + + win->beginExternalCommands(); + // Bind the IO surface to a texture + GLuint glTexture; + glFun->glGenTextures(1, &glTexture); + glFun->glBindTexture(GL_TEXTURE_RECTANGLE_ARB, glTexture); + CGLTexImageIOSurface2D(cglContext, GL_TEXTURE_RECTANGLE_ARB, GL_RGBA, width, height, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, io_surface, 0); + glFun->glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0); + glFun->glViewport(0, 0, width, height); + + // The bound IO surface is a weird dynamic bind, so take a snapshot of it to a normal texture + { + QOpenGLFramebufferObject fbo(width, height, GL_TEXTURE_2D); + auto success = fbo.bind(); + Q_ASSERT(success); + + QOpenGLTextureBlitter blitter; + success = blitter.create(); + Q_ASSERT(success); + glFun->glDisable(GL_BLEND); + glFun->glDisable(GL_SCISSOR_TEST); + blitter.bind(GL_TEXTURE_RECTANGLE_ARB); + blitter.blit(glTexture, {}, QOpenGLTextureBlitter::OriginBottomLeft); + blitter.release(); + blitter.destroy(); + + glFun->glDeleteTextures(1, &glTexture); + glTexture = fbo.takeTexture(); + fbo.release(); + } + win->endExternalCommands(); + + *heldTexture = glTexture; + QQuickWindow::CreateTextureOptions texOpts(textureOptions); + return QNativeInterface::QSGOpenGLTexture::fromNative(glTexture, win, {width, height}, texOpts); +} +#endif // QT_CONFIG(opengl) } // namespace diff --git a/src/core/compositor/native_skia_output_device_mac2.mm b/src/core/compositor/native_skia_output_device_mac2.mm deleted file mode 100644 index 87b65855c..000000000 --- a/src/core/compositor/native_skia_output_device_mac2.mm +++ /dev/null @@ -1,89 +0,0 @@ -// Copyright (C) 2023 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only - -#include -#include -#include - -#if QT_CONFIG(opengl) -#include -#include -#include -#include -#include -#endif - -@protocol MTLDevice; -@protocol MTLTexture; - -namespace QtWebEngineCore { - -id getRhiDev(QQuickWindow *win) -{ - QSGRendererInterface *ri = win->rendererInterface(); - return (__bridge id)(ri->getResource(win, QSGRendererInterface::DeviceResource)); -} - -QSGTexture *makeMetalTexture2(QQuickWindow *win, id mtlTexture, int width, int height, uint32_t textureOptions) -{ - QQuickWindow::CreateTextureOptions texOpts(textureOptions); - return QNativeInterface::QSGMetalTexture::fromNative(mtlTexture, win, {width, height}, texOpts); -} - -#if QT_CONFIG(opengl) -CGLContextObj getCGLContext(NSOpenGLContext *context); - -void releaseGlTexture(uint32_t glTexture) -{ - auto *glContext = QOpenGLContext::currentContext(); - if (!glContext) - return; - auto glFun = glContext->functions(); - glFun->glDeleteTextures(1, &glTexture); -} - -QSGTexture *makeCGLTexture(QQuickWindow *win, IOSurfaceRef io_surface, int width, int height, uint32_t textureOptions, uint32_t *heldTexture) -{ - auto glContext = QOpenGLContext::currentContext(); - auto glFun = glContext->extraFunctions(); - auto nscontext = glContext->nativeInterface()->nativeContext(); - CGLContextObj cglContext = getCGLContext(nscontext); - - win->beginExternalCommands(); - // Bind the IO surface to a texture - GLuint glTexture; - glFun->glGenTextures(1, &glTexture); - glFun->glBindTexture(GL_TEXTURE_RECTANGLE_ARB, glTexture); - CGLTexImageIOSurface2D(cglContext, GL_TEXTURE_RECTANGLE_ARB, GL_RGBA, width, height, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, io_surface, 0); - glFun->glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0); - glFun->glViewport(0, 0, width, height); - - // The bound IO surface is a weird dynamic bind, so take a snapshot of it to a normal texture - { - QOpenGLFramebufferObject fbo(width, height, GL_TEXTURE_2D); - auto success = fbo.bind(); - Q_ASSERT(success); - - QOpenGLTextureBlitter blitter; - success = blitter.create(); - Q_ASSERT(success); - glFun->glDisable(GL_BLEND); - glFun->glDisable(GL_SCISSOR_TEST); - blitter.bind(GL_TEXTURE_RECTANGLE_ARB); - blitter.blit(glTexture, {}, QOpenGLTextureBlitter::OriginBottomLeft); - blitter.release(); - blitter.destroy(); - - glFun->glDeleteTextures(1, &glTexture); - glTexture = fbo.takeTexture(); - fbo.release(); - } - win->endExternalCommands(); - - *heldTexture = glTexture; - QQuickWindow::CreateTextureOptions texOpts(textureOptions); - return QNativeInterface::QSGOpenGLTexture::fromNative(glTexture, win, {width, height}, texOpts); -} -#endif - -} // namespace -- cgit v1.2.3