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/android/window_android.h"
15 #include "ui/android/window_android_compositor.h"
16 #include "ui/gfx/android/java_bitmap.h"
17 #include "ui/gfx/geometry/rect.h"
23 void OnFinishCopyOutputRequest(
24 const ReadbackRequestCallback
& result_callback
,
25 scoped_ptr
<cc::CopyOutputResult
> copy_output_result
) {
26 if (!copy_output_result
->HasBitmap()) {
27 result_callback
.Run(SkBitmap(), READBACK_FAILED
);
31 scoped_ptr
<SkBitmap
> bitmap
= copy_output_result
->TakeBitmap();
32 result_callback
.Run(*bitmap
, READBACK_SUCCESS
);
35 } // anonymous namespace
38 bool ContentReadbackHandler::RegisterContentReadbackHandler(JNIEnv
* env
) {
39 return RegisterNativesImpl(env
);
42 ContentReadbackHandler::ContentReadbackHandler(JNIEnv
* env
, jobject obj
)
43 : weak_factory_(this) {
44 java_obj_
.Reset(env
, obj
);
47 void ContentReadbackHandler::Destroy(JNIEnv
* env
, jobject obj
) {
51 void ContentReadbackHandler::GetContentBitmap(JNIEnv
* env
,
60 jobject content_view_core
) {
61 ContentViewCore
* view
=
62 ContentViewCore::GetNativeContentViewCore(env
, content_view_core
);
65 ReadbackRequestCallback result_callback
=
66 base::Bind(&ContentReadbackHandler::OnFinishReadback
,
67 weak_factory_
.GetWeakPtr(),
70 SkColorType sk_color_type
= gfx::ConvertToSkiaColorType(color_type
);
71 view
->GetScaledContentBitmap(
72 scale
, sk_color_type
, gfx::Rect(x
, y
, width
, height
), result_callback
);
75 void ContentReadbackHandler::GetCompositorBitmap(JNIEnv
* env
,
78 jlong native_window_android
) {
79 ui::WindowAndroid
* window_android
=
80 reinterpret_cast<ui::WindowAndroid
*>(native_window_android
);
81 DCHECK(window_android
);
83 ReadbackRequestCallback result_callback
=
84 base::Bind(&ContentReadbackHandler::OnFinishReadback
,
85 weak_factory_
.GetWeakPtr(),
88 base::Callback
<void(scoped_ptr
<cc::CopyOutputResult
>)> copy_output_callback
=
89 base::Bind(&OnFinishCopyOutputRequest
,
92 ui::WindowAndroidCompositor
* compositor
= window_android
->GetCompositor();
95 copy_output_callback
.Run(cc::CopyOutputResult::CreateEmptyResult());
99 compositor
->RequestCopyOfOutputOnRootLayer(
100 cc::CopyOutputRequest::CreateBitmapRequest(copy_output_callback
));
103 ContentReadbackHandler::~ContentReadbackHandler() {}
105 void ContentReadbackHandler::OnFinishReadback(int readback_id
,
106 const SkBitmap
& bitmap
,
107 ReadbackResponse response
) {
108 JNIEnv
* env
= base::android::AttachCurrentThread();
109 ScopedJavaLocalRef
<jobject
> java_bitmap
;
110 if (response
== READBACK_SUCCESS
)
111 java_bitmap
= gfx::ConvertToJavaBitmap(&bitmap
);
113 Java_ContentReadbackHandler_notifyGetBitmapFinished(
114 env
, java_obj_
.obj(), readback_id
, java_bitmap
.obj());
118 static jlong
Init(JNIEnv
* env
, jobject obj
) {
119 ContentReadbackHandler
* content_readback_handler
=
120 new ContentReadbackHandler(env
, obj
);
121 return reinterpret_cast<intptr_t>(content_readback_handler
);
124 } // namespace content