Cocoa: reimplement QPlatformBackingStore::scroll()
[profile/ivi/qtbase.git] / src / plugins / platforms / xcb / qxcbsharedbuffermanager.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the plugins of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef XCBSHAREDBUFFERMANAGER_H
43 #define XCBSHAREDBUFFERMANAGER_H
44
45 #if defined(QT_USE_XCB_SHARED_GRAPHICS_CACHE)
46
47 #include <QtCore/qset.h>
48 #include <QtCore/qhash.h>
49 #include <QtCore/qsharedmemory.h>
50
51 #include <GLES2/gl2.h>
52
53 #include <EGL/egl.h>
54
55 #include <EGL/eglext.h>
56
57 class wl_resource;
58
59 QT_BEGIN_HEADER
60
61 QT_BEGIN_NAMESPACE
62
63 class QXcbSharedBufferManager
64 {
65 public:
66     struct Buffer {
67         Buffer()
68             : width(-1)
69             , height(-1)
70             , bytesPerPixel(1)
71             , nextX(-1)
72             , nextY(-1)
73             , currentLineMaxHeight(0)
74             , next(0)
75             , prev(0)
76             , buffer(0)
77             , textureId(0)
78         {
79         }
80
81         ~Buffer()
82         {
83             delete buffer;
84
85             if (textureId != 0)
86                 glDeleteTextures(1, &textureId);
87         }
88
89         QByteArray cacheId;
90         int width;
91         int height;
92         int bytesPerPixel;
93         int nextX;
94         int nextY;
95         int currentLineMaxHeight;
96
97         Buffer *next;
98         Buffer *prev;
99
100         QSharedMemory *buffer;
101
102         GLuint textureId;
103
104         QAtomicInt ref;
105     };
106
107     typedef QHash<QByteArray, QSet<quint32> > PendingItemIds;
108
109     QXcbSharedBufferManager();
110     ~QXcbSharedBufferManager();
111
112     void beginSharedBufferAction(const QByteArray &cacheId);
113     void insertItem(quint32 itemId, uchar *data, int itemWidth, int itemHeight);
114     void requestItems(const QSet<quint32> &itemIds);
115     void releaseItems(const QSet<quint32> &itemIds);
116     void endSharedBufferAction();
117
118     void getBufferForItem(const QByteArray &cacheId, quint32 itemId, Buffer **buffer,
119                           int *x, int *y) const;
120     QPair<QByteArray, int> serializeBuffer(QSharedMemory *buffer) const;
121
122     PendingItemIds pendingItemsInvalidated() const
123     {
124         Q_ASSERT_X(m_currentCacheId.isEmpty(), Q_FUNC_INFO,
125                    "Call endSharedBufferAction() before accessing data");
126         return m_pendingInvalidatedItems;
127     }
128
129     PendingItemIds pendingItemsReady() const
130     {
131         Q_ASSERT_X(m_currentCacheId.isEmpty(), Q_FUNC_INFO,
132                    "Call endSharedBufferAction() before accessing data");
133         return m_pendingReadyItems;
134     }
135
136     PendingItemIds pendingItemsMissing() const
137     {
138         Q_ASSERT_X(m_currentCacheId.isEmpty(), Q_FUNC_INFO,
139                    "Call endSharedBufferAction() before accessing data");
140         return m_pendingMissingItems;
141     }
142
143 private:
144     struct Item {
145         Item()
146             : next(0)
147             , prev(0)
148             , buffer(0)
149             , itemId(0)
150             , x(-1)
151             , y(-1)
152             , width(-1)
153             , height(-1)
154         {
155         }
156
157         Item *next;
158         Item *prev;
159
160         Buffer *buffer;
161         quint32 itemId;
162         int x;
163         int y;
164         int width;
165         int height;
166     };
167
168     struct Items
169     {
170         Items() : leastRecentlyUsed(0), mostRecentlyUsed(0) {}
171
172         Item *leastRecentlyUsed;
173         Item *mostRecentlyUsed;
174
175         QByteArray cacheId;
176         QHash<quint32, Item *> items;
177     };
178
179     void findAvailableBuffer(int itemWidth, int itemHeight, Buffer **buffer, int *x, int *y);
180     void recycleItem(Buffer **buffer, int *x, int *y);
181     void copyIntoBuffer(Buffer *buffer, int x, int y, int itemWidth, int itemHeight, uchar *data);
182     void touchBuffer(Buffer *buffer);
183     void deleteLeastRecentlyUsed();
184
185     Buffer *createNewBuffer(const QByteArray &cacheId, int heightRequired);
186     Buffer *resizeBuffer(Buffer *buffer, const QSize &newSize);
187     Buffer *allocateBuffer(int width, int height);
188
189     Items *itemsForCache(const QByteArray &cacheId) const;
190     void pushItemToBack(Items *items, Item *item);
191     void touchItem(Items *items, Item *item);
192     void deleteItem(Items *items, Item *item);
193     void recycleItem(const QByteArray &cacheId, Buffer **sharedBuffer, int *glyphX, int *glyphY);
194
195     QByteArray m_currentCacheId;
196
197     quint32 m_memoryUsed;
198     Buffer *m_mostRecentlyUsed;
199     Buffer *m_leastRecentlyUsed;
200
201     mutable QHash<QByteArray, Items *> m_items;
202     QMultiHash<QByteArray, Buffer *> m_buffers;
203
204     PendingItemIds m_pendingInvalidatedItems;
205     PendingItemIds m_pendingReadyItems;
206     PendingItemIds m_pendingMissingItems;
207 };
208
209 QT_END_NAMESPACE
210
211 QT_END_HEADER
212
213 #endif // QT_USE_XCB_SHARED_GRAPHICS_CACHE
214
215 #endif // XCBSHAREDBUFFERMANAGER_H