OSDN Git Service

util: add gradient pattern
[android-x86/external-libdrm.git] / freedreno / freedreno_ringbuffer.c
1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3 /*
4  * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  *
25  * Authors:
26  *    Rob Clark <robclark@freedesktop.org>
27  */
28
29 #include <assert.h>
30
31 #include "freedreno_drmif.h"
32 #include "freedreno_priv.h"
33 #include "freedreno_ringbuffer.h"
34
35 drm_public struct fd_ringbuffer *
36 fd_ringbuffer_new_flags(struct fd_pipe *pipe, uint32_t size,
37                 enum fd_ringbuffer_flags flags)
38 {
39         struct fd_ringbuffer *ring;
40
41         /* we can't really support "growable" rb's in general for
42          * stateobj's since we need a single gpu addr (ie. can't
43          * do the trick of a chain of IB packets):
44          */
45         if (flags & FD_RINGBUFFER_OBJECT)
46                 assert(size);
47
48         ring = pipe->funcs->ringbuffer_new(pipe, size, flags);
49         if (!ring)
50                 return NULL;
51
52         ring->flags = flags;
53         ring->pipe = pipe;
54         ring->start = ring->funcs->hostptr(ring);
55         ring->end = &(ring->start[ring->size/4]);
56
57         ring->cur = ring->last_start = ring->start;
58
59         return ring;
60 }
61
62 drm_public struct fd_ringbuffer *
63 fd_ringbuffer_new(struct fd_pipe *pipe, uint32_t size)
64 {
65         return fd_ringbuffer_new_flags(pipe, size, 0);
66 }
67
68 drm_public struct fd_ringbuffer *
69 fd_ringbuffer_new_object(struct fd_pipe *pipe, uint32_t size)
70 {
71         return fd_ringbuffer_new_flags(pipe, size, FD_RINGBUFFER_OBJECT);
72 }
73
74 drm_public void fd_ringbuffer_del(struct fd_ringbuffer *ring)
75 {
76         if (!atomic_dec_and_test(&ring->refcnt))
77                 return;
78
79         fd_ringbuffer_reset(ring);
80         ring->funcs->destroy(ring);
81 }
82
83 drm_public struct fd_ringbuffer *
84 fd_ringbuffer_ref(struct fd_ringbuffer *ring)
85 {
86         STATIC_ASSERT(sizeof(ring->refcnt) <= sizeof(ring->__pad));
87         atomic_inc(&ring->refcnt);
88         return ring;
89 }
90
91 /* ringbuffers which are IB targets should set the toplevel rb (ie.
92  * the IB source) as it's parent before emitting reloc's, to ensure
93  * the bookkeeping works out properly.
94  */
95 drm_public void fd_ringbuffer_set_parent(struct fd_ringbuffer *ring,
96                                          struct fd_ringbuffer *parent)
97 {
98         /* state objects should not be parented! */
99         assert(!(ring->flags & FD_RINGBUFFER_OBJECT));
100         ring->parent = parent;
101 }
102
103 drm_public void fd_ringbuffer_reset(struct fd_ringbuffer *ring)
104 {
105         uint32_t *start = ring->start;
106         if (ring->pipe->id == FD_PIPE_2D)
107                 start = &ring->start[0x140];
108         ring->cur = ring->last_start = start;
109         if (ring->funcs->reset)
110                 ring->funcs->reset(ring);
111 }
112
113 drm_public int fd_ringbuffer_flush(struct fd_ringbuffer *ring)
114 {
115         return ring->funcs->flush(ring, ring->last_start, -1, NULL);
116 }
117
118 drm_public int fd_ringbuffer_flush2(struct fd_ringbuffer *ring, int in_fence_fd,
119                 int *out_fence_fd)
120 {
121         return ring->funcs->flush(ring, ring->last_start, in_fence_fd, out_fence_fd);
122 }
123
124 drm_public void fd_ringbuffer_grow(struct fd_ringbuffer *ring, uint32_t ndwords)
125 {
126         assert(ring->funcs->grow);     /* unsupported on kgsl */
127
128         /* there is an upper bound on IB size, which appears to be 0x100000 */
129         if (ring->size < 0x100000)
130                 ring->size *= 2;
131
132         ring->funcs->grow(ring, ring->size);
133
134         ring->start = ring->funcs->hostptr(ring);
135         ring->end = &(ring->start[ring->size/4]);
136
137         ring->cur = ring->last_start = ring->start;
138 }
139
140 drm_public uint32_t fd_ringbuffer_timestamp(struct fd_ringbuffer *ring)
141 {
142         return ring->last_timestamp;
143 }
144
145 drm_public void fd_ringbuffer_reloc(struct fd_ringbuffer *ring,
146                                     const struct fd_reloc *reloc)
147 {
148         assert(ring->pipe->gpu_id < 500);
149         ring->funcs->emit_reloc(ring, reloc);
150 }
151
152 drm_public void fd_ringbuffer_reloc2(struct fd_ringbuffer *ring,
153                                      const struct fd_reloc *reloc)
154 {
155         ring->funcs->emit_reloc(ring, reloc);
156 }
157
158 drm_public uint32_t fd_ringbuffer_cmd_count(struct fd_ringbuffer *ring)
159 {
160         if (!ring->funcs->cmd_count)
161                 return 1;
162         return ring->funcs->cmd_count(ring);
163 }
164
165 drm_public uint32_t
166 fd_ringbuffer_emit_reloc_ring_full(struct fd_ringbuffer *ring,
167                 struct fd_ringbuffer *target, uint32_t cmd_idx)
168 {
169         return ring->funcs->emit_reloc_ring(ring, target, cmd_idx);
170 }
171
172 drm_public uint32_t
173 fd_ringbuffer_size(struct fd_ringbuffer *ring)
174 {
175         /* only really needed for stateobj ringbuffers, and won't really
176          * do what you expect for growable rb's.. so lets just restrict
177          * this to stateobj's for now:
178          */
179         assert(ring->flags & FD_RINGBUFFER_OBJECT);
180         return offset_bytes(ring->cur, ring->start);
181 }
182