Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/libretro/LibretroGraphicsContext.h
3185 views
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
class LibretroSoftwareContext : public LibretroGraphicsContext {
72
public:
73
LibretroSoftwareContext() {}
74
bool Init() override { return true; }
75
void SwapBuffers() override {
76
GPUDebugBuffer buf;
77
u16 w = NATIVEWIDTH;
78
u16 h = NATIVEHEIGHT;
79
gpuDebug->GetOutputFramebuffer(buf);
80
const std::vector<u32> pixels = TranslateDebugBufferToCompare(&buf, w, h);
81
memcpy(soft_bmp, pixels.data(), SOFT_BMP_SIZE);
82
u32 offset = g_Config.bDisplayCropTo16x9 ? w << 1 : 0;
83
h -= g_Config.bDisplayCropTo16x9 ? 2 : 0;
84
video_cb(soft_bmp + offset, w, h, w << 2);
85
}
86
GPUCore GetGPUCore() override { return GPUCORE_SOFTWARE; }
87
const char *Ident() override { return "Software"; }
88
89
u16 soft_bmp[SOFT_BMP_SIZE] = {0};
90
};
91
92
namespace Libretro {
93
extern LibretroGraphicsContext *ctx;
94
extern retro_environment_t environ_cb;
95
extern retro_hw_context_type backend;
96
97
enum class EmuThreadState {
98
DISABLED,
99
START_REQUESTED,
100
RUNNING,
101
PAUSE_REQUESTED,
102
PAUSED,
103
QUIT_REQUESTED,
104
STOPPED,
105
};
106
extern bool useEmuThread;
107
extern std::atomic<EmuThreadState> emuThreadState;
108
void EmuThreadStart();
109
void EmuThreadStop();
110
void EmuThreadPause();
111
} // namespace Libretro
112
113