From 009ecbccf53c332cb194d31cdad5c8a36c5bc0d4 Mon Sep 17 00:00:00 2001 From: dgis Date: Tue, 12 Oct 2021 20:49:50 +0200 Subject: [PATCH] Cleanup win32 code. --- app/build.gradle | 11 ++-- app/src/main/cpp/emu-jni.c | 16 ----- app/src/main/cpp/win32-layer.c | 112 ++++++++++++++++++++++++++------- app/src/main/cpp/win32-layer.h | 67 ++++++++++---------- build.gradle | 2 +- 5 files changed, 131 insertions(+), 77 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index bf48f81..f79b71e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -80,17 +80,16 @@ android { sourceCompatibility = '1.8' targetCompatibility = '1.8' } - ndkVersion '22.1.7171670' } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) - implementation 'androidx.appcompat:appcompat:1.2.0' - implementation 'androidx.constraintlayout:constraintlayout:2.0.4' + implementation 'androidx.appcompat:appcompat:1.3.1' + implementation 'androidx.constraintlayout:constraintlayout:2.1.1' implementation 'androidx.preference:preference:1.1.1' - implementation 'com.google.android.material:material:1.2.1' + implementation 'com.google.android.material:material:1.4.0' implementation 'androidx.documentfile:documentfile:1.0.1' testImplementation 'junit:junit:4.13.1' - androidTestImplementation 'androidx.test:runner:1.3.0' - androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' + androidTestImplementation 'androidx.test:runner:1.4.0' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' } diff --git a/app/src/main/cpp/emu-jni.c b/app/src/main/cpp/emu-jni.c index be894af..bdd32db 100644 --- a/app/src/main/cpp/emu-jni.c +++ b/app/src/main/cpp/emu-jni.c @@ -668,10 +668,6 @@ JNIEXPORT jint JNICALL Java_org_emulator_calculator_NativeLib_onFileNew(JNIEnv * chooseCurrentKmlMode = ChooseKmlMode_UNKNOWN; if(result) { - if(hLcdDC && hLcdDC->selectedBitmap) { - hLcdDC->selectedBitmap->bitmapInfoHeader->biHeight = -abs(hLcdDC->selectedBitmap->bitmapInfoHeader->biHeight); - } - mainViewResizeCallback(nBackgroundW, nBackgroundH); draw(); @@ -718,11 +714,6 @@ JNIEXPORT jint JNICALL Java_org_emulator_calculator_NativeLib_onFileOpen(JNIEnv } BOOL result = OpenDocument(szBufferFilename); if(pbyRomBackup) pbyRomBackup = NULL; - if (result) { - if(hLcdDC && hLcdDC->selectedBitmap) { - hLcdDC->selectedBitmap->bitmapInfoHeader->biHeight = -abs(hLcdDC->selectedBitmap->bitmapInfoHeader->biHeight); - } - } chooseCurrentKmlMode = ChooseKmlMode_UNKNOWN; mainViewResizeCallback(nBackgroundW, nBackgroundH); if(result) { @@ -1069,10 +1060,6 @@ JNIEXPORT jint JNICALL Java_org_emulator_calculator_NativeLib_onViewScript(JNIEn chooseCurrentKmlMode = ChooseKmlMode_UNKNOWN; if(bSucc) { - if(hLcdDC && hLcdDC->selectedBitmap) { - hLcdDC->selectedBitmap->bitmapInfoHeader->biHeight = -abs(hLcdDC->selectedBitmap->bitmapInfoHeader->biHeight); - } - mainViewResizeCallback(nBackgroundW, nBackgroundH); draw(); if (Chipset.wRomCrc != wRomCrc) // ROM changed @@ -1102,9 +1089,6 @@ JNIEXPORT void JNICALL Java_org_emulator_calculator_NativeLib_onBackupSave(JNIEn JNIEXPORT void JNICALL Java_org_emulator_calculator_NativeLib_onBackupRestore(JNIEnv *env, jobject thisz) { SwitchToState(SM_INVALID); RestoreBackup(); - if(hLcdDC && hLcdDC->selectedBitmap) { - hLcdDC->selectedBitmap->bitmapInfoHeader->biHeight = -abs(hLcdDC->selectedBitmap->bitmapInfoHeader->biHeight); - } if (pbyRom) SwitchToState(SM_RUN); } diff --git a/app/src/main/cpp/win32-layer.c b/app/src/main/cpp/win32-layer.c index d034591..99c02a8 100644 --- a/app/src/main/cpp/win32-layer.c +++ b/app/src/main/cpp/win32-layer.c @@ -30,6 +30,7 @@ extern JavaVM *java_machine; extern jobject bitmapMainScreen; extern AndroidBitmapInfo androidBitmapInfo; +//extern RECT mainViewRectangleToUpdate; extern HANDLE hWnd; extern LPTSTR szTitle; @@ -1737,7 +1738,7 @@ BOOL DeleteMenu(HMENU hMenu, UINT uPosition, UINT uFlags) { return FALSE; } BOOL InsertMenu(HMENU hMenu, UINT uPosition, UINT uFlags, UINT_PTR uIDNewItem, LPCTSTR lpNewItem) { return FALSE; } BOOL SetWindowPos(HWND hWnd, HWND hWndInsertAfter, int X, int Y, int cx, int cy, UINT uFlags) { return 0; } -BOOL IsRectEmpty(CONST RECT *lprc) { return 0; } + BOOL WINAPI SetWindowOrgEx(HDC hdc, int x, int y, LPPOINT lppt) { if(lppt) { lppt->x = hdc->windowOriginX; @@ -1757,21 +1758,21 @@ HGDIOBJ SelectObject(HDC hdc, HGDIOBJ h) { case HGDIOBJ_TYPE_BRUSH: { HBRUSH oldSelectedBrushColor = hdc->selectedBrushColor; hdc->selectedBrushColor = h; - return oldSelectedBrushColor; //h; + return oldSelectedBrushColor; } case HGDIOBJ_TYPE_FONT: break; case HGDIOBJ_TYPE_BITMAP: { - //HBITMAP oldSelectedBitmap = hdc->selectedBitmap; + HBITMAP oldSelectedBitmap = hdc->selectedBitmap; hdc->selectedBitmap = h; - return h; //oldSelectedBitmap; + return oldSelectedBitmap; } case HGDIOBJ_TYPE_REGION: break; case HGDIOBJ_TYPE_PALETTE: { - //HPALETTE oldSelectedPalette = hdc->selectedPalette; + HPALETTE oldSelectedPalette = hdc->selectedPalette; hdc->selectedPalette = h; - return h; //oldSelectedPalette; + return oldSelectedPalette; } default: break; @@ -1842,6 +1843,13 @@ BOOL DeleteObject(HGDIOBJ ho) { free(ho); return TRUE; } + case HGDIOBJ_TYPE_BRUSH: { + PAINT_LOGD("PAINT DeleteObject() HGDIOBJ_TYPE_BRUSH"); + ho->handleType = HGDIOBJ_TYPE_INVALID; + ho->brushColor = 0; + free(ho); + return TRUE; + } default: break; } @@ -1962,6 +1970,13 @@ BOOL PatBlt(HDC hdcDest, int x, int y, int w, int h, DWORD rop) { destinationStride = androidBitmapInfo.stride; +// RECT newRectangleToUpdate; +// newRectangleToUpdate.left = x; +// newRectangleToUpdate.top = y; +// newRectangleToUpdate.right = x + w; +// newRectangleToUpdate.bottom = y + h; +// UnionRect(&mainViewRectangleToUpdate, &mainViewRectangleToUpdate, &newRectangleToUpdate); + if ((ret = AndroidBitmap_lockPixels(jniEnv, bitmapMainScreen, &pixelsDestination)) < 0) { LOGD("AndroidBitmap_lockPixels() failed ! error=%d", ret); return FALSE; @@ -1972,6 +1987,7 @@ BOOL PatBlt(HDC hdcDest, int x, int y, int w, int h, DWORD rop) { destinationWidth = hBitmapDestination->bitmapInfoHeader->biWidth; destinationHeight = abs(hBitmapDestination->bitmapInfoHeader->biHeight); + //TODO destinationTopDown = hBitmapDestination->bitmapInfoHeader->biHeight < 0; destinationStride = (float)(4 * ((destinationWidth * hBitmapDestination->bitmapInfoHeader->biBitCount + 31) / 32)); } @@ -2060,7 +2076,8 @@ BOOL StretchBlt(HDC hdcDest, int xDest, int yDest, int wDest, int hDest, HDC hdc void * pixelsDestination = NULL; int destinationBitCount = 8; - BOOL reverseHeight = hBitmapSource->bitmapInfoHeader->biHeight < 0; + BOOL sourceTopDown = hBitmapSource->bitmapInfoHeader->biHeight < 0; + BOOL destinationTopDown = FALSE; int sourceWidth = hBitmapSource->bitmapInfoHeader->biWidth; int sourceHeight = abs(hBitmapSource->bitmapInfoHeader->biHeight); // Can be < 0 @@ -2092,7 +2109,16 @@ BOOL StretchBlt(HDC hdcDest, int xDest, int yDest, int wDest, int hDest, HDC hdc destinationBitCount = 32; destinationStride = androidBitmapInfo.stride; - if ((ret = AndroidBitmap_lockPixels(jniEnv, bitmapMainScreen, &pixelsDestination)) < 0) { + destinationTopDown = TRUE; + +// RECT newRectangleToUpdate; +// newRectangleToUpdate.left = xDest; +// newRectangleToUpdate.top = yDest; +// newRectangleToUpdate.right = xDest + wDest; +// newRectangleToUpdate.bottom = yDest + hDest; +// UnionRect(&mainViewRectangleToUpdate, &mainViewRectangleToUpdate, &newRectangleToUpdate); + + if ((ret = AndroidBitmap_lockPixels(jniEnv, bitmapMainScreen, &pixelsDestination)) < 0) { LOGD("AndroidBitmap_lockPixels() failed ! error=%d", ret); return FALSE; } @@ -2102,6 +2128,7 @@ BOOL StretchBlt(HDC hdcDest, int xDest, int yDest, int wDest, int hDest, HDC hdc destinationWidth = hBitmapDestination->bitmapInfoHeader->biWidth; destinationHeight = abs(hBitmapDestination->bitmapInfoHeader->biHeight); + destinationTopDown = hBitmapDestination->bitmapInfoHeader->biHeight < 0; destinationBitCount = hBitmapDestination->bitmapInfoHeader->biBitCount; destinationStride = 4 * ((destinationWidth * hBitmapDestination->bitmapInfoHeader->biBitCount + 31) / 32); } @@ -2133,11 +2160,11 @@ BOOL StretchBlt(HDC hdcDest, int xDest, int yDest, int wDest, int hDest, HDC hdc backgroundColor = hdcDest->backgroundColor; } - StretchBltInternal(xDest, yDest, wDest, hDest, pixelsDestination, destinationBitCount, - destinationStride, destinationWidth, destinationHeight, - xSrc, ySrc, wSrc, hSrc, pixelsSource, sourceBitCount, - sourceStride, sourceWidth, sourceHeight, - rop, reverseHeight, palPalEntry, brushColor, backgroundColor); + StretchBltInternal(xDest, yDest, wDest, hDest, + pixelsDestination, destinationBitCount, destinationStride, destinationWidth, destinationHeight, + xSrc, ySrc, wSrc, hSrc, + pixelsSource, sourceBitCount, sourceStride, sourceWidth, sourceHeight, + rop, sourceTopDown, destinationTopDown, palPalEntry, brushColor, backgroundColor); if(jniEnv && hdcDest->hdcCompatible == NULL && (ret = AndroidBitmap_unlockPixels(jniEnv, bitmapMainScreen)) < 0) { LOGD("AndroidBitmap_unlockPixels() failed ! error=%d", ret); @@ -2153,7 +2180,7 @@ void StretchBltInternal(int xDest, int yDest, int wDest, int hDest, const void *pixelsDestination, int destinationBitCount, int destinationStride, int destinationWidth, int destinationHeight, int xSrc, int ySrc, int wSrc, int hSrc, const void *pixelsSource, UINT sourceBitCount, int sourceStride, int sourceWidth, int sourceHeight, - DWORD rop, BOOL reverseHeight, const PALETTEENTRY *palPalEntry, COLORREF brushColor, COLORREF backgroundColor) { + DWORD rop, BOOL sourceTopDown, BOOL destinationTopDown, const PALETTEENTRY *palPalEntry, COLORREF brushColor, COLORREF backgroundColor) { int dst_maxx = xDest + wDest; int dst_maxy = yDest + hDest; @@ -2170,12 +2197,19 @@ void StretchBltInternal(int xDest, int yDest, int wDest, int hDest, if(dst_maxy > destinationHeight) dst_maxy = destinationHeight; + int src_cury, dst_cury; for (int y = yDest; y < dst_maxy; y++) { - int src_cury = ySrc + (y - yDest) * hSrc / hDest; - if(!reverseHeight) - src_cury = sourceHeight - 1 - src_cury; + if(sourceTopDown) + src_cury = ySrc + (y - yDest) * hSrc / hDest; // Source top-down + else + src_cury = sourceHeight - 1 - (ySrc + (y - yDest) * hSrc / hDest); // Source bottom-up if (src_cury < 0 || src_cury >= sourceHeight) continue; + if(destinationTopDown) + dst_cury = y; // Destination top-down + else + dst_cury = destinationHeight - 1 - y; // Destination bottom-up + BYTE parity = (BYTE) xSrc; for (int x = xDest; x < dst_maxx; x++, parity++) { int src_curx = xSrc + (x - xDest) * wSrc / wDest; @@ -2183,7 +2217,7 @@ void StretchBltInternal(int xDest, int yDest, int wDest, int hDest, continue; BYTE * sourcePixelBase = pixelsSource + sourceStride * src_cury; - BYTE * destinationPixelBase = pixelsDestination + destinationStride * y; + BYTE * destinationPixelBase = pixelsDestination + destinationStride * dst_cury; COLORREF sourceColor = 0xFF000000; BYTE * sourceColorPointer = (BYTE *) &sourceColor; @@ -2343,7 +2377,7 @@ HBITMAP CreateBitmap( int nWidth, int nHeight, UINT nPlanes, UINT nBitCount, CON newBitmapInfo->bmiHeader.biBitCount = (WORD) nBitCount; newBitmapInfo->bmiHeader.biClrUsed = 0; newBitmapInfo->bmiHeader.biWidth = nWidth; - newBitmapInfo->bmiHeader.biHeight = -nHeight; + newBitmapInfo->bmiHeader.biHeight = nHeight; newBitmapInfo->bmiHeader.biPlanes = (WORD) nPlanes; newHBITMAP->bitmapInfo = newBitmapInfo; newHBITMAP->bitmapInfoHeader = (BITMAPINFOHEADER *)newBitmapInfo; @@ -2519,8 +2553,12 @@ COLORREF GetPixel(HDC hdc, int x ,int y) { return resultColor; } BOOL SetRect(LPRECT lprc, int xLeft, int yTop, int xRight, int yBottom) { - //TODO - return 0; + if (!lprc) return FALSE; + lprc->left = xLeft; + lprc->right = xRight; + lprc->top = yTop; + lprc->bottom = yBottom; + return TRUE; } BOOL SetRectEmpty(LPRECT lprc) { if(lprc) { @@ -2532,6 +2570,38 @@ BOOL SetRectEmpty(LPRECT lprc) { } return FALSE; } + +BOOL IsRectEmpty(CONST RECT *lprc) { + if (!lprc) + return TRUE; + return lprc->left >= lprc->right || lprc->top >= lprc->bottom; +} + +// This comes from Wine source code +BOOL UnionRect(LPRECT dest, CONST RECT *src1, CONST RECT *src2) { + if (!dest) return FALSE; + if (IsRectEmpty(src1)) + { + if (IsRectEmpty(src2)) + { + SetRectEmpty( dest ); + return FALSE; + } + else *dest = *src2; + } + else + { + if (IsRectEmpty(src2)) *dest = *src1; + else + { + dest->left = min( src1->left, src2->left ); + dest->right = max( src1->right, src2->right ); + dest->top = min( src1->top, src2->top ); + dest->bottom = max( src1->bottom, src2->bottom ); + } + } + return TRUE; +} int SetWindowRgn(HWND hWnd, HRGN hRgn, BOOL bRedraw) { //TODO return 0; diff --git a/app/src/main/cpp/win32-layer.h b/app/src/main/cpp/win32-layer.h index 9d71091..ea187ef 100644 --- a/app/src/main/cpp/win32-layer.h +++ b/app/src/main/cpp/win32-layer.h @@ -554,46 +554,46 @@ struct _HANDLE { enum HANDLE_TYPE handleType; union { struct { - // HANDLE_TYPE_FILE* - int fileDescriptor; - BOOL fileOpenFileFromContentResolver; + // HANDLE_TYPE_FILE* + int fileDescriptor; + BOOL fileOpenFileFromContentResolver; - AAsset* fileAsset; + AAsset* fileAsset; - // HANDLE_TYPE_FILE_MAPPING* - off_t fileMappingOffset; - size_t fileMappingSize; - void* fileMappingAddress; - DWORD fileMappingProtect; + // HANDLE_TYPE_FILE_MAPPING* + off_t fileMappingOffset; + size_t fileMappingSize; + void* fileMappingAddress; + DWORD fileMappingProtect; }; struct { - // HANDLE_TYPE_THREAD - pthread_t threadId; - DWORD (*threadStartAddress)(LPVOID); - LPVOID threadParameter; - struct _HANDLE * threadEventMessage; - struct tagMSG threadMessage; - int threadIndex; + // HANDLE_TYPE_THREAD + pthread_t threadId; + DWORD (*threadStartAddress)(LPVOID); + LPVOID threadParameter; + struct _HANDLE * threadEventMessage; + struct tagMSG threadMessage; + int threadIndex; }; struct { - // HANDLE_TYPE_EVENT - pthread_cond_t eventCVariable; - pthread_mutex_t eventMutex; - BOOL eventAutoReset; - BOOL eventState; + // HANDLE_TYPE_EVENT + pthread_cond_t eventCVariable; + pthread_mutex_t eventMutex; + BOOL eventAutoReset; + BOOL eventState; }; struct { - // HANDLE_TYPE_WINDOW - HDC windowDC; + // HANDLE_TYPE_WINDOW + HDC windowDC; }; struct { - // HANDLE_TYPE_ICON - HBITMAP icon; - }; + // HANDLE_TYPE_ICON + HBITMAP icon; +}; struct { // HANDLE_TYPE_COM @@ -783,12 +783,12 @@ extern BOOL BitBlt(HDC hdc, int x, int y, int cx, int cy, HDC hdcSrc, int x1, in extern int SetStretchBltMode(HDC hdc, int mode); extern BOOL StretchBlt(HDC hdcDest, int xDest, int yDest, int wDest, int hDest, HDC hdcSrc, int xSrc, int ySrc, int wSrc, int hSrc, DWORD rop); extern void StretchBltInternal(int xDest, int yDest, int wDest, int hDest, const void *pixelsDestination, - int destinationBitCount, int destinationStride, int destinationWidth, - int destinationHeight, int xSrc, int ySrc, int hSrc, int wSrc, - const void *pixelsSource, UINT sourceBitCount, int sourceStride, - int sourceWidth, int sourceHeight, DWORD rop, BOOL reverseHeight, - const PALETTEENTRY *palPalEntry, COLORREF brushColor, - COLORREF backgroundColor); + int destinationBitCount, int destinationStride, int destinationWidth, + int destinationHeight, int xSrc, int ySrc, int hSrc, int wSrc, + const void *pixelsSource, UINT sourceBitCount, int sourceStride, + int sourceWidth, int sourceHeight, DWORD rop, BOOL sourceTopDown, BOOL destinationTopDown, + const PALETTEENTRY *palPalEntry, COLORREF brushColor, + COLORREF backgroundColor); extern UINT SetDIBColorTable(HDC hdc, UINT iStart, UINT cEntries, CONST RGBQUAD *prgbq); /* constants for CreateDIBitmap */ #define CBM_INIT 0x04L /* initialize bitmap */ @@ -819,6 +819,8 @@ extern COLORREF SetBkColor(HDC hdc, COLORREF color); #define RDH_RECTANGLES 1 extern BOOL SetRect(LPRECT lprc, int xLeft, int yTop, int xRight, int yBottom); extern BOOL SetRectEmpty(LPRECT lprc); +extern BOOL IsRectEmpty(CONST RECT *lprc); +extern BOOL UnionRect(LPRECT dest, CONST RECT *src1, CONST RECT *src2); struct HRGN__ { int unused; }; typedef struct HRGN__ *HRGN; @@ -1003,7 +1005,6 @@ extern BOOL InsertMenu(HMENU hMenu, UINT uPosition, UINT uFlags, UINT_PTR uIDNew #define SWP_NOMOVE 0x0002 #define SWP_NOZORDER 0x0004 extern BOOL SetWindowPos(HWND hWnd, HWND hWndInsertAfter, int X, int Y, int cx, int cy, UINT uFlags); -extern BOOL IsRectEmpty(CONST RECT *lprc); extern BOOL WINAPI SetWindowOrgEx(HDC hdc, int x, int y, LPPOINT lppt); #define _MAX_PATH 260 // max. length of full pathname diff --git a/build.gradle b/build.gradle index 5623fbd..689dbbf 100644 --- a/build.gradle +++ b/build.gradle @@ -7,7 +7,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:7.0.0' + classpath 'com.android.tools.build:gradle:7.0.2' // NOTE: Do not place your application dependencies here; they belong