aboutsummaryrefslogtreecommitdiff
path: root/src/wld/intel/intel.c
blob: 4ee2c2e34100cd5603410d24d8e646b48b863197 (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
/* wld: intel.c
 *
 * Copyright (c) 2013, 2014 Michael Forney
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include "wld/drm-private.h"
#include "wld/drm.h"
#include "batch.h"
#include "blt.h"
#include "wld/wld-private.h"

#include <unistd.h>
#include <intel_bufmgr.h>
#include <i915_drm.h>

struct intel_context
{
    struct wld_context base;
    drm_intel_bufmgr * bufmgr;
};

struct intel_renderer
{
    struct wld_renderer base;
    struct intel_batch batch;
    struct intel_buffer * target;
};

struct intel_buffer
{
    struct buffer base;
    struct wld_exporter exporter;
    drm_intel_bo * bo;
};

#include "../interface/context.h"
#include "../interface/renderer.h"
#include "../interface/buffer.h"
#define DRM_DRIVER_NAME intel
#include "../interface/drm.h"
IMPL(intel_context, wld_context)
IMPL(intel_renderer, wld_renderer)
IMPL(intel_buffer, wld_buffer)

/**** DRM driver ****/
bool driver_device_supported(uint32_t vendor_id, uint32_t device_id)
{
    return vendor_id == 0x8086;
}

struct wld_context * driver_create_context(int drm_fd)
{
    struct intel_context * context;

    context = malloc(sizeof *context);

    if (!context)
        goto error0;

    context_initialize(&context->base, &wld_context_impl);
    context->bufmgr = drm_intel_bufmgr_gem_init(drm_fd, INTEL_BATCH_SIZE);

    if (!context->bufmgr)
        goto error1;

    return &context->base;

  error1:
    free(context);
  error0:
    return NULL;
}

/**** Context ****/
struct wld_renderer * context_create_renderer(struct wld_context * base)
{
    struct intel_context * context = intel_context(base);
    struct intel_renderer * renderer;

    if (!(renderer = malloc(sizeof *renderer)))
        goto error0;

    if (!(intel_batch_initialize(&renderer->batch, context->bufmgr)))
        goto error1;

    renderer_initialize(&renderer->base, &wld_renderer_impl);

    return &renderer->base;

  error1:
    free(renderer);
  error0:
    return NULL;
}

static bool export(struct wld_exporter * exporter, struct wld_buffer * base,
                   uint32_t type, union wld_object * object)
{
    struct intel_buffer * buffer = intel_buffer(base);

    switch (type)
    {
        case WLD_DRM_OBJECT_HANDLE:
            object->u32 = buffer->bo->handle;
            return true;
        case WLD_DRM_OBJECT_PRIME_FD:
            if (drm_intel_bo_gem_export_to_prime(buffer->bo, &object->i) != 0)
                return false;
            return true;
        default:
            return false;
    }
}

static struct buffer * new_buffer(uint32_t width, uint32_t height,
                                  uint32_t format, uint32_t pitch,
                                  drm_intel_bo * bo)
{
    struct intel_buffer * buffer;

    if (!(buffer = malloc(sizeof *buffer)))
        return NULL;

    buffer_initialize(&buffer->base, &wld_buffer_impl,
                      width, height, format, pitch);
    buffer->bo = bo;
    buffer->exporter.export = &export;
    wld_buffer_add_exporter(&buffer->base.base, &buffer->exporter);

    return &buffer->base;
}

struct buffer * context_create_buffer(struct wld_context * base,
                                      uint32_t width, uint32_t height,
                                      uint32_t format, uint32_t flags)
{
    struct intel_context * context = intel_context(base);
    struct buffer * buffer;
    drm_intel_bo * bo;
    uint32_t tiling_mode = width >= 128 ? I915_TILING_X : I915_TILING_NONE;
    unsigned long pitch;

    bo = drm_intel_bo_alloc_tiled(context->bufmgr, "buffer", width, height, 4,
                                  &tiling_mode, &pitch, 0);

    if (!bo)
        goto error0;

    if (!(buffer = new_buffer(width, height, format, pitch, bo)))
        goto error1;

    return buffer;

  error1:
    drm_intel_bo_unreference(bo);
  error0:
    return NULL;
}

struct buffer * context_import_buffer(struct wld_context * base,
                                      uint32_t type, union wld_object object,
                                      uint32_t width, uint32_t height,
                                      uint32_t format, uint32_t pitch)
{
    struct intel_context * context = intel_context(base);
    struct buffer * buffer;
    drm_intel_bo * bo;

    switch (type)
    {
        case WLD_DRM_OBJECT_PRIME_FD:
        {
            uint32_t size = width * height * format_bytes_per_pixel(format);
            bo = drm_intel_bo_gem_create_from_prime(context->bufmgr,
                                                    object.i, size);
            break;
        }
        default: bo = NULL;
    };

    if (!bo)
        goto error0;

    if (!(buffer = new_buffer(width, height, format, pitch, bo)))
        goto error1;

    return buffer;

  error1:
    drm_intel_bo_unreference(bo);
  error0:
    return NULL;
}

void context_destroy(struct wld_context * base)
{
    struct intel_context * context = intel_context(base);

    drm_intel_bufmgr_destroy(context->bufmgr);
    free(context);
}

/**** Renderer ****/
uint32_t renderer_capabilities(struct wld_renderer * renderer,
                               struct buffer * buffer)
{
    if (buffer->base.impl == &wld_buffer_impl)
        return WLD_CAPABILITY_READ | WLD_CAPABILITY_WRITE;

    return 0;
}

bool renderer_set_target(struct wld_renderer * base, struct buffer * buffer)
{
    struct intel_renderer * renderer = intel_renderer(base);

    if (buffer && buffer->base.impl != &wld_buffer_impl)
        return false;

    renderer->target = buffer ? intel_buffer(&buffer->base) : NULL;

    return true;
}

void renderer_fill_rectangle(struct wld_renderer * base, uint32_t color,
                             int32_t x, int32_t y,
                             uint32_t width, uint32_t height)
{
    struct intel_renderer * renderer = intel_renderer(base);
    struct intel_buffer * dst = renderer->target;

    xy_color_blt(&renderer->batch, dst->bo, dst->base.base.pitch,
                 x, y, x + width, y + height, color);
}

void renderer_copy_rectangle(struct wld_renderer * base,
                             struct buffer * buffer_base,
                             int32_t dst_x, int32_t dst_y,
                             int32_t src_x, int32_t src_y,
                             uint32_t width, uint32_t height)
{
    struct intel_renderer * renderer = intel_renderer(base);

    if (buffer_base->base.impl != &wld_buffer_impl)
        return;

    struct intel_buffer * src = intel_buffer(&buffer_base->base),
                        * dst = renderer->target;

    xy_src_copy_blt(&renderer->batch,
                    src->bo, src->base.base.pitch, src_x, src_y,
                    dst->bo, dst->base.base.pitch, dst_x, dst_y, width, height);
}

void renderer_draw_text(struct wld_renderer * base,
                        struct font * font, uint32_t color,
                        int32_t x, int32_t y, const char * text,
                        uint32_t length, struct wld_extents * extents)
{
    struct intel_renderer * renderer = intel_renderer(base);
    struct intel_buffer * dst = renderer->target;
    int ret;
    struct glyph * glyph;
    uint32_t row;
    FT_UInt glyph_index;
    uint32_t c;
    uint8_t immediate[512];
    uint8_t * byte;
    int32_t origin_x = x;

    xy_setup_blt(&renderer->batch, true, BLT_RASTER_OPERATION_SRC,
                 0, color, dst->bo, dst->base.base.pitch);

    if (length == -1)
        length = strlen(text);

    while ((ret = FcUtf8ToUcs4((FcChar8 *) text, &c, length)) > 0 && c != '\0')
    {
        text += ret;
        length -= ret;
        glyph_index = FT_Get_Char_Index(font->face, c);

        if (!font_ensure_glyph(font, glyph_index))
            continue;

        glyph = font->glyphs[glyph_index];

        if (glyph->bitmap.width == 0 || glyph->bitmap.rows == 0)
            goto advance;

        byte = immediate;

        /* XY_TEXT_IMMEDIATE requires a pitch with no extra bytes */
        for (row = 0; row < glyph->bitmap.rows; ++row)
        {
            memcpy(byte, glyph->bitmap.buffer + (row * glyph->bitmap.pitch),
                   (glyph->bitmap.width + 7) / 8);
            byte += (glyph->bitmap.width + 7) / 8;
        }

      retry:
        ret = xy_text_immediate_blt(&renderer->batch, dst->bo,
                                    origin_x + glyph->x, y + glyph->y,
                                    origin_x + glyph->x + glyph->bitmap.width,
                                    y + glyph->y + glyph->bitmap.rows,
                                    (byte - immediate + 3) / 4,
                                    (uint32_t *) immediate);

        if (ret == INTEL_BATCH_NO_SPACE)
        {
            intel_batch_flush(&renderer->batch);
            xy_setup_blt(&renderer->batch, true, BLT_RASTER_OPERATION_SRC,
                         0, color, dst->bo, dst->base.base.pitch);
            goto retry;
        }

      advance:
        origin_x += glyph->advance;
    }

    if (extents)
        extents->advance = origin_x - x;
}

void renderer_flush(struct wld_renderer * base)
{
    struct intel_renderer * renderer = intel_renderer(base);

    intel_batch_flush(&renderer->batch);
}

void renderer_destroy(struct wld_renderer * base)
{
    struct intel_renderer * renderer = intel_renderer(base);

    intel_batch_finalize(&renderer->batch);
    free(renderer);
}

/**** Buffer ****/
bool buffer_map(struct buffer * base)
{
    struct intel_buffer * buffer = intel_buffer(&base->base);

    if (drm_intel_gem_bo_map_gtt(buffer->bo) != 0)
        return false;

    buffer->base.base.map = buffer->bo->virtual;

    return true;
}

bool buffer_unmap(struct buffer * base)
{
    struct intel_buffer * buffer = intel_buffer(&base->base);

    if (drm_intel_gem_bo_unmap_gtt(buffer->bo) != 0)
        return false;

    buffer->base.base.map = NULL;

    return true;
}

void buffer_destroy(struct buffer * base)
{
    struct intel_buffer * buffer = intel_buffer(&base->base);

    drm_intel_bo_unreference(buffer->bo);
    free(buffer);
}