Coverage Report

Created: 2024-05-20 07:14

/src/skia/tools/UrlDataManager.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2016 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
#ifndef SkUrlDataManager_DEFINED
9
#define SkUrlDataManager_DEFINED
10
11
#include "include/core/SkData.h"
12
#include "include/core/SkImage.h"
13
#include "include/core/SkString.h"
14
#include "src/core/SkChecksum.h"
15
#include "src/core/SkTDynamicHash.h"
16
17
#include <unordered_map>
18
#include <vector>
19
20
/*
21
 * A simple class which allows clients to add opaque data types, and returns a url where this data
22
 * will be hosted.  Its up to the owner of this class to actually serve the data.
23
 */
24
bool operator==(const SkData& a, const SkData& b);
25
26
class UrlDataManager {
27
public:
28
    UrlDataManager(SkString rootUrl);
29
0
    ~UrlDataManager() { this->reset(); }
30
31
    /*
32
     * Adds a data blob to the cache with a particular content type.  UrlDataManager will hash
33
     * the blob data to ensure uniqueness
34
     */
35
    SkString addData(SkData*, const char* contentType);
36
37
    struct UrlData : public SkRefCnt {
38
        SkString fUrl;
39
        SkString fContentType;
40
        sk_sp<SkData> fData;
41
    };
42
43
    /*
44
     * returns the UrlData object which should be hosted at 'url'
45
     */
46
0
    UrlData* getDataFromUrl(const SkString& url) {
47
0
        return fUrlLookup.find(url);
48
0
    }
49
    void reset();
50
51
    // Methods used to identify images differently in wasm debugger for mskp animations.
52
    // serving is uncessary, as a collection of images with identifiers is already present, we
53
    // just want to use it when serializing commands.
54
55
    /*
56
     * Construct an index from a list of images
57
     * (expected to be the list that was loaded from the mskp file)
58
     * Use only once.
59
     */
60
    void indexImages(const std::vector<sk_sp<SkImage>>&);
61
62
    /*
63
     * Reports whether this UDM has an initialized image index (effevitely whether we're in wasm)
64
     */
65
0
    bool hasImageIndex() { return imageMap.size() > 0; }
66
67
    /*
68
     * Return the file id (index of the image in the originally provided list) of an SkImage
69
     */
70
    int lookupImage(const SkImage*);
71
72
private:
73
    struct LookupTrait {
74
        // We use the data as a hash, this is not really optimal but is fine until proven otherwise
75
0
        static const SkData& GetKey(const UrlData& data) {
76
0
            return *data.fData;
77
0
        }
78
79
0
        static uint32_t Hash(const SkData& key) {
80
0
            return SkChecksum::Hash32(key.bytes(), key.size());
81
0
        }
82
    };
83
84
    struct ReverseLookupTrait {
85
0
        static const SkString& GetKey(const UrlData& data) {
86
0
            return data.fUrl;
87
0
        }
88
89
0
        static uint32_t Hash(const SkString& key) {
90
0
            return SkChecksum::Hash32(key.c_str(), strlen(key.c_str()));
91
0
        }
92
    };
93
94
95
    SkString fRootUrl;
96
    SkTDynamicHash<UrlData, SkData, LookupTrait> fCache;
97
    SkTDynamicHash<UrlData, SkString, ReverseLookupTrait> fUrlLookup;
98
    uint32_t fDataId;
99
    std::unordered_map<const SkImage*, int> imageMap;
100
};
101
102
#endif