• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/SkOpts.h"
15 #include "src/core/SkTDynamicHash.h"
16 
17 #include <unordered_map>
18 
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);
~UrlDataManager()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;
39         sk_sp<SkData> fData;
40     };
41 
42     /*
43      * returns the UrlData object which should be hosted at 'url'
44      */
getDataFromUrl(SkString url)45     UrlData* getDataFromUrl(SkString url) {
46         return fUrlLookup.find(url);
47     }
48     void reset();
49 
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      */
hasImageIndex()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 
71 private:
72     struct LookupTrait {
73         // We use the data as a hash, this is not really optimal but is fine until proven otherwise
GetKeyLookupTrait74         static const SkData& GetKey(const UrlData& data) {
75             return *data.fData;
76         }
77 
HashLookupTrait78         static uint32_t Hash(const SkData& key) {
79             return SkOpts::hash(key.bytes(), key.size());
80         }
81     };
82 
83     struct ReverseLookupTrait {
GetKeyReverseLookupTrait84         static const SkString& GetKey(const UrlData& data) {
85             return data.fUrl;
86         }
87 
HashReverseLookupTrait88         static uint32_t Hash(const SkString& key) {
89             return SkOpts::hash(key.c_str(), strlen(key.c_str()));
90         }
91     };
92 
93 
94     SkString fRootUrl;
95     SkTDynamicHash<UrlData, SkData, LookupTrait> fCache;
96     SkTDynamicHash<UrlData, SkString, ReverseLookupTrait> fUrlLookup;
97     uint32_t fDataId;
98     std::unordered_map<const SkImage*, int> imageMap;
99 };
100 
101 #endif
102