summaryrefslogtreecommitdiffstats
path: root/mojo/services/native_viewport/android/mojo_viewport.cc
blob: 15f868f0f50010e895913392fbf3071a050adfb3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
82
// Copyright 2013 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.

#include "mojo/services/native_viewport/android/mojo_viewport.h"

#include <android/native_window_jni.h>
#include "base/android/jni_android.h"
#include "jni/MojoViewport_jni.h"
#include "ui/gl/gl_bindings.h"
#include "ui/gl/gl_context.h"
#include "ui/gl/scoped_make_current.h"

namespace mojo {
namespace services {

MojoViewportInit::MojoViewportInit() {
}

MojoViewportInit::~MojoViewportInit() {
}

static jint Init(JNIEnv* env, jclass obj, jint jinit) {
  MojoViewportInit* init = reinterpret_cast<MojoViewportInit*>(jinit);
  MojoViewport* viewport = new MojoViewport(init);
  return reinterpret_cast<jint>(viewport);
}

MojoViewport::MojoViewport(MojoViewportInit* init)
    : ui_runner_(init->ui_runner),
      native_viewport_(init->native_viewport) {
  delete init;
}

MojoViewport::~MojoViewport() {
}

void MojoViewport::CreateForActivity(
    jobject activity,
    MojoViewportInit* init) {
  JNIEnv* env = base::android::AttachCurrentThread();
  Java_MojoViewport_createForActivity(
      env, activity, reinterpret_cast<jint>(init));
}

void MojoViewport::Destroy(JNIEnv* env, jobject obj) {
  delete this;
}

void MojoViewport::SurfaceCreated(JNIEnv* env, jobject obj, jobject jsurface) {
  base::android::ScopedJavaLocalRef<jobject> protector(env, jsurface);
  DCHECK(jsurface);
  window_ = ANativeWindow_fromSurface(env, jsurface);
  DCHECK(window_);

  surface_ = new gfx::NativeViewGLSurfaceEGL(window_);
  CHECK(surface_->Initialize());

  gl_context_ = gfx::GLContext::CreateGLContext(
      0, surface_.get(), gfx::PreferDiscreteGpu);
  ui::ScopedMakeCurrent make_current(gl_context_.get(), surface_.get());
  glClearColor(0, 1, 0, 0);
  glClear(GL_COLOR_BUFFER_BIT);
  surface_->SwapBuffers();
}

void MojoViewport::SurfaceDestroyed(JNIEnv* env, jobject obj) {
  DCHECK(window_);
  ANativeWindow_release(window_);
  window_ = NULL;
}

void MojoViewport::SurfaceSetSize(
    JNIEnv* env, jobject obj, jint width, jint height) {
}

bool MojoViewport::Register(JNIEnv* env) {
  return RegisterNativesImpl(env);
}

}  // namespace services
}  // namespace mojo