summaryrefslogtreecommitdiff
path: root/src/egl/drivers/android/droid_intel.c
blob: 2f1b26c53fe56e3f8e973313fffccc45d8c81730 (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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
 * Copyright (C) 2009 Chia-I Wu <olvaffe@gmail.com>
 *
 * This is based on the work of eagle, by
 * Copyright © 2008, 2009 Kristian Høgsberg
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that copyright
 * notice and this permission notice appear in supporting documentation, and
 * that the name of the copyright holders not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  The copyright holders make no representations
 * about the suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
 * OF THIS SOFTWARE.
 */

#define LOG_TAG "DROID-INTEL"
#include <utils/Log.h>

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <i915_drm.h>
#include <GL/gl.h> /* dri_interface.h uses some GL integer types... */
#include <GL/internal/dri_interface.h>
#include <EGL/egl.h>

#include "droid.h"
#include "droid_ui.h"

#define INTEL_STRIDE_ALIGNMENT 64

enum {
   INTEL_SURFACE_TYPE_WINDOW,
};

struct droid_backend_intel {
   struct droid_backend base;
   int fd;
   int screen_number;
};

struct droid_surface_intel {
   int type;
   union {
      NativeWindowType win;
   } native;
   __DRIbuffer native_buffer;
   unsigned int native_width, native_height;
   int native_changed;

   unsigned int attachments[20];
   __DRIbuffer buffers[10];
   uint32_t handles[10];
   int num_buffers;
   int depth_idx;

   _EGLSurface *surf;
};

static INLINE struct droid_backend_intel *
lookup_backend(struct droid_backend *backend)
{
   return (struct droid_backend_intel *) backend;
}

static INLINE struct droid_surface_intel *
lookup_surface(struct droid_surface *surface)
{
   return (struct droid_surface_intel *) surface;
}

static __DRIbuffer *
intel_get_native_buffer(struct droid_backend *backend,
                        struct droid_surface *surf,
                        int *width, int *height)
{
   struct droid_surface_intel *isurf = lookup_surface(surf);

   if (!isurf->native_buffer.name)
      return NULL;

   if (width)
      *width = isurf->native_width;
   if (height)
      *height = isurf->native_height;

   return &isurf->native_buffer;
}

static inline uint32_t
align_to(uint32_t value, uint32_t align)
{
   return (value + align - 1) & ~(align - 1);
}

static int
create_buffer(int fd, GLint width, GLint height, GLint cpp, __DRIbuffer *buffer)
{
   struct drm_i915_gem_create create;
   struct drm_gem_flink flink;
   uint32_t size;

   buffer->pitch = align_to(width * cpp, INTEL_STRIDE_ALIGNMENT);
   size = buffer->pitch * height;
   create.size = size;
   if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create)) {
      LOGE("failed to create buffer");
      return 0;
   }

   flink.handle = create.handle;
   if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) < 0) {
      LOGE("failed to flink buffer");
      return 0;
   }

   buffer->name = flink.name;
   buffer->cpp = cpp;
   buffer->flags = 0;

   return create.handle;
}

static void
delete_buffers(struct droid_backend *backend, struct droid_surface *surf)
{
   struct droid_backend_intel *intel = lookup_backend(backend);
   struct droid_surface_intel *isurf = lookup_surface(surf);
   int i;

   for (i = 0; i < isurf->num_buffers; i++) {
      if (isurf->handles[i]) {
         struct drm_gem_close close;

         close.handle = isurf->handles[i];
         if (ioctl(intel->fd, DRM_IOCTL_GEM_CLOSE, &close) < 0)
            LOGE("failed to close bo %d", close.handle);
         isurf->handles[i] = 0;
      }
   }

   isurf->num_buffers = 0;
}

static __DRIbuffer *
intel_get_surface_buffers(struct droid_backend *backend,
                          struct droid_surface *surf,
                          int *width, int *height,
                          unsigned int *attachments, int count,
                          int *out_count, int has_format)
{
   struct droid_backend_intel *intel = lookup_backend(backend);
   struct droid_surface_intel *isurf = lookup_surface(surf);
   unsigned int att_size;
   __DRIbuffer buffers[10];
   uint32_t handles[10];
   int num = 0;

   if (count > 10) {
      LOGW("too many buffers requested");
      count = 10;
   }

   att_size = sizeof(attachments[0]) * count * ((has_format) ? 2 : 1);

   if (isurf->native_changed) {
      delete_buffers(backend, surf);
      isurf->native_changed = 0;
   }

   /* same buffers requested */
   if (isurf->num_buffers == count &&
       memcmp(isurf->attachments, attachments, att_size) == 0) {
      num = isurf->num_buffers;
      goto end;
   }
   memcpy(isurf->attachments, attachments, att_size);

   while (count-- > 0) {
      unsigned int att = *attachments++;
      unsigned int format = (has_format) ? *attachments++ : 0;
      unsigned int cpp = (format) ? format / 8 : isurf->native_buffer.cpp;
      __DRIbuffer *buf = NULL;
      int reuse;

      /* re-use buffer */
      for (reuse = 0; reuse < isurf->num_buffers; reuse++) {
         if (isurf->buffers[reuse].attachment == att) {
            if (isurf->buffers[reuse].cpp == cpp &&
                isurf->handles[reuse])
               buf = &isurf->buffers[reuse];
            break;
         }
      }

      if (0)
         LOGD("%s buffer %d: att %d cpp %d",
               (buf) ? "reuse" : "create", num, att, cpp);

      if (buf) {
         buffers[num] = isurf->buffers[reuse];
         handles[num] = isurf->handles[reuse];
         isurf->handles[reuse] = 0;
      }
      else {
         buffers[num].attachment = att;
         handles[num] = create_buffer(intel->fd,
                                          isurf->native_width,
                                          isurf->native_height,
                                          cpp,
                                          &buffers[num]);
      }
      num++;
   }

   /* delete buffers that are not re-used */
   delete_buffers(backend, surf);

   memcpy(isurf->buffers, buffers, sizeof(buffers[0]) * num);
   memcpy(isurf->handles, handles, sizeof(handles[0]) * num);
   isurf->num_buffers = num;

end:
   *out_count = num;
   *width = isurf->native_width;
   *height = isurf->native_height;

   return isurf->buffers;
}

static void
update_native_buffer(struct droid_surface *surf)
{
   struct droid_surface_intel *isurf = lookup_surface(surf);
   unsigned int name, cpp, pitch, width, height;

   switch (isurf->type) {
   case INTEL_SURFACE_TYPE_WINDOW:
      /* oem[0] always point to the buffer that a client is drawing to */
      name = isurf->native.win->oem[0];
      cpp = ui_bytes_per_pixel(isurf->native.win->format);
      pitch = isurf->native.win->stride * cpp;
      width = isurf->native.win->width;
      height = isurf->native.win->height;
      break;
   default:
      name = cpp = pitch = width = height = 0;
      break;
   }

   isurf->native_buffer.attachment = __DRI_BUFFER_FRONT_LEFT;
   isurf->native_buffer.name = name;
   isurf->native_buffer.cpp = cpp;
   isurf->native_buffer.pitch = pitch;
   isurf->native_buffer.flags = 0;

   isurf->native_width = width;
   isurf->native_height = height;

   isurf->native_changed = 1;
}

static struct droid_surface *
intel_create_window_surface(struct droid_backend *backend,
                            _EGLSurface *surf,
                            NativeWindowType win)
{
   struct droid_surface_intel *isurf;

   if (!win) {
      LOGE("invalid native window");
      _eglError(EGL_BAD_NATIVE_WINDOW, "eglCreateWindowSurface");
      return NULL;
   }

   /* TODO lift this limitation */
   if (!win->oem[0]) {
      LOGE("TODO support for non-gem based window");
      _eglError(EGL_BAD_NATIVE_WINDOW, "eglCreateWindowSurface");
      return NULL;
   }

   isurf = calloc(1, sizeof(*isurf));
   if (!isurf) {
      _eglError(EGL_BAD_ALLOC, "eglCreateWindowSurface");
      return NULL;
   }

   isurf->type = INTEL_SURFACE_TYPE_WINDOW;
   isurf->native.win = win;

   surf->Width = win->width;
   surf->Height = win->height;
   /* always back buffer */
   surf->RenderBuffer = EGL_BACK_BUFFER;

   isurf->surf = surf;

   update_native_buffer((struct droid_surface *) isurf);

   return (struct droid_surface *) isurf;
}

static void
intel_destroy_surface(struct droid_backend *backend, struct droid_surface *surf)
{
   struct droid_surface_intel *isurf = lookup_surface(surf);
   delete_buffers(backend, surf);
   free(isurf);
}

static void
intel_swap_native_buffers(struct droid_backend *backend,
                          struct droid_surface *surf)
{
   struct droid_surface_intel *isurf = lookup_surface(surf);

   if (isurf->type == INTEL_SURFACE_TYPE_WINDOW) {
      uint32_t flags;

      flags = isurf->native.win->swapBuffers(isurf->native.win);
      if (flags & EGL_NATIVES_FLAG_SIZE_CHANGED) {
         update_native_buffer(surf);
      } else {
         /* oem[0] is changed after buffer swap */
         isurf->native_buffer.name = isurf->native.win->oem[0];
      }
   }
}

static int
intel_initialize(struct droid_backend *backend, int *fd, int *screen_number)
{
   struct droid_backend_intel *intel = lookup_backend(backend);
   drm_auth_t auth;
   int err;

   err = ioctl(intel->fd, DRM_IOCTL_GET_MAGIC, &auth);
   if (!err)
      err = ui_auth_gpu(auth.magic);

   if (err) {
      LOGE("failed to authenticate");
      return 0;
   }

   if (fd)
      *fd = intel->fd;
   if (screen_number)
      *screen_number = intel->screen_number;

   return 1;
}

static void
intel_destroy(struct droid_backend *backend)
{
   struct droid_backend_intel *intel = lookup_backend(backend);
   close(intel->fd);
   free(intel);
}

struct droid_backend *
droid_backend_create_intel(const char *dev)
{
   struct droid_backend_intel *intel;

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

   intel->fd = open(dev, O_RDWR);
   if (intel->fd < 0) {
      LOGE("failed to open %s", dev);
      free(intel);
      return NULL;
   }

   intel->screen_number = 0;
   intel->base.driver_name = "i915";
   intel->base.initialize = intel_initialize;
   intel->base.destroy = intel_destroy;

   intel->base.get_native_buffer = intel_get_native_buffer;
   intel->base.get_surface_buffers = intel_get_surface_buffers;

   intel->base.create_window_surface = intel_create_window_surface;
   intel->base.destroy_surface = intel_destroy_surface;
   intel->base.swap_native_buffers = intel_swap_native_buffers;

   return &intel->base;
}