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
)
21 : weak_java_window_(env
, obj
), compositor_(NULL
) {
24 void WindowAndroid::Destroy(JNIEnv
* env
, jobject obj
) {
28 ScopedJavaLocalRef
<jobject
> WindowAndroid::GetJavaObject() {
29 return weak_java_window_
.get(AttachCurrentThread());
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::AddObserver(WindowAndroidObserver
* observer
) {
47 if (!observer_list_
.HasObserver(observer
))
48 observer_list_
.AddObserver(observer
);
51 void WindowAndroid::RemoveObserver(WindowAndroidObserver
* observer
) {
52 observer_list_
.RemoveObserver(observer
);
55 void WindowAndroid::AttachCompositor(WindowAndroidCompositor
* compositor
) {
56 if (compositor_
&& compositor
!= compositor_
)
59 compositor_
= compositor
;
60 FOR_EACH_OBSERVER(WindowAndroidObserver
,
62 OnAttachCompositor());
65 void WindowAndroid::DetachCompositor() {
67 FOR_EACH_OBSERVER(WindowAndroidObserver
,
69 OnDetachCompositor());
70 observer_list_
.Clear();
73 void WindowAndroid::RequestVSyncUpdate() {
74 JNIEnv
* env
= AttachCurrentThread();
75 Java_WindowAndroid_requestVSyncUpdate(env
, GetJavaObject().obj());
78 void WindowAndroid::SetNeedsAnimate() {
80 compositor_
->SetNeedsAnimate();
83 void WindowAndroid::Animate(base::TimeTicks begin_frame_time
) {
85 WindowAndroidObserver
, observer_list_
, OnAnimate(begin_frame_time
));
88 void WindowAndroid::OnVSync(JNIEnv
* env
,
91 jlong period_micros
) {
92 base::TimeTicks
frame_time(base::TimeTicks::FromInternalValue(time_micros
));
93 base::TimeDelta
vsync_period(
94 base::TimeDelta::FromMicroseconds(period_micros
));
96 WindowAndroidObserver
,
98 OnVSync(frame_time
, vsync_period
));
100 compositor_
->OnVSync(frame_time
, vsync_period
);
103 // ----------------------------------------------------------------------------
104 // Native JNI methods
105 // ----------------------------------------------------------------------------
107 jlong
Init(JNIEnv
* env
, jobject obj
) {
108 WindowAndroid
* window
= new WindowAndroid(env
, obj
);
109 return reinterpret_cast<intptr_t>(window
);