| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "blimp/client/app/android/blimp_contents_display.h" | 5 #include "blimp/client/app/android/blimp_contents_display.h" |
| 6 | 6 |
| 7 #include <android/native_window_jni.h> | 7 #include <android/native_window_jni.h> |
| 8 | 8 |
| 9 #include "base/bind.h" |
| 9 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 10 #include "blimp/client/app/android/blimp_client_session_android.h" | 11 #include "blimp/client/app/android/blimp_environment.h" |
| 11 #include "blimp/client/app/compositor/browser_compositor.h" | 12 #include "blimp/client/app/compositor/browser_compositor.h" |
| 12 #include "blimp/client/core/compositor/blimp_compositor_dependencies.h" | 13 #include "blimp/client/public/compositor/compositor_dependencies.h" |
| 13 #include "blimp/client/core/render_widget/blimp_document_manager.h" | 14 #include "blimp/client/public/contents/blimp_contents.h" |
| 14 #include "blimp/client/core/render_widget/render_widget_feature.h" | 15 #include "blimp/client/public/contents/blimp_contents_view.h" |
| 15 #include "blimp/client/support/compositor/compositor_dependencies_impl.h" | 16 #include "blimp/client/support/compositor/compositor_dependencies_impl.h" |
| 16 #include "jni/BlimpContentsDisplay_jni.h" | 17 #include "jni/BlimpContentsDisplay_jni.h" |
| 18 #include "ui/android/view_android.h" |
| 17 #include "ui/events/android/motion_event_android.h" | 19 #include "ui/events/android/motion_event_android.h" |
| 18 #include "ui/gfx/geometry/size.h" | 20 #include "ui/gfx/geometry/size.h" |
| 19 | 21 |
| 20 namespace { | |
| 21 const int kDummyBlimpContentsId = 0; | |
| 22 } // namespace | |
| 23 | |
| 24 namespace blimp { | 22 namespace blimp { |
| 25 namespace client { | 23 namespace client { |
| 26 namespace app { | 24 namespace app { |
| 27 | 25 |
| 28 static jlong Init( | 26 static jlong Init( |
| 29 JNIEnv* env, | 27 JNIEnv* env, |
| 30 const base::android::JavaParamRef<jobject>& jobj, | 28 const base::android::JavaParamRef<jobject>& jobj, |
| 31 const base::android::JavaParamRef<jobject>& blimp_client_session, | 29 const base::android::JavaParamRef<jobject>& jblimp_environment, |
| 30 const base::android::JavaParamRef<jobject>& jblimp_contents, |
| 32 jint real_width, | 31 jint real_width, |
| 33 jint real_height, | 32 jint real_height, |
| 34 jint width, | 33 jint width, |
| 35 jint height, | 34 jint height) { |
| 36 jfloat dp_to_px) { | 35 BlimpEnvironment* blimp_environment = |
| 37 BlimpClientSession* client_session = | 36 BlimpEnvironment::FromJavaObject(env, jblimp_environment); |
| 38 BlimpClientSessionAndroid::FromJavaObject(env, blimp_client_session); | 37 BlimpContents* blimp_contents = |
| 39 | 38 BlimpContents::FromJavaObject(env, jblimp_contents); |
| 40 // TODO(dtrainor): Pull the feature object from the BlimpClientSession and | |
| 41 // pass it through to the BlimpCompositor. | |
| 42 ALLOW_UNUSED_LOCAL(client_session); | |
| 43 | |
| 44 return reinterpret_cast<intptr_t>(new BlimpContentsDisplay( | 39 return reinterpret_cast<intptr_t>(new BlimpContentsDisplay( |
| 45 env, jobj, gfx::Size(real_width, real_height), gfx::Size(width, height), | 40 env, jobj, gfx::Size(real_width, real_height), gfx::Size(width, height), |
| 46 dp_to_px, client_session->GetRenderWidgetFeature())); | 41 blimp_environment, blimp_contents)); |
| 47 } | 42 } |
| 48 | 43 |
| 49 // static | 44 // static |
| 50 bool BlimpContentsDisplay::RegisterJni(JNIEnv* env) { | 45 bool BlimpContentsDisplay::RegisterJni(JNIEnv* env) { |
| 51 return RegisterNativesImpl(env); | 46 return RegisterNativesImpl(env); |
| 52 } | 47 } |
| 53 | 48 |
| 54 BlimpContentsDisplay::BlimpContentsDisplay( | 49 BlimpContentsDisplay::BlimpContentsDisplay( |
| 55 JNIEnv* env, | 50 JNIEnv* env, |
| 56 const base::android::JavaParamRef<jobject>& jobj, | 51 const base::android::JavaParamRef<jobject>& jobj, |
| 57 const gfx::Size& real_size, | 52 const gfx::Size& real_size, |
| 58 const gfx::Size& size, | 53 const gfx::Size& size, |
| 59 float dp_to_px, | 54 BlimpEnvironment* blimp_environment, |
| 60 blimp::client::RenderWidgetFeature* render_widget_feature) | 55 BlimpContents* blimp_contents) |
| 61 : device_scale_factor_(dp_to_px), | 56 : blimp_contents_(blimp_contents), |
| 62 current_surface_format_(0), | 57 current_surface_format_(0), |
| 63 window_(gfx::kNullAcceleratedWidget), | 58 window_(gfx::kNullAcceleratedWidget), |
| 64 weak_ptr_factory_(this) { | 59 weak_ptr_factory_(this) { |
| 65 compositor_dependencies_ = base::MakeUnique<BlimpCompositorDependencies>( | 60 DCHECK(blimp_contents_); |
| 66 base::MakeUnique<CompositorDependenciesImpl>()); | |
| 67 | 61 |
| 68 compositor_ = base::MakeUnique<BrowserCompositor>( | 62 compositor_dependencies_ = blimp_environment->CreateCompositorDepencencies(); |
| 69 compositor_dependencies_->GetEmbedderDependencies()); | 63 |
| 64 compositor_ = |
| 65 base::MakeUnique<BrowserCompositor>(compositor_dependencies_.get()); |
| 70 compositor_->set_did_complete_swap_buffers_callback( | 66 compositor_->set_did_complete_swap_buffers_callback( |
| 71 base::Bind(&BlimpContentsDisplay::OnSwapBuffersCompleted, | 67 base::Bind(&BlimpContentsDisplay::OnSwapBuffersCompleted, |
| 72 weak_ptr_factory_.GetWeakPtr())); | 68 weak_ptr_factory_.GetWeakPtr())); |
| 73 | 69 |
| 74 document_manager_ = base::MakeUnique<BlimpDocumentManager>( | 70 compositor_->SetContentLayer( |
| 75 kDummyBlimpContentsId, render_widget_feature, | 71 blimp_contents_->GetView()->GetNativeView()->GetLayer()); |
| 76 compositor_dependencies_.get()); | |
| 77 compositor_->SetContentLayer(document_manager_->layer()); | |
| 78 | 72 |
| 79 java_obj_.Reset(env, jobj); | 73 java_obj_.Reset(env, jobj); |
| 80 } | 74 } |
| 81 | 75 |
| 82 BlimpContentsDisplay::~BlimpContentsDisplay() { | 76 BlimpContentsDisplay::~BlimpContentsDisplay() { |
| 83 SetSurface(nullptr); | 77 SetSurface(nullptr); |
| 84 | 78 |
| 85 // Destroy the BrowserCompositor and the BlimpCompositorManager before the | 79 // Destroy the BrowserCompositor before the BlimpCompositorDependencies. |
| 86 // BlimpCompositorDependencies. | |
| 87 compositor_.reset(); | 80 compositor_.reset(); |
| 88 document_manager_.reset(); | |
| 89 compositor_dependencies_.reset(); | 81 compositor_dependencies_.reset(); |
| 90 } | 82 } |
| 91 | 83 |
| 92 void BlimpContentsDisplay::Destroy( | 84 void BlimpContentsDisplay::Destroy( |
| 93 JNIEnv* env, | 85 JNIEnv* env, |
| 94 const base::android::JavaParamRef<jobject>& jobj) { | 86 const base::android::JavaParamRef<jobject>& jobj) { |
| 95 delete this; | 87 delete this; |
| 96 } | 88 } |
| 97 | 89 |
| 98 void BlimpContentsDisplay::OnContentAreaSizeChanged( | 90 void BlimpContentsDisplay::OnContentAreaSizeChanged( |
| 99 JNIEnv* env, | 91 JNIEnv* env, |
| 100 const base::android::JavaParamRef<jobject>& jobj, | 92 const base::android::JavaParamRef<jobject>& jobj, |
| 101 jint width, | 93 jint width, |
| 102 jint height, | 94 jint height, |
| 103 jfloat dpToPx) { | 95 jfloat dpToPx) { |
| 104 compositor_->SetSize(gfx::Size(width, height)); | 96 compositor_->SetSize(gfx::Size(width, height)); |
| 105 } | 97 } |
| 106 | 98 |
| 107 void BlimpContentsDisplay::OnSurfaceChanged( | 99 void BlimpContentsDisplay::OnSurfaceChanged( |
| 108 JNIEnv* env, | 100 JNIEnv* env, |
| 109 const base::android::JavaParamRef<jobject>& jobj, | 101 const base::android::JavaParamRef<jobject>& jobj, |
| 110 jint format, | 102 jint format, |
| 111 jint width, | 103 jint width, |
| 112 jint height, | 104 jint height, |
| 113 const base::android::JavaParamRef<jobject>& jsurface) { | 105 const base::android::JavaParamRef<jobject>& jsurface) { |
| 114 if (current_surface_format_ != format) { | 106 if (current_surface_format_ == format) { |
| 115 current_surface_format_ = format; | 107 return; |
| 116 SetSurface(nullptr); | 108 } |
| 117 | 109 |
| 118 if (jsurface) { | 110 current_surface_format_ = format; |
| 119 SetSurface(jsurface); | 111 SetSurface(nullptr); |
| 120 } | 112 |
| 113 if (jsurface) { |
| 114 SetSurface(jsurface); |
| 121 } | 115 } |
| 122 } | 116 } |
| 123 | 117 |
| 124 void BlimpContentsDisplay::OnSurfaceCreated( | 118 void BlimpContentsDisplay::OnSurfaceCreated( |
| 125 JNIEnv* env, | 119 JNIEnv* env, |
| 126 const base::android::JavaParamRef<jobject>& jobj) { | 120 const base::android::JavaParamRef<jobject>& jobj) { |
| 127 current_surface_format_ = 0 /** PixelFormat.UNKNOWN */; | 121 current_surface_format_ = 0 /** PixelFormat.UNKNOWN */; |
| 128 } | 122 } |
| 129 | 123 |
| 130 void BlimpContentsDisplay::OnSurfaceDestroyed( | 124 void BlimpContentsDisplay::OnSurfaceDestroyed( |
| 131 JNIEnv* env, | 125 JNIEnv* env, |
| 132 const base::android::JavaParamRef<jobject>& jobj) { | 126 const base::android::JavaParamRef<jobject>& jobj) { |
| 133 current_surface_format_ = 0 /** PixelFormat.UNKNOWN */; | 127 current_surface_format_ = 0 /** PixelFormat.UNKNOWN */; |
| 134 SetSurface(nullptr); | 128 SetSurface(nullptr); |
| 135 } | 129 } |
| 136 | 130 |
| 137 void BlimpContentsDisplay::SetSurface(jobject surface) { | 131 void BlimpContentsDisplay::SetSurface(jobject surface) { |
| 138 JNIEnv* env = base::android::AttachCurrentThread(); | 132 JNIEnv* env = base::android::AttachCurrentThread(); |
| 139 // Release all references to the old surface. | 133 // Release all references to the old surface. |
| 140 if (window_ != gfx::kNullAcceleratedWidget) { | 134 if (window_ != gfx::kNullAcceleratedWidget) { |
| 141 compositor_->SetAcceleratedWidget(gfx::kNullAcceleratedWidget); | 135 compositor_->SetAcceleratedWidget(gfx::kNullAcceleratedWidget); |
| 142 document_manager_->SetVisible(false); | 136 blimp_contents_->Hide(); |
| 143 ANativeWindow_release(window_); | 137 ANativeWindow_release(window_); |
| 144 window_ = gfx::kNullAcceleratedWidget; | 138 window_ = gfx::kNullAcceleratedWidget; |
| 145 } | 139 } |
| 146 | 140 |
| 147 if (surface) { | 141 if (surface) { |
| 148 base::android::ScopedJavaLocalFrame scoped_local_reference_frame(env); | 142 base::android::ScopedJavaLocalFrame scoped_local_reference_frame(env); |
| 149 window_ = ANativeWindow_fromSurface(env, surface); | 143 window_ = ANativeWindow_fromSurface(env, surface); |
| 150 compositor_->SetAcceleratedWidget(window_); | 144 compositor_->SetAcceleratedWidget(window_); |
| 151 document_manager_->SetVisible(true); | 145 blimp_contents_->Show(); |
| 152 } | 146 } |
| 153 } | 147 } |
| 154 | 148 |
| 155 jboolean BlimpContentsDisplay::OnTouchEvent( | |
| 156 JNIEnv* env, | |
| 157 const base::android::JavaParamRef<jobject>& obj, | |
| 158 const base::android::JavaParamRef<jobject>& motion_event, | |
| 159 jlong time_ms, | |
| 160 jint android_action, | |
| 161 jint pointer_count, | |
| 162 jint history_size, | |
| 163 jint action_index, | |
| 164 jfloat pos_x_0, | |
| 165 jfloat pos_y_0, | |
| 166 jfloat pos_x_1, | |
| 167 jfloat pos_y_1, | |
| 168 jint pointer_id_0, | |
| 169 jint pointer_id_1, | |
| 170 jfloat touch_major_0, | |
| 171 jfloat touch_major_1, | |
| 172 jfloat touch_minor_0, | |
| 173 jfloat touch_minor_1, | |
| 174 jfloat orientation_0, | |
| 175 jfloat orientation_1, | |
| 176 jfloat tilt_0, | |
| 177 jfloat tilt_1, | |
| 178 jfloat raw_pos_x, | |
| 179 jfloat raw_pos_y, | |
| 180 jint android_tool_type_0, | |
| 181 jint android_tool_type_1, | |
| 182 jint android_button_state, | |
| 183 jint android_meta_state) { | |
| 184 ui::MotionEventAndroid::Pointer pointer0( | |
| 185 pointer_id_0, pos_x_0, pos_y_0, touch_major_0, touch_minor_0, | |
| 186 orientation_0, tilt_0, android_tool_type_0); | |
| 187 ui::MotionEventAndroid::Pointer pointer1( | |
| 188 pointer_id_1, pos_x_1, pos_y_1, touch_major_1, touch_minor_1, | |
| 189 orientation_1, tilt_1, android_tool_type_1); | |
| 190 ui::MotionEventAndroid event(1.f / device_scale_factor_, env, motion_event, | |
| 191 time_ms, android_action, pointer_count, | |
| 192 history_size, action_index, android_button_state, | |
| 193 android_meta_state, raw_pos_x - pos_x_0, | |
| 194 raw_pos_y - pos_y_0, &pointer0, &pointer1); | |
| 195 | |
| 196 return document_manager_->OnTouchEvent(event); | |
| 197 } | |
| 198 | |
| 199 void BlimpContentsDisplay::OnSwapBuffersCompleted() { | 149 void BlimpContentsDisplay::OnSwapBuffersCompleted() { |
| 200 JNIEnv* env = base::android::AttachCurrentThread(); | 150 JNIEnv* env = base::android::AttachCurrentThread(); |
| 201 Java_BlimpContentsDisplay_onSwapBuffersCompleted(env, java_obj_); | 151 Java_BlimpContentsDisplay_onSwapBuffersCompleted(env, java_obj_); |
| 202 } | 152 } |
| 203 | 153 |
| 204 } // namespace app | 154 } // namespace app |
| 205 } // namespace client | 155 } // namespace client |
| 206 } // namespace blimp | 156 } // namespace blimp |
| OLD | NEW |