summaryrefslogtreecommitdiff
path: root/src/mcompositewindowgroup.cpp
blob: 274c0fc74d441fcf343f84de6a4dcd6ef56cb391 (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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/***************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (directui@nokia.com)
**
** This file is part of mcompositor.
**
** If you have questions regarding the use of this file, please contact
** Nokia at directui@nokia.com.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation
** and appearing in the file LICENSE.LGPL included in the packaging
** of this file.
**
****************************************************************************/
/*!
  \class MCompositeWindowGroup
  \brief MCompositeWindowGroup allows a collection of windows to be rendered
  as a single texture.
  
  This class is useful for rendering a list of windows that needs to 
  be treated as one item with transformations applied only to a single texture.
  Unlike QGraphicsItemGroup where each item is rendered separately for each
  frame, MCompositeWindowGroup can pre-render all items to an off-screen buffer 
  before a frame starts and can render the resulting texture afterwards as a 
  single quad which reduces GPU load and helps performance especially in panning
  and scaling transformations. 
  
  Use this class to render a main window with its transient windows. Another
  use case is for a window that needs to have similar transformations to a
  main window. The addChildWindow() function adds windows that need to 
  animate synchronously  with the main window. The removeChildWindow() function
  removes a window from the group.  

  Implementation is hw-dependent. It relies on framebuffer objects on GLES2 
  and the GL_EXT_framebuffer_object extension on the desktop.
*/

#include <QtOpenGL> 
#include <QList>
#include <mcompositewindowgroup.h>

#include <mtexturepixmapitem.h>
#include <mcompositemanager.h>

#ifdef GLES2_VERSION
#define FORMAT GL_RGBA
#define DEPTH GL_DEPTH_COMPONENT16
#else
#define FORMAT GL_RGBA8
#define DEPTH GL_DEPTH_COMPONENT
#endif

class MCompositeWindowGroupPrivate
{
public:
    MCompositeWindowGroupPrivate(MTexturePixmapItem* mainWindow)
        :main_window(mainWindow),
         texture(0),
         fbo(0),
         depth_buffer(0),
         valid(false),
         renderer(new MTexturePixmapPrivate(0, mainWindow))            
    {       
    }
    MTexturePixmapItem* main_window;
    GLuint texture;
    GLuint fbo;
    GLuint depth_buffer;
    
    bool valid;
    QList<MTexturePixmapItem*> item_list;
    MTexturePixmapPrivate* renderer;
};

/*!
  Creates a window group object. Specify the main window
  with \a mainWindow
 */
MCompositeWindowGroup::MCompositeWindowGroup(MTexturePixmapItem* mainWindow)
    :MCompositeWindow(0, MWindowDummyPropertyCache::get()),
     d_ptr(new MCompositeWindowGroupPrivate(mainWindow))
{
    MCompositeManager *p = (MCompositeManager *) qApp;
    p->scene()->addItem(this);
    
    mainWindow->d->current_window_group = this;
    connect(mainWindow, SIGNAL(destroyed()), SLOT(deleteLater()));
    init();
    updateWindowPixmap();
    setZValue(mainWindow->zValue());
    stackBefore(mainWindow);
}

/*!
  Destroys this window group and frees resources. All windows that are within 
  this group revert back to directly rendering its own texture.
 */
MCompositeWindowGroup::~MCompositeWindowGroup()
{
    Q_D(MCompositeWindowGroup);
    
    if (!QGLContext::currentContext()) {
        qWarning("MCompositeWindowGroup::%s(): no current GL context",
                 __func__);
        return;
    }
    
    GLuint texture = d->texture;
    glDeleteTextures(1, &texture);
    GLuint depth_buffer = d->depth_buffer;
    glDeleteRenderbuffers(1, &depth_buffer);
    GLuint fbo = d->fbo;
    glDeleteFramebuffers(1, &fbo);
    glBindFramebuffer(GL_FRAMEBUFFER, 0);

    update();
}

void MCompositeWindowGroup::init()
{
    Q_D(MCompositeWindowGroup);
    
    if (!QGLContext::currentContext()) {
        qWarning("MCompositeWindowGroup::%s(): no current GL context",
                 __func__);
        d->valid = false;
        return;
    }
    d->renderer->current_window_group = this;
    
    glGenFramebuffers(1, &d->fbo);
    glGenRenderbuffers(1, &d->depth_buffer);
    glBindFramebuffer(GL_RENDERBUFFER, d->fbo);
    
    glGenTextures(1, &d->texture);
    glBindTexture(GL_TEXTURE_2D, d->texture);
    
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    
    glBindRenderbuffer(GL_RENDERBUFFER, d->depth_buffer);
    glRenderbufferStorage(GL_RENDERBUFFER, DEPTH, 
                          d->main_window->boundingRect().width(), 
                          d->main_window->boundingRect().height());

    glBindFramebuffer(GL_FRAMEBUFFER, d->fbo);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
                           GL_TEXTURE_2D, d->texture, 0);

    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
                              GL_RENDERBUFFER, d->depth_buffer);
    
    glBindTexture(GL_TEXTURE_2D, d->texture);
    glTexImage2D(GL_TEXTURE_2D, 0, FORMAT, 
                 d->main_window->boundingRect().width(), 
                 d->main_window->boundingRect().height(), 0,
                 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
    
    GLenum ret = glCheckFramebufferStatus(GL_FRAMEBUFFER);
    if (ret == GL_FRAMEBUFFER_COMPLETE)
        d->valid = true;
    else         
        qWarning("MCompositeWindowGroup::%s(): incomplete FBO attachment 0x%x",
                 __func__, ret);           

    glBindTexture(GL_TEXTURE_2D, 0);
    glBindFramebuffer(GL_FRAMEBUFFER, 0);    
}

static bool behindCompare(MTexturePixmapItem* a, MTexturePixmapItem* b)
{
    return a->indexInStack() < b->indexInStack();
}

/*!
  Adds the given \a window to this group. The window's transformations will
  remain unmodified
 */
void MCompositeWindowGroup::addChildWindow(MTexturePixmapItem* window)
{
    Q_D(MCompositeWindowGroup);
    window->d->current_window_group = this;
    connect(window, SIGNAL(destroyed()), SLOT(q_removeWindow()));
    d->item_list.append(window);
    
    // ensure group windows are already stacked in proper order in advance
    // for back to front rendering. Could use depth buffer attachment at some 
    // point so this might be unecessary
    qSort(d->item_list.begin(), d->item_list.end(), behindCompare);
    updateWindowPixmap();
}

/*!
  Removes the given \a window to from group. 
 */
void MCompositeWindowGroup::removeChildWindow(MTexturePixmapItem* window)
{
    Q_D(MCompositeWindowGroup);
    window->d->current_window_group = 0;
    d->item_list.removeAll(window);
}

void MCompositeWindowGroup::q_removeWindow()
{
    Q_D(MCompositeWindowGroup);
    MTexturePixmapItem* w = qobject_cast<MTexturePixmapItem*>(sender());
    if (w) 
        d->item_list.removeAll(w);
}

void MCompositeWindowGroup::saveBackingStore() {}

void MCompositeWindowGroup::resize(int , int) {}

void MCompositeWindowGroup::clearTexture() {}

bool MCompositeWindowGroup::isDirectRendered() const { return false; }

QRectF MCompositeWindowGroup::boundingRect() const 
{
    Q_D(const MCompositeWindowGroup);
    return d->main_window->boundingRect();
}

void MCompositeWindowGroup::paint(QPainter* painter, 
                                  const QStyleOptionGraphicsItem* options, 
                                  QWidget* widget) 
{    
    Q_D(MCompositeWindowGroup);
    Q_UNUSED(options)
    Q_UNUSED(widget)

#if QT_VERSION < 0x040600
    if (painter->paintEngine()->type() != QPaintEngine::OpenGL)
        return;
#else
    if (painter->paintEngine()->type() != QPaintEngine::OpenGL2) {
        return;
    }
#endif
    
    glBindTexture(GL_TEXTURE_2D, d->texture);    
    if (d->main_window->propertyCache()->hasAlpha() || 
        (opacity() < 1.0f && !dimmedEffect()) ) {
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    }
    d->renderer->drawTexture(painter->combinedTransform(), boundingRect(), 
                             opacity());    
    glBlendFunc(GL_ONE, GL_ZERO);
    glDisable(GL_BLEND);
}

void MCompositeWindowGroup::windowRaised()
{
}

void MCompositeWindowGroup::updateWindowPixmap(XRectangle *rects, int num,
                                               Time t)
{
    Q_UNUSED(rects)
    Q_UNUSED(num)
    Q_UNUSED(t)
    Q_D(MCompositeWindowGroup);
    
    if (!d->valid) {
        qDebug() << "invalid fbo";
        return;
    }

    glBindFramebuffer(GL_FRAMEBUFFER, d->fbo);
    GLenum ret = glCheckFramebufferStatus(GL_FRAMEBUFFER);
    if (ret == GL_FRAMEBUFFER_COMPLETE)
        d->valid = true;
    else {
        qWarning("MCompositeWindowGroup::%s(): incomplete FBO attachment 0x%x",
                 __func__, ret); 
        d->valid = false;
    }
    d->main_window->d->inverted_texture = false;
    d->main_window->renderTexture(d->main_window->sceneTransform());
    d->main_window->d->inverted_texture = true;
    for (int i = 0; i < d->item_list.size(); ++i) {
        MTexturePixmapItem* item = d->item_list[i];
        item->d->inverted_texture = false;
        item->renderTexture(item->sceneTransform());
        item->d->inverted_texture = true;
    }
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
}

// internal re-implementation from MCompositeWindow
MTexturePixmapPrivate* MCompositeWindowGroup::renderer() const
{
    Q_D(const MCompositeWindowGroup);
    return d->renderer;
}

/*!
  \return Returns the texture used by the off-screen buffer
*/
GLuint MCompositeWindowGroup::texture()
{
    Q_D(MCompositeWindowGroup);
    return d->texture;
}