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

如何在通常的iOS应用程序(基于UIWindow)中使用GPU加速SkCanvas?,ios,opengl-es,skia,Ios,Opengl Es,Skia,是否可以将OpenGL视图添加到UIWindow并使用Skia在其上绘制 我找到了一个例子,我们初始化了这样的视图: - (void)initialize { CAEAGLLayer* layer = (CAEAGLLayer*)self.layer; layer.opaque = NO; layer.drawableProperties = @{kEAGLDrawablePropertyRetainedBacking: @(NO),

是否可以将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使用此视图

我的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);
}
const GrGLInterface*glInterface=GrGLCreateNativeInterface();
NSAssert(glInterface->validate(),nil);
GrContextOptions contextOptions;
GrContext*GrContext=GrContext::Create(kOpenGL_GrBackend,(intptr_t)glInterface,contextOptions);
sk_sp colorSpace=SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named);
GrBackendRenderTargetDesc GrBackendRenderTargetDesc;
//这是上面视图设置代码中的GLuint帧缓冲区值。
grbackendRenderTargetDescr.fRenderTargetHandle=self.framebufferID;
grBackendRenderTargetDesc.fWidth=self.width;
grBackendRenderTargetDesc.fHight=self.rboHeight;
grBackendRenderTargetDesc.fConfig=kRGBA_8888_GrPixelConfig;
grBackendRenderTargetDesc.fOrigin=kTopLeft\u GrSurfaceOrigin;
grBackendRenderTargetDesc.fSampleCnt=0;
grBackendRenderTargetDesc.fStencilBits=0;
SkSurfaceProps SkSurfaceProps(0,kUnknown_SkPixelGeometry);
sk_sp skSurface=skSurface::MakeFromBackendRenderTarget(grContext、grBackendRenderTargetDesc和skSurfaceProps);
如果(表面)
{
skCanvas=skSurface->getCanvas();
油漆;
油漆。设置颜色(SK_颜色绿色);
skCanvas->drawPaint(油漆);
}
当我将画布绘制代码放在[self.context presentRenderbuffer:GL_RENDERBUFFER]之前的[…drawFrame:]中时,它不会绘制任何东西


我错过了什么?如何将SkSurface/SkCanvas与给定的EagleContext链接,以及如何调用skia绘图代码使其正确工作?

调用

skSurface->prepareForExternalIO() 
毕竟是Skia绘图代码,在我打电话之前

[self.context presentRenderbuffer:GL_RENDERBUFFER];