Index: skia/ext/refptr.h |
diff --git a/skia/ext/refptr.h b/skia/ext/refptr.h |
index 514d453e034d69ccd10b96de8bb3a7f8666a93c0..660dc0a722d1d45fb8c356e1a1058181cb43061a 100644 |
--- a/skia/ext/refptr.h |
+++ b/skia/ext/refptr.h |
@@ -13,7 +13,7 @@ namespace skia { |
// this class to avoid dealing with the ref-counting and prevent leaks/crashes |
// due to ref-counting bugs. |
// |
-// Example of Creating an SkShader* and setting it on a SkPaint: |
+// Example of creating a new SkShader* and setting it on a SkPaint: |
// skia::RefPtr<SkShader> shader = skia::AdoptRef(SkGradientShader::Create()); |
// paint.setShader(shader.get()); |
// |
@@ -25,12 +25,16 @@ namespace skia { |
// } |
// skia::RefPtr<SkShader> member_refptr_; |
// |
-// When returning a ref-counted ponter, also return the skia::RefPtr instead. An |
-// example method that creates an SkShader* and returns it: |
+// When returning a ref-counted pointer, also return the skia::RefPtr instead. |
+// An example method that creates an SkShader* and returns it: |
// skia::RefPtr<SkShader> MakeAShader() { |
// return skia::AdoptRef(SkGradientShader::Create()); |
// } |
// |
+// If a Skia API passes a raw pointer that happens to be owned by Skia and |
vandebo (ex-Chrome)
2013/05/17 01:12:31
Why is it important that it's owned by Skia? The
enne (OOO)
2013/05/17 01:23:34
Skia starts its reference counting at ref count of
|
+// already reference counted, then use ShareRef: |
+// skia::RefPtr<SkShader> shader = ShareRef(paint.getShader()); |
+// |
// Never call ref() or unref() on the underlying ref-counted pointer. If you |
// AdoptRef() the raw pointer immediately into a skia::RefPtr and always work |
// with skia::RefPtr instances instead, the ref-counting will be taken care of |
@@ -84,15 +88,27 @@ class RefPtr { |
private: |
T* ptr_; |
+ // This function cannot be public because Skia starts its ref-counted |
+ // objects at refcnt=1. This makes it impossible to differentiate |
+ // between a newly created object (that doesn't need a ref) or an |
+ // already existing object with one owner (that does need a ref). |
explicit RefPtr(T* ptr) : ptr_(ptr) {} |
template<typename U> |
friend RefPtr<U> AdoptRef(U* ptr); |
+ |
+ template<typename U> |
+ friend RefPtr<U> ShareRef(U* ptr); |
}; |
+// For newly created raw pointers. |
template<typename T> |
RefPtr<T> AdoptRef(T* ptr) { return RefPtr<T>(ptr); } |
+// For pointers that are already ref'd by at least one other object. |
piman
2013/05/17 01:04:20
nit: the description threw me off. I guess what ma
enne (OOO)
2013/05/17 01:23:34
Do you have a suggestion for a better succinct com
|
+template<typename T> |
+RefPtr<T> ShareRef(T* ptr) { return RefPtr<T>(SkSafeRef(ptr)); } |
vandebo (ex-Chrome)
2013/05/17 01:12:31
I'm not sure that ShareRef is the right name for t
enne (OOO)
2013/05/17 01:23:34
It is shared. As opposed to AdoptRef (where the c
vandebo (ex-Chrome)
2013/05/17 20:47:49
It is sharing the object, the same way shared_ptr
enne (OOO)
2013/05/17 21:37:02
I dislike NewRef, AddRef, and CreateRef because I
vandebo (ex-Chrome)
2013/05/18 00:06:02
I disagree on both counts. I don't think it's an i
|
+ |
} // namespace skia |
#endif // SKIA_EXT_REFPTR_H_ |