mirror of
https://github.com/switchbrew/libnx.git
synced 2025-06-21 12:32:40 +02:00
gfx: finally get rid of nvgfx, replaced with an NvMap object
This commit is contained in:
parent
5fe01c065a
commit
3a31df429e
@ -86,7 +86,6 @@ extern "C" {
|
||||
#include "switch/display/binder.h"
|
||||
#include "switch/display/parcel.h"
|
||||
#include "switch/display/buffer_producer.h"
|
||||
#include "switch/display/nvgfx.h"
|
||||
|
||||
#include "switch/nvidia/ioctl.h"
|
||||
#include "switch/nvidia/map.h"
|
||||
|
@ -1,7 +0,0 @@
|
||||
#pragma once
|
||||
#include "../types.h"
|
||||
|
||||
Result nvgfxInitialize(void);
|
||||
void nvgfxExit(void);
|
||||
Result nvgfxSubmitGpfifo(void);
|
||||
Result nvgfxGetFramebuffer(u8 **buffer, size_t *size, u32 *handle);
|
@ -9,8 +9,8 @@
|
||||
#include "services/nv.h"
|
||||
#include "display/binder.h"
|
||||
#include "display/buffer_producer.h"
|
||||
#include "display/nvgfx.h"
|
||||
#include "display/gfx.h"
|
||||
#include "nvidia/map.h"
|
||||
|
||||
static bool g_gfxInitialized = 0;
|
||||
static ViDisplay g_gfxDisplay;
|
||||
@ -24,7 +24,6 @@ static bool g_gfx_ProducerConnected = 0;
|
||||
static u32 g_gfx_ProducerSlotsRequested = 0;
|
||||
static u8 *g_gfxFramebuf;
|
||||
static size_t g_gfxFramebufSize;
|
||||
static u32 g_gfxFramebufHandle;
|
||||
static BqQueueBufferOutput g_gfx_Connect_QueueBufferOutput;
|
||||
static BqQueueBufferOutput g_gfx_QueueBuffer_QueueBufferOutput;
|
||||
|
||||
@ -48,7 +47,8 @@ static s32 g_gfx_autoresolution_docked_height;
|
||||
|
||||
extern u32 __nx_applet_type;
|
||||
|
||||
extern u32 g_nvgfx_totalframebufs;
|
||||
static const u32 g_nvgfx_totalframebufs = 2;
|
||||
static NvMap g_nvmap_obj;
|
||||
|
||||
//static Result _gfxGetDisplayResolution(u64 *width, u64 *height);
|
||||
|
||||
@ -179,7 +179,6 @@ Result gfxInitDefault(void) {
|
||||
g_gfx_ProducerConnected = 0;
|
||||
g_gfxFramebuf = NULL;
|
||||
g_gfxFramebufSize = 0;
|
||||
g_gfxFramebufHandle = 0;
|
||||
g_gfxMode = GfxMode_LinearDouble;
|
||||
|
||||
g_gfxQueueBufferData.transform = 0;
|
||||
@ -223,6 +222,15 @@ Result gfxInitDefault(void) {
|
||||
return rc;
|
||||
}
|
||||
|
||||
g_gfxFramebufSize = g_nvgfx_totalframebufs*g_gfx_singleframebuf_size;
|
||||
g_gfxFramebuf = memalign(0x1000, g_gfxFramebufSize);
|
||||
if (!g_gfxFramebuf) {
|
||||
free(g_gfxFramebufLinear);
|
||||
g_gfxFramebufLinear = NULL;
|
||||
rc = MAKERESULT(Module_Libnx, LibnxError_OutOfMemory);
|
||||
return rc;
|
||||
}
|
||||
|
||||
rc = viInitialize(ViServiceType_Default);
|
||||
|
||||
if (R_SUCCEEDED(rc)) rc = viOpenDefaultDisplay(&g_gfxDisplay);
|
||||
@ -248,9 +256,22 @@ Result gfxInitDefault(void) {
|
||||
|
||||
if (R_SUCCEEDED(rc)) rc = nvFenceInit();
|
||||
|
||||
if (R_SUCCEEDED(rc)) rc = nvgfxInitialize();
|
||||
if (R_SUCCEEDED(rc)) rc = nvMapInit();
|
||||
|
||||
if (R_SUCCEEDED(rc)) rc = nvgfxGetFramebuffer(&g_gfxFramebuf, &g_gfxFramebufSize, &g_gfxFramebufHandle);
|
||||
if (R_SUCCEEDED(rc)) rc = nvMapCreate(&g_nvmap_obj, g_gfxFramebuf, g_gfxFramebufSize, 0x20000, NvKind_Pitch, true);
|
||||
|
||||
if (R_SUCCEEDED(rc)) {
|
||||
for (s32 i = 0; i < g_nvgfx_totalframebufs; i ++) {
|
||||
g_gfx_BufferInitData.refcount = i;
|
||||
g_gfx_BufferInitData.data.nvmap_handle0 = nvMapGetId(&g_nvmap_obj);
|
||||
g_gfx_BufferInitData.data.nvmap_handle1 = nvMapGetHandle(&g_nvmap_obj);
|
||||
g_gfx_BufferInitData.data.buffer_offset = g_gfx_singleframebuf_size*i;
|
||||
//g_gfx_BufferInitData.data.timestamp = svcGetSystemTick();
|
||||
rc = bqSetPreallocatedBuffer(&g_gfxBinderSession, i, &g_gfx_BufferInitData);
|
||||
if (R_FAILED(rc))
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (R_SUCCEEDED(rc)) rc = _gfxDequeueBuffer();
|
||||
|
||||
@ -263,7 +284,8 @@ Result gfxInitDefault(void) {
|
||||
if (g_gfx_ProducerSlotsRequested & BIT(i)) bqDetachBuffer(&g_gfxBinderSession, i);
|
||||
}
|
||||
bqDisconnect(&g_gfxBinderSession, NATIVE_WINDOW_API_CPU);
|
||||
nvgfxExit();
|
||||
nvMapFree(&g_nvmap_obj);
|
||||
nvMapExit();
|
||||
nvFenceExit();
|
||||
nvExit();
|
||||
}
|
||||
@ -275,6 +297,7 @@ Result gfxInitDefault(void) {
|
||||
viCloseDisplay(&g_gfxDisplay);
|
||||
viExit();
|
||||
|
||||
free(g_gfxFramebuf);
|
||||
free(g_gfxFramebufLinear);
|
||||
g_gfxFramebufLinear = NULL;
|
||||
|
||||
@ -283,7 +306,6 @@ Result gfxInitDefault(void) {
|
||||
g_gfx_ProducerConnected = 0;
|
||||
g_gfxFramebuf = NULL;
|
||||
g_gfxFramebufSize = 0;
|
||||
g_gfxFramebufHandle = 0;
|
||||
|
||||
g_gfx_framebuf_width = 0;
|
||||
g_gfx_framebuf_height = 0;
|
||||
@ -307,7 +329,8 @@ void gfxExit(void)
|
||||
if (g_gfx_ProducerSlotsRequested & BIT(i)) bqDetachBuffer(&g_gfxBinderSession, i);
|
||||
}
|
||||
bqDisconnect(&g_gfxBinderSession, NATIVE_WINDOW_API_CPU);
|
||||
nvgfxExit();
|
||||
nvMapFree(&g_nvmap_obj);
|
||||
nvMapExit();
|
||||
nvFenceExit();
|
||||
nvExit();
|
||||
}
|
||||
@ -319,6 +342,7 @@ void gfxExit(void)
|
||||
viCloseDisplay(&g_gfxDisplay);
|
||||
viExit();
|
||||
|
||||
free(g_gfxFramebuf);
|
||||
free(g_gfxFramebufLinear);
|
||||
g_gfxFramebufLinear = NULL;
|
||||
|
||||
@ -417,16 +441,6 @@ void gfxConfigureAutoResolutionDefault(bool enable) {
|
||||
gfxConfigureAutoResolution(enable, 1280, 720, 0, 0);
|
||||
}
|
||||
|
||||
Result _gfxGraphicBufferInit(s32 buf, u32 nvmap_id, u32 nvmap_handle) {
|
||||
g_gfx_BufferInitData.refcount = buf;
|
||||
g_gfx_BufferInitData.data.nvmap_handle0 = nvmap_id;
|
||||
g_gfx_BufferInitData.data.nvmap_handle1 = nvmap_handle;
|
||||
g_gfx_BufferInitData.data.buffer_offset = g_gfx_singleframebuf_size*buf;
|
||||
//g_gfx_BufferInitData.data.timestamp = svcGetSystemTick();
|
||||
|
||||
return bqSetPreallocatedBuffer(&g_gfxBinderSession, buf, &g_gfx_BufferInitData);
|
||||
}
|
||||
|
||||
void gfxWaitForVsync(void) {
|
||||
Result rc = eventWait(&g_gfxDisplayVsyncEvent, U64_MAX);
|
||||
if (R_FAILED(rc))
|
||||
@ -450,7 +464,7 @@ u32 gfxGetFramebufferHandle(u32 index, u32* offset) {
|
||||
index = (g_gfxCurrentBuffer + index) & (g_nvgfx_totalframebufs-1);
|
||||
*offset = index*g_gfx_singleframebuf_size;
|
||||
}
|
||||
return g_gfxFramebufHandle;
|
||||
return nvMapGetHandle(&g_nvmap_obj);
|
||||
}
|
||||
|
||||
u8* gfxGetFramebuffer(u32* width, u32* height) {
|
||||
|
@ -1,136 +0,0 @@
|
||||
#include <string.h>
|
||||
#include <malloc.h>
|
||||
#include "types.h"
|
||||
#include "result.h"
|
||||
#include "arm/cache.h"
|
||||
#include "kernel/svc.h"
|
||||
#include "services/nv.h"
|
||||
#include "display/binder.h"
|
||||
#include "display/buffer_producer.h"
|
||||
#include "display/nvgfx.h"
|
||||
#include "nvidia/ioctl.h"
|
||||
|
||||
typedef struct {
|
||||
bool initialized;
|
||||
u32 handle;
|
||||
u8 *mem;
|
||||
size_t mem_size;
|
||||
} nvmapobj;
|
||||
|
||||
static bool g_nvgfxInitialized;
|
||||
static u32 g_nvgfx_fd_nvmap;
|
||||
|
||||
u32 g_nvgfx_totalframebufs = 2;
|
||||
|
||||
static nvmapobj nvmap_fb_obj;
|
||||
|
||||
extern size_t g_gfx_singleframebuf_size;
|
||||
|
||||
Result _gfxGraphicBufferInit(s32 buf, u32 nvmap_id, u32 nvmap_handle);
|
||||
|
||||
static Result nvmapobjInitialize(nvmapobj *obj, size_t size) {
|
||||
Result rc=0;
|
||||
|
||||
if(obj->initialized)return 0;
|
||||
|
||||
memset(obj, 0, sizeof(nvmapobj));
|
||||
|
||||
obj->mem_size = size;
|
||||
|
||||
obj->mem = memalign(0x1000, size);
|
||||
if (obj->mem==NULL) rc = MAKERESULT(Module_Libnx, LibnxError_OutOfMemory);
|
||||
if (R_SUCCEEDED(rc)) memset(obj->mem, 0, size);
|
||||
|
||||
if (R_SUCCEEDED(rc)) armDCacheFlush(obj->mem, size);
|
||||
|
||||
if (R_SUCCEEDED(rc)) obj->initialized = 1;
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
static void nvmapobjClose(nvmapobj *obj) {
|
||||
if(!obj->initialized)return;
|
||||
|
||||
if (obj->mem) {
|
||||
free(obj->mem);
|
||||
obj->mem = NULL;
|
||||
}
|
||||
|
||||
memset(obj, 0, sizeof(nvmapobj));
|
||||
}
|
||||
|
||||
static Result nvmapobjSetup(nvmapobj *obj, u32 heapmask, u32 flags, u32 align, u8 kind) {
|
||||
Result rc=0;
|
||||
|
||||
rc = nvioctlNvmap_Create(g_nvgfx_fd_nvmap, obj->mem_size, &obj->handle);
|
||||
if (R_SUCCEEDED(rc)) rc = nvioctlNvmap_Alloc(g_nvgfx_fd_nvmap, obj->handle, heapmask, flags, align, kind, obj->mem);
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
Result nvgfxInitialize(void) {
|
||||
Result rc=0;
|
||||
|
||||
if (g_nvgfxInitialized)
|
||||
return 0;
|
||||
|
||||
if (R_SUCCEEDED(rc)) rc = nvOpen(&g_nvgfx_fd_nvmap, "/dev/nvmap");
|
||||
if (R_SUCCEEDED(rc)) rc = nvmapobjInitialize(&nvmap_fb_obj, g_nvgfx_totalframebufs*g_gfx_singleframebuf_size);
|
||||
if (R_SUCCEEDED(rc)) rc = nvmapobjSetup(&nvmap_fb_obj, 0, 0x1, 0x20000, 0);
|
||||
|
||||
if (R_SUCCEEDED(rc))
|
||||
{
|
||||
u32 id = 0;
|
||||
rc = nvioctlNvmap_GetId(g_nvgfx_fd_nvmap, nvmap_fb_obj.handle, &id);
|
||||
if (R_SUCCEEDED(rc))
|
||||
{
|
||||
for (int i = 0; i < g_nvgfx_totalframebufs; i ++)
|
||||
{
|
||||
rc = _gfxGraphicBufferInit(i, id, nvmap_fb_obj.handle);
|
||||
if (R_FAILED(rc))
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (R_FAILED(rc)) {
|
||||
nvmapobjClose(&nvmap_fb_obj);
|
||||
if (g_nvgfx_fd_nvmap != -1) {
|
||||
nvClose(g_nvgfx_fd_nvmap);
|
||||
g_nvgfx_fd_nvmap = -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (R_SUCCEEDED(rc))
|
||||
g_nvgfxInitialized = true;
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
void nvgfxExit(void) {
|
||||
if (!g_nvgfxInitialized)
|
||||
return;
|
||||
|
||||
nvmapobjClose(&nvmap_fb_obj);
|
||||
if (g_nvgfx_fd_nvmap != -1) {
|
||||
nvClose(g_nvgfx_fd_nvmap);
|
||||
g_nvgfx_fd_nvmap = -1;
|
||||
}
|
||||
|
||||
g_nvgfxInitialized = false;
|
||||
}
|
||||
|
||||
Result nvgfxGetFramebuffer(u8 **buffer, size_t *size, u32 *handle)
|
||||
{
|
||||
if (!g_nvgfxInitialized)
|
||||
return MAKERESULT(Module_Libnx, LibnxError_NotInitialized);
|
||||
|
||||
if (buffer != NULL)
|
||||
*buffer = nvmap_fb_obj.mem;
|
||||
if (size != NULL)
|
||||
*size = nvmap_fb_obj.mem_size;
|
||||
if (handle != NULL)
|
||||
*handle = nvmap_fb_obj.handle;
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user