diff options
author | Mike Reed <reed@google.com> | 2016-11-15 11:52:55 -0500 |
---|---|---|
committer | Skia Commit-Bot <skia-commit-bot@chromium.org> | 2016-11-15 17:32:03 +0000 |
commit | 584ca89d3b7a7781ea0407ee4d1c953fc7085e75 (patch) | |
tree | a3ce8ce64779b55a0a08b82c0b0692c8eff0c85f /tests/CanvasTest.cpp | |
parent | d5a78805c5133bc55e07c5da21f8d72b91a3df4f (diff) |
change SkCanvasStack to take ownership of its subcanvases
Inspired by https://bugs.chromium.org/p/chromium/issues/detail?id=663959
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=4799
Change-Id: I69f7ac73386bb7ca96778e2fec4cb2757b982a52
Reviewed-on: https://skia-review.googlesource.com/4799
Commit-Queue: Mike Reed <reed@google.com>
Reviewed-by: Florin Malita <fmalita@chromium.org>
Diffstat (limited to 'tests/CanvasTest.cpp')
-rw-r--r-- | tests/CanvasTest.cpp | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/tests/CanvasTest.cpp b/tests/CanvasTest.cpp index 3f418db4ae..1824c25aad 100644 --- a/tests/CanvasTest.cpp +++ b/tests/CanvasTest.cpp @@ -734,3 +734,71 @@ DEF_TEST(DeferredCanvas, r) { canvas.restore(); } +/////////////////////////////////////////////////////////////////////////////////////////////////// + +#include "SkCanvasStack.h" +#include "SkNWayCanvas.h" + +// Subclass that takes a bool*, which it updates in its construct (true) and destructor (false) +// to allow the caller to know how long the object is alive. +class LifeLineCanvas : public SkCanvas { + bool* fLifeLine; +public: + LifeLineCanvas(int w, int h, bool* lifeline) : SkCanvas(w, h), fLifeLine(lifeline) { + *fLifeLine = true; + } + ~LifeLineCanvas() { + *fLifeLine = false; + } +}; + +// Check that NWayCanvas does NOT try to manage the lifetime of its sub-canvases +DEF_TEST(NWayCanvas, r) { + const int w = 10; + const int h = 10; + bool life[2]; + { + LifeLineCanvas c0(w, h, &life[0]); + REPORTER_ASSERT(r, life[0]); + } + REPORTER_ASSERT(r, !life[0]); + + + std::unique_ptr<SkCanvas> c0 = std::unique_ptr<SkCanvas>(new LifeLineCanvas(w, h, &life[0])); + std::unique_ptr<SkCanvas> c1 = std::unique_ptr<SkCanvas>(new LifeLineCanvas(w, h, &life[1])); + REPORTER_ASSERT(r, life[0]); + REPORTER_ASSERT(r, life[1]); + + { + SkNWayCanvas nway(w, h); + nway.addCanvas(c0.get()); + nway.addCanvas(c1.get()); + REPORTER_ASSERT(r, life[0]); + REPORTER_ASSERT(r, life[1]); + } + // Now assert that the death of the nway has NOT also killed the sub-canvases + REPORTER_ASSERT(r, life[0]); + REPORTER_ASSERT(r, life[1]); +} + +// Check that CanvasStack DOES manage the lifetime of its sub-canvases +DEF_TEST(CanvasStack, r) { + const int w = 10; + const int h = 10; + bool life[2]; + std::unique_ptr<SkCanvas> c0 = std::unique_ptr<SkCanvas>(new LifeLineCanvas(w, h, &life[0])); + std::unique_ptr<SkCanvas> c1 = std::unique_ptr<SkCanvas>(new LifeLineCanvas(w, h, &life[1])); + REPORTER_ASSERT(r, life[0]); + REPORTER_ASSERT(r, life[1]); + + { + SkCanvasStack stack(w, h); + stack.pushCanvas(std::move(c0), {0,0}); + stack.pushCanvas(std::move(c1), {0,0}); + REPORTER_ASSERT(r, life[0]); + REPORTER_ASSERT(r, life[1]); + } + // Now assert that the death of the canvasstack has also killed the sub-canvases + REPORTER_ASSERT(r, !life[0]); + REPORTER_ASSERT(r, !life[1]); +} |