aboutsummaryrefslogtreecommitdiffstats
path: root/src/newt/native/egl_gbm.c
blob: d4b56378ce1db3297c1eef706b7bb1ddea02fd3a (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
#include <stdlib.h>
#include <errno.h>
#include <string.h>

#include "jogamp_newt_driver_egl_gbm_DisplayDriver.h"
#include "jogamp_newt_driver_egl_gbm_ScreenDriver.h"
#include "jogamp_newt_driver_egl_gbm_WindowDriver.h"

#include <xf86drm.h>
#include <xf86drmMode.h>
#include <gbm.h>

#define VERBOSE_ON 1

#ifdef VERBOSE_ON
    #define DBG_PRINT(...) fprintf(stderr, __VA_ARGS__); fflush(stderr) 
#else
    #define DBG_PRINT(...)
#endif
#define ERR_PRINT(...) fprintf(stderr, __VA_ARGS__); fflush(stderr) 

typedef struct {
    int fd; // drmClose
    drmModeRes *resources; // drmModeFreeResources
    drmModeConnector *connector; // drmModeFreeConnector
    drmModeEncoder *encoder; // drmModeFreeEncoder
    drmModeModeInfo *current_mode;
} DRM_HANDLE;

typedef struct {
    struct gbm_bo *bo;
    uint32_t fb_id;
} DRM_GBM_FB;

static jmethodID notifyScreenModeID = NULL;

/**
 * Display
 */

static void freeDrm(DRM_HANDLE *drm) {
    if( NULL != drm ) {
        if( NULL != drm->encoder ) {
            drmModeFreeEncoder(drm->encoder);
            drm->encoder = NULL;
        }
        if( NULL != drm->connector ) {
            drmModeFreeConnector(drm->connector);
            drm->connector = NULL;
        }
        if( NULL != drm->resources ) {
            drmModeFreeResources(drm->resources);
            drm->resources = NULL;
        }
        if( 0 <= drm->fd ) {
            drmClose(drm->fd);
            drm->fd = -1;
        }
        free(drm);
    }
}

JNIEXPORT jboolean JNICALL Java_jogamp_newt_driver_egl_gbm_DisplayDriver_initIDs
  (JNIEnv *env, jclass clazz)
{
    DBG_PRINT( "EGL_GBM.Display initIDs ok\n" );
    return JNI_TRUE;
}

JNIEXPORT jlong JNICALL Java_jogamp_newt_driver_egl_gbm_DisplayDriver_initDrm
  (JNIEnv *env, jclass clazz, jboolean verbose)
{
    static const char *modules[] = {
            "/dev/dri/card0", "i915", "radeon", "nouveau", "vmwgfx", "omapdrm", "exynos", "msm"
    };
    int module_count = sizeof(modules) / sizeof(const char*);
    const char * module_used = NULL;
    int i, j, area;
    DRM_HANDLE *drm = calloc(1, sizeof(DRM_HANDLE));

#ifdef VERBOSE_ON
    verbose = JNI_TRUE;
#endif

    if( verbose ) {
        ERR_PRINT( "EGL_GBM.Display initDrm start (testing %d modules)\n", module_count );
    }

    for (i = 0; i < module_count; i++) {
        if( verbose ) {
            ERR_PRINT("EGL_GBM.Display trying to load module[%d/%d] %s...", 
                i, module_count, modules[i]);
        }
        drm->fd = drmOpen(modules[i], NULL);
        if (drm->fd < 0) {
            if( verbose ) {
                ERR_PRINT("failed.\n");
            }
        } else {
            if( verbose ) {
                ERR_PRINT("success.\n");
            }
            module_used = modules[i];
            break;
        }
    }
    if (drm->fd < 0) {
        ERR_PRINT("EGL_GBM.Display could not open drm device\n");
        goto error;
    }

    drm->resources = drmModeGetResources(drm->fd);
    if ( NULL == drm->resources ) {
        ERR_PRINT("EGL_GBM.Display drmModeGetResources failed on module %s: %s\n", 
            module_used, strerror(errno));
        goto error;
    }

    if( verbose ) {
        for (i = 0; i < drm->resources->count_connectors; i++) {
            drmModeConnector * c = drmModeGetConnector(drm->fd, drm->resources->connectors[i]);
            int chosen = DRM_MODE_CONNECTED == c->connection;
            ERR_PRINT( "EGL_GBM.Display Connector %d/%d chosen %d: id[con 0x%X, enc 0x%X], type %d[id 0x%X], connection %d, dim %dx%x mm, modes %d, encoders %d\n",
                i, drm->resources->count_connectors, chosen,
                c->connector_id, c->encoder_id, c->connector_type, c->connector_type_id, 
                c->connection, c->mmWidth, c->mmHeight, c->count_modes, c->count_encoders);
            drmModeFreeConnector(c);
        }
    }
    /* find a connected connector: */
    for (i = 0; i < drm->resources->count_connectors; i++) {
        drm->connector = drmModeGetConnector(drm->fd, drm->resources->connectors[i]);
        if( DRM_MODE_CONNECTED == drm->connector->connection ) {
            break;
        } else {
            drmModeFreeConnector(drm->connector);
            drm->connector = NULL;
        }
    }
    if( i >= drm->resources->count_connectors ) {
        /* we could be fancy and listen for hotplug events and wait for
         * a connector..
         */
        ERR_PRINT("EGL_GBM.Display no connected connector (connector count %d, module %s)!\n", 
            drm->resources->count_connectors, module_used);
        goto error;
    }

    /* find highest resolution mode: */
    for (i = 0, j = -1, area = 0; i < drm->connector->count_modes; i++) {
        drmModeModeInfo *current_mode = &drm->connector->modes[i];
        int current_area = current_mode->hdisplay * current_mode->vdisplay;
        if (current_area > area) {
            drm->current_mode = current_mode;
            area = current_area;
            j = i;
        }
        if( verbose ) {
            ERR_PRINT( "EGL_GBM.Display Mode %d/%d (max-chosen %d): clock %d, %dx%d @ %d Hz, type %d, name <%s>\n",
                i, drm->connector->count_modes, j,
                current_mode->clock, current_mode->hdisplay, current_mode->vdisplay, current_mode->vrefresh, 
                current_mode->type, current_mode->name);
        }
    }
    if ( NULL == drm->current_mode ) {
        ERR_PRINT("EGL_GBM.Display could not find mode (module %s)!\n", module_used);
        goto error;
    }

    if( verbose ) {
        for (i = 0; i < drm->resources->count_encoders; i++) {
            drmModeEncoder * e = drmModeGetEncoder(drm->fd, drm->resources->encoders[i]);
            int chosen = e->encoder_id == drm->connector->encoder_id;
            ERR_PRINT( "EGL_GBM.Display Encoder %d/%d chosen %d: id 0x%X, type %d, crtc_id 0x%X, possible[crtcs %d, clones %d]\n",
                i, drm->resources->count_encoders, chosen,
                e->encoder_id, e->encoder_type, e->crtc_id,
                e->possible_crtcs, e->possible_clones);
            drmModeFreeEncoder(e);
        }
    }
    /* find encoder: */
    for (i = 0; i < drm->resources->count_encoders; i++) {
        drm->encoder = drmModeGetEncoder(drm->fd, drm->resources->encoders[i]);
        if( drm->encoder->encoder_id == drm->connector->encoder_id ) {
            break;
        } else {
            drmModeFreeEncoder(drm->encoder);
            drm->encoder = NULL;
        }
    }
    if ( i >= drm->resources->count_encoders ) {
        ERR_PRINT("EGL_GBM.Display no encoder (module %s)!\n", module_used);
        goto error;
    }

    if( verbose ) {
        DBG_PRINT( "EGL_GBM.Display initDrm end.X0 OK\n");
    }

    // drm->crtc_id = encoder->crtc_id;
    // drm->connector_id = connector->connector_id;
    return (jlong) (intptr_t) drm;

error:
    if( verbose ) {
        DBG_PRINT( "EGL_GBM.Display initDrm end.X2 ERROR\n");
    }
    freeDrm(drm);
    return 0;
}

JNIEXPORT void JNICALL Java_jogamp_newt_driver_egl_gbm_DisplayDriver_freeDrm
  (JNIEnv *env, jclass clazz, jlong jdrm) {
    freeDrm( (DRM_HANDLE*) (intptr_t) jdrm );
}

JNIEXPORT jlong JNICALL Java_jogamp_newt_driver_egl_gbm_DisplayDriver_OpenGBMDisplay0
  (JNIEnv *env, jclass clazz, jlong jdrm)
{
    DRM_HANDLE *drm = (DRM_HANDLE*) (intptr_t) jdrm;
    struct gbm_device * dev = gbm_create_device(drm->fd);
    return (jlong) (intptr_t) dev;
}

JNIEXPORT void JNICALL Java_jogamp_newt_driver_egl_gbm_DisplayDriver_CloseGBMDisplay0
  (JNIEnv *env, jclass clazz, jlong jgbm)
{
    struct gbm_device * dev = (struct gbm_device *) (intptr_t) jgbm;
    gbm_device_destroy(dev);
}

JNIEXPORT void JNICALL Java_jogamp_newt_driver_egl_gbm_DisplayDriver_DispatchMessages0
  (JNIEnv *env, jclass clazz)
{
}

/**
 * Screen
 */
JNIEXPORT jboolean JNICALL Java_jogamp_newt_driver_egl_gbm_ScreenDriver_initIDs
  (JNIEnv *env, jclass clazz)
{
    notifyScreenModeID = (*env)->GetMethodID(env, clazz, "notifyScreenMode", "(III)V");
    if (notifyScreenModeID == NULL) {
        DBG_PRINT( "EGL_GBM.Screen initIDs FALSE\n" );
        return JNI_FALSE;
    }
    DBG_PRINT( "EGL_GBM.Screen initIDs ok\n" );
    return JNI_TRUE;
}

JNIEXPORT void JNICALL Java_jogamp_newt_driver_egl_gbm_ScreenDriver_initNative
  (JNIEnv *env, jobject obj, jlong jdrm)
{
    DRM_HANDLE *drm = (DRM_HANDLE*) (intptr_t) jdrm;
    uint32_t screen_width = 0;
    uint32_t screen_height = 0;
    uint32_t screen_vrefresh = 0;
    int32_t success = 0;

    if( NULL != drm ) {
        /**
        connector.modes.hdisplay; // width
        connector.modes.vdisplay; // height
        connector.modes.flags; // flags
        encoder.crtc_id; // crt_idx
        */
        screen_width = drm->current_mode->hdisplay;
        screen_height = drm->current_mode->vdisplay;
        screen_vrefresh = drm->current_mode->vrefresh;

        DBG_PRINT( "EGL_GBM.Screen initNative ok %dx%d @ %d\n", screen_width, screen_height, screen_vrefresh );
        (*env)->CallVoidMethod(env, obj, notifyScreenModeID, (jint) screen_width, (jint) screen_height, (jint) screen_vrefresh);
    } else {
        DBG_PRINT( "BCM.Screen initNative failed\n" );
    }
}

/**
 * Window
 */

JNIEXPORT jboolean JNICALL Java_jogamp_newt_driver_egl_gbm_WindowDriver_initIDs
  (JNIEnv *env, jclass clazz)
{
    DBG_PRINT( "EGL_GBM.Window initIDs ok\n" );
    return JNI_TRUE;
}

JNIEXPORT jlong JNICALL Java_jogamp_newt_driver_egl_gbm_WindowDriver_CreateWindow0
  (JNIEnv *env, jobject obj, jlong jdrm, jlong jgbm, jint x, jint y, jint width, jint height, jboolean opaque, jint alphaBits)
{
    DRM_HANDLE *drm = (DRM_HANDLE*) (intptr_t) jdrm;
    struct gbm_device *dev = (struct gbm_device *) (intptr_t) jgbm;

    struct gbm_surface *surface = gbm_surface_create(dev,
              drm->current_mode->hdisplay, drm->current_mode->vdisplay,
              opaque ? GBM_FORMAT_XRGB8888 : GBM_BO_FORMAT_ARGB8888,
              GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING);
    if ( NULL == surface ) {
        DBG_PRINT("failed to create gbm surface\n");
        return -1;
    }
    return (jlong) (intptr_t) surface;
}

JNIEXPORT void JNICALL Java_jogamp_newt_driver_egl_gbm_WindowDriver_CloseWindow0
  (JNIEnv *env, jobject obj, jlong display, jlong window)
{
    struct gbm_surface *surface = (struct gbm_surface *) (intptr_t) window;
    gbm_surface_destroy(surface);
}