mirror of
https://github.com/thorvg/thorvg.git
synced 2025-06-08 13:43:43 +00:00
initializer: add reference counting for the engines initialization.
Introduced the reference counting for the backend engines so that tvg prevents unpaired engine initialization/termination calls by user mistake. @Issues: 296
This commit is contained in:
parent
53d23e9862
commit
e8cf21a3c9
3 changed files with 23 additions and 38 deletions
|
@ -28,8 +28,8 @@
|
||||||
/************************************************************************/
|
/************************************************************************/
|
||||||
/* Internal Class Implementation */
|
/* Internal Class Implementation */
|
||||||
/************************************************************************/
|
/************************************************************************/
|
||||||
static bool initEngine = false;
|
static int32_t initEngineCnt = false;
|
||||||
static uint32_t rendererCnt = 0;
|
static int32_t rendererCnt = 0;
|
||||||
|
|
||||||
|
|
||||||
static void _termEngine()
|
static void _termEngine()
|
||||||
|
@ -236,22 +236,19 @@ RenderData GlRenderer::prepare(const Shape& shape, RenderData data, const Render
|
||||||
|
|
||||||
int GlRenderer::init(uint32_t threads)
|
int GlRenderer::init(uint32_t threads)
|
||||||
{
|
{
|
||||||
if (rendererCnt > 0) return false;
|
if ((initEngineCnt++) > 0) return true;
|
||||||
if (initEngine) return true;
|
|
||||||
|
|
||||||
//TODO:
|
//TODO:
|
||||||
|
|
||||||
initEngine = true;
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int GlRenderer::term()
|
int GlRenderer::term()
|
||||||
{
|
{
|
||||||
if (!initEngine) return true;
|
if ((--initEngineCnt) > 0) return true;
|
||||||
|
|
||||||
initEngine = false;
|
initEngineCnt = 0;
|
||||||
|
|
||||||
_termEngine();
|
_termEngine();
|
||||||
|
|
||||||
|
@ -270,7 +267,8 @@ GlRenderer::~GlRenderer()
|
||||||
mRenderTasks.clear();
|
mRenderTasks.clear();
|
||||||
|
|
||||||
--rendererCnt;
|
--rendererCnt;
|
||||||
if (!initEngine) _termEngine();
|
|
||||||
|
if (rendererCnt == 0 && initEngineCnt == 0) _termEngine();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -308,13 +306,11 @@ void GlRenderer::drawPrimitive(GlShape& sdata, const Fill* fill, uint32_t primit
|
||||||
{
|
{
|
||||||
const Fill::ColorStop* stops = nullptr;
|
const Fill::ColorStop* stops = nullptr;
|
||||||
auto stopCnt = fill->colorStops(&stops);
|
auto stopCnt = fill->colorStops(&stops);
|
||||||
if (stopCnt < 2)
|
if (stopCnt < 2) return;
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
GlGradientRenderTask* rTask = nullptr;
|
GlGradientRenderTask* rTask = nullptr;
|
||||||
GlSize size = sdata.geometry->getPrimitiveSize(primitiveIndex);
|
auto size = sdata.geometry->getPrimitiveSize(primitiveIndex);
|
||||||
float* matrix = sdata.geometry->getTransforMatrix();
|
auto matrix = sdata.geometry->getTransforMatrix();
|
||||||
|
|
||||||
switch (fill->id()) {
|
switch (fill->id()) {
|
||||||
case FILL_ID_LINEAR: {
|
case FILL_ID_LINEAR: {
|
||||||
|
@ -344,16 +340,15 @@ void GlRenderer::drawPrimitive(GlShape& sdata, const Fill* fill, uint32_t primit
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (rTask)
|
if (rTask) {
|
||||||
{
|
auto vertexLoc = rTask->getLocationPropertyId();
|
||||||
int32_t vertexLoc = rTask->getLocationPropertyId();
|
|
||||||
rTask->setPrimitveSize(size.x, size.y);
|
rTask->setPrimitveSize(size.x, size.y);
|
||||||
rTask->setCanvasSize(sdata.viewWd, sdata.viewHt);
|
rTask->setCanvasSize(sdata.viewWd, sdata.viewHt);
|
||||||
rTask->setNoise(NOISE_LEVEL);
|
rTask->setNoise(NOISE_LEVEL);
|
||||||
rTask->setStopCount((int)stopCnt);
|
rTask->setStopCount((int)stopCnt);
|
||||||
rTask->setTransform(FORMAT_SIZE_MAT_4x4, matrix);
|
rTask->setTransform(FORMAT_SIZE_MAT_4x4, matrix);
|
||||||
for (uint32_t i = 0; i < stopCnt; ++i)
|
|
||||||
{
|
for (uint32_t i = 0; i < stopCnt; ++i) {
|
||||||
rTask->setStopColor(i, stops[i].offset, stops[i].r, stops[i].g, stops[i].b, stops[i].a);
|
rTask->setStopColor(i, stops[i].offset, stops[i].r, stops[i].g, stops[i].b, stops[i].a);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -27,8 +27,8 @@
|
||||||
/************************************************************************/
|
/************************************************************************/
|
||||||
/* Internal Class Implementation */
|
/* Internal Class Implementation */
|
||||||
/************************************************************************/
|
/************************************************************************/
|
||||||
static bool initEngine = false;
|
static int32_t initEngineCnt = false;
|
||||||
static uint32_t rendererCnt = 0;
|
static int32_t rendererCnt = 0;
|
||||||
|
|
||||||
|
|
||||||
struct SwTask : Task
|
struct SwTask : Task
|
||||||
|
@ -229,7 +229,8 @@ SwRenderer::~SwRenderer()
|
||||||
if (surface) delete(surface);
|
if (surface) delete(surface);
|
||||||
|
|
||||||
--rendererCnt;
|
--rendererCnt;
|
||||||
if (!initEngine) _termEngine();
|
|
||||||
|
if (rendererCnt == 0 && initEngineCnt == 0) _termEngine();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -546,22 +547,19 @@ RenderData SwRenderer::prepare(const Shape& sdata, RenderData data, const Render
|
||||||
|
|
||||||
bool SwRenderer::init(uint32_t threads)
|
bool SwRenderer::init(uint32_t threads)
|
||||||
{
|
{
|
||||||
if (rendererCnt > 0) return false;
|
if ((initEngineCnt++) > 0) return true;
|
||||||
if (initEngine) return true;
|
|
||||||
|
|
||||||
if (!mpoolInit(threads)) return false;
|
if (!mpoolInit(threads)) return false;
|
||||||
|
|
||||||
initEngine = true;
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool SwRenderer::term()
|
bool SwRenderer::term()
|
||||||
{
|
{
|
||||||
if (!initEngine) return true;
|
if ((--initEngineCnt) > 0) return true;
|
||||||
|
|
||||||
initEngine = false;
|
initEngineCnt = 0;
|
||||||
|
|
||||||
_termEngine();
|
_termEngine();
|
||||||
|
|
||||||
|
|
|
@ -35,7 +35,7 @@
|
||||||
/************************************************************************/
|
/************************************************************************/
|
||||||
/* Internal Class Implementation */
|
/* Internal Class Implementation */
|
||||||
/************************************************************************/
|
/************************************************************************/
|
||||||
static bool initialized = false;
|
|
||||||
|
|
||||||
/************************************************************************/
|
/************************************************************************/
|
||||||
/* External Class Implementation */
|
/* External Class Implementation */
|
||||||
|
@ -43,8 +43,6 @@ static bool initialized = false;
|
||||||
|
|
||||||
Result Initializer::init(CanvasEngine engine, uint32_t threads) noexcept
|
Result Initializer::init(CanvasEngine engine, uint32_t threads) noexcept
|
||||||
{
|
{
|
||||||
if (initialized) return Result::InsufficientCondition;
|
|
||||||
|
|
||||||
auto nonSupport = true;
|
auto nonSupport = true;
|
||||||
|
|
||||||
if (static_cast<uint32_t>(engine) & static_cast<uint32_t>(CanvasEngine::Sw)) {
|
if (static_cast<uint32_t>(engine) & static_cast<uint32_t>(CanvasEngine::Sw)) {
|
||||||
|
@ -67,16 +65,12 @@ Result Initializer::init(CanvasEngine engine, uint32_t threads) noexcept
|
||||||
|
|
||||||
TaskScheduler::init(threads);
|
TaskScheduler::init(threads);
|
||||||
|
|
||||||
initialized = true;
|
|
||||||
|
|
||||||
return Result::Success;
|
return Result::Success;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Result Initializer::term(CanvasEngine engine) noexcept
|
Result Initializer::term(CanvasEngine engine) noexcept
|
||||||
{
|
{
|
||||||
if (!initialized) return Result::InsufficientCondition;
|
|
||||||
|
|
||||||
auto nonSupport = true;
|
auto nonSupport = true;
|
||||||
|
|
||||||
if (static_cast<uint32_t>(engine) & static_cast<uint32_t>(CanvasEngine::Sw)) {
|
if (static_cast<uint32_t>(engine) & static_cast<uint32_t>(CanvasEngine::Sw)) {
|
||||||
|
@ -99,7 +93,5 @@ Result Initializer::term(CanvasEngine engine) noexcept
|
||||||
|
|
||||||
if (!LoaderMgr::term()) return Result::Unknown;
|
if (!LoaderMgr::term()) return Result::Unknown;
|
||||||
|
|
||||||
initialized = false;
|
|
||||||
|
|
||||||
return Result::Success;
|
return Result::Success;
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue