• 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 #include "tools/UrlDataManager.h"
9 
10 #include <unordered_map>
11 
operator ==(const SkData & a,const SkData & b)12 bool operator==(const SkData& a, const SkData& b) {
13     return a.equals(&b);
14 }
15 
UrlDataManager(SkString rootUrl)16 UrlDataManager::UrlDataManager(SkString rootUrl) : fRootUrl(rootUrl), fDataId(0) {}
17 
addData(SkData * data,const char * contentType)18 SkString UrlDataManager::addData(SkData* data, const char* contentType) {
19     UrlData* urlData = fCache.find(*data);
20     if (fCache.find(*data)) {
21         SkASSERT(data->equals(urlData->fData.get()));
22         return urlData->fUrl;
23     }
24 
25     urlData = new UrlData;
26     urlData->fData.reset(SkRef(data));
27     urlData->fContentType.set(contentType);
28     urlData->fUrl.appendf("%s/%d", fRootUrl.c_str(), fDataId++);
29 
30     fCache.add(urlData);
31 
32     SkASSERT(!fUrlLookup.find(urlData->fUrl));
33     fUrlLookup.add(urlData);
34     return urlData->fUrl;
35 }
36 
reset()37 void UrlDataManager::reset() {
38     SkTDynamicHash<UrlData, SkData, LookupTrait>::Iter iter(&fCache);
39     while (!iter.done()) {
40         UrlData* urlData = &(*iter);
41         urlData->unref();
42         ++iter;
43     }
44 
45     fCache.rewind();
46 }
47 
indexImages(const std::vector<sk_sp<SkImage>> & images)48 void UrlDataManager::indexImages(const std::vector<sk_sp<SkImage>>& images) {
49   SkASSERT(imageMap.size() == 0); // this method meant only for initialization once.
50   for (size_t i = 0; i < images.size(); ++i) {
51     imageMap.insert({images[i].get(), i});
52   }
53 }
54 
lookupImage(const SkImage * im)55 int UrlDataManager::lookupImage(const SkImage* im) {
56   auto search = imageMap.find(im);
57   if (search != imageMap.end()) {
58     return search->second;
59   } else {
60       // -1 signals the pointer to this image wasn't in the original list.
61       // Maybe it was synthesized after file load? If so, you shouldn't be looking it up here.
62       return -1;
63   }
64 }
65