| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.hardware; |
| 18 |
|
| 19 |
import android.annotation.IntDef; |
| 20 |
import android.annotation.IntRange; |
| 21 |
import android.annotation.LongDef; |
| 22 |
import android.annotation.NonNull; |
| 23 |
import android.compat.annotation.UnsupportedAppUsage; |
| 24 |
import android.graphics.GraphicBuffer; |
| 25 |
import android.os.Build; |
| 26 |
import android.os.Parcel; |
| 27 |
import android.os.Parcelable; |
| 28 |
import android.view.SurfaceControl; |
| 29 |
|
| 30 |
import dalvik.annotation.optimization.CriticalNative; |
| 31 |
import dalvik.annotation.optimization.FastNative; |
| 32 |
import dalvik.system.CloseGuard; |
| 33 |
|
| 34 |
import libcore.util.NativeAllocationRegistry; |
| 35 |
|
| 36 |
import java.lang.annotation.Retention; |
| 37 |
import java.lang.annotation.RetentionPolicy; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public final class HardwareBuffer implements Parcelable, AutoCloseable { |
| 49 |
|
| 50 |
@Retention(RetentionPolicy.SOURCE) |
| 51 |
@IntDef(prefix = { "RGB", "BLOB", "YCBCR_", "D_", "DS_", "S_" }, value = { |
| 52 |
RGBA_8888, |
| 53 |
RGBA_FP16, |
| 54 |
RGBA_1010102, |
| 55 |
RGBX_8888, |
| 56 |
RGB_888, |
| 57 |
RGB_565, |
| 58 |
BLOB, |
| 59 |
YCBCR_420_888, |
| 60 |
D_16, |
| 61 |
D_24, |
| 62 |
DS_24UI8, |
| 63 |
D_FP32, |
| 64 |
DS_FP32UI8, |
| 65 |
S_UI8, |
| 66 |
YCBCR_P010, |
| 67 |
}) |
| 68 |
public @interface Format { |
| 69 |
} |
| 70 |
|
| 71 |
@Format |
| 72 |
|
| 73 |
public static final int RGBA_8888 = 1; |
| 74 |
|
| 75 |
public static final int RGBX_8888 = 2; |
| 76 |
|
| 77 |
public static final int RGB_888 = 3; |
| 78 |
|
| 79 |
public static final int RGB_565 = 4; |
| 80 |
|
| 81 |
public static final int RGBA_FP16 = 0x16; |
| 82 |
|
| 83 |
public static final int RGBA_1010102 = 0x2b; |
| 84 |
|
| 85 |
public static final int BLOB = 0x21; |
| 86 |
|
| 87 |
public static final int YCBCR_420_888 = 0x23; |
| 88 |
|
| 89 |
public static final int D_16 = 0x30; |
| 90 |
|
| 91 |
public static final int D_24 = 0x31; |
| 92 |
|
| 93 |
public static final int DS_24UI8 = 0x32; |
| 94 |
|
| 95 |
public static final int D_FP32 = 0x33; |
| 96 |
|
| 97 |
public static final int DS_FP32UI8 = 0x34; |
| 98 |
|
| 99 |
public static final int S_UI8 = 0x35; |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public static final int YCBCR_P010 = 0x36; |
| 108 |
|
| 109 |
|
| 110 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 111 |
private long mNativeObject; |
| 112 |
|
| 113 |
|
| 114 |
private Runnable mCleaner; |
| 115 |
|
| 116 |
private final CloseGuard mCloseGuard = CloseGuard.get(); |
| 117 |
|
| 118 |
|
| 119 |
@Retention(RetentionPolicy.SOURCE) |
| 120 |
@LongDef(flag = true, value = {USAGE_CPU_READ_RARELY, USAGE_CPU_READ_OFTEN, |
| 121 |
USAGE_CPU_WRITE_RARELY, USAGE_CPU_WRITE_OFTEN, USAGE_GPU_SAMPLED_IMAGE, |
| 122 |
USAGE_GPU_COLOR_OUTPUT, USAGE_COMPOSER_OVERLAY, USAGE_PROTECTED_CONTENT, |
| 123 |
USAGE_VIDEO_ENCODE, USAGE_GPU_DATA_BUFFER, USAGE_SENSOR_DIRECT_DATA, |
| 124 |
USAGE_GPU_CUBE_MAP, USAGE_GPU_MIPMAP_COMPLETE, USAGE_FRONT_BUFFER}) |
| 125 |
public @interface Usage {}; |
| 126 |
|
| 127 |
@Usage |
| 128 |
|
| 129 |
public static final long USAGE_CPU_READ_RARELY = 2; |
| 130 |
|
| 131 |
public static final long USAGE_CPU_READ_OFTEN = 3; |
| 132 |
|
| 133 |
|
| 134 |
public static final long USAGE_CPU_WRITE_RARELY = 2 << 4; |
| 135 |
|
| 136 |
public static final long USAGE_CPU_WRITE_OFTEN = 3 << 4; |
| 137 |
|
| 138 |
|
| 139 |
public static final long USAGE_GPU_SAMPLED_IMAGE = 1 << 8; |
| 140 |
|
| 141 |
public static final long USAGE_GPU_COLOR_OUTPUT = 1 << 9; |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
public static final long USAGE_COMPOSER_OVERLAY = 1 << 11; |
| 152 |
|
| 153 |
public static final long USAGE_PROTECTED_CONTENT = 1 << 14; |
| 154 |
|
| 155 |
public static final long USAGE_VIDEO_ENCODE = 1 << 16; |
| 156 |
|
| 157 |
public static final long USAGE_SENSOR_DIRECT_DATA = 1 << 23; |
| 158 |
|
| 159 |
public static final long USAGE_GPU_DATA_BUFFER = 1 << 24; |
| 160 |
|
| 161 |
public static final long USAGE_GPU_CUBE_MAP = 1 << 25; |
| 162 |
|
| 163 |
public static final long USAGE_GPU_MIPMAP_COMPLETE = 1 << 26; |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
public static final long USAGE_FRONT_BUFFER = 1L << 32; |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
@NonNull |
| 188 |
public static HardwareBuffer create( |
| 189 |
@IntRange(from = 1) int width, @IntRange(from = 1) int height, |
| 190 |
@Format int format, @IntRange(from = 1) int layers, @Usage long usage) { |
| 191 |
if (width <= 0) { |
| 192 |
throw new IllegalArgumentException("Invalid width " + width); |
| 193 |
} |
| 194 |
if (height <= 0) { |
| 195 |
throw new IllegalArgumentException("Invalid height " + height); |
| 196 |
} |
| 197 |
if (layers <= 0) { |
| 198 |
throw new IllegalArgumentException("Invalid layer count " + layers); |
| 199 |
} |
| 200 |
if (format == BLOB && height != 1) { |
| 201 |
throw new IllegalArgumentException("Height must be 1 when using the BLOB format"); |
| 202 |
} |
| 203 |
long nativeObject = nCreateHardwareBuffer(width, height, format, layers, usage); |
| 204 |
if (nativeObject == 0) { |
| 205 |
throw new IllegalArgumentException("Unable to create a HardwareBuffer, either the " + |
| 206 |
"dimensions passed were too large, too many image layers were requested, " + |
| 207 |
"or an invalid set of usage flags or invalid format was passed"); |
| 208 |
} |
| 209 |
return new HardwareBuffer(nativeObject); |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
public static boolean isSupported(@IntRange(from = 1) int width, @IntRange(from = 1) int height, |
| 225 |
@Format int format, @IntRange(from = 1) int layers, @Usage long usage) { |
| 226 |
if (width <= 0) { |
| 227 |
throw new IllegalArgumentException("Invalid width " + width); |
| 228 |
} |
| 229 |
if (height <= 0) { |
| 230 |
throw new IllegalArgumentException("Invalid height " + height); |
| 231 |
} |
| 232 |
if (layers <= 0) { |
| 233 |
throw new IllegalArgumentException("Invalid layer count " + layers); |
| 234 |
} |
| 235 |
if (format == BLOB && height != 1) { |
| 236 |
throw new IllegalArgumentException("Height must be 1 when using the BLOB format"); |
| 237 |
} |
| 238 |
return nIsSupported(width, height, format, layers, usage); |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
@NonNull |
| 249 |
public static HardwareBuffer createFromGraphicBuffer(@NonNull GraphicBuffer graphicBuffer) { |
| 250 |
long nativeObject = nCreateFromGraphicBuffer(graphicBuffer); |
| 251 |
return new HardwareBuffer(nativeObject); |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
| 259 |
private HardwareBuffer(long nativeObject) { |
| 260 |
mNativeObject = nativeObject; |
| 261 |
long bufferSize = nEstimateSize(nativeObject); |
| 262 |
ClassLoader loader = HardwareBuffer.class.getClassLoader(); |
| 263 |
NativeAllocationRegistry registry = new NativeAllocationRegistry( |
| 264 |
loader, nGetNativeFinalizer(), bufferSize); |
| 265 |
mCleaner = registry.registerNativeAllocation(this, mNativeObject); |
| 266 |
mCloseGuard.open("HardwareBuffer.close"); |
| 267 |
} |
| 268 |
|
| 269 |
@Override |
| 270 |
protected void finalize() throws Throwable { |
| 271 |
try { |
| 272 |
mCloseGuard.warnIfOpen(); |
| 273 |
close(); |
| 274 |
} finally { |
| 275 |
super.finalize(); |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
public int getWidth() { |
| 283 |
checkClosed("width"); |
| 284 |
return nGetWidth(mNativeObject); |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
public int getHeight() { |
| 291 |
checkClosed("height"); |
| 292 |
return nGetHeight(mNativeObject); |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
@Format |
| 299 |
public int getFormat() { |
| 300 |
checkClosed("format"); |
| 301 |
return nGetFormat(mNativeObject); |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
public int getLayers() { |
| 308 |
checkClosed("layer count"); |
| 309 |
return nGetLayers(mNativeObject); |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
public long getUsage() { |
| 316 |
checkClosed("usage"); |
| 317 |
return nGetUsage(mNativeObject); |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
public long getId() { |
| 325 |
checkClosed("id"); |
| 326 |
return nGetId(mNativeObject); |
| 327 |
} |
| 328 |
|
| 329 |
private void checkClosed(String name) { |
| 330 |
if (isClosed()) { |
| 331 |
throw new IllegalStateException("This HardwareBuffer has been closed and its " |
| 332 |
+ name + " cannot be obtained."); |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
@Override |
| 344 |
public void close() { |
| 345 |
if (!isClosed()) { |
| 346 |
mCloseGuard.close(); |
| 347 |
mNativeObject = 0; |
| 348 |
mCleaner.run(); |
| 349 |
mCleaner = null; |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
public boolean isClosed() { |
| 363 |
return mNativeObject == 0; |
| 364 |
} |
| 365 |
|
| 366 |
@Override |
| 367 |
public int describeContents() { |
| 368 |
return Parcelable.CONTENTS_FILE_DESCRIPTOR; |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
@Override |
| 382 |
public void writeToParcel(Parcel dest, int flags) { |
| 383 |
if (isClosed()) { |
| 384 |
throw new IllegalStateException("This HardwareBuffer has been closed and cannot be " |
| 385 |
+ "written to a parcel."); |
| 386 |
} |
| 387 |
nWriteHardwareBufferToParcel(mNativeObject, dest); |
| 388 |
} |
| 389 |
|
| 390 |
public static final @android.annotation.NonNull Parcelable.Creator<HardwareBuffer> CREATOR = |
| 391 |
new Parcelable.Creator<HardwareBuffer>() { |
| 392 |
public HardwareBuffer createFromParcel(Parcel in) { |
| 393 |
long nativeObject = nReadHardwareBufferFromParcel(in); |
| 394 |
if (nativeObject != 0) { |
| 395 |
return new HardwareBuffer(nativeObject); |
| 396 |
} |
| 397 |
return null; |
| 398 |
} |
| 399 |
|
| 400 |
public HardwareBuffer[] newArray(int size) { |
| 401 |
return new HardwareBuffer[size]; |
| 402 |
} |
| 403 |
}; |
| 404 |
|
| 405 |
private static native long nCreateHardwareBuffer(int width, int height, int format, int layers, |
| 406 |
long usage); |
| 407 |
private static native long nCreateFromGraphicBuffer(GraphicBuffer graphicBuffer); |
| 408 |
private static native long nGetNativeFinalizer(); |
| 409 |
private static native void nWriteHardwareBufferToParcel(long nativeObject, Parcel dest); |
| 410 |
private static native long nReadHardwareBufferFromParcel(Parcel in); |
| 411 |
@FastNative |
| 412 |
private static native int nGetWidth(long nativeObject); |
| 413 |
@FastNative |
| 414 |
private static native int nGetHeight(long nativeObject); |
| 415 |
@FastNative |
| 416 |
private static native int nGetFormat(long nativeObject); |
| 417 |
@FastNative |
| 418 |
private static native int nGetLayers(long nativeObject); |
| 419 |
@FastNative |
| 420 |
private static native long nGetUsage(long nativeObject); |
| 421 |
private static native boolean nIsSupported(int width, int height, int format, int layers, |
| 422 |
long usage); |
| 423 |
@CriticalNative |
| 424 |
private static native long nEstimateSize(long nativeObject); |
| 425 |
@CriticalNative |
| 426 |
private static native long nGetId(long nativeObject); |
| 427 |
} |