summaryrefslogtreecommitdiffstats
path: root/gpu/demos/framework/pepper.cc
blob: 2d3d50b99ce957ec747f5023a64ac3b57114b312 (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
// Copyright (c) 2010 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 "base/at_exit.h"
#include "base/scoped_ptr.h"
#include "gpu/demos/framework/demo.h"
#include "gpu/demos/framework/demo_factory.h"
#include "ppapi/cpp/completion_callback.h"
#include "ppapi/cpp/instance.h"
#include "ppapi/cpp/dev/graphics_3d_dev.h"
#include "ppapi/cpp/module.h"
#include "ppapi/cpp/rect.h"
#include "ppapi/cpp/size.h"

namespace gpu {
namespace demos {

class PluginInstance : public pp::Instance {
 public:
  PluginInstance(PP_Instance instance, pp::Module* module)
      : pp::Instance(instance),
        module_(module),
        demo_(CreateDemo()) {
    // Set the callback object outside of the initializer list to avoid a
    // compiler warning about using "this" in an initializer list.
    callback_factory_.Initialize(this);
  }

  ~PluginInstance() {
    if (!graphics_.is_null()) {
      graphics_.MakeCurrent();
      demo_.reset();
      pp::Graphics3D_Dev::ResetCurrent();
    }
  }

  virtual void DidChangeView(const pp::Rect& position,
                             const pp::Rect& /*clip*/) {
    if (size_ == position.size())
      return;

    size_ = position.size();
    demo_->InitWindowSize(size_.width(), size_.height());

    if (graphics_.is_null()) {
      graphics_ = pp::Graphics3D_Dev(*this, 0, NULL, NULL);
      if (graphics_.is_null())
        return;

      if (!pp::Instance::BindGraphics(graphics_))
        return;

      graphics_.MakeCurrent();
      demo_->InitGL();
      pp::Graphics3D_Dev::ResetCurrent();
    }

    if (demo_->IsAnimated())
      Animate(0);
    else
      Paint();
  }

  void Paint() {
    graphics_.MakeCurrent();
    demo_->Draw();
    graphics_.SwapBuffers();
    pp::Graphics3D_Dev::ResetCurrent();
  }

 private:
  void Animate(int delay) {
    Paint();
    module_->core()->CallOnMainThread(delay,
        callback_factory_.NewCallback(&PluginInstance::Animate), delay);
  }

  pp::Module* module_;
  scoped_ptr<Demo> demo_;
  pp::Graphics3D_Dev graphics_;
  pp::Size size_;
  pp::CompletionCallbackFactory<PluginInstance> callback_factory_;
};

class PluginModule : public pp::Module {
 public:
  PluginModule() : pp::Module(), at_exit_manager_(new base::AtExitManager) {}

  virtual pp::Instance* CreateInstance(PP_Instance instance) {
    return new PluginInstance(instance, this);
  }

 private:
  scoped_ptr<base::AtExitManager> at_exit_manager_;
};

}  // namespace demos
}  // namespace gpu

namespace pp {

Module* CreateModule() {
  return new gpu::demos::PluginModule();
}

}  // namespace pp