Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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 Loading... | |
| 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 | |
| 131 // Skia's smart pointer APIs are preferable over their legacy raw pointer counte rparts. | |
| 132 // The following helpers ensure interoperability between Skia's SkRefCnt wrapper sk_sp<T> and | |
| 133 // Blink's RefPtr<T>/PassRefPtr<T>. | |
| 134 // | |
| 135 // - adoptRef(sk_sp<T>&&): adopts an sk_sp rvalue into a PassRefPtr (to be used when | |
| 136 // transferring ownership from Skia to Blink). | |
| 137 // - adoptSkSp(PassRefPtr<T>&&): adopts a PassRefPtr rvalue into a sk_sp (to b e used when | |
| 138 // transferring ownership from Blink to Skia). | |
| 139 // | |
| 140 // General guidelines | |
| 141 // | |
| 142 // When receiving ref counted objects from Skia: | |
| 143 // | |
| 144 // 1) use sk_sp-based Skia factories if available (e.g. SkShader::MakeFoo() in stead of | |
| 145 // SkShader::CreateFoo()) | |
| 146 // | |
| 147 // 2) use sk_sp<T> locals for temporary objects (to be immediately transferred back to Skia) | |
| 148 // | |
| 149 // 3) use RefPtr<T>/PassRefPtr<T> for objects to be retained in Blink, use | |
| 150 // adoptRef(sk_sp<T>&&) to convert | |
| 151 // | |
| 152 // When passing ref counted objects to Skia: | |
| 153 // | |
| 154 // 1) use sk_sk-based Skia APIs when available (e.g. SkPaint::setShader(sk_sp< SkShader>) | |
| 155 // instead of SkPaint::setShader(SkShader*)) | |
| 156 // | |
| 157 // 2) if the object ownership is being passed to Skia, use std::move(sk_sp<T>) or | |
| 158 // adoptSkSp(PassRefPtr<T>&&) to transfer without refcount churn | |
| 159 // | |
| 160 // 3) if the object ownership is shared with Skia (Blink retains a reference), use | |
| 161 // sk_ref_sp(RefPtr<T>::get()) | |
| 162 // | |
| 163 // Example (creating a SkShader and setting it on SkPaint): | |
| 164 // | |
| 165 // a) legacy/old style | |
| 166 // | |
| 167 // RefPtr<SkShader> shader = adoptRef(SkShader::CreateFoo(...)); | |
| 168 // paint.setShader(shader.get()); | |
| 169 // | |
| 170 // (Note: the legacy approach introduces refcount churn as Skia grabs a ref whi le Blink is | |
| 171 // temporarily holding on to its own) | |
| 172 // | |
| 173 // b) new style, ownership transferred | |
| 174 // | |
| 175 // // using Skia smart pointer locals | |
| 176 // sk_sp<SkShader> shader = SkShader::MakeFoo(...); | |
| 177 // paint.setShader(std::move(shader)); | |
| 178 // | |
| 179 // // using Blink smart pointer locals | |
| 180 // RefPtr<SkShader> shader = adoptRef(SkShader::MakeFoo(...)); | |
| 181 // paint.setShader(adoptSkSp(shader.release()); | |
| 182 // | |
| 183 // // using no locals | |
| 184 // paint.setShader(SkShader::MakeFoo(...)); | |
| 185 // | |
| 186 // (Note: while all these variants avoid ref count churn, the last/inlined-fact ory version | |
| 187 // is preferred when feasible because it also skips the adopt/move op) | |
|
jbroman
2016/03/16 14:43:08
Why? The adopt/move will presumably be elided by t
f(malita)
2016/03/16 16:51:45
Experimentally, the first example above does not e
| |
| 188 // | |
| 189 // c) new style, shared ownership | |
| 190 // | |
| 191 // // using sk_ref_sp | |
| 192 // RefPtr<SkShader> m_shader = adoptRef(SkShader::MakeFoo(...)); | |
| 193 // paint.setShader(sk_ref_sp(m_shader.get())); | |
| 194 // | |
| 195 // // using a temp PassRefPtr and sk_sp adoption | |
| 196 // RefPtr<SkShader> m_shader = adoptRef(SkShader::MakeFoo(...)); | |
| 197 // paint.setShader(adoptSkSp(PassRefPtr<SkShader>(m_shader))); | |
| 198 // | |
| 199 template <typename T> PassRefPtr<T> adoptRef(sk_sp<T>&& sp) | |
|
jbroman
2016/03/16 14:43:08
I also think taking the sk_sp by value is probably
f(malita)
2016/03/16 16:51:45
We've had a little discussion regarding val vs. rv
danakj
2016/03/16 17:54:53
(google style guide also says not to do write && e
| |
| 200 { | |
| 201 return adoptRef(sp.release()); | |
| 202 } | |
| 203 | |
| 204 template <typename T> sk_sp<T> adoptSkSp(PassRefPtr<T>&& ref) | |
|
jbroman
2016/03/16 14:43:08
To be consistent with other usage of PassRefPtr, I
f(malita)
2016/03/16 16:51:45
Makes sense, done.
| |
| 205 { | |
| 206 return sk_sp<T>(ref.leakRef()); | |
| 207 } | |
| 208 | |
| 129 } // namespace blink | 209 } // namespace blink |
| 130 | 210 |
| 131 #endif // SkiaUtils_h | 211 #endif // SkiaUtils_h |
| OLD | NEW |