joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Google Inc. |
| 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 | #ifndef GrProgramDesc_DEFINED |
| 9 | #define GrProgramDesc_DEFINED |
| 10 | |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 11 | #include "GrColor.h" |
| 12 | #include "GrTypesPriv.h" |
| 13 | #include "SkChecksum.h" |
| 14 | |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 15 | /** This class describes a program to generate. It also serves as a program cache key. Very little |
| 16 | of this is GL-specific. The GL-specific parts could be factored out into a subclass. */ |
| 17 | class GrProgramDesc { |
| 18 | public: |
| 19 | // Creates an uninitialized key that must be populated by GrGpu::buildProgramDesc() |
| 20 | GrProgramDesc() {} |
| 21 | |
| 22 | // Returns this as a uint32_t array to be used as a key in the program cache. |
| 23 | const uint32_t* asKey() const { |
| 24 | return reinterpret_cast<const uint32_t*>(fKey.begin()); |
| 25 | } |
| 26 | |
| 27 | // Gets the number of bytes in asKey(). It will be a 4-byte aligned value. When comparing two |
| 28 | // keys the size of either key can be used with memcmp() since the lengths themselves begin the |
| 29 | // keys and thus the memcmp will exit early if the keys are of different lengths. |
| 30 | uint32_t keyLength() const { return *this->atOffset<uint32_t, kLengthOffset>(); } |
| 31 | |
| 32 | // Gets the a checksum of the key. Can be used as a hash value for a fast lookup in a cache. |
| 33 | uint32_t getChecksum() const { return *this->atOffset<uint32_t, kChecksumOffset>(); } |
| 34 | |
| 35 | GrProgramDesc& operator= (const GrProgramDesc& other) { |
bsalomon | ccb328d | 2014-12-11 13:31:06 -0800 | [diff] [blame] | 36 | uint32_t keyLength = other.keyLength(); |
bsalomon | ef3fcd8 | 2014-12-12 08:51:38 -0800 | [diff] [blame] | 37 | fKey.reset(SkToInt(keyLength)); |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 38 | memcpy(fKey.begin(), other.fKey.begin(), keyLength); |
| 39 | return *this; |
| 40 | } |
| 41 | |
| 42 | bool operator== (const GrProgramDesc& other) const { |
| 43 | // The length is masked as a hint to the compiler that the address will be 4 byte aligned. |
| 44 | return 0 == memcmp(this->asKey(), other.asKey(), this->keyLength() & ~0x3); |
| 45 | } |
| 46 | |
| 47 | bool operator!= (const GrProgramDesc& other) const { |
| 48 | return !(*this == other); |
| 49 | } |
| 50 | |
| 51 | static bool Less(const GrProgramDesc& a, const GrProgramDesc& b) { |
| 52 | return memcmp(a.asKey(), b.asKey(), a.keyLength() & ~0x3) < 0; |
| 53 | } |
| 54 | |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 55 | struct KeyHeader { |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 56 | uint8_t fFragPosKey; // set by GrGLShaderBuilder if there are |
| 57 | // effects that read the fragment position. |
| 58 | // Otherwise, 0. |
| 59 | |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 60 | int8_t fColorEffectCnt; |
| 61 | int8_t fCoverageEffectCnt; |
| 62 | }; |
| 63 | |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 64 | int numColorEffects() const { |
| 65 | return this->header().fColorEffectCnt; |
| 66 | } |
| 67 | |
| 68 | int numCoverageEffects() const { |
| 69 | return this->header().fCoverageEffectCnt; |
| 70 | } |
| 71 | |
| 72 | int numTotalEffects() const { return this->numColorEffects() + this->numCoverageEffects(); } |
| 73 | |
| 74 | // This should really only be used internally, base classes should return their own headers |
| 75 | const KeyHeader& header() const { return *this->atOffset<KeyHeader, kHeaderOffset>(); } |
| 76 | |
jvanverth | d1e7287 | 2015-04-20 12:29:37 -0700 | [diff] [blame^] | 77 | protected: |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 78 | template<typename T, size_t OFFSET> T* atOffset() { |
| 79 | return reinterpret_cast<T*>(reinterpret_cast<intptr_t>(fKey.begin()) + OFFSET); |
| 80 | } |
| 81 | |
| 82 | template<typename T, size_t OFFSET> const T* atOffset() const { |
| 83 | return reinterpret_cast<const T*>(reinterpret_cast<intptr_t>(fKey.begin()) + OFFSET); |
| 84 | } |
| 85 | |
| 86 | void finalize() { |
| 87 | int keyLength = fKey.count(); |
| 88 | SkASSERT(0 == (keyLength % 4)); |
| 89 | *(this->atOffset<uint32_t, GrProgramDesc::kLengthOffset>()) = SkToU32(keyLength); |
| 90 | |
| 91 | uint32_t* checksum = this->atOffset<uint32_t, GrProgramDesc::kChecksumOffset>(); |
| 92 | *checksum = 0; |
| 93 | *checksum = SkChecksum::Compute(reinterpret_cast<uint32_t*>(fKey.begin()), keyLength); |
| 94 | } |
| 95 | |
| 96 | // The key, stored in fKey, is composed of four parts: |
| 97 | // 1. uint32_t for total key length. |
| 98 | // 2. uint32_t for a checksum. |
| 99 | // 3. Header struct defined above. Also room for extensions to the header |
| 100 | // 4. A Backend specific payload. Room is preallocated for this |
| 101 | enum KeyOffsets { |
| 102 | // Part 1. |
| 103 | kLengthOffset = 0, |
| 104 | // Part 2. |
| 105 | kChecksumOffset = kLengthOffset + sizeof(uint32_t), |
| 106 | // Part 3. |
| 107 | kHeaderOffset = kChecksumOffset + sizeof(uint32_t), |
| 108 | kHeaderSize = SkAlign4(2 * sizeof(KeyHeader)), |
| 109 | }; |
| 110 | |
| 111 | enum { |
| 112 | kMaxPreallocProcessors = 8, |
| 113 | kIntsPerProcessor = 4, // This is an overestimate of the average effect key size. |
| 114 | kPreAllocSize = kHeaderOffset + kHeaderSize + |
| 115 | kMaxPreallocProcessors * sizeof(uint32_t) * kIntsPerProcessor, |
| 116 | }; |
| 117 | |
jvanverth | d1e7287 | 2015-04-20 12:29:37 -0700 | [diff] [blame^] | 118 | SkSTArray<kPreAllocSize, uint8_t, true>& key() { return fKey; } |
| 119 | const SkSTArray<kPreAllocSize, uint8_t, true>& key() const { return fKey; } |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 120 | |
jvanverth | d1e7287 | 2015-04-20 12:29:37 -0700 | [diff] [blame^] | 121 | private: |
| 122 | SkSTArray<kPreAllocSize, uint8_t, true> fKey; |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | #endif |