Fix some prevent_unsafe_narrowing warnings

These changes reduce failing targets for skia_unittest when adding
"//build/config/compiler:prevent_unsafe_narrowing" to component("skia")
from 588 to 484 on my local Mac build.

Bug: chromium:1292951
Change-Id: Ice8210e1a2f038feb84184c88e6a34f52168952f
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/552376
Commit-Queue: Greg Daniel <egdaniel@google.com>
Auto-Submit: Peter Boström <pbos@chromium.org>
Reviewed-by: Greg Daniel <egdaniel@google.com>
Reviewed-by: John Stiles <johnstiles@google.com>
This commit is contained in:
Peter Boström 2022-07-14 09:27:44 -07:00 committed by SkCQ
parent 5b61aae7f4
commit cd938c5b0f
6 changed files with 23 additions and 20 deletions

View File

@ -256,7 +256,9 @@ public:
float getAlphaf() const { return fColor4f.fA; } float getAlphaf() const { return fColor4f.fA; }
// Helper that scales the alpha by 255. // Helper that scales the alpha by 255.
uint8_t getAlpha() const { return sk_float_round2int(this->getAlphaf() * 255); } uint8_t getAlpha() const {
return static_cast<uint8_t>(sk_float_round2int(this->getAlphaf() * 255));
}
/** Replaces alpha, leaving RGB /** Replaces alpha, leaving RGB
unchanged. An out of range value triggers an assert in the debug unchanged. An out of range value triggers an assert in the debug

View File

@ -154,7 +154,7 @@ public:
this->poisonRange(kDataStart, fSize); this->poisonRange(kDataStart, fSize);
} }
SkDEBUGCODE(int fSentinel;) // known value to check for bad back pointers to blocks SkDEBUGCODE(uint32_t fSentinel;) // known value to check for bad back pointers to blocks
Block* fNext; // doubly-linked list of blocks Block* fNext; // doubly-linked list of blocks
Block* fPrev; Block* fPrev;
@ -403,8 +403,8 @@ public:
inline BlockIter<false, true> rblocks() const; inline BlockIter<false, true> rblocks() const;
#ifdef SK_DEBUG #ifdef SK_DEBUG
inline static constexpr int kAssignedMarker = 0xBEEFFACE; inline static constexpr uint32_t kAssignedMarker = 0xBEEFFACE;
inline static constexpr int kFreedMarker = 0xCAFEBABE; inline static constexpr uint32_t kFreedMarker = 0xCAFEBABE;
void validate() const; void validate() const;
#endif #endif

View File

@ -161,7 +161,7 @@ constexpr int SkCLZ_portable(uint32_t x) {
y = x >> 4; if (y != 0) {n -= 4; x = y;} y = x >> 4; if (y != 0) {n -= 4; x = y;}
y = x >> 2; if (y != 0) {n -= 2; x = y;} y = x >> 2; if (y != 0) {n -= 2; x = y;}
y = x >> 1; if (y != 0) {return n - 2;} y = x >> 1; if (y != 0) {return n - 2;}
return n - x; return n - static_cast<int>(x);
} }
static_assert(32 == SkCLZ_portable(0)); static_assert(32 == SkCLZ_portable(0));
@ -279,12 +279,12 @@ constexpr int SkPrevLog2_portable(uint32_t value) {
*/ */
static inline int SkNextPow2(int value) { static inline int SkNextPow2(int value) {
SkASSERT(value > 0); SkASSERT(value > 0);
return 1 << SkNextLog2(value); return 1 << SkNextLog2(static_cast<uint32_t>(value));
} }
constexpr int SkNextPow2_portable(int value) { constexpr int SkNextPow2_portable(int value) {
SkASSERT(value > 0); SkASSERT(value > 0);
return 1 << SkNextLog2_portable(value); return 1 << SkNextLog2_portable(static_cast<uint32_t>(value));
} }
/** /**
@ -294,12 +294,12 @@ constexpr int SkNextPow2_portable(int value) {
*/ */
static inline int SkPrevPow2(int value) { static inline int SkPrevPow2(int value) {
SkASSERT(value > 0); SkASSERT(value > 0);
return 1 << SkPrevLog2(value); return 1 << SkPrevLog2(static_cast<uint32_t>(value));
} }
constexpr int SkPrevPow2_portable(int value) { constexpr int SkPrevPow2_portable(int value) {
SkASSERT(value > 0); SkASSERT(value > 0);
return 1 << SkPrevLog2_portable(value); return 1 << SkPrevLog2_portable(static_cast<uint32_t>(value));
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////

View File

@ -38,7 +38,7 @@ public:
constexpr char operator[](int i) const { constexpr char operator[](int i) const {
SkASSERT(i >= 0 && i < 4); SkASSERT(i >= 0 && i < 4);
int idx = (fKey >> (4U * i)) & 0xfU; int idx = (fKey >> (4 * i)) & 0xfU;
return IToC(idx); return IToC(idx);
} }
@ -62,7 +62,7 @@ public:
private: private:
explicit constexpr Swizzle(uint16_t key) : fKey(key) {} explicit constexpr Swizzle(uint16_t key) : fKey(key) {}
static constexpr float ComponentIndexToFloat(std::array<float, 4>, int idx); static constexpr float ComponentIndexToFloat(std::array<float, 4>, size_t idx);
static constexpr int CToI(char c); static constexpr int CToI(char c);
static constexpr char IToC(int idx); static constexpr char IToC(int idx);
@ -70,7 +70,8 @@ private:
}; };
constexpr Swizzle::Swizzle(const char c[4]) constexpr Swizzle::Swizzle(const char c[4])
: fKey((CToI(c[0]) << 0) | (CToI(c[1]) << 4) | (CToI(c[2]) << 8) | (CToI(c[3]) << 12)) {} : fKey(static_cast<uint16_t>((CToI(c[0]) << 0) | (CToI(c[1]) << 4) | (CToI(c[2]) << 8) |
(CToI(c[3]) << 12))) {}
constexpr Swizzle::Swizzle(const Swizzle& that) constexpr Swizzle::Swizzle(const Swizzle& that)
: fKey(that.fKey) {} : fKey(that.fKey) {}
@ -83,7 +84,7 @@ constexpr Swizzle& Swizzle::operator=(const Swizzle& that) {
constexpr std::array<float, 4> Swizzle::applyTo(std::array<float, 4> color) const { constexpr std::array<float, 4> Swizzle::applyTo(std::array<float, 4> color) const {
uint32_t key = fKey; uint32_t key = fKey;
// Index of the input color that should be mapped to output r. // Index of the input color that should be mapped to output r.
int idx = (key & 15); size_t idx = (key & 15);
float outR = ComponentIndexToFloat(color, idx); float outR = ComponentIndexToFloat(color, idx);
key >>= 4; key >>= 4;
idx = (key & 15); idx = (key & 15);
@ -97,14 +98,14 @@ constexpr std::array<float, 4> Swizzle::applyTo(std::array<float, 4> color) cons
return { outR, outG, outB, outA }; return { outR, outG, outB, outA };
} }
constexpr float Swizzle::ComponentIndexToFloat(std::array<float, 4> color, int idx) { constexpr float Swizzle::ComponentIndexToFloat(std::array<float, 4> color, size_t idx) {
if (idx <= 3) { if (idx <= 3) {
return color[idx]; return color[idx];
} }
if (idx == CToI('1')) { if (idx == static_cast<size_t>(CToI('1'))) {
return 1.0f; return 1.0f;
} }
if (idx == CToI('0')) { if (idx == static_cast<size_t>(CToI('0'))) {
return 0.0f; return 0.0f;
} }
SkUNREACHABLE; SkUNREACHABLE;
@ -138,11 +139,11 @@ constexpr char Swizzle::IToC(int idx) {
constexpr Swizzle Swizzle::Concat(const Swizzle& a, const Swizzle& b) { constexpr Swizzle Swizzle::Concat(const Swizzle& a, const Swizzle& b) {
uint16_t key = 0; uint16_t key = 0;
for (int i = 0; i < 4; ++i) { for (int i = 0; i < 4; ++i) {
int idx = (b.fKey >> (4U * i)) & 0xfU; int idx = (b.fKey >> (4 * i)) & 0xfU;
if (idx != CToI('0') && idx != CToI('1')) { if (idx != CToI('0') && idx != CToI('1')) {
SkASSERT(idx >= 0 && idx < 4); SkASSERT(idx >= 0 && idx < 4);
// Get the index value stored in a at location idx. // Get the index value stored in a at location idx.
idx = ((a.fKey >> (4U * idx)) & 0xfU); idx = ((a.fKey >> (4 * idx)) & 0xfU);
} }
key |= (idx << (4U * i)); key |= (idx << (4U * i));
} }

View File

@ -352,7 +352,7 @@ public:
size_t bufferMapThreshold() const { size_t bufferMapThreshold() const {
SkASSERT(fBufferMapThreshold >= 0); SkASSERT(fBufferMapThreshold >= 0);
return fBufferMapThreshold; return static_cast<size_t>(fBufferMapThreshold);
} }
/** True in environments that will issue errors if memory uploaded to buffers /** True in environments that will issue errors if memory uploaded to buffers

View File

@ -113,7 +113,7 @@ private:
int fID; // ID that can be used to track down leaks by clients. int fID; // ID that can be used to track down leaks by clients.
#endif #endif
#if defined(SK_DEBUG) || defined(SK_SANITIZE_ADDRESS) #if defined(SK_DEBUG) || defined(SK_SANITIZE_ADDRESS)
int fSentinel; // set to a known value to check for memory stomping; poisoned in ASAN mode uint32_t fSentinel; // set to a known value to check for memory stomping; poisoned in ASAN mode
#endif #endif
}; };