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

Apple api add CVPixelBuffer、CMSampleBuffer、UIImage convenient #3800

Open
wants to merge 49 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
9e0385e
add convenient Mat from apple SampleBuffer
DaChengTechnology May 16, 2022
6e39611
[WIP]add to pixelbuffer
DaChengTechnology May 16, 2022
78aab5c
apply code-format changes
DaChengTechnology May 16, 2022
0d8ff9f
apple platform universal API complete
DaChengTechnology May 17, 2022
98696fa
Merge branch 'apple-api' of https://github.com/DaChengTechnology/ncnn…
DaChengTechnology May 17, 2022
1899509
add UIImage and fix build
DaChengTechnology May 17, 2022
837978e
fix char* to uint8_t*
DaChengTechnology May 17, 2022
d2a5bda
test TARGET_OS_IOS
DaChengTechnology May 18, 2022
40a4b7d
Update CMakeLists.txt
nihui May 19, 2022
8532ace
add test and fix bug
DaChengTechnology May 20, 2022
696dc48
fix test apple api
DaChengTechnology May 20, 2022
78965a8
fix malloc error
DaChengTechnology May 20, 2022
c5695bb
fix ncnn alloc
DaChengTechnology May 21, 2022
aa42613
apply code-format changes
DaChengTechnology May 21, 2022
651418c
fix posix_memalign err
DaChengTechnology May 21, 2022
4f682bc
Merge branch 'apple-api' of
DaChengTechnology May 21, 2022
5586cec
merge
DaChengTechnology May 21, 2022
a3e50f8
apply code-format changes
DaChengTechnology May 21, 2022
1166c54
add NCNN_MALLOC_OVERREAD
DaChengTechnology May 21, 2022
52926a9
stronger code
DaChengTechnology May 21, 2022
79e3d99
fix elemsize and fastFree
DaChengTechnology May 22, 2022
d4533f5
Update allocator.h
nihui May 22, 2022
0dcfd11
Update codeformat.sh
nihui May 22, 2022
6842f8b
Update CMakeLists.txt
nihui May 22, 2022
9a37156
Update CMakeLists.txt
nihui May 22, 2022
7132252
revert
nihui May 22, 2022
a9e2a6f
Update CMakeLists.txt
nihui May 22, 2022
a427a95
add oc codeformat
DaChengTechnology May 23, 2022
f3b6fdb
add Copyright
DaChengTechnology May 23, 2022
35abefd
add compare test
DaChengTechnology May 23, 2022
26e3a10
apply code-format changes
DaChengTechnology May 23, 2022
557a5df
fix intel Mac avx512 build err
DaChengTechnology May 25, 2022
8264673
Merge branch 'apple-api' of https://github.com/DaChengTechnology/ncnn…
DaChengTechnology May 25, 2022
c64b0fa
fix samplebuffer
DaChengTechnology May 31, 2022
557b962
fix code style
DaChengTechnology Jun 3, 2022
beb6be4
add NCNN_PLATFORM_API verdict
DaChengTechnology Jun 3, 2022
18dd99c
Update CMakeLists.txt
nihui Jun 6, 2022
1b58d5f
Update allocator.h
nihui Jun 6, 2022
9eb7c1b
wip
nihui Jun 6, 2022
3daa3e2
wip
nihui Jun 6, 2022
792b5f2
wip
nihui Jun 6, 2022
49caf0e
wip
nihui Jun 6, 2022
81e746f
wip
nihui Jun 6, 2022
c520230
wip
nihui Jun 6, 2022
b64e630
Merge remote-tracking branch 'upstream/master' into apple-api
nihui Jun 6, 2022
884f58d
apple nsimage test
nihui Jun 27, 2022
392f51d
Update test_apple_api.mm
nihui Jun 27, 2022
6c66b54
fix
nihui Jun 27, 2022
38bdb98
Update test_apple_api.mm
nihui Jun 27, 2022
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
12 changes: 6 additions & 6 deletions codeformat.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

# we run clang-format and astyle twice to get stable format output

find src/ tools/ tests/ examples/ benchmark/ python/ -type f -name '*.c' -o -name '*.cpp' -o -name '*.cc' -o -name '*.h' | grep -v python/pybind11 | grep -v stb_image | xargs -i clang-format -i {}
astyle -n -r "benchmark/*.h,*.cpp,*.cc" "tests/*.h,*.cpp,*.cc" "tools/*.h,*.cpp,*.cc" "examples/*.h,*.cpp,*.cc"
astyle -n -r "src/*.h,*.cpp,*.cc" --exclude=src/stb_image.h --exclude=src/stb_image_write.h
find src/ tools/ tests/ examples/ benchmark/ python/ -type f -name '*.c' -o -name '*.cpp' -o -name '*.cc' -o -name '*.mm' -o -name '*.h' | grep -v python/pybind11 | grep -v stb_image | xargs -i clang-format -i {}
astyle -n -r "benchmark/*.h,*.cpp,*.cc" "tests/*.h,*.cpp,*.cc,*.mm" "tools/*.h,*.cpp,*.cc" "examples/*.h,*.cpp,*.cc"
astyle -n -r "src/*.h,*.cpp,*.cc,*.mm" --exclude=src/stb_image.h --exclude=src/stb_image_write.h
astyle -n -r "python/*.h,*.cpp,*.cc" --exclude=python/pybind11

find src/ tools/ tests/ examples/ benchmark/ python/ -type f -name '*.c' -o -name '*.cpp' -o -name '*.cc' -o -name '*.h' | grep -v python/pybind11 | grep -v stb_image | xargs -i clang-format -i {}
astyle -n -r "benchmark/*.h,*.cpp,*.cc" "tests/*.h,*.cpp,*.cc" "tools/*.h,*.cpp,*.cc" "examples/*.h,*.cpp,*.cc"
astyle -n -r "src/*.h,*.cpp,*.cc" --exclude=src/stb_image.h --exclude=src/stb_image_write.h
find src/ tools/ tests/ examples/ benchmark/ python/ -type f -name '*.c' -o -name '*.cpp' -o -name '*.cc' -o -name '*.mm' -o -name '*.h' | grep -v python/pybind11 | grep -v stb_image | xargs -i clang-format -i {}
astyle -n -r "benchmark/*.h,*.cpp,*.cc" "tests/*.h,*.cpp,*.cc,*.mm" "tools/*.h,*.cpp,*.cc" "examples/*.h,*.cpp,*.cc"
astyle -n -r "src/*.h,*.cpp,*.cc,*.mm" --exclude=src/stb_image.h --exclude=src/stb_image_write.h
astyle -n -r "python/*.h,*.cpp,*.cc" --exclude=python/pybind11
18 changes: 16 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ if(ANDROID)
list(APPEND ncnn_SRCS mat_pixel_android.cpp)
endif()

if(APPLE)
enable_language(OBJCXX)
list(APPEND ncnn_SRCS mat_pixel_apple.mm)
endif()

ncnn_src_group(ncnn_SRCS "sources")

include_directories("${CMAKE_CURRENT_SOURCE_DIR}/layer/${NCNN_TARGET_ARCH}")
Expand Down Expand Up @@ -270,8 +275,17 @@ if(NCNN_VULKAN)
target_link_libraries(ncnn PRIVATE glslang SPIRV)
endif()

if(NCNN_PLATFORM_API AND ANDROID_NDK)
target_link_libraries(ncnn PUBLIC android jnigraphics log)
if(NCNN_PLATFORM_API)
if(ANDROID_NDK)
target_link_libraries(ncnn PUBLIC android jnigraphics log)
endif()
if(APPLE)
if(IOS)
target_link_libraries(ncnn PUBLIC "-framework Foundation" "-framework CoreMedia" "-framework CoreVideo" "-framework Accelerate" "-framework UIKit" "-framework CoreGraphics")
else()
target_link_libraries(ncnn PUBLIC "-framework Foundation" "-framework CoreMedia" "-framework CoreVideo" "-framework Accelerate" "-framework AppKit" "-framework CoreGraphics")
endif()
endif()
endif()

if(WIN32)
Expand Down
7 changes: 5 additions & 2 deletions src/allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
#if __ANDROID_API__ >= 26
#include <android/hardware_buffer.h>
#endif // __ANDROID_API__ >= 26
#if __APPLE__
#include <errno.h>
#endif // __APPLE__
#endif // NCNN_PLATFORM_API

namespace ncnn {
Expand Down Expand Up @@ -72,7 +75,7 @@ static NCNN_FORCEINLINE void* fastMalloc(size_t size)
{
#if _MSC_VER
return _aligned_malloc(size, NCNN_MALLOC_ALIGN);
#elif (defined(__unix__) || defined(__APPLE__)) && _POSIX_C_SOURCE >= 200112L || (__ANDROID__ && __ANDROID_API__ >= 17)
#elif (defined(__unix__) && _POSIX_C_SOURCE >= 200112L) || defined(__APPLE__) || (__ANDROID__ && __ANDROID_API__ >= 17)
void* ptr = 0;
if (posix_memalign(&ptr, NCNN_MALLOC_ALIGN, size + NCNN_MALLOC_OVERREAD))
ptr = 0;
Expand All @@ -95,7 +98,7 @@ static NCNN_FORCEINLINE void fastFree(void* ptr)
{
#if _MSC_VER
_aligned_free(ptr);
#elif (defined(__unix__) || defined(__APPLE__)) && _POSIX_C_SOURCE >= 200112L || (__ANDROID__ && __ANDROID_API__ >= 17)
#elif (defined(__unix__) && _POSIX_C_SOURCE >= 200112L) || defined(__APPLE__) || (__ANDROID__ && __ANDROID_API__ >= 17)
free(ptr);
#elif __ANDROID__ && __ANDROID_API__ < 17
free(ptr);
Expand Down
33 changes: 33 additions & 0 deletions src/mat.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@
#include <android/bitmap.h>
#include <jni.h>
#endif // __ANDROID_API__ >= 9
#ifdef __OBJC__
#import <Foundation/Foundation.h>
#if __APPLE__
#if TARGET_OS_IOS
#import <UIKit/UIKit.h>
#else
#import <AppKit/Appkit.h>
#endif // TARGET_OS_IOS
#import <CoreMedia/CoreMedia.h>
#import <CoreVideo/CoreVideo.h>
#endif // __APPLE__
#endif
#endif // NCNN_PLATFORM_API
#endif // NCNN_PIXEL

Expand Down Expand Up @@ -301,6 +313,27 @@ class NCNN_EXPORT Mat
// convenient export to android Bitmap and resize to the android Bitmap size
void to_android_bitmap(JNIEnv* env, jobject bitmap, int type_from) const;
#endif // __ANDROID_API__ >= 9
#ifdef __OBJC__
#if __APPLE__
// convenient construct from apple SampleBuffer, mat is RGBA
static Mat from_apple_samplebuffer(CMSampleBufferRef samplebuffer);
// convenient construct from apple PixelBuffer, mat is RGBA
static Mat from_apple_pixelbuffer(CVPixelBufferRef pixelbuffer);
// convenient export to apple PixelBuffer
int to_apple_pixelbuffer(CVPixelBufferRef* pixelbuffer);
#if TARGET_OS_IOS
// convenient construct from apple UIImage
static Mat from_apple_image(UIImage* image);
// convenient export to apple UIImage
UIImage* to_apple_image();
#else
// convenient construct from apple NSImage
static Mat from_apple_image(NSImage* image);
// convenient export to apple NSImage
NSImage* to_apple_image();
#endif // TARGET_OS_IOS
#endif // __APPLE__
#endif
#endif // NCNN_PLATFORM_API
#endif // NCNN_PIXEL

Expand Down
Loading