在通常的iOS应用中,可以通过以下步骤来使用GPU加速的SkCanvas:
EAGLContext *context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
[EAGLContext setCurrentContext:context];
GrContext* grContext = SkGpuDevice::CreateGLContext(context);
GrBackendRenderTargetDesc desc;
desc.fWidth = width;
desc.fHeight = height;
desc.fConfig = kSkia8888_GrPixelConfig;
desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
desc.fSampleCnt = 0;
desc.fStencilBits = 8;
desc.fRenderTargetHandle = 0;
sk_sp<SkSurface> surface(SkSurface::MakeFromBackendRenderTarget(grContext, desc, nullptr));
sk_sp<SkSurface> surface(SkSurface::MakeFromBackendRenderTarget(grContext, desc, nullptr));
sk_sp<SkCanvas> canvas(surface->getCanvas());
canvas->clear(SK_ColorWHITE);
SkPaint paint;
paint.setColor(SK_ColorBLUE);
canvas->drawRect(SkRect::MakeXYWH(0, 0, 100, 100), paint);
CALayer* layer = [[CALayer alloc] init];
layer.bounds = CGRectMake(0, 0, width, height);
layer.position = CGPointMake(width/2, height/2);
layer.contents = (__bridge id) surface->getBackendTexture(false);
[window.layer addSublayer:layer];
通过以上步骤,就可以在通常的iOS应用中使用GPU加速的SkCanvas进行绘图操作了。这样可以提高绘图性能,并且可以实现更复杂的图形效果。
领取专属 10元无门槛券
手把手带您无忧上云