1 // Copyright 2014 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_readback_handler.h"
7 #include "base/android/jni_android.h"
9 #include "cc/output/copy_output_request.h"
10 #include "cc/output/copy_output_result.h"
11 #include "content/browser/android/content_view_core_impl.h"
12 #include "jni/ContentReadbackHandler_jni.h"
13 #include "third_party/skia/include/core/SkBitmap.h"
14 #include "ui/base/android/window_android.h"
15 #include "ui/base/android/window_android_compositor.h"
16 #include "ui/gfx/android/java_bitmap.h"
17 #include "ui/gfx/rect.h"
23 typedef base::Callback
<void(bool, const SkBitmap
&)> ResultCallback
;
25 void OnFinishCopyOutputRequest(
26 const ResultCallback
& result_callback
,
27 scoped_ptr
<cc::CopyOutputResult
> copy_output_result
) {
28 if (!copy_output_result
->HasBitmap()) {
29 result_callback
.Run(false, SkBitmap());
33 scoped_ptr
<SkBitmap
> bitmap
= copy_output_result
->TakeBitmap();
34 result_callback
.Run(true, *bitmap
.Pass());
37 } // anonymous namespace
40 bool ContentReadbackHandler::RegisterContentReadbackHandler(JNIEnv
* env
) {
41 return RegisterNativesImpl(env
);
44 ContentReadbackHandler::ContentReadbackHandler(JNIEnv
* env
, jobject obj
)
45 : weak_factory_(this) {
46 java_obj_
.Reset(env
, obj
);
49 void ContentReadbackHandler::Destroy(JNIEnv
* env
, jobject obj
) {
53 void ContentReadbackHandler::GetContentBitmap(JNIEnv
* env
,
62 jobject content_view_core
) {
63 ContentViewCore
* view
=
64 ContentViewCore::GetNativeContentViewCore(env
, content_view_core
);
67 ResultCallback result_callback
=
68 base::Bind(&ContentReadbackHandler::OnFinishReadback
,
69 weak_factory_
.GetWeakPtr(),
72 view
->GetScaledContentBitmap(
73 scale
, config
, gfx::Rect(x
, y
, width
, height
), result_callback
);
76 void ContentReadbackHandler::GetCompositorBitmap(JNIEnv
* env
,
79 jlong native_window_android
) {
80 ui::WindowAndroid
* window_android
=
81 reinterpret_cast<ui::WindowAndroid
*>(native_window_android
);
82 DCHECK(window_android
);
84 ResultCallback result_callback
=
85 base::Bind(&ContentReadbackHandler::OnFinishReadback
,
86 weak_factory_
.GetWeakPtr(),
89 base::Callback
<void(scoped_ptr
<cc::CopyOutputResult
>)> copy_output_callback
=
90 base::Bind(&OnFinishCopyOutputRequest
,
93 ui::WindowAndroidCompositor
* compositor
= window_android
->GetCompositor();
96 copy_output_callback
.Run(cc::CopyOutputResult::CreateEmptyResult());
100 compositor
->RequestCopyOfOutputOnRootLayer(
101 cc::CopyOutputRequest::CreateBitmapRequest(copy_output_callback
));
104 ContentReadbackHandler::~ContentReadbackHandler() {}
106 void ContentReadbackHandler::OnFinishReadback(int readback_id
,
108 const SkBitmap
& bitmap
) {
109 JNIEnv
* env
= base::android::AttachCurrentThread();
110 ScopedJavaLocalRef
<jobject
> java_bitmap
;
112 java_bitmap
= gfx::ConvertToJavaBitmap(&bitmap
);
114 Java_ContentReadbackHandler_notifyGetBitmapFinished(
115 env
, java_obj_
.obj(), readback_id
, success
, java_bitmap
.obj());
119 static jlong
Init(JNIEnv
* env
, jobject obj
) {
120 ContentReadbackHandler
* content_readback_handler
=
121 new ContentReadbackHandler(env
, obj
);
122 return reinterpret_cast<intptr_t>(content_readback_handler
);
125 } // namespace content