joshualitt | cdad12f | 2016-02-08 07:08:21 -0800 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkData.h" |
Nathaniel Nifong | a14d809 | 2020-01-03 10:29:13 -0500 | [diff] [blame] | 12 | #include "include/core/SkImage.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/core/SkString.h" |
| 14 | #include "src/core/SkOpts.h" |
| 15 | #include "src/core/SkTDynamicHash.h" |
joshualitt | cdad12f | 2016-02-08 07:08:21 -0800 | [diff] [blame] | 16 | |
Nathaniel Nifong | a14d809 | 2020-01-03 10:29:13 -0500 | [diff] [blame] | 17 | #include <unordered_map> |
| 18 | |
joshualitt | cdad12f | 2016-02-08 07:08:21 -0800 | [diff] [blame] | 19 | /* |
| 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 | */ |
| 23 | bool operator==(const SkData& a, const SkData& b); |
| 24 | |
| 25 | class UrlDataManager { |
| 26 | public: |
| 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; |
bungeman | ffae30d | 2016-08-03 13:32:32 -0700 | [diff] [blame] | 39 | sk_sp<SkData> fData; |
joshualitt | cdad12f | 2016-02-08 07:08:21 -0800 | [diff] [blame] | 40 | }; |
| 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 Nifong | a14d809 | 2020-01-03 10:29:13 -0500 | [diff] [blame] | 50 | // 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 | |
joshualitt | cdad12f | 2016-02-08 07:08:21 -0800 | [diff] [blame] | 71 | private: |
| 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) { |
| 75 | return *data.fData.get(); |
| 76 | } |
| 77 | |
| 78 | static uint32_t Hash(const SkData& key) { |
mtklein | 4e97607 | 2016-08-08 09:06:27 -0700 | [diff] [blame] | 79 | return SkOpts::hash(key.bytes(), key.size()); |
joshualitt | cdad12f | 2016-02-08 07:08:21 -0800 | [diff] [blame] | 80 | } |
| 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) { |
mtklein | 4e97607 | 2016-08-08 09:06:27 -0700 | [diff] [blame] | 89 | return SkOpts::hash(key.c_str(), strlen(key.c_str())); |
joshualitt | cdad12f | 2016-02-08 07:08:21 -0800 | [diff] [blame] | 90 | } |
| 91 | }; |
| 92 | |
| 93 | |
| 94 | SkString fRootUrl; |
| 95 | SkTDynamicHash<UrlData, SkData, LookupTrait> fCache; |
| 96 | SkTDynamicHash<UrlData, SkString, ReverseLookupTrait> fUrlLookup; |
| 97 | uint32_t fDataId; |
Nathaniel Nifong | a14d809 | 2020-01-03 10:29:13 -0500 | [diff] [blame] | 98 | std::unordered_map<const SkImage*, int> imageMap; |
joshualitt | cdad12f | 2016-02-08 07:08:21 -0800 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | #endif |