r600g: remove now-unused r600_bo::size
[mesa.git] / src / gallium / winsys / r600 / drm / r600_bo.c
1 /*
2 * Copyright 2010 Dave Airlie
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Dave Airlie
25 */
26 #include "r600_priv.h"
27 #include "r600d.h"
28 #include "state_tracker/drm_driver.h"
29 #include "radeon_drm.h"
30
31 struct r600_bo *r600_bo(struct radeon *radeon,
32 unsigned size, unsigned alignment,
33 unsigned binding, unsigned usage)
34 {
35 struct r600_bo *bo;
36 struct radeon_bo *rbo;
37 uint32_t initial_domain, domains;
38
39 /* Staging resources particpate in transfers and blits only
40 * and are used for uploads and downloads from regular
41 * resources. We generate them internally for some transfers.
42 */
43 if (usage == PIPE_USAGE_STAGING)
44 domains = RADEON_GEM_DOMAIN_CPU | RADEON_GEM_DOMAIN_GTT;
45 else
46 domains = (RADEON_GEM_DOMAIN_CPU |
47 RADEON_GEM_DOMAIN_GTT |
48 RADEON_GEM_DOMAIN_VRAM);
49
50 switch(usage) {
51 case PIPE_USAGE_DYNAMIC:
52 case PIPE_USAGE_STREAM:
53 case PIPE_USAGE_STAGING:
54 initial_domain = RADEON_GEM_DOMAIN_GTT;
55 break;
56 case PIPE_USAGE_DEFAULT:
57 case PIPE_USAGE_STATIC:
58 case PIPE_USAGE_IMMUTABLE:
59 default:
60 initial_domain = RADEON_GEM_DOMAIN_VRAM;
61 break;
62 }
63 rbo = radeon_bo(radeon, 0, size, alignment, binding, initial_domain);
64 if (rbo == NULL) {
65 return NULL;
66 }
67
68 bo = calloc(1, sizeof(struct r600_bo));
69 bo->domains = domains;
70 bo->bo = rbo;
71
72 pipe_reference_init(&bo->reference, 1);
73 return bo;
74 }
75
76 struct r600_bo *r600_bo_handle(struct radeon *radeon, struct winsys_handle *whandle,
77 unsigned *stride, unsigned *array_mode)
78 {
79 struct r600_bo *bo = calloc(1, sizeof(struct r600_bo));
80 struct radeon_bo *rbo;
81 unsigned tiling_flags;
82
83 rbo = bo->bo = radeon_bo(radeon, whandle->handle, 0, 0, 0, 0);
84 if (rbo == NULL) {
85 free(bo);
86 return NULL;
87 }
88
89 pipe_reference_init(&bo->reference, 1);
90 bo->domains = (RADEON_GEM_DOMAIN_CPU |
91 RADEON_GEM_DOMAIN_GTT |
92 RADEON_GEM_DOMAIN_VRAM);
93
94 if (stride)
95 *stride = whandle->stride;
96
97 radeon_bo_get_tiling_flags(radeon, rbo, &tiling_flags);
98 if (array_mode) {
99 if (tiling_flags) {
100 if (tiling_flags & RADEON_TILING_MACRO)
101 *array_mode = V_0280A0_ARRAY_2D_TILED_THIN1;
102 else if (tiling_flags & RADEON_TILING_MICRO)
103 *array_mode = V_0280A0_ARRAY_1D_TILED_THIN1;
104 } else {
105 *array_mode = 0;
106 }
107 }
108 return bo;
109 }
110
111 void *r600_bo_map(struct radeon *radeon, struct r600_bo *bo, unsigned usage, void *ctx)
112 {
113 struct pipe_context *pctx = ctx;
114
115 if (usage & PIPE_TRANSFER_UNSYNCHRONIZED) {
116 radeon_bo_map(radeon, bo->bo);
117 return (uint8_t *) bo->bo->data;
118 }
119
120 if (p_atomic_read(&bo->bo->reference.count) > 1) {
121 if (usage & PIPE_TRANSFER_DONTBLOCK) {
122 return NULL;
123 }
124 if (ctx) {
125 pctx->flush(pctx, NULL);
126 }
127 }
128
129 if (usage & PIPE_TRANSFER_DONTBLOCK) {
130 uint32_t domain;
131
132 if (radeon_bo_busy(radeon, bo->bo, &domain))
133 return NULL;
134 if (radeon_bo_map(radeon, bo->bo)) {
135 return NULL;
136 }
137 goto out;
138 }
139
140 radeon_bo_map(radeon, bo->bo);
141 if (radeon_bo_wait(radeon, bo->bo)) {
142 radeon_bo_unmap(radeon, bo->bo);
143 return NULL;
144 }
145
146 out:
147 return (uint8_t *) bo->bo->data;
148 }
149
150 void r600_bo_unmap(struct radeon *radeon, struct r600_bo *bo)
151 {
152 radeon_bo_unmap(radeon, bo->bo);
153 }
154
155 void r600_bo_destroy(struct radeon *radeon, struct r600_bo *bo)
156 {
157 radeon_bo_reference(radeon, &bo->bo, NULL);
158 free(bo);
159 }
160
161 boolean r600_bo_get_winsys_handle(struct radeon *radeon, struct r600_bo *bo,
162 unsigned stride, struct winsys_handle *whandle)
163 {
164 whandle->stride = stride;
165 switch(whandle->type) {
166 case DRM_API_HANDLE_TYPE_KMS:
167 whandle->handle = bo->bo->handle;
168 break;
169 case DRM_API_HANDLE_TYPE_SHARED:
170 if (radeon_bo_get_name(radeon, bo->bo, &whandle->handle))
171 return FALSE;
172 break;
173 default:
174 return FALSE;
175 }
176
177 return TRUE;
178 }