ppb_graphics_3d_shared.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
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 PPAPI_SHARED_IMPL_GRAPHICS_3D_IMPL_H_
6#define PPAPI_SHARED_IMPL_GRAPHICS_3D_IMPL_H_
7
8#include "base/basictypes.h"
9#include "base/memory/scoped_ptr.h"
10#include "ppapi/c/pp_completion_callback.h"
11#include "ppapi/shared_impl/ppapi_shared_export.h"
12#include "ppapi/shared_impl/resource.h"
13#include "ppapi/shared_impl/tracked_callback.h"
14#include "ppapi/thunk/ppb_graphics_3d_api.h"
15
16namespace gpu {
17class CommandBuffer;
18class TransferBuffer;
19namespace gles2 {
20class GLES2CmdHelper;
21class GLES2Implementation;
22}  // namespace gles2
23}  // namespace gpu.
24
25namespace ppapi {
26
27class PPAPI_SHARED_EXPORT PPB_Graphics3D_Shared
28    : public Resource,
29      public thunk::PPB_Graphics3D_API {
30 public:
31  // Resource overrides.
32  virtual thunk::PPB_Graphics3D_API* AsPPB_Graphics3D_API() OVERRIDE;
33
34  // PPB_Graphics3D_API implementation.
35  virtual int32_t GetAttribs(int32_t attrib_list[]) OVERRIDE;
36  virtual int32_t SetAttribs(const int32_t attrib_list[]) OVERRIDE;
37  virtual int32_t GetError() OVERRIDE;
38  virtual int32_t ResizeBuffers(int32_t width, int32_t height) OVERRIDE;
39  virtual int32_t SwapBuffers(scoped_refptr<TrackedCallback> callback) OVERRIDE;
40  virtual void* MapTexSubImage2DCHROMIUM(GLenum target,
41                                         GLint level,
42                                         GLint xoffset,
43                                         GLint yoffset,
44                                         GLsizei width,
45                                         GLsizei height,
46                                         GLenum format,
47                                         GLenum type,
48                                         GLenum access) OVERRIDE;
49  virtual void UnmapTexSubImage2DCHROMIUM(const void* mem) OVERRIDE;
50
51  gpu::gles2::GLES2Implementation* gles2_impl() {
52    return gles2_impl_.get();
53  }
54
55  // Sends swap-buffers notification to the plugin.
56  void SwapBuffersACK(int32_t pp_error);
57
58 protected:
59  // ScopedNoLocking makes sure we don't try to lock again when we already have
60  // the proxy lock. This is used when we need to use the CommandBuffer
61  // (possibly via gles2_impl) but we already have the proxy lock. The
62  // CommandBuffer in the plugin side of the proxy will otherwise try to acquire
63  // the ProxyLock, causing a crash because we already own the lock. (Locks in
64  // Chromium are never recursive).
65  class ScopedNoLocking {
66   public:
67    explicit ScopedNoLocking(PPB_Graphics3D_Shared* graphics3d_shared)
68        : graphics3d_shared_(graphics3d_shared) {
69      graphics3d_shared_->PushAlreadyLocked();
70    }
71    ~ScopedNoLocking() {
72      graphics3d_shared_->PopAlreadyLocked();
73    }
74   private:
75    PPB_Graphics3D_Shared* graphics3d_shared_;  // Weak
76
77    DISALLOW_COPY_AND_ASSIGN(ScopedNoLocking);
78  };
79
80
81  PPB_Graphics3D_Shared(PP_Instance instance);
82  PPB_Graphics3D_Shared(const HostResource& host_resource);
83  virtual ~PPB_Graphics3D_Shared();
84
85  virtual gpu::CommandBuffer* GetCommandBuffer() = 0;
86  virtual int32 DoSwapBuffers() = 0;
87
88  bool HasPendingSwap() const;
89  bool CreateGLES2Impl(int32 command_buffer_size,
90                       int32 transfer_buffer_size,
91                       gpu::gles2::GLES2Implementation* share_gles2);
92  void DestroyGLES2Impl();
93
94 private:
95  // On the plugin side, we need to know that we already have the lock, so that
96  // we don't try to acquire it again. The default implementation does nothing;
97  // the Plugin side of the proxy must implement these.
98  friend class ScopedNoLocking;
99  virtual void PushAlreadyLocked();
100  virtual void PopAlreadyLocked();
101
102  scoped_ptr<gpu::gles2::GLES2CmdHelper> gles2_helper_;
103  scoped_ptr<gpu::TransferBuffer> transfer_buffer_;
104  scoped_ptr<gpu::gles2::GLES2Implementation> gles2_impl_;
105
106  // Callback that needs to be executed when swap-buffers is completed.
107  scoped_refptr<TrackedCallback> swap_callback_;
108
109  DISALLOW_COPY_AND_ASSIGN(PPB_Graphics3D_Shared);
110};
111
112}  // namespace ppapi
113
114#endif  // PPAPI_SHARED_IMPL_GRAPHICS_3D_IMPL_H_
115
116