1 // Copyright (c) 2012 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 "content/browser/android/content_view_render_view.h" 6 7 #include "base/android/jni_android.h" 8 #include "base/android/jni_string.h" 9 #include "base/android/scoped_java_ref.h" 10 #include "base/bind.h" 11 #include "base/lazy_instance.h" 12 #include "base/memory/scoped_ptr.h" 13 #include "base/message_loop/message_loop.h" 14 #include "cc/layers/layer.h" 15 #include "content/browser/android/content_view_core_impl.h" 16 #include "content/public/browser/android/compositor.h" 17 #include "content/public/browser/android/content_view_layer_renderer.h" 18 #include "jni/ContentViewRenderView_jni.h" 19 #include "ui/gfx/size.h" 20 21 #include <android/native_window_jni.h> 22 23 using base::android::ScopedJavaLocalRef; 24 25 namespace content { 26 27 // static 28 bool ContentViewRenderView::RegisterContentViewRenderView(JNIEnv* env) { 29 return RegisterNativesImpl(env); 30 } 31 32 ContentViewRenderView::ContentViewRenderView() 33 : scheduled_composite_(false), 34 weak_factory_(this) { 35 } 36 37 ContentViewRenderView::~ContentViewRenderView() { 38 } 39 40 // static 41 jint Init(JNIEnv* env, jclass clazz) { 42 ContentViewRenderView* content_view_render_view = 43 new ContentViewRenderView(); 44 return reinterpret_cast<jint>(content_view_render_view); 45 } 46 47 void ContentViewRenderView::Destroy(JNIEnv* env, jobject obj) { 48 delete this; 49 } 50 51 void ContentViewRenderView::SetCurrentContentView( 52 JNIEnv* env, jobject obj, int native_content_view) { 53 InitCompositor(); 54 ContentViewCoreImpl* content_view = 55 reinterpret_cast<ContentViewCoreImpl*>(native_content_view); 56 if (content_view) 57 compositor_->SetRootLayer(content_view->GetLayer()); 58 } 59 60 void ContentViewRenderView::SurfaceCreated( 61 JNIEnv* env, jobject obj, jobject jsurface) { 62 InitCompositor(); 63 compositor_->SetSurface(jsurface); 64 } 65 66 void ContentViewRenderView::SurfaceDestroyed(JNIEnv* env, jobject obj) { 67 compositor_->SetSurface(NULL); 68 } 69 70 void ContentViewRenderView::SurfaceSetSize( 71 JNIEnv* env, jobject obj, jint width, jint height) { 72 compositor_->SetWindowBounds(gfx::Size(width, height)); 73 } 74 75 void ContentViewRenderView::ScheduleComposite() { 76 if (scheduled_composite_) 77 return; 78 79 scheduled_composite_ = true; 80 base::MessageLoop::current()->PostTask( 81 FROM_HERE, 82 base::Bind(&ContentViewRenderView::Composite, 83 weak_factory_.GetWeakPtr())); 84 } 85 86 void ContentViewRenderView::InitCompositor() { 87 if (!compositor_) 88 compositor_.reset(Compositor::Create(this)); 89 } 90 91 void ContentViewRenderView::Composite() { 92 if (!compositor_) 93 return; 94 95 scheduled_composite_ = false; 96 compositor_->Composite(); 97 } 98 99 } // namespace content 100