From 86b352c1f2e0e9a617eef6200ec337b234fb8460 Mon Sep 17 00:00:00 2001 From: sin365 <353374337@qq.com> Date: Sun, 19 Jan 2025 11:10:54 +0800 Subject: [PATCH] =?UTF-8?q?video=E4=B8=AD=20Palette=E4=B8=AD=20=E7=94=A8GC?= =?UTF-8?q?Handle=E7=9A=84=E6=96=B9=E5=BC=8F=E4=BB=A3=E6=9B=BF=E9=A2=91?= =?UTF-8?q?=E7=B9=81fixed=20|=20=E4=BA=A4=E6=8D=A2=E5=A4=A7=E9=83=A8?= =?UTF-8?q?=E5=88=86=E9=A2=9C=E8=89=B2=E9=80=9A=E9=81=93=EF=BC=8C=E8=80=83?= =?UTF-8?q?=E8=99=91PSVita?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MAME.Unity/Assets/Plugins/UMAME/emu/Motion.cs | 150 ++++++++++++++---- .../Assets/Plugins/UMAME/emu/Palette.cs | 28 ++++ MAME.Unity/Assets/Plugins/UMAME/emu/Video.cs | 45 ++++-- 3 files changed, 179 insertions(+), 44 deletions(-) diff --git a/MAME.Unity/Assets/Plugins/UMAME/emu/Motion.cs b/MAME.Unity/Assets/Plugins/UMAME/emu/Motion.cs index ad8b266..a4e7236 100644 --- a/MAME.Unity/Assets/Plugins/UMAME/emu/Motion.cs +++ b/MAME.Unity/Assets/Plugins/UMAME/emu/Motion.cs @@ -1,5 +1,6 @@ using MAME.Core; using System; +using System.Reflection; namespace MAME.Core { @@ -17,8 +18,90 @@ namespace MAME.Core motion_update_callback(); motion_handler_callback(); } - public static void ui_updateC() + //public static void ui_updateC() + //{ + // //不再填充完整画布 + // //{ + // // int i; + // // int red, green, blue; + // // if (single_step || Mame.paused) + // // { + // // byte bright = 0xa7; + // // for (i = 0; i < Video.fullwidth * Video.fullheight; i++) + // // { + // // red = (int)(((Palette.entry_color[Video.bitmapbase[Video.curbitmap][i]] & 0xff0000) >> 16) * bright / 0xff); + // // green = (int)(((Palette.entry_color[Video.bitmapbase[Video.curbitmap][i]] & 0xff00) >> 8) * bright / 0xff); + // // blue = (int)((Palette.entry_color[Video.bitmapbase[Video.curbitmap][i]] & 0xff) * bright / 0xff); + // // Video.bitmapcolor[i] = (int)Palette.make_argb(0xff, red, green, blue); + // // } + // // } + // // else + // // { + // // for (i = 0; i < Video.fullwidth * Video.fullheight; i++) + // // { + // // Video.bitmapcolor[i] = (int)Palette.entry_color[Video.bitmapbase[Video.curbitmap][i]]; + // // } + // // } + // //} + + // { + // int i; + // int target_i = 0; + // int x, y; + // int red, green, blue; + + // int startX = Video.offsetx; + // int endX = Video.offsetx + Video.width; + // int startY = Video.offsety; + // int endY = Video.offsety + Video.height; + + // if (single_step || Mame.paused) + // { + // byte bright = 0xa7; + // for (y = startY; y < endY; y++) + // { + // int stepIndex = y * Video.fullwidth; + // for (x = startX; x < endX; x++, target_i++) + // { + // //i = y * Video.fullwidth + x; + // i = stepIndex + x; + // red = (int)(((Palette.entry_color[Video.bitmapbase[Video.curbitmap][i]] & 0xff0000) >> 16) * bright / 0xff); + // green = (int)(((Palette.entry_color[Video.bitmapbase[Video.curbitmap][i]] & 0xff00) >> 8) * bright / 0xff); + // blue = (int)((Palette.entry_color[Video.bitmapbase[Video.curbitmap][i]] & 0xff) * bright / 0xff); + // Video.bitmapcolorRect[target_i] = (int)Palette.make_argb(0xff, red, green, blue); + // } + // } + // } + // else + // { + + // for (y = startY; y < endY; y++) + // { + // int stepIndex = y * Video.fullwidth; + // for (x = startX; x < endX; x++, target_i++) + // { + // //i = y * Video.fullwidth + x; + // i = stepIndex + x; + // Video.bitmapcolorRect[target_i] = (int)Palette.entry_color[Video.bitmapbase[Video.curbitmap][i]]; + // } + // } + // } + // } + //} + public unsafe static void ui_updateC() { + //fixed (ushort* curbitmapPtr = &Video.bitmapbase[Video.curbitmap][0]) + //fixed (uint* entry_colorPtr = &Palette.entry_color[0]) + //fixed (int* bitmapcolorRectPtr = &Video.bitmapcolorRect[0]) + { + //ushort* curbitmap = curbitmapPtr; + ushort* curbitmap = (ushort*)Video.bitmapbase_Ptrs[Video.curbitmap]; + //uint* entry_color = entry_colorPtr; + uint* entry_color = (uint*)Palette.entry_color_Ptr; + //int* bitmapcolorRect = bitmapcolorRectPtr; + int* bitmapcolorRect = (int*)Video.bitmapcolorRect_Ptr; + + /* //不再填充完整画布 //{ // int i; @@ -42,46 +125,45 @@ namespace MAME.Core // } // } //} - - { - int i; - int target_i = 0; - int x, y; - int red, green, blue; - - int startX = Video.offsetx; - int endX = Video.offsetx + Video.width; - int startY = Video.offsety; - int endY = Video.offsety + Video.height; - - if (single_step || Mame.paused) + */ { - byte bright = 0xa7; - for (y = startY; y < endY; y++) + int i; + int target_i = 0; + int x, y; + int red, green, blue; + + int startX = Video.offsetx; + int endX = Video.offsetx + Video.width; + int startY = Video.offsety; + int endY = Video.offsety + Video.height; + + if (single_step || Mame.paused) { - int stepIndex = y * Video.fullwidth; - for (x = startX; x < endX; x++, target_i++) + byte bright = 0xa7; + for (y = startY; y < endY; y++) { - //i = y * Video.fullwidth + x; - i = stepIndex + x; - red = (int)(((Palette.entry_color[Video.bitmapbase[Video.curbitmap][i]] & 0xff0000) >> 16) * bright / 0xff); - green = (int)(((Palette.entry_color[Video.bitmapbase[Video.curbitmap][i]] & 0xff00) >> 8) * bright / 0xff); - blue = (int)((Palette.entry_color[Video.bitmapbase[Video.curbitmap][i]] & 0xff) * bright / 0xff); - Video.bitmapcolorRect[target_i] = (int)Palette.make_argb(0xff, red, green, blue); + int stepIndex = y * Video.fullwidth; + for (x = startX; x < endX; x++, target_i++) + { + i = stepIndex + x; + red = (int)(((entry_color[curbitmap[i]] & 0xff0000) >> 16) * bright / 0xff); + green = (int)(((entry_color[curbitmap[i]] & 0xff00) >> 8) * bright / 0xff); + blue = (int)((entry_color[curbitmap[i]] & 0xff) * bright / 0xff); + bitmapcolorRect[target_i] = (int)Palette.make_argb(0xff, red, green, blue); + } } } - } - else - { - - for (y = startY; y < endY; y++) + else { - int stepIndex = y * Video.fullwidth; - for (x = startX; x < endX; x++, target_i++) + + for (y = startY; y < endY; y++) { - //i = y * Video.fullwidth + x; - i = stepIndex + x; - Video.bitmapcolorRect[target_i] = (int)Palette.entry_color[Video.bitmapbase[Video.curbitmap][i]]; + int stepIndex = y * Video.fullwidth; + for (x = startX; x < endX; x++, target_i++) + { + i = stepIndex + x; + bitmapcolorRect[target_i] = (int)entry_color[curbitmap[i]]; + } } } } diff --git a/MAME.Unity/Assets/Plugins/UMAME/emu/Palette.cs b/MAME.Unity/Assets/Plugins/UMAME/emu/Palette.cs index 20c5cea..ac5e30a 100644 --- a/MAME.Unity/Assets/Plugins/UMAME/emu/Palette.cs +++ b/MAME.Unity/Assets/Plugins/UMAME/emu/Palette.cs @@ -1,4 +1,6 @@ using MAME.Core.AxiBitmap; +using System.Runtime.InteropServices; +using System; using Color = MAME.Core.AxiBitmap.AxiColor; namespace MAME.Core @@ -6,6 +8,11 @@ namespace MAME.Core public class Palette { public static uint[] entry_color; + /** entry_color的指针管理 **/ + static GCHandle entry_color_handle; + public static IntPtr entry_color_Ptr; + /** end **/ + public static float[] entry_contrast; private static uint trans_uint; private static int numcolors, numgroups; @@ -165,8 +172,29 @@ namespace MAME.Core } break; } + + + //entry_color = new uint[numcolors]; + + + /** entry_color的指针管理 **/ + // 释放句柄 + if (entry_color != null && entry_color_handle.IsAllocated) + entry_color_handle.Free(); + entry_color = new uint[numcolors]; + // 固定数组,防止垃圾回收器移动它 + entry_color_handle = GCHandle.Alloc(entry_color, GCHandleType.Pinned); + // 获取数组的指针 + entry_color_Ptr = entry_color_handle.AddrOfPinnedObject(); + /** end **/ + + + entry_contrast = new float[numcolors]; + + + for (index = 0; index < numcolors; index++) { palette_set_callback(index, make_argb(0xff, pal1bit((byte)(index >> 0)), pal1bit((byte)(index >> 1)), pal1bit((byte)(index >> 2)))); diff --git a/MAME.Unity/Assets/Plugins/UMAME/emu/Video.cs b/MAME.Unity/Assets/Plugins/UMAME/emu/Video.cs index a1c73e2..7f36dba 100644 --- a/MAME.Unity/Assets/Plugins/UMAME/emu/Video.cs +++ b/MAME.Unity/Assets/Plugins/UMAME/emu/Video.cs @@ -36,16 +36,23 @@ namespace MAME.Core private static double speed_percent; private static uint throttle_history, overall_valid_counter, overall_real_seconds; private static int[] popcount; - public static ushort[][] bitmapbase; + //public static ushort[][] bitmapbase; public static int[][] bitmapbaseN; //public static int[] bitmapcolor; + /** bitmapcolor的指针管理 **/ + public static ushort[][] bitmapbase; + static GCHandle[] bitmapbase_handles; + public static IntPtr[] bitmapbase_Ptrs; + /** end **/ + /** bitmapcolor的指针管理 **/ //不再拷贝完整画布 //public static int[] bitmapcolor; //static GCHandle bitmapcolor_handle; //public static IntPtr bitmapcolor_Ptr; + public static int[] bitmapcolorRect; static GCHandle bitmapcolorRect_handle; public static IntPtr bitmapcolorRect_Ptr; @@ -611,6 +618,29 @@ namespace MAME.Core screenstate.frame_number = 0; + /** bitmapbase的指针管理 **/ + // 释放句柄 + if (bitmapbase_handles != null) + { + for (int i = 0; i < bitmapbase_handles.Length; i++) + { + if (bitmapbase_handles[i].IsAllocated) + bitmapbase_handles[i].Free(); + } + bitmapbase_handles = null; + bitmapbase_Ptrs = null; + } + + bitmapbase_handles = new GCHandle[bitmapbase.Length]; + bitmapbase_Ptrs = new IntPtr[bitmapbase.Length]; + for (int i = 0; i < bitmapbase.Length; i++) + { + bitmapbase_handles[i] = GCHandle.Alloc(bitmapbase[i], GCHandleType.Pinned); + bitmapbase_Ptrs[i] = bitmapbase_handles[i].AddrOfPinnedObject(); + } + + /** end **/ + //bitmapcolor = new int[Video.fullwidth * Video.fullheight]; /** bitmapcolor的指针管理 **/ //不再拷贝完整画布 @@ -631,20 +661,15 @@ namespace MAME.Core - if (bitmapcolorRect != null) - { - // 释放句柄 - if (bitmapcolorRect_handle.IsAllocated) - { - bitmapcolorRect_handle.Free(); - } - } + // 释放句柄 + if (bitmapcolorRect != null && bitmapcolorRect_handle.IsAllocated) + bitmapcolorRect_handle.Free(); + bitmapcolorRect = new int[width * height]; // 固定数组,防止垃圾回收器移动它 bitmapcolorRect_handle = GCHandle.Alloc(bitmapcolorRect, GCHandleType.Pinned); // 获取数组的指针 bitmapcolorRect_Ptr = bitmapcolorRect_handle.AddrOfPinnedObject(); - /** end **/