xref: /aosp_15_r20/external/skia/src/gpu/ResourceKey.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 "src/gpu/ResourceKey.h"
9 
10 #include "src/core/SkChecksum.h"
11 
12 #include <atomic>
13 
14 namespace skgpu {
15 
GenerateResourceType()16 ScratchKey::ResourceType ScratchKey::GenerateResourceType() {
17     static std::atomic<int32_t> nextType{ResourceKey::kInvalidDomain + 1};
18 
19     int32_t type = nextType.fetch_add(1, std::memory_order_relaxed);
20     if (type > SkTo<int32_t>(UINT16_MAX)) {
21         SK_ABORT("Too many Resource Types");
22     }
23 
24     return static_cast<ResourceType>(type);
25 }
26 
GenerateDomain()27 UniqueKey::Domain UniqueKey::GenerateDomain() {
28     static std::atomic<int32_t> nextDomain{ResourceKey::kInvalidDomain + 1};
29 
30     int32_t domain = nextDomain.fetch_add(1, std::memory_order_relaxed);
31     if (domain > SkTo<int32_t>(UINT16_MAX)) {
32         SK_ABORT("Too many skgpu::UniqueKey Domains");
33     }
34 
35     return static_cast<Domain>(domain);
36 }
37 
ResourceKeyHash(const uint32_t * data,size_t size)38 uint32_t ResourceKeyHash(const uint32_t* data, size_t size) {
39     return SkChecksum::Hash32(data, size);
40 }
41 
42 } // namespace skgpu
43 
44