package com.google.android.testing.nativedriver.client;
import com.google.common.base.Preconditions;
import com.google.common.io.LittleEndianDataInputStream;
import com.google.common.primitives.Ints;
import java.awt.image.BufferedImage;
import java.io.DataInput;
import java.io.IOException;
import java.io.InputStream;
protected static final int FBIOGET_FSCREENINFO = 0x4600;
public static final String FB_DEVICEFILE = "/dev/graphics/fb0";
private static final int FB_VAR_SCREENINFO_XRESOLUTION_OFFSET = 0;
private static final int FB_VAR_SCREENINFO_YRESOLUTION_OFFSET = 4;
private static final int FB_VAR_SCREENINFO_BITSPERPIXEL_OFFSET = 24;
private static final int FB_VAR_SCREENINFO_STRUCTSIZE = 28;
private final int xResolution, yResolution, bitsPerPixel;
Preconditions.checkArgument(isSupportedBitsPerPixel(bitsPerPixel));
this.xResolution = xResolution;
this.yResolution = yResolution;
this.bitsPerPixel = bitsPerPixel;
}
return bitsPerPixel == 15 || bitsPerPixel == 16 || bitsPerPixel == 32;
}
public static FrameBufferFormat
ofDevice(AdbConnection adb) {
byte[] varScreenInfo = adb.doIoctlForReading(
FB_DEVICEFILE, FBIOGET_FSCREENINFO, FB_VAR_SCREENINFO_STRUCTSIZE);
int xResolution = readLittleEndianInteger32(
varScreenInfo, FB_VAR_SCREENINFO_XRESOLUTION_OFFSET);
int yResolution = readLittleEndianInteger32(
varScreenInfo, FB_VAR_SCREENINFO_YRESOLUTION_OFFSET);
int bitsPerPixel = readLittleEndianInteger32(
varScreenInfo, FB_VAR_SCREENINFO_BITSPERPIXEL_OFFSET);
return new FrameBufferFormat(xResolution, yResolution, bitsPerPixel);
}
return xResolution;
}
return yResolution;
}
return bitsPerPixel;
}
byte byte4 = source[byteIndex++];
byte byte3 = source[byteIndex++];
byte byte2 = source[byteIndex++];
byte byte1 = source[byteIndex];
return Ints.fromBytes(byte1, byte2, byte3, byte4);
}
try {
switch (bitsPerPixel) {
case 15:
for (int x = 0; x < into.length; x++) {
int rgb = frameBuffer.readShort() & 0x7fff;
int red = rgb >> 10;
red = (red << 3) | (red >> 2);
int green = (rgb >> 5) & 31;
green = (green << 3) | (green >> 2);
int blue = rgb & 31;
blue = (blue << 3) | (blue >> 2);
into[x] = 0xff000000 | (red << 16) | (green << 8) | blue;
}
break;
case 16:
for (int x = 0; x < into.length; x++) {
int rgb = frameBuffer.readShort() & 0xffff;
int red = rgb >> 11;
red = (red << 3) | (red >> 2);
int green = (rgb >> 5) & 63;
green = (green << 2) | (green >> 4);
int blue = rgb & 31;
blue = (blue << 3) | (blue >> 2);
into[x] = 0xff000000 | (red << 16) | (green << 8) | blue;
}
break;
case 32:
for (int x = 0; x < into.length; x++) {
into[x] = frameBuffer.readInt();
}
}
} catch (IOException exception) {
throw new AdbException(
"IOException when reading screenshot data over adb.", exception);
}
}
InputStream source, BufferedImage destination) {
DataInput sourceDataStream
= new LittleEndianDataInputStream(source);
int[] oneLine = new int[getXResolution()];
for (int y = 0; y < yResolution; y++) {
convertToRgba32(sourceDataStream, oneLine);
destination.setRGB(0, y, xResolution, 1, oneLine, 0, xResolution);
}
}
@Override
return String.format("{FrameBufferFormat xres: %d, yres: %d, bpp: %d}",
xResolution, yResolution, bitsPerPixel);
}
}