Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a111187a authored by Dianne Hackborn's avatar Dianne Hackborn
Browse files

Change rotation freeze to all be implemented in window manager.

Lots of work for no visible change in behavior, but now we can
do some fancier stuff...

Also allow rotation in all 4 directions.

Change-Id: I7e5e9537c5e359f69b83c10f65cc1ce95f371461
parent 9fb2807e
Loading
Loading
Loading
Loading
+13 −0
Original line number Diff line number Diff line
@@ -373,6 +373,19 @@ public class Surface implements Parcelable {
        setOrientation(display, orientation, 0);
    }
    
    /**
     * Copy the current screen contents into a bitmap and return it.
     *
     * @param width The desired width of the returned bitmap; the raw
     * screen will be scaled down to this size.
     * @param height The desired height of the returned bitmap; the raw
     * screen will be scaled down to this size.
     * @return Returns a Bitmap containing the screen contents.
     *
     * @hide
     */
    public static native Bitmap screenshot(int width, int height);

    /**
     * set surface parameters.
     * needs to be inside open/closeTransaction block
+53 −9
Original line number Diff line number Diff line
@@ -19,7 +19,9 @@
#include <stdio.h>

#include "android_util_Binder.h"
#include "android/graphics/GraphicsJNI.h"

#include <binder/IMemory.h>
#include <surfaceflinger/SurfaceComposerClient.h>
#include <surfaceflinger/Surface.h>
#include <ui/Region.h>
@@ -91,15 +93,6 @@ struct no_t {
static no_t no;


static __attribute__((noinline))
void doThrow(JNIEnv* env, const char* exc, const char* msg = NULL)
{
    if (!env->ExceptionOccurred()) {
        jclass npeClazz = env->FindClass(exc);
        env->ThrowNew(npeClazz, msg);
    }
}

// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
@@ -444,6 +437,56 @@ static void Surface_unfreezeDisplay(
    }
}

class ScreenshotBitmap : public SkBitmap {
public:
    ScreenshotBitmap() {
    }

    status_t update(int width, int height) {
        status_t res = (width > 0 && height > 0)
                ? mScreenshot.update(width, height)
                : mScreenshot.update();
        if (res != NO_ERROR) {
            return res;
        }

        void const* base = mScreenshot.getPixels();
        uint32_t w = mScreenshot.getWidth();
        uint32_t h = mScreenshot.getHeight();
        uint32_t s = mScreenshot.getStride();
        uint32_t f = mScreenshot.getFormat();

        ssize_t bpr = s * android::bytesPerPixel(f);
        setConfig(convertPixelFormat(f), w, h, bpr);
        if (f == PIXEL_FORMAT_RGBX_8888) {
            setIsOpaque(true);
        }
        if (w > 0 && h > 0) {
            setPixels((void*)base);
        } else {
            // be safe with an empty bitmap.
            setPixels(NULL);
        }

        return NO_ERROR;
    }

private:
    ScreenshotClient mScreenshot;
};

static jobject Surface_screenshot(JNIEnv* env, jobject clazz, jint width, jint height)
{
    ScreenshotBitmap* bitmap = new ScreenshotBitmap();

    if (bitmap->update(width, height) != NO_ERROR) {
        delete bitmap;
        return 0;
    }

    return GraphicsJNI::createBitmap(env, bitmap, false, NULL);
}

static void Surface_setLayer(
        JNIEnv* env, jobject clazz, jint zorder)
{
@@ -669,6 +712,7 @@ static JNINativeMethod gSurfaceMethods[] = {
    {"setOrientation",      "(III)V", (void*)Surface_setOrientation },
    {"freezeDisplay",       "(I)V", (void*)Surface_freezeDisplay },
    {"unfreezeDisplay",     "(I)V", (void*)Surface_unfreezeDisplay },
    {"screenshot",          "(II)Landroid/graphics/Bitmap;", (void*)Surface_screenshot },
    {"setLayer",            "(I)V", (void*)Surface_setLayer },
    {"setPosition",         "(II)V",(void*)Surface_setPosition },
    {"setSize",             "(II)V",(void*)Surface_setSize },
+4 −0
Original line number Diff line number Diff line
@@ -195,6 +195,10 @@
    
    <!-- XXXXXX END OF RESOURCES USING WRONG NAMING CONVENTION -->

    <!-- If true, the screen can be rotated via the accelerometer in all 4
         rotations as the default behavior. -->
    <bool name="config_allowAllRotations">true</bool>

    <!-- The number of degrees to rotate the display when the keyboard is open. -->
    <integer name="config_lidOpenRotation">90</integer>

+4 −1
Original line number Diff line number Diff line
@@ -241,6 +241,7 @@ public class PhoneWindowManager implements WindowManagerPolicy {
    int mUserRotationMode = WindowManagerPolicy.USER_ROTATION_FREE;
    int mUserRotation = Surface.ROTATION_0;

    boolean mAllowAllRotations;
    boolean mCarDockEnablesAccelerometer;
    boolean mDeskDockEnablesAccelerometer;
    int mLidKeyboardAccessibility;
@@ -657,6 +658,8 @@ public class PhoneWindowManager implements WindowManagerPolicy {
                com.android.internal.R.integer.config_carDockRotation);
        mDeskDockRotation = readRotation(
                com.android.internal.R.integer.config_deskDockRotation);
        mAllowAllRotations = mContext.getResources().getBoolean(
                com.android.internal.R.bool.config_allowAllRotations);
        mCarDockEnablesAccelerometer = mContext.getResources().getBoolean(
                com.android.internal.R.bool.config_carDockEnablesAccelerometer);
        mDeskDockEnablesAccelerometer = mContext.getResources().getBoolean(
@@ -2344,7 +2347,7 @@ public class PhoneWindowManager implements WindowManagerPolicy {
                    return getCurrentPortraitRotation(lastRotation);
            }

            mOrientationListener.setAllow180Rotation(
            mOrientationListener.setAllow180Rotation(mAllowAllRotations ||
                    orientation == ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);

            // case for nosensor meaning ignore sensor and consider only lid
+152 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server;  // TODO: use com.android.server.wm, once things move there

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.util.Slog;
import android.view.Display;
import android.view.Surface;
import android.view.SurfaceSession;

class ScreenRotationAnimation {
    private static final String TAG = "ScreenRotationAnimation";

    Surface mSurface;
    int mWidth, mHeight;

    int mBaseRotation;
    int mCurRotation;
    int mDeltaRotation;

    final Matrix mMatrix = new Matrix();
    final float[] mTmpFloats = new float[9];

    public ScreenRotationAnimation(Display display, SurfaceSession session) {
        final DisplayMetrics dm = new DisplayMetrics();
        display.getMetrics(dm);

        Bitmap screenshot = Surface.screenshot(0, 0);

        if (screenshot != null) {
            // Screenshot does NOT include rotation!
            mBaseRotation = 0;
            mWidth = screenshot.getWidth();
            mHeight = screenshot.getHeight();
        } else {
            // Just in case.
            mBaseRotation = display.getRotation();
            mWidth = dm.widthPixels;
            mHeight = dm.heightPixels;
        }

        Surface.openTransaction();
        if (mSurface != null) {
            mSurface.destroy();
            mSurface = null;
        }
        try {
            mSurface = new Surface(session, 0, "FreezeSurface",
                    -1, mWidth, mHeight, PixelFormat.OPAQUE, 0);
        } catch (Surface.OutOfResourcesException e) {
            Slog.w(TAG, "Unable to allocate freeze surface", e);
        }
        mSurface.setLayer(WindowManagerService.TYPE_LAYER_MULTIPLIER * 200);
        setRotation(display.getRotation());

        Rect dirty = new Rect(0, 0, mWidth, mHeight);
        Canvas c = null;
        try {
            c = mSurface.lockCanvas(dirty);
        } catch (IllegalArgumentException e) {
            Slog.w(TAG, "Unable to lock surface", e);
            return;
        } catch (Surface.OutOfResourcesException e) {
            Slog.w(TAG, "Unable to lock surface", e);
            return;
        }
        if (c == null) {
            Slog.w(TAG, "Null surface");
            return;
        }

        if (screenshot != null) {
            c.drawBitmap(screenshot, 0, 0, new Paint(0));
        } else {
            c.drawColor(Color.GREEN);
        }

        mSurface.unlockCanvasAndPost(c);
        Surface.closeTransaction();

        screenshot.recycle();
    }

    // Must be called while in a transaction.
    public void setRotation(int rotation) {
        mCurRotation = rotation;
        int delta = mCurRotation - mBaseRotation;
        if (delta < 0) delta += 4;
        mDeltaRotation = delta;

        switch (delta) {
            case Surface.ROTATION_0:
                mMatrix.reset();
                break;
            case Surface.ROTATION_90:
                mMatrix.setRotate(90, 0, 0);
                mMatrix.postTranslate(0, mWidth);
                break;
            case Surface.ROTATION_180:
                mMatrix.setRotate(180, 0, 0);
                mMatrix.postTranslate(mWidth, mHeight);
                break;
            case Surface.ROTATION_270:
                mMatrix.setRotate(270, 0, 0);
                mMatrix.postTranslate(mHeight, 0);
                break;
        }

        mMatrix.getValues(mTmpFloats);
        mSurface.setPosition((int)mTmpFloats[Matrix.MTRANS_X],
                (int)mTmpFloats[Matrix.MTRANS_Y]);
        mSurface.setMatrix(
                mTmpFloats[Matrix.MSCALE_X], mTmpFloats[Matrix.MSKEW_X],
                mTmpFloats[Matrix.MSKEW_Y], mTmpFloats[Matrix.MSCALE_Y]);

        if (false) {
            float[] srcPnts = new float[] { 0, 0, mWidth, mHeight };
            float[] dstPnts = new float[8];
            mMatrix.mapPoints(dstPnts, srcPnts);
            Slog.i(TAG, "**** ROTATION: " + delta);
            Slog.i(TAG, "Original  : (" + srcPnts[0] + "," + srcPnts[1]
                    + ")-(" + srcPnts[2] + "," + srcPnts[3] + ")");
            Slog.i(TAG, "Transformed: (" + dstPnts[0] + "," + dstPnts[1]
                    + ")-(" + dstPnts[2] + "," + dstPnts[3] + ")");
        }
    }

    public void dismiss() {
        mSurface.destroy();
    }
}
Loading