Fix bug of SkSVGCanvas::Make
The method `SkSVGCanvas::Make(const SkRect& bounds, SkWStream* writer)`
passes a pointer to a stack-allocated object to the returned SkCanvas.
TBR=
Change-Id: Ica7933adc59764a69eb2fb6312df91ffffd5627b
Reviewed-on: https://skia-review.googlesource.com/c/192040
Commit-Queue: Florin Malita <fmalita@chromium.org>
Reviewed-by: Mike Reed <reed@google.com>
Reviewed-by: Florin Malita <fmalita@chromium.org>
diff --git a/AUTHORS b/AUTHORS
index a0fd225..ff6fc6c 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -54,3 +54,4 @@
Kaloyan Donev <kdonev@gmail.com>
Yong-Hwan Baek <meisterdevhwan@gmail.com>
Alexander Khovansky <alx.khovansky@gmail.com>
+Zhuo Qingliang <zhuo.dev@gmail.com>
diff --git a/include/svg/SkSVGCanvas.h b/include/svg/SkSVGCanvas.h
index 1cc091a..bf1328e 100644
--- a/include/svg/SkSVGCanvas.h
+++ b/include/svg/SkSVGCanvas.h
@@ -29,7 +29,7 @@
static std::unique_ptr<SkCanvas> Make(const SkRect& bounds, SkWStream*);
// Internal only.
- static std::unique_ptr<SkCanvas> Make(const SkRect& bounds, SkXMLWriter*);
+ static std::unique_ptr<SkCanvas> Make(const SkRect& bounds, SkXMLWriter*, bool ownsWriter=false);
};
#endif
diff --git a/src/svg/SkSVGCanvas.cpp b/src/svg/SkSVGCanvas.cpp
index 3d1ff0e..c469551 100644
--- a/src/svg/SkSVGCanvas.cpp
+++ b/src/svg/SkSVGCanvas.cpp
@@ -10,15 +10,15 @@
#include "SkMakeUnique.h"
#include "SkXMLWriter.h"
-std::unique_ptr<SkCanvas> SkSVGCanvas::Make(const SkRect& bounds, SkXMLWriter* writer) {
+std::unique_ptr<SkCanvas> SkSVGCanvas::Make(const SkRect& bounds, SkXMLWriter* writer, bool ownsWriter) {
// TODO: pass full bounds to the device
SkISize size = bounds.roundOut().size();
- sk_sp<SkBaseDevice> device(SkSVGDevice::Create(size, writer));
+ sk_sp<SkBaseDevice> device(SkSVGDevice::Create(size, writer, ownsWriter));
return skstd::make_unique<SkCanvas>(device);
}
std::unique_ptr<SkCanvas> SkSVGCanvas::Make(const SkRect& bounds, SkWStream* writer) {
- SkXMLStreamWriter xmlWriter(writer);
- return Make(bounds, &xmlWriter);
+ SkXMLStreamWriter *xmlWriter = new SkXMLStreamWriter(writer);
+ return Make(bounds, xmlWriter, true);
}
diff --git a/src/svg/SkSVGDevice.cpp b/src/svg/SkSVGDevice.cpp
index a924737..579cc79 100644
--- a/src/svg/SkSVGDevice.cpp
+++ b/src/svg/SkSVGDevice.cpp
@@ -634,18 +634,19 @@
}
}
-SkBaseDevice* SkSVGDevice::Create(const SkISize& size, SkXMLWriter* writer) {
+SkBaseDevice* SkSVGDevice::Create(const SkISize& size, SkXMLWriter* writer, bool ownsWriter) {
if (!writer) {
return nullptr;
}
- return new SkSVGDevice(size, writer);
+ return new SkSVGDevice(size, writer, ownsWriter);
}
-SkSVGDevice::SkSVGDevice(const SkISize& size, SkXMLWriter* writer)
+SkSVGDevice::SkSVGDevice(const SkISize& size, SkXMLWriter* writer, bool ownsWriter)
: INHERITED(SkImageInfo::MakeUnknown(size.fWidth, size.fHeight),
SkSurfaceProps(0, kUnknown_SkPixelGeometry))
, fWriter(writer)
+ , fOwnsWriter(ownsWriter)
, fResourceBucket(new ResourceBucket)
{
SkASSERT(writer);
@@ -662,6 +663,9 @@
}
SkSVGDevice::~SkSVGDevice() {
+ if (fOwnsWriter && fWriter) {
+ delete fWriter;
+ }
}
void SkSVGDevice::drawPaint(const SkPaint& paint) {
diff --git a/src/svg/SkSVGDevice.h b/src/svg/SkSVGDevice.h
index 803b971..57d2b5c 100644
--- a/src/svg/SkSVGDevice.h
+++ b/src/svg/SkSVGDevice.h
@@ -15,7 +15,7 @@
class SkSVGDevice : public SkClipStackDevice {
public:
- static SkBaseDevice* Create(const SkISize& size, SkXMLWriter* writer);
+ static SkBaseDevice* Create(const SkISize& size, SkXMLWriter* writer, bool ownsWriter=false);
protected:
void drawPaint(const SkPaint& paint) override;
@@ -42,7 +42,7 @@
const SkPaint&) override;
private:
- SkSVGDevice(const SkISize& size, SkXMLWriter* writer);
+ SkSVGDevice(const SkISize& size, SkXMLWriter* writer, bool ownsWriter=false);
~SkSVGDevice() override;
struct MxCp;
@@ -52,6 +52,7 @@
class ResourceBucket;
SkXMLWriter* fWriter;
+ bool fOwnsWriter;
std::unique_ptr<AutoElement> fRootElement;
std::unique_ptr<ResourceBucket> fResourceBucket;