| OLD | NEW |
| 1 #include "DMSrcSink.h" | 1 #include "DMSrcSink.h" |
| 2 #include "SamplePipeControllers.h" | 2 #include "SamplePipeControllers.h" |
| 3 #include "SkCommonFlags.h" | 3 #include "SkCommonFlags.h" |
| 4 #include "SkDocument.h" | 4 #include "SkDocument.h" |
| 5 #include "SkMultiPictureDraw.h" | 5 #include "SkMultiPictureDraw.h" |
| 6 #include "SkOSFile.h" | 6 #include "SkOSFile.h" |
| 7 #include "SkPictureRecorder.h" | 7 #include "SkPictureRecorder.h" |
| 8 #include "SkRandom.h" | 8 #include "SkRandom.h" |
| 9 | 9 |
| 10 namespace DM { | 10 namespace DM { |
| 11 | 11 |
| 12 void SafeUnref(SkPicture* p) { SkSafeUnref(p); } | |
| 13 void SafeUnref(SkData* d) { SkSafeUnref(d); } | |
| 14 | |
| 15 // FIXME: the GM objects themselves are not threadsafe, so we create and destroy
them as needed. | |
| 16 | |
| 17 GMSrc::GMSrc(skiagm::GMRegistry::Factory factory) : fFactory(factory) {} | 12 GMSrc::GMSrc(skiagm::GMRegistry::Factory factory) : fFactory(factory) {} |
| 18 | 13 |
| 19 Error GMSrc::draw(SkCanvas* canvas) const { | 14 Error GMSrc::draw(SkCanvas* canvas) const { |
| 20 SkAutoTDelete<skiagm::GM> gm(fFactory(NULL)); | 15 SkAutoTDelete<skiagm::GM> gm(fFactory(NULL)); |
| 21 canvas->concat(gm->getInitialTransform()); | 16 canvas->concat(gm->getInitialTransform()); |
| 22 gm->draw(canvas); | 17 gm->draw(canvas); |
| 23 return ""; | 18 return ""; |
| 24 } | 19 } |
| 25 | 20 |
| 26 SkISize GMSrc::size() const { | 21 SkISize GMSrc::size() const { |
| 27 SkAutoTDelete<skiagm::GM> gm(fFactory(NULL)); | 22 SkAutoTDelete<skiagm::GM> gm(fFactory(NULL)); |
| 28 return gm->getISize(); | 23 return gm->getISize(); |
| 29 } | 24 } |
| 30 | 25 |
| 31 Name GMSrc::name() const { | 26 Name GMSrc::name() const { |
| 32 SkAutoTDelete<skiagm::GM> gm(fFactory(NULL)); | 27 SkAutoTDelete<skiagm::GM> gm(fFactory(NULL)); |
| 33 return gm->getName(); | 28 return gm->getName(); |
| 34 } | 29 } |
| 35 | 30 |
| 36 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~*/ | 31 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~*/ |
| 37 | 32 |
| 38 // The first call to draw() or size() will mmap the file to an SkData. ~ImageSr
c unrefs it. | |
| 39 struct LazyLoadImage { | |
| 40 LazyLoadImage(const char* path) : path(path) {} | |
| 41 const char* path; | |
| 42 | |
| 43 SkData* operator()() const { return SkData::NewFromFileName(path); } | |
| 44 }; | |
| 45 | |
| 46 ImageSrc::ImageSrc(SkString path, int subsets) : fPath(path), fSubsets(subsets)
{} | 33 ImageSrc::ImageSrc(SkString path, int subsets) : fPath(path), fSubsets(subsets)
{} |
| 47 | 34 |
| 48 Error ImageSrc::draw(SkCanvas* canvas) const { | 35 Error ImageSrc::draw(SkCanvas* canvas) const { |
| 49 const SkData* encoded = fEncoded.get(LazyLoadImage(fPath.c_str())); | 36 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); |
| 50 if (!encoded) { | 37 if (!encoded) { |
| 51 return SkStringPrintf("Couldn't read %s.", fPath.c_str()); | 38 return SkStringPrintf("Couldn't read %s.", fPath.c_str()); |
| 52 } | 39 } |
| 53 if (fSubsets == 0) { | 40 if (fSubsets == 0) { |
| 54 // Decode the full image. | 41 // Decode the full image. |
| 55 SkBitmap bitmap; | 42 SkBitmap bitmap; |
| 56 if (!SkImageDecoder::DecodeMemory(encoded->data(), encoded->size(), &bit
map)) { | 43 if (!SkImageDecoder::DecodeMemory(encoded->data(), encoded->size(), &bit
map)) { |
| 57 return SkStringPrintf("Couldn't decode %s.", fPath.c_str()); | 44 return SkStringPrintf("Couldn't decode %s.", fPath.c_str()); |
| 58 } | 45 } |
| 46 encoded.reset((SkData*)NULL); // Might as well drop this when we're don
e with it. |
| 59 canvas->drawBitmap(bitmap, 0,0); | 47 canvas->drawBitmap(bitmap, 0,0); |
| 60 return ""; | 48 return ""; |
| 61 } | 49 } |
| 62 // Decode random subsets. This is a little involved. | 50 // Decode random subsets. This is a little involved. |
| 63 SkMemoryStream stream(encoded->data(), encoded->size()); | 51 SkMemoryStream stream(encoded->data(), encoded->size()); |
| 64 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(&stream)); | 52 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(&stream)); |
| 65 if (!decoder) { | 53 if (!decoder) { |
| 66 return SkStringPrintf("Can't find a good decoder for %s.", fPath.c_str()
); | 54 return SkStringPrintf("Can't find a good decoder for %s.", fPath.c_str()
); |
| 67 } | 55 } |
| 68 int w,h; | 56 int w,h; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 82 SkBitmap subset; | 70 SkBitmap subset; |
| 83 if (!decoder->decodeSubset(&subset, rect, kUnknown_SkColorType/*use best
fit*/)) { | 71 if (!decoder->decodeSubset(&subset, rect, kUnknown_SkColorType/*use best
fit*/)) { |
| 84 return SkStringPrintf("Could not decode subset %d.\n", i); | 72 return SkStringPrintf("Could not decode subset %d.\n", i); |
| 85 } | 73 } |
| 86 canvas->drawBitmap(subset, SkIntToScalar(rect.fLeft), SkIntToScalar(rect
.fTop)); | 74 canvas->drawBitmap(subset, SkIntToScalar(rect.fLeft), SkIntToScalar(rect
.fTop)); |
| 87 } | 75 } |
| 88 return ""; | 76 return ""; |
| 89 } | 77 } |
| 90 | 78 |
| 91 SkISize ImageSrc::size() const { | 79 SkISize ImageSrc::size() const { |
| 92 const SkData* encoded = fEncoded.get(LazyLoadImage(fPath.c_str())); | 80 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); |
| 93 SkBitmap bitmap; | 81 SkBitmap bitmap; |
| 94 if (!encoded || !SkImageDecoder::DecodeMemory(encoded->data(), | 82 if (!encoded || !SkImageDecoder::DecodeMemory(encoded->data(), |
| 95 encoded->size(), | 83 encoded->size(), |
| 96 &bitmap, | 84 &bitmap, |
| 97 kUnknown_SkColorType, | 85 kUnknown_SkColorType, |
| 98 SkImageDecoder::kDecodeBounds_
Mode)) { | 86 SkImageDecoder::kDecodeBounds_
Mode)) { |
| 99 return SkISize::Make(0,0); | 87 return SkISize::Make(0,0); |
| 100 } | 88 } |
| 101 return bitmap.dimensions(); | 89 return bitmap.dimensions(); |
| 102 } | 90 } |
| 103 | 91 |
| 104 Name ImageSrc::name() const { return SkOSPath::Basename(fPath.c_str()); } | 92 Name ImageSrc::name() const { return SkOSPath::Basename(fPath.c_str()); } |
| 105 | 93 |
| 106 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~*/ | 94 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~*/ |
| 107 | 95 |
| 108 static const SkRect kSKPViewport = {0,0, 1000,1000}; | 96 static const SkRect kSKPViewport = {0,0, 1000,1000}; |
| 109 | 97 |
| 110 // The first call to draw() or size() will read the file into an SkPicture. ~SK
PSrc unrefs it. | |
| 111 struct LazyLoadPicture { | |
| 112 LazyLoadPicture(const char* path) : path(path) {} | |
| 113 const char* path; | |
| 114 | |
| 115 SkPicture* operator()() const { | |
| 116 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path)); | |
| 117 if (!stream) { | |
| 118 return NULL; | |
| 119 } | |
| 120 return SkPicture::CreateFromStream(stream); | |
| 121 } | |
| 122 }; | |
| 123 | |
| 124 SKPSrc::SKPSrc(SkString path) : fPath(path) {} | 98 SKPSrc::SKPSrc(SkString path) : fPath(path) {} |
| 125 | 99 |
| 126 Error SKPSrc::draw(SkCanvas* canvas) const { | 100 Error SKPSrc::draw(SkCanvas* canvas) const { |
| 127 const SkPicture* pic = fPic.get(LazyLoadPicture(fPath.c_str())); | 101 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(fPath.c_str())); |
| 128 if (!pic) { | 102 if (!stream) { |
| 129 return SkStringPrintf("Couldn't read %s.", fPath.c_str()); | 103 return SkStringPrintf("Couldn't read %s.", fPath.c_str()); |
| 130 } | 104 } |
| 105 SkAutoTUnref<SkPicture> pic(SkPicture::CreateFromStream(stream)); |
| 106 if (!pic) { |
| 107 return SkStringPrintf("Couldn't decode %s as a picture.", fPath.c_str())
; |
| 108 } |
| 109 stream.reset((SkStream*)NULL); // Might as well drop this when we're done w
ith it. |
| 131 canvas->clipRect(kSKPViewport); | 110 canvas->clipRect(kSKPViewport); |
| 132 canvas->drawPicture(pic); | 111 canvas->drawPicture(pic); |
| 133 return ""; | 112 return ""; |
| 134 } | 113 } |
| 135 | 114 |
| 136 SkISize SKPSrc::size() const { | 115 SkISize SKPSrc::size() const { |
| 137 const SkPicture* pic = fPic.get(LazyLoadPicture(fPath.c_str())); | 116 // This may be unnecessarily large. |
| 138 if (!pic) { | 117 return kSKPViewport.roundOut().size(); |
| 139 return SkISize::Make(0,0); | |
| 140 } | |
| 141 SkRect cull = pic->cullRect(); | |
| 142 if (!cull.intersect(kSKPViewport)) { | |
| 143 sk_throw(); | |
| 144 } | |
| 145 SkIRect bounds; | |
| 146 cull.roundOut(&bounds); | |
| 147 SkISize size = { bounds.width(), bounds.height() }; | |
| 148 return size; | |
| 149 } | 118 } |
| 150 | 119 |
| 151 Name SKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); } | 120 Name SKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); } |
| 152 | 121 |
| 153 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~*/ | 122 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~*/ |
| 154 | 123 |
| 155 GPUSink::GPUSink(GrContextFactory::GLContextType ct, | 124 GPUSink::GPUSink(GrContextFactory::GLContextType ct, |
| 156 GrGLStandard api, | 125 GrGLStandard api, |
| 157 int samples, | 126 int samples, |
| 158 bool dfText, | 127 bool dfText, |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 372 surfaces.unrefAll(); | 341 surfaces.unrefAll(); |
| 373 return ""; | 342 return ""; |
| 374 } | 343 } |
| 375 SkISize size() const SK_OVERRIDE { return fSize; } | 344 SkISize size() const SK_OVERRIDE { return fSize; } |
| 376 Name name() const SK_OVERRIDE { sk_throw(); return ""; } // No one shou
ld be calling this. | 345 Name name() const SK_OVERRIDE { sk_throw(); return ""; } // No one shou
ld be calling this. |
| 377 } proxy(fW, fH, pic, src.size()); | 346 } proxy(fW, fH, pic, src.size()); |
| 378 return fSink->draw(proxy, bitmap, stream); | 347 return fSink->draw(proxy, bitmap, stream); |
| 379 } | 348 } |
| 380 | 349 |
| 381 } // namespace DM | 350 } // namespace DM |
| OLD | NEW |