diff options
author | Greg Daniel <egdaniel@google.com> | 2017-07-13 15:07:54 -0400 |
---|---|---|
committer | Skia Commit-Bot <skia-commit-bot@chromium.org> | 2017-07-13 21:29:33 +0000 |
commit | b76a72a659f53b8bf449c5ca2685045d08a21e43 (patch) | |
tree | cc2e1b3c997817f514438e79c248d3b890e65ef7 /src | |
parent | ee3e0bacc8bedd1e3ebfb52fbc8b1cc3ccdad358 (diff) |
Add support for creating a GrContext backed by Metal.
Also adds the support code to allow our TestContext to create a Metal
backend.
Bug: skia:
Change-Id: Ia850687019d79b897bb16e2c151f4f8526721ad9
Reviewed-on: https://skia-review.googlesource.com/22644
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Greg Daniel <egdaniel@google.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/gpu/GrContext.cpp | 23 | ||||
-rw-r--r-- | src/gpu/GrGpuFactory.cpp | 7 | ||||
-rw-r--r-- | src/gpu/mtl/GrMtlGpu.h | 24 | ||||
-rw-r--r-- | src/gpu/mtl/GrMtlGpu.mm | 29 | ||||
-rw-r--r-- | src/gpu/mtl/GrMtlTrampoline.h | 6 | ||||
-rw-r--r-- | src/gpu/mtl/GrMtlTrampoline.mm | 10 |
6 files changed, 68 insertions, 31 deletions
diff --git a/src/gpu/GrContext.cpp b/src/gpu/GrContext.cpp index 8cad6be20b..2f3c30d997 100644 --- a/src/gpu/GrContext.cpp +++ b/src/gpu/GrContext.cpp @@ -28,6 +28,10 @@ #include "effects/GrConfigConversionEffect.h" #include "text/GrTextBlobCache.h" +#ifdef SK_METAL +#include "mtl/GrMtlTrampoline.h" +#endif + #define ASSERT_OWNED_PROXY(P) \ SkASSERT(!(P) || !((P)->priv().peekTexture()) || (P)->priv().peekTexture()->getContext() == this) #define ASSERT_OWNED_PROXY_PRIV(P) \ @@ -61,6 +65,21 @@ GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext, return context.release(); } +#ifdef SK_METAL +sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue, const GrContextOptions& options) { + sk_sp<GrContext> context(new GrContext); + context->fGpu = GrMtlTrampoline::CreateGpu(context.get(), options, device, queue); + if (!context->fGpu) { + return nullptr; + } + context->fBackend = kMetal_GrBackend; + if (!context->init(options)) { + return nullptr; + } + return context; +} +#endif + static int32_t gNextID = 1; static int32_t next_id() { int32_t id; @@ -89,7 +108,11 @@ bool GrContext::init(GrBackend backend, GrBackendContext backendContext, if (!fGpu) { return false; } + return this->init(options); +} +bool GrContext::init(const GrContextOptions& options) { + ASSERT_SINGLE_OWNER fCaps = SkRef(fGpu->caps()); fResourceCache = new GrResourceCache(fCaps, fUniqueID); fResourceProvider = new GrResourceProvider(fGpu, fResourceCache, &fSingleOwner); diff --git a/src/gpu/GrGpuFactory.cpp b/src/gpu/GrGpuFactory.cpp index 7cfec4a900..1fc19c144a 100644 --- a/src/gpu/GrGpuFactory.cpp +++ b/src/gpu/GrGpuFactory.cpp @@ -12,9 +12,6 @@ #ifdef SK_VULKAN #include "vk/GrVkGpu.h" #endif -#ifdef SK_METAL -#include "mtl/GrMtlTrampoline.h" -#endif GrGpu* GrGpu::Create(GrBackend backend, GrBackendContext backendContext, @@ -27,10 +24,6 @@ GrGpu* GrGpu::Create(GrBackend backend, case kVulkan_GrBackend: return GrVkGpu::Create(backendContext, options, context); #endif -#ifdef SK_METAL - case kMetal_GrBackend: - return GrMtlTrampoline::CreateGpu(backendContext, options, context); -#endif case kMock_GrBackend: return GrMockGpu::Create(backendContext, options, context); default: diff --git a/src/gpu/mtl/GrMtlGpu.h b/src/gpu/mtl/GrMtlGpu.h index 7ec9907675..b63f9080be 100644 --- a/src/gpu/mtl/GrMtlGpu.h +++ b/src/gpu/mtl/GrMtlGpu.h @@ -16,11 +16,12 @@ #import <Metal/Metal.h> class GrSemaphore; +struct GrMtlBackendContext; class GrMtlGpu : public GrGpu { public: - static GrGpu* Create(GrBackendContext backendContext, const GrContextOptions& options, - GrContext* context); + static GrGpu* Create(GrContext* context, const GrContextOptions& options, + id<MTLDevice> device, id<MTLCommandQueue> queue); ~GrMtlGpu() override {} @@ -59,19 +60,8 @@ public: sk_sp<GrSemaphore> prepareTextureForCrossContextUsage(GrTexture*) override { return nullptr; } private: - GrMtlGpu(GrContext* context, const GrContextOptions& options) : INHERITED(context) { - id<MTLDevice> device = MTLCreateSystemDefaultDevice(); - - MTLTextureDescriptor* txDesc = [[MTLTextureDescriptor alloc] init]; - txDesc.textureType = MTLTextureType3D; - txDesc.height = 64; - txDesc.width = 64; - txDesc.depth = 64; - txDesc.pixelFormat = MTLPixelFormatBGRA8Unorm; - txDesc.arrayLength = 1; - txDesc.mipmapLevelCount = 1; - fTestHeaderTexture = [device newTextureWithDescriptor:txDesc]; - } + GrMtlGpu(GrContext* context, const GrContextOptions& options, + id<MTLDevice> device, id<MTLCommandQueue> queue); void onResetContext(uint32_t resetBits) override {} @@ -146,7 +136,9 @@ private: bool isTestingOnlyBackendTexture(GrBackendObject ) const override { return false; } void deleteTestingOnlyBackendTexture(GrBackendObject, bool abandonTexture) override {} - id<MTLTexture> fTestHeaderTexture; + id<MTLDevice> fDevice; + id<MTLCommandQueue> fQueue; + typedef GrGpu INHERITED; }; diff --git a/src/gpu/mtl/GrMtlGpu.mm b/src/gpu/mtl/GrMtlGpu.mm index 7d9339cf29..986bb8a6f7 100644 --- a/src/gpu/mtl/GrMtlGpu.mm +++ b/src/gpu/mtl/GrMtlGpu.mm @@ -11,8 +11,31 @@ #error This file must be compiled with Arc. Use -fobjc-arc flag #endif -GrGpu* GrMtlGpu::Create(GrBackendContext backendContext, const GrContextOptions& options, - GrContext* context) { - return nullptr; +GrGpu* GrMtlGpu::Create(GrContext* context, const GrContextOptions& options, + id<MTLDevice> device, id<MTLCommandQueue> queue) { + if (!device || !queue) { + return nullptr; + } + return new GrMtlGpu(context, options, device, queue); } +GrMtlGpu::GrMtlGpu(GrContext* context, const GrContextOptions& options, + id<MTLDevice> device, id<MTLCommandQueue> queue) + : INHERITED(context) + , fDevice(device) + , fQueue(queue) { + MTLTextureDescriptor* txDesc = [[MTLTextureDescriptor alloc] init]; + txDesc.textureType = MTLTextureType3D; + txDesc.height = 64; + txDesc.width = 64; + txDesc.depth = 64; + txDesc.pixelFormat = MTLPixelFormatRGBA8Unorm; + txDesc.arrayLength = 1; + txDesc.mipmapLevelCount = 1; + id<MTLTexture> testTexture = [fDevice newTextureWithDescriptor:txDesc]; + // To get ride of unused var warning + int width = [testTexture width]; + SkDebugf("width: %d\n", width); + // Unused queue warning fix + SkDebugf("ptr to queue: %p\n", fQueue); +} diff --git a/src/gpu/mtl/GrMtlTrampoline.h b/src/gpu/mtl/GrMtlTrampoline.h index acce98cd1f..531a4ce00c 100644 --- a/src/gpu/mtl/GrMtlTrampoline.h +++ b/src/gpu/mtl/GrMtlTrampoline.h @@ -20,8 +20,10 @@ struct GrContextOptions; */ class GrMtlTrampoline { public: - static GrGpu* CreateGpu(GrBackendContext backendContext, const GrContextOptions& options, - GrContext* context); + static GrGpu* CreateGpu(GrContext* context, + const GrContextOptions& options, + void* device, + void* queue); }; #endif diff --git a/src/gpu/mtl/GrMtlTrampoline.mm b/src/gpu/mtl/GrMtlTrampoline.mm index ea91ebb827..0f112e4da5 100644 --- a/src/gpu/mtl/GrMtlTrampoline.mm +++ b/src/gpu/mtl/GrMtlTrampoline.mm @@ -9,9 +9,13 @@ #include "GrMtlGpu.h" -GrGpu* GrMtlTrampoline::CreateGpu(GrBackendContext backendContext, +GrGpu* GrMtlTrampoline::CreateGpu(GrContext* context, const GrContextOptions& options, - GrContext* context) { - return GrMtlGpu::Create(backendContext, options, context); + void* device, + void* queue) { + return GrMtlGpu::Create(context, + options, + (__bridge_transfer id<MTLDevice>)device, + (__bridge_transfer id<MTLCommandQueue>)queue); } |