backend.h 10.6 KB
Newer Older
Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
1 2 3
/*
    KWin - the KDE window manager
    This file is part of the KDE project.
4

Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
5 6
    SPDX-FileCopyrightText: 2006 Lubos Lunak <l.lunak@kde.org>
    SPDX-FileCopyrightText: 2009, 2010, 2011 Martin Gräßlin <mgraesslin@kde.org>
7

Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
8 9
    SPDX-License-Identifier: GPL-2.0-or-later
*/
10 11 12 13 14 15 16 17 18 19
#ifndef KWIN_SCENE_OPENGL_BACKEND_H
#define KWIN_SCENE_OPENGL_BACKEND_H

#include <QElapsedTimer>
#include <QRegion>

#include <kwin_export.h>

namespace KWin
{
20
class AbstractOutput;
21 22 23 24 25 26
class OpenGLBackend;
class OverlayWindow;
class SceneOpenGL;
class SceneOpenGLTexture;
class SceneOpenGLTexturePrivate;
class WindowPixmap;
27
class GLTexture;
28 29 30 31 32 33 34 35 36 37 38 39 40 41

/**
 * @brief The OpenGLBackend creates and holds the OpenGL context and is responsible for Texture from Pixmap.
 *
 * The OpenGLBackend is an abstract base class used by the SceneOpenGL to abstract away the differences
 * between various OpenGL windowing systems such as GLX and EGL.
 *
 * A concrete implementation has to create and release the OpenGL context in a way so that the
 * SceneOpenGL does not have to care about it.
 *
 * In addition a major task for this class is to generate the SceneOpenGLTexturePrivate which is
 * able to perform the texture from pixmap operation in the given backend.
 *
 * @author Martin Gräßlin <mgraesslin@kde.org>
42
 */
43 44 45 46 47 48 49 50 51 52
class KWIN_EXPORT OpenGLBackend
{
public:
    OpenGLBackend();
    virtual ~OpenGLBackend();

    virtual void init() = 0;
    /**
     * @return Time passes since start of rendering current frame.
     * @see startRenderTimer
53
     */
54 55 56 57 58 59 60 61 62 63 64
    qint64 renderTime() {
        return m_renderTimer.nsecsElapsed();
    }
    virtual void screenGeometryChanged(const QSize &size) = 0;
    virtual SceneOpenGLTexturePrivate *createBackendTexture(SceneOpenGLTexture *texture) = 0;

    /**
     * @brief Backend specific code to prepare the rendering of a frame including flushing the
     * previously rendered frame to the screen if the backend works this way.
     *
     * @return A region that if not empty will be repainted in addition to the damaged region
65
     */
66 67
    virtual QRegion prepareRenderingFrame() = 0;

68 69 70 71 72
    /**
     * Notifies about starting to paint.
     *
     * @p damage contains the reported damage as suggested by windows and effects on prepaint calls.
     */
73
    virtual void aboutToStartPainting(int screenId, const QRegion &damage);
74

75 76 77 78 79
    /**
     * @brief Backend specific code to handle the end of rendering a frame.
     *
     * @param renderedRegion The possibly larger region that has been rendered
     * @param damagedRegion The damaged region that should be posted
80
     */
81 82 83 84 85 86 87 88
    virtual void endRenderingFrame(const QRegion &damage, const QRegion &damagedRegion) = 0;
    virtual void endRenderingFrameForScreen(int screenId, const QRegion &damage, const QRegion &damagedRegion);
    virtual bool makeCurrent() = 0;
    virtual void doneCurrent() = 0;
    virtual bool usesOverlayWindow() const = 0;
    /**
     * Whether the rendering needs to be split per screen.
     * Default implementation returns @c false.
89
     */
90 91 92 93
    virtual bool perScreenRendering() const;
    virtual QRegion prepareRenderingForScreen(int screenId);
    /**
     * @brief Compositor is going into idle mode, flushes any pending paints.
94
     */
95 96 97 98
    void idle();

    /**
     * @return bool Whether the scene needs to flush a frame.
99
     */
100 101 102 103 104 105 106 107 108 109 110 111
    bool hasPendingFlush() const {
        return !m_lastDamage.isEmpty();
    }

    /**
     * @brief Returns the OverlayWindow used by the backend.
     *
     * A backend does not have to use an OverlayWindow, this is mostly for the X world.
     * In case the backend does not use an OverlayWindow it is allowed to return @c null.
     * It's the task of the caller to check whether it is @c null.
     *
     * @return :OverlayWindow*
112
     */
113
    virtual OverlayWindow *overlayWindow() const;
114 115 116 117 118 119 120
    /**
     * @brief Whether the creation of the Backend failed.
     *
     * The SceneOpenGL should test whether the Backend got constructed correctly. If this method
     * returns @c true, the SceneOpenGL should not try to start the rendering.
     *
     * @return bool @c true if the creation of the Backend failed, @c false otherwise.
121
     */
122 123 124
    bool isFailed() const {
        return m_failed;
    }
125 126 127 128 129 130 131 132 133 134
    /**
     * @brief Whether the Backend provides VSync.
     *
     * Currently only the GLX backend can provide VSync.
     *
     * @return bool @c true if VSync support is available, @c false otherwise
     */
    bool syncsToVBlank() const {
        return m_syncsToVBlank;
    }
135 136 137
    /**
     * @brief Whether VSync blocks execution until the screen is in the retrace
     *
138
     * Case for waitVideoSync and non triple buffering buffer swaps
139 140 141 142 143
     *
     */
    bool blocksForRetrace() const {
        return m_blocksForRetrace;
    }
144 145 146 147 148 149 150
    /**
     * @brief Whether the backend uses direct rendering.
     *
     * Some OpenGLScene modes require direct rendering. E.g. the OpenGL 2 should not be used
     * if direct rendering is not supported by the Scene.
     *
     * @return bool @c true if the GL context is direct, @c false if indirect
151
     */
152 153 154 155 156 157 158 159
    bool isDirectRendering() const {
        return m_directRendering;
    }

    bool supportsBufferAge() const {
        return m_haveBufferAge;
    }

160 161 162 163 164 165 166 167 168
    bool supportsPartialUpdate() const
    {
        return m_havePartialUpdate;
    }
    bool supportsSwapBuffersWithDamage() const
    {
        return m_haveSwapBuffersWithDamage;
    }

169 170 171
    bool supportsSurfacelessContext() const
    {
        return m_haveSurfacelessContext;
172
    }
173 174 175 176
    bool supportsNativeFence() const
    {
        return m_haveNativeFence;
    }
177 178 179

    /**
     * Returns the damage that has accumulated since a buffer of the given age was presented.
180
     */
181 182 183 184
    QRegion accumulatedDamageHistory(int bufferAge) const;

    /**
     * Saves the given region to damage history.
185
     */
186 187 188 189 190 191
    void addToDamageHistory(const QRegion &region);

    /**
     * The backend specific extensions (e.g. EGL/GLX extensions).
     *
     * Not the OpenGL (ES) extension!
192
     */
193 194 195 196 197 198
    QList<QByteArray> extensions() const {
        return m_extensions;
    }

    /**
     * @returns whether the backend specific extensions contains @p extension.
199
     */
200 201 202 203 204 205
    bool hasExtension(const QByteArray &extension) const {
        return m_extensions.contains(extension);
    }

    /**
     * Copy a region of pixels from the current read to the current draw buffer
206
     */
207 208
    void copyPixels(const QRegion &region);

209 210
    virtual QSharedPointer<GLTexture> textureForOutput(AbstractOutput *output) const;

211 212 213
protected:
    /**
     * @brief Backend specific flushing of frame to screen.
214
     */
215 216 217 218 219 220 221 222
    virtual void present() = 0;
    /**
     * @brief Sets the backend initialization to failed.
     *
     * This method should be called by the concrete subclass in case the initialization failed.
     * The given @p reason is logged as a warning.
     *
     * @param reason The reason why the initialization failed.
223
     */
224
    void setFailed(const QString &reason);
225 226 227 228 229 230 231 232 233 234
    /**
     * @brief Sets whether the backend provides VSync.
     *
     * Should be called by the concrete subclass once it is determined whether VSync is supported.
     * If the subclass does not call this method, the backend defaults to @c false.
     * @param enabled @c true if VSync support available, @c false otherwise.
     */
    void setSyncsToVBlank(bool enabled) {
        m_syncsToVBlank = enabled;
    }
235 236 237 238 239 240 241 242 243 244
    /**
     * @brief Sets whether the VSync iplementation blocks
     *
     * Should be called by the concrete subclass once it is determined how VSync works.
     * If the subclass does not call this method, the backend defaults to @c false.
     * @param enabled @c true if VSync blocks, @c false otherwise.
     */
    void setBlocksForRetrace(bool enabled) {
        m_blocksForRetrace = enabled;
    }
245 246 247 248 249 250 251 252
    /**
     * @brief Sets whether the OpenGL context is direct.
     *
     * Should be called by the concrete subclass once it is determined whether the OpenGL context is
     * direct or indirect.
     * If the subclass does not call this method, the backend defaults to @c false.
     *
     * @param direct @c true if the OpenGL context is direct, @c false if indirect
253
     */
254 255 256 257 258 259 260 261
    void setIsDirectRendering(bool direct) {
        m_directRendering = direct;
    }

    void setSupportsBufferAge(bool value) {
        m_haveBufferAge = value;
    }

262 263 264 265 266 267 268 269 270 271
    void setSupportsPartialUpdate(bool value)
    {
        m_havePartialUpdate = value;
    }

    void setSupportsSwapBuffersWithDamage(bool value)
    {
        m_haveSwapBuffersWithDamage = value;
    }

272 273 274 275 276
    void setSupportsSurfacelessContext(bool value)
    {
        m_haveSurfacelessContext = value;
    }

277 278 279 280 281
    void setSupportsNativeFence(bool value)
    {
        m_haveNativeFence = value;
    }

282 283
    /**
     * @return const QRegion& Damage of previously rendered frame
284
     */
285 286 287 288 289 290 291 292 293 294
    const QRegion &lastDamage() const {
        return m_lastDamage;
    }
    void setLastDamage(const QRegion &damage) {
        m_lastDamage = damage;
    }
    /**
     * @brief Starts the timer for how long it takes to render the frame.
     *
     * @see renderTime
295
     */
296 297 298 299 300 301 302 303
    void startRenderTimer() {
        m_renderTimer.start();
    }

    /**
     * Sets the platform-specific @p extensions.
     *
     * These are the EGL/GLX extensions, not the OpenGL extensions
304
     */
305 306 307 308 309
    void setExtensions(const QList<QByteArray> &extensions) {
        m_extensions = extensions;
    }

private:
310 311 312 313
    /**
     * @brief Whether VSync is available and used, defaults to @c false.
     */
    bool m_syncsToVBlank;
314 315 316 317
    /**
     * @brief Whether present() will block execution until the next vertical retrace @c false.
     */
    bool m_blocksForRetrace;
318 319
    /**
     * @brief Whether direct rendering is used, defaults to @c false.
320
     */
321 322 323
    bool m_directRendering;
    /**
     * @brief Whether the backend supports GLX_EXT_buffer_age / EGL_EXT_buffer_age.
324
     */
325
    bool m_haveBufferAge;
326 327 328 329 330
    /**
     * @brief Whether the backend supports EGL_KHR_partial_update
     */
    bool m_havePartialUpdate;
    bool m_haveSwapBuffersWithDamage = false;
331 332 333 334
    /**
     * @brief Whether the backend supports EGL_KHR_surfaceless_context.
     */
    bool m_haveSurfacelessContext = false;
335 336 337 338
    /**
     * @brief Whether the backend supports EGL_ANDROID_native_fence_sync.
     */
    bool m_haveNativeFence = false;
339 340
    /**
     * @brief Whether the initialization failed, of course default to @c false.
341
     */
342 343 344
    bool m_failed;
    /**
     * @brief Damaged region of previously rendered frame.
345
     */
346 347 348
    QRegion m_lastDamage;
    /**
     * @brief The damage history for the past 10 frames.
349
     */
350 351 352
    QList<QRegion> m_damageHistory;
    /**
     * @brief Timer to measure how long a frame renders.
353
     */
354 355 356 357 358 359 360 361
    QElapsedTimer m_renderTimer;

    QList<QByteArray> m_extensions;
};

}

#endif