diff --git a/modules/gapi/src/executor/gexecutor.cpp b/modules/gapi/src/executor/gexecutor.cpp index 2594cde444..ede38e8e99 100644 --- a/modules/gapi/src/executor/gexecutor.cpp +++ b/modules/gapi/src/executor/gexecutor.cpp @@ -152,17 +152,31 @@ void cv::gimpl::GExecutor::run(cv::gimpl::GRuntimeArgs &&args) { using cv::util::get; const auto desc = get(d.meta); + + auto check_own_mat = [&desc, &args, &index]() + { + auto& out_mat = *get(args.outObjs.at(index)); + GAPI_Assert(out_mat.data != nullptr && + desc.canDescribe(out_mat)); + }; + #if !defined(GAPI_STANDALONE) // Building as part of OpenCV - follow OpenCV behavior - // if output buffer is not enough to hold the result, reallocate it - auto& out_mat = *get(args.outObjs.at(index)); - createMat(desc, out_mat); + // In the case of cv::Mat if output buffer is not enough to hold the result, reallocate it + if (cv::util::holds_alternative(args.outObjs.at(index))) + { + auto& out_mat = *get(args.outObjs.at(index)); + createMat(desc, out_mat); + } + // In the case of own::Mat never reallocated, checked to perfectly fit required meta + else + { + check_own_mat(); + } #else // Building standalone - output buffer should always exist, // and _exact_ match our inferred metadata - auto& out_mat = *get(args.outObjs.at(index)); - GAPI_Assert(out_mat.data != nullptr && - desc.canDescribe(out_mat)) + check_own_mat(); #endif // !defined(GAPI_STANDALONE) } } diff --git a/modules/gapi/test/gapi_sample_pipelines.cpp b/modules/gapi/test/gapi_sample_pipelines.cpp index 815aa0d872..0bfb4f6548 100644 --- a/modules/gapi/test/gapi_sample_pipelines.cpp +++ b/modules/gapi/test/gapi_sample_pipelines.cpp @@ -298,4 +298,20 @@ TEST(GAPI_Pipeline, PipelineAllocatingKernel) EXPECT_THROW(comp.apply(in_mat, out_mat, cv::compile_args(pkg)), std::logic_error); } + +TEST(GAPI_Pipeline, CanUseOwnMatAsOutput) +{ + cv::GMat in; + cv::GComputation comp(in, cv::gapi::bitwise_not(in)); + + cv::Mat in_mat(3, 3, CV_8UC1); + cv::Mat out_mat(3, 3, CV_8UC1); + + cv::gapi::own::Mat in_own_mat(in_mat.rows, in_mat.cols, CV_8UC1, in_mat.data); + cv::gapi::own::Mat out_own_mat(out_mat.rows, out_mat.cols, CV_8UC1, out_mat.data); + + // FIXME add overload for apply(cv::gapi::own::Mat in, cv::gapi::own::Mat& out) + EXPECT_NO_THROW(comp.apply({in_own_mat}, {out_own_mat})); +} + } // namespace opencv_test