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 package org.chromium.content.browser; | 5 package org.chromium.content.browser; |
6 | 6 |
7 import android.graphics.Bitmap; | 7 import android.graphics.Bitmap; |
8 import android.graphics.Rect; | 8 import android.graphics.Rect; |
9 import android.util.SparseArray; | 9 import android.util.SparseArray; |
10 | 10 |
11 import org.chromium.base.CalledByNative; | 11 import org.chromium.base.CalledByNative; |
12 import org.chromium.base.JNINamespace; | 12 import org.chromium.base.JNINamespace; |
13 import org.chromium.base.ThreadUtils; | 13 import org.chromium.base.ThreadUtils; |
| 14 import org.chromium.ui.base.WindowAndroid; |
14 | 15 |
15 /** | 16 /** |
16 * A class for reading back content. | 17 * A class for reading back content. |
17 */ | 18 */ |
18 @JNINamespace("content") | 19 @JNINamespace("content") |
19 public abstract class ContentReadbackHandler { | 20 public abstract class ContentReadbackHandler { |
20 /** | 21 /** |
21 * A callback interface for content readback into a bitmap. | 22 * A callback interface for content readback into a bitmap. |
22 */ | 23 */ |
23 public static interface GetContentBitmapCallback { | 24 public static interface GetBitmapCallback { |
24 /** | 25 /** |
25 * Called when the content readback finishes. | 26 * Called when the content readback finishes. |
26 * @param success Indicates whether the readback succeeded or not. | 27 * @param success Indicates whether the readback succeeded or not. |
27 * @param bitmap The {@link Bitmap} of the content. | 28 * @param bitmap The {@link Bitmap} of the content. |
28 */ | 29 */ |
29 public void onFinishGetContentBitmap(boolean success, Bitmap bitmap); | 30 public void onFinishGetBitmap(boolean success, Bitmap bitmap); |
30 } | 31 } |
31 | 32 |
32 private int mNextReadbackId = 1; | 33 private int mNextReadbackId = 1; |
33 private SparseArray<GetContentBitmapCallback> mGetContentBitmapRequests; | 34 private SparseArray<GetBitmapCallback> mGetBitmapRequests; |
34 | 35 |
35 private long mNativeContentReadbackHandler; | 36 private long mNativeContentReadbackHandler; |
36 | 37 |
37 /** | 38 /** |
38 * Creates a {@link ContentReadbackHandler}. | 39 * Creates a {@link ContentReadbackHandler}. |
39 */ | 40 */ |
40 public ContentReadbackHandler() { | 41 public ContentReadbackHandler() { |
41 mGetContentBitmapRequests = new SparseArray<GetContentBitmapCallback>(); | 42 mGetBitmapRequests = new SparseArray<GetBitmapCallback>(); |
42 } | 43 } |
43 | 44 |
44 /** | 45 /** |
45 * Initialize the native object. | 46 * Initialize the native object. |
46 */ | 47 */ |
47 public void initNativeContentReadbackHandler() { | 48 public void initNativeContentReadbackHandler() { |
48 mNativeContentReadbackHandler = nativeInit(); | 49 mNativeContentReadbackHandler = nativeInit(); |
49 } | 50 } |
50 | 51 |
51 /** | 52 /** |
52 * Should be called when the ContentReadackHandler is not needed anymore. | 53 * Should be called when the ContentReadackHandler is not needed anymore. |
53 */ | 54 */ |
54 public void destroy() { | 55 public void destroy() { |
55 if (mNativeContentReadbackHandler != 0) nativeDestroy(mNativeContentRead
backHandler); | 56 if (mNativeContentReadbackHandler != 0) nativeDestroy(mNativeContentRead
backHandler); |
56 mNativeContentReadbackHandler = 0; | 57 mNativeContentReadbackHandler = 0; |
57 } | 58 } |
58 | 59 |
59 | 60 |
60 @CalledByNative | 61 @CalledByNative |
61 private void notifyGetContentBitmapFinished(int readbackId, boolean success,
Bitmap bitmap) { | 62 private void notifyGetBitmapFinished(int readbackId, boolean success, Bitmap
bitmap) { |
62 GetContentBitmapCallback callback = mGetContentBitmapRequests.get(readba
ckId); | 63 GetBitmapCallback callback = mGetBitmapRequests.get(readbackId); |
63 if (callback != null) { | 64 if (callback != null) { |
64 mGetContentBitmapRequests.delete(readbackId); | 65 mGetBitmapRequests.delete(readbackId); |
65 callback.onFinishGetContentBitmap(success, bitmap); | 66 callback.onFinishGetBitmap(success, bitmap); |
66 } else { | 67 } else { |
67 // readback Id is unregistered. | 68 // readback Id is unregistered. |
68 assert false : "Readback finished for unregistered Id: " + readbackI
d; | 69 assert false : "Readback finished for unregistered Id: " + readbackI
d; |
69 } | 70 } |
70 } | 71 } |
71 | 72 |
72 /** | 73 /** |
73 * Asynchronously, generate and grab a bitmap representing what is currently
on the screen | 74 * Asynchronously, generate and grab a bitmap representing what is currently
on the screen |
74 * for {@code view}. | 75 * for {@code view}. |
75 * | 76 * |
76 * @param scale The scale that should be applied to the content. | 77 * @param scale The scale that should be applied to the content. |
77 * @param srcRect A subrect of the original content to capture. If this is
empty, it will grab | 78 * @param srcRect A subrect of the original content to capture. If this is
empty, it will grab |
78 * the whole surface. | 79 * the whole surface. |
79 * @param view The {@link ContentViewCore} to grab the bitmap from. | 80 * @param view The {@link ContentViewCore} to grab the bitmap from. |
80 * @param callback The callback to be executed after readback completes. | 81 * @param callback The callback to be executed after readback completes. |
81 */ | 82 */ |
82 public void getContentBitmapAsync(float scale, Rect srcRect, ContentViewCore
view, | 83 public void getContentBitmapAsync(float scale, Rect srcRect, ContentViewCore
view, |
83 GetContentBitmapCallback callback) { | 84 GetBitmapCallback callback) { |
84 if (!readyForReadback()) { | 85 if (!readyForReadback()) { |
85 callback.onFinishGetContentBitmap(false, null); | 86 callback.onFinishGetBitmap(false, null); |
86 return; | 87 return; |
87 } | 88 } |
88 ThreadUtils.assertOnUiThread(); | 89 ThreadUtils.assertOnUiThread(); |
89 | 90 |
90 int readbackId = mNextReadbackId++; | 91 int readbackId = mNextReadbackId++; |
91 mGetContentBitmapRequests.put(readbackId, callback); | 92 mGetBitmapRequests.put(readbackId, callback); |
92 nativeGetContentBitmap(mNativeContentReadbackHandler, readbackId, scale, | 93 nativeGetContentBitmap(mNativeContentReadbackHandler, readbackId, scale, |
93 Bitmap.Config.ARGB_8888, srcRect.top, srcRect.left, srcRect.widt
h(), | 94 Bitmap.Config.ARGB_8888, srcRect.top, srcRect.left, srcRect.widt
h(), |
94 srcRect.height(), view); | 95 srcRect.height(), view); |
95 } | 96 } |
96 | 97 |
97 /** | 98 /** |
| 99 * Asynchronously, grab a bitmap of the current browser compositor root laye
r. |
| 100 * |
| 101 * @param windowAndroid The window that hosts the compositor. |
| 102 * @param callback The callback to be executed after readback completes
. |
| 103 */ |
| 104 public void getCompositorBitmapAsync(WindowAndroid windowAndroid, GetBitmapC
allback callback) { |
| 105 if (!readyForReadback()) { |
| 106 callback.onFinishGetBitmap(false, null); |
| 107 return; |
| 108 } |
| 109 ThreadUtils.assertOnUiThread(); |
| 110 |
| 111 int readbackId = mNextReadbackId++; |
| 112 mGetBitmapRequests.put(readbackId, callback); |
| 113 nativeGetCompositorBitmap(mNativeContentReadbackHandler, readbackId, |
| 114 windowAndroid.getNativePointer()); |
| 115 } |
| 116 |
| 117 /** |
98 * Implemented by the owner of this class to signal whether readback is poss
ible or not. | 118 * Implemented by the owner of this class to signal whether readback is poss
ible or not. |
99 * @return Whether readback is possible or not. | 119 * @return Whether readback is possible or not. |
100 */ | 120 */ |
101 protected abstract boolean readyForReadback(); | 121 protected abstract boolean readyForReadback(); |
102 | 122 |
103 private native long nativeInit(); | 123 private native long nativeInit(); |
104 private native void nativeDestroy(long nativeContentReadbackHandler); | 124 private native void nativeDestroy(long nativeContentReadbackHandler); |
105 private native void nativeGetContentBitmap(long nativeContentReadbackHandler
, int readback_id, | 125 private native void nativeGetContentBitmap(long nativeContentReadbackHandler
, int readback_id, |
106 float scale, Bitmap.Config config, float x, float y, float width, fl
oat height, | 126 float scale, Bitmap.Config config, float x, float y, float width, fl
oat height, |
107 Object contentViewCore); | 127 Object contentViewCore); |
| 128 private native void nativeGetCompositorBitmap(long nativeContentReadbackHand
ler, |
| 129 int readback_id, long nativeWindowAndroid); |
108 } | 130 } |
OLD | NEW |