diff --git a/src/platform/android/JPAGSurface.cpp b/src/platform/android/JPAGSurface.cpp index e23c9125ca..31d34804dc 100644 --- a/src/platform/android/JPAGSurface.cpp +++ b/src/platform/android/JPAGSurface.cpp @@ -17,6 +17,9 @@ ///////////////////////////////////////////////////////////////////////////////////////////////// #include "JPAGSurface.h" +#include +#include +#include #include #include "GPUDecoder.h" #include "GPUDrawable.h" @@ -130,9 +133,9 @@ JNIEXPORT jlong Java_org_libpag_PAGSurface_SetupFromTexture(JNIEnv*, jclass, jin jint width, jint height, jboolean flipY, jboolean forAsyncThread) { GLTextureInfo glInfo = {}; - glInfo.target = GL::TEXTURE_2D; + glInfo.target = GL_TEXTURE_2D; glInfo.id = static_cast(textureID); - glInfo.format = GL::RGBA8; + glInfo.format = GL_RGBA8; BackendTexture glTexture(glInfo, width, height); auto origin = flipY ? ImageOrigin::BottomLeft : ImageOrigin::TopLeft; diff --git a/src/platform/android/VideoSurface.cpp b/src/platform/android/VideoSurface.cpp index d53bd08d84..311cc96268 100644 --- a/src/platform/android/VideoSurface.cpp +++ b/src/platform/android/VideoSurface.cpp @@ -18,6 +18,7 @@ #include "VideoSurface.h" #include "android/native_window_jni.h" +#include "gpu/opengl/GLContext.h" #include "gpu/opengl/GLTexture.h" namespace pag { @@ -44,10 +45,9 @@ void VideoSurface::InitJNI(JNIEnv* env, const std::string& className) { VideoSurface_onRelease = env->GetMethodID(VideoSurfaceClass.get(), "onRelease", "()V"); } -OESTexture::OESTexture(tgfx::GLTextureInfo info, int width, int height) +OESTexture::OESTexture(const tgfx::GLSampler& glSampler, int width, int height) : GLTexture(width, height, tgfx::ImageOrigin::TopLeft) { - sampler.glInfo = info; - sampler.config = tgfx::PixelFormat::RGBA_8888; + sampler = glSampler; } void OESTexture::setTextureSize(int width, int height) { @@ -85,9 +85,9 @@ tgfx::Point OESTexture::getTextureCoord(float x, float y) const { } void OESTexture::onRelease(tgfx::Context* context) { - if (sampler.glInfo.id > 0) { + if (sampler.id > 0) { auto gl = tgfx::GLContext::Unwrap(context); - gl->deleteTextures(1, &sampler.glInfo.id); + gl->deleteTextures(1, &sampler.id); } } @@ -163,20 +163,21 @@ bool VideoSurface::attachToContext(JNIEnv* env, tgfx::Context* context) { return true; } auto gl = tgfx::GLContext::Unwrap(context); - tgfx::GLTextureInfo info = {}; - info.target = GL::TEXTURE_EXTERNAL_OES; - info.format = GL::RGBA8; - gl->genTextures(1, &info.id); - if (info.id == 0) { + tgfx::GLSampler sampler = {}; + sampler.target = GL::TEXTURE_EXTERNAL_OES; + sampler.format = tgfx::PixelFormat::RGBA_8888; + gl->genTextures(1, &sampler.id); + if (sampler.id == 0) { return false; } - auto result = env->CallBooleanMethod(videoSurface.get(), VideoSurface_attachToGLContext, info.id); + auto result = + env->CallBooleanMethod(videoSurface.get(), VideoSurface_attachToGLContext, sampler.id); if (!result) { - gl->deleteTextures(1, &info.id); + gl->deleteTextures(1, &sampler.id); LOGE("VideoSurface::attachToGLContext(): failed to attached to a Surface!"); return false; } - glInfo = info; + glInfo = sampler; deviceID = context->device()->uniqueID(); return true; } diff --git a/src/platform/android/VideoSurface.h b/src/platform/android/VideoSurface.h index ef7b362f81..ba57c88795 100644 --- a/src/platform/android/VideoSurface.h +++ b/src/platform/android/VideoSurface.h @@ -24,7 +24,7 @@ namespace pag { class OESTexture : public tgfx::GLTexture { public: - OESTexture(tgfx::GLTextureInfo info, int width, int height); + OESTexture(const tgfx::GLSampler& sampler, int width, int height); tgfx::Point getTextureCoord(float x, float y) const override; @@ -73,7 +73,7 @@ class VideoSurface { int width = 0; int height = 0; uint32_t deviceID = 0; - tgfx::GLTextureInfo glInfo = {}; + tgfx::GLSampler glInfo = {}; std::shared_ptr oesTexture = nullptr; mutable std::atomic_bool hasPendingTextureImage = {false}; diff --git a/tgfx/src/gpu/opengl/GLDefines.h b/tgfx/src/gpu/opengl/GLDefines.h index 6903b4721e..57018f74a0 100644 --- a/tgfx/src/gpu/opengl/GLDefines.h +++ b/tgfx/src/gpu/opengl/GLDefines.h @@ -22,6 +22,9 @@ #include namespace GL { +#undef FALSE +#undef TRUE + // The following constants consist of the intersection of GL constants // exported by GLES 1.0, GLES 2.0, and desktop GL required by the system. diff --git a/tgfx/src/gpu/opengl/cgl/CGLWindow.mm b/tgfx/src/gpu/opengl/cgl/CGLWindow.mm index 729e17f192..4093f24f00 100644 --- a/tgfx/src/gpu/opengl/cgl/CGLWindow.mm +++ b/tgfx/src/gpu/opengl/cgl/CGLWindow.mm @@ -20,6 +20,7 @@ #include #include #include "CGLHardwareTexture.h" +#include "gpu/opengl/GLRenderTarget.h" namespace tgfx { static std::mutex threadCacheLocker = {}; @@ -106,11 +107,11 @@ #pragma clang diagnostic ignored "-Wdeprecated-declarations" [glContext setView:view]; #pragma clang diagnostic pop - GLFrameBufferInfo glInfo = {}; - glInfo.id = 0; - glInfo.format = GL_RGBA8; - BackendRenderTarget renderTarget(glInfo, size.width, size.height); - return Surface::MakeFrom(context, renderTarget, ImageOrigin::BottomLeft); + GLFrameBuffer frameBuffer = {}; + frameBuffer.id = 0; + frameBuffer.format = PixelFormat::RGBA_8888; + auto renderTarget = GLRenderTarget::MakeFrom(context, frameBuffer, size.width, size.height, ImageOrigin::BottomLeft); + return Surface::MakeFrom(context, renderTarget); } auto texture = CGLHardwareTexture::MakeFrom(context, pixelBuffer); return Surface::MakeFrom(context, texture); diff --git a/tgfx/src/gpu/opengl/eagl/EAGLHardwareTexture.mm b/tgfx/src/gpu/opengl/eagl/EAGLHardwareTexture.mm index b87aaf0acd..d5c34a1321 100644 --- a/tgfx/src/gpu/opengl/eagl/EAGLHardwareTexture.mm +++ b/tgfx/src/gpu/opengl/eagl/EAGLHardwareTexture.mm @@ -19,31 +19,28 @@ #include "EAGLHardwareTexture.h" #include "gpu/opengl/eagl/EAGLDevice.h" #include "core/utils/UniqueID.h" +#include "gpu/opengl/GLContext.h" namespace tgfx { static CVOpenGLESTextureRef GetTextureRef(Context* context, CVPixelBufferRef pixelBuffer, - CVOpenGLESTextureCacheRef textureCache, - unsigned* sizedFormat) { + CVOpenGLESTextureCacheRef textureCache) { if (textureCache == nil) { return nil; } auto width = static_cast(CVPixelBufferGetWidth(pixelBuffer)); auto height = static_cast(CVPixelBufferGetHeight(pixelBuffer)); - auto pixelFormat = CVPixelBufferGetPixelFormatType(pixelBuffer); CVOpenGLESTextureRef texture = nil; CVReturn result; - if (pixelFormat == kCVPixelFormatType_OneComponent8) { - auto gl = GLContext::Unwrap(context); + if (CVPixelBufferGetPixelFormatType(pixelBuffer) == kCVPixelFormatType_OneComponent8) { + auto gl = GLContext::Unwrap(context); const auto& format = gl->caps->getTextureFormat(PixelFormat::ALPHA_8); - *sizedFormat = format.sizedFormat; // 返回的 texture 对象是一个强引用计数为 1 的对象。 result = CVOpenGLESTextureCacheCreateTextureFromImage( kCFAllocatorDefault, textureCache, pixelBuffer, NULL, /* texture attributes */ GL::TEXTURE_2D, format.internalFormatTexImage, /* opengl format */ - width, height, format.externalFormat, /* native iOS format */ + width, height, GL::RED, /* native iOS format */ GL::UNSIGNED_BYTE, 0, &texture); } else { - *sizedFormat = GL::RGBA8; // 返回的 texture 对象是一个强引用计数为 1 的对象。 result = CVOpenGLESTextureCacheCreateTextureFromImage( kCFAllocatorDefault, textureCache, pixelBuffer, NULL, /* texture attributes */ @@ -72,20 +69,19 @@ static CVOpenGLESTextureRef GetTextureRef(Context* context, CVPixelBufferRef pix if (eaglDevice == nullptr) { return nullptr; } - unsigned sizedFormat = 0; - auto texture = GetTextureRef(context, pixelBuffer, eaglDevice->getTextureCache(), &sizedFormat); + + auto texture = GetTextureRef(context, pixelBuffer, eaglDevice->getTextureCache()); if (texture == nil) { return nullptr; } - GLTextureInfo glInfo = {}; + GLSampler glInfo = {}; + auto oneComponent8 = + CVPixelBufferGetPixelFormatType(pixelBuffer) == kCVPixelFormatType_OneComponent8; + glInfo.format = oneComponent8 ? PixelFormat::ALPHA_8 : PixelFormat::RGBA_8888; glInfo.target = CVOpenGLESTextureGetTarget(texture); glInfo.id = CVOpenGLESTextureGetName(texture); - glInfo.format = sizedFormat; - auto oneComponent8 = - CVPixelBufferGetPixelFormatType(pixelBuffer) == kCVPixelFormatType_OneComponent8; glTexture = Resource::Wrap(context, new EAGLHardwareTexture(pixelBuffer)); - glTexture->sampler.glInfo = glInfo; - glTexture->sampler.format = oneComponent8 ? PixelFormat::ALPHA_8 : PixelFormat::RGBA_8888; + glTexture->sampler = glInfo; glTexture->texture = texture; return glTexture; } diff --git a/tgfx/src/gpu/opengl/eagl/EAGLNV12Texture.mm b/tgfx/src/gpu/opengl/eagl/EAGLNV12Texture.mm index 79a4c7cf76..3c2ffd29e5 100644 --- a/tgfx/src/gpu/opengl/eagl/EAGLNV12Texture.mm +++ b/tgfx/src/gpu/opengl/eagl/EAGLNV12Texture.mm @@ -17,15 +17,16 @@ ///////////////////////////////////////////////////////////////////////////////////////////////// #include "EAGLNV12Texture.h" +#include "gpu/opengl/GLContext.h" #include "gpu/opengl/eagl/EAGLDevice.h" namespace tgfx { -static GLTextureInfo ToGLTexture(CVOpenGLESTextureRef texture, unsigned format) { - GLTextureInfo glInfo = {}; - glInfo.target = CVOpenGLESTextureGetTarget(texture); - glInfo.id = CVOpenGLESTextureGetName(texture); - glInfo.format = format; - return glInfo; +static GLSampler ToGLSampler(CVOpenGLESTextureRef texture, PixelFormat format) { + GLSampler sampler = {}; + sampler.target = CVOpenGLESTextureGetTarget(texture); + sampler.id = CVOpenGLESTextureGetName(texture); + sampler.format = format; + return sampler; } std::shared_ptr EAGLNV12Texture::MakeFrom(Context* context, @@ -44,31 +45,29 @@ static GLTextureInfo ToGLTexture(CVOpenGLESTextureRef texture, unsigned format) auto height = static_cast(CVPixelBufferGetHeight(pixelBuffer)); CVOpenGLESTextureRef outputTextureLuma = nil; CVOpenGLESTextureRef outputTextureChroma = nil; - auto oneComponentFormat = PixelFormat::GRAY_8; auto gl = GLContext::Unwrap(context); - const auto& oneComponentFormat = gl->caps->getTextureFormat(oneComponentFormat); + auto lumaComponentFormat = PixelFormat::GRAY_8; + const auto& oneComponentFormat = gl->caps->getTextureFormat(lumaComponentFormat); // 返回的 texture 对象是一个强引用计数为 1 的对象。 CVOpenGLESTextureCacheCreateTextureFromImage( - kCFAllocatorDefault, textureCache, pixelBuffer, NULL, GL_TEXTURE_2D, + kCFAllocatorDefault, textureCache, pixelBuffer, NULL, GL::TEXTURE_2D, oneComponentFormat.internalFormatTexImage, width, height, oneComponentFormat.externalFormat, - GL_UNSIGNED_BYTE, 0, &outputTextureLuma); - auto twoComponentFormat = PixelFormat::RG_88; - const auto& twoComponentFormat = gl->caps->getTextureFormat(twoComponentFormat); + GL::UNSIGNED_BYTE, 0, &outputTextureLuma); + auto chromaComponentFormat = PixelFormat::RG_88; + const auto& twoComponentFormat = gl->caps->getTextureFormat(chromaComponentFormat); // 返回的 texture 对象是一个强引用计数为 1 的对象。 CVOpenGLESTextureCacheCreateTextureFromImage( - kCFAllocatorDefault, textureCache, pixelBuffer, NULL, GL_TEXTURE_2D, + kCFAllocatorDefault, textureCache, pixelBuffer, NULL, GL::TEXTURE_2D, twoComponentFormat.internalFormatTexImage, width / 2, height / 2, - twoComponentFormat.externalFormat, GL_UNSIGNED_BYTE, 1, &outputTextureChroma); + twoComponentFormat.externalFormat, GL::UNSIGNED_BYTE, 1, &outputTextureChroma); if (outputTextureLuma == nil || outputTextureChroma == nil) { return nullptr; } auto texture = Resource::Wrap(context, new EAGLNV12Texture(pixelBuffer, colorSpace, colorRange)); texture->lumaTexture = outputTextureLuma; - texture->samplers.emplace_back(oneComponentFormat, - ToGLTexture(outputTextureLuma, oneComponentFormat.sizedFormat)); + texture->samplers.push_back(ToGLSampler(outputTextureLuma, lumaComponentFormat)); texture->chromaTexture = outputTextureChroma; - texture->samplers.emplace_back(twoComponentFormat, - ToGLTexture(outputTextureChroma, twoComponentFormat.sizedFormat)); + texture->samplers.push_back(ToGLSampler(outputTextureChroma, chromaComponentFormat)); return texture; } diff --git a/tgfx/src/gpu/opengl/eagl/EAGLWindow.mm b/tgfx/src/gpu/opengl/eagl/EAGLWindow.mm index 7e7cf64421..a6062812d1 100644 --- a/tgfx/src/gpu/opengl/eagl/EAGLWindow.mm +++ b/tgfx/src/gpu/opengl/eagl/EAGLWindow.mm @@ -137,11 +137,11 @@ LOGE("EAGLWindow::onCreateSurface() Framebuffer is not complete!"); return nullptr; } - GLFrameBufferInfo glInfo = {}; + GLFrameBuffer glInfo = {}; glInfo.id = frameBufferID; - glInfo.format = GL::RGBA8; - BackendRenderTarget renderTarget(glInfo, static_cast(width), static_cast(height)); - return Surface::MakeFrom(context, renderTarget, ImageOrigin::BottomLeft); + glInfo.format = PixelFormat::RGBA_8888; + auto renderTarget = GLRenderTarget::MakeFrom(context, glInfo, static_cast(width), static_cast(height), ImageOrigin::BottomLeft); + return Surface::MakeFrom(context, renderTarget); } void EAGLWindow::onPresent(Context* context, int64_t) { diff --git a/tgfx/src/gpu/opengl/egl/EGLHardwareTexture.cpp b/tgfx/src/gpu/opengl/egl/EGLHardwareTexture.cpp index d1369c10c8..c7ba74cf07 100644 --- a/tgfx/src/gpu/opengl/egl/EGLHardwareTexture.cpp +++ b/tgfx/src/gpu/opengl/egl/EGLHardwareTexture.cpp @@ -70,24 +70,25 @@ std::shared_ptr EGLHardwareTexture::MakeFrom(Context* contex if (eglImage == EGL_NO_IMAGE_KHR) { return nullptr; } - GLTextureInfo glInfo = {}; - glGenTextures(1, &glInfo.id); - if (glInfo.id == 0) { + GLSampler sampler = {}; + sampler.target = GL_TEXTURE_2D; + sampler.format = PixelFormat::RGBA_8888; + glGenTextures(1, &sampler.id); + if (sampler.id == 0) { eglext::eglDestroyImageKHR(display, eglImage); return nullptr; } - glBindTexture(GL_TEXTURE_2D, glInfo.id); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - eglext::glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)eglImage); + glBindTexture(sampler.target, sampler.id); + glTexParameteri(sampler.target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(sampler.target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(sampler.target, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(sampler.target, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + eglext::glEGLImageTargetTexture2DOES(sampler.target, (GLeglImageOES)eglImage); AHardwareBuffer_Desc desc; HardwareBufferInterface::Describe(hardwareBuffer, &desc); glTexture = Resource::Wrap( context, new EGLHardwareTexture(hardwareBuffer, eglImage, desc.width, desc.height)); - glTexture->sampler.glInfo = glInfo; - glTexture->sampler.format = PixelFormat::RGBA_8888; + glTexture->sampler = sampler; return glTexture; } @@ -110,8 +111,7 @@ void EGLHardwareTexture::ComputeRecycleKey(BytesKey* recycleKey, void* hardwareB } void EGLHardwareTexture::onRelease(Context* context) { - auto gl = GLContext::Unwrap(context); - gl->deleteTextures(1, &sampler.glInfo.id); + glDeleteTextures(1, &sampler.id); auto display = static_cast(context->device())->getDisplay(); eglext::eglDestroyImageKHR(display, eglImage); } diff --git a/tgfx/src/gpu/opengl/qt/QGLWindow.cpp b/tgfx/src/gpu/opengl/qt/QGLWindow.cpp index 1a671f061d..37eee40825 100644 --- a/tgfx/src/gpu/opengl/qt/QGLWindow.cpp +++ b/tgfx/src/gpu/opengl/qt/QGLWindow.cpp @@ -72,7 +72,7 @@ QSGTexture* QGLWindow::getTexture() { delete outTexture; outTexture = nullptr; } - auto textureID = frontTexture->getGLInfo().id; + auto textureID = frontTexture->glSampler().id; auto width = static_cast(ceil(quickItem->width())); auto height = static_cast(ceil(quickItem->height())); outTexture = nativeWindow->createTextureFromId(textureID, QSize(width, height), @@ -97,10 +97,11 @@ std::shared_ptr QGLWindow::onCreateSurface(Context* context) { if (width <= 0 || height <= 0) { return nullptr; } - frontTexture = GLTexture::MakeRGBA(context, width, height); - backTexture = GLTexture::MakeRGBA(context, width, height); - renderTarget = GLRenderTarget::MakeFrom(context, backTexture.get()); - return GLSurface::MakeFrom(context, renderTarget); + frontTexture = std::static_pointer_cast(Texture::MakeRGBA(context, width, height)); + backTexture = std::static_pointer_cast(Texture::MakeRGBA(context, width, height)); + auto surface = Surface::MakeFrom(context, backTexture); + renderTarget = std::static_pointer_cast(surface->getRenderTarget()); + return surface; } void QGLWindow::onPresent(Context* context, int64_t) { @@ -110,9 +111,9 @@ void QGLWindow::onPresent(Context* context, int64_t) { auto gl = GLContext::Unwrap(context); std::swap(frontTexture, backTexture); gl->flush(); - gl->bindFramebuffer(GL_FRAMEBUFFER, renderTarget->getGLInfo().id); + gl->bindFramebuffer(GL_FRAMEBUFFER, renderTarget->glFrameBuffer().id); gl->framebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, - backTexture->getGLInfo().id, 0); + backTexture->glSampler().id, 0); gl->bindFramebuffer(GL_FRAMEBUFFER, 0); invalidateTexture(); }