xref: /aosp_15_r20/external/mesa3d/src/mesa/state_tracker/st_cb_bitmap.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /**************************************************************************
2  *
3  * Copyright 2007 VMware, Inc.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21  * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27 
28  /*
29   * Authors:
30   *   Brian Paul
31   */
32 
33 #include "main/errors.h"
34 
35 #include "main/image.h"
36 #include "main/bufferobj.h"
37 #include "main/framebuffer.h"
38 #include "main/macros.h"
39 #include "main/pbo.h"
40 #include "program/program.h"
41 #include "program/prog_print.h"
42 
43 #include "st_context.h"
44 #include "st_atom.h"
45 #include "st_atom_constbuf.h"
46 #include "st_draw.h"
47 #include "st_program.h"
48 #include "st_cb_bitmap.h"
49 #include "st_cb_drawpixels.h"
50 #include "st_sampler_view.h"
51 #include "st_texture.h"
52 #include "st_util.h"
53 
54 #include "pipe/p_context.h"
55 #include "pipe/p_defines.h"
56 #include "pipe/p_shader_tokens.h"
57 #include "util/u_inlines.h"
58 #include "program/prog_instruction.h"
59 #include "cso_cache/cso_context.h"
60 
61 
62 /**
63  * glBitmaps are drawn as textured quads.  The user's bitmap pattern
64  * is stored in a texture image.  An alpha8 texture format is used.
65  * The fragment shader samples a bit (texel) from the texture, then
66  * discards the fragment if the bit is off.
67  *
68  * Note that we actually store the inverse image of the bitmap to
69  * simplify the fragment program.  An "on" bit gets stored as texel=0x0
70  * and an "off" bit is stored as texel=0xff.  Then we kill the
71  * fragment if the negated texel value is less than zero.
72  */
73 
74 
75 /**
76  * The bitmap cache attempts to accumulate multiple glBitmap calls in a
77  * buffer which is then rendered en mass upon a flush, state change, etc.
78  * A wide, short buffer is used to target the common case of a series
79  * of glBitmap calls being used to draw text.
80  */
81 static GLboolean UseBitmapCache = GL_TRUE;
82 
83 
84 #define BITMAP_CACHE_WIDTH  512
85 #define BITMAP_CACHE_HEIGHT 32
86 
87 
88 /** Epsilon for Z comparisons */
89 #define Z_EPSILON 1e-06
90 
91 static void
92 init_bitmap_state(struct st_context *st);
93 
94 /**
95  * Copy user-provide bitmap bits into texture buffer, expanding
96  * bits into texels.
97  * "On" bits will set texels to 0x0.
98  * "Off" bits will not modify texels.
99  * Note that the image is actually going to be upside down in
100  * the texture.  We deal with that with texcoords.
101  */
102 static void
unpack_bitmap(struct st_context * st,GLint px,GLint py,GLsizei width,GLsizei height,const struct gl_pixelstore_attrib * unpack,const GLubyte * bitmap,uint8_t * destBuffer,uint destStride)103 unpack_bitmap(struct st_context *st,
104               GLint px, GLint py, GLsizei width, GLsizei height,
105               const struct gl_pixelstore_attrib *unpack,
106               const GLubyte *bitmap,
107               uint8_t *destBuffer, uint destStride)
108 {
109    destBuffer += py * destStride + px;
110 
111    _mesa_expand_bitmap(width, height, unpack, bitmap,
112                        destBuffer, destStride, 0x0);
113 }
114 
115 
116 /**
117  * Create a texture which represents a bitmap image.
118  */
119 struct pipe_resource *
st_make_bitmap_texture(struct gl_context * ctx,GLsizei width,GLsizei height,const struct gl_pixelstore_attrib * unpack,const GLubyte * bitmap)120 st_make_bitmap_texture(struct gl_context *ctx, GLsizei width, GLsizei height,
121                        const struct gl_pixelstore_attrib *unpack,
122                        const GLubyte *bitmap)
123 {
124    struct st_context *st = st_context(ctx);
125    struct pipe_context *pipe = st->pipe;
126    struct pipe_transfer *transfer;
127    uint8_t *dest;
128    struct pipe_resource *pt;
129 
130    if (!st->bitmap.tex_format)
131       init_bitmap_state(st);
132 
133    /* PBO source... */
134    bitmap = _mesa_map_pbo_source(ctx, unpack, bitmap);
135    if (!bitmap) {
136       return NULL;
137    }
138 
139    /**
140     * Create texture to hold bitmap pattern.
141     */
142    pt = st_texture_create(st, st->internal_target, st->bitmap.tex_format,
143                           0, width, height, 1, 1, 0,
144                           PIPE_BIND_SAMPLER_VIEW, false,
145                           PIPE_COMPRESSION_FIXED_RATE_NONE);
146    if (!pt) {
147       _mesa_unmap_pbo_source(ctx, unpack);
148       return NULL;
149    }
150 
151    dest = pipe_texture_map(st->pipe, pt, 0, 0,
152                             PIPE_MAP_WRITE,
153                             0, 0, width, height, &transfer);
154 
155    /* Put image into texture transfer */
156    memset(dest, 0xff, height * transfer->stride);
157    unpack_bitmap(st, 0, 0, width, height, unpack, bitmap,
158                  dest, transfer->stride);
159 
160    _mesa_unmap_pbo_source(ctx, unpack);
161 
162    /* Release transfer */
163    pipe_texture_unmap(pipe, transfer);
164    return pt;
165 }
166 
167 
168 /**
169  * Setup pipeline state prior to rendering the bitmap textured quad.
170  */
171 static void
setup_render_state(struct gl_context * ctx,struct pipe_sampler_view * sv,const GLfloat * color,struct gl_program * fp,bool scissor_enabled,bool clamp_frag_color)172 setup_render_state(struct gl_context *ctx,
173                    struct pipe_sampler_view *sv,
174                    const GLfloat *color, struct gl_program *fp,
175                    bool scissor_enabled, bool clamp_frag_color)
176 {
177    struct st_context *st = st_context(ctx);
178    struct pipe_context *pipe = st->pipe;
179    struct cso_context *cso = st->cso_context;
180    struct st_fp_variant *fpv;
181    struct st_fp_variant_key key;
182 
183    memset(&key, 0, sizeof(key));
184    key.st = st->has_shareable_shaders ? NULL : st;
185    key.bitmap = GL_TRUE;
186    key.clamp_color = st->clamp_frag_color_in_shader &&
187                      clamp_frag_color;
188    key.lower_alpha_func = COMPARE_FUNC_ALWAYS;
189 
190    fpv = st_get_fp_variant(st, fp, &key);
191 
192    /* As an optimization, Mesa's fragment programs will sometimes get the
193     * primary color from a statevar/constant rather than a varying variable.
194     * when that's the case, we need to ensure that we use the 'color'
195     * parameter and not the current attribute color (which may have changed
196     * through glRasterPos and state validation.
197     * So, we force the proper color here.  Not elegant, but it works.
198     */
199    {
200       GLfloat colorSave[4];
201       COPY_4V(colorSave, ctx->Current.Attrib[VERT_ATTRIB_COLOR0]);
202       COPY_4V(ctx->Current.Attrib[VERT_ATTRIB_COLOR0], color);
203       st_upload_constants(st, fp, MESA_SHADER_FRAGMENT);
204       COPY_4V(ctx->Current.Attrib[VERT_ATTRIB_COLOR0], colorSave);
205    }
206 
207    cso_save_state(cso, (CSO_BIT_RASTERIZER |
208                         CSO_BIT_FRAGMENT_SAMPLERS |
209                         CSO_BIT_VIEWPORT |
210                         CSO_BIT_STREAM_OUTPUTS |
211                         CSO_BIT_VERTEX_ELEMENTS |
212                         CSO_BITS_ALL_SHADERS));
213 
214 
215    /* rasterizer state: just scissor */
216    st->bitmap.rasterizer.scissor = scissor_enabled;
217    cso_set_rasterizer(cso, &st->bitmap.rasterizer);
218 
219    /* fragment shader state: TEX lookup program */
220    cso_set_fragment_shader_handle(cso, fpv->base.driver_shader);
221 
222    /* vertex shader state: position + texcoord pass-through */
223    cso_set_vertex_shader_handle(cso, st->passthrough_vs);
224 
225    /* disable other shaders */
226    cso_set_tessctrl_shader_handle(cso, NULL);
227    cso_set_tesseval_shader_handle(cso, NULL);
228    cso_set_geometry_shader_handle(cso, NULL);
229 
230    /* user samplers, plus our bitmap sampler */
231    {
232       struct pipe_sampler_state *samplers[PIPE_MAX_SAMPLERS];
233       uint num = MAX2(fpv->bitmap_sampler + 1,
234                       st->state.num_frag_samplers);
235       uint i;
236       for (i = 0; i < st->state.num_frag_samplers; i++) {
237          samplers[i] = &st->state.frag_samplers[i];
238       }
239       samplers[fpv->bitmap_sampler] = &st->bitmap.sampler;
240       cso_set_samplers(cso, PIPE_SHADER_FRAGMENT, num,
241                        (const struct pipe_sampler_state **) samplers);
242    }
243 
244    /* user textures, plus the bitmap texture */
245    {
246       struct pipe_sampler_view *sampler_views[PIPE_MAX_SAMPLERS];
247       unsigned num_views =
248          st_get_sampler_views(st, PIPE_SHADER_FRAGMENT, fp, sampler_views);
249 
250       num_views = MAX2(fpv->bitmap_sampler + 1, num_views);
251       sampler_views[fpv->bitmap_sampler] = sv;
252       pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, num_views, 0,
253                               true, sampler_views);
254       st->state.num_sampler_views[PIPE_SHADER_FRAGMENT] = num_views;
255    }
256 
257    /* viewport state: viewport matching window dims */
258    cso_set_viewport_dims(cso, st->state.fb_width,
259                          st->state.fb_height,
260                          st->state.fb_orientation == Y_0_TOP);
261 
262    st->util_velems.count = 3;
263    cso_set_vertex_elements(cso, &st->util_velems);
264 
265    cso_set_stream_outputs(st->cso_context, 0, NULL, NULL);
266 }
267 
268 
269 /**
270  * Restore pipeline state after rendering the bitmap textured quad.
271  */
272 static void
restore_render_state(struct gl_context * ctx)273 restore_render_state(struct gl_context *ctx)
274 {
275    struct st_context *st = st_context(ctx);
276    struct cso_context *cso = st->cso_context;
277 
278    /* Unbind all because st/mesa won't do it if the current shader doesn't
279     * use them.
280     */
281    cso_restore_state(cso, CSO_UNBIND_FS_SAMPLERVIEWS);
282    st->state.num_sampler_views[PIPE_SHADER_FRAGMENT] = 0;
283 
284    ctx->Array.NewVertexElements = true;
285    ctx->NewDriverState |= ST_NEW_VERTEX_ARRAYS |
286                           ST_NEW_FS_SAMPLER_VIEWS;
287 }
288 
289 
290 /**
291  * Render a glBitmap by drawing a textured quad
292  *
293  * take_ownership means the callee will be resposible for unreferencing sv.
294  */
295 static void
draw_bitmap_quad(struct gl_context * ctx,GLint x,GLint y,GLfloat z,GLsizei width,GLsizei height,struct pipe_sampler_view * sv,const GLfloat * color,struct gl_program * fp,bool scissor_enabled,bool clamp_frag_color)296 draw_bitmap_quad(struct gl_context *ctx, GLint x, GLint y, GLfloat z,
297                  GLsizei width, GLsizei height,
298                  struct pipe_sampler_view *sv, const GLfloat *color,
299                  struct gl_program *fp, bool scissor_enabled,
300                  bool clamp_frag_color)
301 {
302    struct st_context *st = st_context(ctx);
303    const float fb_width = (float) st->state.fb_width;
304    const float fb_height = (float) st->state.fb_height;
305    const float x0 = (float) x;
306    const float x1 = (float) (x + width);
307    const float y0 = (float) y;
308    const float y1 = (float) (y + height);
309    float sLeft = 0.0f, sRight = 1.0f;
310    float tTop = 0.0f, tBot = 1.0f - tTop;
311    const float clip_x0 = x0 / fb_width * 2.0f - 1.0f;
312    const float clip_y0 = y0 / fb_height * 2.0f - 1.0f;
313    const float clip_x1 = x1 / fb_width * 2.0f - 1.0f;
314    const float clip_y1 = y1 / fb_height * 2.0f - 1.0f;
315 
316    /* limit checks */
317    {
318       /* XXX if the bitmap is larger than the max texture size, break
319        * it up into chunks.
320        */
321       ASSERTED GLuint maxSize =
322          st->screen->get_param(st->screen, PIPE_CAP_MAX_TEXTURE_2D_SIZE);
323       assert(width <= (GLsizei) maxSize);
324       assert(height <= (GLsizei) maxSize);
325    }
326 
327    if (sv->texture->target == PIPE_TEXTURE_RECT) {
328       /* use non-normalized texcoords */
329       sRight = (float) width;
330       tBot = (float) height;
331    }
332 
333    setup_render_state(ctx, sv, color, fp, scissor_enabled, clamp_frag_color);
334 
335    /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
336    z = z * 2.0f - 1.0f;
337 
338    if (!st_draw_quad(st, clip_x0, clip_y0, clip_x1, clip_y1, z,
339                      sLeft, tBot, sRight, tTop, color, 0)) {
340       _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBitmap");
341    }
342 
343    restore_render_state(ctx);
344 
345    /* We uploaded modified constants, need to invalidate them. */
346    ctx->NewDriverState |= ST_NEW_FS_CONSTANTS;
347 }
348 
349 
350 static void
reset_cache(struct st_context * st)351 reset_cache(struct st_context *st)
352 {
353    struct st_bitmap_cache *cache = &st->bitmap.cache;
354 
355    /*memset(cache->buffer, 0xff, sizeof(cache->buffer));*/
356    cache->empty = GL_TRUE;
357 
358    cache->xmin = 1000000;
359    cache->xmax = -1000000;
360    cache->ymin = 1000000;
361    cache->ymax = -1000000;
362 
363    _mesa_reference_program(st->ctx, &cache->fp, NULL);
364 
365    assert(!cache->texture);
366 
367    /* allocate a new texture */
368    cache->texture = st_texture_create(st, st->internal_target,
369                                       st->bitmap.tex_format, 0,
370                                       BITMAP_CACHE_WIDTH, BITMAP_CACHE_HEIGHT,
371                                       1, 1, 0,
372                                       PIPE_BIND_SAMPLER_VIEW,
373                                       false,
374                                       PIPE_COMPRESSION_FIXED_RATE_NONE);
375 }
376 
377 
378 /** Print bitmap image to stdout (debug) */
379 static void
print_cache(const struct st_bitmap_cache * cache)380 print_cache(const struct st_bitmap_cache *cache)
381 {
382    int i, j, k;
383 
384    for (i = 0; i < BITMAP_CACHE_HEIGHT; i++) {
385       k = BITMAP_CACHE_WIDTH * (BITMAP_CACHE_HEIGHT - i - 1);
386       for (j = 0; j < BITMAP_CACHE_WIDTH; j++) {
387          if (cache->buffer[k])
388             printf("X");
389          else
390             printf(" ");
391          k++;
392       }
393       printf("\n");
394    }
395 }
396 
397 
398 /**
399  * Create gallium pipe_transfer object for the bitmap cache.
400  */
401 static void
create_cache_trans(struct st_context * st)402 create_cache_trans(struct st_context *st)
403 {
404    struct pipe_context *pipe = st->pipe;
405    struct st_bitmap_cache *cache = &st->bitmap.cache;
406 
407    if (cache->trans)
408       return;
409 
410    /* Map the texture transfer.
411     * Subsequent glBitmap calls will write into the texture image.
412     */
413    cache->buffer = pipe_texture_map(pipe, cache->texture, 0, 0,
414                                      PIPE_MAP_WRITE, 0, 0,
415                                      BITMAP_CACHE_WIDTH,
416                                      BITMAP_CACHE_HEIGHT, &cache->trans);
417 
418    /* init image to all 0xff */
419    memset(cache->buffer, 0xff, cache->trans->stride * BITMAP_CACHE_HEIGHT);
420 }
421 
422 
423 /**
424  * If there's anything in the bitmap cache, draw/flush it now.
425  */
426 void
st_flush_bitmap_cache(struct st_context * st)427 st_flush_bitmap_cache(struct st_context *st)
428 {
429    struct st_bitmap_cache *cache = &st->bitmap.cache;
430 
431    if (!cache->empty) {
432       struct pipe_context *pipe = st->pipe;
433       struct pipe_sampler_view *sv;
434 
435       assert(cache->xmin <= cache->xmax);
436 
437       if (0)
438          printf("flush bitmap, size %d x %d  at %d, %d\n",
439                 cache->xmax - cache->xmin,
440                 cache->ymax - cache->ymin,
441                 cache->xpos, cache->ypos);
442 
443       /* The texture transfer has been mapped until now.
444        * So unmap and release the texture transfer before drawing.
445        */
446       if (cache->trans && cache->buffer) {
447          if (0)
448             print_cache(cache);
449          pipe_texture_unmap(pipe, cache->trans);
450          cache->buffer = NULL;
451          cache->trans = NULL;
452       }
453 
454       sv = st_create_texture_sampler_view(st->pipe, cache->texture);
455       if (sv) {
456          draw_bitmap_quad(st->ctx,
457                           cache->xpos,
458                           cache->ypos,
459                           cache->zpos,
460                           BITMAP_CACHE_WIDTH, BITMAP_CACHE_HEIGHT,
461                           sv,
462                           cache->color,
463                           cache->fp,
464                           cache->scissor_enabled,
465                           cache->clamp_frag_color);
466       }
467 
468       /* release/free the texture */
469       pipe_resource_reference(&cache->texture, NULL);
470 
471       reset_cache(st);
472    }
473 }
474 
475 
476 /**
477  * Try to accumulate this glBitmap call in the bitmap cache.
478  * \return  GL_TRUE for success, GL_FALSE if bitmap is too large, etc.
479  */
480 static GLboolean
accum_bitmap(struct gl_context * ctx,GLint x,GLint y,GLsizei width,GLsizei height,const struct gl_pixelstore_attrib * unpack,const GLubyte * bitmap)481 accum_bitmap(struct gl_context *ctx,
482              GLint x, GLint y, GLsizei width, GLsizei height,
483              const struct gl_pixelstore_attrib *unpack,
484              const GLubyte *bitmap )
485 {
486    struct st_context *st = ctx->st;
487    struct st_bitmap_cache *cache = &st->bitmap.cache;
488    int px = -999, py = -999;
489    const GLfloat z = ctx->Current.RasterPos[2];
490 
491    if (width > BITMAP_CACHE_WIDTH ||
492        height > BITMAP_CACHE_HEIGHT)
493       return GL_FALSE; /* too big to cache */
494 
495    bool scissor_enabled = ctx->Scissor.EnableFlags & 0x1;
496    bool clamp_frag_color = ctx->Color._ClampFragmentColor;
497 
498    if (!cache->empty) {
499       px = x - cache->xpos;  /* pos in buffer */
500       py = y - cache->ypos;
501       if (px < 0 || px + width > BITMAP_CACHE_WIDTH ||
502           py < 0 || py + height > BITMAP_CACHE_HEIGHT ||
503           !TEST_EQ_4V(ctx->Current.RasterColor, cache->color) ||
504           ctx->FragmentProgram._Current != cache->fp ||
505           scissor_enabled != cache->scissor_enabled ||
506           clamp_frag_color != cache->clamp_frag_color ||
507           ((fabsf(z - cache->zpos) > Z_EPSILON))) {
508          /* This bitmap would extend beyond cache bounds, or the bitmap
509           * color is changing
510           * so flush and continue.
511           */
512          st_flush_bitmap_cache(st);
513       }
514    }
515 
516    if (cache->empty) {
517       /* Initialize.  Center bitmap vertically in the buffer. */
518       px = 0;
519       py = (BITMAP_CACHE_HEIGHT - height) / 2;
520       cache->xpos = x;
521       cache->ypos = y - py;
522       cache->zpos = z;
523       cache->empty = GL_FALSE;
524       COPY_4FV(cache->color, ctx->Current.RasterColor);
525       _mesa_reference_program(ctx, &cache->fp, ctx->FragmentProgram._Current);
526       cache->scissor_enabled = scissor_enabled;
527       cache->clamp_frag_color = clamp_frag_color;
528    }
529 
530    assert(px != -999);
531    assert(py != -999);
532 
533    if (x < cache->xmin)
534       cache->xmin = x;
535    if (y < cache->ymin)
536       cache->ymin = y;
537    if (x + width > cache->xmax)
538       cache->xmax = x + width;
539    if (y + height > cache->ymax)
540       cache->ymax = y + height;
541 
542    /* create the transfer if needed */
543    create_cache_trans(st);
544 
545    /* PBO source... */
546    bitmap = _mesa_map_pbo_source(ctx, unpack, bitmap);
547    if (!bitmap) {
548       return false;
549    }
550 
551    unpack_bitmap(st, px, py, width, height, unpack, bitmap,
552                  cache->buffer, BITMAP_CACHE_WIDTH);
553 
554    _mesa_unmap_pbo_source(ctx, unpack);
555 
556    return GL_TRUE; /* accumulated */
557 }
558 
559 
560 /**
561  * One-time init for drawing bitmaps.
562  */
563 static void
init_bitmap_state(struct st_context * st)564 init_bitmap_state(struct st_context *st)
565 {
566    struct pipe_screen *screen = st->screen;
567 
568    /* This function should only be called once */
569    assert(!st->bitmap.tex_format);
570 
571    assert(st->internal_target == PIPE_TEXTURE_2D ||
572           st->internal_target == PIPE_TEXTURE_RECT);
573 
574    /* init sampler state once */
575    memset(&st->bitmap.sampler, 0, sizeof(st->bitmap.sampler));
576    st->bitmap.sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
577    st->bitmap.sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
578    st->bitmap.sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
579    st->bitmap.sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
580    st->bitmap.sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
581    st->bitmap.sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
582    st->bitmap.sampler.unnormalized_coords = !(st->internal_target == PIPE_TEXTURE_2D ||
583                                               (st->internal_target == PIPE_TEXTURE_RECT &&
584                                                st->lower_rect_tex));
585 
586    /* init baseline rasterizer state once */
587    memset(&st->bitmap.rasterizer, 0, sizeof(st->bitmap.rasterizer));
588    st->bitmap.rasterizer.half_pixel_center = 1;
589    st->bitmap.rasterizer.bottom_edge_rule = 1;
590    st->bitmap.rasterizer.depth_clip_near = 1;
591    st->bitmap.rasterizer.depth_clip_far = 1;
592 
593    /* find a usable texture format */
594    if (screen->is_format_supported(screen, PIPE_FORMAT_R8_UNORM,
595                                    st->internal_target, 0, 0,
596                                    PIPE_BIND_SAMPLER_VIEW)) {
597       st->bitmap.tex_format = PIPE_FORMAT_R8_UNORM;
598    }
599    else if (screen->is_format_supported(screen, PIPE_FORMAT_A8_UNORM,
600                                         st->internal_target, 0, 0,
601                                         PIPE_BIND_SAMPLER_VIEW)) {
602       st->bitmap.tex_format = PIPE_FORMAT_A8_UNORM;
603    }
604    else {
605       /* XXX support more formats */
606       assert(0);
607    }
608 
609    /* Create the vertex shader */
610    st_make_passthrough_vertex_shader(st);
611 
612    reset_cache(st);
613 }
614 
615 void
st_Bitmap(struct gl_context * ctx,GLint x,GLint y,GLsizei width,GLsizei height,const struct gl_pixelstore_attrib * unpack,const GLubyte * bitmap,struct pipe_resource * tex)616 st_Bitmap(struct gl_context *ctx, GLint x, GLint y,
617           GLsizei width, GLsizei height,
618           const struct gl_pixelstore_attrib *unpack, const GLubyte *bitmap,
619           struct pipe_resource *tex)
620 {
621    struct st_context *st = st_context(ctx);
622 
623    assert(width > 0);
624    assert(height > 0);
625 
626    st_invalidate_readpix_cache(st);
627    if (tex)
628       st_flush_bitmap_cache(st);
629 
630    if (!st->bitmap.tex_format) {
631       init_bitmap_state(st);
632    }
633 
634    /* We only need to validate any non-ST_NEW_CONSTANTS state. The VS we use
635     * for bitmap drawing uses no constants and the FS constants are
636     * explicitly uploaded in the draw_bitmap_quad() function.
637     */
638    st_validate_state(st, ST_PIPELINE_META_STATE_MASK & ~ST_NEW_CONSTANTS);
639 
640    struct pipe_sampler_view *view = NULL;
641 
642    if (!tex) {
643       if (UseBitmapCache && accum_bitmap(ctx, x, y, width, height, unpack, bitmap))
644          return;
645 
646       struct pipe_resource *pt =
647          st_make_bitmap_texture(ctx, width, height, unpack, bitmap);
648       if (!pt)
649          return;
650 
651       assert(pt->target == PIPE_TEXTURE_2D || pt->target == PIPE_TEXTURE_RECT);
652 
653       view = st_create_texture_sampler_view(st->pipe, pt);
654       /* unreference the texture because it's referenced by sv */
655       pipe_resource_reference(&pt, NULL);
656    } else {
657       /* tex comes from a display list. */
658       view = st_create_texture_sampler_view(st->pipe, tex);
659    }
660 
661    if (view) {
662       draw_bitmap_quad(ctx, x, y, ctx->Current.RasterPos[2],
663                        width, height, view, ctx->Current.RasterColor,
664                        ctx->FragmentProgram._Current,
665                        ctx->Scissor.EnableFlags & 0x1,
666                        ctx->Color._ClampFragmentColor);
667    }
668 }
669 
670 /** Per-context tear-down */
671 void
st_destroy_bitmap(struct st_context * st)672 st_destroy_bitmap(struct st_context *st)
673 {
674    struct pipe_context *pipe = st->pipe;
675    struct st_bitmap_cache *cache = &st->bitmap.cache;
676 
677    if (cache->trans && cache->buffer) {
678       pipe_texture_unmap(pipe, cache->trans);
679    }
680    pipe_resource_reference(&st->bitmap.cache.texture, NULL);
681    _mesa_reference_program(st->ctx, &st->bitmap.cache.fp, NULL);
682 }
683