Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exposes the OpenGL functions as Public APIs. #141

Merged
merged 6 commits into from
Feb 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion include/pag/pag.h
Original file line number Diff line number Diff line change
Expand Up @@ -1141,6 +1141,8 @@ class Drawable {

class Graphic;

class GLRestorer;

class PAG_API PAGSurface {
public:
/**
Expand Down Expand Up @@ -1212,8 +1214,10 @@ class PAG_API PAGSurface {
std::shared_ptr<Drawable> drawable = nullptr;
std::shared_ptr<tgfx::Device> device = nullptr;
std::shared_ptr<tgfx::Surface> surface = nullptr;
bool contextAdopted = false;
GLRestorer* glRestorer = nullptr;

explicit PAGSurface(std::shared_ptr<Drawable> drawable);
explicit PAGSurface(std::shared_ptr<Drawable> drawable, bool contextAdopted = false);

bool draw(RenderCache* cache, std::shared_ptr<Graphic> graphic, BackendSemaphore* signalSemaphore,
bool autoClear = true);
Expand Down
2 changes: 0 additions & 2 deletions src/platform/Platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
/////////////////////////////////////////////////////////////////////////////////////////////////

#include "Platform.h"
#include "core/Image.h"
#include "gpu/opengl/GLProcGetter.h"
#include "video/VideoDecoder.h"

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

Expand Down Expand Up @@ -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;
}
Expand Down
1 change: 0 additions & 1 deletion src/platform/web/VideoSequenceReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ std::shared_ptr<tgfx::Texture> 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);
}
Expand Down
57 changes: 34 additions & 23 deletions src/rendering/PAGSurface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -36,35 +36,39 @@ std::shared_ptr<PAGSurface> PAGSurface::MakeFrom(std::shared_ptr<Drawable> drawa
return std::shared_ptr<PAGSurface>(new PAGSurface(std::move(drawable)));
}

static std::shared_ptr<tgfx::Device> 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> PAGSurface::MakeFrom(const BackendRenderTarget& renderTarget,
ImageOrigin origin) {
auto device = tgfx::GLDevice::Current();
if (device == nullptr || !renderTarget.isValid()) {
return nullptr;
}
auto drawable = std::make_shared<RenderTargetDrawable>(device, renderTarget, ToTGFX(origin));
return MakeFrom(std::move(drawable));
if (drawable == nullptr) {
return nullptr;
}
return std::shared_ptr<PAGSurface>(new PAGSurface(std::move(drawable), true));
}

std::shared_ptr<PAGSurface> PAGSurface::MakeFrom(const BackendTexture& texture, ImageOrigin origin,
bool forAsyncThread) {
auto device = GetCurrentDevice(forAsyncThread);
std::shared_ptr<tgfx::Device> 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<TextureDrawable>(device, texture, ToTGFX(origin));
return MakeFrom(std::move(drawable));
if (drawable == nullptr) {
return nullptr;
}
return std::shared_ptr<PAGSurface>(new PAGSurface(std::move(drawable), isAdopted));
}

std::shared_ptr<PAGSurface> PAGSurface::MakeOffscreen(int width, int height) {
Expand All @@ -76,7 +80,8 @@ std::shared_ptr<PAGSurface> PAGSurface::MakeOffscreen(int width, int height) {
return std::shared_ptr<PAGSurface>(new PAGSurface(drawable));
}

PAGSurface::PAGSurface(std::shared_ptr<Drawable> drawable) : drawable(std::move(drawable)) {
PAGSurface::PAGSurface(std::shared_ptr<Drawable> drawable, bool contextAdopted)
: drawable(std::move(drawable)), contextAdopted(contextAdopted) {
rootLocker = std::make_shared<std::mutex>();
}

Expand All @@ -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;
}
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/rendering/caches/TextAtlas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
23 changes: 12 additions & 11 deletions src/rendering/filters/BulgeFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<tgfx::Point> BulgeFilter::computeVertices(const tgfx::Rect& inputBounds,
Expand Down
21 changes: 11 additions & 10 deletions src/rendering/filters/CornerPinFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<unsigned>(positionHandle), 2, GL_FLOAT, GL_FALSE,
5 * sizeof(float), static_cast<void*>(0));
gl->enableVertexAttribArray(static_cast<unsigned>(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<unsigned>(positionHandle), 2, GL_FLOAT, GL_FALSE,
5 * sizeof(float), static_cast<void*>(0));
gl->functions->enableVertexAttribArray(static_cast<unsigned>(positionHandle));

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);
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);
}
} // namespace pag
40 changes: 21 additions & 19 deletions src/rendering/filters/DisplacementMapFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -122,17 +123,18 @@ void DisplacementMapFilter::onUpdateParams(const tgfx::GLInterface* gl,
auto* pagEffect = reinterpret_cast<const DisplacementMapEffect*>(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
Loading