Home | History | Annotate | Download | only in browser
      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.
      4 
      5 #ifndef ANDROID_WEBVIEW_BROWSER_GLOBAL_TILE_MANAGER_H_
      6 #define ANDROID_WEBVIEW_BROWSER_GLOBAL_TILE_MANAGER_H_
      7 
      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"
     13 #include "content/public/browser/android/synchronous_compositor.h"
     14 
     15 namespace android_webview {
     16 
     17 class GlobalTileManagerClient;
     18 
     19 // A global tile manager that keeps track of the number of tile resources. Each
     20 // tile needs file descriptors (typically 2) and there is a soft limit of 1024
     21 // file descriptors per Android process. The GlobalTileManager does not keep
     22 // track of how many tiles each individual view is actually using. The purpose
     23 // of GlobalTileManager is to behave gracefully (as in not crashing) when the
     24 // embedder of webview creates a lot of webviews and draw them at the same time.
     25 class GlobalTileManager {
     26  private:
     27   typedef std::list<GlobalTileManagerClient*> ListType;
     28 
     29  public:
     30   typedef ListType::iterator Key;
     31   static GlobalTileManager* GetInstance();
     32 
     33   void SetTileLimit(size_t num_tiles_limit);
     34 
     35   // Requests the |new_num_of_tiles| from the available global pool. Calls
     36   // GlobalTileManagerClient.SetNumTiles after the manager determines how many
     37   // tiles are available for the client. If the number of tiles left is not
     38   // enough to satisfy the request, the manager will evict tiles allocated to
     39   // other clients.
     40   void RequestTiles(content::SynchronousCompositorMemoryPolicy new_policy,
     41                     Key key);
     42 
     43   Key PushBack(GlobalTileManagerClient* client);
     44 
     45   // |key| must be already in manager. Move the tile manager client
     46   // corresponding to |key| to most recent. This function should be called after
     47   // RequestTiles.
     48   void DidUse(Key key);
     49 
     50   void Remove(Key key);
     51 
     52  private:
     53   friend struct base::DefaultLazyInstanceTraits<GlobalTileManager>;
     54   GlobalTileManager();
     55   ~GlobalTileManager();
     56 
     57   // Continues evicting the inactive views until freeing up at least amount of
     58   // tiles specified by |desired_num_tiles| to draw a view specified by |key|,
     59   // or until all inactive views have been evicted. Returns the amount of
     60   // memory that was actually evicted. This function is called when a
     61   // request cannot be satisfied.
     62   size_t Evict(size_t desired_num_tiles, Key key);
     63 
     64   // Check that the sum of all client's tiles is equal to
     65   // total_allocated_tiles_.
     66   bool IsConsistent() const;
     67 
     68   size_t num_tiles_limit_;
     69 
     70   size_t total_allocated_tiles_;
     71   ListType mru_list_;
     72   base::SequenceChecker sequence_checker_;
     73 
     74   DISALLOW_COPY_AND_ASSIGN(GlobalTileManager);
     75 };
     76 
     77 }  // namespace android_webview
     78 
     79 #endif  // ANDROID_WEBVIEW_BROWSER_GLOBAL_TILE_MANAGER_H_
     80