summaryrefslogtreecommitdiffstats
path: root/src/adaptationlayers/adaptationlayer.h
blob: e43571b0ed4005634a1785a1ed0c4c2f9d18fbd7 (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
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the Qt scene graph research project.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used 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.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef ADAPTATIONINTERFACES_H
#define ADAPTATIONINTERFACES_H

#include <QtCore/qobject.h>
#include <QtCore/qrect.h>
#include <QtGui/qcolor.h>
#include <QtCore/qsharedpointer.h>
#include <QtGui/qglyphs.h>
#include <QtCore/qurl.h>

#include "node.h"

class Node;
class QImage;
class TextureReference;

// TODO: Rename from XInterface to AbstractX.

class RectangleNodeInterface : public GeometryNode
{
public:
    RectangleNodeInterface() : m_radius(0), m_opacity(1), m_pen_width(0) { }

    virtual void setRect(const QRectF &rect) = 0;
    QRectF rect() const { return m_rect; }

    virtual void setColor(const QColor &color) = 0;
    QColor color() const { return m_color; }

    virtual void setPenColor(const QColor &color) = 0;
    QColor penColor() const { return m_pen_color; }

    virtual void setPenWidth(int width) = 0;
    int penWidth() const { return m_pen_width; }

    virtual void setOpacity(qreal opacity) = 0;
    qreal opacity() const { return m_opacity; }

    virtual void setGradientStops(const QGradientStops &stops) = 0;
    QGradientStops gradientStops() const { return m_gradient_stops; }

    virtual void setRadius(qreal radius) = 0;
    qreal radius() const { return m_radius; }

protected:
    QRectF m_rect;
    QGradientStops m_gradient_stops;
    QColor m_color;
    QColor m_pen_color;
    qreal m_radius;
    qreal m_opacity;
    int m_pen_width;
};

class TextureNodeInterface : public GeometryNode
{
public:
    TextureNodeInterface() : m_texture(0), m_opacity(1), m_clamp_to_edge(true), m_linear_filtering(false) { }

    virtual void setRect(const QRectF &rect) = 0;
    QRectF rect() const { return m_rect; }

    // Normalized source coordinates..
    virtual void setSourceRect(const QRectF &rect) = 0;
    QRectF sourceRect() const { return m_source_rect; }

    virtual void setOpacity(qreal opacity) = 0;
    qreal opacity() const { return m_opacity; }

    virtual void setTexture(const TextureReference *ref) = 0;
    const TextureReference *texture() const;

    virtual void setClampToEdge(bool clampToEdge) = 0;
    bool clampToEdge() const { return m_clamp_to_edge; }

    virtual void setLinearFiltering(bool linearFiltering) = 0;
    bool linearFiltering() const { return m_linear_filtering; }

protected:
    const TextureReference *m_texture;
    QRectF m_rect;
    QRectF m_source_rect;
    qreal m_opacity;
    bool m_clamp_to_edge;
    bool m_linear_filtering;

};

class TextureReference : public QObject
{
    Q_OBJECT
public:
    enum Status {
        Null,
        Uploaded,
        Uploading,
        Error
    };

    TextureReference();
    ~TextureReference();

    void setTextureId(int id) { m_texture_id = id; }
    int textureId() const { return m_texture_id; }

    void setTextureSize(const QSize &size) { m_texture_size = size; }
    QSize textureSize() const { return m_texture_size; }

    void setSubRect(const QRectF &subrect) { m_sub_rect = subrect; }
    QRectF subRect() const { return m_sub_rect; }

    void setAlphaChannel(bool hasAlpha) { m_has_alpha = hasAlpha; }
    bool hasAlphaChannel() const { return m_has_alpha; }

    void setOwnsTexture(bool owns) { m_owns_texture = owns; }
    bool ownsTexture() const { return m_owns_texture; }

    void setMipmaps(bool mipmapped) { m_mipmap = mipmapped; }
    bool hasMipmaps() const { return m_mipmap; }

    /*
      ### gunnar: the texture reference is potentially written to in a thread
      while uploading, so ANY access to the other accessors is strictly not
      allowed until status returns Uploaded.
      To support fetching and writing status from multiple threads, which I'm
      unsure if will happen, we should change status to be an QAtomicInt
     */

    void setStatus(Status s);
    Status status() const { return m_status; }

signals:
    void statusChanged(int status);

protected:

    Status m_status;
    int m_texture_id;

    QSize m_texture_size;
    QRectF m_sub_rect;

    uint m_has_alpha : 1;
    uint m_owns_texture : 1;
    uint m_mipmap : 1;
};

class TextureManager
{
public:
    enum UploadHint {
        SynchronousUploadHint = 0x0001,
        CanUseAtlasUploadHint = 0x0002,

        GenerateMipmapUploadHint = 0x0004,

        DefaultUploadHints = 0
    };
    Q_DECLARE_FLAGS(UploadHints, UploadHint);

    virtual ~TextureManager() { };

    virtual const TextureReference *requestUploadedTexture(const QImage &image, UploadHints hints = DefaultUploadHints, QObject *listener = 0, const char *slot = 0);
};


//typedef QSharedPointer<const QGLTexture2D> QGLTexture2DConstPtr;
//typedef QSharedPointer<QGLTexture2D> QGLTexture2DPtr;

//class TextureAtlasInterface
//{
//public:
//    enum Flag
//    {
//        DynamicFlag = 0x01 // Set if images are added to the texture atlas every few frames.
//    };

//    TextureAtlasInterface(uint flags) : m_flags(flags) { }
//    virtual ~TextureAtlasInterface() { }

//    virtual QGLTexture2DConstPtr texture() = 0;
//    virtual QRect allocate(const QImage &image, bool clampToEdge) = 0;
//    virtual void deallocate(const QRect &rect) = 0;

//    uint flags() const { return m_flags; }
//protected:
//    uint m_flags;
//};


class GlyphNodeInterface: public GeometryNode
{
public:
    GlyphNodeInterface() : m_opacity(1.0) {}

    virtual void setGlyphs(const QPointF &position, const QGlyphs &glyphs) = 0;
    QPointF position() const { return m_position; }
    QGlyphs glyphs() const { return m_glyphs; }

    virtual void setOpacity(qreal opacity) = 0;
    qreal opacity() const { return m_opacity; }

    virtual void setColor(const QColor &color) = 0;
    QColor color() const { return m_color; }

    virtual QPointF baseLine() const = 0;

protected:
    qreal m_opacity;
    QGlyphs m_glyphs;
    QPointF m_position;
    QColor m_color;
};

class AdaptationLayerInterface
{
public:
    virtual RectangleNodeInterface *createRectangleNode() = 0;
    virtual TextureNodeInterface *createTextureNode() = 0;
    virtual GlyphNodeInterface *createGlyphNode() = 0;
    virtual Renderer *createRenderer() = 0;    
    virtual TextureManager *textureManager() = 0;
};

#endif