/*** Olive - Non-Linear Video Editor Copyright (C) 2019 Olive Team This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . ***/ #include "opengltexture.h" #include #include #include "render/pixelservice.h" OpenGLTexture::OpenGLTexture() : created_ctx_(nullptr), texture_(0), width_(0), height_(0), format_(olive::PIX_FMT_INVALID) { } OpenGLTexture::~OpenGLTexture() { Destroy(); } bool OpenGLTexture::IsCreated() const { return (texture_); } void OpenGLTexture::Create(QOpenGLContext *ctx, int width, int height, const olive::PixelFormat &format, const void* data) { if (!ctx) { qWarning() << "RenderTexture::Create was passed an invalid context"; return; } Destroy(); created_ctx_ = ctx; width_ = width; height_ = height; format_ = format; connect(created_ctx_, SIGNAL(aboutToBeDestroyed()), this, SLOT(Destroy())); // Create main texture CreateInternal(created_ctx_, &texture_, data); } void OpenGLTexture::Create(QOpenGLContext *ctx, FramePtr frame) { Create(ctx, frame->width(), frame->height(), frame->format(), frame->data()); } void OpenGLTexture::Destroy() { if (created_ctx_) { disconnect(created_ctx_, SIGNAL(aboutToBeDestroyed()), this, SLOT(Destroy())); created_ctx_->functions()->glDeleteTextures(1, &texture_); texture_ = 0; created_ctx_ = nullptr; } } void OpenGLTexture::Bind() { QOpenGLContext* context = QOpenGLContext::currentContext(); if (!context) { qWarning() << "RenderTexture::Bind() called with an invalid context"; return; } context->functions()->glBindTexture(GL_TEXTURE_2D, texture_); } void OpenGLTexture::Release() { QOpenGLContext* context = QOpenGLContext::currentContext(); if (!context) { qWarning() << "RenderTexture::Release() called with an invalid context"; return; } context->functions()->glBindTexture(GL_TEXTURE_2D, 0); } const int &OpenGLTexture::width() const { return width_; } const int &OpenGLTexture::height() const { return height_; } const olive::PixelFormat &OpenGLTexture::format() const { return format_; } const GLuint &OpenGLTexture::texture() const { return texture_; } void OpenGLTexture::Upload(const void *data) { if (!IsCreated()) { qWarning() << "RenderTexture::Upload() called while it wasn't created"; return; } QOpenGLContext* context = QOpenGLContext::currentContext(); if (!context) { qWarning() << "RenderTexture::Release() called with an invalid context"; return; } Bind(); PixelFormatInfo info = PixelService::GetPixelFormatInfo(format_); context->functions()->glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width_, height_, info.pixel_format, info.gl_pixel_type, data); Release(); } uchar *OpenGLTexture::Download() const { if (!IsCreated()) { qWarning() << "RenderTexture::Download() called while it wasn't created"; return nullptr; } QOpenGLContext* context = QOpenGLContext::currentContext(); QOpenGLFunctions* f = context->functions(); GLuint read_fbo; f->glGenFramebuffers(1, &read_fbo); f->glBindFramebuffer(GL_READ_FRAMEBUFFER, read_fbo); context->extraFunctions()->glFramebufferTexture2D( GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture_, 0 ); PixelFormatInfo format_info = PixelService::GetPixelFormatInfo(format_); uchar* data = new uchar[PixelService::GetBufferSize(format_, width_, height_)]; f->glReadPixels(0, 0, width_, height_, format_info.pixel_format, format_info.gl_pixel_type, data); f->glBindFramebuffer(GL_READ_FRAMEBUFFER, 0); f->glDeleteFramebuffers(1, &read_fbo); return data; } void OpenGLTexture::CreateInternal(QOpenGLContext* create_ctx, GLuint* tex, const void *data) { QOpenGLFunctions* f = create_ctx->functions(); // Create texture f->glGenTextures(1, tex); // Verify texture if (texture_ == 0) { qWarning() << "OpenGL texture creation failed"; return; } // Bind texture f->glBindTexture(GL_TEXTURE_2D, *tex); // Allocate storage for texture const PixelFormatInfo& bit_depth = PixelService::GetPixelFormatInfo(format_); f->glTexImage2D( GL_TEXTURE_2D, 0, bit_depth.internal_format, width_, height_, 0, bit_depth.pixel_format, bit_depth.gl_pixel_type, data ); // Set texture filtering to bilinear f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Release texture f->glBindTexture(GL_TEXTURE_2D, 0); }