15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ppapi/proxy/gamepad_resource.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string.h>
85f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
95f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "base/bind.h"
105f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "base/threading/platform_thread.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ppapi/proxy/dispatch_reply_message.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ppapi/proxy/ppapi_messages.h"
135f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "ppapi/shared_impl/ppb_gamepad_shared.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
155f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)namespace ppapi {
165f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)namespace proxy {
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace {
195f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
205f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// This is the read logic from content/common/gamepad_seqlock.h
215f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)base::subtle::Atomic32 ReadBegin(const base::subtle::Atomic32* sequence) {
225f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  base::subtle::Atomic32 version;
235f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  for (;;) {
245f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    version = base::subtle::NoBarrier_Load(sequence);
255f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
265f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    // If the counter is even, then the associated data might be in a
275f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    // consistent state, so we can try to read.
285f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    if ((version & 1) == 0)
295f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      break;
305f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
315f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    // Otherwise, the writer is in the middle of an update. Retry the read.
325f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    base::PlatformThread::YieldCurrentThread();
335f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  }
345f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  return version;
355f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
365f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
375f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)bool ReadRetry(const base::subtle::Atomic32* sequence,
385f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)               base::subtle::Atomic32 version) {
395f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // If the sequence number was updated then a read should be re-attempted.
405f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // -- Load fence, read membarrier
415f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  return base::subtle::Release_Load(sequence) != version;
425f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
435f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
445f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}  // namespace
455f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
465f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)GamepadResource::GamepadResource(Connection connection, PP_Instance instance)
475f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    : PluginResource(connection, instance),
485f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      buffer_(NULL) {
495f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  memset(&last_read_, 0, sizeof(last_read_));
505f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
515f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  SendCreate(BROWSER, PpapiHostMsg_Gamepad_Create());
525f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  Call<PpapiPluginMsg_Gamepad_SendMemory>(
535f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      BROWSER,
545f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      PpapiHostMsg_Gamepad_RequestMemory(),
555f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      base::Bind(&GamepadResource::OnPluginMsgSendMemory, this));
565f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
575f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
585f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)GamepadResource::~GamepadResource() {
595f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
605f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)thunk::PPB_Gamepad_API* GamepadResource::AsPPB_Gamepad_API() {
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return this;
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
64
65void GamepadResource::Sample(PP_Instance /* instance */,
66                             PP_GamepadsSampleData* data) {
67  if (!buffer_) {
68    // Browser hasn't sent back our shared memory, give the plugin gamepad
69    // data corresponding to "not connected".
70    memset(data, 0, sizeof(PP_GamepadsSampleData));
71    return;
72  }
73
74  // ==========
75  //   DANGER
76  // ==========
77  //
78  // This logic is duplicated in the renderer as well. If you change it, that
79  // also needs to be in sync. See gamepad_shared_memory_reader.cc.
80
81  // Only try to read this many times before failing to avoid waiting here
82  // very long in case of contention with the writer.
83  const int kMaximumContentionCount = 10;
84  int contention_count = -1;
85  base::subtle::Atomic32 version;
86  WebKitGamepads read_into;
87  do {
88    version = ReadBegin(&buffer_->sequence);
89    memcpy(&read_into, &buffer_->buffer, sizeof(read_into));
90    ++contention_count;
91    if (contention_count == kMaximumContentionCount)
92      break;
93  } while (ReadRetry(&buffer_->sequence, version));
94
95  // In the event of a read failure, just leave the last read data as-is (the
96  // hardware thread is taking unusally long).
97  if (contention_count < kMaximumContentionCount)
98    ConvertWebKitGamepadData(read_into, &last_read_);
99
100  memcpy(data, &last_read_, sizeof(PP_GamepadsSampleData));
101}
102
103void GamepadResource::OnPluginMsgSendMemory(
104    const ResourceMessageReplyParams& params) {
105  // On failure, the handle will be null and the CHECK below will be tripped.
106  base::SharedMemoryHandle handle = base::SharedMemory::NULLHandle();
107  params.TakeSharedMemoryHandleAtIndex(0, &handle);
108
109  shared_memory_.reset(new base::SharedMemory(handle, true));
110  CHECK(shared_memory_->Map(sizeof(ContentGamepadHardwareBuffer)));
111  buffer_ = static_cast<const ContentGamepadHardwareBuffer*>(
112      shared_memory_->memory());
113}
114
115}  // namespace proxy
116}  // namespace ppapi
117