1cb93a386Sopenharmony_ci/* 2cb93a386Sopenharmony_ci * Copyright 2016 Google Inc. 3cb93a386Sopenharmony_ci * 4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be 5cb93a386Sopenharmony_ci * found in the LICENSE file. 6cb93a386Sopenharmony_ci */ 7cb93a386Sopenharmony_ci 8cb93a386Sopenharmony_ci#include "tools/UrlDataManager.h" 9cb93a386Sopenharmony_ci 10cb93a386Sopenharmony_ci#include <unordered_map> 11cb93a386Sopenharmony_ci 12cb93a386Sopenharmony_cibool operator==(const SkData& a, const SkData& b) { 13cb93a386Sopenharmony_ci return a.equals(&b); 14cb93a386Sopenharmony_ci} 15cb93a386Sopenharmony_ci 16cb93a386Sopenharmony_ciUrlDataManager::UrlDataManager(SkString rootUrl) : fRootUrl(rootUrl), fDataId(0) {} 17cb93a386Sopenharmony_ci 18cb93a386Sopenharmony_ciSkString UrlDataManager::addData(SkData* data, const char* contentType) { 19cb93a386Sopenharmony_ci UrlData* urlData = fCache.find(*data); 20cb93a386Sopenharmony_ci if (fCache.find(*data)) { 21cb93a386Sopenharmony_ci SkASSERT(data->equals(urlData->fData.get())); 22cb93a386Sopenharmony_ci return urlData->fUrl; 23cb93a386Sopenharmony_ci } 24cb93a386Sopenharmony_ci 25cb93a386Sopenharmony_ci urlData = new UrlData; 26cb93a386Sopenharmony_ci urlData->fData.reset(SkRef(data)); 27cb93a386Sopenharmony_ci urlData->fContentType.set(contentType); 28cb93a386Sopenharmony_ci urlData->fUrl.appendf("%s/%d", fRootUrl.c_str(), fDataId++); 29cb93a386Sopenharmony_ci 30cb93a386Sopenharmony_ci fCache.add(urlData); 31cb93a386Sopenharmony_ci 32cb93a386Sopenharmony_ci SkASSERT(!fUrlLookup.find(urlData->fUrl)); 33cb93a386Sopenharmony_ci fUrlLookup.add(urlData); 34cb93a386Sopenharmony_ci return urlData->fUrl; 35cb93a386Sopenharmony_ci} 36cb93a386Sopenharmony_ci 37cb93a386Sopenharmony_civoid UrlDataManager::reset() { 38cb93a386Sopenharmony_ci fCache.foreach([&](UrlData* urlData) { 39cb93a386Sopenharmony_ci urlData->unref(); 40cb93a386Sopenharmony_ci }); 41cb93a386Sopenharmony_ci fCache.rewind(); 42cb93a386Sopenharmony_ci} 43cb93a386Sopenharmony_ci 44cb93a386Sopenharmony_civoid UrlDataManager::indexImages(const std::vector<sk_sp<SkImage>>& images) { 45cb93a386Sopenharmony_ci SkASSERT(imageMap.size() == 0); // this method meant only for initialization once. 46cb93a386Sopenharmony_ci for (size_t i = 0; i < images.size(); ++i) { 47cb93a386Sopenharmony_ci imageMap.insert({images[i].get(), i}); 48cb93a386Sopenharmony_ci } 49cb93a386Sopenharmony_ci} 50cb93a386Sopenharmony_ci 51cb93a386Sopenharmony_ciint UrlDataManager::lookupImage(const SkImage* im) { 52cb93a386Sopenharmony_ci auto search = imageMap.find(im); 53cb93a386Sopenharmony_ci if (search != imageMap.end()) { 54cb93a386Sopenharmony_ci return search->second; 55cb93a386Sopenharmony_ci } else { 56cb93a386Sopenharmony_ci // -1 signals the pointer to this image wasn't in the original list. 57cb93a386Sopenharmony_ci // Maybe it was synthesized after file load? If so, you shouldn't be looking it up here. 58cb93a386Sopenharmony_ci return -1; 59cb93a386Sopenharmony_ci } 60cb93a386Sopenharmony_ci} 61