Add remoting and PPAPI tests to GN build
[chromium-blink-merge.git] / content / browser / android / content_readback_handler.cc
blob5964a78066b5261cdf2718fd7e7b8a5d956ef95b
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"
8 #include "base/bind.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"
19 namespace content {
21 namespace {
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);
28 return;
31 scoped_ptr<SkBitmap> bitmap = copy_output_result->TakeBitmap();
32 result_callback.Run(*bitmap, READBACK_SUCCESS);
35 } // anonymous namespace
37 // static
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) {
48 delete this;
51 void ContentReadbackHandler::GetContentBitmap(JNIEnv* env,
52 jobject obj,
53 jint readback_id,
54 jfloat scale,
55 jobject color_type,
56 jfloat x,
57 jfloat y,
58 jfloat width,
59 jfloat height,
60 jobject content_view_core) {
61 ContentViewCore* view =
62 ContentViewCore::GetNativeContentViewCore(env, content_view_core);
63 DCHECK(view);
65 ReadbackRequestCallback result_callback =
66 base::Bind(&ContentReadbackHandler::OnFinishReadback,
67 weak_factory_.GetWeakPtr(),
68 readback_id);
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,
76 jobject obj,
77 jint readback_id,
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(),
86 readback_id);
88 base::Callback<void(scoped_ptr<cc::CopyOutputResult>)> copy_output_callback =
89 base::Bind(&OnFinishCopyOutputRequest,
90 result_callback);
92 ui::WindowAndroidCompositor* compositor = window_android->GetCompositor();
94 if (!compositor) {
95 copy_output_callback.Run(cc::CopyOutputResult::CreateEmptyResult());
96 return;
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());
117 // static
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