OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/android/window_android.h" | 5 #include "ui/android/window_android.h" |
6 | 6 |
| 7 #include <stdint.h> |
| 8 #include <unordered_set> |
| 9 |
7 #include "base/android/context_utils.h" | 10 #include "base/android/context_utils.h" |
8 #include "base/android/jni_android.h" | 11 #include "base/android/jni_android.h" |
9 #include "base/android/jni_array.h" | 12 #include "base/android/jni_array.h" |
10 #include "base/android/jni_string.h" | 13 #include "base/android/jni_string.h" |
11 #include "base/android/jni_weak_ref.h" | 14 #include "base/android/jni_weak_ref.h" |
12 #include "base/android/scoped_java_ref.h" | 15 #include "base/android/scoped_java_ref.h" |
| 16 #include "base/auto_reset.h" |
| 17 #include "base/stl_util.h" |
| 18 #include "cc/output/begin_frame_args.h" |
| 19 #include "cc/scheduler/begin_frame_source.h" |
13 #include "jni/WindowAndroid_jni.h" | 20 #include "jni/WindowAndroid_jni.h" |
14 #include "ui/android/window_android_compositor.h" | 21 #include "ui/android/window_android_compositor.h" |
15 #include "ui/android/window_android_observer.h" | 22 #include "ui/android/window_android_observer.h" |
16 | 23 |
17 namespace ui { | 24 namespace ui { |
18 | 25 |
19 using base::android::AttachCurrentThread; | 26 using base::android::AttachCurrentThread; |
20 using base::android::JavaParamRef; | 27 using base::android::JavaParamRef; |
21 using base::android::JavaRef; | 28 using base::android::JavaRef; |
22 using base::android::ScopedJavaLocalRef; | 29 using base::android::ScopedJavaLocalRef; |
23 | 30 |
| 31 class WindowAndroid::WindowBeginFrameSource : public cc::BeginFrameSource { |
| 32 public: |
| 33 explicit WindowBeginFrameSource(WindowAndroid* window) |
| 34 : window_(window), |
| 35 next_sequence_number_(cc::BeginFrameArgs::kStartingFrameNumber), |
| 36 in_on_vsync_(false) {} |
| 37 ~WindowBeginFrameSource() override {} |
| 38 |
| 39 // cc::BeginFrameSource implementation. |
| 40 void AddObserver(cc::BeginFrameObserver* obs) override; |
| 41 void RemoveObserver(cc::BeginFrameObserver* obs) override; |
| 42 void DidFinishFrame(cc::BeginFrameObserver* obs, |
| 43 const cc::BeginFrameAck& ack) override {} |
| 44 bool IsThrottled() const override { return true; } |
| 45 |
| 46 void OnVSync(base::TimeTicks frame_time, |
| 47 base::TimeDelta vsync_period); |
| 48 |
| 49 private: |
| 50 WindowAndroid* const window_; |
| 51 std::unordered_set<cc::BeginFrameObserver*> observers_; |
| 52 cc::BeginFrameArgs last_begin_frame_args_; |
| 53 uint64_t next_sequence_number_; |
| 54 bool in_on_vsync_; |
| 55 }; |
| 56 |
| 57 void WindowAndroid::WindowBeginFrameSource::AddObserver( |
| 58 cc::BeginFrameObserver* obs) { |
| 59 DCHECK(obs); |
| 60 DCHECK(observers_.find(obs) == observers_.end()); |
| 61 |
| 62 observers_.insert(obs); |
| 63 obs->OnBeginFrameSourcePausedChanged(false); |
| 64 window_->OnNeedsBeginFramesChange(true); |
| 65 |
| 66 // Send a MISSED BeginFrame if possible and necessary. If an observer is added |
| 67 // during OnVSync(), it will get a NORMAL BeginFrame from OnVSync() instead. |
| 68 if (!in_on_vsync_ && last_begin_frame_args_.IsValid()) { |
| 69 cc::BeginFrameArgs last_args = obs->LastUsedBeginFrameArgs(); |
| 70 if (!last_args.IsValid() || |
| 71 last_args.sequence_number < last_begin_frame_args_.sequence_number) { |
| 72 last_begin_frame_args_.type = cc::BeginFrameArgs::MISSED; |
| 73 // TODO(crbug.com/602485): A deadline doesn't make too much sense |
| 74 // for a missed BeginFrame (the intention rather is 'immediately'), |
| 75 // but currently the retro frame logic is very strict in discarding |
| 76 // BeginFrames. |
| 77 last_begin_frame_args_.deadline = |
| 78 base::TimeTicks::Now() + last_begin_frame_args_.interval; |
| 79 obs->OnBeginFrame(last_begin_frame_args_); |
| 80 } |
| 81 } |
| 82 } |
| 83 |
| 84 void WindowAndroid::WindowBeginFrameSource::RemoveObserver( |
| 85 cc::BeginFrameObserver* obs) { |
| 86 DCHECK(obs); |
| 87 DCHECK(observers_.find(obs) != observers_.end()); |
| 88 |
| 89 observers_.erase(obs); |
| 90 if (observers_.empty()) |
| 91 window_->OnNeedsBeginFramesChange(false); |
| 92 } |
| 93 |
| 94 void WindowAndroid::WindowBeginFrameSource::OnVSync( |
| 95 base::TimeTicks frame_time, |
| 96 base::TimeDelta vsync_period) { |
| 97 base::AutoReset<bool> auto_reset(&in_on_vsync_, true); |
| 98 |
| 99 // frame time is in the past, so give the next vsync period as the deadline. |
| 100 base::TimeTicks deadline = frame_time + vsync_period; |
| 101 last_begin_frame_args_ = cc::BeginFrameArgs::Create( |
| 102 BEGINFRAME_FROM_HERE, source_id(), next_sequence_number_++, frame_time, |
| 103 deadline, vsync_period, cc::BeginFrameArgs::NORMAL); |
| 104 DCHECK(last_begin_frame_args_.IsValid()); |
| 105 |
| 106 // Support SynchronousCompositorBrowserFilter: Observers added during vsync |
| 107 // distribution receive a NORMAL args in addition to the missed ones, after |
| 108 // all current observers were notified. We also have to support observers |
| 109 // removing themselves during OnBeginFrame, so we iterate on set copies until |
| 110 // all observers were notified once. |
| 111 std::unordered_set<cc::BeginFrameObserver*> notified_observers; |
| 112 bool observer_was_notified = true; |
| 113 while (observer_was_notified) { |
| 114 observer_was_notified = false; |
| 115 std::unordered_set<cc::BeginFrameObserver*> observers(observers_); |
| 116 for (auto* obs : observers) { |
| 117 if (notified_observers.find(obs) == notified_observers.end()) { |
| 118 obs->OnBeginFrame(last_begin_frame_args_); |
| 119 notified_observers.insert(obs); |
| 120 observer_was_notified = true; |
| 121 } |
| 122 } |
| 123 } |
| 124 } |
| 125 |
24 WindowAndroid::WindowAndroid(JNIEnv* env, jobject obj, int display_id) | 126 WindowAndroid::WindowAndroid(JNIEnv* env, jobject obj, int display_id) |
25 : display_id_(display_id), compositor_(NULL) { | 127 : display_id_(display_id), |
| 128 compositor_(NULL), |
| 129 begin_frame_source_(new WindowBeginFrameSource(this)), |
| 130 needs_begin_frames_(false) { |
26 java_window_.Reset(env, obj); | 131 java_window_.Reset(env, obj); |
27 } | 132 } |
28 | 133 |
29 void WindowAndroid::Destroy(JNIEnv* env, const JavaParamRef<jobject>& obj) { | 134 void WindowAndroid::Destroy(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
30 delete this; | 135 delete this; |
31 } | 136 } |
32 | 137 |
33 ScopedJavaLocalRef<jobject> WindowAndroid::GetJavaObject() { | 138 ScopedJavaLocalRef<jobject> WindowAndroid::GetJavaObject() { |
34 return base::android::ScopedJavaLocalRef<jobject>(java_window_); | 139 return base::android::ScopedJavaLocalRef<jobject>(java_window_); |
35 } | 140 } |
(...skipping 26 matching lines...) Expand all Loading... |
62 | 167 |
63 void WindowAndroid::AddObserver(WindowAndroidObserver* observer) { | 168 void WindowAndroid::AddObserver(WindowAndroidObserver* observer) { |
64 if (!observer_list_.HasObserver(observer)) | 169 if (!observer_list_.HasObserver(observer)) |
65 observer_list_.AddObserver(observer); | 170 observer_list_.AddObserver(observer); |
66 } | 171 } |
67 | 172 |
68 void WindowAndroid::RemoveObserver(WindowAndroidObserver* observer) { | 173 void WindowAndroid::RemoveObserver(WindowAndroidObserver* observer) { |
69 observer_list_.RemoveObserver(observer); | 174 observer_list_.RemoveObserver(observer); |
70 } | 175 } |
71 | 176 |
| 177 cc::BeginFrameSource* WindowAndroid::GetBeginFrameSource() { |
| 178 return begin_frame_source_.get(); |
| 179 } |
| 180 |
72 void WindowAndroid::AttachCompositor(WindowAndroidCompositor* compositor) { | 181 void WindowAndroid::AttachCompositor(WindowAndroidCompositor* compositor) { |
73 if (compositor_ && compositor != compositor_) | 182 if (compositor_ && compositor != compositor_) |
74 DetachCompositor(); | 183 DetachCompositor(); |
75 | 184 |
76 compositor_ = compositor; | 185 compositor_ = compositor; |
77 for (WindowAndroidObserver& observer : observer_list_) | 186 for (WindowAndroidObserver& observer : observer_list_) |
78 observer.OnAttachCompositor(); | 187 observer.OnAttachCompositor(); |
79 } | 188 } |
80 | 189 |
81 void WindowAndroid::DetachCompositor() { | 190 void WindowAndroid::DetachCompositor() { |
(...skipping 18 matching lines...) Expand all Loading... |
100 observer.OnAnimate(begin_frame_time); | 209 observer.OnAnimate(begin_frame_time); |
101 } | 210 } |
102 | 211 |
103 void WindowAndroid::OnVSync(JNIEnv* env, | 212 void WindowAndroid::OnVSync(JNIEnv* env, |
104 const JavaParamRef<jobject>& obj, | 213 const JavaParamRef<jobject>& obj, |
105 jlong time_micros, | 214 jlong time_micros, |
106 jlong period_micros) { | 215 jlong period_micros) { |
107 base::TimeTicks frame_time(base::TimeTicks::FromInternalValue(time_micros)); | 216 base::TimeTicks frame_time(base::TimeTicks::FromInternalValue(time_micros)); |
108 base::TimeDelta vsync_period( | 217 base::TimeDelta vsync_period( |
109 base::TimeDelta::FromMicroseconds(period_micros)); | 218 base::TimeDelta::FromMicroseconds(period_micros)); |
110 for (WindowAndroidObserver& observer : observer_list_) | 219 begin_frame_source_->OnVSync(frame_time, vsync_period); |
111 observer.OnVSync(frame_time, vsync_period); | 220 if (needs_begin_frames_) |
112 if (compositor_) | 221 RequestVSyncUpdate(); |
113 compositor_->OnVSync(frame_time, vsync_period); | |
114 } | 222 } |
115 | 223 |
116 void WindowAndroid::OnVisibilityChanged(JNIEnv* env, | 224 void WindowAndroid::OnVisibilityChanged(JNIEnv* env, |
117 const JavaParamRef<jobject>& obj, | 225 const JavaParamRef<jobject>& obj, |
118 bool visible) { | 226 bool visible) { |
119 for (WindowAndroidObserver& observer : observer_list_) | 227 for (WindowAndroidObserver& observer : observer_list_) |
120 observer.OnRootWindowVisibilityChanged(visible); | 228 observer.OnRootWindowVisibilityChanged(visible); |
121 } | 229 } |
122 | 230 |
123 void WindowAndroid::OnActivityStopped(JNIEnv* env, | 231 void WindowAndroid::OnActivityStopped(JNIEnv* env, |
(...skipping 15 matching lines...) Expand all Loading... |
139 base::android::ConvertUTF8ToJavaString(env, permission)); | 247 base::android::ConvertUTF8ToJavaString(env, permission)); |
140 } | 248 } |
141 | 249 |
142 bool WindowAndroid::CanRequestPermission(const std::string& permission) { | 250 bool WindowAndroid::CanRequestPermission(const std::string& permission) { |
143 JNIEnv* env = AttachCurrentThread(); | 251 JNIEnv* env = AttachCurrentThread(); |
144 return Java_WindowAndroid_canRequestPermission( | 252 return Java_WindowAndroid_canRequestPermission( |
145 env, GetJavaObject(), | 253 env, GetJavaObject(), |
146 base::android::ConvertUTF8ToJavaString(env, permission)); | 254 base::android::ConvertUTF8ToJavaString(env, permission)); |
147 } | 255 } |
148 | 256 |
| 257 void WindowAndroid::OnNeedsBeginFramesChange(bool needs_begin_frames) { |
| 258 if (needs_begin_frames_ == needs_begin_frames) |
| 259 return; |
| 260 |
| 261 needs_begin_frames_ = needs_begin_frames; |
| 262 if (needs_begin_frames_) |
| 263 RequestVSyncUpdate(); |
| 264 } |
| 265 |
149 WindowAndroid* WindowAndroid::GetWindowAndroid() const { | 266 WindowAndroid* WindowAndroid::GetWindowAndroid() const { |
150 DCHECK(parent_ == nullptr); | 267 DCHECK(parent_ == nullptr); |
151 return const_cast<WindowAndroid*>(this); | 268 return const_cast<WindowAndroid*>(this); |
152 } | 269 } |
153 | 270 |
154 // ---------------------------------------------------------------------------- | 271 // ---------------------------------------------------------------------------- |
155 // Native JNI methods | 272 // Native JNI methods |
156 // ---------------------------------------------------------------------------- | 273 // ---------------------------------------------------------------------------- |
157 | 274 |
158 jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj, int sdk_display_id) { | 275 jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj, int sdk_display_id) { |
159 WindowAndroid* window = new WindowAndroid(env, obj, sdk_display_id); | 276 WindowAndroid* window = new WindowAndroid(env, obj, sdk_display_id); |
160 return reinterpret_cast<intptr_t>(window); | 277 return reinterpret_cast<intptr_t>(window); |
161 } | 278 } |
162 | 279 |
163 } // namespace ui | 280 } // namespace ui |
OLD | NEW |