Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(495)

Side by Side Diff: third_party/WebKit/Source/platform/graphics/skia/SkiaUtils.h

Issue 2290903002: Change (Pass)RefPtr<SkXxx> into sk_sp<SkXxx>. (Closed)
Patch Set: Rebasing... Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2006,2007,2008, Google Inc. All rights reserved. 2 * Copyright (c) 2006,2007,2008, Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 136
137 // TODO(fmalita): remove in favor of direct SrcRectConstraint use. 137 // TODO(fmalita): remove in favor of direct SrcRectConstraint use.
138 inline SkCanvas::SrcRectConstraint WebCoreClampingModeToSkiaRectConstraint(Image ::ImageClampingMode clampMode) 138 inline SkCanvas::SrcRectConstraint WebCoreClampingModeToSkiaRectConstraint(Image ::ImageClampingMode clampMode)
139 { 139 {
140 return clampMode == Image::ClampImageToSourceRect 140 return clampMode == Image::ClampImageToSourceRect
141 ? SkCanvas::kStrict_SrcRectConstraint 141 ? SkCanvas::kStrict_SrcRectConstraint
142 : SkCanvas::kFast_SrcRectConstraint; 142 : SkCanvas::kFast_SrcRectConstraint;
143 } 143 }
144 144
145 // Skia's smart pointer APIs are preferable over their legacy raw pointer counte rparts. 145 // Skia's smart pointer APIs are preferable over their legacy raw pointer counte rparts.
146 // The following helpers ensure interoperability between Skia's SkRefCnt wrapper sk_sp<T> and
147 // Blink's RefPtr<T>/PassRefPtr<T>.
148 //
149 // - fromSkSp(sk_sp<T>): adopts an sk_sp into a PassRefPtr (to be used w hen transferring
150 // ownership from Skia to Blink).
151 // - toSkSp(PassRefPtr<T>): releases a PassRefPtr into a sk_sp (to be used when transferring
152 // ownership from Blink to Skia).
153 // - toSkSp(const RefPtr<T>&): shares a RefPtr as a new sk_sp (to be used when sharing
154 // ownership).
155 // 146 //
156 // General guidelines 147 // General guidelines
157 // 148 //
158 // When receiving ref counted objects from Skia: 149 // When receiving ref counted objects from Skia:
159 // 150 //
160 // 1) use sk_sp-based Skia factories if available (e.g. SkShader::MakeFoo() in stead of 151 // 1) use sk_sp-based Skia factories if available (e.g. SkShader::MakeFoo() in stead of
161 // SkShader::CreateFoo()) 152 // SkShader::CreateFoo())
162 // 153 //
163 // 2) use sk_sp<T> locals for temporary objects (to be immediately transferred back to Skia) 154 // 2) use sk_sp<T> locals for all objects
164 //
165 // 3) use RefPtr<T>/PassRefPtr<T> for objects to be retained in Blink, use
166 // fromSkSp(sk_sp<T>) to convert
167 // 155 //
168 // When passing ref counted objects to Skia: 156 // When passing ref counted objects to Skia:
169 // 157 //
170 // 1) use sk_sk-based Skia APIs when available (e.g. SkPaint::setShader(sk_sp< SkShader>) 158 // 1) use sk_sp-based Skia APIs when available (e.g. SkPaint::setShader(sk_sp< SkShader>)
171 // instead of SkPaint::setShader(SkShader*)) 159 // instead of SkPaint::setShader(SkShader*))
172 // 160 //
173 // 2) if the object ownership is being passed to Skia, use std::move(sk_sp<T>) or 161 // 2) if the object ownership is being passed to Skia, use std::move(sk_sp<T>)
174 // toSkSp(PassRefPtr<T>) to transfer without refcount churn
175 //
176 // 3) if the object ownership is shared with Skia (Blink retains a reference), use
177 // toSkSp(const RefPtr<T>&)
178 // 162 //
179 // Example (creating a SkShader and setting it on SkPaint): 163 // Example (creating a SkShader and setting it on SkPaint):
180 // 164 //
181 // a) legacy/old style 165 // a) ownership transferred
182 //
183 // RefPtr<SkShader> shader = adoptRef(SkShader::CreateFoo(...));
184 // paint.setShader(shader.get());
185 //
186 // (Note: the legacy approach introduces refcount churn as Skia grabs a ref whi le Blink is
187 // temporarily holding on to its own)
188 //
189 // b) new style, ownership transferred
190 // 166 //
191 // // using Skia smart pointer locals 167 // // using Skia smart pointer locals
192 // sk_sp<SkShader> shader = SkShader::MakeFoo(...); 168 // sk_sp<SkShader> shader = SkShader::MakeFoo(...);
193 // paint.setShader(std::move(shader)); 169 // paint.setShader(std::move(shader));
194 // 170 //
195 // // using Blink smart pointer locals
196 // RefPtr<SkShader> shader = fromSkSp(SkShader::MakeFoo(...));
197 // paint.setShader(toSkSp(shader.release());
198 //
199 // // using no locals 171 // // using no locals
200 // paint.setShader(SkShader::MakeFoo(...)); 172 // paint.setShader(SkShader::MakeFoo(...));
201 // 173 //
202 // c) new style, shared ownership 174 // b) shared ownership
203 // 175 //
204 // RefPtr<SkShader> shader = fromSkSp(SkShader::MakeFoo(...)); 176 // sk_sp<SkShader> shader = SkShader::MakeFoo(...);
205 // paint.setShader(toSkSp(shader)); 177 // paint.setShader(shader);
206 //
207 template <typename T> PassRefPtr<T> fromSkSp(sk_sp<T> sp)
208 {
209 return adoptRef(sp.release());
210 }
211
212 template <typename T> sk_sp<T> toSkSp(PassRefPtr<T> ref)
213 {
214 return sk_sp<T>(ref.leakRef());
215 }
216
217 template <typename T> sk_sp<T> toSkSp(const RefPtr<T>& ref)
218 {
219 return toSkSp(PassRefPtr<T>(ref));
220 }
221 178
222 } // namespace blink 179 } // namespace blink
223 180
224 #endif // SkiaUtils_h 181 #endif // SkiaUtils_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698