| 1 |
/* |
| 2 |
* Copyright 2025 Google LLC |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "src/capture/SkCapture.h" |
| 9 |
|
| 10 |
#include "include/core/SkBitmap.h" |
| 11 |
#include "include/core/SkCanvas.h" |
| 12 |
#include "include/core/SkData.h" |
| 13 |
#include "include/core/SkFourByteTag.h" |
| 14 |
#include "include/core/SkImage.h" |
| 15 |
#include "include/core/SkPicture.h" |
| 16 |
#include "include/core/SkRefCnt.h" |
| 17 |
#include "include/core/SkSerialProcs.h" |
| 18 |
#include "include/core/SkStream.h" |
| 19 |
#include "include/private/base/SkTArray.h" |
| 20 |
#include "src/capture/SkCaptureManager.h" |
| 21 |
|
| 22 |
constexpr SkFourByteTag kMagic1 = SkSetFourByteTag('s','k','i','a'); |
| 23 |
constexpr SkFourByteTag kMagic2 = SkSetFourByteTag('c','a','p','t'); |
| 24 |
|
| 25 |
sk_sp<SkCapture> SkCapture::MakeFromData(sk_sp<const SkData> data) { |
| 26 |
if (!data) { |
| 27 |
return nullptr; |
| 28 |
} |
| 29 |
|
| 30 |
// 1. Setup Stream |
| 31 |
SkMemoryStream stream(data->data(), data->size()); |
| 32 |
|
| 33 |
// 2. Read and Validate Magic Number |
| 34 |
uint32_t magic1; |
| 35 |
uint32_t magic2; |
| 36 |
if (!stream.readU32(&magic1) || !stream.readU32(&magic2) || |
| 37 |
magic1 != kMagic1 || magic2 != kMagic2) { |
| 38 |
SkDebugf("Invalid magic number for SkCapture.\n"); |
| 39 |
return nullptr; |
| 40 |
} |
| 41 |
|
| 42 |
// 3. Read and Validate Version |
| 43 |
uint32_t version; |
| 44 |
if (!stream.readU32(&version) || version != kVersion) { |
| 45 |
SkDebugf("Unsupported SkCapture version: %u.\n", version); |
| 46 |
return nullptr; |
| 47 |
} |
| 48 |
|
| 49 |
// 4. Read Picture Count |
| 50 |
uint32_t pictureCount; |
| 51 |
if (!stream.readU32(&pictureCount)) { |
| 52 |
SkDebugf("Failed to read picture count.\n"); |
| 53 |
return nullptr; |
| 54 |
} |
| 55 |
|
| 56 |
auto capture = sk_make_sp<SkCapture>(); |
| 57 |
capture->fMetadata = {version, pictureCount}; |
| 58 |
|
| 59 |
// 5. Loop and Deserialize Each Picture |
| 60 |
for (uint32_t i = 0; i < pictureCount; ++i) { |
| 61 |
uint32_t pictureDataSize; |
| 62 |
if (!stream.readU32(&pictureDataSize)) { |
| 63 |
SkDebugf("Failed to read picture data size for picture %u.\n", i); |
| 64 |
return nullptr; |
| 65 |
} |
| 66 |
|
| 67 |
// Read the picture data into an SkData object |
| 68 |
sk_sp<SkData> pictureData = SkData::MakeUninitialized(pictureDataSize); |
| 69 |
|
| 70 |
// Check if allocation failed *or* if the stream read failed. |
| 71 |
if (!pictureData || stream.read(pictureData->writable_data(), |
| 72 |
pictureDataSize) != pictureDataSize) { |
| 73 |
SkDebugf("Failed to read picture data for picture %u or allocation failed.\n", i); |
| 74 |
return nullptr; |
| 75 |
} |
| 76 |
|
| 77 |
// Deserialize the SkPicture from its raw data |
| 78 |
SkDeserialProcs procs; |
| 79 |
procs.fImageDataProc = SkCapture::deserializeImageProc; |
| 80 |
sk_sp<SkPicture> picture = SkPicture::MakeFromData(pictureData.get(), &procs); |
| 81 |
if (!picture) { |
| 82 |
SkDebugf("Failed to deserialize SkPicture for picture %u.\n", i); |
| 83 |
return nullptr; |
| 84 |
} |
| 85 |
|
| 86 |
// Add the deserialized picture to the SkCapture object |
| 87 |
capture->fPictures.emplace_back(std::move(picture)); |
| 88 |
} |
| 89 |
|
| 90 |
SkDebugf("Successfully read %d pictures into SkCapture.\n", capture->fPictures.size()); |
| 91 |
return capture; |
| 92 |
} |
| 93 |
|
| 94 |
sk_sp<SkCapture> SkCapture::MakeFromPictures(skia_private::TArray<sk_sp<SkPicture>> pictures) { |
| 95 |
auto capture = sk_make_sp<SkCapture>(); |
| 96 |
|
| 97 |
capture->fMetadata = {SkCapture::kVersion, static_cast<uint32_t>(pictures.size())}; |
| 98 |
capture->fPictures = pictures; |
| 99 |
return capture; |
| 100 |
} |
| 101 |
|
| 102 |
sk_sp<SkPicture> SkCapture::getPicture(int i) const { |
| 103 |
if (i >= 0 && i < fPictures.size()) { |
| 104 |
return fPictures[i]; |
| 105 |
} |
| 106 |
return nullptr; |
| 107 |
} |
| 108 |
|
| 109 |
SkCapture::Metadata SkCapture::getMetadata() const { |
| 110 |
return fMetadata; |
| 111 |
} |
| 112 |
|
| 113 |
sk_sp<SkData> SkCapture::serializeCapture() { |
| 114 |
SkDynamicMemoryWStream stream; |
| 115 |
|
| 116 |
stream.write32(kMagic1); |
| 117 |
stream.write32(kMagic2); |
| 118 |
stream.write32(SkCapture::kVersion); |
| 119 |
|
| 120 |
// Number of pictures |
| 121 |
stream.write32(fPictures.size()); |
| 122 |
|
| 123 |
// TODO (b/412351769): Write metadata on each picture and assosiated canvas. This will be needed |
| 124 |
// when we have multiple SkPictures per canvas and we want to track which ones are drawn into |
| 125 |
// each other. |
| 126 |
|
| 127 |
for (const auto& picture : fPictures) { |
| 128 |
SkDynamicMemoryWStream pictureStream; |
| 129 |
SkSerialProcs procs; |
| 130 |
procs.fImageProc = SkCapture::serializeImageProc; |
| 131 |
picture->serialize(&pictureStream, &procs); |
| 132 |
sk_sp<SkData> pictureData = pictureStream.detachAsData(); |
| 133 |
|
| 134 |
// Write size and then data |
| 135 |
stream.write32(pictureData->size()); |
| 136 |
stream.write(pictureData->data(), pictureData->size()); |
| 137 |
} |
| 138 |
|
| 139 |
auto data = stream.detachAsData(); |
| 140 |
SkDebugf("Wrote %d pictures to SkData block.\n", fPictures.size()); |
| 141 |
return data; |
| 142 |
} |
| 143 |
|
| 144 |
// TODO(b/412351769): When serializing our SkPictures, the images that are drawn through drawImage |
| 145 |
// and similar functions will also need to be serialized. Instead of naively encoding all images as |
| 146 |
// PNGs, we want the images that refer to content created from an SkSurface to point to its |
| 147 |
// corresponding SkPicture. This means that we need to create a context for the proc to track these |
| 148 |
// contentIDs. |
| 149 |
SkSerialReturnType SkCapture::serializeImageProc(SkImage* img, void* ctx) { |
| 150 |
const int contentID = -1; // TODO: replace with real content ID. |
| 151 |
return SkData::MakeWithCopy(&contentID, sizeof(int)); |
| 152 |
} |
| 153 |
|
| 154 |
sk_sp<SkImage> SkCapture::deserializeImageProc(sk_sp<SkData>, std::optional<SkAlphaType>, void*) { |
| 155 |
// TODO: set up the SkCapture context and inspect it to grab SkPictures and pass them as images. |
| 156 |
SkBitmap b; |
| 157 |
b.allocN32Pixels(5, 5); |
| 158 |
SkCanvas canvas(b); |
| 159 |
canvas.drawColor(SK_ColorMAGENTA); |
| 160 |
return b.asImage(); |
| 161 |
} |