My favorites | Sign in
Project Home Wiki Issues Source
Search
for
GrContext  
Top-level object for using the GPU library
Updated Jan 27, 2012 by bsalo...@google.com

Introduction

GrContext is the public API object you create for talking to the GPU. It manages the GPU context, and related caches for textures and fonts.

Details

Pseudocode for getting started with Gr in Skia.

// Setup your opengl world and have your context bound. 
GrContext* ctx = GrContext::Create(kOpenGL_Shaders_GrEngine, 0);

// We are going to create an SkDevice subclass that uses the GPU. But
// first we need to wrap a GL object ID in a skia-ism to specify the target
// of the drawing.

#if 1 // You want to render to a FBO that you create. You wont be able to
        // read from it (i.e. use it as a src to a skia draw). Skia will need
        // you to have attached a stencil buffer to your FBO.

SkAutoTUnref<GrRenderTarget> target;

// setup framebuffer for Gr to render into (could be FBO 0)
GLint fboID;
//... assign fboID

// The client provides attributes of the FBO so that skia doesn't
// have to perform state queries.
// Skia will assume the attributes are immutable for the life of the
// GrRenderTarget (even though OpenGL would allow these values to change).
GrPlatformRenderTargetDesc targetDesc;
targetDesc.fWidth = width;
targetDesc.fHeight = height;
targetDesc.fConfig = kRGBA_8888_PM_GrPixelConfig;
targetDesc.fSampleCnt = 0; // Number of samples in the color/stencil attachments
targetDesc.fStencilBits = 8;
targetDesc.fRenderTargetHandle = fboID;
target.set(ctx->createPlatformRenderTarget(targetDesc));

#else // You have a texture id and you want Skia to wrap it an FBO in
         // order to draw into it. Because you're giving skia the texture id,
         // skia can also use it as a src to a draw (e.g. passing the result of
         // SkGpuDevice::accessBitmap to SkCanvas::drawBitmap()).

GLint texID;
//... assign texID

GrPlatformTextureDesc texDesc;
SkAutoTUnref<GrTexture> target;
// This flag lets skia know that this texture should be renderable
texDesc.fFlags = kRenderTarget_GrPlatformTextureFlag;
texDesc.fWidth = width;
texDesc.fHeight = height;
texDesc.fConfig = kRGBA_8888_PM_GrPixelConfig;
// this tells skia to create an MSAA buffer to downsample into the 
// texture. Zero means "no MSAA buffer please".
texDesc.fSampleCnt = 0;
texDesc.fTextureHandle = texID;

target.set(ctx->createPlatformTexture(texDesc));

#endif

/**
 * Setup a skia canvas to use the GrContext by installing an SkGpuDevice.
 */

SkCanvas* canvas = newCanvas(...);
SkDevice* device = new SkGpuDevice(ctx, target.get());
canvas->setDevice(device)->unref();

// skia drawing here.
canvas->drawRect(...);

//...

// Maybe your app does its own GL drawing here
glDisable(GL_ALPHA_TEST);
//...
//...
glDrawElements(...)

// Now the GrContext will be confused because the GL context state was 
// modified externally. If we inform it using resetContext() then it won't
// make assumptions about the context state:

ctx->resetContext();

//... skia draws some more to the canvas


// Finished drawing, ready to present, must tell GrContext to flush itself. This
// doesn't call glFlush, it just flushes deferred draws in GrContext to OpenGL.
ctx->flush();


// OS specific way of performing a swap
eglSwapBuffers();
Powered by Google Project Hosting