summaryrefslogtreecommitdiff
path: root/src/gralloc/gralloc_mod.c
blob: 7f708616c88fc5da41b4ea91415b4a3fe1f0fc50 (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#define LOG_TAG "GRALLOC-MOD"

#include <cutils/log.h>
#include <stdlib.h>
#include <errno.h>
#include <stdarg.h>

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

static int
drm_mod_perform(const struct gralloc_module_t *mod, int op, ...)
{
   struct drm_module_t *drm = (struct drm_module_t *) mod;
   va_list args;
   int ret;

   va_start(args, op);
   switch (op) {
   case GRALLOC_MODULE_PERFORM_GET_DRM_FD:
      {
         int *fd = va_arg(args, int *);

         ret = drm_gem_init(drm);
         if (!ret)
            *fd = drm->fd;
      }
      break;
   case GRALLOC_MODULE_PERFORM_GET_DRM_MAGIC:
      {
         int32_t *magic = va_arg(args, int32_t *);

         ret = drm_gem_init(drm);
         if (!ret)
            ret = drm_gem_get_magic(drm, magic);
      }
      break;
   case GRALLOC_MODULE_PERFORM_AUTH_DRM_MAGIC:
      {
         int32_t magic = va_arg(args, int32_t);

         ret = drm_gem_init(drm);
         if (!ret)
            ret = drm_gem_auth_magic(drm, magic);
      }
      break;
   default:
      ret = -EINVAL;
      break;
   }
   va_end(args);

   return ret;
}

static int
drm_mod_register_buffer(const gralloc_module_t *mod, buffer_handle_t handle)
{
   return (drm_gem_get(handle)) ? 0 : -EINVAL;
}

static int
drm_mod_unregister_buffer(const gralloc_module_t *mod, buffer_handle_t handle)
{
   return (drm_gem_get(handle)) ? 0 : -EINVAL;
}

static int
drm_mod_lock(const gralloc_module_t *mod, buffer_handle_t handle,
             int usage, int x, int y, int w, int h, void **ptr)
{
   struct drm_module_t *drm = (struct drm_module_t *) mod;
   struct drm_bo_t *bo;
   int ret;

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

   bo = drm_gem_validate(handle);
   if (!bo)
      return -EINVAL;

   return drm_gem_drv_map(drm, bo, x, y, w, h, 1, ptr);
}

static int
drm_mod_unlock(const gralloc_module_t *mod, buffer_handle_t handle)
{
   struct drm_module_t *drm = (struct drm_module_t *) mod;
   struct drm_bo_t *bo;

   bo = drm_gem_validate(handle);
   if (!bo)
      return -EINVAL;

   drm_gem_drv_unmap(drm, bo);

   return 0;
}

static int
drm_mod_close_gpu0(struct hw_device_t *dev)
{
   struct alloc_device_t *alloc = (struct alloc_device_t *) dev;

   free(alloc);

   return 0;
}

static int
drm_mod_free_gpu0(alloc_device_t *dev, buffer_handle_t handle)
{
   struct drm_module_t *drm = (struct drm_module_t *) dev->common.module;
   struct drm_bo_t *bo;

   bo = drm_gem_validate(handle);
   if (!bo)
      return -EINVAL;

   if (bo->usage & GRALLOC_USAGE_HW_FB)
      drm_kms_rm_fb(drm, bo);

   drm_gem_drv_free(drm, bo);

   return 0;
}

static int
drm_mod_alloc_gpu0(alloc_device_t *dev, int w, int h, int format, int usage,
                   buffer_handle_t *handle, int *stride)
{
   struct drm_module_t *drm = (struct drm_module_t *) dev->common.module;
   struct drm_bo_t *bo;
   int size, bpp, ret;

	LOGE("PFO gralloc line : %d", __LINE__);
   ret = drm_gem_drv_init(drm);
   if (ret)
      return ret;

	LOGE("PFO gralloc line : %d", __LINE__);
   bpp = drm_mod_get_bpp(format);
   if (!bpp)
      return -EINVAL;

	LOGE("PFO gralloc line : %d", __LINE__);
   bo = drm_gem_drv_alloc(drm, w, h, format, usage, stride);
   if (!bo)
      return -EINVAL;
 	LOGE("PFO gralloc line : %d", __LINE__);
  if (bo->usage & GRALLOC_USAGE_HW_FB) {
      ret = drm_kms_add_fb(drm, bo);
      if (ret) {
         LOGE("failed to add fb");
         drm_gem_drv_free(drm, bo);
         return ret;
      }
   }

   *stride /= bpp;
   *handle = &bo->base;

   return 0;
}

static int
drm_mod_open_gpu0(struct drm_module_t *drm, hw_device_t **dev)
{
   struct alloc_device_t *alloc;
   int ret;

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

   alloc = calloc(1, sizeof(*alloc));
   if (!alloc)
      return -EINVAL;

   alloc->common.tag = HARDWARE_DEVICE_TAG;
   alloc->common.version = 0;
   alloc->common.module = (hw_module_t *) drm;
   alloc->common.close = drm_mod_close_gpu0;

   alloc->alloc = drm_mod_alloc_gpu0;
   alloc->free = drm_mod_free_gpu0;

   *dev = &alloc->common;

   return 0;
}

static int
drm_mod_close_fb0(struct hw_device_t *dev)
{
   struct framebuffer_device_t *fb = (struct framebuffer_device_t *) dev;

   free(fb);

   return 0;
}

static int
drm_mod_set_swap_interval_fb0(struct framebuffer_device_t *fb, int interval)
{
   if (interval < fb->minSwapInterval || interval > fb->maxSwapInterval)
      return -EINVAL;
   return 0;
}

static int
drm_mod_post_fb0(struct framebuffer_device_t *fb, buffer_handle_t handle)
{
   struct drm_module_t *drm = (struct drm_module_t *) fb->common.module;
   struct drm_bo_t *bo;

   bo = drm_gem_validate(handle);
   if (!bo)
      return -EINVAL;

   return drm_kms_post(drm, bo);
}

#include <GLES/gl.h>
static int
drm_mod_composition_complete_fb0(struct framebuffer_device_t *fb)
{
   struct drm_module_t *drm = (struct drm_module_t *) fb->common.module;

   if (drm->mode_page_flip)
      glFlush();
   else
      glFinish();

   return 0;
}

static int
drm_mod_open_fb0(struct drm_module_t *drm, struct hw_device_t **dev)
{
   struct framebuffer_device_t *fb;
   int ret;

   fb = calloc(1, sizeof(*fb));
   if (!fb)
      return -ENOMEM;

   ret = drm_gem_init(drm);
   if (!ret)
      ret = drm_kms_init(drm);
   if (ret) {
      free(fb);
      return ret;
   }

   fb->common.tag = HARDWARE_DEVICE_TAG;
   fb->common.version = 0;
   fb->common.module = (hw_module_t *) drm;
   fb->common.close = drm_mod_close_fb0;

   fb->setSwapInterval = drm_mod_set_swap_interval_fb0;
   fb->post = drm_mod_post_fb0;
   fb->compositionComplete = drm_mod_composition_complete_fb0;

   *((uint32_t *) &fb->flags) = 0x0;
   *((uint32_t *) &fb->width) = drm->mode.hdisplay;
   *((uint32_t *) &fb->height) = drm->mode.vdisplay;
   *((int *)      &fb->stride) = drm->mode.hdisplay;
   *((float *)    &fb->fps) = drm->mode.vrefresh;

   *((int *)      &fb->format) = drm->format;
   *((float *)    &fb->xdpi) = drm->xdpi;
   *((float *)    &fb->ydpi) = drm->ydpi;
   *((int *)      &fb->minSwapInterval) = drm->swap_interval;
   *((int *)      &fb->maxSwapInterval) = drm->swap_interval;

   *dev = &fb->common;

   LOGI("mode.hdisplay %d\n"
        "mode.vdisplay %d\n"
        "mode.vrefresh %d\n"
        "format 0x%x\n"
        "xdpi %d\n"
        "ydpi %d\n",
        drm->mode.hdisplay,
        drm->mode.vdisplay,
        drm->mode.vrefresh,
        drm->format,
        drm->xdpi, drm->ydpi);

   return 0;
}

static int
drm_mod_open(const struct hw_module_t *mod, const char *name, struct hw_device_t **dev)
{
   struct drm_module_t *drm = (struct drm_module_t *) mod;
   int ret;

    if (strcmp(name, GRALLOC_HARDWARE_GPU0) == 0)
       ret = drm_mod_open_gpu0(drm, dev);
    else if (strcmp(name, GRALLOC_HARDWARE_FB0) == 0)
       ret = drm_mod_open_fb0(drm, dev);
    else
       ret = -EINVAL;

    return ret;
}

static struct hw_module_methods_t drm_mod_methods = {
   .open = drm_mod_open
};

struct drm_module_t HAL_MODULE_INFO_SYM = {
   .base = {
      .common = {
         .tag = HARDWARE_MODULE_TAG,
         .version_major = 1,
         .version_minor = 0,
         .id = GRALLOC_HARDWARE_MODULE_ID,
         .name = "DRM Memory Allocator",
         .author = "Chia-I Wu",
         .methods = &drm_mod_methods
      },
      .registerBuffer = drm_mod_register_buffer,
      .unregisterBuffer = drm_mod_unregister_buffer,
      .lock = drm_mod_lock,
      .unlock = drm_mod_unlock,
      .perform = drm_mod_perform
   },
   .mutex = PTHREAD_MUTEX_INITIALIZER,
   .fd = -1
};