forked from angelsl/Wabbitemu-Android
-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathMainThread.java
More file actions
94 lines (78 loc) · 2.62 KB
/
Copy pathMainThread.java
File metadata and controls
94 lines (78 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package io.github.angelsl.wabbitemu.calc;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import androidx.annotation.Nullable;
import android.view.SurfaceHolder;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
public class MainThread implements SurfaceHolder.Callback, Runnable {
private final Paint mPaint;
private final Object mScreenLock = new Object();
private IntBuffer mCurrentScreenBuffer;
private volatile Bitmap mScreenBitmap;
private volatile boolean mHasCreatedLcd;
private Rect mLcdRect;
private Rect mScreenRect;
private volatile SurfaceHolder mSurfaceHolder;
public MainThread() {
mPaint = new Paint();
mPaint.setAntiAlias(false);
mPaint.setARGB(0xFF, 0xFF, 0xFF, 0xFF);
}
public void recreateScreen(final Rect lcdRect, final Rect screenRect) {
mLcdRect = lcdRect;
mScreenRect = new Rect(screenRect);
mScreenRect.offset(-mScreenRect.left, -mScreenRect.top);
mScreenBitmap = Bitmap.createBitmap(mLcdRect.width(), mLcdRect.height(), Bitmap.Config.ARGB_8888);
mCurrentScreenBuffer = ByteBuffer.allocateDirect(mLcdRect.width() * mLcdRect.height() * 4).asIntBuffer();
mHasCreatedLcd = true;
}
public IntBuffer getScreenBuffer() {
return mCurrentScreenBuffer;
}
@Nullable
public Bitmap getScreen() {
synchronized (mScreenLock) {
return mScreenBitmap;
}
}
@Override
public void run() {
if (mSurfaceHolder == null || !mHasCreatedLcd) {
return;
}
synchronized (mScreenLock) {
Canvas canvas = null;
try {
canvas = mSurfaceHolder.lockCanvas();
if (canvas == null) {
return;
}
mScreenBitmap.copyPixelsFromBuffer(mCurrentScreenBuffer);
if (getScreen() != null) {
canvas.drawBitmap(mScreenBitmap, mLcdRect, mScreenRect, mPaint);
}
} finally {
if (canvas != null) {
mSurfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
mSurfaceHolder = holder;
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// no-op
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
synchronized (mScreenLock) {
mSurfaceHolder = null;
}
}
}