1 /* 2 * Copyright (C) 2021 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 android.window; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.graphics.Matrix; 22 import android.graphics.Rect; 23 import android.os.Parcel; 24 import android.os.Parcelable; 25 import android.view.SurfaceControl; 26 27 import java.util.Arrays; 28 import java.util.Objects; 29 30 /** 31 * Represents a set of {@link android.view.SurfaceControl.Transaction} operations used to 32 * operate on the {@link android.view.SurfaceControl} for picture-in-picture. 33 * 34 * @hide 35 */ 36 public final class PictureInPictureSurfaceTransaction implements Parcelable { 37 38 public final float mPositionX; 39 public final float mPositionY; 40 41 public final float[] mFloat9; 42 43 // Though this can be determined by mFloat9, it's easier to set the value directly 44 public final float mRotation; 45 46 public final float mCornerRadius; 47 48 private final Rect mWindowCrop = new Rect(); 49 PictureInPictureSurfaceTransaction(Parcel in)50 public PictureInPictureSurfaceTransaction(Parcel in) { 51 mPositionX = in.readFloat(); 52 mPositionY = in.readFloat(); 53 mFloat9 = new float[9]; 54 in.readFloatArray(mFloat9); 55 mRotation = in.readFloat(); 56 mCornerRadius = in.readFloat(); 57 mWindowCrop.set(Objects.requireNonNull(in.readTypedObject(Rect.CREATOR))); 58 } 59 PictureInPictureSurfaceTransaction(float positionX, float positionY, float[] float9, float rotation, float cornerRadius, @Nullable Rect windowCrop)60 public PictureInPictureSurfaceTransaction(float positionX, float positionY, 61 float[] float9, float rotation, float cornerRadius, 62 @Nullable Rect windowCrop) { 63 mPositionX = positionX; 64 mPositionY = positionY; 65 mFloat9 = Arrays.copyOf(float9, 9); 66 mRotation = rotation; 67 mCornerRadius = cornerRadius; 68 if (windowCrop != null) { 69 mWindowCrop.set(windowCrop); 70 } 71 } 72 PictureInPictureSurfaceTransaction(PictureInPictureSurfaceTransaction other)73 public PictureInPictureSurfaceTransaction(PictureInPictureSurfaceTransaction other) { 74 this(other.mPositionX, other.mPositionY, 75 other.mFloat9, other.mRotation, other.mCornerRadius, other.mWindowCrop); 76 } 77 78 /** @return {@link Matrix} from {@link #mFloat9} */ getMatrix()79 public Matrix getMatrix() { 80 final Matrix matrix = new Matrix(); 81 matrix.setValues(mFloat9); 82 return matrix; 83 } 84 85 @Override equals(Object o)86 public boolean equals(Object o) { 87 if (this == o) return true; 88 if (!(o instanceof PictureInPictureSurfaceTransaction)) return false; 89 PictureInPictureSurfaceTransaction that = (PictureInPictureSurfaceTransaction) o; 90 return Objects.equals(mPositionX, that.mPositionX) 91 && Objects.equals(mPositionY, that.mPositionY) 92 && Arrays.equals(mFloat9, that.mFloat9) 93 && Objects.equals(mRotation, that.mRotation) 94 && Objects.equals(mCornerRadius, that.mCornerRadius) 95 && Objects.equals(mWindowCrop, that.mWindowCrop); 96 } 97 98 @Override hashCode()99 public int hashCode() { 100 return Objects.hash(mPositionX, mPositionY, Arrays.hashCode(mFloat9), 101 mRotation, mCornerRadius, mWindowCrop); 102 } 103 104 @Override describeContents()105 public int describeContents() { 106 return 0; 107 } 108 109 @Override writeToParcel(Parcel out, int flags)110 public void writeToParcel(Parcel out, int flags) { 111 out.writeFloat(mPositionX); 112 out.writeFloat(mPositionY); 113 out.writeFloatArray(mFloat9); 114 out.writeFloat(mRotation); 115 out.writeFloat(mCornerRadius); 116 out.writeTypedObject(mWindowCrop, 0 /* flags */); 117 } 118 119 @Override toString()120 public String toString() { 121 final Matrix matrix = getMatrix(); 122 return "PictureInPictureSurfaceTransaction(" 123 + " posX=" + mPositionX 124 + " posY=" + mPositionY 125 + " matrix=" + matrix.toShortString() 126 + " rotation=" + mRotation 127 + " cornerRadius=" + mCornerRadius 128 + " crop=" + mWindowCrop 129 + ")"; 130 } 131 132 /** Applies {@link PictureInPictureSurfaceTransaction} to a given leash. */ apply(@onNull PictureInPictureSurfaceTransaction surfaceTransaction, @NonNull SurfaceControl surfaceControl, @NonNull SurfaceControl.Transaction tx)133 public static void apply(@NonNull PictureInPictureSurfaceTransaction surfaceTransaction, 134 @NonNull SurfaceControl surfaceControl, 135 @NonNull SurfaceControl.Transaction tx) { 136 final Matrix matrix = surfaceTransaction.getMatrix(); 137 tx.setMatrix(surfaceControl, matrix, new float[9]) 138 .setPosition(surfaceControl, 139 surfaceTransaction.mPositionX, surfaceTransaction.mPositionY) 140 .setWindowCrop(surfaceControl, surfaceTransaction.mWindowCrop) 141 .setCornerRadius(surfaceControl, surfaceTransaction.mCornerRadius); 142 } 143 144 public static final @android.annotation.NonNull Creator<PictureInPictureSurfaceTransaction> 145 CREATOR = 146 new Creator<PictureInPictureSurfaceTransaction>() { 147 public PictureInPictureSurfaceTransaction createFromParcel(Parcel in) { 148 return new PictureInPictureSurfaceTransaction(in); 149 } 150 public PictureInPictureSurfaceTransaction[] newArray(int size) { 151 return new PictureInPictureSurfaceTransaction[size]; 152 } 153 }; 154 } 155