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
|
/*
quickscreengrabber.h
This file is part of GammaRay, the Qt application inspection and manipulation tool.
SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
Author: Filipe Azevedo <filipe.azevedo@kdab.com>
SPDX-License-Identifier: GPL-2.0-or-later
Contact KDAB at <info@kdab.com> for commercial licensing options.
*/
#ifndef GAMMARAY_QUICKINSPECTOR_QUICKSCREENGRABBER_H
#define GAMMARAY_QUICKINSPECTOR_QUICKSCREENGRABBER_H
#include "quickdecorationsdrawer.h"
#include <QObject>
#include <QPointer>
#include <QQuickItem>
#include <QMutex>
#include <memory>
QT_BEGIN_NAMESPACE
class QQuickWindow;
#ifndef QT_NO_OPENGL
class QOpenGLPaintDevice;
#endif
class QSGSoftwareRenderer;
QT_END_NAMESPACE
namespace GammaRay {
class ItemOrLayoutFacade
{
public:
ItemOrLayoutFacade() = default;
ItemOrLayoutFacade(QQuickItem *item); // krazy:exclude=explicit
/// Get either the layout of the widget or the this-pointer
QQuickItem *layout() const;
/// Get either the parent widget of the layout or the this-pointer
QQuickItem *item() const;
QRectF geometry() const;
bool isVisible() const;
QPointF pos() const;
inline bool isNull() const
{
return !m_object;
}
inline QQuickItem *data()
{
return m_object;
}
inline QQuickItem *operator->() const
{
Q_ASSERT(!isNull());
return m_object;
}
inline void clear()
{
m_object = nullptr;
}
private:
bool isLayout() const;
inline QQuickItem *asLayout() const
{
return m_object;
}
inline QQuickItem *asItem() const
{
return m_object;
}
QPointer<QQuickItem> m_object;
};
class GrabbedFrame
{
public:
QImage image;
QTransform transform;
QRectF itemsGeometryRect;
QVector<QuickItemGeometry> itemsGeometry;
};
class AbstractScreenGrabber : public QObject
{
Q_OBJECT
public:
struct RenderInfo
{
// Keep in sync with QSGRendererInterface::GraphicsApi
enum GraphicsApi
{
Unknown = 0,
Software,
OpenVG,
OpenGL,
Direct3D11,
Vulkan,
Metal,
Null,
};
RenderInfo()
: dpr(qQNaN())
{
}
qreal dpr;
QPoint windowPosition;
QSize windowSize;
GraphicsApi graphicsApi = Unknown;
};
explicit AbstractScreenGrabber(QQuickWindow *window);
~AbstractScreenGrabber() override;
static RenderInfo::GraphicsApi graphicsApiFor(QQuickWindow *window);
static std::unique_ptr<AbstractScreenGrabber> get(QQuickWindow *window);
QQuickWindow *window() const;
QuickDecorationsSettings settings() const;
void setSettings(const QuickDecorationsSettings &settings);
bool decorationsEnabled() const;
void setDecorationsEnabled(bool enabled);
/**
* Place the overlay on @p item
*
* @param item The overlay can be cover a widget or a layout of the current window
*/
void placeOn(const ItemOrLayoutFacade &item);
virtual void requestGrabWindow(const QRectF &userViewport) = 0;
signals:
void grabberReadyChanged(bool ready);
void sceneChanged();
void sceneGrabbed(const GammaRay::GrabbedFrame &frame);
protected:
void doDrawDecorations(QPainter &painter);
void gatherRenderInfo();
virtual void drawDecorations() = 0;
virtual void updateOverlay();
static QuickItemGeometry initFromItem(QQuickItem *item);
private:
void itemParentChanged(QQuickItem *parent);
void itemWindowChanged(QQuickWindow *window);
void connectItemChanges(QQuickItem *item) const;
void disconnectItemChanges(QQuickItem *item) const;
void connectTopItemChanges(QQuickItem *item) const;
void disconnectTopItemChanges(QQuickItem *item) const;
protected:
QPointer<QQuickWindow> m_window;
QPointer<QQuickItem> m_currentToplevelItem;
ItemOrLayoutFacade m_currentItem;
QuickDecorationsSettings m_settings;
bool m_decorationsEnabled = true;
QRectF m_userViewport;
GrabbedFrame m_grabbedFrame;
RenderInfo m_renderInfo;
};
#ifndef QT_NO_OPENGL
class OpenGLScreenGrabber : public AbstractScreenGrabber
{
Q_OBJECT
public:
explicit OpenGLScreenGrabber(QQuickWindow *window);
~OpenGLScreenGrabber() override;
void requestGrabWindow(const QRectF &userViewport) override;
void drawDecorations() override;
private:
void setGrabbingMode(bool isGrabbingMode, const QRectF &userViewport);
void windowAfterSynchronizing();
void windowAfterRendering();
bool m_isGrabbing;
QMutex m_mutex;
};
#endif
class SoftwareScreenGrabber : public AbstractScreenGrabber
{
Q_OBJECT
public:
explicit SoftwareScreenGrabber(QQuickWindow *window);
~SoftwareScreenGrabber() override;
void requestGrabWindow(const QRectF &userViewport) override;
void drawDecorations() override;
private:
void windowAfterRendering();
void windowBeforeRendering();
void updateOverlay() override;
QSGSoftwareRenderer *softwareRenderer() const;
bool m_isGrabbing = false;
QPointF m_lastItemPosition;
};
class UnsupportedScreenGrabber : public AbstractScreenGrabber
{
Q_OBJECT
public:
explicit UnsupportedScreenGrabber(QQuickWindow *window);
~UnsupportedScreenGrabber() override;
void requestGrabWindow(const QRectF &userViewport) override;
void drawDecorations() override;
private:
void updateOverlay() override;
QPointF m_lastItemPosition;
};
}
Q_DECLARE_METATYPE(GammaRay::GrabbedFrame)
#endif
|