Skip to content

Instantly share code, notes, and snippets.

@zester
Created March 14, 2013 17:27
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zester/5163313 to your computer and use it in GitHub Desktop.
Save zester/5163313 to your computer and use it in GitHub Desktop.
Using Skia With GLFW (Or any other framework that provides an OpenGL context [GLUT,SDL,etc...])
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include "glfw/glfw.h"
#include "skia/include/gpu/GrContext.h"
#include "skia/include/gpu/GrRenderTarget.h"
#include "skia/include/gpu/GrGLInterface.h"
#include "skia/include/gpu/SkGpuDevice.h"
#include "skia/include/gpu/SkGpuCanvas.h"
#include "skia/include/gpu/SkNativeGLContext.h"
#include "skia/include/core/SkCanvas.h"
#include "skia/include/core/SkGraphics.h"
int main( int ac,char** av )
{
SkAutoGraphics ag;
int window_width = 800,
window_height = 600;
//*
if( glfwInit() == GL_FALSE )
{
printf( "Could not initialize GLFW. Aborting.\n" );
exit( 0 );
}
// This function calls wglMakeCurrent somewhere in its execution, so make sure that (or its platform specific equivalent) it is called when porting to other frameworks.
if (glfwOpenWindow(window_width, window_height, 8,8,8,8,24,8, GLFW_WINDOW) == GL_FALSE)
{
printf( "Could not open GLFW window. Aborting.\n" );
exit( 0 );
}
// */
const GrGLInterface* fGL = GrGLCreateNativeInterface();
GrContext* fGrContext = GrContext::Create( kOpenGL_Shaders_GrEngine,(GrPlatform3DContext)fGL );
if( fGrContext == 0x0 )
{
printf("\nfGrContext was null");
exit( 0 );
}
GrRenderTarget* fGrRenderTarget;)
GrPlatformRenderTargetDesc desc;)
desc.fWidth = window_width;)
desc.fHeight = window_height;)
desc.fConfig = kSkia8888_PM_GrPixelConfig;)
GR_GL_GetIntegerv(fGL, GR_GL_SAMPLES, &desc.fSampleCnt);)
GR_GL_GetIntegerv(fGL, GR_GL_STENCIL_BITS, &desc.fStencilBits);)
GrGLint buffer;)
GR_GL_GetIntegerv(fGL, GR_GL_FRAMEBUFFER_BINDING, &buffer);)
desc.fRenderTargetHandle = buffer;
// I had some serious trouble with segmentation faults and failures arising from this function. Perhaps some more robust error checking is in order?
fGrRenderTarget = fGrContext->createPlatformRenderTarget(desc);
// Canvi (Canvasses?) only really care about the device they contain, so you can ignore the SkGpuCanvas.
SkCanvas* gpuCanvas = new SkCanvas();
gpuCanvas->setDevice(new SkGpuDevice(fGrContext,fGrRenderTarget))->unref();
glfwSetWindowTitle( "Skia GLFW Test" );
while( true )
{
// Draw a red background with a gray, semi-transparent circle in the top left corner.
gpuCanvas->drawColor( SK_ColorRED );
SkRect r(SkRect::MakeWH(200,200));
SkPaint p;
p.setARGB( 200,100,100,100 );
gpuCanvas->drawOval( r,p );
glfwSwapBuffers();
fGrContext->flush(false);
Sleep( 1 );
}
return 1;
}
@ad8e
Copy link

ad8e commented Feb 7, 2018

This gist no longer works because skia's API has changed, so users may be interested in a more recent bootstrap (Feb 2018): https://gist.github.com/ad8e/dd150b775ae6aa4d5cf1a092e4713add

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment