2 * Copyright (C) 2010-2011 Chia-I Wu <olvaffe@gmail.com>
3 * Copyright (C) 2010-2011 LunarG Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
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 NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 #define LOG_TAG "GRALLOC-PIPE"
26 #include <cutils/log.h>
29 #include <pipe/p_screen.h>
30 #include <pipe/p_context.h>
31 #include <state_tracker/drm_driver.h>
32 #include <util/u_inlines.h>
33 #include <util/u_memory.h>
35 #include "gralloc_drm.h"
36 #include "gralloc_drm_priv.h"
39 struct gralloc_drm_drv_t base;
43 pthread_mutex_t mutex;
44 struct pipe_screen *screen;
45 struct pipe_context *context;
49 struct gralloc_drm_bo_t base;
51 struct pipe_resource *resource;
52 struct winsys_handle winsys;
54 struct pipe_transfer *transfer;
57 static enum pipe_format get_pipe_format(int format)
62 case HAL_PIXEL_FORMAT_RGBA_8888:
63 fmt = PIPE_FORMAT_R8G8B8A8_UNORM;
65 case HAL_PIXEL_FORMAT_RGBX_8888:
66 fmt = PIPE_FORMAT_R8G8B8X8_UNORM;
68 case HAL_PIXEL_FORMAT_RGB_888:
69 fmt = PIPE_FORMAT_R8G8B8_UNORM;
71 case HAL_PIXEL_FORMAT_RGB_565:
72 fmt = PIPE_FORMAT_B5G6R5_UNORM;
74 case HAL_PIXEL_FORMAT_BGRA_8888:
75 fmt = PIPE_FORMAT_B8G8R8A8_UNORM;
77 case HAL_PIXEL_FORMAT_RGBA_5551:
78 case HAL_PIXEL_FORMAT_RGBA_4444:
80 fmt = PIPE_FORMAT_NONE;
87 static unsigned get_pipe_bind(int usage)
89 unsigned bind = PIPE_BIND_SHARED;
91 if (usage & GRALLOC_USAGE_SW_READ_MASK)
92 bind |= PIPE_BIND_TRANSFER_READ;
93 if (usage & GRALLOC_USAGE_SW_WRITE_MASK)
94 bind |= PIPE_BIND_TRANSFER_WRITE;
96 if (usage & GRALLOC_USAGE_HW_TEXTURE)
97 bind |= PIPE_BIND_SAMPLER_VIEW;
98 if (usage & GRALLOC_USAGE_HW_RENDER)
99 bind |= PIPE_BIND_RENDER_TARGET;
100 if (usage & GRALLOC_USAGE_HW_FB) {
101 bind |= PIPE_BIND_RENDER_TARGET;
102 bind |= PIPE_BIND_SCANOUT;
108 static struct pipe_buffer *get_pipe_buffer_locked(struct pipe_manager *pm,
109 const struct gralloc_drm_handle_t *handle)
111 struct pipe_buffer *buf;
112 struct pipe_resource templ;
114 memset(&templ, 0, sizeof(templ));
115 templ.format = get_pipe_format(handle->format);
116 templ.bind = get_pipe_bind(handle->usage);
117 templ.target = PIPE_TEXTURE_2D;
119 if (templ.format == PIPE_FORMAT_NONE ||
120 !pm->screen->is_format_supported(pm->screen, templ.format,
121 templ.target, 0, templ.bind)) {
122 LOGE("unsupported format 0x%x", handle->format);
126 buf = CALLOC(1, sizeof(*buf));
128 LOGE("failed to allocate pipe buffer");
132 templ.width0 = handle->width;
133 templ.height0 = handle->height;
137 buf->winsys.type = DRM_API_HANDLE_TYPE_SHARED;
138 buf->winsys.handle = handle->name;
139 buf->winsys.stride = handle->stride;
141 buf->resource = pm->screen->resource_from_handle(pm->screen,
142 &templ, &buf->winsys);
148 pm->screen->resource_create(pm->screen, &templ);
152 buf->winsys.type = DRM_API_HANDLE_TYPE_SHARED;
153 if (!pm->screen->resource_get_handle(pm->screen,
154 buf->resource, &buf->winsys))
158 /* need the gem handle for fb */
159 if (handle->usage & GRALLOC_USAGE_HW_FB) {
160 struct winsys_handle tmp;
162 memset(&tmp, 0, sizeof(tmp));
163 tmp.type = DRM_API_HANDLE_TYPE_KMS;
164 if (!pm->screen->resource_get_handle(pm->screen,
165 buf->resource, &tmp))
168 buf->base.fb_handle = tmp.handle;
174 LOGE("failed to allocate pipe buffer");
176 pipe_resource_reference(&buf->resource, NULL);
182 static struct gralloc_drm_bo_t *pipe_alloc(struct gralloc_drm_drv_t *drv,
183 struct gralloc_drm_handle_t *handle)
185 struct pipe_manager *pm = (struct pipe_manager *) drv;
186 struct pipe_buffer *buf;
188 pthread_mutex_lock(&pm->mutex);
189 buf = get_pipe_buffer_locked(pm, handle);
190 pthread_mutex_unlock(&pm->mutex);
193 handle->name = (int) buf->winsys.handle;
194 handle->stride = (int) buf->winsys.stride;
196 buf->base.handle = handle;
202 static void pipe_free(struct gralloc_drm_drv_t *drv, struct gralloc_drm_bo_t *bo)
204 struct pipe_manager *pm = (struct pipe_manager *) drv;
205 struct pipe_buffer *buf = (struct pipe_buffer *) bo;
207 pthread_mutex_lock(&pm->mutex);
210 pipe_transfer_destroy(pm->context, buf->transfer);
211 pipe_resource_reference(&buf->resource, NULL);
213 pthread_mutex_unlock(&pm->mutex);
218 static int pipe_map(struct gralloc_drm_drv_t *drv,
219 struct gralloc_drm_bo_t *bo, int x, int y, int w, int h,
220 int enable_write, void **addr)
222 struct pipe_manager *pm = (struct pipe_manager *) drv;
223 struct pipe_buffer *buf = (struct pipe_buffer *) bo;
226 pthread_mutex_lock(&pm->mutex);
228 /* need a context to get transfer */
230 pm->context = pm->screen->context_create(pm->screen, NULL);
232 LOGE("failed to create pipe context");
238 enum pipe_transfer_usage usage;
240 usage = PIPE_TRANSFER_READ;
242 usage |= PIPE_TRANSFER_WRITE;
244 assert(!buf->transfer);
247 * ignore x, y, w and h so that returned addr points at the
248 * start of the buffer
250 buf->transfer = pipe_get_transfer(pm->context, buf->resource,
252 buf->resource->width0, buf->resource->height0);
254 *addr = pipe_transfer_map(pm->context, buf->transfer);
259 pthread_mutex_unlock(&pm->mutex);
264 static void pipe_unmap(struct gralloc_drm_drv_t *drv,
265 struct gralloc_drm_bo_t *bo)
267 struct pipe_manager *pm = (struct pipe_manager *) drv;
268 struct pipe_buffer *buf = (struct pipe_buffer *) bo;
270 pthread_mutex_lock(&pm->mutex);
272 assert(buf && buf->transfer);
274 pipe_transfer_unmap(pm->context, buf->transfer);
275 pipe_transfer_destroy(pm->context, buf->transfer);
276 buf->transfer = NULL;
278 pm->context->flush(pm->context, NULL);
280 pthread_mutex_unlock(&pm->mutex);
283 static void pipe_init_kms_features(struct gralloc_drm_drv_t *drv, struct gralloc_drm_t *drm)
285 struct pipe_manager *pm = (struct pipe_manager *) drv;
287 drm->mode_dirty_fb = (strcmp(pm->name, "vmwgfx") == 0);
288 drm->swap_mode = DRM_SWAP_FLIP;
289 drm->mode_sync_flip = 1;
290 drm->swap_interval = 1;
291 drm->vblank_secondary = 0;
294 static void pipe_destroy(struct gralloc_drm_drv_t *drv)
296 struct pipe_manager *pm = (struct pipe_manager *) drv;
299 pm->context->destroy(pm->context);
300 pm->screen->destroy(pm->screen);
305 #include "nouveau/drm/nouveau_drm_public.h"
307 #include "target-helpers/inline_debug_helper.h"
309 static int pipe_init_screen(struct pipe_manager *pm)
311 struct pipe_screen *screen;
314 if (strcmp(pm->name, "nouveau") == 0) {
315 screen = nouveau_drm_screen_create(pm->fd);
320 LOGW("unknown driver %s", pm->name);
326 screen = debug_screen_wrap(screen);
333 struct gralloc_drm_drv_t *gralloc_drm_drv_create_for_pipe(int fd, const char *name)
335 struct pipe_manager *pm;
337 pm = CALLOC(1, sizeof(*pm));
339 LOGE("failed to allocate pipe manager for %s", name);
344 strncpy(pm->name, name, sizeof(pm->name) - 1);
346 pthread_mutex_init(&pm->mutex, NULL);
348 if (pipe_init_screen(pm)) {
353 pm->base.destroy = pipe_destroy;
354 pm->base.init_kms_features = pipe_init_kms_features;
355 pm->base.alloc = pipe_alloc;
356 pm->base.free = pipe_free;
357 pm->base.map = pipe_map;
358 pm->base.unmap = pipe_unmap;