common engines: code refactoring

Introduce RenderRegion structure for region data
to simplify the methods paratemers.

also depends on the NRVO for the return data.
This commit is contained in:
Hermet Park 2021-03-23 14:18:17 +09:00 committed by GitHub
parent 234a9b2f93
commit e5381da223
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 56 additions and 60 deletions

View file

@ -73,9 +73,9 @@ bool GlRenderer::sync()
}
bool GlRenderer::region(TVG_UNUSED RenderData data, TVG_UNUSED uint32_t* x, TVG_UNUSED uint32_t* y, TVG_UNUSED uint32_t* w, TVG_UNUSED uint32_t* h)
RenderRegion GlRenderer::region(TVG_UNUSED RenderData data)
{
return true;
return {0, 0, 0, 0};
}
@ -102,7 +102,7 @@ bool GlRenderer::postRender()
}
Compositor* GlRenderer::target(TVG_UNUSED uint32_t x, TVG_UNUSED uint32_t y, TVG_UNUSED uint32_t w, TVG_UNUSED uint32_t h)
Compositor* GlRenderer::target(TVG_UNUSED const RenderRegion& region)
{
//TODO: Prepare frameBuffer & Setup render target for composition
return nullptr;

View file

@ -37,13 +37,13 @@ public:
bool renderImage(RenderData data) override;
bool postRender() override;
bool dispose(RenderData data) override;;
bool region(RenderData data, uint32_t* x, uint32_t* y, uint32_t* w, uint32_t* h) override;
RenderRegion region(RenderData data) override;
bool target(uint32_t* buffer, uint32_t stride, uint32_t w, uint32_t h);
bool sync() override;
bool clear() override;
Compositor* target(uint32_t x, uint32_t y, uint32_t w, uint32_t h) override;
Compositor* target(const RenderRegion& region) override;
bool beginComposite(Compositor* cmp, CompositeMethod method, uint32_t opacity) override;
bool endComposite(Compositor* cmp) override;

View file

@ -40,16 +40,18 @@ struct SwTask : Task
uint32_t opacity;
SwBBox bbox = {{0, 0}, {0, 0}}; //Whole Rendering Region
void bounds(uint32_t* x, uint32_t* y, uint32_t* w, uint32_t* h) const
RenderRegion bounds() const
{
//Range over?
auto xx = bbox.min.x > 0 ? bbox.min.x : 0;
auto yy = bbox.min.y > 0 ? bbox.min.y : 0;
RenderRegion region;
if (x) *x = xx;
if (y) *y = yy;
if (w) *w = bbox.max.x - xx;
if (h) *h = bbox.max.y - yy;
//Range over?
region.x = bbox.min.x > 0 ? bbox.min.x : 0;
region.y = bbox.min.y > 0 ? bbox.min.y : 0;
region.w = bbox.max.x - region.x;
region.h = bbox.max.y - region.y;
return region;
}
virtual bool dispose() = 0;
@ -313,10 +315,8 @@ bool SwRenderer::renderShape(RenderData data)
//Do Stroking Composition
if (task->cmpStroking) {
uint32_t x, y, w, h;
task->bounds(&x, &y, &w, &h);
opacity = 255;
cmp = target(x, y, w, h);
cmp = target(task->bounds());
beginComposite(cmp, CompositeMethod::None, task->opacity);
//No Stroking Composition
} else {
@ -348,11 +348,9 @@ bool SwRenderer::renderShape(RenderData data)
return true;
}
bool SwRenderer::region(RenderData data, uint32_t* x, uint32_t* y, uint32_t* w, uint32_t* h)
RenderRegion SwRenderer::region(RenderData data)
{
static_cast<SwTask*>(data)->bounds(x, y, w, h);
return true;
return static_cast<SwTask*>(data)->bounds();
}
@ -374,8 +372,13 @@ bool SwRenderer::beginComposite(Compositor* cmp, CompositeMethod method, uint32_
}
Compositor* SwRenderer::target(uint32_t x, uint32_t y, uint32_t w, uint32_t h)
Compositor* SwRenderer::target(const RenderRegion& region)
{
auto x = region.x;
auto y = region.y;
auto w = region.w;
auto h = region.h;
//Out of boundary
if (x > surface->w || y > surface->h) return nullptr;

View file

@ -41,13 +41,13 @@ public:
bool renderImage(RenderData data) override;
bool postRender() override;
bool dispose(RenderData data) override;
bool region(RenderData data, uint32_t* x, uint32_t* y, uint32_t* w, uint32_t* h) override;
RenderRegion region(RenderData data) override;
bool clear() override;
bool sync() override;
bool target(uint32_t* buffer, uint32_t stride, uint32_t w, uint32_t h, uint32_t cs);
Compositor* target(uint32_t x, uint32_t y, uint32_t w, uint32_t h) override;
Compositor* target(const RenderRegion& region) override;
bool beginComposite(Compositor* cmp, CompositeMethod method, uint32_t opacity) override;
bool endComposite(Compositor* cmp) override;

View file

@ -38,7 +38,7 @@ namespace tvg
virtual void* update(RenderMethod& renderer, const RenderTransform* transform, uint32_t opacity, Array<RenderData>& clips, RenderUpdateFlag pFlag) = 0; //Return engine data if it has.
virtual bool render(RenderMethod& renderer) = 0;
virtual bool bounds(float* x, float* y, float* w, float* h) const = 0;
virtual bool bounds(RenderMethod& renderer, uint32_t* x, uint32_t* y, uint32_t* w, uint32_t* h) const = 0;
virtual RenderRegion bounds(RenderMethod& renderer) const = 0;
virtual Paint* duplicate() = 0;
};
@ -129,9 +129,9 @@ namespace tvg
return smethod->bounds(x, y, w, h);
}
bool bounds(RenderMethod& renderer, uint32_t* x, uint32_t* y, uint32_t* w, uint32_t* h) const
RenderRegion bounds(RenderMethod& renderer) const
{
return smethod->bounds(renderer, x, y, w, h);
return smethod->bounds(renderer);
}
bool dispose(RenderMethod& renderer)
@ -182,9 +182,9 @@ namespace tvg
/* Note: only ClipPath is processed in update() step.
Create a composition image. */
if (cmpTarget && cmpMethod != CompositeMethod::ClipPath) {
uint32_t x, y, w, h;
if (!cmpTarget->pImpl->bounds(renderer, &x, &y, &w, &h)) return false;
cmp = renderer.target(x, y, w, h);
auto region = cmpTarget->pImpl->bounds(renderer);
if (region.w == 0 || region.h == 0) return false;
cmp = renderer.target(region);
renderer.beginComposite(cmp, CompositeMethod::None, 255);
cmpTarget->pImpl->render(renderer);
}
@ -244,9 +244,9 @@ namespace tvg
return inst->bounds(x, y, w, h);
}
bool bounds(RenderMethod& renderer, uint32_t* x, uint32_t* y, uint32_t* w, uint32_t* h) const override
RenderRegion bounds(RenderMethod& renderer) const override
{
return inst->bounds(renderer, x, y, w, h);
return inst->bounds(renderer);
}
bool dispose(RenderMethod& renderer) override

View file

@ -155,11 +155,11 @@ struct Picture::Impl
return paint->pImpl->bounds(x, y, w, h);
}
bool bounds(RenderMethod& renderer, uint32_t* x, uint32_t* y, uint32_t* w, uint32_t* h)
RenderRegion bounds(RenderMethod& renderer)
{
if (rdata) return renderer.region(rdata, x, y, w, h);
if (paint) return paint->pImpl->bounds(renderer, x, y, w, h);
return false;
if (rdata) return renderer.region(rdata);
if (paint) return paint->pImpl->bounds(renderer);
return {0, 0, 0, 0};
}
Result load(const string& path)

View file

@ -28,6 +28,8 @@
namespace tvg
{
enum RenderUpdateFlag {None = 0, Path = 1, Color = 2, Gradient = 4, Stroke = 8, Transform = 16, Image = 32, GradientStroke = 64, All = 127};
struct Surface
{
//TODO: Union for multiple types
@ -44,7 +46,9 @@ struct Compositor {
uint32_t opacity;
};
enum RenderUpdateFlag {None = 0, Path = 1, Color = 2, Gradient = 4, Stroke = 8, Transform = 16, Image = 32, GradientStroke = 64, All = 127};
struct RenderRegion {
uint32_t x, y, w, h;
};
struct RenderTransform
{
@ -73,12 +77,12 @@ public:
virtual bool renderImage(RenderData data) = 0;
virtual bool postRender() = 0;
virtual bool dispose(RenderData data) = 0;
virtual bool region(RenderData data, uint32_t* x, uint32_t* y, uint32_t* w, uint32_t* h) = 0;
virtual RenderRegion region(RenderData data) = 0;
virtual bool clear() = 0;
virtual bool sync() = 0;
virtual Compositor* target(uint32_t x, uint32_t y, uint32_t w, uint32_t h) = 0;
virtual Compositor* target(const RenderRegion& region) = 0;
virtual bool beginComposite(Compositor* cmp, CompositeMethod method, uint32_t opacity) = 0;
virtual bool endComposite(Compositor* cmp) = 0;
};

View file

@ -76,9 +76,7 @@ struct Scene::Impl
Compositor* cmp = nullptr;
if (needComposition(opacity)) {
uint32_t x, y, w, h;
if (!bounds(renderer, &x, &y, &w, &h)) return false;
cmp = renderer.target(x, y, w, h);
cmp = renderer.target(bounds(renderer));
renderer.beginComposite(cmp, CompositeMethod::None, opacity);
}
@ -91,9 +89,9 @@ struct Scene::Impl
return true;
}
bool bounds(RenderMethod& renderer, uint32_t* px, uint32_t* py, uint32_t* pw, uint32_t* ph) const
RenderRegion bounds(RenderMethod& renderer) const
{
if (paints.count == 0) return false;
if (paints.count == 0) return {0, 0, 0, 0};
uint32_t x1 = UINT32_MAX;
uint32_t y1 = UINT32_MAX;
@ -101,26 +99,17 @@ struct Scene::Impl
uint32_t y2 = 0;
for (auto paint = paints.data; paint < (paints.data + paints.count); ++paint) {
uint32_t x = UINT32_MAX;
uint32_t y = UINT32_MAX;
uint32_t w = 0;
uint32_t h = 0;
if (!(*paint)->pImpl->bounds(renderer, &x, &y, &w, &h)) continue;
auto region = (*paint)->pImpl->bounds(renderer);
//Merge regions
if (x < x1) x1 = x;
if (x2 < x + w) x2 = (x + w);
if (y < y1) y1 = y;
if (y2 < y + h) y2 = (y + h);
if (region.x < x1) x1 = region.x;
if (x2 < region.x + region.w) x2 = (region.x + region.w);
if (region.y < y1) y1 = region.y;
if (y2 < region.y + region.h) y2 = (region.y + region.h);
}
if (px) *px = x1;
if (py) *py = y1;
if (pw) *pw = (x2 - x1);
if (ph) *ph = (y2 - y1);
return true;
return {x1, y1, (x2 - x1), (y2 - y1)};
}
bool bounds(float* px, float* py, float* pw, float* ph) const

View file

@ -236,9 +236,9 @@ struct Shape::Impl
return this->rdata;
}
bool bounds(RenderMethod& renderer, uint32_t* x, uint32_t* y, uint32_t* w, uint32_t* h)
RenderRegion bounds(RenderMethod& renderer)
{
return renderer.region(rdata, x, y, w, h);
return renderer.region(rdata);
}
bool bounds(float* x, float* y, float* w, float* h)