blob: 3245859dfcacf116b2506a5c3145a987eb27633d [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "tools/UrlDataManager.h"
joshualittcdad12f2016-02-08 07:08:21 -08009
Nathaniel Nifonga14d8092020-01-03 10:29:13 -050010#include <unordered_map>
11
joshualittcdad12f2016-02-08 07:08:21 -080012bool operator==(const SkData& a, const SkData& b) {
13 return a.equals(&b);
14}
15
16UrlDataManager::UrlDataManager(SkString rootUrl) : fRootUrl(rootUrl), fDataId(0) {}
17
18SkString 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
37void UrlDataManager::reset() {
Mike Kleincff63962020-03-14 16:22:45 -050038 fCache.foreach([&](UrlData* urlData) {
joshualittcdad12f2016-02-08 07:08:21 -080039 urlData->unref();
Mike Kleincff63962020-03-14 16:22:45 -050040 });
joshualittcdad12f2016-02-08 07:08:21 -080041 fCache.rewind();
42}
Nathaniel Nifonga14d8092020-01-03 10:29:13 -050043
44void UrlDataManager::indexImages(const std::vector<sk_sp<SkImage>>& images) {
45 SkASSERT(imageMap.size() == 0); // this method meant only for initialization once.
46 for (size_t i = 0; i < images.size(); ++i) {
47 imageMap.insert({images[i].get(), i});
48 }
49}
50
51int UrlDataManager::lookupImage(const SkImage* im) {
52 auto search = imageMap.find(im);
53 if (search != imageMap.end()) {
54 return search->second;
55 } else {
56 // -1 signals the pointer to this image wasn't in the original list.
57 // Maybe it was synthesized after file load? If so, you shouldn't be looking it up here.
58 return -1;
59 }
60}