Commit c2fb7552 authored by Christopher Cameron's avatar Christopher Cameron Committed by Commit Bot

SkiaRenderer/macOS: Make Metal work

Create a viz::ToMTLPixelFormat helper function, and use it in the
shared image factory, and in the output surface allocation.

Update the default gfx::BufferFormat for SkiaOutputDeviceBufferQueue
for macOS, and add a TODO noting that the correct value should be
determined at the Reshape call, and not at initialization.

This is enough to get --enable-features=SkiaRenderer,Metal to draw
the browser and some simple pages.

Bug: 894929
Change-Id: I3fde39349fecaea856293609e5b877c49e46d0e7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2225485
Commit-Queue: ccameron <ccameron@chromium.org>
Reviewed-by: default avatarCorentin Wallez <cwallez@chromium.org>
Cr-Commit-Position: refs/heads/master@{#774843}
parent 8aa0fe2d
...@@ -19,6 +19,7 @@ viz_component("resource_format_utils") { ...@@ -19,6 +19,7 @@ viz_component("resource_format_utils") {
sources = [ sources = [
"resources/resource_format_utils.cc", "resources/resource_format_utils.cc",
"resources/resource_format_utils.h", "resources/resource_format_utils.h",
"resources/resource_format_utils_mac.mm",
"resources/resource_sizes.h", "resources/resource_sizes.h",
"viz_resource_format_export.h", "viz_resource_format_export.h",
] ]
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#ifndef COMPONENTS_VIZ_COMMON_RESOURCES_RESOURCE_FORMAT_UTILS_H_ #ifndef COMPONENTS_VIZ_COMMON_RESOURCES_RESOURCE_FORMAT_UTILS_H_
#define COMPONENTS_VIZ_COMMON_RESOURCES_RESOURCE_FORMAT_UTILS_H_ #define COMPONENTS_VIZ_COMMON_RESOURCES_RESOURCE_FORMAT_UTILS_H_
#include "build/build_config.h"
#include "components/viz/common/resources/resource_format.h" #include "components/viz/common/resources/resource_format.h"
#include "components/viz/common/viz_resource_format_export.h" #include "components/viz/common/viz_resource_format_export.h"
#include "gpu/vulkan/buildflags.h" #include "gpu/vulkan/buildflags.h"
...@@ -73,6 +74,10 @@ VIZ_RESOURCE_FORMAT_EXPORT wgpu::TextureFormat ToDawnFormat( ...@@ -73,6 +74,10 @@ VIZ_RESOURCE_FORMAT_EXPORT wgpu::TextureFormat ToDawnFormat(
VIZ_RESOURCE_FORMAT_EXPORT WGPUTextureFormat VIZ_RESOURCE_FORMAT_EXPORT WGPUTextureFormat
ToWGPUFormat(ResourceFormat format); ToWGPUFormat(ResourceFormat format);
#if defined(OS_MACOSX)
VIZ_RESOURCE_FORMAT_EXPORT unsigned int ToMTLPixelFormat(ResourceFormat format);
#endif
} // namespace viz } // namespace viz
#endif // COMPONENTS_VIZ_COMMON_RESOURCES_RESOURCE_FORMAT_UTILS_H_ #endif // COMPONENTS_VIZ_COMMON_RESOURCES_RESOURCE_FORMAT_UTILS_H_
// Copyright 2020 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/resources/resource_format_utils.h"
#include <Metal/MTLPixelFormat.h>
#include "base/logging.h"
namespace viz {
unsigned int ToMTLPixelFormat(ResourceFormat format) {
if (@available(macOS 10.11, *)) {
MTLPixelFormat mtl_pixel_format = MTLPixelFormatInvalid;
switch (format) {
case RED_8:
case ALPHA_8:
case LUMINANCE_8:
mtl_pixel_format = MTLPixelFormatR8Unorm;
break;
case RG_88:
mtl_pixel_format = MTLPixelFormatRG8Unorm;
break;
case RGBA_8888:
mtl_pixel_format = MTLPixelFormatRGBA8Unorm;
break;
case BGRA_8888:
mtl_pixel_format = MTLPixelFormatBGRA8Unorm;
break;
default:
DLOG(ERROR) << "Invalid Metal pixel format.";
break;
}
return static_cast<unsigned int>(mtl_pixel_format);
}
return 0;
}
} // namespace viz
...@@ -269,8 +269,14 @@ SkiaOutputDeviceBufferQueue::SkiaOutputDeviceBufferQueue( ...@@ -269,8 +269,14 @@ SkiaOutputDeviceBufferQueue::SkiaOutputDeviceBufferQueue(
std::make_unique<gpu::SharedImageRepresentationFactory>( std::make_unique<gpu::SharedImageRepresentationFactory>(
dependency_->GetSharedImageManager(), memory_tracker); dependency_->GetSharedImageManager(), memory_tracker);
// TODO(https://crbug.com/958166): The initial |image_format_| should not be
// used, and the gfx::BufferFormat specified in Reshape should be used
// instead, because it may be updated to reflect changes in the content being
// displayed (e.g, HDR content appearing on-screen).
#if defined(USE_OZONE) #if defined(USE_OZONE)
image_format_ = GetResourceFormat(display::DisplaySnapshot::PrimaryFormat()); image_format_ = GetResourceFormat(display::DisplaySnapshot::PrimaryFormat());
#elif defined(OS_MACOSX)
image_format_ = BGRA_8888;
#else #else
image_format_ = RGBA_8888; image_format_ = RGBA_8888;
#endif #endif
......
...@@ -120,6 +120,10 @@ class VIZ_SERVICE_EXPORT SkiaOutputSurfaceDependency { ...@@ -120,6 +120,10 @@ class VIZ_SERVICE_EXPORT SkiaOutputSurfaceDependency {
bool IsUsingDawn() const { bool IsUsingDawn() const {
return gr_context_type() == gpu::GrContextType::kDawn; return gr_context_type() == gpu::GrContextType::kDawn;
} }
bool IsUsingMetal() const {
return gr_context_type() == gpu::GrContextType::kMetal;
}
}; };
} // namespace viz } // namespace viz
......
...@@ -884,6 +884,10 @@ GrBackendFormat SkiaOutputSurfaceImpl::GetGrBackendFormatForTexture( ...@@ -884,6 +884,10 @@ GrBackendFormat SkiaOutputSurfaceImpl::GetGrBackendFormatForTexture(
#if BUILDFLAG(SKIA_USE_DAWN) #if BUILDFLAG(SKIA_USE_DAWN)
wgpu::TextureFormat format = ToDawnFormat(resource_format); wgpu::TextureFormat format = ToDawnFormat(resource_format);
return GrBackendFormat::MakeDawn(format); return GrBackendFormat::MakeDawn(format);
#endif
} else if (dependency_->IsUsingMetal()) {
#if defined(OS_MACOSX)
return GrBackendFormat::MakeMtl(ToMTLPixelFormat(resource_format));
#endif #endif
} else { } else {
DCHECK(!ycbcr_info); DCHECK(!ycbcr_info);
......
...@@ -110,28 +110,11 @@ base::scoped_nsprotocol<id<MTLTexture>> API_AVAILABLE(macos(10.11)) ...@@ -110,28 +110,11 @@ base::scoped_nsprotocol<id<MTLTexture>> API_AVAILABLE(macos(10.11))
viz::ResourceFormat format) { viz::ResourceFormat format) {
TRACE_EVENT0("gpu", "SharedImageBackingFactoryIOSurface::CreateMetalTexture"); TRACE_EVENT0("gpu", "SharedImageBackingFactoryIOSurface::CreateMetalTexture");
base::scoped_nsprotocol<id<MTLTexture>> mtl_texture; base::scoped_nsprotocol<id<MTLTexture>> mtl_texture;
MTLPixelFormat mtl_pixel_format; MTLPixelFormat mtl_pixel_format =
switch (format) { static_cast<MTLPixelFormat>(viz::ToMTLPixelFormat(format));
case viz::RED_8: if (mtl_pixel_format == MTLPixelFormatInvalid)
case viz::ALPHA_8:
case viz::LUMINANCE_8:
mtl_pixel_format = MTLPixelFormatR8Unorm;
break;
case viz::RG_88:
mtl_pixel_format = MTLPixelFormatRG8Unorm;
break;
case viz::RGBA_8888:
mtl_pixel_format = MTLPixelFormatRGBA8Unorm;
break;
case viz::BGRA_8888:
mtl_pixel_format = MTLPixelFormatBGRA8Unorm;
break;
default:
// TODO(https://crbug.com/952063): Add support for all formats supported
// by GLImageIOSurface.
DLOG(ERROR) << "Resource format not yet supported in Metal.";
return mtl_texture; return mtl_texture;
}
base::scoped_nsobject<MTLTextureDescriptor> mtl_tex_desc( base::scoped_nsobject<MTLTextureDescriptor> mtl_tex_desc(
[MTLTextureDescriptor new]); [MTLTextureDescriptor new]);
[mtl_tex_desc setTextureType:MTLTextureType2D]; [mtl_tex_desc setTextureType:MTLTextureType2D];
......
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