Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / ui / base / android / window_android.cc
blob490c6f3b545f2adfaabf67d3e4efb2766fed84f9
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"
15 namespace ui {
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),
22 compositor_(NULL),
23 vsync_period_(base::TimeDelta::FromInternalValue(vsync_period)) {
26 void WindowAndroid::Destroy(JNIEnv* env, jobject obj) {
27 delete this;
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() {
39 DCHECK(!compositor_);
42 void WindowAndroid::OnCompositingDidCommit() {
43 FOR_EACH_OBSERVER(WindowAndroidObserver,
44 observer_list_,
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_)
59 DetachCompositor();
61 compositor_ = compositor;
62 FOR_EACH_OBSERVER(WindowAndroidObserver,
63 observer_list_,
64 OnAttachCompositor());
67 void WindowAndroid::DetachCompositor() {
68 compositor_ = NULL;
69 FOR_EACH_OBSERVER(WindowAndroidObserver,
70 observer_list_,
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() {
81 if (compositor_)
82 compositor_->SetNeedsAnimate();
85 void WindowAndroid::Animate(base::TimeTicks begin_frame_time) {
86 FOR_EACH_OBSERVER(
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));
92 FOR_EACH_OBSERVER(
93 WindowAndroidObserver,
94 observer_list_,
95 OnVSync(frame_time, vsync_period_));
96 if (compositor_)
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);
109 } // namespace ui