summaryrefslogtreecommitdiffstats
path: root/chalk/ui/kis_opengl_image_context.h
blob: 6bdca958d03afa75705c968d6125a6cdab07accc (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
/*
 *  Copyright (c) 2005 Adrian Page <[email protected]>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
#ifndef KIS_OPENGL_IMAGE_CONTEXT_H_
#define KIS_OPENGL_IMAGE_CONTEXT_H_

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#ifdef HAVE_GL

#include <map>

#include <tqgl.h>
#include <tqobject.h>
#include <tqvaluevector.h>

#include <koffice_export.h>

#include "kis_types.h"

class TQRegion;

class KisOpenGLImageContext;
typedef TDESharedPtr<KisOpenGLImageContext> KisOpenGLImageContextSP;
class KisColorSpace;

class KRITACORE_EXPORT KisOpenGLImageContext : public TQObject , public TDEShared {

    Q_OBJECT
  

public:
    static KisOpenGLImageContextSP getImageContext(KisImageSP image, KisProfile *monitorProfile);

    KisOpenGLImageContext();
    virtual ~KisOpenGLImageContext();

public:
    // In order to use the image textures, the caller must pass
    // the sharedContextWidget() as the shareWidget argument to the 
    // TQGLWidget constructor.
    TQGLWidget *sharedContextWidget() const;

    void setMonitorProfile(KisProfile *profile);
    void setHDRExposure(float exposure);

    GLuint backgroundTexture() const;

    static const int BACKGROUND_TEXTURE_WIDTH = 32;
    static const int BACKGROUND_TEXTURE_HEIGHT = 32;

    // Get the image texture tile containing the point (pixelX, pixelY).
    GLuint imageTextureTile(int pixelX, int pixelY) const;

    int imageTextureTileWidth() const;
    int imageTextureTileHeight() const;

    /**
     * Select selection visualisation rendering.
     * 
     * @param enable Set to true to enable selection visualisation rendering.
     */
    void setSelectionDisplayEnabled(bool enable);

    /**
     * Update the image textures for the given image rectangle.
     * 
     * @param imageRect The rectangle to update in image coordinates.
     */
    void update(const TQRect& imageRect);

signals:
    /**
     * Clients using the KisOpenGLImageContext should connect to the
     * following signals rather than to the KisImage's own equivalent
     * signals. This ensures that the image textures are always up to date
     * when used.
     */

    /**
     * Emitted whenever an action has caused the image to be recomposited.
     *
     * @param rc     The rect that has been recomposited.
     */
    void sigImageUpdated(TQRect rc);

    /**
     * Emitted whenever the image size changes.
     *
     * @param width  New image width 
     * @param height New image height
     */
    void sigSizeChanged(TQ_INT32 width, TQ_INT32 height);

protected:
    KisOpenGLImageContext(KisImageSP image, KisProfile *monitorProfile);

    void generateBackgroundTexture();
    void createImageTextureTiles();
    void destroyImageTextureTiles();
    int imageTextureTileIndex(int x, int y) const;
    void updateImageTextureTiles(const TQRect& rect);

    static KisColorSpace* textureColorSpaceForImageColorSpace(KisColorSpace *imageColorSpace);
    static bool imageCanShareImageContext(KisImageSP image);

protected slots:
    void slotImageUpdated(TQRect r);
    void slotImageSizeChanged(TQ_INT32 w, TQ_INT32 h);

private:
    KisImageSP m_image;
    KisProfile *m_monitorProfile;
    float m_exposure;
    bool m_displaySelection;

    GLuint m_backgroundTexture;

    static const int PREFERRED_IMAGE_TEXTURE_WIDTH = 256;
    static const int PREFERRED_IMAGE_TEXTURE_HEIGHT = 256;

    TQValueVector<GLuint> m_imageTextureTiles;
    int m_imageTextureTileWidth;
    int m_imageTextureTileHeight;
    int m_numImageTextureTileColumns;

    // We create a single OpenGL context and share it between all views
    // in the process. Apparently with some OpenGL implementations, only
    // one context will be hardware accelerated.
    static TQGLWidget *SharedContextWidget;
    static int SharedContextWidgetRefCount;

    typedef std::map<KisImageSP, KisOpenGLImageContext*> ImageContextMap;

    static ImageContextMap imageContextMap;
};

#endif // HAVE_GL

#endif // KIS_OPENGL_IMAGE_CONTEXT_H_