Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 #include <string.h> | |
| 6 | |
| 7 #include <iostream> | |
| 8 #include <sstream> | |
| 9 | |
| 10 #include "ppapi/c/dev/ppb_console_dev.h" | |
| 11 #include "ppapi/c/pp_errors.h" | |
| 12 #include "ppapi/c/ppb_opengles2.h" | |
| 13 #include "ppapi/cpp/graphics_3d.h" | |
| 14 #include "ppapi/cpp/graphics_3d_client.h" | |
| 15 #include "ppapi/cpp/instance.h" | |
| 16 #include "ppapi/cpp/module.h" | |
| 17 #include "ppapi/cpp/rect.h" | |
| 18 #include "ppapi/cpp/var.h" | |
| 19 #include "ppapi/lib/gl/include/GLES2/gl2.h" | |
| 20 #include "ppapi/utility/completion_callback_factory.h" | |
| 21 | |
| 22 // Use assert as a poor-man's CHECK, even in non-debug mode. | |
| 23 // Since <assert.h> redefines assert on every inclusion (it doesn't use | |
| 24 // include-guards), make sure this is the last file #include'd in this file. | |
| 25 #undef NDEBUG | |
| 26 #include <assert.h> | |
| 27 | |
| 28 // Assert |context_| isn't holding any GL Errors. Done as a macro instead of a | |
| 29 // function to preserve line number information in the failure message. | |
| 30 #define assertNoGLError() \ | |
| 31 assert(!gles2_if_->GetError(context_->pp_resource())); | |
| 32 | |
| 33 namespace { | |
| 34 | |
| 35 class GLES2DemoInstance : public pp::Instance, | |
| 36 public pp::Graphics3DClient { | |
| 37 public: | |
| 38 GLES2DemoInstance(PP_Instance instance, pp::Module* module); | |
| 39 virtual ~GLES2DemoInstance(); | |
| 40 | |
| 41 // pp::Instance implementation (see PPP_Instance). | |
| 42 virtual void DidChangeView(const pp::Rect& position, | |
| 43 const pp::Rect& clip_ignored); | |
| 44 | |
| 45 // pp::Graphics3DClient implementation. | |
| 46 virtual void Graphics3DContextLost() { | |
| 47 // TODO(jamesr): What's the state of context_? Should we delete the old one | |
| 48 // or try to revive it somehow? | |
|
piman
2012/03/08 00:03:44
The context is dead, indeed you need to destroy it
| |
| 49 // For now, just delete it and construct+bind a new context. | |
| 50 delete context_; | |
| 51 context_ = NULL; | |
| 52 InitGL(); | |
| 53 } | |
| 54 | |
| 55 private: | |
| 56 | |
| 57 // GL-related functions. | |
| 58 void InitGL(); | |
| 59 void FlickerAndPaint(int32_t result, bool paint_blue); | |
| 60 | |
| 61 pp::Size plugin_size_; | |
| 62 pp::CompletionCallbackFactory<GLES2DemoInstance> callback_factory_; | |
| 63 | |
| 64 // Unowned pointers. | |
| 65 const PPB_OpenGLES2* gles2_if_; | |
| 66 | |
| 67 // Owned data. | |
| 68 pp::Graphics3D* context_; | |
| 69 }; | |
| 70 | |
| 71 GLES2DemoInstance::GLES2DemoInstance(PP_Instance instance, pp::Module* module) | |
| 72 : pp::Instance(instance), pp::Graphics3DClient(this), | |
| 73 callback_factory_(this), | |
| 74 context_(NULL) { | |
| 75 assert((gles2_if_ = static_cast<const PPB_OpenGLES2*>( | |
| 76 module->GetBrowserInterface(PPB_OPENGLES2_INTERFACE)))); | |
| 77 } | |
| 78 | |
| 79 GLES2DemoInstance::~GLES2DemoInstance() { | |
| 80 delete context_; | |
| 81 } | |
| 82 | |
| 83 void GLES2DemoInstance::DidChangeView( | |
| 84 const pp::Rect& position, const pp::Rect& clip_ignored) { | |
| 85 if (position.width() == 0 || position.height() == 0) | |
| 86 return; | |
| 87 if (plugin_size_.width()) { | |
| 88 assert(position.size() == plugin_size_); | |
| 89 return; | |
| 90 } | |
| 91 plugin_size_ = position.size(); | |
| 92 | |
| 93 // Initialize graphics. | |
| 94 InitGL(); | |
| 95 } | |
| 96 | |
| 97 // This object is the global object representing this plugin library as long | |
| 98 // as it is loaded. | |
| 99 class GLES2DemoModule : public pp::Module { | |
| 100 public: | |
| 101 GLES2DemoModule() : pp::Module() {} | |
| 102 virtual ~GLES2DemoModule() {} | |
| 103 | |
| 104 virtual pp::Instance* CreateInstance(PP_Instance instance) { | |
| 105 return new GLES2DemoInstance(instance, this); | |
| 106 } | |
| 107 }; | |
| 108 | |
| 109 void GLES2DemoInstance::InitGL() { | |
| 110 assert(plugin_size_.width() && plugin_size_.height()); | |
| 111 | |
| 112 assert(!context_); | |
| 113 int32_t context_attributes[] = { | |
| 114 PP_GRAPHICS3DATTRIB_ALPHA_SIZE, 8, | |
| 115 PP_GRAPHICS3DATTRIB_BLUE_SIZE, 8, | |
| 116 PP_GRAPHICS3DATTRIB_GREEN_SIZE, 8, | |
| 117 PP_GRAPHICS3DATTRIB_RED_SIZE, 8, | |
| 118 PP_GRAPHICS3DATTRIB_DEPTH_SIZE, 0, | |
| 119 PP_GRAPHICS3DATTRIB_STENCIL_SIZE, 0, | |
| 120 PP_GRAPHICS3DATTRIB_SAMPLES, 0, | |
| 121 PP_GRAPHICS3DATTRIB_SAMPLE_BUFFERS, 0, | |
| 122 PP_GRAPHICS3DATTRIB_WIDTH, plugin_size_.width(), | |
| 123 PP_GRAPHICS3DATTRIB_HEIGHT, plugin_size_.height(), | |
| 124 PP_GRAPHICS3DATTRIB_NONE, | |
| 125 }; | |
| 126 context_ = new pp::Graphics3D(this, context_attributes); | |
| 127 assert(!context_->is_null()); | |
| 128 assert(BindGraphics(*context_)); | |
| 129 | |
| 130 // Clear color bit. | |
| 131 gles2_if_->ClearColor(context_->pp_resource(), 0, 1, 0, 1); | |
| 132 gles2_if_->Clear(context_->pp_resource(), GL_COLOR_BUFFER_BIT); | |
| 133 | |
| 134 assertNoGLError(); | |
| 135 | |
| 136 FlickerAndPaint(0, true); | |
| 137 } | |
| 138 | |
| 139 void GLES2DemoInstance::FlickerAndPaint(int32_t result, bool paint_blue) { | |
| 140 if (result != 0) | |
| 141 return; | |
| 142 float r = paint_blue ? 0 : 1; | |
| 143 float g = 0; | |
| 144 float b = paint_blue ? 1 : 0; | |
| 145 float a = 0.75; | |
| 146 gles2_if_->ClearColor(context_->pp_resource(), r, g, b, a); | |
| 147 gles2_if_->Clear(context_->pp_resource(), GL_COLOR_BUFFER_BIT); | |
| 148 assertNoGLError(); | |
| 149 | |
| 150 pp::CompletionCallback cb = callback_factory_.NewCallback( | |
| 151 &GLES2DemoInstance::FlickerAndPaint, !paint_blue); | |
| 152 context_->SwapBuffers(cb); | |
| 153 assertNoGLError(); | |
| 154 } | |
| 155 | |
| 156 } // anonymous namespace | |
| 157 | |
| 158 namespace pp { | |
| 159 // Factory function for your specialization of the Module object. | |
| 160 Module* CreateModule() { | |
| 161 return new GLES2DemoModule(); | |
| 162 } | |
| 163 } // namespace pp | |
| OLD | NEW |