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/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/android/window_android_compositor.h"
13 #include "ui/android/window_android_observer.h"
17 using base::android::AttachCurrentThread
;
18 using base::android::ScopedJavaLocalRef
;
20 WindowAndroid::WindowAndroid(JNIEnv
* env
, jobject obj
) : compositor_(NULL
) {
21 java_window_
.Reset(env
, obj
);
24 void WindowAndroid::Destroy(JNIEnv
* env
, jobject obj
) {
28 ScopedJavaLocalRef
<jobject
> WindowAndroid::GetJavaObject() {
29 return base::android::ScopedJavaLocalRef
<jobject
>(java_window_
);
32 bool WindowAndroid::RegisterWindowAndroid(JNIEnv
* env
) {
33 return RegisterNativesImpl(env
);
36 WindowAndroid::~WindowAndroid() {
40 void WindowAndroid::OnCompositingDidCommit() {
41 FOR_EACH_OBSERVER(WindowAndroidObserver
,
43 OnCompositingDidCommit());
46 void WindowAndroid::OnVisibilityChanged(bool visible
) {
47 FOR_EACH_OBSERVER(WindowAndroidObserver
, observer_list_
,
48 OnRootWindowVisibilityChanged(visible
));
51 void WindowAndroid::AddObserver(WindowAndroidObserver
* observer
) {
52 if (!observer_list_
.HasObserver(observer
))
53 observer_list_
.AddObserver(observer
);
56 void WindowAndroid::RemoveObserver(WindowAndroidObserver
* observer
) {
57 observer_list_
.RemoveObserver(observer
);
60 void WindowAndroid::AttachCompositor(WindowAndroidCompositor
* compositor
) {
61 if (compositor_
&& compositor
!= compositor_
)
64 compositor_
= compositor
;
65 FOR_EACH_OBSERVER(WindowAndroidObserver
,
67 OnAttachCompositor());
70 void WindowAndroid::DetachCompositor() {
72 FOR_EACH_OBSERVER(WindowAndroidObserver
,
74 OnDetachCompositor());
75 observer_list_
.Clear();
78 void WindowAndroid::RequestVSyncUpdate() {
79 JNIEnv
* env
= AttachCurrentThread();
80 Java_WindowAndroid_requestVSyncUpdate(env
, GetJavaObject().obj());
83 void WindowAndroid::SetNeedsAnimate() {
85 compositor_
->SetNeedsAnimate();
88 void WindowAndroid::Animate(base::TimeTicks begin_frame_time
) {
90 WindowAndroidObserver
, observer_list_
, OnAnimate(begin_frame_time
));
93 void WindowAndroid::OnVSync(JNIEnv
* env
,
96 jlong period_micros
) {
97 base::TimeTicks
frame_time(base::TimeTicks::FromInternalValue(time_micros
));
98 base::TimeDelta
vsync_period(
99 base::TimeDelta::FromMicroseconds(period_micros
));
101 WindowAndroidObserver
,
103 OnVSync(frame_time
, vsync_period
));
105 compositor_
->OnVSync(frame_time
, vsync_period
);
108 void WindowAndroid::OnActivityResumed(JNIEnv
* env
, jobject obj
) {
109 FOR_EACH_OBSERVER(WindowAndroidObserver
, observer_list_
, OnActivityResumed());
112 void WindowAndroid::OnActivityPaused(JNIEnv
* env
, jobject obj
) {
113 FOR_EACH_OBSERVER(WindowAndroidObserver
, observer_list_
, OnActivityPaused());
116 // ----------------------------------------------------------------------------
117 // Native JNI methods
118 // ----------------------------------------------------------------------------
120 jlong
Init(JNIEnv
* env
, jobject obj
) {
121 WindowAndroid
* window
= new WindowAndroid(env
, obj
);
122 return reinterpret_cast<intptr_t>(window
);