xref: /aosp_15_r20/external/skia/src/gpu/SkBackingFit.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2023 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/SkBackingFit.h"
9 
10 #include "include/private/base/SkMath.h"
11 #include "src/base/SkMathPriv.h"
12 
13 #include <algorithm>
14 
15 namespace skgpu {
16 
GetApproxSize(SkISize size)17 SkISize GetApproxSize(SkISize size) {
18     // Map 'value' to a larger multiple of 2. Values <= 'kMagicTol' will pop up to
19     // the next power of 2. Those above 'kMagicTol' will only go up half the floor power of 2.
20     auto adjust = [](int value) {
21         constexpr int kMinApproxSize = 16;
22         constexpr int kMagicTol = 1024;
23 
24         value = std::max(kMinApproxSize, value);
25 
26         if (SkIsPow2(value)) {
27             return value;
28         }
29 
30         int ceilPow2 = SkNextPow2(value);
31         if (value <= kMagicTol) {
32             return ceilPow2;
33         }
34 
35         int floorPow2 = ceilPow2 >> 1;
36         int mid = floorPow2 + (floorPow2 >> 1);
37 
38         if (value <= mid) {
39             return mid;
40         }
41         return ceilPow2;
42     };
43 
44     return {adjust(size.width()), adjust(size.height())};
45 }
46 
47 }  // namespace skgpu
48