如何在通常的iOS应用程序中使用GPU加速的SkCanvas(基于UIWindow)



是否可以将OpenGL视图添加到UIWindow并使用Skia在其上绘制?

我找到了一个示例,我们在其中初始化了这样的视图:

 - (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];
}

绘制了红色背景,但我无法使 Skia 使用此视图。

我的滑雪板代码:

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);
}

当我将画布绘制代码放入 [...drawFrame:] 在 [self.context presentRenderbuffer:GL_RENDERBUFFER] 之前,它不会绘制任何内容。

我错过了什么?我应该如何将SkSurface/SkCanvas与给定的EAGLContext链接起来,我应该如何调用skia绘图代码来使其正常工作?

当我

调用时它按预期工作

skSurface->prepareForExternalIO() 

毕竟 Skia 绘图代码和我打电话之前

[self.context presentRenderbuffer:GL_RENDERBUFFER];

相关内容

  • 没有找到相关文章

最新更新