CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/libretro/LibretroGraphicsContext.h
Views: 1401
1
#pragma once
2
#include <atomic>
3
4
#include "libretro/libretro.h"
5
#include "Common/GraphicsContext.h"
6
#include "Common/GPU/thin3d_create.h"
7
8
#include "Core/Config.h"
9
#include "Core/System.h"
10
#include "GPU/GPUState.h"
11
#include "GPU/Software/SoftGpu.h"
12
#include "headless/Compare.h"
13
#include "Common/Data/Convert/ColorConv.h"
14
15
#define NATIVEWIDTH 480
16
#define NATIVEHEIGHT 272
17
#define SOFT_BMP_SIZE NATIVEWIDTH * NATIVEHEIGHT * 4
18
19
class LibretroGraphicsContext : public GraphicsContext {
20
public:
21
LibretroGraphicsContext() {}
22
~LibretroGraphicsContext() override { Shutdown(); }
23
24
virtual bool Init() = 0;
25
virtual void SetRenderTarget() {}
26
virtual GPUCore GetGPUCore() = 0;
27
virtual const char *Ident() = 0;
28
29
void Shutdown() override {
30
DestroyDrawContext();
31
}
32
virtual void SwapBuffers() = 0;
33
void Resize() override {}
34
35
virtual void GotBackbuffer();
36
virtual void LostBackbuffer();
37
38
virtual void CreateDrawContext() {}
39
virtual void DestroyDrawContext() {
40
if (!draw_) {
41
return;
42
}
43
delete draw_;
44
draw_ = nullptr;
45
}
46
Draw::DrawContext *GetDrawContext() override { return draw_; }
47
48
static LibretroGraphicsContext *CreateGraphicsContext();
49
50
static retro_video_refresh_t video_cb;
51
52
protected:
53
Draw::DrawContext *draw_ = nullptr;
54
};
55
56
class LibretroHWRenderContext : public LibretroGraphicsContext {
57
public:
58
LibretroHWRenderContext(retro_hw_context_type context_type, unsigned version_major = 0, unsigned version_minor = 0);
59
bool Init(bool cache_context);
60
void SetRenderTarget() override {}
61
void SwapBuffers() override {
62
video_cb(RETRO_HW_FRAME_BUFFER_VALID, PSP_CoreParameter().pixelWidth, PSP_CoreParameter().pixelHeight, 0);
63
}
64
virtual void ContextReset();
65
virtual void ContextDestroy();
66
67
protected:
68
retro_hw_render_callback hw_render_ = {};
69
};
70
71
#ifdef _WIN32
72
class LibretroD3D9Context : public LibretroHWRenderContext {
73
public:
74
LibretroD3D9Context() : LibretroHWRenderContext(RETRO_HW_CONTEXT_DIRECT3D, 9) {}
75
bool Init() override { return false; }
76
77
void CreateDrawContext() override {
78
draw_ = Draw::T3DCreateDX9Context(nullptr, nullptr, 0, nullptr, nullptr);
79
draw_->CreatePresets();
80
}
81
82
GPUCore GetGPUCore() override { return GPUCORE_DIRECTX9; }
83
const char *Ident() override { return "DirectX 9"; }
84
};
85
#endif
86
87
class LibretroSoftwareContext : public LibretroGraphicsContext {
88
public:
89
LibretroSoftwareContext() {}
90
bool Init() override { return true; }
91
void SwapBuffers() override {
92
GPUDebugBuffer buf;
93
u16 w = NATIVEWIDTH;
94
u16 h = NATIVEHEIGHT;
95
gpuDebug->GetOutputFramebuffer(buf);
96
const std::vector<u32> pixels = TranslateDebugBufferToCompare(&buf, w, h);
97
memcpy(soft_bmp, pixels.data(), SOFT_BMP_SIZE);
98
u32 offset = g_Config.bDisplayCropTo16x9 ? w << 1 : 0;
99
h -= g_Config.bDisplayCropTo16x9 ? 2 : 0;
100
video_cb(soft_bmp + offset, w, h, w << 2);
101
}
102
GPUCore GetGPUCore() override { return GPUCORE_SOFTWARE; }
103
const char *Ident() override { return "Software"; }
104
105
u16 soft_bmp[SOFT_BMP_SIZE] = {0};
106
};
107
108
namespace Libretro {
109
extern LibretroGraphicsContext *ctx;
110
extern retro_environment_t environ_cb;
111
extern retro_hw_context_type backend;
112
113
enum class EmuThreadState {
114
DISABLED,
115
START_REQUESTED,
116
RUNNING,
117
PAUSE_REQUESTED,
118
PAUSED,
119
QUIT_REQUESTED,
120
STOPPED,
121
};
122
extern bool useEmuThread;
123
extern std::atomic<EmuThreadState> emuThreadState;
124
void EmuThreadStart();
125
void EmuThreadStop();
126
void EmuThreadPause();
127
} // namespace Libretro
128
129