diff options
author | enne@chromium.org <enne@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-05-18 09:40:26 +0000 |
---|---|---|
committer | enne@chromium.org <enne@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-05-18 09:40:26 +0000 |
commit | 584abe77f84b1bf56e9633e63d4fe60947eabe27 (patch) | |
tree | 8f362a9a98484c54f67968d42705c82f16328b0c /skia | |
parent | 5a260744b81b9d3be148c2ac2b9387ab2f015213 (diff) | |
download | chromium_src-584abe77f84b1bf56e9633e63d4fe60947eabe27.zip chromium_src-584abe77f84b1bf56e9633e63d4fe60947eabe27.tar.gz chromium_src-584abe77f84b1bf56e9633e63d4fe60947eabe27.tar.bz2 |
Only use skia::RefPtr for refcounting
For consistency and sanity in Chromium, only use skia::RefPtr in Chromium to
ref count skia classes. SkRefPtr is unsafe to use for newly created objects
because it refs the object that is passed to its constructor. skia::RefPtr
makes this adoption explicit it via skia::AdoptRef and so is much clearer.
This patch also adds a skia::ShareRef function which makes it explicit that the
callsite is adopting a ref which is already owned somewhere else. Using
AdoptRef vs. ShareRef seems much clearer than using SkRefPtr vs. skia::RefPtr.
These are the remaining code sites that use internal Skia reference counted
classes. Once these have been removed, then we can use a PRESUBMIT rule to
prevent new uses from being added.
BUG=none
Review URL: https://chromiumcodereview.appspot.com/15004024
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@200989 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'skia')
-rw-r--r-- | skia/ext/refptr.h | 26 |
1 files changed, 23 insertions, 3 deletions
diff --git a/skia/ext/refptr.h b/skia/ext/refptr.h index 514d453..a3900f6 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,18 @@ 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()); // } // +// To take a scoped reference to an object whose references are all owned +// by other objects (i.e. does not have one that needs to be adopted) use the +// skia::SharePtr helper: +// +// skia::RefPtr<SkShader> shader = skia::SharePtr(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 +90,29 @@ 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 to be ref'd) or an + // already existing object with one owner (that does need to be ref'd so that + // this RefPtr can also manage its lifetime). explicit RefPtr(T* ptr) : ptr_(ptr) {} template<typename U> friend RefPtr<U> AdoptRef(U* ptr); + + template<typename U> + friend RefPtr<U> SharePtr(U* ptr); }; +// For objects that have an unowned reference (such as newly created objects). template<typename T> RefPtr<T> AdoptRef(T* ptr) { return RefPtr<T>(ptr); } +// For objects that are already owned. This doesn't take ownership of existing +// references and adds a new one. +template<typename T> +RefPtr<T> SharePtr(T* ptr) { return RefPtr<T>(SkSafeRef(ptr)); } + } // namespace skia #endif // SKIA_EXT_REFPTR_H_ |