• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 Google LLC
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 "experimental/graphite/src/UniformCache.h"
9 
10 #include "experimental/graphite/src/ContextUtils.h"
11 #include "src/core/SkOpts.h"
12 
13 namespace skgpu {
14 
operator ()(sk_sp<UniformData> ud) const15 size_t UniformCache::Hash::operator()(sk_sp<UniformData> ud) const {
16     return SkOpts::hash_fn(ud->data(), ud->dataSize(), 0);
17 }
18 
operator ()(sk_sp<UniformData> a,sk_sp<UniformData> b) const19 bool UniformCache::Eq::operator()(sk_sp<UniformData> a, sk_sp<UniformData> b) const {
20     if (a->count() != b->count() ||
21         a->uniforms() != b->uniforms() ||
22         a->dataSize() != b->dataSize()) {
23         return false;
24     }
25 
26     return !memcmp(a->data(), b->data(), a->dataSize()) &&
27            !memcmp(a->offsets(), b->offsets(), a->count()*sizeof(uint32_t));
28 };
29 
UniformCache()30 UniformCache::UniformCache() {
31     // kInvalidUniformID (aka 0) is reserved
32     fUniformDataVector.push_back(nullptr);
33 }
34 
findOrCreate(sk_sp<UniformData> ud)35 sk_sp<UniformData> UniformCache::findOrCreate(sk_sp<UniformData> ud) {
36 
37     auto iter = fUniformDataHash.find(ud);
38     if (iter != fUniformDataHash.end()) {
39         SkASSERT((*iter)->id() != UniformData::kInvalidUniformID);
40         return *iter;
41     }
42 
43     ud->setID(fNextUniqueID++);
44     fUniformDataHash.insert(ud);
45     fUniformDataVector.push_back(ud);
46     SkASSERT(fUniformDataVector[ud->id()] == ud);
47     return ud;
48 }
49 
lookup(uint32_t uniqueID)50 sk_sp<UniformData> UniformCache::lookup(uint32_t uniqueID) {
51     SkASSERT(uniqueID < fUniformDataVector.size());
52     return fUniformDataVector[uniqueID];
53 }
54 
55 } // namespace skgpu
56