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

Unified Diff: src/core/SkColorSpace.cpp

Issue 2192903002: Add writeToMemory() API to SkColorSpace (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Added unit test Created 4 years, 5 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « include/core/SkColorSpace.h ('k') | tests/ColorSpaceTest.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/core/SkColorSpace.cpp
diff --git a/src/core/SkColorSpace.cpp b/src/core/SkColorSpace.cpp
index ba278dfcd69d81bde8235f0e392f96c7c67a14f8..24c1833a9227f350c4e3f963ec5585f6f9a14500 100644
--- a/src/core/SkColorSpace.cpp
+++ b/src/core/SkColorSpace.cpp
@@ -200,15 +200,16 @@ struct ColorSpaceHeader {
uint8_t fFlags; // Some combination of the flags listed above
};
-sk_sp<SkData> SkColorSpace::serialize() const {
+size_t SkColorSpace::writeToMemory(void* memory) const {
// If we have a named profile, only write the enum.
switch (fNamed) {
case kSRGB_Named:
case kAdobeRGB_Named: {
- sk_sp<SkData> data = SkData::MakeUninitialized(sizeof(ColorSpaceHeader));
- *((ColorSpaceHeader*) data->writable_data()) =
- ColorSpaceHeader::Pack(k0_Version, fNamed, fGammaNamed, 0);
- return data;
+ if (memory) {
+ *((ColorSpaceHeader*) memory) =
+ ColorSpaceHeader::Pack(k0_Version, fNamed, fGammaNamed, 0);
+ }
+ return sizeof(ColorSpaceHeader);
}
default:
break;
@@ -219,16 +220,14 @@ sk_sp<SkData> SkColorSpace::serialize() const {
case kSRGB_GammaNamed:
case k2Dot2Curve_GammaNamed:
case kLinear_GammaNamed: {
- sk_sp<SkData> data = SkData::MakeUninitialized(sizeof(ColorSpaceHeader) +
- 12 * sizeof(float));
- void* dataPtr = data->writable_data();
-
- *((ColorSpaceHeader*) dataPtr) = ColorSpaceHeader::Pack(k0_Version, fNamed, fGammaNamed,
- ColorSpaceHeader::kMatrix_Flag);
- dataPtr = SkTAddOffset<void>(dataPtr, sizeof(ColorSpaceHeader));
-
- fToXYZD50.as4x3ColMajorf((float*) dataPtr);
- return data;
+ if (memory) {
+ *((ColorSpaceHeader*) memory) =
+ ColorSpaceHeader::Pack(k0_Version, fNamed, fGammaNamed,
+ ColorSpaceHeader::kMatrix_Flag);
+ memory = SkTAddOffset<void>(memory, sizeof(ColorSpaceHeader));
+ fToXYZD50.as4x3ColMajorf((float*) memory);
+ }
+ return sizeof(ColorSpaceHeader) + 12 * sizeof(float);
}
default:
break;
@@ -240,22 +239,31 @@ sk_sp<SkData> SkColorSpace::serialize() const {
size_t profileSize = as_CSB(this)->fProfileData->size();
if (SkAlign4(profileSize) != (uint32_t) SkAlign4(profileSize)) {
- return nullptr;
+ return 0;
}
- sk_sp<SkData> data = SkData::MakeUninitialized(sizeof(ColorSpaceHeader) + sizeof(uint32_t) +
- SkAlign4(profileSize));
- void* dataPtr = data->writable_data();
+ if (memory) {
+ *((ColorSpaceHeader*) memory) = ColorSpaceHeader::Pack(k0_Version, fNamed, fGammaNamed,
+ ColorSpaceHeader::kICC_Flag);
+ memory = SkTAddOffset<void>(memory, sizeof(ColorSpaceHeader));
- *((ColorSpaceHeader*) dataPtr) = ColorSpaceHeader::Pack(k0_Version, fNamed, fGammaNamed,
- ColorSpaceHeader::kICC_Flag);
- dataPtr = SkTAddOffset<void>(dataPtr, sizeof(ColorSpaceHeader));
+ *((uint32_t*) memory) = (uint32_t) SkAlign4(profileSize);
+ memory = SkTAddOffset<void>(memory, sizeof(uint32_t));
- *((uint32_t*) dataPtr) = (uint32_t) SkAlign4(profileSize);
- dataPtr = SkTAddOffset<void>(dataPtr, sizeof(uint32_t));
+ memcpy(memory, as_CSB(this)->fProfileData->data(), profileSize);
+ memset(SkTAddOffset<void>(memory, profileSize), 0, SkAlign4(profileSize) - profileSize);
+ }
+ return sizeof(ColorSpaceHeader) + sizeof(uint32_t) + SkAlign4(profileSize);
+}
+
+sk_sp<SkData> SkColorSpace::serialize() const {
+ size_t size = this->writeToMemory(nullptr);
+ if (0 == size) {
+ return nullptr;
+ }
- memcpy(dataPtr, as_CSB(this)->fProfileData->data(), profileSize);
- memset(SkTAddOffset<void>(dataPtr, profileSize), 0, SkAlign4(profileSize) - profileSize);
+ sk_sp<SkData> data = SkData::MakeUninitialized(size);
+ this->writeToMemory(data->writable_data());
return data;
}
« no previous file with comments | « include/core/SkColorSpace.h ('k') | tests/ColorSpaceTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698