summaryrefslogtreecommitdiffstats
path: root/src/core/compositor/native_skia_output_device_mac.mm
blob: fd3930a9f6ebef80ded912a06f0265be37ebc5c1 (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
// 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 <AppKit/AppKit.h>
#import <IOSurface/IOSurface.h>
#import <Metal/Metal.h>

#include <QtGui/qtguiglobal.h>
#include <QtQuick/qquickwindow.h>
#include <QtQuick/qsgrendererinterface.h>
#include <QtQuick/qsgtexture.h>

#if QT_CONFIG(opengl)
#include <OpenGL/OpenGL.h>
#include <QtGui/qopenglcontext.h>
#include <QtGui/qopenglextrafunctions.h>
#include <QtOpenGL/qopengltextureblitter.h>
#include <QtOpenGL/qopenglframebufferobject.h>
#endif

namespace QtWebEngineCore {

QSGTexture *makeMetalTexture(QQuickWindow *win, IOSurfaceRef ioSurface, uint ioSurfacePlane,
                             const QSize &size, QQuickWindow::CreateTextureOptions texOpts)
{
    auto desc = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatBGRA8Unorm
                                                                   width:size.width()
                                                                  height:size.height()
                                                               mipmapped:false];

    QSGRendererInterface *ri = win->rendererInterface();
    auto device = (__bridge id<MTLDevice>)(ri->getResource(win, QSGRendererInterface::DeviceResource));
    auto texture = [device newTextureWithDescriptor:desc iosurface:ioSurface plane:ioSurfacePlane];
    return QNativeInterface::QSGMetalTexture::fromNative(texture, win, size, texOpts);
}

#if QT_CONFIG(opengl)
uint32_t makeCGLTexture(QQuickWindow *win, IOSurfaceRef ioSurface, const QSize &size)
{
    const int width = size.width();
    const int height = size.height();

    auto glContext = QOpenGLContext::currentContext();
    auto glFun = glContext->extraFunctions();
    auto nscontext = glContext->nativeInterface<QNativeInterface::QCocoaGLContext>()->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, ioSurface, 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();

    return glTexture;
}
#endif // QT_CONFIG(opengl)

} // namespace