Skip to content

Commit

Permalink
Clean up the references to the internal classes of tgfx.
Browse files Browse the repository at this point in the history
  • Loading branch information
domchen committed Mar 3, 2022
1 parent 6dc9878 commit 94f0687
Show file tree
Hide file tree
Showing 17 changed files with 186 additions and 96 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ set(TGFX_USE_WEBP_ENCODE ${PAG_USE_WEBP_ENCODE})

set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)
add_subdirectory(tgfx/ EXCLUDE_FROM_ALL)
list(APPEND PAG_INCLUDES tgfx/include tgfx/src)
list(APPEND PAG_INCLUDES tgfx/include)

if (PAG_USE_LIBAVC)
add_definitions(-DPAG_USE_LIBAVC)
Expand Down Expand Up @@ -352,7 +352,7 @@ if (PAG_BUILD_TESTS)

file(GLOB FFAVC_LIB vendor/ffavc/${LIBRARY_ENTRY}/*${CMAKE_SHARED_LIBRARY_SUFFIX})
list(APPEND TEST_PLATFORM_LIBS ${FFAVC_LIB})
list(APPEND TEST_INCLUDES vendor/ffavc/include)
list(APPEND TEST_INCLUDES tgfx/src vendor/ffavc/include)

add_executable(PAGUnitTest ${Test_VENDOR_TARGET} ${PAG_TEST_FILES})
target_include_directories(PAGUnitTest PUBLIC ${TEST_INCLUDES})
Expand Down
2 changes: 1 addition & 1 deletion src/rendering/filters/DisplacementMapFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ void DisplacementMapFilter::updateMapTexture(RenderCache* cache, const Graphic*
void DisplacementMapFilter::onUpdateParams(tgfx::Context* context, const tgfx::Rect& contentBounds,
const tgfx::Point&) {
auto* pagEffect = reinterpret_cast<const DisplacementMapEffect*>(effect);
tgfx::GLContext::Unwrap(context)->bindTexture(1, mapSurface->getTexture()->getSampler());
ActiveGLTexture(context, 1, mapSurface->getTexture()->getSampler());
auto gl = tgfx::GLFunctions::Get(context);
gl->uniform2f(useForDisplacementHandle,
pagEffect->useForHorizontalDisplacement->getValueAt(layerFrame),
Expand Down
13 changes: 5 additions & 8 deletions src/rendering/filters/LayerFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,13 @@ std::shared_ptr<const FilterProgram> FilterProgram::Make(tgfx::Context* context,
const std::string& vertex,
const std::string& fragment) {
auto gl = tgfx::GLFunctions::Get(context);
auto caps = tgfx::GLCaps::Get(context);
auto program = tgfx::CreateGLProgram(context, vertex, fragment);
auto program = CreateGLProgram(context, vertex, fragment);
if (program == 0) {
return nullptr;
}
auto filterProgram = new FilterProgram();
filterProgram->program = program;
if (caps->vertexArrayObjectSupport) {
if (gl->bindVertexArray != nullptr) {
gl->genVertexArrays(1, &filterProgram->vertexArray);
}
gl->genBuffers(1, &filterProgram->vertexBuffer);
Expand Down Expand Up @@ -205,16 +204,14 @@ void LayerFilter::update(Frame frame, const tgfx::Rect& inputBounds, const tgfx:
}

static void EnableMultisample(tgfx::Context* context, bool usesMSAA) {
auto caps = tgfx::GLCaps::Get(context);
if (usesMSAA && caps->multisampleDisableSupport) {
if (usesMSAA && context->caps()->multisampleDisableSupport) {
auto gl = tgfx::GLFunctions::Get(context);
gl->enable(GL_MULTISAMPLE);
}
}

static void DisableMultisample(tgfx::Context* context, bool usesMSAA) {
auto caps = tgfx::GLCaps::Get(context);
if (usesMSAA && caps->multisampleDisableSupport) {
if (usesMSAA && context->caps()->multisampleDisableSupport) {
auto gl = tgfx::GLFunctions::Get(context);
gl->disable(GL_MULTISAMPLE);
}
Expand All @@ -237,7 +234,7 @@ void LayerFilter::draw(tgfx::Context* context, const FilterSource* source,
gl->blendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
gl->bindFramebuffer(GL_FRAMEBUFFER, target->frameBuffer.id);
gl->viewport(0, 0, target->width, target->height);
tgfx::GLContext::Unwrap(context)->bindTexture(0, &source->sampler);
ActiveGLTexture(context, 0, &source->sampler);
gl->uniformMatrix3fv(vertexMatrixHandle, 1, GL_FALSE, target->vertexMatrix.data());
gl->uniformMatrix3fv(textureMatrixHandle, 1, GL_FALSE, source->textureMatrix.data());
onUpdateParams(context, contentBounds, filterScale);
Expand Down
6 changes: 2 additions & 4 deletions src/rendering/filters/MotionBlurFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,8 @@ void MotionBlurFilter::onUpdateParams(tgfx::Context* context, const tgfx::Rect&

previousMatrix.preTranslate(contentBounds.left, contentBounds.top);
currentMatrix.preTranslate(contentBounds.left, contentBounds.top);
std::array<float, 9> previousGLMatrix =
tgfx::ToGLTextureMatrix(previousMatrix, width, height, origin);
std::array<float, 9> currentGLMatrix =
tgfx::ToGLTextureMatrix(currentMatrix, width, height, origin);
std::array<float, 9> previousGLMatrix = ToGLTextureMatrix(previousMatrix, width, height, origin);
std::array<float, 9> currentGLMatrix = ToGLTextureMatrix(currentMatrix, width, height, origin);

auto scaling = (previousMatrix.getScaleX() != currentMatrix.getScaleX() ||
previousMatrix.getScaleY() != currentMatrix.getScaleY());
Expand Down
2 changes: 1 addition & 1 deletion src/rendering/filters/glow/GlowMergeFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@ void GlowMergeFilter::onUpdateParams(tgfx::Context* context, const tgfx::Rect&,
// TODO(domrjchen): 下面这行之前写成了 gl->uniform1i(progressHandle, 1), 会导致 glError,
// 暂时注释掉。目前的发光效果跟 AE 也没有对齐,后面重写发光效果时时再修复。
// gl->uniform1i(blurTextureHandle, 1);
tgfx::GLContext::Unwrap(context)->bindTexture(1, &blurTexture);
ActiveGLTexture(context, 1, &blurTexture);
}
} // namespace pag
9 changes: 5 additions & 4 deletions src/rendering/filters/utils/FilterBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
/////////////////////////////////////////////////////////////////////////////////////////////////

#include "FilterBuffer.h"
#include "FilterHelper.h"

namespace pag {
std::shared_ptr<FilterBuffer> FilterBuffer::Make(tgfx::Context* context, int width, int height,
Expand Down Expand Up @@ -52,8 +53,8 @@ std::unique_ptr<FilterSource> FilterBuffer::toFilterSource(const tgfx::Point& sc
filterSource->height = surface->height();
filterSource->scale = scale;
// TODO(domrjchen): 这里的 ImageOrigin 是错的
filterSource->textureMatrix = tgfx::ToGLTextureMatrix(
tgfx::Matrix::I(), surface->width(), surface->height(), tgfx::ImageOrigin::BottomLeft);
filterSource->textureMatrix = ToGLTextureMatrix(tgfx::Matrix::I(), surface->width(),
surface->height(), tgfx::ImageOrigin::BottomLeft);
return std::unique_ptr<FilterSource>(filterSource);
}

Expand All @@ -63,8 +64,8 @@ std::unique_ptr<FilterTarget> FilterBuffer::toFilterTarget(
filterTarget->frameBuffer = getFramebuffer();
filterTarget->width = surface->width();
filterTarget->height = surface->height();
filterTarget->vertexMatrix = tgfx::ToGLVertexMatrix(
drawingMatrix, surface->width(), surface->height(), tgfx::ImageOrigin::BottomLeft);
filterTarget->vertexMatrix = ToGLVertexMatrix(drawingMatrix, surface->width(), surface->height(),
tgfx::ImageOrigin::BottomLeft);
return std::unique_ptr<FilterTarget>(filterTarget);
}
} // namespace pag
1 change: 0 additions & 1 deletion src/rendering/filters/utils/FilterBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

#include "gpu/Surface.h"
#include "gpu/opengl/GLRenderTarget.h"
#include "gpu/opengl/GLUtil.h"
#include "pag/file.h"
#include "pag/pag.h"
#include "rendering/filters/Filter.h"
Expand Down
134 changes: 131 additions & 3 deletions src/rendering/filters/utils/FilterHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,62 @@
/////////////////////////////////////////////////////////////////////////////////////////////////

#include "FilterHelper.h"
#include "gpu/opengl/GLSurface.h"
#include "base/utils/USE.h"
#include "gpu/Surface.h"
#include "gpu/opengl/GLRenderTarget.h"
#include "gpu/opengl/GLTexture.h"

namespace pag {
static std::array<float, 9> ToGLMatrix(const tgfx::Matrix& matrix) {
float values[9];
matrix.get9(values);
return {values[0], values[3], values[6], values[1], values[4],
values[7], values[2], values[5], values[8]};
}

std::array<float, 9> ToGLVertexMatrix(const tgfx::Matrix& matrix, int width, int height,
tgfx::ImageOrigin origin) {
auto w = static_cast<float>(width);
auto h = static_cast<float>(height);
auto result = matrix;
tgfx::Matrix convertMatrix = {};
// The following is equivalent:
// convertMatrix.setScale(1.0f, -1.0f);
// convertMatrix.postTranslate(1.0f, 1.0f);
// convertMatrix.postScale(width/2.0f, height/2f);
convertMatrix.setAll(w * 0.5f, 0.0f, w * 0.5f, 0.0f, h * -0.5f, h * 0.5f, 0.0f, 0.0f, 1.0f);
result.preConcat(convertMatrix);
if (convertMatrix.invert(&convertMatrix)) {
result.postConcat(convertMatrix);
}
if (origin == tgfx::ImageOrigin::TopLeft) {
result.postScale(1.0f, -1.0f);
}
return ToGLMatrix(result);
}

std::array<float, 9> ToGLTextureMatrix(const tgfx::Matrix& matrix, int width, int height,
tgfx::ImageOrigin origin) {
auto w = static_cast<float>(width);
auto h = static_cast<float>(height);
auto result = matrix;
tgfx::Matrix convertMatrix = {};
// The following is equivalent:
// convertMatrix.setScale(1.0f, -1.0f);
// convertMatrix.postTranslate(0.0f, 1.0f);
// convertMatrix.postScale(width, height);
convertMatrix.setAll(w, 0.0f, 0.0f, 0.0f, -h, h, 0.0f, 0.0f, 1.0f);
result.preConcat(convertMatrix);
if (convertMatrix.invert(&convertMatrix)) {
result.postConcat(convertMatrix);
}
if (origin == tgfx::ImageOrigin::TopLeft) {
result.postScale(1.0f, -1.0f);
result.postTranslate(0.0f, 1.0f);
}
return ToGLMatrix(result);
}

tgfx::Matrix ToMatrix(const FilterTarget* target, bool flipY) {
tgfx::Matrix matrix = {};
auto values = target->vertexMatrix;
Expand Down Expand Up @@ -90,7 +142,83 @@ tgfx::Point ToGLVertexPoint(const FilterTarget* target, const FilterSource* sour
void PreConcatMatrix(FilterTarget* target, const tgfx::Matrix& matrix) {
auto vertexMatrix = ToMatrix(target);
vertexMatrix.preConcat(matrix);
target->vertexMatrix = tgfx::ToGLVertexMatrix(vertexMatrix, target->width, target->height,
tgfx::ImageOrigin::BottomLeft);
target->vertexMatrix =
ToGLVertexMatrix(vertexMatrix, target->width, target->height, tgfx::ImageOrigin::BottomLeft);
}

static unsigned LoadGLShader(tgfx::Context* context, unsigned shaderType,
const std::string& source) {
auto gl = tgfx::GLFunctions::Get(context);
auto shader = gl->createShader(shaderType);
const char* files[] = {source.c_str()};
gl->shaderSource(shader, 1, files, nullptr);
gl->compileShader(shader);
int success;
gl->getShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success) {
char infoLog[512];
gl->getShaderInfoLog(shader, 512, nullptr, infoLog);
LOGE("Could not compile shader: %d %s", shaderType, infoLog);
gl->deleteShader(shader);
shader = 0;
}
return shader;
}

unsigned CreateGLProgram(tgfx::Context* context, const std::string& vertex,
const std::string& fragment) {
auto vertexShader = LoadGLShader(context, GL_VERTEX_SHADER, vertex);
if (vertexShader == 0) {
return 0;
}
auto fragmentShader = LoadGLShader(context, GL_FRAGMENT_SHADER, fragment);
if (fragmentShader == 0) {
return 0;
}
auto gl = tgfx::GLFunctions::Get(context);
auto programHandle = gl->createProgram();
gl->attachShader(programHandle, vertexShader);
gl->attachShader(programHandle, fragmentShader);
gl->linkProgram(programHandle);
int success;
gl->getProgramiv(programHandle, GL_LINK_STATUS, &success);
if (!success) {
char infoLog[512];
gl->getProgramInfoLog(programHandle, 512, nullptr, infoLog);
gl->deleteProgram(programHandle);
}
gl->deleteShader(vertexShader);
gl->deleteShader(fragmentShader);
return programHandle;
}

void ActiveGLTexture(tgfx::Context* context, int unitIndex, const tgfx::TextureSampler* sampler) {
if (sampler == nullptr) {
return;
}
auto glSampler = static_cast<const tgfx::GLSampler*>(sampler);
auto gl = tgfx::GLFunctions::Get(context);
gl->activeTexture(GL_TEXTURE0 + unitIndex);
gl->bindTexture(glSampler->target, glSampler->id);
gl->texParameteri(glSampler->target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
gl->texParameteri(glSampler->target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
gl->texParameteri(glSampler->target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
gl->texParameteri(glSampler->target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}

bool CheckGLError(tgfx::Context* context) {
#ifdef PAG_BUILD_FOR_WEB
USE(context);
return true;
#else
auto gl = tgfx::GLFunctions::Get(context);
bool success = true;
unsigned errorCode;
while ((errorCode = gl->getError()) != GL_NO_ERROR) {
success = false;
LOGE("glCheckError: %d", errorCode);
}
return success;
#endif
}
} // namespace pag
17 changes: 16 additions & 1 deletion src/rendering/filters/utils/FilterHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,21 @@

#pragma once

#include "base/utils/Log.h"
#include "base/utils/TGFXCast.h"
#include "gpu/Texture.h"
#include "gpu/opengl/GLUtil.h"
#include "gpu/opengl/GLFunctions.h"
#include "pag/file.h"
#include "pag/pag.h"
#include "rendering/filters/Filter.h"

namespace pag {
std::array<float, 9> ToGLVertexMatrix(const tgfx::Matrix& matrix, int width, int height,
tgfx::ImageOrigin origin);

std::array<float, 9> ToGLTextureMatrix(const tgfx::Matrix& matrix, int width, int height,
tgfx::ImageOrigin origin);

tgfx::Matrix ToMatrix(const FilterTarget* target, bool flipY = false);

std::unique_ptr<FilterSource> ToFilterSource(const tgfx::Texture* texture,
Expand All @@ -40,4 +47,12 @@ tgfx::Point ToGLVertexPoint(const FilterTarget* target, const FilterSource* sour
const tgfx::Rect& contentBounds, const tgfx::Point& contentPoint);

void PreConcatMatrix(FilterTarget* target, const tgfx::Matrix& matrix);

unsigned CreateGLProgram(tgfx::Context* context, const std::string& vertex,
const std::string& fragment);

void ActiveGLTexture(tgfx::Context* context, int unitIndex, const tgfx::TextureSampler* sampler);

bool CheckGLError(tgfx::Context* context);

} // namespace pag
1 change: 0 additions & 1 deletion src/rendering/renderers/FilterRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "FilterRenderer.h"
#include "base/utils/MatrixUtil.h"
#include "gpu/Surface.h"
#include "gpu/opengl/GLContext.h"
#include "rendering/caches/LayerCache.h"
#include "rendering/caches/RenderCache.h"
#include "rendering/filters/DisplacementMapFilter.h"
Expand Down
19 changes: 19 additions & 0 deletions test/framework/utils/PAGTestUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "PAGTestUtils.h"
#include "base/utils/TGFXCast.h"
#include "core/Image.h"
#include "gpu/opengl/GLFunctions.h"

namespace pag {
using namespace tgfx;
Expand Down Expand Up @@ -56,4 +57,22 @@ std::shared_ptr<PAGLayer> GetLayer(std::shared_ptr<PAGComposition> root, LayerTy
}
return nullptr;
}

bool CreateGLTexture(Context* context, int width, int height, GLSampler* texture) {
texture->target = GL_TEXTURE_2D;
texture->format = PixelFormat::RGBA_8888;
auto gl = GLFunctions::Get(context);
gl->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);
return true;
}
} // namespace pag
2 changes: 2 additions & 0 deletions test/framework/utils/PAGTestUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ std::shared_ptr<tgfx::PixelBuffer> MakeSnapshot(std::shared_ptr<PAGSurface> pagS

std::shared_ptr<PAGLayer> GetLayer(std::shared_ptr<PAGComposition> root, LayerType type,
int& targetIndex);

bool CreateGLTexture(tgfx::Context* context, int width, int height, tgfx::GLSampler* texture);
} // namespace pag
1 change: 1 addition & 0 deletions tgfx/src/gpu/Caps.h → tgfx/include/gpu/Caps.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ class Caps {
public:
bool floatIs32Bits = true;
int maxTextureSize = 0;
bool multisampleDisableSupport = false;
};
} // namespace tgfx
3 changes: 1 addition & 2 deletions tgfx/include/gpu/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "core/BytesKey.h"
#include "core/Color.h"
#include "gpu/Backend.h"
#include "gpu/Caps.h"
#include "gpu/Device.h"

namespace tgfx {
Expand All @@ -30,8 +31,6 @@ class GradientCache;

class ResourceCache;

class Caps;

class Context {
public:
virtual ~Context();
Expand Down
1 change: 0 additions & 1 deletion tgfx/src/gpu/opengl/GLCaps.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ class GLCaps : public Caps {
bool packRowLengthSupport = false;
bool unpackRowLengthSupport = false;
bool textureRedSupport = false;
bool multisampleDisableSupport = false;
MSFBOType msFBOType = MSFBOType::None;
bool frameBufferFetchSupport = false;
bool frameBufferFetchRequiresEnablePerSample = false;
Expand Down
Loading

0 comments on commit 94f0687

Please sign in to comment.