Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial C API for FFI #663

Closed
wants to merge 5 commits into from
Closed
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
80 changes: 78 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,11 @@ list(
"${draco_src_root}/javascript/emscripten/draco_animation_encoder_glue_wrapper.cc"
)

list(
APPEND draco_c_api_dec_sources
"${draco_src_root}/c_api/c_api_decoder.cc"
)

list(APPEND draco_unity_plug_sources
"${draco_src_root}/unity/draco_unity_plugin.cc"
"${draco_src_root}/unity/draco_unity_plugin.h")
Expand Down Expand Up @@ -773,6 +778,16 @@ else()
${draco_defines}
INCLUDES
${draco_include_paths})
draco_add_library(NAME
draco_c_api_dec
TYPE
OBJECT
SOURCES
${draco_c_api_dec_sources}
DEFINES
${draco_defines}
INCLUDES
${draco_include_paths})

set(draco_object_library_deps
draco_attributes
Expand Down Expand Up @@ -803,6 +818,10 @@ else()
draco_points_dec
draco_points_enc)

list(APPEND draco_c_api_object_library_deps
${draco_object_library_deps}
draco_c_api_dec)

# Library targets that consume the object collections.
if(MSVC OR WIN32)
# In order to produce a DLL and import library the Windows tools require
Expand All @@ -829,7 +848,28 @@ else()
${draco_include_paths}
OBJLIB_DEPS
${draco_object_library_deps})

if(BUILD_SHARED_LIBS)
set_target_properties(draco PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS TRUE)
endif()
if(DRACO_C_API)
draco_add_library(NAME
cdraco
OUTPUT_NAME
cdraco
TYPE
${draco_lib_type}
DEFINES
${draco_defines}
INCLUDES
${draco_include_paths}
PUBLIC_INCLUDES
${draco_c_public_api_include_paths}
OBJLIB_DEPS
${draco_c_api_object_library_deps})
if(BUILD_SHARED_LIBS)
set_target_properties(cdraco PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS FALSE)
endif()
endif()
else()
draco_add_library(NAME
draco_static
Expand All @@ -843,7 +883,22 @@ else()
${draco_include_paths}
OBJLIB_DEPS
${draco_object_library_deps})

if(DRACO_C_API)
draco_add_library(NAME
cdraco_static
OUTPUT_NAME
cdraco
TYPE
STATIC
DEFINES
${draco_defines}
INCLUDES
${draco_include_paths}
PUBLIC_INCLUDES
${draco_c_public_api_include_paths}
OBJLIB_DEPS
${draco_c_api_object_library_deps})
endif()
if(BUILD_SHARED_LIBS)
draco_add_library(NAME
draco_shared
Expand All @@ -859,6 +914,27 @@ else()
${draco_include_paths}
LIB_DEPS
draco_static)
if(DRACO_C_API)
draco_add_library(NAME
cdraco_shared
SOURCES
"${draco_src_root}/core/draco_version.h"
OUTPUT_NAME
cdraco
TYPE
SHARED
DEFINES
${draco_defines}
INCLUDES
${draco_include_paths}
PUBLIC_INCLUDES
${draco_c_public_api_include_paths}
OBJLIB_DEPS
cdraco_static)
set_target_properties(cdraco_static PROPERTIES C_VISIBILITY_PRESET hidden)
set_target_properties(cdraco_static PROPERTIES CXX_VISIBILITY_PRESET hidden)
set_target_properties(cdraco_static PROPERTIES VISIBILITY_INLINES_HIDDEN ON)
endif()
endif()
endif()

Expand Down
6 changes: 2 additions & 4 deletions cmake/draco_build_definitions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ macro(draco_set_build_definitions)
list(APPEND draco_include_paths "${draco_root}" "${draco_root}/src"
"${draco_build}")

list(APPEND draco_c_public_api_include_paths "${draco_root}/src/draco/c_api")

if(DRACO_ABSL)
list(APPEND draco_include_path "${draco_root}/third_party/abseil-cpp")
endif()
Expand All @@ -59,10 +61,6 @@ macro(draco_set_build_definitions)

if(MSVC OR WIN32)
list(APPEND draco_defines "_CRT_SECURE_NO_DEPRECATE=1" "NOMINMAX=1")

if(BUILD_SHARED_LIBS)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS TRUE)
endif()
endif()

if(ANDROID)
Expand Down
6 changes: 6 additions & 0 deletions cmake/draco_options.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ macro(draco_set_default_options)
"Enable attribute deduping." VALUE OFF)
draco_option(NAME DRACO_TESTS HELPSTRING "Enables tests." VALUE OFF)
draco_option(NAME DRACO_WASM HELPSTRING "Enables WASM support." VALUE OFF)
draco_option(NAME DRACO_C_API HELPSTRING
"Build C wrapper." VALUE OFF)
draco_option(NAME DRACO_UNITY_PLUGIN HELPSTRING
"Build plugin library for Unity." VALUE OFF)
draco_option(NAME DRACO_ANIMATION_ENCODING HELPSTRING "Enable animation."
Expand Down Expand Up @@ -170,6 +172,10 @@ macro(draco_set_optional_features)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
endif()

if(DRACO_C_API)
draco_enable_feature(FEATURE "DRACO_C_API_SUPPORTED")
endif()

endmacro()

# Macro that handles tracking of Draco preprocessor symbols for the purpose of
Expand Down
20 changes: 13 additions & 7 deletions cmake/draco_tests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,17 @@ list(
"${draco_src_root}/metadata/metadata_encoder_test.cc"
"${draco_src_root}/metadata/metadata_test.cc"
"${draco_src_root}/point_cloud/point_cloud_builder_test.cc"
"${draco_src_root}/point_cloud/point_cloud_test.cc")
"${draco_src_root}/point_cloud/point_cloud_test.cc"
"${draco_src_root}/c_api/c_api_test.cc")

list(APPEND draco_test_lib_deps
draco_static
draco_gtest
draco_gtest_main)

if(DRACO_C_API)
list(APPEND draco_test_lib_deps cdraco_static)
endif()

list(APPEND draco_gtest_all
"${draco_root}/../googletest/googletest/src/gtest-all.cc")
Expand Down Expand Up @@ -112,9 +122,7 @@ macro(draco_setup_test_targets)
INCLUDES
${draco_test_include_paths}
LIB_DEPS
draco_static
draco_gtest
draco_gtest_main)
${draco_test_lib_deps})

draco_add_executable(NAME
draco_factory_tests
Expand All @@ -126,8 +134,6 @@ macro(draco_setup_test_targets)
INCLUDES
${draco_test_include_paths}
LIB_DEPS
draco_static
draco_gtest
draco_gtest_main)
${draco_test_lib_deps})
endif()
endmacro()
101 changes: 101 additions & 0 deletions src/draco/c_api/c_api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright 2020 The Draco Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#ifndef DRACO_C_API_H_
#define DRACO_C_API_H_

#include <stdint.h>
#include <stdbool.h>

#ifdef __cplusplus
extern "C" {
#endif

#ifdef _WIN32
# if defined(DRACO_BUILDING_DLL)
# define EXPORT_API __declspec(dllexport)
# elif !defined(DRACO_STATIC)
# define EXPORT_API __declspec(dllimport)
# else
# define EXPORT_API
# endif
#elif __GNUC__ >= 4 || defined(__clang__)
# define EXPORT_API __attribute__((visibility ("default")))
#else
#define EXPORT_API
#endif // defined(_WIN32)

typedef const char* draco_string; // NULL terminated

// draco::Status

typedef struct draco_status draco_status;

EXPORT_API void dracoStatusRelease(draco_status *status);

EXPORT_API int dracoStatusCode(const draco_status *status);

EXPORT_API bool dracoStatusOk(const draco_status *status);

// Returns the status message.
// The memory backing memory is valid meanwhile status is not released.
EXPORT_API draco_string dracoStatusErrorMsg(const draco_status *status);

// draco::Mesh

typedef uint32_t draco_face[3];
typedef struct draco_mesh draco_mesh;

EXPORT_API draco_mesh* dracoNewMesh();

EXPORT_API void dracoMeshRelease(draco_mesh *mesh);

EXPORT_API uint32_t dracoMeshNumFaces(const draco_mesh *mesh);

EXPORT_API uint32_t dracoMeshNumPoints(const draco_mesh *mesh);

// Queries an array of 3*face_count elements containing the triangle indices.
// out_values must be allocated to contain at least 3*face_count uint16_t elements.
// out_size must be exactly 3*face_count*sizeof(uint16_t), else out_values
// won´t be filled and returns false.
EXPORT_API bool dracoMeshGetTrianglesUint16(const draco_mesh *mesh,
const size_t out_size,
uint16_t *out_values);

// Queries an array of 3*face_count elements containing the triangle indices.
// out_values must be allocated to contain at least 3*face_count uint32_t elements.
// out_size must be exactly 3*face_count*sizeof(uint32_t), else out_values
// won´t be filled and returns false.
EXPORT_API bool dracoMeshGetTrianglesUint32(const draco_mesh *mesh,
const size_t out_size,
uint32_t *out_values);

// draco::Decoder

typedef struct draco_decoder draco_decoder;

EXPORT_API draco_decoder* dracoNewDecoder();

EXPORT_API void dracoDecoderRelease(draco_decoder *decoder);

EXPORT_API draco_status* dracoDecoderArrayToMesh(draco_decoder *decoder,
const char *data,
size_t data_size,
draco_mesh *out_mesh);

#ifdef __cplusplus
}
#endif

#endif // DRACO_C_API_H_
Loading