diff --git a/inc/thorvg.h b/inc/thorvg.h index 1ee76984..00255da5 100644 --- a/inc/thorvg.h +++ b/inc/thorvg.h @@ -468,7 +468,7 @@ public: * @brief Guarantees that drawing task is finished. * * The Canvas rendering can be performed asynchronously. To make sure that rendering is finished, - * the sync() should be called after the draw(). + * the sync() must be called after the draw() regardless of threading. * * @return Result::Success when succeed, Result::InsufficientCondition otherwise. * @see Canvas::draw() diff --git a/src/lib/tvgCanvas.cpp b/src/lib/tvgCanvas.cpp index d09ff7a2..bb42441c 100644 --- a/src/lib/tvgCanvas.cpp +++ b/src/lib/tvgCanvas.cpp @@ -69,7 +69,5 @@ Result Canvas::update(Paint* paint) noexcept Result Canvas::sync() noexcept { - if (pImpl->renderer->sync()) return Result::Success; - - return Result::InsufficientCondition; + return pImpl->sync(); } diff --git a/src/lib/tvgCanvasImpl.h b/src/lib/tvgCanvasImpl.h index a225ad80..cce790e4 100644 --- a/src/lib/tvgCanvasImpl.h +++ b/src/lib/tvgCanvasImpl.h @@ -33,6 +33,7 @@ struct Canvas::Impl Array paints; RenderMethod* renderer; bool refresh = false; //if all paints should be updated by force. + bool drawing = false; //on drawing condition? Impl(RenderMethod* pRenderer):renderer(pRenderer) { @@ -46,6 +47,9 @@ struct Canvas::Impl Result push(unique_ptr paint) { + //You can not push paints during rendering. + if (drawing) return Result::InsufficientCondition; + auto p = paint.release(); if (!p) return Result::MemoryCorruption; paints.push(p); @@ -66,6 +70,8 @@ struct Canvas::Impl paints.clear(); + drawing = false; + return Result::Success; } @@ -99,7 +105,7 @@ struct Canvas::Impl Result draw() { - if (!renderer || !renderer->preRender()) return Result::InsufficientCondition; + if (drawing || paints.count == 0 || !renderer || !renderer->preRender()) return Result::InsufficientCondition; for (auto paint = paints.data; paint < (paints.data + paints.count); ++paint) { if (!(*paint)->pImpl->render(*renderer)) return Result::InsufficientCondition; @@ -107,8 +113,21 @@ struct Canvas::Impl if (!renderer->postRender()) return Result::InsufficientCondition; + drawing = true; + return Result::Success; } + + Result sync() + { + if (!drawing) return Result::InsufficientCondition; + if (renderer->sync()) { + drawing = false; + return Result::Success; + } + + return Result::InsufficientCondition; + } }; #endif /* _TVG_CANVAS_IMPL_H_ */