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