surface.cpp 4.07 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
// Copyright (c) 2012-2017 VideoStitch SAS
// Copyright (c) 2018 stitchEm

#include "surface.hpp"
#include "deviceBuffer.hpp"
#include "deviceStream.hpp"
#include "cl_error.hpp"
#include "gpu/allocator.hpp"
#include "gpu/stream.hpp"
#include "gpu/surface.hpp"

#include "../common/allocStats.hpp"

namespace VideoStitch {
namespace GPU {

Surface::~Surface() { delete pimpl; }

CubemapSurface::~CubemapSurface() { delete pimpl; }

bool Surface::operator==(const Surface& other) const {
  if (pimpl && other.pimpl) {
    return *pimpl == *other.pimpl;
  }
  return !pimpl && !other.pimpl;
}

DeviceSurface::~DeviceSurface() {
  if (ownsImage) {
    deviceStats.deletePtr(image);
    clReleaseMemObject(image);
  }
}

DeviceCubemapSurface::~DeviceCubemapSurface() {}

Surface::Surface(DeviceSurface* pimpl, size_t width, size_t height) : pimpl(pimpl), _width(width), _height(height) {}

Surface::Surface(const Surface& other) : pimpl(other.pimpl), _width(other._width), _height(other._height) {}

DeviceSurface& Surface::get() {
  assert(pimpl);
  return *pimpl;
}
const DeviceSurface& Surface::get() const {
  assert(pimpl);
  return *pimpl;
}

}  // namespace GPU

namespace Core {

#ifdef __GNUC__
#pragma GCC diagnostic ignored "-Wunused-variable"
#elif defined(_MSC_VER)
#pragma warning(disable : 4189)
#endif

Status SourceOpenGLSurface::Pimpl::acquire() {
  acquired = true;
  return CL_ERROR(clEnqueueAcquireGLObjects(stream.get(), 1, &surface->get().raw(), 0, 0, nullptr));
}

Status SourceOpenGLSurface::Pimpl::release() {
  if (acquired) {
    acquired = false;
    return CL_ERROR(clEnqueueReleaseGLObjects(stream.get(), 1, &surface->get().raw(), 0, 0, nullptr));
  }
  return Status::OK();
}

Status PanoOpenGLSurface::Pimpl::acquire() {
  acquired = true;
  cl_mem pbo = buffer.get();
  return CL_ERROR(clEnqueueAcquireGLObjects(stream.get(), 1, &pbo, 0, 0, nullptr));
}

Status PanoOpenGLSurface::Pimpl::release() {
  if (acquired) {
    acquired = false;
    cl_mem pbo = buffer.get();
    return CL_ERROR(clEnqueueReleaseGLObjects(stream.get(), 1, &pbo, 0, 0, nullptr));
  }
  return Status::OK();
}

Status CubemapOpenGLSurface::Pimpl::acquire() {
  for (int i = 0; i < 6; ++i) {
    cl_mem pbo = buffers[i].get();
    FAIL_RETURN(CL_ERROR(clEnqueueAcquireGLObjects(stream.get(), 1, &pbo, 0, 0, nullptr)));
  }
  return Status::OK();
}

Status CubemapOpenGLSurface::Pimpl::release() {
  for (int i = 0; i < 6; ++i) {
    cl_mem pbo = buffers[i].get();
    FAIL_RETURN(CL_ERROR(clEnqueueReleaseGLObjects(stream.get(), 1, &pbo, 0, 0, nullptr)));
  }
  return Status::OK();
}

Potential<PanoOpenGLSurface::Pimpl> PanoOpenGLSurface::Pimpl::create(GPU::Buffer<uint32_t> buffer,
                                                                     GPU::Surface* remapSurf, size_t w, size_t h) {
  PotentialValue<GPU::Stream> stream = GPU::Stream::create();
  if (stream.ok()) {
    return Potential<Pimpl>(new Pimpl(stream.value(), buffer, remapSurf, w, h));
  } else {
    return Potential<Pimpl>(stream.status());
  }
}

Potential<CubemapOpenGLSurface::Pimpl> CubemapOpenGLSurface::Pimpl::create(GPU::Buffer<uint32_t>* buffers,
                                                                           GPU::Buffer<uint32_t> buffer,
                                                                           GPU::CubemapSurface* remapSurf,
                                                                           GPU::Buffer<uint32_t> tmp, size_t w,
                                                                           bool equiangular) {
  PotentialValue<GPU::Stream> stream = GPU::Stream::create();
  if (stream.ok()) {
    return Potential<Pimpl>(new Pimpl(equiangular, buffers, buffer, remapSurf, tmp, w, stream.value()));
  } else {
    return Potential<Pimpl>(stream.status());
  }
}

Potential<SourceOpenGLSurface::Pimpl> SourceOpenGLSurface::Pimpl::create(GPU::Surface* surf) {
  PotentialValue<GPU::Stream> stream = GPU::Stream::create();
  if (stream.ok()) {
    return Potential<Pimpl>(new Pimpl(surf, stream.value()));
  } else {
    return Potential<Pimpl>(stream.status());
  }
}

}  // namespace Core
}  // namespace VideoStitch