aboutsummaryrefslogtreecommitdiff
path: root/ui/egl-headless.c
blob: d8d800f8a610fac0598b32297fae860e52a11674 (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
#include "qemu/osdep.h"
#include "qemu-common.h"
#include "sysemu/sysemu.h"
#include "ui/console.h"
#include "ui/egl-helpers.h"
#include "ui/egl-context.h"

typedef struct egl_dpy {
    DisplayChangeListener dcl;
    DisplaySurface *ds;
    int width, height;
    GLuint texture;
    GLuint framebuffer;
    GLuint blit_texture;
    GLuint blit_framebuffer;
    bool y_0_top;
} egl_dpy;

static void egl_refresh(DisplayChangeListener *dcl)
{
    graphic_hw_update(dcl->con);
}

static void egl_gfx_update(DisplayChangeListener *dcl,
                           int x, int y, int w, int h)
{
}

static void egl_gfx_switch(DisplayChangeListener *dcl,
                           struct DisplaySurface *new_surface)
{
    egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);

    edpy->ds = new_surface;
}

static void egl_scanout_disable(DisplayChangeListener *dcl)
{
    egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);

    edpy->texture = 0;
    /* XXX: delete framebuffers here ??? */
}

static void egl_scanout_texture(DisplayChangeListener *dcl,
                                uint32_t backing_id,
                                bool backing_y_0_top,
                                uint32_t backing_width,
                                uint32_t backing_height,
                                uint32_t x, uint32_t y,
                                uint32_t w, uint32_t h)
{
    egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);

    edpy->texture = backing_id;
    edpy->y_0_top = backing_y_0_top;

    /* source framebuffer */
    if (!edpy->framebuffer) {
        glGenFramebuffers(1, &edpy->framebuffer);
    }
    glBindFramebuffer(GL_FRAMEBUFFER_EXT, edpy->framebuffer);
    glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
                              GL_TEXTURE_2D, edpy->texture, 0);

    /* dest framebuffer */
    if (!edpy->blit_framebuffer) {
        glGenFramebuffers(1, &edpy->blit_framebuffer);
        glGenTextures(1, &edpy->blit_texture);
        edpy->width = 0;
        edpy->height = 0;
    }
    if (edpy->width != backing_width || edpy->height != backing_height) {
        edpy->width   = backing_width;
        edpy->height  = backing_height;
        glBindTexture(GL_TEXTURE_2D, edpy->blit_texture);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
                     edpy->width, edpy->height,
                     0, GL_BGRA, GL_UNSIGNED_BYTE, 0);
        glBindFramebuffer(GL_FRAMEBUFFER_EXT, edpy->blit_framebuffer);
        glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
                                  GL_TEXTURE_2D, edpy->blit_texture, 0);
    }
}

static void egl_scanout_flush(DisplayChangeListener *dcl,
                              uint32_t x, uint32_t y,
                              uint32_t w, uint32_t h)
{
    egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
    GLuint y1, y2;

    if (!edpy->texture || !edpy->ds) {
        return;
    }
    assert(surface_width(edpy->ds)  == edpy->width);
    assert(surface_height(edpy->ds) == edpy->height);
    assert(surface_format(edpy->ds) == PIXMAN_x8r8g8b8);

    /* blit framebuffer, flip if needed */
    glBindFramebuffer(GL_READ_FRAMEBUFFER, edpy->framebuffer);
    glBindFramebuffer(GL_DRAW_FRAMEBUFFER, edpy->blit_framebuffer);
    glViewport(0, 0, edpy->width, edpy->height);
    y1 = edpy->y_0_top ? edpy->height : 0;
    y2 = edpy->y_0_top ? 0 : edpy->height;
    glBlitFramebuffer(0, y1, edpy->width, y2,
                      0, 0, edpy->width, edpy->height,
                      GL_COLOR_BUFFER_BIT, GL_NEAREST);

    /* read pixels to surface */
    glBindFramebuffer(GL_READ_FRAMEBUFFER, edpy->blit_framebuffer);
    glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);
    glReadPixels(0, 0, edpy->width, edpy->height,
                 GL_BGRA, GL_UNSIGNED_BYTE, surface_data(edpy->ds));

    /* notify about updates */
    dpy_gfx_update(edpy->dcl.con, x, y, w, h);
}

static const DisplayChangeListenerOps egl_ops = {
    .dpy_name                = "egl-headless",
    .dpy_refresh             = egl_refresh,
    .dpy_gfx_update          = egl_gfx_update,
    .dpy_gfx_switch          = egl_gfx_switch,

    .dpy_gl_ctx_create       = qemu_egl_create_context,
    .dpy_gl_ctx_destroy      = qemu_egl_destroy_context,
    .dpy_gl_ctx_make_current = qemu_egl_make_context_current,
    .dpy_gl_ctx_get_current  = qemu_egl_get_current_context,

    .dpy_gl_scanout_disable  = egl_scanout_disable,
    .dpy_gl_scanout_texture  = egl_scanout_texture,
    .dpy_gl_update           = egl_scanout_flush,
};

void egl_headless_init(void)
{
    QemuConsole *con;
    egl_dpy *edpy;
    int idx;

    if (egl_rendernode_init(NULL) < 0) {
        error_report("egl: render node init failed");
        exit(1);
    }

    for (idx = 0;; idx++) {
        con = qemu_console_lookup_by_index(idx);
        if (!con || !qemu_console_is_graphic(con)) {
            break;
        }

        edpy = g_new0(egl_dpy, 1);
        edpy->dcl.con = con;
        edpy->dcl.ops = &egl_ops;
        register_displaychangelistener(&edpy->dcl);
    }
}