| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.wm.shell.pip; |
| 18 |
|
| 19 |
import android.content.Context; |
| 20 |
import android.graphics.Matrix; |
| 21 |
import android.graphics.Rect; |
| 22 |
import android.graphics.RectF; |
| 23 |
import android.view.Choreographer; |
| 24 |
import android.view.SurfaceControl; |
| 25 |
|
| 26 |
import com.android.wm.shell.R; |
| 27 |
import com.android.wm.shell.transition.Transitions; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public class PipSurfaceTransactionHelper { |
| 33 |
|
| 34 |
private final Matrix mTmpTransform = new Matrix(); |
| 35 |
private final float[] mTmpFloat9 = new float[9]; |
| 36 |
private final RectF mTmpSourceRectF = new RectF(); |
| 37 |
private final RectF mTmpDestinationRectF = new RectF(); |
| 38 |
private final Rect mTmpDestinationRect = new Rect(); |
| 39 |
|
| 40 |
private int mCornerRadius; |
| 41 |
private int mShadowRadius; |
| 42 |
|
| 43 |
public PipSurfaceTransactionHelper(Context context) { |
| 44 |
onDensityOrFontScaleChanged(context); |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public void onDensityOrFontScaleChanged(Context context) { |
| 53 |
mCornerRadius = context.getResources().getDimensionPixelSize(R.dimen.pip_corner_radius); |
| 54 |
mShadowRadius = context.getResources().getDimensionPixelSize(R.dimen.pip_shadow_radius); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public PipSurfaceTransactionHelper alpha(SurfaceControl.Transaction tx, SurfaceControl leash, |
| 62 |
float alpha) { |
| 63 |
tx.setAlpha(leash, alpha); |
| 64 |
return this; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public PipSurfaceTransactionHelper crop(SurfaceControl.Transaction tx, SurfaceControl leash, |
| 72 |
Rect destinationBounds) { |
| 73 |
tx.setWindowCrop(leash, destinationBounds.width(), destinationBounds.height()) |
| 74 |
.setPosition(leash, destinationBounds.left, destinationBounds.top); |
| 75 |
return this; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public PipSurfaceTransactionHelper scale(SurfaceControl.Transaction tx, SurfaceControl leash, |
| 83 |
Rect sourceBounds, Rect destinationBounds) { |
| 84 |
return scale(tx, leash, sourceBounds, destinationBounds, 0 ); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public PipSurfaceTransactionHelper scale(SurfaceControl.Transaction tx, SurfaceControl leash, |
| 92 |
Rect sourceBounds, Rect destinationBounds, float degrees) { |
| 93 |
mTmpSourceRectF.set(sourceBounds); |
| 94 |
|
| 95 |
|
| 96 |
mTmpSourceRectF.offsetTo(0, 0); |
| 97 |
mTmpDestinationRectF.set(destinationBounds); |
| 98 |
mTmpTransform.setRectToRect(mTmpSourceRectF, mTmpDestinationRectF, Matrix.ScaleToFit.FILL); |
| 99 |
mTmpTransform.postRotate(degrees, |
| 100 |
mTmpDestinationRectF.centerX(), mTmpDestinationRectF.centerY()); |
| 101 |
tx.setMatrix(leash, mTmpTransform, mTmpFloat9); |
| 102 |
return this; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public PipSurfaceTransactionHelper scaleAndCrop(SurfaceControl.Transaction tx, |
| 110 |
SurfaceControl leash, Rect sourceRectHint, |
| 111 |
Rect sourceBounds, Rect destinationBounds, Rect insets, |
| 112 |
boolean isInPipDirection, float fraction) { |
| 113 |
mTmpDestinationRect.set(sourceBounds); |
| 114 |
|
| 115 |
|
| 116 |
mTmpDestinationRect.offsetTo(0, 0); |
| 117 |
mTmpDestinationRect.inset(insets); |
| 118 |
|
| 119 |
|
| 120 |
final float scale; |
| 121 |
if (isInPipDirection |
| 122 |
&& sourceRectHint != null && sourceRectHint.width() < sourceBounds.width()) { |
| 123 |
|
| 124 |
final float endScale = sourceBounds.width() <= sourceBounds.height() |
| 125 |
? (float) destinationBounds.width() / sourceRectHint.width() |
| 126 |
: (float) destinationBounds.height() / sourceRectHint.height(); |
| 127 |
final float startScale = sourceBounds.width() <= sourceBounds.height() |
| 128 |
? (float) destinationBounds.width() / sourceBounds.width() |
| 129 |
: (float) destinationBounds.height() / sourceBounds.height(); |
| 130 |
scale = (1 - fraction) * startScale + fraction * endScale; |
| 131 |
} else { |
| 132 |
scale = Math.max((float) destinationBounds.width() / sourceBounds.width(), |
| 133 |
(float) destinationBounds.height() / sourceBounds.height()); |
| 134 |
} |
| 135 |
final float left = destinationBounds.left - insets.left * scale; |
| 136 |
final float top = destinationBounds.top - insets.top * scale; |
| 137 |
mTmpTransform.setScale(scale, scale); |
| 138 |
tx.setMatrix(leash, mTmpTransform, mTmpFloat9) |
| 139 |
.setCrop(leash, mTmpDestinationRect) |
| 140 |
.setPosition(leash, left, top); |
| 141 |
return this; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
public PipSurfaceTransactionHelper rotateAndScaleWithCrop(SurfaceControl.Transaction tx, |
| 150 |
SurfaceControl leash, Rect sourceBounds, Rect destinationBounds, Rect insets, |
| 151 |
float degrees, float positionX, float positionY, boolean isExpanding, |
| 152 |
boolean clockwise) { |
| 153 |
mTmpDestinationRect.set(sourceBounds); |
| 154 |
mTmpDestinationRect.inset(insets); |
| 155 |
final int srcW = mTmpDestinationRect.width(); |
| 156 |
final int srcH = mTmpDestinationRect.height(); |
| 157 |
final int destW = destinationBounds.width(); |
| 158 |
final int destH = destinationBounds.height(); |
| 159 |
|
| 160 |
|
| 161 |
final float scale = srcW <= srcH ? (float) destW / srcW : (float) destH / srcH; |
| 162 |
final Rect crop = mTmpDestinationRect; |
| 163 |
crop.set(0, 0, Transitions.SHELL_TRANSITIONS_ROTATION ? destH |
| 164 |
: destW, Transitions.SHELL_TRANSITIONS_ROTATION ? destW : destH); |
| 165 |
|
| 166 |
crop.scale(1 / scale); |
| 167 |
crop.offset(insets.left, insets.top); |
| 168 |
if (isExpanding) { |
| 169 |
|
| 170 |
positionX -= insets.left * scale; |
| 171 |
positionY -= insets.top * scale; |
| 172 |
} else { |
| 173 |
|
| 174 |
if (clockwise) { |
| 175 |
positionX -= insets.top * scale; |
| 176 |
positionY += insets.left * scale; |
| 177 |
} else { |
| 178 |
positionX += insets.top * scale; |
| 179 |
positionY -= insets.left * scale; |
| 180 |
} |
| 181 |
} |
| 182 |
mTmpTransform.setScale(scale, scale); |
| 183 |
mTmpTransform.postRotate(degrees); |
| 184 |
mTmpTransform.postTranslate(positionX, positionY); |
| 185 |
tx.setMatrix(leash, mTmpTransform, mTmpFloat9).setCrop(leash, crop); |
| 186 |
return this; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
public PipSurfaceTransactionHelper resetScale(SurfaceControl.Transaction tx, |
| 195 |
SurfaceControl leash, |
| 196 |
Rect destinationBounds) { |
| 197 |
tx.setMatrix(leash, Matrix.IDENTITY_MATRIX, mTmpFloat9) |
| 198 |
.setPosition(leash, destinationBounds.left, destinationBounds.top); |
| 199 |
return this; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
public PipSurfaceTransactionHelper round(SurfaceControl.Transaction tx, SurfaceControl leash, |
| 207 |
boolean applyCornerRadius) { |
| 208 |
tx.setCornerRadius(leash, applyCornerRadius ? mCornerRadius : 0); |
| 209 |
return this; |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
public PipSurfaceTransactionHelper round(SurfaceControl.Transaction tx, SurfaceControl leash, |
| 217 |
Rect fromBounds, Rect toBounds) { |
| 218 |
final float scale = (float) (Math.hypot(fromBounds.width(), fromBounds.height()) |
| 219 |
/ Math.hypot(toBounds.width(), toBounds.height())); |
| 220 |
tx.setCornerRadius(leash, mCornerRadius * scale); |
| 221 |
return this; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
public PipSurfaceTransactionHelper shadow(SurfaceControl.Transaction tx, SurfaceControl leash, |
| 229 |
boolean applyShadowRadius) { |
| 230 |
tx.setShadowRadius(leash, applyShadowRadius ? mShadowRadius : 0); |
| 231 |
return this; |
| 232 |
} |
| 233 |
|
| 234 |
public interface SurfaceControlTransactionFactory { |
| 235 |
SurfaceControl.Transaction getTransaction(); |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
public static class VsyncSurfaceControlTransactionFactory |
| 243 |
implements SurfaceControlTransactionFactory { |
| 244 |
@Override |
| 245 |
public SurfaceControl.Transaction getTransaction() { |
| 246 |
final SurfaceControl.Transaction tx = new SurfaceControl.Transaction(); |
| 247 |
tx.setFrameTimelineVsync(Choreographer.getInstance().getVsyncId()); |
| 248 |
return tx; |
| 249 |
} |
| 250 |
} |
| 251 |
} |