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