aboutsummaryrefslogtreecommitdiffstats
path: root/recipes-graphics/xorg-xserver/xserver-xorg/0008-glamor-Introduce-a-central-place-for-our-pixmap-form.patch
blob: dd82340a61513a428a5735a647e770f7604dbc59 (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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
From b75296bee6ab3578f3a13cfb6de5d77ec02b9047 Mon Sep 17 00:00:00 2001
From: Eric Anholt <eric@anholt.net>
Date: Tue, 26 Mar 2019 15:10:49 -0700
Subject: [PATCH 8/8] glamor: Introduce a central place for our pixmap
 format/type handling.

We had various helper functions trying to come up with the
internalformat/format/type/render formats for pixmaps, and it's much
nicer to just detect what those should be once at startup.  This gives
us a chance to do the right thing for GLES.

It also, notably, fixes our format/type for depth 15 and 16 setup for
desktop GL, so that we actually allocate 16bpp (GL_RGB/565) on most
drivers instead of 32bpp (GL_RGB/UBYTE).

GLES still has regressions over desktop (2 regressions in llvmpipe
XTS, many in rendercheck), but I think this is a good baseline.

Upstream-Status: Backport
Signed-off-by: Eric Anholt <eric@anholt.net>
---
 glamor/glamor.c           | 167 ++++++++++++++++++++++++++++++++++++--
 glamor/glamor_fbo.c       |  16 ++--
 glamor/glamor_picture.c   |   7 +-
 glamor/glamor_priv.h      |  22 ++++-
 glamor/glamor_render.c    |   7 +-
 glamor/glamor_spans.c     |  14 ++--
 glamor/glamor_transfer.c  |  56 ++-----------
 glamor/glamor_transfer.h  |   3 -
 glamor/glamor_transform.c |   5 +-
 glamor/glamor_utils.h     |  57 -------------
 10 files changed, 209 insertions(+), 145 deletions(-)

diff --git a/glamor/glamor.c b/glamor/glamor.c
index 019edbbb1..3450113e0 100644
--- a/glamor/glamor.c
+++ b/glamor/glamor.c
@@ -212,7 +212,7 @@ glamor_create_pixmap(ScreenPtr screen, int w, int h, int depth,
              w <= glamor_priv->glyph_max_dim &&
              h <= glamor_priv->glyph_max_dim)
          || (w == 0 && h == 0)
-         || !glamor_check_pixmap_fbo_depth(depth)))
+         || !glamor_priv->formats[depth].format))
         return fbCreatePixmap(screen, w, h, depth, usage);
     else
         pixmap = fbCreatePixmap(screen, 0, 0, depth, usage);
@@ -440,6 +440,165 @@ glamor_setup_debug_output(ScreenPtr screen)
         glEnable(GL_DEBUG_OUTPUT);
 }
 
+const struct glamor_format *
+glamor_format_for_pixmap(PixmapPtr pixmap)
+{
+    ScreenPtr pScreen = pixmap->drawable.pScreen;
+    glamor_screen_private *glamor_priv = glamor_get_screen_private(pScreen);
+    glamor_pixmap_private *pixmap_priv = glamor_get_pixmap_private(pixmap);
+
+    if (pixmap_priv->is_cbcr)
+        return &glamor_priv->cbcr_format;
+    else
+        return &glamor_priv->formats[pixmap->drawable.depth];
+}
+
+static void
+glamor_add_format(ScreenPtr screen, int depth, CARD32 render_format,
+                  GLenum internalformat, GLenum format, GLenum type)
+{
+    glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
+    struct glamor_format *f = &glamor_priv->formats[depth];
+
+    /* If we're trying to run on GLES, make sure that we get the read
+     * formats that we're expecting, since glamor_transfer relies on
+     * them matching to get data back out.  To avoid this limitation, we
+     * would need to have a more general glReadPixels() path in
+     * glamor_transfer that re-encoded the bits to the pixel format that
+     * we intended after.
+     *
+     * Note that we can't just create a pixmap because we're in
+     * screeninit.
+     */
+    if (glamor_priv->is_gles) {
+        unsigned fbo, tex;
+        int read_format, read_type;
+        GLenum status;
+
+        glGenTextures(1, &tex);
+        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);
+        glTexImage2D(GL_TEXTURE_2D, 0, internalformat, 1, 1, 0,
+                     format, type, NULL);
+
+        glGenFramebuffers(1, &fbo);
+        glBindFramebuffer(GL_FRAMEBUFFER, fbo);
+        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
+                               GL_TEXTURE_2D, tex, 0);
+        status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
+        if (status != GL_FRAMEBUFFER_COMPLETE) {
+            ErrorF("glamor: Test fbo for depth %d incomplete.  "
+                   "Falling back to software.\n", depth);
+            glDeleteTextures(1, &tex);
+            glDeleteFramebuffers(1, &fbo);
+            return;
+        }
+
+        glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_FORMAT, &read_format);
+        glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_TYPE, &read_type);
+
+        glDeleteTextures(1, &tex);
+        glDeleteFramebuffers(1, &fbo);
+
+        if (format != read_format || type != read_type) {
+            ErrorF("glamor: Implementation returned 0x%x/0x%x read format/type "
+                   "for depth %d, expected 0x%x/0x%x.  "
+                   "Falling back to software.\n",
+                   read_format, read_type, depth, format, type);
+            return;
+        }
+    }
+
+    f->depth = depth;
+    f->render_format = render_format;
+    f->internalformat = internalformat;
+    f->format = format;
+    f->type = type;
+}
+
+/* Set up the GL format/types that glamor will use for the various depths
+ *
+ * X11's pixel data doesn't have channels, but to store our data in GL
+ * we have to pick some sort of format to move X11 pixel data in and
+ * out with in glamor_transfer.c.  For X11 core operations, other than
+ * GL logic ops (non-GXcopy GC ops) what the driver chooses internally
+ * doesn't matter as long as it doesn't drop any bits (we expect them
+ * to generally expand, if anything).  For Render, we can expect
+ * clients to tend to render with PictFormats matching our channel
+ * layouts here since ultimately X11 pixels tend to end up on the
+ * screen.  The render implementation will fall back to fb if the
+ * channels don't match.
+ *
+ * Note that these formats don't affect what glamor_egl.c or
+ * Xwayland's EGL layer choose for surfaces exposed through DRI or
+ * scanout.  For now, those layers need to match what we're choosing
+ * here, or channels will end up swizzled around.  Similarly, the
+ * driver's visual masks also need to match what we're doing here.
+ */
+static void
+glamor_setup_formats(ScreenPtr screen)
+{
+    glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
+
+    /* Prefer r8 textures since they're required by GLES3 and core,
+     * only falling back to a8 if we can't do them.
+     */
+    if (glamor_priv->is_gles || epoxy_has_gl_extension("GL_ARB_texture_rg")) {
+        glamor_add_format(screen, 8, PICT_a8,
+                          GL_R8, GL_RED, GL_UNSIGNED_BYTE);
+    } else {
+        glamor_add_format(screen, 8, PICT_a8,
+                          GL_ALPHA, GL_ALPHA, GL_UNSIGNED_BYTE);
+    }
+
+    if (glamor_priv->is_gles) {
+        /* For 15bpp, GLES supports format/type RGBA/5551, rather than
+         * bgra/1555_rev.  GL_EXT_bgra lets the impl say the color
+         * read format/type is bgra/1555 even if we had to create it
+         * with rgba/5551, with Mesa does.  That means we can't use
+         * the same format/type for TexSubImage and readpixels.
+         *
+         * Instead, just store 16 bits using the trusted 565 path, and
+         * disable render accel for now.
+         */
+        glamor_add_format(screen, 15, PICT_x1r5g5b5,
+                          GL_RGB5_A1, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1);
+    } else {
+        glamor_add_format(screen, 15, PICT_x1r5g5b5,
+                          GL_RGBA, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV);
+    }
+
+    glamor_add_format(screen, 16, PICT_r5g6b5,
+                      GL_RGB, GL_RGB, GL_UNSIGNED_SHORT_5_6_5);
+
+    if (glamor_priv->is_gles) {
+        assert(X_BYTE_ORDER == X_LITTLE_ENDIAN);
+        glamor_add_format(screen, 24, PICT_x8b8g8r8,
+                          GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE);
+        glamor_add_format(screen, 32, PICT_a8b8g8r8,
+                          GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE);
+    } else {
+        glamor_add_format(screen, 24, PICT_x8r8g8b8,
+                          GL_RGBA, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV);
+        glamor_add_format(screen, 32, PICT_a8r8g8b8,
+                          GL_RGBA, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV);
+    }
+
+    if (glamor_priv->is_gles) {
+        glamor_add_format(screen, 30, PICT_x2b10g10r10,
+                          GL_RGB10_A2, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV);
+    } else {
+        glamor_add_format(screen, 30, PICT_x2r10g10b10,
+                          GL_RGB10_A2, GL_BGRA, GL_UNSIGNED_INT_2_10_10_10_REV);
+    }
+
+    glamor_priv->cbcr_format.depth = 16;
+    glamor_priv->cbcr_format.internalformat = GL_RG8;
+    glamor_priv->cbcr_format.format = GL_RG;
+    glamor_priv->cbcr_format.type = GL_UNSIGNED_BYTE;
+}
+
 /** Set up glamor for an already-configured GL context. */
 Bool
 glamor_init(ScreenPtr screen, unsigned int flags)
@@ -665,11 +824,7 @@ glamor_init(ScreenPtr screen, unsigned int flags)
         (epoxy_has_gl_extension("GL_ARB_texture_swizzle") ||
          (glamor_priv->is_gles && gl_version >= 30));
 
-    glamor_priv->one_channel_format = GL_ALPHA;
-    if (epoxy_has_gl_extension("GL_ARB_texture_rg") &&
-        glamor_priv->has_texture_swizzle) {
-        glamor_priv->one_channel_format = GL_RED;
-    }
+    glamor_setup_formats(screen);
 
     glamor_set_debug_level(&glamor_debug_level);
 
diff --git a/glamor/glamor_fbo.c b/glamor/glamor_fbo.c
index 75f7e2baa..dfb3f754d 100644
--- a/glamor/glamor_fbo.c
+++ b/glamor/glamor_fbo.c
@@ -97,7 +97,7 @@ glamor_pixmap_fbo *
 glamor_create_fbo_from_tex(glamor_screen_private *glamor_priv,
                            PixmapPtr pixmap, int w, int h, GLint tex, int flag)
 {
-    GLenum format = gl_iformat_for_pixmap(pixmap);
+    const struct glamor_format *f = glamor_format_for_pixmap(pixmap);
     glamor_pixmap_fbo *fbo;
 
     fbo = calloc(1, sizeof(*fbo));
@@ -107,7 +107,7 @@ glamor_create_fbo_from_tex(glamor_screen_private *glamor_priv,
     fbo->tex = tex;
     fbo->width = w;
     fbo->height = h;
-    fbo->is_red = format == GL_RED;
+    fbo->is_red = f->format == GL_RED;
 
     if (flag != GLAMOR_CREATE_FBO_NO_FBO) {
         if (glamor_pixmap_ensure_fb(glamor_priv, fbo) != 0) {
@@ -123,23 +123,19 @@ static int
 _glamor_create_tex(glamor_screen_private *glamor_priv,
                    PixmapPtr pixmap, int w, int h)
 {
-    GLenum iformat = gl_iformat_for_pixmap(pixmap);
-    GLenum format = iformat;
+    const struct glamor_format *f = glamor_format_for_pixmap(pixmap);
     unsigned int tex;
 
-    if (format == GL_RGB10_A2)
-        format = GL_RGBA;
-
     glamor_make_current(glamor_priv);
     glGenTextures(1, &tex);
     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);
-    if (format == glamor_priv->one_channel_format && format == GL_RED)
+    if (f->format == GL_RED)
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_RED);
     glamor_priv->suppress_gl_out_of_memory_logging = true;
-    glTexImage2D(GL_TEXTURE_2D, 0, iformat, w, h, 0,
-                 format, GL_UNSIGNED_BYTE, NULL);
+    glTexImage2D(GL_TEXTURE_2D, 0, f->internalformat, w, h, 0,
+                 f->format, f->type, NULL);
     glamor_priv->suppress_gl_out_of_memory_logging = false;
 
     if (glGetError() == GL_OUT_OF_MEMORY) {
diff --git a/glamor/glamor_picture.c b/glamor/glamor_picture.c
index ed2decc83..33b3bebd9 100644
--- a/glamor/glamor_picture.c
+++ b/glamor/glamor_picture.c
@@ -83,7 +83,7 @@ glamor_get_tex_format_type_from_pictformat(ScreenPtr pScreen,
 
     switch (format) {
     case PICT_a1:
-        *tex_format = glamor_priv->one_channel_format;
+        *tex_format = glamor_priv->formats[1].format;
         *tex_type = GL_UNSIGNED_BYTE;
         *temp_format = PICT_a8;
         break;
@@ -195,7 +195,7 @@ glamor_get_tex_format_type_from_pictformat(ScreenPtr pScreen,
         break;
 
     case PICT_a8:
-        *tex_format = glamor_priv->one_channel_format;
+        *tex_format = glamor_priv->formats[8].format;
         *tex_type = GL_UNSIGNED_BYTE;
         break;
 
@@ -286,6 +286,7 @@ glamor_upload_picture_to_texture(PicturePtr picture)
     Bool ret = TRUE;
     Bool needs_swizzle;
     pixman_image_t *converted_image = NULL;
+    const struct glamor_format *f = glamor_format_for_pixmap(pixmap);
 
     assert(glamor_pixmap_is_memory(pixmap));
     assert(!pixmap_priv->fbo);
@@ -336,7 +337,7 @@ glamor_upload_picture_to_texture(PicturePtr picture)
     }
 
     if (!glamor_priv->is_gles)
-        iformat = gl_iformat_for_pixmap(pixmap);
+        iformat = f->internalformat;
     else
         iformat = format;
 
diff --git a/glamor/glamor_priv.h b/glamor/glamor_priv.h
index 8e8433ff3..b8e2b932e 100644
--- a/glamor/glamor_priv.h
+++ b/glamor/glamor_priv.h
@@ -157,6 +157,21 @@ struct glamor_pixmap_private;
 
 #define GLAMOR_COMPOSITE_VBO_VERT_CNT (64*1024)
 
+struct glamor_format {
+    /** X Server's "depth" value */
+    int depth;
+    /** GL internalformat for creating textures of this type */
+    GLenum internalformat;
+    /** GL format transferring pixels in/out of textures of this type. */
+    GLenum format;
+    /** GL type transferring pixels in/out of textures of this type. */
+    GLenum type;
+    /* Render PICT_* matching GL's channel layout for pixels
+     * transferred using format/type.
+     */
+    CARD32 render_format;
+};
+
 struct glamor_saved_procs {
     CloseScreenProcPtr close_screen;
     CreateGCProcPtr create_gc;
@@ -199,7 +214,8 @@ typedef struct glamor_screen_private {
     Bool can_copyplane;
     int max_fbo_size;
 
-    GLuint one_channel_format;
+    struct glamor_format formats[33];
+    struct glamor_format cbcr_format;
 
     /* glamor point shader */
     glamor_program point_prog;
@@ -537,6 +553,8 @@ void glamor_pixmap_destroy_fbo(PixmapPtr pixmap);
 Bool glamor_pixmap_fbo_fixup(ScreenPtr screen, PixmapPtr pixmap);
 void glamor_pixmap_clear_fbo(glamor_screen_private *glamor_priv, glamor_pixmap_fbo *fbo);
 
+const struct glamor_format *glamor_format_for_pixmap(PixmapPtr pixmap);
+
 /* Return whether 'picture' is alpha-only */
 static inline Bool glamor_picture_is_alpha(PicturePtr picture)
 {
@@ -549,7 +567,7 @@ glamor_picture_red_is_alpha(PicturePtr picture)
 {
     /* True when the picture is alpha only and the screen is using GL_RED for alpha pictures */
     return glamor_picture_is_alpha(picture) &&
-        glamor_get_screen_private(picture->pDrawable->pScreen)->one_channel_format == GL_RED;
+        glamor_get_screen_private(picture->pDrawable->pScreen)->formats[8].format == GL_RED;
 }
 
 void glamor_bind_texture(glamor_screen_private *glamor_priv,
diff --git a/glamor/glamor_render.c b/glamor/glamor_render.c
index d3859e4d1..a8dc3924b 100644
--- a/glamor/glamor_render.c
+++ b/glamor/glamor_render.c
@@ -772,12 +772,15 @@ static Bool
 glamor_render_format_is_supported(PicturePtr picture)
 {
     PictFormatShort storage_format;
+    glamor_screen_private *glamor_priv;
 
     /* Source-only pictures should always work */
     if (!picture->pDrawable)
         return TRUE;
 
-    storage_format = format_for_depth(picture->pDrawable->depth);
+    glamor_priv = glamor_get_screen_private(picture->pDrawable->pScreen);
+    storage_format =
+        glamor_priv->formats[picture->pDrawable->depth].render_format;
 
     switch (picture->format) {
     case PICT_x2r10g10b10:
@@ -898,7 +901,7 @@ glamor_composite_choose_shader(CARD8 op,
     }
 
     if (dest_pixmap->drawable.bitsPerPixel <= 8 &&
-        glamor_priv->one_channel_format == GL_RED) {
+        glamor_priv->formats[8].format == GL_RED) {
         key.dest_swizzle = SHADER_DEST_SWIZZLE_ALPHA_TO_RED;
     } else {
         key.dest_swizzle = SHADER_DEST_SWIZZLE_DEFAULT;
diff --git a/glamor/glamor_spans.c b/glamor/glamor_spans.c
index b3c028d67..b5f297d2f 100644
--- a/glamor/glamor_spans.c
+++ b/glamor/glamor_spans.c
@@ -187,9 +187,8 @@ glamor_get_spans_gl(DrawablePtr drawable, int wmax,
     int box_index;
     int n;
     char *d;
-    GLenum type;
-    GLenum format;
     int off_x, off_y;
+    const struct glamor_format *f = glamor_format_for_pixmap(pixmap);
 
     pixmap_priv = glamor_get_pixmap_private(pixmap);
     if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(pixmap_priv))
@@ -197,8 +196,6 @@ glamor_get_spans_gl(DrawablePtr drawable, int wmax,
 
     glamor_get_drawable_deltas(drawable, pixmap, &off_x, &off_y);
 
-    glamor_format_for_pixmap(pixmap, &format, &type);
-
     glamor_make_current(glamor_priv);
 
     glamor_pixmap_loop(pixmap_priv, box_index) {
@@ -234,7 +231,8 @@ glamor_get_spans_gl(DrawablePtr drawable, int wmax,
             if (y >= box->y2)
                 continue;
 
-            glReadPixels(x1 - box->x1, y - box->y1, x2 - x1, 1, format, type, l);
+            glReadPixels(x1 - box->x1, y - box->y1, x2 - x1, 1,
+                         f->format, f->type, l);
         }
     }
 
@@ -269,11 +267,10 @@ glamor_set_spans_gl(DrawablePtr drawable, GCPtr gc, char *src,
     glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
     PixmapPtr pixmap = glamor_get_drawable_pixmap(drawable);
     glamor_pixmap_private *pixmap_priv;
+    const struct glamor_format *f = glamor_format_for_pixmap(pixmap);
     int box_index;
     int n;
     char *s;
-    GLenum type;
-    GLenum format;
     int off_x, off_y;
 
     pixmap_priv = glamor_get_pixmap_private(pixmap);
@@ -287,7 +284,6 @@ glamor_set_spans_gl(DrawablePtr drawable, GCPtr gc, char *src,
         goto bail;
 
     glamor_get_drawable_deltas(drawable, pixmap, &off_x, &off_y);
-    glamor_format_for_pixmap(pixmap, &format, &type);
 
     glamor_make_current(glamor_priv);
 
@@ -348,7 +344,7 @@ glamor_set_spans_gl(DrawablePtr drawable, GCPtr gc, char *src,
 
                 glTexSubImage2D(GL_TEXTURE_2D, 0,
                                 x1 - box->x1, y1 - box->y1, x2 - x1, 1,
-                                format, type,
+                                f->format, f->type,
                                 l);
             }
             s += PixmapBytePad(w, drawable->depth);
diff --git a/glamor/glamor_transfer.c b/glamor/glamor_transfer.c
index 215752d7b..e706e0fb4 100644
--- a/glamor/glamor_transfer.c
+++ b/glamor/glamor_transfer.c
@@ -23,44 +23,6 @@
 #include "glamor_priv.h"
 #include "glamor_transfer.h"
 
-/* XXX a kludge for now */
-void
-glamor_format_for_pixmap(PixmapPtr pixmap, GLenum *format, GLenum *type)
-{
-    glamor_pixmap_private       *priv = glamor_get_pixmap_private(pixmap);
-    switch (pixmap->drawable.depth) {
-    case 24:
-    case 32:
-        *format = GL_BGRA;
-        *type = GL_UNSIGNED_INT_8_8_8_8_REV;
-        break;
-    case 30:
-        *format = GL_BGRA;
-        *type = GL_UNSIGNED_INT_2_10_10_10_REV;
-        break;
-    case 16:
-        if (priv->is_cbcr) {
-          *format = GL_RG;
-          *type = GL_UNSIGNED_BYTE;
-        } else {
-          *format = GL_RGB;
-          *type = GL_UNSIGNED_SHORT_5_6_5;
-        }
-        break;
-    case 15:
-        *format = GL_BGRA;
-        *type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
-        break;
-    case 8:
-        *format = glamor_get_screen_private(pixmap->drawable.pScreen)->one_channel_format;
-        *type = GL_UNSIGNED_BYTE;
-        break;
-    default:
-        FatalError("Invalid pixmap depth %d\n", pixmap->drawable.depth);
-        break;
-    }
-}
-
 /*
  * Write a region of bits into a pixmap
  */
@@ -75,10 +37,7 @@ glamor_upload_boxes(PixmapPtr pixmap, BoxPtr in_boxes, int in_nbox,
     glamor_pixmap_private       *priv = glamor_get_pixmap_private(pixmap);
     int                         box_index;
     int                         bytes_per_pixel = pixmap->drawable.bitsPerPixel >> 3;
-    GLenum                      type;
-    GLenum                      format;
-
-    glamor_format_for_pixmap(pixmap, &format, &type);
+    const struct glamor_format *f = glamor_format_for_pixmap(pixmap);
 
     glamor_make_current(glamor_priv);
 
@@ -116,14 +75,14 @@ glamor_upload_boxes(PixmapPtr pixmap, BoxPtr in_boxes, int in_nbox,
                 glTexSubImage2D(GL_TEXTURE_2D, 0,
                                 x1 - box->x1, y1 - box->y1,
                                 x2 - x1, y2 - y1,
-                                format, type,
+                                f->format, f->type,
                                 bits + ofs);
             } else {
                 for (; y1 < y2; y1++, ofs += byte_stride)
                     glTexSubImage2D(GL_TEXTURE_2D, 0,
                                     x1 - box->x1, y1 - box->y1,
                                     x2 - x1, 1,
-                                    format, type,
+                                    f->format, f->type,
                                     bits + ofs);
             }
         }
@@ -178,10 +137,7 @@ glamor_download_boxes(PixmapPtr pixmap, BoxPtr in_boxes, int in_nbox,
     glamor_pixmap_private *priv = glamor_get_pixmap_private(pixmap);
     int box_index;
     int bytes_per_pixel = pixmap->drawable.bitsPerPixel >> 3;
-    GLenum type;
-    GLenum format;
-
-    glamor_format_for_pixmap(pixmap, &format, &type);
+    const struct glamor_format *f = glamor_format_for_pixmap(pixmap);
 
     glamor_make_current(glamor_priv);
 
@@ -216,10 +172,10 @@ glamor_download_boxes(PixmapPtr pixmap, BoxPtr in_boxes, int in_nbox,
 
             if (glamor_priv->has_pack_subimage ||
                 x2 - x1 == byte_stride / bytes_per_pixel) {
-                glReadPixels(x1 - box->x1, y1 - box->y1, x2 - x1, y2 - y1, format, type, bits + ofs);
+                glReadPixels(x1 - box->x1, y1 - box->y1, x2 - x1, y2 - y1, f->format, f->type, bits + ofs);
             } else {
                 for (; y1 < y2; y1++, ofs += byte_stride)
-                    glReadPixels(x1 - box->x1, y1 - box->y1, x2 - x1, 1, format, type, bits + ofs);
+                    glReadPixels(x1 - box->x1, y1 - box->y1, x2 - x1, 1, f->format, f->type, bits + ofs);
             }
         }
     }
diff --git a/glamor/glamor_transfer.h b/glamor/glamor_transfer.h
index de8186a70..a6137b3ff 100644
--- a/glamor/glamor_transfer.h
+++ b/glamor/glamor_transfer.h
@@ -23,9 +23,6 @@
 #ifndef _GLAMOR_TRANSFER_H_
 #define _GLAMOR_TRANSFER_H_
 
-void
-glamor_format_for_pixmap(PixmapPtr pixmap, GLenum *format, GLenum *type);
-
 void
 glamor_upload_boxes(PixmapPtr pixmap, BoxPtr in_boxes, int in_nbox,
                     int dx_src, int dy_src,
diff --git a/glamor/glamor_transform.c b/glamor/glamor_transform.c
index 2d5a634a8..348d00be1 100644
--- a/glamor/glamor_transform.c
+++ b/glamor/glamor_transform.c
@@ -121,10 +121,9 @@ glamor_set_color_depth(ScreenPtr      pScreen,
 
     glamor_get_rgba_from_pixel(pixel,
                                &color[0], &color[1], &color[2], &color[3],
-                               format_for_depth(depth));
+                               glamor_priv->formats[depth].render_format);
 
-    if ((depth == 1 || depth == 8) &&
-        glamor_priv->one_channel_format == GL_RED)
+    if ((depth <= 8) && glamor_priv->formats[8].format == GL_RED)
       color[0] = color[3];
 
     glUniform4fv(uniform, 1, color);
diff --git a/glamor/glamor_utils.h b/glamor/glamor_utils.h
index cbb808294..651faf2fe 100644
--- a/glamor/glamor_utils.h
+++ b/glamor/glamor_utils.h
@@ -570,65 +570,8 @@
                                                     && (_w_) <= _glamor_->max_fbo_size  \
                                                     && (_h_) <= _glamor_->max_fbo_size)
 
-/* For 1bpp pixmap, we don't store it as texture. */
-#define glamor_check_pixmap_fbo_depth(_depth_) (			\
-						_depth_ == 8		\
-						|| _depth_ == 15	\
-						|| _depth_ == 16	\
-						|| _depth_ == 24	\
-						|| _depth_ == 30	\
-						|| _depth_ == 32)
-
 #define GLAMOR_PIXMAP_PRIV_HAS_FBO(pixmap_priv)    (pixmap_priv->gl_fbo == GLAMOR_FBO_NORMAL)
 
-/**
- * Borrow from uxa.
- */
-static inline CARD32
-format_for_depth(int depth)
-{
-    switch (depth) {
-    case 1:
-        return PICT_a1;
-    case 4:
-        return PICT_a4;
-    case 8:
-        return PICT_a8;
-    case 15:
-        return PICT_x1r5g5b5;
-    case 16:
-        return PICT_r5g6b5;
-    default:
-    case 24:
-        return PICT_x8r8g8b8;
-    case 30:
-        return PICT_x2r10g10b10;
-    case 32:
-        return PICT_a8r8g8b8;
-    }
-}
-
-static inline GLenum
-gl_iformat_for_pixmap(PixmapPtr pixmap)
-{
-    glamor_screen_private *glamor_priv =
-        glamor_get_screen_private((pixmap)->drawable.pScreen);
-    glamor_pixmap_private *pixmap_priv = glamor_get_pixmap_private(pixmap);
-
-    if (!glamor_priv->is_gles &&
-        ((pixmap)->drawable.depth == 1 || (pixmap)->drawable.depth == 8)) {
-        return glamor_priv->one_channel_format;
-    } else if (!glamor_priv->is_gles &&
-               (pixmap)->drawable.depth == 16 && pixmap_priv->is_cbcr) {
-        return GL_RG;
-    } else if (!glamor_priv->is_gles &&
-               (pixmap)->drawable.depth == 30) {
-        return GL_RGB10_A2;
-    } else {
-        return GL_RGBA;
-    }
-}
-
 #define REVERT_NONE       		0
 #define REVERT_NORMAL     		1
 #define REVERT_UPLOADING_A1		3
-- 
2.17.1