1/*
2 * Copyright 2012 Red Hat Inc.
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 OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * Authors: Ben Skeggs
23 *
24 */
25
26#include "util/format/u_format.h"
27#include "util/u_inlines.h"
28
29#include "nv30/nv30_screen.h"
30#include "nv30/nv30_context.h"
31#include "nv30/nv30_resource.h"
32#include "nv30/nv30_transfer.h"
33
34static void
35nv30_memory_barrier(struct pipe_context *pipe, unsigned flags)
36{
37   struct nv30_context *nv30 = nv30_context(pipe);
38   int i;
39
40   if (flags & PIPE_BARRIER_MAPPED_BUFFER) {
41      for (i = 0; i < nv30->num_vtxbufs; ++i) {
42         if (!nv30->vtxbuf[i].buffer.resource)
43            continue;
44         if (nv30->vtxbuf[i].buffer.resource->flags & PIPE_RESOURCE_FLAG_MAP_PERSISTENT)
45            nv30->base.vbo_dirty = true;
46      }
47   }
48}
49
50static struct pipe_resource *
51nv30_resource_create(struct pipe_screen *pscreen,
52                     const struct pipe_resource *tmpl)
53{
54   switch (tmpl->target) {
55   case PIPE_BUFFER:
56      return nouveau_buffer_create(pscreen, tmpl);
57   default:
58      return nv30_miptree_create(pscreen, tmpl);
59   }
60}
61
62static void
63nv30_resource_destroy(struct pipe_screen *pscreen, struct pipe_resource *res)
64{
65   if (res->target == PIPE_BUFFER)
66      nouveau_buffer_destroy(pscreen, res);
67   else
68      nv30_miptree_destroy(pscreen, res);
69}
70
71static struct pipe_resource *
72nv30_resource_from_handle(struct pipe_screen *pscreen,
73                          const struct pipe_resource *tmpl,
74                          struct winsys_handle *handle,
75                          unsigned usage)
76{
77   if (tmpl->target == PIPE_BUFFER)
78      return NULL;
79   else
80      return nv30_miptree_from_handle(pscreen, tmpl, handle);
81}
82
83void
84nv30_resource_screen_init(struct pipe_screen *pscreen)
85{
86   pscreen->resource_create = nv30_resource_create;
87   pscreen->resource_from_handle = nv30_resource_from_handle;
88   pscreen->resource_get_handle = nv30_miptree_get_handle;
89   pscreen->resource_destroy = nv30_resource_destroy;
90}
91
92void
93nv30_resource_init(struct pipe_context *pipe)
94{
95   pipe->buffer_map = nouveau_buffer_transfer_map;
96   pipe->texture_map = nv30_miptree_transfer_map;
97   pipe->transfer_flush_region = nouveau_buffer_transfer_flush_region;
98   pipe->buffer_unmap = nouveau_buffer_transfer_unmap;
99   pipe->texture_unmap = nv30_miptree_transfer_unmap;
100   pipe->buffer_subdata = u_default_buffer_subdata;
101   pipe->texture_subdata = u_default_texture_subdata;
102   pipe->create_surface = nv30_miptree_surface_new;
103   pipe->surface_destroy = nv30_miptree_surface_del;
104   pipe->resource_copy_region = nv30_resource_copy_region;
105   pipe->blit = nv30_blit;
106   pipe->flush_resource = nv30_flush_resource;
107   pipe->memory_barrier = nv30_memory_barrier;
108}
109