1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "gpu/command_buffer/service/metal_context_provider.h"
#include <Foundation/Foundation.h>
#import <Metal/Metal.h>
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "components/metal_util/device.h"
#include "gpu/command_buffer/service/graphite_shared_context.h"
#include "gpu/config/gpu_finch_features.h"
#include "third_party/skia/include/gpu/graphite/Context.h"
#include "third_party/skia/include/gpu/graphite/mtl/MtlBackendContext.h"
#include "third_party/skia/include/gpu/graphite/mtl/MtlGraphiteUtils.h"
namespace viz {
struct MetalContextProvider::ObjCStorage {
id<MTLDevice> __strong device;
std::unique_ptr<gpu::GraphiteSharedContext> graphite_shared_context;
};
MetalContextProvider::MetalContextProvider(id<MTLDevice> device)
: objc_storage_(std::make_unique<ObjCStorage>()) {
objc_storage_->device = device;
CHECK(objc_storage_->device);
}
MetalContextProvider::~MetalContextProvider() = default;
// static
std::unique_ptr<MetalContextProvider> MetalContextProvider::Create() {
// First attempt to find a low power device to use.
id<MTLDevice> device = metal::GetDefaultDevice();
if (!device) {
DLOG(ERROR) << "Failed to find MTLDevice.";
return nullptr;
}
return base::WrapUnique(new MetalContextProvider(std::move(device)));
}
bool MetalContextProvider::InitializeGraphiteContext(
const skgpu::graphite::ContextOptions& options,
gpu::GpuProcessShmCount* use_shader_cache_shm_count) {
CHECK(!objc_storage_->graphite_shared_context);
CHECK(objc_storage_->device);
skgpu::graphite::MtlBackendContext backend_context = {};
// ARC note: MtlBackendContext contains two owning smart pointers of CFTypeRef
// so give them owning references.
backend_context.fDevice.reset(CFBridgingRetain(objc_storage_->device));
backend_context.fQueue.reset(
CFBridgingRetain([objc_storage_->device newCommandQueue]));
std::unique_ptr<skgpu::graphite::Context> graphite_context =
skgpu::graphite::ContextFactory::MakeMetal(backend_context, options);
if (!graphite_context) {
DLOG(ERROR) << "Failed to create Graphite Context for Metal";
return false;
}
bool is_thread_safe = features::IsGraphiteContextThreadSafe();
objc_storage_->graphite_shared_context =
std::make_unique<gpu::GraphiteSharedContext>(std::move(graphite_context),
use_shader_cache_shm_count,
is_thread_safe);
return true;
}
gpu::GraphiteSharedContext* MetalContextProvider::GetGraphiteSharedContext()
const {
return objc_storage_->graphite_shared_context.get();
}
int32_t MetalContextProvider::GetMaxTextureSize() const {
#if BUILDFLAG(IS_IOS)
if (id<MTLDevice> device = GetMTLDevice()) {
if ([device supportsFamily:MTLGPUFamilyApple3]) {
return 16384;
}
}
return 8192;
#elif BUILDFLAG(IS_MAC)
return 16384;
#else
NOTREACHED();
#endif
}
id<MTLDevice> MetalContextProvider::GetMTLDevice() const {
return objc_storage_->device;
}
} // namespace viz
|