| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.wm.shell.common; |
| 18 |
|
| 19 |
import android.graphics.PixelFormat; |
| 20 |
import android.graphics.Rect; |
| 21 |
import android.view.SurfaceControl; |
| 22 |
|
| 23 |
import java.util.function.Consumer; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public class ScreenshotUtils { |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public static void captureLayer(SurfaceControl sc, Rect crop, |
| 38 |
Consumer<SurfaceControl.ScreenshotHardwareBuffer> consumer) { |
| 39 |
consumer.accept(SurfaceControl.captureLayers( |
| 40 |
new SurfaceControl.LayerCaptureArgs.Builder(sc) |
| 41 |
.setSourceCrop(crop) |
| 42 |
.setCaptureSecureLayers(true) |
| 43 |
.setAllowProtected(true) |
| 44 |
.build())); |
| 45 |
} |
| 46 |
|
| 47 |
private static class BufferConsumer implements |
| 48 |
Consumer<SurfaceControl.ScreenshotHardwareBuffer> { |
| 49 |
SurfaceControl mScreenshot = null; |
| 50 |
SurfaceControl.Transaction mTransaction; |
| 51 |
SurfaceControl mSurfaceControl; |
| 52 |
SurfaceControl mParentSurfaceControl; |
| 53 |
int mLayer; |
| 54 |
|
| 55 |
BufferConsumer(SurfaceControl.Transaction t, SurfaceControl sc, SurfaceControl parentSc, |
| 56 |
int layer) { |
| 57 |
mTransaction = t; |
| 58 |
mSurfaceControl = sc; |
| 59 |
mParentSurfaceControl = parentSc; |
| 60 |
mLayer = layer; |
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
public void accept(SurfaceControl.ScreenshotHardwareBuffer buffer) { |
| 65 |
if (buffer == null || buffer.getHardwareBuffer() == null) { |
| 66 |
return; |
| 67 |
} |
| 68 |
mScreenshot = new SurfaceControl.Builder() |
| 69 |
.setName("ScreenshotUtils screenshot") |
| 70 |
.setFormat(PixelFormat.TRANSLUCENT) |
| 71 |
.setSecure(buffer.containsSecureLayers()) |
| 72 |
.setCallsite("ScreenshotUtils.takeScreenshot") |
| 73 |
.setBLASTLayer() |
| 74 |
.build(); |
| 75 |
|
| 76 |
mTransaction.setBuffer(mScreenshot, buffer.getHardwareBuffer()); |
| 77 |
mTransaction.setColorSpace(mScreenshot, buffer.getColorSpace()); |
| 78 |
mTransaction.reparent(mScreenshot, mParentSurfaceControl); |
| 79 |
mTransaction.setLayer(mScreenshot, mLayer); |
| 80 |
mTransaction.show(mScreenshot); |
| 81 |
mTransaction.apply(); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public static SurfaceControl takeScreenshot(SurfaceControl.Transaction t, SurfaceControl sc, |
| 96 |
Rect crop, int layer) { |
| 97 |
return takeScreenshot(t, sc, sc , crop, layer); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public static SurfaceControl takeScreenshot(SurfaceControl.Transaction t, SurfaceControl sc, |
| 112 |
SurfaceControl parentSc, Rect crop, int layer) { |
| 113 |
BufferConsumer consumer = new BufferConsumer(t, sc, parentSc, layer); |
| 114 |
captureLayer(sc, crop, consumer); |
| 115 |
return consumer.mScreenshot; |
| 116 |
} |
| 117 |
} |