aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar bungeman <bungeman@google.com>2015-10-01 12:28:49 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2015-10-01 12:28:49 -0700
commit77a53de20d723ca21cc824fd97e68aaa60e022ea (patch)
treef5fc67e8b4c3ca465e4c6fe820c945d138af80d1
parent52f8deba607fd67558b57551cd43406c8b743072 (diff)
Base SkAutoTUnref on skstd::unique_ptr.
To further consolidate the various unique owning classes, this bases SkAutoTUnref on skstd::unique_ptr. Users are updated because of two breaking changes, swap now takes a reference and reset no longer returns its argument. Review URL: https://codereview.chromium.org/1370803002
-rw-r--r--include/core/SkRefCnt.h47
-rw-r--r--src/core/SkPath.cpp2
-rw-r--r--src/effects/SkGpuBlurUtils.cpp10
-rw-r--r--tests/FontHostStreamTest.cpp3
-rw-r--r--tools/CopyTilesRenderer.cpp2
-rw-r--r--tools/PictureRenderer.cpp4
6 files changed, 22 insertions, 46 deletions
diff --git a/include/core/SkRefCnt.h b/include/core/SkRefCnt.h
index 1ba59b33c0..0e8d857722 100644
--- a/include/core/SkRefCnt.h
+++ b/include/core/SkRefCnt.h
@@ -9,6 +9,7 @@
#define SkRefCnt_DEFINED
#include "../private/SkAtomics.h"
+#include "../private/SkUniquePtr.h"
#include "SkTypes.h"
/** \class SkRefCntBase
@@ -143,7 +144,7 @@ class SK_API SkRefCnt : public SkRefCntBase { };
} while (0)
-/** Call obj->ref() and return obj. The obj must not be NULL.
+/** Call obj->ref() and return obj. The obj must not be nullptr.
*/
template <typename T> static inline T* SkRef(T* obj) {
SkASSERT(obj);
@@ -171,51 +172,25 @@ template <typename T> static inline void SkSafeUnref(T* obj) {
template<typename T> static inline void SkSafeSetNull(T*& obj) {
if (obj) {
obj->unref();
- obj = NULL;
+ obj = nullptr;
}
}
///////////////////////////////////////////////////////////////////////////////
+template <typename T> struct SkTUnref {
+ void operator()(T* t) { t->unref(); }
+};
+
/**
* Utility class that simply unref's its argument in the destructor.
*/
-template <typename T> class SkAutoTUnref : SkNoncopyable {
+template <typename T> class SkAutoTUnref : public skstd::unique_ptr<T, SkTUnref<T>> {
public:
- explicit SkAutoTUnref(T* obj = NULL) : fObj(obj) {}
- ~SkAutoTUnref() { SkSafeUnref(fObj); }
-
- T* get() const { return fObj; }
-
- T* reset(T* obj) {
- SkSafeUnref(fObj);
- fObj = obj;
- return obj;
- }
-
- void swap(SkAutoTUnref* other) {
- T* tmp = fObj;
- fObj = other->fObj;
- other->fObj = tmp;
- }
+ explicit SkAutoTUnref(T* obj = nullptr) : skstd::unique_ptr<T, SkTUnref<T>>(obj) {}
- /**
- * Return the hosted object (which may be null), transferring ownership.
- * The reference count is not modified, and the internal ptr is set to NULL
- * so unref() will not be called in our destructor. A subsequent call to
- * detach() will do nothing and return null.
- */
- T* detach() {
- T* obj = fObj;
- fObj = NULL;
- return obj;
- }
-
- T* operator->() const { return fObj; }
- operator T*() const { return fObj; }
-
-private:
- T* fObj;
+ T* detach() { return this->release(); }
+ operator T*() const { return this->get(); }
};
// Can't use the #define trick below to guard a bare SkAutoTUnref(...) because it's templated. :(
diff --git a/src/core/SkPath.cpp b/src/core/SkPath.cpp
index 97e34386dc..1326362524 100644
--- a/src/core/SkPath.cpp
+++ b/src/core/SkPath.cpp
@@ -180,7 +180,7 @@ bool operator==(const SkPath& a, const SkPath& b) {
void SkPath::swap(SkPath& that) {
if (this != &that) {
- fPathRef.swap(&that.fPathRef);
+ fPathRef.swap(that.fPathRef);
SkTSwap<int>(fLastMoveToIndex, that.fLastMoveToIndex);
SkTSwap<uint8_t>(fFillType, that.fFillType);
SkTSwap<uint8_t>(fConvexity, that.fConvexity);
diff --git a/src/effects/SkGpuBlurUtils.cpp b/src/effects/SkGpuBlurUtils.cpp
index fd27cc3600..5690c2893f 100644
--- a/src/effects/SkGpuBlurUtils.cpp
+++ b/src/effects/SkGpuBlurUtils.cpp
@@ -231,7 +231,7 @@ GrTexture* GaussianBlur(GrContext* context,
dstDrawContext->drawNonAARectToRect(dstTexture->asRenderTarget(), clip, paint,
SkMatrix::I(), dstRect, srcRect);
- srcDrawContext.swap(&dstDrawContext);
+ srcDrawContext.swap(dstDrawContext);
srcRect = dstRect;
srcTexture = dstTexture;
SkTSwap(dstTexture, tempTexture);
@@ -254,7 +254,7 @@ GrTexture* GaussianBlur(GrContext* context,
convolve_gaussian_2d(dstDrawContext, dstTexture->asRenderTarget(), clip, srcRect, dstRect,
srcTexture, radiusX, radiusY, sigmaX, sigmaY, cropToRect, srcIRect);
- srcDrawContext.swap(&dstDrawContext);
+ srcDrawContext.swap(dstDrawContext);
srcRect = dstRect;
srcTexture = dstTexture;
SkTSwap(dstTexture, tempTexture);
@@ -286,7 +286,7 @@ GrTexture* GaussianBlur(GrContext* context,
srcTexture, Gr1DKernelEffect::kX_Direction, radiusX, sigmaX,
cropToRect);
- srcDrawContext.swap(&dstDrawContext);
+ srcDrawContext.swap(dstDrawContext);
srcTexture = dstTexture;
srcRect = dstRect;
SkTSwap(dstTexture, tempTexture);
@@ -319,7 +319,7 @@ GrTexture* GaussianBlur(GrContext* context,
dstRect, srcTexture, Gr1DKernelEffect::kY_Direction, radiusY, sigmaY,
cropToRect);
- srcDrawContext.swap(&dstDrawContext);
+ srcDrawContext.swap(dstDrawContext);
srcTexture = dstTexture;
srcRect = dstRect;
SkTSwap(dstTexture, tempTexture);
@@ -355,7 +355,7 @@ GrTexture* GaussianBlur(GrContext* context,
dstDrawContext->drawNonAARectToRect(dstTexture->asRenderTarget(), clip, paint,
SkMatrix::I(), dstRect, srcRect);
- srcDrawContext.swap(&dstDrawContext);
+ srcDrawContext.swap(dstDrawContext);
srcRect = dstRect;
srcTexture = dstTexture;
SkTSwap(dstTexture, tempTexture);
diff --git a/tests/FontHostStreamTest.cpp b/tests/FontHostStreamTest.cpp
index 7843a412a1..f4cc5bf3a9 100644
--- a/tests/FontHostStreamTest.cpp
+++ b/tests/FontHostStreamTest.cpp
@@ -92,7 +92,8 @@ DEF_TEST(FontHostStream, reporter) {
SkTypeface* origTypeface = paint.getTypeface();
SkAutoTUnref<SkTypeface> aur;
if (nullptr == origTypeface) {
- origTypeface = aur.reset(SkTypeface::RefDefault());
+ aur.reset(SkTypeface::RefDefault());
+ origTypeface = aur.get();
}
int ttcIndex;
diff --git a/tools/CopyTilesRenderer.cpp b/tools/CopyTilesRenderer.cpp
index f5ffb25d03..a78d772c68 100644
--- a/tools/CopyTilesRenderer.cpp
+++ b/tools/CopyTilesRenderer.cpp
@@ -35,7 +35,7 @@ namespace sk_tools {
SkASSERT(pict != nullptr);
// Only work with absolute widths (as opposed to percentages).
SkASSERT(this->getTileWidth() != 0 && this->getTileHeight() != 0);
- fPicture.reset(pict)->ref();
+ fPicture.reset(SkRef(pict));
this->CopyString(&fWritePath, writePath);
this->CopyString(&fMismatchPath, mismatchPath);
this->CopyString(&fInputFilename, inputFilename);
diff --git a/tools/PictureRenderer.cpp b/tools/PictureRenderer.cpp
index 21bb933b35..19ee7b5f2a 100644
--- a/tools/PictureRenderer.cpp
+++ b/tools/PictureRenderer.cpp
@@ -76,7 +76,7 @@ void PictureRenderer::init(const SkPicture* pict,
return;
}
- fPicture.reset(pict)->ref();
+ fPicture.reset(SkRef(pict));
fCanvas.reset(this->setupCanvas());
}
@@ -503,7 +503,7 @@ void TiledPictureRenderer::init(const SkPicture* pict, const SkString* writePath
// Do not call INHERITED::init(), which would create a (potentially large) canvas which is not
// used by bench_pictures.
- fPicture.reset(pict)->ref();
+ fPicture.reset(SkRef(pict));
this->CopyString(&fWritePath, writePath);
this->CopyString(&fMismatchPath, mismatchPath);
this->CopyString(&fInputFilename, inputFilename);