Tizen 2.0 Release
[profile/ivi/osmesa.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_debug.h"
41
42
43 void
44 svga_texture_copy_handle(struct svga_context *svga,
45                          struct svga_winsys_surface *src_handle,
46                          unsigned src_x, unsigned src_y, unsigned src_z,
47                          unsigned src_level, unsigned src_face,
48                          struct svga_winsys_surface *dst_handle,
49                          unsigned dst_x, unsigned dst_y, unsigned dst_z,
50                          unsigned dst_level, unsigned dst_face,
51                          unsigned width, unsigned height, unsigned depth)
52 {
53    struct svga_surface dst, src;
54    enum pipe_error ret;
55    SVGA3dCopyBox box, *boxes;
56
57    assert(svga);
58
59    src.handle = src_handle;
60    src.real_level = src_level;
61    src.real_face = src_face;
62    src.real_zslice = 0;
63
64    dst.handle = dst_handle;
65    dst.real_level = dst_level;
66    dst.real_face = dst_face;
67    dst.real_zslice = 0;
68
69    box.x = dst_x;
70    box.y = dst_y;
71    box.z = dst_z;
72    box.w = width;
73    box.h = height;
74    box.d = depth;
75    box.srcx = src_x;
76    box.srcy = src_y;
77    box.srcz = src_z;
78
79 /*
80    SVGA_DBG(DEBUG_VIEWS, "mipcopy src: %p %u (%ux%ux%u), dst: %p %u (%ux%ux%u)\n",
81             src_handle, src_level, src_x, src_y, src_z,
82             dst_handle, dst_level, dst_x, dst_y, dst_z);
83 */
84
85    ret = SVGA3D_BeginSurfaceCopy(svga->swc,
86                                  &src.base,
87                                  &dst.base,
88                                  &boxes, 1);
89    if(ret != PIPE_OK) {
90       svga_context_flush(svga, NULL);
91       ret = SVGA3D_BeginSurfaceCopy(svga->swc,
92                                     &src.base,
93                                     &dst.base,
94                                     &boxes, 1);
95       assert(ret == PIPE_OK);
96    }
97    *boxes = box;
98    SVGA_FIFOCommitAll(svga->swc);
99 }
100
101
102 struct svga_winsys_surface *
103 svga_texture_view_surface(struct svga_context *svga,
104                           struct svga_texture *tex,
105                           SVGA3dSurfaceFlags flags,
106                           SVGA3dSurfaceFormat format,
107                           unsigned start_mip,
108                           unsigned num_mip,
109                           int face_pick,
110                           int zslice_pick,
111                           struct svga_host_surface_cache_key *key) /* OUT */
112 {
113    struct svga_screen *ss = svga_screen(svga->pipe.screen);
114    struct svga_winsys_surface *handle;
115    uint32_t i, j;
116    unsigned z_offset = 0;
117
118    SVGA_DBG(DEBUG_PERF, 
119             "svga: Create surface view: face %d zslice %d mips %d..%d\n",
120             face_pick, zslice_pick, start_mip, start_mip+num_mip-1);
121
122    key->flags = flags;
123    key->format = format;
124    key->numMipLevels = num_mip;
125    key->size.width = u_minify(tex->b.b.width0, start_mip);
126    key->size.height = u_minify(tex->b.b.height0, start_mip);
127    key->size.depth = zslice_pick < 0 ? u_minify(tex->b.b.depth0, start_mip) : 1;
128    key->cachable = 1;
129    assert(key->size.depth == 1);
130    
131    if(tex->b.b.target == PIPE_TEXTURE_CUBE && face_pick < 0) {
132       key->flags |= SVGA3D_SURFACE_CUBEMAP;
133       key->numFaces = 6;
134    } else {
135       key->numFaces = 1;
136    }
137
138    if(key->format == SVGA3D_FORMAT_INVALID) {
139       key->cachable = 0;
140       return NULL;
141    }
142
143    SVGA_DBG(DEBUG_DMA, "surface_create for texture view\n");
144    handle = svga_screen_surface_create(ss, key);
145    if (!handle) {
146       key->cachable = 0;
147       return NULL;
148    }
149
150    SVGA_DBG(DEBUG_DMA, " --> got sid %p (texture view)\n", handle);
151
152    if (face_pick < 0)
153       face_pick = 0;
154
155    if (zslice_pick >= 0)
156        z_offset = zslice_pick;
157
158    for (i = 0; i < key->numMipLevels; i++) {
159       for (j = 0; j < key->numFaces; j++) {
160          if(tex->defined[j + face_pick][i + start_mip]) {
161             unsigned depth = (zslice_pick < 0 ?
162                               u_minify(tex->b.b.depth0, i + start_mip) :
163                               1);
164
165             svga_texture_copy_handle(svga,
166                                      tex->handle, 
167                                      0, 0, z_offset, 
168                                      i + start_mip, 
169                                      j + face_pick,
170                                      handle, 0, 0, 0, i, j,
171                                      u_minify(tex->b.b.width0, i + start_mip),
172                                      u_minify(tex->b.b.height0, i + start_mip),
173                                      depth);
174          }
175       }
176    }
177
178    return handle;
179 }
180
181
182 static struct pipe_surface *
183 svga_create_surface(struct pipe_context *pipe,
184                     struct pipe_resource *pt,
185                     const struct pipe_surface *surf_tmpl)
186 {
187    struct svga_context *svga = svga_context(pipe);
188    struct svga_texture *tex = svga_texture(pt);
189    struct pipe_screen *screen = pipe->screen;
190    struct svga_surface *s;
191    unsigned face, zslice;
192    /* XXX surfaces should only be used for rendering purposes nowadays */
193    boolean render = (surf_tmpl->usage & (PIPE_BIND_RENDER_TARGET |
194                                          PIPE_BIND_DEPTH_STENCIL)) ? TRUE : FALSE;
195    boolean view = FALSE;
196    SVGA3dSurfaceFlags flags;
197    SVGA3dSurfaceFormat format;
198
199    assert(surf_tmpl->u.tex.first_layer == surf_tmpl->u.tex.last_layer);
200
201    s = CALLOC_STRUCT(svga_surface);
202    if (!s)
203       return NULL;
204
205    if (pt->target == PIPE_TEXTURE_CUBE) {
206          face = surf_tmpl->u.tex.first_layer;
207          zslice = 0;
208    }
209    else {
210       face = 0;
211       zslice = surf_tmpl->u.tex.first_layer;
212    }
213
214    pipe_reference_init(&s->base.reference, 1);
215    pipe_resource_reference(&s->base.texture, pt);
216    s->base.context = pipe;
217    s->base.format = surf_tmpl->format;
218    s->base.width = u_minify(pt->width0, surf_tmpl->u.tex.level);
219    s->base.height = u_minify(pt->height0, surf_tmpl->u.tex.level);
220    s->base.usage = surf_tmpl->usage;
221    s->base.u.tex.level = surf_tmpl->u.tex.level;
222    s->base.u.tex.first_layer = surf_tmpl->u.tex.first_layer;
223    s->base.u.tex.last_layer = surf_tmpl->u.tex.last_layer;
224
225    if (!render) {
226       flags = SVGA3D_SURFACE_HINT_TEXTURE;
227       format = svga_translate_format(surf_tmpl->format);
228    } else {
229       if (surf_tmpl->usage & PIPE_BIND_RENDER_TARGET) {
230          flags = SVGA3D_SURFACE_HINT_RENDERTARGET;
231       }
232       if (surf_tmpl->usage & PIPE_BIND_DEPTH_STENCIL) {
233          flags = SVGA3D_SURFACE_HINT_DEPTHSTENCIL;
234       }
235       format = svga_translate_format_render(surf_tmpl->format);
236    }
237
238    assert(format != SVGA3D_FORMAT_INVALID);
239
240    if (svga_screen(screen)->debug.force_surface_view)
241       view = TRUE;
242
243    /* Currently only used for compressed textures */
244    if (render && 
245        format != svga_translate_format(surf_tmpl->format)) {
246       view = TRUE;
247    }
248
249    if (surf_tmpl->u.tex.level != 0 &&
250        svga_screen(screen)->debug.force_level_surface_view)
251       view = TRUE;
252
253    if (pt->target == PIPE_TEXTURE_3D)
254       view = TRUE;
255
256    if (svga_screen(screen)->debug.no_surface_view)
257       view = FALSE;
258
259    if (view) {
260       SVGA_DBG(DEBUG_VIEWS, "svga: Surface view: yes %p, level %u face %u z %u, %p\n",
261                pt, surf_tmpl->u.tex.level, face, zslice, s);
262
263       s->handle = svga_texture_view_surface(svga, tex, flags, format,
264                                             surf_tmpl->u.tex.level,
265                                             1, face, zslice, &s->key);
266       s->real_face = 0;
267       s->real_level = 0;
268       s->real_zslice = 0;
269    } else {
270       SVGA_DBG(DEBUG_VIEWS, "svga: Surface view: no %p, level %u, face %u, z %u, %p\n",
271                pt, surf_tmpl->u.tex.level, face, zslice, s);
272
273       memset(&s->key, 0, sizeof s->key);
274       s->handle = tex->handle;
275       s->real_face = face;
276       s->real_zslice = zslice;
277       s->real_level = surf_tmpl->u.tex.level;
278    }
279
280    return &s->base;
281 }
282
283
284 static void
285 svga_surface_destroy(struct pipe_context *pipe,
286                      struct pipe_surface *surf)
287 {
288    struct svga_surface *s = svga_surface(surf);
289    struct svga_texture *t = svga_texture(surf->texture);
290    struct svga_screen *ss = svga_screen(surf->texture->screen);
291
292    if(s->handle != t->handle) {
293       SVGA_DBG(DEBUG_DMA, "unref sid %p (tex surface)\n", s->handle);
294       svga_screen_surface_destroy(ss, &s->key, &s->handle);
295    }
296
297    pipe_resource_reference(&surf->texture, NULL);
298    FREE(surf);
299 }
300
301
302 static INLINE void 
303 svga_mark_surface_dirty(struct pipe_surface *surf)
304 {
305    struct svga_surface *s = svga_surface(surf);
306
307    if(!s->dirty) {
308       struct svga_texture *tex = svga_texture(surf->texture);
309
310       s->dirty = TRUE;
311
312       if (s->handle == tex->handle) {
313          /* hmm so 3d textures always have all their slices marked ? */
314          if (surf->texture->target == PIPE_TEXTURE_CUBE)
315             tex->defined[surf->u.tex.first_layer][surf->u.tex.level] = TRUE;
316          else
317             tex->defined[0][surf->u.tex.level] = TRUE;
318       }
319       else {
320          /* this will happen later in svga_propagate_surface */
321       }
322    }
323 }
324
325
326 void svga_mark_surfaces_dirty(struct svga_context *svga)
327 {
328    unsigned i;
329
330    for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
331       if (svga->curr.framebuffer.cbufs[i])
332          svga_mark_surface_dirty(svga->curr.framebuffer.cbufs[i]);
333    }
334    if (svga->curr.framebuffer.zsbuf)
335       svga_mark_surface_dirty(svga->curr.framebuffer.zsbuf);
336 }
337
338
339 /**
340  * Progagate any changes from surfaces to texture.
341  * pipe is optional context to inline the blit command in.
342  */
343 void
344 svga_propagate_surface(struct svga_context *svga, struct pipe_surface *surf)
345 {
346    struct svga_surface *s = svga_surface(surf);
347    struct svga_texture *tex = svga_texture(surf->texture);
348    struct svga_screen *ss = svga_screen(surf->texture->screen);
349    unsigned zslice, face;
350
351    if (!s->dirty)
352       return;
353
354    if (surf->texture->target == PIPE_TEXTURE_CUBE) {
355       zslice = 0;
356       face = surf->u.tex.first_layer;
357    }
358    else {
359       zslice = surf->u.tex.first_layer;
360       face = 0;
361    }
362
363    s->dirty = FALSE;
364    ss->texture_timestamp++;
365    tex->view_age[surf->u.tex.level] = ++(tex->age);
366
367    if (s->handle != tex->handle) {
368       SVGA_DBG(DEBUG_VIEWS, "svga: Surface propagate: tex %p, level %u, from %p\n", tex, surf->u.tex.level, surf);
369       svga_texture_copy_handle(svga,
370                                s->handle, 0, 0, 0, s->real_level, s->real_face,
371                                tex->handle, 0, 0, zslice, surf->u.tex.level, face,
372                                u_minify(tex->b.b.width0, surf->u.tex.level),
373                                u_minify(tex->b.b.height0, surf->u.tex.level), 1);
374       tex->defined[face][surf->u.tex.level] = TRUE;
375    }
376 }
377
378 /**
379  * Check if we should call svga_propagate_surface on the surface.
380  */
381 boolean
382 svga_surface_needs_propagation(struct pipe_surface *surf)
383 {
384    struct svga_surface *s = svga_surface(surf);
385    struct svga_texture *tex = svga_texture(surf->texture);
386
387    return s->dirty && s->handle != tex->handle;
388 }
389
390
391
392
393
394
395 void
396 svga_init_surface_functions(struct svga_context *svga)
397 {
398    svga->pipe.create_surface = svga_create_surface;
399    svga->pipe.surface_destroy = svga_surface_destroy;
400 }
401