SkFontData to use smart pointers.
authorbungeman <bungeman@google.com>
Thu, 15 Sep 2016 17:03:27 +0000 (10:03 -0700)
committerCommit bot <commit-bot@chromium.org>
Thu, 15 Sep 2016 17:03:27 +0000 (10:03 -0700)
The SkFontData type is not exposed externally, so any method which uses
it can be updated to use smart pointers without affecting external
users. Updating this first will make updating the public API much
easier.

This also updates SkStreamAsset* SkStream::NewFromFile(const char*) to
std::unique_ptr<SkStreamAsset> SkStream::MakeFromFile(const char*). It
appears that no one outside Skia is currently using SkStream::NewfromFile
so this is a good time to update it as well.

GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2339273002

Review-Url: https://codereview.chromium.org/2339273002

40 files changed:
bench/nanobench.cpp
dm/DMSrcSink.cpp
include/core/SkStream.h
include/core/SkTypeface.h
include/ports/SkFontMgr.h
include/utils/mac/SkCGUtils.h
samplecode/SampleAnimator.cpp
src/animator/SkAnimateMaker.cpp
src/animator/SkAnimator.cpp
src/core/SkFontDescriptor.cpp
src/core/SkFontDescriptor.h
src/core/SkFontMgr.cpp
src/core/SkStream.cpp
src/core/SkTypeface.cpp
src/images/SkMovie.cpp
src/ports/SkFontConfigInterface_direct.cpp
src/ports/SkFontConfigTypeface.h
src/ports/SkFontHost_FreeType.cpp
src/ports/SkFontHost_FreeType_common.h
src/ports/SkFontHost_mac.cpp
src/ports/SkFontHost_win.cpp
src/ports/SkFontMgr_FontConfigInterface.cpp
src/ports/SkFontMgr_android.cpp
src/ports/SkFontMgr_custom.cpp
src/ports/SkFontMgr_fontconfig.cpp
src/ports/SkFontMgr_win_dw.cpp
src/utils/SkWhitelistTypefaces.cpp
src/utils/mac/SkCreateCGImageRef.cpp
src/utils/mac/SkStream_mac.cpp
tests/BadIcoTest.cpp
tests/CodecTest.cpp
tests/ColorSpaceTest.cpp
tests/ExifTest.cpp
tests/SerializationTest.cpp
tests/YUVTest.cpp
tools/Resources.cpp
tools/dump_record.cpp
tools/get_images_from_skps.cpp
tools/lua/lua_pictures.cpp
tools/viewer/SKPSlide.cpp

index 5db5137ffd6f1f288da6e607bf3919586a38aa10..433cfa89d9cdde2998fa3b4923ee1c2baf555631 100644 (file)
@@ -645,8 +645,8 @@ public:
             return nullptr;
         }
 
-        SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
-        if (stream.get() == nullptr) {
+        std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(path);
+        if (!stream) {
             SkDebugf("Could not read %s.\n", path);
             return nullptr;
         }
index 26d90f24606c18087e6621d5acc9a2762cc7f034..e35bbad24e6c0981cbc9ab9a5c2c1f7937a2a7d8 100644 (file)
@@ -997,15 +997,15 @@ static const SkRect kSKPViewport = {0,0, 1000,1000};
 SKPSrc::SKPSrc(Path path) : fPath(path) {}
 
 Error SKPSrc::draw(SkCanvas* canvas) const {
-    SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(fPath.c_str()));
+    std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(fPath.c_str());
     if (!stream) {
         return SkStringPrintf("Couldn't read %s.", fPath.c_str());
     }
-    sk_sp<SkPicture> pic(SkPicture::MakeFromStream(stream));
+    sk_sp<SkPicture> pic(SkPicture::MakeFromStream(stream.get()));
     if (!pic) {
         return SkStringPrintf("Couldn't decode %s as a picture.", fPath.c_str());
     }
-    stream.reset((SkStream*)nullptr);  // Might as well drop this when we're done with it.
+    stream = nullptr;  // Might as well drop this when we're done with it.
 
     canvas->clipRect(kSKPViewport);
     canvas->drawPicture(pic);
@@ -1013,12 +1013,12 @@ Error SKPSrc::draw(SkCanvas* canvas) const {
 }
 
 SkISize SKPSrc::size() const {
-    SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(fPath.c_str()));
+    std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(fPath.c_str());
     if (!stream) {
         return SkISize::Make(0,0);
     }
     SkPictInfo info;
-    if (!SkPicture::InternalOnly_StreamIsSKP(stream, &info)) {
+    if (!SkPicture::InternalOnly_StreamIsSKP(stream.get(), &info)) {
         return SkISize::Make(0,0);
     }
     SkRect viewport = kSKPViewport;
@@ -1072,7 +1072,7 @@ bool SVGSrc::veto(SinkFlags flags) const {
 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
 
 MSKPSrc::MSKPSrc(Path path) : fPath(path) {
-    std::unique_ptr<SkStreamAsset> stream(SkStream::NewFromFile(fPath.c_str()));
+    std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(fPath.c_str());
     (void)fReader.init(stream.get());
 }
 
@@ -1083,7 +1083,7 @@ SkISize MSKPSrc::size(int i) const { return fReader.pageSize(i).toCeil(); }
 
 Error MSKPSrc::draw(SkCanvas* c) const { return this->draw(0, c); }
 Error MSKPSrc::draw(int i, SkCanvas* canvas) const {
-    std::unique_ptr<SkStreamAsset> stream(SkStream::NewFromFile(fPath.c_str()));
+    std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(fPath.c_str());
     if (!stream) {
         return SkStringPrintf("Unable to open file: %s", fPath.c_str());
     }
index 48ac577070b289a7ef31016b68c302fd32a5f6e8..7afce712401c98e78f560ad8f4853f7395123092 100644 (file)
@@ -40,11 +40,9 @@ public:
     virtual ~SkStream() {}
 
     /**
-     *  Attempts to open the specified file, and return a stream to it (using
-     *  mmap if available). On success, the caller is responsible for deleting.
-     *  On failure, returns NULL.
+     *  Attempts to open the specified file as a stream, returns nullptr on failure.
      */
-    static SkStreamAsset* NewFromFile(const char path[]);
+    static std::unique_ptr<SkStreamAsset> MakeFromFile(const char path[]);
 
     /** Reads or skips size number of bytes.
      *  If buffer == NULL, skip size bytes, return how many were skipped.
index b2c288ebcdd0a543e8e4711c4f77913aa1003ae7..61618697e8549fac9c9b98fbc34eb9c9d36cf7ed 100644 (file)
@@ -150,10 +150,9 @@ public:
 #endif
 
     /** Return a new typeface given font data and configuration. If the data
-        is not valid font data, returns nullptr. Ownership of the font data is
-        transferred, so the caller must not reference it again.
+        is not valid font data, returns nullptr.
     */
-    static sk_sp<SkTypeface> MakeFromFontData(SkFontData*);
+    static sk_sp<SkTypeface> MakeFromFontData(std::unique_ptr<SkFontData>);
 
     /** Write a unique signature to a stream, sufficient to reconstruct a
         typeface referencing the same font when Deserialize is called.
@@ -300,10 +299,9 @@ public:
     SkStreamAsset* openStream(int* ttcIndex) const;
 
     /**
-     *  Return the font data, or NULL on failure.
-     *  The caller is responsible for deleting the font data.
+     *  Return the font data, or nullptr on failure.
      */
-    SkFontData* createFontData() const;
+    std::unique_ptr<SkFontData> makeFontData() const;
 
     /**
      *  Return a scalercontext for the given descriptor. If this fails, then
@@ -361,7 +359,7 @@ protected:
 
     virtual SkStreamAsset* onOpenStream(int* ttcIndex) const = 0;
     // TODO: make pure virtual.
-    virtual SkFontData* onCreateFontData() const;
+    virtual std::unique_ptr<SkFontData> onMakeFontData() const;
 
     virtual void onGetFontDescriptor(SkFontDescriptor*, bool* isLocal) const = 0;
 
index a5b78c624178aa6ec60cf7c24533882c1aa3a589..afadeaaa9785fe55f4f7ce686878d5487aed8c0f 100644 (file)
@@ -150,11 +150,10 @@ public:
 
     /**
      *  Create a typeface from the specified font data.
-     *  Takes ownership of the font data, so the caller should not reference it again.
      *  Will return NULL if the typeface could not be created.
      *  The caller must call unref() on the returned object if it is not null.
      */
-    SkTypeface* createFromFontData(SkFontData*) const;
+    SkTypeface* createFromFontData(std::unique_ptr<SkFontData>) const;
 
     /**
      *  Create a typeface for the specified fileName and TTC index
@@ -192,7 +191,7 @@ protected:
     virtual SkTypeface* onCreateFromStream(SkStreamAsset*, int ttcIndex) const = 0;
     // TODO: make pure virtual.
     virtual SkTypeface* onCreateFromStream(SkStreamAsset*, const FontParameters&) const;
-    virtual SkTypeface* onCreateFromFontData(SkFontData*) const;
+    virtual SkTypeface* onCreateFromFontData(std::unique_ptr<SkFontData>) const;
     virtual SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const = 0;
 
     virtual SkTypeface* onLegacyCreateTypeface(const char familyName[], SkFontStyle) const = 0;
index 3d9aff44515facfe437ef8ae2ccc015ca1788705..a8f86de35bfe01778f8c183bfa1f97adf58c1caa 100644 (file)
@@ -65,20 +65,12 @@ static inline CGImageRef SkCreateCGImageRef(const SkBitmap& bm) {
 void SkCGDrawBitmap(CGContextRef, const SkBitmap&, float x, float y);
 
 /**
- *  Create an SkBitmap drawing of the encoded PDF document, returning true on
- *  success. Deletes the stream when finished.
- */
-bool SkPDFDocumentToBitmap(SkStream* stream, SkBitmap* output);
-
-/**
- *  Return a provider that wraps the specified stream. It will become the only
- *  owner of the stream, so the caller must stop referring to the stream.
- *
+ *  Return a provider that wraps the specified stream.
  *  When the provider is finally deleted, it will delete the stream.
  */
-CGDataProviderRef SkCreateDataProviderFromStream(SkStream*);
+CGDataProviderRef SkCreateDataProviderFromStream(std::unique_ptr<SkStream>);
 
-CGDataProviderRef SkCreateDataProviderFromData(SkData*);
+CGDataProviderRef SkCreateDataProviderFromData(sk_sp<SkData>);
 
 #endif  // defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
 #endif  // SkCGUtils_DEFINED
index d4a4cdd2887f150c405310f742b67f65c9ec494e..a3ec83afe673968e45d09a91824eb8552e8ce1de 100644 (file)
@@ -12,6 +12,8 @@
 #include "SkStream.h"
 #include "SkDOM.h"
 
+#include <memory>
+
 ///////////////////////////////////////////////////////////////////////////////
 
 class SkAnimatorView : public SkView {
@@ -50,8 +52,8 @@ void SkAnimatorView::setURIBase(const char dir[]) {
 }
 
 bool SkAnimatorView::decodeFile(const char path[]) {
-    SkAutoTDelete<SkStream> is(SkStream::NewFromFile(path));
-    return is.get() != nullptr && this->decodeStream(is);
+    std::unique_ptr<SkStream> is = SkStream::MakeFromFile(path);
+    return is && this->decodeStream(is.get());
 }
 
 bool SkAnimatorView::decodeMemory(const void* buffer, size_t size) {
index 5186da04d814ef4c2c876589385611c8efc066a9..066f877a9b50e9dcce864a3b5e6a302dfc2b92bf 100644 (file)
@@ -99,9 +99,9 @@ bool SkAnimateMaker::decodeURI(const char uri[]) {
 //  SkDebugf("animator decode %s\n", uri);
 
 //    SkStream* stream = SkStream::GetURIStream(fPrefix.c_str(), uri);
-    SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(uri));
-    if (stream.get()) {
-        bool success = decodeStream(stream);
+    std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(uri);
+    if (stream) {
+        bool success = decodeStream(stream.get());
         if (hasError() && fError.hasNoun() == false)
             fError.setNoun(uri);
         return success;
index ee75ab14affa8fc49f49df9e0ba6a200054338d6..c5aabbba4bdc62b7dd2ba81fed10ebf5e20e6262 100644 (file)
@@ -82,10 +82,10 @@ bool SkAnimator::decodeURI(const char uri[]) {
 //  SkDebugf("animator decode %s\n", uri);
 
 //    SkStream* stream = SkStream::GetURIStream(fMaker->fPrefix.c_str(), uri);
-    SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(uri));
-    if (stream.get()) {
+    std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(uri);
+    if (stream) {
         this->setURIBase(uri);
-        return decodeStream(stream);
+        return decodeStream(stream.get());
     } else {
         return false;
     }
index 85629efa7ec640bf53d5a6d9533b0916c8d3afd7..73ea2058ce79c618022334b970b6d702035e3f67 100644 (file)
@@ -6,6 +6,7 @@
  */
 
 #include "SkFontDescriptor.h"
+#include "SkMakeUnique.h"
 #include "SkStream.h"
 #include "SkData.h"
 
@@ -106,8 +107,8 @@ bool SkFontDescriptor::Deserialize(SkStream* stream, SkFontDescriptor* result) {
     if (length > 0) {
         sk_sp<SkData> data(SkData::MakeUninitialized(length));
         if (stream->read(data->writable_data(), length) == length) {
-            result->fFontData.reset(new SkFontData(new SkMemoryStream(data),
-                                                   index, axis, axisCount));
+            result->fFontData = skstd::make_unique<SkFontData>(
+                skstd::make_unique<SkMemoryStream>(data), index, axis, axisCount);
         } else {
             SkDEBUGFAIL("Could not read font data");
             return false;
@@ -138,10 +139,10 @@ void SkFontDescriptor::serialize(SkWStream* stream) {
     stream->writePackedUInt(kSentinel);
 
     if (fFontData.get() && fFontData->hasStream()) {
-        SkAutoTDelete<SkStreamAsset> fontData(fFontData->detachStream());
-        size_t length = fontData->getLength();
+        std::unique_ptr<SkStreamAsset> fontStream = fFontData->detachStream();
+        size_t length = fontStream->getLength();
         stream->writePackedUInt(length);
-        stream->writeStream(fontData, length);
+        stream->writeStream(fontStream.get(), length);
     } else {
         stream->writePackedUInt(0);
     }
index cb8d2f4f03562484755e4147ca7b3fd85e4adfab..de1462177bc949c3d3a8975a9279e71a2d85657a 100644 (file)
@@ -15,9 +15,9 @@
 
 class SkFontData {
 public:
-    /** This takes ownership of 'stream'. Makes a copy of the data in 'axis'. */
-    SkFontData(SkStreamAsset* stream, int index, const SkFixed axis[], int axisCount)
-        : fStream(stream), fIndex(index), fAxisCount(axisCount), fAxis(axisCount)
+    /** Makes a copy of the data in 'axis'. */
+    SkFontData(std::unique_ptr<SkStreamAsset> stream, int index, const SkFixed axis[],int axisCount)
+        : fStream(std::move(stream)), fIndex(index), fAxisCount(axisCount), fAxis(axisCount)
     {
         for (int i = 0; i < axisCount; ++i) {
             fAxis[i] = axis[i];
@@ -34,15 +34,15 @@ public:
         }
     }
     bool hasStream() const { return fStream.get() != nullptr; }
-    SkStreamAsset* duplicateStream() const { return fStream->duplicate(); }
-    SkStreamAsset* detachStream() { return fStream.release(); }
+    std::unique_ptr<SkStreamAsset> detachStream() { return std::move(fStream); }
     SkStreamAsset* getStream() { return fStream.get(); }
+    SkStreamAsset const* getStream() const { return fStream.get(); }
     int getIndex() const { return fIndex; }
     int getAxisCount() const { return fAxisCount; }
     const SkFixed* getAxis() const { return fAxis.get(); }
 
 private:
-    SkAutoTDelete<SkStreamAsset> fStream;
+    std::unique_ptr<SkStreamAsset> fStream;
     int fIndex;
     int fAxisCount;
     SkAutoSTMalloc<4, SkFixed> fAxis;
@@ -63,20 +63,19 @@ public:
     const char* getFullName() const { return fFullName.c_str(); }
     const char* getPostscriptName() const { return fPostscriptName.c_str(); }
     bool hasFontData() const { return fFontData.get() != nullptr; }
-    SkFontData* detachFontData() { return fFontData.release(); }
+    std::unique_ptr<SkFontData> detachFontData() { return std::move(fFontData); }
 
     void setFamilyName(const char* name) { fFamilyName.set(name); }
     void setFullName(const char* name) { fFullName.set(name); }
     void setPostscriptName(const char* name) { fPostscriptName.set(name); }
-    /** Set the font data only if it is necessary for serialization.
-     *  This method takes ownership of the font data. */
-    void setFontData(SkFontData* data) { fFontData.reset(data); }
+    /** Set the font data only if it is necessary for serialization. */
+    void setFontData(std::unique_ptr<SkFontData> data) { fFontData = std::move(data); }
 
 private:
     SkString fFamilyName;
     SkString fFullName;
     SkString fPostscriptName;
-    SkAutoTDelete<SkFontData> fFontData;
+    std::unique_ptr<SkFontData> fFontData;
 
     SkFontStyle fStyle;
 };
index 0f0066792636837af660e610ab96ede11b5d7bb2..57f82b03ba28201a21959f67ace59c7787191bc7 100644 (file)
@@ -139,11 +139,11 @@ SkTypeface* SkFontMgr::createFromStream(SkStreamAsset* stream, const FontParamet
     return this->onCreateFromStream(stream, params);
 }
 
-SkTypeface* SkFontMgr::createFromFontData(SkFontData* data) const {
+SkTypeface* SkFontMgr::createFromFontData(std::unique_ptr<SkFontData> data) const {
     if (nullptr == data) {
         return nullptr;
     }
-    return this->onCreateFromFontData(data);
+    return this->onCreateFromFontData(std::move(data));
 }
 
 // This implementation is temporary until it can be made pure virtual.
@@ -152,10 +152,8 @@ SkTypeface* SkFontMgr::onCreateFromStream(SkStreamAsset* stream, const FontParam
 }
 
 // This implementation is temporary until it can be made pure virtual.
-SkTypeface* SkFontMgr::onCreateFromFontData(SkFontData* data) const {
-    SkTypeface* ret = this->createFromStream(data->detachStream(), data->getIndex());
-    delete data;
-    return ret;
+SkTypeface* SkFontMgr::onCreateFromFontData(std::unique_ptr<SkFontData> data) const {
+    return this->createFromStream(data->detachStream().release(), data->getIndex());
 }
 
 SkTypeface* SkFontMgr::createFromFile(const char path[], int ttcIndex) const {
index 48eb92cae5c5631c1e4546f95e471040fa2297e9..e7b3a7a7e5777ff40de437a8628a441921b1b259 100644 (file)
@@ -10,6 +10,7 @@
 #include "SkStreamPriv.h"
 #include "SkData.h"
 #include "SkFixed.h"
+#include "SkMakeUnique.h"
 #include "SkString.h"
 #include "SkOSFile.h"
 #include "SkTypes.h"
@@ -854,20 +855,18 @@ static sk_sp<SkData> mmap_filename(const char path[]) {
     return data;
 }
 
-SkStreamAsset* SkStream::NewFromFile(const char path[]) {
+std::unique_ptr<SkStreamAsset> SkStream::MakeFromFile(const char path[]) {
     auto data(mmap_filename(path));
     if (data) {
-        return new SkMemoryStream(std::move(data));
+        return skstd::make_unique<SkMemoryStream>(std::move(data));
     }
 
-    // If we get here, then our attempt at using mmap failed, so try normal
-    // file access.
-    SkFILEStream* stream = new SkFILEStream(path);
+    // If we get here, then our attempt at using mmap failed, so try normal file access.
+    auto stream = skstd::make_unique<SkFILEStream>(path);
     if (!stream->isValid()) {
-        delete stream;
-        stream = nullptr;
+        return nullptr;
     }
-    return stream;
+    return std::move(stream);
 }
 
 // Declared in SkStreamPriv.h:
index 0c960d591510a4b7992164208ac985587ec8b482..3c4f5cb7e7929eb856f35713437ff4450ba52777 100644 (file)
@@ -9,6 +9,7 @@
 #include "SkEndian.h"
 #include "SkFontDescriptor.h"
 #include "SkFontMgr.h"
+#include "SkMakeUnique.h"
 #include "SkMutex.h"
 #include "SkOTTable_OS_2.h"
 #include "SkOnce.h"
@@ -150,9 +151,9 @@ sk_sp<SkTypeface> SkTypeface::MakeFromStream(SkStreamAsset* stream, int index) {
     return sk_sp<SkTypeface>(fm->createFromStream(stream, index));
 }
 
-sk_sp<SkTypeface> SkTypeface::MakeFromFontData(SkFontData* data) {
+sk_sp<SkTypeface> SkTypeface::MakeFromFontData(std::unique_ptr<SkFontData> data) {
     SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault());
-    return sk_sp<SkTypeface>(fm->createFromFontData(data));
+    return sk_sp<SkTypeface>(fm->createFromFontData(std::move(data)));
 }
 
 sk_sp<SkTypeface> SkTypeface::MakeFromFile(const char path[], int index) {
@@ -173,7 +174,7 @@ void SkTypeface::serialize(SkWStream* wstream) const {
 
     // Embed font data if it's a local font.
     if (isLocal && !desc.hasFontData()) {
-        desc.setFontData(this->onCreateFontData());
+        desc.setFontData(this->onMakeFontData());
     }
     desc.serialize(wstream);
 }
@@ -188,9 +189,9 @@ sk_sp<SkTypeface> SkTypeface::MakeDeserialize(SkStream* stream) {
         return nullptr;
     }
 
-    SkFontData* data = desc.detachFontData();
+    std::unique_ptr<SkFontData> data = desc.detachFontData();
     if (data) {
-        sk_sp<SkTypeface> typeface(SkTypeface::MakeFromFontData(data));
+        sk_sp<SkTypeface> typeface(SkTypeface::MakeFromFontData(std::move(data)));
         if (typeface) {
             return typeface;
         }
@@ -227,15 +228,15 @@ SkStreamAsset* SkTypeface::openStream(int* ttcIndex) const {
     return this->onOpenStream(ttcIndex);
 }
 
-SkFontData* SkTypeface::createFontData() const {
-    return this->onCreateFontData();
+std::unique_ptr<SkFontData> SkTypeface::makeFontData() const {
+    return this->onMakeFontData();
 }
 
 // This implementation is temporary until this method can be made pure virtual.
-SkFontData* SkTypeface::onCreateFontData() const {
+std::unique_ptr<SkFontData> SkTypeface::onMakeFontData() const {
     int index;
-    SkAutoTDelete<SkStreamAsset> stream(this->onOpenStream(&index));
-    return new SkFontData(stream.release(), index, nullptr, 0);
+    std::unique_ptr<SkStreamAsset> stream(this->onOpenStream(&index));
+    return skstd::make_unique<SkFontData>(std::move(stream), index, nullptr, 0);
 };
 
 int SkTypeface::charsToGlyphs(const void* chars, Encoding encoding,
index bf94f281c8ceff2c816490876239386062ec1cbf..a0a37dcffa36abe038b4f43b28adb1bebce6ec65 100644 (file)
@@ -89,6 +89,6 @@ SkMovie* SkMovie::DecodeMemory(const void* data, size_t length) {
 }
 
 SkMovie* SkMovie::DecodeFile(const char path[]) {
-    SkAutoTDelete<SkStreamRewindable> stream(SkStream::NewFromFile(path));
-    return stream.get() ? SkMovie::DecodeStream(stream) : nullptr;
+    std::unique_ptr<SkStreamRewindable> stream = SkStream::MakeFromFile(path);
+    return stream ? SkMovie::DecodeStream(stream.get()) : nullptr;
 }
index 8fe9898bc719e9cf4be76663ad3769d3caf55422..cdd420c38c675b879683c5cd5227e06fe2b36766 100644 (file)
@@ -702,7 +702,7 @@ bool SkFontConfigInterfaceDirect::matchFamilyName(const char familyName[],
 }
 
 SkStreamAsset* SkFontConfigInterfaceDirect::openStream(const FontIdentity& identity) {
-    return SkStream::NewFromFile(identity.fString.c_str());
+    return SkStream::MakeFromFile(identity.fString.c_str()).release();
 }
 
 ///////////////////////////////////////////////////////////////////////////////
index b636052c471829e73f6b462aadd0b9c86ffc9f11..1a59d7c1ed303f3ed0be2f495f0f88cfda434fee 100644 (file)
@@ -60,7 +60,7 @@ protected:
     void onGetFamilyName(SkString* familyName) const override { *familyName = fFamilyName; }
     void onGetFontDescriptor(SkFontDescriptor*, bool*) const override;
     SkStreamAsset* onOpenStream(int* ttcIndex) const override;
-    SkFontData* onCreateFontData() const override;
+    std::unique_ptr<SkFontData> onMakeFontData() const override;
 
 private:
     typedef SkTypeface_FreeType INHERITED;
index 84a74af469b35b81f01d9fb168982065f478092b..71ce865f080c78698f5d61c292ffd4c4aa87fe63 100644 (file)
@@ -234,12 +234,11 @@ struct SkFaceRec {
     SkFaceRec* fNext;
     FT_Face fFace;
     FT_StreamRec fFTStream;
-    SkAutoTDelete<SkStreamAsset> fSkStream;
+    std::unique_ptr<SkStreamAsset> fSkStream;
     uint32_t fRefCnt;
     uint32_t fFontID;
 
-    // assumes ownership of the stream, will delete when its done
-    SkFaceRec(SkStreamAsset* strm, uint32_t fontID);
+    SkFaceRec(std::unique_ptr<SkStreamAsset> stream, uint32_t fontID);
 };
 
 extern "C" {
@@ -262,12 +261,12 @@ extern "C" {
     static void sk_ft_stream_close(FT_Stream) {}
 }
 
-SkFaceRec::SkFaceRec(SkStreamAsset* stream, uint32_t fontID)
-        : fNext(nullptr), fSkStream(stream), fRefCnt(1), fFontID(fontID)
+SkFaceRec::SkFaceRec(std::unique_ptr<SkStreamAsset> stream, uint32_t fontID)
+        : fNext(nullptr), fSkStream(std::move(stream)), fRefCnt(1), fFontID(fontID)
 {
     sk_bzero(&fFTStream, sizeof(fFTStream));
     fFTStream.size = fSkStream->getLength();
-    fFTStream.descriptor.pointer = fSkStream;
+    fFTStream.descriptor.pointer = fSkStream.get();
     fFTStream.read  = sk_ft_stream_io;
     fFTStream.close = sk_ft_stream_close;
 }
@@ -319,12 +318,11 @@ static FT_Face ref_ft_face(const SkTypeface* typeface) {
         rec = rec->fNext;
     }
 
-    SkAutoTDelete<SkFontData> data(typeface->createFontData());
+    std::unique_ptr<SkFontData> data = typeface->makeFontData();
     if (nullptr == data || !data->hasStream()) {
         return nullptr;
     }
 
-    // this passes ownership of stream to the rec
     rec = new SkFaceRec(data->detachStream(), fontID);
 
     FT_Open_Args args;
@@ -1564,7 +1562,7 @@ SkTypeface_FreeType::Scanner::~Scanner() {
     }
 }
 
-FT_Face SkTypeface_FreeType::Scanner::openFace(SkStream* stream, int ttcIndex,
+FT_Face SkTypeface_FreeType::Scanner::openFace(SkStreamAsset* stream, int ttcIndex,
                                                FT_Stream ftStream) const
 {
     if (fLibrary == nullptr) {
@@ -1598,7 +1596,7 @@ FT_Face SkTypeface_FreeType::Scanner::openFace(SkStream* stream, int ttcIndex,
     return face;
 }
 
-bool SkTypeface_FreeType::Scanner::recognizedFont(SkStream* stream, int* numFaces) const {
+bool SkTypeface_FreeType::Scanner::recognizedFont(SkStreamAsset* stream, int* numFaces) const {
     SkAutoMutexAcquire libraryLock(fLibraryMutex);
 
     FT_StreamRec streamRec;
@@ -1615,7 +1613,7 @@ bool SkTypeface_FreeType::Scanner::recognizedFont(SkStream* stream, int* numFace
 
 #include "SkTSearch.h"
 bool SkTypeface_FreeType::Scanner::scanFont(
-    SkStream* stream, int ttcIndex,
+    SkStreamAsset* stream, int ttcIndex,
     SkString* name, SkFontStyle* style, bool* isFixedPitch, AxisDefinitions* axes) const
 {
     SkAutoMutexAcquire libraryLock(fLibraryMutex);
index 6b50af27ab3acbdd3d62e520ec63c7ae68b4b28d..21e7748662822aa5838c0d776147bb8fc40ac33b 100644 (file)
@@ -53,8 +53,8 @@ public:
             SkFixed fMaximum;
         };
         using AxisDefinitions = SkSTArray<4, AxisDefinition, true>;
-        bool recognizedFont(SkStream* stream, int* numFonts) const;
-        bool scanFont(SkStream* stream, int ttcIndex,
+        bool recognizedFont(SkStreamAsset* stream, int* numFonts) const;
+        bool scanFont(SkStreamAsset* stream, int ttcIndex,
                       SkString* name, SkFontStyle* style, bool* isFixedPitch,
                       AxisDefinitions* axes) const;
         static void computeAxisValues(
@@ -64,7 +64,7 @@ public:
             const SkString& name);
 
     private:
-        FT_Face openFace(SkStream* stream, int ttcIndex, FT_Stream ftStream) const;
+        FT_Face openFace(SkStreamAsset* stream, int ttcIndex, FT_Stream ftStream) const;
         FT_Library fLibrary;
         mutable SkMutex fLibraryMutex;
     };
index 0c2e5c26281851030e53f41930f3406722310c90..7d1ef750bd5d3f74336fe7e0a14d25c942103f53 100644 (file)
@@ -28,6 +28,7 @@
 #include "SkFontDescriptor.h"
 #include "SkFontMgr.h"
 #include "SkGlyph.h"
+#include "SkMakeUnique.h"
 #include "SkMaskGamma.h"
 #include "SkMathPriv.h"
 #include "SkMutex.h"
@@ -511,7 +512,7 @@ public:
 protected:
     int onGetUPEM() const override;
     SkStreamAsset* onOpenStream(int* ttcIndex) const override;
-    SkFontData* onCreateFontData() const override;
+    std::unique_ptr<SkFontData> onMakeFontData() const override;
     void onGetFamilyName(SkString* familyName) const override;
     SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override;
     int onGetTableTags(SkFontTableTag tags[]) const override;
@@ -1885,16 +1886,17 @@ static bool get_variations(CTFontRef fFontRef, CFIndex* cgAxisCount,
 
     return true;
 }
-SkFontData* SkTypeface_Mac::onCreateFontData() const {
+std::unique_ptr<SkFontData> SkTypeface_Mac::onMakeFontData() const {
     int index;
-    SkAutoTDelete<SkStreamAsset> stream(this->onOpenStream(&index));
+    std::unique_ptr<SkStreamAsset> stream(this->onOpenStream(&index));
 
     CFIndex cgAxisCount;
     SkAutoSTMalloc<4, SkFixed> axisValues;
     if (get_variations(fFontRef, &cgAxisCount, &axisValues)) {
-        return new SkFontData(stream.release(), index, axisValues.get(), cgAxisCount);
+        return skstd::make_unique<SkFontData>(std::move(stream), index,
+                                              axisValues.get(), cgAxisCount);
     }
-    return new SkFontData(stream.release(), index, nullptr, 0);
+    return skstd::make_unique<SkFontData>(std::move(stream), index, nullptr, 0);
 }
 
 ///////////////////////////////////////////////////////////////////////////////
@@ -2367,15 +2369,16 @@ protected:
     }
 
     SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const override {
-        AutoCFRelease<CGDataProviderRef> pr(SkCreateDataProviderFromData(data));
+        AutoCFRelease<CGDataProviderRef> pr(SkCreateDataProviderFromData(sk_ref_sp(data)));
         if (nullptr == pr) {
             return nullptr;
         }
         return create_from_dataProvider(pr);
     }
 
-    SkTypeface* onCreateFromStream(SkStreamAsset* stream, int ttcIndex) const override {
-        AutoCFRelease<CGDataProviderRef> pr(SkCreateDataProviderFromStream(stream));
+    SkTypeface* onCreateFromStream(SkStreamAsset* bareStream, int ttcIndex) const override {
+        std::unique_ptr<SkStreamAsset> stream(bareStream);
+        AutoCFRelease<CGDataProviderRef> pr(SkCreateDataProviderFromStream(std::move(stream)));
         if (nullptr == pr) {
             return nullptr;
         }
@@ -2493,8 +2496,9 @@ protected:
         }
         return dict;
     }
-    SkTypeface* onCreateFromStream(SkStreamAsset* s, const FontParameters& params) const override {
-        AutoCFRelease<CGDataProviderRef> provider(SkCreateDataProviderFromStream(s));
+    SkTypeface* onCreateFromStream(SkStreamAsset* bs, const FontParameters& params) const override {
+        std::unique_ptr<SkStreamAsset> s(bs);
+        AutoCFRelease<CGDataProviderRef> provider(SkCreateDataProviderFromStream(std::move(s)));
         if (nullptr == provider) {
             return nullptr;
         }
@@ -2574,10 +2578,9 @@ protected:
         }
         return dict;
     }
-    SkTypeface* onCreateFromFontData(SkFontData* data) const override {
-        SkAutoTDelete<SkFontData> fontData(data);
-        SkStreamAsset* stream = fontData->detachStream();
-        AutoCFRelease<CGDataProviderRef> provider(SkCreateDataProviderFromStream(stream));
+    SkTypeface* onCreateFromFontData(std::unique_ptr<SkFontData> fontData) const override {
+        AutoCFRelease<CGDataProviderRef> provider(
+                SkCreateDataProviderFromStream(fontData->detachStream()));
         if (nullptr == provider) {
             return nullptr;
         }
@@ -2586,7 +2589,7 @@ protected:
             return nullptr;
         }
 
-        AutoCFRelease<CFDictionaryRef> cgVariations(get_axes(cg, fontData));
+        AutoCFRelease<CFDictionaryRef> cgVariations(get_axes(cg, fontData.get()));
         // The CGFontRef returned by CGFontCreateCopyWithVariations when the passed CGFontRef was
         // created from a data provider does not appear to have any ownership of the underlying
         // data. The original CGFontRef must be kept alive until the copy will no longer be used.
index ca80baec4ff4b83a235464d88773cc2616b5d034..0e878bee0360d02dc92ad34e5ed4641800d64126 100644 (file)
@@ -2463,7 +2463,7 @@ protected:
 
     SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const override {
         // could be in base impl
-        return this->createFromStream(SkStream::NewFromFile(path));
+        return this->createFromStream(SkStream::MakeFromFile(path).release());
     }
 
     SkTypeface* onLegacyCreateTypeface(const char familyName[], SkFontStyle style) const override {
index c432a6b93fe61388dd531993295bf201bdf46c47..a6a055a9d2f24f4aa7723a2797fae5df460e8661 100644 (file)
@@ -10,6 +10,7 @@
 #include "SkFontDescriptor.h"
 #include "SkFontMgr.h"
 #include "SkFontStyle.h"
+#include "SkMakeUnique.h"
 #include "SkMutex.h"
 #include "SkString.h"
 #include "SkTypeface.h"
@@ -30,13 +31,14 @@ SkStreamAsset* SkTypeface_FCI::onOpenStream(int* ttcIndex) const {
     return fFCI->openStream(this->getIdentity());
 }
 
-SkFontData* SkTypeface_FCI::onCreateFontData() const {
+std::unique_ptr<SkFontData> SkTypeface_FCI::onMakeFontData() const {
     if (fFontData) {
-        return new SkFontData(*fFontData.get());
+        return skstd::make_unique<SkFontData>(*fFontData);
     }
 
     const SkFontConfigInterface::FontIdentity& id = this->getIdentity();
-    return new SkFontData( fFCI->openStream(id), id.fTTCIndex, nullptr, 0);
+    return skstd::make_unique<SkFontData>(std::unique_ptr<SkStreamAsset>(fFCI->openStream(id)),
+                                          id.fTTCIndex, nullptr, 0);
 }
 
 void SkTypeface_FCI::onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocalStream) const {
@@ -199,7 +201,7 @@ protected:
     SkTypeface* onCreateFromData(SkData*, int ttcIndex) const override { return nullptr; }
 
     SkTypeface* onCreateFromStream(SkStreamAsset* bareStream, int ttcIndex) const override {
-        SkAutoTDelete<SkStreamAsset> stream(bareStream);
+        std::unique_ptr<SkStreamAsset> stream(bareStream);
         const size_t length = stream->getLength();
         if (!length) {
             return nullptr;
@@ -211,18 +213,17 @@ protected:
         // TODO should the caller give us the style or should we get it from freetype?
         SkFontStyle style;
         bool isFixedPitch = false;
-        if (!fScanner.scanFont(stream, 0, nullptr, &style, &isFixedPitch, nullptr)) {
+        if (!fScanner.scanFont(stream.get(), 0, nullptr, &style, &isFixedPitch, nullptr)) {
             return nullptr;
         }
 
-        std::unique_ptr<SkFontData> fontData(new SkFontData(stream.release(), ttcIndex,
-                                                            nullptr, 0));
+        auto fontData = skstd::make_unique<SkFontData>(std::move(stream), ttcIndex, nullptr, 0);
         return SkTypeface_FCI::Create(std::move(fontData), style, isFixedPitch);
     }
 
     SkTypeface* onCreateFromStream(SkStreamAsset* s, const FontParameters& params) const override {
         using Scanner = SkTypeface_FreeType::Scanner;
-        SkAutoTDelete<SkStreamAsset> stream(s);
+        std::unique_ptr<SkStreamAsset> stream(s);
         const size_t length = stream->getLength();
         if (!length) {
             return nullptr;
@@ -235,8 +236,8 @@ protected:
         SkFontStyle style;
         SkString name;
         Scanner::AxisDefinitions axisDefinitions;
-        if (!fScanner.scanFont(stream, params.getCollectionIndex(), &name, &style, &isFixedPitch,
-                               &axisDefinitions))
+        if (!fScanner.scanFont(stream.get(), params.getCollectionIndex(),
+                               &name, &style, &isFixedPitch, &axisDefinitions))
         {
             return nullptr;
         }
@@ -246,15 +247,15 @@ protected:
         SkAutoSTMalloc<4, SkFixed> axisValues(axisDefinitions.count());
         Scanner::computeAxisValues(axisDefinitions, paramAxes, paramAxisCount, axisValues, name);
 
-        std::unique_ptr<SkFontData> fontData(new SkFontData(stream.release(),
-                                                            params.getCollectionIndex(),
-                                                            axisValues.get(),
-                                                            axisDefinitions.count()));
+        auto fontData = skstd::make_unique<SkFontData>(std::move(stream),
+                                                       params.getCollectionIndex(),
+                                                       axisValues.get(),
+                                                       axisDefinitions.count());
         return SkTypeface_FCI::Create(std::move(fontData), style, isFixedPitch);
     }
 
     SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const override {
-        SkAutoTDelete<SkStreamAsset> stream(SkStream::NewFromFile(path));
+        std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(path);
         return stream.get() ? this->createFromStream(stream.release(), ttcIndex) : nullptr;
     }
 
index 8a1916a59b74fe01563d8f62343f25e661c0441c..3a84ecad8b1888df441369b89345687b0ee6e07c 100644 (file)
@@ -15,6 +15,7 @@
 #include "SkFontMgr_android.h"
 #include "SkFontMgr_android_parser.h"
 #include "SkFontStyle.h"
+#include "SkMakeUnique.h"
 #include "SkOSFile.h"
 #include "SkPaint.h"
 #include "SkRefCnt.h"
@@ -73,12 +74,12 @@ public:
         }
     }
 
-    SkStreamAsset* createStream() const {
+    std::unique_ptr<SkStreamAsset> makeStream() const {
         if (fFile) {
             sk_sp<SkData> data(SkData::MakeFromFILE(fFile));
-            return data ? new SkMemoryStream(std::move(data)) : nullptr;
+            return data ? skstd::make_unique<SkMemoryStream>(std::move(data)) : nullptr;
         }
-        return SkStream::NewFromFile(fPathName.c_str());
+        return SkStream::MakeFromFile(fPathName.c_str());
     }
 
     virtual void onGetFontDescriptor(SkFontDescriptor* desc, bool* serialize) const override {
@@ -90,10 +91,11 @@ public:
     }
     SkStreamAsset* onOpenStream(int* ttcIndex) const override {
         *ttcIndex = fIndex;
-        return this->createStream();
+        return this->makeStream().release();
     }
-    SkFontData* onCreateFontData() const override {
-        return new SkFontData(this->createStream(), fIndex, fAxes.begin(), fAxes.count());
+    std::unique_ptr<SkFontData> onMakeFontData() const override {
+        return skstd::make_unique<SkFontData>(this->makeStream(), fIndex,
+                                              fAxes.begin(), fAxes.count());
     }
 
     const SkString fPathName;
@@ -108,12 +110,12 @@ public:
 
 class SkTypeface_AndroidStream : public SkTypeface_Android {
 public:
-    SkTypeface_AndroidStream(SkFontData* data,
+    SkTypeface_AndroidStream(std::unique_ptr<SkFontData> data,
                              const SkFontStyle& style,
                              bool isFixedPitch,
                              const SkString& familyName)
         : INHERITED(style, isFixedPitch, familyName)
-        , fData(data)
+        , fData(std::move(data))
     { }
 
     virtual void onGetFontDescriptor(SkFontDescriptor* desc,
@@ -126,15 +128,15 @@ public:
 
     SkStreamAsset* onOpenStream(int* ttcIndex) const override {
         *ttcIndex = fData->getIndex();
-        return fData->duplicateStream();
+        return fData->getStream()->duplicate();
     }
 
-    SkFontData* onCreateFontData() const override {
-        return new SkFontData(*fData.get());
+    std::unique_ptr<SkFontData> onMakeFontData() const override {
+        return skstd::make_unique<SkFontData>(*fData);
     }
 
 private:
-    const SkAutoTDelete<const SkFontData> fData;
+    const std::unique_ptr<const SkFontData> fData;
     typedef SkTypeface_Android INHERITED;
 };
 
@@ -155,8 +157,8 @@ public:
             SkString pathName(family.fBasePath);
             pathName.append(fontFile.fFileName);
 
-            SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(pathName.c_str()));
-            if (!stream.get()) {
+            std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(pathName.c_str());
+            if (!stream) {
                 SkDEBUGF(("Requested font file %s does not exist or cannot be opened.\n",
                           pathName.c_str()));
                 continue;
@@ -410,31 +412,31 @@ protected:
     }
 
     SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const override {
-        SkAutoTDelete<SkStreamAsset> stream(SkStream::NewFromFile(path));
+        std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(path);
         return stream.get() ? this->createFromStream(stream.release(), ttcIndex) : nullptr;
     }
 
     SkTypeface* onCreateFromStream(SkStreamAsset* bareStream, int ttcIndex) const override {
-        SkAutoTDelete<SkStreamAsset> stream(bareStream);
+        std::unique_ptr<SkStreamAsset> stream(bareStream);
         bool isFixedPitch;
         SkFontStyle style;
         SkString name;
-        if (!fScanner.scanFont(stream, ttcIndex, &name, &style, &isFixedPitch, nullptr)) {
+        if (!fScanner.scanFont(stream.get(), ttcIndex, &name, &style, &isFixedPitch, nullptr)) {
             return nullptr;
         }
-        SkFontData* data(new SkFontData(stream.release(), ttcIndex, nullptr, 0));
-        return new SkTypeface_AndroidStream(data, style, isFixedPitch, name);
+        auto data = skstd::make_unique<SkFontData>(std::move(stream), ttcIndex, nullptr, 0);
+        return new SkTypeface_AndroidStream(std::move(data), style, isFixedPitch, name);
     }
 
     SkTypeface* onCreateFromStream(SkStreamAsset* s, const FontParameters& params) const override {
         using Scanner = SkTypeface_FreeType::Scanner;
-        SkAutoTDelete<SkStreamAsset> stream(s);
+        std::unique_ptr<SkStreamAsset> stream(s);
         bool isFixedPitch;
         SkFontStyle style;
         SkString name;
         Scanner::AxisDefinitions axisDefinitions;
-        if (!fScanner.scanFont(stream, params.getCollectionIndex(), &name, &style, &isFixedPitch,
-                               &axisDefinitions))
+        if (!fScanner.scanFont(stream.get(), params.getCollectionIndex(),
+                               &name, &style, &isFixedPitch, &axisDefinitions))
         {
             return nullptr;
         }
@@ -444,12 +446,12 @@ protected:
         SkAutoSTMalloc<4, SkFixed> axisValues(axisDefinitions.count());
         Scanner::computeAxisValues(axisDefinitions, paramAxes, paramAxisCount, axisValues, name);
 
-        SkFontData* data(new SkFontData(stream.release(), params.getCollectionIndex(),
-                                        axisValues.get(), axisDefinitions.count()));
-        return new SkTypeface_AndroidStream(data, style, isFixedPitch, name);
+        auto data = skstd::make_unique<SkFontData>(std::move(stream), params.getCollectionIndex(),
+                                                   axisValues.get(), axisDefinitions.count());
+        return new SkTypeface_AndroidStream(std::move(data), style, isFixedPitch, name);
     }
 
-    SkTypeface* onCreateFromFontData(SkFontData* data) const override {
+    SkTypeface* onCreateFromFontData(std::unique_ptr<SkFontData> data) const override {
         SkStreamAsset* stream(data->getStream());
         bool isFixedPitch;
         SkFontStyle style;
@@ -457,7 +459,7 @@ protected:
         if (!fScanner.scanFont(stream, data->getIndex(), &name, &style, &isFixedPitch, nullptr)) {
             return nullptr;
         }
-        return new SkTypeface_AndroidStream(data, style, isFixedPitch, name);
+        return new SkTypeface_AndroidStream(std::move(data), style, isFixedPitch, name);
     }
 
     SkTypeface* onLegacyCreateTypeface(const char familyName[], SkFontStyle style) const override {
index 97489de50d6fba69b71303722349a2d304379941..9a8aa4946fcb8cc3f900a16eba1819ab067bdcde 100644 (file)
@@ -10,6 +10,7 @@
 #include "SkFontMgr.h"
 #include "SkFontMgr_custom.h"
 #include "SkFontStyle.h"
+#include "SkMakeUnique.h"
 #include "SkOSFile.h"
 #include "SkRefCnt.h"
 #include "SkStream.h"
@@ -84,15 +85,15 @@ public:
 protected:
     SkStreamAsset* onOpenStream(int* ttcIndex) const override {
         *ttcIndex = fData->getIndex();
-        return fData->duplicateStream();
+        return fData->getStream()->duplicate();
     }
 
-    SkFontData* onCreateFontData() const override {
-        return new SkFontData(*fData.get());
+    std::unique_ptr<SkFontData> onMakeFontData() const override {
+        return skstd::make_unique<SkFontData>(*fData);
     }
 
 private:
-    std::unique_ptr<const SkFontData> fData;
+    const std::unique_ptr<const SkFontData> fData;
 
     typedef SkTypeface_Custom INHERITED;
 };
@@ -109,7 +110,7 @@ public:
 protected:
     SkStreamAsset* onOpenStream(int* ttcIndex) const override {
         *ttcIndex = this->getIndex();
-        return SkStream::NewFromFile(fPath.c_str());
+        return SkStream::MakeFromFile(fPath.c_str()).release();
     }
 
 private:
@@ -270,13 +271,13 @@ protected:
 
     SkTypeface* onCreateFromStream(SkStreamAsset* s, const FontParameters& params) const override {
         using Scanner = SkTypeface_FreeType::Scanner;
-        SkAutoTDelete<SkStreamAsset> stream(s);
+        std::unique_ptr<SkStreamAsset> stream(s);
         bool isFixedPitch;
         SkFontStyle style;
         SkString name;
         Scanner::AxisDefinitions axisDefinitions;
-        if (!fScanner.scanFont(stream, params.getCollectionIndex(), &name, &style, &isFixedPitch,
-                               &axisDefinitions))
+        if (!fScanner.scanFont(stream.get(), params.getCollectionIndex(),
+                               &name, &style, &isFixedPitch, &axisDefinitions))
         {
             return nullptr;
         }
@@ -286,13 +287,12 @@ protected:
         SkAutoSTMalloc<4, SkFixed> axisValues(axisDefinitions.count());
         Scanner::computeAxisValues(axisDefinitions, paramAxes, paramAxisCount, axisValues, name);
 
-        std::unique_ptr<SkFontData> data(new SkFontData(stream.release(),
-                                                        params.getCollectionIndex(),
-                                                        axisValues.get(), axisDefinitions.count()));
+        auto data = skstd::make_unique<SkFontData>(std::move(stream), params.getCollectionIndex(),
+                                                   axisValues.get(), axisDefinitions.count());
         return new SkTypeface_Stream(std::move(data), style, isFixedPitch, false, name);
     }
 
-    SkTypeface* onCreateFromFontData(SkFontData* data) const override {
+    SkTypeface* onCreateFromFontData(std::unique_ptr<SkFontData> data) const override {
         bool isFixedPitch;
         SkFontStyle style;
         SkString name;
@@ -301,12 +301,11 @@ protected:
         {
             return nullptr;
         }
-        std::unique_ptr<SkFontData> unique_data(data);
-        return new SkTypeface_Stream(std::move(unique_data), style, isFixedPitch, false, name);
+        return new SkTypeface_Stream(std::move(data), style, isFixedPitch, false, name);
     }
 
     SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const override {
-        SkAutoTDelete<SkStreamAsset> stream(SkStream::NewFromFile(path));
+        std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(path);
         return stream.get() ? this->createFromStream(stream.release(), ttcIndex) : nullptr;
     }
 
@@ -372,14 +371,14 @@ private:
 
         while (iter.next(&name, false)) {
             SkString filename(SkOSPath::Join(directory.c_str(), name.c_str()));
-            SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(filename.c_str()));
-            if (!stream.get()) {
+            std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(filename.c_str());
+            if (!stream) {
                 SkDebugf("---- failed to open <%s>\n", filename.c_str());
                 continue;
             }
 
             int numFaces;
-            if (!scanner.recognizedFont(stream, &numFaces)) {
+            if (!scanner.recognizedFont(stream.get(), &numFaces)) {
                 SkDebugf("---- failed to open <%s> as a font\n", filename.c_str());
                 continue;
             }
@@ -388,7 +387,9 @@ private:
                 bool isFixedPitch;
                 SkString realname;
                 SkFontStyle style = SkFontStyle(); // avoid uninitialized warning
-                if (!scanner.scanFont(stream, faceIndex, &realname, &style, &isFixedPitch, nullptr)) {
+                if (!scanner.scanFont(stream.get(), faceIndex,
+                                      &realname, &style, &isFixedPitch, nullptr))
+                {
                     SkDebugf("---- failed to open <%s> <%d> as a font\n",
                              filename.c_str(), faceIndex);
                     continue;
@@ -462,10 +463,10 @@ private:
                                    const uint8_t* data, size_t size, int index,
                                    SkFontMgr_Custom::Families* families)
     {
-        SkAutoTDelete<SkMemoryStream> stream(new SkMemoryStream(data, size, false));
+        auto stream = skstd::make_unique<SkMemoryStream>(data, size, false);
 
         int numFaces;
-        if (!scanner.recognizedFont(stream, &numFaces)) {
+        if (!scanner.recognizedFont(stream.get(), &numFaces)) {
             SkDebugf("---- failed to open <%d> as a font\n", index);
             return;
         }
@@ -474,7 +475,9 @@ private:
             bool isFixedPitch;
             SkString realname;
             SkFontStyle style = SkFontStyle(); // avoid uninitialized warning
-            if (!scanner.scanFont(stream, faceIndex, &realname, &style, &isFixedPitch, nullptr)) {
+            if (!scanner.scanFont(stream.get(), faceIndex,
+                                  &realname, &style, &isFixedPitch, nullptr))
+            {
                 SkDebugf("---- failed to open <%d> <%d> as a font\n", index, faceIndex);
                 return;
             }
@@ -484,8 +487,7 @@ private:
                 addTo = new SkFontStyleSet_Custom(realname);
                 families->push_back().reset(addTo);
             }
-            std::unique_ptr<SkFontData> data(
-                new SkFontData(stream.release(), faceIndex, nullptr, 0));
+            auto data = skstd::make_unique<SkFontData>(std::move(stream), faceIndex, nullptr, 0);
             addTo->appendTypeface(sk_make_sp<SkTypeface_Stream>(std::move(data),
                                                                 style, isFixedPitch,
                                                                 true, realname));
index 5cfd81e411eca1f81bea113bd5e99ba1f4bbb3a7..1e17cb67747fec7bacda2ee6eb877951ec92513b 100644 (file)
@@ -12,6 +12,7 @@
 #include "SkFontHost_FreeType_common.h"
 #include "SkFontMgr.h"
 #include "SkFontStyle.h"
+#include "SkMakeUnique.h"
 #include "SkMath.h"
 #include "SkMutex.h"
 #include "SkOSFile.h"
@@ -405,9 +406,9 @@ static void fcpattern_from_skfontstyle(SkFontStyle style, FcPattern* pattern) {
 class SkTypeface_stream : public SkTypeface_FreeType {
 public:
     /** @param data takes ownership of the font data.*/
-    SkTypeface_stream(SkFontData* data, const SkFontStyle& style, bool fixedWidth)
+    SkTypeface_stream(std::unique_ptr<SkFontData> data, const SkFontStyle& style, bool fixedWidth)
         : INHERITED(style, fixedWidth)
-        , fData(data)
+        , fData(std::move(data))
     { };
 
     void onGetFamilyName(SkString* familyName) const override {
@@ -420,15 +421,15 @@ public:
 
     SkStreamAsset* onOpenStream(int* ttcIndex) const override {
         *ttcIndex = fData->getIndex();
-        return fData->duplicateStream();
+        return fData->getStream()->duplicate();
     }
 
-    SkFontData* onCreateFontData() const override {
-        return new SkFontData(*fData.get());
+    std::unique_ptr<SkFontData> onMakeFontData() const override {
+        return skstd::make_unique<SkFontData>(*fData);
     }
 
 private:
-    const SkAutoTDelete<const SkFontData> fData;
+    const std::unique_ptr<const SkFontData> fData;
 
     typedef SkTypeface_FreeType INHERITED;
 };
@@ -457,7 +458,7 @@ public:
     SkStreamAsset* onOpenStream(int* ttcIndex) const override {
         FCLocker lock;
         *ttcIndex = get_int(fPattern, FC_INDEX, 0);
-        return SkStream::NewFromFile(get_string(fPattern, FC_FILE));
+        return SkStream::MakeFromFile(get_string(fPattern, FC_FILE)).release();
     }
 
     void onFilterRec(SkScalerContextRec* rec) const override {
@@ -878,7 +879,7 @@ protected:
     }
 
     SkTypeface* onCreateFromStream(SkStreamAsset* bareStream, int ttcIndex) const override {
-        SkAutoTDelete<SkStreamAsset> stream(bareStream);
+        std::unique_ptr<SkStreamAsset> stream(bareStream);
         const size_t length = stream->getLength();
         if (length <= 0 || (1u << 30) < length) {
             return nullptr;
@@ -886,23 +887,23 @@ protected:
 
         SkFontStyle style;
         bool isFixedWidth = false;
-        if (!fScanner.scanFont(stream, ttcIndex, nullptr, &style, &isFixedWidth, nullptr)) {
+        if (!fScanner.scanFont(stream.get(), ttcIndex, nullptr, &style, &isFixedWidth, nullptr)) {
             return nullptr;
         }
 
-        return new SkTypeface_stream(new SkFontData(stream.release(), ttcIndex, nullptr, 0), style,
-                                     isFixedWidth);
+        auto data = skstd::make_unique<SkFontData>(std::move(stream), ttcIndex, nullptr, 0);
+        return new SkTypeface_stream(std::move(data), style, isFixedWidth);
     }
 
     SkTypeface* onCreateFromStream(SkStreamAsset* s, const FontParameters& params) const override {
         using Scanner = SkTypeface_FreeType::Scanner;
-        SkAutoTDelete<SkStreamAsset> stream(s);
+        std::unique_ptr<SkStreamAsset> stream(s);
         bool isFixedPitch;
         SkFontStyle style;
         SkString name;
         Scanner::AxisDefinitions axisDefinitions;
-        if (!fScanner.scanFont(stream, params.getCollectionIndex(), &name, &style, &isFixedPitch,
-                               &axisDefinitions))
+        if (!fScanner.scanFont(stream.get(), params.getCollectionIndex(),
+                               &name, &style, &isFixedPitch, &axisDefinitions))
         {
             return nullptr;
         }
@@ -912,9 +913,9 @@ protected:
         SkAutoSTMalloc<4, SkFixed> axisValues(axisDefinitions.count());
         Scanner::computeAxisValues(axisDefinitions, paramAxes, paramAxisCount, axisValues, name);
 
-        SkFontData* data(new SkFontData(stream.release(), params.getCollectionIndex(),
-                                        axisValues.get(), axisDefinitions.count()));
-        return new SkTypeface_stream(data, style, isFixedPitch);
+        auto data = skstd::make_unique<SkFontData>(std::move(stream), params.getCollectionIndex(),
+                                                   axisValues.get(), axisDefinitions.count());
+        return new SkTypeface_stream(std::move(data), style, isFixedPitch);
     }
 
     SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const override {
@@ -922,10 +923,10 @@ protected:
     }
 
     SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const override {
-        return this->createFromStream(SkStream::NewFromFile(path), ttcIndex);
+        return this->createFromStream(SkStream::MakeFromFile(path).release(), ttcIndex);
     }
 
-    SkTypeface* onCreateFromFontData(SkFontData* fontData) const override {
+    SkTypeface* onCreateFromFontData(std::unique_ptr<SkFontData> fontData) const override {
         SkStreamAsset* stream(fontData->getStream());
         const size_t length = stream->getLength();
         if (length <= 0 || (1u << 30) < length) {
@@ -939,7 +940,7 @@ protected:
             return nullptr;
         }
 
-        return new SkTypeface_stream(fontData, style, isFixedWidth);
+        return new SkTypeface_stream(std::move(fontData), style, isFixedWidth);
     }
 
     SkTypeface* onLegacyCreateTypeface(const char familyName[], SkFontStyle style) const override {
index 833f95bb0ef72c899eec3dc15fdc044a93eb41bf..7201dc10b03b8380d2ccdb14a6ce95a43e9dad44 100644 (file)
@@ -933,7 +933,7 @@ SkTypeface* SkFontMgr_DirectWrite::onCreateFromData(SkData* data, int ttcIndex)
 }
 
 SkTypeface* SkFontMgr_DirectWrite::onCreateFromFile(const char path[], int ttcIndex) const {
-    return this->createFromStream(SkStream::NewFromFile(path), ttcIndex);
+    return this->createFromStream(SkStream::MakeFromFile(path).release(), ttcIndex);
 }
 
 HRESULT SkFontMgr_DirectWrite::getByFamilyName(const WCHAR wideFamilyName[],
index 139e697ca08f4dd0eb540329ab446829dd3cbe64..007def6d8be7f55836db5c581d6e5215a6ee5902 100644 (file)
@@ -63,7 +63,7 @@ static int whitelist_name_index(const SkTypeface* tf) {
 }
 
 static uint32_t compute_checksum(const SkTypeface* tf) {
-    SkFontData* fontData = tf->createFontData();
+    std::unique_ptr<SkFontData> fontData = tf->makeFontData();
     if (!fontData) {
         return 0;
     }
@@ -118,7 +118,7 @@ static void serialize_full(const SkTypeface* tf, SkWStream* wstream) {
 
     // Embed font data if it's a local font.
     if (isLocal && !desc.hasFontData()) {
-        desc.setFontData(tf->createFontData());
+        desc.setFontData(tf->makeFontData());
     }
     desc.serialize(wstream);
 }
@@ -190,9 +190,9 @@ sk_sp<SkTypeface> WhitelistDeserializeTypeface(SkStream* stream) {
         return nullptr;
     }
 
-    SkFontData* data = desc.detachFontData();
+    std::unique_ptr<SkFontData> data = desc.detachFontData();
     if (data) {
-        sk_sp<SkTypeface> typeface(SkTypeface::MakeFromFontData(data));
+        sk_sp<SkTypeface> typeface(SkTypeface::MakeFromFontData(std::move(data)));
         if (typeface) {
             return typeface;
         }
index 7f15ed7090b1a26cba83ab084926498a1193e83c..d9cdb86e49f975193fd6cf0cda1ab86820ab9740 100644 (file)
@@ -176,71 +176,6 @@ void SkCGDrawBitmap(CGContextRef cg, const SkBitmap& bm, float x, float y) {
     }
 }
 
-///////////////////////////////////////////////////////////////////////////////
-
-#include "SkStream.h"
-
-class SkAutoPDFRelease {
-public:
-    SkAutoPDFRelease(CGPDFDocumentRef doc) : fDoc(doc) {}
-    ~SkAutoPDFRelease() {
-        if (fDoc) {
-            CGPDFDocumentRelease(fDoc);
-        }
-    }
-private:
-    CGPDFDocumentRef fDoc;
-};
-#define SkAutoPDFRelease(...) SK_REQUIRE_LOCAL_VAR(SkAutoPDFRelease)
-
-bool SkPDFDocumentToBitmap(SkStream* stream, SkBitmap* output) {
-    CGDataProviderRef data = SkCreateDataProviderFromStream(stream);
-    if (nullptr == data) {
-        return false;
-    }
-
-    CGPDFDocumentRef pdf = CGPDFDocumentCreateWithProvider(data);
-    CGDataProviderRelease(data);
-    if (nullptr == pdf) {
-        return false;
-    }
-    SkAutoPDFRelease releaseMe(pdf);
-
-    CGPDFPageRef page = CGPDFDocumentGetPage(pdf, 1);
-    if (nullptr == page) {
-        return false;
-    }
-
-    CGRect bounds = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
-
-    int w = (int)CGRectGetWidth(bounds);
-    int h = (int)CGRectGetHeight(bounds);
-
-    SkBitmap bitmap;
-    if (!bitmap.tryAllocN32Pixels(w, h)) {
-        return false;
-    }
-    bitmap.eraseColor(SK_ColorWHITE);
-
-    size_t bitsPerComponent;
-    CGBitmapInfo info;
-    getBitmapInfo(bitmap, &bitsPerComponent, &info, nullptr);
-
-    CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB();
-    CGContextRef ctx = CGBitmapContextCreate(bitmap.getPixels(), w, h,
-                                             bitsPerComponent, bitmap.rowBytes(),
-                                             cs, info);
-    CGColorSpaceRelease(cs);
-
-    if (ctx) {
-        CGContextDrawPDFPage(ctx, page);
-        CGContextRelease(ctx);
-    }
-
-    output->swap(bitmap);
-    return true;
-}
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
 SK_API bool SkCopyPixelsFromCGImage(const SkImageInfo& info, size_t rowBytes, void* pixels,
index 36e5194a1990b3e65bdbf0fe3654bcff5659b167..a1d63b5fd61306b8045ccff791abbc8ac4cd72e9 100644 (file)
@@ -48,13 +48,13 @@ static void release_info_proc(void* info) {
     delete (SkStream*)info;
 }
 
-CGDataProviderRef SkCreateDataProviderFromStream(SkStream* stream) {
+CGDataProviderRef SkCreateDataProviderFromStream(std::unique_ptr<SkStream> stream) {
     // TODO: Replace with SkStream::getData() when that is added. Then we only
     // have one version of CGDataProviderCreateWithData (i.e. same release proc)
     const void* addr = stream->getMemoryBase();
     if (addr) {
         // special-case when the stream is just a block of ram
-        return CGDataProviderCreateWithData(stream, addr, stream->getLength(),
+        return CGDataProviderCreateWithData(stream.release(), addr, stream->getLength(),
                                             delete_stream_proc);
     }
 
@@ -65,17 +65,15 @@ CGDataProviderRef SkCreateDataProviderFromStream(SkStream* stream) {
     rec.skipForward = skip_forward_proc;
     rec.rewind = rewind_proc;
     rec.releaseInfo = release_info_proc;
-    return CGDataProviderCreateSequential(stream, &rec);
+    return CGDataProviderCreateSequential(stream.release(), &rec);
 }
 
 ///////////////////////////////////////////////////////////////////////////////
 
 #include "SkData.h"
 
-CGDataProviderRef SkCreateDataProviderFromData(SkData* data) {
-    data->ref();
-    return CGDataProviderCreateWithData(data, data->data(), data->size(),
-                                            unref_proc);
+CGDataProviderRef SkCreateDataProviderFromData(sk_sp<SkData> data) {
+    return CGDataProviderCreateWithData(data.release(), data->data(), data->size(), unref_proc);
 }
 
 #endif//defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
index 5c01490f9b0884a149fcfa93f8844eecda637d7f..4affa85b7b8743862f5edefb0a91b2298a7db3bb 100644 (file)
@@ -26,11 +26,9 @@ DEF_TEST(BadImage, reporter) {
 
     const char* badImagesFolder = "invalid_images";
 
-    SkString resourcePath = GetResourcePath(badImagesFolder);
-
     for (size_t i = 0; i < SK_ARRAY_COUNT(badImages); ++i) {
-        SkString fullPath = SkOSPath::Join(resourcePath.c_str(), badImages[i]);
-        SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(fullPath.c_str()));
+        SkString resourcePath = SkOSPath::Join(badImagesFolder, badImages[i]);
+        SkAutoTDelete<SkStream> stream(GetResourceAsStream(resourcePath.c_str()));
         SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
 
         // These images are corrupt.  It's not important whether we succeed/fail in codec
index 044c2fd6b45ff733b5c7c2c55eec5fd265570ee9..341433b379674b1db376765d80392bb650947dea 100644 (file)
 
 #include "png.h"
 
-static SkStreamAsset* resource(const char path[]) {
-    SkString fullPath = GetResourcePath(path);
-    return SkStream::NewFromFile(fullPath.c_str());
-}
-
 static void md5(const SkBitmap& bm, SkMD5::Digest* digest) {
     SkAutoLockPixels autoLockPixels(bm);
     SkASSERT(bm.getPixels());
@@ -199,9 +194,8 @@ static void check(skiatest::Reporter* r,
                   bool supportsSubsetDecoding,
                   bool supportsIncomplete = true) {
 
-    SkAutoTDelete<SkStream> stream(resource(path));
+    SkAutoTDelete<SkStream> stream(GetResourceAsStream(path));
     if (!stream) {
-        SkDebugf("Missing resource '%s'\n", path);
         return;
     }
 
@@ -329,9 +323,8 @@ static void check(skiatest::Reporter* r,
     // SkAndroidCodec tests
     if (supportsScanlineDecoding || supportsSubsetDecoding) {
 
-        SkAutoTDelete<SkStream> stream(resource(path));
+        SkAutoTDelete<SkStream> stream(GetResourceAsStream(path));
         if (!stream) {
-            SkDebugf("Missing resource '%s'\n", path);
             return;
         }
 
@@ -356,7 +349,7 @@ static void check(skiatest::Reporter* r,
 
     if (!isIncomplete) {
         // Test SkCodecImageGenerator
-        SkAutoTDelete<SkStream> stream(resource(path));
+        SkAutoTDelete<SkStream> stream(GetResourceAsStream(path));
         sk_sp<SkData> fullData(SkData::MakeFromStream(stream, stream->getLength()));
         SkAutoTDelete<SkImageGenerator> gen(
                 SkCodecImageGenerator::NewFromEncodedCodec(fullData.get()));
@@ -449,9 +442,10 @@ DEF_TEST(Codec, r) {
 // Test interlaced PNG in stripes, similar to DM's kStripe_Mode
 DEF_TEST(Codec_stripes, r) {
     const char * path = "plane_interlaced.png";
-    SkAutoTDelete<SkStream> stream(resource(path));
+    SkAutoTDelete<SkStream> stream(GetResourceAsStream(path));
+    REPORTER_ASSERT(r, stream);
     if (!stream) {
-        SkDebugf("Missing resource '%s'\n", path);
+        return;
     }
 
     SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
@@ -582,9 +576,8 @@ DEF_TEST(Codec_null, r) {
 
 static void test_dimensions(skiatest::Reporter* r, const char path[]) {
     // Create the codec from the resource file
-    SkAutoTDelete<SkStream> stream(resource(path));
+    SkAutoTDelete<SkStream> stream(GetResourceAsStream(path));
     if (!stream) {
-        SkDebugf("Missing resource '%s'\n", path);
         return;
     }
     SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.release()));
@@ -647,9 +640,8 @@ DEF_TEST(Codec_Dimensions, r) {
 }
 
 static void test_invalid(skiatest::Reporter* r, const char path[]) {
-    SkAutoTDelete<SkStream> stream(resource(path));
+    SkAutoTDelete<SkStream> stream(GetResourceAsStream(path));
     if (!stream) {
-        SkDebugf("Missing resource '%s'\n", path);
         return;
     }
     SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
@@ -673,9 +665,8 @@ DEF_TEST(Codec_Empty, r) {
 }
 
 static void test_invalid_parameters(skiatest::Reporter* r, const char path[]) {
-    SkAutoTDelete<SkStream> stream(resource(path));
+    SkAutoTDelete<SkStream> stream(GetResourceAsStream(path));
     if (!stream) {
-        SkDebugf("Missing resource '%s'\n", path);
         return;
     }
     SkAutoTDelete<SkCodec> decoder(SkCodec::NewFromStream(stream.release()));
@@ -965,9 +956,8 @@ DEF_TEST(Codec_webp_peek, r) {
 // Test that SkCodec now supports an image with these bits set.
 DEF_TEST(Codec_wbmp, r) {
     const char* path = "mandrill.wbmp";
-    SkAutoTDelete<SkStream> stream(resource(path));
+    SkAutoTDelete<SkStream> stream(GetResourceAsStream(path));
     if (!stream) {
-        SkDebugf("Missing resource '%s'\n", path);
         return;
     }
 
@@ -1016,9 +1006,8 @@ DEF_TEST(Codec_wbmp_max_size, r) {
 
 DEF_TEST(Codec_jpeg_rewind, r) {
     const char* path = "mandrill_512_q075.jpg";
-    SkAutoTDelete<SkStream> stream(resource(path));
+    SkAutoTDelete<SkStream> stream(GetResourceAsStream(path));
     if (!stream) {
-        SkDebugf("Missing resource '%s'\n", path);
         return;
     }
     SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.release()));
@@ -1044,7 +1033,7 @@ DEF_TEST(Codec_jpeg_rewind, r) {
 }
 
 static void check_color_xform(skiatest::Reporter* r, const char* path) {
-    SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(resource(path)));
+    SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(GetResourceAsStream(path)));
 
     SkAndroidCodec::AndroidOptions opts;
     opts.fSampleSize = 3;
@@ -1130,7 +1119,7 @@ static void check_round_trip(skiatest::Reporter* r, SkCodec* origCodec, const Sk
 
 DEF_TEST(Codec_PngRoundTrip, r) {
     const char* path = "mandrill_512_q075.jpg";
-    SkAutoTDelete<SkStream> stream(resource(path));
+    SkAutoTDelete<SkStream> stream(GetResourceAsStream(path));
     SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
 
     SkColorType colorTypesOpaque[] = {
@@ -1142,12 +1131,12 @@ DEF_TEST(Codec_PngRoundTrip, r) {
     }
 
     path = "grayscale.jpg";
-    stream.reset(resource(path));
+    stream.reset(GetResourceAsStream(path));
     codec.reset(SkCodec::NewFromStream(stream.release()));
     check_round_trip(r, codec.get(), codec->getInfo());
 
     path = "yellow_rose.png";
-    stream.reset(resource(path));
+    stream.reset(GetResourceAsStream(path));
     codec.reset(SkCodec::NewFromStream(stream.release()));
 
     SkColorType colorTypesWithAlpha[] = {
@@ -1167,7 +1156,7 @@ DEF_TEST(Codec_PngRoundTrip, r) {
     }
 
     path = "index8.png";
-    stream.reset(resource(path));
+    stream.reset(GetResourceAsStream(path));
     codec.reset(SkCodec::NewFromStream(stream.release()));
 
     for (SkAlphaType alphaType : alphaTypes) {
@@ -1179,7 +1168,7 @@ DEF_TEST(Codec_PngRoundTrip, r) {
 
 static void test_conversion_possible(skiatest::Reporter* r, const char* path,
                                      bool testScanlineDecoder) {
-    SkAutoTDelete<SkStream> stream(resource(path));
+    SkAutoTDelete<SkStream> stream(GetResourceAsStream(path));
     SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
     SkImageInfo infoF16 = codec->getInfo().makeColorType(kRGBA_F16_SkColorType);
 
index 4a644603707c7e16c30eac49d59ffee50820b788..7e247d304d60860329966a4e58af8fef288df619 100644 (file)
@@ -40,15 +40,10 @@ static void test_space(skiatest::Reporter* r, SkColorSpace* space,
     }
 }
 
-static SkStreamAsset* resource(const char path[]) {
-    SkString fullPath = GetResourcePath(path);
-    return SkStream::NewFromFile(fullPath.c_str());
-}
-
 static void test_path(skiatest::Reporter* r, const char* path,
                       const float red[], const float green[], const float blue[],
                       const SkGammaNamed expectedGamma) {
-    SkAutoTDelete<SkStream> stream(resource(path));
+    SkAutoTDelete<SkStream> stream(GetResourceAsStream(path));
     REPORTER_ASSERT(r, nullptr != stream);
     if (!stream) {
         return;
index 7fcd8b861d66820ef80fb8fcb905981db02a17ff..4aac4870647db6e32090db0abd020628efc9923d 100644 (file)
@@ -9,13 +9,8 @@
 #include "SkCodec.h"
 #include "Test.h"
 
-static SkStreamAsset* resource(const char path[]) {
-    SkString fullPath = GetResourcePath(path);
-    return SkStream::NewFromFile(fullPath.c_str());
-}
-
 DEF_TEST(ExifOrientation, r) {
-    SkAutoTDelete<SkStream> stream(resource("exif-orientation-2-ur.jpg"));
+    SkAutoTDelete<SkStream> stream(GetResourceAsStream("exif-orientation-2-ur.jpg"));
     REPORTER_ASSERT(r, nullptr != stream);
     if (!stream) {
         return;
@@ -26,7 +21,7 @@ DEF_TEST(ExifOrientation, r) {
     SkCodec::Origin origin = codec->getOrigin();
     REPORTER_ASSERT(r, SkCodec::kTopRight_Origin == origin);
 
-    stream.reset(resource("mandrill_512_q075.jpg"));
+    stream.reset(GetResourceAsStream("mandrill_512_q075.jpg"));
     codec.reset(SkCodec::NewFromStream(stream.release()));
     REPORTER_ASSERT(r, nullptr != codec);
     origin = codec->getOrigin();
index ff9b342074adc1d7d8b5d54d4c7364077cda633b..bc2770ebda0d0a9e0143a408bf115e2ceb2f7bc8 100644 (file)
@@ -13,6 +13,7 @@
 #include "SkImage.h"
 #include "SkImageSource.h"
 #include "SkLightingShader.h"
+#include "SkMakeUnique.h"
 #include "SkMallocPixelRef.h"
 #include "SkNormalSource.h"
 #include "SkOSFile.h"
@@ -370,13 +371,13 @@ static void TestPictureTypefaceSerialization(skiatest::Reporter* reporter) {
 
     {
         // Load typeface as stream to create with axis settings.
-        SkAutoTDelete<SkStreamAsset> distortable(GetResourceAsStream("/fonts/Distortable.ttf"));
+        std::unique_ptr<SkStreamAsset> distortable(GetResourceAsStream("/fonts/Distortable.ttf"));
         if (!distortable) {
             INFOF(reporter, "Could not run fontstream test because Distortable.ttf not found.");
         } else {
             SkFixed axis = SK_FixedSqrt2;
             sk_sp<SkTypeface> typeface(SkTypeface::MakeFromFontData(
-                new SkFontData(distortable.release(), 0, &axis, 1)));
+                skstd::make_unique<SkFontData>(std::move(distortable), 0, &axis, 1)));
             if (!typeface) {
                 INFOF(reporter, "Could not run fontstream test because Distortable.ttf not created.");
             } else {
index f7b3306b98fa935b02b5cda64c1caf6506c3d442..d3b916784372fdeab18d8883fbd82d0d9edf5ad7 100644 (file)
 #include "SkYUVSizeInfo.h"
 #include "Test.h"
 
-static SkStreamAsset* resource(const char path[]) {
-    SkString fullPath = GetResourcePath(path);
-    return SkStream::NewFromFile(fullPath.c_str());
-}
-
 static void codec_yuv(skiatest::Reporter* reporter,
                   const char path[],
                   SkISize expectedSizes[3]) {
-    SkAutoTDelete<SkStream> stream(resource(path));
+    SkAutoTDelete<SkStream> stream(GetResourceAsStream(path));
     if (!stream) {
-        INFOF(reporter, "Missing resource '%s'\n", path);
         return;
     }
     SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
index 899f5d2bc4332321941777bdf43f2fc1e3888851..9c12a67c20a3250c0b9c99cd1b46918e7427596b 100644 (file)
@@ -41,12 +41,11 @@ sk_sp<SkImage> GetResourceAsImage(const char* resource) {
 SkStreamAsset* GetResourceAsStream(const char* resource) {
     SkString resourcePath = GetResourcePath(resource);
     SkAutoTDelete<SkFILEStream> stream(new SkFILEStream(resourcePath.c_str()));
-    if (stream->isValid()) {
-        return stream.release();
-    } else {
+    if (!stream->isValid()) {
         SkDebugf("Resource %s not found.\n", resource);
         return nullptr;
     }
+    return stream.release();
 }
 
 sk_sp<SkTypeface> MakeResourceAsTypeface(const char* resource) {
index 5d54f4d561c10c040fee0be22da29c4810f5b5c9..03ef93e58167eb1cd81a0fff39651cbe57198287 100644 (file)
@@ -46,12 +46,12 @@ int tool_main(int argc, char** argv) {
             continue;
         }
 
-        SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(FLAGS_skps[i]));
+        std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(FLAGS_skps[i]);
         if (!stream) {
             SkDebugf("Could not read %s.\n", FLAGS_skps[i]);
             return 1;
         }
-        sk_sp<SkPicture> src(SkPicture::MakeFromStream(stream));
+        sk_sp<SkPicture> src(SkPicture::MakeFromStream(stream.get()));
         if (!src) {
             SkDebugf("Could not read %s as an SkPicture.\n", FLAGS_skps[i]);
             return 1;
index 1ec70b3531f7dcd345d6c882648b92e340bccaeb..e38a245915b4c5335a0278c9de387e9e7330d2c4 100644 (file)
@@ -128,9 +128,9 @@ int main(int argc, char** argv) {
 
     SkOSFile::Iter iter(inputs, "skp");
     for (SkString file; iter.next(&file); ) {
-        SkAutoTDelete<SkStream> stream =
-                SkStream::NewFromFile(SkOSPath::Join(inputs, file.c_str()).c_str());
-        sk_sp<SkPicture> picture(SkPicture::MakeFromStream(stream));
+        std::unique_ptr<SkStream> stream =
+                SkStream::MakeFromFile(SkOSPath::Join(inputs, file.c_str()).c_str());
+        sk_sp<SkPicture> picture(SkPicture::MakeFromStream(stream.get()));
 
         SkDynamicMemoryWStream scratch;
         Sniffer sniff(file.c_str());
index eee7088f0d14dc97b012cf0b736e1a37483ab775..58983f4d58f56d9f7f2d6f861bc21918c10d1095 100644 (file)
@@ -39,8 +39,8 @@ DEFINE_string2(tailFunc, s, "", "Optional lua function to call at end");
 DEFINE_bool2(quiet, q, false, "Silence all non-error related output");
 
 static sk_sp<SkPicture> load_picture(const char path[]) {
-    SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
-    if (stream.get()) {
+    std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(path);
+    if (stream) {
         return SkPicture::MakeFromStream(stream.get());
     }
     return nullptr;
index 9419253f0c25a2aa4f0a238018e0790bb84c09c3..e24e1fdb048d35718b6cd32f3e497fd9dbf9e267 100644 (file)
@@ -35,8 +35,8 @@ void SKPSlide::draw(SkCanvas* canvas) {
 }
 
 static sk_sp<SkPicture> read_picture(const char path[]) {
-    SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
-    if (stream.get() == nullptr) {
+    std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(path);
+    if (!stream) {
         SkDebugf("Could not read %s.\n", path);
         return nullptr;
     }