зеркало из https://github.com/mozilla/moz-skia.git
[PDF] Improve PDF support in gm.
Currently gm ends up writing the same PDF four times. This fixes that problem, making PDF a first class citizen and providing a better framework for additional backends. Read support for PDF is still missing though. Review URL: http://codereview.appspot.com/4130045 git-svn-id: http://skia.googlecode.com/svn/trunk@759 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
Родитель
c3a2ae5823
Коммит
686abdfab0
|
@ -157,38 +157,29 @@ static bool compare(const SkBitmap& target, const SkBitmap& base,
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void write_pdf(GM* gm, const char writePath[]) {
|
static bool write_pdf(const SkString& path, const SkDynamicMemoryWStream& pdf) {
|
||||||
#ifdef SK_SUPPORT_PDF
|
|
||||||
SkISize size = gm->getISize();
|
|
||||||
SkPDFDevice* dev = new SkPDFDevice(size.width(), size.height());
|
|
||||||
SkAutoUnref aur(dev);
|
|
||||||
|
|
||||||
{
|
|
||||||
SkCanvas c(dev);
|
|
||||||
gm->draw(&c);
|
|
||||||
}
|
|
||||||
|
|
||||||
SkDynamicMemoryWStream output;
|
|
||||||
SkPDFDocument doc;
|
|
||||||
doc.appendPage(dev);
|
|
||||||
doc.emitPDF(&output);
|
|
||||||
|
|
||||||
SkString shortName(gm->shortName());
|
|
||||||
SkString path = make_filename(writePath, shortName, "pdf");
|
|
||||||
SkFILEWStream stream(path.c_str());
|
SkFILEWStream stream(path.c_str());
|
||||||
stream.write(output.getStream(), output.getOffset());
|
return stream.write(pdf.getStream(), pdf.getOffset());
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum Backend {
|
||||||
|
kRaster_Backend,
|
||||||
|
kGPU_Backend,
|
||||||
|
kPDF_Backend,
|
||||||
|
};
|
||||||
|
|
||||||
static const struct {
|
static const struct {
|
||||||
SkBitmap::Config fConfig;
|
SkBitmap::Config fConfig;
|
||||||
bool fUseGPU;
|
Backend fBackend;
|
||||||
const char* fName;
|
const char* fName;
|
||||||
} gRec[] = {
|
} gRec[] = {
|
||||||
{ SkBitmap::kARGB_8888_Config, false, "8888" },
|
{ SkBitmap::kARGB_8888_Config, kRaster_Backend, "8888" },
|
||||||
{ SkBitmap::kARGB_4444_Config, false, "4444" },
|
{ SkBitmap::kARGB_4444_Config, kRaster_Backend, "4444" },
|
||||||
{ SkBitmap::kRGB_565_Config, false, "565" },
|
{ SkBitmap::kRGB_565_Config, kRaster_Backend, "565" },
|
||||||
{ SkBitmap::kARGB_8888_Config, true, "gpu" },
|
{ SkBitmap::kARGB_8888_Config, kGPU_Backend, "gpu" },
|
||||||
|
#ifdef SK_SUPPORT_PDF
|
||||||
|
{ SkBitmap::kARGB_8888_Config, kPDF_Backend, "pdf" },
|
||||||
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
int main (int argc, char * const argv[]) {
|
int main (int argc, char * const argv[]) {
|
||||||
|
@ -240,34 +231,63 @@ int main (int argc, char * const argv[]) {
|
||||||
size.width(), size.height());
|
size.width(), size.height());
|
||||||
|
|
||||||
SkBitmap bitmap;
|
SkBitmap bitmap;
|
||||||
|
SkDynamicMemoryWStream pdf;
|
||||||
for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
|
||||||
|
if (gRec[i].fBackend == kRaster_Backend ||
|
||||||
|
gRec[i].fBackend == kGPU_Backend) {
|
||||||
bitmap.setConfig(gRec[i].fConfig, size.width(), size.height());
|
bitmap.setConfig(gRec[i].fConfig, size.width(), size.height());
|
||||||
bitmap.allocPixels();
|
bitmap.allocPixels();
|
||||||
bitmap.eraseColor(0);
|
bitmap.eraseColor(0);
|
||||||
SkCanvas canvas(bitmap);
|
SkCanvas canvas(bitmap);
|
||||||
|
|
||||||
if (gRec[i].fUseGPU) {
|
if (gRec[i].fBackend == kRaster_Backend) {
|
||||||
|
gm->draw(&canvas);
|
||||||
|
} else { // GPU
|
||||||
if (NULL == context) {
|
if (NULL == context) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
SkGpuCanvas gc(context, SkGpuDevice::Current3DApiRenderTarget());
|
SkGpuCanvas gc(context,
|
||||||
gc.setDevice(gc.createDevice(bitmap.config(), bitmap.width(), bitmap.height(),
|
SkGpuDevice::Current3DApiRenderTarget());
|
||||||
bitmap.isOpaque(), false))->unref();
|
gc.setDevice(gc.createDevice(bitmap.config(),
|
||||||
|
bitmap.width(),
|
||||||
|
bitmap.height(),
|
||||||
|
bitmap.isOpaque(),
|
||||||
|
false))->unref();
|
||||||
gm->draw(&gc);
|
gm->draw(&gc);
|
||||||
gc.readPixels(&bitmap); // overwrite our previous allocation
|
gc.readPixels(&bitmap); // overwrite our previous allocation
|
||||||
} else {
|
}
|
||||||
gm->draw(&canvas);
|
}
|
||||||
|
// TODO: Figure out a way to compare PDFs.
|
||||||
|
if (gRec[i].fBackend == kPDF_Backend && writePath) {
|
||||||
|
#ifdef SK_SUPPORT_PDF
|
||||||
|
SkISize size = gm->getISize();
|
||||||
|
SkPDFDevice* dev = new SkPDFDevice(size.width(), size.height());
|
||||||
|
SkAutoUnref aur(dev);
|
||||||
|
|
||||||
|
SkCanvas c(dev);
|
||||||
|
gm->draw(&c);
|
||||||
|
|
||||||
|
SkPDFDocument doc;
|
||||||
|
doc.appendPage(dev);
|
||||||
|
doc.emitPDF(&pdf);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
SkString name = make_name(gm->shortName(), gRec[i].fName);
|
SkString name = make_name(gm->shortName(), gRec[i].fName);
|
||||||
|
|
||||||
if (writePath) {
|
if (writePath) {
|
||||||
SkString path = make_filename(writePath, name, "png");
|
SkString path;
|
||||||
bool success = write_bitmap(path, bitmap);
|
bool success;
|
||||||
if (!success) {
|
if (gRec[i].fBackend != kPDF_Backend) {
|
||||||
fprintf(stderr, "FAILED to write %s\n", path.c_str());
|
path = make_filename(writePath, name, "png");
|
||||||
|
success = write_bitmap(path, bitmap);
|
||||||
|
} else {
|
||||||
|
path = make_filename(writePath, name, "pdf");
|
||||||
|
success = write_pdf(path, pdf);
|
||||||
}
|
}
|
||||||
write_pdf(gm, writePath);
|
if (!success)
|
||||||
} else if (readPath) {
|
fprintf(stderr, "FAILED to write %s\n", path.c_str());
|
||||||
|
// TODO: Figure out a way to compare PDFs.
|
||||||
|
} else if (readPath && gRec[i].fBackend != kPDF_Backend) {
|
||||||
SkString path = make_filename(readPath, name, "png");
|
SkString path = make_filename(readPath, name, "png");
|
||||||
SkBitmap orig;
|
SkBitmap orig;
|
||||||
bool success = SkImageDecoder::DecodeFile(path.c_str(), &orig,
|
bool success = SkImageDecoder::DecodeFile(path.c_str(), &orig,
|
||||||
|
|
Загрузка…
Ссылка в новой задаче