1/* Display a cleared blue window.  This demo has no dependencies on
2 * any utility code, just the graw interface and gallium.
3 */
4
5#include <stdio.h>
6#include "state_tracker/graw.h"
7#include "pipe/p_screen.h"
8#include "pipe/p_context.h"
9#include "pipe/p_state.h"
10#include "pipe/p_defines.h"
11
12enum pipe_format formats[] = {
13   PIPE_FORMAT_R8G8B8A8_UNORM,
14   PIPE_FORMAT_B8G8R8A8_UNORM,
15   PIPE_FORMAT_NONE
16};
17
18static const int WIDTH = 300;
19static const int HEIGHT = 300;
20
21struct pipe_screen *screen;
22struct pipe_context *ctx;
23struct pipe_surface *surf;
24struct pipe_resource *tex;
25static void *window = NULL;
26
27static void draw( void )
28{
29   union pipe_color_union clear_color = { {1, 0, 1, 1} };
30
31   ctx->clear(ctx, PIPE_CLEAR_COLOR, &clear_color, 0, 0);
32   ctx->flush(ctx, NULL);
33
34   graw_save_surface_to_file(ctx, surf, NULL);
35
36   screen->flush_frontbuffer(screen, tex, 0, 0, window);
37}
38
39static void init( void )
40{
41   struct pipe_framebuffer_state fb;
42   struct pipe_resource templat;
43   struct pipe_surface surf_tmpl;
44   int i;
45
46   /* It's hard to say whether window or screen should be created
47    * first.  Different environments would prefer one or the other.
48    *
49    * Also, no easy way of querying supported formats if the screen
50    * cannot be created first.
51    */
52   for (i = 0; formats[i] != PIPE_FORMAT_NONE; i++) {
53      screen = graw_create_window_and_screen(0, 0, 300, 300,
54                                             formats[i],
55                                             &window);
56      if (window && screen)
57         break;
58   }
59   if (!screen || !window) {
60      fprintf(stderr, "Unable to create window\n");
61      exit(1);
62   }
63
64   ctx = screen->context_create(screen, NULL);
65   if (ctx == NULL)
66      exit(3);
67
68   templat.target = PIPE_TEXTURE_2D;
69   templat.format = formats[i];
70   templat.width0 = WIDTH;
71   templat.height0 = HEIGHT;
72   templat.depth0 = 1;
73   templat.array_size = 1;
74   templat.last_level = 0;
75   templat.nr_samples = 1;
76   templat.bind = (PIPE_BIND_RENDER_TARGET |
77                   PIPE_BIND_DISPLAY_TARGET);
78
79   tex = screen->resource_create(screen,
80                                 &templat);
81   if (tex == NULL)
82      exit(4);
83
84   surf_tmpl.format = templat.format;
85   surf_tmpl.usage = PIPE_BIND_RENDER_TARGET;
86   surf_tmpl.u.tex.level = 0;
87   surf_tmpl.u.tex.first_layer = 0;
88   surf_tmpl.u.tex.last_layer = 0;
89   surf = ctx->create_surface(ctx, tex, &surf_tmpl);
90   if (surf == NULL)
91      exit(5);
92
93   memset(&fb, 0, sizeof fb);
94   fb.nr_cbufs = 1;
95   fb.width = WIDTH;
96   fb.height = HEIGHT;
97   fb.cbufs[0] = surf;
98
99   ctx->set_framebuffer_state(ctx, &fb);
100}
101
102static void args(int argc, char *argv[])
103{
104   int i;
105
106   for (i = 1; i < argc;) {
107      if (graw_parse_args(&i, argc, argv)) {
108         continue;
109      }
110      exit(1);
111   }
112}
113
114int main( int argc, char *argv[] )
115{
116   args(argc, argv);
117   init();
118
119   graw_set_display_func( draw );
120   graw_main_loop();
121   return 0;
122}
123