graw: combine graw_init and graw_create_window functions
[mesa.git] / src / gallium / tests / graw / clear.c
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 "state_tracker/graw.h"
6 #include "pipe/p_screen.h"
7 #include "pipe/p_context.h"
8 #include "pipe/p_state.h"
9 #include "pipe/p_defines.h"
10
11 #include "util/u_debug.h" /* debug_dump_surface_bmp() */
12 #include "os/os_time.h" /* os_time_sleep() */
13
14 enum pipe_format formats[] = {
15 PIPE_FORMAT_R8G8B8A8_UNORM,
16 PIPE_FORMAT_B8G8R8A8_UNORM,
17 PIPE_FORMAT_NONE
18 };
19
20 static const int WIDTH = 300;
21 static const int HEIGHT = 300;
22
23 struct pipe_screen *screen;
24 struct pipe_context *ctx;
25 struct pipe_surface *surf;
26 static void *window = NULL;
27
28 static void draw( void )
29 {
30 float clear_color[4] = {1,0,1,1};
31
32 ctx->clear(ctx, PIPE_CLEAR_COLOR, clear_color, 0, 0);
33 ctx->flush(ctx, PIPE_FLUSH_RENDER_CACHE, NULL);
34
35 #if 0
36 /* At the moment, libgraw leaks out/makes available some of the
37 * symbols from gallium/auxiliary, including these debug helpers.
38 * Will eventually want to bless some of these paths, and lock the
39 * others down so they aren't accessible from test programs.
40 *
41 * This currently just happens to work on debug builds - a release
42 * build will probably fail to link here:
43 */
44 debug_dump_surface_bmp(ctx, "result.bmp", surf);
45 #endif
46
47 screen->flush_frontbuffer(screen, surf, window);
48 }
49
50 static void init( void )
51 {
52 struct pipe_framebuffer_state fb;
53 struct pipe_resource *tex, templat;
54 int i;
55
56 /* It's hard to say whether window or screen should be created
57 * first. Different environments would prefer one or the other.
58 *
59 * Also, no easy way of querying supported formats if the screen
60 * cannot be created first.
61 */
62 for (i = 0;
63 window == NULL && formats[i] != PIPE_FORMAT_NONE;
64 i++) {
65
66 screen = graw_create_window_and_screen(0,0,300,300,
67 formats[i],
68 &window);
69 }
70 if (window == NULL)
71 exit(2);
72
73 ctx = screen->context_create(screen, NULL);
74 if (ctx == NULL)
75 exit(3);
76
77 templat.target = PIPE_TEXTURE_2D;
78 templat.format = formats[i];
79 templat.width0 = WIDTH;
80 templat.height0 = HEIGHT;
81 templat.depth0 = 1;
82 templat.last_level = 0;
83 templat.nr_samples = 1;
84 templat.bind = (PIPE_BIND_RENDER_TARGET |
85 PIPE_BIND_DISPLAY_TARGET);
86
87 tex = screen->resource_create(screen,
88 &templat);
89 if (tex == NULL)
90 exit(4);
91
92 surf = screen->get_tex_surface(screen, tex, 0, 0, 0,
93 PIPE_BIND_RENDER_TARGET |
94 PIPE_BIND_DISPLAY_TARGET);
95 if (surf == NULL)
96 exit(5);
97
98 memset(&fb, 0, sizeof fb);
99 fb.nr_cbufs = 1;
100 fb.width = WIDTH;
101 fb.height = HEIGHT;
102 fb.cbufs[0] = surf;
103
104 ctx->set_framebuffer_state(ctx, &fb);
105 }
106
107
108
109 int main( int argc, char *argv[] )
110 {
111 init();
112
113 graw_set_display_func( draw );
114 graw_main_loop();
115 return 0;
116 }