1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
4 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
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:
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
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
26 * Rob Clark <robclark@freedesktop.org>
29 #include "util/u_format.h"
30 #include "util/u_inlines.h"
31 #include "util/u_transfer.h"
32 #include "util/u_string.h"
34 #include "freedreno_resource.h"
35 #include "freedreno_screen.h"
36 #include "freedreno_surface.h"
37 #include "freedreno_context.h"
38 #include "freedreno_util.h"
41 fd_resource_transfer_map(struct pipe_context
*pctx
,
42 struct pipe_resource
*prsc
,
43 unsigned level
, unsigned usage
,
44 const struct pipe_box
*box
,
45 struct pipe_transfer
**pptrans
)
47 struct fd_context
*ctx
= fd_context(pctx
);
48 struct fd_resource
*rsc
= fd_resource(prsc
);
49 struct pipe_transfer
*ptrans
= util_slab_alloc(&ctx
->transfer_pool
);
50 enum pipe_format format
= prsc
->format
;
56 ptrans
->resource
= prsc
;
57 ptrans
->level
= level
;
58 ptrans
->usage
= usage
;
60 ptrans
->stride
= rsc
->pitch
* rsc
->cpp
;
61 ptrans
->layer_stride
= ptrans
->stride
;
63 buf
= fd_bo_map(rsc
->bo
);
68 box
->y
/ util_format_get_blockheight(format
) * ptrans
->stride
+
69 box
->x
/ util_format_get_blockwidth(format
) * rsc
->cpp
;
72 static void fd_resource_transfer_flush_region(struct pipe_context
*pctx
,
73 struct pipe_transfer
*ptrans
,
74 const struct pipe_box
*box
)
76 struct fd_context
*ctx
= fd_context(pctx
);
77 struct fd_resource
*rsc
= fd_resource(ptrans
->resource
);
80 fd_context_render(pctx
);
83 fd_pipe_wait(ctx
->screen
->pipe
, rsc
->timestamp
);
89 fd_resource_transfer_unmap(struct pipe_context
*pctx
,
90 struct pipe_transfer
*ptrans
)
92 struct fd_context
*ctx
= fd_context(pctx
);
93 util_slab_free(&ctx
->transfer_pool
, ptrans
);
97 fd_resource_destroy(struct pipe_screen
*pscreen
,
98 struct pipe_resource
*prsc
)
100 struct fd_resource
*rsc
= fd_resource(prsc
);
106 fd_resource_get_handle(struct pipe_screen
*pscreen
,
107 struct pipe_resource
*prsc
,
108 struct winsys_handle
*handle
)
110 struct fd_resource
*rsc
= fd_resource(prsc
);
112 return fd_screen_bo_get_handle(pscreen
, rsc
->bo
, rsc
->pitch
, handle
);
116 const struct u_resource_vtbl fd_resource_vtbl
= {
117 .resource_get_handle
= fd_resource_get_handle
,
118 .resource_destroy
= fd_resource_destroy
,
119 .transfer_map
= fd_resource_transfer_map
,
120 .transfer_flush_region
= fd_resource_transfer_flush_region
,
121 .transfer_unmap
= fd_resource_transfer_unmap
,
122 .transfer_inline_write
= u_default_transfer_inline_write
,
126 * Create a new texture object, using the given template info.
128 static struct pipe_resource
*
129 fd_resource_create(struct pipe_screen
*pscreen
,
130 const struct pipe_resource
*tmpl
)
132 struct fd_screen
*screen
= fd_screen(pscreen
);
133 struct fd_resource
*rsc
= CALLOC_STRUCT(fd_resource
);
134 struct pipe_resource
*prsc
= &rsc
->base
.b
;
135 uint32_t flags
, size
;
137 DBG("target=%d, format=%s, %ux%u@%u, array_size=%u, last_level=%u, "
138 "nr_samples=%u, usage=%u, bind=%x, flags=%x",
139 tmpl
->target
, util_format_name(tmpl
->format
),
140 tmpl
->width0
, tmpl
->height0
, tmpl
->depth0
,
141 tmpl
->array_size
, tmpl
->last_level
, tmpl
->nr_samples
,
142 tmpl
->usage
, tmpl
->bind
, tmpl
->flags
);
149 pipe_reference_init(&prsc
->reference
, 1);
150 prsc
->screen
= pscreen
;
152 rsc
->base
.vtbl
= &fd_resource_vtbl
;
153 rsc
->pitch
= ALIGN(tmpl
->width0
, 32);
154 rsc
->cpp
= util_format_get_blocksize(tmpl
->format
);
156 size
= rsc
->pitch
* tmpl
->height0
* rsc
->cpp
;
157 flags
= DRM_FREEDRENO_GEM_TYPE_KMEM
; /* TODO */
159 rsc
->bo
= fd_bo_new(screen
->dev
, size
, flags
);
165 * Create a texture from a winsys_handle. The handle is often created in
166 * another process by first creating a pipe texture and then calling
167 * resource_get_handle.
169 static struct pipe_resource
*
170 fd_resource_from_handle(struct pipe_screen
*pscreen
,
171 const struct pipe_resource
*tmpl
,
172 struct winsys_handle
*handle
)
174 struct fd_resource
*rsc
= CALLOC_STRUCT(fd_resource
);
175 struct pipe_resource
*prsc
= &rsc
->base
.b
;
177 DBG("target=%d, format=%s, %ux%u@%u, array_size=%u, last_level=%u, "
178 "nr_samples=%u, usage=%u, bind=%x, flags=%x",
179 tmpl
->target
, util_format_name(tmpl
->format
),
180 tmpl
->width0
, tmpl
->height0
, tmpl
->depth0
,
181 tmpl
->array_size
, tmpl
->last_level
, tmpl
->nr_samples
,
182 tmpl
->usage
, tmpl
->bind
, tmpl
->flags
);
189 pipe_reference_init(&prsc
->reference
, 1);
190 prsc
->screen
= pscreen
;
192 rsc
->bo
= fd_screen_bo_from_handle(pscreen
, handle
, &rsc
->pitch
);
194 rsc
->base
.vtbl
= &fd_resource_vtbl
;
195 rsc
->pitch
= ALIGN(tmpl
->width0
, 32);
201 * Copy a block of pixels from one resource to another.
202 * The resource must be of the same format.
203 * Resources with nr_samples > 1 are not allowed.
206 fd_resource_copy_region(struct pipe_context
*pctx
,
207 struct pipe_resource
*dst
,
209 unsigned dstx
, unsigned dsty
, unsigned dstz
,
210 struct pipe_resource
*src
,
212 const struct pipe_box
*src_box
)
218 /* Optimal hardware path for blitting pixels.
219 * Scaling, format conversion, up- and downsampling (resolve) are allowed.
222 fd_blit(struct pipe_context
*pctx
, const struct pipe_blit_info
*info
)
229 fd_resource_screen_init(struct pipe_screen
*pscreen
)
231 pscreen
->resource_create
= fd_resource_create
;
232 pscreen
->resource_from_handle
= fd_resource_from_handle
;
233 pscreen
->resource_get_handle
= u_resource_get_handle_vtbl
;
234 pscreen
->resource_destroy
= u_resource_destroy_vtbl
;
238 fd_resource_context_init(struct pipe_context
*pctx
)
240 pctx
->transfer_map
= u_transfer_map_vtbl
;
241 pctx
->transfer_flush_region
= u_transfer_flush_region_vtbl
;
242 pctx
->transfer_unmap
= u_transfer_unmap_vtbl
;
243 pctx
->transfer_inline_write
= u_transfer_inline_write_vtbl
;
244 pctx
->create_surface
= fd_create_surface
;
245 pctx
->surface_destroy
= fd_surface_destroy
;
246 pctx
->resource_copy_region
= fd_resource_copy_region
;
247 pctx
->blit
= fd_blit
;