2017-07-13 19:07:54 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2017 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/gpu/mtl/MtlTestContext.h"
|
2017-07-13 19:07:54 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/gpu/GrContext.h"
|
|
|
|
#include "include/gpu/GrContextOptions.h"
|
2017-07-13 19:07:54 +00:00
|
|
|
|
2019-05-08 15:40:55 +00:00
|
|
|
#include "src/gpu/mtl/GrMtlUtil.h"
|
|
|
|
|
2019-05-22 20:23:43 +00:00
|
|
|
#ifdef SK_METAL
|
2017-07-13 19:07:54 +00:00
|
|
|
|
2019-03-15 19:22:39 +00:00
|
|
|
#import <Metal/Metal.h>
|
|
|
|
|
2017-07-13 19:07:54 +00:00
|
|
|
namespace {
|
|
|
|
/**
|
|
|
|
* Implements sk_gpu_test::FenceSync for Metal.
|
2019-05-08 15:40:55 +00:00
|
|
|
*
|
2019-06-04 13:57:14 +00:00
|
|
|
* Fences as MTLSharedEvents are not supported across all Metal platforms, so we do
|
|
|
|
* the next best thing and submit an empty MTLCommandBuffer and track when it's complete.
|
2017-07-13 19:07:54 +00:00
|
|
|
*/
|
|
|
|
class MtlFenceSync : public sk_gpu_test::FenceSync {
|
|
|
|
public:
|
2019-06-04 13:57:14 +00:00
|
|
|
MtlFenceSync(id<MTLCommandQueue> queue)
|
|
|
|
: fQueue(queue) {
|
2017-07-13 19:07:54 +00:00
|
|
|
SkDEBUGCODE(fUnfinishedSyncs = 0;)
|
|
|
|
}
|
|
|
|
|
2019-05-08 15:40:55 +00:00
|
|
|
~MtlFenceSync() override {
|
2017-07-13 19:07:54 +00:00
|
|
|
SkASSERT(!fUnfinishedSyncs);
|
|
|
|
}
|
|
|
|
|
|
|
|
sk_gpu_test::PlatformFence SK_WARN_UNUSED_RESULT insertFence() const override {
|
2019-05-08 15:40:55 +00:00
|
|
|
id<MTLCommandBuffer> cmdBuffer = [fQueue commandBuffer];
|
2019-06-04 13:57:14 +00:00
|
|
|
cmdBuffer.label = @"Fence";
|
2019-05-08 15:40:55 +00:00
|
|
|
[cmdBuffer commit];
|
|
|
|
|
2017-07-13 19:07:54 +00:00
|
|
|
SkDEBUGCODE(++fUnfinishedSyncs;)
|
2019-06-04 13:57:14 +00:00
|
|
|
|
|
|
|
void* cfCmdBuffer = (__bridge_retained void*)cmdBuffer;
|
|
|
|
return (sk_gpu_test::PlatformFence)cfCmdBuffer;
|
2017-07-13 19:07:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool waitFence(sk_gpu_test::PlatformFence opaqueFence) const override {
|
2019-06-04 13:57:14 +00:00
|
|
|
void* cfCmdBuffer = (void*) opaqueFence;
|
|
|
|
id<MTLCommandBuffer> cmdBuffer = (__bridge id<MTLCommandBuffer>) cfCmdBuffer;
|
2019-05-08 15:40:55 +00:00
|
|
|
|
2019-06-04 13:57:14 +00:00
|
|
|
[cmdBuffer waitUntilCompleted];
|
2019-05-08 15:40:55 +00:00
|
|
|
|
2019-06-04 13:57:14 +00:00
|
|
|
return (MTLCommandBufferStatusError != cmdBuffer.status);
|
2017-07-13 19:07:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void deleteFence(sk_gpu_test::PlatformFence opaqueFence) const override {
|
2019-06-04 13:57:14 +00:00
|
|
|
CFRelease((void*) opaqueFence);
|
2017-07-13 19:07:54 +00:00
|
|
|
SkDEBUGCODE(--fUnfinishedSyncs;)
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-05-08 15:40:55 +00:00
|
|
|
id<MTLCommandQueue> fQueue;
|
2017-07-13 19:07:54 +00:00
|
|
|
SkDEBUGCODE(mutable int fUnfinishedSyncs;)
|
|
|
|
typedef sk_gpu_test::FenceSync INHERITED;
|
|
|
|
};
|
|
|
|
|
2019-12-18 15:43:10 +00:00
|
|
|
static_assert(sizeof(uint64_t) <= sizeof(sk_gpu_test::PlatformFence));
|
2017-07-13 19:07:54 +00:00
|
|
|
|
2019-03-15 19:22:39 +00:00
|
|
|
class MtlTestContextImpl : public sk_gpu_test::MtlTestContext {
|
2017-07-13 19:07:54 +00:00
|
|
|
public:
|
2019-03-15 19:22:39 +00:00
|
|
|
static MtlTestContext* Create(MtlTestContext* sharedContext) {
|
|
|
|
id<MTLDevice> device;
|
|
|
|
id<MTLCommandQueue> queue;
|
|
|
|
if (sharedContext) {
|
|
|
|
MtlTestContextImpl* sharedContextImpl = (MtlTestContextImpl*) sharedContext;
|
|
|
|
device = sharedContextImpl->device();
|
|
|
|
queue = sharedContextImpl->queue();
|
|
|
|
} else {
|
|
|
|
device = MTLCreateSystemDefaultDevice();
|
|
|
|
queue = [device newCommandQueue];
|
|
|
|
}
|
|
|
|
|
|
|
|
return new MtlTestContextImpl(device, queue);
|
2017-07-13 19:07:54 +00:00
|
|
|
}
|
|
|
|
|
2019-03-15 19:22:39 +00:00
|
|
|
~MtlTestContextImpl() override { this->teardown(); }
|
2017-07-13 19:07:54 +00:00
|
|
|
|
|
|
|
void testAbandon() override {}
|
|
|
|
|
2019-06-04 13:57:14 +00:00
|
|
|
// There is really nothing to do here since we don't own any unqueued command buffers here.
|
2017-07-13 19:07:54 +00:00
|
|
|
void submit() override {}
|
|
|
|
|
|
|
|
void finish() override {}
|
|
|
|
|
|
|
|
sk_sp<GrContext> makeGrContext(const GrContextOptions& options) override {
|
2019-05-31 13:10:55 +00:00
|
|
|
return GrContext::MakeMetal((__bridge void*)fDevice,
|
|
|
|
(__bridge void*)fQueue,
|
2019-05-28 03:03:45 +00:00
|
|
|
options);
|
2017-07-13 19:07:54 +00:00
|
|
|
}
|
|
|
|
|
2019-03-15 19:22:39 +00:00
|
|
|
id<MTLDevice> device() { return fDevice; }
|
|
|
|
id<MTLCommandQueue> queue() { return fQueue; }
|
|
|
|
|
2017-07-13 19:07:54 +00:00
|
|
|
private:
|
2019-03-15 19:22:39 +00:00
|
|
|
MtlTestContextImpl(id<MTLDevice> device, id<MTLCommandQueue> queue)
|
|
|
|
: INHERITED(), fDevice(device), fQueue(queue) {
|
2019-06-04 13:57:14 +00:00
|
|
|
fFenceSync.reset(new MtlFenceSync(queue));
|
|
|
|
}
|
2017-07-13 19:07:54 +00:00
|
|
|
|
|
|
|
void onPlatformMakeCurrent() const override {}
|
2017-11-17 14:25:23 +00:00
|
|
|
std::function<void()> onPlatformGetAutoContextRestore() const override { return nullptr; }
|
2017-07-13 19:07:54 +00:00
|
|
|
void onPlatformSwapBuffers() const override {}
|
|
|
|
|
2019-03-15 19:22:39 +00:00
|
|
|
id<MTLDevice> fDevice;
|
2017-07-13 19:07:54 +00:00
|
|
|
id<MTLCommandQueue> fQueue;
|
|
|
|
|
2019-03-15 19:22:39 +00:00
|
|
|
typedef sk_gpu_test::MtlTestContext INHERITED;
|
2017-07-13 19:07:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
namespace sk_gpu_test {
|
|
|
|
|
2019-03-15 19:22:39 +00:00
|
|
|
MtlTestContext* CreatePlatformMtlTestContext(MtlTestContext* sharedContext) {
|
|
|
|
return MtlTestContextImpl::Create(sharedContext);
|
2017-07-13 19:07:54 +00:00
|
|
|
}
|
2019-03-15 19:22:39 +00:00
|
|
|
|
2017-07-13 19:07:54 +00:00
|
|
|
} // namespace sk_gpu_test
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|