ios_context_metal.mm 2.7 KB
Newer Older
1 2 3 4
// 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.

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 14 15

namespace flutter {

IOSContextMetal::IOSContextMetal() {
16 17
  darwin_context_metal_ = fml::scoped_nsobject<FlutterDarwinContextMetal>{
      [[[FlutterDarwinContextMetal alloc] initWithDefaultMTLDevice] retain]};
18

19
  if (!darwin_context_metal_) {
20 21 22
    return;
  }

23
  main_command_queue_.reset([darwin_context_metal_.get().commandQueue retain]);
24

25 26
  CVMetalTextureCacheRef texture_cache_raw = NULL;
  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 31 32 33 34 35 36
                                             &texture_cache_raw  // [out] cache
  );
  if (cv_return != kCVReturnSuccess) {
    FML_DLOG(ERROR) << "Could not create Metal texture cache.";
    return;
  }
  texture_cache_.Reset(texture_cache_raw);
37 38 39 40
}

IOSContextMetal::~IOSContextMetal() = default;

41 42
fml::scoped_nsobject<FlutterDarwinContextMetal> IOSContextMetal::GetDarwinContext() const {
  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 65 66 67
// |IOSContext|
std::unique_ptr<Texture> IOSContextMetal::CreateExternalTexture(
    int64_t texture_id,
    fml::scoped_nsobject<NSObject<FlutterTexture>> texture) {
68 69 70 71
  return std::make_unique<IOSExternalTextureMetal>(
      fml::scoped_nsobject<FlutterDarwinExternalTextureMetal>{
          [[darwin_context_metal_ createExternalTextureWithIdentifier:texture_id
                                                              texture:texture] retain]});
72 73
}

74
}  // namespace flutter