user3810135
user3810135

Reputation: 51

How to use GPU accelerated SkCanvas in usual iOS application (based on UIWindow)?

Is it possible to add OpenGL view to UIWindow and use Skia to draw on it?

I found an example, where we initialize such view:

 - (void)initialize {
     CAEAGLLayer* layer = (CAEAGLLayer*)self.layer;

     layer.opaque = NO;
     layer.drawableProperties = @{kEAGLDrawablePropertyRetainedBacking: @(NO),
                                  kEAGLDrawablePropertyColorFormat: kEAGLColorFormatRGBA8};

     self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
      if (!self.context || ![EAGLContext setCurrentContext:self.context])
         NSLog(@"Cannot create EAGLContext!");

     GLuint framebuffer;
     GLuint renderbuffer;
     glGenFramebuffers(1, &framebuffer);
     glGenRenderbuffers(1, &renderbuffer);
     glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
     glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);

     [self.context renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer*)layer];
     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, renderbuffer);

     GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
     if (status != GL_FRAMEBUFFER_COMPLETE)
         NSLog(@"Failed to make complete frame buffer: %x", status);

     CADisplayLink* displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(drawFrame:)];
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}

- (void)drawFrame:(CADisplayLink*)sender {
    glClearColor(1.0, 0.0, 0.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT);

    [self.context presentRenderbuffer:GL_RENDERBUFFER];
}

Red background is drawn, but I can't make Skia work with this view.

My skia code:

const GrGLInterface* glInterface = GrGLCreateNativeInterface();
NSAssert(glInterface->validate(), nil);

GrContextOptions contextOptions;
GrContext* grContext = GrContext::Create(kOpenGL_GrBackend, (intptr_t)glInterface, contextOptions);
sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named);

GrBackendRenderTargetDesc grBackendRenderTargetDesc;

// This is GLuint framebuffer value from view setup code above.
grBackendRenderTargetDesc.fRenderTargetHandle = self.framebufferID; 

grBackendRenderTargetDesc.fWidth = self.rboWidth;
grBackendRenderTargetDesc.fHeight = self.rboHeight;
grBackendRenderTargetDesc.fConfig = kRGBA_8888_GrPixelConfig;
grBackendRenderTargetDesc.fOrigin = kTopLeft_GrSurfaceOrigin;
grBackendRenderTargetDesc.fSampleCnt = 0;
grBackendRenderTargetDesc.fStencilBits = 0;

SkSurfaceProps skSurfaceProps(0, kUnknown_SkPixelGeometry);
sk_sp<SkSurface> skSurface = SkSurface::MakeFromBackendRenderTarget(grContext, grBackendRenderTargetDesc, &skSurfaceProps);

if (skSurface)
{
    skCanvas = skSurface->getCanvas();

    SkPaint paint;
    paint.setColor(SK_ColorGREEN);
    skCanvas->drawPaint(paint);
}

When I place canvas draw code in [... drawFrame:] before [self.context presentRenderbuffer:GL_RENDERBUFFER], it doesn't draw anything.

What am I missing? How should I link SkSurface/SkCanvas with given EAGLContext and how should I call skia drawing code to make work it correctly?

Upvotes: 2

Views: 882

Answers (1)

user3810135
user3810135

Reputation: 51

It works as expected when I call

skSurface->prepareForExternalIO() 

after all Skia drawing code and before I call

[self.context presentRenderbuffer:GL_RENDERBUFFER];

Upvotes: 2

Related Questions