13464ebd5Sriastradh/* Display a cleared blue window.  This demo has no dependencies on
23464ebd5Sriastradh * any utility code, just the graw interface and gallium.
33464ebd5Sriastradh */
43464ebd5Sriastradh
53464ebd5Sriastradh#include <stdio.h>
67ec681f3Smrg#include "frontend/graw.h"
73464ebd5Sriastradh#include "pipe/p_screen.h"
83464ebd5Sriastradh#include "pipe/p_context.h"
93464ebd5Sriastradh#include "pipe/p_state.h"
103464ebd5Sriastradh#include "pipe/p_defines.h"
113464ebd5Sriastradh
123464ebd5Sriastradhenum pipe_format formats[] = {
13af69d88dSmrg   PIPE_FORMAT_RGBA8888_UNORM,
14af69d88dSmrg   PIPE_FORMAT_BGRA8888_UNORM,
153464ebd5Sriastradh   PIPE_FORMAT_NONE
163464ebd5Sriastradh};
173464ebd5Sriastradh
183464ebd5Sriastradhstatic const int WIDTH = 300;
193464ebd5Sriastradhstatic const int HEIGHT = 300;
203464ebd5Sriastradh
213464ebd5Sriastradhstruct pipe_screen *screen;
223464ebd5Sriastradhstruct pipe_context *ctx;
233464ebd5Sriastradhstruct pipe_surface *surf;
243464ebd5Sriastradhstruct pipe_resource *tex;
253464ebd5Sriastradhstatic void *window = NULL;
263464ebd5Sriastradh
273464ebd5Sriastradhstatic void draw( void )
283464ebd5Sriastradh{
29af69d88dSmrg   union pipe_color_union clear_color = { {1, 0, 1, 1} };
303464ebd5Sriastradh
317ec681f3Smrg   ctx->clear(ctx, PIPE_CLEAR_COLOR, NULL, &clear_color, 0, 0);
32af69d88dSmrg   ctx->flush(ctx, NULL, 0);
333464ebd5Sriastradh
343464ebd5Sriastradh   graw_save_surface_to_file(ctx, surf, NULL);
353464ebd5Sriastradh
367ec681f3Smrg   screen->flush_frontbuffer(screen, ctx, tex, 0, 0, window, NULL);
373464ebd5Sriastradh}
383464ebd5Sriastradh
393464ebd5Sriastradhstatic void init( void )
403464ebd5Sriastradh{
413464ebd5Sriastradh   struct pipe_framebuffer_state fb;
423464ebd5Sriastradh   struct pipe_resource templat;
433464ebd5Sriastradh   struct pipe_surface surf_tmpl;
443464ebd5Sriastradh   int i;
453464ebd5Sriastradh
463464ebd5Sriastradh   /* It's hard to say whether window or screen should be created
473464ebd5Sriastradh    * first.  Different environments would prefer one or the other.
483464ebd5Sriastradh    *
493464ebd5Sriastradh    * Also, no easy way of querying supported formats if the screen
503464ebd5Sriastradh    * cannot be created first.
513464ebd5Sriastradh    */
523464ebd5Sriastradh   for (i = 0; formats[i] != PIPE_FORMAT_NONE; i++) {
533464ebd5Sriastradh      screen = graw_create_window_and_screen(0, 0, 300, 300,
543464ebd5Sriastradh                                             formats[i],
553464ebd5Sriastradh                                             &window);
563464ebd5Sriastradh      if (window && screen)
573464ebd5Sriastradh         break;
583464ebd5Sriastradh   }
593464ebd5Sriastradh   if (!screen || !window) {
603464ebd5Sriastradh      fprintf(stderr, "Unable to create window\n");
613464ebd5Sriastradh      exit(1);
623464ebd5Sriastradh   }
633464ebd5Sriastradh
6401e04c3fSmrg   ctx = screen->context_create(screen, NULL, 0);
653464ebd5Sriastradh   if (ctx == NULL)
663464ebd5Sriastradh      exit(3);
673464ebd5Sriastradh
6801e04c3fSmrg   memset(&templat, 0, sizeof(templat));
693464ebd5Sriastradh   templat.target = PIPE_TEXTURE_2D;
703464ebd5Sriastradh   templat.format = formats[i];
713464ebd5Sriastradh   templat.width0 = WIDTH;
723464ebd5Sriastradh   templat.height0 = HEIGHT;
733464ebd5Sriastradh   templat.depth0 = 1;
743464ebd5Sriastradh   templat.array_size = 1;
753464ebd5Sriastradh   templat.last_level = 0;
763464ebd5Sriastradh   templat.bind = (PIPE_BIND_RENDER_TARGET |
773464ebd5Sriastradh                   PIPE_BIND_DISPLAY_TARGET);
783464ebd5Sriastradh
793464ebd5Sriastradh   tex = screen->resource_create(screen,
803464ebd5Sriastradh                                 &templat);
813464ebd5Sriastradh   if (tex == NULL)
823464ebd5Sriastradh      exit(4);
833464ebd5Sriastradh
843464ebd5Sriastradh   surf_tmpl.format = templat.format;
853464ebd5Sriastradh   surf_tmpl.u.tex.level = 0;
863464ebd5Sriastradh   surf_tmpl.u.tex.first_layer = 0;
873464ebd5Sriastradh   surf_tmpl.u.tex.last_layer = 0;
883464ebd5Sriastradh   surf = ctx->create_surface(ctx, tex, &surf_tmpl);
893464ebd5Sriastradh   if (surf == NULL)
903464ebd5Sriastradh      exit(5);
913464ebd5Sriastradh
923464ebd5Sriastradh   memset(&fb, 0, sizeof fb);
933464ebd5Sriastradh   fb.nr_cbufs = 1;
943464ebd5Sriastradh   fb.width = WIDTH;
953464ebd5Sriastradh   fb.height = HEIGHT;
963464ebd5Sriastradh   fb.cbufs[0] = surf;
973464ebd5Sriastradh
983464ebd5Sriastradh   ctx->set_framebuffer_state(ctx, &fb);
993464ebd5Sriastradh}
1003464ebd5Sriastradh
1013464ebd5Sriastradhstatic void args(int argc, char *argv[])
1023464ebd5Sriastradh{
1033464ebd5Sriastradh   int i;
1043464ebd5Sriastradh
1053464ebd5Sriastradh   for (i = 1; i < argc;) {
1063464ebd5Sriastradh      if (graw_parse_args(&i, argc, argv)) {
1073464ebd5Sriastradh         continue;
1083464ebd5Sriastradh      }
1093464ebd5Sriastradh      exit(1);
1103464ebd5Sriastradh   }
1113464ebd5Sriastradh}
1123464ebd5Sriastradh
1133464ebd5Sriastradhint main( int argc, char *argv[] )
1143464ebd5Sriastradh{
1153464ebd5Sriastradh   args(argc, argv);
1163464ebd5Sriastradh   init();
1173464ebd5Sriastradh
1183464ebd5Sriastradh   graw_set_display_func( draw );
1193464ebd5Sriastradh   graw_main_loop();
1203464ebd5Sriastradh   return 0;
1213464ebd5Sriastradh}
122