Allow lambdas with captures for SkSerialProcs/SkDeserialProcs
See also: http://ag/24692530
Canary-Android-Topic: explicit_png
Change-Id: Id5bb9c34b7a89fdfff35a8508856d697b4c9b004
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/753496
Owners-Override: Kevin Lubick <kjlubick@google.com>
Reviewed-by: Leon Scroggins <scroggo@google.com>
Commit-Queue: Kevin Lubick <kjlubick@google.com>
diff --git a/include/core/SkSerialProcs.h b/include/core/SkSerialProcs.h
index 01a3669..d33ab84 100644
--- a/include/core/SkSerialProcs.h
+++ b/include/core/SkSerialProcs.h
@@ -12,6 +12,7 @@
#include "include/private/base/SkAPI.h"
#include <cstddef>
+#include <functional>
class SkData;
class SkImage;
@@ -28,15 +29,16 @@
* The default action for typefaces is to use Skia's internal format.
*/
-using SkSerialPictureProc = sk_sp<SkData> (*)(SkPicture*, void* ctx);
-using SkSerialImageProc = sk_sp<SkData> (*)(SkImage*, void* ctx);
-using SkSerialTypefaceProc = sk_sp<SkData> (*)(SkTypeface*, void* ctx);
+using SkSerialPictureProc = std::function<sk_sp<SkData>(SkPicture*, void* ctx)>;
+using SkSerialImageProc = std::function<sk_sp<SkData>(SkImage*, void* ctx)>;
+using SkSerialTypefaceProc = std::function<sk_sp<SkData>(SkTypeface*, void* ctx)>;
/**
* Called with the encoded form of a picture (previously written with a custom
* SkSerialPictureProc proc). Return a picture object, or nullptr indicating failure.
*/
-using SkDeserialPictureProc = sk_sp<SkPicture> (*)(const void* data, size_t length, void* ctx);
+using SkDeserialPictureProc =
+ std::function<sk_sp<SkPicture>(const void* data, size_t length, void* ctx)>;
/**
* Called with the encoded form of an image. The proc can return an image object, or if it
@@ -47,13 +49,15 @@
* Note that unlike SkDeserialPictureProc and SkDeserialTypefaceProc, return nullptr from this
* does not indicate failure, but is a signal for Skia to take its default action.
*/
-using SkDeserialImageProc = sk_sp<SkImage> (*)(const void* data, size_t length, void* ctx);
+using SkDeserialImageProc =
+ std::function<sk_sp<SkImage>(const void* data, size_t length, void* ctx)>;
/**
* Called with the encoded form of a typeface (previously written with a custom
* SkSerialTypefaceProc proc). Return a typeface object, or nullptr indicating failure.
*/
-using SkDeserialTypefaceProc = sk_sp<SkTypeface> (*)(const void* data, size_t length, void* ctx);
+using SkDeserialTypefaceProc =
+ std::function<sk_sp<SkTypeface>(const void* data, size_t length, void* ctx)>;
struct SK_API SkSerialProcs {
SkSerialPictureProc fPictureProc = nullptr;
diff --git a/relnotes/procs-functional.md b/relnotes/procs-functional.md
new file mode 100644
index 0000000..ebcb657
--- /dev/null
+++ b/relnotes/procs-functional.md
@@ -0,0 +1,3 @@
+The structs `SkSerialProcs` and `SkDeserialProcs` now take in `std::function`
+instead of bare function pointers. This allows clients to pass in functors
+(lambdas with captured objects) if desired.
\ No newline at end of file
diff --git a/src/core/SkPicture.cpp b/src/core/SkPicture.cpp
index 5d53829..fad684a 100644
--- a/src/core/SkPicture.cpp
+++ b/src/core/SkPicture.cpp
@@ -26,6 +26,7 @@
#include <atomic>
#include <cstring>
+#include <functional>
#include <memory>
// When we read/write the SkPictInfo via a stream, we have a sentinel byte right after the info.
diff --git a/src/core/SkReadBuffer.cpp b/src/core/SkReadBuffer.cpp
index 48c293d..4c9ddb9 100644
--- a/src/core/SkReadBuffer.cpp
+++ b/src/core/SkReadBuffer.cpp
@@ -30,6 +30,7 @@
#include "src/core/SkMipmapBuilder.h"
#include "src/core/SkWriteBuffer.h"
+#include <functional>
#include <memory>
#include <optional>
#include <utility>
diff --git a/src/core/SkWriteBuffer.cpp b/src/core/SkWriteBuffer.cpp
index 5b2ca36..70758bb 100644
--- a/src/core/SkWriteBuffer.cpp
+++ b/src/core/SkWriteBuffer.cpp
@@ -28,6 +28,7 @@
#include "src/image/SkImage_Base.h"
#include <cstring>
+#include <functional>
#include <utility>
class SkMatrix;
diff --git a/tests/MultiPictureDocumentTest.cpp b/tests/MultiPictureDocumentTest.cpp
index 7694be3..427226a 100644
--- a/tests/MultiPictureDocumentTest.cpp
+++ b/tests/MultiPictureDocumentTest.cpp
@@ -33,6 +33,7 @@
#include "tools/SkSharingProc.h"
#include "tools/ToolUtils.h"
+#include <functional>
#include <memory>
#include <vector>
diff --git a/tests/SerializationTest.cpp b/tests/SerializationTest.cpp
index e7d867e..0e175e7 100644
--- a/tests/SerializationTest.cpp
+++ b/tests/SerializationTest.cpp
@@ -61,6 +61,7 @@
#include <array>
#include <cstdint>
#include <cstring>
+#include <functional>
#include <memory>
#include <utility>
diff --git a/tests/TextBlobTest.cpp b/tests/TextBlobTest.cpp
index 11b6dc7..c932a15 100644
--- a/tests/TextBlobTest.cpp
+++ b/tests/TextBlobTest.cpp
@@ -33,6 +33,7 @@
#include <algorithm>
#include <cstdint>
#include <cstring>
+#include <functional>
#include <string>
using namespace skia_private;