Skip to content

Commit

Permalink
Merge pull request openstf#19 from DeviceFarmer/issue-17
Browse files Browse the repository at this point in the history
Fix rotation issue
  • Loading branch information
koral-- authored Jul 16, 2021
2 parents 391f6ad + a0f3982 commit 8cbf4ed
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 19 deletions.
16 changes: 13 additions & 3 deletions experimental/app/src/main/java/io/devicefarmer/minicap/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ class Main {
Size(
params.projection.targetSize.width,
params.projection.targetSize.height
)
),
angleToRotation(params.projection.rotation)
)
}
provider.quality = params.quality
Expand Down Expand Up @@ -105,9 +106,18 @@ class Main {
}
}

private fun angleToRotation(value: Int): Int =
when(value) {
0 -> 0
90 -> 1
180 -> 2
270 -> 3
else -> throw IllegalStateException("Invalid rotation")
}

data class Projection(
val realSize: Size, var targetSize: Size,
val orientation: Int
val rotation: Int
) {
fun forceAspectRatio() {
val aspect = realSize.width.toFloat() / realSize.height.toFloat()
Expand All @@ -119,7 +129,7 @@ data class Projection(
}

override fun toString(): String =
"${realSize.width}x${realSize.height}@${targetSize.width}x${targetSize.height}/${orientation}"
"${realSize.width}x${realSize.height}@${targetSize.width}x${targetSize.height}/${rotation}"
}

class Parameters private constructor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,18 @@ class MinicapClientOutput(
/**
* Sends the banner required at connection time
*/
fun sendBanner(screenSize: Size, targetSize: Size) {
fun sendBanner(screenSize: Size, targetSize: Size, rotation: Int ) {
val byteArray = ByteArray(BANNER_SIZE)
ByteBuffer.wrap(byteArray).apply {
order(ByteOrder.LITTLE_ENDIAN)
put(BANNER_VERSION.toByte())
put(BANNER_SIZE.toByte())
putInt(android.os.Process.myPid()) //PID
putInt(screenSize.width)//Width
putInt(screenSize.height)//Height
putInt(targetSize.width)//resized Width
putInt(targetSize.height)//resized height
put(0.toByte()) //orientation
putInt(screenSize.width)
putInt(screenSize.height)
putInt(targetSize.width)
putInt(targetSize.height)
put(rotation.toByte()) //as per libui ui::Rotation enum
put(QUIRK_ALWAYS_UPRIGHT.toByte()) //quirk
}
with(socket.outputStream) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import java.nio.ByteBuffer
* and sends the results to an output (could be a file for screenshot, or a minicap client receiving the
* jpeg stream)
*/
abstract class BaseProvider(private val targetSize: Size) : SimpleServer.Listener,
abstract class BaseProvider(private val targetSize: Size, val rotation: Int) : SimpleServer.Listener,
ImageReader.OnImageAvailableListener {

companion object {
Expand All @@ -60,7 +60,7 @@ abstract class BaseProvider(private val targetSize: Size) : SimpleServer.Listene
abstract fun screenshot(printer: PrintStream)
abstract fun getScreenSize(): Size

fun getTargetSize(): Size = targetSize
fun getTargetSize(): Size = if(rotation%2 != 0) Size(targetSize.height, targetSize.width) else targetSize
fun getImageReader(): ImageReader = imageReader

fun init(out: DisplayOutput) {
Expand All @@ -75,7 +75,7 @@ abstract class BaseProvider(private val targetSize: Size) : SimpleServer.Listene

override fun onConnection(socket: LocalSocket) {
clientOutput = MinicapClientOutput(socket).apply {
sendBanner(getScreenSize(),getTargetSize())
sendBanner(getScreenSize(),getTargetSize(),rotation)
}
init(clientOutput)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ import kotlin.system.exitProcess
* Provides screen images using [SurfaceControl]. This is pretty similar to the native version
* of minicap but here it is done at a higher level making things a bit easier.
*/
class SurfaceProvider(targetSize: Size) : BaseProvider(targetSize) {
constructor() : this(currentScreenSize())
class SurfaceProvider(targetSize: Size, orientation: Int) : BaseProvider(targetSize, orientation) {
constructor() : this(currentScreenSize(), currentRotation())

companion object {
private fun currentScreenSize(): Size {
Expand All @@ -43,6 +43,8 @@ class SurfaceProvider(targetSize: Size) : BaseProvider(targetSize) {
}
}

private fun currentRotation(): Int = currentDisplayInfo().rotation

private fun currentDisplayInfo(): DisplayInfo {
return DisplayManagerGlobal.getDisplayInfo(0)
}
Expand All @@ -53,9 +55,8 @@ class SurfaceProvider(targetSize: Size) : BaseProvider(targetSize) {

val displayInfo: DisplayInfo = currentDisplayInfo()

override fun getScreenSize(): Size {
return displayInfo.size
}
override fun getScreenSize(): Size = displayInfo.size


override fun screenshot(printer: PrintStream) {
init(ScreenshotOutput(printer))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ object SurfaceControl {

fun setDisplayProjection(
displayToken: IBinder?,
orientation: Int,
rotation: Int, //Rotation0 = 0, Rotation90 = 1, Rotation180 = 2, Rotation270 = 3
layerStackRect: Rect?,
displayRect: Rect?
) {
Expand All @@ -82,7 +82,7 @@ object SurfaceControl {
"setDisplayProjection", IBinder::class.java,
Int::class.javaPrimitiveType, Rect::class.java, Rect::class.java
)
.invoke(null, displayToken, orientation, layerStackRect, displayRect)
.invoke(null, displayToken, rotation, layerStackRect, displayRect)
} catch (e: Exception) {
logAndThrow(e)
}
Expand Down

0 comments on commit 8cbf4ed

Please sign in to comment.