Repository: opencv
Issue: G-API: gapi::own::Mat does not fully support ROI cv::Mat
Labels: category: g-api / gapi

##### System information (version)
- OpenCV => 4.1.2
- Operating System / Platform => in theory, any with OpenCL support. reproduced on `Ubuntu 18.04 LTS 64-bit (Linux kernel 5.3.0-40-generic)`

##### Detailed description

Currently there is an issue with how `own::Mat` handles external data provided by `cv::Mat`. Particularly, in case of OCL backend we lose track of original data source when doing `own::Mat -> cv::Mat -> cv::UMat` and this results in the creation of new memory not "connected" to original `cv::Mat`.

At the OCL backend level, this ends up being a failure at checking that populated UMat data equals original cv::Mat data for output data nodes during `writeBack` procedure (according to assert message inside OCL backend during kernel execution). As far as I can tell, this implies that output was reallocated internally and graph execution result was written into that new memory instead of the one provided by the user originally.

see #15087 for a fix prototype + test changes

##### Steps to reproduce

> Note: you need to have OpenCL installed to verify this issue.

OpenCL configuration:
```
OpenCL Platforms: 
    Intel(R) OpenCL HD Graphics
        iGPU: Intel(R) Gen9 HD Graphics NEO (OpenCL 2.1 NEO )
    NVIDIA CUDA
        dGPU: GeForce GTX 1050 Ti (OpenCL 1.2 CUDA)
Current OpenCL device: 
    Type = iGPU
    Name = Intel(R) Gen9 HD Graphics NEO
    Version = OpenCL 2.1 NEO 
    Driver version = 20.02.15268
```

Narrowing down the issue, the following simple test must pass (currently it does not):
```cpp
TEST(OwnMat, ROIViewWithUMat)
{
    auto sz = cv::Size(50, 50);
    cv::Mat mat = cv::Mat(sz * 2, CV_8UC3);

    cv::Mat roi_view = mat(cv::Rect({5, 8}, sz));
    const auto roi_view_ref = roi_view;

    // access Mat through UMat
    {
    EXPECT_EQ(roi_view_ref.data, roi_view.getUMat(ACCESS_RW).getMat(ACCESS_RW).data);
    EXPECT_EQ(roi_view_ref.datastart, roi_view.getUMat(ACCESS_RW).getMat(ACCESS_RW).datastart);
    }

    // access Mat through UMat but wrap Mat with gapi::own::Mat
    {
    auto own_roi_view = to_own(roi_view);
    EXPECT_EQ(roi_view_ref.data, to_ocv(own_roi_view).getUMat(ACCESS_RW).getMat(ACCESS_RW).data);
    EXPECT_EQ(roi_view_ref.datastart, to_ocv(own_roi_view).getUMat(ACCESS_RW).getMat(ACCESS_RW).datastart);
    }
}
```

Also, there's a `BackendOutputAllocationLargeSizeWithCorrectSubmatrixTest` test which is right now disabled for OCL backend - [latest master link](https://github.com/opencv/opencv/blob/6271192a3298e3237fa7e6c498686a8454e67e0e/modules/gapi/test/gpu/gapi_core_tests_gpu.cpp#L402). Once this issue is resolved, this must pass as well

##### Issue submission checklist

 - [x] I report the issue, it's not a question
   <!--
   OpenCV team works with answers.opencv.org, Stack Overflow and other communities
   to discuss problems. Tickets with question without real issue statement will be
   closed.
   -->
 - [ ] I checked the problem with documentation, FAQ, open issues,
       answers.opencv.org, Stack Overflow, etc and have not found solution
   <!--
   Places to check:
   * OpenCV documentation: https://docs.opencv.org
   * FAQ page: https://github.com/opencv/opencv/wiki/FAQ
   * OpenCV forum: https://answers.opencv.org
   * OpenCV issue tracker: https://github.com/opencv/opencv/issues?q=is%3Aissue
   * Stack Overflow branch: https://stackoverflow.com/questions/tagged/opencv
   -->
 - [x] I updated to latest OpenCV version and the issue is still there (**comment**: the issue is still reproduced on 4.2.0-dev)
   <!--
   master branch for OpenCV 4.x and 3.4 branch for OpenCV 3.x releases.
   OpenCV team supports only latest release for each branch.
   The ticket is closed, if the problem is not reproduced with modern version.
   -->
 - [x] There is reproducer code and related data files: videos, images, onnx, etc
   <!--
   The best reproducer -- test case for OpenCV that we can add to the library.
   Recommendations for media files and binary files:
   * Try to reproduce the issue with images and videos in opencv_extra repository
     to reduce attachment size
   * Use PNG for images, if you report some CV related bug, but not image reader
     issue
   * Attach the image as archite to the ticket, if you report some reader issue.
     Image hosting services compress images and it breaks the repro code.
   * Provide ONNX file for some public model or ONNX file with with random weights,
     if you report ONNX parsing or handling issue. Architecture details diagram
     from netron tool can be very useful too. See https://lutzroeder.github.io/netron/
   -->