Skip to content

Commit

Permalink
Reland "[metal] Darwin unified external metal textures (flutter#24157)…
Browse files Browse the repository at this point in the history
  • Loading branch information
iskakaushik authored Feb 9, 2021
1 parent 625d13a commit 2e10a97
Show file tree
Hide file tree
Showing 12 changed files with 370 additions and 268 deletions.
2 changes: 2 additions & 0 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,8 @@ FILE: ../../../flutter/shell/platform/darwin/common/framework/Source/flutter_cod
FILE: ../../../flutter/shell/platform/darwin/common/framework/Source/flutter_standard_codec_unittest.mm
FILE: ../../../flutter/shell/platform/darwin/graphics/FlutterDarwinContextMetal.h
FILE: ../../../flutter/shell/platform/darwin/graphics/FlutterDarwinContextMetal.mm
FILE: ../../../flutter/shell/platform/darwin/graphics/FlutterDarwinExternalTextureMetal.h
FILE: ../../../flutter/shell/platform/darwin/graphics/FlutterDarwinExternalTextureMetal.mm
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Flutter.podspec
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Headers/Flutter.h
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Headers/FlutterAppDelegate.h
Expand Down
2 changes: 2 additions & 0 deletions common/config.gni
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,6 @@ if (is_ios || is_mac) {
"-Werror=undeclared-selector",
]
flutter_cflags_objcc = flutter_cflags_objc
flutter_cflags_objc_arc = flutter_cflags_objc + [ "-fobjc-arc" ]
flutter_cflags_objcc_arc = flutter_cflags_objc_arc
}
4 changes: 2 additions & 2 deletions common/graphics/texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ namespace flutter {

class Texture {
public:
Texture(int64_t id); // Called from UI or raster thread.
virtual ~Texture(); // Called from raster thread.
explicit Texture(int64_t id); // Called from UI or raster thread.
virtual ~Texture(); // Called from raster thread.

// Called from raster thread.
virtual void Paint(SkCanvas& canvas,
Expand Down
9 changes: 7 additions & 2 deletions shell/platform/darwin/graphics/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,24 @@ assert(is_ios || is_mac)
import("//flutter/common/config.gni")

source_set("graphics") {
cflags_objc = flutter_cflags_objc
cflags_objcc = flutter_cflags_objcc
cflags_objc = flutter_cflags_objc_arc
cflags_objcc = flutter_cflags_objcc_arc

sources = [
"FlutterDarwinContextMetal.h",
"FlutterDarwinContextMetal.mm",
"FlutterDarwinExternalTextureMetal.h",
"FlutterDarwinExternalTextureMetal.mm",
]

deps = [
"//flutter/common/graphics",
"//flutter/fml",
"//flutter/shell/platform/darwin/common:framework_shared",
]

libs = [ "CoreVideo.framework" ]

public_deps = [ "//third_party/skia" ]

public_configs = [ "//flutter:config" ]
Expand Down
15 changes: 15 additions & 0 deletions shell/platform/darwin/graphics/FlutterDarwinContextMetal.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
#ifndef SHELL_PLATFORM_DARWIN_GRAPHICS_DARWIN_CONTEXT_METAL_H_
#define SHELL_PLATFORM_DARWIN_GRAPHICS_DARWIN_CONTEXT_METAL_H_

#import <CoreVideo/CVMetalTextureCache.h>
#import <Foundation/Foundation.h>
#import <Metal/Metal.h>

#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterTexture.h"
#import "flutter/shell/platform/darwin/graphics/FlutterDarwinExternalTextureMetal.h"
#include "third_party/skia/include/gpu/GrDirectContext.h"

NS_ASSUME_NONNULL_BEGIN
Expand All @@ -29,6 +32,13 @@ NS_ASSUME_NONNULL_BEGIN
- (instancetype)initWithMTLDevice:(id<MTLDevice>)device
commandQueue:(id<MTLCommandQueue>)commandQueue;

/**
* Creates an external texture with the specified ID and contents.
*/
- (FlutterDarwinExternalTextureMetal*)
createExternalTextureWithIdentifier:(int64_t)textureID
texture:(NSObject<FlutterTexture>*)texture;

/**
* MTLDevice that is backing this context.s
*/
Expand All @@ -50,6 +60,11 @@ NS_ASSUME_NONNULL_BEGIN
*/
@property(nonatomic, readonly) sk_sp<GrDirectContext> resourceContext;

/*
* Texture cache for external textures.
*/
@property(nonatomic, readonly) CVMetalTextureCacheRef textureCache;

@end

NS_ASSUME_NONNULL_END
Expand Down
49 changes: 39 additions & 10 deletions shell/platform/darwin/graphics/FlutterDarwinContextMetal.mm
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@

#include "flutter/common/graphics/persistent_cache.h"
#include "flutter/fml/logging.h"
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h"
#include "third_party/skia/include/gpu/GrContextOptions.h"

FLUTTER_ASSERT_ARC

static GrContextOptions CreateMetalGrContextOptions() {
GrContextOptions options = {};
if (flutter::PersistentCache::cache_sksl()) {
Expand All @@ -33,32 +36,34 @@ - (instancetype)initWithMTLDevice:(id<MTLDevice>)device

if (!_device) {
FML_DLOG(ERROR) << "Could not acquire Metal device.";
[self release];
return nil;
}

_commandQueue = commandQueue;

if (!_commandQueue) {
FML_DLOG(ERROR) << "Could not create Metal command queue.";
[self release];
return nil;
}

[_commandQueue setLabel:@"Flutter Main Queue"];

auto contextOptions = CreateMetalGrContextOptions();
CVReturn cvReturn = CVMetalTextureCacheCreate(kCFAllocatorDefault, // allocator
nil, // cache attributes (nil default)
_device, // metal device
nil, // texture attributes (nil default)
&_textureCache // [out] cache
);
if (cvReturn != kCVReturnSuccess) {
FML_DLOG(ERROR) << "Could not create Metal texture cache.";
return nil;
}

// Skia expect arguments to `MakeMetal` transfer ownership of the reference in for release later
// when the GrDirectContext is collected.
_mainContext =
GrDirectContext::MakeMetal([_device retain], [_commandQueue retain], contextOptions);
_resourceContext =
GrDirectContext::MakeMetal([_device retain], [_commandQueue retain], contextOptions);
_mainContext = [self createGrContext];
_resourceContext = [self createGrContext];

if (!_mainContext || !_resourceContext) {
FML_DLOG(ERROR) << "Could not create Skia Metal contexts.";
[self release];
return nil;
}

Expand All @@ -67,4 +72,28 @@ - (instancetype)initWithMTLDevice:(id<MTLDevice>)device
return self;
}

- (sk_sp<GrDirectContext>)createGrContext {
auto contextOptions = CreateMetalGrContextOptions();
id<MTLDevice> device = _device;
id<MTLCommandQueue> commandQueue = _commandQueue;
// Skia expect arguments to `MakeMetal` transfer ownership of the reference in for release later
// when the GrDirectContext is collected.
return GrDirectContext::MakeMetal((__bridge_retained void*)device,
(__bridge_retained void*)commandQueue, contextOptions);
}

- (void)dealloc {
if (_textureCache) {
CFRelease(_textureCache);
}
}

- (FlutterDarwinExternalTextureMetal*)
createExternalTextureWithIdentifier:(int64_t)textureID
texture:(NSObject<FlutterTexture>*)texture {
return [[FlutterDarwinExternalTextureMetal alloc] initWithTextureCache:_textureCache
textureID:textureID
texture:texture];
}

@end
34 changes: 34 additions & 0 deletions shell/platform/darwin/graphics/FlutterDarwinExternalTextureMetal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import <Foundation/Foundation.h>
#import <Metal/Metal.h>

#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterTexture.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/gpu/GrDirectContext.h"

@interface FlutterDarwinExternalTextureMetal : NSObject

- (nullable instancetype)initWithTextureCache:(nonnull CVMetalTextureCacheRef)textureCache
textureID:(int64_t)textureID
texture:(nonnull NSObject<FlutterTexture>*)texture;

- (void)paint:(SkCanvas&)canvas
bounds:(const SkRect&)bounds
freeze:(BOOL)freeze
grContext:(nonnull GrDirectContext*)grContext
sampling:(const SkSamplingOptions&)sampling;

- (void)onGrContextCreated;

- (void)onGrContextDestroyed;

- (void)markNewFrameAvailable;

- (void)onTextureUnregistered;

@property(nonatomic, readonly) int64_t textureID;

@end
Loading

0 comments on commit 2e10a97

Please sign in to comment.