summaryrefslogtreecommitdiffstats
path: root/o3d/gpu_plugin/gpu_plugin_object.cc
blob: 165addcb7225b058dea4978a302bae821e91e1da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stdlib.h>

#include "base/logging.h"
#include "o3d/gpu_plugin/np_utils/np_utils.h"
#include "o3d/gpu_plugin/gpu_plugin_object.h"
#include "o3d/gpu_plugin/gpu_processor.h"

namespace gpu_plugin {

const NPUTF8 GPUPluginObject::kPluginType[] =
    "application/vnd.google.chrome.gpu-plugin";

GPUPluginObject::GPUPluginObject(NPP npp)
    : npp_(npp),
      status_(kWaitingForNew),
      command_buffer_(NPCreateObject<CommandBuffer>(npp)),
      processor_(new GPUProcessor(npp, command_buffer_.Get())) {
  memset(&window_, 0, sizeof(window_));
}

NPError GPUPluginObject::New(NPMIMEType plugin_type,
                             int16 argc,
                             char* argn[],
                             char* argv[],
                             NPSavedData* saved) {
  if (status_ != kWaitingForNew)
    return NPERR_GENERIC_ERROR;

  status_ = kWaitingForSetWindow;

  return NPERR_NO_ERROR;
}

NPError GPUPluginObject::SetWindow(NPWindow* new_window) {
  if (status_ == kWaitingForNew || status_ == kDestroyed)
    return NPERR_GENERIC_ERROR;

  // PlatformSpecificSetWindow advances the status depending on what happens.
  NPError error = PlatformSpecificSetWindow(new_window);
  if (error == NPERR_NO_ERROR) {
    window_ = *new_window;

    if (event_sync_.Get()) {
      NPInvokeVoid(npp_,
                   event_sync_,
                   "resize",
                   static_cast<int32>(window_.width),
                   static_cast<int32>(window_.height));
    }
  } else {
    memset(&window_, 0, sizeof(window_));
  }

  return error;
}

int16 GPUPluginObject::HandleEvent(NPEvent* event) {
  return 0;
}

NPError GPUPluginObject::Destroy(NPSavedData** saved) {
  if (status_ == kWaitingForNew || status_ == kDestroyed)
    return NPERR_GENERIC_ERROR;

  if (command_buffer_.Get()) {
    command_buffer_->SetPutOffsetChangeCallback(NULL);
  }

  status_ = kDestroyed;

  return NPERR_NO_ERROR;
}

void GPUPluginObject::Release() {
  DCHECK(status_ == kWaitingForNew || status_ == kDestroyed);
  NPBrowser::get()->ReleaseObject(this);
}

NPObject*GPUPluginObject::GetScriptableNPObject() {
  NPBrowser::get()->RetainObject(this);
  return this;
}

NPObjectPointer<NPObject> GPUPluginObject::OpenCommandBuffer() {
  if (status_ == kInitializationSuccessful)
    return command_buffer_;

  // SetWindow must have been called before OpenCommandBuffer.
  // PlatformSpecificSetWindow advances the status to
  // kWaitingForOpenCommandBuffer.
  if (status_ != kWaitingForOpenCommandBuffer)
    return NPObjectPointer<NPObject>();

  NPObjectPointer<NPObject> window = NPObjectPointer<NPObject>::FromReturned(
      NPBrowser::get()->GetWindowNPObject(npp_));
  if (!window.Get())
    return NPObjectPointer<NPObject>();

  NPObjectPointer<NPObject> chromium;
  if (!NPGetProperty(npp_, window, "chromium", &chromium)) {
    return NPObjectPointer<NPObject>();
  }

  NPObjectPointer<NPObject> system;
  if (!NPGetProperty(npp_, chromium, "system", &system)) {
    return NPObjectPointer<NPObject>();
  }

  NPObjectPointer<NPObject> ring_buffer;
  if (!NPInvoke(npp_, system, "createSharedMemory", kCommandBufferSize,
                &ring_buffer)) {
    return NPObjectPointer<NPObject>();
  }

  if (!ring_buffer.Get()) {
    return NPObjectPointer<NPObject>();
  }

  if (command_buffer_->Initialize(ring_buffer)) {
    if (processor_->Initialize(static_cast<HWND>(window_.window))) {
      command_buffer_->SetPutOffsetChangeCallback(
          NewCallback(processor_.get(),
                      &GPUProcessor::ProcessCommands));
      status_ = kInitializationSuccessful;
      return command_buffer_;
    }
  }

  return NPObjectPointer<CommandBuffer>();
}

}  // namespace gpu_plugin