Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2014 Google Inc. | 3 * Copyright 2014 Google Inc. |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 #ifndef GrResourceKey_DEFINED | 9 #ifndef GrResourceKey_DEFINED |
| 10 #define GrResourceKey_DEFINED | 10 #define GrResourceKey_DEFINED |
| 11 | 11 |
| 12 #include "GrTypes.h" | 12 #include "GrTypes.h" |
| 13 #include "SkTemplates.h" | |
| 13 #include "GrBinHashKey.h" | 14 #include "GrBinHashKey.h" |
| 14 | 15 |
| 16 /** | |
| 17 * A key used for scratch resources. The key consists of a resource type (subcla ss) identifier, a | |
| 18 * hash, a data length, and type-specific data. A Builder object is used to init ialize the | |
| 19 * key contents. The contents must be initialized before the key can be used. | |
| 20 */ | |
| 21 class GrScratchKey { | |
| 22 public: | |
| 23 /** Uniquely identifies the type of resource that is cached as scratch. */ | |
| 24 typedef uint32_t ResourceType; | |
| 25 /** Generate a unique ResourceType. */ | |
| 26 static ResourceType GenerateResourceType(); | |
| 27 | |
| 28 GrScratchKey() { this->reset(); } | |
| 29 GrScratchKey(const GrScratchKey& that) { *this = that; } | |
| 30 | |
| 31 /** Reset to an invalid key. */ | |
| 32 void reset() { | |
| 33 fKey.reset(kExtraDataCnt); | |
| 34 fKey[kHashIdx] = 0; | |
| 35 fKey[kTypeAndSizeIdx] = kInvalidResourceType; | |
| 36 } | |
| 37 | |
| 38 bool isValid() const { return kInvalidResourceType != this->resourceType(); } | |
| 39 | |
| 40 ResourceType resourceType() const { return fKey[kTypeAndSizeIdx] & 0xffff; } | |
| 41 | |
| 42 uint32_t hash() const { return fKey[kHashIdx]; } | |
| 43 | |
| 44 size_t size() const { return SkToInt(fKey[kTypeAndSizeIdx] >> 16); } | |
| 45 | |
| 46 const uint32_t* data() const { return &fKey[kExtraDataCnt]; } | |
| 47 | |
| 48 GrScratchKey& operator=(const GrScratchKey& that) { | |
| 49 size_t size = that.size(); | |
| 50 fKey.reset(SkToInt(size)); | |
| 51 memcpy(fKey.get(), that.fKey.get(), size); | |
| 52 return *this; | |
| 53 } | |
| 54 | |
| 55 bool operator==(const GrScratchKey& that) const { | |
| 56 return 0 == memcmp(fKey.get(), that.fKey.get(), this->size()); | |
| 57 } | |
| 58 bool operator!=(const GrScratchKey& that) const { return !(*this == that); } | |
| 59 | |
| 60 /** Used to initialize scratch key. */ | |
| 61 class Builder { | |
| 62 public: | |
| 63 Builder(GrScratchKey* key, ResourceType type, int data32Count) : fKey(ke y) { | |
| 64 SkASSERT(data32Count >= 0); | |
| 65 SkASSERT(type != kInvalidResourceType); | |
| 66 key->fKey.reset(kExtraDataCnt + data32Count); | |
| 67 SkASSERT(type <= SK_MaxU16); | |
| 68 int size = (data32Count + kExtraDataCnt) * sizeof(uint32_t); | |
| 69 SkASSERT(size <= SK_MaxU16); | |
| 70 key->fKey[kTypeAndSizeIdx] = type | (size << 16); | |
| 71 } | |
| 72 | |
| 73 ~Builder() { this->finish(); } | |
| 74 | |
| 75 void finish(); | |
| 76 | |
| 77 uint32_t& operator[](int dataIdx) { | |
| 78 SkASSERT(fKey); | |
| 79 SkDEBUGCODE(size_t dataCount = fKey->size() / sizeof(uint32_t) - kEx traDataCnt;) | |
| 80 SkASSERT(SkToU32(dataIdx) < dataCount); | |
| 81 return fKey->fKey[kExtraDataCnt + dataIdx]; | |
| 82 } | |
| 83 | |
| 84 private: | |
| 85 GrScratchKey* fKey; | |
| 86 }; | |
| 87 | |
| 88 private: | |
|
robertphillips
2014/12/30 18:19:52
This enum is a bit mixed. Would it make more sense
bsalomon
2014/12/30 18:37:09
Done.
| |
| 89 enum { | |
| 90 kInvalidResourceType = 1, | |
| 91 kExtraDataCnt = 2, | |
| 92 kHashIdx = 0, | |
|
robertphillips
2014/12/30 18:19:52
// type and size are folded together into one 32-b
bsalomon
2014/12/30 18:37:09
Done.
| |
| 93 kTypeAndSizeIdx = 1, | |
| 94 }; | |
|
robertphillips
2014/12/30 18:19:52
// 2 b.c. the stencil and texture resource both re
bsalomon
2014/12/30 18:37:09
Done.
| |
| 95 SkAutoSTArray<kExtraDataCnt + 2, uint32_t> fKey; | |
| 96 }; | |
| 97 | |
| 15 class GrResourceKey { | 98 class GrResourceKey { |
| 16 public: | 99 public: |
| 17 static GrCacheID::Domain ScratchDomain(); | |
| 18 | |
| 19 /** Uniquely identifies the GrGpuResource subclass in the key to avoid colli sions | |
| 20 across resource types. */ | |
| 21 typedef uint8_t ResourceType; | |
| 22 | |
| 23 /** Flags set by the GrGpuResource subclass. */ | 100 /** Flags set by the GrGpuResource subclass. */ |
| 24 typedef uint8_t ResourceFlags; | 101 typedef uint8_t ResourceFlags; |
| 25 | 102 |
| 26 /** Generate a unique ResourceType */ | |
| 27 static ResourceType GenerateResourceType(); | |
| 28 | |
| 29 /** Creates a key for resource */ | 103 /** Creates a key for resource */ |
| 30 GrResourceKey(const GrCacheID& id, ResourceType type, ResourceFlags flags) { | 104 GrResourceKey(const GrCacheID& id, ResourceFlags flags) { |
| 31 this->init(id.getDomain(), id.getKey(), type, flags); | 105 this->init(id.getDomain(), id.getKey(), flags); |
| 32 }; | 106 }; |
| 33 | 107 |
| 34 GrResourceKey(const GrResourceKey& src) { fKey = src.fKey; } | 108 GrResourceKey(const GrResourceKey& src) { fKey = src.fKey; } |
| 35 | 109 |
| 36 GrResourceKey() { fKey.reset(); } | 110 GrResourceKey() { fKey.reset(); } |
| 37 | 111 |
| 38 void reset(const GrCacheID& id, ResourceType type, ResourceFlags flags) { | 112 void reset(const GrCacheID& id, ResourceFlags flags) { |
| 39 this->init(id.getDomain(), id.getKey(), type, flags); | 113 this->init(id.getDomain(), id.getKey(), flags); |
| 40 } | 114 } |
| 41 | 115 |
| 42 uint32_t getHash() const { return fKey.getHash(); } | 116 uint32_t getHash() const { return fKey.getHash(); } |
| 43 | 117 |
| 44 bool isScratch() const { | |
| 45 return ScratchDomain() == | |
| 46 *reinterpret_cast<const GrCacheID::Domain*>(fKey.getData() + | |
| 47 kCacheIDDomainOffset); | |
| 48 } | |
| 49 | |
| 50 ResourceType getResourceType() const { | |
| 51 return *reinterpret_cast<const ResourceType*>(fKey.getData() + | |
| 52 kResourceTypeOffset); | |
| 53 } | |
| 54 | |
| 55 ResourceFlags getResourceFlags() const { | 118 ResourceFlags getResourceFlags() const { |
| 56 return *reinterpret_cast<const ResourceFlags*>(fKey.getData() + | 119 return *reinterpret_cast<const ResourceFlags*>(fKey.getData() + |
| 57 kResourceFlagsOffset); | 120 kResourceFlagsOffset); |
| 58 } | 121 } |
| 59 | 122 |
| 60 bool operator==(const GrResourceKey& other) const { return fKey == other.fKe y; } | 123 bool operator==(const GrResourceKey& other) const { return fKey == other.fKe y; } |
| 61 | 124 |
| 62 // A key indicating that the resource is not usable as a scratch resource. | 125 // A key indicating that the resource is not usable as a scratch resource. |
| 63 static GrResourceKey& NullScratchKey(); | 126 static GrResourceKey& NullScratchKey(); |
| 64 | 127 |
| 65 bool isNullScratch() const { | |
| 66 return this->isScratch() && NoneResourceType() == this->getResourceType( ); | |
| 67 } | |
| 68 | |
| 69 private: | 128 private: |
| 70 enum { | 129 enum { |
| 71 kCacheIDKeyOffset = 0, | 130 kCacheIDKeyOffset = 0, |
| 72 kCacheIDDomainOffset = kCacheIDKeyOffset + sizeof(GrCacheID::Key), | 131 kCacheIDDomainOffset = kCacheIDKeyOffset + sizeof(GrCacheID::Key), |
| 73 kResourceTypeOffset = kCacheIDDomainOffset + sizeof(GrCacheID::Domain), | 132 kResourceFlagsOffset = kCacheIDDomainOffset + sizeof(GrCacheID::Domain), |
| 74 kResourceFlagsOffset = kResourceTypeOffset + sizeof(ResourceType), | |
| 75 kPadOffset = kResourceFlagsOffset + sizeof(ResourceFlags), | 133 kPadOffset = kResourceFlagsOffset + sizeof(ResourceFlags), |
| 76 kKeySize = SkAlign4(kPadOffset), | 134 kKeySize = SkAlign4(kPadOffset), |
| 77 kPadSize = kKeySize - kPadOffset | 135 kPadSize = kKeySize - kPadOffset |
| 78 }; | 136 }; |
| 79 | 137 |
| 80 static ResourceType NoneResourceType(); | 138 void init(const GrCacheID::Domain domain, const GrCacheID::Key& key, Resourc eFlags flags) { |
| 81 | |
| 82 void init(const GrCacheID::Domain domain, | |
| 83 const GrCacheID::Key& key, | |
| 84 ResourceType type, | |
| 85 ResourceFlags flags) { | |
| 86 union { | 139 union { |
| 87 uint8_t fKey8[kKeySize]; | 140 uint8_t fKey8[kKeySize]; |
| 88 uint32_t fKey32[kKeySize / 4]; | 141 uint32_t fKey32[kKeySize / 4]; |
| 89 } keyData; | 142 } keyData; |
| 90 | 143 |
| 91 uint8_t* k = keyData.fKey8; | 144 uint8_t* k = keyData.fKey8; |
| 92 memcpy(k + kCacheIDKeyOffset, key.fData8, sizeof(GrCacheID::Key)); | 145 memcpy(k + kCacheIDKeyOffset, key.fData8, sizeof(GrCacheID::Key)); |
| 93 memcpy(k + kCacheIDDomainOffset, &domain, sizeof(GrCacheID::Domain)); | 146 memcpy(k + kCacheIDDomainOffset, &domain, sizeof(GrCacheID::Domain)); |
| 94 memcpy(k + kResourceTypeOffset, &type, sizeof(ResourceType)); | |
| 95 memcpy(k + kResourceFlagsOffset, &flags, sizeof(ResourceFlags)); | 147 memcpy(k + kResourceFlagsOffset, &flags, sizeof(ResourceFlags)); |
| 96 memset(k + kPadOffset, 0, kPadSize); | 148 memset(k + kPadOffset, 0, kPadSize); |
| 97 fKey.setKeyData(keyData.fKey32); | 149 fKey.setKeyData(keyData.fKey32); |
| 98 } | 150 } |
| 99 GrBinHashKey<kKeySize> fKey; | 151 GrBinHashKey<kKeySize> fKey; |
| 100 }; | 152 }; |
| 101 | 153 |
| 102 #endif | 154 #endif |
| OLD | NEW |