SkData to SkStreamAsset to avoid unneeded copying
R=mtklein@google.com Author: halcanary@google.com Review URL: https://codereview.chromium.org/502193002
This commit is contained in:
parent
f2d83caf13
commit
a4c6094177
@ -16,18 +16,20 @@
|
|||||||
namespace DM {
|
namespace DM {
|
||||||
|
|
||||||
PDFRasterizeTask::PDFRasterizeTask(const Task& parent,
|
PDFRasterizeTask::PDFRasterizeTask(const Task& parent,
|
||||||
SkData* pdf,
|
SkStreamAsset* pdf,
|
||||||
RasterizePdfProc proc)
|
RasterizePdfProc proc)
|
||||||
: CpuTask(parent)
|
: CpuTask(parent)
|
||||||
, fName(UnderJoin(parent.name().c_str(), "rasterize"))
|
, fName(UnderJoin(parent.name().c_str(), "rasterize"))
|
||||||
, fPdf(SkRef(pdf))
|
, fPdf(pdf)
|
||||||
, fRasterize(proc) {}
|
, fRasterize(proc) {
|
||||||
|
SkASSERT(fPdf.get());
|
||||||
|
SkASSERT(fPdf->unique());
|
||||||
|
}
|
||||||
|
|
||||||
void PDFRasterizeTask::draw() {
|
void PDFRasterizeTask::draw() {
|
||||||
SkMemoryStream pdfStream(fPdf.get());
|
|
||||||
SkBitmap bitmap;
|
SkBitmap bitmap;
|
||||||
|
|
||||||
if (fRasterize(&pdfStream, &bitmap)) {
|
if (fRasterize(fPdf.get(), &bitmap)) {
|
||||||
this->spawnChild(SkNEW_ARGS(WriteTask, (*this, bitmap)));
|
this->spawnChild(SkNEW_ARGS(WriteTask, (*this, bitmap)));
|
||||||
} else {
|
} else {
|
||||||
this->fail();
|
this->fail();
|
||||||
|
@ -21,8 +21,9 @@ typedef bool (*RasterizePdfProc)(SkStream* pdf, SkBitmap* output);
|
|||||||
|
|
||||||
class PDFRasterizeTask : public CpuTask {
|
class PDFRasterizeTask : public CpuTask {
|
||||||
public:
|
public:
|
||||||
|
// takes ownership of SkStreamAsset.
|
||||||
PDFRasterizeTask(const Task& parent,
|
PDFRasterizeTask(const Task& parent,
|
||||||
SkData* pdf,
|
SkStreamAsset* pdf,
|
||||||
RasterizePdfProc);
|
RasterizePdfProc);
|
||||||
|
|
||||||
virtual void draw() SK_OVERRIDE;
|
virtual void draw() SK_OVERRIDE;
|
||||||
@ -31,7 +32,7 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
const SkString fName;
|
const SkString fName;
|
||||||
SkAutoTUnref<SkData> fPdf;
|
SkAutoTDelete<SkStreamAsset> fPdf;
|
||||||
RasterizePdfProc fRasterize;
|
RasterizePdfProc fRasterize;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -50,11 +50,11 @@ public:
|
|||||||
|
|
||||||
SkCanvas* canvas() { return fCanvas; }
|
SkCanvas* canvas() { return fCanvas; }
|
||||||
|
|
||||||
SkData* end() {
|
SkStreamAsset* end() {
|
||||||
fCanvas->flush();
|
fCanvas->flush();
|
||||||
fDocument->endPage();
|
fDocument->endPage();
|
||||||
fDocument->close();
|
fDocument->close();
|
||||||
return fWriteStream.copyToData();
|
return fWriteStream.detachAsStream();
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -66,7 +66,7 @@ private:
|
|||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
void PDFTask::draw() {
|
void PDFTask::draw() {
|
||||||
SkAutoTUnref<SkData> pdfData;
|
SkAutoTDelete<SkStreamAsset> pdfData;
|
||||||
bool rasterize = true;
|
bool rasterize = true;
|
||||||
if (fGM.get()) {
|
if (fGM.get()) {
|
||||||
rasterize = 0 == (fGM->getFlags() & skiagm::GM::kSkipPDFRasterization_Flag);
|
rasterize = 0 == (fGM->getFlags() & skiagm::GM::kSkipPDFRasterization_Flag);
|
||||||
@ -83,9 +83,11 @@ void PDFTask::draw() {
|
|||||||
|
|
||||||
SkASSERT(pdfData.get());
|
SkASSERT(pdfData.get());
|
||||||
if (rasterize) {
|
if (rasterize) {
|
||||||
this->spawnChild(SkNEW_ARGS(PDFRasterizeTask, (*this, pdfData.get(), fRasterize)));
|
this->spawnChild(SkNEW_ARGS(PDFRasterizeTask,
|
||||||
|
(*this, pdfData->duplicate(), fRasterize)));
|
||||||
}
|
}
|
||||||
this->spawnChild(SkNEW_ARGS(WriteTask, (*this, pdfData.get(), ".pdf")));
|
this->spawnChild(SkNEW_ARGS(WriteTask,
|
||||||
|
(*this, pdfData->duplicate(), ".pdf")));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PDFTask::shouldSkip() const {
|
bool PDFTask::shouldSkip() const {
|
||||||
|
@ -41,11 +41,14 @@ WriteTask::WriteTask(const Task& parent, SkBitmap bitmap)
|
|||||||
, fData(NULL)
|
, fData(NULL)
|
||||||
, fExtension(".png") {}
|
, fExtension(".png") {}
|
||||||
|
|
||||||
WriteTask::WriteTask(const Task& parent, SkData *data, const char* ext)
|
WriteTask::WriteTask(const Task& parent, SkStreamAsset *data, const char* ext)
|
||||||
: CpuTask(parent)
|
: CpuTask(parent)
|
||||||
, fGmName(find_gm_name(parent, &fSuffixes))
|
, fGmName(find_gm_name(parent, &fSuffixes))
|
||||||
, fData(SkRef(data))
|
, fData(data)
|
||||||
, fExtension(ext) {}
|
, fExtension(ext) {
|
||||||
|
SkASSERT(fData.get());
|
||||||
|
SkASSERT(fData->unique());
|
||||||
|
}
|
||||||
|
|
||||||
void WriteTask::makeDirOrFail(SkString dir) {
|
void WriteTask::makeDirOrFail(SkString dir) {
|
||||||
if (!sk_mkdir(dir.c_str())) {
|
if (!sk_mkdir(dir.c_str())) {
|
||||||
@ -115,9 +118,10 @@ struct PngAndRaw {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Does not take ownership of data.
|
// Does not take ownership of data.
|
||||||
bool save_data_to_file(const SkData* data, const char* path) {
|
bool save_data_to_file(SkStreamAsset* data, const char* path) {
|
||||||
|
data->rewind();
|
||||||
SkFILEWStream stream(path);
|
SkFILEWStream stream(path);
|
||||||
if (!stream.isValid() || !stream.write(data->data(), data->size())) {
|
if (!stream.isValid() || !stream.writeStream(data, data->getLength())) {
|
||||||
SkDebugf("Can't write %s.\n", path);
|
SkDebugf("Can't write %s.\n", path);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -142,7 +146,7 @@ void WriteTask::draw() {
|
|||||||
SkString path = SkOSPath::Join(dir.c_str(), fGmName.c_str());
|
SkString path = SkOSPath::Join(dir.c_str(), fGmName.c_str());
|
||||||
path.append(fExtension);
|
path.append(fExtension);
|
||||||
|
|
||||||
const bool ok = fData.get() ? save_data_to_file(fData, path.c_str())
|
const bool ok = fData.get() ? save_data_to_file(fData.get(), path.c_str())
|
||||||
: PngAndRaw::Encode(fBitmap, path.c_str());
|
: PngAndRaw::Encode(fBitmap, path.c_str());
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
this->fail();
|
this->fail();
|
||||||
|
@ -18,8 +18,9 @@ public:
|
|||||||
WriteTask(const Task& parent, // WriteTask must be a child task.
|
WriteTask(const Task& parent, // WriteTask must be a child task.
|
||||||
SkBitmap bitmap); // Bitmap to encode to PNG and write to disk.
|
SkBitmap bitmap); // Bitmap to encode to PNG and write to disk.
|
||||||
|
|
||||||
|
// Takes ownership of SkStreamAsset
|
||||||
WriteTask(const Task& parent, // WriteTask must be a child task.
|
WriteTask(const Task& parent, // WriteTask must be a child task.
|
||||||
SkData *data, // Pre-encoded data to write to disk.
|
SkStreamAsset* data, // Pre-encoded data to write to disk.
|
||||||
const char* ext); // File extension.
|
const char* ext); // File extension.
|
||||||
|
|
||||||
virtual void draw() SK_OVERRIDE;
|
virtual void draw() SK_OVERRIDE;
|
||||||
@ -40,7 +41,7 @@ private:
|
|||||||
SkTArray<SkString> fSuffixes;
|
SkTArray<SkString> fSuffixes;
|
||||||
const SkString fGmName;
|
const SkString fGmName;
|
||||||
const SkBitmap fBitmap;
|
const SkBitmap fBitmap;
|
||||||
SkAutoTUnref<SkData> fData;
|
SkAutoTDelete<SkStreamAsset> fData;
|
||||||
const char* fExtension;
|
const char* fExtension;
|
||||||
|
|
||||||
void makeDirOrFail(SkString dir);
|
void makeDirOrFail(SkString dir);
|
||||||
|
Loading…
Reference in New Issue
Block a user