aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/ports
diff options
context:
space:
mode:
authorGravatar scroggo <scroggo@google.com>2015-01-21 12:09:53 -0800
committerGravatar Commit bot <commit-bot@chromium.org>2015-01-21 12:09:53 -0800
commita1193e4b0e34a7e4e1bd33e9708d7341679f8321 (patch)
treee63e73eeadfd6d979e81596804e34e7af8fa0390 /src/ports
parentfd1ad48d4d9073b90f58e60219637046a8446267 (diff)
Make SkStream *not* ref counted.
SkStream is a stateful object, so it does not make sense for it to have multiple owners. Make SkStream inherit directly from SkNoncopyable. Update methods which previously called SkStream::ref() (e.g. SkImageDecoder::buildTileIndex() and SkFrontBufferedStream::Create(), which required the existing owners to call SkStream::unref()) to take ownership of their SkStream parameters and delete when done (including on failure). Switch all SkAutoTUnref<SkStream>s to SkAutoTDelete<SkStream>s. In some cases this means heap allocating streams that were previously stack allocated. Respect ownership rules of SkTypeface::CreateFromStream() and SkImageDecoder::buildTileIndex(). Update the comments for exceptional methods which do not affect the ownership of their SkStream parameters (e.g. SkPicture::CreateFromStream() and SkTypeface::Deserialize()) to be explicit about ownership. Remove test_stream_life, which tested that buildTileIndex() behaved correctly when SkStream was a ref counted object. The test does not make sense now that it is not. In SkPDFStream, remove the SkMemoryStream member. Instead of using it, create a new SkMemoryStream to pass to fDataStream (which is now an SkAutoTDelete). Make other pdf rasterizers behave like SkPDFDocumentToBitmap. SkPDFDocumentToBitmap delete the SkStream, so do the same in the following pdf rasterizers: SkPopplerRasterizePDF SkNativeRasterizePDF SkNoRasterizePDF Requires a change to Android, which currently treats SkStreams as ref counted objects. Review URL: https://codereview.chromium.org/849103004
Diffstat (limited to 'src/ports')
-rw-r--r--src/ports/SkFontConfigTypeface.h13
-rw-r--r--src/ports/SkFontHost_FreeType.cpp19
-rw-r--r--src/ports/SkFontHost_fontconfig.cpp7
-rw-r--r--src/ports/SkFontHost_linux.cpp15
-rwxr-xr-xsrc/ports/SkFontHost_win.cpp8
-rw-r--r--src/ports/SkFontMgr_android.cpp16
-rw-r--r--src/ports/SkFontMgr_fontconfig.cpp16
-rw-r--r--src/ports/SkFontMgr_win_dw.cpp14
-rw-r--r--src/ports/SkImageDecoder_empty.cpp10
-rw-r--r--src/ports/SkTypeface_win_dw.cpp2
10 files changed, 56 insertions, 64 deletions
diff --git a/src/ports/SkFontConfigTypeface.h b/src/ports/SkFontConfigTypeface.h
index fcbc16f3c0..821f4908d5 100644
--- a/src/ports/SkFontConfigTypeface.h
+++ b/src/ports/SkFontConfigTypeface.h
@@ -15,7 +15,7 @@ class SkFontDescriptor;
class FontConfigTypeface : public SkTypeface_FreeType {
SkFontConfigInterface::FontIdentity fIdentity;
SkString fFamilyName;
- SkStream* fLocalStream;
+ SkAutoTDelete<SkStream> fLocalStream;
public:
static FontConfigTypeface* Create(const SkFontStyle& style,
@@ -29,16 +29,12 @@ public:
return SkNEW_ARGS(FontConfigTypeface, (style, fixedWidth, localStream));
}
- virtual ~FontConfigTypeface() {
- SkSafeUnref(fLocalStream);
- }
-
const SkFontConfigInterface::FontIdentity& getIdentity() const {
return fIdentity;
}
const char* getFamilyName() const { return fFamilyName.c_str(); }
- SkStream* getLocalStream() const { return fLocalStream; }
+ SkStream* getLocalStream() const { return fLocalStream.get(); }
bool isFamilyName(const char* name) const {
return fFamilyName.equals(name);
@@ -60,10 +56,9 @@ protected:
, fLocalStream(NULL) {}
FontConfigTypeface(const SkFontStyle& style, bool fixedWidth, SkStream* localStream)
- : INHERITED(style, SkTypefaceCache::NewFontID(), fixedWidth) {
+ : INHERITED(style, SkTypefaceCache::NewFontID(), fixedWidth)
+ , fLocalStream(localStream) {
// we default to empty fFamilyName and fIdentity
- fLocalStream = localStream;
- SkSafeRef(localStream);
}
void onGetFamilyName(SkString* familyName) const SK_OVERRIDE;
diff --git a/src/ports/SkFontHost_FreeType.cpp b/src/ports/SkFontHost_FreeType.cpp
index ce762d8221..72fe8f67db 100644
--- a/src/ports/SkFontHost_FreeType.cpp
+++ b/src/ports/SkFontHost_FreeType.cpp
@@ -233,18 +233,15 @@ private:
///////////////////////////////////////////////////////////////////////////
struct SkFaceRec {
- SkFaceRec* fNext;
- FT_Face fFace;
- FT_StreamRec fFTStream;
- SkStream* fSkStream;
- uint32_t fRefCnt;
- uint32_t fFontID;
-
- // assumes ownership of the stream, will call unref() when its done
+ SkFaceRec* fNext;
+ FT_Face fFace;
+ FT_StreamRec fFTStream;
+ SkAutoTDelete<SkStream> fSkStream;
+ uint32_t fRefCnt;
+ uint32_t fFontID;
+
+ // assumes ownership of the stream, will delete when its done
SkFaceRec(SkStream* strm, uint32_t fontID);
- ~SkFaceRec() {
- fSkStream->unref();
- }
};
extern "C" {
diff --git a/src/ports/SkFontHost_fontconfig.cpp b/src/ports/SkFontHost_fontconfig.cpp
index 32e9f80560..4c577d8c27 100644
--- a/src/ports/SkFontHost_fontconfig.cpp
+++ b/src/ports/SkFontHost_fontconfig.cpp
@@ -132,7 +132,7 @@ SkStream* FontConfigTypeface::onOpenStream(int* ttcIndex) const {
// should have been provided by CreateFromStream()
*ttcIndex = 0;
- SkAutoTUnref<SkStream> dupStream(stream->duplicate());
+ SkAutoTDelete<SkStream> dupStream(stream->duplicate());
if (dupStream) {
return dupStream.detach();
}
@@ -148,13 +148,10 @@ SkStream* FontConfigTypeface::onOpenStream(int* ttcIndex) const {
SkAutoTMalloc<uint8_t> allocMemory(length);
stream->rewind();
if (length == stream->read(allocMemory.get(), length)) {
- SkAutoTUnref<SkMemoryStream> copyStream(new SkMemoryStream());
+ SkAutoTDelete<SkMemoryStream> copyStream(new SkMemoryStream());
copyStream->setMemoryOwned(allocMemory.detach(), length);
return copyStream.detach();
}
-
- stream->rewind();
- stream->ref();
} else {
SkAutoTUnref<SkFontConfigInterface> fci(RefFCI());
if (NULL == fci.get()) {
diff --git a/src/ports/SkFontHost_linux.cpp b/src/ports/SkFontHost_linux.cpp
index ec8e33870c..b6fab622e8 100644
--- a/src/ports/SkFontHost_linux.cpp
+++ b/src/ports/SkFontHost_linux.cpp
@@ -139,7 +139,7 @@ protected:
private:
SkString fPath;
- const SkAutoTUnref<SkStreamAsset> fStream;
+ const SkAutoTDelete<SkStreamAsset> fStream;
typedef SkTypeface_Custom INHERITED;
};
@@ -283,13 +283,12 @@ protected:
}
SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const SK_OVERRIDE {
- SkAutoTUnref<SkStream> stream(new SkMemoryStream(data));
- return this->createFromStream(stream, ttcIndex);
+ return this->createFromStream(new SkMemoryStream(data), ttcIndex);
}
SkTypeface* onCreateFromStream(SkStream* stream, int ttcIndex) const SK_OVERRIDE {
+ SkAutoTDelete<SkStream> streamDeleter(stream);
if (NULL == stream || stream->getLength() <= 0) {
- SkDELETE(stream);
return NULL;
}
@@ -298,15 +297,15 @@ protected:
SkString name;
if (fScanner.scanFont(stream, ttcIndex, &name, &style, &isFixedPitch)) {
return SkNEW_ARGS(SkTypeface_Stream, (style, isFixedPitch, false, name,
- stream, ttcIndex));
+ streamDeleter.detach(), ttcIndex));
} else {
return NULL;
}
}
SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const SK_OVERRIDE {
- SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path));
- return stream.get() ? this->createFromStream(stream, ttcIndex) : NULL;
+ SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
+ return stream.get() ? this->createFromStream(stream.detach(), ttcIndex) : NULL;
}
virtual SkTypeface* onLegacyCreateTypeface(const char familyName[],
@@ -341,7 +340,7 @@ private:
while (iter.next(&name, false)) {
SkString filename(SkOSPath::Join(directory.c_str(), name.c_str()));
- SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(filename.c_str()));
+ SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(filename.c_str()));
if (!stream.get()) {
SkDebugf("---- failed to open <%s>\n", filename.c_str());
continue;
diff --git a/src/ports/SkFontHost_win.cpp b/src/ports/SkFontHost_win.cpp
index 1ec2bb5cf8..70f3ddfdf8 100755
--- a/src/ports/SkFontHost_win.cpp
+++ b/src/ports/SkFontHost_win.cpp
@@ -1924,6 +1924,7 @@ static HANDLE activate_font(SkData* fontData) {
return fontHandle;
}
+// Does not affect ownership of stream.
static SkTypeface* create_from_stream(SkStream* stream) {
// Create a unique and unpredictable font name.
// Avoids collisions and access from CSS.
@@ -2481,19 +2482,18 @@ protected:
}
SkTypeface* onCreateFromStream(SkStream* stream, int ttcIndex) const SK_OVERRIDE {
+ SkAutoTDelete<SkStream> streamDeleter(stream);
return create_from_stream(stream);
}
SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const SK_OVERRIDE {
// could be in base impl
- SkAutoTUnref<SkStream> stream(SkNEW_ARGS(SkMemoryStream, (data)));
- return this->createFromStream(stream);
+ return this->createFromStream(SkNEW_ARGS(SkMemoryStream, (data)));
}
SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const SK_OVERRIDE {
// could be in base impl
- SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path));
- return this->createFromStream(stream);
+ return this->createFromStream(SkStream::NewFromFile(path));
}
virtual SkTypeface* onLegacyCreateTypeface(const char familyName[],
diff --git a/src/ports/SkFontMgr_android.cpp b/src/ports/SkFontMgr_android.cpp
index 794790be59..ce931941cd 100644
--- a/src/ports/SkFontMgr_android.cpp
+++ b/src/ports/SkFontMgr_android.cpp
@@ -104,7 +104,7 @@ public:
bool isFixedPitch,
const SkString& familyName)
: INHERITED(index, style, isFixedPitch, familyName)
- , fStream(SkRef(stream)) { }
+ , fStream(stream) { }
virtual void onGetFontDescriptor(SkFontDescriptor* desc,
bool* serialize) const SK_OVERRIDE {
@@ -121,7 +121,7 @@ public:
}
private:
- SkAutoTUnref<SkStream> fStream;
+ SkAutoTDelete<SkStream> fStream;
typedef SkTypeface_Android INHERITED;
};
@@ -152,7 +152,7 @@ public:
SkString pathName;
get_path_for_sys_fonts(basePath, fontFile.fFileName, &pathName);
- SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(pathName.c_str()));
+ SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(pathName.c_str()));
if (!stream.get()) {
DEBUG_FONT(("---- SystemFonts[%d] file=%s (NOT EXIST)", i, pathName.c_str()));
continue;
@@ -409,23 +409,23 @@ protected:
}
SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const SK_OVERRIDE {
- SkAutoTUnref<SkStream> stream(new SkMemoryStream(data));
- return this->createFromStream(stream, ttcIndex);
+ return this->createFromStream(new SkMemoryStream(data), ttcIndex);
}
SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const SK_OVERRIDE {
- SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path));
- return stream.get() ? this->createFromStream(stream, ttcIndex) : NULL;
+ SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
+ return stream.get() ? this->createFromStream(stream.detach(), ttcIndex) : NULL;
}
SkTypeface* onCreateFromStream(SkStream* stream, int ttcIndex) const SK_OVERRIDE {
+ SkAutoTDelete<SkStream> streamDeleter(stream);
bool isFixedPitch;
SkFontStyle style;
SkString name;
if (!fScanner.scanFont(stream, ttcIndex, &name, &style, &isFixedPitch)) {
return NULL;
}
- return SkNEW_ARGS(SkTypeface_AndroidStream, (stream, ttcIndex,
+ return SkNEW_ARGS(SkTypeface_AndroidStream, (streamDeleter.detach(), ttcIndex,
style, isFixedPitch, name));
}
diff --git a/src/ports/SkFontMgr_fontconfig.cpp b/src/ports/SkFontMgr_fontconfig.cpp
index b5e4eaed64..28df5d4790 100644
--- a/src/ports/SkFontMgr_fontconfig.cpp
+++ b/src/ports/SkFontMgr_fontconfig.cpp
@@ -378,7 +378,7 @@ public:
/** @param stream does not take ownership of the reference, does take ownership of the stream.*/
SkTypeface_stream(const SkFontStyle& style, bool fixedWidth, int index, SkStreamAsset* stream)
: INHERITED(style, SkTypefaceCache::NewFontID(), fixedWidth)
- , fStream(SkRef(stream))
+ , fStream(stream)
, fIndex(index)
{ };
@@ -397,7 +397,7 @@ public:
}
private:
- SkAutoTUnref<SkStreamAsset> fStream;
+ SkAutoTDelete<SkStreamAsset> fStream;
int fIndex;
typedef SkTypeface_FreeType INHERITED;
@@ -809,8 +809,8 @@ protected:
return this->matchFamilyStyle(get_string(fcTypeface->fPattern, FC_FAMILY), style);
}
- /** @param stream does not take ownership of the reference. */
- SkTypeface* onCreateFromStream(SkStream* stream, int ttcIndex) const SK_OVERRIDE {
+ SkTypeface* onCreateFromStream(SkStream* inputStream, int ttcIndex) const SK_OVERRIDE {
+ SkAutoTDelete<SkStream> stream(inputStream);
const size_t length = stream->getLength();
if (length <= 0 || (1u << 30) < length) {
return NULL;
@@ -823,17 +823,15 @@ protected:
}
return SkNEW_ARGS(SkTypeface_stream, (style, isFixedWidth, ttcIndex,
- static_cast<SkStreamAsset*>(stream)));
+ static_cast<SkStreamAsset*>(stream.detach())));
}
SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const SK_OVERRIDE {
- SkAutoTUnref<SkStreamAsset> stream(SkNEW_ARGS(SkMemoryStream, (data)));
- return this->createFromStream(stream, ttcIndex);
+ return this->createFromStream(SkNEW_ARGS(SkMemoryStream, (data)), ttcIndex);
}
SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const SK_OVERRIDE {
- SkAutoTUnref<SkStreamAsset> stream(SkStream::NewFromFile(path));
- return this->createFromStream(stream, ttcIndex);
+ return this->createFromStream(SkStream::NewFromFile(path), ttcIndex);
}
virtual SkTypeface* onLegacyCreateTypeface(const char familyName[],
diff --git a/src/ports/SkFontMgr_win_dw.cpp b/src/ports/SkFontMgr_win_dw.cpp
index e7ea980872..31c1d3fef4 100644
--- a/src/ports/SkFontMgr_win_dw.cpp
+++ b/src/ports/SkFontMgr_win_dw.cpp
@@ -34,6 +34,7 @@ public:
UINT32 fontFileReferenceKeySize,
IDWriteFontFileStream** fontFileStream);
+ // Takes ownership of stream.
static HRESULT Create(SkStream* stream, StreamFontFileLoader** streamFontFileLoader) {
*streamFontFileLoader = new StreamFontFileLoader(stream);
if (NULL == streamFontFileLoader) {
@@ -42,10 +43,10 @@ public:
return S_OK;
}
- SkAutoTUnref<SkStream> fStream;
+ SkAutoTDelete<SkStream> fStream;
private:
- StreamFontFileLoader(SkStream* stream) : fRefCount(1), fStream(SkRef(stream)) { }
+ StreamFontFileLoader(SkStream* stream) : fRefCount(1), fStream(stream) { }
virtual ~StreamFontFileLoader() { }
ULONG fRefCount;
@@ -80,7 +81,7 @@ HRESULT StreamFontFileLoader::CreateStreamFromKey(
IDWriteFontFileStream** fontFileStream)
{
SkTScopedComPtr<SkDWriteFontFileStreamWrapper> stream;
- HR(SkDWriteFontFileStreamWrapper::Create(fStream, &stream));
+ HR(SkDWriteFontFileStreamWrapper::Create(fStream->duplicate(), &stream));
*fontFileStream = stream.release();
return S_OK;
}
@@ -535,6 +536,7 @@ private:
SkTypeface* SkFontMgr_DirectWrite::onCreateFromStream(SkStream* stream, int ttcIndex) const {
SkTScopedComPtr<StreamFontFileLoader> fontFileLoader;
+ // This transfers ownership of stream to the new object.
HRN(StreamFontFileLoader::Create(stream, &fontFileLoader));
HRN(fFactory->RegisterFontFileLoader(fontFileLoader.get()));
SkAutoIDWriteUnregister<StreamFontFileLoader> autoUnregisterFontFileLoader(
@@ -580,13 +582,11 @@ SkTypeface* SkFontMgr_DirectWrite::onCreateFromStream(SkStream* stream, int ttcI
}
SkTypeface* SkFontMgr_DirectWrite::onCreateFromData(SkData* data, int ttcIndex) const {
- SkAutoTUnref<SkStream> stream(SkNEW_ARGS(SkMemoryStream, (data)));
- return this->createFromStream(stream, ttcIndex);
+ return this->createFromStream(SkNEW_ARGS(SkMemoryStream, (data)), ttcIndex);
}
SkTypeface* SkFontMgr_DirectWrite::onCreateFromFile(const char path[], int ttcIndex) const {
- SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path));
- return this->createFromStream(stream, ttcIndex);
+ return this->createFromStream(SkStream::NewFromFile(path), ttcIndex);
}
HRESULT SkFontMgr_DirectWrite::getByFamilyName(const WCHAR wideFamilyName[],
diff --git a/src/ports/SkImageDecoder_empty.cpp b/src/ports/SkImageDecoder_empty.cpp
index 069cf7fbcb..e5ad6da6eb 100644
--- a/src/ports/SkImageDecoder_empty.cpp
+++ b/src/ports/SkImageDecoder_empty.cpp
@@ -11,10 +11,9 @@
#include "SkImageDecoder.h"
#include "SkImageEncoder.h"
#include "SkMovie.h"
+#include "SkStream.h"
class SkColorTable;
-class SkStream;
-class SkStreamRewindable;
// Empty implementations for SkImageDecoder.
@@ -48,6 +47,13 @@ bool SkImageDecoder::buildTileIndex(SkStreamRewindable*, int *width, int *height
return false;
}
+bool SkImageDecoder::onBuildTileIndex(SkStreamRewindable* stream,
+ int* /*width*/, int* /*height*/) {
+ SkDELETE(stream);
+ return false;
+}
+
+
bool SkImageDecoder::decodeSubset(SkBitmap*, const SkIRect&, SkColorType) {
return false;
}
diff --git a/src/ports/SkTypeface_win_dw.cpp b/src/ports/SkTypeface_win_dw.cpp
index 563a5153d6..b7778060f2 100644
--- a/src/ports/SkTypeface_win_dw.cpp
+++ b/src/ports/SkTypeface_win_dw.cpp
@@ -186,7 +186,7 @@ int DWriteFontTypeface::onGetTableTags(SkFontTableTag tags[]) const {
}
int ttcIndex;
- SkAutoTUnref<SkStream> stream(this->openStream(&ttcIndex));
+ SkAutoTDelete<SkStream> stream(this->openStream(&ttcIndex));
return stream.get() ? SkFontStream::GetTableTags(stream, ttcIndex, tags) : 0;
}