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