Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc037344

Browse files
authored
Merge pull request#28159 from asmorkalov:as/java_cleaners
Introduce option to generate Java code with finalize() or Cleaners interface#28159Closes#22260Replaces#23467The PR introduce configuration option to generate Java code with Cleaner interface for Java 9+ and old-fashion finalize() method for old Java and Android. Mat class and derivatives are manually written. The PR introduce 2 base classes for it depending on the generator configuration.Pros:1. No need to implement complex and error prone cleaner on library side.2. No new CMake templates, easier to modify code in IDE.Cons:1. More generator branches and different code for modern desktop and Android.TODO: - [x] Add Java version check to cmake- [x] Use Cleaners for ANDROID API 33+### Pull Request Readiness ChecklistSee details athttps://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request- [x] I agree to contribute to the project under Apache 2 License.- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV- [ ] The PR is proposed to the proper branch- [ ] There is a reference to the original bug report and related work- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name.- [ ] The feature is well documented and sample code can be built with the project CMake
1 parent7ca9d9c commitc037344

File tree

10 files changed

+145
-57
lines changed

10 files changed

+145
-57
lines changed

‎CMakeLists.txt‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -834,12 +834,12 @@ if(BUILD_JAVA)
834834
if(ANDROID)
835835
include(cmake/android/OpenCVDetectAndroidSDK.cmake)
836836
else()
837-
include(cmake/OpenCVDetectApacheAnt.cmake)
838-
if(ANT_EXECUTABLEANDNOT OPENCV_JAVA_IGNORE_ANT)
839-
ocv_update(OPENCV_JAVA_SDK_BUILD_TYPE"ANT")
840-
elseif(NOTANDROID)
841-
find_package(Java)
842-
if(Java_FOUND)
837+
find_package(JavaQUIET)
838+
if(Java_FOUND)
839+
include(cmake/OpenCVDetectApacheAnt.cmake)
840+
if(ANT_EXECUTABLEANDNOTOPENCV_JAVA_IGNORE_ANT)
841+
ocv_update(OPENCV_JAVA_SDK_BUILD_TYPE"ANT")
842+
else()
843843
include(UseJava)
844844
ocv_update(OPENCV_JAVA_SDK_BUILD_TYPE"JAVA")
845845
endif()
@@ -1997,7 +1997,7 @@ if(BUILD_JAVA)
19971997
status(" Java:" Java_FOUND THEN"YES (ver${Java_VERSION})" ELSENO)
19981998
status(" JNI:" JNI_INCLUDE_DIRS THEN"${JNI_INCLUDE_DIRS}" ELSENO)
19991999
endif()
2000-
status(" Java wrappers:" HAVE_opencv_javaTHEN"YES (${OPENCV_JAVA_SDK_BUILD_TYPE})" ELSENO)
2000+
status(" Java wrappers:" HAVE_opencv_java THEN"YES (${OPENCV_JAVA_SDK_BUILD_TYPE})" ELSENO)
20012001
status(" Java tests:" BUILD_TESTSAND (opencv_test_java_BINARY_DIRORopencv_test_android_BINARY_DIR) THENYES ELSENO)
20022002
endif()
20032003

‎cmake/android/OpenCVDetectAndroidSDK.cmake‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,10 @@ else()
220220
endif()# BUILD_ANDROID_PROJECTS
221221

222222
if(ANDROID_PROJECTS_BUILD_TYPESTREQUAL"ANT")
223+
ocv_update(OPENCV_JAVA_SDK_BUILD_TYPE"ANT")
223224
include(${CMAKE_CURRENT_LIST_DIR}/android_ant_projects.cmake)
224225
elseif(ANDROID_PROJECTS_BUILD_TYPESTREQUAL"GRADLE")
226+
ocv_update(OPENCV_JAVA_SDK_BUILD_TYPE"GRADLE")
225227
include(${CMAKE_CURRENT_LIST_DIR}/android_gradle_projects.cmake)
226228
elseif(BUILD_ANDROID_PROJECTS)
227229
message(FATAL_ERROR"Internal error")

‎modules/core/misc/java/src/java/core+Mat.java‎

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,10 @@
44

55
// C++: class Mat
66
//javadoc: Mat
7-
publicclassMat {
8-
9-
publicfinallongnativeObj;
7+
publicclassMatextendsCleanableMat {
108

119
publicMat(longaddr) {
12-
if (addr ==0)
13-
thrownewUnsupportedOperationException("Native object address is NULL");
14-
nativeObj =addr;
10+
super(addr);
1511
}
1612

1713
//
@@ -20,7 +16,7 @@ public Mat(long addr) {
2016

2117
// javadoc: Mat::Mat()
2218
publicMat() {
23-
nativeObj =n_Mat();
19+
super(n_Mat());
2420
}
2521

2622
//
@@ -29,7 +25,7 @@ public Mat() {
2925

3026
// javadoc: Mat::Mat(rows, cols, type)
3127
publicMat(introws,intcols,inttype) {
32-
nativeObj =n_Mat(rows,cols,type);
28+
super(n_Mat(rows,cols,type));
3329
}
3430

3531
//
@@ -38,7 +34,7 @@ public Mat(int rows, int cols, int type) {
3834

3935
// javadoc: Mat::Mat(rows, cols, type, data)
4036
publicMat(introws,intcols,inttype,ByteBufferdata) {
41-
nativeObj =n_Mat(rows,cols,type,data);
37+
super(n_Mat(rows,cols,type,data));
4238
}
4339

4440
//
@@ -47,7 +43,7 @@ public Mat(int rows, int cols, int type, ByteBuffer data) {
4743

4844
// javadoc: Mat::Mat(rows, cols, type, data, step)
4945
publicMat(introws,intcols,inttype,ByteBufferdata,longstep) {
50-
nativeObj =n_Mat(rows,cols,type,data,step);
46+
super(n_Mat(rows,cols,type,data,step));
5147
}
5248

5349
//
@@ -56,7 +52,7 @@ public Mat(int rows, int cols, int type, ByteBuffer data, long step) {
5652

5753
// javadoc: Mat::Mat(size, type)
5854
publicMat(Sizesize,inttype) {
59-
nativeObj =n_Mat(size.width,size.height,type);
55+
super(n_Mat(size.width,size.height,type));
6056
}
6157

6258
//
@@ -65,7 +61,7 @@ public Mat(Size size, int type) {
6561

6662
// javadoc: Mat::Mat(sizes, type)
6763
publicMat(int[]sizes,inttype) {
68-
nativeObj =n_Mat(sizes.length,sizes,type);
64+
super(n_Mat(sizes.length,sizes,type));
6965
}
7066

7167
//
@@ -74,7 +70,7 @@ public Mat(int[] sizes, int type) {
7470

7571
// javadoc: Mat::Mat(rows, cols, type, s)
7672
publicMat(introws,intcols,inttype,Scalars) {
77-
nativeObj =n_Mat(rows,cols,type,s.val[0],s.val[1],s.val[2],s.val[3]);
73+
super(n_Mat(rows,cols,type,s.val[0],s.val[1],s.val[2],s.val[3]));
7874
}
7975

8076
//
@@ -83,7 +79,7 @@ public Mat(int rows, int cols, int type, Scalar s) {
8379

8480
// javadoc: Mat::Mat(size, type, s)
8581
publicMat(Sizesize,inttype,Scalars) {
86-
nativeObj =n_Mat(size.width,size.height,type,s.val[0],s.val[1],s.val[2],s.val[3]);
82+
super(n_Mat(size.width,size.height,type,s.val[0],s.val[1],s.val[2],s.val[3]));
8783
}
8884

8985
//
@@ -92,7 +88,7 @@ public Mat(Size size, int type, Scalar s) {
9288

9389
// javadoc: Mat::Mat(sizes, type, s)
9490
publicMat(int[]sizes,inttype,Scalars) {
95-
nativeObj =n_Mat(sizes.length,sizes,type,s.val[0],s.val[1],s.val[2],s.val[3]);
91+
super(n_Mat(sizes.length,sizes,type,s.val[0],s.val[1],s.val[2],s.val[3]));
9692
}
9793

9894
//
@@ -101,12 +97,12 @@ public Mat(int[] sizes, int type, Scalar s) {
10197

10298
// javadoc: Mat::Mat(m, rowRange, colRange)
10399
publicMat(Matm,RangerowRange,RangecolRange) {
104-
nativeObj =n_Mat(m.nativeObj,rowRange.start,rowRange.end,colRange.start,colRange.end);
100+
super(n_Mat(m.nativeObj,rowRange.start,rowRange.end,colRange.start,colRange.end));
105101
}
106102

107103
// javadoc: Mat::Mat(m, rowRange)
108104
publicMat(Matm,RangerowRange) {
109-
nativeObj =n_Mat(m.nativeObj,rowRange.start,rowRange.end);
105+
super(n_Mat(m.nativeObj,rowRange.start,rowRange.end));
110106
}
111107

112108
//
@@ -115,7 +111,7 @@ public Mat(Mat m, Range rowRange) {
115111

116112
// javadoc: Mat::Mat(m, ranges)
117113
publicMat(Matm,Range[]ranges) {
118-
nativeObj =n_Mat(m.nativeObj,ranges);
114+
super(n_Mat(m.nativeObj,ranges));
119115
}
120116

121117
//
@@ -124,7 +120,7 @@ public Mat(Mat m, Range[] ranges) {
124120

125121
// javadoc: Mat::Mat(m, roi)
126122
publicMat(Matm,Rectroi) {
127-
nativeObj =n_Mat(m.nativeObj,roi.y,roi.y +roi.height,roi.x,roi.x +roi.width);
123+
super(n_Mat(m.nativeObj,roi.y,roi.y +roi.height,roi.x,roi.x +roi.width));
128124
}
129125

130126
//
@@ -754,12 +750,6 @@ public static Mat zeros(int[] sizes, int type) {
754750
returnnewMat(n_zeros(sizes.length,sizes,type));
755751
}
756752

757-
@Override
758-
protectedvoidfinalize()throwsThrowable {
759-
n_delete(nativeObj);
760-
super.finalize();
761-
}
762-
763753
// javadoc:Mat::toString()
764754
@Override
765755
publicStringtoString() {
@@ -1834,9 +1824,6 @@ public long getNativeObjAddr() {
18341824
// C++: static Mat Mat::zeros(int ndims, const int* sizes, int type)
18351825
privatestaticnativelongn_zeros(intndims,int[]sizes,inttype);
18361826

1837-
// native support for java finalize()
1838-
privatestaticnativevoidn_delete(longnativeObj);
1839-
18401827
privatestaticnativeintnPutD(longself,introw,intcol,intcount,double[]data);
18411828

18421829
privatestaticnativeintnPutDIdx(longself,int[]idx,intcount,double[]data);

‎modules/java/generator/CMakeLists.txt‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,24 @@ ocv_bindings_generator_populate_preprocessor_definitions(
6262
opencv_preprocessor_defs
6363
)
6464

65+
if(OPENCV_JAVA_CLEANING_API)
66+
if(OPENCV_JAVA_CLEANING_APISTREQUAL"finalize")
67+
set(opencv_supported_cleaners"false")
68+
elseif(OPENCV_JAVA_CLEANING_APISTREQUAL"cleaner")
69+
set(opencv_supported_cleaners"true")
70+
else()
71+
message(FATAL_ERROR"OPENCV_JAVA_CLEANING_API should be one of\"finalize\" or\"cleaner\"")
72+
endif()
73+
else()
74+
if(ANDROIDOR (Java_VERSIONVERSION_LESS 9))
75+
message(STATUS"Set Cleaners to False")
76+
set(opencv_supported_cleaners"false")
77+
else()
78+
message(STATUS"Set Cleaners to True")
79+
set(opencv_supported_cleaners"true")
80+
endif()
81+
endif(OPENCV_JAVA_CLEANING_API)
82+
6583
set(CONFIG_FILE"${CMAKE_CURRENT_BINARY_DIR}/gen_java.json")
6684
set(__config_str
6785
"{
@@ -74,7 +92,8 @@ ${opencv_preprocessor_defs}
7492
},
7593
\"files_remap\": [
7694
${__remap_config}
77-
]
95+
],
96+
\"support_cleaners\":${opencv_supported_cleaners}
7897
}
7998
")
8099
if(EXISTS"${CONFIG_FILE}")

‎modules/java/generator/gen_java.py‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def write(self, s):
2323
# list of modules + files remap
2424
config=None
2525
ROOT_DIR=None
26+
USE_CLEANERS=True
2627
FILES_REMAP= {}
2728
defcheckFileRemap(path):
2829
path=os.path.realpath(path)
@@ -366,6 +367,7 @@ def generateJavaCode(self, m, M):
366367
module=m,
367368
name=self.name,
368369
jname=self.jname,
370+
jcleaner="long nativeObjCopy = nativeObj;\n org.opencv.core.Mat.cleaner.register(this, () -> delete(nativeObjCopy));"ifUSE_CLEANERSelse"",
369371
imports="\n".join(self.getAllImports(M)),
370372
docs=self.docstring,
371373
annotation="\n"+"\n".join(self.annotation)ifself.annotationelse"",
@@ -948,6 +950,7 @@ def gen_func(self, ci, fi, prop_name=''):
948950
tail=")"
949951
else:
950952
ret_val="nativeObj = "
953+
tail=";\n long nativeObjCopy = nativeObj;\n org.opencv.core.Mat.cleaner.register(this, () -> delete(nativeObjCopy))"ifUSE_CLEANERSelse""
951954
ret=""
952955
elifself.isWrapped(ret_type):# wrapped class
953956
constructor=self.getClass(ret_type).jname+"("
@@ -1214,8 +1217,9 @@ def const_value(v):
12141217
ci.cpp_code.write("\n".join(fn["cpp_code"]))
12151218

12161219
ifci.name!=self.Moduleorci.base:
1217-
# finalize()
1218-
ci.j_code.write(
1220+
# finalize() for old Java
1221+
ifnotUSE_CLEANERS:
1222+
ci.j_code.write(
12191223
"""
12201224
@Override
12211225
protected void finalize() throws Throwable {
@@ -1225,15 +1229,15 @@ def const_value(v):
12251229

12261230
ci.jn_code.write(
12271231
"""
1228-
// native support for java finalize()
1232+
// native support for java finalize() or cleaner
12291233
private static native void delete(long nativeObj);
12301234
""" )
12311235

12321236
# native support for java finalize()
12331237
ci.cpp_code.write(
12341238
"""
12351239
//
1236-
// native support for java finalize()
1240+
// native support for java finalize() or cleaner
12371241
// static void %(cls)s::delete( __int64 self )
12381242
//
12391243
JNIEXPORT void JNICALL Java_org_opencv_%(module)s_%(j_cls)s_delete(JNIEnv*, jclass, jlong);
@@ -1454,6 +1458,12 @@ def sanitize_java_documentation_string(doc, type):
14541458
FILES_REMAP= {os.path.realpath(os.path.join(ROOT_DIR,f['src'])):f['target']forfinconfig['files_remap'] }
14551459
logging.info("\nRemapped configured files (%d):\n%s",len(FILES_REMAP),pformat(FILES_REMAP))
14561460

1461+
USE_CLEANERS=config['support_cleaners']
1462+
if (USE_CLEANERS):
1463+
logging.info("\nUse Java 9+ cleaners\n")
1464+
else:
1465+
logging.info("\nUse old style Java finalize()\n")
1466+
14571467
dstdir="./gen"
14581468
jni_path=os.path.join(dstdir,'cpp');mkdir_p(jni_path)
14591469
java_base_path=os.path.join(dstdir,'java');mkdir_p(java_base_path)
@@ -1543,6 +1553,17 @@ def sanitize_java_documentation_string(doc, type):
15431553
preprocessor_definitions)
15441554
else:
15451555
logging.info("No generated code for module: %s",module)
1556+
1557+
# Copy Cleaner / finalize() related files
1558+
ifUSE_CLEANERS:
1559+
cleaner_src=os.path.join(SCRIPT_DIR,"src","java9","CleanableMat.java")
1560+
else:
1561+
cleaner_src=os.path.join(SCRIPT_DIR,"src","java_classic","CleanableMat.java")
1562+
1563+
cleaner_dst=os.path.join(java_base_path,"org","opencv","core","CleanableMat.java")
1564+
print("cleaner_dst: ",cleaner_dst)
1565+
copyfile(cleaner_src,cleaner_dst)
1566+
15461567
generator.finalize(jni_path)
15471568

15481569
print('Generated files: %d (updated %d)'% (total_files,updated_files))
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// This file is part of OpenCV project.
2+
// It is subject to the license terms in the LICENSE file found in the top-level directory
3+
// of this distribution and at http://opencv.org/license.html
4+
5+
#include"opencv2/core.hpp"
6+
7+
#defineLOG_TAG"org.opencv.core.CleanableMat"
8+
#include"common.h"
9+
#include<iostream>
10+
11+
usingnamespacecv;
12+
13+
extern"C" {
14+
//
15+
// native support for java finalize() or cleaners
16+
// static void CleanableMat::n_delete( __int64 self )
17+
//
18+
19+
JNIEXPORTvoid JNICALL Java_org_opencv_core_CleanableMat_n_1delete
20+
(JNIEnv*, jclass, jlong self);
21+
22+
JNIEXPORTvoid JNICALL Java_org_opencv_core_CleanableMat_n_1delete
23+
(JNIEnv*, jclass, jlong self)
24+
{
25+
// LOGD("CleanableMat.n_delete() called\n");
26+
delete (Mat*) self;
27+
}
28+
}

‎modules/java/generator/src/cpp/Mat.cpp‎

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2114,22 +2114,6 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1zeros__I_3II
21142114
return0;
21152115
}
21162116

2117-
2118-
2119-
//
2120-
// native support for java finalize()
2121-
// static void Mat::n_delete( __int64 self )
2122-
//
2123-
2124-
JNIEXPORTvoid JNICALL Java_org_opencv_core_Mat_n_1delete
2125-
(JNIEnv*, jclass, jlong self);
2126-
2127-
JNIEXPORTvoid JNICALL Java_org_opencv_core_Mat_n_1delete
2128-
(JNIEnv*, jclass, jlong self)
2129-
{
2130-
delete (Mat*) self;
2131-
}
2132-
21332117
}// extern "C"
21342118

21352119
namespace {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
packageorg.opencv.core;
2+
3+
importjava.lang.ref.Cleaner;
4+
5+
publicabstractclassCleanableMat {
6+
// A native memory cleaner for the OpenCV library
7+
publicstaticCleanercleaner =Cleaner.create();
8+
9+
protectedCleanableMat(longobj) {
10+
if (obj ==0)
11+
thrownewUnsupportedOperationException("Native object address is NULL");
12+
13+
nativeObj =obj;
14+
15+
// The n_delete action must not refer to the object being registered. So, do not use nativeObj directly.
16+
longnativeObjCopy =nativeObj;
17+
cleaner.register(this, () ->n_delete(nativeObjCopy));
18+
}
19+
20+
privatestaticnativevoidn_delete(longnativeObj);
21+
22+
publicfinallongnativeObj;
23+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp