1 /*
2  * Copyright 2017 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 SkBitmapProcState_utils_DEFINED
9 #define SkBitmapProcState_utils_DEFINED
10 
11 // Helper to ensure that when we shift down, we do it w/o sign-extension
12 // so the caller doesn't have to manually mask off the top 16 bits
13 //
14 static inline unsigned SK_USHIFT16(unsigned x) {
15     return x >> 16;
16 }
17 
18 /*
19  *  The decal_ functions require that
20  *  1. dx > 0
21  *  2. [fx, fx+dx, fx+2dx, fx+3dx, ... fx+(count-1)dx] are all <= maxX
22  *
23  *  In addition, we use SkFractionalInt to keep more fractional precision than
24  *  just SkFixed, so we will abort the decal_ call if dx is very small, since
25  *  the decal_ function just operates on SkFixed. If that were changed, we could
26  *  skip the very_small test here.
27  */
28 static inline bool can_truncate_to_fixed_for_decal(SkFixed fx,
29                                                    SkFixed dx,
30                                                    int count, unsigned max) {
31     SkASSERT(count > 0);
32 
33     // if decal_ kept SkFractionalInt precision, this would just be dx <= 0
34     // I just made up the 1/256. Just don't want to perceive accumulated error
35     // if we truncate frDx and lose its low bits.
36     if (dx <= SK_Fixed1 / 256) {
37         return false;
38     }
39 
40     // Note: it seems the test should be (fx <= max && lastFx <= max); but
41     // historically it's been a strict inequality check, and changing produces
42     // unexpected diffs.  Further investigation is needed.
43 
44     // We cast to unsigned so we don't have to check for negative values, which
45     // will now appear as very large positive values, and thus fail our test!
46     if ((unsigned)SkFixedFloorToInt(fx) >= max) {
47         return false;
48     }
49 
50     // Promote to 64bit (48.16) to avoid overflow.
51     const uint64_t lastFx = fx + sk_64_mul(dx, count - 1);
52 
53     return sk_64_isS32(lastFx) && (unsigned)SkFixedFloorToInt(sk_64_asS32(lastFx)) < max;
54 }
55 
56 #endif /* #ifndef SkBitmapProcState_utils_DEFINED */
57