是否可以将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);
}
当我在[self.context presentRenderbuffer:GL_RENDERBUFFER]之前的[... drawFrame:]中放置画布绘制代码时,它不会绘制任何内容。
我想念什么?如何将SkSurface / SkCanvas与给定的EAGLContext链接,如何调用Skia绘图代码以使其正常工作?
最佳答案
当我打电话时它按预期工作
skSurface->prepareForExternalIO()
毕竟,Skia绘图代码是在我致电之前
[self.context presentRenderbuffer:GL_RENDERBUFFER];
关于ios - 如何在普通的iOS应用程序(基于UIWindow)中使用GPU加速的SkCanvas?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42096415/