|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <chrono> |
| 4 | + |
| 5 | +namespace rnwgpu { |
| 6 | + |
| 7 | +// purgeGpuResources(), which the system also brings about when it is short of |
| 8 | +// memory and, on Android, when the app goes to the background: Dawn gives the |
| 9 | +// GPU memory it keeps for reuse (the memory of destroyed textures and buffers, |
| 10 | +// staging memory) back to the system, for every live device (see |
| 11 | +// GPUDevice::reduceMemoryUsage). Nothing alive is affected. |
| 12 | +// |
| 13 | +// The platform modules only report what the system said (onMemoryWarning, |
| 14 | +// onLowMemory, onTrimMemory): whether it calls for a purge is decided here. |
| 15 | +// |
| 16 | +// request() returns at once. The work runs on a thread of its own, started by |
| 17 | +// a request and gone once done; a request made while it runs is served by one |
| 18 | +// more run of the same thread, however many came in. |
| 19 | +class GpuPurge { |
| 20 | +public: |
| 21 | + // purgeGpuResources() from JS. Any thread. |
| 22 | + static void request() noexcept; |
| 23 | + |
| 24 | + // iOS, UIApplicationDidReceiveMemoryWarningNotification: the system is short |
| 25 | + // of memory, and ends the apps that hold the most of it first. Any thread. |
| 26 | + static void onMemoryWarning() noexcept; |
| 27 | + |
| 28 | + // Android, ComponentCallbacks.onLowMemory(): the whole system is short of |
| 29 | + // memory. Any thread. |
| 30 | + static void onLowMemory() noexcept; |
| 31 | + |
| 32 | + // Android, ComponentCallbacks2.onTrimMemory(): purges from |
| 33 | + // kTrimMemoryUiHidden, which is each time the app goes to the background. |
| 34 | + // Any thread. |
| 35 | + static void onTrimMemory(int level) noexcept; |
| 36 | + |
| 37 | +private: |
| 38 | + // One run asks Dawn again for as long as it says deletions wait for |
| 39 | + // submitted work to complete, this many times at most and this far apart: |
| 40 | + // about a second in all. |
| 41 | + static constexpr int kMaxPasses = 10; |
| 42 | + static constexpr std::chrono::milliseconds kPassInterval{100}; |
| 43 | + |
| 44 | + // The passes of a run that also ask the devices only the thread of their |
| 45 | + // runtime may use (see GPUDevice::reduceMemoryUsageOnRuntimeThreads). What |
| 46 | + // Dawn answers there is not waited for, hence a fixed number: the first lets |
| 47 | + // the pending deletions go through, the next free what they leave. |
| 48 | + static constexpr int kRuntimeThreadPasses = 3; |
| 49 | + |
| 50 | + // ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN: the app is no longer visible. It |
| 51 | + // renders nothing there, and the system ends the processes in the background |
| 52 | + // that hold the most memory first. The system sends it each time the app |
| 53 | + // leaves the foreground, and it may be all it sends: TRIM_MEMORY_BACKGROUND |
| 54 | + // did not come to an app left in the background until it was cached and |
| 55 | + // frozen (a Samsung on Android 17). The levels below this one are for an app |
| 56 | + // in the foreground, and are no longer sent since Android 14. |
| 57 | + static constexpr int kTrimMemoryUiHidden = 20; |
| 58 | + |
| 59 | + // The purge thread: runs until no request is left. |
| 60 | + static void runRequests() noexcept; |
| 61 | + |
| 62 | + // One run: the passes over the live devices. |
| 63 | + static void run(); |
| 64 | +}; |
| 65 | + |
| 66 | +} // namespace rnwgpu |
0 commit comments