common transform: support matrix transform interface.

this interface is designed for user-defined arbitrary affine-transform paints.

required by svg loader.

Change-Id: I7f08023605d224e36cef3770098d3757aee81848
This commit is contained in:
Hermet Park 2020-06-22 20:53:52 +09:00
parent f56a3b791c
commit c6742e9590
12 changed files with 317 additions and 110 deletions

1
.gitignore vendored
View file

@ -11,6 +11,7 @@ testUpdate
testDirectUpdate testDirectUpdate
testScene testScene
testTransform testTransform
testCustomTransform
testSceneTransform testSceneTransform
testStroke testStroke
testStrokeLine testStrokeLine

View file

@ -78,6 +78,14 @@ struct Point
}; };
struct Matrix
{
float e11, e12, e13;
float e21, e22, e23;
float e31, e32, e33;
};
/** /**
* @class Paint * @class Paint
* *
@ -91,12 +99,6 @@ class TVG_EXPORT Paint
public: public:
virtual ~Paint() {} virtual ~Paint() {}
virtual Result rotate(float degree) = 0;
virtual Result scale(float factor) = 0;
virtual Result translate(float x, float y) = 0;
virtual Result bounds(float* x, float* y, float* w, float* h) const = 0;
_TVG_DECALRE_IDENTIFIER(); _TVG_DECALRE_IDENTIFIER();
}; };
@ -242,16 +244,17 @@ public:
Result fill(std::unique_ptr<Fill> f) noexcept; Result fill(std::unique_ptr<Fill> f) noexcept;
//Transform //Transform
Result rotate(float degree) noexcept override; Result rotate(float degree) noexcept;
Result scale(float factor) noexcept override; Result scale(float factor) noexcept;
Result translate(float x, float y) noexcept override; Result translate(float x, float y) noexcept;
Result transform(const Matrix& m) noexcept;
//Getters //Getters
uint32_t pathCommands(const PathCommand** cmds) const noexcept; uint32_t pathCommands(const PathCommand** cmds) const noexcept;
uint32_t pathCoords(const Point** pts) const noexcept; uint32_t pathCoords(const Point** pts) const noexcept;
Result fill(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept; Result fill(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept;
const Fill* fill() const noexcept; const Fill* fill() const noexcept;
Result bounds(float* x, float* y, float* w, float* h) const noexcept override; Result bounds(float* x, float* y, float* w, float* h) const noexcept;
float strokeWidth() const noexcept; float strokeWidth() const noexcept;
Result strokeColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept; Result strokeColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept;
@ -282,14 +285,14 @@ public:
Result push(std::unique_ptr<Paint> paint) noexcept; Result push(std::unique_ptr<Paint> paint) noexcept;
Result reserve(uint32_t size) noexcept; Result reserve(uint32_t size) noexcept;
Result load(const std::string& path) noexcept; Result load(const std::string& path) noexcept;
Result rotate(float degree) noexcept override; Result rotate(float degree) noexcept;
Result scale(float factor) noexcept override; Result scale(float factor) noexcept;
Result translate(float x, float y) noexcept override; Result translate(float x, float y) noexcept;
Result transform(const Matrix& m) noexcept;
Result bounds(float* x, float* y, float* w, float* h) const noexcept override; Result bounds(float* x, float* y, float* w, float* h) const noexcept;
static std::unique_ptr<Scene> gen() noexcept; static std::unique_ptr<Scene> gen() noexcept;

View file

@ -100,12 +100,12 @@ bool _prepareLinear(SwFill* fill, const LinearGradient* linear, const RenderTran
auto cy = (y2 - y1) * 0.5f + y1; auto cy = (y2 - y1) * 0.5f + y1;
auto dx = x1 - cx; auto dx = x1 - cx;
auto dy = y1 - cy; auto dy = y1 - cy;
x1 = dx * transform->e11 + dy * transform->e12 + transform->e31; x1 = dx * transform->m.e11 + dy * transform->m.e12 + transform->m.e31;
y1 = dx * transform->e21 + dy * transform->e22 + transform->e32; y1 = dx * transform->m.e21 + dy * transform->m.e22 + transform->m.e32;
dx = x2 - cx; dx = x2 - cx;
dy = y2 - cy; dy = y2 - cy;
x2 = dx * transform->e11 + dy * transform->e12 + transform->e31; x2 = dx * transform->m.e11 + dy * transform->m.e12 + transform->m.e31;
y2 = dx * transform->e21 + dy * transform->e22 + transform->e32; y2 = dx * transform->m.e21 + dy * transform->m.e22 + transform->m.e32;
} }
fill->linear.dx = x2 - x1; fill->linear.dx = x2 - x1;
@ -131,11 +131,11 @@ bool _prepareRadial(SwFill* fill, const RadialGradient* radial, const RenderTran
if (radius < FLT_EPSILON) return true; if (radius < FLT_EPSILON) return true;
if (transform) { if (transform) {
auto tx = fill->radial.cx * transform->e11 + fill->radial.cy * transform->e12 + transform->e31; auto tx = fill->radial.cx * transform->m.e11 + fill->radial.cy * transform->m.e12 + transform->m.e31;
auto ty = fill->radial.cx * transform->e21 + fill->radial.cy * transform->e22 + transform->e32; auto ty = fill->radial.cx * transform->m.e21 + fill->radial.cy * transform->m.e22 + transform->m.e32;
fill->radial.cx = tx; fill->radial.cx = tx;
fill->radial.cy = ty; fill->radial.cy = ty;
radius *= transform->e33; radius *= transform->m.e33;
} }
fill->radial.a = radius * radius; fill->radial.a = radius * radius;

View file

@ -347,8 +347,8 @@ static void _transformOutline(SwOutline* outline, const RenderTransform* transfo
for(uint32_t i = 0; i < outline->ptsCnt; ++i) { for(uint32_t i = 0; i < outline->ptsCnt; ++i) {
auto dx = static_cast<float>(outline->pts[i].x >> 6); auto dx = static_cast<float>(outline->pts[i].x >> 6);
auto dy = static_cast<float>(outline->pts[i].y >> 6); auto dy = static_cast<float>(outline->pts[i].y >> 6);
auto tx = dx * transform->e11 + dy * transform->e12 + transform->e31; auto tx = dx * transform->m.e11 + dy * transform->m.e12 + transform->m.e31;
auto ty = dx * transform->e21 + dy * transform->e22 + transform->e32; auto ty = dx * transform->m.e21 + dy * transform->m.e22 + transform->m.e32;
auto pt = Point{tx, ty}; auto pt = Point{tx, ty};
outline->pts[i] = TO_SWPOINT(&pt); outline->pts[i] = TO_SWPOINT(&pt);
} }

View file

@ -30,10 +30,24 @@
/* External Class Implementation */ /* External Class Implementation */
/************************************************************************/ /************************************************************************/
void RenderTransform::override(const Matrix& m)
{
this->m = m;
if (m.e11 == 0.0f && m.e12 == 0.0f && m.e13 == 0.0f &&
m.e21 == 0.0f && m.e22 == 0.0f && m.e23 == 0.0f &&
m.e31 == 0.0f && m.e32 == 0.0f && m.e33 == 0.0f) {
overriding = false;
} else overriding = true;
}
bool RenderTransform::update() bool RenderTransform::update()
{ {
constexpr auto PI = 3.141592f; constexpr auto PI = 3.141592f;
if (overriding) return true;
//Init Status //Init Status
if (fabsf(x) <= FLT_EPSILON && fabsf(y) <= FLT_EPSILON && if (fabsf(x) <= FLT_EPSILON && fabsf(y) <= FLT_EPSILON &&
fabsf(degree) <= FLT_EPSILON && fabsf(factor - 1) <= FLT_EPSILON) { fabsf(degree) <= FLT_EPSILON && fabsf(factor - 1) <= FLT_EPSILON) {
@ -41,20 +55,20 @@ bool RenderTransform::update()
} }
//identity //identity
e11 = 1.0f; m.e11 = 1.0f;
e12 = 0.0f; m.e12 = 0.0f;
e13 = 0.0f; m.e13 = 0.0f;
e21 = 0.0f; m.e21 = 0.0f;
e22 = 1.0f; m.e22 = 1.0f;
e23 = 0.0f; m.e23 = 0.0f;
e31 = 0.0f; m.e31 = 0.0f;
e32 = 0.0f; m.e32 = 0.0f;
e33 = 1.0f; m.e33 = 1.0f;
//scale //scale
e11 *= factor; m.e11 *= factor;
e22 *= factor; m.e22 *= factor;
e33 *= factor; m.e33 *= factor;
//rotation //rotation
if (fabsf(degree) > FLT_EPSILON) { if (fabsf(degree) > FLT_EPSILON) {
@ -62,23 +76,23 @@ bool RenderTransform::update()
auto cosVal = cosf(radian); auto cosVal = cosf(radian);
auto sinVal = sinf(radian); auto sinVal = sinf(radian);
auto t11 = e11 * cosVal + e12 * sinVal; auto t11 = m.e11 * cosVal + m.e12 * sinVal;
auto t12 = e11 * -sinVal + e12 * cosVal; auto t12 = m.e11 * -sinVal + m.e12 * cosVal;
auto t21 = e21 * cosVal + e22 * sinVal; auto t21 = m.e21 * cosVal + m.e22 * sinVal;
auto t22 = e21 * -sinVal + e22 * cosVal; auto t22 = m.e21 * -sinVal + m.e22 * cosVal;
auto t31 = e31 * cosVal + e32 * sinVal; auto t31 = m.e31 * cosVal + m.e32 * sinVal;
auto t32 = e31 * -sinVal + e32 * cosVal; auto t32 = m.e31 * -sinVal + m.e32 * cosVal;
e11 = t11; m.e11 = t11;
e12 = t12; m.e12 = t12;
e21 = t21; m.e21 = t21;
e22 = t22; m.e22 = t22;
e31 = t31; m.e31 = t31;
e32 = t32; m.e32 = t32;
} }
e31 += x; m.e31 += x;
e32 += y; m.e32 += y;
return true; return true;
} }
@ -95,8 +109,8 @@ RenderTransform::RenderTransform(const RenderTransform* lhs, const RenderTransfo
auto dx = rhs->x * lhs->factor; auto dx = rhs->x * lhs->factor;
auto dy = rhs->y * lhs->factor; auto dy = rhs->y * lhs->factor;
auto tx = dx * lhs->e11 + dy * lhs->e12 + lhs->e13; auto tx = dx * lhs->m.e11 + dy * lhs->m.e12 + lhs->m.e13;
auto ty = dx * lhs->e21 + dy * lhs->e22 + lhs->e23; auto ty = dx * lhs->m.e21 + dy * lhs->m.e22 + lhs->m.e23;
x = lhs->x + tx; x = lhs->x + tx;
y = lhs->y + ty; y = lhs->y + ty;

View file

@ -32,17 +32,15 @@ enum RenderUpdateFlag {None = 0, Path = 1, Color = 2, Gradient = 4, Stroke = 8,
struct RenderTransform struct RenderTransform
{ {
//3x3 Matrix Elements Matrix m; //3x3 Matrix Elements
float e11, e12, e13;
float e21, e22, e23;
float e31, e32, e33;
float x = 0.0f; float x = 0.0f;
float y = 0.0f; float y = 0.0f;
float degree = 0.0f; //rotation degree float degree = 0.0f; //rotation degree
float factor = 1.0f; //scale factor float factor = 1.0f; //scale factor
bool overriding = false; //user transform?
bool update(); bool update();
void override(const Matrix& m);
RenderTransform(); RenderTransform();
RenderTransform(const RenderTransform* lhs, const RenderTransform* rhs); RenderTransform(const RenderTransform* lhs, const RenderTransform* rhs);

View file

@ -97,6 +97,17 @@ Result Scene::translate(float x, float y) noexcept
} }
Result Scene::transform(const Matrix& m) noexcept
{
auto impl = pImpl.get();
if (!impl) return Result::MemoryCorruption;
if (!impl->transform(m)) return Result::FailedAllocation;
return Result::Success;
}
Result Scene::bounds(float* x, float* y, float* w, float* h) const noexcept Result Scene::bounds(float* x, float* y, float* w, float* h) const noexcept
{ {
auto impl = pImpl.get(); auto impl = pImpl.get();

View file

@ -26,7 +26,7 @@
struct Scene::Impl struct Scene::Impl
{ {
vector<Paint*> paints; vector<Paint*> paints;
RenderTransform *transform = nullptr; RenderTransform *rTransform = nullptr;
uint32_t flag = RenderUpdateFlag::None; uint32_t flag = RenderUpdateFlag::None;
unique_ptr<Loader> loader = nullptr; unique_ptr<Loader> loader = nullptr;
@ -34,7 +34,7 @@ struct Scene::Impl
{ {
//Are you sure clear() prior to this? //Are you sure clear() prior to this?
assert(paints.empty()); assert(paints.empty());
if (transform) delete(transform); if (rTransform) delete(rTransform);
} }
bool clear(RenderMethod& renderer) bool clear(RenderMethod& renderer)
@ -82,20 +82,20 @@ struct Scene::Impl
} }
if (flag & RenderUpdateFlag::Transform) { if (flag & RenderUpdateFlag::Transform) {
if (!transform) return false; if (!rTransform) return false;
if (!transform->update()) { if (!rTransform->update()) {
delete(transform); delete(rTransform);
transform = nullptr; rTransform = nullptr;
} }
} }
auto ret = true; auto ret = true;
if (transform && pTransform) { if (rTransform && pTransform) {
RenderTransform outTransform(pTransform, transform); RenderTransform outTransform(pTransform, rTransform);
ret = updateInternal(renderer, &outTransform, pFlag | flag); ret = updateInternal(renderer, &outTransform, pFlag | flag);
} else { } else {
auto outTransform = pTransform ? pTransform : transform; auto outTransform = pTransform ? pTransform : rTransform;
ret = updateInternal(renderer, outTransform, pFlag | flag); ret = updateInternal(renderer, outTransform, pFlag | flag);
} }
@ -158,14 +158,14 @@ struct Scene::Impl
bool scale(float factor) bool scale(float factor)
{ {
if (transform) { if (rTransform) {
if (fabsf(factor - transform->factor) <= FLT_EPSILON) return true; if (fabsf(factor - rTransform->factor) <= FLT_EPSILON) return true;
} else { } else {
if (fabsf(factor) <= FLT_EPSILON) return true; if (fabsf(factor) <= FLT_EPSILON) return true;
transform = new RenderTransform(); rTransform = new RenderTransform();
if (!transform) return false; if (!rTransform) return false;
} }
transform->factor = factor; rTransform->factor = factor;
flag |= RenderUpdateFlag::Transform; flag |= RenderUpdateFlag::Transform;
return true; return true;
@ -173,14 +173,14 @@ struct Scene::Impl
bool rotate(float degree) bool rotate(float degree)
{ {
if (transform) { if (rTransform) {
if (fabsf(degree - transform->degree) <= FLT_EPSILON) return true; if (fabsf(degree - rTransform->degree) <= FLT_EPSILON) return true;
} else { } else {
if (fabsf(degree) <= FLT_EPSILON) return true; if (fabsf(degree) <= FLT_EPSILON) return true;
transform = new RenderTransform(); rTransform = new RenderTransform();
if (!transform) return false; if (!rTransform) return false;
} }
transform->degree = degree; rTransform->degree = degree;
flag |= RenderUpdateFlag::Transform; flag |= RenderUpdateFlag::Transform;
return true; return true;
@ -188,20 +188,34 @@ struct Scene::Impl
bool translate(float x, float y) bool translate(float x, float y)
{ {
if (transform) { if (rTransform) {
if (fabsf(x - transform->x) <= FLT_EPSILON && fabsf(y - transform->y) <= FLT_EPSILON) return true; if (fabsf(x - rTransform->x) <= FLT_EPSILON && fabsf(y - rTransform->y) <= FLT_EPSILON) return true;
} else { } else {
if (fabsf(x) <= FLT_EPSILON && fabsf(y) <= FLT_EPSILON) return true; if (fabsf(x) <= FLT_EPSILON && fabsf(y) <= FLT_EPSILON) return true;
transform = new RenderTransform(); rTransform = new RenderTransform();
if (!transform) return false; if (!rTransform) return false;
} }
transform->x = x; rTransform->x = x;
transform->y = y; rTransform->y = y;
flag |= RenderUpdateFlag::Transform; flag |= RenderUpdateFlag::Transform;
return true; return true;
} }
bool transform(const Matrix& m)
{
if (!rTransform) {
rTransform = new RenderTransform();
if (!rTransform) return false;
}
rTransform->override(m);
flag |= RenderUpdateFlag::Transform;
return true;
}
Result load(const string& path) Result load(const string& path)
{ {
if (loader) loader->close(); if (loader) loader->close();

View file

@ -303,7 +303,18 @@ Result Shape::translate(float x, float y) noexcept
auto impl = pImpl.get(); auto impl = pImpl.get();
if (!impl) return Result::MemoryCorruption; if (!impl) return Result::MemoryCorruption;
impl->translate(x, y); if (!impl->translate(x, y)) return Result::FailedAllocation;
return Result::Success;
}
Result Shape::transform(const Matrix& m) noexcept
{
auto impl = pImpl.get();
if (!impl) return Result::MemoryCorruption;
if (!impl->transform(m)) return Result::FailedAllocation;
return Result::Success; return Result::Success;
} }

View file

@ -44,7 +44,7 @@ struct Shape::Impl
ShapePath *path = nullptr; ShapePath *path = nullptr;
Fill *fill = nullptr; Fill *fill = nullptr;
ShapeStroke *stroke = nullptr; ShapeStroke *stroke = nullptr;
RenderTransform *transform = nullptr; RenderTransform *rTransform = nullptr;
uint8_t color[4] = {0, 0, 0, 0}; //r, g, b, a uint8_t color[4] = {0, 0, 0, 0}; //r, g, b, a
uint32_t flag = RenderUpdateFlag::None; uint32_t flag = RenderUpdateFlag::None;
void *edata = nullptr; //engine data void *edata = nullptr; //engine data
@ -59,7 +59,7 @@ struct Shape::Impl
if (path) delete(path); if (path) delete(path);
if (fill) delete(fill); if (fill) delete(fill);
if (stroke) delete(stroke); if (stroke) delete(stroke);
if (transform) delete(transform); if (rTransform) delete(rTransform);
} }
bool dispose(Shape& shape, RenderMethod& renderer) bool dispose(Shape& shape, RenderMethod& renderer)
@ -75,18 +75,18 @@ struct Shape::Impl
bool update(Shape& shape, RenderMethod& renderer, const RenderTransform* pTransform = nullptr, uint32_t pFlag = 0) bool update(Shape& shape, RenderMethod& renderer, const RenderTransform* pTransform = nullptr, uint32_t pFlag = 0)
{ {
if (flag & RenderUpdateFlag::Transform) { if (flag & RenderUpdateFlag::Transform) {
if (!transform) return false; if (!rTransform) return false;
if (!transform->update()) { if (!rTransform->update()) {
delete(transform); delete(rTransform);
transform = nullptr; rTransform = nullptr;
} }
} }
if (transform && pTransform) { if (rTransform && pTransform) {
RenderTransform outTransform(pTransform, transform); RenderTransform outTransform(pTransform, rTransform);
edata = renderer.prepare(shape, edata, &outTransform, static_cast<RenderUpdateFlag>(pFlag | flag)); edata = renderer.prepare(shape, edata, &outTransform, static_cast<RenderUpdateFlag>(pFlag | flag));
} else { } else {
auto outTransform = pTransform ? pTransform : transform; auto outTransform = pTransform ? pTransform : rTransform;
edata = renderer.prepare(shape, edata, outTransform, static_cast<RenderUpdateFlag>(pFlag | flag)); edata = renderer.prepare(shape, edata, outTransform, static_cast<RenderUpdateFlag>(pFlag | flag));
} }
@ -104,50 +104,64 @@ struct Shape::Impl
bool scale(float factor) bool scale(float factor)
{ {
if (transform) { if (rTransform) {
if (fabsf(factor - transform->factor) <= FLT_EPSILON) return true; if (fabsf(factor - rTransform->factor) <= FLT_EPSILON) return true;
} else { } else {
if (fabsf(factor) <= FLT_EPSILON) return true; if (fabsf(factor) <= FLT_EPSILON) return true;
transform = new RenderTransform(); rTransform = new RenderTransform();
if (!transform) return false; if (!rTransform) return false;
} }
transform->factor = factor; rTransform->factor = factor;
flag |= RenderUpdateFlag::Transform; if (!rTransform->overriding) flag |= RenderUpdateFlag::Transform;
return true; return true;
} }
bool rotate(float degree) bool rotate(float degree)
{ {
if (transform) { if (rTransform) {
if (fabsf(degree - transform->degree) <= FLT_EPSILON) return true; if (fabsf(degree - rTransform->degree) <= FLT_EPSILON) return true;
} else { } else {
if (fabsf(degree) <= FLT_EPSILON) return true; if (fabsf(degree) <= FLT_EPSILON) return true;
transform = new RenderTransform(); rTransform = new RenderTransform();
if (!transform) return false; if (!rTransform) return false;
} }
transform->degree = degree; rTransform->degree = degree;
flag |= RenderUpdateFlag::Transform; if (!rTransform->overriding) flag |= RenderUpdateFlag::Transform;
return true; return true;
} }
bool translate(float x, float y) bool translate(float x, float y)
{ {
if (transform) { if (rTransform) {
if (fabsf(x - transform->x) <= FLT_EPSILON && fabsf(y - transform->y) <= FLT_EPSILON) return true; if (fabsf(x - rTransform->x) <= FLT_EPSILON && fabsf(y - rTransform->y) <= FLT_EPSILON) return true;
} else { } else {
if (fabsf(x) <= FLT_EPSILON && fabsf(y) <= FLT_EPSILON) return true; if (fabsf(x) <= FLT_EPSILON && fabsf(y) <= FLT_EPSILON) return true;
transform = new RenderTransform(); rTransform = new RenderTransform();
if (!transform) return false; if (!rTransform) return false;
} }
transform->x = x; rTransform->x = x;
transform->y = y; rTransform->y = y;
if (!rTransform->overriding) flag |= RenderUpdateFlag::Transform;
return true;
}
bool transform(const Matrix& m)
{
if (!rTransform) {
rTransform = new RenderTransform();
if (!rTransform) return false;
}
rTransform->override(m);
flag |= RenderUpdateFlag::Transform; flag |= RenderUpdateFlag::Transform;
return true; return true;
} }
bool strokeWidth(float width) bool strokeWidth(float width)
{ {
//TODO: Size Exception? //TODO: Size Exception?

View file

@ -9,6 +9,7 @@ all:
gcc -o testDirectUpdate testDirectUpdate.cpp -g -lstdc++ `pkg-config --cflags --libs elementary tizenvg` gcc -o testDirectUpdate testDirectUpdate.cpp -g -lstdc++ `pkg-config --cflags --libs elementary tizenvg`
gcc -o testScene testScene.cpp -g -lstdc++ `pkg-config --cflags --libs elementary tizenvg` gcc -o testScene testScene.cpp -g -lstdc++ `pkg-config --cflags --libs elementary tizenvg`
gcc -o testTransform testTransform.cpp -g -lstdc++ `pkg-config --cflags --libs elementary tizenvg` gcc -o testTransform testTransform.cpp -g -lstdc++ `pkg-config --cflags --libs elementary tizenvg`
gcc -o testCustomTransform testCustomTransform.cpp -g -lstdc++ `pkg-config --cflags --libs elementary tizenvg`
gcc -o testSceneTransform testSceneTransform.cpp -g -lstdc++ `pkg-config --cflags --libs elementary tizenvg` gcc -o testSceneTransform testSceneTransform.cpp -g -lstdc++ `pkg-config --cflags --libs elementary tizenvg`
gcc -o testStroke testStroke.cpp -g -lstdc++ `pkg-config --cflags --libs elementary tizenvg` gcc -o testStroke testStroke.cpp -g -lstdc++ `pkg-config --cflags --libs elementary tizenvg`
gcc -o testStrokeLine testStrokeLine.cpp -g -lstdc++ `pkg-config --cflags --libs elementary tizenvg` gcc -o testStrokeLine testStrokeLine.cpp -g -lstdc++ `pkg-config --cflags --libs elementary tizenvg`

View file

@ -0,0 +1,140 @@
#include <tizenvg.h>
#include <Elementary.h>
using namespace std;
#define WIDTH 800
#define HEIGHT 800
static uint32_t buffer[WIDTH * HEIGHT];
unique_ptr<tvg::SwCanvas> canvas = nullptr;
tvg::Shape* pShape = nullptr;
void tvgtest()
{
//Create a Canvas
canvas = tvg::SwCanvas::gen();
canvas->target(buffer, WIDTH, WIDTH, HEIGHT);
//Shape1
auto shape = tvg::Shape::gen();
/* Acquire shape pointer to access it again.
instead, you should consider not to interrupt this pointer life-cycle. */
pShape = shape.get();
shape->moveTo(0, -114.5);
shape->lineTo(54, -5.5);
shape->lineTo(175, 11.5);
shape->lineTo(88, 95.5);
shape->lineTo(108, 216.5);
shape->lineTo(0, 160.5);
shape->lineTo(-102, 216.5);
shape->lineTo(-87, 96.5);
shape->lineTo(-173, 12.5);
shape->lineTo(-53, -5.5);
shape->close();
shape->fill(0, 0, 255, 255);
canvas->push(move(shape));
//Draw first frame
canvas->draw();
canvas->sync();
}
void transit_cb(Elm_Transit_Effect *effect, Elm_Transit* transit, double progress)
{
/* Update shape directly.
You can update only necessary properties of this shape,
while retaining other properties. */
//Transform Matrix
tvg::Matrix m = {1, 0, 0, 0, 1, 0, 0, 0, 1};
//scale x
m.e11 = 1 - (progress * 0.5f);
//scale y
m.e22 = 1 + (progress * 2.0f);
//rotation
constexpr auto PI = 3.141592f;
auto degree = 45.0f;
auto radian = degree / 180.0f * PI;
auto cosVal = cosf(radian);
auto sinVal = sinf(radian);
auto t11 = m.e11 * cosVal + m.e12 * sinVal;
auto t12 = m.e11 * -sinVal + m.e12 * cosVal;
auto t21 = m.e21 * cosVal + m.e22 * sinVal;
auto t22 = m.e21 * -sinVal + m.e22 * cosVal;
auto t31 = m.e31 * cosVal + m.e32 * sinVal;
auto t32 = m.e31 * -sinVal + m.e32 * cosVal;
m.e11 = t11;
m.e12 = t12;
m.e21 = t21;
m.e22 = t22;
m.e31 = t31;
m.e32 = t32;
//translate
m.e31 = progress * 300.0f + 300.0f;
m.e32 = progress * -100.0f + 300.0f;
pShape->transform(m);
//Update shape for drawing (this may work asynchronously)
canvas->update(pShape);
//Draw Next frames
canvas->draw();
canvas->sync();
//Update Efl Canvas
Eo* img = (Eo*) effect;
evas_object_image_data_update_add(img, 0, 0, WIDTH, HEIGHT);
}
void
win_del(void *data, Evas_Object *o, void *ev)
{
elm_exit();
}
int main(int argc, char **argv)
{
//Initialize TizenVG Engine
tvg::Initializer::init(tvg::CanvasEngine::Sw);
tvgtest();
//Show the result using EFL...
elm_init(argc, argv);
Eo* win = elm_win_util_standard_add(NULL, "TizenVG Test");
evas_object_smart_callback_add(win, "delete,request", win_del, 0);
Eo* img = evas_object_image_filled_add(evas_object_evas_get(win));
evas_object_image_size_set(img, WIDTH, HEIGHT);
evas_object_image_data_set(img, buffer);
evas_object_size_hint_weight_set(img, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
evas_object_show(img);
elm_win_resize_object_add(win, img);
evas_object_geometry_set(win, 0, 0, WIDTH, HEIGHT);
evas_object_show(win);
Elm_Transit *transit = elm_transit_add();
elm_transit_effect_add(transit, transit_cb, img, nullptr);
elm_transit_duration_set(transit, 2);
elm_transit_repeat_times_set(transit, -1);
elm_transit_auto_reverse_set(transit, EINA_TRUE);
elm_transit_go(transit);
elm_run();
elm_shutdown();
//Terminate TizenVG Engine
tvg::Initializer::term(tvg::CanvasEngine::Sw);
}