gallium: move surface utility functions into u_surface.c
[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_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 struct llvmpipe_resource *src_tex = llvmpipe_resource(src->texture);
60 struct llvmpipe_resource *dst_tex = llvmpipe_resource(dst->texture);
61 const enum pipe_format format = src_tex->base.format;
62
63 llvmpipe_flush_resource(pipe,
64 dst->texture, dst->face, dst->level,
65 0, /* flush_flags */
66 FALSE, /* read_only */
67 FALSE, /* cpu_access */
68 FALSE); /* do_not_block */
69
70 llvmpipe_flush_resource(pipe,
71 src->texture, src->face, src->level,
72 0, /* flush_flags */
73 TRUE, /* read_only */
74 FALSE, /* cpu_access */
75 FALSE); /* do_not_block */
76
77 /*
78 printf("surface copy from %u to %u: %u,%u to %u,%u %u x %u\n",
79 src_tex->id, dst_tex->id,
80 srcx, srcy, dstx, dsty, width, height);
81 */
82
83 /* set src tiles to linear layout */
84 {
85 unsigned tx, ty, tw, th;
86 unsigned x, y;
87
88 adjust_to_tile_bounds(srcx, srcy, width, height, &tx, &ty, &tw, &th);
89
90 for (y = 0; y < th; y += TILE_SIZE) {
91 for (x = 0; x < tw; x += TILE_SIZE) {
92 (void) llvmpipe_get_texture_tile_linear(src_tex,
93 src->face, src->level,
94 LP_TEX_USAGE_READ,
95 tx + x, ty + y);
96 }
97 }
98 }
99
100 /* set dst tiles to linear layout */
101 {
102 unsigned tx, ty, tw, th;
103 unsigned x, y;
104 enum lp_texture_usage usage;
105
106 /* XXX for the tiles which are completely contained by the
107 * dest rectangle, we could set the usage mode to WRITE_ALL.
108 * Just test for the case of replacing the whole dest region for now.
109 */
110 if (width == dst_tex->base.width0 && height == dst_tex->base.height0)
111 usage = LP_TEX_USAGE_WRITE_ALL;
112 else
113 usage = LP_TEX_USAGE_READ_WRITE;
114
115 adjust_to_tile_bounds(dstx, dsty, width, height, &tx, &ty, &tw, &th);
116
117 for (y = 0; y < th; y += TILE_SIZE) {
118 for (x = 0; x < tw; x += TILE_SIZE) {
119 (void) llvmpipe_get_texture_tile_linear(dst_tex,
120 dst->face, dst->level,
121 usage,
122 tx + x, ty + y);
123 }
124 }
125 }
126
127 /* copy */
128 {
129 const ubyte *src_linear_ptr
130 = llvmpipe_get_texture_image_address(src_tex, src->face,
131 src->level,
132 LP_TEX_LAYOUT_LINEAR);
133 ubyte *dst_linear_ptr
134 = llvmpipe_get_texture_image_address(dst_tex, dst->face,
135 dst->level,
136 LP_TEX_LAYOUT_LINEAR);
137
138 util_copy_rect(dst_linear_ptr, format,
139 llvmpipe_resource_stride(&dst_tex->base, dst->level),
140 dstx, dsty,
141 width, height,
142 src_linear_ptr,
143 llvmpipe_resource_stride(&src_tex->base, src->level),
144 srcx, srcy);
145 }
146 }
147
148
149 void
150 llvmpipe_init_surface_functions(struct llvmpipe_context *lp)
151 {
152 lp->pipe.surface_copy = lp_surface_copy;
153 lp->pipe.surface_fill = util_surface_fill;
154 }