| OLD | NEW | 
|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be | 
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. | 
| 4 | 4 | 
| 5 #ifndef CHROMECAST_BASE_CAST_RESOURCE_H_ | 5 #ifndef CHROMECAST_BASE_CAST_RESOURCE_H_ | 
| 6 #define CHROMECAST_BASE_CAST_RESOURCE_H_ | 6 #define CHROMECAST_BASE_CAST_RESOURCE_H_ | 
| 7 | 7 | 
| 8 #include "base/macros.h" | 8 #include "base/macros.h" | 
| 9 | 9 | 
| 10 namespace chromecast { | 10 namespace chromecast { | 
| 11 | 11 | 
| 12 // Interface for resources needed to run application. | 12 // A CastResource is a user of 1 or more Resources (primary screen, audio, | 
|  | 13 // etc). As a user, it is responsible for doing any cast-specific component | 
|  | 14 // initialization when the Resources it uses are granted. This initialization | 
|  | 15 // is referred to as "acquiring" the Resources. Conversely, when the Resources | 
|  | 16 // it uses are revoked, it must deinitialize these cast-specific components. | 
|  | 17 // This deinitialization is referred to as "releasing" the Resources. | 
|  | 18 // TODO(maclellant): RENAME/DESIGN THIS CLASS IN COMING REFACTOR. | 
| 13 class CastResource { | 19 class CastResource { | 
| 14  public: | 20  public: | 
| 15   // Resources necessary to run cast apps. CastResource may contain union of the | 21   // Resources necessary to run cast apps. CastResource may contain union of the | 
| 16   // following types. | 22   // following types. | 
| 17   // TODO(yucliu): Split video resources and graphic resources. | 23   // TODO(yucliu): Split video resources and graphic resources. | 
| 18   enum Resource { | 24   enum Resource { | 
| 19     kResourceNone = 0, | 25     kResourceNone = 0, | 
| 20     // All resources necessary to render sounds, for example, audio pipeline, | 26     // All resources necessary to render sounds, for example, audio pipeline, | 
| 21     // speaker, etc. | 27     // speaker, etc. | 
| 22     kResourceAudio = 1 << 0, | 28     kResourceAudio = 1 << 0, | 
| 23     // All resources necessary to render videos or images, for example, video | 29     // All resources necessary to render videos or images, for example, video | 
| 24     // pipeline, primary graphics plane, display, etc. | 30     // pipeline, primary graphics plane, display, etc. | 
| 25     kResourceScreenPrimary = 1 << 1, | 31     kResourceScreenPrimary = 1 << 1, | 
| 26     // All resources necessary to render overlaid images, for example, secondary | 32     // All resources necessary to render overlaid images, for example, secondary | 
| 27     // graphics plane, LCD, etc. | 33     // graphics plane, LCD, etc. | 
| 28     kResourceScreenSecondary = 1 << 2, | 34     kResourceScreenSecondary = 1 << 2, | 
| 29     // Collection of resources used for display only combined with bitwise or. | 35     // Collection of resources used for display only combined with bitwise or. | 
| 30     kResourceDisplayOnly = (kResourceScreenPrimary | kResourceScreenSecondary), | 36     kResourceDisplayOnly = (kResourceScreenPrimary | kResourceScreenSecondary), | 
| 31     // Collection of all resources combined with bitwise or. | 37     // Collection of all resources combined with bitwise or. | 
| 32     kResourceAll = | 38     kResourceAll = | 
| 33         (kResourceAudio | kResourceScreenPrimary | kResourceScreenSecondary), | 39         (kResourceAudio | kResourceScreenPrimary | kResourceScreenSecondary), | 
| 34   }; | 40   }; | 
| 35 | 41 | 
|  | 42   // A Client is responsible for notifying all registered CastResource's when | 
|  | 43   // Resources are granted/revoked so that they can acquire/release those | 
|  | 44   // Resources. When a CastResource is done acquiring/releasing, it responds | 
|  | 45   // to the Client that it has completed. A Client can have multiple registered | 
|  | 46   // CastResource's, but each CastResouce has 1 Client that it responds to. | 
| 36   class Client { | 47   class Client { | 
| 37    public: | 48    public: | 
| 38     // Called when resource is created. CastResource should not be owned by | 49     // Called to register a CastResource with a Client. After registering, a | 
| 39     // Client. It can be called from any thread. | 50     // CastResource will start getting notified when to acquire/release | 
| 40     virtual void OnResourceAcquired(CastResource* cast_resource) = 0; | 51     // Resources. The Client does not take ownership of |cast_resource|. It can | 
|  | 52     // be called from any thread. | 
|  | 53     virtual void RegisterCastResource(CastResource* cast_resource) = 0; | 
|  | 54 | 
|  | 55     // TODO(esum): Add OnResourceAcquired() here once AcquireResource is | 
|  | 56     // allowed to be asynchronous. | 
|  | 57 | 
| 41     // Called when part or all resources are released. It can be called from any | 58     // Called when part or all resources are released. It can be called from any | 
| 42     // thread. | 59     // thread. | 
| 43     //   |cast_resource| the CastResource that is released. The pointer may be | 60     //   |cast_resource| the CastResource that is released. The pointer may be | 
| 44     //                   invalid. Client can't call functions with that pointer. | 61     //                   invalid. Client can't call functions with that pointer. | 
| 45     //   |remain| the unreleased resource of CastResource. If kResourceNone is | 62     //   |remain| the unreleased resource of CastResource. If kResourceNone is | 
| 46     //            returned, Client will remove the resource from its watching | 63     //            returned, Client will remove the resource from its watching | 
| 47     //            list. | 64     //            list. | 
| 48     virtual void OnResourceReleased(CastResource* cast_resource, | 65     virtual void OnResourceReleased(CastResource* cast_resource, | 
| 49                                     Resource remain) = 0; | 66                                     Resource remain) = 0; | 
| 50 | 67 | 
| 51    protected: | 68    protected: | 
| 52     virtual ~Client() {} | 69     virtual ~Client() {} | 
| 53   }; | 70   }; | 
| 54 | 71 | 
|  | 72   // Sets the Client for the CastResource to respond to when it is done with | 
|  | 73   // Acquire/ReleaseResource. | 
| 55   void SetCastResourceClient(Client* client); | 74   void SetCastResourceClient(Client* client); | 
|  | 75   // Called to acquire resources after OEM has granted them, and before | 
|  | 76   // they start getting used by consumers. Implementation must be synchronous | 
|  | 77   // since consumers will start using the resource immediately afterwards. | 
|  | 78   // TODO(esum): We should allow this method to be asynchronous in case an | 
|  | 79   // implementer needs to make expensive calls and doesn't want to block the | 
|  | 80   // UI thread (b/26239576). For now, don't do anything expensive in your | 
|  | 81   // implementation; if you really need to, then this bug has to be resolved. | 
|  | 82   virtual void AcquireResource(Resource resource) = 0; | 
| 56   // Called to release resources. Implementation should call | 83   // Called to release resources. Implementation should call | 
| 57   // Client::OnResourceReleased when resource is released on its side. | 84   // Client::OnResourceReleased when resource is released on its side. | 
| 58   virtual void ReleaseResource(Resource resource) = 0; | 85   virtual void ReleaseResource(Resource resource) = 0; | 
| 59 | 86 | 
| 60  protected: | 87  protected: | 
| 61   CastResource() : client_(nullptr) {} | 88   CastResource() : client_(nullptr) {} | 
| 62   virtual ~CastResource() {} | 89   virtual ~CastResource() {} | 
| 63 | 90 | 
| 64   void NotifyResourceAcquired(); | 91   // For derived classes to register themselves with their Client through | 
|  | 92   // Client::RegisterCastResource. | 
|  | 93   void RegisterWithClient(); | 
| 65   void NotifyResourceReleased(Resource remain); | 94   void NotifyResourceReleased(Resource remain); | 
| 66 | 95 | 
| 67  private: | 96  private: | 
| 68   Client* client_; | 97   Client* client_; | 
| 69 | 98 | 
| 70   DISALLOW_COPY_AND_ASSIGN(CastResource); | 99   DISALLOW_COPY_AND_ASSIGN(CastResource); | 
| 71 }; | 100 }; | 
| 72 | 101 | 
| 73 }  // namespace chromecast | 102 }  // namespace chromecast | 
| 74 | 103 | 
| 75 #endif | 104 #endif | 
| OLD | NEW | 
|---|