aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core
diff options
context:
space:
mode:
authorGravatar bungeman <bungeman@google.com>2016-09-16 06:24:20 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2016-09-16 06:24:20 -0700
commitf93d71122e4fcfcdc674a0163455990b13855f2f (patch)
treeb3713fad2da586bf997264247961c1a91453c383 /src/core
parentf560ef86813f712eff70feb9b00b70d0fe92c3cd (diff)
SkFontData to use smart pointers.
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 Committed: https://skia.googlesource.com/skia/+/d8c2476a8b1e1e1a1771b17e8dd4db8645914f8c Review-Url: https://codereview.chromium.org/2339273002
Diffstat (limited to 'src/core')
-rw-r--r--src/core/SkFontDescriptor.cpp11
-rw-r--r--src/core/SkFontDescriptor.h21
-rw-r--r--src/core/SkFontMgr.cpp10
-rw-r--r--src/core/SkStream.cpp15
-rw-r--r--src/core/SkTypeface.cpp21
5 files changed, 38 insertions, 40 deletions
diff --git a/src/core/SkFontDescriptor.cpp b/src/core/SkFontDescriptor.cpp
index 85629efa7e..73ea2058ce 100644
--- a/src/core/SkFontDescriptor.cpp
+++ b/src/core/SkFontDescriptor.cpp
@@ -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);
}
diff --git a/src/core/SkFontDescriptor.h b/src/core/SkFontDescriptor.h
index cb8d2f4f03..de1462177b 100644
--- a/src/core/SkFontDescriptor.h
+++ b/src/core/SkFontDescriptor.h
@@ -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;
};
diff --git a/src/core/SkFontMgr.cpp b/src/core/SkFontMgr.cpp
index 0f00667926..57f82b03ba 100644
--- a/src/core/SkFontMgr.cpp
+++ b/src/core/SkFontMgr.cpp
@@ -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 {
diff --git a/src/core/SkStream.cpp b/src/core/SkStream.cpp
index 48eb92cae5..e7b3a7a7e5 100644
--- a/src/core/SkStream.cpp
+++ b/src/core/SkStream.cpp
@@ -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:
diff --git a/src/core/SkTypeface.cpp b/src/core/SkTypeface.cpp
index 0c960d5915..3c4f5cb7e7 100644
--- a/src/core/SkTypeface.cpp
+++ b/src/core/SkTypeface.cpp
@@ -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,