Home | History | Annotate | Download | only in android
      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.
      4 
      5 #include "ui/base/android/window_android.h"
      6 
      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"
     14 
     15 namespace ui {
     16 
     17 using base::android::AttachCurrentThread;
     18 using base::android::ScopedJavaLocalRef;
     19 
     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)) {
     24 }
     25 
     26 void WindowAndroid::Destroy(JNIEnv* env, jobject obj) {
     27   delete this;
     28 }
     29 
     30 ScopedJavaLocalRef<jobject> WindowAndroid::GetJavaObject() {
     31   return weak_java_window_.get(AttachCurrentThread());
     32 }
     33 
     34 bool WindowAndroid::RegisterWindowAndroid(JNIEnv* env) {
     35   return RegisterNativesImpl(env);
     36 }
     37 
     38 WindowAndroid::~WindowAndroid() {
     39   DCHECK(!compositor_);
     40 }
     41 
     42 void WindowAndroid::OnCompositingDidCommit() {
     43   FOR_EACH_OBSERVER(WindowAndroidObserver,
     44                     observer_list_,
     45                     OnCompositingDidCommit());
     46 }
     47 
     48 void WindowAndroid::AddObserver(WindowAndroidObserver* observer) {
     49   if (!observer_list_.HasObserver(observer))
     50     observer_list_.AddObserver(observer);
     51 }
     52 
     53 void WindowAndroid::RemoveObserver(WindowAndroidObserver* observer) {
     54   observer_list_.RemoveObserver(observer);
     55 }
     56 
     57 void WindowAndroid::AttachCompositor(WindowAndroidCompositor* compositor) {
     58   if (compositor_ && compositor != compositor_)
     59     DetachCompositor();
     60 
     61   compositor_ = compositor;
     62   FOR_EACH_OBSERVER(WindowAndroidObserver,
     63                     observer_list_,
     64                     OnAttachCompositor());
     65 }
     66 
     67 void WindowAndroid::DetachCompositor() {
     68   compositor_ = NULL;
     69   FOR_EACH_OBSERVER(WindowAndroidObserver,
     70                     observer_list_,
     71                     OnDetachCompositor());
     72   observer_list_.Clear();
     73 }
     74 
     75 void WindowAndroid::RequestVSyncUpdate() {
     76   JNIEnv* env = AttachCurrentThread();
     77   Java_WindowAndroid_requestVSyncUpdate(env, GetJavaObject().obj());
     78 }
     79 
     80 void WindowAndroid::SetNeedsAnimate() {
     81   if (compositor_)
     82     compositor_->SetNeedsAnimate();
     83 }
     84 
     85 void WindowAndroid::Animate(base::TimeTicks begin_frame_time) {
     86   FOR_EACH_OBSERVER(
     87       WindowAndroidObserver, observer_list_, OnAnimate(begin_frame_time));
     88 }
     89 
     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_);
     98 }
     99 
    100 // ----------------------------------------------------------------------------
    101 // Native JNI methods
    102 // ----------------------------------------------------------------------------
    103 
    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);
    107 }
    108 
    109 }  // namespace ui
    110