1// Copyright (c) 2012 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 CONTENT_RENDERER_PEPPER_PPB_GRAPHICS_3D_IMPL_H_
6#define CONTENT_RENDERER_PEPPER_PPB_GRAPHICS_3D_IMPL_H_
7
8#include "base/memory/weak_ptr.h"
9#include "gpu/command_buffer/common/mailbox.h"
10#include "ppapi/shared_impl/ppb_graphics_3d_shared.h"
11#include "ppapi/shared_impl/resource.h"
12
13namespace content {
14class CommandBufferProxyImpl;
15class GpuChannelHost;
16
17class PPB_Graphics3D_Impl : public ppapi::PPB_Graphics3D_Shared {
18 public:
19  static PP_Resource Create(PP_Instance instance,
20                            PP_Resource share_context,
21                            const int32_t* attrib_list);
22  static PP_Resource CreateRaw(PP_Instance instance,
23                               PP_Resource share_context,
24                               const int32_t* attrib_list);
25
26  // PPB_Graphics3D_API trusted implementation.
27  virtual PP_Bool SetGetBuffer(int32_t transfer_buffer_id) OVERRIDE;
28  virtual scoped_refptr<gpu::Buffer> CreateTransferBuffer(uint32_t size,
29                                                          int32* id) OVERRIDE;
30  virtual PP_Bool DestroyTransferBuffer(int32_t id) OVERRIDE;
31  virtual PP_Bool Flush(int32_t put_offset) OVERRIDE;
32  virtual gpu::CommandBuffer::State WaitForTokenInRange(int32_t start,
33                                                        int32_t end) OVERRIDE;
34  virtual gpu::CommandBuffer::State WaitForGetOffsetInRange(int32_t start,
35                                                            int32_t end)
36      OVERRIDE;
37  virtual uint32_t InsertSyncPoint() OVERRIDE;
38
39  // Binds/unbinds the graphics of this context with the associated instance.
40  // Returns true if binding/unbinding is successful.
41  bool BindToInstance(bool bind);
42
43  // Returns true if the backing texture is always opaque.
44  bool IsOpaque();
45
46  // Notifications about the view's progress painting.  See PluginInstance.
47  // These messages are used to send Flush callbacks to the plugin.
48  void ViewInitiatedPaint();
49  void ViewFlushedPaint();
50
51  void GetBackingMailbox(gpu::Mailbox* mailbox, uint32* sync_point) {
52    *mailbox = mailbox_;
53    *sync_point = sync_point_;
54  }
55
56  int GetCommandBufferRouteId();
57
58  GpuChannelHost* channel() { return channel_; }
59
60 protected:
61  virtual ~PPB_Graphics3D_Impl();
62  // ppapi::PPB_Graphics3D_Shared overrides.
63  virtual gpu::CommandBuffer* GetCommandBuffer() OVERRIDE;
64  virtual gpu::GpuControl* GetGpuControl() OVERRIDE;
65  virtual int32 DoSwapBuffers() OVERRIDE;
66
67 private:
68  explicit PPB_Graphics3D_Impl(PP_Instance instance);
69
70  bool Init(PPB_Graphics3D_API* share_context, const int32_t* attrib_list);
71  bool InitRaw(PPB_Graphics3D_API* share_context, const int32_t* attrib_list);
72
73  // Notifications received from the GPU process.
74  void OnSwapBuffers();
75  void OnContextLost();
76  void OnConsoleMessage(const std::string& msg, int id);
77  // Notifications sent to plugin.
78  void SendContextLost();
79
80  // True if context is bound to instance.
81  bool bound_to_instance_;
82  // True when waiting for compositor to commit our backing texture.
83  bool commit_pending_;
84
85  gpu::Mailbox mailbox_;
86  uint32 sync_point_;
87  bool has_alpha_;
88  scoped_refptr<GpuChannelHost> channel_;
89  CommandBufferProxyImpl* command_buffer_;
90
91  base::WeakPtrFactory<PPB_Graphics3D_Impl> weak_ptr_factory_;
92
93  DISALLOW_COPY_AND_ASSIGN(PPB_Graphics3D_Impl);
94};
95
96}  // namespace content
97
98#endif  // CONTENT_RENDERER_PEPPER_PPB_GRAPHICS_3D_IMPL_H_
99