Merge branch 'mesa_7_7_branch'
[mesa.git] / src / gallium / auxiliary / util / u_surface.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc. All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * 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
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 **************************************************************************/
26
27 /**
28 * @file
29 * Surface utility functions.
30 *
31 * @author Brian Paul
32 */
33
34
35 #include "pipe/p_screen.h"
36 #include "pipe/p_state.h"
37 #include "pipe/p_defines.h"
38
39 #include "util/u_format.h"
40 #include "util/u_surface.h"
41
42
43 /**
44 * Helper to quickly create an RGBA rendering surface of a certain size.
45 * \param textureOut returns the new texture
46 * \param surfaceOut returns the new surface
47 * \return TRUE for success, FALSE if failure
48 */
49 boolean
50 util_create_rgba_surface(struct pipe_screen *screen,
51 uint width, uint height,
52 struct pipe_texture **textureOut,
53 struct pipe_surface **surfaceOut)
54 {
55 static const enum pipe_format rgbaFormats[] = {
56 PIPE_FORMAT_A8R8G8B8_UNORM,
57 PIPE_FORMAT_B8G8R8A8_UNORM,
58 PIPE_FORMAT_R8G8B8A8_UNORM,
59 PIPE_FORMAT_NONE
60 };
61 const uint target = PIPE_TEXTURE_2D;
62 const uint usage = PIPE_TEXTURE_USAGE_RENDER_TARGET;
63 enum pipe_format format = PIPE_FORMAT_NONE;
64 struct pipe_texture templ;
65 uint i;
66
67 /* Choose surface format */
68 for (i = 0; rgbaFormats[i]; i++) {
69 if (screen->is_format_supported(screen, rgbaFormats[i],
70 target, usage, 0)) {
71 format = rgbaFormats[i];
72 break;
73 }
74 }
75 if (format == PIPE_FORMAT_NONE)
76 return FALSE; /* unable to get an rgba format!?! */
77
78 /* create texture */
79 memset(&templ, 0, sizeof(templ));
80 templ.target = target;
81 templ.format = format;
82 templ.last_level = 0;
83 templ.width0 = width;
84 templ.height0 = height;
85 templ.depth0 = 1;
86 templ.tex_usage = usage;
87
88 *textureOut = screen->texture_create(screen, &templ);
89 if (!*textureOut)
90 return FALSE;
91
92 /* create surface / view into texture */
93 *surfaceOut = screen->get_tex_surface(screen, *textureOut, 0, 0, 0, PIPE_BUFFER_USAGE_GPU_WRITE);
94 if (!*surfaceOut) {
95 pipe_texture_reference(textureOut, NULL);
96 return FALSE;
97 }
98
99 return TRUE;
100 }
101
102
103 /**
104 * Release the surface and texture from util_create_rgba_surface().
105 */
106 void
107 util_destroy_rgba_surface(struct pipe_texture *texture,
108 struct pipe_surface *surface)
109 {
110 pipe_surface_reference(&surface, NULL);
111 pipe_texture_reference(&texture, NULL);
112 }
113