From 4ed1c01add8f7a2f569082f9d01e765ee5bb5b1e Mon Sep 17 00:00:00 2001 From: domrjchen Date: Sat, 26 Feb 2022 19:45:51 +0800 Subject: [PATCH 1/6] Move GLDefines.h and GLFunctions.h to tgfx/include. --- src/platform/android/VideoSurface.cpp | 10 +- src/rendering/filters/BulgeFilter.cpp | 23 +- src/rendering/filters/CornerPinFilter.cpp | 21 +- .../filters/DisplacementMapFilter.cpp | 40 +-- src/rendering/filters/LayerFilter.cpp | 71 +++--- .../filters/LevelsIndividualFilter.cpp | 121 +++++---- src/rendering/filters/MosaicFilter.cpp | 12 +- src/rendering/filters/MotionBlurFilter.cpp | 16 +- src/rendering/filters/MotionTileFilter.cpp | 36 +-- src/rendering/filters/RadialBlurFilter.cpp | 10 +- .../filters/dropshadow/DropShadowFilter.cpp | 4 +- .../dropshadow/DropShadowSpreadFilter.cpp | 14 +- .../filters/gaussblur/GaussBlurFilter.cpp | 2 +- .../gaussblur/SinglePassBlurFilter.cpp | 32 +-- src/rendering/filters/glow/GlowBlurFilter.cpp | 8 +- src/rendering/filters/glow/GlowFilter.cpp | 2 +- .../filters/glow/GlowMergeFilter.cpp | 14 +- src/rendering/renderers/FilterRenderer.cpp | 2 +- test/PAGBlendTest.cpp | 24 +- test/PAGReadPixelsTest.cpp | 4 +- test/PAGSurfaceTest.cpp | 8 +- tgfx/{src => include}/gpu/opengl/GLDefines.h | 0 .../{src => include}/gpu/opengl/GLFunctions.h | 222 +++++++++++++++- tgfx/src/core/vectors/web/WebMask.cpp | 4 +- .../gpu/opengl/GLAssembledGLESInterface.cpp | 57 ++--- .../src/gpu/opengl/GLAssembledGLESInterface.h | 4 +- .../src/gpu/opengl/GLAssembledGLInterface.cpp | 45 ++-- tgfx/src/gpu/opengl/GLAssembledGLInterface.h | 4 +- .../gpu/opengl/GLAssembledWebGLInterface.cpp | 33 +-- .../gpu/opengl/GLAssembledWebGLInterface.h | 4 +- tgfx/src/gpu/opengl/GLBlend.cpp | 2 +- tgfx/src/gpu/opengl/GLBuffer.cpp | 16 +- tgfx/src/gpu/opengl/GLCanvas.cpp | 2 +- tgfx/src/gpu/opengl/GLCaps.h | 6 +- tgfx/src/gpu/opengl/GLContext.h | 9 +- tgfx/src/gpu/opengl/GLDrawer.cpp | 86 ++++--- .../gpu/opengl/GLFragmentShaderBuilder.cpp | 2 +- tgfx/src/gpu/opengl/GLFunctions.cpp | 26 ++ tgfx/src/gpu/opengl/GLInterface.cpp | 240 ++++++++++++------ tgfx/src/gpu/opengl/GLInterface.h | 101 +------- tgfx/src/gpu/opengl/GLProgram.cpp | 8 +- tgfx/src/gpu/opengl/GLProgramBuilder.cpp | 2 +- tgfx/src/gpu/opengl/GLProgramCreator.cpp | 2 +- tgfx/src/gpu/opengl/GLProgramDataManager.cpp | 8 +- tgfx/src/gpu/opengl/GLRenderTarget.cpp | 103 ++++---- tgfx/src/gpu/opengl/GLSampler.cpp | 2 +- tgfx/src/gpu/opengl/GLState.cpp | 222 ++++++++-------- tgfx/src/gpu/opengl/GLSurface.cpp | 16 +- tgfx/src/gpu/opengl/GLTexture.cpp | 32 +-- tgfx/src/gpu/opengl/GLUniformHandler.cpp | 10 +- tgfx/src/gpu/opengl/GLUtil.cpp | 103 ++++---- tgfx/src/gpu/opengl/GLYUVTexture.cpp | 10 +- .../gpu/opengl/eagl/EAGLHardwareTexture.mm | 2 +- tgfx/src/gpu/opengl/eagl/EAGLNV12Texture.mm | 2 +- tgfx/src/gpu/opengl/eagl/EAGLWindow.mm | 36 +-- tgfx/src/gpu/opengl/qt/QGLWindow.cpp | 12 +- tgfx/src/platform/web/NativeTextureBuffer.cpp | 4 +- 57 files changed, 1069 insertions(+), 842 deletions(-) rename tgfx/{src => include}/gpu/opengl/GLDefines.h (100%) rename tgfx/{src => include}/gpu/opengl/GLFunctions.h (50%) create mode 100644 tgfx/src/gpu/opengl/GLFunctions.cpp diff --git a/src/platform/android/VideoSurface.cpp b/src/platform/android/VideoSurface.cpp index e2cccc171a..eb7b06b01f 100644 --- a/src/platform/android/VideoSurface.cpp +++ b/src/platform/android/VideoSurface.cpp @@ -86,8 +86,8 @@ tgfx::Point OESTexture::getTextureCoord(float x, float y) const { void OESTexture::onRelease(tgfx::Context* context) { if (sampler.id > 0) { - auto gl = tgfx::GLContext::Unwrap(context); - gl->deleteTextures(1, &sampler.id); + auto gl = tgfx::GLInterface::Get(context); + gl->functions->deleteTextures(1, &sampler.id); } } @@ -162,18 +162,18 @@ bool VideoSurface::attachToContext(JNIEnv* env, tgfx::Context* context) { } return true; } - auto gl = tgfx::GLContext::Unwrap(context); + auto gl = tgfx::GLInterface::Get(context); tgfx::GLSampler sampler = {}; sampler.target = GL_TEXTURE_EXTERNAL_OES; sampler.format = tgfx::PixelFormat::RGBA_8888; - gl->genTextures(1, &sampler.id); + gl->functions->genTextures(1, &sampler.id); if (sampler.id == 0) { return false; } auto result = env->CallBooleanMethod(videoSurface.get(), VideoSurface_attachToGLContext, sampler.id); if (!result) { - gl->deleteTextures(1, &sampler.id); + gl->functions->deleteTextures(1, &sampler.id); LOGE("VideoSurface::attachToGLContext(): failed to attached to a Surface!"); return false; } diff --git a/src/rendering/filters/BulgeFilter.cpp b/src/rendering/filters/BulgeFilter.cpp index a26fb0d27c..5375c212df 100644 --- a/src/rendering/filters/BulgeFilter.cpp +++ b/src/rendering/filters/BulgeFilter.cpp @@ -92,11 +92,11 @@ std::string BulgeFilter::onBuildFragmentShader() { } void BulgeFilter::onPrepareProgram(const tgfx::GLInterface* gl, unsigned int program) { - horizontalRadiusHandle = gl->getUniformLocation(program, "uHorizontalRadius"); - verticalRadiusHandle = gl->getUniformLocation(program, "uVerticalRadius"); - bulgeCenterHandle = gl->getUniformLocation(program, "uBulgeCenter"); - bulgeHeightHandle = gl->getUniformLocation(program, "uBulgeHeight"); - pinningHandle = gl->getUniformLocation(program, "uPinning"); + horizontalRadiusHandle = gl->functions->getUniformLocation(program, "uHorizontalRadius"); + verticalRadiusHandle = gl->functions->getUniformLocation(program, "uVerticalRadius"); + bulgeCenterHandle = gl->functions->getUniformLocation(program, "uBulgeCenter"); + bulgeHeightHandle = gl->functions->getUniformLocation(program, "uBulgeHeight"); + pinningHandle = gl->functions->getUniformLocation(program, "uPinning"); } void BulgeFilter::onUpdateParams(const tgfx::GLInterface* gl, const tgfx::Rect& contentBounds, @@ -108,12 +108,13 @@ void BulgeFilter::onUpdateParams(const tgfx::GLInterface* gl, const tgfx::Rect& auto bulgeHeight = bulgeEffect->bulgeHeight->getValueAt(layerFrame); auto pinning = bulgeEffect->pinning->getValueAt(layerFrame); - gl->uniform1f(horizontalRadiusHandle, horizontalRadius / contentBounds.width()); - gl->uniform1f(verticalRadiusHandle, verticalRadius / contentBounds.height()); - gl->uniform2f(bulgeCenterHandle, (bulgeCenter.x - contentBounds.x()) / contentBounds.width(), - 1.0f - (bulgeCenter.y - contentBounds.y()) / contentBounds.height()); - gl->uniform1f(bulgeHeightHandle, bulgeHeight); - gl->uniform1i(pinningHandle, pinning); + gl->functions->uniform1f(horizontalRadiusHandle, horizontalRadius / contentBounds.width()); + gl->functions->uniform1f(verticalRadiusHandle, verticalRadius / contentBounds.height()); + gl->functions->uniform2f(bulgeCenterHandle, + (bulgeCenter.x - contentBounds.x()) / contentBounds.width(), + 1.0f - (bulgeCenter.y - contentBounds.y()) / contentBounds.height()); + gl->functions->uniform1f(bulgeHeightHandle, bulgeHeight); + gl->functions->uniform1i(pinningHandle, pinning); } std::vector BulgeFilter::computeVertices(const tgfx::Rect& inputBounds, diff --git a/src/rendering/filters/CornerPinFilter.cpp b/src/rendering/filters/CornerPinFilter.cpp index 61b560b81c..5da7eb6680 100644 --- a/src/rendering/filters/CornerPinFilter.cpp +++ b/src/rendering/filters/CornerPinFilter.cpp @@ -141,17 +141,18 @@ void CornerPinFilter::bindVertices(const tgfx::GLInterface* gl, const FilterSour } if (filterProgram->vertexArray > 0) { - gl->bindVertexArray(filterProgram->vertexArray); + gl->functions->bindVertexArray(filterProgram->vertexArray); } - gl->bindBuffer(GL_ARRAY_BUFFER, filterProgram->vertexBuffer); - gl->bufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), &vertices[0], GL_STREAM_DRAW); - gl->vertexAttribPointer(static_cast(positionHandle), 2, GL_FLOAT, GL_FALSE, - 5 * sizeof(float), static_cast(0)); - gl->enableVertexAttribArray(static_cast(positionHandle)); + gl->functions->bindBuffer(GL_ARRAY_BUFFER, filterProgram->vertexBuffer); + gl->functions->bufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), &vertices[0], + GL_STREAM_DRAW); + gl->functions->vertexAttribPointer(static_cast(positionHandle), 2, GL_FLOAT, GL_FALSE, + 5 * sizeof(float), static_cast(0)); + gl->functions->enableVertexAttribArray(static_cast(positionHandle)); - gl->vertexAttribPointer(textureCoordHandle, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), - reinterpret_cast(2 * sizeof(float))); - gl->enableVertexAttribArray(textureCoordHandle); - gl->bindBuffer(GL_ARRAY_BUFFER, 0); + gl->functions->vertexAttribPointer(textureCoordHandle, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), + reinterpret_cast(2 * sizeof(float))); + gl->functions->enableVertexAttribArray(textureCoordHandle); + gl->functions->bindBuffer(GL_ARRAY_BUFFER, 0); } } // namespace pag diff --git a/src/rendering/filters/DisplacementMapFilter.cpp b/src/rendering/filters/DisplacementMapFilter.cpp index b82130185e..550ad0ad52 100644 --- a/src/rendering/filters/DisplacementMapFilter.cpp +++ b/src/rendering/filters/DisplacementMapFilter.cpp @@ -97,13 +97,14 @@ std::string DisplacementMapFilter::onBuildFragmentShader() { } void DisplacementMapFilter::onPrepareProgram(const tgfx::GLInterface* gl, unsigned int program) { - useForDisplacementHandle = gl->getUniformLocation(program, "uUseForDisplacement"); - maxDisplacementHandle = gl->getUniformLocation(program, "uMaxDisplacement"); - displacementMapBehaviorHandle = gl->getUniformLocation(program, "uDisplacementMapBehavior"); - edgeBehaviorHandle = gl->getUniformLocation(program, "uEdgeBehavior"); - expandOutputHandle = gl->getUniformLocation(program, "uExpandOutput"); - mapTextureHandle = gl->getUniformLocation(program, "mapTexture"); - mapTextureSizeHandle = gl->getUniformLocation(program, "mapTextureSize"); + useForDisplacementHandle = gl->functions->getUniformLocation(program, "uUseForDisplacement"); + maxDisplacementHandle = gl->functions->getUniformLocation(program, "uMaxDisplacement"); + displacementMapBehaviorHandle = + gl->functions->getUniformLocation(program, "uDisplacementMapBehavior"); + edgeBehaviorHandle = gl->functions->getUniformLocation(program, "uEdgeBehavior"); + expandOutputHandle = gl->functions->getUniformLocation(program, "uExpandOutput"); + mapTextureHandle = gl->functions->getUniformLocation(program, "mapTexture"); + mapTextureSizeHandle = gl->functions->getUniformLocation(program, "mapTextureSize"); } void DisplacementMapFilter::updateMapTexture(RenderCache* cache, const Graphic* mapGraphic, @@ -122,17 +123,18 @@ void DisplacementMapFilter::onUpdateParams(const tgfx::GLInterface* gl, auto* pagEffect = reinterpret_cast(effect); auto mapTextureID = GetTextureID(mapSurface->getTexture().get()); ActiveGLTexture(gl, GL_TEXTURE1, GL_TEXTURE_2D, mapTextureID); - gl->uniform2f(useForDisplacementHandle, - pagEffect->useForHorizontalDisplacement->getValueAt(layerFrame), - pagEffect->useForVerticalDisplacement->getValueAt(layerFrame)); - gl->uniform2f(maxDisplacementHandle, pagEffect->maxHorizontalDisplacement->getValueAt(layerFrame), - pagEffect->maxVerticalDisplacement->getValueAt(layerFrame)); - gl->uniform1i(displacementMapBehaviorHandle, - pagEffect->displacementMapBehavior->getValueAt(layerFrame)); - gl->uniform1i(edgeBehaviorHandle, pagEffect->edgeBehavior->getValueAt(layerFrame)); - gl->uniform1i(expandOutputHandle, pagEffect->expandOutput->getValueAt(layerFrame)); - gl->uniform1i(mapTextureHandle, 1); - gl->uniform2f(mapTextureSizeHandle, mapBounds.width() / contentBounds.width(), - mapBounds.height() / contentBounds.height()); + gl->functions->uniform2f(useForDisplacementHandle, + pagEffect->useForHorizontalDisplacement->getValueAt(layerFrame), + pagEffect->useForVerticalDisplacement->getValueAt(layerFrame)); + gl->functions->uniform2f(maxDisplacementHandle, + pagEffect->maxHorizontalDisplacement->getValueAt(layerFrame), + pagEffect->maxVerticalDisplacement->getValueAt(layerFrame)); + gl->functions->uniform1i(displacementMapBehaviorHandle, + pagEffect->displacementMapBehavior->getValueAt(layerFrame)); + gl->functions->uniform1i(edgeBehaviorHandle, pagEffect->edgeBehavior->getValueAt(layerFrame)); + gl->functions->uniform1i(expandOutputHandle, pagEffect->expandOutput->getValueAt(layerFrame)); + gl->functions->uniform1i(mapTextureHandle, 1); + gl->functions->uniform2f(mapTextureSizeHandle, mapBounds.width() / contentBounds.width(), + mapBounds.height() / contentBounds.height()); } } // namespace pag diff --git a/src/rendering/filters/LayerFilter.cpp b/src/rendering/filters/LayerFilter.cpp index 8a10551c83..124c0fe6f8 100644 --- a/src/rendering/filters/LayerFilter.cpp +++ b/src/rendering/filters/LayerFilter.cpp @@ -80,7 +80,7 @@ std::vector ComputeVerticesForMotionBlurAndBulge(const tgfx::Rect& std::shared_ptr FilterProgram::Make(tgfx::Context* context, const std::string& vertex, const std::string& fragment) { - auto gl = tgfx::GLContext::Unwrap(context); + auto gl = tgfx::GLInterface::Get(context); auto program = tgfx::CreateGLProgram(gl, vertex, fragment); if (program == 0) { return nullptr; @@ -88,24 +88,24 @@ std::shared_ptr FilterProgram::Make(tgfx::Context* context, auto filterProgram = new FilterProgram(); filterProgram->program = program; if (gl->caps->vertexArrayObjectSupport) { - gl->genVertexArrays(1, &filterProgram->vertexArray); + gl->functions->genVertexArrays(1, &filterProgram->vertexArray); } - gl->genBuffers(1, &filterProgram->vertexBuffer); + gl->functions->genBuffers(1, &filterProgram->vertexBuffer); return Resource::Wrap(context, filterProgram); } void FilterProgram::onRelease(tgfx::Context* context) { - auto gl = tgfx::GLContext::Unwrap(context); + auto gl = tgfx::GLInterface::Get(context); if (program > 0) { - gl->deleteProgram(program); + gl->functions->deleteProgram(program); program = 0; } if (vertexArray > 0) { - gl->deleteVertexArrays(1, &vertexArray); + gl->functions->deleteVertexArrays(1, &vertexArray); vertexArray = 0; } if (vertexBuffer > 0) { - gl->deleteBuffers(1, &vertexBuffer); + gl->functions->deleteBuffers(1, &vertexBuffer); vertexBuffer = 0; } } @@ -158,7 +158,7 @@ std::unique_ptr LayerFilter::Make(Effect* effect) { } bool LayerFilter::initialize(tgfx::Context* context) { - auto gl = tgfx::GLContext::Unwrap(context); + auto gl = tgfx::GLInterface::Get(context); // 防止前面产生的GLError,导致后面CheckGLError逻辑返回错误结果 CheckGLError(gl); @@ -169,10 +169,10 @@ bool LayerFilter::initialize(tgfx::Context* context) { return false; } auto program = filterProgram->program; - positionHandle = gl->getAttribLocation(program, "aPosition"); - textureCoordHandle = gl->getAttribLocation(program, "aTextureCoord"); - vertexMatrixHandle = gl->getUniformLocation(program, "uVertexMatrix"); - textureMatrixHandle = gl->getUniformLocation(program, "uTextureMatrix"); + positionHandle = gl->functions->getAttribLocation(program, "aPosition"); + textureCoordHandle = gl->functions->getAttribLocation(program, "aTextureCoord"); + vertexMatrixHandle = gl->functions->getUniformLocation(program, "uVertexMatrix"); + textureMatrixHandle = gl->functions->getUniformLocation(program, "uTextureMatrix"); onPrepareProgram(gl, program); if (!CheckGLError(gl)) { filterProgram = nullptr; @@ -205,13 +205,13 @@ void LayerFilter::update(Frame frame, const tgfx::Rect& inputBounds, const tgfx: static void EnableMultisample(const tgfx::GLInterface* gl, bool usesMSAA) { if (usesMSAA && gl->caps->multisampleDisableSupport) { - gl->enable(GL_MULTISAMPLE); + gl->functions->enable(GL_MULTISAMPLE); } } static void DisableMultisample(const tgfx::GLInterface* gl, bool usesMSAA) { if (usesMSAA && gl->caps->multisampleDisableSupport) { - gl->disable(GL_MULTISAMPLE); + gl->functions->disable(GL_MULTISAMPLE); } } @@ -223,24 +223,24 @@ void LayerFilter::draw(tgfx::Context* context, const FilterSource* source, "because the argument(source/target) is null"); return; } - auto gl = tgfx::GLContext::Unwrap(context); + auto gl = tgfx::GLInterface::Get(context); EnableMultisample(gl, needsMSAA()); - gl->useProgram(filterProgram->program); - gl->enable(GL_BLEND); - gl->blendEquation(GL_FUNC_ADD); - gl->blendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); - gl->bindFramebuffer(GL_FRAMEBUFFER, target->frameBufferID); - gl->viewport(0, 0, target->width, target->height); + gl->functions->useProgram(filterProgram->program); + gl->functions->enable(GL_BLEND); + gl->functions->blendEquation(GL_FUNC_ADD); + gl->functions->blendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); + gl->functions->bindFramebuffer(GL_FRAMEBUFFER, target->frameBufferID); + gl->functions->viewport(0, 0, target->width, target->height); ActiveGLTexture(gl, GL_TEXTURE0, GL_TEXTURE_2D, source->textureID); - gl->uniformMatrix3fv(vertexMatrixHandle, 1, GL_FALSE, target->vertexMatrix.data()); - gl->uniformMatrix3fv(textureMatrixHandle, 1, GL_FALSE, source->textureMatrix.data()); + gl->functions->uniformMatrix3fv(vertexMatrixHandle, 1, GL_FALSE, target->vertexMatrix.data()); + gl->functions->uniformMatrix3fv(textureMatrixHandle, 1, GL_FALSE, source->textureMatrix.data()); onUpdateParams(gl, contentBounds, filterScale); auto vertices = computeVertices(contentBounds, transformedBounds, filterScale); bindVertices(gl, source, target, vertices); - gl->drawArrays(GL_TRIANGLE_STRIP, 0, 4); + gl->functions->drawArrays(GL_TRIANGLE_STRIP, 0, 4); if (filterProgram->vertexArray > 0) { - gl->bindVertexArray(0); + gl->functions->bindVertexArray(0); } DisableMultisample(gl, needsMSAA()); CheckGLError(gl); @@ -278,18 +278,19 @@ void LayerFilter::bindVertices(const tgfx::GLInterface* gl, const FilterSource* } if (filterProgram->vertexArray > 0) { - gl->bindVertexArray(filterProgram->vertexArray); + gl->functions->bindVertexArray(filterProgram->vertexArray); } - gl->bindBuffer(GL_ARRAY_BUFFER, filterProgram->vertexBuffer); - gl->bufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), &vertices[0], GL_STREAM_DRAW); - gl->vertexAttribPointer(static_cast(positionHandle), 2, GL_FLOAT, GL_FALSE, - 4 * sizeof(float), static_cast(0)); - gl->enableVertexAttribArray(static_cast(positionHandle)); + gl->functions->bindBuffer(GL_ARRAY_BUFFER, filterProgram->vertexBuffer); + gl->functions->bufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), &vertices[0], + GL_STREAM_DRAW); + gl->functions->vertexAttribPointer(static_cast(positionHandle), 2, GL_FLOAT, GL_FALSE, + 4 * sizeof(float), static_cast(0)); + gl->functions->enableVertexAttribArray(static_cast(positionHandle)); - gl->vertexAttribPointer(textureCoordHandle, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), - reinterpret_cast(2 * sizeof(float))); - gl->enableVertexAttribArray(textureCoordHandle); - gl->bindBuffer(GL_ARRAY_BUFFER, 0); + gl->functions->vertexAttribPointer(textureCoordHandle, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), + reinterpret_cast(2 * sizeof(float))); + gl->functions->enableVertexAttribArray(textureCoordHandle); + gl->functions->bindBuffer(GL_ARRAY_BUFFER, 0); } bool LayerFilter::needsMSAA() const { diff --git a/src/rendering/filters/LevelsIndividualFilter.cpp b/src/rendering/filters/LevelsIndividualFilter.cpp index 8b7ddc43c4..57d1d379b8 100644 --- a/src/rendering/filters/LevelsIndividualFilter.cpp +++ b/src/rendering/filters/LevelsIndividualFilter.cpp @@ -87,66 +87,75 @@ std::string LevelsIndividualFilter::onBuildFragmentShader() { } void LevelsIndividualFilter::onPrepareProgram(const tgfx::GLInterface* gl, unsigned int program) { - inputBlackHandle = gl->getUniformLocation(program, "inputBlack"); - inputWhiteHandle = gl->getUniformLocation(program, "inputWhite"); - gammaHandle = gl->getUniformLocation(program, "gamma"); - outputBlackHandle = gl->getUniformLocation(program, "outputBlack"); - outputWhiteHandle = gl->getUniformLocation(program, "outputWhite"); - - redInputBlackHandle = gl->getUniformLocation(program, "redInputBlack"); - redInputWhiteHandle = gl->getUniformLocation(program, "redInputWhite"); - redGammaHandle = gl->getUniformLocation(program, "redGamma"); - redOutputBlackHandle = gl->getUniformLocation(program, "redOutputBlack"); - redOutputWhiteHandle = gl->getUniformLocation(program, "redOutputWhite"); - - greenInputBlackHandle = gl->getUniformLocation(program, "greenInputBlack"); - greenInputWhiteHandle = gl->getUniformLocation(program, "greenInputWhite"); - greenGammaHandle = gl->getUniformLocation(program, "greenGamma"); - greenOutputBlackHandle = gl->getUniformLocation(program, "greenOutputBlack"); - greenOutputWhiteHandle = gl->getUniformLocation(program, "greenOutputWhite"); - - blueInputBlackHandle = gl->getUniformLocation(program, "blueInputBlack"); - blueInputWhiteHandle = gl->getUniformLocation(program, "blueInputWhite"); - blueGammaHandle = gl->getUniformLocation(program, "blueGamma"); - blueOutputBlackHandle = gl->getUniformLocation(program, "blueOutputBlack"); - blueOutputWhiteHandle = gl->getUniformLocation(program, "blueOutputWhite"); + inputBlackHandle = gl->functions->getUniformLocation(program, "inputBlack"); + inputWhiteHandle = gl->functions->getUniformLocation(program, "inputWhite"); + gammaHandle = gl->functions->getUniformLocation(program, "gamma"); + outputBlackHandle = gl->functions->getUniformLocation(program, "outputBlack"); + outputWhiteHandle = gl->functions->getUniformLocation(program, "outputWhite"); + + redInputBlackHandle = gl->functions->getUniformLocation(program, "redInputBlack"); + redInputWhiteHandle = gl->functions->getUniformLocation(program, "redInputWhite"); + redGammaHandle = gl->functions->getUniformLocation(program, "redGamma"); + redOutputBlackHandle = gl->functions->getUniformLocation(program, "redOutputBlack"); + redOutputWhiteHandle = gl->functions->getUniformLocation(program, "redOutputWhite"); + + greenInputBlackHandle = gl->functions->getUniformLocation(program, "greenInputBlack"); + greenInputWhiteHandle = gl->functions->getUniformLocation(program, "greenInputWhite"); + greenGammaHandle = gl->functions->getUniformLocation(program, "greenGamma"); + greenOutputBlackHandle = gl->functions->getUniformLocation(program, "greenOutputBlack"); + greenOutputWhiteHandle = gl->functions->getUniformLocation(program, "greenOutputWhite"); + + blueInputBlackHandle = gl->functions->getUniformLocation(program, "blueInputBlack"); + blueInputWhiteHandle = gl->functions->getUniformLocation(program, "blueInputWhite"); + blueGammaHandle = gl->functions->getUniformLocation(program, "blueGamma"); + blueOutputBlackHandle = gl->functions->getUniformLocation(program, "blueOutputBlack"); + blueOutputWhiteHandle = gl->functions->getUniformLocation(program, "blueOutputWhite"); } void LevelsIndividualFilter::onUpdateParams(const tgfx::GLInterface* gl, const tgfx::Rect&, const tgfx::Point&) { auto* levelsIndividualFilter = reinterpret_cast(effect); - gl->uniform1f(inputBlackHandle, levelsIndividualFilter->inputBlack->getValueAt(layerFrame)); - gl->uniform1f(inputWhiteHandle, levelsIndividualFilter->inputWhite->getValueAt(layerFrame)); - gl->uniform1f(gammaHandle, levelsIndividualFilter->gamma->getValueAt(layerFrame)); - gl->uniform1f(outputBlackHandle, levelsIndividualFilter->outputBlack->getValueAt(layerFrame)); - gl->uniform1f(outputWhiteHandle, levelsIndividualFilter->outputWhite->getValueAt(layerFrame)); - - gl->uniform1f(redInputBlackHandle, levelsIndividualFilter->redInputBlack->getValueAt(layerFrame)); - gl->uniform1f(redInputWhiteHandle, levelsIndividualFilter->redInputWhite->getValueAt(layerFrame)); - gl->uniform1f(redGammaHandle, levelsIndividualFilter->redGamma->getValueAt(layerFrame)); - gl->uniform1f(redOutputBlackHandle, - levelsIndividualFilter->redOutputBlack->getValueAt(layerFrame)); - gl->uniform1f(redOutputWhiteHandle, - levelsIndividualFilter->redOutputWhite->getValueAt(layerFrame)); - - gl->uniform1f(greenInputBlackHandle, - levelsIndividualFilter->greenInputBlack->getValueAt(layerFrame)); - gl->uniform1f(greenInputWhiteHandle, - levelsIndividualFilter->greenInputWhite->getValueAt(layerFrame)); - gl->uniform1f(greenGammaHandle, levelsIndividualFilter->greenGamma->getValueAt(layerFrame)); - gl->uniform1f(greenOutputBlackHandle, - levelsIndividualFilter->greenOutputBlack->getValueAt(layerFrame)); - gl->uniform1f(greenOutputWhiteHandle, - levelsIndividualFilter->greenOutputWhite->getValueAt(layerFrame)); - - gl->uniform1f(blueInputBlackHandle, - levelsIndividualFilter->blueInputBlack->getValueAt(layerFrame)); - gl->uniform1f(blueInputWhiteHandle, - levelsIndividualFilter->blueInputWhite->getValueAt(layerFrame)); - gl->uniform1f(blueGammaHandle, levelsIndividualFilter->blueGamma->getValueAt(layerFrame)); - gl->uniform1f(blueOutputBlackHandle, - levelsIndividualFilter->blueOutputBlack->getValueAt(layerFrame)); - gl->uniform1f(blueOutputWhiteHandle, - levelsIndividualFilter->blueOutputWhite->getValueAt(layerFrame)); + gl->functions->uniform1f(inputBlackHandle, + levelsIndividualFilter->inputBlack->getValueAt(layerFrame)); + gl->functions->uniform1f(inputWhiteHandle, + levelsIndividualFilter->inputWhite->getValueAt(layerFrame)); + gl->functions->uniform1f(gammaHandle, levelsIndividualFilter->gamma->getValueAt(layerFrame)); + gl->functions->uniform1f(outputBlackHandle, + levelsIndividualFilter->outputBlack->getValueAt(layerFrame)); + gl->functions->uniform1f(outputWhiteHandle, + levelsIndividualFilter->outputWhite->getValueAt(layerFrame)); + + gl->functions->uniform1f(redInputBlackHandle, + levelsIndividualFilter->redInputBlack->getValueAt(layerFrame)); + gl->functions->uniform1f(redInputWhiteHandle, + levelsIndividualFilter->redInputWhite->getValueAt(layerFrame)); + gl->functions->uniform1f(redGammaHandle, + levelsIndividualFilter->redGamma->getValueAt(layerFrame)); + gl->functions->uniform1f(redOutputBlackHandle, + levelsIndividualFilter->redOutputBlack->getValueAt(layerFrame)); + gl->functions->uniform1f(redOutputWhiteHandle, + levelsIndividualFilter->redOutputWhite->getValueAt(layerFrame)); + + gl->functions->uniform1f(greenInputBlackHandle, + levelsIndividualFilter->greenInputBlack->getValueAt(layerFrame)); + gl->functions->uniform1f(greenInputWhiteHandle, + levelsIndividualFilter->greenInputWhite->getValueAt(layerFrame)); + gl->functions->uniform1f(greenGammaHandle, + levelsIndividualFilter->greenGamma->getValueAt(layerFrame)); + gl->functions->uniform1f(greenOutputBlackHandle, + levelsIndividualFilter->greenOutputBlack->getValueAt(layerFrame)); + gl->functions->uniform1f(greenOutputWhiteHandle, + levelsIndividualFilter->greenOutputWhite->getValueAt(layerFrame)); + + gl->functions->uniform1f(blueInputBlackHandle, + levelsIndividualFilter->blueInputBlack->getValueAt(layerFrame)); + gl->functions->uniform1f(blueInputWhiteHandle, + levelsIndividualFilter->blueInputWhite->getValueAt(layerFrame)); + gl->functions->uniform1f(blueGammaHandle, + levelsIndividualFilter->blueGamma->getValueAt(layerFrame)); + gl->functions->uniform1f(blueOutputBlackHandle, + levelsIndividualFilter->blueOutputBlack->getValueAt(layerFrame)); + gl->functions->uniform1f(blueOutputWhiteHandle, + levelsIndividualFilter->blueOutputWhite->getValueAt(layerFrame)); } } // namespace pag diff --git a/src/rendering/filters/MosaicFilter.cpp b/src/rendering/filters/MosaicFilter.cpp index b65f241c6e..a78f8fbe43 100644 --- a/src/rendering/filters/MosaicFilter.cpp +++ b/src/rendering/filters/MosaicFilter.cpp @@ -44,9 +44,9 @@ std::string MosaicFilter::onBuildFragmentShader() { } void MosaicFilter::onPrepareProgram(const tgfx::GLInterface* gl, unsigned int program) { - horizontalBlocksHandle = gl->getUniformLocation(program, "mHorizontalBlocks"); - verticalBlocksHandle = gl->getUniformLocation(program, "mVerticalBlocks"); - sharpColorsHandle = gl->getUniformLocation(program, "mSharpColors"); + horizontalBlocksHandle = gl->functions->getUniformLocation(program, "mHorizontalBlocks"); + verticalBlocksHandle = gl->functions->getUniformLocation(program, "mVerticalBlocks"); + sharpColorsHandle = gl->functions->getUniformLocation(program, "mSharpColors"); } void MosaicFilter::onUpdateParams(const tgfx::GLInterface* gl, const tgfx::Rect& contentBounds, @@ -70,8 +70,8 @@ void MosaicFilter::onUpdateParams(const tgfx::GLInterface* gl, const tgfx::Rect& verticalBlocks *= 1.0f * placeHolderHeight / contentHeight; } - gl->uniform1f(horizontalBlocksHandle, horizontalBlocks); - gl->uniform1f(verticalBlocksHandle, verticalBlocks); - gl->uniform1f(sharpColorsHandle, sharpColors); + gl->functions->uniform1f(horizontalBlocksHandle, horizontalBlocks); + gl->functions->uniform1f(verticalBlocksHandle, verticalBlocks); + gl->functions->uniform1f(sharpColorsHandle, sharpColors); } } // namespace pag diff --git a/src/rendering/filters/MotionBlurFilter.cpp b/src/rendering/filters/MotionBlurFilter.cpp index d6ee92d94b..7c9a213038 100644 --- a/src/rendering/filters/MotionBlurFilter.cpp +++ b/src/rendering/filters/MotionBlurFilter.cpp @@ -108,10 +108,10 @@ std::string MotionBlurFilter::onBuildFragmentShader() { } void MotionBlurFilter::onPrepareProgram(const tgfx::GLInterface* gl, unsigned int program) { - prevTransformHandle = gl->getUniformLocation(program, "uPrevTransform"); - transformHandle = gl->getUniformLocation(program, "uTransform"); - velCenterHandle = gl->getUniformLocation(program, "uVelCenter"); - maxDistanceHandle = gl->getUniformLocation(program, "maxDistance"); + prevTransformHandle = gl->functions->getUniformLocation(program, "uPrevTransform"); + transformHandle = gl->functions->getUniformLocation(program, "uTransform"); + velCenterHandle = gl->functions->getUniformLocation(program, "uVelCenter"); + maxDistanceHandle = gl->functions->getUniformLocation(program, "maxDistance"); } bool MotionBlurFilter::updateLayer(Layer* targetLayer, Frame layerFrame) { @@ -138,10 +138,10 @@ void MotionBlurFilter::onUpdateParams(const tgfx::GLInterface* gl, const tgfx::R auto scaling = (previousMatrix.getScaleX() != currentMatrix.getScaleX() || previousMatrix.getScaleY() != currentMatrix.getScaleY()); - gl->uniformMatrix3fv(prevTransformHandle, 1, GL_FALSE, previousGLMatrix.data()); - gl->uniformMatrix3fv(transformHandle, 1, GL_FALSE, currentGLMatrix.data()); - gl->uniform1f(velCenterHandle, scaling ? 0.0f : 0.5f); - gl->uniform1f(maxDistanceHandle, (MOTION_BLUR_SCALE_FACTOR - 1.0) * 0.5f); + gl->functions->uniformMatrix3fv(prevTransformHandle, 1, GL_FALSE, previousGLMatrix.data()); + gl->functions->uniformMatrix3fv(transformHandle, 1, GL_FALSE, currentGLMatrix.data()); + gl->functions->uniform1f(velCenterHandle, scaling ? 0.0f : 0.5f); + gl->functions->uniform1f(maxDistanceHandle, (MOTION_BLUR_SCALE_FACTOR - 1.0) * 0.5f); } std::vector MotionBlurFilter::computeVertices(const tgfx::Rect& inputBounds, diff --git a/src/rendering/filters/MotionTileFilter.cpp b/src/rendering/filters/MotionTileFilter.cpp index e66954d9bb..e0dc320b0d 100644 --- a/src/rendering/filters/MotionTileFilter.cpp +++ b/src/rendering/filters/MotionTileFilter.cpp @@ -99,14 +99,15 @@ std::string MotionTileFilter::onBuildFragmentShader() { } void MotionTileFilter::onPrepareProgram(const tgfx::GLInterface* gl, unsigned int program) { - tileCenterHandle = gl->getUniformLocation(program, "uTileCenter"); - tileWidthHandle = gl->getUniformLocation(program, "uTileWidth"); - tileHeightHandle = gl->getUniformLocation(program, "uTileHeight"); - outputWidthHandle = gl->getUniformLocation(program, "uOutputWidth"); - outputHeightHandle = gl->getUniformLocation(program, "uOutputHeight"); - mirrorEdgesHandle = gl->getUniformLocation(program, "uMirrorEdges"); - phaseHandle = gl->getUniformLocation(program, "uPhase"); - isHorizontalPhaseShiftHandle = gl->getUniformLocation(program, "uIsHorizontalPhaseShift"); + tileCenterHandle = gl->functions->getUniformLocation(program, "uTileCenter"); + tileWidthHandle = gl->functions->getUniformLocation(program, "uTileWidth"); + tileHeightHandle = gl->functions->getUniformLocation(program, "uTileHeight"); + outputWidthHandle = gl->functions->getUniformLocation(program, "uOutputWidth"); + outputHeightHandle = gl->functions->getUniformLocation(program, "uOutputHeight"); + mirrorEdgesHandle = gl->functions->getUniformLocation(program, "uMirrorEdges"); + phaseHandle = gl->functions->getUniformLocation(program, "uPhase"); + isHorizontalPhaseShiftHandle = + gl->functions->getUniformLocation(program, "uIsHorizontalPhaseShift"); } void MotionTileFilter::onUpdateParams(const tgfx::GLInterface* gl, const tgfx::Rect& contentBounds, @@ -121,14 +122,15 @@ void MotionTileFilter::onUpdateParams(const tgfx::GLInterface* gl, const tgfx::R auto phase = pagEffect->phase->getValueAt(layerFrame); auto isHorizontalPhaseShift = pagEffect->horizontalPhaseShift->getValueAt(layerFrame); - gl->uniform2f(tileCenterHandle, (tileCenter.x - contentBounds.x()) / contentBounds.width(), - 1.0f - (tileCenter.y - contentBounds.y()) / contentBounds.height()); - gl->uniform1f(tileWidthHandle, tileWidth / 100.f); - gl->uniform1f(tileHeightHandle, tileHeight / 100.f); - gl->uniform1f(outputWidthHandle, outputWidth / 100.f); - gl->uniform1f(outputHeightHandle, outputHeight / 100.f); - gl->uniform1i(mirrorEdgesHandle, mirrorEdges); - gl->uniform1f(phaseHandle, phase); - gl->uniform1i(isHorizontalPhaseShiftHandle, isHorizontalPhaseShift); + gl->functions->uniform2f(tileCenterHandle, + (tileCenter.x - contentBounds.x()) / contentBounds.width(), + 1.0f - (tileCenter.y - contentBounds.y()) / contentBounds.height()); + gl->functions->uniform1f(tileWidthHandle, tileWidth / 100.f); + gl->functions->uniform1f(tileHeightHandle, tileHeight / 100.f); + gl->functions->uniform1f(outputWidthHandle, outputWidth / 100.f); + gl->functions->uniform1f(outputHeightHandle, outputHeight / 100.f); + gl->functions->uniform1i(mirrorEdgesHandle, mirrorEdges); + gl->functions->uniform1f(phaseHandle, phase); + gl->functions->uniform1i(isHorizontalPhaseShiftHandle, isHorizontalPhaseShift); } } // namespace pag diff --git a/src/rendering/filters/RadialBlurFilter.cpp b/src/rendering/filters/RadialBlurFilter.cpp index ed041eea2c..91e9b674ed 100644 --- a/src/rendering/filters/RadialBlurFilter.cpp +++ b/src/rendering/filters/RadialBlurFilter.cpp @@ -62,8 +62,8 @@ std::string RadialBlurFilter::onBuildFragmentShader() { } void RadialBlurFilter::onPrepareProgram(const tgfx::GLInterface* gl, unsigned int program) { - amountHandle = gl->getUniformLocation(program, "uAmount"); - centerHandle = gl->getUniformLocation(program, "uCenter"); + amountHandle = gl->functions->getUniformLocation(program, "uAmount"); + centerHandle = gl->functions->getUniformLocation(program, "uCenter"); } void RadialBlurFilter::onUpdateParams(const tgfx::GLInterface* gl, const tgfx::Rect& contentBounds, @@ -74,8 +74,8 @@ void RadialBlurFilter::onUpdateParams(const tgfx::GLInterface* gl, const tgfx::R amount = amount < 0.25 ? amount : 0.25; - gl->uniform1f(amountHandle, amount); - gl->uniform2f(centerHandle, (center.x - contentBounds.x()) / contentBounds.width(), - (center.y - contentBounds.y()) / contentBounds.height()); + gl->functions->uniform1f(amountHandle, amount); + gl->functions->uniform2f(centerHandle, (center.x - contentBounds.x()) / contentBounds.width(), + (center.y - contentBounds.y()) / contentBounds.height()); } } // namespace pag diff --git a/src/rendering/filters/dropshadow/DropShadowFilter.cpp b/src/rendering/filters/dropshadow/DropShadowFilter.cpp index 551583fc1d..150e2b900e 100644 --- a/src/rendering/filters/dropshadow/DropShadowFilter.cpp +++ b/src/rendering/filters/dropshadow/DropShadowFilter.cpp @@ -176,7 +176,7 @@ void DropShadowFilter::onDrawModeNotSpread(tgfx::Context* context, const FilterS if (blurFilterBuffer == nullptr) { return; } - auto gl = tgfx::GLContext::Unwrap(context); + auto gl = tgfx::GLInterface::Get(context); blurFilterBuffer->clearColor(gl); auto offsetMatrix = @@ -215,7 +215,7 @@ void DropShadowFilter::onDrawModeNotFullSpread(tgfx::Context* context, const Fil if (spreadFilterBuffer == nullptr) { return; } - auto gl = tgfx::GLContext::Unwrap(context); + auto gl = tgfx::GLInterface::Get(context); spreadFilterBuffer->clearColor(gl); auto offsetMatrix = tgfx::Matrix::MakeTrans((lastBounds.left - filterBounds.left) * source->scale.x, diff --git a/src/rendering/filters/dropshadow/DropShadowSpreadFilter.cpp b/src/rendering/filters/dropshadow/DropShadowSpreadFilter.cpp index cd686336ae..37b900b4b1 100644 --- a/src/rendering/filters/dropshadow/DropShadowSpreadFilter.cpp +++ b/src/rendering/filters/dropshadow/DropShadowSpreadFilter.cpp @@ -89,9 +89,9 @@ std::string DropShadowSpreadFilter::onBuildFragmentShader() { } void DropShadowSpreadFilter::onPrepareProgram(const tgfx::GLInterface* gl, unsigned program) { - spreadColorHandle = gl->getUniformLocation(program, "uColor"); - spreadAlphaHandle = gl->getUniformLocation(program, "uAlpha"); - spreadSizeHandle = gl->getUniformLocation(program, "uSize"); + spreadColorHandle = gl->functions->getUniformLocation(program, "uColor"); + spreadAlphaHandle = gl->functions->getUniformLocation(program, "uAlpha"); + spreadSizeHandle = gl->functions->getUniformLocation(program, "uSize"); } void DropShadowSpreadFilter::onUpdateParams(const tgfx::GLInterface* gl, @@ -108,10 +108,10 @@ void DropShadowSpreadFilter::onUpdateParams(const tgfx::GLInterface* gl, spreadSizeX = std::min(spreadSizeX, DROPSHADOW_MAX_SPREAD_SIZE); spreadSizeY = std::min(spreadSizeY, DROPSHADOW_MAX_SPREAD_SIZE); - gl->uniform3f(spreadColorHandle, color.red, color.green, color.blue); - gl->uniform1f(spreadAlphaHandle, alpha); - gl->uniform2f(spreadSizeHandle, spreadSizeX / contentBounds.width(), - spreadSizeY / contentBounds.height()); + gl->functions->uniform3f(spreadColorHandle, color.red, color.green, color.blue); + gl->functions->uniform1f(spreadAlphaHandle, alpha); + gl->functions->uniform2f(spreadSizeHandle, spreadSizeX / contentBounds.width(), + spreadSizeY / contentBounds.height()); } std::vector DropShadowSpreadFilter::computeVertices(const tgfx::Rect&, diff --git a/src/rendering/filters/gaussblur/GaussBlurFilter.cpp b/src/rendering/filters/gaussblur/GaussBlurFilter.cpp index 3948e935db..377dfefb99 100644 --- a/src/rendering/filters/gaussblur/GaussBlurFilter.cpp +++ b/src/rendering/filters/gaussblur/GaussBlurFilter.cpp @@ -101,7 +101,7 @@ void GaussBlurFilter::draw(tgfx::Context* context, const FilterSource* source, if (blurFilterBuffer == nullptr) { return; } - auto gl = tgfx::GLContext::Unwrap(context); + auto gl = tgfx::GLInterface::Get(context); blurFilterBuffer->clearColor(gl); auto offsetMatrix = diff --git a/src/rendering/filters/gaussblur/SinglePassBlurFilter.cpp b/src/rendering/filters/gaussblur/SinglePassBlurFilter.cpp index 0150dcaf85..dc9a67ba5f 100644 --- a/src/rendering/filters/gaussblur/SinglePassBlurFilter.cpp +++ b/src/rendering/filters/gaussblur/SinglePassBlurFilter.cpp @@ -84,12 +84,12 @@ std::string SinglePassBlurFilter::onBuildFragmentShader() { } void SinglePassBlurFilter::onPrepareProgram(const tgfx::GLInterface* gl, unsigned int program) { - radiusHandle = gl->getUniformLocation(program, "uRadius"); - levelHandle = gl->getUniformLocation(program, "uLevel"); - repeatEdgeHandle = gl->getUniformLocation(program, "uRepeatEdge"); - colorHandle = gl->getUniformLocation(program, "uColor"); - colorValidHandle = gl->getUniformLocation(program, "uColorValid"); - alphaHandle = gl->getUniformLocation(program, "uAlpha"); + radiusHandle = gl->functions->getUniformLocation(program, "uRadius"); + levelHandle = gl->functions->getUniformLocation(program, "uLevel"); + repeatEdgeHandle = gl->functions->getUniformLocation(program, "uRepeatEdge"); + colorHandle = gl->functions->getUniformLocation(program, "uColor"); + colorValidHandle = gl->functions->getUniformLocation(program, "uColorValid"); + alphaHandle = gl->functions->getUniformLocation(program, "uAlpha"); } void SinglePassBlurFilter::updateParams(float blurrinessValue, float blurAlphaValue, @@ -130,16 +130,16 @@ void SinglePassBlurFilter::onUpdateParams(const tgfx::GLInterface* gl, auto blurRadius = blurValue / BLUR_LIMIT_BLURRINESS * (maxRadius - 1.0) + 1.0; auto blurLevel = blurValue / BLUR_LIMIT_BLURRINESS * (maxLevel - 1.0) + 1.0; - gl->uniform1f(radiusHandle, blurRadius); - gl->uniform2f(levelHandle, - blurLevel / static_cast(contentBounds.width()) * - (direction == BlurDirection::Horizontal), - blurLevel / static_cast(contentBounds.height()) * - (direction == BlurDirection::Vertical)); - gl->uniform1f(repeatEdgeHandle, repeatEdge); - gl->uniform3f(colorHandle, color.red, color.green, color.blue); - gl->uniform1f(colorValidHandle, isColorValid); - gl->uniform1f(alphaHandle, alpha); + gl->functions->uniform1f(radiusHandle, blurRadius); + gl->functions->uniform2f(levelHandle, + blurLevel / static_cast(contentBounds.width()) * + (direction == BlurDirection::Horizontal), + blurLevel / static_cast(contentBounds.height()) * + (direction == BlurDirection::Vertical)); + gl->functions->uniform1f(repeatEdgeHandle, repeatEdge); + gl->functions->uniform3f(colorHandle, color.red, color.green, color.blue); + gl->functions->uniform1f(colorValidHandle, isColorValid); + gl->functions->uniform1f(alphaHandle, alpha); } std::vector SinglePassBlurFilter::computeVertices(const tgfx::Rect& inputBounds, diff --git a/src/rendering/filters/glow/GlowBlurFilter.cpp b/src/rendering/filters/glow/GlowBlurFilter.cpp index ec95e6cce4..cbf82da639 100644 --- a/src/rendering/filters/glow/GlowBlurFilter.cpp +++ b/src/rendering/filters/glow/GlowBlurFilter.cpp @@ -72,8 +72,8 @@ std::string GlowBlurFilter::onBuildFragmentShader() { } void GlowBlurFilter::onPrepareProgram(const tgfx::GLInterface* gl, unsigned int program) { - textureOffsetHHandle = gl->getUniformLocation(program, "textureOffsetH"); - textureOffsetVHandle = gl->getUniformLocation(program, "textureOffsetV"); + textureOffsetHHandle = gl->functions->getUniformLocation(program, "textureOffsetH"); + textureOffsetVHandle = gl->functions->getUniformLocation(program, "textureOffsetV"); } void GlowBlurFilter::updateOffset(float offset) { @@ -84,7 +84,7 @@ void GlowBlurFilter::onUpdateParams(const tgfx::GLInterface* gl, const tgfx::Rec const tgfx::Point&) { auto textureOffsetH = blurDirection == BlurDirection::Horizontal ? blurOffset : 0; auto textureOffsetV = blurDirection == BlurDirection::Vertical ? blurOffset : 0; - gl->uniform1f(textureOffsetHHandle, textureOffsetH); - gl->uniform1f(textureOffsetVHandle, textureOffsetV); + gl->functions->uniform1f(textureOffsetHHandle, textureOffsetH); + gl->functions->uniform1f(textureOffsetVHandle, textureOffsetV); } } // namespace pag diff --git a/src/rendering/filters/glow/GlowFilter.cpp b/src/rendering/filters/glow/GlowFilter.cpp index fc3c0f0059..53b9f95025 100644 --- a/src/rendering/filters/glow/GlowFilter.cpp +++ b/src/rendering/filters/glow/GlowFilter.cpp @@ -93,7 +93,7 @@ void GlowFilter::draw(tgfx::Context* context, const FilterSource* source, if (!checkBuffer(context, blurWidth, blurHeight)) { return; } - auto gl = tgfx::GLContext::Unwrap(context); + auto gl = tgfx::GLInterface::Get(context); blurFilterBufferH->clearColor(gl); blurFilterBufferV->clearColor(gl); diff --git a/src/rendering/filters/glow/GlowMergeFilter.cpp b/src/rendering/filters/glow/GlowMergeFilter.cpp index ce6df58176..1e322ff043 100644 --- a/src/rendering/filters/glow/GlowMergeFilter.cpp +++ b/src/rendering/filters/glow/GlowMergeFilter.cpp @@ -50,9 +50,9 @@ std::string GlowMergeFilter::onBuildFragmentShader() { } void GlowMergeFilter::onPrepareProgram(const tgfx::GLInterface* gl, unsigned int program) { - inputTextureHandle = gl->getUniformLocation(program, "inputImageTexture"); - blurTextureHandle = gl->getUniformLocation(program, "blurImageTexture"); - progressHandle = gl->getUniformLocation(program, "progress"); + inputTextureHandle = gl->functions->getUniformLocation(program, "inputImageTexture"); + blurTextureHandle = gl->functions->getUniformLocation(program, "blurImageTexture"); + progressHandle = gl->functions->getUniformLocation(program, "progress"); } void GlowMergeFilter::updateTexture(unsigned blurTexture) { @@ -63,11 +63,11 @@ void GlowMergeFilter::onUpdateParams(const tgfx::GLInterface* gl, const tgfx::Re const tgfx::Point&) { auto glowEffect = static_cast(effect); auto glowThreshold = glowEffect->glowThreshold->getValueAt(layerFrame); - gl->uniform1f(progressHandle, glowThreshold); - gl->uniform1i(inputTextureHandle, 0); - // TODO(domrjchen): 下面这行之前写成了 gl->uniform1i(progressHandle, 1), 会导致 glError, + gl->functions->uniform1f(progressHandle, glowThreshold); + gl->functions->uniform1i(inputTextureHandle, 0); + // TODO(domrjchen): 下面这行之前写成了 gl->functions->uniform1i(progressHandle, 1), 会导致 glError, // 暂时注释掉。目前的发光效果跟 AE 也没有对齐,后面重写发光效果时时再修复。 - // gl->uniform1i(blurTextureHandle, 1); + // gl->functions->uniform1i(blurTextureHandle, 1); ActiveGLTexture(gl, GL_TEXTURE1, GL_TEXTURE_2D, blurTextureID); } } // namespace pag diff --git a/src/rendering/renderers/FilterRenderer.cpp b/src/rendering/renderers/FilterRenderer.cpp index 5ae5d5b58f..f7477dd20f 100644 --- a/src/rendering/renderers/FilterRenderer.cpp +++ b/src/rendering/renderers/FilterRenderer.cpp @@ -309,7 +309,7 @@ void ApplyFilters(tgfx::Context* context, std::vector filterNodes, const tgfx::Rect& contentBounds, FilterSource* filterSource, FilterTarget* filterTarget) { tgfx::GLStateGuard stateGuard(context); - auto gl = tgfx::GLContext::Unwrap(context); + auto gl = tgfx::GLInterface::Get(context); auto scale = filterSource->scale; std::shared_ptr freeBuffer = nullptr; std::shared_ptr lastBuffer = nullptr; diff --git a/test/PAGBlendTest.cpp b/test/PAGBlendTest.cpp index 05323a2596..ab7f2d6023 100644 --- a/test/PAGBlendTest.cpp +++ b/test/PAGBlendTest.cpp @@ -53,7 +53,7 @@ PAG_TEST_F(PAGBlendTest, Blend) { tgfx::GLSampler GetBottomLeftImage(std::shared_ptr device, int width, int height) { auto context = device->lockContext(); - auto gl = GLContext::Unwrap(context); + auto gl = GLInterface::Get(context); tgfx::GLSampler textureInfo; CreateGLTexture(gl, width, height, &textureInfo); auto backendTexture = ToBackendTexture(textureInfo, width, height); @@ -79,7 +79,7 @@ PAG_TEST_F(PAGBlendTest, CopyDstTexture) { auto device = GLDevice::Make(); auto context = device->lockContext(); ASSERT_TRUE(context != nullptr); - auto gl = GLContext::Unwrap(context); + auto gl = GLInterface::Get(context); tgfx::GLSampler textureInfo; CreateGLTexture(gl, width, height, &textureInfo); auto backendTexture = ToBackendTexture(textureInfo, width, height); @@ -95,8 +95,8 @@ PAG_TEST_F(PAGBlendTest, CopyDstTexture) { EXPECT_TRUE(Baseline::Compare(pagSurface, "PAGBlendTest/CopyDstTexture")); context = device->lockContext(); - gl = GLContext::Unwrap(context); - gl->deleteTextures(1, &textureInfo.id); + gl = GLInterface::Get(context); + gl->functions->deleteTextures(1, &textureInfo.id); device->unlock(); } @@ -110,7 +110,7 @@ PAG_TEST_F(PAGBlendTest, TextureBottomLeft) { auto replaceTextureInfo = GetBottomLeftImage(device, width, height); auto context = device->lockContext(); ASSERT_TRUE(context != nullptr); - auto gl = GLContext::Unwrap(context); + auto gl = GLInterface::Get(context); auto backendTexture = ToBackendTexture(replaceTextureInfo, width, height); auto replaceImage = PAGImage::FromTexture(backendTexture, ImageOrigin::BottomLeft); tgfx::GLSampler textureInfo; @@ -130,9 +130,9 @@ PAG_TEST_F(PAGBlendTest, TextureBottomLeft) { EXPECT_TRUE(Baseline::Compare(pagSurface, "PAGBlendTest/TextureBottomLeft")); context = device->lockContext(); - gl = GLContext::Unwrap(context); - gl->deleteTextures(1, &replaceTextureInfo.id); - gl->deleteTextures(1, &textureInfo.id); + gl = GLInterface::Get(context); + gl->functions->deleteTextures(1, &replaceTextureInfo.id); + gl->functions->deleteTextures(1, &textureInfo.id); device->unlock(); } @@ -150,7 +150,7 @@ PAG_TEST_F(PAGBlendTest, BothBottomLeft) { auto replaceImage = PAGImage::FromTexture(backendTexture, ImageOrigin::BottomLeft); replaceImage->setMatrix( Matrix::MakeTrans(static_cast(width) * 0.1, static_cast(height) * 0.2)); - auto gl = GLContext::Unwrap(context); + auto gl = GLInterface::Get(context); tgfx::GLSampler textureInfo = {}; CreateGLTexture(gl, width, height, &textureInfo); backendTexture = ToBackendTexture(textureInfo, width, height); @@ -172,9 +172,9 @@ PAG_TEST_F(PAGBlendTest, BothBottomLeft) { context = device->lockContext(); ASSERT_TRUE(context != nullptr); - gl = GLContext::Unwrap(context); - gl->deleteTextures(1, &replaceTextureInfo.id); - gl->deleteTextures(1, &textureInfo.id); + gl = GLInterface::Get(context); + gl->functions->deleteTextures(1, &replaceTextureInfo.id); + gl->functions->deleteTextures(1, &textureInfo.id); device->unlock(); } } // namespace pag diff --git a/test/PAGReadPixelsTest.cpp b/test/PAGReadPixelsTest.cpp index 8697abdb04..fff12228f6 100644 --- a/test/PAGReadPixelsTest.cpp +++ b/test/PAGReadPixelsTest.cpp @@ -224,7 +224,7 @@ PAG_TEST(PAGReadPixelsTest, TestSurfaceReadPixels) { ASSERT_TRUE(result); CHECK_PIXELS(AlphaRectInfo, pixels, "Surface_alpha_to_alpha_100_-100"); - auto gl = GLContext::Unwrap(context); + auto gl = GLInterface::Get(context); tgfx::GLSampler textureInfo = {}; result = CreateGLTexture(gl, width, height, &textureInfo); ASSERT_TRUE(result); @@ -255,7 +255,7 @@ PAG_TEST(PAGReadPixelsTest, TestSurfaceReadPixels) { ASSERT_TRUE(result); CHECK_PIXELS(RGBARectInfo, pixels, "Surface_BL_rgbA_to_rgbA_100_-100"); - gl->deleteTextures(1, &textureInfo.id); + gl->functions->deleteTextures(1, &textureInfo.id); device->unlock(); } diff --git a/test/PAGSurfaceTest.cpp b/test/PAGSurfaceTest.cpp index dd1a655f2f..6e44112cbf 100644 --- a/test/PAGSurfaceTest.cpp +++ b/test/PAGSurfaceTest.cpp @@ -38,7 +38,7 @@ PAG_TEST(PAGSurfaceTest, FromTexture) { auto device = GLDevice::Make(); auto context = device->lockContext(); ASSERT_TRUE(context != nullptr); - auto gl = GLContext::Unwrap(context); + auto gl = GLInterface::Get(context); auto glVersion = gl->caps->version; tgfx::GLSampler textureInfo; CreateGLTexture(gl, width, height, &textureInfo); @@ -91,7 +91,7 @@ PAG_TEST(PAGSurfaceTest, Mask) { auto device = GLDevice::Make(); auto context = device->lockContext(); ASSERT_TRUE(context != nullptr); - auto gl = GLContext::Unwrap(context); + auto gl = GLInterface::Get(context); tgfx::GLSampler textureInfo; CreateGLTexture(gl, width, height, &textureInfo); auto backendTexture = ToBackendTexture(textureInfo, width, height); @@ -108,8 +108,8 @@ PAG_TEST(PAGSurfaceTest, Mask) { context = device->lockContext(); ASSERT_TRUE(context != nullptr); - gl = GLContext::Unwrap(context); - gl->deleteTextures(1, &textureInfo.id); + gl = GLInterface::Get(context); + gl->functions->deleteTextures(1, &textureInfo.id); device->unlock(); } } // namespace pag diff --git a/tgfx/src/gpu/opengl/GLDefines.h b/tgfx/include/gpu/opengl/GLDefines.h similarity index 100% rename from tgfx/src/gpu/opengl/GLDefines.h rename to tgfx/include/gpu/opengl/GLDefines.h diff --git a/tgfx/src/gpu/opengl/GLFunctions.h b/tgfx/include/gpu/opengl/GLFunctions.h similarity index 50% rename from tgfx/src/gpu/opengl/GLFunctions.h rename to tgfx/include/gpu/opengl/GLFunctions.h index a1b4904aa7..6424c31ece 100644 --- a/tgfx/src/gpu/opengl/GLFunctions.h +++ b/tgfx/include/gpu/opengl/GLFunctions.h @@ -17,7 +17,11 @@ ///////////////////////////////////////////////////////////////////////////////////////////////// #pragma once + +#include +#include #include +#include namespace tgfx { #if !defined(GL_FUNCTION_TYPE) @@ -31,15 +35,16 @@ namespace tgfx { typedef intptr_t GLintptr; typedef intptr_t GLsizeiptr; -extern "C" { - using GLActiveTexture = void GL_FUNCTION_TYPE(unsigned texture); using GLAttachShader = void GL_FUNCTION_TYPE(unsigned program, unsigned shader); +using GLBindAttribLocation = void GL_FUNCTION_TYPE(unsigned program, unsigned index, + const char* name); using GLBindBuffer = void GL_FUNCTION_TYPE(unsigned target, unsigned buffer); using GLBindVertexArray = void GL_FUNCTION_TYPE(unsigned vertexArray); using GLBindFramebuffer = void GL_FUNCTION_TYPE(unsigned target, unsigned framebuffer); using GLBindRenderbuffer = void GL_FUNCTION_TYPE(unsigned target, unsigned renderbuffer); using GLBindTexture = void GL_FUNCTION_TYPE(unsigned target, unsigned texture); +using GLBlendColor = void GL_FUNCTION_TYPE(float red, float green, float blue, float alpha); using GLBlendEquation = void GL_FUNCTION_TYPE(unsigned mode); using GLBlendEquationSeparate = void GL_FUNCTION_TYPE(unsigned modeRGB, unsigned modeAlpha); using GLBlendFunc = void GL_FUNCTION_TYPE(unsigned sfactor, unsigned dfactor); @@ -47,19 +52,33 @@ using GLBlendFuncSeparate = void GL_FUNCTION_TYPE(unsigned sfactorRGB, unsigned unsigned sfactorAlpha, unsigned dfactorAlpha); using GLBufferData = void GL_FUNCTION_TYPE(unsigned target, GLsizeiptr size, const void* data, unsigned usage); +using GLBufferSubData = void GL_FUNCTION_TYPE(unsigned target, GLintptr offset, GLsizeiptr size, + const void* data); using GLCheckFramebufferStatus = unsigned GL_FUNCTION_TYPE(unsigned target); using GLClear = void GL_FUNCTION_TYPE(unsigned mask); using GLClearColor = void GL_FUNCTION_TYPE(float red, float green, float blue, float alpha); +using GLClearStencil = void GL_FUNCTION_TYPE(int s); +using GLColorMask = void GL_FUNCTION_TYPE(unsigned char red, unsigned char green, + unsigned char blue, unsigned char alpha); using GLCompileShader = void GL_FUNCTION_TYPE(unsigned shader); +using GLCompressedTexImage2D = void GL_FUNCTION_TYPE(unsigned target, int level, + unsigned internalformat, int width, int height, + int border, int imageSize, const void* data); +using GLCompressedTexSubImage2D = void GL_FUNCTION_TYPE(unsigned target, int level, int xoffset, + int yoffset, int width, int height, + unsigned format, int imageSize, + const void* data); using GLCopyTexSubImage2D = void GL_FUNCTION_TYPE(unsigned target, int level, int xoffset, int yoffset, int x, int y, int width, int height); using GLCreateProgram = unsigned GL_FUNCTION_TYPE(); using GLCreateShader = unsigned GL_FUNCTION_TYPE(unsigned type); +using GLCullFace = void GL_FUNCTION_TYPE(unsigned mode); using GLDeleteBuffers = void GL_FUNCTION_TYPE(int n, const unsigned* buffers); using GLDeleteFramebuffers = void GL_FUNCTION_TYPE(int n, const unsigned* framebuffers); using GLDeleteProgram = void GL_FUNCTION_TYPE(unsigned program); using GLDeleteRenderbuffers = void GL_FUNCTION_TYPE(int n, const unsigned* renderbuffers); using GLDeleteShader = void GL_FUNCTION_TYPE(unsigned shader); +using GLDeleteSync = void GL_FUNCTION_TYPE(void* sync); using GLDeleteTextures = void GL_FUNCTION_TYPE(int n, const unsigned* textures); using GLDeleteVertexArrays = void GL_FUNCTION_TYPE(int n, const unsigned* vertexArrays); using GLDepthMask = void GL_FUNCTION_TYPE(unsigned char flag); @@ -71,6 +90,7 @@ using GLDrawElements = void GL_FUNCTION_TYPE(unsigned mode, int count, unsigned using GLEnable = void GL_FUNCTION_TYPE(unsigned cap); using GLIsEnabled = unsigned char GL_FUNCTION_TYPE(unsigned cap); using GLEnableVertexAttribArray = void GL_FUNCTION_TYPE(unsigned index); +using GLFenceSync = void* GL_FUNCTION_TYPE(unsigned condition, unsigned flags); using GLFinish = void GL_FUNCTION_TYPE(); using GLFlush = void GL_FUNCTION_TYPE(); using GLFramebufferRenderbuffer = void GL_FUNCTION_TYPE(unsigned target, unsigned attachment, @@ -84,14 +104,22 @@ using GLFramebufferTexture2DMultisample = void GL_FUNCTION_TYPE(unsigned target, unsigned textarget, unsigned texture, int level, int samples); +using GLFrontFace = void GL_FUNCTION_TYPE(unsigned mode); using GLGenBuffers = void GL_FUNCTION_TYPE(int n, unsigned* buffers); using GLGenVertexArrays = void GL_FUNCTION_TYPE(int n, unsigned* vertexArrays); using GLGenFramebuffers = void GL_FUNCTION_TYPE(int n, unsigned* framebuffers); +using GLGenerateMipmap = void GL_FUNCTION_TYPE(unsigned target); using GLGenRenderbuffers = void GL_FUNCTION_TYPE(int n, unsigned* renderbuffers); using GLGenTextures = void GL_FUNCTION_TYPE(int n, unsigned* textures); +using GLGetBooleanv = void GL_FUNCTION_TYPE(unsigned pname, unsigned char* data); +using GLGetBufferParameteriv = void GL_FUNCTION_TYPE(unsigned target, unsigned pname, int* params); using GLGetError = unsigned GL_FUNCTION_TYPE(); +using GLGetFramebufferAttachmentParameteriv = void GL_FUNCTION_TYPE(unsigned target, + unsigned attachment, + unsigned pname, int* params); using GLGetIntegerv = void GL_FUNCTION_TYPE(unsigned pname, int* params); -using GLGetBooleanv = void GL_FUNCTION_TYPE(unsigned pname, unsigned char* data); +using GLGetInternalformativ = void GL_FUNCTION_TYPE(unsigned target, unsigned internalformat, + unsigned pname, int bufSize, int* params); using GLGetProgramInfoLog = void GL_FUNCTION_TYPE(unsigned program, int bufsize, int* length, char* infolog); using GLGetProgramiv = void GL_FUNCTION_TYPE(unsigned program, unsigned pname, int* params); @@ -100,6 +128,9 @@ using GLGetRenderbufferParameteriv = void GL_FUNCTION_TYPE(unsigned target, unsi using GLGetShaderInfoLog = void GL_FUNCTION_TYPE(unsigned shader, int bufsize, int* length, char* infolog); using GLGetShaderiv = void GL_FUNCTION_TYPE(unsigned shader, unsigned pname, int* params); +using GLGetShaderPrecisionFormat = void GL_FUNCTION_TYPE(unsigned shadertype, + unsigned precisiontype, int* range, + int* precision); using GLGetString = const unsigned char* GL_FUNCTION_TYPE(unsigned name); using GLGetStringi = const unsigned char* GL_FUNCTION_TYPE(unsigned name, unsigned index); using GLGetVertexAttribiv = void GL_FUNCTION_TYPE(unsigned index, unsigned pname, int* params); @@ -107,6 +138,8 @@ using GLGetVertexAttribPointerv = void GL_FUNCTION_TYPE(unsigned index, unsigned void** pointer); using GLGetAttribLocation = int GL_FUNCTION_TYPE(unsigned program, const char* name); using GLGetUniformLocation = int GL_FUNCTION_TYPE(unsigned program, const char* name); +using GLIsTexture = unsigned char GL_FUNCTION_TYPE(unsigned texture); +using GLLineWidth = void GL_FUNCTION_TYPE(float width); using GLLinkProgram = void GL_FUNCTION_TYPE(unsigned program); using GLPixelStorei = void GL_FUNCTION_TYPE(unsigned pname, int param); using GLReadPixels = void GL_FUNCTION_TYPE(int x, int y, int width, int height, unsigned format, @@ -129,38 +162,58 @@ using GLBlitFramebuffer = void GL_FUNCTION_TYPE(int srcX0, int srcY0, int srcX1, using GLScissor = void GL_FUNCTION_TYPE(int x, int y, int width, int height); using GLShaderSource = void GL_FUNCTION_TYPE(unsigned shader, int count, const char* const* str, const int* length); +using GLStencilFunc = void GL_FUNCTION_TYPE(unsigned func, int ref, unsigned mask); +using GLStencilFuncSeparate = void GL_FUNCTION_TYPE(unsigned face, unsigned func, int ref, + unsigned mask); +using GLStencilMask = void GL_FUNCTION_TYPE(unsigned mask); +using GLStencilMaskSeparate = void GL_FUNCTION_TYPE(unsigned face, unsigned mask); +using GLStencilOp = void GL_FUNCTION_TYPE(unsigned fail, unsigned zfail, unsigned zpass); +using GLStencilOpSeparate = void GL_FUNCTION_TYPE(unsigned face, unsigned fail, unsigned zfail, + unsigned zpass); using GLTexImage2D = void GL_FUNCTION_TYPE(unsigned target, int level, int internalformat, int width, int height, int border, unsigned format, unsigned type, const void* pixels); +using GLTexParameterf = void GL_FUNCTION_TYPE(unsigned target, unsigned pname, float param); +using GLTexParameterfv = void GL_FUNCTION_TYPE(unsigned target, unsigned pname, + const float* params); using GLTexParameteri = void GL_FUNCTION_TYPE(unsigned target, unsigned pname, int param); using GLTexParameteriv = void GL_FUNCTION_TYPE(unsigned target, unsigned pname, const int* params); using GLTexSubImage2D = void GL_FUNCTION_TYPE(unsigned target, int level, int xoffset, int yoffset, int width, int height, unsigned format, unsigned type, const void* pixels); +using GLTextureBarrier = void GL_FUNCTION_TYPE(); using GLUniform1f = void GL_FUNCTION_TYPE(int location, float v0); using GLUniform1i = void GL_FUNCTION_TYPE(int location, int v0); +using GLUniform1fv = void GL_FUNCTION_TYPE(int location, int count, const float* v); +using GLUniform1iv = void GL_FUNCTION_TYPE(int location, int count, const int* v); using GLUniform2f = void GL_FUNCTION_TYPE(int location, float v0, float v1); +using GLUniform2i = void GL_FUNCTION_TYPE(int location, int v0, int v1); +using GLUniform2fv = void GL_FUNCTION_TYPE(int location, int count, const float* v); +using GLUniform2iv = void GL_FUNCTION_TYPE(int location, int count, const int* v); using GLUniform3f = void GL_FUNCTION_TYPE(int location, float v0, float v1, float v2); +using GLUniform3i = void GL_FUNCTION_TYPE(int location, int v0, int v1, int v2); +using GLUniform3fv = void GL_FUNCTION_TYPE(int location, int count, const float* v); +using GLUniform3iv = void GL_FUNCTION_TYPE(int location, int count, const int* v); +using GLUniform4f = void GL_FUNCTION_TYPE(int location, float v0, float v1, float v2, float v3); +using GLUniform4i = void GL_FUNCTION_TYPE(int location, int v0, int v1, int v2, int v3); using GLUniform4fv = void GL_FUNCTION_TYPE(int location, int count, const float* v); +using GLUniform4iv = void GL_FUNCTION_TYPE(int location, int count, const int* v); +using GLUniformMatrix2fv = void GL_FUNCTION_TYPE(int location, int count, unsigned char transpose, + const float* value); using GLUniformMatrix3fv = void GL_FUNCTION_TYPE(int location, int count, unsigned char transpose, const float* value); +using GLUniformMatrix4fv = void GL_FUNCTION_TYPE(int location, int count, unsigned char transpose, + const float* value); using GLUseProgram = void GL_FUNCTION_TYPE(unsigned program); +using GLVertexAttrib1f = void GL_FUNCTION_TYPE(unsigned indx, float value); +using GLVertexAttrib2fv = void GL_FUNCTION_TYPE(unsigned indx, const float* values); +using GLVertexAttrib3fv = void GL_FUNCTION_TYPE(unsigned indx, const float* values); +using GLVertexAttrib4fv = void GL_FUNCTION_TYPE(unsigned indx, const float* values); using GLVertexAttribPointer = void GL_FUNCTION_TYPE(unsigned indx, int size, unsigned type, unsigned char normalized, int stride, const void* ptr); using GLViewport = void GL_FUNCTION_TYPE(int x, int y, int width, int height); - -/* ARB_internalformat_query */ -using GLGetInternalformativ = void GL_FUNCTION_TYPE(unsigned target, unsigned internalformat, - unsigned pname, int bufSize, int* params); -using GLTextureBarrier = void GL_FUNCTION_TYPE(); -using GLGetShaderPrecisionFormat = void GL_FUNCTION_TYPE(unsigned shadertype, - unsigned precisiontype, int* range, - int* precision); -using GLFenceSync = void* GL_FUNCTION_TYPE(unsigned condition, unsigned flags); using GLWaitSync = void GL_FUNCTION_TYPE(void* sync, unsigned flags, uint64_t timeout); -using GLDeleteSync = void GL_FUNCTION_TYPE(void* sync); -} // extern "C" // This is a lighter-weight std::function, trying to reduce code size and compile time by only // supporting the exact use cases we require. @@ -203,4 +256,145 @@ class GLFunction { size_t buffer[4] = {}; }; +class Context; + +/** + * GLFunctions holds the OpenGL function pointers. + */ +class GLFunctions { + public: + /** + * Returns the GLFunctions pointer associated with the specified context. + */ + static const GLFunctions* Get(const Context* context); + + GLFunction activeTexture = nullptr; + GLFunction attachShader = nullptr; + GLFunction bindAttribLocation = nullptr; + GLFunction bindBuffer = nullptr; + GLFunction bindFramebuffer = nullptr; + GLFunction bindRenderbuffer = nullptr; + GLFunction bindTexture = nullptr; + GLFunction bindVertexArray = nullptr; + GLFunction blendColor = nullptr; + GLFunction blendEquation = nullptr; + GLFunction blendEquationSeparate = nullptr; + GLFunction blendFunc = nullptr; + GLFunction blendFuncSeparate = nullptr; + GLFunction bufferData = nullptr; + GLFunction bufferSubData = nullptr; + GLFunction checkFramebufferStatus = nullptr; + GLFunction clear = nullptr; + GLFunction clearColor = nullptr; + GLFunction clearStencil = nullptr; + GLFunction colorMask = nullptr; + GLFunction compileShader = nullptr; + GLFunction compressedTexImage2D = nullptr; + GLFunction compressedTexSubImage2D = nullptr; + GLFunction copyTexSubImage2D = nullptr; + GLFunction createProgram = nullptr; + GLFunction createShader = nullptr; + GLFunction cullFace = nullptr; + GLFunction deleteBuffers = nullptr; + GLFunction deleteFramebuffers = nullptr; + GLFunction deleteProgram = nullptr; + GLFunction deleteRenderbuffers = nullptr; + GLFunction deleteShader = nullptr; + GLFunction deleteSync = nullptr; + GLFunction deleteTextures = nullptr; + GLFunction deleteVertexArrays = nullptr; + GLFunction depthMask = nullptr; + GLFunction disable = nullptr; + GLFunction disableVertexAttribArray = nullptr; + GLFunction drawArrays = nullptr; + GLFunction drawElements = nullptr; + GLFunction enable = nullptr; + GLFunction isEnabled = nullptr; + GLFunction enableVertexAttribArray = nullptr; + GLFunction fenceSync = nullptr; + GLFunction finish = nullptr; + GLFunction flush = nullptr; + GLFunction framebufferRenderbuffer = nullptr; + GLFunction framebufferTexture2D = nullptr; + GLFunction framebufferTexture2DMultisample = nullptr; + GLFunction frontFace = nullptr; + GLFunction genBuffers = nullptr; + GLFunction genFramebuffers = nullptr; + GLFunction generateMipmap = nullptr; + GLFunction genRenderbuffers = nullptr; + GLFunction genTextures = nullptr; + GLFunction genVertexArrays = nullptr; + GLFunction getBufferParameteriv = nullptr; + GLFunction getError = nullptr; + GLFunction getFramebufferAttachmentParameteriv = nullptr; + GLFunction getIntegerv = nullptr; + GLFunction getInternalformativ = nullptr; + GLFunction getBooleanv = nullptr; + GLFunction getProgramInfoLog = nullptr; + GLFunction getProgramiv = nullptr; + GLFunction getRenderbufferParameteriv = nullptr; + GLFunction getShaderInfoLog = nullptr; + GLFunction getShaderiv = nullptr; + GLFunction getShaderPrecisionFormat = nullptr; + GLFunction getString = nullptr; + GLFunction getStringi = nullptr; + GLFunction getVertexAttribiv = nullptr; + GLFunction getVertexAttribPointerv = nullptr; + GLFunction getAttribLocation = nullptr; + GLFunction getUniformLocation = nullptr; + GLFunction isTexture = nullptr; + GLFunction lineWidth = nullptr; + GLFunction linkProgram = nullptr; + GLFunction pixelStorei = nullptr; + GLFunction readPixels = nullptr; + GLFunction renderbufferStorage = nullptr; + GLFunction renderbufferStorageMultisample = nullptr; + GLFunction renderbufferStorageMultisampleAPPLE = nullptr; + GLFunction renderbufferStorageMultisampleEXT = nullptr; + GLFunction resolveMultisampleFramebuffer = nullptr; + GLFunction blitFramebuffer = nullptr; + GLFunction scissor = nullptr; + GLFunction shaderSource = nullptr; + GLFunction stencilFunc = nullptr; + GLFunction stencilFuncSeparate = nullptr; + GLFunction stencilMask = nullptr; + GLFunction stencilMaskSeparate = nullptr; + GLFunction stencilOp = nullptr; + GLFunction stencilOpSeparate = nullptr; + GLFunction texImage2D = nullptr; + GLFunction texParameterf = nullptr; + GLFunction texParameterfv = nullptr; + GLFunction texParameteri = nullptr; + GLFunction texParameteriv = nullptr; + GLFunction texSubImage2D = nullptr; + GLFunction textureBarrier = nullptr; + GLFunction uniform1f = nullptr; + GLFunction uniform1i = nullptr; + GLFunction uniform1fv = nullptr; + GLFunction uniform1iv = nullptr; + GLFunction uniform2f = nullptr; + GLFunction uniform2i = nullptr; + GLFunction uniform2fv = nullptr; + GLFunction uniform2iv = nullptr; + GLFunction uniform3f = nullptr; + GLFunction uniform3i = nullptr; + GLFunction uniform3fv = nullptr; + GLFunction uniform3iv = nullptr; + GLFunction uniform4f = nullptr; + GLFunction uniform4i = nullptr; + GLFunction uniform4fv = nullptr; + GLFunction uniform4iv = nullptr; + GLFunction uniformMatrix2fv = nullptr; + GLFunction uniformMatrix3fv = nullptr; + GLFunction uniformMatrix4fv = nullptr; + GLFunction useProgram = nullptr; + GLFunction vertexAttrib1f = nullptr; + GLFunction vertexAttrib2fv = nullptr; + GLFunction vertexAttrib3fv = nullptr; + GLFunction vertexAttrib4fv = nullptr; + GLFunction vertexAttribPointer = nullptr; + GLFunction viewport = nullptr; + GLFunction waitSync = nullptr; +}; + } // namespace tgfx \ No newline at end of file diff --git a/tgfx/src/core/vectors/web/WebMask.cpp b/tgfx/src/core/vectors/web/WebMask.cpp index 35dc22552c..b62d389abf 100644 --- a/tgfx/src/core/vectors/web/WebMask.cpp +++ b/tgfx/src/core/vectors/web/WebMask.cpp @@ -43,8 +43,8 @@ std::shared_ptr WebMask::makeTexture(Context* context) const { return nullptr; } auto& glInfo = std::static_pointer_cast(texture)->glSampler(); - const auto* gl = GLContext::Unwrap(context); - gl->bindTexture(glInfo.target, glInfo.id); + auto gl = GLInterface::Get(context); + gl->functions->bindTexture(glInfo.target, glInfo.id); webMask.call("update", val::module_property("GL")); return texture; } diff --git a/tgfx/src/gpu/opengl/GLAssembledGLESInterface.cpp b/tgfx/src/gpu/opengl/GLAssembledGLESInterface.cpp index a545aa2398..4d0dd963cb 100644 --- a/tgfx/src/gpu/opengl/GLAssembledGLESInterface.cpp +++ b/tgfx/src/gpu/opengl/GLAssembledGLESInterface.cpp @@ -21,93 +21,90 @@ #include "GLInterface.h" namespace tgfx { -static void InitFramebufferTexture2DMultisample(const GLProcGetter* getter, GLInterface* interface, +static void InitFramebufferTexture2DMultisample(const GLProcGetter* getter, GLFunctions* functions, const GLInfo& info) { if (info.hasExtension("GL_EXT_multisampled_render_to_texture")) { - interface->framebufferTexture2DMultisample = + functions->framebufferTexture2DMultisample = reinterpret_cast( getter->getProcAddress("glFramebufferTexture2DMultisampleEXT")); } else if (info.hasExtension("GL_IMG_multisampled_render_to_texture")) { - interface->framebufferTexture2DMultisample = + functions->framebufferTexture2DMultisample = reinterpret_cast( getter->getProcAddress("glFramebufferTexture2DMultisampleIMG")); } } -static void InitRenderbufferStorageMultisample(const GLProcGetter* getter, GLInterface* interface, +static void InitRenderbufferStorageMultisample(const GLProcGetter* getter, GLFunctions* functions, const GLInfo& info) { if (info.version >= GL_VER(3, 0)) { - interface->renderbufferStorageMultisample = reinterpret_cast( + functions->renderbufferStorageMultisample = reinterpret_cast( getter->getProcAddress("glRenderbufferStorageMultisample")); } else if (info.hasExtension("GL_CHROMIUM_framebuffer_multisample")) { - interface->renderbufferStorageMultisample = reinterpret_cast( + functions->renderbufferStorageMultisample = reinterpret_cast( getter->getProcAddress("glRenderbufferStorageMultisampleCHROMIUM")); } else if (info.hasExtension("GL_ANGLE_framebuffer_multisample")) { - interface->renderbufferStorageMultisample = reinterpret_cast( + functions->renderbufferStorageMultisample = reinterpret_cast( getter->getProcAddress("glRenderbufferStorageMultisampleANGLE")); } if (info.hasExtension("GL_EXT_multisampled_render_to_texture")) { - interface->renderbufferStorageMultisampleEXT = + functions->renderbufferStorageMultisampleEXT = reinterpret_cast( getter->getProcAddress("glRenderbufferStorageMultisampleEXT")); } if (info.hasExtension("GL_IMG_multisampled_render_to_texture")) { - interface->renderbufferStorageMultisampleEXT = + functions->renderbufferStorageMultisampleEXT = reinterpret_cast( getter->getProcAddress("glRenderbufferStorageMultisampleIMG")); } if (info.hasExtension("GL_APPLE_framebuffer_multisample")) { - interface->renderbufferStorageMultisampleAPPLE = + functions->renderbufferStorageMultisampleAPPLE = reinterpret_cast( getter->getProcAddress("glRenderbufferStorageMultisampleAPPLE")); } } -static void InitBlitFramebuffer(const GLProcGetter* getter, GLInterface* interface, +static void InitBlitFramebuffer(const GLProcGetter* getter, GLFunctions* functions, const GLInfo& info) { if (info.version >= GL_VER(3, 0)) { - interface->blitFramebuffer = + functions->blitFramebuffer = reinterpret_cast(getter->getProcAddress("glBlitFramebuffer")); } else if (info.hasExtension("GL_CHROMIUM_framebuffer_multisample")) { - interface->blitFramebuffer = + functions->blitFramebuffer = reinterpret_cast(getter->getProcAddress("glBlitFramebufferCHROMIUM")); } else if (info.hasExtension("GL_ANGLE_framebuffer_blit")) { - interface->blitFramebuffer = + functions->blitFramebuffer = reinterpret_cast(getter->getProcAddress("glBlitFramebufferANGLE")); } } -static void InitVertexArray(const GLProcGetter* getter, GLInterface* interface, +static void InitVertexArray(const GLProcGetter* getter, GLFunctions* functions, const GLInfo& info) { if (info.version >= GL_VER(3, 0)) { - interface->bindVertexArray = + functions->bindVertexArray = reinterpret_cast(getter->getProcAddress("glBindVertexArray")); - interface->deleteVertexArrays = + functions->deleteVertexArrays = reinterpret_cast(getter->getProcAddress("glDeleteVertexArrays")); - interface->genVertexArrays = + functions->genVertexArrays = reinterpret_cast(getter->getProcAddress("glGenVertexArrays")); } else if (info.hasExtension("GL_OES_vertex_array_object")) { - interface->bindVertexArray = + functions->bindVertexArray = reinterpret_cast(getter->getProcAddress("glBindVertexArrayOES")); - interface->deleteVertexArrays = + functions->deleteVertexArrays = reinterpret_cast(getter->getProcAddress("glDeleteVertexArraysOES")); - interface->genVertexArrays = + functions->genVertexArrays = reinterpret_cast(getter->getProcAddress("glGenVertexArraysOES")); } } -void GLAssembleGLESInterface(const GLProcGetter* getter, GLInterface* interface, +void GLAssembleGLESInterface(const GLProcGetter* getter, GLFunctions* functions, const GLInfo& info) { - interface->checkFramebufferStatus = reinterpret_cast( - getter->getProcAddress("glCheckFramebufferStatus")); - interface->getError = reinterpret_cast(getter->getProcAddress("glGetError")); if (info.hasExtension("GL_NV_texture_barrier")) { - interface->textureBarrier = + functions->textureBarrier = reinterpret_cast(getter->getProcAddress("glTextureBarrierNV")); } - InitBlitFramebuffer(getter, interface, info); - InitRenderbufferStorageMultisample(getter, interface, info); - InitFramebufferTexture2DMultisample(getter, interface, info); - InitVertexArray(getter, interface, info); + InitBlitFramebuffer(getter, functions, info); + InitRenderbufferStorageMultisample(getter, functions, info); + InitFramebufferTexture2DMultisample(getter, functions, info); + InitVertexArray(getter, functions, info); } } // namespace tgfx diff --git a/tgfx/src/gpu/opengl/GLAssembledGLESInterface.h b/tgfx/src/gpu/opengl/GLAssembledGLESInterface.h index 0b4bb0aed1..87f24a3195 100644 --- a/tgfx/src/gpu/opengl/GLAssembledGLESInterface.h +++ b/tgfx/src/gpu/opengl/GLAssembledGLESInterface.h @@ -20,9 +20,9 @@ namespace tgfx { class GLProcGetter; -class GLInterface; +class GLFunctions; class GLInfo; -void GLAssembleGLESInterface(const GLProcGetter* getter, GLInterface* interface, +void GLAssembleGLESInterface(const GLProcGetter* getter, GLFunctions* functions, const GLInfo& info); } // namespace tgfx diff --git a/tgfx/src/gpu/opengl/GLAssembledGLInterface.cpp b/tgfx/src/gpu/opengl/GLAssembledGLInterface.cpp index 720ec32003..ec5cac278f 100644 --- a/tgfx/src/gpu/opengl/GLAssembledGLInterface.cpp +++ b/tgfx/src/gpu/opengl/GLAssembledGLInterface.cpp @@ -21,65 +21,62 @@ #include "GLInterface.h" namespace tgfx { -static void InitTextureBarrier(const GLProcGetter* getter, GLInterface* interface, +static void InitTextureBarrier(const GLProcGetter* getter, GLFunctions* functions, const GLInfo& info) { if (info.version >= GL_VER(4, 5) || info.hasExtension("GL_ARB_texture_barrier")) { - interface->textureBarrier = + functions->textureBarrier = reinterpret_cast(getter->getProcAddress("glTextureBarrier")); } else if (info.hasExtension("GL_NV_texture_barrier")) { - interface->textureBarrier = + functions->textureBarrier = reinterpret_cast(getter->getProcAddress("glTextureBarrierNV")); } } -static void InitBlitFrameBuffer(const GLProcGetter* getter, GLInterface* interface, +static void InitBlitFrameBuffer(const GLProcGetter* getter, GLFunctions* functions, const GLInfo& info) { if (info.version >= GL_VER(3, 0) || info.hasExtension("GL_ARB_framebuffer_object")) { - interface->blitFramebuffer = + functions->blitFramebuffer = reinterpret_cast(getter->getProcAddress("glBlitFramebuffer")); } else if (info.hasExtension("GL_EXT_framebuffer_blit")) { - interface->blitFramebuffer = + functions->blitFramebuffer = reinterpret_cast(getter->getProcAddress("glBlitFramebufferEXT")); } } -static void InitVertexArray(const GLProcGetter* getter, GLInterface* interface, +static void InitVertexArray(const GLProcGetter* getter, GLFunctions* functions, const GLInfo& info) { if (info.version >= GL_VER(3, 0) || info.hasExtension("GL_ARB_vertex_array_object")) { - interface->bindVertexArray = + functions->bindVertexArray = reinterpret_cast(getter->getProcAddress("glBindVertexArray")); - interface->deleteVertexArrays = + functions->deleteVertexArrays = reinterpret_cast(getter->getProcAddress("glDeleteVertexArrays")); - interface->genVertexArrays = + functions->genVertexArrays = reinterpret_cast(getter->getProcAddress("glGenVertexArrays")); } else if (info.hasExtension("GL_APPLE_vertex_array_object")) { - interface->bindVertexArray = + functions->bindVertexArray = reinterpret_cast(getter->getProcAddress("glBindVertexArrayAPPLE")); - interface->deleteVertexArrays = reinterpret_cast( + functions->deleteVertexArrays = reinterpret_cast( getter->getProcAddress("glDeleteVertexArraysAPPLE")); - interface->genVertexArrays = + functions->genVertexArrays = reinterpret_cast(getter->getProcAddress("glGenVertexArraysAPPLE")); } } -static void InitRenderbufferStorageMultisample(const GLProcGetter* getter, GLInterface* interface, +static void InitRenderbufferStorageMultisample(const GLProcGetter* getter, GLFunctions* functions, const GLInfo& info) { if (info.version >= GL_VER(3, 0) || info.hasExtension("GL_ARB_framebuffer_object")) { - interface->renderbufferStorageMultisample = reinterpret_cast( + functions->renderbufferStorageMultisample = reinterpret_cast( getter->getProcAddress("glRenderbufferStorageMultisample")); } else if (info.hasExtension("GL_EXT_framebuffer_multisample")) { - interface->renderbufferStorageMultisample = reinterpret_cast( + functions->renderbufferStorageMultisample = reinterpret_cast( getter->getProcAddress("glRenderbufferStorageMultisampleEXT")); } } -void GLAssembleGLInterface(const GLProcGetter* getter, GLInterface* interface, const GLInfo& info) { - interface->checkFramebufferStatus = reinterpret_cast( - getter->getProcAddress("glCheckFramebufferStatus")); - interface->getError = reinterpret_cast(getter->getProcAddress("glGetError")); - InitTextureBarrier(getter, interface, info); - InitBlitFrameBuffer(getter, interface, info); - InitRenderbufferStorageMultisample(getter, interface, info); - InitVertexArray(getter, interface, info); +void GLAssembleGLInterface(const GLProcGetter* getter, GLFunctions* functions, const GLInfo& info) { + InitTextureBarrier(getter, functions, info); + InitBlitFrameBuffer(getter, functions, info); + InitRenderbufferStorageMultisample(getter, functions, info); + InitVertexArray(getter, functions, info); } } // namespace tgfx diff --git a/tgfx/src/gpu/opengl/GLAssembledGLInterface.h b/tgfx/src/gpu/opengl/GLAssembledGLInterface.h index 8c1a9131e3..98e10c60e9 100644 --- a/tgfx/src/gpu/opengl/GLAssembledGLInterface.h +++ b/tgfx/src/gpu/opengl/GLAssembledGLInterface.h @@ -20,8 +20,8 @@ namespace tgfx { class GLProcGetter; -class GLInterface; +class GLFunctions; class GLInfo; -void GLAssembleGLInterface(const GLProcGetter* getter, GLInterface* interface, const GLInfo& info); +void GLAssembleGLInterface(const GLProcGetter* getter, GLFunctions* functions, const GLInfo& info); } // namespace tgfx diff --git a/tgfx/src/gpu/opengl/GLAssembledWebGLInterface.cpp b/tgfx/src/gpu/opengl/GLAssembledWebGLInterface.cpp index e9864f2468..261787fb37 100644 --- a/tgfx/src/gpu/opengl/GLAssembledWebGLInterface.cpp +++ b/tgfx/src/gpu/opengl/GLAssembledWebGLInterface.cpp @@ -21,45 +21,34 @@ #include "GLInterface.h" namespace tgfx { -static unsigned GetErrorFake() { - return GL_NO_ERROR; -} - -static unsigned CheckFramebufferStatusFake(unsigned) { - return GL_FRAMEBUFFER_COMPLETE; -} - -static void InitVertexArray(const GLProcGetter* getter, GLInterface* interface, +static void InitVertexArray(const GLProcGetter* getter, GLFunctions* functions, const GLInfo& info) { if (info.version >= GL_VER(2, 0)) { - interface->bindVertexArray = + functions->bindVertexArray = reinterpret_cast(getter->getProcAddress("glBindVertexArray")); - interface->deleteVertexArrays = + functions->deleteVertexArrays = reinterpret_cast(getter->getProcAddress("glDeleteVertexArrays")); - interface->genVertexArrays = + functions->genVertexArrays = reinterpret_cast(getter->getProcAddress("glGenVertexArrays")); } else if (info.hasExtension("GL_OES_vertex_array_object") || info.hasExtension("OES_vertex_array_object")) { - interface->bindVertexArray = + functions->bindVertexArray = reinterpret_cast(getter->getProcAddress("glBindVertexArrayOES")); - interface->deleteVertexArrays = + functions->deleteVertexArrays = reinterpret_cast(getter->getProcAddress("glDeleteVertexArraysOES")); - interface->genVertexArrays = + functions->genVertexArrays = reinterpret_cast(getter->getProcAddress("glGenVertexArraysOES")); } } -void GLAssembleWebGLInterface(const GLProcGetter* getter, GLInterface* interface, +void GLAssembleWebGLInterface(const GLProcGetter* getter, GLFunctions* functions, const GLInfo& info) { - interface->getError = reinterpret_cast(GetErrorFake); - interface->checkFramebufferStatus = - reinterpret_cast(CheckFramebufferStatusFake); if (info.version >= GL_VER(2, 0)) { - interface->blitFramebuffer = + functions->blitFramebuffer = reinterpret_cast(getter->getProcAddress("glBlitFramebuffer")); - interface->renderbufferStorageMultisample = reinterpret_cast( + functions->renderbufferStorageMultisample = reinterpret_cast( getter->getProcAddress("glRenderbufferStorageMultisample")); } - InitVertexArray(getter, interface, info); + InitVertexArray(getter, functions, info); } } // namespace tgfx diff --git a/tgfx/src/gpu/opengl/GLAssembledWebGLInterface.h b/tgfx/src/gpu/opengl/GLAssembledWebGLInterface.h index 35c434f25d..ad51dbcb59 100644 --- a/tgfx/src/gpu/opengl/GLAssembledWebGLInterface.h +++ b/tgfx/src/gpu/opengl/GLAssembledWebGLInterface.h @@ -20,9 +20,9 @@ namespace tgfx { class GLProcGetter; -class GLInterface; +class GLFunctions; class GLInfo; -void GLAssembleWebGLInterface(const GLProcGetter* getter, GLInterface* interface, +void GLAssembleWebGLInterface(const GLProcGetter* getter, GLFunctions* functions, const GLInfo& info); } // namespace tgfx diff --git a/tgfx/src/gpu/opengl/GLBlend.cpp b/tgfx/src/gpu/opengl/GLBlend.cpp index 6709537ff1..98a4abf04a 100644 --- a/tgfx/src/gpu/opengl/GLBlend.cpp +++ b/tgfx/src/gpu/opengl/GLBlend.cpp @@ -17,7 +17,7 @@ ///////////////////////////////////////////////////////////////////////////////////////////////// #include "GLBlend.h" -#include "GLDefines.h" +#include "gpu/opengl/GLDefines.h" namespace tgfx { static void HardLight(FragmentShaderBuilder* fsBuilder, const char* final, const char* src, diff --git a/tgfx/src/gpu/opengl/GLBuffer.cpp b/tgfx/src/gpu/opengl/GLBuffer.cpp index ef6afe662e..506a50f5f5 100644 --- a/tgfx/src/gpu/opengl/GLBuffer.cpp +++ b/tgfx/src/gpu/opengl/GLBuffer.cpp @@ -39,14 +39,15 @@ std::shared_ptr GLBuffer::Make(Context* context, const uint16_t* buffe if (glBuffer != nullptr) { return glBuffer; } - const auto* gl = GLContext::Unwrap(context); + auto gl = GLInterface::Get(context); glBuffer = Resource::Wrap(context, new GLBuffer(buffer, length)); - gl->genBuffers(1, &glBuffer->_bufferID); + gl->functions->genBuffers(1, &glBuffer->_bufferID); if (buffer) { - gl->bindBuffer(GL_ELEMENT_ARRAY_BUFFER, glBuffer->_bufferID); - gl->bufferData(GL_ELEMENT_ARRAY_BUFFER, static_cast(sizeof(uint16_t) * length), - buffer, GL_STATIC_DRAW); - gl->bindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + gl->functions->bindBuffer(GL_ELEMENT_ARRAY_BUFFER, glBuffer->_bufferID); + gl->functions->bufferData(GL_ELEMENT_ARRAY_BUFFER, + static_cast(sizeof(uint16_t) * length), buffer, + GL_STATIC_DRAW); + gl->functions->bindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } return glBuffer; } @@ -57,7 +58,8 @@ void GLBuffer::computeRecycleKey(BytesKey* bytesKey) const { void GLBuffer::onRelease(Context* context) { if (_bufferID > 0) { - GLContext::Unwrap(context)->deleteBuffers(1, &_bufferID); + auto gl = GLInterface::Get(context); + gl->functions->deleteBuffers(1, &_bufferID); _bufferID = 0; } } diff --git a/tgfx/src/gpu/opengl/GLCanvas.cpp b/tgfx/src/gpu/opengl/GLCanvas.cpp index cb064156fa..26fb695f8a 100644 --- a/tgfx/src/gpu/opengl/GLCanvas.cpp +++ b/tgfx/src/gpu/opengl/GLCanvas.cpp @@ -35,7 +35,7 @@ GLCanvas::GLCanvas(Surface* surface) : Canvas(surface) { void GLCanvas::clear() { auto renderTarget = std::static_pointer_cast(surface->getRenderTarget()); - renderTarget->clear(GLContext::Unwrap(getContext())); + renderTarget->clear(GLInterface::Get(getContext())); } void GLCanvas::drawTexture(const Texture* texture, const Texture* mask, bool inverted) { diff --git a/tgfx/src/gpu/opengl/GLCaps.h b/tgfx/src/gpu/opengl/GLCaps.h index c2b3af809e..aeb156f9e1 100644 --- a/tgfx/src/gpu/opengl/GLCaps.h +++ b/tgfx/src/gpu/opengl/GLCaps.h @@ -21,13 +21,13 @@ #include #include #include -#include "GLDefines.h" -#include "GLFunctions.h" #include "core/utils/EnumHasher.h" #include "core/utils/Log.h" #include "gpu/Caps.h" #include "gpu/PixelFormat.h" #include "gpu/Swizzle.h" +#include "gpu/opengl/GLDefines.h" +#include "gpu/opengl/GLFunctions.h" #define GL_VER(major, minor) ((static_cast(major) << 16) | static_cast(minor)) @@ -112,7 +112,7 @@ class GLCaps : public Caps { public: GLStandard standard = GLStandard::None; uint32_t version = 0; - GLVendor vendor; + GLVendor vendor = GLVendor::Other; bool vertexArrayObjectSupport = false; bool packRowLengthSupport = false; bool unpackRowLengthSupport = false; diff --git a/tgfx/src/gpu/opengl/GLContext.h b/tgfx/src/gpu/opengl/GLContext.h index 0d3ec84911..d2ac34f7cc 100644 --- a/tgfx/src/gpu/opengl/GLContext.h +++ b/tgfx/src/gpu/opengl/GLContext.h @@ -27,16 +27,16 @@ class GLCaps; class GLContext : public Context { public: - static const GLInterface* Unwrap(Context* context) { - return context ? static_cast(context)->interface.get() : nullptr; - } - GLContext(Device* device, const GLInterface* glInterface); Backend backend() const override { return Backend::OPENGL; } + const GLFunctions* functions() const { + return interface->functions.get(); + } + const Caps* caps() const override { return interface->caps.get(); } @@ -47,5 +47,6 @@ class GLContext : public Context { friend class GLStateGuard; friend class GLDevice; + friend class GLInterface; }; } // namespace tgfx diff --git a/tgfx/src/gpu/opengl/GLDrawer.cpp b/tgfx/src/gpu/opengl/GLDrawer.cpp index d6f58b7f0b..e4927862e6 100644 --- a/tgfx/src/gpu/opengl/GLDrawer.cpp +++ b/tgfx/src/gpu/opengl/GLDrawer.cpp @@ -76,30 +76,30 @@ std::shared_ptr GLDrawer::Make(Context* context) { } bool GLDrawer::init(Context* context) { - auto gl = GLContext::Unwrap(context); + auto gl = GLInterface::Get(context); CheckGLError(gl); if (gl->caps->vertexArrayObjectSupport) { // Using VAO is required in the core profile. - gl->genVertexArrays(1, &vertexArray); + gl->functions->genVertexArrays(1, &vertexArray); } - gl->genBuffers(1, &vertexBuffer); + gl->functions->genBuffers(1, &vertexBuffer); return CheckGLError(gl); } void GLDrawer::onRelease(Context* context) { - auto gl = GLContext::Unwrap(context); + auto gl = GLInterface::Get(context); if (vertexArray > 0) { - gl->deleteVertexArrays(1, &vertexArray); + gl->functions->deleteVertexArrays(1, &vertexArray); vertexArray = 0; } if (vertexBuffer > 0) { - gl->deleteBuffers(1, &vertexBuffer); + gl->functions->deleteBuffers(1, &vertexBuffer); vertexBuffer = 0; } } static std::shared_ptr CreateDstTexture(const DrawArgs& args, Point* dstOffset) { - auto gl = GLContext::Unwrap(args.context); + auto gl = GLInterface::Get(args.context); if (gl->caps->textureBarrierSupport && args.renderTargetTexture) { *dstOffset = {0, 0}; return args.renderTargetTexture; @@ -122,16 +122,16 @@ static std::shared_ptr CreateDstTexture(const DrawArgs& args, Point* ds } GLStateGuard stateGuard(args.context); auto renderTarget = static_cast(args.renderTarget); - gl->bindFramebuffer(GL_FRAMEBUFFER, renderTarget->glFrameBuffer().id); + gl->functions->bindFramebuffer(GL_FRAMEBUFFER, renderTarget->glFrameBuffer().id); auto glSampler = std::static_pointer_cast(dstTexture)->glSampler(); - gl->bindTexture(glSampler.target, glSampler.id); + gl->functions->bindTexture(glSampler.target, glSampler.id); // format != BGRA && !srcHasMSAARenderBuffer && !dstHasMSAARenderBuffer && dstIsTextureable && // dstOrigin == srcOrigin && canConfigBeFBOColorAttachment(srcConfig) && (!srcIsTextureable || // srcIsGLTexture2D) - gl->copyTexSubImage2D(glSampler.target, 0, 0, 0, static_cast(dstRect.x()), - static_cast(dstRect.y()), static_cast(dstRect.width()), - static_cast(dstRect.height())); - gl->bindTexture(glSampler.target, 0); + gl->functions->copyTexSubImage2D(glSampler.target, 0, 0, 0, static_cast(dstRect.x()), + static_cast(dstRect.y()), static_cast(dstRect.width()), + static_cast(dstRect.height())); + gl->functions->bindTexture(glSampler.target, 0); return dstTexture; } @@ -144,24 +144,24 @@ static PixelFormat GetOutputPixelFormat(const DrawArgs& args) { static void UpdateScissor(const GLInterface* gl, const DrawArgs& args) { if (args.scissorRect.isEmpty()) { - gl->disable(GL_SCISSOR_TEST); + gl->functions->disable(GL_SCISSOR_TEST); } else { - gl->enable(GL_SCISSOR_TEST); - gl->scissor(static_cast(args.scissorRect.x()), static_cast(args.scissorRect.y()), - static_cast(args.scissorRect.width()), - static_cast(args.scissorRect.height())); + gl->functions->enable(GL_SCISSOR_TEST); + gl->functions->scissor( + static_cast(args.scissorRect.x()), static_cast(args.scissorRect.y()), + static_cast(args.scissorRect.width()), static_cast(args.scissorRect.height())); } } static void UpdateBlend(const GLInterface* gl, bool blendAsCoeff, unsigned first, unsigned second) { if (blendAsCoeff) { - gl->enable(GL_BLEND); - gl->blendFunc(first, second); - gl->blendEquation(GL_FUNC_ADD); + gl->functions->enable(GL_BLEND); + gl->functions->blendFunc(first, second); + gl->functions->blendEquation(GL_FUNC_ADD); } else { - gl->disable(GL_BLEND); + gl->functions->disable(GL_BLEND); if (gl->caps->frameBufferFetchSupport && gl->caps->frameBufferFetchRequiresEnablePerSample) { - gl->enable(GL_FETCH_PER_SAMPLE_ARM); + gl->functions->enable(GL_FETCH_PER_SAMPLE_ARM); } } } @@ -176,7 +176,7 @@ void GLDrawer::draw(DrawArgs args, std::unique_ptr op) const { std::move(args.colors.begin(), args.colors.end(), fragmentProcessors.begin()); std::move(args.masks.begin(), args.masks.end(), fragmentProcessors.begin() + static_cast(numColorProcessors)); - const auto* gl = GLContext::Unwrap(args.context); + auto gl = GLInterface::Get(args.context); std::unique_ptr xferProcessor; std::shared_ptr dstTexture; Point dstTextureOffset = Point::Zero(); @@ -202,40 +202,42 @@ void GLDrawer::draw(DrawArgs args, std::unique_ptr op) const { } CheckGLError(gl); auto renderTarget = static_cast(args.renderTarget); - gl->useProgram(program->programID()); - gl->bindFramebuffer(GL_FRAMEBUFFER, renderTarget->glFrameBuffer().id); - gl->viewport(0, 0, renderTarget->width(), renderTarget->height()); + gl->functions->useProgram(program->programID()); + gl->functions->bindFramebuffer(GL_FRAMEBUFFER, renderTarget->glFrameBuffer().id); + gl->functions->viewport(0, 0, renderTarget->width(), renderTarget->height()); UpdateScissor(gl, args); UpdateBlend(gl, blendAsCoeff, first, second); if (pipeline.needsBarrierTexture(args.renderTargetTexture.get())) { - gl->textureBarrier(); + gl->functions->textureBarrier(); } program->updateUniformsAndTextureBindings(gl, *geometryProcessor, pipeline); if (vertexArray > 0) { - gl->bindVertexArray(vertexArray); + gl->functions->bindVertexArray(vertexArray); } - gl->bindBuffer(GL_ARRAY_BUFFER, vertexBuffer); + gl->functions->bindBuffer(GL_ARRAY_BUFFER, vertexBuffer); auto vertices = op->vertices(args); - gl->bufferData(GL_ARRAY_BUFFER, static_cast(vertices.size()) * sizeof(float), - &vertices[0], GL_STATIC_DRAW); + gl->functions->bufferData(GL_ARRAY_BUFFER, + static_cast(vertices.size()) * sizeof(float), &vertices[0], + GL_STATIC_DRAW); for (const auto& attribute : program->vertexAttributes()) { const AttribLayout& layout = GetAttribLayout(attribute.gpuType); - gl->vertexAttribPointer(static_cast(attribute.location), layout.count, layout.type, - layout.normalized, program->vertexStride(), - reinterpret_cast(attribute.offset)); - gl->enableVertexAttribArray(static_cast(attribute.location)); + gl->functions->vertexAttribPointer(static_cast(attribute.location), layout.count, + layout.type, layout.normalized, program->vertexStride(), + reinterpret_cast(attribute.offset)); + gl->functions->enableVertexAttribArray(static_cast(attribute.location)); } - gl->bindBuffer(GL_ARRAY_BUFFER, 0); + gl->functions->bindBuffer(GL_ARRAY_BUFFER, 0); auto indexBuffer = op->getIndexBuffer(args); if (indexBuffer) { - gl->bindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer->bufferID()); - gl->drawElements(GL_TRIANGLES, static_cast(indexBuffer->length()), GL_UNSIGNED_SHORT, 0); - gl->bindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + gl->functions->bindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer->bufferID()); + gl->functions->drawElements(GL_TRIANGLES, static_cast(indexBuffer->length()), + GL_UNSIGNED_SHORT, 0); + gl->functions->bindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } else { - gl->drawArrays(GL_TRIANGLE_STRIP, 0, 4); + gl->functions->drawArrays(GL_TRIANGLE_STRIP, 0, 4); } if (vertexArray > 0) { - gl->bindVertexArray(0); + gl->functions->bindVertexArray(0); } CheckGLError(gl); } diff --git a/tgfx/src/gpu/opengl/GLFragmentShaderBuilder.cpp b/tgfx/src/gpu/opengl/GLFragmentShaderBuilder.cpp index ea16b2ff99..2a5673886b 100644 --- a/tgfx/src/gpu/opengl/GLFragmentShaderBuilder.cpp +++ b/tgfx/src/gpu/opengl/GLFragmentShaderBuilder.cpp @@ -29,7 +29,7 @@ GLFragmentShaderBuilder::GLFragmentShaderBuilder(ProgramBuilder* program) } std::string GLFragmentShaderBuilder::dstColor() { - const auto* gl = static_cast(programBuilder)->gl(); + auto gl = static_cast(programBuilder)->gl(); if (gl->caps->frameBufferFetchSupport) { addFeature(PrivateFeature::FramebufferFetch, gl->caps->frameBufferFetchExtensionString); return gl->caps->frameBufferFetchColorName; diff --git a/tgfx/src/gpu/opengl/GLFunctions.cpp b/tgfx/src/gpu/opengl/GLFunctions.cpp new file mode 100644 index 0000000000..24b2c2c9f1 --- /dev/null +++ b/tgfx/src/gpu/opengl/GLFunctions.cpp @@ -0,0 +1,26 @@ +///////////////////////////////////////////////////////////////////////////////////////////////// +// +// Tencent is pleased to support the open source community by making libpag available. +// +// Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file +// except in compliance with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// unless required by applicable law or agreed to in writing, software distributed under the +// license is distributed on an "as is" basis, without warranties or conditions of any kind, +// either express or implied. see the license for the specific language governing permissions +// and limitations under the license. +// +///////////////////////////////////////////////////////////////////////////////////////////////// + +#include "gpu/opengl/GLFunctions.h" +#include "gpu/opengl/GLContext.h" + +namespace tgfx { +const GLFunctions* GLFunctions::Get(const Context* context) { + return context ? static_cast(context)->functions() : nullptr; +} +} // namespace tgfx \ No newline at end of file diff --git a/tgfx/src/gpu/opengl/GLInterface.cpp b/tgfx/src/gpu/opengl/GLInterface.cpp index 253be1d4e8..66aaa0fe79 100644 --- a/tgfx/src/gpu/opengl/GLInterface.cpp +++ b/tgfx/src/gpu/opengl/GLInterface.cpp @@ -20,7 +20,6 @@ #include #include - #include "GLAssembledGLESInterface.h" #include "GLAssembledGLInterface.h" #include "GLAssembledWebGLInterface.h" @@ -43,6 +42,10 @@ static int GetGLVersion(const GLProcGetter* getter) { return GetGLVersion(versionString).majorVersion; } +const GLInterface* GLInterface::Get(const Context* context) { + return context ? static_cast(context)->interface.get() : nullptr; +} + const GLInterface* GLInterface::GetNative() { auto getter = GLProcGetter::Make(); if (getter == nullptr) { @@ -71,13 +74,13 @@ GLFunction Bind(GLState* interface, R (GLState::*me #ifdef TGFX_BUILD_FOR_WEB #define Hook(X) state #else -#define Hook(X) interface->X = Bind(state, &GLState::X) +#define Hook(X) functions->X = Bind(state, &GLState::X) #endif std::unique_ptr GLInterface::HookWithState(const GLInterface* gl, GLState* state) { - auto interface = new GLInterface(); - *interface = *gl; + auto functions = std::make_shared(); + *functions = *gl->functions; Hook(activeTexture); Hook(blendEquation); Hook(blendFunc); @@ -98,6 +101,9 @@ std::unique_ptr GLInterface::HookWithState(const GLInterface* if (gl->caps->vertexArrayObjectSupport) { Hook(bindVertexArray); } + auto interface = new GLInterface(); + interface->functions = functions; + interface->caps = gl->caps; return std::unique_ptr(interface); } @@ -120,138 +126,210 @@ std::unique_ptr GLInterface::MakeNativeInterface(const GLProc reinterpret_cast(getter->getProcAddress("glGetInternalformativ")); GLInfo info(getString, getStringi, getIntegerv, getInternalformativ, getShaderPrecisionFormat); auto interface = new GLInterface(); - interface->activeTexture = + auto functions = std::make_shared(); + interface->functions = functions; + functions->activeTexture = reinterpret_cast(getter->getProcAddress("glActiveTexture")); - interface->attachShader = + functions->attachShader = reinterpret_cast(getter->getProcAddress("glAttachShader")); - interface->bindBuffer = reinterpret_cast(getter->getProcAddress("glBindBuffer")); - interface->bindFramebuffer = + functions->bindAttribLocation = + reinterpret_cast(getter->getProcAddress("glBindAttribLocation")); + functions->bindBuffer = reinterpret_cast(getter->getProcAddress("glBindBuffer")); + functions->bindFramebuffer = reinterpret_cast(getter->getProcAddress("glBindFramebuffer")); - interface->bindRenderbuffer = + functions->bindRenderbuffer = reinterpret_cast(getter->getProcAddress("glBindRenderbuffer")); - interface->bindTexture = + functions->bindTexture = reinterpret_cast(getter->getProcAddress("glBindTexture")); - interface->blendEquation = + functions->blendColor = reinterpret_cast(getter->getProcAddress("glBlendColor")); + functions->blendEquation = reinterpret_cast(getter->getProcAddress("glBlendEquation")); - interface->blendEquationSeparate = + functions->blendEquationSeparate = reinterpret_cast(getter->getProcAddress("glBlendEquationSeparate")); - interface->blendFunc = reinterpret_cast(getter->getProcAddress("glBlendFunc")); - interface->blendFuncSeparate = + functions->blendFunc = reinterpret_cast(getter->getProcAddress("glBlendFunc")); + functions->blendFuncSeparate = reinterpret_cast(getter->getProcAddress("glBlendFuncSeparate")); - interface->bufferData = reinterpret_cast(getter->getProcAddress("glBufferData")); - interface->clear = reinterpret_cast(getter->getProcAddress("glClear")); - interface->clearColor = reinterpret_cast(getter->getProcAddress("glClearColor")); - interface->compileShader = + functions->bufferData = reinterpret_cast(getter->getProcAddress("glBufferData")); + functions->bufferSubData = + reinterpret_cast(getter->getProcAddress("glBufferSubData")); + functions->checkFramebufferStatus = reinterpret_cast( + getter->getProcAddress("glCheckFramebufferStatus")); + functions->clear = reinterpret_cast(getter->getProcAddress("glClear")); + functions->clearColor = reinterpret_cast(getter->getProcAddress("glClearColor")); + functions->clearStencil = + reinterpret_cast(getter->getProcAddress("glClearStencil")); + functions->colorMask = reinterpret_cast(getter->getProcAddress("glColorMask")); + functions->compileShader = reinterpret_cast(getter->getProcAddress("glCompileShader")); - interface->copyTexSubImage2D = + functions->compressedTexImage2D = + reinterpret_cast(getter->getProcAddress("glCompressedTexImage2D")); + functions->compressedTexSubImage2D = reinterpret_cast( + getter->getProcAddress("glCompressedTexSubImage2D")); + functions->copyTexSubImage2D = reinterpret_cast(getter->getProcAddress("glCopyTexSubImage2D")); - interface->createProgram = + functions->createProgram = reinterpret_cast(getter->getProcAddress("glCreateProgram")); - interface->createShader = + functions->createShader = reinterpret_cast(getter->getProcAddress("glCreateShader")); - interface->deleteBuffers = + functions->cullFace = reinterpret_cast(getter->getProcAddress("glCullFace")); + functions->deleteBuffers = reinterpret_cast(getter->getProcAddress("glDeleteBuffers")); - interface->deleteFramebuffers = + functions->deleteFramebuffers = reinterpret_cast(getter->getProcAddress("glDeleteFramebuffers")); - interface->deleteProgram = + functions->deleteProgram = reinterpret_cast(getter->getProcAddress("glDeleteProgram")); - interface->deleteRenderbuffers = + functions->deleteRenderbuffers = reinterpret_cast(getter->getProcAddress("glDeleteRenderbuffers")); - interface->deleteShader = + functions->deleteShader = reinterpret_cast(getter->getProcAddress("glDeleteShader")); - interface->deleteTextures = + functions->deleteSync = reinterpret_cast(getter->getProcAddress("glDeleteSync")); + functions->deleteTextures = reinterpret_cast(getter->getProcAddress("glDeleteTextures")); - interface->depthMask = reinterpret_cast(getter->getProcAddress("glDepthMask")); - interface->disable = reinterpret_cast(getter->getProcAddress("glDisable")); - interface->disableVertexAttribArray = reinterpret_cast( + functions->depthMask = reinterpret_cast(getter->getProcAddress("glDepthMask")); + functions->disable = reinterpret_cast(getter->getProcAddress("glDisable")); + functions->disableVertexAttribArray = reinterpret_cast( getter->getProcAddress("glDisableVertexAttribArray")); - interface->drawArrays = reinterpret_cast(getter->getProcAddress("glDrawArrays")); - interface->drawElements = + functions->drawArrays = reinterpret_cast(getter->getProcAddress("glDrawArrays")); + functions->drawElements = reinterpret_cast(getter->getProcAddress("glDrawElements")); - interface->enable = reinterpret_cast(getter->getProcAddress("glEnable")); - interface->isEnabled = reinterpret_cast(getter->getProcAddress("glIsEnabled")); - interface->enableVertexAttribArray = reinterpret_cast( + functions->enable = reinterpret_cast(getter->getProcAddress("glEnable")); + functions->isEnabled = reinterpret_cast(getter->getProcAddress("glIsEnabled")); + functions->enableVertexAttribArray = reinterpret_cast( getter->getProcAddress("glEnableVertexAttribArray")); - interface->finish = reinterpret_cast(getter->getProcAddress("glFinish")); - interface->flush = reinterpret_cast(getter->getProcAddress("glFlush")); - interface->framebufferRenderbuffer = reinterpret_cast( + functions->fenceSync = reinterpret_cast(getter->getProcAddress("glFenceSync")); + functions->finish = reinterpret_cast(getter->getProcAddress("glFinish")); + functions->flush = reinterpret_cast(getter->getProcAddress("glFlush")); + functions->framebufferRenderbuffer = reinterpret_cast( getter->getProcAddress("glFramebufferRenderbuffer")); - interface->framebufferTexture2D = + functions->framebufferTexture2D = reinterpret_cast(getter->getProcAddress("glFramebufferTexture2D")); - interface->genBuffers = reinterpret_cast(getter->getProcAddress("glGenBuffers")); - interface->genFramebuffers = + functions->frontFace = reinterpret_cast(getter->getProcAddress("glFrontFace")); + functions->genBuffers = reinterpret_cast(getter->getProcAddress("glGenBuffers")); + functions->genFramebuffers = reinterpret_cast(getter->getProcAddress("glGenFramebuffers")); - interface->genRenderbuffers = + functions->generateMipmap = + reinterpret_cast(getter->getProcAddress("glGenerateMipmap")); + functions->genRenderbuffers = reinterpret_cast(getter->getProcAddress("glGenRenderbuffers")); - interface->genTextures = + functions->genTextures = reinterpret_cast(getter->getProcAddress("glGenTextures")); - interface->getIntegerv = + functions->getBufferParameteriv = + reinterpret_cast(getter->getProcAddress("glGetBufferParameteriv")); + functions->getError = reinterpret_cast(getter->getProcAddress("glGetError")); + functions->getFramebufferAttachmentParameteriv = + reinterpret_cast( + getter->getProcAddress("glGetFramebufferAttachmentParameteriv")); + functions->getIntegerv = reinterpret_cast(getter->getProcAddress("glGetIntegerv")); - interface->getBooleanv = + functions->getInternalformativ = + reinterpret_cast(getter->getProcAddress("glGetInternalformativ")); + functions->getBooleanv = reinterpret_cast(getter->getProcAddress("glGetBooleanv")); - interface->getProgramInfoLog = + functions->getProgramInfoLog = reinterpret_cast(getter->getProcAddress("glGetProgramInfoLog")); - interface->getProgramiv = + functions->getProgramiv = reinterpret_cast(getter->getProcAddress("glGetProgramiv")); - interface->getRenderbufferParameteriv = reinterpret_cast( + functions->getRenderbufferParameteriv = reinterpret_cast( getter->getProcAddress("glGetRenderbufferParameteriv")); - interface->getShaderInfoLog = + functions->getShaderInfoLog = reinterpret_cast(getter->getProcAddress("glGetShaderInfoLog")); - interface->getShaderiv = + functions->getShaderiv = reinterpret_cast(getter->getProcAddress("glGetShaderiv")); - interface->getString = reinterpret_cast(getter->getProcAddress("glGetString")); - interface->getVertexAttribiv = + functions->getShaderPrecisionFormat = reinterpret_cast( + getter->getProcAddress("glGetShaderPrecisionFormat")); + functions->getString = reinterpret_cast(getter->getProcAddress("glGetString")); + functions->getStringi = reinterpret_cast(getter->getProcAddress("glGetStringi")); + functions->getVertexAttribiv = reinterpret_cast(getter->getProcAddress("glGetVertexAttribiv")); - interface->getVertexAttribPointerv = reinterpret_cast( + functions->getVertexAttribPointerv = reinterpret_cast( getter->getProcAddress("glGetVertexAttribPointerv")); - interface->getAttribLocation = + functions->getAttribLocation = reinterpret_cast(getter->getProcAddress("glGetAttribLocation")); - interface->getUniformLocation = + functions->getUniformLocation = reinterpret_cast(getter->getProcAddress("glGetUniformLocation")); - interface->linkProgram = + functions->isTexture = reinterpret_cast(getter->getProcAddress("glIsTexture")); + functions->lineWidth = reinterpret_cast(getter->getProcAddress("glLineWidth")); + functions->linkProgram = reinterpret_cast(getter->getProcAddress("glLinkProgram")); - interface->pixelStorei = + functions->pixelStorei = reinterpret_cast(getter->getProcAddress("glPixelStorei")); - interface->readPixels = reinterpret_cast(getter->getProcAddress("glReadPixels")); - interface->renderbufferStorage = + functions->readPixels = reinterpret_cast(getter->getProcAddress("glReadPixels")); + functions->renderbufferStorage = reinterpret_cast(getter->getProcAddress("glRenderbufferStorage")); - interface->resolveMultisampleFramebuffer = reinterpret_cast( + functions->resolveMultisampleFramebuffer = reinterpret_cast( getter->getProcAddress("glResolveMultisampleFramebufferAPPLE")); - interface->scissor = reinterpret_cast(getter->getProcAddress("glScissor")); - interface->shaderSource = + functions->scissor = reinterpret_cast(getter->getProcAddress("glScissor")); + functions->shaderSource = reinterpret_cast(getter->getProcAddress("glShaderSource")); - interface->texImage2D = reinterpret_cast(getter->getProcAddress("glTexImage2D")); - interface->texParameteri = + functions->stencilFunc = + reinterpret_cast(getter->getProcAddress("glStencilFunc")); + functions->stencilFuncSeparate = + reinterpret_cast(getter->getProcAddress("glStencilFuncSeparate")); + functions->stencilMask = + reinterpret_cast(getter->getProcAddress("glStencilMask")); + functions->stencilMaskSeparate = + reinterpret_cast(getter->getProcAddress("glStencilMaskSeparate")); + functions->stencilOp = reinterpret_cast(getter->getProcAddress("glStencilOp")); + functions->stencilOpSeparate = + reinterpret_cast(getter->getProcAddress("glStencilOpSeparate")); + functions->texImage2D = reinterpret_cast(getter->getProcAddress("glTexImage2D")); + functions->texParameterf = + reinterpret_cast(getter->getProcAddress("glTexParameterf")); + functions->texParameterfv = + reinterpret_cast(getter->getProcAddress("glTexParameterfv")); + functions->texParameteri = reinterpret_cast(getter->getProcAddress("glTexParameteri")); - interface->texParameteriv = + functions->texParameteriv = reinterpret_cast(getter->getProcAddress("glTexParameteriv")); - interface->texSubImage2D = + functions->texSubImage2D = reinterpret_cast(getter->getProcAddress("glTexSubImage2D")); - interface->uniform1f = reinterpret_cast(getter->getProcAddress("glUniform1f")); - interface->uniform1i = reinterpret_cast(getter->getProcAddress("glUniform1i")); - interface->uniform2f = reinterpret_cast(getter->getProcAddress("glUniform2f")); - interface->uniform3f = reinterpret_cast(getter->getProcAddress("glUniform3f")); - interface->uniform4fv = reinterpret_cast(getter->getProcAddress("glUniform4fv")); - interface->uniformMatrix3fv = + functions->uniform1f = reinterpret_cast(getter->getProcAddress("glUniform1f")); + functions->uniform1i = reinterpret_cast(getter->getProcAddress("glUniform1i")); + functions->uniform1fv = reinterpret_cast(getter->getProcAddress("glUniform1fv")); + functions->uniform1iv = reinterpret_cast(getter->getProcAddress("glUniform1iv")); + functions->uniform2f = reinterpret_cast(getter->getProcAddress("glUniform2f")); + functions->uniform2i = reinterpret_cast(getter->getProcAddress("glUniform2i")); + functions->uniform2fv = reinterpret_cast(getter->getProcAddress("glUniform2fv")); + functions->uniform2iv = reinterpret_cast(getter->getProcAddress("glUniform2iv")); + functions->uniform3f = reinterpret_cast(getter->getProcAddress("glUniform3f")); + functions->uniform3i = reinterpret_cast(getter->getProcAddress("glUniform3i")); + functions->uniform3fv = reinterpret_cast(getter->getProcAddress("glUniform3fv")); + functions->uniform3iv = reinterpret_cast(getter->getProcAddress("glUniform3iv")); + functions->uniform4f = reinterpret_cast(getter->getProcAddress("glUniform4f")); + functions->uniform4i = reinterpret_cast(getter->getProcAddress("glUniform4i")); + functions->uniform4fv = reinterpret_cast(getter->getProcAddress("glUniform4fv")); + functions->uniform4iv = reinterpret_cast(getter->getProcAddress("glUniform4iv")); + functions->uniformMatrix2fv = + reinterpret_cast(getter->getProcAddress("glUniformMatrix2fv")); + functions->uniformMatrix3fv = reinterpret_cast(getter->getProcAddress("glUniformMatrix3fv")); - interface->useProgram = reinterpret_cast(getter->getProcAddress("glUseProgram")); - interface->vertexAttribPointer = + functions->uniformMatrix4fv = + reinterpret_cast(getter->getProcAddress("glUniformMatrix4fv")); + functions->useProgram = reinterpret_cast(getter->getProcAddress("glUseProgram")); + functions->vertexAttrib1f = + reinterpret_cast(getter->getProcAddress("glVertexAttrib1f")); + functions->vertexAttrib2fv = + reinterpret_cast(getter->getProcAddress("glVertexAttrib2fv")); + functions->vertexAttrib3fv = + reinterpret_cast(getter->getProcAddress("glVertexAttrib3fv")); + functions->vertexAttrib4fv = + reinterpret_cast(getter->getProcAddress("glVertexAttrib4fv")); + functions->vertexAttribPointer = reinterpret_cast(getter->getProcAddress("glVertexAttribPointer")); - interface->viewport = reinterpret_cast(getter->getProcAddress("glViewport")); - interface->fenceSync = reinterpret_cast(getter->getProcAddress("glFenceSync")); - interface->waitSync = reinterpret_cast(getter->getProcAddress("glWaitSync")); - interface->deleteSync = reinterpret_cast(getter->getProcAddress("glDeleteSync")); + functions->viewport = reinterpret_cast(getter->getProcAddress("glViewport")); + functions->waitSync = reinterpret_cast(getter->getProcAddress("glWaitSync")); + switch (info.standard) { case GLStandard::None: break; case GLStandard::GL: - GLAssembleGLInterface(getter, interface, info); + GLAssembleGLInterface(getter, functions.get(), info); break; case GLStandard::GLES: - GLAssembleGLESInterface(getter, interface, info); + GLAssembleGLESInterface(getter, functions.get(), info); break; case GLStandard::WebGL: - GLAssembleWebGLInterface(getter, interface, info); + GLAssembleWebGLInterface(getter, functions.get(), info); break; } interface->caps = std::shared_ptr(new GLCaps(info)); diff --git a/tgfx/src/gpu/opengl/GLInterface.h b/tgfx/src/gpu/opengl/GLInterface.h index 64f775302d..d28114cbdf 100644 --- a/tgfx/src/gpu/opengl/GLInterface.h +++ b/tgfx/src/gpu/opengl/GLInterface.h @@ -19,115 +19,26 @@ #pragma once #include "GLCaps.h" -#include "GLDefines.h" -#include "GLFunctions.h" #include "GLProcGetter.h" +#include "gpu/opengl/GLDefines.h" +#include "gpu/opengl/GLFunctions.h" namespace tgfx { class GLState; -/** - * NOTE: - * If added API can change OpenGL state, you should add code to recovery it. - * 1. GLInterface.cpp - add HOOK function at HookWithState - * 2. GLState.h/cpp - add hook function and GLAttribute - */ class GLInterface { public: - static const GLInterface* GetNative(); - - GLFunction activeTexture; - GLFunction attachShader; - GLFunction bindBuffer; - GLFunction bindFramebuffer; - GLFunction bindRenderbuffer; - GLFunction bindTexture; - GLFunction bindVertexArray; - GLFunction blendEquation; - GLFunction blendEquationSeparate; - GLFunction blendFunc; - GLFunction blendFuncSeparate; - GLFunction bufferData; - GLFunction checkFramebufferStatus; - GLFunction clear; - GLFunction clearColor; - GLFunction compileShader; - GLFunction copyTexSubImage2D; - GLFunction createProgram; - GLFunction createShader; - GLFunction deleteBuffers; - GLFunction deleteFramebuffers; - GLFunction deleteProgram; - GLFunction deleteRenderbuffers; - GLFunction deleteShader; - GLFunction deleteTextures; - GLFunction deleteVertexArrays; - GLFunction depthMask; - GLFunction disable; - GLFunction disableVertexAttribArray; - GLFunction drawArrays; - GLFunction drawElements; - GLFunction enable; - GLFunction isEnabled; - GLFunction enableVertexAttribArray; - GLFunction finish; - GLFunction flush; - GLFunction framebufferRenderbuffer; - GLFunction framebufferTexture2D; - GLFunction framebufferTexture2DMultisample; - GLFunction genBuffers; - GLFunction genFramebuffers; - GLFunction genRenderbuffers; - GLFunction genTextures; - GLFunction genVertexArrays; - GLFunction getError; - GLFunction getIntegerv; - GLFunction getBooleanv; - GLFunction getProgramInfoLog; - GLFunction getProgramiv; - GLFunction getRenderbufferParameteriv; - GLFunction getShaderInfoLog; - GLFunction getShaderiv; - GLFunction getString; - GLFunction getVertexAttribiv; - GLFunction getVertexAttribPointerv; - GLFunction getAttribLocation; - GLFunction getUniformLocation; - GLFunction linkProgram; - GLFunction pixelStorei; - GLFunction readPixels; - GLFunction renderbufferStorage; - GLFunction renderbufferStorageMultisample; - GLFunction renderbufferStorageMultisampleAPPLE; - GLFunction renderbufferStorageMultisampleEXT; - GLFunction resolveMultisampleFramebuffer; - GLFunction blitFramebuffer; - GLFunction scissor; - GLFunction shaderSource; - GLFunction texImage2D; - GLFunction texParameteri; - GLFunction texParameteriv; - GLFunction texSubImage2D; - GLFunction uniform1f; - GLFunction uniform1i; - GLFunction uniform2f; - GLFunction uniform3f; - GLFunction uniform4fv; - GLFunction uniformMatrix3fv; - GLFunction useProgram; - GLFunction vertexAttribPointer; - GLFunction viewport; - GLFunction textureBarrier; - GLFunction fenceSync; - GLFunction waitSync; - GLFunction deleteSync; + static const GLInterface* Get(const Context* context); std::shared_ptr caps = nullptr; + std::shared_ptr functions = nullptr; private: + static const GLInterface* GetNative(); static std::unique_ptr MakeNativeInterface(const GLProcGetter* getter); static std::unique_ptr HookWithState(const GLInterface* gl, GLState* state); + friend class GLDevice; friend class GLContext; }; } // namespace tgfx diff --git a/tgfx/src/gpu/opengl/GLProgram.cpp b/tgfx/src/gpu/opengl/GLProgram.cpp index 21fe327d5b..c1dc0961dc 100644 --- a/tgfx/src/gpu/opengl/GLProgram.cpp +++ b/tgfx/src/gpu/opengl/GLProgram.cpp @@ -41,21 +41,21 @@ GLProgram::GLProgram(unsigned programID, const std::vector& uniforms, void GLProgram::setupSamplerUniforms(const GLInterface* gl, const std::vector& textureSamplers) const { - gl->useProgram(programId); + gl->functions->useProgram(programId); // Assign texture units to sampler uniforms one time up front. auto count = static_cast(textureSamplers.size()); for (int i = 0; i < count; ++i) { const auto& sampler = textureSamplers[i]; if (kUnusedUniform != sampler.location) { - gl->uniform1i(sampler.location, i); + gl->functions->uniform1i(sampler.location, i); } } } void GLProgram::onRelease(Context* context) { if (programId) { - auto gl = GLContext::Unwrap(context); - gl->deleteProgram(programId); + auto gl = GLInterface::Get(context); + gl->functions->deleteProgram(programId); } } diff --git a/tgfx/src/gpu/opengl/GLProgramBuilder.cpp b/tgfx/src/gpu/opengl/GLProgramBuilder.cpp index 5e394d986a..dc05e8d5b2 100644 --- a/tgfx/src/gpu/opengl/GLProgramBuilder.cpp +++ b/tgfx/src/gpu/opengl/GLProgramBuilder.cpp @@ -129,7 +129,7 @@ void GLProgramBuilder::computeCountsAndStrides(unsigned int programID) { attribute.gpuType = attr->gpuType(); attribute.offset = vertexStride; vertexStride += static_cast(attr->sizeAlign4()); - attribute.location = _gl->getAttribLocation(programID, attr->name().c_str()); + attribute.location = _gl->functions->getAttribLocation(programID, attr->name().c_str()); if (attribute.location >= 0) { attributes.push_back(attribute); } diff --git a/tgfx/src/gpu/opengl/GLProgramCreator.cpp b/tgfx/src/gpu/opengl/GLProgramCreator.cpp index ef9c4b2f5e..5513929e0b 100644 --- a/tgfx/src/gpu/opengl/GLProgramCreator.cpp +++ b/tgfx/src/gpu/opengl/GLProgramCreator.cpp @@ -32,7 +32,7 @@ void GLProgramCreator::computeUniqueKey(Context* context, BytesKey* bytesKey) co } std::unique_ptr GLProgramCreator::createProgram(Context* context) const { - const auto* gl = GLContext::Unwrap(context); + auto gl = GLInterface::Get(context); return GLProgramBuilder::CreateProgram(gl, geometryProcessor, pipeline); } } // namespace tgfx diff --git a/tgfx/src/gpu/opengl/GLProgramDataManager.cpp b/tgfx/src/gpu/opengl/GLProgramDataManager.cpp index 16c756511f..b037caf237 100644 --- a/tgfx/src/gpu/opengl/GLProgramDataManager.cpp +++ b/tgfx/src/gpu/opengl/GLProgramDataManager.cpp @@ -30,28 +30,28 @@ GLProgramDataManager::GLProgramDataManager(const GLInterface* gl, const std::vec void GLProgramDataManager::set1f(UniformHandle handle, float v0) const { auto location = uniforms->at(handle.toIndex()); if (kUnusedUniform != location) { - gl->uniform1f(location, v0); + gl->functions->uniform1f(location, v0); } } void GLProgramDataManager::set2f(UniformHandle handle, float v0, float v1) const { auto location = uniforms->at(handle.toIndex()); if (kUnusedUniform != location) { - gl->uniform2f(location, v0, v1); + gl->functions->uniform2f(location, v0, v1); } } void GLProgramDataManager::set4fv(UniformHandle handle, int arrayCount, const float* v) const { auto location = uniforms->at(handle.toIndex()); if (kUnusedUniform != location) { - gl->uniform4fv(location, arrayCount, v); + gl->functions->uniform4fv(location, arrayCount, v); } } void GLProgramDataManager::setMatrix3f(UniformHandle handle, const float matrix[]) const { auto location = uniforms->at(handle.toIndex()); if (kUnusedUniform != location) { - gl->uniformMatrix3fv(location, 1, GL_FALSE, matrix); + gl->functions->uniformMatrix3fv(location, 1, GL_FALSE, matrix); } } diff --git a/tgfx/src/gpu/opengl/GLRenderTarget.cpp b/tgfx/src/gpu/opengl/GLRenderTarget.cpp index 8aec4dec6f..eeb3c5be29 100644 --- a/tgfx/src/gpu/opengl/GLRenderTarget.cpp +++ b/tgfx/src/gpu/opengl/GLRenderTarget.cpp @@ -54,14 +54,17 @@ static bool RenderbufferStorageMSAA(const GLInterface* gl, int sampleCount, Pixe auto format = gl->caps->getTextureFormat(pixelFormat).sizedFormat; switch (gl->caps->msFBOType) { case MSFBOType::Standard: - gl->renderbufferStorageMultisample(GL_RENDERBUFFER, sampleCount, format, width, height); + gl->functions->renderbufferStorageMultisample(GL_RENDERBUFFER, sampleCount, format, width, + height); break; case MSFBOType::ES_Apple: - gl->renderbufferStorageMultisampleAPPLE(GL_RENDERBUFFER, sampleCount, format, width, height); + gl->functions->renderbufferStorageMultisampleAPPLE(GL_RENDERBUFFER, sampleCount, format, + width, height); break; case MSFBOType::ES_EXT_MsToTexture: case MSFBOType::ES_IMG_MsToTexture: - gl->renderbufferStorageMultisampleEXT(GL_RENDERBUFFER, sampleCount, format, width, height); + gl->functions->renderbufferStorageMultisampleEXT(GL_RENDERBUFFER, sampleCount, format, width, + height); break; case MSFBOType::None: LOGE("Shouldn't be here if we don't support multisampled renderbuffers."); @@ -74,19 +77,20 @@ static void FrameBufferTexture2D(const GLInterface* gl, unsigned textureTarget, int sampleCount) { // 解绑的时候framebufferTexture2DMultisample在华为手机上会出现crash,统一走framebufferTexture2D解绑 if (textureID != 0 && sampleCount > 1 && gl->caps->usesImplicitMSAAResolve()) { - gl->framebufferTexture2DMultisample(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, textureTarget, - textureID, 0, sampleCount); + gl->functions->framebufferTexture2DMultisample(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, + textureTarget, textureID, 0, sampleCount); } else { - gl->framebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, textureTarget, textureID, 0); + gl->functions->framebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, textureTarget, + textureID, 0); } } static void ReleaseResource(Context* context, GLFrameBuffer* textureFBInfo, GLFrameBuffer* renderTargetFBInfo = nullptr, unsigned* msRenderBufferID = nullptr) { - auto gl = GLContext::Unwrap(context); + auto gl = GLInterface::Get(context); if (textureFBInfo && textureFBInfo->id) { - gl->deleteFramebuffers(1, &(textureFBInfo->id)); + gl->functions->deleteFramebuffers(1, &(textureFBInfo->id)); if (renderTargetFBInfo && renderTargetFBInfo->id == textureFBInfo->id) { renderTargetFBInfo->id = 0; } @@ -95,14 +99,15 @@ static void ReleaseResource(Context* context, GLFrameBuffer* textureFBInfo, if (renderTargetFBInfo && renderTargetFBInfo->id > 0) { { GLStateGuard stateGuard(context); - gl->bindFramebuffer(GL_FRAMEBUFFER, renderTargetFBInfo->id); - gl->framebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, 0); + gl->functions->bindFramebuffer(GL_FRAMEBUFFER, renderTargetFBInfo->id); + gl->functions->framebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, + 0); } - gl->deleteFramebuffers(1, &(renderTargetFBInfo->id)); + gl->functions->deleteFramebuffers(1, &(renderTargetFBInfo->id)); renderTargetFBInfo->id = 0; } if (msRenderBufferID && *msRenderBufferID > 0) { - gl->deleteRenderbuffers(1, msRenderBufferID); + gl->functions->deleteRenderbuffers(1, msRenderBufferID); *msRenderBufferID = 0; } } @@ -110,23 +115,23 @@ static void ReleaseResource(Context* context, GLFrameBuffer* textureFBInfo, static bool CreateRenderBuffer(const GLInterface* gl, const GLTexture* texture, GLFrameBuffer* renderTargetFBInfo, unsigned* msRenderBufferID, int sampleCount) { - gl->genFramebuffers(1, &(renderTargetFBInfo->id)); + gl->functions->genFramebuffers(1, &(renderTargetFBInfo->id)); if (renderTargetFBInfo->id == 0) { return false; } - gl->genRenderbuffers(1, msRenderBufferID); + gl->functions->genRenderbuffers(1, msRenderBufferID); if (*msRenderBufferID == 0) { return false; } - gl->bindRenderbuffer(GL_RENDERBUFFER, *msRenderBufferID); + gl->functions->bindRenderbuffer(GL_RENDERBUFFER, *msRenderBufferID); if (!RenderbufferStorageMSAA(gl, sampleCount, renderTargetFBInfo->format, texture->width(), texture->height())) { return false; } - gl->bindFramebuffer(GL_FRAMEBUFFER, renderTargetFBInfo->id); - gl->framebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, - *msRenderBufferID); - return gl->checkFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE; + gl->functions->bindFramebuffer(GL_FRAMEBUFFER, renderTargetFBInfo->id); + gl->functions->framebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, + *msRenderBufferID); + return gl->functions->checkFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE; } std::shared_ptr GLRenderTarget::MakeFrom(Context* context, const GLTexture* texture, @@ -134,10 +139,10 @@ std::shared_ptr GLRenderTarget::MakeFrom(Context* context, const if (texture == nullptr || context == nullptr) { return nullptr; } - auto gl = GLContext::Unwrap(context); + auto gl = GLInterface::Get(context); GLFrameBuffer textureFBInfo = {}; textureFBInfo.format = texture->glSampler().format; - gl->genFramebuffers(1, &textureFBInfo.id); + gl->functions->genFramebuffers(1, &textureFBInfo.id); if (textureFBInfo.id == 0) { return nullptr; } @@ -153,12 +158,12 @@ std::shared_ptr GLRenderTarget::MakeFrom(Context* context, const } else { renderTargetFBInfo = textureFBInfo; } - gl->bindFramebuffer(GL_FRAMEBUFFER, textureFBInfo.id); + gl->functions->bindFramebuffer(GL_FRAMEBUFFER, textureFBInfo.id); auto textureInfo = texture->glSampler(); FrameBufferTexture2D(gl, textureInfo.target, textureInfo.id, sampleCount); std::shared_ptr renderTarget = nullptr; - if (gl->checkFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { + if (gl->functions->checkFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { ReleaseResource(context, &textureFBInfo, &renderTargetFBInfo, &msRenderBufferID); } else { auto textureTarget = texture->glSampler().target; @@ -180,13 +185,13 @@ GLRenderTarget::GLRenderTarget(int width, int height, ImageOrigin origin, int sa void GLRenderTarget::clear(const GLInterface* gl) const { int oldFb = 0; - gl->getIntegerv(GL_FRAMEBUFFER_BINDING, &oldFb); - gl->bindFramebuffer(GL_FRAMEBUFFER, renderTargetFBInfo.id); - gl->viewport(0, 0, width(), height()); - gl->scissor(0, 0, width(), height()); - gl->clearColor(0.0f, 0.0f, 0.0f, 0.0f); - gl->clear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - gl->bindFramebuffer(GL_FRAMEBUFFER, oldFb); + gl->functions->getIntegerv(GL_FRAMEBUFFER_BINDING, &oldFb); + gl->functions->bindFramebuffer(GL_FRAMEBUFFER, renderTargetFBInfo.id); + gl->functions->viewport(0, 0, width(), height()); + gl->functions->scissor(0, 0, width(), height()); + gl->functions->clearColor(0.0f, 0.0f, 0.0f, 0.0f); + gl->functions->clear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + gl->functions->bindFramebuffer(GL_FRAMEBUFFER, oldFb); } static bool CanReadDirectly(const GLInterface* gl, ImageOrigin origin, const ImageInfo& srcInfo, @@ -231,9 +236,9 @@ bool GLRenderTarget::readPixels(Context* context, const ImageInfo& dstInfo, void } auto pixelFormat = renderTargetFBInfo.format; GLStateGuard stateGuard(context); - auto gl = GLContext::Unwrap(context); + auto gl = GLInterface::Get(context); const auto& format = gl->caps->getTextureFormat(pixelFormat); - gl->bindFramebuffer(GL_FRAMEBUFFER, renderTargetFBInfo.id); + gl->functions->bindFramebuffer(GL_FRAMEBUFFER, renderTargetFBInfo.id); auto colorType = pixelFormat == PixelFormat::ALPHA_8 ? ColorType::ALPHA_8 : ColorType::RGBA_8888; auto srcInfo = ImageInfo::Make(outInfo.width(), outInfo.height(), colorType, AlphaType::Premultiplied); @@ -242,23 +247,23 @@ bool GLRenderTarget::readPixels(Context* context, const ImageInfo& dstInfo, void if (CanReadDirectly(gl, origin(), srcInfo, outInfo)) { pixels = dstPixels; if (outInfo.rowBytes() != outInfo.minRowBytes()) { - gl->pixelStorei(GL_PACK_ROW_LENGTH, - static_cast(outInfo.rowBytes() / outInfo.bytesPerPixel())); + gl->functions->pixelStorei(GL_PACK_ROW_LENGTH, + static_cast(outInfo.rowBytes() / outInfo.bytesPerPixel())); } } else { tempPixels = new uint8_t[srcInfo.byteSize()]; pixels = tempPixels; } auto alignment = pixelFormat == PixelFormat::ALPHA_8 ? 1 : 4; - gl->pixelStorei(GL_PACK_ALIGNMENT, alignment); + gl->functions->pixelStorei(GL_PACK_ALIGNMENT, alignment); auto flipY = origin() == ImageOrigin::BottomLeft; auto readX = std::max(0, srcX); auto readY = std::max(0, srcY); if (flipY) { readY = height() - readY - outInfo.height(); } - gl->readPixels(readX, readY, outInfo.width(), outInfo.height(), format.externalFormat, - GL_UNSIGNED_BYTE, pixels); + gl->functions->readPixels(readX, readY, outInfo.width(), outInfo.height(), format.externalFormat, + GL_UNSIGNED_BYTE, pixels); if (tempPixels != nullptr) { CopyPixels(srcInfo, tempPixels, outInfo, dstPixels, flipY); delete[] tempPixels; @@ -270,24 +275,24 @@ void GLRenderTarget::resolve(Context* context) const { if (sampleCount() <= 1) { return; } - auto gl = GLContext::Unwrap(context); + auto gl = GLInterface::Get(context); if (!gl->caps->usesMSAARenderBuffers()) { return; } GLStateGuard stateGuard(context); - gl->bindFramebuffer(GL_READ_FRAMEBUFFER, renderTargetFBInfo.id); - gl->bindFramebuffer(GL_DRAW_FRAMEBUFFER, textureFBInfo.id); + gl->functions->bindFramebuffer(GL_READ_FRAMEBUFFER, renderTargetFBInfo.id); + gl->functions->bindFramebuffer(GL_DRAW_FRAMEBUFFER, textureFBInfo.id); if (gl->caps->msFBOType == MSFBOType::ES_Apple) { // Apple's extension uses the scissor as the blit bounds. - gl->enable(GL_SCISSOR_TEST); - gl->scissor(0, 0, width(), height()); - gl->resolveMultisampleFramebuffer(); - gl->disable(GL_SCISSOR_TEST); + gl->functions->enable(GL_SCISSOR_TEST); + gl->functions->scissor(0, 0, width(), height()); + gl->functions->resolveMultisampleFramebuffer(); + gl->functions->disable(GL_SCISSOR_TEST); } else { // BlitFrameBuffer respects the scissor, so disable it. - gl->disable(GL_SCISSOR_TEST); - gl->blitFramebuffer(0, 0, width(), height(), 0, 0, width(), height(), GL_COLOR_BUFFER_BIT, - GL_NEAREST); + gl->functions->disable(GL_SCISSOR_TEST); + gl->functions->blitFramebuffer(0, 0, width(), height(), 0, 0, width(), height(), + GL_COLOR_BUFFER_BIT, GL_NEAREST); } } @@ -299,8 +304,8 @@ void GLRenderTarget::onRelease(Context* context) { // The currently bound fboID may be the same as textureFBInfo.id, we must restore and then // delete, otherwise GL_INVALID_OPERATION(1282) will be reported。 GLStateGuard stateGuard(context); - auto gl = GLContext::Unwrap(context); - gl->bindFramebuffer(GL_FRAMEBUFFER, textureFBInfo.id); + auto gl = GLInterface::Get(context); + gl->functions->bindFramebuffer(GL_FRAMEBUFFER, textureFBInfo.id); FrameBufferTexture2D(gl, textureTarget, 0, sampleCount()); } ReleaseResource(context, &textureFBInfo, &renderTargetFBInfo, &msRenderBufferID); diff --git a/tgfx/src/gpu/opengl/GLSampler.cpp b/tgfx/src/gpu/opengl/GLSampler.cpp index cfe2ca5cf6..beae3122d4 100644 --- a/tgfx/src/gpu/opengl/GLSampler.cpp +++ b/tgfx/src/gpu/opengl/GLSampler.cpp @@ -21,7 +21,7 @@ namespace tgfx { void GLSampler::computeKey(Context* context, BytesKey* bytesKey) const { - const auto* gl = GLContext::Unwrap(context); + auto gl = GLInterface::Get(context); bytesKey->write(static_cast(gl->caps->getTextureSwizzle(format).asKey())); bytesKey->write(target); } diff --git a/tgfx/src/gpu/opengl/GLState.cpp b/tgfx/src/gpu/opengl/GLState.cpp index 1d7aac96cc..eceeb0aac2 100644 --- a/tgfx/src/gpu/opengl/GLState.cpp +++ b/tgfx/src/gpu/opengl/GLState.cpp @@ -51,7 +51,7 @@ namespace tgfx { class ActiveTexture : public GLAttribute { public: explicit ActiveTexture(const GLInterface* gl) { - gl->getIntegerv(GL_ACTIVE_TEXTURE, &activeTexture); + gl->functions->getIntegerv(GL_ACTIVE_TEXTURE, &activeTexture); } GLAttributeType type() const override { @@ -63,7 +63,7 @@ class ActiveTexture : public GLAttribute { } void apply(GLState* state) const override { - state->gl->activeTexture(activeTexture); + state->gl->functions->activeTexture(activeTexture); state->currentTextureUnit = activeTexture; } @@ -73,8 +73,8 @@ class ActiveTexture : public GLAttribute { class BlendEquationSeparate : public GLAttribute { public: explicit BlendEquationSeparate(const GLInterface* gl) { - gl->getIntegerv(GL_BLEND_EQUATION_RGB, &equationRGB); - gl->getIntegerv(GL_BLEND_EQUATION_ALPHA, &equationAlpha); + gl->functions->getIntegerv(GL_BLEND_EQUATION_RGB, &equationRGB); + gl->functions->getIntegerv(GL_BLEND_EQUATION_ALPHA, &equationAlpha); } GLAttributeType type() const override { @@ -86,7 +86,7 @@ class BlendEquationSeparate : public GLAttribute { } void apply(GLState* state) const override { - state->gl->blendEquationSeparate(equationRGB, equationAlpha); + state->gl->functions->blendEquationSeparate(equationRGB, equationAlpha); } int equationRGB = 0; @@ -96,10 +96,10 @@ class BlendEquationSeparate : public GLAttribute { class BlendFuncSeparate : public GLAttribute { public: explicit BlendFuncSeparate(const GLInterface* gl) { - gl->getIntegerv(GL_BLEND_SRC_RGB, &srcRGB); - gl->getIntegerv(GL_BLEND_DST_RGB, &dstRGB); - gl->getIntegerv(GL_BLEND_SRC_ALPHA, &srcAlpha); - gl->getIntegerv(GL_BLEND_DST_ALPHA, &dstAlpha); + gl->functions->getIntegerv(GL_BLEND_SRC_RGB, &srcRGB); + gl->functions->getIntegerv(GL_BLEND_DST_RGB, &dstRGB); + gl->functions->getIntegerv(GL_BLEND_SRC_ALPHA, &srcAlpha); + gl->functions->getIntegerv(GL_BLEND_DST_ALPHA, &dstAlpha); } GLAttributeType type() const override { @@ -111,7 +111,7 @@ class BlendFuncSeparate : public GLAttribute { } void apply(GLState* state) const override { - state->gl->blendFuncSeparate(srcRGB, dstRGB, srcAlpha, dstAlpha); + state->gl->functions->blendFuncSeparate(srcRGB, dstRGB, srcAlpha, dstAlpha); } int srcRGB = 0; @@ -123,7 +123,7 @@ class BlendFuncSeparate : public GLAttribute { class CurrentProgram : public GLAttribute { public: explicit CurrentProgram(const GLInterface* gl) { - gl->getIntegerv(GL_CURRENT_PROGRAM, &program); + gl->functions->getIntegerv(GL_CURRENT_PROGRAM, &program); } GLAttributeType type() const override { @@ -135,7 +135,7 @@ class CurrentProgram : public GLAttribute { } void apply(GLState* state) const override { - state->gl->useProgram(program); + state->gl->functions->useProgram(program); } int program = 0; @@ -144,7 +144,7 @@ class CurrentProgram : public GLAttribute { class EnableBlend : public GLAttribute { public: explicit EnableBlend(const GLInterface* gl) { - enabled = gl->isEnabled(GL_BLEND); + enabled = gl->functions->isEnabled(GL_BLEND); } GLAttributeType type() const override { @@ -157,9 +157,9 @@ class EnableBlend : public GLAttribute { void apply(GLState* state) const override { if (enabled) { - state->gl->enable(GL_BLEND); + state->gl->functions->enable(GL_BLEND); } else { - state->gl->disable(GL_BLEND); + state->gl->functions->disable(GL_BLEND); } } @@ -169,7 +169,7 @@ class EnableBlend : public GLAttribute { class EnableCullFace : public GLAttribute { public: explicit EnableCullFace(const GLInterface* gl) { - enabled = gl->isEnabled(GL_CULL_FACE); + enabled = gl->functions->isEnabled(GL_CULL_FACE); } GLAttributeType type() const override { @@ -182,9 +182,9 @@ class EnableCullFace : public GLAttribute { void apply(GLState* state) const override { if (enabled) { - state->gl->enable(GL_CULL_FACE); + state->gl->functions->enable(GL_CULL_FACE); } else { - state->gl->disable(GL_CULL_FACE); + state->gl->functions->disable(GL_CULL_FACE); } } @@ -194,7 +194,7 @@ class EnableCullFace : public GLAttribute { class EnableDepth : public GLAttribute { public: explicit EnableDepth(const GLInterface* gl) { - enabled = gl->isEnabled(GL_DEPTH_TEST); + enabled = gl->functions->isEnabled(GL_DEPTH_TEST); } GLAttributeType type() const override { @@ -207,9 +207,9 @@ class EnableDepth : public GLAttribute { void apply(GLState* state) const override { if (enabled) { - state->gl->enable(GL_DEPTH_TEST); + state->gl->functions->enable(GL_DEPTH_TEST); } else { - state->gl->disable(GL_DEPTH_TEST); + state->gl->functions->disable(GL_DEPTH_TEST); } } @@ -219,7 +219,7 @@ class EnableDepth : public GLAttribute { class EnableDither : public GLAttribute { public: explicit EnableDither(const GLInterface* gl) { - enabled = gl->isEnabled(GL_DITHER); + enabled = gl->functions->isEnabled(GL_DITHER); } GLAttributeType type() const override { @@ -232,9 +232,9 @@ class EnableDither : public GLAttribute { void apply(GLState* state) const override { if (enabled) { - state->gl->enable(GL_DITHER); + state->gl->functions->enable(GL_DITHER); } else { - state->gl->disable(GL_DITHER); + state->gl->functions->disable(GL_DITHER); } } @@ -244,7 +244,7 @@ class EnableDither : public GLAttribute { class EnableFramebufferSRGB : public GLAttribute { public: explicit EnableFramebufferSRGB(const GLInterface* gl) { - enabled = gl->isEnabled(GL_FRAMEBUFFER_SRGB); + enabled = gl->functions->isEnabled(GL_FRAMEBUFFER_SRGB); } GLAttributeType type() const override { @@ -257,9 +257,9 @@ class EnableFramebufferSRGB : public GLAttribute { void apply(GLState* state) const override { if (enabled) { - state->gl->enable(GL_FRAMEBUFFER_SRGB); + state->gl->functions->enable(GL_FRAMEBUFFER_SRGB); } else { - state->gl->disable(GL_FRAMEBUFFER_SRGB); + state->gl->functions->disable(GL_FRAMEBUFFER_SRGB); } } @@ -269,7 +269,7 @@ class EnableFramebufferSRGB : public GLAttribute { class EnableScissor : public GLAttribute { public: explicit EnableScissor(const GLInterface* gl) { - enabled = gl->isEnabled(GL_SCISSOR_TEST); + enabled = gl->functions->isEnabled(GL_SCISSOR_TEST); } GLAttributeType type() const override { @@ -282,9 +282,9 @@ class EnableScissor : public GLAttribute { void apply(GLState* state) const override { if (enabled) { - state->gl->enable(GL_SCISSOR_TEST); + state->gl->functions->enable(GL_SCISSOR_TEST); } else { - state->gl->disable(GL_SCISSOR_TEST); + state->gl->functions->disable(GL_SCISSOR_TEST); } } @@ -294,7 +294,7 @@ class EnableScissor : public GLAttribute { class EnableStencil : public GLAttribute { public: explicit EnableStencil(const GLInterface* gl) { - enabled = gl->isEnabled(GL_STENCIL_TEST); + enabled = gl->functions->isEnabled(GL_STENCIL_TEST); } GLAttributeType type() const override { @@ -307,9 +307,9 @@ class EnableStencil : public GLAttribute { void apply(GLState* state) const override { if (enabled) { - state->gl->enable(GL_STENCIL_TEST); + state->gl->functions->enable(GL_STENCIL_TEST); } else { - state->gl->disable(GL_STENCIL_TEST); + state->gl->functions->disable(GL_STENCIL_TEST); } } @@ -319,7 +319,7 @@ class EnableStencil : public GLAttribute { class EnableVertexProgramPointSize : public GLAttribute { public: explicit EnableVertexProgramPointSize(const GLInterface* gl) { - enabled = gl->isEnabled(GL_VERTEX_PROGRAM_POINT_SIZE); + enabled = gl->functions->isEnabled(GL_VERTEX_PROGRAM_POINT_SIZE); } GLAttributeType type() const override { @@ -332,9 +332,9 @@ class EnableVertexProgramPointSize : public GLAttribute { void apply(GLState* state) const override { if (enabled) { - state->gl->enable(GL_VERTEX_PROGRAM_POINT_SIZE); + state->gl->functions->enable(GL_VERTEX_PROGRAM_POINT_SIZE); } else { - state->gl->disable(GL_VERTEX_PROGRAM_POINT_SIZE); + state->gl->functions->disable(GL_VERTEX_PROGRAM_POINT_SIZE); } } @@ -344,7 +344,7 @@ class EnableVertexProgramPointSize : public GLAttribute { class EnableMultisample : public GLAttribute { public: explicit EnableMultisample(const GLInterface* gl) { - enabled = gl->isEnabled(GL_MULTISAMPLE); + enabled = gl->functions->isEnabled(GL_MULTISAMPLE); } GLAttributeType type() const override { @@ -357,9 +357,9 @@ class EnableMultisample : public GLAttribute { void apply(GLState* state) const override { if (enabled) { - state->gl->enable(GL_MULTISAMPLE); + state->gl->functions->enable(GL_MULTISAMPLE); } else { - state->gl->disable(GL_MULTISAMPLE); + state->gl->functions->disable(GL_MULTISAMPLE); } } @@ -369,7 +369,7 @@ class EnableMultisample : public GLAttribute { class EnableFetchPerSampleARM : public GLAttribute { public: explicit EnableFetchPerSampleARM(const GLInterface* gl) { - enabled = gl->isEnabled(GL_FETCH_PER_SAMPLE_ARM); + enabled = gl->functions->isEnabled(GL_FETCH_PER_SAMPLE_ARM); } GLAttributeType type() const override { @@ -382,9 +382,9 @@ class EnableFetchPerSampleARM : public GLAttribute { void apply(GLState* state) const override { if (enabled) { - state->gl->enable(GL_FETCH_PER_SAMPLE_ARM); + state->gl->functions->enable(GL_FETCH_PER_SAMPLE_ARM); } else { - state->gl->disable(GL_FETCH_PER_SAMPLE_ARM); + state->gl->functions->disable(GL_FETCH_PER_SAMPLE_ARM); } } @@ -394,7 +394,7 @@ class EnableFetchPerSampleARM : public GLAttribute { class EnablePolygonOffsetFill : public GLAttribute { public: explicit EnablePolygonOffsetFill(const GLInterface* gl) { - enabled = gl->isEnabled(GL_POLYGON_OFFSET_FILL); + enabled = gl->functions->isEnabled(GL_POLYGON_OFFSET_FILL); } GLAttributeType type() const override { @@ -407,9 +407,9 @@ class EnablePolygonOffsetFill : public GLAttribute { void apply(GLState* state) const override { if (enabled) { - state->gl->enable(GL_POLYGON_OFFSET_FILL); + state->gl->functions->enable(GL_POLYGON_OFFSET_FILL); } else { - state->gl->disable(GL_POLYGON_OFFSET_FILL); + state->gl->functions->disable(GL_POLYGON_OFFSET_FILL); } } @@ -419,7 +419,7 @@ class EnablePolygonOffsetFill : public GLAttribute { class ElementBufferBinding : public GLAttribute { public: explicit ElementBufferBinding(const GLInterface* gl) { - gl->getIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &buffer); + gl->functions->getIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &buffer); } GLAttributeType type() const override { @@ -431,7 +431,7 @@ class ElementBufferBinding : public GLAttribute { } void apply(GLState* state) const override { - state->gl->bindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer); + state->gl->functions->bindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer); } int buffer = 0; @@ -440,7 +440,7 @@ class ElementBufferBinding : public GLAttribute { class FrameBufferBinding : public GLAttribute { public: explicit FrameBufferBinding(const GLInterface* gl) { - gl->getIntegerv(GL_FRAMEBUFFER_BINDING, &buffer); + gl->functions->getIntegerv(GL_FRAMEBUFFER_BINDING, &buffer); } GLAttributeType type() const override { @@ -452,7 +452,7 @@ class FrameBufferBinding : public GLAttribute { } void apply(GLState* state) const override { - state->gl->bindFramebuffer(GL_FRAMEBUFFER, buffer); + state->gl->functions->bindFramebuffer(GL_FRAMEBUFFER, buffer); } int buffer = 0; @@ -461,7 +461,7 @@ class FrameBufferBinding : public GLAttribute { class ReadFrameBufferBinding : public GLAttribute { public: explicit ReadFrameBufferBinding(const GLInterface* gl) { - gl->getIntegerv(GL_READ_FRAMEBUFFER_BINDING, &buffer); + gl->functions->getIntegerv(GL_READ_FRAMEBUFFER_BINDING, &buffer); } GLAttributeType type() const override { @@ -473,7 +473,7 @@ class ReadFrameBufferBinding : public GLAttribute { } void apply(GLState* state) const override { - state->gl->bindFramebuffer(GL_READ_FRAMEBUFFER, buffer); + state->gl->functions->bindFramebuffer(GL_READ_FRAMEBUFFER, buffer); } int buffer = 0; @@ -482,7 +482,7 @@ class ReadFrameBufferBinding : public GLAttribute { class DrawFrameBufferBinding : public GLAttribute { public: explicit DrawFrameBufferBinding(const GLInterface* gl) { - gl->getIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &buffer); + gl->functions->getIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &buffer); } GLAttributeType type() const override { @@ -494,7 +494,7 @@ class DrawFrameBufferBinding : public GLAttribute { } void apply(GLState* state) const override { - state->gl->bindFramebuffer(GL_DRAW_FRAMEBUFFER, buffer); + state->gl->functions->bindFramebuffer(GL_DRAW_FRAMEBUFFER, buffer); } int buffer = 0; @@ -503,7 +503,7 @@ class DrawFrameBufferBinding : public GLAttribute { class RenderBufferBinding : public GLAttribute { public: explicit RenderBufferBinding(const GLInterface* gl) { - gl->getIntegerv(GL_RENDERBUFFER_BINDING, &buffer); + gl->functions->getIntegerv(GL_RENDERBUFFER_BINDING, &buffer); } GLAttributeType type() const override { @@ -515,7 +515,7 @@ class RenderBufferBinding : public GLAttribute { } void apply(GLState* state) const override { - state->gl->bindRenderbuffer(GL_RENDERBUFFER, buffer); + state->gl->functions->bindRenderbuffer(GL_RENDERBUFFER, buffer); } int buffer = 0; @@ -524,7 +524,7 @@ class RenderBufferBinding : public GLAttribute { class PackAlignment : public GLAttribute { public: explicit PackAlignment(const GLInterface* gl) { - gl->getIntegerv(GL_PACK_ALIGNMENT, &alignment); + gl->functions->getIntegerv(GL_PACK_ALIGNMENT, &alignment); } GLAttributeType type() const override { @@ -536,7 +536,7 @@ class PackAlignment : public GLAttribute { } void apply(GLState* state) const override { - state->gl->pixelStorei(GL_PACK_ALIGNMENT, alignment); + state->gl->functions->pixelStorei(GL_PACK_ALIGNMENT, alignment); } int alignment = 0; @@ -545,7 +545,7 @@ class PackAlignment : public GLAttribute { class PackRowLength : public GLAttribute { public: explicit PackRowLength(const GLInterface* gl) { - gl->getIntegerv(GL_PACK_ROW_LENGTH, &rowLength); + gl->functions->getIntegerv(GL_PACK_ROW_LENGTH, &rowLength); } GLAttributeType type() const override { @@ -557,7 +557,7 @@ class PackRowLength : public GLAttribute { } void apply(GLState* state) const override { - state->gl->pixelStorei(GL_PACK_ROW_LENGTH, rowLength); + state->gl->functions->pixelStorei(GL_PACK_ROW_LENGTH, rowLength); } int rowLength = 0; @@ -566,7 +566,7 @@ class PackRowLength : public GLAttribute { class ScissorBox : public GLAttribute { public: explicit ScissorBox(const GLInterface* gl) { - gl->getIntegerv(GL_SCISSOR_BOX, box); + gl->functions->getIntegerv(GL_SCISSOR_BOX, box); } GLAttributeType type() const override { @@ -578,7 +578,7 @@ class ScissorBox : public GLAttribute { } void apply(GLState* state) const override { - state->gl->scissor(box[0], box[1], box[2], box[3]); + state->gl->functions->scissor(box[0], box[1], box[2], box[3]); } int box[4] = {}; @@ -590,13 +590,13 @@ class TextureBinding : public GLAttribute { : textureUnit(textureUnit), textureTarget(textureTarget) { switch (textureTarget) { case GL_TEXTURE_2D: - gl->getIntegerv(GL_TEXTURE_BINDING_2D, &textureID); + gl->functions->getIntegerv(GL_TEXTURE_BINDING_2D, &textureID); break; case GL_TEXTURE_EXTERNAL_OES: - gl->getIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &textureID); + gl->functions->getIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &textureID); break; case GL_TEXTURE_RECTANGLE: - gl->getIntegerv(GL_TEXTURE_BINDING_RECTANGLE, &textureID); + gl->functions->getIntegerv(GL_TEXTURE_BINDING_RECTANGLE, &textureID); break; default: UNSUPPORTED_STATE_WARNING() @@ -613,8 +613,8 @@ class TextureBinding : public GLAttribute { } void apply(GLState* state) const override { - state->gl->activeTexture(textureUnit); - state->gl->bindTexture(textureTarget, textureID); + state->gl->functions->activeTexture(textureUnit); + state->gl->functions->bindTexture(textureTarget, textureID); } private: @@ -626,7 +626,7 @@ class TextureBinding : public GLAttribute { class UnpackAlignment : public GLAttribute { public: explicit UnpackAlignment(const GLInterface* gl) { - gl->getIntegerv(GL_UNPACK_ALIGNMENT, &alignment); + gl->functions->getIntegerv(GL_UNPACK_ALIGNMENT, &alignment); } GLAttributeType type() const override { @@ -638,7 +638,7 @@ class UnpackAlignment : public GLAttribute { } void apply(GLState* state) const override { - state->gl->pixelStorei(GL_UNPACK_ALIGNMENT, alignment); + state->gl->functions->pixelStorei(GL_UNPACK_ALIGNMENT, alignment); } int alignment = 0; @@ -647,7 +647,7 @@ class UnpackAlignment : public GLAttribute { class UnpackRowLength : public GLAttribute { public: explicit UnpackRowLength(const GLInterface* gl) { - gl->getIntegerv(GL_UNPACK_ROW_LENGTH, &rowLength); + gl->functions->getIntegerv(GL_UNPACK_ROW_LENGTH, &rowLength); } GLAttributeType type() const override { @@ -659,7 +659,7 @@ class UnpackRowLength : public GLAttribute { } void apply(GLState* state) const override { - state->gl->pixelStorei(GL_UNPACK_ROW_LENGTH, rowLength); + state->gl->functions->pixelStorei(GL_UNPACK_ROW_LENGTH, rowLength); } int rowLength = 0; @@ -668,7 +668,7 @@ class UnpackRowLength : public GLAttribute { class VertexArrayBinding : public GLAttribute { public: explicit VertexArrayBinding(const GLInterface* gl) { - gl->getIntegerv(GL_VERTEX_ARRAY_BINDING, &vertexArray); + gl->functions->getIntegerv(GL_VERTEX_ARRAY_BINDING, &vertexArray); } GLAttributeType type() const override { @@ -680,7 +680,7 @@ class VertexArrayBinding : public GLAttribute { } void apply(GLState* state) const override { - state->gl->bindVertexArray(vertexArray); + state->gl->functions->bindVertexArray(vertexArray); state->currentVAO = vertexArray; } @@ -690,13 +690,13 @@ class VertexArrayBinding : public GLAttribute { class VertexAttribute : public GLAttribute { public: explicit VertexAttribute(const GLInterface* gl, unsigned index) : index(index) { - gl->getVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING, &vbo); - gl->getVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_ENABLED, &enabled); - gl->getVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_SIZE, &size); - gl->getVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_TYPE, &dataType); - gl->getVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_NORMALIZED, &normalized); - gl->getVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_STRIDE, &stride); - gl->getVertexAttribPointerv(index, GL_VERTEX_ATTRIB_ARRAY_POINTER, &pointer); + gl->functions->getVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING, &vbo); + gl->functions->getVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_ENABLED, &enabled); + gl->functions->getVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_SIZE, &size); + gl->functions->getVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_TYPE, &dataType); + gl->functions->getVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_NORMALIZED, &normalized); + gl->functions->getVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_STRIDE, &stride); + gl->functions->getVertexAttribPointerv(index, GL_VERTEX_ATTRIB_ARRAY_POINTER, &pointer); } GLAttributeType type() const override { @@ -708,12 +708,12 @@ class VertexAttribute : public GLAttribute { } void apply(GLState* state) const override { - state->gl->bindBuffer(GL_ARRAY_BUFFER, vbo); - state->gl->vertexAttribPointer(index, size, dataType, normalized, stride, pointer); + state->gl->functions->bindBuffer(GL_ARRAY_BUFFER, vbo); + state->gl->functions->vertexAttribPointer(index, size, dataType, normalized, stride, pointer); if (enabled == GL_TRUE) { - state->gl->enableVertexAttribArray(index); + state->gl->functions->enableVertexAttribArray(index); } else { - state->gl->disableVertexAttribArray(index); + state->gl->functions->disableVertexAttribArray(index); } } @@ -731,7 +731,7 @@ class VertexAttribute : public GLAttribute { class VertexBufferBinding : public GLAttribute { public: explicit VertexBufferBinding(const GLInterface* gl) { - gl->getIntegerv(GL_ARRAY_BUFFER_BINDING, &buffer); + gl->functions->getIntegerv(GL_ARRAY_BUFFER_BINDING, &buffer); } GLAttributeType type() const override { @@ -743,7 +743,7 @@ class VertexBufferBinding : public GLAttribute { } void apply(GLState* state) const override { - state->gl->bindBuffer(GL_ARRAY_BUFFER, buffer); + state->gl->functions->bindBuffer(GL_ARRAY_BUFFER, buffer); } int buffer = 0; @@ -752,7 +752,7 @@ class VertexBufferBinding : public GLAttribute { class DepthMask : public GLAttribute { public: explicit DepthMask(const GLInterface* gl) { - gl->getBooleanv(GL_DEPTH_WRITEMASK, &flag); + gl->functions->getBooleanv(GL_DEPTH_WRITEMASK, &flag); } GLAttributeType type() const override { @@ -764,7 +764,7 @@ class DepthMask : public GLAttribute { } void apply(GLState* state) const override { - state->gl->depthMask(flag); + state->gl->functions->depthMask(flag); } unsigned char flag = false; @@ -773,7 +773,7 @@ class DepthMask : public GLAttribute { class Viewport : public GLAttribute { public: explicit Viewport(const GLInterface* gl) { - gl->getIntegerv(GL_VIEWPORT, viewport); + gl->functions->getIntegerv(GL_VIEWPORT, viewport); } GLAttributeType type() const override { @@ -785,7 +785,7 @@ class Viewport : public GLAttribute { } void apply(GLState* state) const override { - state->gl->viewport(viewport[0], viewport[1], viewport[2], viewport[3]); + state->gl->functions->viewport(viewport[0], viewport[1], viewport[2], viewport[3]); } int viewport[4] = {}; @@ -798,11 +798,11 @@ GLState::GLState(const GLInterface* gl) : gl(gl) { void GLState::reset() { if (gl->caps->vertexArrayObjectSupport) { int vao = 0; - gl->getIntegerv(GL_VERTEX_ARRAY_BINDING, &vao); + gl->functions->getIntegerv(GL_VERTEX_ARRAY_BINDING, &vao); currentVAO = vao; } int texture = 0; - gl->getIntegerv(GL_ACTIVE_TEXTURE, &texture); + gl->functions->getIntegerv(GL_ACTIVE_TEXTURE, &texture); currentTextureUnit = texture; currentRecord = nullptr; recordList = {}; @@ -836,28 +836,28 @@ void GLState::activeTexture(unsigned textureUnit) { } SAVE_DEFAULT(ActiveTexture) currentTextureUnit = textureUnit; - gl->activeTexture(textureUnit); + gl->functions->activeTexture(textureUnit); } void GLState::blendEquation(unsigned mode) { SAVE_DEFAULT(BlendEquationSeparate) - gl->blendEquation(mode); + gl->functions->blendEquation(mode); } void GLState::blendEquationSeparate(unsigned modeRGB, unsigned modeAlpha) { SAVE_DEFAULT(BlendEquationSeparate) - gl->blendEquationSeparate(modeRGB, modeAlpha); + gl->functions->blendEquationSeparate(modeRGB, modeAlpha); } void GLState::blendFunc(unsigned int srcFactor, unsigned int dstFactor) { SAVE_DEFAULT(BlendFuncSeparate) - gl->blendFunc(srcFactor, dstFactor); + gl->functions->blendFunc(srcFactor, dstFactor); } void GLState::blendFuncSeparate(unsigned srcRGB, unsigned dstRGB, unsigned srcAlpha, unsigned dstAlpha) { SAVE_DEFAULT(BlendFuncSeparate) - gl->blendFuncSeparate(srcRGB, dstRGB, srcAlpha, dstAlpha); + gl->functions->blendFuncSeparate(srcRGB, dstRGB, srcAlpha, dstAlpha); } void GLState::bindFramebuffer(unsigned target, unsigned framebuffer) { @@ -875,7 +875,7 @@ void GLState::bindFramebuffer(unsigned target, unsigned framebuffer) { UNSUPPORTED_STATE_WARNING() break; } - gl->bindFramebuffer(target, framebuffer); + gl->functions->bindFramebuffer(target, framebuffer); } void GLState::bindRenderbuffer(unsigned int target, unsigned int renderbuffer) { @@ -884,7 +884,7 @@ void GLState::bindRenderbuffer(unsigned int target, unsigned int renderbuffer) { } else { UNSUPPORTED_STATE_WARNING() } - gl->bindRenderbuffer(target, renderbuffer); + gl->functions->bindRenderbuffer(target, renderbuffer); } void GLState::bindBuffer(unsigned target, unsigned buffer) { @@ -903,7 +903,7 @@ void GLState::bindBuffer(unsigned target, unsigned buffer) { UNSUPPORTED_STATE_WARNING() break; } - gl->bindBuffer(target, buffer); + gl->functions->bindBuffer(target, buffer); } void GLState::bindTexture(unsigned target, unsigned texture) { @@ -916,7 +916,7 @@ void GLState::bindTexture(unsigned target, unsigned texture) { insertAttribute(std::make_shared(gl, currentTextureUnit, target)); } } - gl->bindTexture(target, texture); + gl->functions->bindTexture(target, texture); } void GLState::bindVertexArray(unsigned vertexArray) { @@ -925,7 +925,7 @@ void GLState::bindVertexArray(unsigned vertexArray) { } SAVE_DEFAULT(VertexArrayBinding) currentVAO = vertexArray; - gl->bindVertexArray(vertexArray); + gl->functions->bindVertexArray(vertexArray); } static void SaveDefaultBlend(GLState* state) { @@ -989,12 +989,12 @@ void GLState::disable(unsigned cap) { if (!found) { // UNSUPPORTED_STATE_WARNING() } - gl->disable(cap); + gl->functions->disable(cap); } void GLState::disableVertexAttribArray(unsigned index) { saveVertexAttribute(index); - gl->disableVertexAttribArray(index); + gl->functions->disableVertexAttribArray(index); } void GLState::enable(unsigned cap) { @@ -1009,12 +1009,12 @@ void GLState::enable(unsigned cap) { if (!found) { UNSUPPORTED_STATE_WARNING() } - gl->enable(cap); + gl->functions->enable(cap); } void GLState::enableVertexAttribArray(unsigned index) { saveVertexAttribute(index); - gl->enableVertexAttribArray(index); + gl->functions->enableVertexAttribArray(index); } void GLState::pixelStorei(unsigned int name, int param) { @@ -1035,33 +1035,33 @@ void GLState::pixelStorei(unsigned int name, int param) { UNSUPPORTED_STATE_WARNING() break; } - gl->pixelStorei(name, param); + gl->functions->pixelStorei(name, param); } void GLState::scissor(int x, int y, int width, int height) { SAVE_DEFAULT(ScissorBox) - gl->scissor(x, y, width, height); + gl->functions->scissor(x, y, width, height); } void GLState::viewport(int x, int y, int width, int height) { SAVE_DEFAULT(Viewport) - gl->viewport(x, y, width, height); + gl->functions->viewport(x, y, width, height); } void GLState::useProgram(unsigned program) { SAVE_DEFAULT(CurrentProgram) - gl->useProgram(program); + gl->functions->useProgram(program); } void GLState::vertexAttribPointer(unsigned index, int size, unsigned type, unsigned char normalized, int stride, const void* ptr) { saveVertexAttribute(index); - gl->vertexAttribPointer(index, size, type, normalized, stride, ptr); + gl->functions->vertexAttribPointer(index, size, type, normalized, stride, ptr); } void GLState::depthMask(unsigned char flag) { SAVE_DEFAULT(DepthMask); - gl->depthMask(flag); + gl->functions->depthMask(flag); } void GLState::saveVertexAttribute(unsigned int index) { diff --git a/tgfx/src/gpu/opengl/GLSurface.cpp b/tgfx/src/gpu/opengl/GLSurface.cpp index 144aec1caf..bf34f37b5c 100644 --- a/tgfx/src/gpu/opengl/GLSurface.cpp +++ b/tgfx/src/gpu/opengl/GLSurface.cpp @@ -50,7 +50,7 @@ std::shared_ptr Surface::Make(Context* context, int width, int height, auto pixelFormat = alphaOnly ? PixelFormat::ALPHA_8 : PixelFormat::RGBA_8888; std::shared_ptr texture; if (alphaOnly) { - if (GLContext::Unwrap(context)->caps->textureRedSupport) { + if (GLInterface::Get(context)->caps->textureRedSupport) { texture = std::static_pointer_cast(Texture::MakeAlpha(context, width, height)); } } else { @@ -59,7 +59,7 @@ std::shared_ptr Surface::Make(Context* context, int width, int height, if (texture == nullptr) { return nullptr; } - auto gl = GLContext::Unwrap(context); + auto gl = GLInterface::Get(context); sampleCount = gl->caps->getSampleCount(sampleCount, pixelFormat); auto renderTarget = GLRenderTarget::MakeFrom(context, texture.get(), sampleCount); if (renderTarget == nullptr) { @@ -92,12 +92,12 @@ bool GLSurface::wait(const Semaphore* semaphore) { if (glSync == nullptr) { return false; } - const auto* gl = GLContext::Unwrap(getContext()); + auto gl = GLInterface::Get(getContext()); if (!gl->caps->semaphoreSupport) { return false; } - gl->waitSync(glSync, 0, GL_TIMEOUT_IGNORED); - gl->deleteSync(glSync); + gl->functions->waitSync(glSync, 0, GL_TIMEOUT_IGNORED); + gl->functions->deleteSync(glSync); return true; } @@ -108,15 +108,15 @@ bool GLSurface::flush(Semaphore* semaphore) { } return false; } - const auto* gl = GLContext::Unwrap(getContext()); + auto gl = GLInterface::Get(getContext()); if (!gl->caps->semaphoreSupport) { return false; } - auto* sync = gl->fenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0); + auto* sync = gl->functions->fenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0); if (sync) { static_cast(semaphore)->glSync = sync; // If we inserted semaphores during the flush, we need to call glFlush. - gl->flush(); + gl->functions->flush(); return true; } return false; diff --git a/tgfx/src/gpu/opengl/GLTexture.cpp b/tgfx/src/gpu/opengl/GLTexture.cpp index e5c3a3b874..b340e29e23 100644 --- a/tgfx/src/gpu/opengl/GLTexture.cpp +++ b/tgfx/src/gpu/opengl/GLTexture.cpp @@ -32,8 +32,8 @@ class GLBackendTexture : public GLTexture { protected: void onRelease(Context* context) override { if (adopted) { - auto gl = GLContext::Unwrap(context); - gl->deleteTextures(1, &sampler.id); + auto gl = GLInterface::Get(context); + gl->functions->deleteTextures(1, &sampler.id); } } @@ -79,8 +79,8 @@ class GLAlphaTexture : public GLTexture { } void onRelease(Context* context) override { - auto gl = GLContext::Unwrap(context); - gl->deleteTextures(1, &sampler.id); + auto gl = GLInterface::Get(context); + gl->functions->deleteTextures(1, &sampler.id); } }; @@ -105,8 +105,8 @@ class GLRGBATexture : public GLTexture { } void onRelease(Context* context) override { - auto gl = GLContext::Unwrap(context); - gl->deleteTextures(1, &sampler.id); + auto gl = GLInterface::Get(context); + gl->functions->deleteTextures(1, &sampler.id); } }; @@ -117,7 +117,7 @@ static bool CheckMaxTextureSize(const GLInterface* gl, int width, int height) { std::shared_ptr Texture::Make(Context* context, int width, int height, void* pixels, size_t rowBytes, ImageOrigin origin, bool alphaOnly) { - auto gl = GLContext::Unwrap(context); + auto gl = GLInterface::Get(context); // Clear the previously generated GLError, causing the subsequent CheckGLError to return an // incorrect result. CheckGLError(gl); @@ -144,21 +144,21 @@ std::shared_ptr Texture::Make(Context* context, int width, int height, } else { sampler.target = GL_TEXTURE_2D; sampler.format = pixelFormat; - gl->genTextures(1, &(sampler.id)); + gl->functions->genTextures(1, &(sampler.id)); if (sampler.id == 0) { return nullptr; } - gl->bindTexture(sampler.target, sampler.id); - gl->texParameteri(sampler.target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - gl->texParameteri(sampler.target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - gl->texParameteri(sampler.target, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - gl->texParameteri(sampler.target, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + gl->functions->bindTexture(sampler.target, sampler.id); + gl->functions->texParameteri(sampler.target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + gl->functions->texParameteri(sampler.target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + gl->functions->texParameteri(sampler.target, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + gl->functions->texParameteri(sampler.target, GL_TEXTURE_MAG_FILTER, GL_LINEAR); if (pixels == nullptr) { - gl->texImage2D(sampler.target, 0, static_cast(format.internalFormatTexImage), width, - height, 0, format.externalFormat, GL_UNSIGNED_BYTE, nullptr); + gl->functions->texImage2D(sampler.target, 0, static_cast(format.internalFormatTexImage), + width, height, 0, format.externalFormat, GL_UNSIGNED_BYTE, nullptr); } if (!CheckGLError(gl)) { - gl->deleteTextures(1, &sampler.id); + gl->functions->deleteTextures(1, &sampler.id); return nullptr; } if (alphaOnly) { diff --git a/tgfx/src/gpu/opengl/GLUniformHandler.cpp b/tgfx/src/gpu/opengl/GLUniformHandler.cpp index b76522fc19..1cdbed9b44 100644 --- a/tgfx/src/gpu/opengl/GLUniformHandler.cpp +++ b/tgfx/src/gpu/opengl/GLUniformHandler.cpp @@ -45,7 +45,7 @@ UniformHandle GLUniformHandler::internalAddUniform(ShaderFlags visibility, Shade SamplerHandle GLUniformHandler::addSampler(const TextureSampler* sampler, const std::string& name) { auto mangleName = programBuilder->nameVariable('u', name); - const auto* gl = static_cast(programBuilder)->gl(); + auto gl = static_cast(programBuilder)->gl(); const auto& swizzle = gl->caps->getTextureSwizzle(sampler->format); ShaderVar::Type type; @@ -90,12 +90,14 @@ std::string GLUniformHandler::getUniformDeclarations(ShaderFlags visibility) con } void GLUniformHandler::resolveUniformLocations(unsigned programID) { - const auto* gl = static_cast(programBuilder)->gl(); + auto gl = static_cast(programBuilder)->gl(); for (auto& uniform : uniforms) { - uniform.location = gl->getUniformLocation(programID, uniform.variable.name().c_str()); + uniform.location = + gl->functions->getUniformLocation(programID, uniform.variable.name().c_str()); } for (auto& sampler : samplers) { - sampler.location = gl->getUniformLocation(programID, sampler.variable.name().c_str()); + sampler.location = + gl->functions->getUniformLocation(programID, sampler.variable.name().c_str()); } } } // namespace tgfx diff --git a/tgfx/src/gpu/opengl/GLUtil.cpp b/tgfx/src/gpu/opengl/GLUtil.cpp index a52d1042bc..479bf3af1a 100644 --- a/tgfx/src/gpu/opengl/GLUtil.cpp +++ b/tgfx/src/gpu/opengl/GLUtil.cpp @@ -17,6 +17,7 @@ ///////////////////////////////////////////////////////////////////////////////////////////////// #include "GLUtil.h" +#include "core/utils/USE.h" namespace tgfx { GLVersion GetGLVersion(const char* versionString) { @@ -53,17 +54,18 @@ GLVersion GetGLVersion(const char* versionString) { bool CreateGLTexture(const GLInterface* gl, int width, int height, GLSampler* texture) { texture->target = GL_TEXTURE_2D; texture->format = PixelFormat::RGBA_8888; - gl->genTextures(1, &texture->id); + gl->functions->genTextures(1, &texture->id); if (texture->id <= 0) { return false; } - gl->bindTexture(texture->target, texture->id); - gl->texParameteri(texture->target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - gl->texParameteri(texture->target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - gl->texParameteri(texture->target, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - gl->texParameteri(texture->target, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - gl->texImage2D(texture->target, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr); - gl->bindTexture(texture->target, 0); + gl->functions->bindTexture(texture->target, texture->id); + gl->functions->texParameteri(texture->target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + gl->functions->texParameteri(texture->target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + gl->functions->texParameteri(texture->target, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + gl->functions->texParameteri(texture->target, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + gl->functions->texImage2D(texture->target, 0, GL_RGBA, width, height, 0, GL_RGBA, + GL_UNSIGNED_BYTE, nullptr); + gl->functions->bindTexture(texture->target, 0); return true; } @@ -95,24 +97,24 @@ static std::array GetGLSwizzleValues(const Swizzle& swizzle) { void ActiveGLTexture(const GLInterface* gl, unsigned textureUnit, unsigned target, unsigned textureID, PixelFormat pixelFormat) { - gl->activeTexture(textureUnit); - gl->bindTexture(target, textureID); - gl->texParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - gl->texParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - gl->texParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - gl->texParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + gl->functions->activeTexture(textureUnit); + gl->functions->bindTexture(target, textureID); + gl->functions->texParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + gl->functions->texParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + gl->functions->texParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + gl->functions->texParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR); if (gl->caps->textureSwizzleSupport) { const auto& swizzle = gl->caps->getSwizzle(pixelFormat); auto glValues = GetGLSwizzleValues(swizzle); if (gl->caps->standard == GLStandard::GL) { - gl->texParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, - reinterpret_cast(&glValues[0])); + gl->functions->texParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, + reinterpret_cast(&glValues[0])); } else if (gl->caps->standard == GLStandard::GLES) { // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA. - gl->texParameteri(target, GL_TEXTURE_SWIZZLE_R, static_cast(glValues[0])); - gl->texParameteri(target, GL_TEXTURE_SWIZZLE_G, static_cast(glValues[1])); - gl->texParameteri(target, GL_TEXTURE_SWIZZLE_B, static_cast(glValues[2])); - gl->texParameteri(target, GL_TEXTURE_SWIZZLE_A, static_cast(glValues[3])); + gl->functions->texParameteri(target, GL_TEXTURE_SWIZZLE_R, static_cast(glValues[0])); + gl->functions->texParameteri(target, GL_TEXTURE_SWIZZLE_G, static_cast(glValues[1])); + gl->functions->texParameteri(target, GL_TEXTURE_SWIZZLE_B, static_cast(glValues[2])); + gl->functions->texParameteri(target, GL_TEXTURE_SWIZZLE_A, static_cast(glValues[3])); } } } @@ -123,28 +125,28 @@ void SubmitGLTexture(const GLInterface* gl, const GLSampler& sampler, int width, return; } const auto& format = gl->caps->getTextureFormat(sampler.format); - gl->bindTexture(sampler.target, sampler.id); - gl->pixelStorei(GL_UNPACK_ALIGNMENT, bytesPerPixel); + gl->functions->bindTexture(sampler.target, sampler.id); + gl->functions->pixelStorei(GL_UNPACK_ALIGNMENT, bytesPerPixel); if (gl->caps->unpackRowLengthSupport) { // the number of pixels, not bytes - gl->pixelStorei(GL_UNPACK_ROW_LENGTH, static_cast(rowBytes / bytesPerPixel)); - gl->texImage2D(sampler.target, 0, static_cast(format.internalFormatTexImage), width, - height, 0, format.externalFormat, GL_UNSIGNED_BYTE, pixels); + gl->functions->pixelStorei(GL_UNPACK_ROW_LENGTH, static_cast(rowBytes / bytesPerPixel)); + gl->functions->texImage2D(sampler.target, 0, static_cast(format.internalFormatTexImage), + width, height, 0, format.externalFormat, GL_UNSIGNED_BYTE, pixels); } else { if (static_cast(width) * bytesPerPixel == rowBytes) { - gl->texImage2D(sampler.target, 0, static_cast(format.internalFormatTexImage), width, - height, 0, format.externalFormat, GL_UNSIGNED_BYTE, pixels); + gl->functions->texImage2D(sampler.target, 0, static_cast(format.internalFormatTexImage), + width, height, 0, format.externalFormat, GL_UNSIGNED_BYTE, pixels); } else { - gl->texImage2D(sampler.target, 0, static_cast(format.internalFormatTexImage), width, - height, 0, format.externalFormat, GL_UNSIGNED_BYTE, nullptr); + gl->functions->texImage2D(sampler.target, 0, static_cast(format.internalFormatTexImage), + width, height, 0, format.externalFormat, GL_UNSIGNED_BYTE, nullptr); auto data = reinterpret_cast(pixels); for (int row = 0; row < height; ++row) { - gl->texSubImage2D(sampler.target, 0, 0, row, width, 1, format.externalFormat, - GL_UNSIGNED_BYTE, data + (row * rowBytes)); + gl->functions->texSubImage2D(sampler.target, 0, 0, row, width, 1, format.externalFormat, + GL_UNSIGNED_BYTE, data + (row * rowBytes)); } } } - gl->bindTexture(sampler.target, 0); + gl->functions->bindTexture(sampler.target, 0); } unsigned CreateGLProgram(const GLInterface* gl, const std::string& vertex, @@ -157,47 +159,52 @@ unsigned CreateGLProgram(const GLInterface* gl, const std::string& vertex, if (fragmentShader == 0) { return 0; } - auto programHandle = gl->createProgram(); - gl->attachShader(programHandle, vertexShader); - gl->attachShader(programHandle, fragmentShader); - gl->linkProgram(programHandle); + auto programHandle = gl->functions->createProgram(); + gl->functions->attachShader(programHandle, vertexShader); + gl->functions->attachShader(programHandle, fragmentShader); + gl->functions->linkProgram(programHandle); int success; - gl->getProgramiv(programHandle, GL_LINK_STATUS, &success); + gl->functions->getProgramiv(programHandle, GL_LINK_STATUS, &success); if (!success) { char infoLog[512]; - gl->getProgramInfoLog(programHandle, 512, nullptr, infoLog); - gl->deleteProgram(programHandle); + gl->functions->getProgramInfoLog(programHandle, 512, nullptr, infoLog); + gl->functions->deleteProgram(programHandle); } - gl->deleteShader(vertexShader); - gl->deleteShader(fragmentShader); + gl->functions->deleteShader(vertexShader); + gl->functions->deleteShader(fragmentShader); return programHandle; } unsigned LoadGLShader(const GLInterface* gl, unsigned shaderType, const std::string& source) { - auto shader = gl->createShader(shaderType); + auto shader = gl->functions->createShader(shaderType); const char* files[] = {source.c_str()}; - gl->shaderSource(shader, 1, files, nullptr); - gl->compileShader(shader); + gl->functions->shaderSource(shader, 1, files, nullptr); + gl->functions->compileShader(shader); int success; - gl->getShaderiv(shader, GL_COMPILE_STATUS, &success); + gl->functions->getShaderiv(shader, GL_COMPILE_STATUS, &success); if (!success) { char infoLog[512]; - gl->getShaderInfoLog(shader, 512, nullptr, infoLog); + gl->functions->getShaderInfoLog(shader, 512, nullptr, infoLog); LOGE("Could not compile shader: %d %s", shaderType, infoLog); - gl->deleteShader(shader); + gl->functions->deleteShader(shader); shader = 0; } return shader; } bool CheckGLError(const GLInterface* gl) { +#ifdef TGFX_BUILD_FOR_WEB + USE(gl); + return true; +#else bool success = true; unsigned errorCode; - while ((errorCode = gl->getError()) != GL_NO_ERROR) { + while ((errorCode = gl->functions->getError()) != GL_NO_ERROR) { success = false; LOGE("glCheckError: %d", errorCode); } return success; +#endif } std::array ToGLMatrix(const Matrix& matrix) { diff --git a/tgfx/src/gpu/opengl/GLYUVTexture.cpp b/tgfx/src/gpu/opengl/GLYUVTexture.cpp index 0a649b9e60..954ab0c095 100644 --- a/tgfx/src/gpu/opengl/GLYUVTexture.cpp +++ b/tgfx/src/gpu/opengl/GLYUVTexture.cpp @@ -92,7 +92,7 @@ class GLNV12Texture : public GLYUVTexture { static std::vector MakeTexturePlanes(const GLInterface* gl, const YUVConfig& yuvConfig) { std::vector texturePlanes{}; unsigned yuvTextureIDs[] = {0, 0, 0}; - gl->genTextures(yuvConfig.planeCount, yuvTextureIDs); + gl->functions->genTextures(yuvConfig.planeCount, yuvTextureIDs); if (yuvTextureIDs[0] == 0) { return texturePlanes; } @@ -123,7 +123,7 @@ static void SubmitYUVTexture(const GLInterface* gl, const YUVConfig& yuvConfig, std::shared_ptr YUVTexture::MakeI420(Context* context, YUVColorSpace colorSpace, YUVColorRange colorRange, int width, int height, uint8_t* pixelsPlane[3], const int lineSize[3]) { - auto gl = GLContext::Unwrap(context); + auto gl = GLInterface::Get(context); GLStateGuard stateGuard(context); YUVConfig yuvConfig = YUVConfig(colorSpace, colorRange, width, height, I420_PLANE_COUNT); @@ -155,7 +155,7 @@ std::shared_ptr YUVTexture::MakeI420(Context* context, YUVColorSpace std::shared_ptr YUVTexture::MakeNV12(Context* context, YUVColorSpace colorSpace, YUVColorRange colorRange, int width, int height, uint8_t* pixelsPlane[2], const int lineSize[2]) { - auto gl = GLContext::Unwrap(context); + auto gl = GLInterface::Get(context); GLStateGuard stateGuard(context); YUVConfig yuvConfig = YUVConfig(colorSpace, colorRange, width, height, NV12_PLANE_COUNT); @@ -203,9 +203,9 @@ const TextureSampler* GLYUVTexture::getSamplerAt(size_t index) const { } void GLYUVTexture::onRelease(Context* context) { - auto gl = GLContext::Unwrap(context); + auto gl = GLInterface::Get(context); for (const auto& sampler : samplers) { - gl->deleteTextures(1, &sampler.id); + gl->functions->deleteTextures(1, &sampler.id); } } } // namespace tgfx diff --git a/tgfx/src/gpu/opengl/eagl/EAGLHardwareTexture.mm b/tgfx/src/gpu/opengl/eagl/EAGLHardwareTexture.mm index feb324b490..50c36d4c48 100644 --- a/tgfx/src/gpu/opengl/eagl/EAGLHardwareTexture.mm +++ b/tgfx/src/gpu/opengl/eagl/EAGLHardwareTexture.mm @@ -32,7 +32,7 @@ static CVOpenGLESTextureRef GetTextureRef(Context* context, CVPixelBufferRef pix CVOpenGLESTextureRef texture = nil; CVReturn result; if (CVPixelBufferGetPixelFormatType(pixelBuffer) == kCVPixelFormatType_OneComponent8) { - auto gl = GLContext::Unwrap(context); + auto gl = GLInterface::Get(context); const auto& format = gl->caps->getTextureFormat(PixelFormat::ALPHA_8); // 返回的 texture 对象是一个强引用计数为 1 的对象。 result = CVOpenGLESTextureCacheCreateTextureFromImage( diff --git a/tgfx/src/gpu/opengl/eagl/EAGLNV12Texture.mm b/tgfx/src/gpu/opengl/eagl/EAGLNV12Texture.mm index 76356e5681..f1059a9763 100644 --- a/tgfx/src/gpu/opengl/eagl/EAGLNV12Texture.mm +++ b/tgfx/src/gpu/opengl/eagl/EAGLNV12Texture.mm @@ -45,7 +45,7 @@ static GLSampler ToGLSampler(CVOpenGLESTextureRef texture, PixelFormat format) { auto height = static_cast(CVPixelBufferGetHeight(pixelBuffer)); CVOpenGLESTextureRef outputTextureLuma = nil; CVOpenGLESTextureRef outputTextureChroma = nil; - auto gl = GLContext::Unwrap(context); + auto gl = GLInterface::Get(context); auto lumaComponentFormat = PixelFormat::GRAY_8; const auto& oneComponentFormat = gl->caps->getTextureFormat(lumaComponentFormat); // 返回的 texture 对象是一个强引用计数为 1 的对象。 diff --git a/tgfx/src/gpu/opengl/eagl/EAGLWindow.mm b/tgfx/src/gpu/opengl/eagl/EAGLWindow.mm index 7e2fa72e30..5d16078d78 100644 --- a/tgfx/src/gpu/opengl/eagl/EAGLWindow.mm +++ b/tgfx/src/gpu/opengl/eagl/EAGLWindow.mm @@ -86,13 +86,13 @@ EAGLWindow::~EAGLWindow() { auto context = device->lockContext(); if (context) { - auto gl = GLContext::Unwrap(context); + auto gl = GLInterface::Get(context); if (frameBufferID > 0) { - gl->deleteFramebuffers(1, &frameBufferID); + gl->functions->deleteFramebuffers(1, &frameBufferID); frameBufferID = 0; } if (colorBuffer) { - gl->deleteRenderbuffers(1, &colorBuffer); + gl->functions->deleteRenderbuffers(1, &colorBuffer); colorBuffer = 0; } device->unlock(); @@ -108,13 +108,13 @@ auto texture = EAGLHardwareTexture::MakeFrom(context, pixelBuffer); return GLSurface::MakeFrom(context, texture); } - auto gl = GLContext::Unwrap(context); + auto gl = GLInterface::Get(context); if (frameBufferID > 0) { - gl->deleteFramebuffers(1, &frameBufferID); + gl->functions->deleteFramebuffers(1, &frameBufferID); frameBufferID = 0; } if (colorBuffer) { - gl->deleteRenderbuffers(1, &colorBuffer); + gl->functions->deleteRenderbuffers(1, &colorBuffer); colorBuffer = 0; } auto width = layer.bounds.size.width * layer.contentsScale; @@ -122,17 +122,17 @@ if (width <= 0 || height <= 0) { return nullptr; } - gl->genFramebuffers(1, &frameBufferID); - gl->bindFramebuffer(GL_FRAMEBUFFER, frameBufferID); - gl->genRenderbuffers(1, &colorBuffer); - gl->bindRenderbuffer(GL_RENDERBUFFER, colorBuffer); - gl->framebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, + gl->functions->genFramebuffers(1, &frameBufferID); + gl->functions->bindFramebuffer(GL_FRAMEBUFFER, frameBufferID); + gl->functions->genRenderbuffers(1, &colorBuffer); + gl->functions->bindRenderbuffer(GL_RENDERBUFFER, colorBuffer); + gl->functions->framebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorBuffer); auto eaglContext = static_cast(context->device())->eaglContext(); [eaglContext renderbufferStorage:GL_RENDERBUFFER fromDrawable:layer]; - auto frameBufferStatus = gl->checkFramebufferStatus(GL_FRAMEBUFFER); - gl->bindFramebuffer(GL_FRAMEBUFFER, 0); - gl->bindRenderbuffer(GL_RENDERBUFFER, 0); + auto frameBufferStatus = gl->functions->checkFramebufferStatus(GL_FRAMEBUFFER); + gl->functions->bindFramebuffer(GL_FRAMEBUFFER, 0); + gl->functions->bindRenderbuffer(GL_RENDERBUFFER, 0); if (frameBufferStatus != GL_FRAMEBUFFER_COMPLETE) { LOGE("EAGLWindow::onCreateSurface() Framebuffer is not complete!"); return nullptr; @@ -145,14 +145,14 @@ } void EAGLWindow::onPresent(Context* context, int64_t) { - auto gl = GLContext::Unwrap(context); + auto gl = GLInterface::Get(context); if (layer) { - gl->bindRenderbuffer(GL_RENDERBUFFER, colorBuffer); + gl->functions->bindRenderbuffer(GL_RENDERBUFFER, colorBuffer); auto eaglContext = static_cast(context->device())->eaglContext(); [eaglContext presentRenderbuffer:GL_RENDERBUFFER]; - gl->bindRenderbuffer(GL_RENDERBUFFER, 0); + gl->functions->bindRenderbuffer(GL_RENDERBUFFER, 0); } else { - gl->flush(); + gl->functions->flush(); } } } // namespace tgfx diff --git a/tgfx/src/gpu/opengl/qt/QGLWindow.cpp b/tgfx/src/gpu/opengl/qt/QGLWindow.cpp index 37eee40825..e0617582bd 100644 --- a/tgfx/src/gpu/opengl/qt/QGLWindow.cpp +++ b/tgfx/src/gpu/opengl/qt/QGLWindow.cpp @@ -108,13 +108,13 @@ void QGLWindow::onPresent(Context* context, int64_t) { if (renderTarget == nullptr) { return; } - auto gl = GLContext::Unwrap(context); + auto gl = GLInterface::Get(context); std::swap(frontTexture, backTexture); - gl->flush(); - gl->bindFramebuffer(GL_FRAMEBUFFER, renderTarget->glFrameBuffer().id); - gl->framebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, - backTexture->glSampler().id, 0); - gl->bindFramebuffer(GL_FRAMEBUFFER, 0); + gl->functions->flush(); + gl->functions->bindFramebuffer(GL_FRAMEBUFFER, renderTarget->glFrameBuffer().id); + gl->functions->framebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, + backTexture->glSampler().id, 0); + gl->functions->bindFramebuffer(GL_FRAMEBUFFER, 0); invalidateTexture(); } } // namespace tgfx \ No newline at end of file diff --git a/tgfx/src/platform/web/NativeTextureBuffer.cpp b/tgfx/src/platform/web/NativeTextureBuffer.cpp index acac8d965d..821838b6b8 100644 --- a/tgfx/src/platform/web/NativeTextureBuffer.cpp +++ b/tgfx/src/platform/web/NativeTextureBuffer.cpp @@ -37,8 +37,8 @@ std::shared_ptr NativeTextureBuffer::makeTexture(Context* context) cons return nullptr; } auto& glInfo = std::static_pointer_cast(texture)->glSampler(); - const auto* gl = GLContext::Unwrap(context); - gl->bindTexture(glInfo.target, glInfo.id); + auto gl = GLInterface::Get(context); + gl->functions->bindTexture(glInfo.target, glInfo.id); source.call("upload", val::module_property("GL")); return texture; } From e39422a3b8a7a36f917ff9507efcd85990cf1112 Mon Sep 17 00:00:00 2001 From: domrjchen Date: Mon, 28 Feb 2022 09:02:43 +0800 Subject: [PATCH 2/6] Add the GLRestorer class. --- include/pag/pag.h | 7 ++- src/platform/Platform.cpp | 2 - src/rendering/PAGSurface.cpp | 57 +++++++++++++--------- src/rendering/caches/TextAtlas.cpp | 2 +- src/rendering/utils/GLRestorer.cpp | 68 +++++++++++++++++++++++++++ src/rendering/utils/GLRestorer.h | 37 +++++++++++++++ tgfx/include/gpu/opengl/GLFunctions.h | 1 + tgfx/src/gpu/opengl/GLDevice.cpp | 5 -- tgfx/src/gpu/opengl/GLState.h | 2 - 9 files changed, 147 insertions(+), 34 deletions(-) create mode 100644 src/rendering/utils/GLRestorer.cpp create mode 100644 src/rendering/utils/GLRestorer.h diff --git a/include/pag/pag.h b/include/pag/pag.h index bd8413a98a..6a95b22416 100644 --- a/include/pag/pag.h +++ b/include/pag/pag.h @@ -1141,6 +1141,8 @@ class Drawable { class Graphic; +class GLRestorer; + class PAG_API PAGSurface { public: /** @@ -1212,8 +1214,11 @@ class PAG_API PAGSurface { std::shared_ptr drawable = nullptr; std::shared_ptr device = nullptr; std::shared_ptr surface = nullptr; + bool contextAdopted = false; + GLRestorer* glRestorer = nullptr; + - explicit PAGSurface(std::shared_ptr drawable); + explicit PAGSurface(std::shared_ptr drawable, bool contextAdopted = false); bool draw(RenderCache* cache, std::shared_ptr graphic, BackendSemaphore* signalSemaphore, bool autoClear = true); diff --git a/src/platform/Platform.cpp b/src/platform/Platform.cpp index 97775eb2de..45a1713328 100644 --- a/src/platform/Platform.cpp +++ b/src/platform/Platform.cpp @@ -17,8 +17,6 @@ ///////////////////////////////////////////////////////////////////////////////////////////////// #include "Platform.h" -#include "core/Image.h" -#include "gpu/opengl/GLProcGetter.h" #include "video/VideoDecoder.h" namespace pag { diff --git a/src/rendering/PAGSurface.cpp b/src/rendering/PAGSurface.cpp index 91257c9a51..02655e1711 100644 --- a/src/rendering/PAGSurface.cpp +++ b/src/rendering/PAGSurface.cpp @@ -18,13 +18,13 @@ #include "base/utils/GetTimer.h" #include "base/utils/TGFXCast.h" -#include "gpu/Canvas.h" #include "gpu/opengl/GLDevice.h" #include "pag/file.h" #include "pag/pag.h" #include "rendering/Drawable.h" #include "rendering/caches/RenderCache.h" #include "rendering/graphics/Recorder.h" +#include "rendering/utils/GLRestorer.h" #include "rendering/utils/LockGuard.h" namespace pag { @@ -36,17 +36,6 @@ std::shared_ptr PAGSurface::MakeFrom(std::shared_ptr drawa return std::shared_ptr(new PAGSurface(std::move(drawable))); } -static std::shared_ptr GetCurrentDevice(bool forAsyncThread) { - if (forAsyncThread) { - auto sharedContext = tgfx::GLDevice::CurrentNativeHandle(); - auto device = tgfx::GLDevice::Make(sharedContext); - if (device) { - return device; - } - } - return tgfx::GLDevice::Current(); -} - std::shared_ptr PAGSurface::MakeFrom(const BackendRenderTarget& renderTarget, ImageOrigin origin) { auto device = tgfx::GLDevice::Current(); @@ -54,17 +43,32 @@ std::shared_ptr PAGSurface::MakeFrom(const BackendRenderTarget& rend return nullptr; } auto drawable = std::make_shared(device, renderTarget, ToTGFX(origin)); - return MakeFrom(std::move(drawable)); + if (drawable == nullptr) { + return nullptr; + } + return std::shared_ptr(new PAGSurface(std::move(drawable), true)); } std::shared_ptr PAGSurface::MakeFrom(const BackendTexture& texture, ImageOrigin origin, bool forAsyncThread) { - auto device = GetCurrentDevice(forAsyncThread); + std::shared_ptr device = nullptr; + bool isAdopted = false; + if (forAsyncThread) { + auto sharedContext = tgfx::GLDevice::CurrentNativeHandle(); + device = tgfx::GLDevice::Make(sharedContext); + } + if (device == nullptr) { + device = tgfx::GLDevice::Current(); + isAdopted = true; + } if (device == nullptr || !texture.isValid()) { return nullptr; } auto drawable = std::make_shared(device, texture, ToTGFX(origin)); - return MakeFrom(std::move(drawable)); + if (drawable == nullptr) { + return nullptr; + } + return std::shared_ptr(new PAGSurface(std::move(drawable), isAdopted)); } std::shared_ptr PAGSurface::MakeOffscreen(int width, int height) { @@ -76,7 +80,8 @@ std::shared_ptr PAGSurface::MakeOffscreen(int width, int height) { return std::shared_ptr(new PAGSurface(drawable)); } -PAGSurface::PAGSurface(std::shared_ptr drawable) : drawable(std::move(drawable)) { +PAGSurface::PAGSurface(std::shared_ptr drawable, bool contextAdopted) + : drawable(std::move(drawable)), contextAdopted(contextAdopted) { rootLocker = std::make_shared(); } @@ -103,12 +108,10 @@ void PAGSurface::freeCache() { pagPlayer->renderCache->releaseAll(); } surface = nullptr; - if (device) { - auto context = device->lockContext(); - if (context) { - context->purgeResourcesNotUsedIn(0); - device->unlock(); - } + auto context = lockContext(); + if (context) { + context->purgeResourcesNotUsedIn(0); + unlockContext(); } device = nullptr; } @@ -239,13 +242,21 @@ tgfx::Context* PAGSurface::lockContext() { if (device == nullptr) { return nullptr; } - return device->lockContext(); + auto context = device->lockContext(); + if (context != nullptr && contextAdopted) { + glRestorer = new GLRestorer(tgfx::GLFunctions::Get(context)); + } + return context; } void PAGSurface::unlockContext() { if (device == nullptr) { return; } + if (contextAdopted) { + delete glRestorer; + glRestorer = nullptr; + } device->unlock(); } } // namespace pag diff --git a/src/rendering/caches/TextAtlas.cpp b/src/rendering/caches/TextAtlas.cpp index 4703225277..7f08f16175 100644 --- a/src/rendering/caches/TextAtlas.cpp +++ b/src/rendering/caches/TextAtlas.cpp @@ -21,7 +21,7 @@ #include "core/Mask.h" #include "gpu/Canvas.h" #include "gpu/Surface.h" -#include "gpu/opengl/GLTexture.h" +#include "gpu/Texture.h" namespace pag { class Atlas { diff --git a/src/rendering/utils/GLRestorer.cpp b/src/rendering/utils/GLRestorer.cpp new file mode 100644 index 0000000000..457a6410de --- /dev/null +++ b/src/rendering/utils/GLRestorer.cpp @@ -0,0 +1,68 @@ +#include "GLRestorer.h" + +namespace pag { +GLRestorer::GLRestorer(const tgfx::GLFunctions* gl) : gl(gl) { + if (gl == nullptr) { + return; + } + gl->getIntegerv(GL_VIEWPORT, viewport); + scissorEnabled = gl->isEnabled(GL_SCISSOR_TEST); + if (scissorEnabled) { + gl->getIntegerv(GL_SCISSOR_BOX, scissorBox); + } + gl->getIntegerv(GL_CURRENT_PROGRAM, &program); + gl->getIntegerv(GL_FRAMEBUFFER_BINDING, &frameBuffer); + gl->getIntegerv(GL_ACTIVE_TEXTURE, &activeTexture); + gl->getIntegerv(GL_TEXTURE_BINDING_2D, &textureID); + gl->getIntegerv(GL_ARRAY_BUFFER_BINDING, &arrayBuffer); + gl->getIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &elementArrayBuffer); + if (gl->bindVertexArray) { + gl->getIntegerv(GL_VERTEX_ARRAY_BINDING, &vertexArray); + } + gl->bindBuffer(GL_ARRAY_BUFFER, 0); + gl->bindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + blendEnabled = gl->isEnabled(GL_BLEND); + if (blendEnabled) { + gl->getIntegerv(GL_BLEND_EQUATION, &blendEquation); + gl->getIntegerv(GL_BLEND_EQUATION_RGB, &equationRGB); + gl->getIntegerv(GL_BLEND_EQUATION_ALPHA, &equationAlpha); + gl->getIntegerv(GL_BLEND_SRC_RGB, &blendSrcRGB); + gl->getIntegerv(GL_BLEND_DST_RGB, &blendDstRGB); + gl->getIntegerv(GL_BLEND_SRC_ALPHA, &blendSrcAlpha); + gl->getIntegerv(GL_BLEND_DST_ALPHA, &blendDstAlpha); + } + if (vertexArray > 0) { + gl->bindVertexArray(0); + } +} + +GLRestorer::~GLRestorer() { + if (gl == nullptr) { + return; + } + gl->viewport(viewport[0], viewport[1], viewport[2], viewport[3]); + if (scissorEnabled) { + gl->enable(GL_SCISSOR_TEST); + gl->scissor(scissorBox[0], scissorBox[1], scissorBox[2], scissorBox[3]); + } else { + gl->disable(GL_SCISSOR_TEST); + } + gl->useProgram(static_cast(program)); + gl->bindFramebuffer(GL_FRAMEBUFFER, static_cast(frameBuffer)); + gl->activeTexture(static_cast(activeTexture)); + gl->bindTexture(GL_TEXTURE_2D, static_cast(textureID)); + if (vertexArray > 0) { + gl->bindVertexArray(vertexArray); + } + gl->bindBuffer(GL_ARRAY_BUFFER, static_cast(arrayBuffer)); + gl->bindBuffer(GL_ELEMENT_ARRAY_BUFFER, static_cast(elementArrayBuffer)); + if (blendEnabled) { + gl->enable(GL_BLEND); + gl->blendEquation(static_cast(blendEquation)); + gl->blendEquationSeparate(equationRGB, equationAlpha); + gl->blendFuncSeparate(blendSrcRGB, blendDstRGB, blendSrcAlpha, blendDstAlpha); + } else { + gl->disable(GL_BLEND); + } +} +} // namespace pag diff --git a/src/rendering/utils/GLRestorer.h b/src/rendering/utils/GLRestorer.h new file mode 100644 index 0000000000..6de6dfb7f6 --- /dev/null +++ b/src/rendering/utils/GLRestorer.h @@ -0,0 +1,37 @@ +#ifndef PAG_GLRESTORER_H +#define PAG_GLRESTORER_H + +#include "gpu/opengl/GLFunctions.h" + +namespace pag { +class GLRestorer { + public: + GLRestorer(const tgfx::GLFunctions* gl); + + ~GLRestorer(); + + private: + const tgfx::GLFunctions* gl = nullptr; + int viewport[4] = {}; + unsigned scissorEnabled = GL_FALSE; + int scissorBox[4] = {}; + int frameBuffer = 0; + int program = 0; + int activeTexture = 0; + int textureID = 0; + int arrayBuffer = 0; + int elementArrayBuffer = 0; + int vertexArray = 0; + + unsigned blendEnabled = GL_FALSE; + int blendEquation = 0; + int equationRGB = 0; + int equationAlpha = 0; + int blendSrcRGB = 0; + int blendDstRGB = 0; + int blendSrcAlpha = 0; + int blendDstAlpha = 0; +}; +} + +#endif //PAG_GLRESTORER_H diff --git a/tgfx/include/gpu/opengl/GLFunctions.h b/tgfx/include/gpu/opengl/GLFunctions.h index 6424c31ece..c3e80b4f11 100644 --- a/tgfx/include/gpu/opengl/GLFunctions.h +++ b/tgfx/include/gpu/opengl/GLFunctions.h @@ -22,6 +22,7 @@ #include #include #include +#include "gpu/opengl/GLDefines.h" namespace tgfx { #if !defined(GL_FUNCTION_TYPE) diff --git a/tgfx/src/gpu/opengl/GLDevice.cpp b/tgfx/src/gpu/opengl/GLDevice.cpp index 8663122848..b10ddd6e7a 100644 --- a/tgfx/src/gpu/opengl/GLDevice.cpp +++ b/tgfx/src/gpu/opengl/GLDevice.cpp @@ -69,7 +69,6 @@ bool GLDevice::onLockContext() { auto glContext = static_cast(context); if (isAdopted) { glContext->glState->reset(); - glContext->glState->save(); // Clear externally generated GLError. CheckGLError(glContext->interface.get()); } @@ -77,10 +76,6 @@ bool GLDevice::onLockContext() { } void GLDevice::onUnlockContext() { - auto glContext = static_cast(context); - if (isAdopted) { - glContext->glState->restore(); - } onClearCurrent(); } } // namespace tgfx \ No newline at end of file diff --git a/tgfx/src/gpu/opengl/GLState.h b/tgfx/src/gpu/opengl/GLState.h index 1185d20b17..370156035d 100644 --- a/tgfx/src/gpu/opengl/GLState.h +++ b/tgfx/src/gpu/opengl/GLState.h @@ -80,8 +80,6 @@ class StateRecord { std::unordered_map, EnumHasher> attributeMap = {}; }; -// TODO(domrjchen): All GL methods that may modify the state should be hooked uniformly onto -// GLState, and the warning output should be added. class GLState { public: explicit GLState(const GLInterface* gl); From 29f581c54f3b1426c50a11ef12832424e924366d Mon Sep 17 00:00:00 2001 From: domrjchen Date: Mon, 28 Feb 2022 14:21:02 +0800 Subject: [PATCH 3/6] Remove GLStateGuard. --- src/platform/web/VideoSequenceReader.cpp | 1 - src/rendering/filters/LayerFilter.cpp | 1 + src/rendering/renderers/FilterRenderer.cpp | 1 - tgfx/src/gpu/opengl/GLContext.h | 1 - tgfx/src/gpu/opengl/GLDrawer.cpp | 3 -- tgfx/src/gpu/opengl/GLRenderTarget.cpp | 45 +++++++++++----------- tgfx/src/gpu/opengl/GLState.cpp | 10 ----- tgfx/src/gpu/opengl/GLState.h | 10 ----- tgfx/src/gpu/opengl/GLTexture.cpp | 1 - tgfx/src/gpu/opengl/GLUtil.cpp | 2 +- tgfx/src/gpu/opengl/GLYUVTexture.cpp | 4 -- 11 files changed, 25 insertions(+), 54 deletions(-) diff --git a/src/platform/web/VideoSequenceReader.cpp b/src/platform/web/VideoSequenceReader.cpp index f30348c06e..008247e8e1 100644 --- a/src/platform/web/VideoSequenceReader.cpp +++ b/src/platform/web/VideoSequenceReader.cpp @@ -81,7 +81,6 @@ std::shared_ptr VideoSequenceReader::readTexture(Frame targetFram if (targetFrame == lastFrame) { return texture; } - tgfx::GLStateGuard stateGuard(cache->getContext()); if (texture == nullptr) { texture = tgfx::GLTexture::MakeRGBA(cache->getContext(), width, height); } diff --git a/src/rendering/filters/LayerFilter.cpp b/src/rendering/filters/LayerFilter.cpp index 124c0fe6f8..924289bb4a 100644 --- a/src/rendering/filters/LayerFilter.cpp +++ b/src/rendering/filters/LayerFilter.cpp @@ -226,6 +226,7 @@ void LayerFilter::draw(tgfx::Context* context, const FilterSource* source, auto gl = tgfx::GLInterface::Get(context); EnableMultisample(gl, needsMSAA()); gl->functions->useProgram(filterProgram->program); + gl->functions->disable(GL_SCISSOR_TEST); gl->functions->enable(GL_BLEND); gl->functions->blendEquation(GL_FUNC_ADD); gl->functions->blendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); diff --git a/src/rendering/renderers/FilterRenderer.cpp b/src/rendering/renderers/FilterRenderer.cpp index f7477dd20f..2d55a71e26 100644 --- a/src/rendering/renderers/FilterRenderer.cpp +++ b/src/rendering/renderers/FilterRenderer.cpp @@ -308,7 +308,6 @@ std::vector FilterRenderer::MakeFilterNodes(const FilterList* filter void ApplyFilters(tgfx::Context* context, std::vector filterNodes, const tgfx::Rect& contentBounds, FilterSource* filterSource, FilterTarget* filterTarget) { - tgfx::GLStateGuard stateGuard(context); auto gl = tgfx::GLInterface::Get(context); auto scale = filterSource->scale; std::shared_ptr freeBuffer = nullptr; diff --git a/tgfx/src/gpu/opengl/GLContext.h b/tgfx/src/gpu/opengl/GLContext.h index d2ac34f7cc..001e6f9b6b 100644 --- a/tgfx/src/gpu/opengl/GLContext.h +++ b/tgfx/src/gpu/opengl/GLContext.h @@ -45,7 +45,6 @@ class GLContext : public Context { std::unique_ptr interface = nullptr; std::unique_ptr glState = nullptr; - friend class GLStateGuard; friend class GLDevice; friend class GLInterface; }; diff --git a/tgfx/src/gpu/opengl/GLDrawer.cpp b/tgfx/src/gpu/opengl/GLDrawer.cpp index e4927862e6..2a203f6aa5 100644 --- a/tgfx/src/gpu/opengl/GLDrawer.cpp +++ b/tgfx/src/gpu/opengl/GLDrawer.cpp @@ -120,7 +120,6 @@ static std::shared_ptr CreateDstTexture(const DrawArgs& args, Point* ds LOGE("Failed to create dst texture(%f*%f).", dstRect.width(), dstRect.height()); return nullptr; } - GLStateGuard stateGuard(args.context); auto renderTarget = static_cast(args.renderTarget); gl->functions->bindFramebuffer(GL_FRAMEBUFFER, renderTarget->glFrameBuffer().id); auto glSampler = std::static_pointer_cast(dstTexture)->glSampler(); @@ -131,7 +130,6 @@ static std::shared_ptr CreateDstTexture(const DrawArgs& args, Point* ds gl->functions->copyTexSubImage2D(glSampler.target, 0, 0, 0, static_cast(dstRect.x()), static_cast(dstRect.y()), static_cast(dstRect.width()), static_cast(dstRect.height())); - gl->functions->bindTexture(glSampler.target, 0); return dstTexture; } @@ -193,7 +191,6 @@ void GLDrawer::draw(DrawArgs args, std::unique_ptr op) const { const auto& swizzle = gl->caps->getOutputSwizzle(config); Pipeline pipeline(std::move(fragmentProcessors), numColorProcessors, std::move(xferProcessor), dstTexture, dstTextureOffset, &swizzle); - GLStateGuard stateGuard(args.context); auto geometryProcessor = op->getGeometryProcessor(args); GLProgramCreator creator(geometryProcessor.get(), &pipeline); auto program = static_cast(args.context->programCache()->getProgram(&creator)); diff --git a/tgfx/src/gpu/opengl/GLRenderTarget.cpp b/tgfx/src/gpu/opengl/GLRenderTarget.cpp index eeb3c5be29..59d90e6a9e 100644 --- a/tgfx/src/gpu/opengl/GLRenderTarget.cpp +++ b/tgfx/src/gpu/opengl/GLRenderTarget.cpp @@ -97,12 +97,10 @@ static void ReleaseResource(Context* context, GLFrameBuffer* textureFBInfo, textureFBInfo->id = 0; } if (renderTargetFBInfo && renderTargetFBInfo->id > 0) { - { - GLStateGuard stateGuard(context); - gl->functions->bindFramebuffer(GL_FRAMEBUFFER, renderTargetFBInfo->id); - gl->functions->framebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, - 0); - } + gl->functions->bindFramebuffer(GL_FRAMEBUFFER, renderTargetFBInfo->id); + gl->functions->framebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, + 0); + gl->functions->bindFramebuffer(GL_FRAMEBUFFER, 0); gl->functions->deleteFramebuffers(1, &(renderTargetFBInfo->id)); renderTargetFBInfo->id = 0; } @@ -131,7 +129,11 @@ static bool CreateRenderBuffer(const GLInterface* gl, const GLTexture* texture, gl->functions->bindFramebuffer(GL_FRAMEBUFFER, renderTargetFBInfo->id); gl->functions->framebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, *msRenderBufferID); +#ifdef TGFX_BUILD_FOR_WEB + return true; +#else return gl->functions->checkFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE; +#endif } std::shared_ptr GLRenderTarget::MakeFrom(Context* context, const GLTexture* texture, @@ -146,7 +148,6 @@ std::shared_ptr GLRenderTarget::MakeFrom(Context* context, const if (textureFBInfo.id == 0) { return nullptr; } - GLStateGuard stateGuard(context); GLFrameBuffer renderTargetFBInfo = {}; renderTargetFBInfo.format = texture->glSampler().format; unsigned msRenderBufferID = 0; @@ -161,19 +162,18 @@ std::shared_ptr GLRenderTarget::MakeFrom(Context* context, const gl->functions->bindFramebuffer(GL_FRAMEBUFFER, textureFBInfo.id); auto textureInfo = texture->glSampler(); FrameBufferTexture2D(gl, textureInfo.target, textureInfo.id, sampleCount); - std::shared_ptr renderTarget = nullptr; - +#ifndef TGFX_BUILD_FOR_WEB if (gl->functions->checkFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { ReleaseResource(context, &textureFBInfo, &renderTargetFBInfo, &msRenderBufferID); - } else { - auto textureTarget = texture->glSampler().target; - auto rt = new GLRenderTarget(texture->width(), texture->height(), texture->origin(), - sampleCount, textureFBInfo, textureTarget); - rt->renderTargetFBInfo = renderTargetFBInfo; - rt->msRenderBufferID = msRenderBufferID; - renderTarget = Resource::Wrap(context, rt); + return nullptr; } - return renderTarget; +#endif + auto textureTarget = texture->glSampler().target; + auto rt = new GLRenderTarget(texture->width(), texture->height(), texture->origin(), sampleCount, + textureFBInfo, textureTarget); + rt->renderTargetFBInfo = renderTargetFBInfo; + rt->msRenderBufferID = msRenderBufferID; + return Resource::Wrap(context, rt); } GLRenderTarget::GLRenderTarget(int width, int height, ImageOrigin origin, int sampleCount, @@ -235,7 +235,6 @@ bool GLRenderTarget::readPixels(Context* context, const ImageInfo& dstInfo, void return false; } auto pixelFormat = renderTargetFBInfo.format; - GLStateGuard stateGuard(context); auto gl = GLInterface::Get(context); const auto& format = gl->caps->getTextureFormat(pixelFormat); gl->functions->bindFramebuffer(GL_FRAMEBUFFER, renderTargetFBInfo.id); @@ -244,11 +243,13 @@ bool GLRenderTarget::readPixels(Context* context, const ImageInfo& dstInfo, void ImageInfo::Make(outInfo.width(), outInfo.height(), colorType, AlphaType::Premultiplied); void* pixels = nullptr; uint8_t* tempPixels = nullptr; + auto restoreGLRowLength = false; if (CanReadDirectly(gl, origin(), srcInfo, outInfo)) { pixels = dstPixels; if (outInfo.rowBytes() != outInfo.minRowBytes()) { gl->functions->pixelStorei(GL_PACK_ROW_LENGTH, static_cast(outInfo.rowBytes() / outInfo.bytesPerPixel())); + restoreGLRowLength = true; } } else { tempPixels = new uint8_t[srcInfo.byteSize()]; @@ -264,6 +265,9 @@ bool GLRenderTarget::readPixels(Context* context, const ImageInfo& dstInfo, void } gl->functions->readPixels(readX, readY, outInfo.width(), outInfo.height(), format.externalFormat, GL_UNSIGNED_BYTE, pixels); + if (restoreGLRowLength) { + gl->functions->pixelStorei(GL_PACK_ROW_LENGTH, 0); + } if (tempPixels != nullptr) { CopyPixels(srcInfo, tempPixels, outInfo, dstPixels, flipY); delete[] tempPixels; @@ -279,7 +283,6 @@ void GLRenderTarget::resolve(Context* context) const { if (!gl->caps->usesMSAARenderBuffers()) { return; } - GLStateGuard stateGuard(context); gl->functions->bindFramebuffer(GL_READ_FRAMEBUFFER, renderTargetFBInfo.id); gl->functions->bindFramebuffer(GL_DRAW_FRAMEBUFFER, textureFBInfo.id); if (gl->caps->msFBOType == MSFBOType::ES_Apple) { @@ -301,12 +304,10 @@ void GLRenderTarget::onRelease(Context* context) { return; } if (textureTarget != 0) { - // The currently bound fboID may be the same as textureFBInfo.id, we must restore and then - // delete, otherwise GL_INVALID_OPERATION(1282) will be reported。 - GLStateGuard stateGuard(context); auto gl = GLInterface::Get(context); gl->functions->bindFramebuffer(GL_FRAMEBUFFER, textureFBInfo.id); FrameBufferTexture2D(gl, textureTarget, 0, sampleCount()); + gl->functions->bindFramebuffer(GL_FRAMEBUFFER, 0); } ReleaseResource(context, &textureFBInfo, &renderTargetFBInfo, &msRenderBufferID); } diff --git a/tgfx/src/gpu/opengl/GLState.cpp b/tgfx/src/gpu/opengl/GLState.cpp index eceeb0aac2..dbad20f3f4 100644 --- a/tgfx/src/gpu/opengl/GLState.cpp +++ b/tgfx/src/gpu/opengl/GLState.cpp @@ -1096,14 +1096,4 @@ std::shared_ptr GLState::insertAttribute( } return attribute; } - -GLStateGuard::GLStateGuard(Context* context) { - state = static_cast(context)->glState.get(); - state->save(); -} - -GLStateGuard::~GLStateGuard() { - state->restore(); -} - } // namespace tgfx diff --git a/tgfx/src/gpu/opengl/GLState.h b/tgfx/src/gpu/opengl/GLState.h index 370156035d..82f4e90499 100644 --- a/tgfx/src/gpu/opengl/GLState.h +++ b/tgfx/src/gpu/opengl/GLState.h @@ -141,14 +141,4 @@ class GLState { }; class Context; - -class GLStateGuard { - public: - explicit GLStateGuard(Context* context); - - ~GLStateGuard(); - - private: - GLState* state = nullptr; -}; } // namespace tgfx diff --git a/tgfx/src/gpu/opengl/GLTexture.cpp b/tgfx/src/gpu/opengl/GLTexture.cpp index b340e29e23..a569c6a55f 100644 --- a/tgfx/src/gpu/opengl/GLTexture.cpp +++ b/tgfx/src/gpu/opengl/GLTexture.cpp @@ -127,7 +127,6 @@ std::shared_ptr Texture::Make(Context* context, int width, int height, } PixelFormat pixelFormat = alphaOnly ? PixelFormat::ALPHA_8 : PixelFormat::RGBA_8888; const auto& format = gl->caps->getTextureFormat(pixelFormat); - GLStateGuard stateGuard(context); BytesKey recycleKey = {}; if (alphaOnly) { GLAlphaTexture::ComputeRecycleKey(&recycleKey, width, height); diff --git a/tgfx/src/gpu/opengl/GLUtil.cpp b/tgfx/src/gpu/opengl/GLUtil.cpp index 479bf3af1a..0824c41b13 100644 --- a/tgfx/src/gpu/opengl/GLUtil.cpp +++ b/tgfx/src/gpu/opengl/GLUtil.cpp @@ -132,6 +132,7 @@ void SubmitGLTexture(const GLInterface* gl, const GLSampler& sampler, int width, gl->functions->pixelStorei(GL_UNPACK_ROW_LENGTH, static_cast(rowBytes / bytesPerPixel)); gl->functions->texImage2D(sampler.target, 0, static_cast(format.internalFormatTexImage), width, height, 0, format.externalFormat, GL_UNSIGNED_BYTE, pixels); + gl->functions->pixelStorei(GL_UNPACK_ROW_LENGTH, 0); } else { if (static_cast(width) * bytesPerPixel == rowBytes) { gl->functions->texImage2D(sampler.target, 0, static_cast(format.internalFormatTexImage), @@ -146,7 +147,6 @@ void SubmitGLTexture(const GLInterface* gl, const GLSampler& sampler, int width, } } } - gl->functions->bindTexture(sampler.target, 0); } unsigned CreateGLProgram(const GLInterface* gl, const std::string& vertex, diff --git a/tgfx/src/gpu/opengl/GLYUVTexture.cpp b/tgfx/src/gpu/opengl/GLYUVTexture.cpp index 954ab0c095..43e9600904 100644 --- a/tgfx/src/gpu/opengl/GLYUVTexture.cpp +++ b/tgfx/src/gpu/opengl/GLYUVTexture.cpp @@ -124,8 +124,6 @@ std::shared_ptr YUVTexture::MakeI420(Context* context, YUVColorSpace YUVColorRange colorRange, int width, int height, uint8_t* pixelsPlane[3], const int lineSize[3]) { auto gl = GLInterface::Get(context); - GLStateGuard stateGuard(context); - YUVConfig yuvConfig = YUVConfig(colorSpace, colorRange, width, height, I420_PLANE_COUNT); for (int i = 0; i < 3; i++) { yuvConfig.pixelsPlane[i] = pixelsPlane[i]; @@ -156,8 +154,6 @@ std::shared_ptr YUVTexture::MakeNV12(Context* context, YUVColorSpace YUVColorRange colorRange, int width, int height, uint8_t* pixelsPlane[2], const int lineSize[2]) { auto gl = GLInterface::Get(context); - GLStateGuard stateGuard(context); - YUVConfig yuvConfig = YUVConfig(colorSpace, colorRange, width, height, NV12_PLANE_COUNT); for (int i = 0; i < 2; i++) { yuvConfig.pixelsPlane[i] = pixelsPlane[i]; From fc9c22c9932b9766333b64e5842da62294ee4e9d Mon Sep 17 00:00:00 2001 From: domrjchen Date: Mon, 28 Feb 2022 14:39:54 +0800 Subject: [PATCH 4/6] Remove the GLState class. --- tgfx/src/gpu/opengl/GLContext.cpp | 5 +- tgfx/src/gpu/opengl/GLContext.h | 4 +- tgfx/src/gpu/opengl/GLDevice.cpp | 6 - tgfx/src/gpu/opengl/GLInterface.cpp | 47 +- tgfx/src/gpu/opengl/GLInterface.h | 1 - tgfx/src/gpu/opengl/GLRenderTarget.cpp | 1 - tgfx/src/gpu/opengl/GLState.cpp | 1099 ------------------------ tgfx/src/gpu/opengl/GLState.h | 144 ---- 8 files changed, 4 insertions(+), 1303 deletions(-) delete mode 100644 tgfx/src/gpu/opengl/GLState.cpp delete mode 100644 tgfx/src/gpu/opengl/GLState.h diff --git a/tgfx/src/gpu/opengl/GLContext.cpp b/tgfx/src/gpu/opengl/GLContext.cpp index dba6d46d94..57b5aa67bf 100644 --- a/tgfx/src/gpu/opengl/GLContext.cpp +++ b/tgfx/src/gpu/opengl/GLContext.cpp @@ -20,8 +20,7 @@ #include "gpu/opengl/GLDevice.h" namespace tgfx { -GLContext::GLContext(Device* device, const GLInterface* glInterface) : Context(device) { - glState = std::make_unique(glInterface); - interface = GLInterface::HookWithState(glInterface, glState.get()); +GLContext::GLContext(Device* device, const GLInterface* glInterface) + : Context(device), interface(glInterface) { } } // namespace tgfx diff --git a/tgfx/src/gpu/opengl/GLContext.h b/tgfx/src/gpu/opengl/GLContext.h index 001e6f9b6b..5830dff84b 100644 --- a/tgfx/src/gpu/opengl/GLContext.h +++ b/tgfx/src/gpu/opengl/GLContext.h @@ -19,7 +19,6 @@ #pragma once #include "GLInterface.h" -#include "GLState.h" #include "gpu/Context.h" namespace tgfx { @@ -42,8 +41,7 @@ class GLContext : public Context { } private: - std::unique_ptr interface = nullptr; - std::unique_ptr glState = nullptr; + const GLInterface* interface = nullptr; friend class GLDevice; friend class GLInterface; diff --git a/tgfx/src/gpu/opengl/GLDevice.cpp b/tgfx/src/gpu/opengl/GLDevice.cpp index b10ddd6e7a..59bbcf6342 100644 --- a/tgfx/src/gpu/opengl/GLDevice.cpp +++ b/tgfx/src/gpu/opengl/GLDevice.cpp @@ -66,12 +66,6 @@ bool GLDevice::onLockContext() { onClearCurrent(); return false; } - auto glContext = static_cast(context); - if (isAdopted) { - glContext->glState->reset(); - // Clear externally generated GLError. - CheckGLError(glContext->interface.get()); - } return true; } diff --git a/tgfx/src/gpu/opengl/GLInterface.cpp b/tgfx/src/gpu/opengl/GLInterface.cpp index 66aaa0fe79..a2d78f1cde 100644 --- a/tgfx/src/gpu/opengl/GLInterface.cpp +++ b/tgfx/src/gpu/opengl/GLInterface.cpp @@ -17,13 +17,11 @@ ///////////////////////////////////////////////////////////////////////////////////////////////// #include "GLInterface.h" - #include #include #include "GLAssembledGLESInterface.h" #include "GLAssembledGLInterface.h" #include "GLAssembledWebGLInterface.h" -#include "GLState.h" #include "GLUtil.h" namespace tgfx { @@ -43,7 +41,7 @@ static int GetGLVersion(const GLProcGetter* getter) { } const GLInterface* GLInterface::Get(const Context* context) { - return context ? static_cast(context)->interface.get() : nullptr; + return context ? static_cast(context)->interface : nullptr; } const GLInterface* GLInterface::GetNative() { @@ -64,49 +62,6 @@ const GLInterface* GLInterface::GetNative() { return glInterfaceMap[version].get(); } -namespace { -template -GLFunction Bind(GLState* interface, R (GLState::*member)(Args...)) { - return [interface, member](Args... a) -> R { return (interface->*member)(a...); }; -} -} // anonymous namespace - -#ifdef TGFX_BUILD_FOR_WEB -#define Hook(X) state -#else -#define Hook(X) functions->X = Bind(state, &GLState::X) -#endif - -std::unique_ptr GLInterface::HookWithState(const GLInterface* gl, - GLState* state) { - auto functions = std::make_shared(); - *functions = *gl->functions; - Hook(activeTexture); - Hook(blendEquation); - Hook(blendFunc); - Hook(bindFramebuffer); - Hook(bindRenderbuffer); - Hook(bindBuffer); - Hook(bindTexture); - Hook(disable); - Hook(disableVertexAttribArray); - Hook(enable); - Hook(enableVertexAttribArray); - Hook(pixelStorei); - Hook(scissor); - Hook(viewport); - Hook(useProgram); - Hook(vertexAttribPointer); - Hook(depthMask); - if (gl->caps->vertexArrayObjectSupport) { - Hook(bindVertexArray); - } - auto interface = new GLInterface(); - interface->functions = functions; - interface->caps = gl->caps; - return std::unique_ptr(interface); -} - std::unique_ptr GLInterface::MakeNativeInterface(const GLProcGetter* getter) { if (getter == nullptr) { return nullptr; diff --git a/tgfx/src/gpu/opengl/GLInterface.h b/tgfx/src/gpu/opengl/GLInterface.h index d28114cbdf..036257e00f 100644 --- a/tgfx/src/gpu/opengl/GLInterface.h +++ b/tgfx/src/gpu/opengl/GLInterface.h @@ -36,7 +36,6 @@ class GLInterface { private: static const GLInterface* GetNative(); static std::unique_ptr MakeNativeInterface(const GLProcGetter* getter); - static std::unique_ptr HookWithState(const GLInterface* gl, GLState* state); friend class GLDevice; friend class GLContext; diff --git a/tgfx/src/gpu/opengl/GLRenderTarget.cpp b/tgfx/src/gpu/opengl/GLRenderTarget.cpp index 59d90e6a9e..aabc05e1b5 100644 --- a/tgfx/src/gpu/opengl/GLRenderTarget.cpp +++ b/tgfx/src/gpu/opengl/GLRenderTarget.cpp @@ -19,7 +19,6 @@ #include "gpu/opengl/GLRenderTarget.h" #include "core/Bitmap.h" #include "gpu/opengl/GLContext.h" -#include "gpu/opengl/GLState.h" #include "gpu/opengl/GLUtil.h" namespace tgfx { diff --git a/tgfx/src/gpu/opengl/GLState.cpp b/tgfx/src/gpu/opengl/GLState.cpp deleted file mode 100644 index dbad20f3f4..0000000000 --- a/tgfx/src/gpu/opengl/GLState.cpp +++ /dev/null @@ -1,1099 +0,0 @@ -///////////////////////////////////////////////////////////////////////////////////////////////// -// -// Tencent is pleased to support the open source community by making libpag available. -// -// Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file -// except in compliance with the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// unless required by applicable law or agreed to in writing, software distributed under the -// license is distributed on an "as is" basis, without warranties or conditions of any kind, -// either express or implied. see the license for the specific language governing permissions -// and limitations under the license. -// -///////////////////////////////////////////////////////////////////////////////////////////////// - -#include "GLState.h" - -#include "GLContext.h" - -namespace tgfx { -#define PRIORITY_HIGH 3 -#define PRIORITY_MEDIUM 2 -#define PRIORITY_DEFAULT 1 -#define PRIORITY_LOW 0 - -#ifdef DEBUG - -#define UNSUPPORTED_STATE_WARNING() \ - LOGE("%s:%d: error: \"modifying unsupported GL state!\"\n", __FILE__, __LINE__); - -#else - -#define UNSUPPORTED_STATE_WARNING() - -#endif - -#define SAVE_DEFAULT_(state, Type) \ - if ((state)->currentRecord) { \ - auto& attributeMap = (state)->currentRecord->attributeMap; \ - if (attributeMap.count(GLAttributeType::Type) == 0) { \ - attributeMap[GLAttributeType::Type] = \ - (state)->insertAttribute(std::make_shared((state)->gl)); \ - } \ - } - -#define SAVE_DEFAULT(Type) SAVE_DEFAULT_(this, Type) - -class ActiveTexture : public GLAttribute { - public: - explicit ActiveTexture(const GLInterface* gl) { - gl->functions->getIntegerv(GL_ACTIVE_TEXTURE, &activeTexture); - } - - GLAttributeType type() const override { - return GLAttributeType::ActiveTexture; - } - - int priority() const override { - return PRIORITY_LOW; - } - - void apply(GLState* state) const override { - state->gl->functions->activeTexture(activeTexture); - state->currentTextureUnit = activeTexture; - } - - int activeTexture = 0; -}; - -class BlendEquationSeparate : public GLAttribute { - public: - explicit BlendEquationSeparate(const GLInterface* gl) { - gl->functions->getIntegerv(GL_BLEND_EQUATION_RGB, &equationRGB); - gl->functions->getIntegerv(GL_BLEND_EQUATION_ALPHA, &equationAlpha); - } - - GLAttributeType type() const override { - return GLAttributeType::BlendEquationSeparate; - } - - int priority() const override { - return PRIORITY_DEFAULT; - } - - void apply(GLState* state) const override { - state->gl->functions->blendEquationSeparate(equationRGB, equationAlpha); - } - - int equationRGB = 0; - int equationAlpha = 0; -}; - -class BlendFuncSeparate : public GLAttribute { - public: - explicit BlendFuncSeparate(const GLInterface* gl) { - gl->functions->getIntegerv(GL_BLEND_SRC_RGB, &srcRGB); - gl->functions->getIntegerv(GL_BLEND_DST_RGB, &dstRGB); - gl->functions->getIntegerv(GL_BLEND_SRC_ALPHA, &srcAlpha); - gl->functions->getIntegerv(GL_BLEND_DST_ALPHA, &dstAlpha); - } - - GLAttributeType type() const override { - return GLAttributeType::BlendFuncSeparate; - } - - int priority() const override { - return PRIORITY_DEFAULT; - } - - void apply(GLState* state) const override { - state->gl->functions->blendFuncSeparate(srcRGB, dstRGB, srcAlpha, dstAlpha); - } - - int srcRGB = 0; - int dstRGB = 0; - int srcAlpha = 0; - int dstAlpha = 0; -}; - -class CurrentProgram : public GLAttribute { - public: - explicit CurrentProgram(const GLInterface* gl) { - gl->functions->getIntegerv(GL_CURRENT_PROGRAM, &program); - } - - GLAttributeType type() const override { - return GLAttributeType::CurrentProgram; - } - - int priority() const override { - return PRIORITY_DEFAULT; - } - - void apply(GLState* state) const override { - state->gl->functions->useProgram(program); - } - - int program = 0; -}; - -class EnableBlend : public GLAttribute { - public: - explicit EnableBlend(const GLInterface* gl) { - enabled = gl->functions->isEnabled(GL_BLEND); - } - - GLAttributeType type() const override { - return GLAttributeType::EnableBlend; - } - - int priority() const override { - return PRIORITY_DEFAULT; - } - - void apply(GLState* state) const override { - if (enabled) { - state->gl->functions->enable(GL_BLEND); - } else { - state->gl->functions->disable(GL_BLEND); - } - } - - bool enabled = false; -}; - -class EnableCullFace : public GLAttribute { - public: - explicit EnableCullFace(const GLInterface* gl) { - enabled = gl->functions->isEnabled(GL_CULL_FACE); - } - - GLAttributeType type() const override { - return GLAttributeType::EnableCullFace; - } - - int priority() const override { - return PRIORITY_DEFAULT; - } - - void apply(GLState* state) const override { - if (enabled) { - state->gl->functions->enable(GL_CULL_FACE); - } else { - state->gl->functions->disable(GL_CULL_FACE); - } - } - - bool enabled = false; -}; - -class EnableDepth : public GLAttribute { - public: - explicit EnableDepth(const GLInterface* gl) { - enabled = gl->functions->isEnabled(GL_DEPTH_TEST); - } - - GLAttributeType type() const override { - return GLAttributeType::EnableDepth; - } - - int priority() const override { - return PRIORITY_DEFAULT; - } - - void apply(GLState* state) const override { - if (enabled) { - state->gl->functions->enable(GL_DEPTH_TEST); - } else { - state->gl->functions->disable(GL_DEPTH_TEST); - } - } - - bool enabled = false; -}; - -class EnableDither : public GLAttribute { - public: - explicit EnableDither(const GLInterface* gl) { - enabled = gl->functions->isEnabled(GL_DITHER); - } - - GLAttributeType type() const override { - return GLAttributeType::EnableDither; - } - - int priority() const override { - return PRIORITY_DEFAULT; - } - - void apply(GLState* state) const override { - if (enabled) { - state->gl->functions->enable(GL_DITHER); - } else { - state->gl->functions->disable(GL_DITHER); - } - } - - bool enabled = false; -}; - -class EnableFramebufferSRGB : public GLAttribute { - public: - explicit EnableFramebufferSRGB(const GLInterface* gl) { - enabled = gl->functions->isEnabled(GL_FRAMEBUFFER_SRGB); - } - - GLAttributeType type() const override { - return GLAttributeType::EnableFramebufferSRGB; - } - - int priority() const override { - return PRIORITY_DEFAULT; - } - - void apply(GLState* state) const override { - if (enabled) { - state->gl->functions->enable(GL_FRAMEBUFFER_SRGB); - } else { - state->gl->functions->disable(GL_FRAMEBUFFER_SRGB); - } - } - - bool enabled = false; -}; - -class EnableScissor : public GLAttribute { - public: - explicit EnableScissor(const GLInterface* gl) { - enabled = gl->functions->isEnabled(GL_SCISSOR_TEST); - } - - GLAttributeType type() const override { - return GLAttributeType::EnableScissor; - } - - int priority() const override { - return PRIORITY_DEFAULT; - } - - void apply(GLState* state) const override { - if (enabled) { - state->gl->functions->enable(GL_SCISSOR_TEST); - } else { - state->gl->functions->disable(GL_SCISSOR_TEST); - } - } - - bool enabled = false; -}; - -class EnableStencil : public GLAttribute { - public: - explicit EnableStencil(const GLInterface* gl) { - enabled = gl->functions->isEnabled(GL_STENCIL_TEST); - } - - GLAttributeType type() const override { - return GLAttributeType::EnableStencil; - } - - int priority() const override { - return PRIORITY_DEFAULT; - } - - void apply(GLState* state) const override { - if (enabled) { - state->gl->functions->enable(GL_STENCIL_TEST); - } else { - state->gl->functions->disable(GL_STENCIL_TEST); - } - } - - bool enabled = false; -}; - -class EnableVertexProgramPointSize : public GLAttribute { - public: - explicit EnableVertexProgramPointSize(const GLInterface* gl) { - enabled = gl->functions->isEnabled(GL_VERTEX_PROGRAM_POINT_SIZE); - } - - GLAttributeType type() const override { - return GLAttributeType::EnableVertexProgramPointSize; - } - - int priority() const override { - return PRIORITY_DEFAULT; - } - - void apply(GLState* state) const override { - if (enabled) { - state->gl->functions->enable(GL_VERTEX_PROGRAM_POINT_SIZE); - } else { - state->gl->functions->disable(GL_VERTEX_PROGRAM_POINT_SIZE); - } - } - - bool enabled = false; -}; - -class EnableMultisample : public GLAttribute { - public: - explicit EnableMultisample(const GLInterface* gl) { - enabled = gl->functions->isEnabled(GL_MULTISAMPLE); - } - - GLAttributeType type() const override { - return GLAttributeType::EnableMultisample; - } - - int priority() const override { - return PRIORITY_DEFAULT; - } - - void apply(GLState* state) const override { - if (enabled) { - state->gl->functions->enable(GL_MULTISAMPLE); - } else { - state->gl->functions->disable(GL_MULTISAMPLE); - } - } - - bool enabled = false; -}; - -class EnableFetchPerSampleARM : public GLAttribute { - public: - explicit EnableFetchPerSampleARM(const GLInterface* gl) { - enabled = gl->functions->isEnabled(GL_FETCH_PER_SAMPLE_ARM); - } - - GLAttributeType type() const override { - return GLAttributeType::EnableFetchPerSampleARM; - } - - int priority() const override { - return PRIORITY_DEFAULT; - } - - void apply(GLState* state) const override { - if (enabled) { - state->gl->functions->enable(GL_FETCH_PER_SAMPLE_ARM); - } else { - state->gl->functions->disable(GL_FETCH_PER_SAMPLE_ARM); - } - } - - bool enabled = false; -}; - -class EnablePolygonOffsetFill : public GLAttribute { - public: - explicit EnablePolygonOffsetFill(const GLInterface* gl) { - enabled = gl->functions->isEnabled(GL_POLYGON_OFFSET_FILL); - } - - GLAttributeType type() const override { - return GLAttributeType::EnablePolygonOffsetFill; - } - - int priority() const override { - return PRIORITY_DEFAULT; - } - - void apply(GLState* state) const override { - if (enabled) { - state->gl->functions->enable(GL_POLYGON_OFFSET_FILL); - } else { - state->gl->functions->disable(GL_POLYGON_OFFSET_FILL); - } - } - - bool enabled = false; -}; - -class ElementBufferBinding : public GLAttribute { - public: - explicit ElementBufferBinding(const GLInterface* gl) { - gl->functions->getIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &buffer); - } - - GLAttributeType type() const override { - return GLAttributeType::ElementBufferBinding; - } - - int priority() const override { - return PRIORITY_MEDIUM; - } - - void apply(GLState* state) const override { - state->gl->functions->bindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer); - } - - int buffer = 0; -}; - -class FrameBufferBinding : public GLAttribute { - public: - explicit FrameBufferBinding(const GLInterface* gl) { - gl->functions->getIntegerv(GL_FRAMEBUFFER_BINDING, &buffer); - } - - GLAttributeType type() const override { - return GLAttributeType::FrameBufferBinding; - } - - int priority() const override { - return PRIORITY_DEFAULT; - } - - void apply(GLState* state) const override { - state->gl->functions->bindFramebuffer(GL_FRAMEBUFFER, buffer); - } - - int buffer = 0; -}; - -class ReadFrameBufferBinding : public GLAttribute { - public: - explicit ReadFrameBufferBinding(const GLInterface* gl) { - gl->functions->getIntegerv(GL_READ_FRAMEBUFFER_BINDING, &buffer); - } - - GLAttributeType type() const override { - return GLAttributeType::ReadFrameBufferBinding; - } - - int priority() const override { - return PRIORITY_DEFAULT; - } - - void apply(GLState* state) const override { - state->gl->functions->bindFramebuffer(GL_READ_FRAMEBUFFER, buffer); - } - - int buffer = 0; -}; - -class DrawFrameBufferBinding : public GLAttribute { - public: - explicit DrawFrameBufferBinding(const GLInterface* gl) { - gl->functions->getIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &buffer); - } - - GLAttributeType type() const override { - return GLAttributeType::DrawFrameBufferBinding; - } - - int priority() const override { - return PRIORITY_DEFAULT; - } - - void apply(GLState* state) const override { - state->gl->functions->bindFramebuffer(GL_DRAW_FRAMEBUFFER, buffer); - } - - int buffer = 0; -}; - -class RenderBufferBinding : public GLAttribute { - public: - explicit RenderBufferBinding(const GLInterface* gl) { - gl->functions->getIntegerv(GL_RENDERBUFFER_BINDING, &buffer); - } - - GLAttributeType type() const override { - return GLAttributeType::RenderBufferBinding; - } - - int priority() const override { - return PRIORITY_DEFAULT; - } - - void apply(GLState* state) const override { - state->gl->functions->bindRenderbuffer(GL_RENDERBUFFER, buffer); - } - - int buffer = 0; -}; - -class PackAlignment : public GLAttribute { - public: - explicit PackAlignment(const GLInterface* gl) { - gl->functions->getIntegerv(GL_PACK_ALIGNMENT, &alignment); - } - - GLAttributeType type() const override { - return GLAttributeType::PackAlignment; - } - - int priority() const override { - return PRIORITY_DEFAULT; - } - - void apply(GLState* state) const override { - state->gl->functions->pixelStorei(GL_PACK_ALIGNMENT, alignment); - } - - int alignment = 0; -}; - -class PackRowLength : public GLAttribute { - public: - explicit PackRowLength(const GLInterface* gl) { - gl->functions->getIntegerv(GL_PACK_ROW_LENGTH, &rowLength); - } - - GLAttributeType type() const override { - return GLAttributeType::PackRowLength; - } - - int priority() const override { - return PRIORITY_DEFAULT; - } - - void apply(GLState* state) const override { - state->gl->functions->pixelStorei(GL_PACK_ROW_LENGTH, rowLength); - } - - int rowLength = 0; -}; - -class ScissorBox : public GLAttribute { - public: - explicit ScissorBox(const GLInterface* gl) { - gl->functions->getIntegerv(GL_SCISSOR_BOX, box); - } - - GLAttributeType type() const override { - return GLAttributeType::ScissorBox; - } - - int priority() const override { - return PRIORITY_DEFAULT; - } - - void apply(GLState* state) const override { - state->gl->functions->scissor(box[0], box[1], box[2], box[3]); - } - - int box[4] = {}; -}; - -class TextureBinding : public GLAttribute { - public: - TextureBinding(const GLInterface* gl, unsigned textureUnit, unsigned textureTarget) - : textureUnit(textureUnit), textureTarget(textureTarget) { - switch (textureTarget) { - case GL_TEXTURE_2D: - gl->functions->getIntegerv(GL_TEXTURE_BINDING_2D, &textureID); - break; - case GL_TEXTURE_EXTERNAL_OES: - gl->functions->getIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &textureID); - break; - case GL_TEXTURE_RECTANGLE: - gl->functions->getIntegerv(GL_TEXTURE_BINDING_RECTANGLE, &textureID); - break; - default: - UNSUPPORTED_STATE_WARNING() - break; - } - } - - GLAttributeType type() const override { - return GLAttributeType::TextureBinding; - } - - int priority() const override { - return PRIORITY_DEFAULT; - } - - void apply(GLState* state) const override { - state->gl->functions->activeTexture(textureUnit); - state->gl->functions->bindTexture(textureTarget, textureID); - } - - private: - unsigned textureUnit = 0; - unsigned textureTarget = 0; - int textureID = 0; -}; - -class UnpackAlignment : public GLAttribute { - public: - explicit UnpackAlignment(const GLInterface* gl) { - gl->functions->getIntegerv(GL_UNPACK_ALIGNMENT, &alignment); - } - - GLAttributeType type() const override { - return GLAttributeType::UnpackAlignment; - } - - int priority() const override { - return PRIORITY_DEFAULT; - } - - void apply(GLState* state) const override { - state->gl->functions->pixelStorei(GL_UNPACK_ALIGNMENT, alignment); - } - - int alignment = 0; -}; - -class UnpackRowLength : public GLAttribute { - public: - explicit UnpackRowLength(const GLInterface* gl) { - gl->functions->getIntegerv(GL_UNPACK_ROW_LENGTH, &rowLength); - } - - GLAttributeType type() const override { - return GLAttributeType::UnpackRowLength; - } - - int priority() const override { - return PRIORITY_DEFAULT; - } - - void apply(GLState* state) const override { - state->gl->functions->pixelStorei(GL_UNPACK_ROW_LENGTH, rowLength); - } - - int rowLength = 0; -}; - -class VertexArrayBinding : public GLAttribute { - public: - explicit VertexArrayBinding(const GLInterface* gl) { - gl->functions->getIntegerv(GL_VERTEX_ARRAY_BINDING, &vertexArray); - } - - GLAttributeType type() const override { - return GLAttributeType::VertexArrayBinding; - } - - int priority() const override { - return PRIORITY_HIGH; - } - - void apply(GLState* state) const override { - state->gl->functions->bindVertexArray(vertexArray); - state->currentVAO = vertexArray; - } - - int vertexArray = 0; -}; - -class VertexAttribute : public GLAttribute { - public: - explicit VertexAttribute(const GLInterface* gl, unsigned index) : index(index) { - gl->functions->getVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING, &vbo); - gl->functions->getVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_ENABLED, &enabled); - gl->functions->getVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_SIZE, &size); - gl->functions->getVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_TYPE, &dataType); - gl->functions->getVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_NORMALIZED, &normalized); - gl->functions->getVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_STRIDE, &stride); - gl->functions->getVertexAttribPointerv(index, GL_VERTEX_ATTRIB_ARRAY_POINTER, &pointer); - } - - GLAttributeType type() const override { - return GLAttributeType::VertexAttribute; - } - - int priority() const override { - return PRIORITY_DEFAULT; - } - - void apply(GLState* state) const override { - state->gl->functions->bindBuffer(GL_ARRAY_BUFFER, vbo); - state->gl->functions->vertexAttribPointer(index, size, dataType, normalized, stride, pointer); - if (enabled == GL_TRUE) { - state->gl->functions->enableVertexAttribArray(index); - } else { - state->gl->functions->disableVertexAttribArray(index); - } - } - - private: - unsigned index = 0; - int vbo = 0; - int enabled = GL_FALSE; - int size = 0; - int dataType = 0; - int normalized = 0; - int stride = 0; - void* pointer = nullptr; -}; - -class VertexBufferBinding : public GLAttribute { - public: - explicit VertexBufferBinding(const GLInterface* gl) { - gl->functions->getIntegerv(GL_ARRAY_BUFFER_BINDING, &buffer); - } - - GLAttributeType type() const override { - return GLAttributeType::VertexBufferBinding; - } - - int priority() const override { - return PRIORITY_LOW; - } - - void apply(GLState* state) const override { - state->gl->functions->bindBuffer(GL_ARRAY_BUFFER, buffer); - } - - int buffer = 0; -}; - -class DepthMask : public GLAttribute { - public: - explicit DepthMask(const GLInterface* gl) { - gl->functions->getBooleanv(GL_DEPTH_WRITEMASK, &flag); - } - - GLAttributeType type() const override { - return GLAttributeType::DepthMask; - } - - int priority() const override { - return PRIORITY_LOW; - } - - void apply(GLState* state) const override { - state->gl->functions->depthMask(flag); - } - - unsigned char flag = false; -}; - -class Viewport : public GLAttribute { - public: - explicit Viewport(const GLInterface* gl) { - gl->functions->getIntegerv(GL_VIEWPORT, viewport); - } - - GLAttributeType type() const override { - return GLAttributeType::Viewport; - } - - int priority() const override { - return PRIORITY_DEFAULT; - } - - void apply(GLState* state) const override { - state->gl->functions->viewport(viewport[0], viewport[1], viewport[2], viewport[3]); - } - - int viewport[4] = {}; -}; - -GLState::GLState(const GLInterface* gl) : gl(gl) { - reset(); -} - -void GLState::reset() { - if (gl->caps->vertexArrayObjectSupport) { - int vao = 0; - gl->functions->getIntegerv(GL_VERTEX_ARRAY_BINDING, &vao); - currentVAO = vao; - } - int texture = 0; - gl->functions->getIntegerv(GL_ACTIVE_TEXTURE, &texture); - currentTextureUnit = texture; - currentRecord = nullptr; - recordList = {}; -} - -void GLState::save() { - if (currentRecord) { - recordList.push_back(currentRecord); - } - currentRecord = std::make_shared(currentVAO); -} - -void GLState::restore() { - if (currentRecord == nullptr) { - return; - } - for (auto& attribute : currentRecord->defaultAttributes) { - attribute->apply(this); - } - if (recordList.empty()) { - currentRecord = nullptr; - } else { - currentRecord = recordList.back(); - recordList.pop_back(); - } -} - -void GLState::activeTexture(unsigned textureUnit) { - if (textureUnit == currentTextureUnit) { - return; - } - SAVE_DEFAULT(ActiveTexture) - currentTextureUnit = textureUnit; - gl->functions->activeTexture(textureUnit); -} - -void GLState::blendEquation(unsigned mode) { - SAVE_DEFAULT(BlendEquationSeparate) - gl->functions->blendEquation(mode); -} - -void GLState::blendEquationSeparate(unsigned modeRGB, unsigned modeAlpha) { - SAVE_DEFAULT(BlendEquationSeparate) - gl->functions->blendEquationSeparate(modeRGB, modeAlpha); -} - -void GLState::blendFunc(unsigned int srcFactor, unsigned int dstFactor) { - SAVE_DEFAULT(BlendFuncSeparate) - gl->functions->blendFunc(srcFactor, dstFactor); -} - -void GLState::blendFuncSeparate(unsigned srcRGB, unsigned dstRGB, unsigned srcAlpha, - unsigned dstAlpha) { - SAVE_DEFAULT(BlendFuncSeparate) - gl->functions->blendFuncSeparate(srcRGB, dstRGB, srcAlpha, dstAlpha); -} - -void GLState::bindFramebuffer(unsigned target, unsigned framebuffer) { - switch (target) { - case GL_FRAMEBUFFER: - SAVE_DEFAULT(FrameBufferBinding) - break; - case GL_READ_FRAMEBUFFER: - SAVE_DEFAULT(ReadFrameBufferBinding) - break; - case GL_DRAW_FRAMEBUFFER: - SAVE_DEFAULT(DrawFrameBufferBinding) - break; - default: - UNSUPPORTED_STATE_WARNING() - break; - } - gl->functions->bindFramebuffer(target, framebuffer); -} - -void GLState::bindRenderbuffer(unsigned int target, unsigned int renderbuffer) { - if (target == GL_RENDERBUFFER) { - SAVE_DEFAULT(RenderBufferBinding) - } else { - UNSUPPORTED_STATE_WARNING() - } - gl->functions->bindRenderbuffer(target, renderbuffer); -} - -void GLState::bindBuffer(unsigned target, unsigned buffer) { - switch (target) { - case GL_ARRAY_BUFFER: - SAVE_DEFAULT(VertexBufferBinding) - break; - case GL_ELEMENT_ARRAY_BUFFER: - if (currentRecord && currentRecord->defaultVAO == currentVAO) { - // EBO is stored on VAO, and we save the default value only if the current VAO is the - // default. - SAVE_DEFAULT(ElementBufferBinding) - } - break; - default: - UNSUPPORTED_STATE_WARNING() - break; - } - gl->functions->bindBuffer(target, buffer); -} - -void GLState::bindTexture(unsigned target, unsigned texture) { - if (currentRecord) { - auto& textureMap = currentRecord->textureMap; - uint32_t uint = currentTextureUnit - GL_TEXTURE0; - auto key = (uint << 28) | target; - if (textureMap.count(key) == 0) { - textureMap[key] = - insertAttribute(std::make_shared(gl, currentTextureUnit, target)); - } - } - gl->functions->bindTexture(target, texture); -} - -void GLState::bindVertexArray(unsigned vertexArray) { - if (vertexArray == currentVAO) { - return; - } - SAVE_DEFAULT(VertexArrayBinding) - currentVAO = vertexArray; - gl->functions->bindVertexArray(vertexArray); -} - -static void SaveDefaultBlend(GLState* state) { - SAVE_DEFAULT_(state, EnableBlend) -} - -static void SaveDefaultCullFace(GLState* state) { - SAVE_DEFAULT_(state, EnableCullFace) -} -static void SaveDefaultDepth(GLState* state) { - SAVE_DEFAULT_(state, EnableDepth) -} -static void SaveDefaultDither(GLState* state) { - SAVE_DEFAULT_(state, EnableDither) -} -static void SaveDefaultFramebufferSRGB(GLState* state) { - SAVE_DEFAULT_(state, EnableFramebufferSRGB) -} -static void SaveDefaultStencil(GLState* state) { - SAVE_DEFAULT_(state, EnableStencil) -} -static void SaveDefaultScissor(GLState* state) { - SAVE_DEFAULT_(state, EnableScissor) -} -static void SaveDefaultVertexProgramPointSize(GLState* state) { - SAVE_DEFAULT_(state, EnableVertexProgramPointSize) -} -static void SaveDefaultMultisample(GLState* state) { - SAVE_DEFAULT_(state, EnableMultisample) -} -static void SaveDefaultFetchPerSampleARM(GLState* state) { - SAVE_DEFAULT_(state, EnableFetchPerSampleARM) -} -static void SaveDefaultPolygonOffsetFill(GLState* state) { - SAVE_DEFAULT_(state, EnablePolygonOffsetFill); -} - -static constexpr std::pair StateMap[] = { - {GL_BLEND, SaveDefaultBlend}, - {GL_CULL_FACE, SaveDefaultCullFace}, - {GL_DEPTH_TEST, SaveDefaultDepth}, - {GL_DITHER, SaveDefaultDither}, - {GL_SCISSOR_TEST, SaveDefaultScissor}, - {GL_FRAMEBUFFER_SRGB, SaveDefaultFramebufferSRGB}, - {GL_STENCIL_TEST, SaveDefaultStencil}, - {GL_VERTEX_PROGRAM_POINT_SIZE, SaveDefaultVertexProgramPointSize}, - {GL_MULTISAMPLE, SaveDefaultMultisample}, - {GL_FETCH_PER_SAMPLE_ARM, SaveDefaultFetchPerSampleARM}, - {GL_POLYGON_OFFSET_FILL, SaveDefaultPolygonOffsetFill}, -}; - -void GLState::disable(unsigned cap) { - auto found = false; - for (const auto& pair : StateMap) { - if (pair.first == cap) { - pair.second(this); - found = true; - break; - } - } - if (!found) { - // UNSUPPORTED_STATE_WARNING() - } - gl->functions->disable(cap); -} - -void GLState::disableVertexAttribArray(unsigned index) { - saveVertexAttribute(index); - gl->functions->disableVertexAttribArray(index); -} - -void GLState::enable(unsigned cap) { - auto found = false; - for (const auto& pair : StateMap) { - if (pair.first == cap) { - pair.second(this); - found = true; - break; - } - } - if (!found) { - UNSUPPORTED_STATE_WARNING() - } - gl->functions->enable(cap); -} - -void GLState::enableVertexAttribArray(unsigned index) { - saveVertexAttribute(index); - gl->functions->enableVertexAttribArray(index); -} - -void GLState::pixelStorei(unsigned int name, int param) { - switch (name) { - case GL_UNPACK_ROW_LENGTH: - SAVE_DEFAULT(UnpackRowLength) - break; - case GL_UNPACK_ALIGNMENT: - SAVE_DEFAULT(UnpackAlignment) - break; - case GL_PACK_ROW_LENGTH: - SAVE_DEFAULT(PackRowLength) - break; - case GL_PACK_ALIGNMENT: - SAVE_DEFAULT(PackAlignment) - break; - default: - UNSUPPORTED_STATE_WARNING() - break; - } - gl->functions->pixelStorei(name, param); -} - -void GLState::scissor(int x, int y, int width, int height) { - SAVE_DEFAULT(ScissorBox) - gl->functions->scissor(x, y, width, height); -} - -void GLState::viewport(int x, int y, int width, int height) { - SAVE_DEFAULT(Viewport) - gl->functions->viewport(x, y, width, height); -} - -void GLState::useProgram(unsigned program) { - SAVE_DEFAULT(CurrentProgram) - gl->functions->useProgram(program); -} - -void GLState::vertexAttribPointer(unsigned index, int size, unsigned type, unsigned char normalized, - int stride, const void* ptr) { - saveVertexAttribute(index); - gl->functions->vertexAttribPointer(index, size, type, normalized, stride, ptr); -} - -void GLState::depthMask(unsigned char flag) { - SAVE_DEFAULT(DepthMask); - gl->functions->depthMask(flag); -} - -void GLState::saveVertexAttribute(unsigned int index) { - if (currentRecord && currentRecord->defaultVAO == currentVAO) { - auto& vertexMap = currentRecord->vertexMap; - if (vertexMap.count(index) == 0) { - vertexMap[index] = insertAttribute(std::make_shared(gl, index)); - } - } -} - -std::shared_ptr GLState::insertAttribute( - std::shared_ptr attribute) const { - auto& defaultAttributes = currentRecord->defaultAttributes; - switch (attribute->priority()) { - case PRIORITY_HIGH: - defaultAttributes.push_front(attribute); - break; - case PRIORITY_LOW: - defaultAttributes.push_back(attribute); - break; - default: - auto position = defaultAttributes.begin(); - for (auto& item : defaultAttributes) { - if (item->priority() <= attribute->priority()) { - break; - } - position++; - } - defaultAttributes.insert(position, attribute); - break; - } - return attribute; -} -} // namespace tgfx diff --git a/tgfx/src/gpu/opengl/GLState.h b/tgfx/src/gpu/opengl/GLState.h deleted file mode 100644 index 82f4e90499..0000000000 --- a/tgfx/src/gpu/opengl/GLState.h +++ /dev/null @@ -1,144 +0,0 @@ -///////////////////////////////////////////////////////////////////////////////////////////////// -// -// Tencent is pleased to support the open source community by making libpag available. -// -// Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file -// except in compliance with the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// unless required by applicable law or agreed to in writing, software distributed under the -// license is distributed on an "as is" basis, without warranties or conditions of any kind, -// either express or implied. see the license for the specific language governing permissions -// and limitations under the license. -// -///////////////////////////////////////////////////////////////////////////////////////////////// - -#pragma once - -#include -#include -#include "GLInterface.h" - -namespace tgfx { -enum class GLAttributeType { - ActiveTexture, - BlendEquationSeparate, - BlendFuncSeparate, - CurrentProgram, - ElementBufferBinding, - EnableBlend, - EnableCullFace, - EnableDepth, - EnableDither, - EnableFramebufferSRGB, - EnableScissor, - EnableStencil, - EnableVertexProgramPointSize, - EnableMultisample, - EnableFetchPerSampleARM, - EnablePolygonOffsetFill, - FrameBufferBinding, - ReadFrameBufferBinding, - DrawFrameBufferBinding, - RenderBufferBinding, - PackAlignment, - PackRowLength, - ScissorBox, - TextureBinding, - UnpackAlignment, - UnpackRowLength, - VertexArrayBinding, - VertexAttribute, - VertexBufferBinding, - DepthMask, - Viewport -}; - -class GLAttribute { - public: - virtual ~GLAttribute() = default; - - virtual GLAttributeType type() const = 0; - - virtual int priority() const = 0; - - virtual void apply(GLState* state) const = 0; -}; - -class StateRecord { - public: - explicit StateRecord(unsigned currentVAO) : defaultVAO(currentVAO) { - } - - unsigned defaultVAO = 0; - std::list> defaultAttributes = {}; - std::unordered_map> vertexMap = {}; - std::unordered_map> textureMap = {}; - std::unordered_map, EnumHasher> attributeMap = {}; -}; - -class GLState { - public: - explicit GLState(const GLInterface* gl); - - void reset(); - - void save(); - - void restore(); - - void activeTexture(unsigned textureUnit); - - void blendEquation(unsigned mode); - - void blendEquationSeparate(unsigned modeRGB, unsigned modeAlpha); - - void blendFunc(unsigned srcFactor, unsigned dstFactor); - - void blendFuncSeparate(unsigned srcRGB, unsigned dstRGB, unsigned srcAlpha, unsigned dstAlpha); - - void bindFramebuffer(unsigned target, unsigned framebuffer); - - void bindRenderbuffer(unsigned target, unsigned renderbuffer); - - void bindBuffer(unsigned target, unsigned buffer); - - void bindTexture(unsigned target, unsigned texture); - - void bindVertexArray(unsigned vertexArray); - - void disable(unsigned cap); - - void disableVertexAttribArray(unsigned index); - - void enable(unsigned cap); - - void enableVertexAttribArray(unsigned index); - - void pixelStorei(unsigned name, int param); - - void scissor(int x, int y, int width, int height); - - void viewport(int x, int y, int width, int height); - - void useProgram(unsigned program); - - void vertexAttribPointer(unsigned index, int size, unsigned type, unsigned char normalized, - int stride, const void* ptr); - - void depthMask(unsigned char flag); - - const GLInterface* gl = nullptr; - unsigned currentVAO = 0; - unsigned currentTextureUnit = 0; - std::shared_ptr currentRecord = nullptr; - std::vector> recordList = {}; - void saveVertexAttribute(unsigned index); - std::shared_ptr insertAttribute(std::shared_ptr attribute) const; -}; - -class Context; -} // namespace tgfx From b02fd530d7b86e79fc61a8dfa5c72ec4d80a6269 Mon Sep 17 00:00:00 2001 From: domrjchen Date: Mon, 28 Feb 2022 14:40:29 +0800 Subject: [PATCH 5/6] Remove the GLFunction template. --- src/rendering/utils/GLRestorer.cpp | 2 +- tgfx/include/gpu/opengl/GLFunctions.h | 295 +++++++++++--------------- 2 files changed, 128 insertions(+), 169 deletions(-) diff --git a/src/rendering/utils/GLRestorer.cpp b/src/rendering/utils/GLRestorer.cpp index 457a6410de..b02948e891 100644 --- a/src/rendering/utils/GLRestorer.cpp +++ b/src/rendering/utils/GLRestorer.cpp @@ -16,7 +16,7 @@ GLRestorer::GLRestorer(const tgfx::GLFunctions* gl) : gl(gl) { gl->getIntegerv(GL_TEXTURE_BINDING_2D, &textureID); gl->getIntegerv(GL_ARRAY_BUFFER_BINDING, &arrayBuffer); gl->getIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &elementArrayBuffer); - if (gl->bindVertexArray) { + if (gl->bindVertexArray != nullptr) { gl->getIntegerv(GL_VERTEX_ARRAY_BINDING, &vertexArray); } gl->bindBuffer(GL_ARRAY_BUFFER, 0); diff --git a/tgfx/include/gpu/opengl/GLFunctions.h b/tgfx/include/gpu/opengl/GLFunctions.h index c3e80b4f11..d88150406a 100644 --- a/tgfx/include/gpu/opengl/GLFunctions.h +++ b/tgfx/include/gpu/opengl/GLFunctions.h @@ -216,47 +216,6 @@ using GLVertexAttribPointer = void GL_FUNCTION_TYPE(unsigned indx, int size, uns using GLViewport = void GL_FUNCTION_TYPE(int x, int y, int width, int height); using GLWaitSync = void GL_FUNCTION_TYPE(void* sync, unsigned flags, uint64_t timeout); -// This is a lighter-weight std::function, trying to reduce code size and compile time by only -// supporting the exact use cases we require. -template -class GLFunction; - -template -class GLFunction { - public: - GLFunction() = default; - - GLFunction(std::nullptr_t) { - } - - template - GLFunction(Closure closure) : GLFunction() { - static_assert(sizeof(Closure) <= sizeof(buffer), "buffer is too small"); - memcpy(buffer, &closure, sizeof(closure)); - call = [](const void* buf, Args... args) { - auto closure = reinterpret_cast(buf); - return (*closure)(std::forward(args)...); - }; - } - - R operator()(Args... args) const { - return call(buffer, std::forward(args)...); - } - - explicit operator bool() const { - return call != nullptr; - } - - void reset() { - call = nullptr; - } - - private: - using Call = R(const void* buf, Args...); - Call* call = nullptr; - size_t buffer[4] = {}; -}; - class Context; /** @@ -269,133 +228,133 @@ class GLFunctions { */ static const GLFunctions* Get(const Context* context); - GLFunction activeTexture = nullptr; - GLFunction attachShader = nullptr; - GLFunction bindAttribLocation = nullptr; - GLFunction bindBuffer = nullptr; - GLFunction bindFramebuffer = nullptr; - GLFunction bindRenderbuffer = nullptr; - GLFunction bindTexture = nullptr; - GLFunction bindVertexArray = nullptr; - GLFunction blendColor = nullptr; - GLFunction blendEquation = nullptr; - GLFunction blendEquationSeparate = nullptr; - GLFunction blendFunc = nullptr; - GLFunction blendFuncSeparate = nullptr; - GLFunction bufferData = nullptr; - GLFunction bufferSubData = nullptr; - GLFunction checkFramebufferStatus = nullptr; - GLFunction clear = nullptr; - GLFunction clearColor = nullptr; - GLFunction clearStencil = nullptr; - GLFunction colorMask = nullptr; - GLFunction compileShader = nullptr; - GLFunction compressedTexImage2D = nullptr; - GLFunction compressedTexSubImage2D = nullptr; - GLFunction copyTexSubImage2D = nullptr; - GLFunction createProgram = nullptr; - GLFunction createShader = nullptr; - GLFunction cullFace = nullptr; - GLFunction deleteBuffers = nullptr; - GLFunction deleteFramebuffers = nullptr; - GLFunction deleteProgram = nullptr; - GLFunction deleteRenderbuffers = nullptr; - GLFunction deleteShader = nullptr; - GLFunction deleteSync = nullptr; - GLFunction deleteTextures = nullptr; - GLFunction deleteVertexArrays = nullptr; - GLFunction depthMask = nullptr; - GLFunction disable = nullptr; - GLFunction disableVertexAttribArray = nullptr; - GLFunction drawArrays = nullptr; - GLFunction drawElements = nullptr; - GLFunction enable = nullptr; - GLFunction isEnabled = nullptr; - GLFunction enableVertexAttribArray = nullptr; - GLFunction fenceSync = nullptr; - GLFunction finish = nullptr; - GLFunction flush = nullptr; - GLFunction framebufferRenderbuffer = nullptr; - GLFunction framebufferTexture2D = nullptr; - GLFunction framebufferTexture2DMultisample = nullptr; - GLFunction frontFace = nullptr; - GLFunction genBuffers = nullptr; - GLFunction genFramebuffers = nullptr; - GLFunction generateMipmap = nullptr; - GLFunction genRenderbuffers = nullptr; - GLFunction genTextures = nullptr; - GLFunction genVertexArrays = nullptr; - GLFunction getBufferParameteriv = nullptr; - GLFunction getError = nullptr; - GLFunction getFramebufferAttachmentParameteriv = nullptr; - GLFunction getIntegerv = nullptr; - GLFunction getInternalformativ = nullptr; - GLFunction getBooleanv = nullptr; - GLFunction getProgramInfoLog = nullptr; - GLFunction getProgramiv = nullptr; - GLFunction getRenderbufferParameteriv = nullptr; - GLFunction getShaderInfoLog = nullptr; - GLFunction getShaderiv = nullptr; - GLFunction getShaderPrecisionFormat = nullptr; - GLFunction getString = nullptr; - GLFunction getStringi = nullptr; - GLFunction getVertexAttribiv = nullptr; - GLFunction getVertexAttribPointerv = nullptr; - GLFunction getAttribLocation = nullptr; - GLFunction getUniformLocation = nullptr; - GLFunction isTexture = nullptr; - GLFunction lineWidth = nullptr; - GLFunction linkProgram = nullptr; - GLFunction pixelStorei = nullptr; - GLFunction readPixels = nullptr; - GLFunction renderbufferStorage = nullptr; - GLFunction renderbufferStorageMultisample = nullptr; - GLFunction renderbufferStorageMultisampleAPPLE = nullptr; - GLFunction renderbufferStorageMultisampleEXT = nullptr; - GLFunction resolveMultisampleFramebuffer = nullptr; - GLFunction blitFramebuffer = nullptr; - GLFunction scissor = nullptr; - GLFunction shaderSource = nullptr; - GLFunction stencilFunc = nullptr; - GLFunction stencilFuncSeparate = nullptr; - GLFunction stencilMask = nullptr; - GLFunction stencilMaskSeparate = nullptr; - GLFunction stencilOp = nullptr; - GLFunction stencilOpSeparate = nullptr; - GLFunction texImage2D = nullptr; - GLFunction texParameterf = nullptr; - GLFunction texParameterfv = nullptr; - GLFunction texParameteri = nullptr; - GLFunction texParameteriv = nullptr; - GLFunction texSubImage2D = nullptr; - GLFunction textureBarrier = nullptr; - GLFunction uniform1f = nullptr; - GLFunction uniform1i = nullptr; - GLFunction uniform1fv = nullptr; - GLFunction uniform1iv = nullptr; - GLFunction uniform2f = nullptr; - GLFunction uniform2i = nullptr; - GLFunction uniform2fv = nullptr; - GLFunction uniform2iv = nullptr; - GLFunction uniform3f = nullptr; - GLFunction uniform3i = nullptr; - GLFunction uniform3fv = nullptr; - GLFunction uniform3iv = nullptr; - GLFunction uniform4f = nullptr; - GLFunction uniform4i = nullptr; - GLFunction uniform4fv = nullptr; - GLFunction uniform4iv = nullptr; - GLFunction uniformMatrix2fv = nullptr; - GLFunction uniformMatrix3fv = nullptr; - GLFunction uniformMatrix4fv = nullptr; - GLFunction useProgram = nullptr; - GLFunction vertexAttrib1f = nullptr; - GLFunction vertexAttrib2fv = nullptr; - GLFunction vertexAttrib3fv = nullptr; - GLFunction vertexAttrib4fv = nullptr; - GLFunction vertexAttribPointer = nullptr; - GLFunction viewport = nullptr; - GLFunction waitSync = nullptr; + GLActiveTexture* activeTexture = nullptr; + GLAttachShader* attachShader = nullptr; + GLBindAttribLocation* bindAttribLocation = nullptr; + GLBindBuffer* bindBuffer = nullptr; + GLBindFramebuffer* bindFramebuffer = nullptr; + GLBindRenderbuffer* bindRenderbuffer = nullptr; + GLBindTexture* bindTexture = nullptr; + GLBindVertexArray* bindVertexArray = nullptr; + GLBlendColor* blendColor = nullptr; + GLBlendEquation* blendEquation = nullptr; + GLBlendEquationSeparate* blendEquationSeparate = nullptr; + GLBlendFunc* blendFunc = nullptr; + GLBlendFuncSeparate* blendFuncSeparate = nullptr; + GLBufferData* bufferData = nullptr; + GLBufferSubData* bufferSubData = nullptr; + GLCheckFramebufferStatus* checkFramebufferStatus = nullptr; + GLClear* clear = nullptr; + GLClearColor* clearColor = nullptr; + GLClearStencil* clearStencil = nullptr; + GLColorMask* colorMask = nullptr; + GLCompileShader* compileShader = nullptr; + GLCompressedTexImage2D* compressedTexImage2D = nullptr; + GLCompressedTexSubImage2D* compressedTexSubImage2D = nullptr; + GLCopyTexSubImage2D* copyTexSubImage2D = nullptr; + GLCreateProgram* createProgram = nullptr; + GLCreateShader* createShader = nullptr; + GLCullFace* cullFace = nullptr; + GLDeleteBuffers* deleteBuffers = nullptr; + GLDeleteFramebuffers* deleteFramebuffers = nullptr; + GLDeleteProgram* deleteProgram = nullptr; + GLDeleteRenderbuffers* deleteRenderbuffers = nullptr; + GLDeleteShader* deleteShader = nullptr; + GLDeleteSync* deleteSync = nullptr; + GLDeleteTextures* deleteTextures = nullptr; + GLDeleteVertexArrays* deleteVertexArrays = nullptr; + GLDepthMask* depthMask = nullptr; + GLDisable* disable = nullptr; + GLDisableVertexAttribArray* disableVertexAttribArray = nullptr; + GLDrawArrays* drawArrays = nullptr; + GLDrawElements* drawElements = nullptr; + GLEnable* enable = nullptr; + GLIsEnabled* isEnabled = nullptr; + GLEnableVertexAttribArray* enableVertexAttribArray = nullptr; + GLFenceSync* fenceSync = nullptr; + GLFinish* finish = nullptr; + GLFlush* flush = nullptr; + GLFramebufferRenderbuffer* framebufferRenderbuffer = nullptr; + GLFramebufferTexture2D* framebufferTexture2D = nullptr; + GLFramebufferTexture2DMultisample* framebufferTexture2DMultisample = nullptr; + GLFrontFace* frontFace = nullptr; + GLGenBuffers* genBuffers = nullptr; + GLGenFramebuffers* genFramebuffers = nullptr; + GLGenerateMipmap* generateMipmap = nullptr; + GLGenRenderbuffers* genRenderbuffers = nullptr; + GLGenTextures* genTextures = nullptr; + GLGenVertexArrays* genVertexArrays = nullptr; + GLGetBufferParameteriv* getBufferParameteriv = nullptr; + GLGetError* getError = nullptr; + GLGetFramebufferAttachmentParameteriv* getFramebufferAttachmentParameteriv = nullptr; + GLGetIntegerv* getIntegerv = nullptr; + GLGetInternalformativ* getInternalformativ = nullptr; + GLGetBooleanv* getBooleanv = nullptr; + GLGetProgramInfoLog* getProgramInfoLog = nullptr; + GLGetProgramiv* getProgramiv = nullptr; + GLGetRenderbufferParameteriv* getRenderbufferParameteriv = nullptr; + GLGetShaderInfoLog* getShaderInfoLog = nullptr; + GLGetShaderiv* getShaderiv = nullptr; + GLGetShaderPrecisionFormat* getShaderPrecisionFormat = nullptr; + GLGetString* getString = nullptr; + GLGetStringi* getStringi = nullptr; + GLGetVertexAttribiv* getVertexAttribiv = nullptr; + GLGetVertexAttribPointerv* getVertexAttribPointerv = nullptr; + GLGetAttribLocation* getAttribLocation = nullptr; + GLGetUniformLocation* getUniformLocation = nullptr; + GLIsTexture* isTexture = nullptr; + GLLineWidth* lineWidth = nullptr; + GLLinkProgram* linkProgram = nullptr; + GLPixelStorei* pixelStorei = nullptr; + GLReadPixels* readPixels = nullptr; + GLRenderbufferStorage* renderbufferStorage = nullptr; + GLRenderbufferStorageMultisample* renderbufferStorageMultisample = nullptr; + GLRenderbufferStorageMultisampleAPPLE* renderbufferStorageMultisampleAPPLE = nullptr; + GLRenderbufferStorageMultisampleEXT* renderbufferStorageMultisampleEXT = nullptr; + GLResolveMultisampleFramebuffer* resolveMultisampleFramebuffer = nullptr; + GLBlitFramebuffer* blitFramebuffer = nullptr; + GLScissor* scissor = nullptr; + GLShaderSource* shaderSource = nullptr; + GLStencilFunc* stencilFunc = nullptr; + GLStencilFuncSeparate* stencilFuncSeparate = nullptr; + GLStencilMask* stencilMask = nullptr; + GLStencilMaskSeparate* stencilMaskSeparate = nullptr; + GLStencilOp* stencilOp = nullptr; + GLStencilOpSeparate* stencilOpSeparate = nullptr; + GLTexImage2D* texImage2D = nullptr; + GLTexParameterf* texParameterf = nullptr; + GLTexParameterfv* texParameterfv = nullptr; + GLTexParameteri* texParameteri = nullptr; + GLTexParameteriv* texParameteriv = nullptr; + GLTexSubImage2D* texSubImage2D = nullptr; + GLTextureBarrier* textureBarrier = nullptr; + GLUniform1f* uniform1f = nullptr; + GLUniform1i* uniform1i = nullptr; + GLUniform1fv* uniform1fv = nullptr; + GLUniform1iv* uniform1iv = nullptr; + GLUniform2f* uniform2f = nullptr; + GLUniform2i* uniform2i = nullptr; + GLUniform2fv* uniform2fv = nullptr; + GLUniform2iv* uniform2iv = nullptr; + GLUniform3f* uniform3f = nullptr; + GLUniform3i* uniform3i = nullptr; + GLUniform3fv* uniform3fv = nullptr; + GLUniform3iv* uniform3iv = nullptr; + GLUniform4f* uniform4f = nullptr; + GLUniform4i* uniform4i = nullptr; + GLUniform4fv* uniform4fv = nullptr; + GLUniform4iv* uniform4iv = nullptr; + GLUniformMatrix2fv* uniformMatrix2fv = nullptr; + GLUniformMatrix3fv* uniformMatrix3fv = nullptr; + GLUniformMatrix4fv* uniformMatrix4fv = nullptr; + GLUseProgram* useProgram = nullptr; + GLVertexAttrib1f* vertexAttrib1f = nullptr; + GLVertexAttrib2fv* vertexAttrib2fv = nullptr; + GLVertexAttrib3fv* vertexAttrib3fv = nullptr; + GLVertexAttrib4fv* vertexAttrib4fv = nullptr; + GLVertexAttribPointer* vertexAttribPointer = nullptr; + GLViewport* viewport = nullptr; + GLWaitSync* waitSync = nullptr; }; } // namespace tgfx \ No newline at end of file From cdd3b9994a44480327195c3eed3da4e98d8982dc Mon Sep 17 00:00:00 2001 From: domchen Date: Mon, 28 Feb 2022 06:45:30 +0000 Subject: [PATCH 6/6] Apply code format changes --- include/pag/pag.h | 1 - src/rendering/utils/GLRestorer.h | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/include/pag/pag.h b/include/pag/pag.h index 6a95b22416..d6ddf1abbe 100644 --- a/include/pag/pag.h +++ b/include/pag/pag.h @@ -1217,7 +1217,6 @@ class PAG_API PAGSurface { bool contextAdopted = false; GLRestorer* glRestorer = nullptr; - explicit PAGSurface(std::shared_ptr drawable, bool contextAdopted = false); bool draw(RenderCache* cache, std::shared_ptr graphic, BackendSemaphore* signalSemaphore, diff --git a/src/rendering/utils/GLRestorer.h b/src/rendering/utils/GLRestorer.h index 6de6dfb7f6..ebd94335eb 100644 --- a/src/rendering/utils/GLRestorer.h +++ b/src/rendering/utils/GLRestorer.h @@ -32,6 +32,6 @@ class GLRestorer { int blendSrcAlpha = 0; int blendDstAlpha = 0; }; -} +} // namespace pag -#endif //PAG_GLRESTORER_H +#endif //PAG_GLRESTORER_H