1 // Copyright 2016 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 THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_WEB_GRAPHICS_CONTEXT_3D_PROVIDER_WRAPPER_H_
6 #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_WEB_GRAPHICS_CONTEXT_3D_PROVIDER_WRAPPER_H_
7 
8 #include <memory>
9 #include <utility>
10 
11 #include "base/memory/ptr_util.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/observer_list.h"
14 #include "third_party/blink/public/platform/web_graphics_context_3d_provider.h"
15 #include "third_party/blink/renderer/platform/graphics/gpu/graphics_context_3d_utils.h"
16 #include "third_party/blink/renderer/platform/platform_export.h"
17 #include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
18 
19 namespace blink {
20 
21 class PLATFORM_EXPORT WebGraphicsContext3DProviderWrapper {
22   USING_FAST_MALLOC(WebGraphicsContext3DProviderWrapper);
23 
24  public:
25   class DestructionObserver {
26    public:
~DestructionObserver()27     virtual ~DestructionObserver() {}
28     virtual void OnContextDestroyed() = 0;
29   };
30 
WebGraphicsContext3DProviderWrapper(std::unique_ptr<WebGraphicsContext3DProvider> provider)31   WebGraphicsContext3DProviderWrapper(
32       std::unique_ptr<WebGraphicsContext3DProvider> provider)
33       : context_provider_(std::move(provider)) {
34     DCHECK(context_provider_);
35     utils_ = base::WrapUnique(new GraphicsContext3DUtils(GetWeakPtr()));
36   }
37   ~WebGraphicsContext3DProviderWrapper();
38 
GetWeakPtr()39   base::WeakPtr<WebGraphicsContext3DProviderWrapper> GetWeakPtr() {
40     return weak_ptr_factory_.GetWeakPtr();
41   }
ContextProvider()42   WebGraphicsContext3DProvider* ContextProvider() {
43     return context_provider_.get();
44   }
45 
Utils()46   GraphicsContext3DUtils* Utils() { return utils_.get(); }
47 
48   void AddObserver(DestructionObserver*);
49   void RemoveObserver(DestructionObserver*);
50 
51  private:
52   std::unique_ptr<GraphicsContext3DUtils> utils_;
53   std::unique_ptr<WebGraphicsContext3DProvider> context_provider_;
54   base::ObserverList<DestructionObserver>::Unchecked observers_;
55   base::WeakPtrFactory<WebGraphicsContext3DProviderWrapper> weak_ptr_factory_{
56       this};
57 };
58 
59 }  // namespace blink
60 
61 #endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_WEB_GRAPHICS_CONTEXT_3D_PROVIDER_WRAPPER_H_
62