Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / content / browser / android / content_view_render_view.cc
blob00d9ad0c7b4ff9ce9df5c843b55ac49916c9cede
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.
5 #include "content/browser/android/content_view_render_view.h"
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"
24 #include <android/bitmap.h>
25 #include <android/native_window_jni.h>
27 using base::android::ScopedJavaLocalRef;
29 namespace content {
31 namespace {
33 class LayerTreeBuildHelperImpl : public LayerTreeBuildHelper {
34 public:
35 LayerTreeBuildHelperImpl() {}
36 virtual ~LayerTreeBuildHelperImpl() {}
38 virtual scoped_refptr<cc::Layer> GetLayerTree(
39 scoped_refptr<cc::Layer> content_root_layer) OVERRIDE {
40 return content_root_layer;
43 private:
44 DISALLOW_COPY_AND_ASSIGN(LayerTreeBuildHelperImpl);
47 } // anonymous namespace
49 // static
50 bool ContentViewRenderView::RegisterContentViewRenderView(JNIEnv* env) {
51 return RegisterNativesImpl(env);
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);
63 ContentViewRenderView::~ContentViewRenderView() {
66 void ContentViewRenderView::SetLayerTreeBuildHelper(JNIEnv* env,
67 jobject obj,
68 jlong native_build_helper) {
69 CHECK(native_build_helper);
71 LayerTreeBuildHelper* build_helper =
72 reinterpret_cast<LayerTreeBuildHelper*>(native_build_helper);
73 layer_tree_build_helper_.reset(build_helper);
74 InitCompositor();
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);
87 void ContentViewRenderView::Destroy(JNIEnv* env, jobject obj) {
88 delete this;
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>());
102 void ContentViewRenderView::SurfaceCreated(
103 JNIEnv* env, jobject obj) {
104 current_surface_format_ = 0;
105 InitCompositor();
108 void ContentViewRenderView::SurfaceDestroyed(JNIEnv* env, jobject obj) {
109 compositor_->SetSurface(NULL);
110 current_surface_format_ = 0;
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);
119 compositor_->SetWindowBounds(gfx::Size(width, height));
122 void ContentViewRenderView::SetOverlayVideoMode(
123 JNIEnv* env, jobject obj, bool enabled) {
124 compositor_->SetHasTransparentBackground(enabled);
125 SetNeedsComposite(env, obj);
128 void ContentViewRenderView::SetNeedsComposite(JNIEnv* env, jobject obj) {
129 if (compositor_)
130 compositor_->SetNeedsComposite();
133 void ContentViewRenderView::Layout() {
134 JNIEnv* env = base::android::AttachCurrentThread();
135 Java_ContentViewRenderView_onCompositorLayout(env, java_obj_.obj());
138 void ContentViewRenderView::OnSwapBuffersCompleted(int pending_swap_buffers) {
139 JNIEnv* env = base::android::AttachCurrentThread();
140 Java_ContentViewRenderView_onSwapBuffersCompleted(env, java_obj_.obj());
143 void ContentViewRenderView::InitCompositor() {
144 if (!compositor_)
145 compositor_.reset(Compositor::Create(this, root_window_));
148 jlong ContentViewRenderView::GetUIResourceProvider(JNIEnv* env,
149 jobject obj) {
150 if (!compositor_)
151 return 0;
152 return reinterpret_cast<intptr_t>(&compositor_->GetUIResourceProvider());
154 } // namespace content