Dianne Hackborn | 7916ac6 | 2011-05-16 20:45:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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.server.wm; |
| 18 | |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 19 | import static android.graphics.PixelFormat.OPAQUE; |
| 20 | import static android.view.SurfaceControl.FX_SURFACE_DIM; |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 21 | import static com.android.server.wm.WindowManagerDebugConfig.SHOW_SURFACE_ALLOC; |
| 22 | import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS; |
| 23 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM; |
| 24 | |
Dianne Hackborn | 9fd7480 | 2012-03-01 19:26:31 -0800 | [diff] [blame] | 25 | import java.io.PrintWriter; |
| 26 | |
Dianne Hackborn | 7916ac6 | 2011-05-16 20:45:48 -0700 | [diff] [blame] | 27 | import android.graphics.Matrix; |
| 28 | import android.graphics.PixelFormat; |
| 29 | import android.graphics.Rect; |
| 30 | import android.util.Slog; |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 31 | import android.view.Surface.OutOfResourcesException; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 32 | import android.view.SurfaceControl; |
Dianne Hackborn | 7916ac6 | 2011-05-16 20:45:48 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Four black surfaces put together to make a black frame. |
| 36 | */ |
| 37 | public class BlackFrame { |
| 38 | class BlackSurface { |
| 39 | final int left; |
| 40 | final int top; |
Craig Mautner | 2fb98b1 | 2012-03-20 17:24:00 -0700 | [diff] [blame] | 41 | final int layer; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 42 | final SurfaceControl surface; |
Dianne Hackborn | 7916ac6 | 2011-05-16 20:45:48 -0700 | [diff] [blame] | 43 | |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame^] | 44 | BlackSurface(int layer, |
| 45 | int l, int t, int r, int b, DisplayContent dc) throws OutOfResourcesException { |
Dianne Hackborn | 7916ac6 | 2011-05-16 20:45:48 -0700 | [diff] [blame] | 46 | left = l; |
| 47 | top = t; |
Craig Mautner | 2fb98b1 | 2012-03-20 17:24:00 -0700 | [diff] [blame] | 48 | this.layer = layer; |
Dianne Hackborn | 91c9ac0 | 2011-07-21 21:52:09 -0700 | [diff] [blame] | 49 | int w = r-l; |
| 50 | int h = b-t; |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 51 | |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame^] | 52 | surface = dc.makeOverlay() |
Robert Carr | e625fcf | 2017-09-01 12:36:28 -0700 | [diff] [blame] | 53 | .setName("BlackSurface") |
| 54 | .setSize(w, h) |
| 55 | .setColorLayer(true) |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame^] | 56 | .setParent(null) // TODO: Work-around for b/69259549 |
Robert Carr | e625fcf | 2017-09-01 12:36:28 -0700 | [diff] [blame] | 57 | .build(); |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 58 | |
Craig Mautner | 924d9b7 | 2012-05-06 13:12:04 -0700 | [diff] [blame] | 59 | surface.setAlpha(1); |
| 60 | surface.setLayer(layer); |
| 61 | surface.show(); |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 62 | if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) Slog.i(TAG_WM, |
Dianne Hackborn | 5fd2169 | 2011-06-07 14:09:47 -0700 | [diff] [blame] | 63 | " BLACK " + surface + ": CREATE layer=" + layer); |
Dianne Hackborn | 7916ac6 | 2011-05-16 20:45:48 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Craig Mautner | 46ac6fa | 2013-08-01 10:06:34 -0700 | [diff] [blame] | 66 | void setAlpha(float alpha) { |
| 67 | surface.setAlpha(alpha); |
| 68 | } |
| 69 | |
Dianne Hackborn | 7916ac6 | 2011-05-16 20:45:48 -0700 | [diff] [blame] | 70 | void setMatrix(Matrix matrix) { |
| 71 | mTmpMatrix.setTranslate(left, top); |
| 72 | mTmpMatrix.postConcat(matrix); |
| 73 | mTmpMatrix.getValues(mTmpFloats); |
Dianne Hackborn | d040edb | 2011-08-31 12:47:58 -0700 | [diff] [blame] | 74 | surface.setPosition(mTmpFloats[Matrix.MTRANS_X], |
| 75 | mTmpFloats[Matrix.MTRANS_Y]); |
Dianne Hackborn | 7916ac6 | 2011-05-16 20:45:48 -0700 | [diff] [blame] | 76 | surface.setMatrix( |
| 77 | mTmpFloats[Matrix.MSCALE_X], mTmpFloats[Matrix.MSKEW_Y], |
| 78 | mTmpFloats[Matrix.MSKEW_X], mTmpFloats[Matrix.MSCALE_Y]); |
| 79 | if (false) { |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 80 | Slog.i(TAG_WM, "Black Surface @ (" + left + "," + top + "): (" |
Dianne Hackborn | 7916ac6 | 2011-05-16 20:45:48 -0700 | [diff] [blame] | 81 | + mTmpFloats[Matrix.MTRANS_X] + "," |
| 82 | + mTmpFloats[Matrix.MTRANS_Y] + ") matrix=[" |
| 83 | + mTmpFloats[Matrix.MSCALE_X] + "," |
| 84 | + mTmpFloats[Matrix.MSCALE_Y] + "][" |
| 85 | + mTmpFloats[Matrix.MSKEW_X] + "," |
| 86 | + mTmpFloats[Matrix.MSKEW_Y] + "]"); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | void clearMatrix() { |
| 91 | surface.setMatrix(1, 0, 0, 1); |
| 92 | } |
| 93 | } |
| 94 | |
Dianne Hackborn | 9fd7480 | 2012-03-01 19:26:31 -0800 | [diff] [blame] | 95 | final Rect mOuterRect; |
| 96 | final Rect mInnerRect; |
Dianne Hackborn | 7916ac6 | 2011-05-16 20:45:48 -0700 | [diff] [blame] | 97 | final Matrix mTmpMatrix = new Matrix(); |
| 98 | final float[] mTmpFloats = new float[9]; |
| 99 | final BlackSurface[] mBlackSurfaces = new BlackSurface[4]; |
| 100 | |
Craig Mautner | 46ac6fa | 2013-08-01 10:06:34 -0700 | [diff] [blame] | 101 | final boolean mForceDefaultOrientation; |
| 102 | |
Dianne Hackborn | 9fd7480 | 2012-03-01 19:26:31 -0800 | [diff] [blame] | 103 | public void printTo(String prefix, PrintWriter pw) { |
| 104 | pw.print(prefix); pw.print("Outer: "); mOuterRect.printShortString(pw); |
| 105 | pw.print(" / Inner: "); mInnerRect.printShortString(pw); |
| 106 | pw.println(); |
| 107 | for (int i=0; i<mBlackSurfaces.length; i++) { |
| 108 | BlackSurface bs = mBlackSurfaces[i]; |
| 109 | pw.print(prefix); pw.print("#"); pw.print(i); |
| 110 | pw.print(": "); pw.print(bs.surface); |
| 111 | pw.print(" left="); pw.print(bs.left); |
| 112 | pw.print(" top="); pw.println(bs.top); |
| 113 | } |
| 114 | } |
| 115 | |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame^] | 116 | public BlackFrame(Rect outer, Rect inner, int layer, DisplayContent dc, |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 117 | boolean forceDefaultOrientation) throws OutOfResourcesException { |
Dianne Hackborn | 7916ac6 | 2011-05-16 20:45:48 -0700 | [diff] [blame] | 118 | boolean success = false; |
| 119 | |
Craig Mautner | 46ac6fa | 2013-08-01 10:06:34 -0700 | [diff] [blame] | 120 | mForceDefaultOrientation = forceDefaultOrientation; |
| 121 | |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame^] | 122 | // TODO: Why do we use 4 surfaces instead of just one big one behind the screenshot? |
| 123 | // b/68253229 |
Dianne Hackborn | 9fd7480 | 2012-03-01 19:26:31 -0800 | [diff] [blame] | 124 | mOuterRect = new Rect(outer); |
| 125 | mInnerRect = new Rect(inner); |
Dianne Hackborn | 7916ac6 | 2011-05-16 20:45:48 -0700 | [diff] [blame] | 126 | try { |
| 127 | if (outer.top < inner.top) { |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame^] | 128 | mBlackSurfaces[0] = new BlackSurface(layer, |
| 129 | outer.left, outer.top, inner.right, inner.top, dc); |
Dianne Hackborn | 7916ac6 | 2011-05-16 20:45:48 -0700 | [diff] [blame] | 130 | } |
| 131 | if (outer.left < inner.left) { |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame^] | 132 | mBlackSurfaces[1] = new BlackSurface(layer, |
| 133 | outer.left, inner.top, inner.left, outer.bottom, dc); |
Dianne Hackborn | 7916ac6 | 2011-05-16 20:45:48 -0700 | [diff] [blame] | 134 | } |
| 135 | if (outer.bottom > inner.bottom) { |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame^] | 136 | mBlackSurfaces[2] = new BlackSurface(layer, |
| 137 | inner.left, inner.bottom, outer.right, outer.bottom, dc); |
Dianne Hackborn | 7916ac6 | 2011-05-16 20:45:48 -0700 | [diff] [blame] | 138 | } |
| 139 | if (outer.right > inner.right) { |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame^] | 140 | mBlackSurfaces[3] = new BlackSurface(layer, |
| 141 | inner.right, outer.top, outer.right, inner.bottom, dc); |
Dianne Hackborn | 7916ac6 | 2011-05-16 20:45:48 -0700 | [diff] [blame] | 142 | } |
| 143 | success = true; |
| 144 | } finally { |
| 145 | if (!success) { |
| 146 | kill(); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | public void kill() { |
| 152 | if (mBlackSurfaces != null) { |
| 153 | for (int i=0; i<mBlackSurfaces.length; i++) { |
| 154 | if (mBlackSurfaces[i] != null) { |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 155 | if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) Slog.i(TAG_WM, |
| 156 | " BLACK " + mBlackSurfaces[i].surface + ": DESTROY"); |
Dianne Hackborn | 7916ac6 | 2011-05-16 20:45:48 -0700 | [diff] [blame] | 157 | mBlackSurfaces[i].surface.destroy(); |
| 158 | mBlackSurfaces[i] = null; |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | public void hide() { |
| 165 | if (mBlackSurfaces != null) { |
| 166 | for (int i=0; i<mBlackSurfaces.length; i++) { |
| 167 | if (mBlackSurfaces[i] != null) { |
| 168 | mBlackSurfaces[i].surface.hide(); |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
Craig Mautner | 46ac6fa | 2013-08-01 10:06:34 -0700 | [diff] [blame] | 174 | public void setAlpha(float alpha) { |
| 175 | for (int i=0; i<mBlackSurfaces.length; i++) { |
| 176 | if (mBlackSurfaces[i] != null) { |
| 177 | mBlackSurfaces[i].setAlpha(alpha); |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
Dianne Hackborn | 7916ac6 | 2011-05-16 20:45:48 -0700 | [diff] [blame] | 182 | public void setMatrix(Matrix matrix) { |
| 183 | for (int i=0; i<mBlackSurfaces.length; i++) { |
| 184 | if (mBlackSurfaces[i] != null) { |
| 185 | mBlackSurfaces[i].setMatrix(matrix); |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
Dianne Hackborn | 7916ac6 | 2011-05-16 20:45:48 -0700 | [diff] [blame] | 190 | public void clearMatrix() { |
| 191 | for (int i=0; i<mBlackSurfaces.length; i++) { |
| 192 | if (mBlackSurfaces[i] != null) { |
| 193 | mBlackSurfaces[i].clearMatrix(); |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | } |