Coverage Report

Created: 2024-05-20 07:14

/src/skia/tools/SkSharingProc.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2019 Google Inc.
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 "tools/SkSharingProc.h"
9
10
#include "include/codec/SkPngDecoder.h"
11
#include "include/core/SkBitmap.h"
12
#include "include/core/SkData.h"
13
#include "include/core/SkImage.h"
14
#include "include/core/SkPicture.h"
15
#include "include/core/SkSerialProcs.h"
16
#include "include/core/SkStream.h"
17
#include "include/encode/SkPngEncoder.h"
18
19
namespace {
20
0
    sk_sp<SkData> collectNonTextureImagesProc(SkImage* img, void* ctx) {
21
0
        SkSharingSerialContext* context = reinterpret_cast<SkSharingSerialContext*>(ctx);
22
0
        uint32_t originalId = img->uniqueID();
23
0
        sk_sp<SkImage>* imageInMap = context->fNonTexMap.find(originalId);
24
0
        if (!imageInMap) {
25
0
            context->fNonTexMap[originalId] = img->makeNonTextureImage();
26
0
        }
27
28
        // This function implements a proc that is more generally for serialization, but
29
        // we really only want to build our map. The output of this function is ignored.
30
0
        return SkData::MakeEmpty();
31
0
    }
32
}
33
34
void SkSharingSerialContext::collectNonTextureImagesFromPicture(
35
0
    const SkPicture* pic, SkSharingSerialContext* sharingCtx) {
36
0
    SkSerialProcs tempProc;
37
0
    tempProc.fImageCtx = sharingCtx;
38
0
    tempProc.fImageProc = collectNonTextureImagesProc;
39
0
    SkNullWStream ns;
40
0
    pic->serialize(&ns, &tempProc);
41
0
}
42
43
0
sk_sp<SkData> SkSharingSerialContext::serializeImage(SkImage* img, void* ctx) {
44
0
    SkSharingSerialContext* context = reinterpret_cast<SkSharingSerialContext*>(ctx);
45
0
    uint32_t id = img->uniqueID(); // get this process's id for the image. these are not hashes.
46
    // find out if we have already serialized this, and if so, what its in-file id is.
47
0
    int* fid = context->fImageMap.find(id);
48
0
    if (!fid) {
49
        // When not present, add its id to the map and return its usual serialized form.
50
0
        context->fImageMap[id] = context->fImageMap.count(); // Next in-file id
51
        // encode the image or it's non-texture replacement if one was collected
52
0
        sk_sp<SkImage>* replacementImage = context->fNonTexMap.find(id);
53
0
        if (replacementImage) {
54
0
            img = replacementImage->get();
55
0
        }
56
0
        return SkPngEncoder::Encode(nullptr, img, {});
57
0
    }
58
    // if present, return only the in-file id we registered the first time we serialized it.
59
0
    return SkData::MakeWithCopy(fid, sizeof(*fid));
60
0
}
61
62
sk_sp<SkImage> SkSharingDeserialContext::deserializeImage(
63
0
  const void* data, size_t length, void* ctx) {
64
0
    if (!data || !length || !ctx) {
65
0
        SkDebugf("SkSharingDeserialContext::deserializeImage arguments invalid %p %zu %p.\n",
66
0
            data, length, ctx);
67
        // Return something so the rest of the debugger can proceed.
68
0
        SkBitmap bm;
69
0
        bm.allocPixels(SkImageInfo::MakeN32Premul(1, 1));
70
0
        return bm.asImage();
71
0
    }
72
0
    SkSharingDeserialContext* context = reinterpret_cast<SkSharingDeserialContext*>(ctx);
73
0
    uint32_t fid;
74
    // If the data is an image fid, look up an already deserialized image from our map
75
0
    if (length == sizeof(fid)) {
76
0
        memcpy(&fid, data, sizeof(fid));
77
0
        if (fid >= context->fImages.size()) {
78
0
            SkDebugf("Cannot deserialize using id, We do not have the data for image %u.\n", fid);
79
0
            return nullptr;
80
0
        }
81
0
        return context->fImages[fid];
82
0
    }
83
    // Otherwise, the data is an image, deserialise it, store it in our map at its fid.
84
    // TODO(nifong): make DeserialProcs accept sk_sp<SkData> so we don't have to copy this.
85
0
    sk_sp<SkData> dataView = SkData::MakeWithCopy(data, length);
86
0
    auto codec = SkPngDecoder::Decode(std::move(dataView), nullptr);
87
0
    if (!codec) {
88
0
        SkDebugf("Cannot deserialize image - might not be a PNG.\n");
89
0
        return nullptr;
90
0
    }
91
0
    SkImageInfo info = codec->getInfo().makeAlphaType(kPremul_SkAlphaType);
92
0
    auto [image, result] = codec->getImage(info);
93
0
    if (result != SkCodec::Result::kSuccess) {
94
0
        SkDebugf("Error decoding image %d.\n", result);
95
        // Might have partially decoded.
96
0
    }
97
0
    context->fImages.push_back(image);
98
0
    return image;
99
0
}