summaryrefslogtreecommitdiffstats
path: root/src/gui/util/qgraphicsframecapturemetal.mm
blob: b0ff0bab2b244d7dd64a9997c7a064b1ed797acd (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
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
// 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 "qgraphicsframecapturemetal_p_p.h"
#include <QtCore/qurl.h>
#include "Metal/Metal.h"
#include "qglobal.h"
#include <QtGui/rhi/qrhi.h>
#include <QtGui/rhi/qrhi_platform.h>

QT_BEGIN_NAMESPACE

Q_LOGGING_CATEGORY(lcGraphicsFrameCapture, "qt.gui.graphicsframecapture")

#if __has_feature(objc_arc)
#error ARC not supported
#endif

uint QGraphicsFrameCaptureMetal::frameNumber = 0;

QGraphicsFrameCaptureMetal::QGraphicsFrameCaptureMetal()
{
    qputenv("METAL_CAPTURE_ENABLED", QByteArrayLiteral("1"));

    m_captureDescriptor = [MTLCaptureDescriptor new];
}

QGraphicsFrameCaptureMetal::~QGraphicsFrameCaptureMetal()
{
#if defined(Q_OS_MACOS) && QT_CONFIG(process)
    if (m_process) {
        m_process->terminate();
        delete m_process;
    }
#endif
    [m_captureDescriptor release];
}

void QGraphicsFrameCaptureMetal::setRhi(QRhi *rhi)
{
    if (!rhi)
        return;

    QRhi::Implementation backend = rhi->backend();
    const QRhiNativeHandles *nh = rhi->nativeHandles();

    switch (backend) {
    case QRhi::Implementation::Metal: {
        const  QRhiMetalNativeHandles *mtlnh = static_cast<const QRhiMetalNativeHandles *>(nh);
        if (mtlnh->cmdQueue) {
            m_captureDescriptor.captureObject = mtlnh->cmdQueue;
        } else if (mtlnh->dev) {
            m_captureDescriptor.captureObject = mtlnh->dev;
        } else {
            qCWarning(lcGraphicsFrameCapture) << "No valid Metal Device or Metal Command Queue found";
            m_initialized = false;
            return;
        }
        break;
    }
    default: {
        qCWarning(lcGraphicsFrameCapture) << "Invalid handles were provided. MTLCaptureManager works only with Metal API";
        m_initialized = false;
        return;
        break;
    }
    }

    if (!m_captureManager) {
        m_captureManager = MTLCaptureManager.sharedCaptureManager;
        bool supportDocs = [m_captureManager supportsDestination:MTLCaptureDestinationGPUTraceDocument];
        if (supportDocs) {
            m_captureDescriptor.destination = MTLCaptureDestinationGPUTraceDocument;
            m_initialized = true;
        }
    }
}

void QGraphicsFrameCaptureMetal::startCaptureFrame()
{
    if (!initialized()) {
        qCWarning(lcGraphicsFrameCapture) << "Capturing on Metal was not initialized. Starting capturing can not be done.";
        return;
    }

    if (isCapturing()) {
        qCWarning(lcGraphicsFrameCapture) << "A frame capture is already in progress,"
                                             "will not initiate another one until QGraphicsFrameCapture::endCaptureFrame is called.";
        return;
    }

    updateCaptureFileName();
    NSError *error;
    if (![m_captureManager startCaptureWithDescriptor:m_captureDescriptor error:&error]) {
        QString errorMsg = QString::fromNSString(error.localizedDescription);
        qCWarning(lcGraphicsFrameCapture, "Failed to start capture : %s", qPrintable(errorMsg));
    }
}

void QGraphicsFrameCaptureMetal::endCaptureFrame()
{
    if (!initialized()) {
        qCWarning(lcGraphicsFrameCapture) << "Capturing on Metal was not initialized. End capturing can not be done.";
        return;
    }

    if (!isCapturing()) {
        qCWarning(lcGraphicsFrameCapture) << "A call to QGraphicsFrameCapture::endCaptureFrame can not be done"
                                             " without a call to QGraphicsFrameCapture::startCaptureFrame";
        return;
    }

    [m_captureManager stopCapture];
    m_capturedFilesNames.append(QString::fromNSString(m_traceURL.path));
    frameNumber++;
}

bool QGraphicsFrameCaptureMetal::initialized() const
{
    return m_initialized;
}

bool QGraphicsFrameCaptureMetal::isCapturing() const
{
    if (!initialized()) {
        qCWarning(lcGraphicsFrameCapture) << "Capturing on Metal was not initialized. Can not query if capturing is in progress or not.";
        return false;
    }

    return [m_captureManager isCapturing];
}

void QGraphicsFrameCaptureMetal::openCapture()
{
#if defined(Q_OS_MACOS)
#if !QT_CONFIG(process)
    qFatal("QGraphicsFrameCapture requires QProcess on macOS");
#else
    if (!initialized()) {
        qCWarning(lcGraphicsFrameCapture) << "Capturing on Metal was not initialized. Can not open XCode with a valid capture.";
        return;
    }

    if (!m_process) {
        m_process = new QProcess();
        m_process->setProgram(QStringLiteral("xed"));
        QStringList args;
        args.append(QUrl::fromNSURL(m_traceURL).toLocalFile());
        m_process->setArguments(args);
    }

    m_process->kill();
    m_process->start();
#endif
#endif
}

void QGraphicsFrameCaptureMetal::updateCaptureFileName()
{
    m_traceURL = QUrl::fromLocalFile(m_capturePath + "/" + m_capturePrefix + "_" + QString::number(frameNumber) + ".gputrace").toNSURL();
    // We need to remove the trace file if it already existed else MTLCaptureManager
    // will fail to.
    if ([NSFileManager.defaultManager fileExistsAtPath:m_traceURL.path])
        [NSFileManager.defaultManager removeItemAtURL:m_traceURL error:nil];

    m_captureDescriptor.outputURL = m_traceURL;
}

QT_END_NAMESPACE