Revert "Remove SkRefPtr" - r7021

samplecode/ still needs to be updated.

Review URL: https://codereview.appspot.com/7032048

git-svn-id: http://skia.googlecode.com/svn/trunk@7022 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/src/pdf/SkPDFDevice.cpp b/src/pdf/SkPDFDevice.cpp
index 7f189aa..0de011c 100644
--- a/src/pdf/SkPDFDevice.cpp
+++ b/src/pdf/SkPDFDevice.cpp
@@ -1006,7 +1006,8 @@
         fResourceDict = SkNEW(SkPDFDict);
 
         if (fGraphicStateResources.count()) {
-            SkAutoTUnref<SkPDFDict> extGState(new SkPDFDict());
+            SkRefPtr<SkPDFDict> 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 @@
         }
 
         if (fXObjectResources.count()) {
-            SkAutoTUnref<SkPDFDict> xObjects(new SkPDFDict());
+            SkRefPtr<SkPDFDict> 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 @@
         }
 
         if (fFontResources.count()) {
-            SkAutoTUnref<SkPDFDict> fonts(new SkPDFDict());
+            SkRefPtr<SkPDFDict> 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 @@
         }
 
         if (fShaderResources.count()) {
-            SkAutoTUnref<SkPDFDict> patterns(new SkPDFDict());
+            SkRefPtr<SkPDFDict> 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 @@
         // For compatibility, add all proc sets (only used for output to PS
         // devices).
         const char procs[][7] = {"PDF", "Text", "ImageB", "ImageC", "ImageI"};
-        SkAutoTUnref<SkPDFArray> procSets(new SkPDFArray());
+        SkRefPtr<SkPDFArray> 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 @@
     SkPaint stockPaint;
     this->drawPaint(draw, stockPaint);
     SkAutoTUnref<SkPDFFormXObject> maskFormXObject(createFormXObjectFromDevice());
-    SkAutoTUnref<SkPDFGraphicState> sMaskGS(
-        SkPDFGraphicState::GetSMaskGraphicState(maskFormXObject, invertClip));
+    SkRefPtr<SkPDFGraphicState> 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 @@
     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 @@
         return;
     }
 
-    SkAutoTUnref<SkPDFGraphicState> sMaskGS;
+    SkRefPtr<SkPDFGraphicState> 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 @@
     entry->fShaderIndex = -1;
 
     // PDF treats a shader as a color, so we only set one or the other.
-    SkAutoTUnref<SkPDFObject> pdfShader;
+    SkRefPtr<SkPDFObject> pdfShader;
     const SkShader* shader = paint.getShader();
     SkColor color = paint.getColor();
     if (shader) {
@@ -1485,7 +1494,8 @@
         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 @@
             if (resourceIndex < 0) {
                 resourceIndex = fShaderResources.count();
                 fShaderResources.push(pdfShader.get());
-                pdfShader.get()->ref();
+                pdfShader->ref();
             }
             entry->fShaderIndex = resourceIndex;
         } else {
@@ -1513,16 +1523,15 @@
         }
     }
 
-    SkAutoTUnref<SkPDFGraphicState> newGraphicState;
+    SkRefPtr<SkPDFGraphicState> 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 @@
 }
 
 int SkPDFDevice::getFontResourceIndex(SkTypeface* typeface, uint16_t glyphID) {
-    SkAutoTUnref<SkPDFFont> newFont(SkPDFFont::GetFontResource(typeface, glyphID));
+    SkRefPtr<SkPDFFont> 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 c7266d8..7e064ee 100644
--- a/src/pdf/SkPDFDocument.cpp
+++ b/src/pdf/SkPDFDocument.cpp
@@ -98,11 +98,13 @@
         fDocCatalog->insert("Pages", new SkPDFObjRef(pageTreeRoot))->unref();
 
         /* TODO(vandebo): output intent
-        SkAutoTUnref<SkPDFDict> outputIntent = new SkPDFDict("OutputIntent");
+        SkRefPtr<SkPDFDict> 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<SkPDFArray> intentArray = new SkPDFArray;
+        SkRefPtr<SkPDFArray> 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 f2b3f53..9a8f136 100644
--- a/src/pdf/SkPDFFont.cpp
+++ b/src/pdf/SkPDFFont.cpp
@@ -155,12 +155,13 @@
     // 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;
+    SkRefPtr<SkMemoryStream> 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 @@
                 break;
             }
             case SkAdvancedTypefaceMetrics::WidthRange::kRange: {
-                SkAutoTUnref<SkPDFArray> advanceArray(new SkPDFArray());
+                SkRefPtr<SkPDFArray> 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 @@
     append_tounicode_header(&cmap);
     append_cmap_sections(glyphToUnicode, subset, &cmap);
     append_cmap_footer(&cmap);
-    SkAutoTUnref<SkMemoryStream> cmapStream(new SkMemoryStream());
+    SkRefPtr<SkMemoryStream> 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 @@
                                   const SkTypeface* typeface,
                                   const SkTDArray<uint32_t>& subset,
                                   SkPDFStream** fontStream) {
-    SkAutoTUnref<SkStream> fontData(
-            SkFontHost::OpenStream(SkTypeface::UniqueID(typeface)));
+    SkRefPtr<SkStream> fontData =
+            SkFontHost::OpenStream(SkTypeface::UniqueID(typeface));
+    fontData->unref();  // SkRefPtr and OpenStream both took a ref.
 
     int fontSize = fontData->getLength();
 
@@ -762,12 +766,11 @@
         return CanonicalFonts()[relatedFontIndex].fFont;
     }
 
-    SkAutoTUnref<SkAdvancedTypefaceMetrics> fontMetrics;
+    SkRefPtr<SkAdvancedTypefaceMetrics> 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 @@
         info = SkTBitOr<SkAdvancedTypefaceMetrics::PerGlyphInfo>(
                   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<SkAdvancedTypefaceMetrics::PerGlyphInfo>(
                       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 @@
           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 @@
     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::addResource(SkPDFObject* object) {
     SkASSERT(object != NULL);
     fResources.push(object);
-    object->ref();
 }
 
 SkPDFDict* SkPDFFont::getFontDescriptor() {
@@ -930,8 +931,7 @@
 }
 
 void SkPDFFont::setFontDescriptor(SkPDFDict* descriptor) {
-    fDescriptor.reset(descriptor);
-    SkSafeRef(descriptor);
+    fDescriptor = descriptor;
 }
 
 bool SkPDFFont::addCommonFontDescriptorEntries(int16_t defaultWidth) {
@@ -1002,9 +1002,9 @@
     if (fFontInfo == NULL || fFontInfo->fGlyphToUnicode.begin() == NULL) {
         return;
     }
-    SkAutoTUnref<SkPDFStream> pdfCmap(
-        generate_tounicode_cmap(fFontInfo->fGlyphToUnicode, subset));
-    addResource(pdfCmap.get());
+    SkRefPtr<SkPDFStream> 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 @@
     insertName("BaseFont", fontInfo()->fFontName);
     insertName("Encoding", "Identity-H");
 
-    SkAutoTUnref<SkPDFCIDFont> newCIDFont(
-            new SkPDFCIDFont(fontInfo(), typeface(), subset));
-    addResource(newCIDFont.get());
-    SkAutoTUnref<SkPDFArray> 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<SkPDFArray> 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 @@
 
 bool SkPDFCIDFont::addFontDescriptor(int16_t defaultWidth,
                                      const SkTDArray<uint32_t>* subset) {
-    SkAutoTUnref<SkPDFDict> descriptor(new SkPDFDict("FontDescriptor"));
+    SkRefPtr<SkPDFDict> 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 @@
                                                   &rawStream);
             SkASSERT(fontSize);
             SkASSERT(rawStream);
-            SkAutoTUnref<SkPDFStream> fontStream(rawStream);
+            SkRefPtr<SkPDFStream> 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<SkStream> fontData(
-                SkFontHost::OpenStream(SkTypeface::UniqueID(typeface())));
-            SkAutoTUnref<SkPDFStream> fontStream(
-                new SkPDFStream(fontData.get()));
+            SkRefPtr<SkStream> fontData =
+                SkFontHost::OpenStream(SkTypeface::UniqueID(typeface()));
+            fontData->unref();  // SkRefPtr and OpenStream both took a ref.
+            SkRefPtr<SkPDFStream> 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 @@
             SkASSERT(false);
     }
 
+    addResource(descriptor.get());
+    descriptor->ref();
+
     insert("FontDescriptor", new SkPDFObjRef(descriptor.get()))->unref();
     return addCommonFontDescriptorEntries(defaultWidth);
 }
@@ -1124,18 +1132,20 @@
             subset->exportTo(&glyphIDs);
         }
 
+        SkRefPtr<SkAdvancedTypefaceMetrics> fontMetrics;
         SkAdvancedTypefaceMetrics::PerGlyphInfo info;
         info = SkAdvancedTypefaceMetrics::kGlyphNames_PerGlyphInfo;
         info = SkTBitOr<SkAdvancedTypefaceMetrics::PerGlyphInfo>(
                   info, SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo);
         uint32_t* glyphs = (glyphIDs.count() == 1) ? NULL : glyphIDs.begin();
         uint32_t glyphsCount = glyphs ? glyphIDs.count() : 0;
-        SkAutoTUnref<SkAdvancedTypefaceMetrics> 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 @@
         SkASSERT(false);
     }
 
-    SkAutoTUnref<SkPDFDict> sysInfo(new SkPDFDict);
+    SkRefPtr<SkPDFDict> 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 @@
 
     if (fontInfo()->fGlyphWidths.get()) {
         int16_t defaultWidth = 0;
-        SkAutoTUnref<SkPDFArray> widths(
+        SkRefPtr<SkPDFArray> 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 @@
         defaultAdvance.fVerticalAdvance = 0;
         defaultAdvance.fOriginXDisp = 0;
         defaultAdvance.fOriginYDisp = 0;
-        SkAutoTUnref<SkPDFArray> advances(
+        SkRefPtr<SkPDFArray> 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() {}
 
 bool SkPDFType1Font::addFontDescriptor(int16_t defaultWidth) {
-    if (getFontDescriptor() != NULL) {
-        SkPDFDict* descriptor = getFontDescriptor();
-        addResource(descriptor);
-        insert("FontDescriptor", new SkPDFObjRef(descriptor))->unref();
+    SkRefPtr<SkPDFDict> descriptor = getFontDescriptor();
+    if (descriptor.get() != NULL) {
+        addResource(descriptor.get());
+        descriptor->ref();
+        insert("FontDescriptor", new SkPDFObjRef(descriptor.get()))->unref();
         return true;
     }
 
-    SkAutoTUnref<SkPDFDict> 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<SkStream> rawFontData(
-        SkFontHost::OpenStream(SkTypeface::UniqueID(typeface())));
+    SkRefPtr<SkStream> 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<SkPDFStream> fontStream(new SkPDFStream(fontData));
+    SkRefPtr<SkPDFStream> 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 @@
     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 @@
 
     addWidthInfoFromRange(defaultWidth, widthRangeEntry);
 
-    SkAutoTUnref<SkPDFDict> encoding(new SkPDFDict("Encoding"));
+    SkRefPtr<SkPDFDict> encoding = new SkPDFDict("Encoding");
+    encoding->unref();  // SkRefPtr and new both took a reference.
     insert("Encoding", encoding.get());
 
-    SkAutoTUnref<SkPDFArray> encDiffs(new SkPDFArray);
+    SkRefPtr<SkPDFArray> 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 @@
 void SkPDFType1Font::addWidthInfoFromRange(
         int16_t defaultWidth,
         const SkAdvancedTypefaceMetrics::WidthRange* widthRangeEntry) {
-    SkAutoTUnref<SkPDFArray> widthArray(new SkPDFArray());
+    SkRefPtr<SkPDFArray> 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 @@
     fontMatrix.setScale(SkScalarInvert(1000), -SkScalarInvert(1000));
     insert("FontMatrix", SkPDFUtils::MatrixToArray(fontMatrix))->unref();
 
-    SkAutoTUnref<SkPDFDict> charProcs(new SkPDFDict);
+    SkRefPtr<SkPDFDict> charProcs = new SkPDFDict;
+    charProcs->unref();  // SkRefPtr and new both took a reference.
     insert("CharProcs", charProcs.get());
 
-    SkAutoTUnref<SkPDFDict> encoding(new SkPDFDict("Encoding"));
+    SkRefPtr<SkPDFDict> encoding = new SkPDFDict("Encoding");
+    encoding->unref();  // SkRefPtr and new both took a reference.
     insert("Encoding", encoding.get());
 
-    SkAutoTUnref<SkPDFArray> encDiffs(new SkPDFArray);
+    SkRefPtr<SkPDFArray> 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<SkPDFArray> widthArray(new SkPDFArray());
+    SkRefPtr<SkPDFArray> 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 @@
             SkPDFUtils::PaintPath(paint.getStyle(), path->getFillType(),
                                   &content);
         }
-        SkAutoTUnref<SkMemoryStream> glyphStream(new SkMemoryStream());
+        SkRefPtr<SkMemoryStream> glyphStream = new SkMemoryStream();
+        glyphStream->unref();  // SkRefPtr and new both took a ref.
         glyphStream->setData(content.copyToData())->unref();
 
-        SkAutoTUnref<SkPDFStream> glyphDescription(
-            new SkPDFStream(glyphStream.get()));
+        SkRefPtr<SkPDFStream> 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 693b911..f463ed7 100644
--- a/src/pdf/SkPDFFont.h
+++ b/src/pdf/SkPDFFont.h
@@ -180,7 +180,7 @@
         FontRec(SkPDFFont* font, uint32_t fontID, uint16_t fGlyphID);
     };
 
-    SkAutoTUnref<SkTypeface> fTypeface;
+    SkRefPtr<SkTypeface> 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 @@
     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<SkAdvancedTypefaceMetrics> fFontInfo;
+    SkRefPtr<SkAdvancedTypefaceMetrics> fFontInfo;
     SkTDArray<SkPDFObject*> fResources;
-    SkAutoTUnref<SkPDFDict> fDescriptor;
+    SkRefPtr<SkPDFDict> fDescriptor;
 
     SkAdvancedTypefaceMetrics::FontType fFontType;
 
diff --git a/src/pdf/SkPDFFormXObject.cpp b/src/pdf/SkPDFFormXObject.cpp
index 5e33995..e148056 100644
--- a/src/pdf/SkPDFFormXObject.cpp
+++ b/src/pdf/SkPDFFormXObject.cpp
@@ -31,7 +31,8 @@
     getResources(&dummy_resourceList);
 #endif
 
-    SkAutoTUnref<SkStream> content(device->content());
+    SkRefPtr<SkStream> content = device->content();
+    content->unref();  // SkRefPtr and content() both took a reference.
     setData(content.get());
 
     insertName("Type", "XObject");
@@ -54,7 +55,8 @@
 
     // Right now SkPDFFormXObject is only used for saveLayer, which implies
     // isolated blending.  Do this conditionally if that changes.
-    SkAutoTUnref<SkPDFDict> group(new SkPDFDict("Group"));
+    SkRefPtr<SkPDFDict> 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 c326b3b..ec9b0e7 100644
--- a/src/pdf/SkPDFGraphicState.cpp
+++ b/src/pdf/SkPDFGraphicState.cpp
@@ -113,14 +113,16 @@
     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<SkPDFArray> domainAndRange(new SkPDFArray);
+        SkRefPtr<SkPDFArray> 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<SkMemoryStream> psInvertStream(
-            new SkMemoryStream(&psInvert, strlen(psInvert), true));
+        SkRefPtr<SkMemoryStream> 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 @@
     // enough that it's not worth canonicalizing.
     SkAutoMutexAcquire lock(CanonicalPaintsMutex());
 
-    SkAutoTUnref<SkPDFDict> sMaskDict(new SkPDFDict("Mask"));
+    SkRefPtr<SkPDFDict> 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 @@
         fPopulated = true;
         insertName("Type", "ExtGState");
 
-        SkAutoTUnref<SkPDFScalar> alpha(
-            new SkPDFScalar(SkScalarDiv(fPaint.getAlpha(), 0xFF)));
+        SkRefPtr<SkPDFScalar> 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 1b93f6e..429667a 100644
--- a/src/pdf/SkPDFImage.cpp
+++ b/src/pdf/SkPDFImage.cpp
@@ -305,7 +305,8 @@
     if (!doingAlpha && alphaOnly) {
         // For alpha only images, we stretch a single pixel of black for
         // the color/shape part.
-        SkAutoTUnref<SkPDFInt> one(new SkPDFInt(1));
+        SkRefPtr<SkPDFInt> 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 @@
     insertInt("BitsPerComponent", bitsPerComp);
 
     if (config == SkBitmap::kRGB_565_Config) {
-        SkAutoTUnref<SkPDFInt> zeroVal(new SkPDFInt(0));
-        SkAutoTUnref<SkPDFScalar> scale5Val(
-                new SkPDFScalar(SkFloatToScalar(8.2258f)));  // 255/2^5-1
-        SkAutoTUnref<SkPDFScalar> scale6Val(
-                new SkPDFScalar(SkFloatToScalar(4.0476f)));  // 255/2^6-1
-        SkAutoTUnref<SkPDFArray> decodeValue(new SkPDFArray());
+        SkRefPtr<SkPDFInt> zeroVal = new SkPDFInt(0);
+        zeroVal->unref();  // SkRefPtr and new both took a reference.
+        SkRefPtr<SkPDFScalar> scale5Val =
+                new SkPDFScalar(SkFloatToScalar(8.2258f));  // 255/2^5-1
+        scale5Val->unref();  // SkRefPtr and new both took a reference.
+        SkRefPtr<SkPDFScalar> scale6Val =
+                new SkPDFScalar(SkFloatToScalar(4.0476f));  // 255/2^6-1
+        scale6Val->unref();  // SkRefPtr and new both took a reference.
+        SkRefPtr<SkPDFArray> 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 a18beb1..717f435 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 @@
             }
         }
 
-        SkAutoTUnref<SkStream> content(fDevice->content());
-        fContentStream.reset(new SkPDFStream(content.get()));
+        SkRefPtr<SkStream> 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 @@
     // one child.
     static const int kNodeSize = 8;
 
-    SkAutoTUnref<SkPDFName> kidsName(new SkPDFName("Kids"));
-    SkAutoTUnref<SkPDFName> countName(new SkPDFName("Count"));
-    SkAutoTUnref<SkPDFName> parentName(new SkPDFName("Parent"));
+    SkRefPtr<SkPDFName> kidsName = new SkPDFName("Kids");
+    kidsName->unref();  // SkRefPtr and new both took a reference.
+    SkRefPtr<SkPDFName> countName = new SkPDFName("Count");
+    countName->unref();  // SkRefPtr and new both took a reference.
+    SkRefPtr<SkPDFName> 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<SkPDFDict*> curNodes;
@@ -91,9 +95,11 @@
             }
 
             SkPDFDict* newNode = new SkPDFDict("Pages");
-            SkAutoTUnref<SkPDFObjRef> newNodeRef(new SkPDFObjRef(newNode));
+            SkRefPtr<SkPDFObjRef> newNodeRef = new SkPDFObjRef(newNode);
+            newNodeRef->unref();  // SkRefPtr and new both took a reference.
 
-            SkAutoTUnref<SkPDFArray> kids(new SkPDFArray);
+            SkRefPtr<SkPDFArray> 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 72ba335..8ef909e 100644
--- a/src/pdf/SkPDFPage.h
+++ b/src/pdf/SkPDFPage.h
@@ -91,10 +91,10 @@
 
 private:
     // Multiple pages may reference the content.
-    SkAutoTUnref<SkPDFDevice> fDevice;
+    SkRefPtr<SkPDFDevice> fDevice;
 
     // Once the content is finalized, put it into a stream for output.
-    SkAutoTUnref<SkPDFStream> fContentStream;
+    SkRefPtr<SkPDFStream> fContentStream;
 };
 
 #endif
diff --git a/src/pdf/SkPDFShader.cpp b/src/pdf/SkPDFShader.cpp
index 7958de3..db5beb8 100644
--- a/src/pdf/SkPDFShader.cpp
+++ b/src/pdf/SkPDFShader.cpp
@@ -612,7 +612,8 @@
         return;
     }
 
-    SkAutoTUnref<SkPDFArray> domain(new SkPDFArray);
+    SkRefPtr<SkPDFArray> 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 @@
         functionCode = codeFunction(*info);
     }
 
-    SkAutoTUnref<SkPDFDict> pdfShader(new SkPDFDict);
+    SkRefPtr<SkPDFStream> function = makePSFunction(functionCode, domain.get());
+    // Pass one reference to fResources, SkRefPtr and new both took a reference.
+    fResources.push(function.get());
+
+    SkRefPtr<SkPDFDict> 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 @@
         }
     }
 
-    SkAutoTUnref<SkPDFArray> patternBBoxArray(new SkPDFArray);
+    SkRefPtr<SkPDFArray> 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 @@
     patternBBoxArray->appendScalar(patternBBox.fBottom);
 
     // Put the canvas into the pattern stream (fContent).
-    SkAutoTUnref<SkStream> content(pattern.content());
-    setData(content.get());
+    SkRefPtr<SkStream> 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 38e874e..d113a0b 100644
--- a/src/pdf/SkPDFStream.cpp
+++ b/src/pdf/SkPDFStream.cpp
@@ -21,20 +21,19 @@
 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 @@
 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 @@
             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 @@
     } 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 6f7a08e..b3a7ad3 100644
--- a/src/pdf/SkPDFStream.h
+++ b/src/pdf/SkPDFStream.h
@@ -62,8 +62,8 @@
     State fState;
 
     // TODO(vandebo): Use SkData (after removing deprecated constructor).
-    SkAutoTUnref<SkStream> fData;
-    SkAutoTUnref<SkPDFStream> fSubstitute;
+    SkRefPtr<SkStream> fData;
+    SkRefPtr<SkPDFStream> fSubstitute;
 
     typedef SkPDFDict INHERITED;
 
diff --git a/src/pdf/SkPDFTypes.cpp b/src/pdf/SkPDFTypes.cpp
index 59250c8..7fb1e95 100644
--- a/src/pdf/SkPDFTypes.cpp
+++ b/src/pdf/SkPDFTypes.cpp
@@ -73,10 +73,7 @@
     }
 }
 
-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 03799d0..28034ef 100644
--- a/src/pdf/SkPDFTypes.h
+++ b/src/pdf/SkPDFTypes.h
@@ -112,7 +112,7 @@
     virtual size_t getOutputSize(SkPDFCatalog* catalog, bool indirect);
 
 private:
-    SkAutoTUnref<SkPDFObject> fObj;
+    SkRefPtr<SkPDFObject> fObj;
 
     typedef SkPDFObject INHERITED;
 };