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 "jni/ContentViewRenderView_jni.h"
20 #include "ui/gfx/android/java_bitmap.h"
21 #include "ui/gfx/size.h"
23 #include <android/bitmap.h>
24 #include <android/native_window_jni.h>
26 using base::android::ScopedJavaLocalRef
;
32 class LayerTreeBuildHelperImpl
: public LayerTreeBuildHelper
{
34 LayerTreeBuildHelperImpl() {}
35 virtual ~LayerTreeBuildHelperImpl() {}
37 virtual scoped_refptr
<cc::Layer
> GetLayerTree(
38 scoped_refptr
<cc::Layer
> content_root_layer
) OVERRIDE
{
39 return content_root_layer
;
43 DISALLOW_COPY_AND_ASSIGN(LayerTreeBuildHelperImpl
);
46 } // anonymous namespace
49 bool ContentViewRenderView::RegisterContentViewRenderView(JNIEnv
* env
) {
50 return RegisterNativesImpl(env
);
53 ContentViewRenderView::ContentViewRenderView(JNIEnv
* env
,
55 gfx::NativeWindow root_window
)
56 : buffers_swapped_during_composite_(false),
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
,
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
);
76 static jlong
Init(JNIEnv
* env
,
78 jlong native_root_window
) {
79 gfx::NativeWindow root_window
=
80 reinterpret_cast<gfx::NativeWindow
>(native_root_window
);
81 ContentViewRenderView
* content_view_render_view
=
82 new ContentViewRenderView(env
, obj
, root_window
);
83 return reinterpret_cast<intptr_t>(content_view_render_view
);
86 void ContentViewRenderView::Destroy(JNIEnv
* env
, jobject obj
) {
90 void ContentViewRenderView::SetCurrentContentViewCore(
91 JNIEnv
* env
, jobject obj
, jlong native_content_view_core
) {
93 ContentViewCoreImpl
* content_view_core
=
94 reinterpret_cast<ContentViewCoreImpl
*>(native_content_view_core
);
95 compositor_
->SetRootLayer(content_view_core
96 ? layer_tree_build_helper_
->GetLayerTree(
97 content_view_core
->GetLayer())
98 : scoped_refptr
<cc::Layer
>());
101 void ContentViewRenderView::SurfaceCreated(
102 JNIEnv
* env
, jobject obj
) {
103 current_surface_format_
= 0;
107 void ContentViewRenderView::SurfaceDestroyed(JNIEnv
* env
, jobject obj
) {
108 compositor_
->SetSurface(NULL
);
109 current_surface_format_
= 0;
112 void ContentViewRenderView::SurfaceChanged(JNIEnv
* env
, jobject obj
,
113 jint format
, jint width
, jint height
, jobject surface
) {
114 if (current_surface_format_
!= format
) {
115 current_surface_format_
= format
;
116 compositor_
->SetSurface(surface
);
118 compositor_
->SetWindowBounds(gfx::Size(width
, height
));
121 jboolean
ContentViewRenderView::Composite(JNIEnv
* env
, jobject obj
) {
125 buffers_swapped_during_composite_
= false;
126 compositor_
->Composite();
127 return buffers_swapped_during_composite_
;
130 jboolean
ContentViewRenderView::CompositeToBitmap(JNIEnv
* env
, jobject obj
,
131 jobject java_bitmap
) {
132 gfx::JavaBitmap
bitmap(java_bitmap
);
133 if (!compositor_
|| bitmap
.format() != ANDROID_BITMAP_FORMAT_RGBA_8888
)
135 return compositor_
->CompositeAndReadback(bitmap
.pixels(),
136 gfx::Rect(bitmap
.size()));
139 void ContentViewRenderView::SetOverlayVideoMode(
140 JNIEnv
* env
, jobject obj
, bool enabled
) {
141 compositor_
->SetHasTransparentBackground(enabled
);
142 Java_ContentViewRenderView_requestRender(env
, obj
);
145 void ContentViewRenderView::ScheduleComposite() {
146 JNIEnv
* env
= base::android::AttachCurrentThread();
147 Java_ContentViewRenderView_requestRender(env
, java_obj_
.obj());
150 void ContentViewRenderView::OnSwapBuffersPosted() {
151 buffers_swapped_during_composite_
= true;
154 void ContentViewRenderView::OnSwapBuffersCompleted() {
155 JNIEnv
* env
= base::android::AttachCurrentThread();
156 Java_ContentViewRenderView_onSwapBuffersCompleted(env
, java_obj_
.obj());
159 void ContentViewRenderView::InitCompositor() {
161 compositor_
.reset(Compositor::Create(this, root_window_
));
163 } // namespace content