1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the plugins of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef QBACKINGSTORE_COCOA_H
41 #define QBACKINGSTORE_COCOA_H
42 
43 #include <QtGraphicsSupport/private/qrasterbackingstore_p.h>
44 
45 #include <private/qcore_mac_p.h>
46 
47 #include <QScopedPointer>
48 #include "qiosurfacegraphicsbuffer.h"
49 
50 #include <unordered_map>
51 
52 QT_BEGIN_NAMESPACE
53 
54 class QCocoaBackingStore : public QRasterBackingStore
55 {
56 protected:
57     QCocoaBackingStore(QWindow *window);
58     QCFType<CGColorSpaceRef> colorSpace() const;
59 };
60 
61 class QNSWindowBackingStore : public QCocoaBackingStore
62 {
63 public:
64     QNSWindowBackingStore(QWindow *window);
65     ~QNSWindowBackingStore();
66 
67     void resize(const QSize &size, const QRegion &staticContents) override;
68     void flush(QWindow *, const QRegion &, const QPoint &) override;
69 
70 private:
71     bool windowHasUnifiedToolbar() const;
72     QImage::Format format() const override;
73     void redrawRoundedBottomCorners(CGRect) const;
74 };
75 
76 class QCALayerBackingStore : public QObject, public QCocoaBackingStore
77 {
78     Q_OBJECT
79 public:
80     QCALayerBackingStore(QWindow *window);
81     ~QCALayerBackingStore();
82 
83     void resize(const QSize &size, const QRegion &staticContents) override;
84 
85     void beginPaint(const QRegion &region) override;
86     QPaintDevice *paintDevice() override;
87     void endPaint() override;
88 
89     void flush(QWindow *, const QRegion &, const QPoint &) override;
90 #ifndef QT_NO_OPENGL
91     void composeAndFlush(QWindow *window, const QRegion &region, const QPoint &offset,
92         QPlatformTextureList *textures, bool translucentBackground) override;
93 #endif
94 
95     QImage toImage() const override;
96     QPlatformGraphicsBuffer *graphicsBuffer() const override;
97 
98 private:
99     void observeBackingPropertiesChanges();
100     bool eventFilter(QObject *watched, QEvent *event) override;
101 
102     QSize m_requestedSize;
103     QRegion m_paintedRegion;
104 
105     class GraphicsBuffer : public QIOSurfaceGraphicsBuffer
106     {
107     public:
108         GraphicsBuffer(const QSize &size, qreal devicePixelRatio,
109                 const QPixelFormat &format, QCFType<CGColorSpaceRef> colorSpace);
110 
111         QRegion dirtyRegion; // In unscaled coordinates
112         QImage *asImage();
devicePixelRatio()113         qreal devicePixelRatio() const { return m_devicePixelRatio; }
114 
115     private:
116         qreal m_devicePixelRatio;
117         QImage m_image;
118     };
119 
120     void ensureBackBuffer();
121     bool recreateBackBufferIfNeeded();
122     bool prepareForFlush();
123 
124     void backingPropertiesChanged();
125     QMacNotificationObserver m_backingPropertiesObserver;
126 
127     std::list<std::unique_ptr<GraphicsBuffer>> m_buffers;
128 
129     void flushSubWindow(QWindow *window);
130     std::unordered_map<QWindow*, std::unique_ptr<QCALayerBackingStore>> m_subWindowBackingstores;
131     void windowDestroyed(QObject *object);
132     bool m_clearSurfaceOnPaint = true;
133 };
134 
135 QT_END_NAMESPACE
136 
137 #endif
138