canvas: add reserve() method.

This allocates nodes slots in advance to avoid memory grow & copy.

it will be benefit if you know how many shapes will be used in your canvas.

Change-Id: I7d93d166c9c054078bd86593d3471a2ade3671ee
This commit is contained in:
Hermet Park 2020-04-27 01:52:44 +09:00
parent 0e6faa9276
commit 809dfd8644
4 changed files with 11 additions and 2 deletions

View file

@ -90,6 +90,7 @@ public:
Canvas(RenderMethod*); Canvas(RenderMethod*);
virtual ~Canvas(); virtual ~Canvas();
int reserve(size_t n);
virtual int push(std::unique_ptr<PaintNode> paint) noexcept; virtual int push(std::unique_ptr<PaintNode> paint) noexcept;
virtual int clear() noexcept; virtual int clear() noexcept;
virtual int update() noexcept; virtual int update() noexcept;

View file

@ -120,6 +120,14 @@ Canvas::~Canvas()
} }
int Canvas::reserve(size_t n)
{
auto impl = pImpl.get();
assert(impl);
return impl->reserve(n);
}
int Canvas::push(unique_ptr<PaintNode> paint) noexcept int Canvas::push(unique_ptr<PaintNode> paint) noexcept
{ {
auto impl = pImpl.get(); auto impl = pImpl.get();

View file

@ -15,7 +15,7 @@ void tvgtest()
//Create a Canvas //Create a Canvas
auto canvas = tvg::SwCanvas::gen(buffer, WIDTH, HEIGHT); auto canvas = tvg::SwCanvas::gen(buffer, WIDTH, HEIGHT);
//canvas->reserve(2); //reserve 2 shape nodes (optional) canvas->reserve(3); //reserve 3 shape nodes (optional)
//Prepare Round Rectangle //Prepare Round Rectangle
auto shape1 = tvg::ShapeNode::gen(); auto shape1 = tvg::ShapeNode::gen();

View file

@ -18,7 +18,7 @@ void tvgtest()
//Prepare a Shape (Rectangle) //Prepare a Shape (Rectangle)
auto shape1 = tvg::ShapeNode::gen(); auto shape1 = tvg::ShapeNode::gen();
shape1->appendRect(0, 0, 400, 400, 0); //x, y, w, h, cornerRadius shape1->appendRect(100, 100, 400, 400, 0); //x, y, w, h, cornerRadius
shape1->fill(255, 0, 0, 255); //r, g, b, a shape1->fill(255, 0, 0, 255); //r, g, b, a
/* Push the shape into the Canvas drawing list /* Push the shape into the Canvas drawing list