Merge the lp-surface-tiling branch into master.
[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 "lp_context.h"
30 #include "lp_flush.h"
31 #include "lp_surface.h"
32 #include "lp_texture.h"
33 #include "lp_tile_image.h"
34 #include "lp_tile_size.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_surface_copy(struct pipe_context *pipe,
55 struct pipe_surface *dst, unsigned dstx, unsigned dsty,
56 struct pipe_surface *src, unsigned srcx, unsigned srcy,
57 unsigned width, unsigned height)
58 {
59 llvmpipe_flush_texture(pipe,
60 dst->texture, dst->face, dst->level,
61 0, /* flush_flags */
62 FALSE, /* read_only */
63 FALSE, /* cpu_access */
64 FALSE); /* do_not_flush */
65
66 llvmpipe_flush_texture(pipe,
67 src->texture, src->face, src->level,
68 0, /* flush_flags */
69 TRUE, /* read_only */
70 FALSE, /* cpu_access */
71 FALSE); /* do_not_flush */
72
73 /* Look for special case in which we're copying from a tiled image
74 * to a linear image.
75 */
76 {
77 struct llvmpipe_resource *src_tex = llvmpipe_resource(src->texture);
78 struct llvmpipe_resource *dst_tex = llvmpipe_resource(dst->texture);
79 enum pipe_format format = src_tex->base.format;
80
81 /*
82 printf("surface copy from %u to %u: %u,%u to %u,%u %u x %u\n",
83 src_tex->id, dst_tex->id,
84 srcx, srcy, dstx, dsty, width, height);
85 */
86
87 /* set src tiles to linear layout */
88 {
89 unsigned tx, ty, tw, th;
90 unsigned x, y;
91
92 adjust_to_tile_bounds(srcx, srcy, width, height, &tx, &ty, &tw, &th);
93
94 for (y = 0; y < th; y += TILE_SIZE) {
95 for (x = 0; x < tw; x += TILE_SIZE) {
96 (void) llvmpipe_get_texture_tile_linear(src_tex,
97 src->face, src->level,
98 LP_TEX_USAGE_READ,
99 tx + x, ty + y);
100 }
101 }
102 }
103
104 /* set dst tiles to linear layout */
105 {
106 unsigned tx, ty, tw, th;
107 unsigned x, y;
108 enum lp_texture_usage usage;
109
110 /* XXX for the tiles which are completely contained by the
111 * dest rectangle, we could set the usage mode to WRITE_ALL.
112 * Just test for the case of replacing the whole dest region for now.
113 */
114 if (width == dst_tex->base.width0 && height == dst_tex->base.height0)
115 usage = LP_TEX_USAGE_WRITE_ALL;
116 else
117 usage = LP_TEX_USAGE_READ_WRITE;
118
119 adjust_to_tile_bounds(dstx, dsty, width, height, &tx, &ty, &tw, &th);
120
121 for (y = 0; y < th; y += TILE_SIZE) {
122 for (x = 0; x < tw; x += TILE_SIZE) {
123 (void) llvmpipe_get_texture_tile_linear(dst_tex,
124 dst->face, dst->level,
125 usage,
126 tx + x, ty + y);
127 }
128 }
129 }
130
131 /* copy */
132 {
133 const ubyte *src_linear_ptr
134 = llvmpipe_get_texture_image_address(src_tex, src->face,
135 src->level,
136 LP_TEX_LAYOUT_LINEAR);
137 ubyte *dst_linear_ptr
138 = llvmpipe_get_texture_image_address(dst_tex, dst->face,
139 dst->level,
140 LP_TEX_LAYOUT_LINEAR);
141
142 util_copy_rect(dst_linear_ptr, format,
143 dst_tex->stride[dst->level],
144 dstx, dsty,
145 width, height,
146 src_linear_ptr, src_tex->stride[src->level],
147 srcx, srcy);
148 }
149 return;
150 }
151
152 util_surface_copy(pipe, FALSE,
153 dst, dstx, dsty,
154 src, srcx, srcy,
155 width, height);
156 }
157
158 void
159 lp_init_surface_functions(struct llvmpipe_context *lp)
160 {
161 lp->pipe.surface_copy = lp_surface_copy;
162 lp->pipe.surface_fill = util_surface_fill;
163 }