platform_view_mac.mm 4.4 KB
Newer Older
C
Chinmay Garde 已提交
1 2 3 4
// Copyright 2015 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.

5
#include "flutter/shell/platform/darwin/desktop/platform_view_mac.h"
C
Chinmay Garde 已提交
6

7
#include <AppKit/AppKit.h>
8
#include <Foundation/Foundation.h>
A
Adam Barth 已提交
9

10
#include "base/command_line.h"
11
#include "base/trace_event/trace_event.h"
A
Adam Barth 已提交
12
#include "flutter/common/threads.h"
13
#include "flutter/shell/common/switches.h"
14
#include "flutter/shell/gpu/gpu_rasterizer.h"
15
#include "flutter/shell/platform/darwin/common/platform_mac.h"
16
#include "flutter/shell/platform/darwin/desktop/vsync_waiter_mac.h"
17
#include "lib/ftl/synchronization/waitable_event.h"
18

C
Chinmay Garde 已提交
19 20
namespace shell {

21
PlatformViewMac::PlatformViewMac(NSOpenGLView* gl_view)
22 23
    : PlatformView(std::make_unique<GPURasterizer>()),
      opengl_view_([gl_view retain]),
24 25
      resource_loading_context_([[NSOpenGLContext alloc]
          initWithFormat:gl_view.pixelFormat
26
            shareContext:gl_view.openGLContext]) {
27 28
  CreateEngine();

29 30
  NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                       NSUserDomainMask, YES);
31
  if (paths.count > 0) {
32
    shell::Shell::Shared().tracing_controller().set_traces_base_path(
33 34
        [[paths objectAtIndex:0] UTF8String]);
  }
35
}
36

37
PlatformViewMac::~PlatformViewMac() = default;
38

39
void PlatformViewMac::SetupAndLoadDart() {
A
Adam Barth 已提交
40
  if (AttemptLaunchFromCommandLineSwitches(&engine())) {
41 42 43 44 45 46 47 48 49
    // This attempts launching from an FLX bundle that does not contain a
    // dart snapshot.
    return;
  }

  base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess();

  std::string bundle_path = command_line.GetSwitchValueASCII(switches::kFLX);
  if (!bundle_path.empty()) {
A
Adam Barth 已提交
50 51 52 53 54
    blink::Threads::UI()->PostTask(
        [ engine = engine().GetWeakPtr(), bundle_path ] {
          if (engine)
            engine->RunBundle(bundle_path);
        });
55 56 57 58 59
    return;
  }

  auto args = command_line.GetArgs();
  if (args.size() > 0) {
A
Adam Barth 已提交
60 61 62 63 64 65 66 67
    std::string main = args[0];
    std::string packages =
        command_line.GetSwitchValueASCII(switches::kPackages);
    blink::Threads::UI()->PostTask(
        [ engine = engine().GetWeakPtr(), main, packages ] {
          if (engine)
            engine->RunBundleAndSource(std::string(), main, packages);
        });
68 69 70 71 72
    return;
  }
}

void PlatformViewMac::SetupAndLoadFromSource(
A
Adam Barth 已提交
73
    const std::string& assets_directory,
74
    const std::string& main,
A
Adam Barth 已提交
75 76 77 78 79 80
    const std::string& packages) {
  blink::Threads::UI()->PostTask(
      [ engine = engine().GetWeakPtr(), assets_directory, main, packages ] {
        if (engine)
          engine->RunBundleAndSource(assets_directory, main, packages);
      });
81 82
}

83
intptr_t PlatformViewMac::GLContextFBO() const {
84
  // Default window bound framebuffer FBO 0.
85 86 87
  return 0;
}

88 89
bool PlatformViewMac::GLContextMakeCurrent() {
  TRACE_EVENT0("flutter", "PlatformViewMac::GLContextMakeCurrent");
90 91 92 93 94 95 96 97
  if (!IsValid()) {
    return false;
  }

  [opengl_view_.get().openGLContext makeCurrentContext];
  return true;
}

98 99 100
bool PlatformViewMac::GLContextClearCurrent() {
  TRACE_EVENT0("flutter", "PlatformViewMac::GLContextClearCurrent");
  if (!IsValid()) {
101 102 103
    return false;
  }

104
  [NSOpenGLContext clearCurrentContext];
105 106 107
  return true;
}

108 109
bool PlatformViewMac::GLContextPresent() {
  TRACE_EVENT0("flutter", "PlatformViewMac::GLContextPresent");
110 111 112 113 114 115 116 117
  if (!IsValid()) {
    return false;
  }

  [opengl_view_.get().openGLContext flushBuffer];
  return true;
}

118 119 120 121 122 123
VsyncWaiter* PlatformViewMac::GetVsyncWaiter() {
  if (!vsync_waiter_)
    vsync_waiter_ = std::make_unique<VsyncWaiterMac>();
  return vsync_waiter_.get();
}

124 125 126 127 128 129 130 131 132 133 134
bool PlatformViewMac::ResourceContextMakeCurrent() {
  NSOpenGLContext* context = resource_loading_context_.get();

  if (context == nullptr) {
    return false;
  }

  [context makeCurrentContext];
  return true;
}

135 136 137 138 139 140 141 142 143 144 145 146
bool PlatformViewMac::IsValid() const {
  if (opengl_view_ == nullptr) {
    return false;
  }

  auto context = opengl_view_.get().openGLContext;

  if (context == nullptr) {
    return false;
  }

  return true;
C
Chinmay Garde 已提交
147 148
}

A
Adam Barth 已提交
149 150 151
void PlatformViewMac::RunFromSource(const std::string& assets_directory,
                                    const std::string& main,
                                    const std::string& packages) {
152 153 154
  auto latch = new ftl::ManualResetWaitableEvent();

  dispatch_async(dispatch_get_main_queue(), ^{
A
Adam Barth 已提交
155
    SetupAndLoadFromSource(assets_directory, main, packages);
156 157 158 159 160
    latch->Signal();
  });

  latch->Wait();
  delete latch;
161 162
}

C
Chinmay Garde 已提交
163
}  // namespace shell