summaryrefslogtreecommitdiff
path: root/src/gralloc/gralloc_gem.c
blob: 327633515929b8824cb16faee075e5cdcab0854d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#define LOG_TAG "GRALLOC-GEM"

#include <cutils/log.h>
#include <cutils/atomic.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "gralloc_mod.h"
#include "gralloc_gem.h"

#define unlikely(x) __builtin_expect(!!(x), 0)

#define DRM_PATH "/dev/dri/card0"

static int32_t drm_gem_pid = 0;

static int
drm_gem_get_pid(void)
{
   if (unlikely(!drm_gem_pid))
      android_atomic_write((int32_t) getpid(), &drm_gem_pid);
   return drm_gem_pid;
}

static int
drm_gem_init_locked(struct drm_module_t *drm)
{
   int ret;

   if (drm->fd >= 0)
      return 0;

   drm->fd = open(DRM_PATH, O_RDWR);
   if (drm->fd < 0) {
      LOGE("failed to open %s", DRM_PATH);
      return -EINVAL;
   }

   return 0;
}

int
drm_gem_init(struct drm_module_t *drm)
{
   int ret;

   pthread_mutex_lock(&drm->mutex);
   ret = drm_gem_init_locked(drm);
   pthread_mutex_unlock(&drm->mutex);

   return ret;
}

int
drm_gem_get_magic(struct drm_module_t *drm, int32_t *magic)
{
   int ret;

   ret = drm_gem_init(drm);
   if (ret)
      return ret;

   return drmGetMagic(drm->fd, (drm_magic_t *) magic);
}

int
drm_gem_auth_magic(struct drm_module_t *drm, int32_t magic)
{
   int ret;

   ret = drm_gem_init(drm);
   if (ret)
      return ret;

   return drmAuthMagic(drm->fd, (drm_magic_t) magic);
}

struct drm_bo_t *
drm_gem_create_bo(int width, int height, int format, int usage)
{
   struct drm_bo_t *bo;

   bo = calloc(1, sizeof(*bo));
   if (!bo)
      return NULL;

   bo->base.version = sizeof(bo->base);
   bo->base.numInts = DRM_HANDLE_NUM_INTS;
   bo->base.numFds = DRM_HANDLE_NUM_FDS;

   bo->magic = DRM_HANDLE_MAGIC;

   bo->width = width;
   bo->height = height;
   bo->format = format;
   bo->usage = usage;

   bo->pid = drm_gem_get_pid();

   return bo;
}

struct drm_bo_t *
drm_gem_validate(buffer_handle_t handle)
{
   struct drm_bo_t *bo = drm_gem_get(handle);

   if (bo && unlikely(bo->pid != drm_gem_pid)) {
      bo->pid = drm_gem_get_pid();
      bo->fb_handle = 0;
      bo->fb_id = 0;
      bo->data = 0;
   }

   return bo;
}

static const struct drm_gem_drv *
get_drv_from_fd(int fd)
{
   const struct drm_gem_drv *drv = NULL;
   drmVersionPtr version;

   version = drmGetVersion(fd);
   if (!version) {
      LOGE("invalid DRM fd");
      return NULL;
   }

   if (version->name) {
#ifdef ENABLE_INTEL
      if (!drv && !strcmp(version->name, "intel"))
         drv = &drm_gem_drv_intel;
#endif
#ifdef ENABLE_RADEON
      if (!drv && !strcmp(version->name, "radeon"))
         drv = &drm_gem_drv_radeon;
#endif
#ifdef ENABLE_VMWGFX
      if (!drv && !strcmp(version->name, "vmwgfx"))
         drv = &drm_gem_drv_pipe;
#endif
   }

   if (!drv)
      LOGE("unknown driver: %s", (version->name) ? version->name : "NULL");

   drmFreeVersion(version);

   return drv;
}

static int
drm_gem_drv_init_locked(struct drm_module_t *drm)
{
   const struct drm_gem_drv *drv;
   int ret;

   if (drm->gem)
      return 0;

   drv = get_drv_from_fd(drm->fd);
   if (!drv)
      return -EINVAL;

   ret = drv->init(drm);
   if (!ret)
      drm->drv = (void *) drv;

   return ret;
}

int
drm_gem_drv_init(struct drm_module_t *drm)
{
   int ret;

   pthread_mutex_lock(&drm->mutex);
   ret = drm_gem_drv_init_locked(drm);
   pthread_mutex_unlock(&drm->mutex);

   return ret;
}