Home | History | Annotate | Download | only in android
      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 "content/public/browser/android/layer_tree_build_helper.h"
     19 #include "jni/ContentViewRenderView_jni.h"
     20 #include "ui/gfx/android/java_bitmap.h"
     21 #include "ui/gfx/size.h"
     22 
     23 #include <android/bitmap.h>
     24 #include <android/native_window_jni.h>
     25 
     26 using base::android::ScopedJavaLocalRef;
     27 
     28 namespace content {
     29 
     30 namespace {
     31 
     32 class LayerTreeBuildHelperImpl : public LayerTreeBuildHelper {
     33  public:
     34   LayerTreeBuildHelperImpl() {}
     35   virtual ~LayerTreeBuildHelperImpl() {}
     36 
     37   virtual scoped_refptr<cc::Layer> GetLayerTree(
     38       scoped_refptr<cc::Layer> content_root_layer) OVERRIDE {
     39     return content_root_layer;
     40   }
     41 
     42  private:
     43   DISALLOW_COPY_AND_ASSIGN(LayerTreeBuildHelperImpl);
     44 };
     45 
     46 }  // anonymous namespace
     47 
     48 // static
     49 bool ContentViewRenderView::RegisterContentViewRenderView(JNIEnv* env) {
     50   return RegisterNativesImpl(env);
     51 }
     52 
     53 ContentViewRenderView::ContentViewRenderView(JNIEnv* env,
     54                                              jobject obj,
     55                                              gfx::NativeWindow root_window)
     56     : layer_tree_build_helper_(new LayerTreeBuildHelperImpl()),
     57       root_window_(root_window),
     58       current_surface_format_(0) {
     59   java_obj_.Reset(env, obj);
     60 }
     61 
     62 ContentViewRenderView::~ContentViewRenderView() {
     63 }
     64 
     65 void ContentViewRenderView::SetLayerTreeBuildHelper(JNIEnv* env,
     66                                                     jobject obj,
     67                                                     jlong native_build_helper) {
     68   CHECK(native_build_helper);
     69 
     70   LayerTreeBuildHelper* build_helper =
     71       reinterpret_cast<LayerTreeBuildHelper*>(native_build_helper);
     72   layer_tree_build_helper_.reset(build_helper);
     73 }
     74 // static
     75 static jlong Init(JNIEnv* env,
     76                   jobject obj,
     77                   jlong native_root_window) {
     78   gfx::NativeWindow root_window =
     79       reinterpret_cast<gfx::NativeWindow>(native_root_window);
     80   ContentViewRenderView* content_view_render_view =
     81       new ContentViewRenderView(env, obj, root_window);
     82   return reinterpret_cast<intptr_t>(content_view_render_view);
     83 }
     84 
     85 void ContentViewRenderView::Destroy(JNIEnv* env, jobject obj) {
     86   delete this;
     87 }
     88 
     89 void ContentViewRenderView::SetCurrentContentViewCore(
     90     JNIEnv* env, jobject obj, jlong native_content_view_core) {
     91   InitCompositor();
     92   ContentViewCoreImpl* content_view_core =
     93       reinterpret_cast<ContentViewCoreImpl*>(native_content_view_core);
     94   compositor_->SetRootLayer(content_view_core
     95                                 ? layer_tree_build_helper_->GetLayerTree(
     96                                       content_view_core->GetLayer())
     97                                 : scoped_refptr<cc::Layer>());
     98 }
     99 
    100 void ContentViewRenderView::SurfaceCreated(
    101     JNIEnv* env, jobject obj) {
    102   current_surface_format_ = 0;
    103   InitCompositor();
    104 }
    105 
    106 void ContentViewRenderView::SurfaceDestroyed(JNIEnv* env, jobject obj) {
    107   compositor_->SetSurface(NULL);
    108   current_surface_format_ = 0;
    109 }
    110 
    111 void ContentViewRenderView::SurfaceChanged(JNIEnv* env, jobject obj,
    112     jint format, jint width, jint height, jobject surface) {
    113   if (current_surface_format_ != format) {
    114     current_surface_format_ = format;
    115     compositor_->SetSurface(surface);
    116   }
    117   compositor_->SetWindowBounds(gfx::Size(width, height));
    118 }
    119 
    120 void ContentViewRenderView::SetOverlayVideoMode(
    121     JNIEnv* env, jobject obj, bool enabled) {
    122   compositor_->SetHasTransparentBackground(enabled);
    123 }
    124 
    125 void ContentViewRenderView::Layout() {
    126   JNIEnv* env = base::android::AttachCurrentThread();
    127   Java_ContentViewRenderView_onCompositorLayout(env, java_obj_.obj());
    128 }
    129 
    130 void ContentViewRenderView::OnSwapBuffersCompleted(int pending_swap_buffers) {
    131   JNIEnv* env = base::android::AttachCurrentThread();
    132   Java_ContentViewRenderView_onSwapBuffersCompleted(env, java_obj_.obj());
    133 }
    134 
    135 void ContentViewRenderView::InitCompositor() {
    136   if (!compositor_)
    137     compositor_.reset(Compositor::Create(this, root_window_));
    138 }
    139 }  // namespace content
    140