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:
Hermet Park 2021-03-19 16:30:45 +09:00 committed by Hermet Park
parent 53d23e9862
commit e8cf21a3c9
3 changed files with 23 additions and 38 deletions

View file

@ -28,8 +28,8 @@
/************************************************************************/
/* Internal Class Implementation */
/************************************************************************/
static bool initEngine = false;
static uint32_t rendererCnt = 0;
static int32_t initEngineCnt = false;
static int32_t rendererCnt = 0;
static void _termEngine()
@ -236,22 +236,19 @@ RenderData GlRenderer::prepare(const Shape& shape, RenderData data, const Render
int GlRenderer::init(uint32_t threads)
{
if (rendererCnt > 0) return false;
if (initEngine) return true;
if ((initEngineCnt++) > 0) return true;
//TODO:
initEngine = true;
return true;
}
int GlRenderer::term()
{
if (!initEngine) return true;
if ((--initEngineCnt) > 0) return true;
initEngine = false;
initEngineCnt = 0;
_termEngine();
@ -270,7 +267,8 @@ GlRenderer::~GlRenderer()
mRenderTasks.clear();
--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;
auto stopCnt = fill->colorStops(&stops);
if (stopCnt < 2)
{
return;
}
if (stopCnt < 2) return;
GlGradientRenderTask* rTask = nullptr;
GlSize size = sdata.geometry->getPrimitiveSize(primitiveIndex);
float* matrix = sdata.geometry->getTransforMatrix();
auto size = sdata.geometry->getPrimitiveSize(primitiveIndex);
auto matrix = sdata.geometry->getTransforMatrix();
switch (fill->id()) {
case FILL_ID_LINEAR: {
@ -344,16 +340,15 @@ void GlRenderer::drawPrimitive(GlShape& sdata, const Fill* fill, uint32_t primit
break;
}
}
if (rTask)
{
int32_t vertexLoc = rTask->getLocationPropertyId();
if (rTask) {
auto vertexLoc = rTask->getLocationPropertyId();
rTask->setPrimitveSize(size.x, size.y);
rTask->setCanvasSize(sdata.viewWd, sdata.viewHt);
rTask->setNoise(NOISE_LEVEL);
rTask->setStopCount((int)stopCnt);
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);
}

View file

@ -27,8 +27,8 @@
/************************************************************************/
/* Internal Class Implementation */
/************************************************************************/
static bool initEngine = false;
static uint32_t rendererCnt = 0;
static int32_t initEngineCnt = false;
static int32_t rendererCnt = 0;
struct SwTask : Task
@ -229,7 +229,8 @@ SwRenderer::~SwRenderer()
if (surface) delete(surface);
--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)
{
if (rendererCnt > 0) return false;
if (initEngine) return true;
if ((initEngineCnt++) > 0) return true;
if (!mpoolInit(threads)) return false;
initEngine = true;
return true;
}
bool SwRenderer::term()
{
if (!initEngine) return true;
if ((--initEngineCnt) > 0) return true;
initEngine = false;
initEngineCnt = 0;
_termEngine();

View file

@ -35,7 +35,7 @@
/************************************************************************/
/* Internal Class Implementation */
/************************************************************************/
static bool initialized = false;
/************************************************************************/
/* External Class Implementation */
@ -43,8 +43,6 @@ static bool initialized = false;
Result Initializer::init(CanvasEngine engine, uint32_t threads) noexcept
{
if (initialized) return Result::InsufficientCondition;
auto nonSupport = true;
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);
initialized = true;
return Result::Success;
}
Result Initializer::term(CanvasEngine engine) noexcept
{
if (!initialized) return Result::InsufficientCondition;
auto nonSupport = true;
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;
initialized = false;
return Result::Success;
}