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 |
17 // static | 21 // static |
18 bool ContentReadbackHandler::RegisterContentReadbackHandler(JNIEnv* env) { | 22 bool ContentReadbackHandler::RegisterContentReadbackHandler(JNIEnv* env) { |
19 return RegisterNativesImpl(env); | 23 return RegisterNativesImpl(env); |
20 } | 24 } |
21 | 25 |
22 ContentReadbackHandler::ContentReadbackHandler(JNIEnv* env, jobject obj) | 26 ContentReadbackHandler::ContentReadbackHandler(JNIEnv* env, jobject obj) |
23 : weak_factory_(this) { | 27 : weak_factory_(this) { |
24 java_obj_.Reset(env, obj); | 28 java_obj_.Reset(env, obj); |
25 } | 29 } |
26 | 30 |
27 ContentReadbackHandler::~ContentReadbackHandler() {} | 31 ContentReadbackHandler::~ContentReadbackHandler() {} |
28 | 32 |
29 void ContentReadbackHandler::Destroy(JNIEnv* env, jobject obj) { | 33 void ContentReadbackHandler::Destroy(JNIEnv* env, jobject obj) { |
30 delete this; | 34 delete this; |
31 } | 35 } |
32 | 36 |
33 void ContentReadbackHandler::OnFinishContentReadback(int readback_id, | 37 void ContentReadbackHandler::OnFinishReadback(int readback_id, |
Ted C
2014/05/21 17:56:29
Just noticed, but the function ordering should mat
powei
2014/05/23 01:15:09
Done.
| |
34 bool success, | 38 bool success, |
35 const SkBitmap& bitmap) { | 39 const SkBitmap& bitmap) { |
36 JNIEnv* env = base::android::AttachCurrentThread(); | 40 JNIEnv* env = base::android::AttachCurrentThread(); |
37 ScopedJavaLocalRef<jobject> java_bitmap; | 41 ScopedJavaLocalRef<jobject> java_bitmap; |
38 if (success) | 42 if (success) |
39 java_bitmap = gfx::ConvertToJavaBitmap(&bitmap); | 43 java_bitmap = gfx::ConvertToJavaBitmap(&bitmap); |
40 | 44 |
41 Java_ContentReadbackHandler_notifyGetContentBitmapFinished( | 45 Java_ContentReadbackHandler_notifyGetBitmapFinished( |
42 env, java_obj_.obj(), readback_id, success, java_bitmap.obj()); | 46 env, java_obj_.obj(), readback_id, success, java_bitmap.obj()); |
43 } | 47 } |
44 | 48 |
45 void ContentReadbackHandler::GetContentBitmap(JNIEnv* env, | 49 void ContentReadbackHandler::GetContentBitmap(JNIEnv* env, |
46 jobject obj, | 50 jobject obj, |
47 jint readback_id, | 51 jint readback_id, |
48 jfloat scale, | 52 jfloat scale, |
49 jobject config, | 53 jobject config, |
50 jfloat x, | 54 jfloat x, |
51 jfloat y, | 55 jfloat y, |
52 jfloat width, | 56 jfloat width, |
53 jfloat height, | 57 jfloat height, |
54 jobject content_view_core) { | 58 jobject content_view_core) { |
55 ContentViewCore* view = | 59 ContentViewCore* view = |
56 ContentViewCore::GetNativeContentViewCore(env, content_view_core); | 60 ContentViewCore::GetNativeContentViewCore(env, content_view_core); |
57 DCHECK(view); | 61 DCHECK(view); |
58 | 62 |
59 base::Callback<void(bool, const SkBitmap&)> result_callback = | 63 ResultCallback result_callback = |
60 base::Bind(&ContentReadbackHandler::OnFinishContentReadback, | 64 base::Bind(&ContentReadbackHandler::OnFinishReadback, |
61 weak_factory_.GetWeakPtr(), | 65 weak_factory_.GetWeakPtr(), |
62 readback_id); | 66 readback_id); |
63 | 67 |
64 view->GetScaledContentBitmap( | 68 view->GetScaledContentBitmap( |
65 scale, config, gfx::Rect(x, y, width, height), result_callback); | 69 scale, config, gfx::Rect(x, y, width, height), result_callback); |
66 return; | 70 } |
71 | |
72 void ContentReadbackHandler::OnFinishCopyOutputRequest( | |
73 const ResultCallback& result_callback, | |
74 scoped_ptr<cc::CopyOutputResult> copy_output_result) { | |
75 if (!copy_output_result->HasBitmap()) { | |
76 result_callback.Run(false, SkBitmap()); | |
77 return; | |
78 } | |
79 | |
80 scoped_ptr<SkBitmap> bitmap = copy_output_result->TakeBitmap(); | |
81 result_callback.Run(true, *bitmap.Pass()); | |
82 } | |
83 | |
84 void ContentReadbackHandler::GetCompositorBitmap(JNIEnv* env, | |
85 jobject obj, | |
86 jint readback_id, | |
87 jlong native_window_android) { | |
88 ui::WindowAndroid* window_android = | |
89 reinterpret_cast<ui::WindowAndroid*>(native_window_android); | |
90 DCHECK(window_android); | |
91 | |
92 ResultCallback result_callback = | |
93 base::Bind(&ContentReadbackHandler::OnFinishReadback, | |
94 weak_factory_.GetWeakPtr(), | |
95 readback_id); | |
96 | |
97 base::Callback<void(scoped_ptr<cc::CopyOutputResult>)> copy_output_callback = | |
98 base::Bind(&ContentReadbackHandler::OnFinishCopyOutputRequest, | |
99 result_callback); | |
100 | |
101 ui::WindowAndroidCompositor* compositor = window_android->GetCompositor(); | |
102 | |
103 if (!compositor) { | |
104 copy_output_callback.Run(cc::CopyOutputResult::CreateEmptyResult()); | |
105 return; | |
106 } | |
107 | |
108 compositor->RequestCopyOfOutputOnRootLayer( | |
109 cc::CopyOutputRequest::CreateBitmapRequest(copy_output_callback)); | |
67 } | 110 } |
68 | 111 |
69 // static | 112 // static |
70 static jlong Init(JNIEnv* env, jobject obj) { | 113 static jlong Init(JNIEnv* env, jobject obj) { |
71 ContentReadbackHandler* content_readback_handler = | 114 ContentReadbackHandler* content_readback_handler = |
72 new ContentReadbackHandler(env, obj); | 115 new ContentReadbackHandler(env, obj); |
73 return reinterpret_cast<intptr_t>(content_readback_handler); | 116 return reinterpret_cast<intptr_t>(content_readback_handler); |
74 } | 117 } |
75 | 118 |
76 } // namespace content | 119 } // namespace content |
OLD | NEW |