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