blob: 11c77452ae21fc74690e081f9910361e5aedf1fe [file] [log] [blame]
joshualittcdad12f2016-02-08 07:08:21 -08001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkData.h"
Nathaniel Nifonga14d8092020-01-03 10:29:13 -050012#include "include/core/SkImage.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkString.h"
14#include "src/core/SkOpts.h"
15#include "src/core/SkTDynamicHash.h"
joshualittcdad12f2016-02-08 07:08:21 -080016
Nathaniel Nifonga14d8092020-01-03 10:29:13 -050017#include <unordered_map>
18
joshualittcdad12f2016-02-08 07:08:21 -080019/*
20 * A simple class which allows clients to add opaque data types, and returns a url where this data
21 * will be hosted. Its up to the owner of this class to actually serve the data.
22 */
23bool operator==(const SkData& a, const SkData& b);
24
25class UrlDataManager {
26public:
27 UrlDataManager(SkString rootUrl);
28 ~UrlDataManager() { this->reset(); }
29
30 /*
31 * Adds a data blob to the cache with a particular content type. UrlDataManager will hash
32 * the blob data to ensure uniqueness
33 */
34 SkString addData(SkData*, const char* contentType);
35
36 struct UrlData : public SkRefCnt {
37 SkString fUrl;
38 SkString fContentType;
bungemanffae30d2016-08-03 13:32:32 -070039 sk_sp<SkData> fData;
joshualittcdad12f2016-02-08 07:08:21 -080040 };
41
42 /*
43 * returns the UrlData object which should be hosted at 'url'
44 */
45 UrlData* getDataFromUrl(SkString url) {
46 return fUrlLookup.find(url);
47 }
48 void reset();
49
Nathaniel Nifonga14d8092020-01-03 10:29:13 -050050 // Methods used to identify images differently in wasm debugger for mskp animations.
51 // serving is uncessary, as a collection of images with identifiers is already present, we
52 // just want to use it when serializing commands.
53
54 /*
55 * Construct an index from a list of images
56 * (expected to be the list that was loaded from the mskp file)
57 * Use only once.
58 */
59 void indexImages(const std::vector<sk_sp<SkImage>>&);
60
61 /*
62 * Reports whether this UDM has an initialized image index (effevitely whether we're in wasm)
63 */
64 bool hasImageIndex() { return imageMap.size() > 0; }
65
66 /*
67 * Return the file id (index of the image in the originally provided list) of an SkImage
68 */
69 int lookupImage(const SkImage*);
70
joshualittcdad12f2016-02-08 07:08:21 -080071private:
72 struct LookupTrait {
73 // We use the data as a hash, this is not really optimal but is fine until proven otherwise
74 static const SkData& GetKey(const UrlData& data) {
John Stilesa008b0f2020-08-16 08:48:02 -040075 return *data.fData;
joshualittcdad12f2016-02-08 07:08:21 -080076 }
77
78 static uint32_t Hash(const SkData& key) {
mtklein4e976072016-08-08 09:06:27 -070079 return SkOpts::hash(key.bytes(), key.size());
joshualittcdad12f2016-02-08 07:08:21 -080080 }
81 };
82
83 struct ReverseLookupTrait {
84 static const SkString& GetKey(const UrlData& data) {
85 return data.fUrl;
86 }
87
88 static uint32_t Hash(const SkString& key) {
mtklein4e976072016-08-08 09:06:27 -070089 return SkOpts::hash(key.c_str(), strlen(key.c_str()));
joshualittcdad12f2016-02-08 07:08:21 -080090 }
91 };
92
93
94 SkString fRootUrl;
95 SkTDynamicHash<UrlData, SkData, LookupTrait> fCache;
96 SkTDynamicHash<UrlData, SkString, ReverseLookupTrait> fUrlLookup;
97 uint32_t fDataId;
Nathaniel Nifonga14d8092020-01-03 10:29:13 -050098 std::unordered_map<const SkImage*, int> imageMap;
joshualittcdad12f2016-02-08 07:08:21 -080099};
100
101#endif