platform_view_android.cc 2.8 KB
Newer Older
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 "sky/shell/platform/android/platform_view_android.h"
6 7 8 9 10

#include <android/input.h>
#include <android/native_window_jni.h>

#include "base/android/jni_android.h"
11 12
#include "base/bind.h"
#include "base/location.h"
13
#include "jni/FlutterView_jni.h"
14
#include "sky/engine/core/script/dart_service_isolate.h"
A
Adam Barth 已提交
15
#include "sky/shell/gpu/direct/surface_notifications_direct.h"
A
Adam Barth 已提交
16
#include "sky/shell/shell.h"
17
#include "sky/shell/shell_view.h"
18 19 20 21

namespace sky {
namespace shell {

22
static jlong Attach(JNIEnv* env, jclass clazz, jint skyEngineHandle) {
23 24
  ShellView* shell_view = new ShellView(Shell::Shared());
  auto view = static_cast<PlatformViewAndroid*>(shell_view->view());
25
  view->SetShellView(std::unique_ptr<ShellView>(shell_view));
26
  view->ConnectToEngine(
27
      mojo::InterfaceRequest<SkyEngine>(mojo::ScopedMessagePipeHandle(
28
          mojo::MessagePipeHandle(skyEngineHandle))));
29
  return reinterpret_cast<jlong>(shell_view->view());
A
Adam Barth 已提交
30 31
}

32 33 34 35
jint GetObservatoryPort(JNIEnv* env, jclass clazz) {
  return blink::DartServiceIsolate::GetObservatoryPort();
}

36
// static
37
bool PlatformViewAndroid::Register(JNIEnv* env) {
38 39 40
  return RegisterNativesImpl(env);
}

41 42 43 44 45
PlatformView* PlatformView::Create(const Config& config) {
  return new PlatformViewAndroid(config);
}

PlatformViewAndroid::PlatformViewAndroid(const Config& config)
46
  : PlatformView(config), window_(nullptr), did_draw_(false, false) {
47 48
}

49
PlatformViewAndroid::~PlatformViewAndroid() {
50 51 52 53
  if (window_)
    ReleaseWindow();
}

54
void PlatformViewAndroid::Detach(JNIEnv* env, jobject obj) {
55 56
  shell_view_.reset();
  // Note: |this| has been destroyed at this point.
57 58
}

59
void PlatformViewAndroid::SurfaceCreated(JNIEnv* env, jobject obj, jobject jsurface) {
60 61 62 63 64 65 66 67
  base::android::ScopedJavaLocalRef<jobject> protector(env, jsurface);
  // Note: This ensures that any local references used by
  // ANativeWindow_fromSurface are released immediately. This is needed as a
  // workaround for https://code.google.com/p/android/issues/detail?id=68174
  {
    base::android::ScopedJavaLocalFrame scoped_local_reference_frame(env);
    window_ = ANativeWindow_fromSurface(env, jsurface);
  }
68 69
  SurfaceNotificationsDirect::NotifyCreated(config_, window_, &did_draw_);
  did_draw_.Wait();
70 71
}

72
void PlatformViewAndroid::SurfaceDestroyed(JNIEnv* env, jobject obj) {
73
  DCHECK(window_);
A
Adam Barth 已提交
74
  SurfaceNotificationsDirect::NotifyDestroyed(config_);
75 76 77
  ReleaseWindow();
}

78
void PlatformViewAndroid::SetShellView(std::unique_ptr<ShellView> shell_view) {
79
  DCHECK(!shell_view_);
80
  shell_view_ = std::move(shell_view);
81 82
}

83
void PlatformViewAndroid::ReleaseWindow() {
84
  ANativeWindow_release(window_);
85
  window_ = nullptr;
86 87 88 89
}

}  // namespace shell
}  // namespace sky