Skip to content

Commit

Permalink
Fix build error on iOS, macOS, Android and QT.
Browse files Browse the repository at this point in the history
  • Loading branch information
domchen committed Feb 23, 2022
1 parent 15f4167 commit 8c84fba
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 80 deletions.
7 changes: 5 additions & 2 deletions src/platform/android/JPAGSurface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
/////////////////////////////////////////////////////////////////////////////////////////////////

#include "JPAGSurface.h"
#include <GLES/gl.h>
#include <GLES/glext.h>
#include <GLES3/gl3.h>
#include <android/native_window_jni.h>
#include "GPUDecoder.h"
#include "GPUDrawable.h"
Expand Down Expand Up @@ -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<unsigned>(textureID);
glInfo.format = GL::RGBA8;
glInfo.format = GL_RGBA8;
BackendTexture glTexture(glInfo, width, height);
auto origin = flipY ? ImageOrigin::BottomLeft : ImageOrigin::TopLeft;

Expand Down
27 changes: 14 additions & 13 deletions src/platform/android/VideoSurface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/platform/android/VideoSurface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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> oesTexture = nullptr;
mutable std::atomic_bool hasPendingTextureImage = {false};

Expand Down
3 changes: 3 additions & 0 deletions tgfx/src/gpu/opengl/GLDefines.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
#include <memory>

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.

Expand Down
11 changes: 6 additions & 5 deletions tgfx/src/gpu/opengl/cgl/CGLWindow.mm
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <OpenGL/gl3.h>
#include <thread>
#include "CGLHardwareTexture.h"
#include "gpu/opengl/GLRenderTarget.h"

namespace tgfx {
static std::mutex threadCacheLocker = {};
Expand Down Expand Up @@ -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);
Expand Down
28 changes: 12 additions & 16 deletions tgfx/src/gpu/opengl/eagl/EAGLHardwareTexture.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(CVPixelBufferGetWidth(pixelBuffer));
auto height = static_cast<int>(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 */
Expand Down Expand Up @@ -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;
}
Expand Down
35 changes: 17 additions & 18 deletions tgfx/src/gpu/opengl/eagl/EAGLNV12Texture.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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> EAGLNV12Texture::MakeFrom(Context* context,
Expand All @@ -44,31 +45,29 @@ static GLTextureInfo ToGLTexture(CVOpenGLESTextureRef texture, unsigned format)
auto height = static_cast<int>(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;
}

Expand Down
8 changes: 4 additions & 4 deletions tgfx/src/gpu/opengl/eagl/EAGLWindow.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(width), static_cast<int>(height));
return Surface::MakeFrom(context, renderTarget, ImageOrigin::BottomLeft);
glInfo.format = PixelFormat::RGBA_8888;
auto renderTarget = GLRenderTarget::MakeFrom(context, glInfo, static_cast<int>(width), static_cast<int>(height), ImageOrigin::BottomLeft);
return Surface::MakeFrom(context, renderTarget);
}

void EAGLWindow::onPresent(Context* context, int64_t) {
Expand Down
26 changes: 13 additions & 13 deletions tgfx/src/gpu/opengl/egl/EGLHardwareTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,25 @@ std::shared_ptr<EGLHardwareTexture> 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;
}

Expand All @@ -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<EGLDevice*>(context->device())->getDisplay();
eglext::eglDestroyImageKHR(display, eglImage);
}
Expand Down
15 changes: 8 additions & 7 deletions tgfx/src/gpu/opengl/qt/QGLWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(ceil(quickItem->width()));
auto height = static_cast<int>(ceil(quickItem->height()));
outTexture = nativeWindow->createTextureFromId(textureID, QSize(width, height),
Expand All @@ -97,10 +97,11 @@ std::shared_ptr<Surface> 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<GLTexture>(Texture::MakeRGBA(context, width, height));
backTexture = std::static_pointer_cast<GLTexture>(Texture::MakeRGBA(context, width, height));
auto surface = Surface::MakeFrom(context, backTexture);
renderTarget = std::static_pointer_cast<GLRenderTarget>(surface->getRenderTarget());
return surface;
}

void QGLWindow::onPresent(Context* context, int64_t) {
Expand All @@ -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();
}
Expand Down

0 comments on commit 8c84fba

Please sign in to comment.