Skip to content

Commit

Permalink
Pass Context* instead of GLInterface* during drawing, Remove unnecess…
Browse files Browse the repository at this point in the history
…ary context parameters. (#143)
  • Loading branch information
domchen authored Mar 2, 2022
1 parent 8542f7e commit be3652e
Show file tree
Hide file tree
Showing 94 changed files with 925 additions and 904 deletions.
12 changes: 6 additions & 6 deletions src/platform/android/VideoSurface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ tgfx::Point OESTexture::getTextureCoord(float x, float y) const {
return {x / static_cast<float>(width()) * sx + tx, y / static_cast<float>(height()) * sy + ty};
}

void OESTexture::onRelease(tgfx::Context* context) {
void OESTexture::onReleaseGPU() {
if (sampler.id > 0) {
auto gl = tgfx::GLInterface::Get(context);
gl->functions->deleteTextures(1, &sampler.id);
auto gl = tgfx::GLFunctions::Get(context);
gl->deleteTextures(1, &sampler.id);
}
}

Expand Down Expand Up @@ -162,18 +162,18 @@ bool VideoSurface::attachToContext(JNIEnv* env, tgfx::Context* context) {
}
return true;
}
auto gl = tgfx::GLInterface::Get(context);
auto gl = tgfx::GLFunctions::Get(context);
tgfx::GLSampler sampler = {};
sampler.target = GL_TEXTURE_EXTERNAL_OES;
sampler.format = tgfx::PixelFormat::RGBA_8888;
gl->functions->genTextures(1, &sampler.id);
gl->genTextures(1, &sampler.id);
if (sampler.id == 0) {
return false;
}
auto result =
env->CallBooleanMethod(videoSurface.get(), VideoSurface_attachToGLContext, sampler.id);
if (!result) {
gl->functions->deleteTextures(1, &sampler.id);
gl->deleteTextures(1, &sampler.id);
LOGE("VideoSurface::attachToGLContext(): failed to attached to a Surface!");
return false;
}
Expand Down
5 changes: 2 additions & 3 deletions src/platform/android/VideoSurface.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,13 @@ class OESTexture : public tgfx::GLTexture {

tgfx::Point getTextureCoord(float x, float y) const override;

protected:
void onRelease(tgfx::Context* context) override;

private:
void setTextureSize(int width, int height);

void computeTransform();

void onReleaseGPU() override;

int textureWidth = 0;
int textureHeight = 0;
// 持有 Java 的 Surface,确保即使 GPUDecoder 提前释放也能正常被使用。
Expand Down
1 change: 0 additions & 1 deletion src/platform/qt/GPUDrawable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

#include "GPUDrawable.h"
#include <QQuickWindow>
#include "gpu/opengl/GLInterface.h"
#include "gpu/opengl/qt/QGLWindow.h"

namespace pag {
Expand Down
4 changes: 2 additions & 2 deletions src/rendering/Drawable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ std::shared_ptr<tgfx::Surface> RenderTargetDrawable::createSurface(tgfx::Context
}
auto glRT = tgfx::GLRenderTarget::MakeFrom(context, frameBuffer, renderTarget.width(),
renderTarget.height(), origin);
return tgfx::Surface::MakeFrom(context, std::move(glRT));
return tgfx::Surface::MakeFrom(std::move(glRT));
}

TextureDrawable::TextureDrawable(std::shared_ptr<tgfx::Device> device,
Expand All @@ -49,7 +49,7 @@ std::shared_ptr<tgfx::Surface> TextureDrawable::createSurface(tgfx::Context* con
}
auto glTexture =
tgfx::GLTexture::MakeFrom(context, sampler, texture.width(), texture.height(), origin);
return tgfx::Surface::MakeFrom(context, std::move(glTexture));
return tgfx::Surface::MakeFrom(std::move(glTexture));
}

OffscreenDrawable::OffscreenDrawable(int width, int height, std::shared_ptr<tgfx::Device> device)
Expand Down
1 change: 1 addition & 0 deletions src/rendering/PAGSurface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ tgfx::Context* PAGSurface::lockContext() {
auto context = device->lockContext();
if (context != nullptr && contextAdopted) {
glRestorer = new GLRestorer(tgfx::GLFunctions::Get(context));
context->resetState();
}
return context;
}
Expand Down
30 changes: 15 additions & 15 deletions src/rendering/filters/BulgeFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,30 +91,30 @@ std::string BulgeFilter::onBuildFragmentShader() {
return BULGE_FRAGMENT_SHADER;
}

void BulgeFilter::onPrepareProgram(const tgfx::GLInterface* gl, unsigned int program) {
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::onPrepareProgram(tgfx::Context* context, unsigned int program) {
auto gl = tgfx::GLFunctions::Get(context);
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");
}

void BulgeFilter::onUpdateParams(const tgfx::GLInterface* gl, const tgfx::Rect& contentBounds,
void BulgeFilter::onUpdateParams(tgfx::Context* context, const tgfx::Rect& contentBounds,
const tgfx::Point&) {
auto* bulgeEffect = reinterpret_cast<const BulgeEffect*>(effect);
auto horizontalRadius = bulgeEffect->horizontalRadius->getValueAt(layerFrame);
auto verticalRadius = bulgeEffect->verticalRadius->getValueAt(layerFrame);
auto bulgeCenter = bulgeEffect->bulgeCenter->getValueAt(layerFrame);
auto bulgeHeight = bulgeEffect->bulgeHeight->getValueAt(layerFrame);
auto pinning = bulgeEffect->pinning->getValueAt(layerFrame);

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);
auto gl = tgfx::GLFunctions::Get(context);
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);
}

std::vector<tgfx::Point> BulgeFilter::computeVertices(const tgfx::Rect& inputBounds,
Expand Down
4 changes: 2 additions & 2 deletions src/rendering/filters/BulgeFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ class BulgeFilter : public LayerFilter {

std::string onBuildFragmentShader() override;

void onPrepareProgram(const tgfx::GLInterface* gl, unsigned program) override;
void onPrepareProgram(tgfx::Context* context, unsigned program) override;

void onUpdateParams(const tgfx::GLInterface* gl, const tgfx::Rect& contentBounds,
void onUpdateParams(tgfx::Context* context, const tgfx::Rect& contentBounds,
const tgfx::Point& filterScale) override;

std::vector<tgfx::Point> computeVertices(const tgfx::Rect& contentBounds,
Expand Down
25 changes: 12 additions & 13 deletions src/rendering/filters/CornerPinFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ std::vector<tgfx::Point> CornerPinFilter::computeVertices(const tgfx::Rect& cont
return vertices;
}

void CornerPinFilter::bindVertices(const tgfx::GLInterface* gl, const FilterSource* source,
void CornerPinFilter::bindVertices(tgfx::Context* context, const FilterSource* source,
const FilterTarget* target,
const std::vector<tgfx::Point>& points) {
std::vector<float> vertices = {};
Expand All @@ -139,20 +139,19 @@ void CornerPinFilter::bindVertices(const tgfx::GLInterface* gl, const FilterSour
vertices.push_back(texturePoint.y * vertexQs[j]);
vertices.push_back(vertexQs[j]);
}

auto gl = tgfx::GLFunctions::Get(context);
if (filterProgram->vertexArray > 0) {
gl->functions->bindVertexArray(filterProgram->vertexArray);
gl->bindVertexArray(filterProgram->vertexArray);
}
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<unsigned>(positionHandle), 2, GL_FLOAT, GL_FALSE,
5 * sizeof(float), static_cast<void*>(0));
gl->functions->enableVertexAttribArray(static_cast<unsigned>(positionHandle));
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<unsigned>(positionHandle), 2, GL_FLOAT, GL_FALSE,
5 * sizeof(float), static_cast<void*>(0));
gl->enableVertexAttribArray(static_cast<unsigned>(positionHandle));

gl->functions->vertexAttribPointer(textureCoordHandle, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float),
reinterpret_cast<void*>(2 * sizeof(float)));
gl->functions->enableVertexAttribArray(textureCoordHandle);
gl->functions->bindBuffer(GL_ARRAY_BUFFER, 0);
gl->vertexAttribPointer(textureCoordHandle, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float),
reinterpret_cast<void*>(2 * sizeof(float)));
gl->enableVertexAttribArray(textureCoordHandle);
gl->bindBuffer(GL_ARRAY_BUFFER, 0);
}
} // namespace pag
4 changes: 2 additions & 2 deletions src/rendering/filters/CornerPinFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class CornerPinFilter : public LayerFilter {
const tgfx::Rect& transformedBounds,
const tgfx::Point& filterScale) override;

void bindVertices(const tgfx::GLInterface* gl, const FilterSource* source,
const FilterTarget* target, const std::vector<tgfx::Point>& points) override;
void bindVertices(tgfx::Context* context, const FilterSource* source, const FilterTarget* target,
const std::vector<tgfx::Point>& points) override;

bool needsMSAA() const override {
return true;
Expand Down
51 changes: 25 additions & 26 deletions src/rendering/filters/DisplacementMapFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ std::string DisplacementMapFilter::onBuildFragmentShader() {
return FRAGMENT_SHADER;
}

void DisplacementMapFilter::onPrepareProgram(const tgfx::GLInterface* gl, unsigned int program) {
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::onPrepareProgram(tgfx::Context* context, unsigned int program) {
auto gl = tgfx::GLFunctions::Get(context);
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");
}

void DisplacementMapFilter::updateMapTexture(RenderCache* cache, const Graphic* mapGraphic,
Expand All @@ -118,23 +118,22 @@ void DisplacementMapFilter::updateMapTexture(RenderCache* cache, const Graphic*
mapGraphic->draw(mapSurface->getCanvas(), cache);
}

void DisplacementMapFilter::onUpdateParams(const tgfx::GLInterface* gl,
const tgfx::Rect& contentBounds, const tgfx::Point&) {
void DisplacementMapFilter::onUpdateParams(tgfx::Context* context, const tgfx::Rect& contentBounds,
const tgfx::Point&) {
auto* pagEffect = reinterpret_cast<const DisplacementMapEffect*>(effect);
auto mapTextureID = GetTextureID(mapSurface->getTexture().get());
ActiveGLTexture(gl, GL_TEXTURE1, GL_TEXTURE_2D, mapTextureID);
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());
tgfx::GLContext::Unwrap(context)->bindTexture(1, mapSurface->getTexture()->getSampler());
auto gl = tgfx::GLFunctions::Get(context);
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());
}
} // namespace pag
4 changes: 2 additions & 2 deletions src/rendering/filters/DisplacementMapFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ class DisplacementMapFilter : public LayerFilter {
protected:
std::string onBuildFragmentShader() override;

void onPrepareProgram(const tgfx::GLInterface* gl, unsigned program) override;
void onPrepareProgram(tgfx::Context* context, unsigned program) override;

void onUpdateParams(const tgfx::GLInterface* gl, const tgfx::Rect& contentBounds,
void onUpdateParams(tgfx::Context* context, const tgfx::Rect& contentBounds,
const tgfx::Point& filterScale) override;

private:
Expand Down
11 changes: 6 additions & 5 deletions src/rendering/filters/Filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@

#include <array>
#include "core/Matrix.h"
#include "gpu/opengl/GLContext.h"
#include "gpu/opengl/GLFrameBuffer.h"
#include "gpu/opengl/GLSampler.h"

namespace pag {
struct FilterSource {
/**
* the id of source texture.
* The source texture sampler.
*/
unsigned textureID = 0;
tgfx::GLSampler sampler = {};

/**
* The width of source texture in pixels after textureMatrix and scale being applied.
Expand Down Expand Up @@ -59,9 +60,9 @@ struct FilterSource {

struct FilterTarget {
/**
* the id of target frame buffer.
* The target frame buffer.
*/
unsigned frameBufferID = 0;
tgfx::GLFrameBuffer frameBuffer = {};

/**
* The width of target frame buffer in pixels.
Expand Down
Loading

0 comments on commit be3652e

Please sign in to comment.