1/*
2 * Mesa 3-D graphics library
3 * Version:  7.12
4 *
5 * Copyright (C) 2011 LunarG Inc.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
24 *
25 * Authors:
26 *    Chia-I Wu <olv@lunarg.com>
27 */
28
29/**
30 * For NULL window system,
31 *
32 *  - the only valid native display is EGL_DEFAULT_DISPLAY
33 *  - there is no window or pixmap
34 */
35
36#include "util/u_memory.h"
37#include "null/null_sw_winsys.h"
38#include "common/native.h"
39
40struct null_display {
41   struct native_display base;
42
43   const struct native_event_handler *event_handler;
44
45   struct native_config *configs;
46   int num_configs;
47};
48
49static INLINE struct null_display *
50null_display(const struct native_display *ndpy)
51{
52   return (struct null_display *) ndpy;
53}
54
55static const struct native_config **
56null_display_get_configs(struct native_display *ndpy, int *num_configs)
57{
58   struct null_display *null = null_display(ndpy);
59   const struct native_config **configs;
60   int i;
61
62   configs = MALLOC(sizeof(*configs) * null->num_configs);
63   if (configs) {
64      for (i = 0; i < null->num_configs; i++)
65         configs[i] = &null->configs[i];
66      if (num_configs)
67         *num_configs = null->num_configs;
68   }
69
70   return configs;
71}
72
73static int
74null_display_get_param(struct native_display *ndpy,
75                      enum native_param_type param)
76{
77   return 0;
78}
79
80static void
81null_display_destroy(struct native_display *ndpy)
82{
83   struct null_display *null = null_display(ndpy);
84
85   FREE(null->configs);
86   ndpy_uninit(&null->base);
87   FREE(null);
88}
89
90static boolean
91null_display_init_config(struct native_display *ndpy)
92{
93   const enum pipe_format color_formats[] =  {
94      PIPE_FORMAT_B8G8R8A8_UNORM,
95      PIPE_FORMAT_B8G8R8X8_UNORM,
96      PIPE_FORMAT_B5G6R5_UNORM,
97      PIPE_FORMAT_NONE
98   };
99   struct null_display *null = null_display(ndpy);
100   int i;
101
102   null->configs = CALLOC(Elements(color_formats) - 1, sizeof(*null->configs));
103   if (!null->configs)
104      return FALSE;
105
106   /* add configs */
107   for (i = 0; color_formats[i] != PIPE_FORMAT_NONE; i++) {
108      if (null->base.screen->is_format_supported(null->base.screen,
109               color_formats[i], PIPE_TEXTURE_2D, 0,
110               PIPE_BIND_RENDER_TARGET)) {
111         struct native_config *nconf = &null->configs[null->num_configs];
112
113         nconf->color_format = color_formats[i];
114         nconf->buffer_mask = 1 << NATIVE_ATTACHMENT_BACK_LEFT;
115         null->num_configs++;
116      }
117   }
118
119   return TRUE;
120}
121
122static boolean
123null_display_init_screen(struct native_display *ndpy)
124{
125   struct null_display *null = null_display(ndpy);
126   struct sw_winsys *ws;
127
128   ws = null_sw_create();
129   if (!ws)
130      return FALSE;
131
132   null->base.screen = null->event_handler->new_sw_screen(&null->base, ws);
133   if (!null->base.screen) {
134      if (ws->destroy)
135         ws->destroy(ws);
136      return FALSE;
137   }
138
139   if (!null_display_init_config(&null->base)) {
140      ndpy_uninit(&null->base);
141      return FALSE;
142   }
143
144   return TRUE;
145}
146
147static struct native_display *
148null_display_create(const struct native_event_handler *event_handler)
149{
150   struct null_display *null;
151
152   null = CALLOC_STRUCT(null_display);
153   if (!null)
154      return NULL;
155
156   null->event_handler = event_handler;
157
158   null->base.init_screen = null_display_init_screen;
159   null->base.destroy = null_display_destroy;
160   null->base.get_param = null_display_get_param;
161   null->base.get_configs = null_display_get_configs;
162
163   return &null->base;
164}
165
166static const struct native_event_handler *null_event_handler;
167
168static struct native_display *
169native_create_display(void *dpy, boolean use_sw)
170{
171   struct native_display *ndpy = NULL;
172
173   /* the only valid display is NULL */
174   if (!dpy)
175      ndpy = null_display_create(null_event_handler);
176
177   return ndpy;
178}
179
180static const struct native_platform null_platform = {
181   "NULL", /* name */
182   native_create_display
183};
184
185const struct native_platform *
186native_get_null_platform(const struct native_event_handler *event_handler)
187{
188   null_event_handler = event_handler;
189   return &null_platform;
190}
191