CaptureLayer的另外一个调用例子TaskSnapshot

在前一篇讨论中,我们查找了系统中调用captureLayers的地方
1323 public static GraphicBuffer captureLayers(IBinder layerHandleToken, Rect sourceCrop,

找到了2个调用的地方
/frameworks/base/services/core/java/com/android/server/wm/
H A D TaskSnapshotController.java 268 final GraphicBuffer buffer = SurfaceControl.captureLayers(
H A D WallpaperController.java 719 GraphicBuffer wallpaperBuffer = SurfaceControl.captureLayers(

上一篇里查看了WallpaperController.java的例子,这里我们再看看TaskSnapshotController里的例子

private TaskSnapshot snapshotTask(Task task) {
223 final AppWindowToken top = task.getTopChild();
224 if (top == null) {
225 return null;
226 }
227 final WindowState mainWindow = top.findMainWindow();
228 if (mainWindow == null) {
229 return null;
230 }
231 if (!mService.mPolicy.isScreenOn()) {
232 if (DEBUG_SCREENSHOT) {
233 Slog.i(TAG_WM, “Attempted to take screenshot while display was off.”);
234 }
235 return null;
236 }
237 if (task.getSurfaceControl() == null) {
238 return null;
239 }
240
241 if (top.hasCommittedReparentToAnimationLeash()) {
242 if (DEBUG_SCREENSHOT) {
243 Slog.w(TAG_WM, "Failed to take screenshot. App is animating " + top);
244 }
245 return null;
246 }
247
248 final boolean hasVisibleChild = top.forAllWindows(
249 // Ensure at least one window for the top app is visible before attempting to take
250 // a screenshot. Visible here means that the WSA surface is shown and has an alpha
251 // greater than 0.
252 ws -> (ws.mAppToken == null || ws.mAppToken.isSurfaceShowing())
253 && ws.mWinAnimator != null && ws.mWinAnimator.getShown()
254 && ws.mWinAnimator.mLastAlpha > 0f, true);
255
256 if (!hasVisibleChild) {
257 if (DEBUG_SCREENSHOT) {
258 Slog.w(TAG_WM, "Failed to take screenshot. No visible windows for " + task);
259 }
260 return null;
261 }
262
263 final boolean isLowRamDevice = ActivityManager.isLowRamDeviceStatic();
264 final float scaleFraction = isLowRamDevice ? REDUCED_SCALE : 1f;
265 task.getBounds(mTmpRect);
266 mTmpRect.offsetTo(0, 0);
267
268 final GraphicBuffer buffer = SurfaceControl.captureLayers(
269 task.getSurfaceControl().getHandle(), mTmpRect, scaleFraction);
270 final boolean isWindowTranslucent = mainWindow.getAttrs().format != PixelFormat.OPAQUE;
271 if (buffer == null || buffer.getWidth() <= 1 || buffer.getHeight() <= 1) {
272 if (DEBUG_SCREENSHOT) {
273 Slog.w(TAG_WM, "Failed to take screenshot for " + task);
274 }
275 return null;
276 }
277 return new TaskSnapshot(buffer, top.getConfiguration().orientation,
278 getInsets(mainWindow), isLowRamDevice /* reduced /, scaleFraction / scale /,
279 true /
isRealSnapshot */, task.getWindowingMode(), getSystemUiVisibility(task),
280 !top.fillsParent() || isWindowTranslucent);
281 }

TaskSnapshotController中也通过获取layer的方式获取了graphicBuffer,这样按recent按键的时候就可以看到每个应用的快照截图,
并且保存了图像信息到手机data/system_ce/0/snapshots中,这样,手机重启后,点击recent任务按键,还可以看到关机前的任务快照截图
我们可以编辑data/system_ce/0/snapshots里面的图片,涂鸦一下,再重启手机,会发现截图变化了。

快照可以设置安全级别来进行不同的显示处理。

iPhone 中是采用了模糊处理,如下图
在這裡插入圖片描述

参考资料

《ActivityManagerService解读之Activity启动闲聊–以TaskSnapshot为点切入AndroidView绘制原理分析》
https://blog.csdn.net/abm1993/article/details/88818361?utm_source=app&app_version=4.5.2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值