OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 #include "webkit/plugins/ppapi/ppb_context_3d_impl.h" |
| 6 |
| 7 #include "gpu/command_buffer/common/command_buffer.h" |
| 8 #include "ppapi/c/dev/ppb_graphics_3d_dev.h" |
| 9 #include "webkit/plugins/ppapi/common.h" |
| 10 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" |
| 11 |
| 12 namespace webkit { |
| 13 namespace ppapi { |
| 14 |
| 15 namespace { |
| 16 |
| 17 // Size of the transfer buffer. |
| 18 enum { kTransferBufferSize = 512 * 1024 }; |
| 19 |
| 20 PP_Resource Create(PP_Instance instance_id, |
| 21 PP_Config3D_Dev config, |
| 22 PP_Resource share_context, |
| 23 const int32_t* attrib_list) { |
| 24 // TODO(alokp): Support shared context. |
| 25 DCHECK_EQ(0, share_context); |
| 26 if (share_context != 0) |
| 27 return 0; |
| 28 |
| 29 PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id); |
| 30 if (!instance) |
| 31 return 0; |
| 32 |
| 33 scoped_refptr<PPB_Context3D_Impl> context( |
| 34 new PPB_Context3D_Impl(instance->module())); |
| 35 if (!context->Init(instance, config, share_context, attrib_list)) |
| 36 return 0; |
| 37 |
| 38 return context->GetReference(); |
| 39 } |
| 40 |
| 41 PP_Bool IsContext3D(PP_Resource resource) { |
| 42 return BoolToPPBool(!!Resource::GetAs<PPB_Context3D_Impl>(resource)); |
| 43 } |
| 44 |
| 45 int32_t GetAttrib(PP_Resource context, |
| 46 int32_t attribute, |
| 47 int32_t* value) { |
| 48 // TODO(alokp): Implement me. |
| 49 return 0; |
| 50 } |
| 51 |
| 52 int32_t BindSurfaces(PP_Resource context, |
| 53 PP_Resource draw, |
| 54 PP_Resource read) { |
| 55 // TODO(alokp): Implement me. |
| 56 return 0; |
| 57 } |
| 58 |
| 59 int32_t GetBoundSurfaces(PP_Resource context, |
| 60 PP_Resource* draw, |
| 61 PP_Resource* read) { |
| 62 // TODO(alokp): Implement me. |
| 63 return 0; |
| 64 } |
| 65 |
| 66 int32_t SwapBuffers(PP_Resource context_id, |
| 67 PP_CompletionCallback callback) { |
| 68 scoped_refptr<PPB_Context3D_Impl> context( |
| 69 Resource::GetAs<PPB_Context3D_Impl>(context_id)); |
| 70 return context->SwapBuffers(); |
| 71 } |
| 72 |
| 73 const PPB_Context3D_Dev ppb_context3d = { |
| 74 &Create, |
| 75 &IsContext3D, |
| 76 &GetAttrib, |
| 77 &BindSurfaces, |
| 78 &GetBoundSurfaces, |
| 79 &SwapBuffers |
| 80 }; |
| 81 |
| 82 } // namespace |
| 83 |
| 84 PPB_Context3D_Impl::PPB_Context3D_Impl(PluginModule* module) |
| 85 : Resource(module), |
| 86 bound_instance_(NULL), |
| 87 gles2_impl_(NULL) { |
| 88 } |
| 89 |
| 90 PPB_Context3D_Impl::~PPB_Context3D_Impl() { |
| 91 Destroy(); |
| 92 } |
| 93 |
| 94 const PPB_Context3D_Dev* PPB_Context3D_Impl::GetInterface() { |
| 95 return &ppb_context3d; |
| 96 } |
| 97 |
| 98 PPB_Context3D_Impl* PPB_Context3D_Impl::AsPPB_Context3D_Impl() { |
| 99 return this; |
| 100 } |
| 101 |
| 102 bool PPB_Context3D_Impl::Init(PluginInstance* instance, |
| 103 PP_Config3D_Dev config, |
| 104 PP_Resource share_context, |
| 105 const int32_t* attrib_list) { |
| 106 DCHECK(instance); |
| 107 // Create and initialize the objects required to issue GLES2 calls. |
| 108 platform_context_.reset(instance->delegate()->CreateContext3D()); |
| 109 if (!platform_context_.get()) { |
| 110 Destroy(); |
| 111 return false; |
| 112 } |
| 113 if (!platform_context_->Init()) { |
| 114 Destroy(); |
| 115 return false; |
| 116 } |
| 117 |
| 118 gles2_impl_ = platform_context_->GetGLES2Implementation(); |
| 119 DCHECK(gles2_impl_); |
| 120 |
| 121 return true; |
| 122 } |
| 123 |
| 124 bool PPB_Context3D_Impl::BindToInstance(PluginInstance* new_instance) { |
| 125 if (bound_instance_ == new_instance) |
| 126 return true; // Rebinding the same device, nothing to do. |
| 127 if (bound_instance_ && new_instance) |
| 128 return false; // Can't change a bound device. |
| 129 |
| 130 if (new_instance) { |
| 131 // Resize the backing texture to the size of the instance when it is bound. |
| 132 platform_context_->ResizeBackingTexture(new_instance->position().size()); |
| 133 |
| 134 // This is a temporary hack. The SwapBuffers is issued to force the resize |
| 135 // to take place before any subsequent rendering. This might lead to a |
| 136 // partially rendered frame being displayed. It is also not thread safe |
| 137 // since the SwapBuffers is written to the command buffer and that command |
| 138 // buffer might be written to by another thread. |
| 139 // TODO(apatrick): Figure out the semantics of binding and resizing. |
| 140 platform_context_->SwapBuffers(); |
| 141 } |
| 142 |
| 143 bound_instance_ = new_instance; |
| 144 return true; |
| 145 } |
| 146 |
| 147 bool PPB_Context3D_Impl::SwapBuffers() { |
| 148 if (!platform_context_.get()) |
| 149 return false; |
| 150 |
| 151 return platform_context_->SwapBuffers(); |
| 152 } |
| 153 |
| 154 void PPB_Context3D_Impl::SetSwapBuffersCallback(Callback0::Type* callback) { |
| 155 if (!platform_context_.get()) |
| 156 return; |
| 157 |
| 158 platform_context_->SetSwapBuffersCallback(callback); |
| 159 } |
| 160 |
| 161 unsigned int PPB_Context3D_Impl::GetBackingTextureId() { |
| 162 if (!platform_context_.get()) |
| 163 return 0; |
| 164 |
| 165 return platform_context_->GetBackingTextureId(); |
| 166 } |
| 167 |
| 168 void PPB_Context3D_Impl::ResizeBackingTexture(const gfx::Size& size) { |
| 169 if (!platform_context_.get()) |
| 170 return; |
| 171 |
| 172 platform_context_->ResizeBackingTexture(size); |
| 173 } |
| 174 |
| 175 void PPB_Context3D_Impl::Destroy() { |
| 176 gles2_impl_ = NULL; |
| 177 platform_context_.reset(); |
| 178 } |
| 179 |
| 180 } // namespace ppapi |
| 181 } // namespace webkit |
| 182 |
OLD | NEW |