stw_st.c revision c3c1976f522a67be6a0619e938a90cf186ad42e6
1/*
2 * Mesa 3-D graphics library
3 * Version:  7.9
4 *
5 * Copyright (C) 2010 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
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 *    Chia-I Wu <olv@lunarg.com>
26 */
27
28#include "util/u_memory.h"
29#include "util/u_inlines.h"
30#include "state_tracker/st_gl_api.h" /* for st_gl_api_create */
31
32#include "stw_st.h"
33#include "stw_device.h"
34#include "stw_framebuffer.h"
35#include "stw_pixelformat.h"
36
37struct stw_st_framebuffer {
38   struct st_framebuffer_iface base;
39
40   struct stw_framebuffer *fb;
41   struct st_visual stvis;
42
43   struct pipe_resource *textures[ST_ATTACHMENT_COUNT];
44   unsigned texture_width, texture_height;
45   unsigned texture_mask;
46};
47
48static INLINE struct stw_st_framebuffer *
49stw_st_framebuffer(struct st_framebuffer_iface *stfb)
50{
51   return (struct stw_st_framebuffer *) stfb;
52}
53
54/**
55 * Remove outdated textures and create the requested ones.
56 */
57static void
58stw_st_framebuffer_validate_locked(struct st_framebuffer_iface *stfb,
59                                   unsigned width, unsigned height,
60                                   unsigned mask)
61{
62   struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb);
63   struct pipe_resource templ;
64   unsigned i;
65
66   /* remove outdated textures */
67   if (stwfb->texture_width != width || stwfb->texture_height != height) {
68      for (i = 0; i < ST_ATTACHMENT_COUNT; i++)
69         pipe_resource_reference(&stwfb->textures[i], NULL);
70   }
71
72   memset(&templ, 0, sizeof(templ));
73   templ.target = PIPE_TEXTURE_2D;
74   templ.width0 = width;
75   templ.height0 = height;
76   templ.depth0 = 1;
77   templ.array_size = 1;
78   templ.last_level = 0;
79
80   for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
81      enum pipe_format format;
82      unsigned bind;
83
84      /* the texture already exists or not requested */
85      if (stwfb->textures[i] || !(mask & (1 << i))) {
86         /* remember the texture */
87         if (stwfb->textures[i])
88            mask |= (1 << i);
89         continue;
90      }
91
92      switch (i) {
93      case ST_ATTACHMENT_FRONT_LEFT:
94      case ST_ATTACHMENT_BACK_LEFT:
95         format = stwfb->stvis.color_format;
96         bind = PIPE_BIND_DISPLAY_TARGET |
97                PIPE_BIND_RENDER_TARGET;
98         break;
99      case ST_ATTACHMENT_DEPTH_STENCIL:
100         format = stwfb->stvis.depth_stencil_format;
101         bind = PIPE_BIND_DEPTH_STENCIL;
102         break;
103      default:
104         format = PIPE_FORMAT_NONE;
105         break;
106      }
107
108      if (format != PIPE_FORMAT_NONE) {
109         templ.format = format;
110         templ.bind = bind;
111
112         stwfb->textures[i] =
113            stw_dev->screen->resource_create(stw_dev->screen, &templ);
114      }
115   }
116
117   stwfb->texture_width = width;
118   stwfb->texture_height = height;
119   stwfb->texture_mask = mask;
120}
121
122static boolean
123stw_st_framebuffer_validate(struct st_framebuffer_iface *stfb,
124                            const enum st_attachment_type *statts,
125                            unsigned count,
126                            struct pipe_resource **out)
127{
128   struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb);
129   unsigned statt_mask, i;
130
131   statt_mask = 0x0;
132   for (i = 0; i < count; i++)
133      statt_mask |= 1 << statts[i];
134
135   pipe_mutex_lock(stwfb->fb->mutex);
136
137   if (stwfb->fb->must_resize || (statt_mask & ~stwfb->texture_mask)) {
138      stw_st_framebuffer_validate_locked(&stwfb->base,
139            stwfb->fb->width, stwfb->fb->height, statt_mask);
140      stwfb->fb->must_resize = FALSE;
141   }
142
143   for (i = 0; i < count; i++) {
144      out[i] = NULL;
145      pipe_resource_reference(&out[i], stwfb->textures[statts[i]]);
146   }
147
148   stw_framebuffer_release(stwfb->fb);
149
150   return TRUE;
151}
152
153/**
154 * Present an attachment of the framebuffer.
155 */
156static boolean
157stw_st_framebuffer_present_locked(HDC hdc,
158                                  struct st_framebuffer_iface *stfb,
159                                  enum st_attachment_type statt)
160{
161   struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb);
162   struct pipe_resource *resource;
163
164   resource = stwfb->textures[statt];
165   if (resource) {
166      stw_framebuffer_present_locked(hdc, stwfb->fb, resource);
167   }
168
169   return TRUE;
170}
171
172static boolean
173stw_st_framebuffer_flush_front(struct st_framebuffer_iface *stfb,
174                               enum st_attachment_type statt)
175{
176   struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb);
177
178   pipe_mutex_lock(stwfb->fb->mutex);
179
180   return stw_st_framebuffer_present_locked(stwfb->fb->hDC, &stwfb->base, statt);
181}
182
183/**
184 * Create a framebuffer interface.
185 */
186struct st_framebuffer_iface *
187stw_st_create_framebuffer(struct stw_framebuffer *fb)
188{
189   struct stw_st_framebuffer *stwfb;
190
191   stwfb = CALLOC_STRUCT(stw_st_framebuffer);
192   if (!stwfb)
193      return NULL;
194
195   stwfb->fb = fb;
196   stwfb->stvis = fb->pfi->stvis;
197
198   stwfb->base.visual = &stwfb->stvis;
199   stwfb->base.flush_front = stw_st_framebuffer_flush_front;
200   stwfb->base.validate = stw_st_framebuffer_validate;
201
202   return &stwfb->base;
203}
204
205/**
206 * Destroy a framebuffer interface.
207 */
208void
209stw_st_destroy_framebuffer_locked(struct st_framebuffer_iface *stfb)
210{
211   struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb);
212   int i;
213
214   for (i = 0; i < ST_ATTACHMENT_COUNT; i++)
215      pipe_resource_reference(&stwfb->textures[i], NULL);
216
217   FREE(stwfb);
218}
219
220/**
221 * Swap the buffers of the given framebuffer.
222 */
223boolean
224stw_st_swap_framebuffer_locked(HDC hdc, struct st_framebuffer_iface *stfb)
225{
226   struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb);
227   unsigned front = ST_ATTACHMENT_FRONT_LEFT, back = ST_ATTACHMENT_BACK_LEFT;
228   struct pipe_resource *ptex;
229   unsigned mask;
230
231   /* swap the textures */
232   ptex = stwfb->textures[front];
233   stwfb->textures[front] = stwfb->textures[back];
234   stwfb->textures[back] = ptex;
235
236   /* convert to mask */
237   front = 1 << front;
238   back = 1 << back;
239
240   /* swap the bits in mask */
241   mask = stwfb->texture_mask & ~(front | back);
242   if (stwfb->texture_mask & front)
243      mask |= back;
244   if (stwfb->texture_mask & back)
245      mask |= front;
246   stwfb->texture_mask = mask;
247
248   front = ST_ATTACHMENT_FRONT_LEFT;
249   return stw_st_framebuffer_present_locked(hdc, &stwfb->base, front);
250}
251
252/**
253 * Create an st_api of the state tracker.
254 */
255struct st_api *
256stw_st_create_api(void)
257{
258   return st_gl_api_create();
259}
260