1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/base/android/window_android.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_array.h"
9 #include "base/android/jni_weak_ref.h"
10 #include "base/android/scoped_java_ref.h"
11 #include "jni/WindowAndroid_jni.h"
12 #include "ui/base/android/window_android_compositor.h"
13 #include "ui/base/android/window_android_observer.h"
17 using base::android::AttachCurrentThread
;
18 using base::android::ScopedJavaLocalRef
;
20 WindowAndroid::WindowAndroid(JNIEnv
* env
, jobject obj
, jlong vsync_period
)
21 : weak_java_window_(env
, obj
),
23 vsync_period_(base::TimeDelta::FromInternalValue(vsync_period
)) {
26 void WindowAndroid::Destroy(JNIEnv
* env
, jobject obj
) {
30 ScopedJavaLocalRef
<jobject
> WindowAndroid::GetJavaObject() {
31 return weak_java_window_
.get(AttachCurrentThread());
34 bool WindowAndroid::RegisterWindowAndroid(JNIEnv
* env
) {
35 return RegisterNativesImpl(env
);
38 WindowAndroid::~WindowAndroid() {
42 void WindowAndroid::OnCompositingDidCommit() {
43 FOR_EACH_OBSERVER(WindowAndroidObserver
,
45 OnCompositingDidCommit());
48 void WindowAndroid::AddObserver(WindowAndroidObserver
* observer
) {
49 if (!observer_list_
.HasObserver(observer
))
50 observer_list_
.AddObserver(observer
);
53 void WindowAndroid::RemoveObserver(WindowAndroidObserver
* observer
) {
54 observer_list_
.RemoveObserver(observer
);
57 void WindowAndroid::AttachCompositor(WindowAndroidCompositor
* compositor
) {
58 if (compositor_
&& compositor
!= compositor_
)
61 compositor_
= compositor
;
62 FOR_EACH_OBSERVER(WindowAndroidObserver
,
64 OnAttachCompositor());
67 void WindowAndroid::DetachCompositor() {
69 FOR_EACH_OBSERVER(WindowAndroidObserver
,
71 OnDetachCompositor());
72 observer_list_
.Clear();
75 void WindowAndroid::RequestVSyncUpdate() {
76 JNIEnv
* env
= AttachCurrentThread();
77 Java_WindowAndroid_requestVSyncUpdate(env
, GetJavaObject().obj());
80 void WindowAndroid::SetNeedsAnimate() {
82 compositor_
->SetNeedsAnimate();
85 void WindowAndroid::Animate(base::TimeTicks begin_frame_time
) {
87 WindowAndroidObserver
, observer_list_
, OnAnimate(begin_frame_time
));
90 void WindowAndroid::OnVSync(JNIEnv
* env
, jobject obj
, jlong time_micros
) {
91 base::TimeTicks
frame_time(base::TimeTicks::FromInternalValue(time_micros
));
93 WindowAndroidObserver
,
95 OnVSync(frame_time
, vsync_period_
));
97 compositor_
->OnVSync(frame_time
, vsync_period_
);
100 // ----------------------------------------------------------------------------
101 // Native JNI methods
102 // ----------------------------------------------------------------------------
104 jlong
Init(JNIEnv
* env
, jobject obj
, jlong vsync_period
) {
105 WindowAndroid
* window
= new WindowAndroid(env
, obj
, vsync_period
);
106 return reinterpret_cast<intptr_t>(window
);