OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/android/content_readback_handler.h" | 5 #include "content/browser/android/content_readback_handler.h" |
6 | 6 |
7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "cc/output/copy_output_request.h" |
| 10 #include "cc/output/copy_output_result.h" |
9 #include "content/browser/android/content_view_core_impl.h" | 11 #include "content/browser/android/content_view_core_impl.h" |
10 #include "jni/ContentReadbackHandler_jni.h" | 12 #include "jni/ContentReadbackHandler_jni.h" |
11 #include "third_party/skia/include/core/SkBitmap.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" |
12 #include "ui/gfx/android/java_bitmap.h" | 16 #include "ui/gfx/android/java_bitmap.h" |
13 #include "ui/gfx/rect.h" | 17 #include "ui/gfx/rect.h" |
14 | 18 |
15 namespace content { | 19 namespace content { |
16 | 20 |
| 21 namespace { |
| 22 |
| 23 typedef base::Callback<void(bool, const SkBitmap&)> ResultCallback; |
| 24 |
| 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()); |
| 30 return; |
| 31 } |
| 32 |
| 33 scoped_ptr<SkBitmap> bitmap = copy_output_result->TakeBitmap(); |
| 34 result_callback.Run(true, *bitmap.Pass()); |
| 35 } |
| 36 |
| 37 } // anonymous namespace |
| 38 |
17 // static | 39 // static |
18 bool ContentReadbackHandler::RegisterContentReadbackHandler(JNIEnv* env) { | 40 bool ContentReadbackHandler::RegisterContentReadbackHandler(JNIEnv* env) { |
19 return RegisterNativesImpl(env); | 41 return RegisterNativesImpl(env); |
20 } | 42 } |
21 | 43 |
22 ContentReadbackHandler::ContentReadbackHandler(JNIEnv* env, jobject obj) | 44 ContentReadbackHandler::ContentReadbackHandler(JNIEnv* env, jobject obj) |
23 : weak_factory_(this) { | 45 : weak_factory_(this) { |
24 java_obj_.Reset(env, obj); | 46 java_obj_.Reset(env, obj); |
25 } | 47 } |
26 | 48 |
27 ContentReadbackHandler::~ContentReadbackHandler() {} | |
28 | |
29 void ContentReadbackHandler::Destroy(JNIEnv* env, jobject obj) { | 49 void ContentReadbackHandler::Destroy(JNIEnv* env, jobject obj) { |
30 delete this; | 50 delete this; |
31 } | 51 } |
32 | 52 |
33 void ContentReadbackHandler::OnFinishContentReadback(int readback_id, | |
34 bool success, | |
35 const SkBitmap& bitmap) { | |
36 JNIEnv* env = base::android::AttachCurrentThread(); | |
37 ScopedJavaLocalRef<jobject> java_bitmap; | |
38 if (success) | |
39 java_bitmap = gfx::ConvertToJavaBitmap(&bitmap); | |
40 | |
41 Java_ContentReadbackHandler_notifyGetContentBitmapFinished( | |
42 env, java_obj_.obj(), readback_id, success, java_bitmap.obj()); | |
43 } | |
44 | |
45 void ContentReadbackHandler::GetContentBitmap(JNIEnv* env, | 53 void ContentReadbackHandler::GetContentBitmap(JNIEnv* env, |
46 jobject obj, | 54 jobject obj, |
47 jint readback_id, | 55 jint readback_id, |
48 jfloat scale, | 56 jfloat scale, |
49 jobject config, | 57 jobject config, |
50 jfloat x, | 58 jfloat x, |
51 jfloat y, | 59 jfloat y, |
52 jfloat width, | 60 jfloat width, |
53 jfloat height, | 61 jfloat height, |
54 jobject content_view_core) { | 62 jobject content_view_core) { |
55 ContentViewCore* view = | 63 ContentViewCore* view = |
56 ContentViewCore::GetNativeContentViewCore(env, content_view_core); | 64 ContentViewCore::GetNativeContentViewCore(env, content_view_core); |
57 DCHECK(view); | 65 DCHECK(view); |
58 | 66 |
59 base::Callback<void(bool, const SkBitmap&)> result_callback = | 67 ResultCallback result_callback = |
60 base::Bind(&ContentReadbackHandler::OnFinishContentReadback, | 68 base::Bind(&ContentReadbackHandler::OnFinishReadback, |
61 weak_factory_.GetWeakPtr(), | 69 weak_factory_.GetWeakPtr(), |
62 readback_id); | 70 readback_id); |
63 | 71 |
64 view->GetScaledContentBitmap( | 72 view->GetScaledContentBitmap( |
65 scale, config, gfx::Rect(x, y, width, height), result_callback); | 73 scale, config, gfx::Rect(x, y, width, height), result_callback); |
66 return; | 74 } |
| 75 |
| 76 void ContentReadbackHandler::GetCompositorBitmap(JNIEnv* env, |
| 77 jobject obj, |
| 78 jint readback_id, |
| 79 jlong native_window_android) { |
| 80 ui::WindowAndroid* window_android = |
| 81 reinterpret_cast<ui::WindowAndroid*>(native_window_android); |
| 82 DCHECK(window_android); |
| 83 |
| 84 ResultCallback result_callback = |
| 85 base::Bind(&ContentReadbackHandler::OnFinishReadback, |
| 86 weak_factory_.GetWeakPtr(), |
| 87 readback_id); |
| 88 |
| 89 base::Callback<void(scoped_ptr<cc::CopyOutputResult>)> copy_output_callback = |
| 90 base::Bind(&OnFinishCopyOutputRequest, |
| 91 result_callback); |
| 92 |
| 93 ui::WindowAndroidCompositor* compositor = window_android->GetCompositor(); |
| 94 |
| 95 if (!compositor) { |
| 96 copy_output_callback.Run(cc::CopyOutputResult::CreateEmptyResult()); |
| 97 return; |
| 98 } |
| 99 |
| 100 compositor->RequestCopyOfOutputOnRootLayer( |
| 101 cc::CopyOutputRequest::CreateBitmapRequest(copy_output_callback)); |
| 102 } |
| 103 |
| 104 ContentReadbackHandler::~ContentReadbackHandler() {} |
| 105 |
| 106 void ContentReadbackHandler::OnFinishReadback(int readback_id, |
| 107 bool success, |
| 108 const SkBitmap& bitmap) { |
| 109 JNIEnv* env = base::android::AttachCurrentThread(); |
| 110 ScopedJavaLocalRef<jobject> java_bitmap; |
| 111 if (success) |
| 112 java_bitmap = gfx::ConvertToJavaBitmap(&bitmap); |
| 113 |
| 114 Java_ContentReadbackHandler_notifyGetBitmapFinished( |
| 115 env, java_obj_.obj(), readback_id, success, java_bitmap.obj()); |
67 } | 116 } |
68 | 117 |
69 // static | 118 // static |
70 static jlong Init(JNIEnv* env, jobject obj) { | 119 static jlong Init(JNIEnv* env, jobject obj) { |
71 ContentReadbackHandler* content_readback_handler = | 120 ContentReadbackHandler* content_readback_handler = |
72 new ContentReadbackHandler(env, obj); | 121 new ContentReadbackHandler(env, obj); |
73 return reinterpret_cast<intptr_t>(content_readback_handler); | 122 return reinterpret_cast<intptr_t>(content_readback_handler); |
74 } | 123 } |
75 | 124 |
76 } // namespace content | 125 } // namespace content |
OLD | NEW |