--- nono/vm/renderer.h 2026/04/29 17:05:25 1.1.1.10 +++ nono/vm/renderer.h 2026/04/29 17:05:29 1.1.1.11 @@ -12,37 +12,42 @@ #include "thread.h" #include "bitmap.h" +#include "fixedqueue.h" #include "monitor.h" #include "spscqueue.h" -#include "stopwatch.h" #include #include class PlaneVRAMDevice; +class Scheduler; class Syncer; class VideoCtlrDevice; // 更新情報交換用構造体 struct ModifyInfo { + // 1ライン単位の更新フラグ。 std::array bits {}; - uint32 n_dirty {}; - // パレットを変更した場合とスクロールした場合のように - // composite に変更によらず bitmap を更新する場合は true。 - bool invalidate2 {}; - - void Fill(uint8 v) { - std::fill(bits.begin(), bits.end(), v); - if (v) { - n_dirty = bits.size(); - } else { - n_dirty = 0; + // 更新フラグの立っているライン数を返す。 + uint GetDirtyCount() const { + uint count = 0; + for (auto v : bits) { + if (v) { + count++; + } } + return count; } - bool IsClean() const { return n_dirty == 0; } - bool IsDirty() const { return n_dirty != 0; } + // bits がいずれかでも立っていれば true。 + bool dirty {}; + bool IsClean() const { return !dirty; } + bool IsDirty() const { return dirty; } + + // パレットを変更した場合とスクロールした場合のように + // composite の変更によらず bitmap を更新する場合は true。 + bool invalidate2 {}; }; class Renderer : public ThreadDevice @@ -50,8 +55,10 @@ class Renderer : public ThreadDevice using inherited = ThreadDevice; public: static const uint REQ_TERMINATE = 0x01; // スレッド終了指示 - static const uint REQ_REND_POWEROFF = 0x02; // 電源オフ時の描画指示 - static const uint REQ_REND_NORMAL = 0x04; // 通常の描画指示 + static const uint REQ_SCREENOFF = 0x02; // 電源オフ時の描画指示 + static const uint REQ_RENDER = 0x04; // 通常の描画指示 + static const uint REQ_RESIZE = 0x08; // リサイズ指示 + static const uint REQ_POST = 0x10; // UI への通知指示 public: Renderer(); @@ -61,36 +68,52 @@ class Renderer : public ThreadDevice void ResetHard(bool poweron) override; void PowerOff() override; - // 出力ビットマップを取得 - const BitmapRGB& GetBitmap() { return bitmap; } - - // 合成処理イネーブル - void Enable(); + // 出力ビットマップが有効なら取得。 + // (wxImage に渡すため const なし) + BitmapRGB *GetBitmap() { + if (__predict_true(bitmap24_valid)) { + return bitmap24.get(); + } else { + return NULL; + } + } - // 画面合成処理 (CRTC/CRTC2 から呼ばれる) - void Render(); + // 画面合成処理の指示 (CRTC/CRTC2 から呼ばれる) + void NotifyRender(); // 全画面更新の指示 (パレット変更とスクロール座標変更で呼ばれる) void Invalidate2(); + // 表示画面サイズを設定 (GUI から呼ばれる) + void ResizeView(uint width_, uint height_); + // 仮想画面幅(ピクセル)を取得 - int GetWidth() const { return width; } + uint GetWidth() const { return width; } // 仮想画面高さ(ピクセル)を取得 - int GetHeight() const { return height; } + uint GetHeight() const { return height; } // 描画更新(通知)間隔を設定 (GUI から呼ばれる) void SetRefreshInterval(uint64 t); protected: + // モニタを初期化する + void InitMonitor(); + // レンダリングスレッド void ThreadRun() override; - // 画面合成。更新があれば true を返す。(派生クラス側で用意) - virtual bool RenderMain() = 0; + // 描画する。 + int64 DoRender(uint32 req); + + // 更新チェック。(派生クラス側で用意) + virtual bool GetModifyMD(ModifyInfo *dst) const = 0; + + // 更新情報をクリア。(派生クラス側で用意) + virtual void ClearDirtyMD() = 0; - // RGBX から RGB への変換。 - void RGBXtoRGB(BitmapRGB& dst, const BitmapRGBX& src); + // 画面合成。更新があれば true を返す。(派生クラス側で用意) + virtual bool RenderMD() = 0; DECLARE_MONITOR_CALLBACK(MonitorUpdate); @@ -100,25 +123,38 @@ class Renderer : public ThreadDevice // LUNA でいう 1280 x 1024 のこと。 // 仮想画面幅(ピクセル) - int width {}; + uint width {}; // 仮想画面高さ(ピクセル) - int height {}; + uint height {}; // レンダリング結果のビットマップ - BitmapRGB bitmap {}; + BitmapRGBX bitmap {}; - // ステージ2 の全画面更新指示フラグ - bool invalidate2 {}; + // wxWidgets 用表示ビットマップ + std::unique_ptr bitmap24 /*{}*/; + + // 表示ビットマップの大きさ + Rect viewrect {}; // リクエストフラグ uint32 request {}; std::mutex mtx {}; std::condition_variable cv {}; - // 合成処理有効フラグ + // リサイズ時の要求サイズ + uint new_viewwidth {}; + uint new_viewheight {}; + + // レンダラ有効フラグ (GUI なら true) bool enable {}; + // ステージ2 の全画面更新指示フラグ + bool invalidate2 {}; + + // 初期化やリサイズ後に一度でも bitmap24 を更新すれば true。 + bool bitmap24_valid {}; + // 更新情報を VM -> Renderer スレッドに渡すための 1 要素キュー SPSCQueue modq {}; @@ -130,17 +166,23 @@ class Renderer : public ThreadDevice // 次回更新を通知する実時刻 uint64 next_refresh_rtime {}; - // 出力画面に更新がある (UI に通知する必要がある) 場合は true - bool out_dirty {}; - // 統計情報 - uint64 render_exec {}; // RenderMain() の実行回数 - Stopwatch render_time {}; // RenderMain() の総実行時間 - uint32 last_mod_blocks {}; // 直近の更新ブロック数 - uint64 total_mod_blocks {}; // 総更新ブロック数 + uint32 last_mod_lines {}; // 直近の更新ライン数 uint64 total_invalidate2 {}; // ステージ2の全画面更新回数 - PlaneVRAMDevice *planevram {}; + uint64 stat_input_count {}; // 入力通知回数 + uint64 last_input_time {}; // 前回の入力通知 VM 時刻 + FixedQueue ma_input_span {}; // 入力通知間隔 + uint64 stat_enqueue_count {}; // リクエストキューに入れた回数 + uint64 stat_enqueue_full {}; // キューに入れられなかった回数 + uint64 stat_render_count {}; // RenderMD() の実行回数 + uint64 stat_noupdate_count {}; // bitmap24 への更新が不要だった回数 + uint64 stat_nobitmap_count {}; // bitmap24 が用意できてなかった回数 + uint64 stat_output_count {}; // UI への出力通知回数 + uint64 last_output_time {}; // 前回の出力通知 RT 時刻 + FixedQueue ma_output_span {}; // 出力通知間隔 + + Scheduler *scheduler {}; Syncer *syncer {}; Monitor monitor { this }; @@ -148,12 +190,6 @@ class Renderer : public ThreadDevice private: // スレッドの終了を指示 void Terminate() override; - - static void RGBXtoRGB_gen(BitmapRGB& dst, const BitmapRGBX& src); -#if defined(HAVE_AVX2) - static void RGBXtoRGB_avx2(BitmapRGB& dst, const BitmapRGBX& src); - bool enable_avx2 {}; -#endif }; // @@ -167,11 +203,12 @@ class X68030Renderer : public Renderer ~X68030Renderer() override; bool Init() override; - bool RenderMain() override; + bool GetModifyMD(ModifyInfo *) const override; + void ClearDirtyMD() override; + bool RenderMD() override; private: - BitmapRGBX bitmap32 {}; - + PlaneVRAMDevice *planevram {}; VideoCtlrDevice *videoctlr {}; }; @@ -186,10 +223,12 @@ class LunaRenderer : public Renderer ~LunaRenderer() override; bool Init() override; - bool RenderMain() override; + bool GetModifyMD(ModifyInfo *) const override; + void ClearDirtyMD() override; + bool RenderMD() override; private: - BitmapRGBX textbmp {}; + PlaneVRAMDevice *planevram {}; }; // @@ -202,7 +241,9 @@ class NopRenderer : public Renderer NopRenderer(); ~NopRenderer() override; - bool RenderMain() override; + bool GetModifyMD(ModifyInfo *) const override; + void ClearDirtyMD() override; + bool RenderMD() override; }; static inline Renderer *GetRenderer() {