Project import generated by Copybara.
GitOrigin-RevId: afeb9cf5a8c069c0a566d16e1622bbb086170e4d
This commit is contained in:
@@ -105,6 +105,7 @@ void GlCalculatorHelperImpl::SetStandardTextureParams(GLenum target,
|
||||
GLint filter;
|
||||
switch (internal_format) {
|
||||
case GL_R32F:
|
||||
case GL_RG32F:
|
||||
case GL_RGBA32F:
|
||||
// 32F (unlike 16f) textures do not always support texture filtering
|
||||
// (According to OpenGL ES specification [TEXTURE IMAGE SPECIFICATION])
|
||||
|
||||
@@ -46,16 +46,40 @@ GlContext::StatusOrGlContext GlContext::Create(NSOpenGLContext* share_context,
|
||||
|
||||
::mediapipe::Status GlContext::CreateContext(NSOpenGLContext* share_context) {
|
||||
// TODO: choose a better list?
|
||||
NSOpenGLPixelFormatAttribute attrs[] = {NSOpenGLPFAAccelerated,
|
||||
NSOpenGLPFAColorSize,
|
||||
24,
|
||||
NSOpenGLPFAAlphaSize,
|
||||
8,
|
||||
NSOpenGLPFADepthSize,
|
||||
16,
|
||||
0};
|
||||
NSOpenGLPixelFormatAttribute attrs[] = {
|
||||
// This is required to get any OpenGL version 3.2 or higher. Note that
|
||||
// once this is enabled up to version 4.1 can be supported (depending on
|
||||
// hardware).
|
||||
// TODO: Remove the need for the OSX_ENABLE_3_2_CORE if this
|
||||
// proves to be safe in general.
|
||||
#if defined(TARGET_OS_OSX) && defined(OSX_ENABLE_3_2_CORE)
|
||||
NSOpenGLPFAOpenGLProfile,
|
||||
NSOpenGLProfileVersion3_2Core,
|
||||
#endif
|
||||
NSOpenGLPFAAccelerated,
|
||||
NSOpenGLPFAColorSize,
|
||||
24,
|
||||
NSOpenGLPFAAlphaSize,
|
||||
8,
|
||||
NSOpenGLPFADepthSize,
|
||||
16,
|
||||
0
|
||||
};
|
||||
|
||||
pixel_format_ = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
|
||||
// If OpenGL 3.2 Core does not work, try again without it.
|
||||
if (!pixel_format_) {
|
||||
NSOpenGLPixelFormatAttribute attrs_2_1[] = {NSOpenGLPFAAccelerated,
|
||||
NSOpenGLPFAColorSize,
|
||||
24,
|
||||
NSOpenGLPFAAlphaSize,
|
||||
8,
|
||||
NSOpenGLPFADepthSize,
|
||||
16,
|
||||
0};
|
||||
|
||||
pixel_format_ = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
|
||||
}
|
||||
if (!pixel_format_) {
|
||||
// On several Forge machines, the default config fails. For now let's do
|
||||
// this.
|
||||
|
||||
@@ -57,13 +57,17 @@ namespace mediapipe {
|
||||
|
||||
// Note: on systems where highp precision for floats is not supported (look up
|
||||
// GL_FRAGMENT_PRECISION_HIGH), we replace it with mediump.
|
||||
#define FRAGMENT_PREAMBLE \
|
||||
PRECISION_COMPAT \
|
||||
"#if __VERSION__ < 130\n" \
|
||||
"#define in varying\n" \
|
||||
"#if GL_ES && !GL_FRAGMENT_PRECISION_HIGH\n" \
|
||||
"#define highp mediump\n" \
|
||||
"#endif // GL_ES && !GL_FRAGMENT_PRECISION_HIGH\n" \
|
||||
// gl_FragColor is re-defined to 'frag_out' when using 3.20 Core (GLSL 330+).
|
||||
#define FRAGMENT_PREAMBLE \
|
||||
PRECISION_COMPAT \
|
||||
"#if __VERSION__ < 130\n" \
|
||||
"#define in varying\n" \
|
||||
"#if defined(GL_ES) && !defined(GL_FRAGMENT_PRECISION_HIGH)\n" \
|
||||
"#define highp mediump\n" \
|
||||
"#endif // GL_ES && !GL_FRAGMENT_PRECISION_HIGH\n" \
|
||||
"#elif __VERSION__ > 320 && !defined(GL_ES)\n" \
|
||||
"out vec4 frag_out; \n" \
|
||||
"#define gl_FragColor frag_out\n" \
|
||||
"#endif // __VERSION__ < 130\n"
|
||||
|
||||
const GLchar* const kMediaPipeVertexShaderPreamble = VERTEX_PREAMBLE;
|
||||
|
||||
@@ -87,11 +87,19 @@ bool GlTextureBuffer::CreateInternal(const void* data) {
|
||||
}
|
||||
|
||||
void GlTextureBuffer::Reuse() {
|
||||
absl::MutexLock lock(&consumer_sync_mutex_);
|
||||
consumer_multi_sync_->WaitOnGpu();
|
||||
// Reset the sync points.
|
||||
consumer_multi_sync_ = absl::make_unique<GlMultiSyncPoint>();
|
||||
producer_sync_ = nullptr;
|
||||
// The old consumer sync destructor may call other contexts to delete their
|
||||
// sync fences; with a single-threaded executor, that means switching to
|
||||
// each of those contexts, grabbing its mutex. Let's do that after releasing
|
||||
// our own mutex.
|
||||
std::unique_ptr<GlMultiSyncPoint> old_consumer_sync;
|
||||
{
|
||||
absl::MutexLock lock(&consumer_sync_mutex_);
|
||||
consumer_multi_sync_->WaitOnGpu();
|
||||
// Reset the sync points.
|
||||
old_consumer_sync = std::move(consumer_multi_sync_);
|
||||
consumer_multi_sync_ = absl::make_unique<GlMultiSyncPoint>();
|
||||
producer_sync_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void GlTextureBuffer::Updated(std::shared_ptr<GlSyncPoint> prod_token) {
|
||||
|
||||
@@ -85,9 +85,9 @@ void GlTextureBufferPool::TrimAvailable(
|
||||
if (available_.size() > keep) {
|
||||
auto trim_it = std::next(available_.begin(), keep);
|
||||
if (trimmed) {
|
||||
std::move(available_.begin(), trim_it, std::back_inserter(*trimmed));
|
||||
std::move(trim_it, available_.end(), std::back_inserter(*trimmed));
|
||||
}
|
||||
available_.resize(keep);
|
||||
available_.erase(trim_it, available_.end());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -118,6 +118,10 @@ const GlTextureInfo& GlTextureInfoForGpuBufferFormat(GpuBufferFormat format,
|
||||
// TODO: use GL_HALF_FLOAT_OES on GLES2?
|
||||
{GL_RG16F, GL_RG, GL_HALF_FLOAT, 1},
|
||||
}},
|
||||
{GpuBufferFormat::kTwoComponentFloat32,
|
||||
{
|
||||
{GL_RG32F, GL_RG, GL_FLOAT, 1},
|
||||
}},
|
||||
{GpuBufferFormat::kGrayHalf16,
|
||||
{
|
||||
{GL_R16F, GL_RED, GL_HALF_FLOAT, 1},
|
||||
@@ -191,6 +195,8 @@ ImageFormat::Format ImageFormatForGpuBufferFormat(GpuBufferFormat format) {
|
||||
return ImageFormat::YCBCR420P;
|
||||
case GpuBufferFormat::kRGB24:
|
||||
return ImageFormat::SRGB;
|
||||
case GpuBufferFormat::kTwoComponentFloat32:
|
||||
return ImageFormat::VEC32F2;
|
||||
case GpuBufferFormat::kGrayHalf16:
|
||||
case GpuBufferFormat::kTwoComponentHalf16:
|
||||
case GpuBufferFormat::kRGBAHalf64:
|
||||
@@ -209,6 +215,8 @@ GpuBufferFormat GpuBufferFormatForImageFormat(ImageFormat::Format format) {
|
||||
return GpuBufferFormat::kBGRA32;
|
||||
case ImageFormat::VEC32F1:
|
||||
return GpuBufferFormat::kGrayFloat32;
|
||||
case ImageFormat::VEC32F2:
|
||||
return GpuBufferFormat::kTwoComponentFloat32;
|
||||
case ImageFormat::GRAY8:
|
||||
return GpuBufferFormat::kOneComponent8;
|
||||
case ImageFormat::YCBCR420P:
|
||||
|
||||
@@ -36,6 +36,7 @@ enum class GpuBufferFormat : uint32_t {
|
||||
kGrayHalf16 = MEDIAPIPE_FOURCC('L', '0', '0', 'h'),
|
||||
kOneComponent8 = MEDIAPIPE_FOURCC('L', '0', '0', '8'),
|
||||
kTwoComponentHalf16 = MEDIAPIPE_FOURCC('2', 'C', '0', 'h'),
|
||||
kTwoComponentFloat32 = MEDIAPIPE_FOURCC('2', 'C', '0', 'f'),
|
||||
kBiPlanar420YpCbCr8VideoRange = MEDIAPIPE_FOURCC('4', '2', '0', 'v'),
|
||||
kBiPlanar420YpCbCr8FullRange = MEDIAPIPE_FOURCC('4', '2', '0', 'f'),
|
||||
kRGB24 = 0x00000018, // Note: prefer BGRA32 whenever possible.
|
||||
@@ -82,6 +83,8 @@ inline OSType CVPixelFormatForGpuBufferFormat(GpuBufferFormat format) {
|
||||
return kCVPixelFormatType_OneComponent8;
|
||||
case GpuBufferFormat::kTwoComponentHalf16:
|
||||
return kCVPixelFormatType_TwoComponent16Half;
|
||||
case GpuBufferFormat::kTwoComponentFloat32:
|
||||
return kCVPixelFormatType_TwoComponent32Float;
|
||||
case GpuBufferFormat::kBiPlanar420YpCbCr8VideoRange:
|
||||
return kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange;
|
||||
case GpuBufferFormat::kBiPlanar420YpCbCr8FullRange:
|
||||
@@ -112,6 +115,8 @@ inline GpuBufferFormat GpuBufferFormatForCVPixelFormat(OSType format) {
|
||||
return GpuBufferFormat::kOneComponent8;
|
||||
case kCVPixelFormatType_TwoComponent16Half:
|
||||
return GpuBufferFormat::kTwoComponentHalf16;
|
||||
case kCVPixelFormatType_TwoComponent32Float:
|
||||
return GpuBufferFormat::kTwoComponentFloat32;
|
||||
case kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange:
|
||||
return GpuBufferFormat::kBiPlanar420YpCbCr8VideoRange;
|
||||
case kCVPixelFormatType_420YpCbCr8BiPlanarFullRange:
|
||||
|
||||
@@ -36,7 +36,7 @@ static constexpr int kMaxPoolCount = 20;
|
||||
#if MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER
|
||||
|
||||
GpuBufferMultiPool::SimplePool GpuBufferMultiPool::MakeSimplePool(
|
||||
BufferSpec spec) {
|
||||
const BufferSpec& spec) {
|
||||
OSType cv_format = CVPixelFormatForGpuBufferFormat(spec.format);
|
||||
CHECK_NE(cv_format, -1) << "unsupported pixel format";
|
||||
return MakeCFHolderAdopting(
|
||||
@@ -87,7 +87,7 @@ GpuBuffer GpuBufferMultiPool::GetBufferFromSimplePool(
|
||||
#else
|
||||
|
||||
GpuBufferMultiPool::SimplePool GpuBufferMultiPool::MakeSimplePool(
|
||||
BufferSpec spec) {
|
||||
const BufferSpec& spec) {
|
||||
return GlTextureBufferPool::Create(spec.width, spec.height, spec.format,
|
||||
kKeepCount);
|
||||
}
|
||||
@@ -99,10 +99,9 @@ GpuBuffer GpuBufferMultiPool::GetBufferFromSimplePool(
|
||||
|
||||
#endif // MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER
|
||||
|
||||
GpuBuffer GpuBufferMultiPool::GetBuffer(int width, int height,
|
||||
GpuBufferFormat format) {
|
||||
GpuBufferMultiPool::SimplePool GpuBufferMultiPool::GetSimplePool(
|
||||
const BufferSpec& key) {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
BufferSpec key(width, height, format);
|
||||
auto pool_it = pools_.find(key);
|
||||
if (pool_it == pools_.end()) {
|
||||
// Discard the least recently used pool in LRU cache.
|
||||
@@ -127,7 +126,15 @@ GpuBuffer GpuBufferMultiPool::GetBuffer(int width, int height,
|
||||
}
|
||||
buffer_specs_.push_back(key);
|
||||
}
|
||||
return GetBufferFromSimplePool(pool_it->first, pool_it->second);
|
||||
return pool_it->second;
|
||||
}
|
||||
|
||||
GpuBuffer GpuBufferMultiPool::GetBuffer(int width, int height,
|
||||
GpuBufferFormat format) {
|
||||
BufferSpec key(width, height, format);
|
||||
SimplePool pool = GetSimplePool(key);
|
||||
// Note: we release our multipool lock before accessing the simple pool.
|
||||
return GetBufferFromSimplePool(key, pool);
|
||||
}
|
||||
|
||||
GpuBufferMultiPool::~GpuBufferMultiPool() {
|
||||
|
||||
@@ -102,7 +102,8 @@ class GpuBufferMultiPool {
|
||||
typedef std::shared_ptr<GlTextureBufferPool> SimplePool;
|
||||
#endif // MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER
|
||||
|
||||
SimplePool MakeSimplePool(BufferSpec spec);
|
||||
SimplePool MakeSimplePool(const BufferSpec& spec);
|
||||
SimplePool GetSimplePool(const BufferSpec& key);
|
||||
GpuBuffer GetBufferFromSimplePool(BufferSpec spec, const SimplePool& pool);
|
||||
|
||||
absl::Mutex mutex_;
|
||||
|
||||
Reference in New Issue
Block a user