abstract_egl_backend.cpp 21.3 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
    SPDX-FileCopyrightText: 2015 Martin Gräßlin <mgraesslin@kde.org>
6

Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
7 8
    SPDX-License-Identifier: GPL-2.0-or-later
*/
9
#include "abstract_egl_backend.h"
Roman Gilg's avatar
Roman Gilg committed
10
#include "egl_dmabuf.h"
11
#include "kwineglext.h"
12
#include "texture.h"
13
#include "composite.h"
14
#include "egl_context_attribute_builder.h"
15
#include "options.h"
16
#include "platform.h"
17
#include "scene.h"
18
#include "wayland_server.h"
19
#include "abstract_wayland_output.h"
20 21 22
#include <KWaylandServer/buffer_interface.h>
#include <KWaylandServer/display.h>
#include <KWaylandServer/surface_interface.h>
23
// kwin libs
24
#include <logging.h>
25
#include <kwinglplatform.h>
26
#include <kwinglutils.h>
27 28
// Qt
#include <QOpenGLContext>
29
#include <QOpenGLFramebufferObject>
30

31 32
#include <memory>

33 34 35 36 37 38 39 40 41 42 43
namespace KWin
{

typedef GLboolean(*eglBindWaylandDisplayWL_func)(EGLDisplay dpy, wl_display *display);
typedef GLboolean(*eglUnbindWaylandDisplayWL_func)(EGLDisplay dpy, wl_display *display);
typedef GLboolean(*eglQueryWaylandBufferWL_func)(EGLDisplay dpy, struct wl_resource *buffer, EGLint attribute, EGLint *value);
eglBindWaylandDisplayWL_func eglBindWaylandDisplayWL = nullptr;
eglUnbindWaylandDisplayWL_func eglUnbindWaylandDisplayWL = nullptr;
eglQueryWaylandBufferWL_func eglQueryWaylandBufferWL = nullptr;

AbstractEglBackend::AbstractEglBackend()
44 45
    : QObject(nullptr)
    , OpenGLBackend()
46
{
47
    connect(Compositor::self(), &Compositor::aboutToDestroy, this, &AbstractEglBackend::unbindWaylandDisplay);
48 49
}

50 51 52 53
AbstractEglBackend::~AbstractEglBackend()
{
    delete m_dmaBuf;
}
54

55
void AbstractEglBackend::unbindWaylandDisplay()
56
{
57 58
    if (eglUnbindWaylandDisplayWL && m_display != EGL_NO_DISPLAY) {
        eglUnbindWaylandDisplayWL(m_display, *(WaylandServer::self()->display()));
59
    }
60 61 62 63
}

void AbstractEglBackend::cleanup()
{
64 65 66
    cleanupGL();
    doneCurrent();
    eglDestroyContext(m_display, m_context);
67
    cleanupSurfaces();
68
    eglReleaseThread();
69 70 71
    kwinApp()->platform()->setSceneEglContext(EGL_NO_CONTEXT);
    kwinApp()->platform()->setSceneEglSurface(EGL_NO_SURFACE);
    kwinApp()->platform()->setSceneEglConfig(nullptr);
72 73
}

74 75 76 77 78 79 80
void AbstractEglBackend::cleanupSurfaces()
{
    if (m_surface != EGL_NO_SURFACE) {
        eglDestroySurface(m_display, m_surface);
    }
}

81 82 83 84
bool AbstractEglBackend::initEglAPI()
{
    EGLint major, minor;
    if (eglInitialize(m_display, &major, &minor) == EGL_FALSE) {
85
        qCWarning(KWIN_OPENGL) << "eglInitialize failed";
86 87
        EGLint error = eglGetError();
        if (error != EGL_SUCCESS) {
88
            qCWarning(KWIN_OPENGL) << "Error during eglInitialize " << error;
89
        }
90 91 92 93
        return false;
    }
    EGLint error = eglGetError();
    if (error != EGL_SUCCESS) {
94
        qCWarning(KWIN_OPENGL) << "Error during eglInitialize " << error;
95 96
        return false;
    }
97
    qCDebug(KWIN_OPENGL) << "Egl Initialize succeeded";
98

99
    if (eglBindAPI(isOpenGLES() ? EGL_OPENGL_ES_API : EGL_OPENGL_API) == EGL_FALSE) {
100
        qCCritical(KWIN_OPENGL) << "bind OpenGL API failed";
101 102
        return false;
    }
103
    qCDebug(KWIN_OPENGL) << "EGL version: " << major << "." << minor;
104 105
    const QByteArray eglExtensions = eglQueryString(m_display, EGL_EXTENSIONS);
    setExtensions(eglExtensions.split(' '));
106 107 108
    return true;
}

109 110 111 112 113 114
typedef void (*eglFuncPtr)();
static eglFuncPtr getProcAddress(const char* name)
{
    return eglGetProcAddress(name);
}

115 116 117 118 119 120 121 122
void AbstractEglBackend::initKWinGL()
{
    GLPlatform *glPlatform = GLPlatform::instance();
    glPlatform->detect(EglPlatformInterface);
    options->setGlPreferBufferSwap(options->glPreferBufferSwap()); // resolve autosetting
    if (options->glPreferBufferSwap() == Options::AutoSwapStrategy)
        options->setGlPreferBufferSwap('e'); // for unknown drivers - should not happen
    glPlatform->printResults();
123
    initGL(&getProcAddress);
124 125 126 127 128 129
}

void AbstractEglBackend::initBufferAge()
{
    setSupportsBufferAge(false);

130
    if (hasExtension(QByteArrayLiteral("EGL_EXT_buffer_age"))) {
131 132 133 134 135
        const QByteArray useBufferAge = qgetenv("KWIN_USE_BUFFER_AGE");

        if (useBufferAge != "0")
            setSupportsBufferAge(true);
    }
136 137 138

    setSupportsPartialUpdate(hasExtension(QByteArrayLiteral("EGL_KHR_partial_update")));
    setSupportsSwapBuffersWithDamage(hasExtension(QByteArrayLiteral("EGL_EXT_swap_buffers_with_damage")));
139 140 141 142 143 144 145
}

void AbstractEglBackend::initWayland()
{
    if (!WaylandServer::self()) {
        return;
    }
146
    if (hasExtension(QByteArrayLiteral("EGL_WL_bind_wayland_display"))) {
147 148 149
        eglBindWaylandDisplayWL = (eglBindWaylandDisplayWL_func)eglGetProcAddress("eglBindWaylandDisplayWL");
        eglUnbindWaylandDisplayWL = (eglUnbindWaylandDisplayWL_func)eglGetProcAddress("eglUnbindWaylandDisplayWL");
        eglQueryWaylandBufferWL = (eglQueryWaylandBufferWL_func)eglGetProcAddress("eglQueryWaylandBufferWL");
150 151 152 153 154 155 156 157
        // only bind if not already done
        if (waylandServer()->display()->eglDisplay() != eglDisplay()) {
            if (!eglBindWaylandDisplayWL(eglDisplay(), *(WaylandServer::self()->display()))) {
                eglUnbindWaylandDisplayWL = nullptr;
                eglQueryWaylandBufferWL = nullptr;
            } else {
                waylandServer()->display()->setEglDisplay(eglDisplay());
            }
158 159
        }
    }
160

161 162
    Q_ASSERT(!m_dmaBuf);
    m_dmaBuf = EglDmabuf::factory(this);
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
}

void AbstractEglBackend::initClientExtensions()
{
    // Get the list of client extensions
    const char* clientExtensionsCString = eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS);
    const QByteArray clientExtensionsString = QByteArray::fromRawData(clientExtensionsCString, qstrlen(clientExtensionsCString));
    if (clientExtensionsString.isEmpty()) {
        // If eglQueryString() returned NULL, the implementation doesn't support
        // EGL_EXT_client_extensions. Expect an EGL_BAD_DISPLAY error.
        (void) eglGetError();
    }

    m_clientExtensions = clientExtensionsString.split(' ');
}

bool AbstractEglBackend::hasClientExtension(const QByteArray &ext) const
{
    return m_clientExtensions.contains(ext);
}

bool AbstractEglBackend::makeCurrent()
{
    if (QOpenGLContext *context = QOpenGLContext::currentContext()) {
        // Workaround to tell Qt that no QOpenGLContext is current
        context->doneCurrent();
    }
    const bool current = eglMakeCurrent(m_display, m_surface, m_surface, m_context);
    return current;
}

void AbstractEglBackend::doneCurrent()
{
    eglMakeCurrent(m_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
}

199 200
bool AbstractEglBackend::isOpenGLES() const
{
201 202 203
    if (qstrcmp(qgetenv("KWIN_COMPOSE"), "O2ES") == 0) {
        return true;
    }
204 205 206
    return QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGLES;
}

207 208
bool AbstractEglBackend::createContext()
{
209 210
    const bool haveRobustness = hasExtension(QByteArrayLiteral("EGL_EXT_create_context_robustness"));
    const bool haveCreateContext = hasExtension(QByteArrayLiteral("EGL_KHR_create_context"));
211
    const bool haveContextPriority = hasExtension(QByteArrayLiteral("EGL_IMG_context_priority"));
212

213
    std::vector<std::unique_ptr<AbstractOpenGLContextAttributeBuilder>> candidates;
214
    if (isOpenGLES()) {
215 216 217 218 219 220 221
        if (haveCreateContext && haveRobustness && haveContextPriority) {
            auto glesRobustPriority = std::unique_ptr<AbstractOpenGLContextAttributeBuilder>(new EglOpenGLESContextAttributeBuilder);
            glesRobustPriority->setVersion(2);
            glesRobustPriority->setRobust(true);
            glesRobustPriority->setHighPriority(true);
            candidates.push_back(std::move(glesRobustPriority));
        }
222
        if (haveCreateContext && haveRobustness) {
223 224 225 226
            auto glesRobust = std::unique_ptr<AbstractOpenGLContextAttributeBuilder>(new EglOpenGLESContextAttributeBuilder);
            glesRobust->setVersion(2);
            glesRobust->setRobust(true);
            candidates.push_back(std::move(glesRobust));
227
        }
228 229 230 231 232 233
        if (haveContextPriority) {
            auto glesPriority = std::unique_ptr<AbstractOpenGLContextAttributeBuilder>(new EglOpenGLESContextAttributeBuilder);
            glesPriority->setVersion(2);
            glesPriority->setHighPriority(true);
            candidates.push_back(std::move(glesPriority));
        }
234 235 236
        auto gles = std::unique_ptr<AbstractOpenGLContextAttributeBuilder>(new EglOpenGLESContextAttributeBuilder);
        gles->setVersion(2);
        candidates.push_back(std::move(gles));
237
    } else {
238
        if (options->glCoreProfile() && haveCreateContext) {
239 240 241 242 243 244 245
            if (haveRobustness && haveContextPriority) {
                auto robustCorePriority = std::unique_ptr<AbstractOpenGLContextAttributeBuilder>(new EglContextAttributeBuilder);
                robustCorePriority->setVersion(3, 1);
                robustCorePriority->setRobust(true);
                robustCorePriority->setHighPriority(true);
                candidates.push_back(std::move(robustCorePriority));
            }
246
            if (haveRobustness) {
247 248 249 250
                auto robustCore = std::unique_ptr<AbstractOpenGLContextAttributeBuilder>(new EglContextAttributeBuilder);
                robustCore->setVersion(3, 1);
                robustCore->setRobust(true);
                candidates.push_back(std::move(robustCore));
251
            }
252 253 254 255 256 257
            if (haveContextPriority) {
                auto corePriority = std::unique_ptr<AbstractOpenGLContextAttributeBuilder>(new EglContextAttributeBuilder);
                corePriority->setVersion(3, 1);
                corePriority->setHighPriority(true);
                candidates.push_back(std::move(corePriority));
            }
258 259 260
            auto core = std::unique_ptr<AbstractOpenGLContextAttributeBuilder>(new EglContextAttributeBuilder);
            core->setVersion(3, 1);
            candidates.push_back(std::move(core));
261
        }
262 263 264 265 266 267
        if (haveRobustness && haveCreateContext && haveContextPriority) {
            auto robustPriority = std::unique_ptr<AbstractOpenGLContextAttributeBuilder>(new EglContextAttributeBuilder);
            robustPriority->setRobust(true);
            robustPriority->setHighPriority(true);
            candidates.push_back(std::move(robustPriority));
        }
268 269 270 271
        if (haveRobustness && haveCreateContext) {
            auto robust = std::unique_ptr<AbstractOpenGLContextAttributeBuilder>(new EglContextAttributeBuilder);
            robust->setRobust(true);
            candidates.push_back(std::move(robust));
272
        }
273 274 275 276 277 278 279 280
        candidates.emplace_back(new EglContextAttributeBuilder);
    }

    EGLContext ctx = EGL_NO_CONTEXT;
    for (auto it = candidates.begin(); it != candidates.end(); it++) {
        const auto attribs = (*it)->build();
        ctx = eglCreateContext(m_display, config(), EGL_NO_CONTEXT, attribs.data());
        if (ctx != EGL_NO_CONTEXT) {
281
            qCDebug(KWIN_OPENGL) << "Created EGL context with attributes:" << (*it).get();
282
            break;
283
        }
284 285 286
    }

    if (ctx == EGL_NO_CONTEXT) {
287
        qCCritical(KWIN_OPENGL) << "Create Context failed";
288 289 290
        return false;
    }
    m_context = ctx;
291
    kwinApp()->platform()->setSceneEglContext(m_context);
292 293 294
    return true;
}

295 296 297 298 299
void AbstractEglBackend::setEglDisplay(const EGLDisplay &display) {
    m_display = display;
    kwinApp()->platform()->setSceneEglDisplay(display);
}

300 301 302 303 304 305 306 307 308 309 310 311
void AbstractEglBackend::setConfig(const EGLConfig &config)
{
    m_config = config;
    kwinApp()->platform()->setSceneEglConfig(config);
}

void AbstractEglBackend::setSurface(const EGLSurface &surface)
{
    m_surface = surface;
    kwinApp()->platform()->setSceneEglSurface(surface);
}

312 313 314 315 316 317 318 319 320 321 322
QSharedPointer<GLTexture> AbstractEglBackend::textureForOutput(AbstractOutput *requestedOutput) const
{
    QSharedPointer<GLTexture> texture(new GLTexture(GL_RGBA8, requestedOutput->pixelSize()));
    GLRenderTarget renderTarget(*texture);

    const QRect geo = requestedOutput->geometry();
    QRect invGeo(geo.left(), geo.bottom(), geo.width(), -geo.height());
    renderTarget.blitFromFramebuffer(invGeo);
    return texture;
}

323 324
AbstractEglTexture::AbstractEglTexture(SceneOpenGLTexture *texture, AbstractEglBackend *backend)
    : SceneOpenGLTexturePrivate()
325 326
    , q(texture)
    , m_backend(backend)
327
    , m_image(EGL_NO_IMAGE_KHR)
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
{
    m_target = GL_TEXTURE_2D;
}

AbstractEglTexture::~AbstractEglTexture()
{
    if (m_image != EGL_NO_IMAGE_KHR) {
        eglDestroyImageKHR(m_backend->eglDisplay(), m_image);
    }
}

OpenGLBackend *AbstractEglTexture::backend()
{
    return m_backend;
}

bool AbstractEglTexture::loadTexture(WindowPixmap *pixmap)
{
Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
346 347
    // FIXME: Refactor this method.

348 349
    const auto buffer = pixmap->buffer();
    if (!buffer) {
350 351 352
        if (updateFromFBO(pixmap->fbo())) {
            return true;
        }
Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
353 354 355
        if (loadInternalImageObject(pixmap)) {
            return true;
        }
356
        return false;
357 358
    }
    // try Wayland loading
359 360 361
    if (auto s = pixmap->surface()) {
        s->resetTrackedDamage();
    }
362 363 364
    if (buffer->linuxDmabufBuffer()) {
        return loadDmabufTexture(buffer);
    } else if (buffer->shmBuffer()) {
365 366
        return loadShmTexture(buffer);
    }
367
    return loadEglTexture(buffer);
368 369 370 371
}

void AbstractEglTexture::updateTexture(WindowPixmap *pixmap)
{
Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
372 373
    // FIXME: Refactor this method.

374 375
    const auto buffer = pixmap->buffer();
    if (!buffer) {
Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
376 377 378 379
        if (updateFromFBO(pixmap->fbo())) {
            return;
        }
        if (updateFromInternalImageObject(pixmap)) {
380 381
            return;
        }
382 383
        return;
    }
384
    auto s = pixmap->surface();
Roman Gilg's avatar
Roman Gilg committed
385
    if (EglDmabufBuffer *dmabuf = static_cast<EglDmabufBuffer *>(buffer->linuxDmabufBuffer())) {
386 387 388 389 390 391 392 393 394
        q->bind();
        glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES) dmabuf->images()[0]);   //TODO
        q->unbind();
        if (m_image != EGL_NO_IMAGE_KHR) {
            eglDestroyImageKHR(m_backend->eglDisplay(), m_image);
        }
        m_image = EGL_NO_IMAGE_KHR; // The wl_buffer has ownership of the image
        // The origin in a dmabuf-buffer is at the upper-left corner, so the meaning
        // of Y-inverted is the inverse of OpenGL.
395
        const bool yInverted = !(dmabuf->flags() & KWaylandServer::LinuxDmabufUnstableV1Interface::YInverted);
396 397 398 399 400 401 402 403 404
        if (m_size != dmabuf->size() || yInverted != q->isYInverted()) {
            m_size = dmabuf->size();
            q->setYInverted(yInverted);
        }
        if (s) {
            s->resetTrackedDamage();
        }
        return;
    }
405 406 407 408 409
    if (!buffer->shmBuffer()) {
        q->bind();
        EGLImageKHR image = attach(buffer);
        q->unbind();
        if (image != EGL_NO_IMAGE_KHR) {
410 411 412
            if (m_image != EGL_NO_IMAGE_KHR) {
                eglDestroyImageKHR(m_backend->eglDisplay(), m_image);
            }
413 414
            m_image = image;
        }
415 416 417
        if (s) {
            s->resetTrackedDamage();
        }
418 419 420 421
        return;
    }
    // shm fallback
    const QImage &image = buffer->data();
422
    if (image.isNull() || !s) {
423 424 425
        return;
    }
    Q_ASSERT(image.size() == m_size);
426
    const QRegion damage = s->mapToBuffer(s->trackedDamage());
427
    s->resetTrackedDamage();
428 429

    // TODO: this should be shared with GLTexture::update
430
    createTextureSubImage(image, damage);
431 432
}

433
bool AbstractEglTexture::createTextureImage(const QImage &image)
434 435 436 437 438 439 440
{
    if (image.isNull()) {
        return false;
    }

    glGenTextures(1, &m_texture);
    q->setFilter(GL_LINEAR);
441
    q->setWrapMode(GL_CLAMP_TO_EDGE);
442 443

    const QSize &size = image.size();
444
    q->bind();
445 446 447 448 449 450 451 452 453 454 455 456
    GLenum format = 0;
    switch (image.format()) {
    case QImage::Format_ARGB32:
    case QImage::Format_ARGB32_Premultiplied:
        format = GL_RGBA8;
        break;
    case QImage::Format_RGB32:
        format = GL_RGB8;
        break;
    default:
        return false;
    }
457 458 459 460 461 462 463 464 465 466 467 468 469 470
    if (GLPlatform::instance()->isGLES()) {
        if (s_supportsARGB32 && format == GL_RGBA8) {
            const QImage im = image.convertToFormat(QImage::Format_ARGB32_Premultiplied);
            glTexImage2D(m_target, 0, GL_BGRA_EXT, im.width(), im.height(),
                         0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, im.bits());
        } else {
            const QImage im = image.convertToFormat(QImage::Format_RGBA8888_Premultiplied);
            glTexImage2D(m_target, 0, GL_RGBA, im.width(), im.height(),
                         0, GL_RGBA, GL_UNSIGNED_BYTE, im.bits());
        }
    } else {
        glTexImage2D(m_target, 0, format, size.width(), size.height(), 0,
                    GL_BGRA, GL_UNSIGNED_BYTE, image.bits());
    }
471 472 473 474 475 476 477
    q->unbind();
    q->setYInverted(true);
    m_size = size;
    updateMatrix();
    return true;
}

478
void AbstractEglTexture::createTextureSubImage(const QImage &image, const QRegion &damage)
479 480 481 482 483 484
{
    q->bind();
    if (GLPlatform::instance()->isGLES()) {
        if (s_supportsARGB32 && (image.format() == QImage::Format_ARGB32 || image.format() == QImage::Format_ARGB32_Premultiplied)) {
            const QImage im = image.convertToFormat(QImage::Format_ARGB32_Premultiplied);
            for (const QRect &rect : damage) {
485 486
                glTexSubImage2D(m_target, 0, rect.x(), rect.y(), rect.width(), rect.height(),
                                GL_BGRA_EXT, GL_UNSIGNED_BYTE, im.copy(rect).bits());
487 488 489 490
            }
        } else {
            const QImage im = image.convertToFormat(QImage::Format_RGBA8888_Premultiplied);
            for (const QRect &rect : damage) {
491 492
                glTexSubImage2D(m_target, 0, rect.x(), rect.y(), rect.width(), rect.height(),
                                GL_RGBA, GL_UNSIGNED_BYTE, im.copy(rect).bits());
493 494 495 496 497
            }
        }
    } else {
        const QImage im = image.convertToFormat(QImage::Format_ARGB32_Premultiplied);
        for (const QRect &rect : damage) {
498 499
            glTexSubImage2D(m_target, 0, rect.x(), rect.y(), rect.width(), rect.height(),
                            GL_BGRA, GL_UNSIGNED_BYTE, im.copy(rect).bits());
500 501 502 503 504
        }
    }
    q->unbind();
}

505
bool AbstractEglTexture::loadShmTexture(const QPointer< KWaylandServer::BufferInterface > &buffer)
506 507 508 509
{
    return createTextureImage(buffer->data());
}

510
bool AbstractEglTexture::loadEglTexture(const QPointer< KWaylandServer::BufferInterface > &buffer)
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
{
    if (!eglQueryWaylandBufferWL) {
        return false;
    }
    if (!buffer->resource()) {
        return false;
    }

    glGenTextures(1, &m_texture);
    q->setWrapMode(GL_CLAMP_TO_EDGE);
    q->setFilter(GL_LINEAR);
    q->bind();
    m_image = attach(buffer);
    q->unbind();

    if (EGL_NO_IMAGE_KHR == m_image) {
527
        qCDebug(KWIN_OPENGL) << "failed to create egl image";
528 529 530 531 532 533 534
        q->discard();
        return false;
    }

    return true;
}

535
bool AbstractEglTexture::loadDmabufTexture(const QPointer< KWaylandServer::BufferInterface > &buffer)
536
{
Roman Gilg's avatar
Roman Gilg committed
537
    auto *dmabuf = static_cast<EglDmabufBuffer *>(buffer->linuxDmabufBuffer());
538 539 540 541 542 543
    if (!dmabuf || dmabuf->images()[0] == EGL_NO_IMAGE_KHR) {
        qCritical(KWIN_OPENGL) << "Invalid dmabuf-based wl_buffer";
        q->discard();
        return false;
    }

Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
544
    Q_ASSERT(m_image == EGL_NO_IMAGE_KHR);
545 546 547 548 549 550 551 552 553

    glGenTextures(1, &m_texture);
    q->setWrapMode(GL_CLAMP_TO_EDGE);
    q->setFilter(GL_NEAREST);
    q->bind();
    glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES) dmabuf->images()[0]);
    q->unbind();

    m_size = dmabuf->size();
554
    q->setYInverted(!(dmabuf->flags() & KWaylandServer::LinuxDmabufUnstableV1Interface::YInverted));
555 556 557 558

    return true;
}

Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
559 560
bool AbstractEglTexture::loadInternalImageObject(WindowPixmap *pixmap)
{
561
    return createTextureImage(pixmap->internalImage());
Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
562 563
}

564
EGLImageKHR AbstractEglTexture::attach(const QPointer< KWaylandServer::BufferInterface > &buffer)
565
{
566
    EGLint format, yInverted;
567 568
    eglQueryWaylandBufferWL(m_backend->eglDisplay(), buffer->resource(), EGL_TEXTURE_FORMAT, &format);
    if (format != EGL_TEXTURE_RGB && format != EGL_TEXTURE_RGBA) {
569
        qCDebug(KWIN_OPENGL) << "Unsupported texture format: " << format;
570 571
        return EGL_NO_IMAGE_KHR;
    }
572 573 574 575
    if (!eglQueryWaylandBufferWL(m_backend->eglDisplay(), buffer->resource(), EGL_WAYLAND_Y_INVERTED_WL, &yInverted)) {
        // if EGL_WAYLAND_Y_INVERTED_WL is not supported wl_buffer should be treated as if value were EGL_TRUE
        yInverted = EGL_TRUE;
    }
576 577 578 579 580 581 582 583 584

    const EGLint attribs[] = {
        EGL_WAYLAND_PLANE_WL, 0,
        EGL_NONE
    };
    EGLImageKHR image = eglCreateImageKHR(m_backend->eglDisplay(), EGL_NO_CONTEXT, EGL_WAYLAND_BUFFER_WL,
                                      (EGLClientBuffer)buffer->resource(), attribs);
    if (image != EGL_NO_IMAGE_KHR) {
        glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image);
585
        m_size = buffer->size();
586 587 588 589 590 591
        updateMatrix();
        q->setYInverted(yInverted);
    }
    return image;
}

592 593 594 595 596 597 598 599 600 601 602 603 604 605
bool AbstractEglTexture::updateFromFBO(const QSharedPointer<QOpenGLFramebufferObject> &fbo)
{
    if (fbo.isNull()) {
        return false;
    }
    m_texture = fbo->texture();
    m_size = fbo->size();
    q->setWrapMode(GL_CLAMP_TO_EDGE);
    q->setFilter(GL_LINEAR);
    q->setYInverted(false);
    updateMatrix();
    return true;
}

606 607 608 609 610 611 612 613 614 615 616 617 618
static QRegion scale(const QRegion &region, qreal scaleFactor)
{
    if (scaleFactor == 1) {
        return region;
    }

    QRegion scaled;
    for (const QRect &rect : region) {
        scaled += QRect(rect.topLeft() * scaleFactor, rect.size() * scaleFactor);
    }
    return scaled;
}

Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
619 620 621 622 623 624 625 626 627 628 629 630
bool AbstractEglTexture::updateFromInternalImageObject(WindowPixmap *pixmap)
{
    const QImage image = pixmap->internalImage();
    if (image.isNull()) {
        return false;
    }

    if (m_size != image.size()) {
        glDeleteTextures(1, &m_texture);
        return loadInternalImageObject(pixmap);
    }

631
    createTextureSubImage(image, scale(pixmap->toplevel()->damage(), image.devicePixelRatio()));
Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
632 633 634 635

    return true;
}

636
}