|
11 | 11 | import android.util.Log; |
12 | 12 | import android.app.Activity; |
13 | 13 | import android.net.Uri; |
| 14 | +import android.content.ContextWrapper; |
14 | 15 | import android.view.View; |
15 | 16 | import android.view.ViewGroup; |
| 17 | +import android.view.Window; |
| 18 | +import android.view.PixelCopy; |
16 | 19 | import android.view.ViewParent; |
17 | 20 | import android.webkit.WebSettings; |
18 | 21 | import android.webkit.WebBackForwardList; |
19 | 22 | import android.webkit.WebHistoryItem; |
20 | 23 | import android.webkit.WebView; |
21 | 24 | import android.os.Handler; |
22 | 25 | import android.os.Looper; |
| 26 | +import android.graphics.Rect; |
23 | 27 |
|
24 | 28 | import java.io.ByteArrayOutputStream; |
25 | 29 | import java.util.ArrayList; |
@@ -422,35 +426,93 @@ public void captureSnapshotForFreeze(long requestId) { |
422 | 426 | return; |
423 | 427 | } |
424 | 428 | if (mWebView == null) { |
425 | | - nativeOnFreezeSnapshotReady(mNativePtr, requestId, 0, 0, null); |
| 429 | + reportFreezeSnapshotFailure(requestId); |
426 | 430 | return; |
427 | 431 | } |
| 432 | + final int w = mWebView.getWidth(); |
| 433 | + final int h = mWebView.getHeight(); |
| 434 | + if (w <= 0 || h <= 0) { |
| 435 | + reportFreezeSnapshotFailure(requestId); |
| 436 | + return; |
| 437 | + } |
| 438 | + |
428 | 439 | try { |
429 | | - int w = mWebView.getWidth(); |
430 | | - int h = mWebView.getHeight(); |
431 | | - if (w <= 0 || h <= 0) { |
432 | | - nativeOnFreezeSnapshotReady(mNativePtr, requestId, 0, 0, null); |
| 440 | + final Window window = resolveWindow(mWebView.getContext()); |
| 441 | + if (window == null) { |
| 442 | + captureFreezeSoftware(requestId, w, h); |
433 | 443 | return; |
434 | 444 | } |
435 | | - Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); |
436 | | - bitmap.eraseColor(android.graphics.Color.WHITE); |
437 | | - Canvas canvas = new Canvas(bitmap); |
438 | | - mWebView.draw(canvas); |
439 | | - ByteBuffer buffer = ByteBuffer.allocateDirect(w * h * 4); |
440 | | - buffer.order(ByteOrder.nativeOrder()); |
441 | | - bitmap.copyPixelsToBuffer(buffer); |
442 | | - byte[] pixels = new byte[w * h * 4]; |
443 | | - buffer.rewind(); |
444 | | - buffer.get(pixels); |
445 | | - bitmap.recycle(); |
446 | | - nativeOnFreezeSnapshotReady(mNativePtr, requestId, w, h, pixels); |
| 445 | + |
| 446 | + final Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); |
| 447 | + final int[] location = new int[2]; |
| 448 | + mWebView.getLocationInWindow(location); |
| 449 | + final Rect srcRect = new Rect(location[0], location[1], location[0] + w, location[1] + h); |
| 450 | + |
| 451 | + PixelCopy.request(window, srcRect, bitmap, result -> { |
| 452 | + if (result == PixelCopy.SUCCESS) { |
| 453 | + deliverFreezeSnapshot(requestId, bitmap, w, h); |
| 454 | + return; |
| 455 | + } |
| 456 | + bitmap.recycle(); |
| 457 | + Log.w(TAG, "PixelCopy freeze snapshot failed with code " + result |
| 458 | + + ", falling back to software draw"); |
| 459 | + captureFreezeSoftware(requestId, w, h); |
| 460 | + }, new Handler(Looper.getMainLooper())); |
447 | 461 | } catch (RuntimeException e) { |
448 | 462 | Log.e(TAG, "captureSnapshotForFreeze failed", e); |
449 | | - nativeOnFreezeSnapshotReady(mNativePtr, requestId, 0, 0, null); |
| 463 | + reportFreezeSnapshotFailure(requestId); |
450 | 464 | } |
451 | 465 | }); |
452 | 466 | } |
453 | 467 |
|
| 468 | + private void captureFreezeSoftware(long requestId, int w, int h) { |
| 469 | + try { |
| 470 | + Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); |
| 471 | + bitmap.eraseColor(android.graphics.Color.WHITE); |
| 472 | + mWebView.draw(new Canvas(bitmap)); |
| 473 | + deliverFreezeSnapshot(requestId, bitmap, w, h); |
| 474 | + } catch (RuntimeException e) { |
| 475 | + Log.e(TAG, "software freeze snapshot failed", e); |
| 476 | + reportFreezeSnapshotFailure(requestId); |
| 477 | + } |
| 478 | + } |
| 479 | + |
| 480 | + private void deliverFreezeSnapshot(long requestId, Bitmap bitmap, int w, int h) { |
| 481 | + final byte[] pixels; |
| 482 | + try { |
| 483 | + ByteBuffer buffer = ByteBuffer.allocate(w * h * 4); |
| 484 | + buffer.order(ByteOrder.nativeOrder()); |
| 485 | + bitmap.copyPixelsToBuffer(buffer); |
| 486 | + pixels = buffer.array(); |
| 487 | + } catch (RuntimeException e) { |
| 488 | + Log.e(TAG, "deliverFreezeSnapshot failed", e); |
| 489 | + reportFreezeSnapshotFailure(requestId); |
| 490 | + return; |
| 491 | + } finally { |
| 492 | + bitmap.recycle(); |
| 493 | + } |
| 494 | + withNativePtr(ptr -> nativeOnFreezeSnapshotReady(ptr, requestId, w, h, pixels)); |
| 495 | + } |
| 496 | + |
| 497 | + private void reportFreezeSnapshotFailure(long requestId) { |
| 498 | + withNativePtr(ptr -> nativeOnFreezeSnapshotReady(ptr, requestId, 0, 0, null)); |
| 499 | + } |
| 500 | + |
| 501 | + private Window resolveWindow(Context context) { |
| 502 | + Context current = context; |
| 503 | + while (current instanceof ContextWrapper) { |
| 504 | + if (current instanceof Activity) { |
| 505 | + return ((Activity) current).getWindow(); |
| 506 | + } |
| 507 | + Context base = ((ContextWrapper) current).getBaseContext(); |
| 508 | + if (base == current) { |
| 509 | + break; |
| 510 | + } |
| 511 | + current = base; |
| 512 | + } |
| 513 | + return null; |
| 514 | + } |
| 515 | + |
454 | 516 | public void setInteractionEnabled(boolean enabled) { |
455 | 517 | runOnMainThread(() -> { |
456 | 518 | if (mWebView == null) return; |
|
0 commit comments