Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / android_webview / browser / global_tile_manager.h
blob3473c20ead34de7946530f3e79be7abdc621104e
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef ANDROID_WEBVIEW_BROWSER_GLOBAL_TILE_MANAGER_H_
6 #define ANDROID_WEBVIEW_BROWSER_GLOBAL_TILE_MANAGER_H_
8 #include <list>
9 #include "base/basictypes.h"
10 #include "base/lazy_instance.h"
11 #include "base/sequence_checker.h"
12 #include "base/synchronization/lock.h"
14 namespace android_webview {
16 class GlobalTileManagerClient;
18 // A global tile manager that keeps track of the number of tile resources. Each
19 // tile needs file descriptors (typically 2) and there is a soft limit of 1024
20 // file descriptors per Android process. The GlobalTileManager does not keep
21 // track of how many tiles each individual view is actually using. The purpose
22 // of GlobalTileManager is to behave gracefully (as in not crashing) when the
23 // embedder of webview creates a lot of webviews and draw them at the same time.
24 class GlobalTileManager {
25 private:
26 typedef std::list<GlobalTileManagerClient*> ListType;
28 public:
29 typedef ListType::iterator Key;
30 static GlobalTileManager* GetInstance();
32 void SetTileLimit(size_t num_tiles_limit);
34 // Requests the |num_of_tiles| from the available global pool. Calls
35 // GlobalTileManagerClient.SetNumTiles after the manager determines how many
36 // tiles are available for the client. If the number of tiles left is not
37 // enough to satisfy the request, the manager will evict tiles allocated to
38 // other clients.
39 void RequestTiles(size_t new_num_of_tiles, Key key);
41 Key PushBack(GlobalTileManagerClient* client);
43 // |key| must be already in manager. Move the tile manager client
44 // corresponding to |key| to most recent. This function should be called after
45 // RequestTiles.
46 void DidUse(Key key);
48 void Remove(Key key);
50 private:
51 friend struct base::DefaultLazyInstanceTraits<GlobalTileManager>;
52 GlobalTileManager();
53 ~GlobalTileManager();
55 // Continues evicting the inactive views until freeing up at least amount of
56 // tiles specified by |desired_num_tiles| to draw a view specified by |key|,
57 // or until all inactive views have been evicted. Returns the amount of
58 // memory that was actually evicted. This function is called when a
59 // request cannot be satisfied.
60 size_t Evict(size_t desired_num_tiles, Key key);
62 // Check that the sum of all client's tiles is equal to
63 // total_allocated_tiles_.
64 bool IsConsistent() const;
66 size_t num_tiles_limit_;
68 size_t total_allocated_tiles_;
69 ListType mru_list_;
70 base::SequenceChecker sequence_checker_;
72 DISALLOW_COPY_AND_ASSIGN(GlobalTileManager);
75 } // namespace android_webview
77 #endif // ANDROID_WEBVIEW_BROWSER_GLOBAL_TILE_MANAGER_H_