Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(82)

Side by Side Diff: content/browser/android/content_readback_handler.cc

Issue 593503003: Support error handling for Surface readbacks (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Formatted code and fixed build issue in test. Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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" 9 #include "cc/output/copy_output_request.h"
10 #include "cc/output/copy_output_result.h" 10 #include "cc/output/copy_output_result.h"
11 #include "content/browser/android/content_view_core_impl.h" 11 #include "content/browser/android/content_view_core_impl.h"
12 #include "jni/ContentReadbackHandler_jni.h" 12 #include "jni/ContentReadbackHandler_jni.h"
13 #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" 14 #include "ui/base/android/window_android.h"
15 #include "ui/base/android/window_android_compositor.h" 15 #include "ui/base/android/window_android_compositor.h"
16 #include "ui/gfx/android/java_bitmap.h" 16 #include "ui/gfx/android/java_bitmap.h"
17 #include "ui/gfx/rect.h" 17 #include "ui/gfx/rect.h"
18 18
19 namespace content { 19 namespace content {
20 20
21 namespace { 21 namespace {
22 22
23 typedef base::Callback<void(bool, const SkBitmap&)> ResultCallback;
24
25 void OnFinishCopyOutputRequest( 23 void OnFinishCopyOutputRequest(
26 const ResultCallback& result_callback, 24 const ReadbackRequestCallback& result_callback,
27 scoped_ptr<cc::CopyOutputResult> copy_output_result) { 25 scoped_ptr<cc::CopyOutputResult> copy_output_result) {
28 if (!copy_output_result->HasBitmap()) { 26 if (!copy_output_result->HasBitmap()) {
29 result_callback.Run(false, SkBitmap()); 27 result_callback.Run(SkBitmap(), READBACK_FAILED);
30 return; 28 return;
31 } 29 }
32 30
33 scoped_ptr<SkBitmap> bitmap = copy_output_result->TakeBitmap(); 31 scoped_ptr<SkBitmap> bitmap = copy_output_result->TakeBitmap();
34 result_callback.Run(true, *bitmap); 32 result_callback.Run(*bitmap, READBACK_SUCCESS);
35 } 33 }
36 34
37 } // anonymous namespace 35 } // anonymous namespace
38 36
39 // static 37 // static
40 bool ContentReadbackHandler::RegisterContentReadbackHandler(JNIEnv* env) { 38 bool ContentReadbackHandler::RegisterContentReadbackHandler(JNIEnv* env) {
41 return RegisterNativesImpl(env); 39 return RegisterNativesImpl(env);
42 } 40 }
43 41
44 ContentReadbackHandler::ContentReadbackHandler(JNIEnv* env, jobject obj) 42 ContentReadbackHandler::ContentReadbackHandler(JNIEnv* env, jobject obj)
(...skipping 12 matching lines...) Expand all
57 jobject color_type, 55 jobject color_type,
58 jfloat x, 56 jfloat x,
59 jfloat y, 57 jfloat y,
60 jfloat width, 58 jfloat width,
61 jfloat height, 59 jfloat height,
62 jobject content_view_core) { 60 jobject content_view_core) {
63 ContentViewCore* view = 61 ContentViewCore* view =
64 ContentViewCore::GetNativeContentViewCore(env, content_view_core); 62 ContentViewCore::GetNativeContentViewCore(env, content_view_core);
65 DCHECK(view); 63 DCHECK(view);
66 64
67 ResultCallback result_callback = 65 ReadbackRequestCallback result_callback =
68 base::Bind(&ContentReadbackHandler::OnFinishReadback, 66 base::Bind(&ContentReadbackHandler::OnFinishReadback,
69 weak_factory_.GetWeakPtr(), 67 weak_factory_.GetWeakPtr(),
70 readback_id); 68 readback_id);
71 69
72 SkColorType sk_color_type = gfx::ConvertToSkiaColorType(color_type); 70 SkColorType sk_color_type = gfx::ConvertToSkiaColorType(color_type);
73 view->GetScaledContentBitmap( 71 view->GetScaledContentBitmap(
74 scale, sk_color_type, gfx::Rect(x, y, width, height), result_callback); 72 scale, sk_color_type, gfx::Rect(x, y, width, height), result_callback);
75 } 73 }
76 74
77 void ContentReadbackHandler::GetCompositorBitmap(JNIEnv* env, 75 void ContentReadbackHandler::GetCompositorBitmap(JNIEnv* env,
78 jobject obj, 76 jobject obj,
79 jint readback_id, 77 jint readback_id,
80 jlong native_window_android) { 78 jlong native_window_android) {
81 ui::WindowAndroid* window_android = 79 ui::WindowAndroid* window_android =
82 reinterpret_cast<ui::WindowAndroid*>(native_window_android); 80 reinterpret_cast<ui::WindowAndroid*>(native_window_android);
83 DCHECK(window_android); 81 DCHECK(window_android);
84 82
85 ResultCallback result_callback = 83 ReadbackRequestCallback result_callback =
86 base::Bind(&ContentReadbackHandler::OnFinishReadback, 84 base::Bind(&ContentReadbackHandler::OnFinishReadback,
87 weak_factory_.GetWeakPtr(), 85 weak_factory_.GetWeakPtr(),
88 readback_id); 86 readback_id);
89 87
90 base::Callback<void(scoped_ptr<cc::CopyOutputResult>)> copy_output_callback = 88 base::Callback<void(scoped_ptr<cc::CopyOutputResult>)> copy_output_callback =
91 base::Bind(&OnFinishCopyOutputRequest, 89 base::Bind(&OnFinishCopyOutputRequest,
92 result_callback); 90 result_callback);
93 91
94 ui::WindowAndroidCompositor* compositor = window_android->GetCompositor(); 92 ui::WindowAndroidCompositor* compositor = window_android->GetCompositor();
95 93
96 if (!compositor) { 94 if (!compositor) {
97 copy_output_callback.Run(cc::CopyOutputResult::CreateEmptyResult()); 95 copy_output_callback.Run(cc::CopyOutputResult::CreateEmptyResult());
98 return; 96 return;
99 } 97 }
100 98
101 compositor->RequestCopyOfOutputOnRootLayer( 99 compositor->RequestCopyOfOutputOnRootLayer(
102 cc::CopyOutputRequest::CreateBitmapRequest(copy_output_callback)); 100 cc::CopyOutputRequest::CreateBitmapRequest(copy_output_callback));
103 } 101 }
104 102
105 ContentReadbackHandler::~ContentReadbackHandler() {} 103 ContentReadbackHandler::~ContentReadbackHandler() {}
106 104
107 void ContentReadbackHandler::OnFinishReadback(int readback_id, 105 void ContentReadbackHandler::OnFinishReadback(int readback_id,
108 bool success, 106 const SkBitmap& bitmap,
109 const SkBitmap& bitmap) { 107 ReadbackResponse response) {
110 JNIEnv* env = base::android::AttachCurrentThread(); 108 JNIEnv* env = base::android::AttachCurrentThread();
111 ScopedJavaLocalRef<jobject> java_bitmap; 109 ScopedJavaLocalRef<jobject> java_bitmap;
112 if (success) 110 if (response == READBACK_SUCCESS)
113 java_bitmap = gfx::ConvertToJavaBitmap(&bitmap); 111 java_bitmap = gfx::ConvertToJavaBitmap(&bitmap);
114 112
115 Java_ContentReadbackHandler_notifyGetBitmapFinished( 113 Java_ContentReadbackHandler_notifyGetBitmapFinished(
116 env, java_obj_.obj(), readback_id, java_bitmap.obj()); 114 env, java_obj_.obj(), readback_id, java_bitmap.obj());
117 } 115 }
118 116
119 // static 117 // static
120 static jlong Init(JNIEnv* env, jobject obj) { 118 static jlong Init(JNIEnv* env, jobject obj) {
121 ContentReadbackHandler* content_readback_handler = 119 ContentReadbackHandler* content_readback_handler =
122 new ContentReadbackHandler(env, obj); 120 new ContentReadbackHandler(env, obj);
123 return reinterpret_cast<intptr_t>(content_readback_handler); 121 return reinterpret_cast<intptr_t>(content_readback_handler);
124 } 122 }
125 123
126 } // namespace content 124 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698