Skip to content

Instantly share code, notes, and snippets.

@ReneHollander
Created December 29, 2016 16:08
Show Gist options
  • Save ReneHollander/0d3025d26deb8e989270d80657233841 to your computer and use it in GitHub Desktop.
Save ReneHollander/0d3025d26deb8e989270d80657233841 to your computer and use it in GitHub Desktop.
Skia on the Raspberry Pi
#include <iostream>
#include <assert.h>
#include <stdio.h>
#include <cstdio>
#include <bcm_host.h>
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#include "GrContext.h"
#include "gl/GrGLInterface.h"
#include "SkData.h"
#include "SkImage.h"
#include "SkStream.h"
#include "SkSurface.h"
#include "SkCanvas.h"
#include "SkPaint.h"
typedef struct {
uint32_t screen_width;
uint32_t screen_height;
EGLDisplay display;
EGLSurface surface;
EGLContext context;
} CUBE_STATE_T;
static CUBE_STATE_T _state, *state=&_state;
static void init_ogl(CUBE_STATE_T *state) {
int32_t success = 0;
EGLBoolean result;
EGLint num_config;
static EGL_DISPMANX_WINDOW_T nativewindow;
DISPMANX_ELEMENT_HANDLE_T dispman_element;
DISPMANX_DISPLAY_HANDLE_T dispman_display;
DISPMANX_UPDATE_HANDLE_T dispman_update;
VC_RECT_T dst_rect;
VC_RECT_T src_rect;
static const EGLint attribute_list[] = {
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_ALPHA_SIZE, 8,
EGL_DEPTH_SIZE, 0,
EGL_STENCIL_SIZE, 8,
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
EGL_NONE
};
const EGLint context_attrib_list[] = {
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
EGLConfig config;
state->display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
assert(state->display!=EGL_NO_DISPLAY);
result = eglInitialize(state->display, NULL, NULL);
assert(EGL_FALSE != result);
result = eglChooseConfig(state->display, attribute_list, &config, 1, &num_config);
assert(EGL_FALSE != result);
result = eglBindAPI(EGL_OPENGL_ES_API);
assert(EGL_FALSE != result);
state->context = eglCreateContext(state->display, config, EGL_NO_CONTEXT, context_attrib_list);
assert(state->context!=EGL_NO_CONTEXT);
success = graphics_get_display_size(0, &state->screen_width, &state->screen_height);
assert( success >= 0 );
dst_rect.x = 0;
dst_rect.y = 0;
dst_rect.width = state->screen_width;
dst_rect.height = state->screen_height;
src_rect.x = 0;
src_rect.y = 0;
src_rect.width = state->screen_width << 16;
src_rect.height = state->screen_height << 16;
dispman_display = vc_dispmanx_display_open( 0 /* LCD */);
dispman_update = vc_dispmanx_update_start( 0 );
dispman_element = vc_dispmanx_element_add ( dispman_update, dispman_display, 0, &dst_rect, 0, &src_rect, DISPMANX_PROTECTION_NONE, 0, 0, DISPMANX_NO_ROTATE);
nativewindow.element = dispman_element;
nativewindow.width = state->screen_width;
nativewindow.height = state->screen_height;
vc_dispmanx_update_submit_sync( dispman_update );
state->surface = eglCreateWindowSurface( state->display, config, &nativewindow, NULL );
assert(state->surface != EGL_NO_SURFACE);
result = eglMakeCurrent(state->display, state->surface, state->surface, state->context);
assert(EGL_FALSE != result);
}
static void exit_func(void) {
eglMakeCurrent( state->display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
eglDestroySurface( state->display, state->surface );
eglDestroyContext( state->display, state->context );
eglTerminate( state->display );
bcm_host_deinit();
}
int main(int argc, char *argv[]) {
bcm_host_init();
memset( state, 0, sizeof( *state ) );
init_ogl(state);
printf("EGL Information:\n");
printf("\tVendor:\t\t%s\n", eglQueryString(state->display, EGL_VENDOR));
printf("\tVersion:\t%s\n", eglQueryString(state->display, EGL_VERSION));
printf("\tClient APIs:\t%s\n", eglQueryString(state->display, EGL_CLIENT_APIS));
printf("\tExtensions:\t%s\n", eglQueryString(state->display, EGL_EXTENSIONS));
printf("\n");
printf("OpenGL Information:\n");
printf("\tVendor:\t\t%s\n", glGetString(GL_VENDOR));
printf("\tRenderer:\t%s\n", glGetString(GL_RENDERER));
printf("\tVersion:\t%s\n", glGetString(GL_VERSION));
printf("\tExtensions:\t%s\n", glGetString(GL_EXTENSIONS));
printf("\n");
glViewport(0, 0, (GLsizei) state->screen_width, (GLsizei) state->screen_height);
glClearColor(1, 0, 0, 1);
glClearStencil(0);
glStencilMask(0xffffffff);
glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
sk_sp<const GrGLInterface> interface(GrGLCreateNativeInterface());
sk_sp<GrContext> grContext(GrContext::Create(kOpenGL_GrBackend, (GrBackendContext)interface.get()));
GrBackendRenderTargetDesc desc;
desc.fWidth = state->screen_width;
desc.fHeight = state->screen_height;
desc.fConfig = kSkia8888_GrPixelConfig;
desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
desc.fSampleCnt = 0;
desc.fStencilBits = 8;
desc.fRenderTargetHandle = 0;
SkSurface *surface = SkSurface::MakeFromBackendRenderTarget(grContext.release(), desc, nullptr, nullptr).release();
SkCanvas *canvas = surface->getCanvas();
canvas->clear(SK_ColorBLUE);
// SkRect rect = SkRect::MakeXYWH(0, 0, 100, 100);
// SkPaint paint;
// paint.setColor(SK_ColorBLUE);
// canvas->drawRect(rect, paint);
canvas->flush();
eglSwapBuffers(state->display, state->surface);
getchar();
exit_func();
printf("Finished!\n");
return 0;
}
EGL Information:
Vendor: Broadcom
Version: 1.4
Client APIs: OpenGL_ES OpenVG
Extensions: EGL_KHR_image EGL_KHR_image_base EGL_KHR_image_pixmap EGL_KHR_vg_parent_image EGL_KHR_gl_texture_2D_image EGL_KHR_gl_texture_cubemap_image EGL_KHR_lock_surface
OpenGL Information:
Vendor: Broadcom
Renderer: VideoCore IV HW
Version: OpenGL ES 2.0
Extensions: GL_OES_compressed_ETC1_RGB8_texture GL_OES_compressed_paletted_texture GL_OES_texture_npot GL_OES_depth24 GL_OES_vertex_half_float GL_OES_EGL_image GL_OES_EGL_image_external GL_EXT_discard_framebuffer GL_OES_rgb8_rgba8 GL_OES_depth32 GL_OES_mapbuffer GL_EXT_texture_format_BGRA8888 GL_APPLE_rgb_422 GL_EXT_debug_marker
ERROR:LEX/PARSE-2 (vertex shader, line 12) Undefined identifier../../src/gpu/gl/builders/GrGLProgramBuilder.cpp:207: fatal error: "Error linking program"
Aborted
EGL Information:
Vendor: Broadcom
Version: 1.4
Client APIs: OpenGL_ES OpenVG
Extensions: EGL_KHR_image EGL_KHR_image_base EGL_KHR_image_pixmap EGL_KHR_vg_parent_image EGL_KHR_gl_texture_2D_image EGL_KHR_gl_texture_cubemap_image EGL_KHR_lock_surface
OpenGL Information:
Vendor: Broadcom
Renderer: VideoCore IV HW
Version: OpenGL ES 2.0
Extensions: GL_OES_compressed_ETC1_RGB8_texture GL_OES_compressed_paletted_texture GL_OES_texture_npot GL_OES_depth24 GL_OES_vertex_half_float GL_OES_EGL_image GL_OES_EGL_image_external GL_EXT_discard_framebuffer GL_OES_rgb8_rgba8 GL_OES_depth32 GL_OES_mapbuffer GL_EXT_texture_format_BGRA8888 GL_APPLE_rgb_422 GL_EXT_debug_marker
Finished!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment