Flutter Engine
ios_context_metal.mm
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #import "flutter/shell/platform/darwin/ios/ios_context_metal.h"
6 
7 #include "flutter/common/graphics/persistent_cache.h"
8 #include "flutter/fml/logging.h"
9 #import "flutter/shell/platform/darwin/graphics/FlutterDarwinContextMetal.h"
10 #import "flutter/shell/platform/darwin/ios/ios_external_texture_metal.h"
11 #include "third_party/skia/include/gpu/GrContextOptions.h"
12 
13 namespace flutter {
14 
17  [[FlutterDarwinContextMetal alloc] initWithDefaultMTLDevice]};
18 
19  if (!darwin_context_metal_) {
20  return;
21  }
22 
23  main_command_queue_.reset([darwin_context_metal_.get().commandQueue retain]);
24 
25  CVMetalTextureCacheRef texture_cache_raw = NULL;
26  auto cv_return = CVMetalTextureCacheCreate(kCFAllocatorDefault, // allocator
27  NULL, // cache attributes (NULL default)
28  darwin_context_metal_.get().device, // metal device
29  NULL, // texture attributes (NULL default)
30  &texture_cache_raw // [out] cache
31  );
32  if (cv_return != kCVReturnSuccess) {
33  FML_DLOG(ERROR) << "Could not create Metal texture cache.";
34  return;
35  }
36  texture_cache_.Reset(texture_cache_raw);
37 }
38 
40 
42  return darwin_context_metal_;
43 }
44 
45 sk_sp<GrDirectContext> IOSContextMetal::GetMainContext() const {
46  return darwin_context_metal_.get().mainContext;
47 }
48 
49 sk_sp<GrDirectContext> IOSContextMetal::GetResourceContext() const {
50  return darwin_context_metal_.get().resourceContext;
51 }
52 
53 // |IOSContext|
54 sk_sp<GrDirectContext> IOSContextMetal::CreateResourceContext() {
55  return darwin_context_metal_.get().resourceContext;
56 }
57 
58 // |IOSContext|
59 std::unique_ptr<GLContextResult> IOSContextMetal::MakeCurrent() {
60  // This only makes sense for context that need to be bound to a specific thread.
61  return std::make_unique<GLContextDefaultResult>(true);
62 }
63 
64 // |IOSContext|
65 std::unique_ptr<Texture> IOSContextMetal::CreateExternalTexture(
66  int64_t texture_id,
67  fml::scoped_nsobject<NSObject<FlutterTexture>> texture) {
68  return std::make_unique<IOSExternalTextureMetal>(
70  [[darwin_context_metal_ createExternalTextureWithIdentifier:texture_id
71  texture:texture] retain]});
72 }
73 
74 } // namespace flutter
sk_sp< GrDirectContext > GetResourceContext() const
id< MTLCommandQueue > commandQueue
G_BEGIN_DECLS FlTexture * texture
void reset(NST object=nil)
int64_t texture_id
sk_sp< GrDirectContext > resourceContext
void Reset(T instance=nullptr)
Definition: cf_utils.h:43
fml::scoped_nsobject< FlutterDarwinContextMetal > GetDarwinContext() const
sk_sp< GrDirectContext > mainContext
#define FML_DLOG(severity)
Definition: logging.h:85
sk_sp< GrDirectContext > GetMainContext() const override
Accessor for the Skia context associated with IOSSurfaces and the raster thread.