Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions core/libs/prx/libSceJxr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ add_library(jxr_lookup_native STATIC src/NativeLookup.c src/NativeQuant.c)
configure_jxr_native(jxr_lookup_native)
target_include_directories(jxr_lookup_native PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(jxr_lookup_native PRIVATE jxr)
add_library(libSceJxr SHARED EXCLUDE_FROM_ALL Export.cpp src/Allocation.cpp src/PixelFormatLookup.cpp src/GuidExports.cpp src/Quantizer.cpp)
target_link_libraries(libSceJxr PRIVATE jxr_lookup_native)
add_library(libSceJxr SHARED EXCLUDE_FROM_ALL Export.cpp src/Allocation.cpp src/GlueHelpers.cpp src/PixelFormatLookup.cpp src/GuidExports.cpp src/Quantizer.cpp)
add_library(jxr_glue_guest STATIC ${JXR_SOURCE_DIR}/jxrgluelib/JXRGlue.c src/NativeGlue.c)
configure_jxr_native(jxr_glue_guest)
set_source_files_properties(${JXR_SOURCE_DIR}/jxrgluelib/JXRGlue.c TARGET_DIRECTORY jxr_glue_guest
PROPERTIES COMPILE_OPTIONS "-include;${CMAKE_CURRENT_SOURCE_DIR}/src/GuestHeap.h")
target_link_libraries(jxr_glue_guest PRIVATE jxr)
target_link_libraries(libSceJxr PRIVATE jxr_glue_guest jxr_lookup_native)
if(NOT APPLE)
target_link_options(libSceJxr PRIVATE LINKER:--exclude-libs,ALL)
endif()
Expand Down Expand Up @@ -70,6 +75,19 @@ if(BUILD_TESTING)
endif()
endif()

if(BUILD_TESTING)
add_test_executable(guest_jxr_glue_tests ${LIBS_INCLUDE_DIR}/tests/GuestJxrGlue.cpp)
target_include_directories(guest_jxr_glue_tests PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(guest_jxr_glue_tests PRIVATE libSceJxr libc jxr)
configure_windows_unwind(guest_jxr_glue_tests)
add_test(NAME guest_jxr_glue COMMAND guest_jxr_glue_tests)
set_tests_properties(guest_jxr_glue PROPERTIES TIMEOUT 20)
if(WIN32)
set_tests_properties(guest_jxr_glue PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceJxr>")
endif()
endif()

if(BUILD_TESTING)
add_library(jxr_quantizer_reference STATIC ${LIBS_INCLUDE_DIR}/tests/GuestJxrQuantizerReference.c)
configure_jxr_native(jxr_quantizer_reference)
Expand Down
24 changes: 0 additions & 24 deletions core/libs/prx/libSceJxr/Export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,18 +139,6 @@ int APS5_VABI FreeDescMetadata_nid_postfix() {
NotImplemented_nid_no_patch(__func__);
}

int APS5_VABI GetImageDecodeIID_nid_postfix() {
NotImplemented_nid_no_patch(__func__);
}

int APS5_VABI GetImageEncodeIID_nid_postfix() {
NotImplemented_nid_no_patch(__func__);
}

int APS5_VABI GetPixelFormatFromHash_nid_postfix() {
NotImplemented_nid_no_patch(__func__);
}

int APS5_VABI GetPosWS_File_nid_postfix() {
NotImplemented_nid_no_patch(__func__);
}
Expand All @@ -165,14 +153,6 @@ std::int64_t APS5_VABI GetPosWS_Memory_nid_postfix(Jxr::Stream* stream, std::siz
return WmpErrSuccess;
}

int APS5_VABI IDPEmpty_nid_postfix() {
NotImplemented_nid_no_patch(__func__);
}

int APS5_VABI PKAllocAligned_nid_postfix() {
NotImplemented_nid_no_patch(__func__);
}

int APS5_VABI PKCodecFactory_CreateCodec_nid_postfix() {
NotImplemented_nid_no_patch(__func__);
}
Expand Down Expand Up @@ -245,10 +225,6 @@ int APS5_VABI PKFormatConverter_Release_nid_postfix() {
NotImplemented_nid_no_patch(__func__);
}

int APS5_VABI PKFreeAligned_nid_postfix() {
NotImplemented_nid_no_patch(__func__);
}

int APS5_VABI PKImageDecode_Copy_nid_postfix() {
NotImplemented_nid_no_patch(__func__);
}
Expand Down
13 changes: 7 additions & 6 deletions core/libs/prx/libSceJxr/include/Api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "SceTypes.hpp"
#include <cstddef>
#include <cstdint>
#include "prx/libSceJxr/GuestPixelInfo.h"
#include "prx/libSceJxr/Stream.hpp"

Expand All @@ -11,6 +12,12 @@ std::int64_t APS5_VABI PKFree_nid_postfix(void** memory);
std::int64_t APS5_VABI PixelFormatLookup_nid_postfix(JxrPixelInfo* value, std::uint8_t mode);
std::int64_t APS5_VABI readIS_nid_postfix(void* codec, Jxr::BitIo* io);
std::int64_t APS5_VABI writeIS_nid_postfix(void* codec, Jxr::BitIo* io);
std::int64_t APS5_VABI GetImageDecodeIID_nid_postfix(const char* extension, const std::uint32_t** iid);
std::int64_t APS5_VABI GetImageEncodeIID_nid_postfix(const char* extension, const std::uint32_t** iid);
const void* APS5_VABI GetPixelFormatFromHash_nid_postfix(std::uint8_t hash);
std::int32_t APS5_VABI IDPEmpty_nid_postfix(void* codec);
std::int64_t APS5_VABI PKAllocAligned_nid_postfix(void** memory, std::size_t size, std::size_t alignment);
std::int64_t APS5_VABI PKFreeAligned_nid_postfix(void** memory);
void APS5_VABI remapQP_nid_postfix(void* quantizer, std::int32_t shift, std::int32_t scaled);
void APS5_VABI formatQuantizer_nid_postfix(void** quantizers, std::uint8_t mode, std::size_t channels,
std::size_t position, std::int32_t shiftedUv, std::int32_t scaled);
Expand All @@ -24,14 +31,9 @@ std::int64_t APS5_VABI CreateWS_Memory_nid_postfix(Jxr::Stream** stream, void* b
int APS5_VABI EOSWS_File_nid_postfix();
std::int32_t APS5_VABI EOSWS_Memory_nid_postfix(Jxr::Stream* stream);
int APS5_VABI FreeDescMetadata_nid_postfix();
int APS5_VABI GetImageDecodeIID_nid_postfix();
int APS5_VABI GetImageEncodeIID_nid_postfix();
int APS5_VABI GetPixelFormatFromHash_nid_postfix();
int APS5_VABI GetPosWS_File_nid_postfix();
int APS5_VABI GetPosWS_List_nid_postfix();
std::int64_t APS5_VABI GetPosWS_Memory_nid_postfix(Jxr::Stream* stream, std::size_t* position);
int APS5_VABI IDPEmpty_nid_postfix();
int APS5_VABI PKAllocAligned_nid_postfix();
std::int64_t APS5_VABI PKAlloc_nid_postfix(void** memory, std::size_t size);
int APS5_VABI PKCodecFactory_CreateCodec_nid_postfix();
int APS5_VABI PKCodecFactory_CreateDecoderFromFile_nid_postfix();
Expand All @@ -51,7 +53,6 @@ int APS5_VABI PKFormatConverter_GetSourcePixelFormat_nid_postfix();
int APS5_VABI PKFormatConverter_InitializeConvert_nid_postfix();
int APS5_VABI PKFormatConverter_Initialize_nid_postfix();
int APS5_VABI PKFormatConverter_Release_nid_postfix();
int APS5_VABI PKFreeAligned_nid_postfix();
int APS5_VABI PKImageDecode_Copy_nid_postfix();
int APS5_VABI PKImageDecode_Create_WMP_nid_postfix();
int APS5_VABI PKImageDecode_Create_nid_postfix();
Expand Down
81 changes: 81 additions & 0 deletions core/libs/prx/libSceJxr/src/GlueHelpers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include "prx/libSceJxr/libSceJxr.hpp"
#include "prx/libc/include/Api.hpp"
#include <cstddef>
#include <cstdint>
#include <limits>
#include <stdexcept>
#include <string>

extern "C" {
const void* JxrHashLookup_nid_no_patch(std::uint8_t hash);
std::int64_t JxrEncodeIid_nid_no_patch(const char* extension, std::uint32_t* value);
std::int64_t JxrDecodeIid_nid_no_patch(const char* extension, std::uint32_t* value);
std::int64_t JxrAllocAligned_nid_no_patch(void** memory, std::size_t size, std::size_t alignment);
std::int64_t JxrFreeAligned_nid_no_patch(void** memory);
std::int32_t JxrIdpEmpty_nid_no_patch(void* codec);

void* JxrGuestCalloc_nid_no_patch(std::size_t count, std::size_t size) {
return calloc_nid_postfix(count, size);
}

void JxrGuestFree_nid_no_patch(void* memory) {
free_nid_postfix(memory);
}
}

namespace {

constexpr std::uint32_t ImageUnsupported = 100;

std::int64_t StoreIid(std::int64_t (*lookup)(const char*, std::uint32_t*), const char* extension,
const std::uint32_t** iid, const char* function) {
if (!extension) throw std::invalid_argument(std::string(function) + " requires an extension");
std::uint32_t value = 0;
const auto result = lookup(extension, &value);
if (result < 0) return result;
if (!iid) throw std::invalid_argument(std::string(function) + " requires an output for a known extension");
switch (value) {
case 101: *iid = &JxrGuest_IID_PKImageWmpEncode_nid_no_patch; break;
case 201: *iid = &JxrGuest_IID_PKImageWmpDecode_nid_no_patch; break;
case ImageUnsupported: *iid = &ImageUnsupported; break;
default: throw std::logic_error(std::string(function) + ": jxrlib returned an unknown IID " + std::to_string(value) + " for " + extension);
}
return result;
}

} // namespace

extern "C" {

const void* APS5_VABI GetPixelFormatFromHash_nid_postfix(std::uint8_t hash) {
return JxrHashLookup_nid_no_patch(hash);
}

std::int64_t APS5_VABI GetImageEncodeIID_nid_postfix(const char* extension, const std::uint32_t** iid) {
return StoreIid(JxrEncodeIid_nid_no_patch, extension, iid, "GetImageEncodeIID");
}

std::int64_t APS5_VABI GetImageDecodeIID_nid_postfix(const char* extension, const std::uint32_t** iid) {
return StoreIid(JxrDecodeIid_nid_no_patch, extension, iid, "GetImageDecodeIID");
}

std::int64_t APS5_VABI PKAllocAligned_nid_postfix(void** memory, std::size_t size, std::size_t alignment) {
if (!memory) throw std::invalid_argument("PKAllocAligned requires an output pointer");
*memory = nullptr;
if (alignment < sizeof(void*))
throw std::invalid_argument("PKAllocAligned: alignments below 8 can leave no room for the original pointer");
constexpr auto maximum = std::numeric_limits<std::size_t>::max();
if (size > maximum - sizeof(void*) || alignment - 1 > maximum - sizeof(void*) - size)
throw std::length_error("PKAllocAligned: block size overflows size_t");
return JxrAllocAligned_nid_no_patch(memory, size, alignment);
}

std::int64_t APS5_VABI PKFreeAligned_nid_postfix(void** memory) {
return JxrFreeAligned_nid_no_patch(memory);
}

std::int32_t APS5_VABI IDPEmpty_nid_postfix(void* codec) {
return JxrIdpEmpty_nid_no_patch(codec);
}

}
13 changes: 13 additions & 0 deletions core/libs/prx/libSceJxr/src/GuestHeap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef CORE_LIBS_PRX_LIBSCEJXR_SRC_GUESTHEAP_H
#define CORE_LIBS_PRX_LIBSCEJXR_SRC_GUESTHEAP_H

#include <stddef.h>
#include <stdlib.h>

void* JxrGuestCalloc_nid_no_patch(size_t count, size_t size);
void JxrGuestFree_nid_no_patch(void* memory);

#define calloc JxrGuestCalloc_nid_no_patch
#define free JxrGuestFree_nid_no_patch

#endif
36 changes: 36 additions & 0 deletions core/libs/prx/libSceJxr/src/NativeGlue.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "strcodec.h"
#include "JXRGlue.h"
#include <stdint.h>

const void* JxrHashLookup_nid_no_patch(U8 hash) {
return GetPixelFormatFromHash(hash);
}

typedef ERR (*IidLookup)(const char*, const PKIID**);

static int64_t StoreIid(IidLookup lookup, const char* extension, uint32_t* value) {
const PKIID* iid = NULL;
const ERR result = lookup(extension, &iid);
if (result >= 0 && iid) *value = *iid;
return result;
}

int64_t JxrEncodeIid_nid_no_patch(const char* extension, uint32_t* value) {
return StoreIid(GetImageEncodeIID, extension, value);
}

int64_t JxrDecodeIid_nid_no_patch(const char* extension, uint32_t* value) {
return StoreIid(GetImageDecodeIID, extension, value);
}

int64_t JxrAllocAligned_nid_no_patch(void** memory, size_t size, size_t alignment) {
return PKAllocAligned(memory, size, alignment);
}

int64_t JxrFreeAligned_nid_no_patch(void** memory) {
return PKFreeAligned(memory);
}

int32_t JxrIdpEmpty_nid_no_patch(void* codec) {
return IDPEmpty((CWMImageStrCodec*)codec);
}
Loading
Loading