Merge remote branch 'origin/gallium-0.2' into gallium-0.2
[mesa.git] / src / gallium / drivers / nv50 / nv50_surface.c
1 /*
2 * Copyright 2008 Ben Skeggs
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 #include "nv50_context.h"
24 #include "pipe/p_defines.h"
25 #include "pipe/p_winsys.h"
26 #include "pipe/p_inlines.h"
27
28 #include "util/u_tile.h"
29
30 static void
31 nv50_surface_copy(struct pipe_context *pipe, boolean flip,
32 struct pipe_surface *dest, unsigned destx, unsigned desty,
33 struct pipe_surface *src, unsigned srcx, unsigned srcy,
34 unsigned width, unsigned height)
35 {
36 struct nv50_context *nv50 = (struct nv50_context *)pipe;
37 struct nouveau_winsys *nvws = nv50->screen->nvws;
38
39 if (flip) {
40 desty += height;
41 while (height--) {
42 nvws->surface_copy(nvws, dest, destx, desty--, src,
43 srcx, srcy++, width, 1);
44 }
45 } else {
46 nvws->surface_copy(nvws, dest, destx, desty, src, srcx, srcy,
47 width, height);
48 }
49 }
50
51 static void
52 nv50_surface_fill(struct pipe_context *pipe, struct pipe_surface *dest,
53 unsigned destx, unsigned desty, unsigned width,
54 unsigned height, unsigned value)
55 {
56 struct nv50_context *nv50 = (struct nv50_context *)pipe;
57 struct nouveau_winsys *nvws = nv50->screen->nvws;
58
59 nvws->surface_fill(nvws, dest, destx, desty, width, height, value);
60 }
61
62 static void *
63 nv50_surface_map(struct pipe_screen *screen, struct pipe_surface *ps,
64 unsigned flags )
65 {
66 struct nouveau_winsys *nvws = nv50_screen(screen)->nvws;
67 struct pipe_winsys *ws = screen->winsys;
68 struct nv50_surface *s = nv50_surface(ps);
69 struct nv50_surface m = *s;
70 void *map;
71
72 if (!s->untiled) {
73 s->untiled = ws->buffer_create(ws, 0, 0, ps->buffer->size);
74
75 m.base.buffer = s->untiled;
76 nvws->surface_copy(nvws, &m.base, 0, 0, &s->base, 0, 0,
77 ps->width, ps->height);
78 }
79
80 /* Map original tiled surface to disallow it being validated while
81 * untiled mirror is mapped.
82 */
83 ws->buffer_map(ws, ps->buffer, flags);
84
85 map = ws->buffer_map(ws, s->untiled, flags);
86 if (!map)
87 return NULL;
88
89 return map;
90 }
91
92 static void
93 nv50_surface_unmap(struct pipe_screen *pscreen, struct pipe_surface *ps)
94 {
95 struct nouveau_winsys *nvws = nv50_screen(pscreen)->nvws;
96 struct pipe_winsys *ws = pscreen->winsys;
97 struct nv50_surface *s = nv50_surface(ps);
98 struct nv50_surface m = *s;
99
100 ws->buffer_unmap(ws, s->untiled);
101 ws->buffer_unmap(ws, ps->buffer);
102
103 m.base.buffer = s->untiled;
104 nvws->surface_copy(nvws, &s->base, 0, 0, &m.base, 0, 0,
105 ps->width, ps->height);
106
107 pipe_buffer_reference(pscreen, &s->untiled, NULL);
108 }
109
110 void
111 nv50_init_surface_functions(struct nv50_context *nv50)
112 {
113 nv50->pipe.surface_copy = nv50_surface_copy;
114 nv50->pipe.surface_fill = nv50_surface_fill;
115 }
116
117 void
118 nv50_surface_init_screen_functions(struct pipe_screen *pscreen)
119 {
120 pscreen->surface_map = nv50_surface_map;
121 pscreen->surface_unmap = nv50_surface_unmap;
122 }
123