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"
12
#include "flutter/shell/common/switches.h"
13
#include "flutter/shell/gpu/gpu_rasterizer.h"
14 15
#include "flutter/shell/platform/darwin/common/platform_mac.h"
#include "flutter/shell/platform/darwin/common/platform_service_provider.h"
16
#include "lib/ftl/synchronization/waitable_event.h"
17

C
Chinmay Garde 已提交
18 19
namespace shell {

20
PlatformViewMac::PlatformViewMac(NSOpenGLView* gl_view)
21 22
    : PlatformView(std::make_unique<GPURasterizer>()),
      opengl_view_([gl_view retain]),
23 24
      resource_loading_context_([[NSOpenGLContext alloc]
          initWithFormat:gl_view.pixelFormat
25
            shareContext:gl_view.openGLContext]) {
26 27
  NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                       NSUserDomainMask, YES);
28
  if (paths.count > 0) {
29
    shell::Shell::Shared().tracing_controller().set_traces_base_path(
30 31
        [[paths objectAtIndex:0] UTF8String]);
  }
32
}
33

34
PlatformViewMac::~PlatformViewMac() = default;
35

36 37 38 39
void PlatformViewMac::ConnectToEngineAndSetupServices() {
  ConnectToEngine(mojo::GetProxy(&sky_engine_));

  mojo::ServiceProviderPtr service_provider;
40
  new PlatformServiceProvider(mojo::GetProxy(&service_provider));
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

  sky::ServicesDataPtr services = sky::ServicesData::New();
  services->incoming_services = service_provider.Pass();
  sky_engine_->SetServices(services.Pass());
}

void PlatformViewMac::SetupAndLoadDart() {
  ConnectToEngineAndSetupServices();

  if (AttemptLaunchFromCommandLineSwitches(sky_engine_)) {
    // 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()) {
    std::string script_uri = std::string("file://") + bundle_path;
    sky_engine_->RunFromBundle(script_uri, bundle_path);
    return;
  }

  auto args = command_line.GetArgs();
  if (args.size() > 0) {
    auto packages = command_line.GetSwitchValueASCII(switches::kPackages);
    sky_engine_->RunFromFile(args[0], packages, "");
    return;
  }
}

void PlatformViewMac::SetupAndLoadFromSource(
    const std::string& main,
    const std::string& packages,
    const std::string& assets_directory) {
  ConnectToEngineAndSetupServices();

  sky_engine_->RunFromFile(main, packages, assets_directory);
}

82
sky::SkyEnginePtr& PlatformViewMac::engineProxy() {
83 84 85
  return sky_engine_;
}

86
intptr_t PlatformViewMac::GLContextFBO() const {
87
  // Default window bound framebuffer FBO 0.
88 89 90
  return 0;
}

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

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

101 102 103
bool PlatformViewMac::GLContextClearCurrent() {
  TRACE_EVENT0("flutter", "PlatformViewMac::GLContextClearCurrent");
  if (!IsValid()) {
104 105 106
    return false;
  }

107
  [NSOpenGLContext clearCurrentContext];
108 109 110
  return true;
}

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

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

121 122 123 124 125 126 127 128 129 130 131
bool PlatformViewMac::ResourceContextMakeCurrent() {
  NSOpenGLContext* context = resource_loading_context_.get();

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

  [context makeCurrentContext];
  return true;
}

132 133 134 135 136 137 138 139 140 141 142 143
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 已提交
144 145
}

146 147 148
void PlatformViewMac::RunFromSource(const std::string& main,
                                    const std::string& packages,
                                    const std::string& assets_directory) {
149 150 151 152 153 154 155 156 157
  auto latch = new ftl::ManualResetWaitableEvent();

  dispatch_async(dispatch_get_main_queue(), ^{
    SetupAndLoadFromSource(main, packages, assets_directory);
    latch->Signal();
  });

  latch->Wait();
  delete latch;
158 159
}

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