Squashed commit of the following:
[profile/ivi/mesa.git] / src / gallium / drivers / svga / svga_surface.c
1 /**********************************************************
2  * Copyright 2008-2009 VMware, Inc.  All rights reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person
5  * obtaining a copy of this software and associated documentation
6  * files (the "Software"), to deal in the Software without
7  * restriction, including without limitation the rights to use, copy,
8  * modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  *
24  **********************************************************/
25
26 #include "svga_cmd.h"
27
28 #include "pipe/p_state.h"
29 #include "pipe/p_defines.h"
30 #include "util/u_inlines.h"
31 #include "os/os_thread.h"
32 #include "util/u_format.h"
33 #include "util/u_math.h"
34 #include "util/u_memory.h"
35
36 #include "svga_screen.h"
37 #include "svga_context.h"
38 #include "svga_resource_texture.h"
39 #include "svga_surface.h"
40 #include "svga_winsys.h"
41 #include "svga_debug.h"
42
43 #include <util/u_string.h>
44
45
46 void
47 svga_texture_copy_handle(struct svga_context *svga,
48                          struct svga_screen *ss,
49                          struct svga_winsys_surface *src_handle,
50                          unsigned src_x, unsigned src_y, unsigned src_z,
51                          unsigned src_level, unsigned src_face,
52                          struct svga_winsys_surface *dst_handle,
53                          unsigned dst_x, unsigned dst_y, unsigned dst_z,
54                          unsigned dst_level, unsigned dst_face,
55                          unsigned width, unsigned height, unsigned depth)
56 {
57    struct svga_surface dst, src;
58    enum pipe_error ret;
59    SVGA3dCopyBox box, *boxes;
60
61    assert(svga || ss);
62
63    src.handle = src_handle;
64    src.real_level = src_level;
65    src.real_face = src_face;
66    src.real_zslice = 0;
67
68    dst.handle = dst_handle;
69    dst.real_level = dst_level;
70    dst.real_face = dst_face;
71    dst.real_zslice = 0;
72
73    box.x = dst_x;
74    box.y = dst_y;
75    box.z = dst_z;
76    box.w = width;
77    box.h = height;
78    box.d = depth;
79    box.srcx = src_x;
80    box.srcy = src_y;
81    box.srcz = src_z;
82
83 /*
84    SVGA_DBG(DEBUG_VIEWS, "mipcopy src: %p %u (%ux%ux%u), dst: %p %u (%ux%ux%u)\n",
85             src_handle, src_level, src_x, src_y, src_z,
86             dst_handle, dst_level, dst_x, dst_y, dst_z);
87 */
88
89    if (svga) {
90       ret = SVGA3D_BeginSurfaceCopy(svga->swc,
91                                     &src.base,
92                                     &dst.base,
93                                     &boxes, 1);
94       if(ret != PIPE_OK) {
95          svga_context_flush(svga, NULL);
96          ret = SVGA3D_BeginSurfaceCopy(svga->swc,
97                                        &src.base,
98                                        &dst.base,
99                                        &boxes, 1);
100          assert(ret == PIPE_OK);
101       }
102       *boxes = box;
103       SVGA_FIFOCommitAll(svga->swc);
104    } else {
105       pipe_mutex_lock(ss->swc_mutex);
106       ret = SVGA3D_BeginSurfaceCopy(ss->swc,
107                                     &src.base,
108                                     &dst.base,
109                                     &boxes, 1);
110       if(ret != PIPE_OK) {
111          ss->swc->flush(ss->swc, NULL);
112          ret = SVGA3D_BeginSurfaceCopy(ss->swc,
113                                        &src.base,
114                                        &dst.base,
115                                        &boxes, 1);
116          assert(ret == PIPE_OK);
117       }
118       *boxes = box;
119       SVGA_FIFOCommitAll(ss->swc);
120       pipe_mutex_unlock(ss->swc_mutex);
121    }
122 }
123
124
125 struct svga_winsys_surface *
126 svga_texture_view_surface(struct pipe_context *pipe,
127                           struct svga_texture *tex,
128                           SVGA3dSurfaceFormat format,
129                           unsigned start_mip,
130                           unsigned num_mip,
131                           int face_pick,
132                           int zslice_pick,
133                           struct svga_host_surface_cache_key *key) /* OUT */
134 {
135    struct svga_screen *ss = svga_screen(pipe->screen);
136    struct svga_winsys_surface *handle;
137    uint32_t i, j;
138    unsigned z_offset = 0;
139
140    SVGA_DBG(DEBUG_PERF, 
141             "svga: Create surface view: face %d zslice %d mips %d..%d\n",
142             face_pick, zslice_pick, start_mip, start_mip+num_mip-1);
143
144    key->flags = 0;
145    key->format = format;
146    key->numMipLevels = num_mip;
147    key->size.width = u_minify(tex->b.b.width0, start_mip);
148    key->size.height = u_minify(tex->b.b.height0, start_mip);
149    key->size.depth = zslice_pick < 0 ? u_minify(tex->b.b.depth0, start_mip) : 1;
150    key->cachable = 1;
151    assert(key->size.depth == 1);
152    
153    if(tex->b.b.target == PIPE_TEXTURE_CUBE && face_pick < 0) {
154       key->flags |= SVGA3D_SURFACE_CUBEMAP;
155       key->numFaces = 6;
156    } else {
157       key->numFaces = 1;
158    }
159
160    if(key->format == SVGA3D_FORMAT_INVALID) {
161       key->cachable = 0;
162       return NULL;
163    }
164
165    SVGA_DBG(DEBUG_DMA, "surface_create for texture view\n");
166    handle = svga_screen_surface_create(ss, key);
167    if (!handle) {
168       key->cachable = 0;
169       return NULL;
170    }
171
172    SVGA_DBG(DEBUG_DMA, " --> got sid %p (texture view)\n", handle);
173
174    if (face_pick < 0)
175       face_pick = 0;
176
177    if (zslice_pick >= 0)
178        z_offset = zslice_pick;
179
180    for (i = 0; i < key->numMipLevels; i++) {
181       for (j = 0; j < key->numFaces; j++) {
182          if(tex->defined[j + face_pick][i + start_mip]) {
183             unsigned depth = (zslice_pick < 0 ?
184                               u_minify(tex->b.b.depth0, i + start_mip) :
185                               1);
186
187             svga_texture_copy_handle(svga_context(pipe),
188                                      ss,
189                                      tex->handle, 
190                                      0, 0, z_offset, 
191                                      i + start_mip, 
192                                      j + face_pick,
193                                      handle, 0, 0, 0, i, j,
194                                      u_minify(tex->b.b.width0, i + start_mip),
195                                      u_minify(tex->b.b.height0, i + start_mip),
196                                      depth);
197          }
198       }
199    }
200
201    return handle;
202 }
203
204
205 static struct pipe_surface *
206 svga_get_tex_surface(struct pipe_screen *screen,
207                      struct pipe_resource *pt,
208                      unsigned face, unsigned level, unsigned zslice,
209                      unsigned flags)
210 {
211    struct svga_texture *tex = svga_texture(pt);
212    struct svga_surface *s;
213    boolean render = (flags & (PIPE_BIND_RENDER_TARGET |
214                               PIPE_BIND_DEPTH_STENCIL)) ? TRUE : FALSE;
215    boolean view = FALSE;
216    SVGA3dSurfaceFormat format;
217
218    s = CALLOC_STRUCT(svga_surface);
219    if (!s)
220       return NULL;
221
222    pipe_reference_init(&s->base.reference, 1);
223    pipe_resource_reference(&s->base.texture, pt);
224    s->base.format = pt->format;
225    s->base.width = u_minify(pt->width0, level);
226    s->base.height = u_minify(pt->height0, level);
227    s->base.usage = flags;
228    s->base.level = level;
229    s->base.face = face;
230    s->base.zslice = zslice;
231
232    if (!render)
233       format = svga_translate_format(pt->format);
234    else
235       format = svga_translate_format_render(pt->format);
236
237    assert(format != SVGA3D_FORMAT_INVALID);
238
239    if (svga_screen(screen)->debug.force_surface_view)
240       view = TRUE;
241
242    /* Currently only used for compressed textures */
243    if (render && 
244        format != svga_translate_format(pt->format)) {
245       view = TRUE;
246    }
247
248    if (level != 0 && 
249        svga_screen(screen)->debug.force_level_surface_view)
250       view = TRUE;
251
252    if (pt->target == PIPE_TEXTURE_3D)
253       view = TRUE;
254
255    if (svga_screen(screen)->debug.no_surface_view)
256       view = FALSE;
257
258    if (view) {
259       SVGA_DBG(DEBUG_VIEWS, "svga: Surface view: yes %p, level %u face %u z %u, %p\n",
260                pt, level, face, zslice, s);
261
262       s->handle = svga_texture_view_surface(NULL, tex, format, level, 1, face, zslice,
263                                             &s->key);
264       s->real_face = 0;
265       s->real_level = 0;
266       s->real_zslice = 0;
267    } else {
268       SVGA_DBG(DEBUG_VIEWS, "svga: Surface view: no %p, level %u, face %u, z %u, %p\n",
269                pt, level, face, zslice, s);
270
271       memset(&s->key, 0, sizeof s->key);
272       s->handle = tex->handle;
273       s->real_face = face;
274       s->real_level = level;
275       s->real_zslice = zslice;
276    }
277
278    return &s->base;
279 }
280
281
282 static void
283 svga_tex_surface_destroy(struct pipe_surface *surf)
284 {
285    struct svga_surface *s = svga_surface(surf);
286    struct svga_texture *t = svga_texture(surf->texture);
287    struct svga_screen *ss = svga_screen(surf->texture->screen);
288
289    if(s->handle != t->handle) {
290       SVGA_DBG(DEBUG_DMA, "unref sid %p (tex surface)\n", s->handle);
291       svga_screen_surface_destroy(ss, &s->key, &s->handle);
292    }
293
294    pipe_resource_reference(&surf->texture, NULL);
295    FREE(surf);
296 }
297
298
299 static INLINE void 
300 svga_mark_surface_dirty(struct pipe_surface *surf)
301 {
302    struct svga_surface *s = svga_surface(surf);
303
304    if(!s->dirty) {
305       struct svga_texture *tex = svga_texture(surf->texture);
306
307       s->dirty = TRUE;
308
309       if (s->handle == tex->handle)
310          tex->defined[surf->face][surf->level] = TRUE;
311       else {
312          /* this will happen later in svga_propagate_surface */
313       }
314    }
315 }
316
317
318 void svga_mark_surfaces_dirty(struct svga_context *svga)
319 {
320    unsigned i;
321
322    for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
323       if (svga->curr.framebuffer.cbufs[i])
324          svga_mark_surface_dirty(svga->curr.framebuffer.cbufs[i]);
325    }
326    if (svga->curr.framebuffer.zsbuf)
327       svga_mark_surface_dirty(svga->curr.framebuffer.zsbuf);
328 }
329
330
331 /**
332  * Progagate any changes from surfaces to texture.
333  * pipe is optional context to inline the blit command in.
334  */
335 void
336 svga_propagate_surface(struct pipe_context *pipe, struct pipe_surface *surf)
337 {
338    struct svga_surface *s = svga_surface(surf);
339    struct svga_texture *tex = svga_texture(surf->texture);
340    struct svga_screen *ss = svga_screen(surf->texture->screen);
341
342    if (!s->dirty)
343       return;
344
345    s->dirty = FALSE;
346    ss->texture_timestamp++;
347    tex->view_age[surf->level] = ++(tex->age);
348
349    if (s->handle != tex->handle) {
350       SVGA_DBG(DEBUG_VIEWS, "svga: Surface propagate: tex %p, level %u, from %p\n", tex, surf->level, surf);
351       svga_texture_copy_handle(svga_context(pipe), ss,
352                                s->handle, 0, 0, 0, s->real_level, s->real_face,
353                                tex->handle, 0, 0, surf->zslice, surf->level, surf->face,
354                                u_minify(tex->b.b.width0, surf->level),
355                                u_minify(tex->b.b.height0, surf->level), 1);
356       tex->defined[surf->face][surf->level] = TRUE;
357    }
358 }
359
360 /**
361  * Check if we should call svga_propagate_surface on the surface.
362  */
363 boolean
364 svga_surface_needs_propagation(struct pipe_surface *surf)
365 {
366    struct svga_surface *s = svga_surface(surf);
367    struct svga_texture *tex = svga_texture(surf->texture);
368
369    return s->dirty && s->handle != tex->handle;
370 }
371
372
373
374
375
376
377 void
378 svga_screen_init_surface_functions(struct pipe_screen *screen)
379 {
380    screen->get_tex_surface = svga_get_tex_surface;
381    screen->tex_surface_destroy = svga_tex_surface_destroy;
382 }
383