llvmpipe: don't crash/assert on out of memory
[mesa.git] / src / gallium / drivers / llvmpipe / lp_surface.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "util/u_rect.h"
29 #include "util/u_surface.h"
30 #include "lp_context.h"
31 #include "lp_flush.h"
32 #include "lp_limits.h"
33 #include "lp_surface.h"
34 #include "lp_texture.h"
35
36
37 /**
38 * Adjust x, y, width, height to lie on tile bounds.
39 */
40 static void
41 adjust_to_tile_bounds(unsigned x, unsigned y, unsigned width, unsigned height,
42 unsigned *x_tile, unsigned *y_tile,
43 unsigned *w_tile, unsigned *h_tile)
44 {
45 *x_tile = x & ~(TILE_SIZE - 1);
46 *y_tile = y & ~(TILE_SIZE - 1);
47 *w_tile = ((x + width + TILE_SIZE - 1) & ~(TILE_SIZE - 1)) - *x_tile;
48 *h_tile = ((y + height + TILE_SIZE - 1) & ~(TILE_SIZE - 1)) - *y_tile;
49 }
50
51
52
53 static void
54 lp_resource_copy(struct pipe_context *pipe,
55 struct pipe_resource *dst, struct pipe_subresource subdst,
56 unsigned dstx, unsigned dsty, unsigned dstz,
57 struct pipe_resource *src, struct pipe_subresource subsrc,
58 unsigned srcx, unsigned srcy, unsigned srcz,
59 unsigned width, unsigned height)
60 {
61 /* XXX what about the dstz/srcz parameters - zslice wasn't used... */
62 struct llvmpipe_resource *src_tex = llvmpipe_resource(src);
63 struct llvmpipe_resource *dst_tex = llvmpipe_resource(dst);
64 const enum pipe_format format = src_tex->base.format;
65
66 llvmpipe_flush_resource(pipe,
67 dst, subdst.face, subdst.level,
68 0, /* flush_flags */
69 FALSE, /* read_only */
70 TRUE, /* cpu_access */
71 FALSE); /* do_not_block */
72
73 llvmpipe_flush_resource(pipe,
74 src, subsrc.face, subsrc.level,
75 0, /* flush_flags */
76 TRUE, /* read_only */
77 TRUE, /* cpu_access */
78 FALSE); /* do_not_block */
79
80 /*
81 printf("surface copy from %u to %u: %u,%u to %u,%u %u x %u\n",
82 src_tex->id, dst_tex->id,
83 srcx, srcy, dstx, dsty, width, height);
84 */
85
86 /* set src tiles to linear layout */
87 {
88 unsigned tx, ty, tw, th;
89 unsigned x, y;
90
91 adjust_to_tile_bounds(srcx, srcy, width, height, &tx, &ty, &tw, &th);
92
93 for (y = 0; y < th; y += TILE_SIZE) {
94 for (x = 0; x < tw; x += TILE_SIZE) {
95 (void) llvmpipe_get_texture_tile_linear(src_tex,
96 subsrc.face, subsrc.level,
97 LP_TEX_USAGE_READ,
98 tx + x, ty + y);
99 }
100 }
101 }
102
103 /* set dst tiles to linear layout */
104 {
105 unsigned tx, ty, tw, th;
106 unsigned x, y;
107 enum lp_texture_usage usage;
108
109 adjust_to_tile_bounds(dstx, dsty, width, height, &tx, &ty, &tw, &th);
110
111 for (y = 0; y < th; y += TILE_SIZE) {
112 boolean contained_y = ty + y >= dsty &&
113 ty + y + TILE_SIZE <= dsty + height ?
114 TRUE : FALSE;
115
116 for (x = 0; x < tw; x += TILE_SIZE) {
117 boolean contained_x = tx + x >= dstx &&
118 tx + x + TILE_SIZE <= dstx + width ?
119 TRUE : FALSE;
120
121 /*
122 * Set the usage mode to WRITE_ALL for the tiles which are
123 * completely contained by the dest rectangle.
124 */
125 if (contained_y && contained_x)
126 usage = LP_TEX_USAGE_WRITE_ALL;
127 else
128 usage = LP_TEX_USAGE_READ_WRITE;
129
130 (void) llvmpipe_get_texture_tile_linear(dst_tex,
131 subdst.face, subdst.level,
132 usage,
133 tx + x, ty + y);
134 }
135 }
136 }
137
138 /* copy */
139 {
140 const ubyte *src_linear_ptr
141 = llvmpipe_get_texture_image_address(src_tex, subsrc.face,
142 subsrc.level,
143 LP_TEX_LAYOUT_LINEAR);
144 ubyte *dst_linear_ptr
145 = llvmpipe_get_texture_image_address(dst_tex, subdst.face,
146 subdst.level,
147 LP_TEX_LAYOUT_LINEAR);
148
149 if (dst_linear_ptr && src_linear_ptr) {
150 util_copy_rect(dst_linear_ptr, format,
151 llvmpipe_resource_stride(&dst_tex->base, subdst.level),
152 dstx, dsty,
153 width, height,
154 src_linear_ptr,
155 llvmpipe_resource_stride(&src_tex->base, subsrc.level),
156 srcx, srcy);
157 }
158 }
159 }
160
161
162 void
163 llvmpipe_init_surface_functions(struct llvmpipe_context *lp)
164 {
165 lp->pipe.resource_copy_region = lp_resource_copy;
166 lp->pipe.clear_render_target = util_clear_render_target;
167 lp->pipe.clear_depth_stencil = util_clear_depth_stencil;
168 }