1 /**************************************************************************
3 * Copyright 2009 VMware, Inc. All Rights Reserved.
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:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
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.
25 **************************************************************************/
29 * Surface utility functions.
35 #include "pipe/p_defines.h"
36 #include "pipe/p_screen.h"
37 #include "pipe/p_state.h"
39 #include "util/u_format.h"
40 #include "util/u_inlines.h"
41 #include "util/u_rect.h"
42 #include "util/u_surface.h"
43 #include "util/u_pack_color.h"
47 * Helper to quickly create an RGBA rendering surface of a certain size.
48 * \param textureOut returns the new texture
49 * \param surfaceOut returns the new surface
50 * \return TRUE for success, FALSE if failure
53 util_create_rgba_surface(struct pipe_screen *screen,
54 uint width, uint height,
56 struct pipe_resource **textureOut,
57 struct pipe_surface **surfaceOut)
59 static const enum pipe_format rgbaFormats[] = {
60 PIPE_FORMAT_B8G8R8A8_UNORM,
61 PIPE_FORMAT_A8R8G8B8_UNORM,
62 PIPE_FORMAT_A8B8G8R8_UNORM,
65 const uint target = PIPE_TEXTURE_2D;
66 enum pipe_format format = PIPE_FORMAT_NONE;
67 struct pipe_resource templ;
70 /* Choose surface format */
71 for (i = 0; rgbaFormats[i]; i++) {
72 if (screen->is_format_supported(screen, rgbaFormats[i],
73 target, 0, bind, 0)) {
74 format = rgbaFormats[i];
78 if (format == PIPE_FORMAT_NONE)
79 return FALSE; /* unable to get an rgba format!?! */
82 memset(&templ, 0, sizeof(templ));
83 templ.target = target;
84 templ.format = format;
87 templ.height0 = height;
91 *textureOut = screen->resource_create(screen, &templ);
95 /* create surface / view into texture */
96 *surfaceOut = screen->get_tex_surface(screen,
101 pipe_resource_reference(textureOut, NULL);
110 * Release the surface and texture from util_create_rgba_surface().
113 util_destroy_rgba_surface(struct pipe_resource *texture,
114 struct pipe_surface *surface)
116 pipe_surface_reference(&surface, NULL);
117 pipe_resource_reference(&texture, NULL);
123 * Fallback function for pipe->resource_copy_region().
124 * Note: (X,Y)=(0,0) is always the upper-left corner.
127 util_resource_copy_region(struct pipe_context *pipe,
128 struct pipe_resource *dst,
129 struct pipe_subresource subdst,
130 unsigned dst_x, unsigned dst_y, unsigned dst_z,
131 struct pipe_resource *src,
132 struct pipe_subresource subsrc,
133 unsigned src_x, unsigned src_y, unsigned src_z,
134 unsigned w, unsigned h)
136 struct pipe_transfer *src_trans, *dst_trans;
139 enum pipe_format src_format, dst_format;
145 src_format = src->format;
146 dst_format = dst->format;
148 src_trans = pipe_get_transfer(pipe,
156 dst_trans = pipe_get_transfer(pipe,
164 assert(util_format_get_blocksize(dst_format) == util_format_get_blocksize(src_format));
165 assert(util_format_get_blockwidth(dst_format) == util_format_get_blockwidth(src_format));
166 assert(util_format_get_blockheight(dst_format) == util_format_get_blockheight(src_format));
168 src_map = pipe->transfer_map(pipe, src_trans);
169 dst_map = pipe->transfer_map(pipe, dst_trans);
174 if (src_map && dst_map) {
175 util_copy_rect(dst_map,
186 pipe->transfer_unmap(pipe, src_trans);
187 pipe->transfer_unmap(pipe, dst_trans);
189 pipe->transfer_destroy(pipe, src_trans);
190 pipe->transfer_destroy(pipe, dst_trans);
195 #define UBYTE_TO_USHORT(B) ((B) | ((B) << 8))
199 * Fallback for pipe->clear_render_target() function.
200 * XXX this looks too hackish to be really useful.
201 * cpp > 4 looks like a gross hack at best...
202 * and we're missing the equivalent clear_depth_stencil fallback.
203 * Plus can't use these transfer fallbacks when clearing
204 * multisampled surfaces for instance.
207 util_clear_render_target(struct pipe_context *pipe,
208 struct pipe_surface *dst,
210 unsigned dstx, unsigned dsty,
211 unsigned width, unsigned height)
213 struct pipe_transfer *dst_trans;
217 assert(dst->texture);
220 util_pack_color(rgba, dst->texture->format, &uc);
221 dst_trans = pipe_get_transfer(pipe,
227 dstx, dsty, width, height);
229 dst_map = pipe->transfer_map(pipe, dst_trans);
234 assert(dst_trans->stride > 0);
236 switch (util_format_get_blocksize(dst->texture->format)) {
240 util_pack_color(rgba, dst->texture->format, &uc);
241 util_fill_rect(dst_map, dst->texture->format,
243 0, 0, width, height, uc.ui);
247 /* expand the 4-byte clear value to an 8-byte value */
248 /* should probably not convert back from ubyte but not
249 sure what this code really achieved since it doesn't even
250 check for format type... */
251 ushort *row = (ushort *) dst_map;
252 ushort val0 = UBYTE_TO_USHORT((uc.ui >> 0) & 0xff);
253 ushort val1 = UBYTE_TO_USHORT((uc.ui >> 8) & 0xff);
254 ushort val2 = UBYTE_TO_USHORT((uc.ui >> 16) & 0xff);
255 ushort val3 = UBYTE_TO_USHORT((uc.ui >> 24) & 0xff);
257 val0 = (val0 << 8) | val0;
258 val1 = (val1 << 8) | val1;
259 val2 = (val2 << 8) | val2;
260 val3 = (val3 << 8) | val3;
261 for (i = 0; i < height; i++) {
262 for (j = 0; j < width; j++) {
268 row += dst_trans->stride/2;
278 pipe->transfer_unmap(pipe, dst_trans);
279 pipe->transfer_destroy(pipe, dst_trans);