return false;
}
- SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path));
+ SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
if (stream.get() == NULL) {
SkDebugf("Could not read %s.\n", path);
return false;
void SkDebuggerGUI::loadPicture(const SkString& fileName) {
fFileName = fileName;
fLoading = true;
- SkStream* stream = SkNEW_ARGS(SkFILEStream, (fileName.c_str()));
+ SkAutoTDelete<SkStream> stream(SkNEW_ARGS(SkFILEStream, (fileName.c_str())));
SkPicture* picture = SkPicture::CreateFromStream(stream);
if (NULL == picture) {
QMessageBox::critical(this, "Error loading file", "Couldn't read file, sorry.");
- SkSafeUnref(stream);
return;
}
fSkipCommands[i] = false;
}
- SkSafeUnref(stream);
SkSafeUnref(picture);
fActionProfile.setDisabled(false);
#include "SkOSFile.h"
#include "SkPictureRecorder.h"
#include "SkRandom.h"
+#include "SkStream.h"
namespace DM {
return "";
}
// Decode random subsets. This is a little involved.
- SkMemoryStream stream(encoded->data(), encoded->size());
- SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(&stream));
+ SkAutoTDelete<SkMemoryStream> stream(new SkMemoryStream(encoded));
+ SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream.get()));
if (!decoder) {
return SkStringPrintf("Can't find a good decoder for %s.", fPath.c_str());
}
+ stream->rewind();
int w,h;
- if (!decoder->buildTileIndex(&stream, &w, &h) || w*h == 1) {
+ if (!decoder->buildTileIndex(stream.detach(), &w, &h) || w*h == 1) {
return ""; // Not an error. Subset decoding is not always supported.
}
SkRandom rand;
SKPSrc::SKPSrc(SkString path) : fPath(path) {}
Error SKPSrc::draw(SkCanvas* canvas) const {
- SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(fPath.c_str()));
+ SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(fPath.c_str()));
if (!stream) {
return SkStringPrintf("Couldn't read %s.", fPath.c_str());
}
// Serialize it and then deserialize it.
SkDynamicMemoryWStream wStream;
pic->serialize(&wStream);
- SkAutoTUnref<SkStream> rStream(wStream.detachAsStream());
+ SkAutoTDelete<SkStream> rStream(wStream.detachAsStream());
SkAutoTUnref<SkPicture> deserialized(SkPicture::CreateFromStream(rStream));
// Turn that deserialized picture into a Src, draw it into our Sink to fill bitmap or stream.
#include "SkData.h"
#include "SkGPipe.h"
#include "SkPicture.h"
-#include "SkStream.h"
#include "gm.h"
namespace DM {
// TODO(edisonn): replace it with a SkSmartStream which would know to to efficiently
// deal with a HTTP stream.
// FIXME: Untested.
+ // Does not affect ownership of SkStream.
static SkPdfRenderer* CreateFromStream(SkStream*);
// Create a new renderer from a file.
static SkPdfRenderer* CreateFromFile(const char* filename);
void reportPdfRenderStats();
// Renders a page of a pdf in a bitmap.
+// Does not affect ownership of stream.
bool SkPDFNativeRenderToBitmap(SkStream* stream,
SkBitmap* output,
int page = 0,
// TODO(edisonn) should be deprecated
// FIXME: Untested.
+ // Does not affect ownership of stream.
SkPdfNativeDoc(SkStream* stream);
~SkPdfNativeDoc();
protected:
void onOnceBeforeDraw() SK_OVERRIDE {
SkString filename = GetResourcePath("/Funkster.ttf");
- SkAutoTUnref<SkFILEStream> stream(new SkFILEStream(filename.c_str()));
+ SkAutoTDelete<SkFILEStream> stream(new SkFILEStream(filename.c_str()));
if (!stream->isValid()) {
SkDebugf("Could not find Funkster.ttf, please set --resourcePath correctly.\n");
return;
}
- fTypeface = SkTypeface::CreateFromStream(stream);
+ fTypeface = SkTypeface::CreateFromStream(stream.detach());
}
SkString onShortName() SK_OVERRIDE {
void setFont(SkPaint* paint) SK_OVERRIDE {
if (!fTypeface) {
SkString filename = GetResourcePath("/Funkster.ttf");
- SkAutoTUnref<SkFILEStream> stream(new SkFILEStream(filename.c_str()));
+ SkAutoTDelete<SkFILEStream> stream(new SkFILEStream(filename.c_str()));
if (!stream->isValid()) {
SkDebugf("Could not find Funkster.ttf, please set --resourcePath "
"correctly.\n");
return;
}
- fTypeface.reset(SkTypeface::CreateFromStream(stream));
+ fTypeface.reset(SkTypeface::CreateFromStream(stream.detach()));
}
paint->setTypeface(fTypeface);
}
protected:
void onOnceBeforeDraw() SK_OVERRIDE {
SkString filename = GetResourcePath("/Funkster.ttf");
- SkAutoTUnref<SkFILEStream> stream(new SkFILEStream(filename.c_str()));
+ SkAutoTDelete<SkFILEStream> stream(new SkFILEStream(filename.c_str()));
if (!stream->isValid()) {
SkDebugf("Could not find Funkster.ttf, please set --resourcePath correctly.\n");
return;
}
- fTypeface = SkTypeface::CreateFromStream(stream);
+ fTypeface = SkTypeface::CreateFromStream(stream.detach());
}
uint32_t onGetFlags() const SK_OVERRIDE {
* The built index will be saved in the decoder, and the image size will
* be returned in width and height.
*
+ * Takes ownership of the SkStreamRewindable, on success or failure.
+ *
* Return true for success or false on failure.
*/
bool buildTileIndex(SkStreamRewindable*, int *width, int *height);
// must be overridden in subclasses. This guy is called by decode(...)
virtual Result onDecode(SkStream*, SkBitmap* bitmap, Mode) = 0;
- // If the decoder wants to support tiled based decoding,
- // this method must be overridden. This guy is called by buildTileIndex(...)
- virtual bool onBuildTileIndex(SkStreamRewindable*, int* /*width*/, int* /*height*/) {
- return false;
- }
+ // If the decoder wants to support tiled based decoding, this method must be overridden.
+ // This is called by buildTileIndex(...)
+ virtual bool onBuildTileIndex(SkStreamRewindable*, int* /*width*/, int* /*height*/);
// If the decoder wants to support tiled based decoding,
// this method must be overridden. This guy is called by decodeRegion(...)
/**
* Recreate a picture that was serialized into a stream.
- * @param SkStream Serialized picture data.
+ * @param SkStream Serialized picture data. Ownership is unchanged by this call.
* @param proc Function pointer for installing pixelrefs on SkBitmaps representing the
* encoded bitmap data from the stream.
* @return A new SkPicture representing the serialized data, or NULL if the stream is
* no more data (at EOF or hit an error). The caller should *not* call again
* in hopes of fulfilling more of the request.
*/
-class SK_API SkStream : public SkRefCnt { //TODO: remove SkRefCnt
+class SK_API SkStream : public SkNoncopyable {
public:
+ virtual ~SkStream() {}
+
+ /**
+ * @deprecated
+ * SkStream is no longer ref counted, but we leave this here for staging.
+ */
+ void unref() {
+ SkDebugf("SkStream is no longer ref counted!");
+ }
+
/**
* Attempts to open the specified file, and return a stream to it (using
- * mmap if available). On success, the caller must call unref() on the
- * returned object. On failure, returns NULL.
+ * mmap if available). On success, the caller is responsible for deleting.
+ * On failure, returns NULL.
*/
static SkStreamAsset* NewFromFile(const char path[]);
- SK_DECLARE_INST_COUNT(SkStream)
-
/** Reads or skips size number of bytes.
* If buffer == NULL, skip size bytes, return how many were skipped.
* If buffer != NULL, copy size bytes into buffer, return how many were copied.
/** Returns the starting address for the data. If this cannot be done, returns NULL. */
//TODO: replace with virtual const SkData* getData()
virtual const void* getMemoryBase() { return NULL; }
-
-private:
- typedef SkRefCnt INHERITED;
};
/** SkStreamRewindable is a SkStream for which rewind and duplicate are required. */
to a typeface referring to the same font. If that font is not available,
return null. If an instance is returned, the caller is responsible for
calling unref() when they are done with it.
+ Does not affect ownership of SkStream.
*/
static SkTypeface* Deserialize(SkStream*);
* If ttcIndex is not null, it is set to the TrueTypeCollection index
* of this typeface within the stream, or 0 if the stream is not a
* collection.
+ * The caller is responsible for deleting the stream.
*/
SkStream* openStream(int* ttcIndex) const;
* The SkData version of this function is preferred. If the stream
* has an underlying SkData (such as a SkMemoryStream) pass that in.
*
- * This object will unref the stream when done or on failure. Since
- * streams have internal state (position), the caller should not pass
- * a shared stream in. Pass either a new duplicated stream in or
- * transfer ownership of the stream. This factory asserts
- * stream->unique().
- *
- * For example:
- * SkStreamRewindable* stream;
- * ...
- * SkImageGenerator* gen
- * = SkDecodingImageGenerator::Create(
- * stream->duplicate(), SkDecodingImageGenerator::Options());
- * ...
- * SkDELETE(gen);
+ * This object, if non-NULL, takes ownership of stream and deletes stream
+ * upon deletion. If NULL is returned, stream is deleted immediately.
*
* @param Options (see above)
*
* @param stream SkStream to buffer. If stream is NULL, NULL is
* returned. When this call succeeds (i.e. returns non NULL),
* SkFrontBufferedStream is expected to be the only owner of
- * stream, so it should be unreffed and no longer used directly.
+ * stream, so it should no be longer used directly.
+ * SkFrontBufferedStream will delete stream upon deletion.
* @param minBufferSize Minimum size of buffer required.
* @return An SkStream that can buffer at least minBufferSize, or
- * NULL on failure.
+ * NULL on failure. The caller is required to delete when finished with
+ * this object.
*/
static SkStreamRewindable* Create(SkStream* stream, size_t minBufferSize);
};
*/
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 an
- * owner of the stream, so the caller must still manage its ownership.
- *
- * To hand-off ownership of the stream to the provider, the caller must do
- * something like the following:
- *
- * SkStream* stream = new ...;
- * CGDataProviderRef provider = SkStreamToDataProvider(stream);
- * stream->unref();
+ * 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.
*
- * Now when the provider is finally deleted, it will delete the stream.
+ * When the provider is finally deleted, it will delete the stream.
*/
CGDataProviderRef SkCreateDataProviderFromStream(SkStream*);
}
bool SkAnimatorView::decodeFile(const char path[]) {
- SkFILEStream* is = new SkFILEStream(path);
- SkAutoUnref aur(is);
- return is->isValid() && this->decodeStream(is);
+ SkAutoTDelete<SkStream> is(SkStream::NewFromFile(path));
+ return is.get() != NULL && this->decodeStream(is);
}
bool SkAnimatorView::decodeMemory(const void* buffer, size_t size) {
- SkMemoryStream* is = new SkMemoryStream(buffer, size);
- SkAutoUnref aur(is);
- return this->decodeStream(is);
+ SkMemoryStream is(buffer, size);
+ return this->decodeStream(&is);
}
static const SkDOMNode* find_nodeID(const SkDOM& dom,
// SkDebugf("animator decode %s\n", uri);
// SkStream* stream = SkStream::GetURIStream(fPrefix.c_str(), uri);
- SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(uri));
+ SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(uri));
if (stream.get()) {
bool success = decodeStream(stream);
if (hasError() && fError.hasNoun() == false)
// SkDebugf("animator decode %s\n", uri);
// SkStream* stream = SkStream::GetURIStream(fMaker->fPrefix.c_str(), uri);
- SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(uri));
+ SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(uri));
if (stream.get()) {
this->setURIBase(uri);
return decodeStream(stream);
fBitmap.reset();
//SkStream* stream = SkStream::GetURIStream(fUriBase, src.c_str());
- SkAutoTUnref<SkStreamAsset> stream(SkStream::NewFromFile(src.c_str()));
+ SkAutoTDelete<SkStreamAsset> stream(SkStream::NewFromFile(src.c_str()));
if (stream.get()) {
SkImageDecoder::DecodeStream(stream, &fBitmap);
}
#include "SkString.h"
#include "SkTypeface.h"
-class SkWStream;
-
class SkFontDescriptor {
public:
SkFontDescriptor(SkTypeface::Style = SkTypeface::kNormal);
+ // Does not affect ownership of SkStream.
SkFontDescriptor(SkStream*);
void serialize(SkWStream*);
const char* getFullName() const { return fFullName.c_str(); }
const char* getPostscriptName() const { return fPostscriptName.c_str(); }
const char* getFontFileName() const { return fFontFileName.c_str(); }
- SkStream* getFontData() const { return fFontData; }
+ bool hasFontData() const { return fFontData.get() != NULL; }
+ // Transfers ownership to the caller.
+ SkStream* transferFontData() { return fFontData.detach(); }
int getFontIndex() const { return fFontIndex; }
void setFamilyName(const char* name) { fFamilyName.set(name); }
SkString fFullName;
SkString fPostscriptName;
SkString fFontFileName;
- SkAutoTUnref<SkStream> fFontData;
+ SkAutoTDelete<SkStream> fFontData;
int fFontIndex;
SkTypeface::Style fStyle;
#include "SkFontMgr.h"
#include "SkLazyPtr.h"
+#include "SkStream.h"
#include "SkTypes.h"
class SkFontStyle;
-class SkStream;
class SkTypeface;
class SkEmptyFontStyleSet : public SkFontStyleSet {
SkTypeface* onCreateFromData(SkData*, int) const SK_OVERRIDE {
return NULL;
}
- SkTypeface* onCreateFromStream(SkStream*, int) const SK_OVERRIDE {
+ SkTypeface* onCreateFromStream(SkStream* stream, int) const SK_OVERRIDE {
+ SkDELETE(stream);
return NULL;
}
SkTypeface* onCreateFromFile(const char[], int) const SK_OVERRIDE {
if (typeface) {
SkDynamicMemoryWStream ostream;
typeface->serialize(&ostream);
- SkAutoTUnref<SkStreamAsset> istream(ostream.detachAsStream());
+ SkAutoTDelete<SkStreamAsset> istream(ostream.detachAsStream());
SkFontDescriptor descriptor(istream);
str->append("<dt>Font Family Name:</dt><dd>");
class SkPictureData {
public:
SkPictureData(const SkPictureRecord& record, const SkPictInfo&, bool deepCopyOps);
+ // Does not affect ownership of SkStream.
static SkPictureData* CreateFromStream(SkStream*,
const SkPictInfo&,
SkPicture::InstallPixelRefProc);
protected:
explicit SkPictureData(const SkPictInfo& info);
+ // Does not affect ownership of SkStream.
bool parseStream(SkStream*, SkPicture::InstallPixelRefProc);
bool parseBuffer(SkReadBuffer& buffer);
void init();
// these help us with reading/writing
+ // Does not affect ownership of SkStream.
bool parseStreamTag(SkStream*, uint32_t tag, uint32_t size, SkPicture::InstallPixelRefProc);
bool parseBufferTag(SkReadBuffer&, uint32_t tag, uint32_t size);
void flattenToBuffer(SkWriteBuffer&) const;
}
if (!fName.isEmpty()) {
- SkAutoTUnref<SkFILEStream> that(new SkFILEStream(fName.c_str()));
+ SkAutoTDelete<SkFILEStream> that(new SkFILEStream(fName.c_str()));
if (sk_fidentical(that->fFILE, this->fFILE)) {
return that.detach();
}
}
SkStreamAsset* SkFILEStream::fork() const {
- SkAutoTUnref<SkStreamAsset> that(this->duplicate());
+ SkAutoTDelete<SkStreamAsset> that(this->duplicate());
that->seek(this->getPosition());
return that.detach();
}
}
SkMemoryStream* SkMemoryStream::fork() const {
- SkAutoTUnref<SkMemoryStream> that(this->duplicate());
+ SkAutoTDelete<SkMemoryStream> that(this->duplicate());
that->seek(fOffset);
return that.detach();
}
}
SkBlockMemoryStream* fork() const SK_OVERRIDE {
- SkAutoTUnref<SkBlockMemoryStream> that(this->duplicate());
+ SkAutoTDelete<SkBlockMemoryStream> that(this->duplicate());
that->fCurrent = this->fCurrent;
that->fOffset = this->fOffset;
that->fCurrentOffset = this->fCurrentOffset;
// file access.
SkFILEStream* stream = SkNEW_ARGS(SkFILEStream, (path));
if (!stream->isValid()) {
- stream->unref();
+ SkDELETE(stream);
stream = NULL;
}
return stream;
if (!stream) {
return NULL;
}
- SkAutoTUnref<SkStreamRewindable> dupStream(stream->duplicate());
+ SkAutoTDelete<SkStreamRewindable> dupStream(stream->duplicate());
if (dupStream) {
return dupStream.detach();
}
this->onGetFontDescriptor(&desc, &isLocal);
// Embed font data if it's a local font.
- if (isLocal && NULL == desc.getFontData()) {
+ if (isLocal && !desc.hasFontData()) {
int ttcIndex;
desc.setFontData(this->onOpenStream(&ttcIndex));
desc.setFontIndex(ttcIndex);
this->onGetFontDescriptor(&desc, &ignoredIsLocal);
// Always embed font data.
- if (NULL == desc.getFontData()) {
+ if (!desc.hasFontData()) {
int ttcIndex;
desc.setFontData(this->onOpenStream(&ttcIndex));
desc.setFontIndex(ttcIndex);
SkTypeface* SkTypeface::Deserialize(SkStream* stream) {
SkFontDescriptor desc(stream);
- SkStream* data = desc.getFontData();
+ SkStream* data = desc.transferFontData();
if (data) {
SkTypeface* typeface = SkTypeface::CreateFromStream(data, desc.getFontIndex());
if (typeface) {
fontPackageBuffer.realloc(bytesWritten);
}
- SkAutoTUnref<SkMemoryStream> newStream(new SkMemoryStream());
+ SkAutoTDelete<SkMemoryStream> newStream(new SkMemoryStream());
newStream->setMemoryOwned(fontPackageBuffer.detach(), bytesWritten + extra);
SkTScopedComPtr<IStream> newIStream;
SkTypeface* onCreateFromData(SkData*, int ttcIndex) const SK_OVERRIDE { return NULL; }
SkTypeface* onCreateFromStream(SkStream* stream, int ttcIndex) const SK_OVERRIDE {
+ SkAutoTDelete<SkStream> streamDeleter(stream);
const size_t length = stream->getLength();
if (!length) {
return NULL;
return NULL;
}
- SkTypeface* face = FontConfigTypeface::Create(style, isFixedWidth, stream);
+ SkTypeface* face = FontConfigTypeface::Create(style, isFixedWidth, streamDeleter.detach());
return face;
}
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[],
// No exact match, but did find a data match.
if (dataTypeface.get() != NULL) {
- SkAutoTUnref<SkStream> stream(dataTypeface->openStream(NULL));
+ SkAutoTDelete<SkStream> stream(dataTypeface->openStream(NULL));
if (stream.get() != NULL) {
- return fImpl->createFromStream(stream.get(), dataTypefaceIndex);
+ return fImpl->createFromStream(stream.detach(), dataTypefaceIndex);
}
}
// No data match, request data and add entry.
- SkAutoTUnref<SkStreamAsset> stream(fProxy->getData(id.fDataId));
+ SkAutoTDelete<SkStreamAsset> stream(fProxy->getData(id.fDataId));
if (stream.get() == NULL) {
return NULL;
}
- SkAutoTUnref<SkTypeface> typeface(fImpl->createFromStream(stream, id.fTtcIndex));
+ SkAutoTUnref<SkTypeface> typeface(fImpl->createFromStream(stream.detach(), id.fTtcIndex));
if (typeface.get() == NULL) {
return NULL;
}
}
int faceIndex;
- SkAutoTUnref<SkStream> fontStream(typeface->openStream(&faceIndex));
+ SkAutoTDelete<SkStream> fontStream(typeface->openStream(&faceIndex));
const size_t fontDataLength = fontStream->getLength();
if (0 == fontDataLength) {
public:
virtual ~DecodingImageGenerator();
- SkData* fData;
- SkStreamRewindable* fStream;
- const SkImageInfo fInfo;
- const int fSampleSize;
- const bool fDitherImage;
+ SkData* fData;
+ SkAutoTDelete<SkStreamRewindable> fStream;
+ const SkImageInfo fInfo;
+ const int fSampleSize;
+ const bool fDitherImage;
DecodingImageGenerator(SkData* data,
SkStreamRewindable* stream,
DecodingImageGenerator::~DecodingImageGenerator() {
SkSafeUnref(fData);
- fStream->unref();
}
SkData* DecodingImageGenerator::onRefEncodedData() {
SkStreamRewindable* stream,
const SkDecodingImageGenerator::Options& opts) {
SkASSERT(stream);
- SkAutoTUnref<SkStreamRewindable> autoStream(stream); // always unref this.
+ SkAutoTDelete<SkStreamRewindable> autoStream(stream); // always delete this
SkAssertResult(autoStream->rewind());
SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(autoStream));
if (NULL == decoder.get()) {
}
SkStreamRewindable* stream = SkNEW_ARGS(SkMemoryStream, (data));
SkASSERT(stream != NULL);
- SkASSERT(stream->unique());
return CreateDecodingImageGenerator(data, stream, opts);
}
SkStreamRewindable* stream,
const SkDecodingImageGenerator::Options& opts) {
SkASSERT(stream != NULL);
- SkASSERT(stream->unique());
- if ((stream == NULL) || !stream->unique()) {
- SkSafeUnref(stream);
+ if (stream == NULL) {
return NULL;
}
return CreateDecodingImageGenerator(NULL, stream, opts);
return this->onBuildTileIndex(stream, width, height);
}
+bool SkImageDecoder::onBuildTileIndex(SkStreamRewindable* stream, int* /*width*/,
+ int* /*height*/) {
+ SkDELETE(stream);
+ return false;
+}
+
+
bool SkImageDecoder::cropBitmap(SkBitmap *dst, SkBitmap *src, int sampleSize,
int dstX, int dstY, int width, int height,
int srcX, int srcY) {
SkASSERT(file);
SkASSERT(bm);
- SkAutoTUnref<SkStreamRewindable> stream(SkStream::NewFromFile(file));
+ SkAutoTDelete<SkStreamRewindable> stream(SkStream::NewFromFile(file));
if (stream.get()) {
if (SkImageDecoder::DecodeStream(stream, bm, pref, mode, format)) {
bm->pixelRef()->setURI(file);
#ifdef SK_BUILD_FOR_ANDROID
class SkJPEGImageIndex {
public:
+ // Takes ownership of stream.
SkJPEGImageIndex(SkStreamRewindable* stream, SkImageDecoder* decoder)
: fSrcMgr(stream, decoder)
+ , fStream(stream)
, fInfoInitialized(false)
, fHuffmanCreated(false)
, fDecompressStarted(false)
private:
skjpeg_source_mgr fSrcMgr;
+ SkAutoTDelete<SkStream> fStream;
jpeg_decompress_struct fCInfo;
huffman_index fHuffmanIndex;
bool fInfoInitialized;
class SkPNGImageIndex {
public:
+ // Takes ownership of stream.
SkPNGImageIndex(SkStreamRewindable* stream, png_structp png_ptr, png_infop info_ptr)
: fStream(stream)
, fPng_ptr(png_ptr)
, fInfo_ptr(info_ptr)
, fColorType(kUnknown_SkColorType) {
SkASSERT(stream != NULL);
- stream->ref();
}
~SkPNGImageIndex() {
if (fPng_ptr) {
}
}
- SkAutoTUnref<SkStreamRewindable> fStream;
+ SkAutoTDelete<SkStreamRewindable> fStream;
png_structp fPng_ptr;
png_infop fInfo_ptr;
SkColorType fColorType;
#ifdef SK_BUILD_FOR_ANDROID
bool SkPNGImageDecoder::onBuildTileIndex(SkStreamRewindable* sk_stream, int *width, int *height) {
+ SkAutoTDelete<SkStreamRewindable> streamDeleter(sk_stream);
png_structp png_ptr;
png_infop info_ptr;
if (fImageIndex) {
SkDELETE(fImageIndex);
}
- fImageIndex = SkNEW_ARGS(SkPNGImageIndex, (sk_stream, png_ptr, info_ptr));
+ fImageIndex = SkNEW_ARGS(SkPNGImageIndex, (streamDeleter.detach(), png_ptr, info_ptr));
return true;
}
class SkWEBPImageDecoder: public SkImageDecoder {
public:
SkWEBPImageDecoder() {
- fInputStream = NULL;
fOrigWidth = 0;
fOrigHeight = 0;
fHasAlpha = 0;
}
- virtual ~SkWEBPImageDecoder() {
- SkSafeUnref(fInputStream);
- }
Format getFormat() const SK_OVERRIDE {
return kWEBP_Format;
bool setDecodeConfig(SkBitmap* decodedBitmap, int width, int height);
- SkStream* fInputStream;
+ SkAutoTDelete<SkStream> fInputStream;
int fOrigWidth;
int fOrigHeight;
int fHasAlpha;
bool SkWEBPImageDecoder::onBuildTileIndex(SkStreamRewindable* stream,
int *width, int *height) {
+ SkAutoTDelete<SkStreamRewindable> streamDeleter(stream);
int origWidth, origHeight, hasAlpha;
if (!webp_parse_header(stream, &origWidth, &origHeight, &hasAlpha)) {
return false;
*width = origWidth;
*height = origHeight;
- SkRefCnt_SafeAssign(this->fInputStream, stream);
+ this->fInputStream.reset(streamDeleter.detach());
this->fOrigWidth = origWidth;
this->fOrigHeight = origHeight;
this->fHasAlpha = hasAlpha;
///////////////////////////////////////////////////////////////////////////////
skjpeg_source_mgr::skjpeg_source_mgr(SkStream* stream, SkImageDecoder* decoder)
- : fStream(SkRef(stream))
+ : fStream(stream)
, fDecoder(decoder) {
init_source = sk_init_source;
// SkDebugf("**************** use memorybase %p %d\n", fMemoryBase, fMemoryBaseSize);
}
-skjpeg_source_mgr::~skjpeg_source_mgr() {
- SkSafeUnref(fStream);
-}
-
///////////////////////////////////////////////////////////////////////////////
static void sk_init_destination(j_compress_ptr cinfo) {
*/
struct skjpeg_source_mgr : jpeg_source_mgr {
skjpeg_source_mgr(SkStream* stream, SkImageDecoder* decoder);
- ~skjpeg_source_mgr();
- // fStream is ref'ed and unref'ed
+ // Unowned.
SkStream* fStream;
// Unowned pointer to the decoder, used to check if the decoding process
// has been cancelled.
}
SkMovie* SkMovie::DecodeFile(const char path[]) {
- SkAutoTUnref<SkStreamRewindable> stream(SkStream::NewFromFile(path));
+ SkAutoTDelete<SkStreamRewindable> stream(SkStream::NewFromFile(path));
return stream.get() ? SkMovie::DecodeStream(stream) : NULL;
}
// if the data was NUL terminated so that we can use strstr() to search it.
// Make as few copies as possible given these constraints.
SkDynamicMemoryWStream dynamicStream;
- SkAutoTUnref<SkMemoryStream> staticStream;
+ SkAutoTDelete<SkMemoryStream> staticStream;
SkData* data = NULL;
const uint8_t* src;
size_t srcLen;
const SkTDArray<uint32_t>& subset,
SkPDFStream** fontStream) {
int ttcIndex;
- SkAutoTUnref<SkStream> fontData(typeface->openStream(&ttcIndex));
+ SkAutoTDelete<SkStream> fontData(typeface->openStream(&ttcIndex));
SkASSERT(fontData.get());
size_t fontSize = fontData->getLength();
fontStream.reset(rawStream);
} else {
int ttcIndex;
- SkAutoTUnref<SkStream> fontData(
+ SkAutoTDelete<SkStream> fontData(
typeface()->openStream(&ttcIndex));
fontStream.reset(new SkPDFStream(fontData.get()));
fontSize = fontData->getLength();
case SkAdvancedTypefaceMetrics::kCFF_Font:
case SkAdvancedTypefaceMetrics::kType1CID_Font: {
int ttcIndex;
- SkAutoTUnref<SkStream> fontData(typeface()->openStream(&ttcIndex));
+ SkAutoTDelete<SkStream> fontData(typeface()->openStream(&ttcIndex));
SkAutoTUnref<SkPDFStream> fontStream(
new SkPDFStream(fontData.get()));
addResource(fontStream.get());
size_t header SK_INIT_TO_AVOID_WARNING;
size_t data SK_INIT_TO_AVOID_WARNING;
size_t trailer SK_INIT_TO_AVOID_WARNING;
- SkAutoTUnref<SkStream> rawFontData(typeface()->openStream(&ttcIndex));
+ SkAutoTDelete<SkStream> rawFontData(typeface()->openStream(&ttcIndex));
SkAutoTUnref<SkData> fontData(handle_type1_stream(rawFontData.get(), &header,
&data, &trailer));
if (fontData.get() == NULL) {
SkPDFUtils::PaintPath(paint.getStyle(), path->getFillType(),
&content);
}
- SkAutoTUnref<SkMemoryStream> glyphStream(new SkMemoryStream());
+ SkAutoTDelete<SkMemoryStream> glyphStream(new SkMemoryStream());
glyphStream->setData(content.copyToData())->unref();
SkAutoTUnref<SkPDFStream> glyphDescription(
SkPDFResourceDict* resourceDict = device->getResourceDict();
resourceDict->getReferencedResources(emptySet, &fResources, false);
- SkAutoTUnref<SkStream> content(device->content());
+ SkAutoTDelete<SkStream> content(device->content());
setData(content.get());
SkAutoTUnref<SkPDFArray> bboxArray(device->copyMediaBox());
}
return NULL;
}
- bool isOpaque = true;
- bool transparent = extractAlpha;
- SkStream* stream = NULL;
SkAutoLockPixels lock(bitmap);
if (NULL == bitmap.getPixels()) {
return NULL;
}
+ bool isOpaque = true;
+ bool transparent = extractAlpha;
+ SkAutoTDelete<SkStream> stream;
+
switch (colorType) {
case kIndex_8_SkColorType:
if (!extractAlpha) {
- stream = extract_index8_image(bitmap, srcRect);
+ stream.reset(extract_index8_image(bitmap, srcRect));
}
break;
case kARGB_4444_SkColorType:
- stream = extract_argb4444_data(bitmap, srcRect, extractAlpha,
- &isOpaque, &transparent);
+ stream.reset(extract_argb4444_data(bitmap, srcRect, extractAlpha,
+ &isOpaque, &transparent));
break;
case kRGB_565_SkColorType:
if (!extractAlpha) {
- stream = extract_rgb565_image(bitmap, srcRect);
+ stream.reset(extract_rgb565_image(bitmap, srcRect));
}
break;
case kN32_SkColorType:
- stream = extract_argb8888_data(bitmap, srcRect, extractAlpha,
- &isOpaque, &transparent);
+ stream.reset(extract_argb8888_data(bitmap, srcRect, extractAlpha,
+ &isOpaque, &transparent));
break;
case kAlpha_8_SkColorType:
if (!extractAlpha) {
- stream = create_black_image();
+ stream.reset(create_black_image());
} else {
- stream = extract_a8_alpha(bitmap, srcRect,
- &isOpaque, &transparent);
+ stream.reset(extract_a8_alpha(bitmap, srcRect,
+ &isOpaque, &transparent));
}
break;
default:
*isTransparent = transparent;
}
if (extractAlpha && (transparent || isOpaque)) {
- SkSafeUnref(stream);
return NULL;
}
- return stream;
+ return stream.detach();
}
static SkPDFArray* make_indexed_color_space(SkColorTable* table) {
}
bool isTransparent = false;
- SkAutoTUnref<SkStream> alphaData;
+ SkAutoTDelete<SkStream> alphaData;
if (!bitmap.isOpaque()) {
// Note that isOpaque is not guaranteed to return false for bitmaps
// with alpha support but a completely opaque alpha channel,
}
// Fallback method
if (!fStreamValid) {
- SkAutoTUnref<SkStream> stream(
+ SkAutoTDelete<SkStream> stream(
extract_image_data(fBitmap, fSrcRect, fIsAlpha, NULL));
this->setData(stream);
fStreamValid = true;
* @param stream The image stream. May be NULL. Otherwise, this
* (instead of the input bitmap) will be used as the
* PDF's content stream, possibly with lossless encoding.
+ * Will be duplicated, and left in indeterminate state.
* @param bitmap The image. If a stream is not given, its color data
* will be used as the image. If a stream is given, this
* is used for configuration only.
SkAutoTUnref<SkPDFObject> luminosityShader(SkPDFShader::GetPDFShaderByState(
fShaderState->CreateAlphaToLuminosityState()));
- SkAutoTUnref<SkStream> alphaStream(create_pattern_fill_content(-1, bbox));
+ SkAutoTDelete<SkStream> alphaStream(create_pattern_fill_content(-1, bbox));
SkAutoTUnref<SkPDFResourceDict>
resources(get_gradient_resource_dict(luminosityShader, NULL));
fResourceDict.reset(
get_gradient_resource_dict(fColorShader.get(), alphaGs.get()));
- SkAutoTUnref<SkStream> colorStream(
+ SkAutoTDelete<SkStream> colorStream(
create_pattern_fill_content(0, bbox));
setData(colorStream.get());
}
// Put the canvas into the pattern stream (fContent).
- SkAutoTUnref<SkStream> content(pattern.content());
+ SkAutoTDelete<SkStream> content(pattern.content());
setData(content.get());
SkPDFResourceDict* resourceDict = pattern.getResourceDict();
resourceDict->getReferencedResources(fResources, &fResources, false);
SkPDFStream::SkPDFStream() : fState(kUnused_State) {}
void SkPDFStream::setData(SkData* data) {
- fMemoryStream.setData(data);
- if (&fMemoryStream != fDataStream.get()) {
- fDataStream.reset(SkRef(&fMemoryStream));
- }
+ // FIXME: Don't swap if the data is the same.
+ fDataStream.reset(SkNEW_ARGS(SkMemoryStream, (data)));
}
void SkPDFStream::setData(SkStream* stream) {
// Code assumes that the stream starts at the beginning and is rewindable.
- if (&fMemoryStream == fDataStream.get()) {
- SkASSERT(&fMemoryStream != stream);
- fMemoryStream.setData(NULL);
- }
- SkASSERT(0 == fMemoryStream.getLength());
if (stream) {
// SkStreamRewindableFromSkStream will try stream->duplicate().
fDataStream.reset(SkStreamRewindableFromSkStream(stream));
SkASSERT(fDataStream.get());
} else {
- fDataStream.reset(SkRef(&fMemoryStream));
+ // Use an empty memory stream.
+ fDataStream.reset(SkNEW(SkMemoryStream));
}
}
SkFlate::Deflate(fDataStream.get(), &compressedData));
SkAssertResult(fDataStream->rewind());
if (compressedData.getOffset() < this->dataSize()) {
- SkAutoTUnref<SkStream> compressed(
+ SkAutoTDelete<SkStream> compressed(
compressedData.detachAsStream());
this->setData(compressed.get());
insertName("Filter", "FlateDecode");
// Mutex guards fState, fDataStream, and fSubstitute in public interface.
SkMutex fMutex;
- SkMemoryStream fMemoryStream; // Used by fDataStream when
- // fDataStream needs to be backed
- // by SkData.
- SkAutoTUnref<SkStreamRewindable> fDataStream;
+ SkAutoTDelete<SkStreamRewindable> fDataStream;
SkAutoTUnref<SkPDFStream> fSubstitute;
typedef SkPDFDict INHERITED;
class FontConfigTypeface : public SkTypeface_FreeType {
SkFontConfigInterface::FontIdentity fIdentity;
SkString fFamilyName;
- SkStream* fLocalStream;
+ SkAutoTDelete<SkStream> fLocalStream;
public:
static FontConfigTypeface* Create(const SkFontStyle& style,
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);
, 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;
///////////////////////////////////////////////////////////////////////////
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" {
// should have been provided by CreateFromStream()
*ttcIndex = 0;
- SkAutoTUnref<SkStream> dupStream(stream->duplicate());
+ SkAutoTDelete<SkStream> dupStream(stream->duplicate());
if (dupStream) {
return dupStream.detach();
}
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()) {
private:
SkString fPath;
- const SkAutoTUnref<SkStreamAsset> fStream;
+ const SkAutoTDelete<SkStreamAsset> fStream;
typedef SkTypeface_Custom INHERITED;
};
}
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;
}
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[],
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;
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.
}
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[],
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 {
}
private:
- SkAutoTUnref<SkStream> fStream;
+ SkAutoTDelete<SkStream> fStream;
typedef SkTypeface_Android INHERITED;
};
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;
}
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));
}
/** @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)
{ };
}
private:
- SkAutoTUnref<SkStreamAsset> fStream;
+ SkAutoTDelete<SkStreamAsset> fStream;
int fIndex;
typedef SkTypeface_FreeType INHERITED;
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;
}
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[],
UINT32 fontFileReferenceKeySize,
IDWriteFontFileStream** fontFileStream);
+ // Takes ownership of stream.
static HRESULT Create(SkStream* stream, StreamFontFileLoader** streamFontFileLoader) {
*streamFontFileLoader = new StreamFontFileLoader(stream);
if (NULL == streamFontFileLoader) {
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;
IDWriteFontFileStream** fontFileStream)
{
SkTScopedComPtr<SkDWriteFontFileStreamWrapper> stream;
- HR(SkDWriteFontFileStreamWrapper::Create(fStream, &stream));
+ HR(SkDWriteFontFileStreamWrapper::Create(fStream->duplicate(), &stream));
*fontFileStream = stream.release();
return S_OK;
}
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(
}
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[],
#include "SkImageDecoder.h"
#include "SkImageEncoder.h"
#include "SkMovie.h"
+#include "SkStream.h"
class SkColorTable;
-class SkStream;
-class SkStreamRewindable;
// Empty implementations for SkImageDecoder.
return false;
}
+bool SkImageDecoder::onBuildTileIndex(SkStreamRewindable* stream,
+ int* /*width*/, int* /*height*/) {
+ SkDELETE(stream);
+ return false;
+}
+
+
bool SkImageDecoder::decodeSubset(SkBitmap*, const SkIRect&, SkColorType) {
return false;
}
}
int ttcIndex;
- SkAutoTUnref<SkStream> stream(this->openStream(&ttcIndex));
+ SkAutoTDelete<SkStream> stream(this->openStream(&ttcIndex));
return stream.get() ? SkFontStream::GetTableTags(stream, ttcIndex, tags) : 0;
}
* UnicodeBMPUCS2, and English_UnitedStates settings.
*
* fontName and fontNameLen must be specified in terms of ASCII chars.
+ *
+ * Does not affect fontData's ownership.
*/
static SkData* RenameFont(SkStream* fontData, const char* fontName, int fontNameLen);
SkStreamRewindable* duplicate() const SK_OVERRIDE { return NULL; }
private:
- SkAutoTUnref<SkStream> fStream;
+ SkAutoTDelete<SkStream> fStream;
const bool fHasLength;
const size_t fLength;
// Current offset into the stream. Always >= 0.
}
FrontBufferedStream::FrontBufferedStream(SkStream* stream, size_t bufferSize)
- : fStream(SkRef(stream))
+ : fStream(stream)
, fHasLength(stream->hasPosition() && stream->hasLength())
, fLength(stream->getLength() - stream->getPosition())
, fOffset(0)
#ifdef SK_BUILD_POPPLER
bool SkPopplerRasterizePDF(SkStream* pdf, SkBitmap* output) {
+ SkAutoTDelete<SkStream> streamDeleter(pdf);
size_t size = pdf->getLength();
SkAutoFree buffer(sk_malloc_throw(size));
pdf->read(buffer.get(), size);
#ifdef SK_BUILD_NATIVE_PDF_RENDERER
bool SkNativeRasterizePDF(SkStream* pdf, SkBitmap* output) {
+ SkAutoTDelete<SkStream> streamDeleter(pdf);
return SkPDFNativeRenderToBitmap(pdf, output);
}
#endif // SK_BUILD_NATIVE_PDF_RENDERER
#include "SkStream.h"
#ifdef SK_BUILD_POPPLER
+// Deletes pdf when finished.
bool SkPopplerRasterizePDF(SkStream* pdf, SkBitmap* output);
#endif // SK_BUILD_POPPLER
#ifdef SK_BUILD_NATIVE_PDF_RENDERER
+// Deletes pdf when finished.
bool SkNativeRasterizePDF(SkStream* pdf, SkBitmap* output);
#endif // SK_BUILD_NATIVE_PDF_RENDERER
#include "SkCGUtils.h"
#include "SkStream.h"
-// This is used by CGDataProviderCreateWithData
+// These are used by CGDataProviderCreateWithData
static void unref_proc(void* info, const void* addr, size_t size) {
SkASSERT(info);
((SkRefCnt*)info)->unref();
}
+static void delete_stream_proc(void* info, const void* addr, size_t size) {
+ SkASSERT(info);
+ SkStream* stream = (SkStream*)info;
+ SkASSERT(stream->getMemoryBase() == addr);
+ SkASSERT(stream->getLength() == size);
+ SkDELETE(stream);
+}
+
// These are used by CGDataProviderSequentialCallbacks
static size_t get_bytes_proc(void* info, void* buffer, size_t bytes) {
((SkStream*)info)->rewind();
}
+// Used when info is an SkStream.
static void release_info_proc(void* info) {
SkASSERT(info);
- ((SkStream*)info)->unref();
+ SkDELETE((SkStream*)info);
}
CGDataProviderRef SkCreateDataProviderFromStream(SkStream* stream) {
- stream->ref(); // unref will be called when the provider is deleted
-
+ // 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(),
- unref_proc);
+ delete_stream_proc);
}
CGDataProviderSequentialCallbacks rec;
}
SkDWriteFontFileStreamWrapper::SkDWriteFontFileStreamWrapper(SkStream* stream)
- : fRefCount(1), fStream(SkRef(stream)) {
+ : fRefCount(1), fStream(stream) {
}
HRESULT STDMETHODCALLTYPE SkDWriteFontFileStreamWrapper::QueryInterface(REFIID iid, void** ppvObject) {
virtual ~SkDWriteFontFileStreamWrapper() { }
ULONG fRefCount;
- SkAutoTUnref<SkStream> fStream;
+ SkAutoTDelete<SkStream> fStream;
SkMutex fStreamMutex;
};
#endif
void init_skin_anim(const char path[], SkAnimator* anim) {
SkASSERT(path && anim);
- SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path));
+ SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
if (!stream.get()) {
SkDEBUGF(("init_skin_anim: loading skin failed <%s>\n", path));
sk_throw();
}
int ttcIndex;
- SkAutoTUnref<SkStream> fontData(origTypeface->openStream(&ttcIndex));
- SkTypeface* streamTypeface = SkTypeface::CreateFromStream(fontData);
+ SkAutoTDelete<SkStream> fontData(origTypeface->openStream(&ttcIndex));
+ SkTypeface* streamTypeface = SkTypeface::CreateFromStream(fontData.detach());
SkFontDescriptor desc;
bool isLocalStream = false;
// Tests reading the stream across boundaries of what has been buffered so far and what
// the total buffer size is.
static void test_incremental_buffering(skiatest::Reporter* reporter, size_t bufferSize) {
- SkMemoryStream memStream(gAbcs, strlen(gAbcs), false);
+ // NOTE: For this and other tests in this file, we cheat and continue to refer to the
+ // wrapped stream, but that's okay because we know the wrapping stream has not been
+ // deleted yet (and we only call const methods in it).
+ SkMemoryStream* memStream = SkNEW_ARGS(SkMemoryStream, (gAbcs, strlen(gAbcs), false));
- SkAutoTUnref<SkStream> bufferedStream(SkFrontBufferedStream::Create(&memStream, bufferSize));
- test_hasLength(reporter, *bufferedStream.get(), memStream);
+ SkAutoTDelete<SkStream> bufferedStream(SkFrontBufferedStream::Create(memStream, bufferSize));
+ test_hasLength(reporter, *bufferedStream.get(), *memStream);
// First, test reading less than the max buffer size.
test_read(reporter, bufferedStream, gAbcs, bufferSize / 2);
}
static void test_perfectly_sized_buffer(skiatest::Reporter* reporter, size_t bufferSize) {
- SkMemoryStream memStream(gAbcs, strlen(gAbcs), false);
- SkAutoTUnref<SkStream> bufferedStream(SkFrontBufferedStream::Create(&memStream, bufferSize));
- test_hasLength(reporter, *bufferedStream.get(), memStream);
+ SkMemoryStream* memStream = SkNEW_ARGS(SkMemoryStream, (gAbcs, strlen(gAbcs), false));
+ SkAutoTDelete<SkStream> bufferedStream(SkFrontBufferedStream::Create(memStream, bufferSize));
+ test_hasLength(reporter, *bufferedStream.get(), *memStream);
// Read exactly the amount that fits in the buffer.
test_read(reporter, bufferedStream, gAbcs, bufferSize);
}
static void test_skipping(skiatest::Reporter* reporter, size_t bufferSize) {
- SkMemoryStream memStream(gAbcs, strlen(gAbcs), false);
- SkAutoTUnref<SkStream> bufferedStream(SkFrontBufferedStream::Create(&memStream, bufferSize));
- test_hasLength(reporter, *bufferedStream.get(), memStream);
+ SkMemoryStream* memStream = SkNEW_ARGS(SkMemoryStream, (gAbcs, strlen(gAbcs), false));
+ SkAutoTDelete<SkStream> bufferedStream(SkFrontBufferedStream::Create(memStream, bufferSize));
+ test_hasLength(reporter, *bufferedStream.get(), *memStream);
// Skip half the buffer.
bufferedStream->skip(bufferSize / 2);
// does not invalidate the buffer.
static void test_read_beyond_buffer(skiatest::Reporter* reporter, size_t bufferSize) {
// Use a stream that behaves like Android's stream.
- AndroidLikeMemoryStream memStream((void*)gAbcs, bufferSize, false);
+ AndroidLikeMemoryStream* memStream = SkNEW_ARGS(AndroidLikeMemoryStream, ((void*)gAbcs, bufferSize, false));
// Create a buffer that matches the length of the stream.
- SkAutoTUnref<SkStream> bufferedStream(SkFrontBufferedStream::Create(&memStream, bufferSize));
- test_hasLength(reporter, *bufferedStream.get(), memStream);
+ SkAutoTDelete<SkStream> bufferedStream(SkFrontBufferedStream::Create(memStream, bufferSize));
+ test_hasLength(reporter, *bufferedStream.get(), *memStream);
// Attempt to read one more than the bufferSize
test_read(reporter, bufferedStream.get(), gAbcs, bufferSize + 1);
static void test_length_combos(skiatest::Reporter* reporter, size_t bufferSize) {
for (int hasLen = 0; hasLen <= 1; hasLen++) {
for (int hasPos = 0; hasPos <= 1; hasPos++) {
- LengthOptionalStream stream(SkToBool(hasLen), SkToBool(hasPos));
- SkAutoTUnref<SkStream> buffered(SkFrontBufferedStream::Create(&stream, bufferSize));
- test_hasLength(reporter, *buffered.get(), stream);
+ LengthOptionalStream* stream = SkNEW_ARGS(LengthOptionalStream, (SkToBool(hasLen), SkToBool(hasPos)));
+ SkAutoTDelete<SkStream> buffered(SkFrontBufferedStream::Create(stream, bufferSize));
+ test_hasLength(reporter, *buffered.get(), *stream);
}
}
}
// Test using a stream with an initial offset.
static void test_initial_offset(skiatest::Reporter* reporter, size_t bufferSize) {
- SkMemoryStream memStream(gAbcs, strlen(gAbcs), false);
+ SkMemoryStream* memStream = SkNEW_ARGS(SkMemoryStream, (gAbcs, strlen(gAbcs), false));
// Skip a few characters into the memStream, so that bufferedStream represents an offset into
// the stream it wraps.
const size_t arbitraryOffset = 17;
- memStream.skip(arbitraryOffset);
- SkAutoTUnref<SkStream> bufferedStream(SkFrontBufferedStream::Create(&memStream, bufferSize));
+ memStream->skip(arbitraryOffset);
+ SkAutoTDelete<SkStream> bufferedStream(SkFrontBufferedStream::Create(memStream, bufferSize));
// Since SkMemoryStream has a length and a position, bufferedStream must also.
REPORTER_ASSERT(reporter, bufferedStream->hasLength());
};
DEF_TEST(ShortFrontBufferedStream, reporter) {
- FailingStream failingStream;
- SkAutoTUnref<SkStreamRewindable> stream(SkFrontBufferedStream::Create(&failingStream, 64));
+ FailingStream* failingStream = SkNEW(FailingStream);
+ SkAutoTDelete<SkStreamRewindable> stream(SkFrontBufferedStream::Create(failingStream, 64));
SkBitmap bm;
// The return value of DecodeStream is not important. We are just using DecodeStream because
// it simulates a bug. DecodeStream will read the stream, then rewind, then attempt to read
// again. FrontBufferedStream::read should not continue to read its underlying stream beyond
// its end.
SkImageDecoder::DecodeStream(stream, &bm);
- REPORTER_ASSERT(reporter, !failingStream.readAfterEnd());
+ REPORTER_ASSERT(reporter, !failingStream->readAfterEnd());
}
#endif // SK_BUILD_FOR_UNIX/ANDROID skbug.com/2388
#ifdef SK_DEBUG
-// Create a stream containing a bitmap encoded to Type type.
-static SkMemoryStream* create_image_stream(SkImageEncoder::Type type) {
- SkBitmap bm;
- const int size = 50;
- bm.allocN32Pixels(size, size);
- SkCanvas canvas(bm);
- SkPoint points[2] = {
- { SkIntToScalar(0), SkIntToScalar(0) },
- { SkIntToScalar(size), SkIntToScalar(size) }
- };
- SkColor colors[2] = { SK_ColorWHITE, SK_ColorBLUE };
- SkShader* shader = SkGradientShader::CreateLinear(points, colors, NULL,
- SK_ARRAY_COUNT(colors),
- SkShader::kClamp_TileMode);
- SkPaint paint;
- paint.setShader(shader)->unref();
- canvas.drawPaint(paint);
- // Now encode it to a stream.
- SkAutoTUnref<SkData> data(SkImageEncoder::EncodeData(bm, type, 100));
- if (NULL == data.get()) {
- return NULL;
- }
- return SkNEW_ARGS(SkMemoryStream, (data.get()));
-}
-
-// For every format that supports tile based decoding, ensure that
-// calling decodeSubset will not fail if the caller has unreffed the
-// stream provided in buildTileIndex.
-// Only runs in debug mode since we are testing for a crash.
-static void test_stream_life() {
- const SkImageEncoder::Type gTypes[] = {
-#ifdef SK_BUILD_FOR_ANDROID
- SkImageEncoder::kJPEG_Type,
- SkImageEncoder::kPNG_Type,
-#endif
- SkImageEncoder::kWEBP_Type,
- };
- for (size_t i = 0; i < SK_ARRAY_COUNT(gTypes); ++i) {
- // SkDebugf("encoding to %i\n", i);
- SkAutoTUnref<SkMemoryStream> stream(create_image_stream(gTypes[i]));
- if (NULL == stream.get()) {
- SkDebugf("no stream\n");
- continue;
- }
- SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream));
- if (NULL == decoder.get()) {
- SkDebugf("no decoder\n");
- continue;
- }
- int width, height;
- if (!decoder->buildTileIndex(stream.get(), &width, &height)) {
- SkDebugf("could not build a tile index\n");
- continue;
- }
- // Now unref the stream to make sure it survives
- stream.reset(NULL);
- SkBitmap bm;
- decoder->decodeSubset(&bm, SkIRect::MakeWH(width, height), kN32_SkColorType);
- }
-}
-
// Test inside SkScaledBitmapSampler.cpp
extern void test_row_proc_choice();
-
#endif // SK_DEBUG
DEF_TEST(ImageDecoding, reporter) {
test_unpremul(reporter);
#ifdef SK_DEBUG
- test_stream_life();
test_row_proc_choice();
#endif
}
SkASSERT(bitmap != NULL);
SkASSERT(stream != NULL);
SkASSERT(stream->rewind());
- SkASSERT(stream->unique());
SkColorType colorType = bitmap->colorType();
SkDecodingImageGenerator::Options opts(sampleSize, ditherImage, colorType);
if (SkInstallDiscardablePixelRef(
*/
DEF_TEST(ImprovedBitmapFactory, reporter) {
SkString pngFilename = GetResourcePath("randPixels.png");
- SkAutoTUnref<SkStreamRewindable> stream(SkStream::NewFromFile(pngFilename.c_str()));
+ SkAutoTDelete<SkStreamRewindable> stream(SkStream::NewFromFile(pngFilename.c_str()));
if (sk_exists(pngFilename.c_str())) {
SkBitmap bm;
SkAssertResult(bm.setInfo(SkImageInfo::MakeN32Premul(1, 1)));
SkAutoDataUnref encodedData(SkData::NewFromFileName(path.c_str()));
REPORTER_ASSERT(reporter, encodedData.get() != NULL);
- SkAutoTUnref<SkStreamRewindable> encodedStream(
+ SkAutoTDelete<SkStreamRewindable> encodedStream(
SkStream::NewFromFile(path.c_str()));
REPORTER_ASSERT(reporter, encodedStream.get() != NULL);
DEF_TEST(ImageDecoding_JpegOverwrite, r) {
SkString resourceDir = GetResourcePath();
SkString path = SkOSPath::Join(resourceDir.c_str(), "randPixels.jpg");
- SkAutoTUnref<SkStreamAsset> stream(
+ SkAutoTDelete<SkStreamAsset> stream(
SkStream::NewFromFile(path.c_str()));
if (!stream.get()) {
SkDebugf("\nPath '%s' missing.\n", path.c_str());
SkAutoDataUnref encodedData(SkImageEncoder::EncodeData(bm8888, SkImageEncoder::kKTX_Type, 0));
REPORTER_ASSERT(reporter, encodedData);
- SkAutoTUnref<SkMemoryStream> stream(SkNEW_ARGS(SkMemoryStream, (encodedData)));
+ SkAutoTDelete<SkMemoryStream> stream(SkNEW_ARGS(SkMemoryStream, (encodedData)));
REPORTER_ASSERT(reporter, stream);
SkBitmap decodedBitmap;
0xFF, 0xFF, 0xFF, 0x80, // Pixel 3
0xFF, 0xFF, 0xFF, 0x80};// Pixel 4
- SkAutoTUnref<SkMemoryStream> stream(
+ SkAutoTDelete<SkMemoryStream> stream(
SkNEW_ARGS(SkMemoryStream, (kHalfWhiteKTX, sizeof(kHalfWhiteKTX))));
REPORTER_ASSERT(reporter, stream);
static void TestPDFStream(skiatest::Reporter* reporter) {
char streamBytes[] = "Test\nFoo\tBar";
- SkAutoTUnref<SkMemoryStream> streamData(new SkMemoryStream(
+ SkAutoTDelete<SkMemoryStream> streamData(new SkMemoryStream(
streamBytes, strlen(streamBytes), true));
SkAutoTUnref<SkPDFStream> stream(new SkPDFStream(streamData.get()));
SimpleCheckObjectOutput(
// Serlialize picture and create its clone from stream.
SkDynamicMemoryWStream stream;
picture->serialize(&stream);
- SkAutoTUnref<SkStream> inputStream(stream.detachAsStream());
+ SkAutoTDelete<SkStream> inputStream(stream.detachAsStream());
SkAutoTUnref<SkPicture> loadedPicture(SkPicture::CreateFromStream(inputStream.get()));
// Draw both original and clone picture and compare bitmaps -- they should be identical.
REPORTER_ASSERT(reporter, stream.isValid());
test_loop_stream(reporter, &stream, s, 26, 100);
- SkAutoTUnref<SkStreamAsset> stream2(stream.duplicate());
+ SkAutoTDelete<SkStreamAsset> stream2(stream.duplicate());
test_loop_stream(reporter, stream2.get(), s, 26, 100);
}
REPORTER_ASSERT(reporter, stream.isValid());
test_loop_stream(reporter, &stream, s, 26, 100);
- SkAutoTUnref<SkStreamAsset> stream2(stream.duplicate());
+ SkAutoTDelete<SkStreamAsset> stream2(stream.duplicate());
test_loop_stream(reporter, stream2.get(), s, 26, 100);
}
}
}
{
- SkAutoTUnref<SkStreamAsset> stream(ds.detachAsStream());
+ SkAutoTDelete<SkStreamAsset> stream(ds.detachAsStream());
REPORTER_ASSERT(reporter, 100 * 26 == stream->getLength());
REPORTER_ASSERT(reporter, ds.getOffset() == 0);
test_loop_stream(reporter, stream.get(), s, 26, 100);
- SkAutoTUnref<SkStreamAsset> stream2(stream->duplicate());
+ SkAutoTDelete<SkStreamAsset> stream2(stream->duplicate());
test_loop_stream(reporter, stream2.get(), s, 26, 100);
- SkAutoTUnref<SkStreamAsset> stream3(stream->fork());
+ SkAutoTDelete<SkStreamAsset> stream3(stream->fork());
REPORTER_ASSERT(reporter, stream3->isAtEnd());
char tmp;
size_t bytes = stream->read(&tmp, 1);
{
// Test that this works after a copyToData.
- SkAutoTUnref<SkStreamAsset> stream(ds.detachAsStream());
+ SkAutoTDelete<SkStreamAsset> stream(ds.detachAsStream());
REPORTER_ASSERT(reporter, ds.getOffset() == 0);
test_loop_stream(reporter, stream.get(), s, 26, 100);
- SkAutoTUnref<SkStreamAsset> stream2(stream->duplicate());
+ SkAutoTDelete<SkStreamAsset> stream2(stream->duplicate());
test_loop_stream(reporter, stream2.get(), s, 26, 100);
}
delete[] dst;
continue;
}
- SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(FLAGS_skps[i]));
+ SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(FLAGS_skps[i]));
if (!stream) {
SkDebugf("Could not read %s.\n", FLAGS_skps[i]);
exit(1);
DEFINE_bool2(quiet, q, false, "Silence all non-error related output");
static SkPicture* load_picture(const char path[]) {
- SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path));
+ SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
SkPicture* pic = NULL;
if (stream.get()) {
pic = SkPicture::CreateFromStream(stream.get(), &sk_tools::LazyDecodeBitmap);