diff --git a/include/core/SkRefCnt.h b/include/core/SkRefCnt.h index 17d77de288..21ed0efec3 100644 --- a/include/core/SkRefCnt.h +++ b/include/core/SkRefCnt.h @@ -229,5 +229,37 @@ private: SkRefCnt* fObj; }; +/** Wrapper class for SkRefCnt pointers. This manages ref/unref of a pointer to + a SkRefCnt (or subclass) object. + */ +template class SkRefPtr { +public: + SkRefPtr() : fObj(NULL) {} + SkRefPtr(T* obj) : fObj(obj) { SkSafeRef(fObj); } + SkRefPtr(const SkRefPtr& o) : fObj(o.fObj) { SkSafeRef(fObj); } + ~SkRefPtr() { SkSafeUnref(fObj); } + + SkRefPtr& operator=(const SkRefPtr& rp) { + SkRefCnt_SafeAssign(fObj, rp.fObj); + return *this; + } + SkRefPtr& operator=(T* obj) { + SkRefCnt_SafeAssign(fObj, obj); + return *this; + } + + T* get() const { return fObj; } + T& operator*() const { return *fObj; } + T* operator->() const { return fObj; } + + typedef T* SkRefPtr::*unspecified_bool_type; + operator unspecified_bool_type() const { + return fObj ? &SkRefPtr::fObj : NULL; + } + +private: + T* fObj; +}; + #endif diff --git a/src/pdf/SkPDFDevice.cpp b/src/pdf/SkPDFDevice.cpp index 7f189aad5f..0de011c63c 100644 --- a/src/pdf/SkPDFDevice.cpp +++ b/src/pdf/SkPDFDevice.cpp @@ -1006,7 +1006,8 @@ SkPDFDict* SkPDFDevice::getResourceDict() { fResourceDict = SkNEW(SkPDFDict); if (fGraphicStateResources.count()) { - SkAutoTUnref extGState(new SkPDFDict()); + SkRefPtr extGState = new SkPDFDict(); + extGState->unref(); // SkRefPtr and new both took a reference. for (int i = 0; i < fGraphicStateResources.count(); i++) { SkString nameString("G"); nameString.appendS32(i); @@ -1018,7 +1019,8 @@ SkPDFDict* SkPDFDevice::getResourceDict() { } if (fXObjectResources.count()) { - SkAutoTUnref xObjects(new SkPDFDict()); + SkRefPtr xObjects = new SkPDFDict(); + xObjects->unref(); // SkRefPtr and new both took a reference. for (int i = 0; i < fXObjectResources.count(); i++) { SkString nameString("X"); nameString.appendS32(i); @@ -1030,7 +1032,8 @@ SkPDFDict* SkPDFDevice::getResourceDict() { } if (fFontResources.count()) { - SkAutoTUnref fonts(new SkPDFDict()); + SkRefPtr fonts = new SkPDFDict(); + fonts->unref(); // SkRefPtr and new both took a reference. for (int i = 0; i < fFontResources.count(); i++) { SkString nameString("F"); nameString.appendS32(i); @@ -1041,7 +1044,8 @@ SkPDFDict* SkPDFDevice::getResourceDict() { } if (fShaderResources.count()) { - SkAutoTUnref patterns(new SkPDFDict()); + SkRefPtr patterns = new SkPDFDict(); + patterns->unref(); // SkRefPtr and new both took a reference. for (int i = 0; i < fShaderResources.count(); i++) { SkString nameString("P"); nameString.appendS32(i); @@ -1054,7 +1058,8 @@ SkPDFDict* SkPDFDevice::getResourceDict() { // For compatibility, add all proc sets (only used for output to PS // devices). const char procs[][7] = {"PDF", "Text", "ImageB", "ImageC", "ImageI"}; - SkAutoTUnref procSets(new SkPDFArray()); + SkRefPtr procSets = new SkPDFArray(); + procSets->unref(); // SkRefPtr and new both took a reference. procSets->reserve(SK_ARRAY_COUNT(procs)); for (size_t i = 0; i < SK_ARRAY_COUNT(procs); i++) procSets->appendName(procs[i]); @@ -1261,8 +1266,9 @@ void SkPDFDevice::drawFormXObjectWithClip(SkPDFFormXObject* xobject, SkPaint stockPaint; this->drawPaint(draw, stockPaint); SkAutoTUnref maskFormXObject(createFormXObjectFromDevice()); - SkAutoTUnref sMaskGS( - SkPDFGraphicState::GetSMaskGraphicState(maskFormXObject, invertClip)); + SkRefPtr sMaskGS = + SkPDFGraphicState::GetSMaskGraphicState(maskFormXObject, invertClip); + sMaskGS->unref(); // SkRefPtr and getSMaskGraphicState both took a ref. // Draw the xobject with the clip as a mask. ScopedContentEntry content(this, &fExistingClipStack, fExistingClipRegion, @@ -1277,7 +1283,8 @@ void SkPDFDevice::drawFormXObjectWithClip(SkPDFFormXObject* xobject, fXObjectResources.push(xobject); xobject->ref(); - sMaskGS.reset(SkPDFGraphicState::GetNoSMaskGraphicState()); + sMaskGS = SkPDFGraphicState::GetNoSMaskGraphicState(); + sMaskGS->unref(); // SkRefPtr and getSMaskGraphicState both took a ref. SkPDFUtils::ApplyGraphicState(addGraphicStateResource(sMaskGS.get()), &content.entry()->fContent); } @@ -1414,25 +1421,27 @@ void SkPDFDevice::finishContentEntry(const SkXfermode::Mode xfermode, return; } - SkAutoTUnref sMaskGS; + SkRefPtr sMaskGS; if (xfermode == SkXfermode::kSrcIn_Mode || xfermode == SkXfermode::kSrcOut_Mode) { - sMaskGS.reset(SkPDFGraphicState::GetSMaskGraphicState( - dst, xfermode == SkXfermode::kSrcOut_Mode)); + sMaskGS = SkPDFGraphicState::GetSMaskGraphicState( + dst, xfermode == SkXfermode::kSrcOut_Mode); fXObjectResources.push(srcFormXObject.get()); srcFormXObject.get()->ref(); } else { - sMaskGS.reset(SkPDFGraphicState::GetSMaskGraphicState( - srcFormXObject.get(), xfermode == SkXfermode::kDstOut_Mode)); + sMaskGS = SkPDFGraphicState::GetSMaskGraphicState( + srcFormXObject.get(), xfermode == SkXfermode::kDstOut_Mode); // dst already added to fXObjectResources in drawFormXObjectWithClip. } + sMaskGS->unref(); // SkRefPtr and getSMaskGraphicState both took a ref. SkPDFUtils::ApplyGraphicState(addGraphicStateResource(sMaskGS.get()), &inClipContentEntry.entry()->fContent); SkPDFUtils::DrawFormXObject(fXObjectResources.count() - 1, &inClipContentEntry.entry()->fContent); - sMaskGS.reset(SkPDFGraphicState::GetNoSMaskGraphicState()); + sMaskGS = SkPDFGraphicState::GetNoSMaskGraphicState(); + sMaskGS->unref(); // SkRefPtr and getSMaskGraphicState both took a ref. SkPDFUtils::ApplyGraphicState(addGraphicStateResource(sMaskGS.get()), &inClipContentEntry.entry()->fContent); } @@ -1465,7 +1474,7 @@ void SkPDFDevice::populateGraphicStateEntryFromPaint( entry->fShaderIndex = -1; // PDF treats a shader as a color, so we only set one or the other. - SkAutoTUnref pdfShader; + SkRefPtr pdfShader; const SkShader* shader = paint.getShader(); SkColor color = paint.getColor(); if (shader) { @@ -1485,7 +1494,8 @@ void SkPDFDevice::populateGraphicStateEntryFromPaint( fInitialTransform.mapRect(&boundsTemp); boundsTemp.roundOut(&bounds); - pdfShader.reset(SkPDFShader::GetPDFShader(*shader, transform, bounds)); + pdfShader = SkPDFShader::GetPDFShader(*shader, transform, bounds); + SkSafeUnref(pdfShader.get()); // getShader and SkRefPtr both took a ref if (pdfShader.get()) { // pdfShader has been canonicalized so we can directly compare @@ -1494,7 +1504,7 @@ void SkPDFDevice::populateGraphicStateEntryFromPaint( if (resourceIndex < 0) { resourceIndex = fShaderResources.count(); fShaderResources.push(pdfShader.get()); - pdfShader.get()->ref(); + pdfShader->ref(); } entry->fShaderIndex = resourceIndex; } else { @@ -1513,16 +1523,15 @@ void SkPDFDevice::populateGraphicStateEntryFromPaint( } } - SkAutoTUnref newGraphicState; + SkRefPtr newGraphicState; if (color == paint.getColor()) { - newGraphicState.reset( - SkPDFGraphicState::GetGraphicStateForPaint(paint)); + newGraphicState = SkPDFGraphicState::GetGraphicStateForPaint(paint); } else { SkPaint newPaint = paint; newPaint.setColor(color); - newGraphicState.reset( - SkPDFGraphicState::GetGraphicStateForPaint(newPaint)); + newGraphicState = SkPDFGraphicState::GetGraphicStateForPaint(newPaint); } + newGraphicState->unref(); // getGraphicState and SkRefPtr both took a ref. int resourceIndex = addGraphicStateResource(newGraphicState.get()); entry->fGraphicStateIndex = resourceIndex; @@ -1563,12 +1572,13 @@ void SkPDFDevice::updateFont(const SkPaint& paint, uint16_t glyphID, } int SkPDFDevice::getFontResourceIndex(SkTypeface* typeface, uint16_t glyphID) { - SkAutoTUnref newFont(SkPDFFont::GetFontResource(typeface, glyphID)); + SkRefPtr newFont = SkPDFFont::GetFontResource(typeface, glyphID); + newFont->unref(); // getFontResource and SkRefPtr both took a ref. int resourceIndex = fFontResources.find(newFont.get()); if (resourceIndex < 0) { resourceIndex = fFontResources.count(); fFontResources.push(newFont.get()); - newFont.get()->ref(); + newFont->ref(); } return resourceIndex; } diff --git a/src/pdf/SkPDFDocument.cpp b/src/pdf/SkPDFDocument.cpp index c7266d8454..7e064ee19d 100644 --- a/src/pdf/SkPDFDocument.cpp +++ b/src/pdf/SkPDFDocument.cpp @@ -98,11 +98,13 @@ bool SkPDFDocument::emitPDF(SkWStream* stream) { fDocCatalog->insert("Pages", new SkPDFObjRef(pageTreeRoot))->unref(); /* TODO(vandebo): output intent - SkAutoTUnref outputIntent = new SkPDFDict("OutputIntent"); + SkRefPtr outputIntent = new SkPDFDict("OutputIntent"); + outputIntent->unref(); // SkRefPtr and new both took a reference. outputIntent->insert("S", new SkPDFName("GTS_PDFA1"))->unref(); outputIntent->insert("OutputConditionIdentifier", new SkPDFString("sRGB"))->unref(); - SkAutoTUnref intentArray = new SkPDFArray; + SkRefPtr intentArray = new SkPDFArray; + intentArray->unref(); // SkRefPtr and new both took a reference. intentArray->append(outputIntent.get()); fDocCatalog->insert("OutputIntent", intentArray.get()); */ diff --git a/src/pdf/SkPDFFont.cpp b/src/pdf/SkPDFFont.cpp index f2b3f53162..9a8f13600b 100644 --- a/src/pdf/SkPDFFont.cpp +++ b/src/pdf/SkPDFFont.cpp @@ -155,12 +155,13 @@ SkStream* handleType1Stream(SkStream* srcStream, size_t* headerLen, // 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 staticStream; + SkRefPtr staticStream; SkData* data = NULL; const uint8_t* src; size_t srcLen; if ((srcLen = srcStream->getLength()) > 0) { - staticStream.reset(new SkMemoryStream(srcLen + 1)); + staticStream = new SkMemoryStream(srcLen + 1); + staticStream->unref(); // new and SkRefPtr both took a ref. src = (const uint8_t*)staticStream->getMemoryBase(); if (srcStream->getMemoryBase() != NULL) { memcpy((void *)src, srcStream->getMemoryBase(), srcLen); @@ -317,7 +318,8 @@ SkPDFArray* composeAdvanceData( break; } case SkAdvancedTypefaceMetrics::WidthRange::kRange: { - SkAutoTUnref advanceArray(new SkPDFArray()); + SkRefPtr advanceArray = new SkPDFArray(); + advanceArray->unref(); // SkRefPtr and new both took a ref. for (int j = 0; j < advanceInfo->fAdvance.count(); j++) appendAdvance(advanceInfo->fAdvance[j], emSize, advanceArray.get()); @@ -528,7 +530,8 @@ static SkPDFStream* generate_tounicode_cmap( append_tounicode_header(&cmap); append_cmap_sections(glyphToUnicode, subset, &cmap); append_cmap_footer(&cmap); - SkAutoTUnref cmapStream(new SkMemoryStream()); + SkRefPtr cmapStream = new SkMemoryStream(); + cmapStream->unref(); // SkRefPtr and new took a reference. cmapStream->setData(cmap.copyToData())->unref(); return new SkPDFStream(cmapStream.get()); } @@ -544,8 +547,9 @@ static int get_subset_font_stream(const char* fontName, const SkTypeface* typeface, const SkTDArray& subset, SkPDFStream** fontStream) { - SkAutoTUnref fontData( - SkFontHost::OpenStream(SkTypeface::UniqueID(typeface))); + SkRefPtr fontData = + SkFontHost::OpenStream(SkTypeface::UniqueID(typeface)); + fontData->unref(); // SkRefPtr and OpenStream both took a ref. int fontSize = fontData->getLength(); @@ -762,12 +766,11 @@ SkPDFFont* SkPDFFont::GetFontResource(SkTypeface* typeface, uint16_t glyphID) { return CanonicalFonts()[relatedFontIndex].fFont; } - SkAutoTUnref fontMetrics; + SkRefPtr fontMetrics; SkPDFDict* relatedFontDescriptor = NULL; if (relatedFontIndex >= 0) { SkPDFFont* relatedFont = CanonicalFonts()[relatedFontIndex].fFont; - fontMetrics.reset(relatedFont->fontInfo()); - SkSafeRef(fontMetrics.get()); + fontMetrics = relatedFont->fontInfo(); relatedFontDescriptor = relatedFont->getFontDescriptor(); // This only is to catch callers who pass invalid glyph ids. @@ -791,16 +794,18 @@ SkPDFFont* SkPDFFont::GetFontResource(SkTypeface* typeface, uint16_t glyphID) { info = SkTBitOr( info, SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo); #endif - fontMetrics.reset( - SkFontHost::GetAdvancedTypefaceMetrics(fontID, info, NULL, 0)); + fontMetrics = + SkFontHost::GetAdvancedTypefaceMetrics(fontID, info, NULL, 0); + SkSafeUnref(fontMetrics.get()); // SkRefPtr and Get both took a ref. #if defined (SK_SFNTLY_SUBSETTER) - if (fontMetrics.get() && + if (fontMetrics && fontMetrics->fType != SkAdvancedTypefaceMetrics::kTrueType_Font) { // Font does not support subsetting, get new info with advance. info = SkTBitOr( info, SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo); - fontMetrics.reset( - SkFontHost::GetAdvancedTypefaceMetrics(fontID, info, NULL, 0)); + fontMetrics = + SkFontHost::GetAdvancedTypefaceMetrics(fontID, info, NULL, 0); + SkSafeUnref(fontMetrics.get()); // SkRefPtr and Get both took a ref } #endif } @@ -851,8 +856,6 @@ SkPDFFont::SkPDFFont(SkAdvancedTypefaceMetrics* info, SkTypeface* typeface, fFirstGlyphID(1), fLastGlyphID(info ? info->fLastGlyphID : 0), fFontInfo(info) { - SkSafeRef(typeface); - SkSafeRef(info); if (info == NULL) { fFontType = SkAdvancedTypefaceMetrics::kNotEmbeddable_Font; } else if (info->fMultiMaster) { @@ -903,8 +906,7 @@ void SkPDFFont::setFontInfo(SkAdvancedTypefaceMetrics* info) { if (info == NULL || info == fFontInfo.get()) { return; } - fFontInfo.reset(info); - SkSafeRef(info); + fFontInfo = info; } uint16_t SkPDFFont::firstGlyphID() const { @@ -922,7 +924,6 @@ void SkPDFFont::setLastGlyphID(uint16_t glyphID) { void SkPDFFont::addResource(SkPDFObject* object) { SkASSERT(object != NULL); fResources.push(object); - object->ref(); } SkPDFDict* SkPDFFont::getFontDescriptor() { @@ -930,8 +931,7 @@ SkPDFDict* SkPDFFont::getFontDescriptor() { } void SkPDFFont::setFontDescriptor(SkPDFDict* descriptor) { - fDescriptor.reset(descriptor); - SkSafeRef(descriptor); + fDescriptor = descriptor; } bool SkPDFFont::addCommonFontDescriptorEntries(int16_t defaultWidth) { @@ -1002,9 +1002,9 @@ void SkPDFFont::populateToUnicodeTable(const SkPDFGlyphSet* subset) { if (fFontInfo == NULL || fFontInfo->fGlyphToUnicode.begin() == NULL) { return; } - SkAutoTUnref pdfCmap( - generate_tounicode_cmap(fFontInfo->fGlyphToUnicode, subset)); - addResource(pdfCmap.get()); + SkRefPtr pdfCmap = + generate_tounicode_cmap(fFontInfo->fGlyphToUnicode, subset); + addResource(pdfCmap.get()); // Pass reference from new. insert("ToUnicode", new SkPDFObjRef(pdfCmap.get()))->unref(); } @@ -1039,11 +1039,14 @@ bool SkPDFType0Font::populate(const SkPDFGlyphSet* subset) { insertName("BaseFont", fontInfo()->fFontName); insertName("Encoding", "Identity-H"); - SkAutoTUnref newCIDFont( - new SkPDFCIDFont(fontInfo(), typeface(), subset)); - addResource(newCIDFont.get()); - SkAutoTUnref descendantFonts(new SkPDFArray()); - descendantFonts->append(new SkPDFObjRef(newCIDFont.get()))->unref(); + SkPDFCIDFont* newCIDFont; + newCIDFont = new SkPDFCIDFont(fontInfo(), typeface(), subset); + + // Pass ref new created to fResources. + addResource(newCIDFont); + SkRefPtr descendantFonts = new SkPDFArray(); + descendantFonts->unref(); // SkRefPtr and new took a reference. + descendantFonts->append(new SkPDFObjRef(newCIDFont))->unref(); insert("DescendantFonts", descendantFonts.get()); populateToUnicodeTable(subset); @@ -1066,9 +1069,9 @@ SkPDFCIDFont::~SkPDFCIDFont() {} bool SkPDFCIDFont::addFontDescriptor(int16_t defaultWidth, const SkTDArray* subset) { - SkAutoTUnref descriptor(new SkPDFDict("FontDescriptor")); + SkRefPtr descriptor = new SkPDFDict("FontDescriptor"); + descriptor->unref(); // SkRefPtr and new both took a ref. setFontDescriptor(descriptor.get()); - addResource(descriptor.get()); switch (getType()) { case SkAdvancedTypefaceMetrics::kTrueType_Font: { @@ -1081,20 +1084,22 @@ bool SkPDFCIDFont::addFontDescriptor(int16_t defaultWidth, &rawStream); SkASSERT(fontSize); SkASSERT(rawStream); - SkAutoTUnref fontStream(rawStream); + SkRefPtr fontStream = rawStream; + // SkRefPtr and new both ref()'d fontStream, pass one. addResource(fontStream.get()); fontStream->insertInt("Length1", fontSize); descriptor->insert("FontFile2", - new SkPDFObjRef(fontStream.get()))->unref(); + new SkPDFObjRef(fontStream.get()))->unref(); break; } case SkAdvancedTypefaceMetrics::kCFF_Font: case SkAdvancedTypefaceMetrics::kType1CID_Font: { - SkAutoTUnref fontData( - SkFontHost::OpenStream(SkTypeface::UniqueID(typeface()))); - SkAutoTUnref fontStream( - new SkPDFStream(fontData.get())); + SkRefPtr fontData = + SkFontHost::OpenStream(SkTypeface::UniqueID(typeface())); + fontData->unref(); // SkRefPtr and OpenStream both took a ref. + SkRefPtr fontStream = new SkPDFStream(fontData.get()); + // SkRefPtr and new both ref()'d fontStream, pass one. addResource(fontStream.get()); if (getType() == SkAdvancedTypefaceMetrics::kCFF_Font) { @@ -1110,6 +1115,9 @@ bool SkPDFCIDFont::addFontDescriptor(int16_t defaultWidth, SkASSERT(false); } + addResource(descriptor.get()); + descriptor->ref(); + insert("FontDescriptor", new SkPDFObjRef(descriptor.get()))->unref(); return addCommonFontDescriptorEntries(defaultWidth); } @@ -1124,18 +1132,20 @@ bool SkPDFCIDFont::populate(const SkPDFGlyphSet* subset) { subset->exportTo(&glyphIDs); } + SkRefPtr fontMetrics; SkAdvancedTypefaceMetrics::PerGlyphInfo info; info = SkAdvancedTypefaceMetrics::kGlyphNames_PerGlyphInfo; info = SkTBitOr( info, SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo); uint32_t* glyphs = (glyphIDs.count() == 1) ? NULL : glyphIDs.begin(); uint32_t glyphsCount = glyphs ? glyphIDs.count() : 0; - SkAutoTUnref fontMetrics( + fontMetrics = SkFontHost::GetAdvancedTypefaceMetrics( SkTypeface::UniqueID(typeface()), info, glyphs, - glyphsCount)); + glyphsCount); + SkSafeUnref(fontMetrics.get()); // SkRefPtr and Get both took a ref setFontInfo(fontMetrics.get()); addFontDescriptor(0, &glyphIDs); } else { @@ -1154,7 +1164,8 @@ bool SkPDFCIDFont::populate(const SkPDFGlyphSet* subset) { SkASSERT(false); } - SkAutoTUnref sysInfo(new SkPDFDict); + SkRefPtr sysInfo = new SkPDFDict; + sysInfo->unref(); // SkRefPtr and new both took a reference. sysInfo->insert("Registry", new SkPDFString("Adobe"))->unref(); sysInfo->insert("Ordering", new SkPDFString("Identity"))->unref(); sysInfo->insertInt("Supplement", 0); @@ -1162,10 +1173,11 @@ bool SkPDFCIDFont::populate(const SkPDFGlyphSet* subset) { if (fontInfo()->fGlyphWidths.get()) { int16_t defaultWidth = 0; - SkAutoTUnref widths( + SkRefPtr widths = composeAdvanceData(fontInfo()->fGlyphWidths.get(), fontInfo()->fEmSize, &appendWidth, - &defaultWidth)); + &defaultWidth); + widths->unref(); // SkRefPtr and compose both took a reference. if (widths->size()) insert("W", widths.get()); if (defaultWidth != 0) { @@ -1178,10 +1190,11 @@ bool SkPDFCIDFont::populate(const SkPDFGlyphSet* subset) { defaultAdvance.fVerticalAdvance = 0; defaultAdvance.fOriginXDisp = 0; defaultAdvance.fOriginYDisp = 0; - SkAutoTUnref advances( + SkRefPtr advances = composeAdvanceData(fontInfo()->fVerticalMetrics.get(), fontInfo()->fEmSize, &appendVerticalAdvance, - &defaultAdvance)); + &defaultAdvance); + advances->unref(); // SkRefPtr and compose both took a ref. if (advances->size()) insert("W2", advances.get()); if (defaultAdvance.fVerticalAdvance || @@ -1211,27 +1224,31 @@ SkPDFType1Font::SkPDFType1Font(SkAdvancedTypefaceMetrics* info, SkPDFType1Font::~SkPDFType1Font() {} bool SkPDFType1Font::addFontDescriptor(int16_t defaultWidth) { - if (getFontDescriptor() != NULL) { - SkPDFDict* descriptor = getFontDescriptor(); - addResource(descriptor); - insert("FontDescriptor", new SkPDFObjRef(descriptor))->unref(); + SkRefPtr descriptor = getFontDescriptor(); + if (descriptor.get() != NULL) { + addResource(descriptor.get()); + descriptor->ref(); + insert("FontDescriptor", new SkPDFObjRef(descriptor.get()))->unref(); return true; } - SkAutoTUnref descriptor(new SkPDFDict("FontDescriptor")); + descriptor = new SkPDFDict("FontDescriptor"); + descriptor->unref(); // SkRefPtr and new both took a ref. setFontDescriptor(descriptor.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 rawFontData( - SkFontHost::OpenStream(SkTypeface::UniqueID(typeface()))); + SkRefPtr rawFontData = + SkFontHost::OpenStream(SkTypeface::UniqueID(typeface())); + rawFontData->unref(); // SkRefPtr and OpenStream both took a ref. SkStream* fontData = handleType1Stream(rawFontData.get(), &header, &data, &trailer); if (fontData == NULL) { return false; } - SkAutoTUnref fontStream(new SkPDFStream(fontData)); + SkRefPtr fontStream = new SkPDFStream(fontData); + // SkRefPtr and new both ref()'d fontStream, pass one. addResource(fontStream.get()); fontStream->insertInt("Length1", header); fontStream->insertInt("Length2", data); @@ -1239,6 +1256,7 @@ bool SkPDFType1Font::addFontDescriptor(int16_t defaultWidth) { descriptor->insert("FontFile", new SkPDFObjRef(fontStream.get()))->unref(); addResource(descriptor.get()); + descriptor->ref(); insert("FontDescriptor", new SkPDFObjRef(descriptor.get()))->unref(); return addCommonFontDescriptorEntries(defaultWidth); @@ -1279,10 +1297,12 @@ bool SkPDFType1Font::populate(int16_t glyphID) { addWidthInfoFromRange(defaultWidth, widthRangeEntry); - SkAutoTUnref encoding(new SkPDFDict("Encoding")); + SkRefPtr encoding = new SkPDFDict("Encoding"); + encoding->unref(); // SkRefPtr and new both took a reference. insert("Encoding", encoding.get()); - SkAutoTUnref encDiffs(new SkPDFArray); + SkRefPtr encDiffs = new SkPDFArray; + encDiffs->unref(); // SkRefPtr and new both took a reference. encoding->insert("Differences", encDiffs.get()); encDiffs->reserve(lastGlyphID() - firstGlyphID() + 2); @@ -1297,7 +1317,8 @@ bool SkPDFType1Font::populate(int16_t glyphID) { void SkPDFType1Font::addWidthInfoFromRange( int16_t defaultWidth, const SkAdvancedTypefaceMetrics::WidthRange* widthRangeEntry) { - SkAutoTUnref widthArray(new SkPDFArray()); + SkRefPtr widthArray = new SkPDFArray(); + widthArray->unref(); // SkRefPtr and new both took a ref. int firstChar = 0; if (widthRangeEntry) { const uint16_t emSize = fontInfo()->fEmSize; @@ -1356,18 +1377,22 @@ bool SkPDFType3Font::populate(int16_t glyphID) { fontMatrix.setScale(SkScalarInvert(1000), -SkScalarInvert(1000)); insert("FontMatrix", SkPDFUtils::MatrixToArray(fontMatrix))->unref(); - SkAutoTUnref charProcs(new SkPDFDict); + SkRefPtr charProcs = new SkPDFDict; + charProcs->unref(); // SkRefPtr and new both took a reference. insert("CharProcs", charProcs.get()); - SkAutoTUnref encoding(new SkPDFDict("Encoding")); + SkRefPtr encoding = new SkPDFDict("Encoding"); + encoding->unref(); // SkRefPtr and new both took a reference. insert("Encoding", encoding.get()); - SkAutoTUnref encDiffs(new SkPDFArray); + SkRefPtr encDiffs = new SkPDFArray; + encDiffs->unref(); // SkRefPtr and new both took a reference. encoding->insert("Differences", encDiffs.get()); encDiffs->reserve(lastGlyphID() - firstGlyphID() + 2); encDiffs->appendInt(1); - SkAutoTUnref widthArray(new SkPDFArray()); + SkRefPtr widthArray = new SkPDFArray(); + widthArray->unref(); // SkRefPtr and new both took a ref. SkIRect bbox = SkIRect::MakeEmpty(); for (int gID = firstGlyphID(); gID <= lastGlyphID(); gID++) { @@ -1390,11 +1415,13 @@ bool SkPDFType3Font::populate(int16_t glyphID) { SkPDFUtils::PaintPath(paint.getStyle(), path->getFillType(), &content); } - SkAutoTUnref glyphStream(new SkMemoryStream()); + SkRefPtr glyphStream = new SkMemoryStream(); + glyphStream->unref(); // SkRefPtr and new both took a ref. glyphStream->setData(content.copyToData())->unref(); - SkAutoTUnref glyphDescription( - new SkPDFStream(glyphStream.get())); + SkRefPtr glyphDescription = + new SkPDFStream(glyphStream.get()); + // SkRefPtr and new both ref()'d charProcs, pass one. addResource(glyphDescription.get()); charProcs->insert(characterName.c_str(), new SkPDFObjRef(glyphDescription.get()))->unref(); diff --git a/src/pdf/SkPDFFont.h b/src/pdf/SkPDFFont.h index 693b911061..f463ed740a 100644 --- a/src/pdf/SkPDFFont.h +++ b/src/pdf/SkPDFFont.h @@ -180,7 +180,7 @@ private: FontRec(SkPDFFont* font, uint32_t fontID, uint16_t fGlyphID); }; - SkAutoTUnref fTypeface; + SkRefPtr fTypeface; // The glyph IDs accessible with this font. For Type1 (non CID) fonts, // this will be a subset if the font has more than 255 glyphs. @@ -188,9 +188,9 @@ private: uint16_t fLastGlyphID; // The font info is only kept around after construction for large // Type1 (non CID) fonts that need multiple "fonts" to access all glyphs. - SkAutoTUnref fFontInfo; + SkRefPtr fFontInfo; SkTDArray fResources; - SkAutoTUnref fDescriptor; + SkRefPtr fDescriptor; SkAdvancedTypefaceMetrics::FontType fFontType; diff --git a/src/pdf/SkPDFFormXObject.cpp b/src/pdf/SkPDFFormXObject.cpp index 5e33995b74..e148056afe 100644 --- a/src/pdf/SkPDFFormXObject.cpp +++ b/src/pdf/SkPDFFormXObject.cpp @@ -31,7 +31,8 @@ SkPDFFormXObject::SkPDFFormXObject(SkPDFDevice* device) { getResources(&dummy_resourceList); #endif - SkAutoTUnref content(device->content()); + SkRefPtr content = device->content(); + content->unref(); // SkRefPtr and content() both took a reference. setData(content.get()); insertName("Type", "XObject"); @@ -54,7 +55,8 @@ SkPDFFormXObject::SkPDFFormXObject(SkPDFDevice* device) { // Right now SkPDFFormXObject is only used for saveLayer, which implies // isolated blending. Do this conditionally if that changes. - SkAutoTUnref group(new SkPDFDict("Group")); + SkRefPtr group = new SkPDFDict("Group"); + group->unref(); // SkRefPtr and new both took a reference. group->insertName("S", "Transparency"); group->insert("I", new SkPDFBool(true))->unref(); // Isolated. insert("Group", group.get()); diff --git a/src/pdf/SkPDFGraphicState.cpp b/src/pdf/SkPDFGraphicState.cpp index c326b3b533..ec9b0e70b2 100644 --- a/src/pdf/SkPDFGraphicState.cpp +++ b/src/pdf/SkPDFGraphicState.cpp @@ -113,14 +113,16 @@ SkPDFObject* SkPDFGraphicState::GetInvertFunction() { if (!invertFunction) { // Acrobat crashes if we use a type 0 function, kpdf crashes if we use // a type 2 function, so we use a type 4 function. - SkAutoTUnref domainAndRange(new SkPDFArray); + SkRefPtr domainAndRange = new SkPDFArray; + domainAndRange->unref(); // SkRefPtr and new both took a reference. domainAndRange->reserve(2); domainAndRange->appendInt(0); domainAndRange->appendInt(1); static const char psInvert[] = "{1 exch sub}"; - SkAutoTUnref psInvertStream( - new SkMemoryStream(&psInvert, strlen(psInvert), true)); + SkRefPtr psInvertStream = + new SkMemoryStream(&psInvert, strlen(psInvert), true); + psInvertStream->unref(); // SkRefPtr and new both took a reference. invertFunction = new SkPDFStream(psInvertStream.get()); invertFunction->insertInt("FunctionType", 4); @@ -137,7 +139,8 @@ SkPDFGraphicState* SkPDFGraphicState::GetSMaskGraphicState( // enough that it's not worth canonicalizing. SkAutoMutexAcquire lock(CanonicalPaintsMutex()); - SkAutoTUnref sMaskDict(new SkPDFDict("Mask")); + SkRefPtr sMaskDict = new SkPDFDict("Mask"); + sMaskDict->unref(); // SkRefPtr and new both took a reference. sMaskDict->insertName("S", "Alpha"); sMaskDict->insert("G", new SkPDFObjRef(sMask))->unref(); @@ -197,8 +200,9 @@ void SkPDFGraphicState::populateDict() { fPopulated = true; insertName("Type", "ExtGState"); - SkAutoTUnref alpha( - new SkPDFScalar(SkScalarDiv(fPaint.getAlpha(), 0xFF))); + SkRefPtr alpha = + new SkPDFScalar(SkScalarDiv(fPaint.getAlpha(), 0xFF)); + alpha->unref(); // SkRefPtr and new both took a reference. insert("CA", alpha.get()); insert("ca", alpha.get()); diff --git a/src/pdf/SkPDFImage.cpp b/src/pdf/SkPDFImage.cpp index 1b93f6e045..429667a5e2 100644 --- a/src/pdf/SkPDFImage.cpp +++ b/src/pdf/SkPDFImage.cpp @@ -305,7 +305,8 @@ SkPDFImage::SkPDFImage(SkStream* imageData, const SkBitmap& bitmap, if (!doingAlpha && alphaOnly) { // For alpha only images, we stretch a single pixel of black for // the color/shape part. - SkAutoTUnref one(new SkPDFInt(1)); + SkRefPtr one = new SkPDFInt(1); + one->unref(); // SkRefPtr and new both took a reference. insert("Width", one.get()); insert("Height", one.get()); } else { @@ -334,12 +335,16 @@ SkPDFImage::SkPDFImage(SkStream* imageData, const SkBitmap& bitmap, insertInt("BitsPerComponent", bitsPerComp); if (config == SkBitmap::kRGB_565_Config) { - SkAutoTUnref zeroVal(new SkPDFInt(0)); - SkAutoTUnref scale5Val( - new SkPDFScalar(SkFloatToScalar(8.2258f))); // 255/2^5-1 - SkAutoTUnref scale6Val( - new SkPDFScalar(SkFloatToScalar(4.0476f))); // 255/2^6-1 - SkAutoTUnref decodeValue(new SkPDFArray()); + SkRefPtr zeroVal = new SkPDFInt(0); + zeroVal->unref(); // SkRefPtr and new both took a reference. + SkRefPtr scale5Val = + new SkPDFScalar(SkFloatToScalar(8.2258f)); // 255/2^5-1 + scale5Val->unref(); // SkRefPtr and new both took a reference. + SkRefPtr scale6Val = + new SkPDFScalar(SkFloatToScalar(4.0476f)); // 255/2^6-1 + scale6Val->unref(); // SkRefPtr and new both took a reference. + SkRefPtr decodeValue = new SkPDFArray(); + decodeValue->unref(); // SkRefPtr and new both took a reference. decodeValue->reserve(6); decodeValue->append(zeroVal.get()); decodeValue->append(scale5Val.get()); diff --git a/src/pdf/SkPDFPage.cpp b/src/pdf/SkPDFPage.cpp index a18beb156f..717f435356 100644 --- a/src/pdf/SkPDFPage.cpp +++ b/src/pdf/SkPDFPage.cpp @@ -15,7 +15,6 @@ SkPDFPage::SkPDFPage(SkPDFDevice* content) : SkPDFDict("Page"), fDevice(content) { - SkSafeRef(content); } SkPDFPage::~SkPDFPage() {} @@ -33,8 +32,10 @@ void SkPDFPage::finalizePage(SkPDFCatalog* catalog, bool firstPage, } } - SkAutoTUnref content(fDevice->content()); - fContentStream.reset(new SkPDFStream(content.get())); + SkRefPtr content = fDevice->content(); + content->unref(); // SkRefPtr and content() both took a reference. + fContentStream = new SkPDFStream(content.get()); + fContentStream->unref(); // SkRefPtr and new both took a reference. insert("Contents", new SkPDFObjRef(fContentStream.get()))->unref(); } catalog->addObject(fContentStream.get(), firstPage); @@ -66,9 +67,12 @@ void SkPDFPage::GeneratePageTree(const SkTDArray& pages, // one child. static const int kNodeSize = 8; - SkAutoTUnref kidsName(new SkPDFName("Kids")); - SkAutoTUnref countName(new SkPDFName("Count")); - SkAutoTUnref parentName(new SkPDFName("Parent")); + SkRefPtr kidsName = new SkPDFName("Kids"); + kidsName->unref(); // SkRefPtr and new both took a reference. + SkRefPtr countName = new SkPDFName("Count"); + countName->unref(); // SkRefPtr and new both took a reference. + SkRefPtr parentName = new SkPDFName("Parent"); + parentName->unref(); // SkRefPtr and new both took a reference. // curNodes takes a reference to its items, which it passes to pageTree. SkTDArray curNodes; @@ -91,9 +95,11 @@ void SkPDFPage::GeneratePageTree(const SkTDArray& pages, } SkPDFDict* newNode = new SkPDFDict("Pages"); - SkAutoTUnref newNodeRef(new SkPDFObjRef(newNode)); + SkRefPtr newNodeRef = new SkPDFObjRef(newNode); + newNodeRef->unref(); // SkRefPtr and new both took a reference. - SkAutoTUnref kids(new SkPDFArray); + SkRefPtr kids = new SkPDFArray; + kids->unref(); // SkRefPtr and new both took a reference. kids->reserve(kNodeSize); int count = 0; diff --git a/src/pdf/SkPDFPage.h b/src/pdf/SkPDFPage.h index 72ba335e66..8ef909e573 100644 --- a/src/pdf/SkPDFPage.h +++ b/src/pdf/SkPDFPage.h @@ -91,10 +91,10 @@ public: private: // Multiple pages may reference the content. - SkAutoTUnref fDevice; + SkRefPtr fDevice; // Once the content is finalized, put it into a stream for output. - SkAutoTUnref fContentStream; + SkRefPtr fContentStream; }; #endif diff --git a/src/pdf/SkPDFShader.cpp b/src/pdf/SkPDFShader.cpp index 7958de38ce..db5beb8fc8 100644 --- a/src/pdf/SkPDFShader.cpp +++ b/src/pdf/SkPDFShader.cpp @@ -612,7 +612,8 @@ SkPDFFunctionShader::SkPDFFunctionShader(SkPDFShader::State* state) return; } - SkAutoTUnref domain(new SkPDFArray); + SkRefPtr domain = new SkPDFArray; + domain->unref(); // SkRefPtr and new both took a reference. domain->reserve(4); domain->appendScalar(bbox.fLeft); domain->appendScalar(bbox.fRight); @@ -639,14 +640,16 @@ SkPDFFunctionShader::SkPDFFunctionShader(SkPDFShader::State* state) functionCode = codeFunction(*info); } - SkAutoTUnref pdfShader(new SkPDFDict); + SkRefPtr function = makePSFunction(functionCode, domain.get()); + // Pass one reference to fResources, SkRefPtr and new both took a reference. + fResources.push(function.get()); + + SkRefPtr pdfShader = new SkPDFDict; + pdfShader->unref(); // SkRefPtr and new both took a reference. pdfShader->insertInt("ShadingType", 1); pdfShader->insertName("ColorSpace", "DeviceRGB"); pdfShader->insert("Domain", domain.get()); - - SkPDFStream* function = makePSFunction(functionCode, domain.get()); - pdfShader->insert("Function", new SkPDFObjRef(function))->unref(); - fResources.push(function); // Pass ownership to resource list. + pdfShader->insert("Function", new SkPDFObjRef(function.get()))->unref(); insertInt("PatternType", 2); insert("Matrix", SkPDFUtils::MatrixToArray(finalMatrix))->unref(); @@ -822,7 +825,8 @@ SkPDFImageShader::SkPDFImageShader(SkPDFShader::State* state) : fState(state) { } } - SkAutoTUnref patternBBoxArray(new SkPDFArray); + SkRefPtr patternBBoxArray = new SkPDFArray; + patternBBoxArray->unref(); // SkRefPtr and new both took a reference. patternBBoxArray->reserve(4); patternBBoxArray->appendScalar(patternBBox.fLeft); patternBBoxArray->appendScalar(patternBBox.fTop); @@ -830,10 +834,11 @@ SkPDFImageShader::SkPDFImageShader(SkPDFShader::State* state) : fState(state) { patternBBoxArray->appendScalar(patternBBox.fBottom); // Put the canvas into the pattern stream (fContent). - SkAutoTUnref content(pattern.content()); - setData(content.get()); + SkRefPtr content = pattern.content(); + content->unref(); // SkRefPtr and content() both took a reference. pattern.getResources(&fResources, false); + setData(content.get()); insertName("Type", "Pattern"); insertInt("PatternType", 1); insertInt("PaintType", 1); diff --git a/src/pdf/SkPDFStream.cpp b/src/pdf/SkPDFStream.cpp index 38e874ea1b..d113a0b355 100644 --- a/src/pdf/SkPDFStream.cpp +++ b/src/pdf/SkPDFStream.cpp @@ -21,20 +21,19 @@ static bool skip_compression(SkPDFCatalog* catalog) { SkPDFStream::SkPDFStream(SkStream* stream) : fState(kUnused_State), fData(stream) { - SkSafeRef(stream); } SkPDFStream::SkPDFStream(SkData* data) : fState(kUnused_State) { SkMemoryStream* stream = new SkMemoryStream; stream->setData(data); - fData.reset(stream); // Transfer ownership. + fData = stream; + fData->unref(); // SkRefPtr and new both took a reference. } SkPDFStream::SkPDFStream(const SkPDFStream& pdfStream) : SkPDFDict(), fState(kUnused_State), - fData(pdfStream.fData.get()) { - fData.get()->ref(); + fData(pdfStream.fData) { bool removeLength = true; // Don't uncompress an already compressed stream, but we could. if (pdfStream.fState == kCompressed_State) { @@ -85,8 +84,7 @@ size_t SkPDFStream::getOutputSize(SkPDFCatalog* catalog, bool indirect) { SkPDFStream::SkPDFStream() : fState(kUnused_State) {} void SkPDFStream::setData(SkStream* stream) { - fData.reset(stream); - SkSafeRef(stream); + fData = stream; } bool SkPDFStream::populate(SkPDFCatalog* catalog) { @@ -98,7 +96,8 @@ bool SkPDFStream::populate(SkPDFCatalog* catalog) { if (compressedData.getOffset() < fData->getLength()) { SkMemoryStream* stream = new SkMemoryStream; stream->setData(compressedData.copyToData())->unref(); - fData.reset(stream); // Transfer ownership. + fData = stream; + fData->unref(); // SkRefPtr and new both took a reference. insertName("Filter", "FlateDecode"); } fState = kCompressed_State; @@ -109,7 +108,8 @@ bool SkPDFStream::populate(SkPDFCatalog* catalog) { } else if (fState == kNoCompression_State && !skip_compression(catalog) && SkFlate::HaveFlate()) { if (!fSubstitute.get()) { - fSubstitute.reset(new SkPDFStream(*this)); + fSubstitute = new SkPDFStream(*this); + fSubstitute->unref(); // SkRefPtr and new both took a reference. catalog->setSubstitute(this, fSubstitute.get()); } return false; diff --git a/src/pdf/SkPDFStream.h b/src/pdf/SkPDFStream.h index 6f7a08eb81..b3a7ad34e3 100644 --- a/src/pdf/SkPDFStream.h +++ b/src/pdf/SkPDFStream.h @@ -62,8 +62,8 @@ private: State fState; // TODO(vandebo): Use SkData (after removing deprecated constructor). - SkAutoTUnref fData; - SkAutoTUnref fSubstitute; + SkRefPtr fData; + SkRefPtr fSubstitute; typedef SkPDFDict INHERITED; diff --git a/src/pdf/SkPDFTypes.cpp b/src/pdf/SkPDFTypes.cpp index 59250c8534..7fb1e95532 100644 --- a/src/pdf/SkPDFTypes.cpp +++ b/src/pdf/SkPDFTypes.cpp @@ -73,10 +73,7 @@ void SkPDFObject::GetResourcesHelper(SkTDArray* resources, } } -SkPDFObjRef::SkPDFObjRef(SkPDFObject* obj) : fObj(obj) { - SkSafeRef(obj); -} - +SkPDFObjRef::SkPDFObjRef(SkPDFObject* obj) : fObj(obj) {} SkPDFObjRef::~SkPDFObjRef() {} void SkPDFObjRef::emitObject(SkWStream* stream, SkPDFCatalog* catalog, diff --git a/src/pdf/SkPDFTypes.h b/src/pdf/SkPDFTypes.h index 03799d0845..28034ef405 100644 --- a/src/pdf/SkPDFTypes.h +++ b/src/pdf/SkPDFTypes.h @@ -112,7 +112,7 @@ public: virtual size_t getOutputSize(SkPDFCatalog* catalog, bool indirect); private: - SkAutoTUnref fObj; + SkRefPtr fObj; typedef SkPDFObject INHERITED; }; diff --git a/tests/PDFPrimitivesTest.cpp b/tests/PDFPrimitivesTest.cpp index 42fc133b3c..5cbb9057be 100644 --- a/tests/PDFPrimitivesTest.cpp +++ b/tests/PDFPrimitivesTest.cpp @@ -94,9 +94,11 @@ static void SimpleCheckObjectOutput(skiatest::Reporter* reporter, static void TestPDFStream(skiatest::Reporter* reporter) { char streamBytes[] = "Test\nFoo\tBar"; - SkAutoTUnref streamData(new SkMemoryStream( - streamBytes, strlen(streamBytes), true)); - SkAutoTUnref stream(new SkPDFStream(streamData.get())); + SkRefPtr streamData = new SkMemoryStream( + streamBytes, strlen(streamBytes), true); + streamData->unref(); // SkRefPtr and new both took a reference. + SkRefPtr stream = new SkPDFStream(streamData.get()); + stream->unref(); // SkRefPtr and new both took a reference. SimpleCheckObjectOutput( reporter, stream.get(), "<> stream\nTest\nFoo\tBar\nendstream"); @@ -112,7 +114,8 @@ static void TestPDFStream(skiatest::Reporter* reporter) { "with an uncompressed string."; SkAutoDataUnref streamData2(SkData::NewWithCopy(streamBytes2, strlen(streamBytes2))); - SkAutoTUnref stream(new SkPDFStream(streamData2.get())); + SkRefPtr stream = new SkPDFStream(streamData2.get()); + stream->unref(); // SkRefPtr and new both took a reference. SkDynamicMemoryWStream compressedByteStream; SkFlate::Deflate(streamData2.get(), &compressedByteStream); @@ -143,11 +146,13 @@ static void TestPDFStream(skiatest::Reporter* reporter) { static void TestCatalog(skiatest::Reporter* reporter) { SkPDFCatalog catalog((SkPDFDocument::Flags)0); - SkAutoTUnref int1(new SkPDFInt(1)); - SkAutoTUnref int2(new SkPDFInt(2)); - SkAutoTUnref int3(new SkPDFInt(3)); - int1.get()->ref(); - SkAutoTUnref int1Again(int1.get()); + SkRefPtr int1 = new SkPDFInt(1); + int1->unref(); // SkRefPtr and new both took a reference. + SkRefPtr int2 = new SkPDFInt(2); + int2->unref(); // SkRefPtr and new both took a reference. + SkRefPtr int3 = new SkPDFInt(3); + int3->unref(); // SkRefPtr and new both took a reference. + SkRefPtr int1Again(int1.get()); catalog.addObject(int1.get(), false); catalog.addObject(int2.get(), false); @@ -168,9 +173,12 @@ static void TestCatalog(skiatest::Reporter* reporter) { } static void TestObjectRef(skiatest::Reporter* reporter) { - SkAutoTUnref int1(new SkPDFInt(1)); - SkAutoTUnref int2(new SkPDFInt(2)); - SkAutoTUnref int2ref(new SkPDFObjRef(int2.get())); + SkRefPtr int1 = new SkPDFInt(1); + int1->unref(); // SkRefPtr and new both took a reference. + SkRefPtr int2 = new SkPDFInt(2); + int2->unref(); // SkRefPtr and new both took a reference. + SkRefPtr int2ref = new SkPDFObjRef(int2.get()); + int2ref->unref(); // SkRefPtr and new both took a reference. SkPDFCatalog catalog((SkPDFDocument::Flags)0); catalog.addObject(int1.get(), false); @@ -187,11 +195,16 @@ static void TestObjectRef(skiatest::Reporter* reporter) { } static void TestSubstitute(skiatest::Reporter* reporter) { - SkAutoTUnref proxy(new SkPDFTestDict()); - SkAutoTUnref stub(new SkPDFTestDict()); - SkAutoTUnref int33(new SkPDFInt(33)); - SkAutoTUnref stubResource(new SkPDFDict()); - SkAutoTUnref int44(new SkPDFInt(44)); + SkRefPtr proxy = new SkPDFTestDict(); + proxy->unref(); // SkRefPtr and new both took a reference. + SkRefPtr stub = new SkPDFTestDict(); + stub->unref(); // SkRefPtr and new both took a reference. + SkRefPtr int33 = new SkPDFInt(33); + int33->unref(); // SkRefPtr and new both took a reference. + SkRefPtr stubResource = new SkPDFDict(); + stubResource->unref(); // SkRefPtr and new both took a reference. + SkRefPtr int44 = new SkPDFInt(44); + int44->unref(); // SkRefPtr and new both took a reference. stub->insert("Value", int33.get()); stubResource->insert("InnerValue", int44.get()); @@ -218,74 +231,89 @@ static void TestSubstitute(skiatest::Reporter* reporter) { } static void TestPDFPrimitives(skiatest::Reporter* reporter) { - SkAutoTUnref int42(new SkPDFInt(42)); + SkRefPtr int42 = new SkPDFInt(42); + int42->unref(); // SkRefPtr and new both took a reference. SimpleCheckObjectOutput(reporter, int42.get(), "42"); - SkAutoTUnref realHalf(new SkPDFScalar(SK_ScalarHalf)); + SkRefPtr realHalf = new SkPDFScalar(SK_ScalarHalf); + realHalf->unref(); // SkRefPtr and new both took a reference. SimpleCheckObjectOutput(reporter, realHalf.get(), "0.5"); #if defined(SK_SCALAR_IS_FLOAT) - SkAutoTUnref bigScalar(new SkPDFScalar(110999.75f)); + SkRefPtr bigScalar = new SkPDFScalar(110999.75f); + bigScalar->unref(); // SkRefPtr and new both took a reference. #if !defined(SK_ALLOW_LARGE_PDF_SCALARS) SimpleCheckObjectOutput(reporter, bigScalar.get(), "111000"); #else SimpleCheckObjectOutput(reporter, bigScalar.get(), "110999.75"); - SkAutoTUnref biggerScalar(new SkPDFScalar(50000000.1)); + SkRefPtr biggerScalar = new SkPDFScalar(50000000.1); + biggerScalar->unref(); // SkRefPtr and new both took a reference. SimpleCheckObjectOutput(reporter, biggerScalar.get(), "50000000"); - SkAutoTUnref smallestScalar(new SkPDFScalar(1.0/65536)); + SkRefPtr smallestScalar = new SkPDFScalar(1.0/65536); + smallestScalar->unref(); // SkRefPtr and new both took a reference. SimpleCheckObjectOutput(reporter, smallestScalar.get(), "0.00001526"); #endif #endif - SkAutoTUnref stringSimple( - new SkPDFString("test ) string ( foo")); + SkRefPtr stringSimple = new SkPDFString("test ) string ( foo"); + stringSimple->unref(); // SkRefPtr and new both took a reference. SimpleCheckObjectOutput(reporter, stringSimple.get(), "(test \\) string \\( foo)"); - SkAutoTUnref stringComplex( - new SkPDFString("\ttest ) string ( foo")); + SkRefPtr stringComplex = + new SkPDFString("\ttest ) string ( foo"); + stringComplex->unref(); // SkRefPtr and new both took a reference. SimpleCheckObjectOutput(reporter, stringComplex.get(), "<0974657374202920737472696E67202820666F6F>"); - SkAutoTUnref name(new SkPDFName("Test name\twith#tab")); + SkRefPtr name = new SkPDFName("Test name\twith#tab"); + name->unref(); // SkRefPtr and new both took a reference. const char expectedResult[] = "/Test#20name#09with#23tab"; CheckObjectOutput(reporter, name.get(), expectedResult, strlen(expectedResult), false, false); - SkAutoTUnref escapedName(new SkPDFName("A#/%()<>[]{}B")); + SkRefPtr escapedName = new SkPDFName("A#/%()<>[]{}B"); + escapedName->unref(); // SkRefPtr and new both took a reference. const char escapedNameExpected[] = "/A#23#2F#25#28#29#3C#3E#5B#5D#7B#7DB"; CheckObjectOutput(reporter, escapedName.get(), escapedNameExpected, strlen(escapedNameExpected), false, false); // Test that we correctly handle characters with the high-bit set. const unsigned char highBitCString[] = {0xDE, 0xAD, 'b', 'e', 0xEF, 0}; - SkAutoTUnref highBitName( - new SkPDFName((const char*)highBitCString)); + SkRefPtr highBitName = new SkPDFName((const char*)highBitCString); + highBitName->unref(); // SkRefPtr and new both took a reference. const char highBitExpectedResult[] = "/#DE#ADbe#EF"; CheckObjectOutput(reporter, highBitName.get(), highBitExpectedResult, strlen(highBitExpectedResult), false, false); - SkAutoTUnref array(new SkPDFArray); + SkRefPtr array = new SkPDFArray; + array->unref(); // SkRefPtr and new both took a reference. SimpleCheckObjectOutput(reporter, array.get(), "[]"); array->append(int42.get()); SimpleCheckObjectOutput(reporter, array.get(), "[42]"); array->append(realHalf.get()); SimpleCheckObjectOutput(reporter, array.get(), "[42 0.5]"); - SkAutoTUnref int0(new SkPDFInt(0)); + SkRefPtr int0 = new SkPDFInt(0); + int0->unref(); // SkRefPtr and new both took a reference. array->append(int0.get()); SimpleCheckObjectOutput(reporter, array.get(), "[42 0.5 0]"); - SkAutoTUnref int1(new SkPDFInt(1)); + SkRefPtr int1 = new SkPDFInt(1); + int1->unref(); // SkRefPtr and new both took a reference. array->setAt(0, int1.get()); SimpleCheckObjectOutput(reporter, array.get(), "[1 0.5 0]"); - SkAutoTUnref dict(new SkPDFDict); + SkRefPtr dict = new SkPDFDict; + dict->unref(); // SkRefPtr and new both took a reference. SimpleCheckObjectOutput(reporter, dict.get(), "<<>>"); - SkAutoTUnref n1(new SkPDFName("n1")); + SkRefPtr n1 = new SkPDFName("n1"); + n1->unref(); // SkRefPtr and new both took a reference. dict->insert(n1.get(), int42.get()); SimpleCheckObjectOutput(reporter, dict.get(), "<>"); - SkAutoTUnref n2(new SkPDFName("n2")); - SkAutoTUnref n3(new SkPDFName("n3")); + SkRefPtr n2 = new SkPDFName("n2"); + n2->unref(); // SkRefPtr and new both took a reference. + SkRefPtr n3 = new SkPDFName("n3"); + n3->unref(); // SkRefPtr and new both took a reference. dict->insert(n2.get(), realHalf.get()); dict->insert(n3.get(), array.get()); SimpleCheckObjectOutput(reporter, dict.get(), diff --git a/tests/UtilsTest.cpp b/tests/UtilsTest.cpp index 7f27f7e78e..1c2f870d1f 100644 --- a/tests/UtilsTest.cpp +++ b/tests/UtilsTest.cpp @@ -27,6 +27,35 @@ private: SK_DEFINE_INST_COUNT(RefClass) +static void test_refptr(skiatest::Reporter* reporter) { + RefClass* r0 = new RefClass(0); + + SkRefPtr rc0; + REPORTER_ASSERT(reporter, rc0.get() == NULL); + REPORTER_ASSERT(reporter, !rc0); + + SkRefPtr rc1; + REPORTER_ASSERT(reporter, rc0 == rc1); + REPORTER_ASSERT(reporter, rc0.get() != r0); + + rc0 = r0; + REPORTER_ASSERT(reporter, rc0); + REPORTER_ASSERT(reporter, rc0 != rc1); + REPORTER_ASSERT(reporter, rc0.get() == r0); + + rc1 = rc0; + REPORTER_ASSERT(reporter, rc1); + REPORTER_ASSERT(reporter, rc0 == rc1); + REPORTER_ASSERT(reporter, rc0.get() == r0); + + rc0 = NULL; + REPORTER_ASSERT(reporter, rc0.get() == NULL); + REPORTER_ASSERT(reporter, !rc0); + REPORTER_ASSERT(reporter, rc0 != rc1); + + r0->unref(); +} + static void test_autounref(skiatest::Reporter* reporter) { RefClass obj(0); REPORTER_ASSERT(reporter, 1 == obj.getRefCnt()); @@ -149,6 +178,7 @@ static void TestUTF(skiatest::Reporter* reporter) { test_utf16(reporter); test_search(reporter); + test_refptr(reporter); test_autounref(reporter); }