internal/graphicsdriver/playstation5: bug fix: Cgo bans to pass a Go pointer to a Go pointer

This commit is contained in:
Hajime Hoshi 2024-09-29 18:12:02 +09:00
parent 1b6a615adb
commit 1c475eff78
3 changed files with 32 additions and 37 deletions

View File

@ -25,14 +25,15 @@ extern "C" ebitengine_Error ebitengine_NewImage(int *image, int width,
return {};
}
extern "C" ebitengine_Error
ebitengine_ReadPixels(int image, ebitengine_PixelsArgs *args, int arg_count) {
return {};
}
extern "C" void ebitengine_ReadPixels(int image, uint8_t *pixels,
ebitengine_Region region) {}
extern "C" ebitengine_Error
ebitengine_WritePixels(int image, const ebitengine_PixelsArgs *args,
int arg_count) {
extern "C" ebitengine_Error ebitengine_FlushReadPixels(int image) { return {}; }
extern "C" void ebitengine_WritePixels(int image, const uint8_t *pixels,
ebitengine_Region region) {}
extern "C" ebitengine_Error ebitengine_FlushWritePixels(int image) {
return {};
}

View File

@ -180,38 +180,32 @@ func (i *Image) Dispose() {
}
func (i *Image) ReadPixels(args []graphicsdriver.PixelsArgs) error {
cArgs := make([]C.ebitengine_PixelsArgs, 0, len(args))
for _, a := range args {
cArgs = append(cArgs, C.ebitengine_PixelsArgs{
min_x: C.int(a.Region.Min.X),
min_y: C.int(a.Region.Min.Y),
max_x: C.int(a.Region.Max.X),
max_y: C.int(a.Region.Max.Y),
pixels: (*C.uint8_t)(unsafe.Pointer(unsafe.SliceData(a.Pixels))),
})
region := C.ebitengine_Region{
min_x: C.int(a.Region.Min.X),
min_y: C.int(a.Region.Min.Y),
max_x: C.int(a.Region.Max.X),
max_y: C.int(a.Region.Max.Y),
}
C.ebitengine_ReadPixels(C.int(i.id), (*C.uint8_t)(unsafe.Pointer(unsafe.SliceData(a.Pixels))), region)
}
defer runtime.KeepAlive(cArgs)
if err := C.ebitengine_ReadPixels(C.int(i.id), unsafe.SliceData(cArgs), C.int(len(cArgs))); !C.ebitengine_IsErrorNil(&err) {
if err := C.ebitengine_FlushReadPixels(C.int(i.id)); !C.ebitengine_IsErrorNil(&err) {
return newPlaystation5Error("(*playstation5.Image).ReadPixels", err)
}
return nil
}
func (i *Image) WritePixels(args []graphicsdriver.PixelsArgs) error {
cArgs := make([]C.ebitengine_PixelsArgs, 0, len(args))
for _, a := range args {
cArgs = append(cArgs, C.ebitengine_PixelsArgs{
min_x: C.int(a.Region.Min.X),
min_y: C.int(a.Region.Min.Y),
max_x: C.int(a.Region.Max.X),
max_y: C.int(a.Region.Max.Y),
pixels: (*C.uint8_t)(unsafe.Pointer(unsafe.SliceData(a.Pixels))),
})
region := C.ebitengine_Region{
min_x: C.int(a.Region.Min.X),
min_y: C.int(a.Region.Min.Y),
max_x: C.int(a.Region.Max.X),
max_y: C.int(a.Region.Max.Y),
}
C.ebitengine_WritePixels(C.int(i.id), (*C.uint8_t)(unsafe.Pointer(unsafe.SliceData(a.Pixels))), region)
}
defer runtime.KeepAlive(cArgs)
if err := C.ebitengine_WritePixels(C.int(i.id), unsafe.SliceData(cArgs), C.int(len(cArgs))); !C.ebitengine_IsErrorNil(&err) {
if err := C.ebitengine_FlushWritePixels(C.int(i.id)); !C.ebitengine_IsErrorNil(&err) {
return newPlaystation5Error("(*playstation5.Image).WritePixels", err)
}
return nil

View File

@ -34,13 +34,12 @@ static bool ebitengine_IsErrorNil(ebitengine_Error *err) {
return err->message == NULL && err->code == 0;
}
typedef struct ebitengine_PixelsArgs {
uint8_t *pixels;
typedef struct ebitengine_Region {
int min_x;
int min_y;
int max_x;
int max_y;
} ebitengine_PixelsArgs;
} ebitengine_Region;
typedef struct ebitengine_DstRegion {
int min_x;
@ -63,11 +62,12 @@ ebitengine_Error ebitengine_InitializeGraphics(void);
ebitengine_Error ebitengine_NewImage(int *image, int width, int height);
ebitengine_Error ebitengine_NewScreenFramebufferImage(int *image, int width,
int height);
ebitengine_Error ebitengine_ReadPixels(int image, ebitengine_PixelsArgs *args,
int arg_count);
ebitengine_Error ebitengine_WritePixels(int image,
const ebitengine_PixelsArgs *args,
int arg_count);
void ebitengine_ReadPixels(int image, uint8_t *pixels,
ebitengine_Region region);
ebitengine_Error ebitengine_FlushReadPixels(int image);
void ebitengine_WritePixels(int image, const uint8_t *pixels,
ebitengine_Region region);
ebitengine_Error ebitengine_FlushWritePixels(int image);
void ebitengine_DisposeImage(int id);
void ebitengine_Begin();