Java Code Examples for android.view.SurfaceControl#screenshot()

The following examples show how to use android.view.SurfaceControl#screenshot() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UiAutomationConnection.java    From android_9.0.0_r45 with Apache License 2.0 8 votes vote down vote up
@Override
public Bitmap takeScreenshot(Rect crop, int rotation) {
    synchronized (mLock) {
        throwIfCalledByNotTrustedUidLocked();
        throwIfShutdownLocked();
        throwIfNotConnectedLocked();
    }
    final long identity = Binder.clearCallingIdentity();
    try {
        int width = crop.width();
        int height = crop.height();
        return SurfaceControl.screenshot(crop, width, height, rotation);
    } finally {
        Binder.restoreCallingIdentity(identity);
    }
}
 
Example 2
Source File: ColorFade.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private boolean captureScreenshotTextureAndSetViewport() {
    if (!attachEglContext()) {
        return false;
    }
    try {
        if (!mTexNamesGenerated) {
            GLES20.glGenTextures(1, mTexNames, 0);
            if (checkGlErrors("glGenTextures")) {
                return false;
            }
            mTexNamesGenerated = true;
        }

        final SurfaceTexture st = new SurfaceTexture(mTexNames[0]);
        final Surface s = new Surface(st);
        try {
            SurfaceControl.screenshot(SurfaceControl.getBuiltInDisplay(
                    SurfaceControl.BUILT_IN_DISPLAY_ID_MAIN), s);
            st.updateTexImage();
            st.getTransformMatrix(mTexMatrix);
        } finally {
            s.release();
            st.release();
        }

        // Set up texture coordinates for a quad.
        // We might need to change this if the texture ends up being
        // a different size from the display for some reason.
        mTexCoordBuffer.put(0, 0f); mTexCoordBuffer.put(1, 0f);
        mTexCoordBuffer.put(2, 0f); mTexCoordBuffer.put(3, 1f);
        mTexCoordBuffer.put(4, 1f); mTexCoordBuffer.put(5, 1f);
        mTexCoordBuffer.put(6, 1f); mTexCoordBuffer.put(7, 0f);

        // Set up our viewport.
        GLES20.glViewport(0, 0, mDisplayWidth, mDisplayHeight);
        ortho(0, mDisplayWidth, 0, mDisplayHeight, -1, 1);
    } finally {
        detachEglContext();
    }
    return true;
}
 
Example 3
Source File: DisplayContent.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/**
 * Takes a snapshot of the display.  In landscape mode this grabs the whole screen.
 * In portrait mode, it grabs the full screenshot.
 *
 * @param config of the output bitmap
 */
Bitmap screenshotDisplayLocked(Bitmap.Config config) {
    if (!mService.mPolicy.isScreenOn()) {
        if (DEBUG_SCREENSHOT) {
            Slog.i(TAG_WM, "Attempted to take screenshot while display was off.");
        }
        return null;
    }

    int dw = mDisplayInfo.logicalWidth;
    int dh = mDisplayInfo.logicalHeight;

    if (dw <= 0 || dh <= 0) {
        return null;
    }

    final Rect frame = new Rect(0, 0, dw, dh);

    // The screenshot API does not apply the current screen rotation.
    int rot = mDisplay.getRotation();

    if (rot == ROTATION_90 || rot == ROTATION_270) {
        rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
    }

    // SurfaceFlinger is not aware of orientation, so convert our logical
    // crop to SurfaceFlinger's portrait orientation.
    convertCropForSurfaceFlinger(frame, rot, dw, dh);

    final ScreenRotationAnimation screenRotationAnimation =
            mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
    final boolean inRotation = screenRotationAnimation != null &&
            screenRotationAnimation.isAnimating();
    if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM, "Taking screenshot while rotating");

    // TODO(b/68392460): We should screenshot Task controls directly
    // but it's difficult at the moment as the Task doesn't have the
    // correct size set.
    final Bitmap bitmap = SurfaceControl.screenshot(frame, dw, dh, 0, 1, inRotation, rot);
    if (bitmap == null) {
        Slog.w(TAG_WM, "Failed to take screenshot");
        return null;
    }

    // Create a copy of the screenshot that is immutable and backed in ashmem.
    // This greatly reduces the overhead of passing the bitmap between processes.
    final Bitmap ret = bitmap.createAshmemBitmap(config);
    bitmap.recycle();
    return ret;
}
 
Example 4
Source File: BitmapUtil.java    From JsDroidCmd with Mozilla Public License 2.0 4 votes vote down vote up
public static Bitmap takeScreenshot(int rotation, int screenWidth,
		int screenHeight) {
	Display display = DisplayManagerGlobal.getInstance().getRealDisplay(
			Display.DEFAULT_DISPLAY);
	Point displaySize = new Point();
	display.getRealSize(displaySize);
	final int displayWidth = screenWidth;
	final int displayHeight = screenHeight;
	final float screenshotWidth;
	final float screenshotHeight;
	switch (rotation) {
	case UiAutomation.ROTATION_FREEZE_0: {
		screenshotWidth = displayWidth;
		screenshotHeight = displayHeight;
	}
		break;
	case UiAutomation.ROTATION_FREEZE_90: {
		screenshotWidth = displayHeight;
		screenshotHeight = displayWidth;
	}
		break;
	case UiAutomation.ROTATION_FREEZE_180: {
		screenshotWidth = displayWidth;
		screenshotHeight = displayHeight;
	}
		break;
	case UiAutomation.ROTATION_FREEZE_270: {
		screenshotWidth = displayHeight;
		screenshotHeight = displayWidth;
	}
		break;
	default: {
		return null;
	}
	}

	Bitmap screenShot = null;
	try {
		screenShot = SurfaceControl.screenshot((int) screenshotWidth,
				(int) screenshotHeight);
		if (screenShot == null) {
			return null;
		}
	} catch (Exception re) {
		return null;
	}
	if (rotation != UiAutomation.ROTATION_FREEZE_0) {
		Bitmap unrotatedScreenShot = Bitmap.createBitmap(displayWidth,
				displayHeight, Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(unrotatedScreenShot);
		canvas.translate(unrotatedScreenShot.getWidth() / 2,
				unrotatedScreenShot.getHeight() / 2);
		canvas.rotate(getDegreesForRotation(rotation));
		canvas.translate(-screenshotWidth / 2, -screenshotHeight / 2);
		canvas.drawBitmap(screenShot, 0, 0, null);
		canvas.setBitmap(null);
		screenShot.recycle();
		screenShot = unrotatedScreenShot;
	}
	// Optimization
	screenShot.setHasAlpha(false);
	return screenShot;
}