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

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

Issue 1789063005: Add sk_sp helpers and switch Blink SkShader clients to the new APIs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fromSkSp, review comments Created 4 years, 9 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 20 matching lines...) Expand all
31 // All of the functions in this file should move to new homes and this file shou ld be deleted. 31 // All of the functions in this file should move to new homes and this file shou ld be deleted.
32 32
33 #ifndef SkiaUtils_h 33 #ifndef SkiaUtils_h
34 #define SkiaUtils_h 34 #define SkiaUtils_h
35 35
36 #include "platform/PlatformExport.h" 36 #include "platform/PlatformExport.h"
37 #include "platform/graphics/GraphicsTypes.h" 37 #include "platform/graphics/GraphicsTypes.h"
38 #include "platform/graphics/Image.h" 38 #include "platform/graphics/Image.h"
39 #include "platform/transforms/AffineTransform.h" 39 #include "platform/transforms/AffineTransform.h"
40 #include "third_party/skia/include/core/SkCanvas.h" 40 #include "third_party/skia/include/core/SkCanvas.h"
41 #include "third_party/skia/include/core/SkRefCnt.h"
41 #include "wtf/MathExtras.h" 42 #include "wtf/MathExtras.h"
42 43
43 namespace blink { 44 namespace blink {
44 45
45 class GraphicsContext; 46 class GraphicsContext;
46 47
47 SkXfermode::Mode PLATFORM_EXPORT WebCoreCompositeToSkiaComposite(CompositeOperat or, WebBlendMode = WebBlendModeNormal); 48 SkXfermode::Mode PLATFORM_EXPORT WebCoreCompositeToSkiaComposite(CompositeOperat or, WebBlendMode = WebBlendModeNormal);
48 CompositeOperator PLATFORM_EXPORT compositeOperatorFromSkia(SkXfermode::Mode); 49 CompositeOperator PLATFORM_EXPORT compositeOperatorFromSkia(SkXfermode::Mode);
49 WebBlendMode PLATFORM_EXPORT blendModeFromSkia(SkXfermode::Mode); 50 WebBlendMode PLATFORM_EXPORT blendModeFromSkia(SkXfermode::Mode);
50 51
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 void drawPlatformFocusRing(const PrimitiveType&, SkCanvas*, SkColor, int width); 120 void drawPlatformFocusRing(const PrimitiveType&, SkCanvas*, SkColor, int width);
120 121
121 // TODO(fmalita): remove in favor of direct SrcRectConstraint use. 122 // TODO(fmalita): remove in favor of direct SrcRectConstraint use.
122 inline SkCanvas::SrcRectConstraint WebCoreClampingModeToSkiaRectConstraint(Image ::ImageClampingMode clampMode) 123 inline SkCanvas::SrcRectConstraint WebCoreClampingModeToSkiaRectConstraint(Image ::ImageClampingMode clampMode)
123 { 124 {
124 return clampMode == Image::ClampImageToSourceRect 125 return clampMode == Image::ClampImageToSourceRect
125 ? SkCanvas::kStrict_SrcRectConstraint 126 ? SkCanvas::kStrict_SrcRectConstraint
126 : SkCanvas::kFast_SrcRectConstraint; 127 : SkCanvas::kFast_SrcRectConstraint;
127 } 128 }
128 129
130 // Skia's smart pointer APIs are preferable over their legacy raw pointer counte rparts.
131 // The following helpers ensure interoperability between Skia's SkRefCnt wrapper sk_sp<T> and
132 // Blink's RefPtr<T>/PassRefPtr<T>.
133 //
134 // - fromSkSp(sk_sp<T>): adopts an sk_sp into a PassRefPtr (to be used w hen transferring
135 // ownership from Skia to Blink).
136 // - toSkSp(PassRefPtr<T>): releases a PassRefPtr into a sk_sp (to be used when transferring
137 // ownership from Blink to Skia).
138 // - toSkSp(const RefPtr<T>&): shares a RefPtr as a new sk_sp (to be used when sharing
139 // ownership).
140 //
141 // General guidelines
142 //
143 // When receiving ref counted objects from Skia:
144 //
145 // 1) use sk_sp-based Skia factories if available (e.g. SkShader::MakeFoo() in stead of
146 // SkShader::CreateFoo())
147 //
148 // 2) use sk_sp<T> locals for temporary objects (to be immediately transferred back to Skia)
149 //
150 // 3) use RefPtr<T>/PassRefPtr<T> for objects to be retained in Blink, use
151 // fromSkSp(sk_sp<T>) to convert
152 //
153 // When passing ref counted objects to Skia:
154 //
155 // 1) use sk_sk-based Skia APIs when available (e.g. SkPaint::setShader(sk_sp< SkShader>)
156 // instead of SkPaint::setShader(SkShader*))
157 //
158 // 2) if the object ownership is being passed to Skia, use std::move(sk_sp<T>) or
159 // toSkSp(PassRefPtr<T>) to transfer without refcount churn
160 //
161 // 3) if the object ownership is shared with Skia (Blink retains a reference), use
162 // toSkSp(const RefPtr<T>&)
163 //
164 // Example (creating a SkShader and setting it on SkPaint):
165 //
166 // a) legacy/old style
167 //
168 // RefPtr<SkShader> shader = adoptRef(SkShader::CreateFoo(...));
169 // paint.setShader(shader.get());
170 //
171 // (Note: the legacy approach introduces refcount churn as Skia grabs a ref whi le Blink is
172 // temporarily holding on to its own)
173 //
174 // b) new style, ownership transferred
175 //
176 // // using Skia smart pointer locals
177 // sk_sp<SkShader> shader = SkShader::MakeFoo(...);
178 // paint.setShader(std::move(shader));
179 //
180 // // using Blink smart pointer locals
181 // RefPtr<SkShader> shader = fromSkSp(SkShader::MakeFoo(...));
182 // paint.setShader(toSkSp(shader.release());
183 //
184 // // using no locals
185 // paint.setShader(SkShader::MakeFoo(...));
186 //
187 // c) new style, shared ownership
188 //
189 // RefPtr<SkShader> shader = fromSkSp(SkShader::MakeFoo(...));
190 // paint.setShader(toSkSp(shader));
191 //
192 template <typename T> PassRefPtr<T> fromSkSp(sk_sp<T> sp)
193 {
194 return adoptRef(sp.release());
195 }
196
197 template <typename T> sk_sp<T> toSkSp(PassRefPtr<T> ref)
198 {
199 return sk_sp<T>(ref.leakRef());
200 }
201
202 template <typename T> sk_sp<T> toSkSp(const RefPtr<T>& ref)
203 {
204 return toSkSp(PassRefPtr<T>(ref));
205 }
206
129 } // namespace blink 207 } // namespace blink
130 208
131 #endif // SkiaUtils_h 209 #endif // SkiaUtils_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698