• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 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 "src/core/SkUniformData.h"
9 
10 #include "src/core/SkOpts.h"
11 
Make(SkSpan<const SkUniform> uniforms,size_t dataSize)12 sk_sp<SkUniformData> SkUniformData::Make(SkSpan<const SkUniform> uniforms, size_t dataSize) {
13     // TODO: the offsets and data should just be allocated right after UniformData in an arena
14     uint32_t* offsets = new uint32_t[uniforms.size()];
15     char* data = new char[dataSize];
16 
17     return sk_sp<SkUniformData>(new SkUniformData(uniforms, offsets, data, dataSize));
18 }
19 
operator ==(const SkUniformData & other) const20 bool SkUniformData::operator==(const SkUniformData& other) const {
21     if (this->uniforms().size() != other.uniforms().size() ||
22         this->dataSize() != other.dataSize()) {
23         return false;
24     }
25 
26     return !memcmp(this->uniforms().data(), other.uniforms().data(),
27                    this->uniforms().size_bytes()) &&
28            !memcmp(this->data(), other.data(), this->dataSize()) &&
29            !memcmp(this->offsets(), other.offsets(), this->count()*sizeof(uint32_t));
30 }
31 
32 ////////////////////////////////////////////////////////////////////////////////////////////////////
add(sk_sp<SkUniformData> uniforms)33 void SkUniformBlock::add(sk_sp<SkUniformData> uniforms) {
34     fUniformData.push_back(std::move(uniforms));
35 }
36 
totalSize() const37 size_t SkUniformBlock::totalSize() const {
38     size_t total = 0;
39 
40     // TODO: It seems like we need to worry about alignment between the separate sets of uniforms
41     for (auto& u : fUniformData) {
42         total += u->dataSize();
43     }
44 
45     return total;
46 }
47 
count() const48 int SkUniformBlock::count() const {
49     int total = 0;
50 
51     for (auto& u : fUniformData) {
52         total += u->count();
53     }
54 
55     return total;
56 }
57 
operator ==(const SkUniformBlock & other) const58 bool SkUniformBlock::operator==(const SkUniformBlock& other) const {
59     if (fUniformData.size() != other.fUniformData.size()) {
60         return false;
61     }
62 
63     for (size_t i = 0; i < fUniformData.size(); ++i) {
64         if (*fUniformData[i] != *other.fUniformData[i]) {
65             return false;
66         }
67     }
68 
69     return true;
70 }
71 
hash() const72 size_t SkUniformBlock::hash() const {
73     int32_t hash = 0;
74 
75     for (auto& u : fUniformData) {
76         hash = SkOpts::hash_fn(u->data(), u->dataSize(), hash);
77     }
78 
79     return hash;
80 }
81