2 * Mesa 3-D graphics library
5 * Copyright (C) 2010 LunarG Inc.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
26 * Chia-I Wu <olv@lunarg.com>
29 #include "util/u_inlines.h"
30 #include "util/u_memory.h"
31 #include "pipe/p_screen.h"
32 #include "pipe/p_context.h"
33 #include "pipe/p_state.h"
35 #include "native_helper.h"
37 struct resource_surface
{
38 struct pipe_screen
*screen
;
39 enum pipe_format format
;
42 struct pipe_resource
*resources
[NUM_NATIVE_ATTACHMENTS
];
47 struct resource_surface
*
48 resource_surface_create(struct pipe_screen
*screen
,
49 enum pipe_format format
, uint bind
)
51 struct resource_surface
*rsurf
= CALLOC_STRUCT(resource_surface
);
54 rsurf
->screen
= screen
;
55 rsurf
->format
= format
;
63 resource_surface_free_resources(struct resource_surface
*rsurf
)
65 if (rsurf
->resource_mask
) {
68 for (i
= 0; i
< NUM_NATIVE_ATTACHMENTS
; i
++) {
69 if (rsurf
->resources
[i
])
70 pipe_resource_reference(&rsurf
->resources
[i
], NULL
);
72 rsurf
->resource_mask
= 0x0;
77 resource_surface_destroy(struct resource_surface
*rsurf
)
79 resource_surface_free_resources(rsurf
);
84 resource_surface_set_size(struct resource_surface
*rsurf
,
85 uint width
, uint height
)
87 boolean changed
= FALSE
;
89 if (rsurf
->width
!= width
|| rsurf
->height
!= height
) {
90 resource_surface_free_resources(rsurf
);
92 rsurf
->height
= height
;
100 resource_surface_get_size(struct resource_surface
*rsurf
,
101 uint
*width
, uint
*height
)
104 *width
= rsurf
->width
;
106 *height
= rsurf
->height
;
110 resource_surface_add_resources(struct resource_surface
*rsurf
,
113 struct pipe_resource templ
;
116 resource_mask
&= ~rsurf
->resource_mask
;
120 if (!rsurf
->width
|| !rsurf
->height
)
123 memset(&templ
, 0, sizeof(templ
));
124 templ
.target
= PIPE_TEXTURE_2D
;
125 templ
.format
= rsurf
->format
;
126 templ
.bind
= rsurf
->bind
;
127 templ
.width0
= rsurf
->width
;
128 templ
.height0
= rsurf
->height
;
130 templ
.array_size
= 1;
132 for (i
= 0; i
< NUM_NATIVE_ATTACHMENTS
; i
++) {
133 if (resource_mask
& (1 <<i
)) {
134 assert(!rsurf
->resources
[i
]);
136 rsurf
->resources
[i
] =
137 rsurf
->screen
->resource_create(rsurf
->screen
, &templ
);
138 if (rsurf
->resources
[i
])
139 rsurf
->resource_mask
|= 1 << i
;
143 return ((rsurf
->resource_mask
& resource_mask
) == resource_mask
);
148 resource_surface_get_resources(struct resource_surface
*rsurf
,
149 struct pipe_resource
**resources
,
154 for (i
= 0; i
< NUM_NATIVE_ATTACHMENTS
; i
++) {
155 if (resource_mask
& (1 << i
)) {
157 pipe_resource_reference(&resources
[i
], rsurf
->resources
[i
]);
162 struct pipe_resource
*
163 resource_surface_get_single_resource(struct resource_surface
*rsurf
,
164 enum native_attachment which
)
166 struct pipe_resource
*pres
= NULL
;
167 pipe_resource_reference(&pres
, rsurf
->resources
[which
]);
172 pointer_swap(const void **p1
, const void **p2
)
174 const void *tmp
= *p1
;
180 resource_surface_swap_buffers(struct resource_surface
*rsurf
,
181 enum native_attachment buf1
,
182 enum native_attachment buf2
,
183 boolean only_if_exist
)
185 const uint buf1_bit
= 1 << buf1
;
186 const uint buf2_bit
= 1 << buf2
;
189 if (only_if_exist
&& !(rsurf
->resources
[buf1
] && rsurf
->resources
[buf2
]))
192 pointer_swap((const void **) &rsurf
->resources
[buf1
],
193 (const void **) &rsurf
->resources
[buf2
]);
196 mask
= rsurf
->resource_mask
& ~(buf1_bit
| buf2_bit
);
197 if (rsurf
->resource_mask
& buf1_bit
)
199 if (rsurf
->resource_mask
& buf2_bit
)
202 rsurf
->resource_mask
= mask
;
206 resource_surface_present(struct resource_surface
*rsurf
,
207 enum native_attachment which
,
208 void *winsys_drawable_handle
)
210 struct pipe_resource
*pres
= rsurf
->resources
[which
];
215 rsurf
->screen
->flush_frontbuffer(rsurf
->screen
,
216 pres
, 0, 0, winsys_drawable_handle
);