summaryrefslogtreecommitdiff
path: root/glamor/glamor_picture.c
blob: e0458a6f2b7482835e1acc8c3a66e14c9f0a7277 (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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/*
 * Copyright © 2009 Intel Corporation
 * Copyright © 1998 Keith Packard
 *
 * 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:
 *    Zhigang Gong <zhigang.gong@gmail.com>
 *
 */

#include <stdlib.h>

#include "glamor_priv.h"
#include "mipict.h"

/*
 * Map picture's format to the correct gl texture format and type.
 * no_alpha is used to indicate whehter we need to wire alpha to 1.
 *
 * Although opengl support A1/GL_BITMAP, we still don't use it
 * here, it seems that mesa has bugs when uploading a A1 bitmap.
 *
 * Return 0 if find a matched texture type. Otherwise return -1.
 **/
static Bool
glamor_get_tex_format_type_from_pictformat(ScreenPtr pScreen,
                                           PictFormatShort format,
                                           GLenum *tex_format,
                                           GLenum *tex_type,
                                           int *no_alpha,
                                           int *revert,
                                           int *swap_rb)
{
    glamor_screen_private *glamor_priv = glamor_get_screen_private(pScreen);
    Bool is_little_endian = IMAGE_BYTE_ORDER == LSBFirst;

    *no_alpha = 0;
    *revert = REVERT_NONE;
    *swap_rb = SWAP_NONE_UPLOADING;

    switch (format) {
    case PICT_a1:
        *tex_format = glamor_priv->one_channel_format;
        *tex_type = GL_UNSIGNED_BYTE;
        *revert = REVERT_UPLOADING_A1;
        break;

    case PICT_b8g8r8x8:
        *no_alpha = 1;
    case PICT_b8g8r8a8:
        if (glamor_priv->gl_flavor == GLAMOR_GL_DESKTOP) {
            *tex_format = GL_BGRA;
            *tex_type = GL_UNSIGNED_INT_8_8_8_8;
        } else {
            *tex_format = GL_RGBA;
            *tex_type = GL_UNSIGNED_BYTE;
            *swap_rb = SWAP_UPLOADING;
            *revert = is_little_endian ? REVERT_NORMAL : REVERT_NONE;
        }
        break;

    case PICT_x8r8g8b8:
        *no_alpha = 1;
    case PICT_a8r8g8b8:
        if (glamor_priv->gl_flavor == GLAMOR_GL_DESKTOP) {
            *tex_format = GL_BGRA;
            *tex_type = GL_UNSIGNED_INT_8_8_8_8_REV;
        } else {
            *tex_format = GL_RGBA;
            *tex_type = GL_UNSIGNED_BYTE;
            *swap_rb = SWAP_UPLOADING;
            *revert = is_little_endian ? REVERT_NONE : REVERT_NORMAL;
            break;
        }
        break;

    case PICT_x8b8g8r8:
        *no_alpha = 1;
    case PICT_a8b8g8r8:
        *tex_format = GL_RGBA;
        if (glamor_priv->gl_flavor == GLAMOR_GL_DESKTOP) {
            *tex_type = GL_UNSIGNED_INT_8_8_8_8_REV;
        } else {
            *tex_type = GL_UNSIGNED_BYTE;
            *revert = is_little_endian ? REVERT_NONE : REVERT_NORMAL;
        }
        break;

    case PICT_x2r10g10b10:
        *no_alpha = 1;
    case PICT_a2r10g10b10:
        if (glamor_priv->gl_flavor == GLAMOR_GL_DESKTOP) {
            *tex_format = GL_BGRA;
            *tex_type = GL_UNSIGNED_INT_2_10_10_10_REV;
        } else {
            return FALSE;
        }
        break;

    case PICT_x2b10g10r10:
        *no_alpha = 1;
    case PICT_a2b10g10r10:
        if (glamor_priv->gl_flavor == GLAMOR_GL_DESKTOP) {
            *tex_format = GL_RGBA;
            *tex_type = GL_UNSIGNED_INT_2_10_10_10_REV;
        } else {
            return FALSE;
        }
        break;

    case PICT_r5g6b5:
        *tex_format = GL_RGB;
        *tex_type = GL_UNSIGNED_SHORT_5_6_5;
        if (glamor_priv->gl_flavor != GLAMOR_GL_DESKTOP)
            *revert = is_little_endian ? REVERT_NONE : REVERT_NORMAL;
        break;
    case PICT_b5g6r5:
        *tex_format = GL_RGB;
        if (glamor_priv->gl_flavor == GLAMOR_GL_DESKTOP) {
            *tex_type = GL_UNSIGNED_SHORT_5_6_5_REV;
        } else {
            *tex_type = GL_UNSIGNED_SHORT_5_6_5;
            if (is_little_endian)
                *swap_rb = SWAP_UPLOADING;
            *revert = is_little_endian ? REVERT_NONE : REVERT_NORMAL;
        }
        break;

    case PICT_x1b5g5r5:
        *no_alpha = 1;
    case PICT_a1b5g5r5:
        *tex_format = GL_RGBA;
        if (glamor_priv->gl_flavor == GLAMOR_GL_DESKTOP) {
            *tex_type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
        } else {
            return FALSE;
        }
        break;

    case PICT_x1r5g5b5:
        *no_alpha = 1;
    case PICT_a1r5g5b5:
        if (glamor_priv->gl_flavor == GLAMOR_GL_DESKTOP) {
            *tex_format = GL_BGRA;
            *tex_type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
        } else {
            return FALSE;
        }
        break;

    case PICT_a8:
        *tex_format = glamor_priv->one_channel_format;
        *tex_type = GL_UNSIGNED_BYTE;
        break;

    case PICT_x4r4g4b4:
        *no_alpha = 1;
    case PICT_a4r4g4b4:
        if (glamor_priv->gl_flavor == GLAMOR_GL_DESKTOP) {
            *tex_format = GL_BGRA;
            *tex_type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
        } else {
            *tex_format = GL_RGBA;
            *tex_type = GL_UNSIGNED_SHORT_4_4_4_4;
            *revert = is_little_endian ? REVERT_NORMAL : REVERT_NONE;
            *swap_rb = SWAP_UPLOADING;
        }
        break;

    case PICT_x4b4g4r4:
        *no_alpha = 1;
    case PICT_a4b4g4r4:
        if (glamor_priv->gl_flavor == GLAMOR_GL_DESKTOP) {
            *tex_format = GL_RGBA;
            *tex_type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
        } else {
            *tex_format = GL_RGBA;
            *tex_type = GL_UNSIGNED_SHORT_4_4_4_4;
            *revert = is_little_endian ? REVERT_NORMAL : REVERT_NONE;
        }
        break;

    default:
        return FALSE;
    }
    return TRUE;
}

/**
 * Takes a set of source bits with a given format and returns an
 * in-memory pixman image of those bits in a destination format.
 */
static pixman_image_t *
glamor_get_converted_image(PictFormatShort dst_format,
                           PictFormatShort src_format,
                           void *src_bits,
                           int src_stride,
                           int w, int h)
{
    pixman_image_t *dst_image;
    pixman_image_t *src_image;

    dst_image = pixman_image_create_bits(dst_format, w, h, NULL, 0);
    if (dst_image == NULL) {
        return NULL;
    }

    src_image = pixman_image_create_bits(src_format, w, h, src_bits, src_stride);

    if (src_image == NULL) {
        pixman_image_unref(dst_image);
        return NULL;
    }

    pixman_image_composite(PictOpSrc, src_image, NULL, dst_image,
                           0, 0, 0, 0, 0, 0, w, h);

    pixman_image_unref(src_image);
    return dst_image;
}

/**
 * Upload pixmap to a specified texture.
 * This texture may not be the one attached to it.
 **/
static Bool
__glamor_upload_pixmap_to_texture(PixmapPtr pixmap, unsigned int *tex,
                                  GLenum format,
                                  GLenum type,
                                  int x, int y, int w, int h,
                                  void *bits)
{
    glamor_screen_private *glamor_priv =
        glamor_get_screen_private(pixmap->drawable.pScreen);
    int non_sub = 0;
    unsigned int iformat = 0;

    glamor_make_current(glamor_priv);
    if (*tex == 0) {
        glGenTextures(1, tex);
        if (glamor_priv->gl_flavor == GLAMOR_GL_DESKTOP)
            iformat = gl_iformat_for_pixmap(pixmap);
        else
            iformat = format;
        non_sub = 1;
        assert(x == 0 && y == 0);
    }

    glBindTexture(GL_TEXTURE_2D, *tex);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 4);

    glamor_priv->suppress_gl_out_of_memory_logging = true;
    if (non_sub)
        glTexImage2D(GL_TEXTURE_2D, 0, iformat, w, h, 0, format, type, bits);
    else
        glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, w, h, format, type, bits);
    glamor_priv->suppress_gl_out_of_memory_logging = false;
    if (glGetError() == GL_OUT_OF_MEMORY) {
        if (non_sub) {
            glDeleteTextures(1, tex);
            *tex = 0;
        }
        return FALSE;
    }

    return TRUE;
}

static Bool
_glamor_upload_bits_to_pixmap_texture(PixmapPtr pixmap, GLenum format,
                                      GLenum type, int no_alpha, int revert,
                                      int swap_rb, int x, int y, int w, int h,
                                      int stride, void *bits)
{
    ScreenPtr screen = pixmap->drawable.pScreen;
    glamor_pixmap_private *pixmap_priv = glamor_get_pixmap_private(pixmap);
    glamor_screen_private *glamor_priv =
        glamor_get_screen_private(pixmap->drawable.pScreen);
    float dst_xscale, dst_yscale;
    GLuint tex = 0;
    pixman_image_t *converted_image = NULL;

    if (revert == REVERT_UPLOADING_A1) {
        converted_image = glamor_get_converted_image(PICT_a8,
                                                     PICT_a1,
                                                     bits,
                                                     PixmapBytePad(w, 1),
                                                     w, h);
        if (!converted_image)
            return FALSE;

        bits = pixman_image_get_data(converted_image);
    }

    /* Try fast path firstly, upload the pixmap to the texture attached
     * to the fbo directly. */
    if (no_alpha == 0
        && revert == REVERT_NONE && swap_rb == SWAP_NONE_UPLOADING
#ifdef WALKAROUND_LARGE_TEXTURE_MAP
        && glamor_pixmap_priv_is_small(pixmap_priv)
#endif
        ) {
        int fbo_x_off, fbo_y_off;

        assert(pixmap_priv->fbo->tex);
        pixmap_priv_get_fbo_off(pixmap_priv, &fbo_x_off, &fbo_y_off);

        assert(x + fbo_x_off >= 0 && y + fbo_y_off >= 0);
        assert(x + fbo_x_off + w <= pixmap_priv->fbo->width);
        assert(y + fbo_y_off + h <= pixmap_priv->fbo->height);
        if (!__glamor_upload_pixmap_to_texture(pixmap, &pixmap_priv->fbo->tex,
                                               format, type,
                                               x + fbo_x_off, y + fbo_y_off,
                                               w, h,
                                               bits)) {
            if (converted_image)
                pixman_image_unref(bits);
            return FALSE;
        }
    } else {
        static const float texcoords_inv[8] = { 0, 0,
                                                1, 0,
                                                1, 1,
                                                0, 1
        };
        GLfloat *v;
        char *vbo_offset;

        v = glamor_get_vbo_space(screen, 16 * sizeof(GLfloat), &vbo_offset);

        pixmap_priv_get_dest_scale(pixmap, pixmap_priv, &dst_xscale, &dst_yscale);
        glamor_set_normalize_vcoords(pixmap_priv, dst_xscale,
                                     dst_yscale,
                                     x, y,
                                     x + w, y + h,
                                     v);
        /* Slow path, we need to flip y or wire alpha to 1. */
        glamor_make_current(glamor_priv);

        if (!__glamor_upload_pixmap_to_texture(pixmap, &tex,
                                               format, type, 0, 0, w, h, bits)) {
            if (converted_image)
                pixman_image_unref(bits);
            return FALSE;
        }

        memcpy(&v[8], texcoords_inv, 8 * sizeof(GLfloat));

        glVertexAttribPointer(GLAMOR_VERTEX_POS, 2, GL_FLOAT,
                              GL_FALSE, 2 * sizeof(float), vbo_offset);
        glEnableVertexAttribArray(GLAMOR_VERTEX_POS);
        glVertexAttribPointer(GLAMOR_VERTEX_SOURCE, 2, GL_FLOAT,
                              GL_FALSE, 2 * sizeof(float), vbo_offset + 8 * sizeof(GLfloat));
        glEnableVertexAttribArray(GLAMOR_VERTEX_SOURCE);

        glamor_put_vbo_space(screen);
        glamor_set_destination_pixmap_priv_nc(glamor_priv, pixmap, pixmap_priv);
        glamor_set_alu(screen, GXcopy);
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, tex);

        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        glUseProgram(glamor_priv->finish_access_prog[no_alpha]);
        glUniform1i(glamor_priv->finish_access_revert[no_alpha], revert);
        glUniform1i(glamor_priv->finish_access_swap_rb[no_alpha], swap_rb);

        glDrawArrays(GL_TRIANGLE_FAN, 0, 4);

        glDisableVertexAttribArray(GLAMOR_VERTEX_POS);
        glDisableVertexAttribArray(GLAMOR_VERTEX_SOURCE);
        glDeleteTextures(1, &tex);
        glBindFramebuffer(GL_FRAMEBUFFER, 0);
    }

    if (converted_image)
        pixman_image_unref(bits);
    return TRUE;
}

/*
 * Prepare to upload a pixmap to texture memory.
 * no_alpha equals 1 means the format needs to wire alpha to 1.
 */
static int
glamor_pixmap_upload_prepare(PixmapPtr pixmap, GLenum format, int no_alpha,
                             int revert, int swap_rb)
{
    int flag = 0;
    glamor_screen_private *glamor_priv =
        glamor_get_screen_private(pixmap->drawable.pScreen);
    GLenum iformat;

    if (!(no_alpha || (revert == REVERT_NORMAL)
          || (swap_rb != SWAP_NONE_UPLOADING))) {
        /* We don't need a fbo, a simple texture uploading should work. */

        flag = GLAMOR_CREATE_FBO_NO_FBO;
    }

    if (glamor_priv->gl_flavor == GLAMOR_GL_DESKTOP)
        iformat = gl_iformat_for_pixmap(pixmap);
    else
        iformat = format;

    if (!glamor_pixmap_ensure_fbo(pixmap, iformat, flag))
        return -1;

    return 0;
}

/*
 * upload sub region to a large region.
 * */
static void
glamor_put_bits(char *dst_bits, int dst_stride, char *src_bits,
                int src_stride, int bpp, int x, int y, int w, int h)
{
    int j;
    int byte_per_pixel;

    byte_per_pixel = bpp / 8;
    src_bits += y * src_stride + (x * byte_per_pixel);

    for (j = y; j < y + h; j++) {
        memcpy(dst_bits, src_bits, w * byte_per_pixel);
        src_bits += src_stride;
        dst_bits += dst_stride;
    }
}

/**
 * Uploads a picture based on a GLAMOR_MEMORY pixmap to a texture in a
 * temporary FBO.
 */
Bool
glamor_upload_picture_to_texture(PicturePtr picture)
{
    PixmapPtr pixmap = glamor_get_drawable_pixmap(picture->pDrawable);
    void *bits = pixmap->devPrivate.ptr;
    int stride = pixmap->devKind;
    ScreenPtr screen = pixmap->drawable.pScreen;
    glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
    GLenum format, type;
    int no_alpha, revert, swap_rb;
    glamor_pixmap_private *pixmap_priv = glamor_get_pixmap_private(pixmap);
    Bool force_clip;

    assert(glamor_pixmap_is_memory(pixmap));
    assert(!pixmap_priv->fbo);

    if (!glamor_get_tex_format_type_from_pictformat(screen,
                                                    picture->format,
                                                    &format,
                                                    &type,
                                                    &no_alpha,
                                                    &revert, &swap_rb)) {
        glamor_fallback("Unknown pixmap depth %d.\n", pixmap->drawable.depth);
        return FALSE;
    }
    if (glamor_pixmap_upload_prepare(pixmap, format, no_alpha, revert, swap_rb))
        return FALSE;

    force_clip = glamor_priv->gl_flavor != GLAMOR_GL_DESKTOP;

    if (glamor_pixmap_priv_is_large(pixmap_priv) || force_clip) {
        RegionRec region;
        BoxRec box;
        int n_region;
        glamor_pixmap_clipped_regions *clipped_regions;
        void *sub_bits;
        int i, j;

        sub_bits = xallocarray(pixmap->drawable.height, stride);
        if (sub_bits == NULL)
            return FALSE;
        box.x1 = 0;
        box.y1 = 0;
        box.x2 = pixmap->drawable.width;
        box.y2 = pixmap->drawable.height;
        RegionInitBoxes(&region, &box, 1);
        if (!force_clip)
            clipped_regions =
                glamor_compute_clipped_regions(pixmap, &region, &n_region,
                                               0, 0, 0);
        else
            clipped_regions =
                glamor_compute_clipped_regions_ext(pixmap, &region,
                                                   &n_region,
                                                   pixmap_priv->block_w,
                                                   pixmap_priv->block_h,
                                                   0,
                                                   0);
        DEBUGF("prepare upload %dx%d to a large pixmap %p\n", w, h, pixmap);
        for (i = 0; i < n_region; i++) {
            BoxPtr boxes;
            int nbox;
            int temp_stride;
            void *temp_bits;

            glamor_set_pixmap_fbo_current(pixmap_priv, clipped_regions[i].block_idx);

            boxes = RegionRects(clipped_regions[i].region);
            nbox = RegionNumRects(clipped_regions[i].region);
            DEBUGF("split to %d boxes\n", nbox);
            for (j = 0; j < nbox; j++) {
                temp_stride = PixmapBytePad(boxes[j].x2 - boxes[j].x1,
                                            pixmap->drawable.depth);

                if (boxes[j].x1 == 0 && temp_stride == stride) {
                    temp_bits = (char *) bits + boxes[j].y1 * stride;
                }
                else {
                    temp_bits = sub_bits;
                    glamor_put_bits(temp_bits, temp_stride, bits, stride,
                                    pixmap->drawable.bitsPerPixel,
                                    boxes[j].x1, boxes[j].y1,
                                    boxes[j].x2 - boxes[j].x1,
                                    boxes[j].y2 - boxes[j].y1);
                }
                DEBUGF("upload x %d y %d w %d h %d temp stride %d \n",
                       boxes[j].x1, boxes[j].y1,
                       boxes[j].x2 - boxes[j].x1,
                       boxes[j].y2 - boxes[j].y1, temp_stride);
                if (!_glamor_upload_bits_to_pixmap_texture
                    (pixmap, format, type, no_alpha, revert, swap_rb,
                     boxes[j].x1, boxes[j].y1, boxes[j].x2 - boxes[j].x1,
                     boxes[j].y2 - boxes[j].y1, temp_stride, temp_bits)) {
                    RegionUninit(&region);
                    free(sub_bits);
                    assert(0);
                    return FALSE;
                }
            }
            RegionDestroy(clipped_regions[i].region);
        }
        free(sub_bits);
        free(clipped_regions);
        RegionUninit(&region);
        return TRUE;
    }
    else
        return _glamor_upload_bits_to_pixmap_texture(pixmap, format, type,
                                                     no_alpha, revert, swap_rb,
                                                     0, 0,
                                                     pixmap->drawable.width,
                                                     pixmap->drawable.height,
                                                     stride, bits);
}