summaryrefslogtreecommitdiffstatsci
path: root/src/skia-egl.cpp
blob: 7a24e29d8482977dc58734a60b3a5b9c67e4a8fb (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
/*
 * Copyright 2014 © Samsung Research America, Silicon Valley
 *
 * Use of this source code is governed by the 3-Clause BSD license
 * specified in the COPYING file included with this source code.
 */
#include <config.h>

#include <err.h>
#include <stdlib.h>

#include <GrGLInterface.h>
#include <GrRenderTarget.h>
#include <GrContext.h>
#include <SkGpuDevice.h>

#include <EGL/egl.h>
#if HAVE_GLES3_H
#include <GLES3/gl3.h>
#elif HAVE_GLES2_H
#include <GLES2/gl2.h>
#endif

#include "egl.h"
#include "device_config.h"

static egl_state_t *state;
static GrContext* ctx;

SkBaseDevice *
create_skia_device_egl (const device_config_t& config)
{
    GrBackendRenderTargetDesc desc;
    GrRenderTarget* target;
    SkGpuDevice *device;

    state = (egl_state_t*) malloc (sizeof (egl_state_t));
    if (!state) {
        warnx ("Out of memory\n");
        return NULL;
    }

    if (!createEGLContextAndWindow(state, config)) {
        cleanup_state_egl(state);
        return NULL;
    }

    eglMakeCurrent(state->egl_display, state->egl_surface, state->egl_surface, state->egl_context);

    desc.fWidth = config.width;
    desc.fHeight = config.height;
    desc.fConfig = kSkia8888_GrPixelConfig;
    desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
    desc.fStencilBits = 1;
    desc.fRenderTargetHandle = 0;
    if (config.egl_sample_buffers > 0)
        desc.fSampleCnt = config.egl_samples;
    else
        desc.fSampleCnt = 0;

    ctx = GrContext::Create(kOpenGL_GrBackend, 0);
    if (!ctx) {
        cleanup_state_egl(state);
        errx(-1, "Could not establish a graphics context for Skia EGL\n");
    }

    target = ctx->wrapBackendRenderTarget(desc);
    device = new SkGpuDevice(ctx, target);

    glClearColor(1, 1, 1, 1);
    glClear(GL_COLOR_BUFFER_BIT);

    return device;
}

void
destroy_skia_egl(void)
{
    delete ctx;
    cleanup_state_egl(state);
}

void
update_skia_egl(void)
{
    ctx->flush();
    eglSwapBuffers(state->egl_display, state->egl_surface);
}

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :