1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.systemui.screenshot 18 19 import android.app.IActivityTaskManager 20 import android.graphics.Bitmap 21 import android.graphics.Rect 22 import android.view.IWindowManager 23 import android.window.ScreenCapture 24 import android.window.ScreenCapture.CaptureArgs 25 import com.android.systemui.dagger.SysUISingleton 26 import com.android.systemui.dagger.qualifiers.Background 27 import javax.inject.Inject 28 import kotlinx.coroutines.CoroutineDispatcher 29 import kotlinx.coroutines.withContext 30 31 private const val TAG = "ImageCaptureImpl" 32 33 @SysUISingleton 34 open class ImageCaptureImpl @Inject constructor( 35 private val windowManager: IWindowManager, 36 private val atmService: IActivityTaskManager, 37 @Background private val bgContext: CoroutineDispatcher 38 ) : ImageCapture { 39 captureDisplaynull40 override fun captureDisplay(displayId: Int, crop: Rect?): Bitmap? { 41 val captureArgs = CaptureArgs.Builder() 42 .setSourceCrop(crop) 43 .build() 44 val syncScreenCapture = ScreenCapture.createSyncCaptureListener() 45 windowManager.captureDisplay(displayId, captureArgs, syncScreenCapture) 46 val buffer = syncScreenCapture.getBuffer() 47 return buffer?.asBitmap() 48 } 49 captureTasknull50 override suspend fun captureTask(taskId: Int): Bitmap? { 51 val snapshot = withContext(bgContext) { 52 atmService.takeTaskSnapshot(taskId, false /* updateCache */) 53 } ?: return null 54 return Bitmap.wrapHardwareBuffer(snapshot.hardwareBuffer, snapshot.colorSpace) 55 } 56 } 57