Commit 45f012f3 authored by Christopher Cameron's avatar Christopher Cameron Committed by Commit Bot

Skia/Metal raster: Add viz::MetalContextProvider

Add viz::MetalContextProvider, and add Metal to the Skia build by
default. Do not wire up any of this yet.

Bug: 952063
Change-Id: I897d052bdbf046114c1ddc0527c1d3cafc8777c5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1577296Reviewed-by: default avatarBrian Osman <brianosman@google.com>
Reviewed-by: default avatarEric Karl <ericrk@chromium.org>
Commit-Queue: ccameron <ccameron@chromium.org>
Cr-Commit-Position: refs/heads/master@{#652899}
parent f8f28226
...@@ -40,6 +40,34 @@ viz_component("resource_format_utils") { ...@@ -40,6 +40,34 @@ viz_component("resource_format_utils") {
] ]
} }
if (is_mac) {
viz_component("metal_context_provider") {
output_name = "viz_metal_context_provider"
defines = [ "VIZ_METAL_CONTEXT_PROVIDER_IMPLEMENTATION" ]
sources = [
"gpu/metal_context_provider.h",
"gpu/metal_context_provider.mm",
"viz_metal_context_provider_export.h",
]
public_deps = [
"//skia",
]
deps = [
"//base",
"//ui/gfx",
]
libs = [
"Metal.framework",
"Foundation.framework",
]
}
}
if (enable_vulkan) { if (enable_vulkan) {
viz_component("vulkan_context_provider") { viz_component("vulkan_context_provider") {
output_name = "viz_vulkan_context_provider" output_name = "viz_vulkan_context_provider"
...@@ -256,6 +284,9 @@ viz_component("common") { ...@@ -256,6 +284,9 @@ viz_component("common") {
if (enable_vulkan) { if (enable_vulkan) {
public_deps += [ ":vulkan_context_provider" ] public_deps += [ ":vulkan_context_provider" ]
} }
if (is_mac) {
public_deps += [ ":metal_context_provider" ]
}
} }
viz_source_set("unit_tests") { viz_source_set("unit_tests") {
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_VIZ_COMMON_GPU_METAL_CONTEXT_PROVIDER_H_
#define COMPONENTS_VIZ_COMMON_GPU_METAL_CONTEXT_PROVIDER_H_
#include <memory>
#include "components/viz/common/viz_metal_context_provider_export.h"
class GrContext;
namespace viz {
// The MetalContextProvider provides a Metal-backed GrContext.
class VIZ_METAL_CONTEXT_PROVIDER_EXPORT MetalContextProvider {
public:
// Create and return a MetalContextProvider if possible. May return nullptr
// if no Metal devices exist.
static std::unique_ptr<MetalContextProvider> Create();
virtual ~MetalContextProvider() {}
virtual GrContext* GetGrContext() = 0;
virtual void* GetMTLDevice() = 0;
};
} // namespace viz
#endif // COMPONENTS_VIZ_COMMON_GPU_METAL_CONTEXT_PROVIDER_H_
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/viz/common/gpu/metal_context_provider.h"
#include "base/mac/scoped_nsobject.h"
#include "third_party/skia/include/gpu/GrContext.h"
#import <Metal/Metal.h>
namespace viz {
namespace {
struct API_AVAILABLE(macos(10.11)) MetalContextProviderImpl
: public MetalContextProvider {
public:
explicit MetalContextProviderImpl(
base::scoped_nsprotocol<id<MTLDevice>> device)
: device_(device) {
command_queue_.reset([device_ newCommandQueue]);
// GrContext::MakeMetal will take ownership of the objects passed in. Retain
// the objects before passing them to MakeMetal so that the objects in
// |this| are also valid.
gr_context_ =
GrContext::MakeMetal([device_ retain], [command_queue_ retain]);
DCHECK(gr_context_);
}
~MetalContextProviderImpl() override {}
GrContext* GetGrContext() override { return gr_context_.get(); }
void* GetMTLDevice() override { return device_.get(); }
private:
base::scoped_nsprotocol<id<MTLDevice>> device_;
base::scoped_nsprotocol<id<MTLCommandQueue>> command_queue_;
sk_sp<GrContext> gr_context_;
};
} // namespace
// static
std::unique_ptr<MetalContextProvider> MetalContextProvider::Create() {
if (@available(macOS 10.11, *)) {
// First attempt to find a low power device to use.
base::scoped_nsprotocol<id<MTLDevice>> device_to_use;
base::scoped_nsobject<NSArray<id<MTLDevice>>> devices(MTLCopyAllDevices());
for (id<MTLDevice> device in devices.get()) {
if ([device isLowPower]) {
device_to_use.reset(device, base::scoped_policy::RETAIN);
break;
}
}
// Failing that, use the system default device.
if (!device_to_use)
device_to_use.reset(MTLCreateSystemDefaultDevice());
if (device_to_use)
return std::make_unique<MetalContextProviderImpl>(device_to_use);
}
// If no device was found, or if the macOS version is too old for Metal,
// return no context provider.
return nullptr;
}
} // namespace viz
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_VIZ_COMMON_VIZ_METAL_CONTEXT_PROVIDER_EXPORT_H_
#define COMPONENTS_VIZ_COMMON_VIZ_METAL_CONTEXT_PROVIDER_EXPORT_H_
#if defined(COMPONENT_BUILD)
#if defined(WIN32)
#if defined(VIZ_METAL_CONTEXT_PROVIDER_IMPLEMENTATION)
#define VIZ_METAL_CONTEXT_PROVIDER_EXPORT __declspec(dllexport)
#else
#define VIZ_METAL_CONTEXT_PROVIDER_EXPORT __declspec(dllimport)
#endif // defined(VIZ_METAL_CONTEXT_PROVIDER_IMPLEMENTATION)
#else // defined(WIN32)
#if defined(VIZ_METAL_CONTEXT_PROVIDER_IMPLEMENTATION)
#define VIZ_METAL_CONTEXT_PROVIDER_EXPORT __attribute__((visibility("default")))
#else
#define VIZ_METAL_CONTEXT_PROVIDER_EXPORT
#endif
#endif
#else // defined(COMPONENT_BUILD)
#define VIZ_METAL_CONTEXT_PROVIDER_EXPORT
#endif
#endif // COMPONENTS_VIZ_COMMON_VIZ_METAL_CONTEXT_PROVIDER_EXPORT_H_
...@@ -25,7 +25,6 @@ skia_support_skottie = true ...@@ -25,7 +25,6 @@ skia_support_skottie = true
declare_args() { declare_args() {
skia_whitelist_serialized_typefaces = false skia_whitelist_serialized_typefaces = false
skia_use_metal = false
} }
# External-facing config for dependent code. # External-facing config for dependent code.
...@@ -102,7 +101,10 @@ config("skia_config") { ...@@ -102,7 +101,10 @@ config("skia_config") {
} }
if (is_mac) { if (is_mac) {
defines += [ "SK_BUILD_FOR_MAC" ] defines += [
"SK_BUILD_FOR_MAC",
"SK_METAL",
]
} }
if (is_win) { if (is_win) {
...@@ -556,20 +558,14 @@ component("skia") { ...@@ -556,20 +558,14 @@ component("skia") {
} }
if (is_mac) { if (is_mac) {
if (skia_use_metal) { deps += [ ":skia_metal" ]
deps += [ ":skia_metal" ] defines += [ "SK_METAL" ]
defines += [ "SK_METAL" ]
libs += [
"Metal.framework",
"Foundation.framework",
]
}
libs = [ libs = [
"AppKit.framework", "AppKit.framework",
"CoreFoundation.framework", "CoreFoundation.framework",
"CoreGraphics.framework", "CoreGraphics.framework",
"CoreText.framework", "CoreText.framework",
"Metal.framework",
"Foundation.framework", "Foundation.framework",
] ]
} }
...@@ -801,7 +797,7 @@ skia_source_set("skia_opts") { ...@@ -801,7 +797,7 @@ skia_source_set("skia_opts") {
} }
# Split out metal sources, because they require ARC. # Split out metal sources, because they require ARC.
if (is_mac && skia_use_metal) { if (is_mac) {
skia_source_set("skia_metal") { skia_source_set("skia_metal") {
defines = [ "SK_METAL" ] defines = [ "SK_METAL" ]
sources = skia_metal_sources sources = skia_metal_sources
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment