mirror of
https://github.com/thorvg/thorvg.git
synced 2025-06-09 06:04:03 +00:00
common paint: code refactoring
Grouping the composite data to add source paint necessarily. this refactoring is a prerequisite job for the texmap anti-aliasing.
This commit is contained in:
parent
d3f3a50309
commit
62c9feb80a
5 changed files with 60 additions and 36 deletions
|
@ -95,7 +95,6 @@ static bool _compFastTrack(Paint* cmpTarget, const RenderTransform* pTransform,
|
||||||
Paint* Paint::Impl::duplicate()
|
Paint* Paint::Impl::duplicate()
|
||||||
{
|
{
|
||||||
auto ret = smethod->duplicate();
|
auto ret = smethod->duplicate();
|
||||||
if (!ret) return nullptr;
|
|
||||||
|
|
||||||
//duplicate Transform
|
//duplicate Transform
|
||||||
if (rTransform) {
|
if (rTransform) {
|
||||||
|
@ -106,9 +105,7 @@ Paint* Paint::Impl::duplicate()
|
||||||
|
|
||||||
ret->pImpl->opacity = opacity;
|
ret->pImpl->opacity = opacity;
|
||||||
|
|
||||||
if (cmpTarget) ret->pImpl->cmpTarget = cmpTarget->duplicate();
|
if (compData) ret->pImpl->composite(ret, compData->target->duplicate(), compData->method);
|
||||||
|
|
||||||
ret->pImpl->cmpMethod = cmpMethod;
|
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
@ -168,15 +165,15 @@ bool Paint::Impl::render(RenderMethod& renderer)
|
||||||
|
|
||||||
/* Note: only ClipPath is processed in update() step.
|
/* Note: only ClipPath is processed in update() step.
|
||||||
Create a composition image. */
|
Create a composition image. */
|
||||||
if (cmpTarget && cmpMethod != CompositeMethod::ClipPath && !(cmpTarget->pImpl->ctxFlag & ContextFlag::FastTrack)) {
|
if (compData && compData->method != CompositeMethod::ClipPath && !(compData->target->pImpl->ctxFlag & ContextFlag::FastTrack)) {
|
||||||
auto region = smethod->bounds(renderer);
|
auto region = smethod->bounds(renderer);
|
||||||
if (region.w == 0 || region.h == 0) return true;
|
if (region.w == 0 || region.h == 0) return true;
|
||||||
cmp = renderer.target(region);
|
cmp = renderer.target(region);
|
||||||
renderer.beginComposite(cmp, CompositeMethod::None, 255);
|
renderer.beginComposite(cmp, CompositeMethod::None, 255);
|
||||||
cmpTarget->pImpl->render(renderer);
|
compData->target->pImpl->render(renderer);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cmp) renderer.beginComposite(cmp, cmpMethod, cmpTarget->pImpl->opacity);
|
if (cmp) renderer.beginComposite(cmp, compData->method, compData->target->pImpl->opacity);
|
||||||
|
|
||||||
auto ret = smethod->render(renderer);
|
auto ret = smethod->render(renderer);
|
||||||
|
|
||||||
|
@ -197,35 +194,37 @@ void* Paint::Impl::update(RenderMethod& renderer, const RenderTransform* pTransf
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 1. Composition Pre Processing */
|
/* 1. Composition Pre Processing */
|
||||||
void *cmpData = nullptr;
|
void *tdata = nullptr;
|
||||||
RenderRegion viewport;
|
RenderRegion viewport;
|
||||||
bool cmpFastTrack = false;
|
bool compFastTrack = false;
|
||||||
|
|
||||||
if (cmpTarget) {
|
if (compData) {
|
||||||
cmpTarget->pImpl->ctxFlag = ContextFlag::Invalid; //reset
|
auto target = compData->target;
|
||||||
|
auto method = compData->method;
|
||||||
|
target->pImpl->ctxFlag = ContextFlag::Invalid; //reset
|
||||||
|
|
||||||
/* If transform has no rotation factors && ClipPath / AlphaMasking is a simple rectangle,
|
/* If transform has no rotation factors && ClipPath / AlphaMasking is a simple rectangle,
|
||||||
we can avoid regular ClipPath / AlphaMasking sequence but use viewport for performance */
|
we can avoid regular ClipPath / AlphaMasking sequence but use viewport for performance */
|
||||||
auto tryFastTrack = false;
|
auto tryFastTrack = false;
|
||||||
if (cmpMethod == CompositeMethod::ClipPath) tryFastTrack = true;
|
if (method == CompositeMethod::ClipPath) tryFastTrack = true;
|
||||||
else if (cmpMethod == CompositeMethod::AlphaMask && cmpTarget->identifier() == TVG_CLASS_ID_SHAPE) {
|
else if (method == CompositeMethod::AlphaMask && target->identifier() == TVG_CLASS_ID_SHAPE) {
|
||||||
auto shape = static_cast<Shape*>(cmpTarget);
|
auto shape = static_cast<Shape*>(target);
|
||||||
uint8_t a;
|
uint8_t a;
|
||||||
shape->fillColor(nullptr, nullptr, nullptr, &a);
|
shape->fillColor(nullptr, nullptr, nullptr, &a);
|
||||||
if (a == 255 && shape->opacity() == 255 && !shape->fill()) tryFastTrack = true;
|
if (a == 255 && shape->opacity() == 255 && !shape->fill()) tryFastTrack = true;
|
||||||
}
|
}
|
||||||
if (tryFastTrack) {
|
if (tryFastTrack) {
|
||||||
RenderRegion viewport2;
|
RenderRegion viewport2;
|
||||||
if ((cmpFastTrack = _compFastTrack(cmpTarget, pTransform, cmpTarget->pImpl->rTransform, viewport2))) {
|
if ((compFastTrack = _compFastTrack(target, pTransform, target->pImpl->rTransform, viewport2))) {
|
||||||
viewport = renderer.viewport();
|
viewport = renderer.viewport();
|
||||||
viewport2.intersect(viewport);
|
viewport2.intersect(viewport);
|
||||||
renderer.viewport(viewport2);
|
renderer.viewport(viewport2);
|
||||||
cmpTarget->pImpl->ctxFlag |= ContextFlag::FastTrack;
|
target->pImpl->ctxFlag |= ContextFlag::FastTrack;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!cmpFastTrack) {
|
if (!compFastTrack) {
|
||||||
cmpData = cmpTarget->pImpl->update(renderer, pTransform, 255, clips, pFlag);
|
tdata = target->pImpl->update(renderer, pTransform, 255, clips, pFlag);
|
||||||
if (cmpMethod == CompositeMethod::ClipPath) clips.push(cmpData);
|
if (method == CompositeMethod::ClipPath) clips.push(tdata);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -244,8 +243,8 @@ void* Paint::Impl::update(RenderMethod& renderer, const RenderTransform* pTransf
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 3. Composition Post Processing */
|
/* 3. Composition Post Processing */
|
||||||
if (cmpFastTrack) renderer.viewport(viewport);
|
if (compFastTrack) renderer.viewport(viewport);
|
||||||
else if (cmpData && cmpMethod == CompositeMethod::ClipPath) clips.pop();
|
else if (tdata && compData->method == CompositeMethod::ClipPath) clips.pop();
|
||||||
|
|
||||||
return edata;
|
return edata;
|
||||||
}
|
}
|
||||||
|
@ -366,7 +365,7 @@ Paint* Paint::duplicate() const noexcept
|
||||||
Result Paint::composite(std::unique_ptr<Paint> target, CompositeMethod method) noexcept
|
Result Paint::composite(std::unique_ptr<Paint> target, CompositeMethod method) noexcept
|
||||||
{
|
{
|
||||||
auto p = target.release();
|
auto p = target.release();
|
||||||
if (pImpl->composite(p, method)) return Result::Success;
|
if (pImpl->composite(this, p, method)) return Result::Success;
|
||||||
if (p) delete(p);
|
if (p) delete(p);
|
||||||
return Result::InvalidArguments;
|
return Result::InvalidArguments;
|
||||||
}
|
}
|
||||||
|
@ -374,9 +373,13 @@ Result Paint::composite(std::unique_ptr<Paint> target, CompositeMethod method) n
|
||||||
|
|
||||||
CompositeMethod Paint::composite(const Paint** target) const noexcept
|
CompositeMethod Paint::composite(const Paint** target) const noexcept
|
||||||
{
|
{
|
||||||
if (target) *target = pImpl->cmpTarget;
|
if (pImpl->compData) {
|
||||||
|
if (target) *target = pImpl->compData->target;
|
||||||
return pImpl->cmpMethod;
|
return pImpl->compData->method;
|
||||||
|
} else {
|
||||||
|
if (target) *target = nullptr;
|
||||||
|
return CompositeMethod::None;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -50,19 +50,28 @@ namespace tvg
|
||||||
virtual Iterator* iterator() = 0;
|
virtual Iterator* iterator() = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct Composite
|
||||||
|
{
|
||||||
|
Paint* target;
|
||||||
|
Paint* source;
|
||||||
|
CompositeMethod method;
|
||||||
|
};
|
||||||
|
|
||||||
struct Paint::Impl
|
struct Paint::Impl
|
||||||
{
|
{
|
||||||
StrategyMethod* smethod = nullptr;
|
StrategyMethod* smethod = nullptr;
|
||||||
RenderTransform* rTransform = nullptr;
|
RenderTransform* rTransform = nullptr;
|
||||||
|
Composite* compData = nullptr;
|
||||||
uint32_t renderFlag = RenderUpdateFlag::None;
|
uint32_t renderFlag = RenderUpdateFlag::None;
|
||||||
Paint* cmpTarget = nullptr;
|
|
||||||
CompositeMethod cmpMethod = CompositeMethod::None;
|
|
||||||
uint32_t ctxFlag = ContextFlag::Invalid;
|
uint32_t ctxFlag = ContextFlag::Invalid;
|
||||||
uint32_t id;
|
uint32_t id;
|
||||||
uint8_t opacity = 255;
|
uint8_t opacity = 255;
|
||||||
|
|
||||||
~Impl() {
|
~Impl() {
|
||||||
if (cmpTarget) delete(cmpTarget);
|
if (compData) {
|
||||||
|
delete(compData->target);
|
||||||
|
free(compData);
|
||||||
|
}
|
||||||
if (smethod) delete(smethod);
|
if (smethod) delete(smethod);
|
||||||
if (rTransform) delete(rTransform);
|
if (rTransform) delete(rTransform);
|
||||||
}
|
}
|
||||||
|
@ -100,7 +109,7 @@ namespace tvg
|
||||||
|
|
||||||
bool dispose(RenderMethod& renderer)
|
bool dispose(RenderMethod& renderer)
|
||||||
{
|
{
|
||||||
if (cmpTarget) cmpTarget->pImpl->dispose(renderer);
|
if (compData) compData->target->pImpl->dispose(renderer);
|
||||||
return smethod->dispose(renderer);
|
return smethod->dispose(renderer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -109,12 +118,26 @@ namespace tvg
|
||||||
return smethod->iterator();
|
return smethod->iterator();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool composite(Paint* target, CompositeMethod method)
|
bool composite(Paint* source, Paint* target, CompositeMethod method)
|
||||||
{
|
{
|
||||||
|
//Invalid case
|
||||||
if ((!target && method != CompositeMethod::None) || (target && method == CompositeMethod::None)) return false;
|
if ((!target && method != CompositeMethod::None) || (target && method == CompositeMethod::None)) return false;
|
||||||
if (cmpTarget) delete(cmpTarget);
|
|
||||||
cmpTarget = target;
|
if (compData) {
|
||||||
cmpMethod = method;
|
delete(compData->target);
|
||||||
|
//Reset scenario
|
||||||
|
if (!target && method == CompositeMethod::None) {
|
||||||
|
free(compData);
|
||||||
|
compData = nullptr;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (!target && method == CompositeMethod::None) return true;
|
||||||
|
compData = static_cast<Composite*>(calloc(1, sizeof(Composite)));
|
||||||
|
}
|
||||||
|
compData->target = target;
|
||||||
|
compData->source = source;
|
||||||
|
compData->method = method;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -225,7 +225,6 @@ struct Picture::Impl
|
||||||
reload();
|
reload();
|
||||||
|
|
||||||
auto ret = Picture::gen();
|
auto ret = Picture::gen();
|
||||||
if (!ret) return nullptr;
|
|
||||||
|
|
||||||
auto dup = ret.get()->pImpl;
|
auto dup = ret.get()->pImpl;
|
||||||
if (paint) dup->paint = paint->duplicate();
|
if (paint) dup->paint = paint->duplicate();
|
||||||
|
|
|
@ -184,7 +184,7 @@ struct Scene::Impl
|
||||||
Paint* duplicate()
|
Paint* duplicate()
|
||||||
{
|
{
|
||||||
auto ret = Scene::gen();
|
auto ret = Scene::gen();
|
||||||
if (!ret) return nullptr;
|
|
||||||
auto dup = ret.get()->pImpl;
|
auto dup = ret.get()->pImpl;
|
||||||
|
|
||||||
dup->paints.reserve(paints.count);
|
dup->paints.reserve(paints.count);
|
||||||
|
|
|
@ -352,7 +352,6 @@ struct Shape::Impl
|
||||||
Paint* duplicate()
|
Paint* duplicate()
|
||||||
{
|
{
|
||||||
auto ret = Shape::gen();
|
auto ret = Shape::gen();
|
||||||
if (!ret) return nullptr;
|
|
||||||
|
|
||||||
auto dup = ret.get()->pImpl;
|
auto dup = ret.get()->pImpl;
|
||||||
dup->rule = rule;
|
dup->rule = rule;
|
||||||
|
|
Loading…
Add table
Reference in a new issue