2 * Copyright 2014 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "PageCachingDocument.h"
10 #include "SkDocument.h"
11 #include "SkPictureRecorder.h"
13 #include "SkTDArray.h"
17 typedef void (*DoneProc)(SkWStream*, bool);
18 typedef SkData* (*Encoder)(size_t*, const SkBitmap&);
20 // This class allows us to compare the relative memory consumption of
21 // the PDF and SkPicture backends.
22 class PageCachingDocument : public SkDocument {
24 PageCachingDocument(SkWStream*, DoneProc, Encoder, SkScalar rasterDpi);
25 virtual ~PageCachingDocument();
26 virtual SkCanvas* onBeginPage(SkScalar width,
28 const SkRect& content) SK_OVERRIDE;
29 void onEndPage() SK_OVERRIDE;
30 bool onClose(SkWStream*) SK_OVERRIDE;
31 void onAbort() SK_OVERRIDE;
37 SkAutoTUnref<SkPicture> fPic;
39 SkPictureRecorder fRecorder;
40 SkTDArray<Page> fPages;
45 PageCachingDocument::PageCachingDocument(SkWStream* stream,
49 : SkDocument(stream, done), fEncoder(encoder), fRasterDpi(rasterDpi) {
52 PageCachingDocument::~PageCachingDocument() {
53 for (Page* p = fPages.begin(); p != fPages.end(); ++p) {
58 SkCanvas* PageCachingDocument::onBeginPage(SkScalar width,
60 const SkRect& content) {
61 Page* page = fPages.push();
62 sk_bzero(page, sizeof(*page));
64 page->fHeight = height;
65 SkASSERT(!page->fPic.get());
66 SkCanvas* canvas = fRecorder.beginRecording(content);
70 void PageCachingDocument::onEndPage() {
71 SkASSERT(fPages.count() > 0);
72 SkASSERT(!fPages[fPages.count() - 1].fPic);
73 fPages[fPages.count() - 1].fPic.reset(fRecorder.endRecording());
76 bool PageCachingDocument::onClose(SkWStream* stream) {
77 SkAutoTUnref<SkDocument> doc(
78 SkDocument::CreatePDF(stream, NULL, fEncoder, fRasterDpi));
79 for (Page* page = fPages.begin(); page != fPages.end(); ++page) {
80 SkRect cullRect = page->fPic->cullRect();
82 doc->beginPage(page->fWidth, page->fHeight, &cullRect);
83 canvas->drawPicture(page->fPic);
89 void PageCachingDocument::onAbort() {
93 SkDocument* CreatePageCachingDocument(SkWStream* stream,
97 return SkNEW_ARGS(PageCachingDocument, (stream, done, encoder, rasterDpi));