platform_view_mac.mm 4.9 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 16
#include "flutter/shell/platform/darwin/common/platform_mac.h"
#include "flutter/shell/platform/darwin/common/platform_service_provider.h"
#include "flutter/shell/platform/darwin/common/view_service_provider.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 26
      resource_loading_context_([[NSOpenGLContext alloc]
          initWithFormat:gl_view.pixelFormat
            shareContext:gl_view.openGLContext]),
27 28 29
      weak_factory_(this) {
  NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                       NSUserDomainMask, YES);
30
  if (paths.count > 0) {
31
    shell::Shell::Shared().tracing_controller().set_traces_base_path(
32 33
        [[paths objectAtIndex:0] UTF8String]);
  }
34
}
35

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

38 39 40
static void IgnoreRequest(
    mojo::InterfaceRequest<flutter::platform::ApplicationMessages>) {}

41 42 43 44
void PlatformViewMac::ConnectToEngineAndSetupServices() {
  ConnectToEngine(mojo::GetProxy(&sky_engine_));

  mojo::ServiceProviderPtr service_provider;
45
  new PlatformServiceProvider(mojo::GetProxy(&service_provider));
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 82 83 84 85 86 87 88 89 90 91

  mojo::ServiceProviderPtr view_service_provider;
  new ViewServiceProvider(IgnoreRequest,
                          mojo::GetProxy(&view_service_provider));

  sky::ServicesDataPtr services = sky::ServicesData::New();
  services->incoming_services = service_provider.Pass();
  services->view_services = view_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);
}

92
sky::SkyEnginePtr& PlatformViewMac::engineProxy() {
93 94 95 96
  return sky_engine_;
}

ftl::WeakPtr<PlatformView> PlatformViewMac::GetWeakViewPtr() {
97
  return weak_factory_.GetWeakPtr();
C
Chinmay Garde 已提交
98 99
}

100
intptr_t PlatformViewMac::GLContextFBO() const {
101
  // Default window bound framebuffer FBO 0.
102 103 104
  return 0;
}

105 106
bool PlatformViewMac::GLContextMakeCurrent() {
  TRACE_EVENT0("flutter", "PlatformViewMac::GLContextMakeCurrent");
107 108 109 110 111 112 113 114
  if (!IsValid()) {
    return false;
  }

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

115 116 117
bool PlatformViewMac::GLContextClearCurrent() {
  TRACE_EVENT0("flutter", "PlatformViewMac::GLContextClearCurrent");
  if (!IsValid()) {
118 119 120
    return false;
  }

121
  [NSOpenGLContext clearCurrentContext];
122 123 124
  return true;
}

125 126
bool PlatformViewMac::GLContextPresent() {
  TRACE_EVENT0("flutter", "PlatformViewMac::GLContextPresent");
127 128 129 130 131 132 133 134
  if (!IsValid()) {
    return false;
  }

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

135 136 137 138 139 140 141 142 143 144 145
bool PlatformViewMac::ResourceContextMakeCurrent() {
  NSOpenGLContext* context = resource_loading_context_.get();

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

  [context makeCurrentContext];
  return true;
}

146 147 148 149 150 151 152 153 154 155 156 157
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 已提交
158 159
}

160 161 162
void PlatformViewMac::RunFromSource(const std::string& main,
                                    const std::string& packages,
                                    const std::string& assets_directory) {
163 164 165 166 167 168 169 170 171
  auto latch = new ftl::ManualResetWaitableEvent();

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

  latch->Wait();
  delete latch;
172 173
}

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