summaryrefslogtreecommitdiff
path: root/glamor/glamor_core.c
blob: fbac86b4d8697a13db2ecfa2dc38712dc45940d0 (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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/*
 * Copyright © 2001 Keith Packard
 * Copyright © 2008 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * Authors:
 *    Eric Anholt <eric@anholt.net>
 *
 */

/** @file glamor_core.c
 *
 * This file covers core X rendering in glamor.
 */

#include <stdlib.h>

#include "glamor_priv.h"

const Bool
glamor_get_drawable_location(const DrawablePtr drawable)
{
    PixmapPtr pixmap = glamor_get_drawable_pixmap(drawable);
    glamor_pixmap_private *pixmap_priv = glamor_get_pixmap_private(pixmap);
    glamor_screen_private *glamor_priv =
        glamor_get_screen_private(drawable->pScreen);
    if (pixmap_priv == NULL ||
        pixmap_priv->base.gl_fbo == GLAMOR_FBO_UNATTACHED)
        return 'm';
    if (pixmap_priv->base.fbo->fb == glamor_priv->screen_fbo)
        return 's';
    else
        return 'f';
}

GLint
glamor_compile_glsl_prog(GLenum type, const char *source)
{
    GLint ok;
    GLint prog;

    prog = glCreateShader(type);
    glShaderSource(prog, 1, (const GLchar **) &source, NULL);
    glCompileShader(prog);
    glGetShaderiv(prog, GL_COMPILE_STATUS, &ok);
    if (!ok) {
        GLchar *info;
        GLint size;

        glGetShaderiv(prog, GL_INFO_LOG_LENGTH, &size);
        info = malloc(size);
        if (info) {
            glGetShaderInfoLog(prog, size, NULL, info);
            ErrorF("Failed to compile %s: %s\n",
                   type == GL_FRAGMENT_SHADER ? "FS" : "VS", info);
            ErrorF("Program source:\n%s", source);
            free(info);
        }
        else
            ErrorF("Failed to get shader compilation info.\n");
        FatalError("GLSL compile failure\n");
    }

    return prog;
}

void
glamor_link_glsl_prog(ScreenPtr screen, GLint prog, const char *format, ...)
{
    GLint ok;
    glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);

    glLinkProgram(prog);
    glGetProgramiv(prog, GL_LINK_STATUS, &ok);
    if (!ok) {
        GLchar *info;
        GLint size;

        glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &size);
        info = malloc(size);

        glGetProgramInfoLog(prog, size, NULL, info);
        ErrorF("Failed to link: %s\n", info);
        FatalError("GLSL link failure\n");
    }

    if (glamor_priv->has_khr_debug) {
        char *label;
        va_list va;

        va_start(va, format);
        XNFvasprintf(&label, format, va);
        glObjectLabel(GL_PROGRAM, prog, -1, label);
        free(label);
        va_end(va);
    }
}

/*
 *  When downloading a unsupported color format to CPU memory,
    we need to shuffle the color elements and then use a supported
    color format to read it back to CPU memory.

    For an example, the picture's format is PICT_b8g8r8a8,
    Then the expecting color layout is as below (little endian):
    0	1	2	3   : address
    a	r	g	b

    Now the in GLES2 the supported color format is GL_RGBA, type is
    GL_UNSIGNED_TYPE, then we need to shuffle the fragment
    color as :
	frag_color = sample(texture).argb;
    before we use glReadPixel to get it back.

    For the uploading process, the shuffle is a revert shuffle.
    We still use GL_RGBA, GL_UNSIGNED_BYTE to upload the color
    to a texture, then let's see
    0	1	2	3   : address
    a	r	g	b   : correct colors
    R	G	B	A   : GL_RGBA with GL_UNSIGNED_BYTE

    Now we need to shuffle again, the mapping rule is
    r = G, g = B, b = A, a = R. Then the uploading shuffle is as
    below:
	frag_color = sample(texture).gbar;
*/

void
glamor_init_finish_access_shaders(ScreenPtr screen)
{
    glamor_screen_private *glamor_priv;
    const char *vs_source =
        "attribute vec4 v_position;\n"
        "attribute vec4 v_texcoord0;\n"
        "varying vec2 source_texture;\n"
        "void main()\n"
        "{\n"
        "	gl_Position = v_position;\n"
        "	source_texture = v_texcoord0.xy;\n"
        "}\n";

    const char *common_source =
        GLAMOR_DEFAULT_PRECISION
        "varying vec2 source_texture;\n"
        "uniform sampler2D sampler;\n"
        "uniform int revert;\n"
        "uniform int swap_rb;\n"
        "#define REVERT_NONE       			0\n"
        "#define REVERT_NORMAL     			1\n"
        "#define SWAP_NONE_DOWNLOADING  		0\n"
        "#define SWAP_DOWNLOADING  			1\n"
        "#define SWAP_UPLOADING	  		2\n"
        "#define SWAP_NONE_UPLOADING		3\n";

    const char *fs_source =
        "void main()\n"
        "{\n"
        "   if (revert == REVERT_NONE) \n"
        "    { \n"
        "     if ((swap_rb != SWAP_NONE_DOWNLOADING) && (swap_rb != SWAP_NONE_UPLOADING))   \n"
        "	  	gl_FragColor = texture2D(sampler, source_texture).bgra;\n"
        "     else \n"
        "	  	gl_FragColor = texture2D(sampler, source_texture).rgba;\n"
        "    } \n"
        "   else \n"
        "    { \n"
        "     if (swap_rb == SWAP_DOWNLOADING)   \n"
        "	  	gl_FragColor = texture2D(sampler, source_texture).argb;\n"
        "     else if (swap_rb == SWAP_NONE_DOWNLOADING)\n"
        "	  	gl_FragColor = texture2D(sampler, source_texture).abgr;\n"
        "     else if (swap_rb == SWAP_UPLOADING)\n"
        "	  	gl_FragColor = texture2D(sampler, source_texture).gbar;\n"
        "     else if (swap_rb == SWAP_NONE_UPLOADING)\n"
        "	  	gl_FragColor = texture2D(sampler, source_texture).abgr;\n"
        "    } \n"
        "}\n";

    const char *set_alpha_source =
        "void main()\n"
        "{\n"
        "   if (revert == REVERT_NONE) \n"
        "    { \n"
        "     if ((swap_rb != SWAP_NONE_DOWNLOADING) && (swap_rb != SWAP_NONE_UPLOADING))   \n"
        "	  	gl_FragColor = vec4(texture2D(sampler, source_texture).bgr, 1);\n"
        "     else \n"
        "	  	gl_FragColor = vec4(texture2D(sampler, source_texture).rgb, 1);\n"
        "    } \n"
        "   else \n"
        "    { \n"
        "     if (swap_rb == SWAP_DOWNLOADING)   \n"
        "	  	gl_FragColor = vec4(1, texture2D(sampler, source_texture).rgb);\n"
        "     else if (swap_rb == SWAP_NONE_DOWNLOADING)\n"
        "	  	gl_FragColor = vec4(1, texture2D(sampler, source_texture).bgr);\n"
        "     else if (swap_rb == SWAP_UPLOADING)\n"
        "	  	gl_FragColor = vec4(texture2D(sampler, source_texture).gba, 1);\n"
        "     else if (swap_rb == SWAP_NONE_UPLOADING)\n"
        "	  	gl_FragColor = vec4(texture2D(sampler, source_texture).abg, 1);\n"
        "    } \n"
        "}\n";
    GLint fs_prog, vs_prog, avs_prog, set_alpha_prog;
    GLint sampler_uniform_location;
    char *source;

    glamor_priv = glamor_get_screen_private(screen);
    glamor_make_current(glamor_priv);
    glamor_priv->finish_access_prog[0] = glCreateProgram();
    glamor_priv->finish_access_prog[1] = glCreateProgram();

    vs_prog = glamor_compile_glsl_prog(GL_VERTEX_SHADER, vs_source);

    XNFasprintf(&source, "%s%s", common_source, fs_source);
    fs_prog = glamor_compile_glsl_prog(GL_FRAGMENT_SHADER, source);
    free(source);

    glAttachShader(glamor_priv->finish_access_prog[0], vs_prog);
    glAttachShader(glamor_priv->finish_access_prog[0], fs_prog);

    avs_prog = glamor_compile_glsl_prog(GL_VERTEX_SHADER, vs_source);

    XNFasprintf(&source, "%s%s", common_source, set_alpha_source);
    set_alpha_prog = glamor_compile_glsl_prog(GL_FRAGMENT_SHADER,
                                              source);
    free(source);

    glAttachShader(glamor_priv->finish_access_prog[1], avs_prog);
    glAttachShader(glamor_priv->finish_access_prog[1], set_alpha_prog);

    glBindAttribLocation(glamor_priv->finish_access_prog[0],
                         GLAMOR_VERTEX_POS, "v_position");
    glBindAttribLocation(glamor_priv->finish_access_prog[0],
                         GLAMOR_VERTEX_SOURCE, "v_texcoord0");
    glamor_link_glsl_prog(screen, glamor_priv->finish_access_prog[0],
                          "finish access 0");

    glBindAttribLocation(glamor_priv->finish_access_prog[1],
                         GLAMOR_VERTEX_POS, "v_position");
    glBindAttribLocation(glamor_priv->finish_access_prog[1],
                         GLAMOR_VERTEX_SOURCE, "v_texcoord0");
    glamor_link_glsl_prog(screen, glamor_priv->finish_access_prog[1],
                          "finish access 1");

    glamor_priv->finish_access_revert[0] =
        glGetUniformLocation(glamor_priv->finish_access_prog[0], "revert");

    glamor_priv->finish_access_swap_rb[0] =
        glGetUniformLocation(glamor_priv->finish_access_prog[0], "swap_rb");
    sampler_uniform_location =
        glGetUniformLocation(glamor_priv->finish_access_prog[0], "sampler");
    glUseProgram(glamor_priv->finish_access_prog[0]);
    glUniform1i(sampler_uniform_location, 0);
    glUniform1i(glamor_priv->finish_access_revert[0], 0);
    glUniform1i(glamor_priv->finish_access_swap_rb[0], 0);

    glamor_priv->finish_access_revert[1] =
        glGetUniformLocation(glamor_priv->finish_access_prog[1], "revert");
    glamor_priv->finish_access_swap_rb[1] =
        glGetUniformLocation(glamor_priv->finish_access_prog[1], "swap_rb");
    sampler_uniform_location =
        glGetUniformLocation(glamor_priv->finish_access_prog[1], "sampler");
    glUseProgram(glamor_priv->finish_access_prog[1]);
    glUniform1i(glamor_priv->finish_access_revert[1], 0);
    glUniform1i(sampler_uniform_location, 0);
    glUniform1i(glamor_priv->finish_access_swap_rb[1], 0);
}

void
glamor_fini_finish_access_shaders(ScreenPtr screen)
{
    glamor_screen_private *glamor_priv;

    glamor_priv = glamor_get_screen_private(screen);
    glamor_make_current(glamor_priv);
    glDeleteProgram(glamor_priv->finish_access_prog[0]);
    glDeleteProgram(glamor_priv->finish_access_prog[1]);
}

GCOps glamor_gc_ops = {
    .FillSpans = glamor_fill_spans,
    .SetSpans = glamor_set_spans,
    .PutImage = glamor_put_image,
    .CopyArea = glamor_copy_area,
    .CopyPlane = glamor_copy_plane,
    .PolyPoint = glamor_poly_point,
    .Polylines = glamor_poly_lines,
    .PolySegment = glamor_poly_segment,
    .PolyRectangle = miPolyRectangle,
    .PolyArc = miPolyArc,
    .FillPolygon = miFillPolygon,
    .PolyFillRect = glamor_poly_fill_rect,
    .PolyFillArc = miPolyFillArc,
    .PolyText8 = glamor_poly_text8,
    .PolyText16 = glamor_poly_text16,
    .ImageText8 = glamor_image_text8,
    .ImageText16 = glamor_image_text16,
    .ImageGlyphBlt = miImageGlyphBlt,
    .PolyGlyphBlt = glamor_poly_glyph_blt,
    .PushPixels = glamor_push_pixels,
};

/*
 * When the stipple is changed or drawn to, invalidate any
 * cached copy
 */
static void
glamor_invalidate_stipple(GCPtr gc)
{
    glamor_gc_private *gc_priv = glamor_get_gc_private(gc);

    if (gc_priv->stipple) {
        if (gc_priv->stipple_damage)
            DamageUnregister(gc_priv->stipple_damage);
        glamor_destroy_pixmap(gc_priv->stipple);
        gc_priv->stipple = NULL;
    }
}

static void
glamor_stipple_damage_report(DamagePtr damage, RegionPtr region,
                             void *closure)
{
    GCPtr       gc = closure;

    glamor_invalidate_stipple(gc);
}

static void
glamor_stipple_damage_destroy(DamagePtr damage, void *closure)
{
    GCPtr               gc = closure;
    glamor_gc_private   *gc_priv = glamor_get_gc_private(gc);

    gc_priv->stipple_damage = NULL;
    glamor_invalidate_stipple(gc);
}

void
glamor_track_stipple(GCPtr gc)
{
    if (gc->stipple) {
        glamor_gc_private *gc_priv = glamor_get_gc_private(gc);

        if (!gc_priv->stipple_damage)
            gc_priv->stipple_damage = DamageCreate(glamor_stipple_damage_report,
                                                   glamor_stipple_damage_destroy,
                                                   DamageReportNonEmpty,
                                                   TRUE, gc->pScreen, gc);
        if (gc_priv->stipple_damage)
            DamageRegister(&gc->stipple->drawable, gc_priv->stipple_damage);
    }
}

static GCFuncs glamor_fixup_gc_funcs;

/**
 * uxa_validate_gc() sets the ops to glamor's implementations, which may be
 * accelerated or may sync the card and fall back to fb.
 */
void
glamor_validate_gc(GCPtr gc, unsigned long changes, DrawablePtr drawable)
{
    /* fbValidateGC will do direct access to pixmaps if the tiling has changed.
     * Preempt fbValidateGC by doing its work and masking the change out, so
     * that we can do the Prepare/finish_access.
     */
#ifdef FB_24_32BIT
    if ((changes & GCTile) && fbGetRotatedPixmap(gc)) {
        gc->pScreen->DestroyPixmap(fbGetRotatedPixmap(gc));
        fbGetRotatedPixmap(gc) = 0;
    }

    /* XXX kludge alert -- check to see if our destroy_gc is going to be called,
     * if not, try to work around that
     */
    if (gc->funcs->DestroyGC != glamor_destroy_gc) {
        ErrorF("Driver not using glamor_destroy_gc, working around that");
        if (gc->funcs->DestroyGC != miDestroyGC)
            ErrorF("Driver also not using miDestroyGC. This may well crash or leak memory.");
        glamor_fixup_gc_funcs = *gc->funcs;
        glamor_fixup_gc_funcs.DestroyGC = glamor_destroy_gc;
        gc->funcs = &glamor_fixup_gc_funcs;
    }

    if (gc->fillStyle == FillTiled) {
        PixmapPtr old_tile, new_tile;

        old_tile = gc->tile.pixmap;
        if (old_tile->drawable.bitsPerPixel != drawable->bitsPerPixel) {
            new_tile = fbGetRotatedPixmap(gc);
            if (!new_tile ||
                new_tile->drawable.bitsPerPixel != drawable->bitsPerPixel) {
                if (new_tile)
                    gc->pScreen->DestroyPixmap(new_tile);
                /* fb24_32ReformatTile will do direct access of a newly-
                 * allocated pixmap.
                 */
                glamor_fallback
                    ("GC %p tile FB_24_32 transformat %p.\n", gc, old_tile);

                if (glamor_prepare_access
                    (&old_tile->drawable, GLAMOR_ACCESS_RO)) {
                    new_tile =
                        fb24_32ReformatTile(old_tile, drawable->bitsPerPixel);
                    glamor_finish_access(&old_tile->drawable);
                }
            }
            if (new_tile) {
                fbGetRotatedPixmap(gc) = old_tile;
                gc->tile.pixmap = new_tile;
                changes |= GCTile;
            }
        }
    }
#endif
    if (changes & GCTile) {
        if (!gc->tileIsPixel) {
            glamor_pixmap_private *pixmap_priv =
                glamor_get_pixmap_private(gc->tile.pixmap);
            if ((!GLAMOR_PIXMAP_PRIV_HAS_FBO(pixmap_priv))
                && FbEvenTile(gc->tile.pixmap->drawable.width *
                              drawable->bitsPerPixel)) {
                glamor_fallback
                    ("GC %p tile changed %p.\n", gc, gc->tile.pixmap);
                if (glamor_prepare_access
                    (&gc->tile.pixmap->drawable, GLAMOR_ACCESS_RW)) {
                    fbPadPixmap(gc->tile.pixmap);
                    glamor_finish_access(&gc->tile.pixmap->drawable);
                }
            }
        }
        /* Mask out the GCTile change notification, now that we've done FB's
         * job for it.
         */
        changes &= ~GCTile;
    }

    if (changes & GCStipple)
        glamor_invalidate_stipple(gc);

    if (changes & GCStipple && gc->stipple) {
        /* We can't inline stipple handling like we do for GCTile because
         * it sets fbgc privates.
         */
        if (glamor_prepare_access(&gc->stipple->drawable, GLAMOR_ACCESS_RW)) {
            fbValidateGC(gc, changes, drawable);
            glamor_finish_access(&gc->stipple->drawable);
        }
    }
    else {
        fbValidateGC(gc, changes, drawable);
    }

    if (changes & GCDashList) {
        glamor_gc_private *gc_priv = glamor_get_gc_private(gc);

        if (gc_priv->dash) {
            glamor_destroy_pixmap(gc_priv->dash);
            gc_priv->dash = NULL;
        }
    }

    gc->ops = &glamor_gc_ops;
}

void
glamor_destroy_gc(GCPtr gc)
{
    glamor_gc_private *gc_priv = glamor_get_gc_private(gc);

    if (gc_priv->dash) {
        glamor_destroy_pixmap(gc_priv->dash);
        gc_priv->dash = NULL;
    }
    glamor_invalidate_stipple(gc);
    if (gc_priv->stipple_damage)
        DamageDestroy(gc_priv->stipple_damage);
    miDestroyGC(gc);
}

static GCFuncs glamor_gc_funcs = {
    glamor_validate_gc,
    miChangeGC,
    miCopyGC,
    glamor_destroy_gc,
    miChangeClip,
    miDestroyClip,
    miCopyClip
};

/**
 * exaCreateGC makes a new GC and hooks up its funcs handler, so that
 * exaValidateGC() will get called.
 */
int
glamor_create_gc(GCPtr gc)
{
    glamor_gc_private *gc_priv = glamor_get_gc_private(gc);

    gc_priv->dash = NULL;
    gc_priv->stipple = NULL;
    if (!fbCreateGC(gc))
        return FALSE;

    gc->funcs = &glamor_gc_funcs;

    return TRUE;
}

RegionPtr
glamor_bitmap_to_region(PixmapPtr pixmap)
{
    RegionPtr ret;

    glamor_fallback("pixmap %p \n", pixmap);
    if (!glamor_prepare_access(&pixmap->drawable, GLAMOR_ACCESS_RO))
        return NULL;
    ret = fbPixmapToRegion(pixmap);
    glamor_finish_access(&pixmap->drawable);
    return ret;
}