native_drm.c revision 6947e5254889b99bfba7104d15e9526a7bc1cdfa
15e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)/*
25e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles) * Mesa 3-D graphics library
35e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles) * Version:  7.8
45e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles) *
568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) * Copyright (C) 2010 Chia-I Wu <olv@0xlab.org>
6f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) *
768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) * Permission is hereby granted, free of charge, to any person obtaining a
8c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch * copy of this software and associated documentation files (the "Software"),
9c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch * to deal in the Software without restriction, including without limitation
10c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch * the rights to use, copy, modify, merge, publish, distribute, sublicense,
115e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles) * and/or sell copies of the Software, and to permit persons to whom the
125e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles) * Software is furnished to do so, subject to the following conditions:
135e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles) *
145e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles) * The above copyright notice and this permission notice shall be included
1568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) * in all copies or substantial portions of the Software.
165e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles) *
175e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles) * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
185e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles) * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
195e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles) * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
2068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
225e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles) * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
235e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles) * DEALINGS IN THE SOFTWARE.
245e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles) */
2568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
26f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include <sys/types.h>
275e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)#include <sys/stat.h>
285e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)#include <fcntl.h>
295e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)
30eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "util/u_memory.h"
315e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)#include "egllog.h"
325e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)
335e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)#include "native_drm.h"
345e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)
355e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)/* see get_drm_screen_name */
365e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)#include <radeon_drm.h>
375e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)#include "radeon/drm/radeon_drm_public.h"
385e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)
395e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)static boolean
405e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)drm_display_is_format_supported(struct native_display *ndpy,
41cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                enum pipe_format fmt, boolean is_color)
425e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles){
435e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)   return ndpy->screen->is_format_supported(ndpy->screen,
445e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)         fmt, PIPE_TEXTURE_2D, 0,
455e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)         (is_color) ? PIPE_BIND_RENDER_TARGET :
465e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)         PIPE_BIND_DEPTH_STENCIL, 0);
475e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)}
485e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)
495e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)static const struct native_config **
505e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)drm_display_get_configs(struct native_display *ndpy, int *num_configs)
515e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles){
525e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)   struct drm_display *drmdpy = drm_display(ndpy);
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)   const struct native_config **configs;
545e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)
555e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)   /* first time */
561320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci   if (!drmdpy->config) {
575e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)      struct native_config *nconf;
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      enum pipe_format format;
59116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      drmdpy->config = CALLOC(1, sizeof(*drmdpy->config));
615e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)      if (!drmdpy->config)
62eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch         return NULL;
63a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
645e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)      nconf = &drmdpy->config->base;
655e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)
665e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)      nconf->buffer_mask =
675e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)         (1 << NATIVE_ATTACHMENT_FRONT_LEFT) |
685e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)         (1 << NATIVE_ATTACHMENT_BACK_LEFT);
69e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
705e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)      format = PIPE_FORMAT_B8G8R8A8_UNORM;
71f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      if (!drm_display_is_format_supported(&drmdpy->base, format, TRUE)) {
72a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch         format = PIPE_FORMAT_A8R8G8B8_UNORM;
731e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)         if (!drm_display_is_format_supported(&drmdpy->base, format, TRUE))
741e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)            format = PIPE_FORMAT_NONE;
755e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)      }
7658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      if (format == PIPE_FORMAT_NONE) {
77a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)         FREE(drmdpy->config);
78f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)         drmdpy->config = NULL;
79cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)         return NULL;
801320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      }
811320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
82cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      nconf->color_format = format;
83cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
846d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      /* support KMS */
850de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)      if (drmdpy->resources)
861320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci         nconf->scanout_bit = TRUE;
87a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)   }
88effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
895e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)   configs = MALLOC(sizeof(*configs));
905e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)   if (configs) {
915e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)      configs[0] = &drmdpy->config->base;
925e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)      if (num_configs)
935e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)         *num_configs = 1;
945e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)   }
955e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)
965e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)   return configs;
975e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)}
985e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)
995e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)static int
1005e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)drm_display_get_param(struct native_display *ndpy,
1015e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)                      enum native_param_type param)
102eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch{
103f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)   int val;
10468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
1055e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)   switch (param) {
1065e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)   case NATIVE_PARAM_USE_NATIVE_BUFFER:
1075e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)   case NATIVE_PARAM_PRESERVE_BUFFER:
1085e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)   case NATIVE_PARAM_MAX_SWAP_INTERVAL:
1095e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)   default:
1105e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)      val = 0;
111eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      break;
1121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci   }
1135e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)
1145e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)   return val;
1155e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)}
1165e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)
1175e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)static void
118116680a4aac90f2aa7413d9095a592090648e557Ben Murdochdrm_display_destroy(struct native_display *ndpy)
119116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch{
120116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch   struct drm_display *drmdpy = drm_display(ndpy);
1215e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)
12268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)   if (drmdpy->config)
1235e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)      FREE(drmdpy->config);
124eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
125eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch   drm_display_fini_modeset(&drmdpy->base);
126eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
127eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch   if (drmdpy->base.screen)
128eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      drmdpy->base.screen->destroy(drmdpy->base.screen);
129eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
130eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch   if (drmdpy->fd >= 0)
131eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      close(drmdpy->fd);
132eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
133eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch   FREE(drmdpy);
134eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
135eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
136eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochstatic const char *
137cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)get_drm_screen_name(int fd, drmVersionPtr version)
138eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch{
139eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch   const char *name = version->name;
140eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
141eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch   if (name && !strcmp(name, "radeon")) {
142eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      int chip_id;
143eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      struct drm_radeon_info info;
144eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
145eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      memset(&info, 0, sizeof(info));
146eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      info.request = RADEON_INFO_DEVICE_ID;
147eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      info.value = pointer_to_intptr(&chip_id);
148eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      if (drmCommandWriteRead(fd, DRM_RADEON_INFO, &info, sizeof(info)) != 0)
1495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)         return NULL;
150eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
151eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      name = is_r3xx(chip_id) ? "r300" : "r600";
152eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch   }
153eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)   return name;
1555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
156eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
157eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch/**
158a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) * Initialize KMS and pipe screen.
159eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch */
160eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochstatic boolean
161eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochdrm_display_init_screen(struct native_display *ndpy)
162eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch{
163eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch   struct drm_display *drmdpy = drm_display(ndpy);
164e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch   drmVersionPtr version;
165eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch   const char *name;
166f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
167a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch   version = drmGetVersion(drmdpy->fd);
1681e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)   if (!version) {
1691e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      _eglLog(_EGL_WARNING, "invalid fd %d", drmdpy->fd);
170eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      return FALSE;
17158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)   }
172a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
173f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)   name = get_drm_screen_name(drmdpy->fd, version);
174cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)   if (name) {
1751320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      drmdpy->base.screen =
1761320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci         drmdpy->event_handler->new_drm_screen(&drmdpy->base, name, drmdpy->fd);
177cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)   }
178cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)   drmFreeVersion(version);
1796d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
1800de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)   if (!drmdpy->base.screen) {
1811320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      _eglLog(_EGL_WARNING, "failed to create DRM screen");
182a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      return FALSE;
183effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch   }
184eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
185eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch   return TRUE;
186eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
187eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
188eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochstatic struct pipe_resource *
189eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochdrm_display_import_buffer(struct native_display *ndpy,
190eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                          const struct pipe_resource *templ,
191eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                          void *buf)
192eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch{
193eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch   return ndpy->screen->resource_from_handle(ndpy->screen,
194eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch         templ, (struct winsys_handle *) buf);
195eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
196eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
197eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochstatic boolean
198eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochdrm_display_export_buffer(struct native_display *ndpy,
199f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                          struct pipe_resource *res,
20068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)                          void *buf)
201eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch{
202eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch   return ndpy->screen->resource_get_handle(ndpy->screen,
203eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch         res, (struct winsys_handle *) buf);
204eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
205eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
206eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochstatic struct native_display_buffer drm_display_buffer = {
207eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch   drm_display_import_buffer,
2081320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci   drm_display_export_buffer
209eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch};
210eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
211eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochstatic struct native_display *
212eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochdrm_create_display(int fd, struct native_event_handler *event_handler,
213eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                   void *user_data)
214116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch{
215116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch   struct drm_display *drmdpy;
216116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
217eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch   drmdpy = CALLOC_STRUCT(drm_display);
21868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)   if (!drmdpy)
219eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      return NULL;
220eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
221eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch   drmdpy->fd = fd;
222eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch   drmdpy->event_handler = event_handler;
223eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch   drmdpy->base.user_data = user_data;
224a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
2255e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)   if (!drm_display_init_screen(&drmdpy->base)) {
22668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      drm_display_destroy(&drmdpy->base);
22768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      return NULL;
22868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)   }
22968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
23068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)   drmdpy->base.destroy = drm_display_destroy;
2315e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)   drmdpy->base.get_param = drm_display_get_param;
2325e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)   drmdpy->base.get_configs = drm_display_get_configs;
233f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
2345e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)   drmdpy->base.buffer = &drm_display_buffer;
2355e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)   drm_display_init_modeset(&drmdpy->base);
236f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
237f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)   return &drmdpy->base;
2385e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)}
23968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
240static struct native_display *
241native_create_display(void *dpy, struct native_event_handler *event_handler,
242                      void *user_data)
243{
244   int fd;
245
246   if (dpy) {
247      fd = dup((int) pointer_to_intptr(dpy));
248   }
249   else {
250      fd = open("/dev/dri/card0", O_RDWR);
251   }
252   if (fd < 0)
253      return NULL;
254
255   return drm_create_display(fd, event_handler, user_data);
256}
257
258static const struct native_platform drm_platform = {
259   "DRM", /* name */
260   native_create_display
261};
262
263const struct native_platform *
264native_get_drm_platform(void)
265{
266   return &drm_platform;
267}
268