OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 <math.h> |
| 6 #include <stdio.h> |
| 7 |
| 8 #include <vector> |
| 9 |
| 10 #include "ppapi/c/pp_errors.h" |
| 11 #include "ppapi/cpp/compositor.h" |
| 12 #include "ppapi/cpp/compositor_layer.h" |
| 13 #include "ppapi/cpp/core.h" |
| 14 #include "ppapi/cpp/graphics_3d.h" |
| 15 #include "ppapi/cpp/graphics_3d_client.h" |
| 16 #include "ppapi/cpp/image_data.h" |
| 17 #include "ppapi/cpp/instance.h" |
| 18 #include "ppapi/cpp/module.h" |
| 19 #include "ppapi/cpp/rect.h" |
| 20 #include "ppapi/examples/compositor/spinning_cube.h" |
| 21 #include "ppapi/lib/gl/gles2/gl2ext_ppapi.h" |
| 22 #include "ppapi/lib/gl/include/GLES2/gl2.h" |
| 23 #include "ppapi/lib/gl/include/GLES2/gl2ext.h" |
| 24 #include "ppapi/utility/completion_callback_factory.h" |
| 25 |
| 26 // Use assert as a poor-man's CHECK, even in non-debug mode. |
| 27 // Since <assert.h> redefines assert on every inclusion (it doesn't use |
| 28 // include-guards), make sure this is the last file #include'd in this file. |
| 29 #undef NDEBUG |
| 30 #include <assert.h> |
| 31 |
| 32 namespace { |
| 33 |
| 34 const int32_t kTextureWidth = 800; |
| 35 const int32_t kTextureHeight = 800; |
| 36 const int32_t kImageWidth = 256; |
| 37 const int32_t kImageHeight = 256; |
| 38 |
| 39 class DemoInstance : public pp::Instance, public pp::Graphics3DClient { |
| 40 public: |
| 41 DemoInstance(PP_Instance instance); |
| 42 virtual ~DemoInstance(); |
| 43 |
| 44 // pp::Instance implementation (see PPP_Instance). |
| 45 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]); |
| 46 virtual void DidChangeView(const pp::Rect& position, |
| 47 const pp::Rect& clip); |
| 48 |
| 49 // pp::Graphics3DClient implementation. |
| 50 virtual void Graphics3DContextLost(); |
| 51 |
| 52 private: |
| 53 // GL-related functions. |
| 54 void InitGL(int32_t result); |
| 55 GLuint PrepareFramebuffer(); |
| 56 pp::ImageData PrepareImage(); |
| 57 void Paint(int32_t result); |
| 58 void OnTextureReleased(int32_t result, GLuint texture); |
| 59 void OnImageReleased(int32_t result, const pp::ImageData& image); |
| 60 |
| 61 pp::Size plugin_size_; |
| 62 pp::CompletionCallbackFactory<DemoInstance> callback_factory_; |
| 63 |
| 64 // Owned data. |
| 65 pp::Graphics3D* context_; |
| 66 |
| 67 GLuint fbo_; |
| 68 GLuint rbo_; |
| 69 |
| 70 std::vector<GLuint> textures_; |
| 71 std::vector<pp::ImageData> images_; |
| 72 |
| 73 pp::Compositor compositor_; |
| 74 |
| 75 pp::CompositorLayer color_layer_; |
| 76 pp::CompositorLayer stable_texture_layer_; |
| 77 pp::CompositorLayer texture_layer_; |
| 78 pp::CompositorLayer image_layer_; |
| 79 uint32_t frame_; |
| 80 |
| 81 SpinningCube* cube_; |
| 82 }; |
| 83 |
| 84 DemoInstance::DemoInstance(PP_Instance instance) |
| 85 : pp::Instance(instance), |
| 86 pp::Graphics3DClient(this), |
| 87 callback_factory_(this), |
| 88 context_(NULL), |
| 89 fbo_(0), |
| 90 rbo_(0), |
| 91 compositor_(this), |
| 92 frame_(0), |
| 93 cube_(new SpinningCube()) {} |
| 94 |
| 95 DemoInstance::~DemoInstance() { |
| 96 delete cube_; |
| 97 assert(glTerminatePPAPI()); |
| 98 delete context_; |
| 99 } |
| 100 |
| 101 bool DemoInstance::Init(uint32_t /*argc*/, |
| 102 const char* /*argn*/[], |
| 103 const char* /*argv*/[]) { |
| 104 return !!glInitializePPAPI(pp::Module::Get()->get_browser_interface()); |
| 105 } |
| 106 |
| 107 void DemoInstance::DidChangeView( |
| 108 const pp::Rect& position, const pp::Rect& /*clip*/) { |
| 109 if (position.width() == 0 || position.height() == 0) |
| 110 return; |
| 111 plugin_size_ = position.size(); |
| 112 |
| 113 // Initialize graphics. |
| 114 InitGL(0); |
| 115 } |
| 116 |
| 117 void DemoInstance::Graphics3DContextLost() { |
| 118 fbo_ = 0; |
| 119 rbo_ = 0; |
| 120 compositor_.ResetLayers(); |
| 121 color_layer_ = pp::CompositorLayer(); |
| 122 stable_texture_layer_ = pp::CompositorLayer(); |
| 123 texture_layer_ = pp::CompositorLayer(); |
| 124 image_layer_ = pp::CompositorLayer(); |
| 125 textures_.clear(); |
| 126 delete context_; |
| 127 context_ = NULL; |
| 128 cube_->OnGLContextLost(); |
| 129 pp::CompletionCallback cb = callback_factory_.NewCallback( |
| 130 &DemoInstance::InitGL); |
| 131 pp::Module::Get()->core()->CallOnMainThread(0, cb, 0); |
| 132 } |
| 133 |
| 134 void DemoInstance::InitGL(int32_t /*result*/) { |
| 135 assert(plugin_size_.width() && plugin_size_.height()); |
| 136 |
| 137 if (context_) { |
| 138 context_->ResizeBuffers(plugin_size_.width(), plugin_size_.height()); |
| 139 return; |
| 140 } |
| 141 int32_t context_attributes[] = { |
| 142 PP_GRAPHICS3DATTRIB_ALPHA_SIZE, 8, |
| 143 PP_GRAPHICS3DATTRIB_BLUE_SIZE, 8, |
| 144 PP_GRAPHICS3DATTRIB_GREEN_SIZE, 8, |
| 145 PP_GRAPHICS3DATTRIB_RED_SIZE, 8, |
| 146 PP_GRAPHICS3DATTRIB_DEPTH_SIZE, 0, |
| 147 PP_GRAPHICS3DATTRIB_STENCIL_SIZE, 0, |
| 148 PP_GRAPHICS3DATTRIB_SAMPLES, 0, |
| 149 PP_GRAPHICS3DATTRIB_SAMPLE_BUFFERS, 0, |
| 150 PP_GRAPHICS3DATTRIB_WIDTH, plugin_size_.width(), |
| 151 PP_GRAPHICS3DATTRIB_HEIGHT, plugin_size_.height(), |
| 152 PP_GRAPHICS3DATTRIB_NONE, |
| 153 }; |
| 154 context_ = new pp::Graphics3D(this, context_attributes); |
| 155 assert(!context_->is_null()); |
| 156 assert(BindGraphics(compositor_)); |
| 157 |
| 158 glSetCurrentContextPPAPI(context_->pp_resource()); |
| 159 |
| 160 cube_->Init(kTextureWidth, kTextureHeight); |
| 161 |
| 162 Paint(PP_OK); |
| 163 } |
| 164 |
| 165 GLuint DemoInstance::PrepareFramebuffer() { |
| 166 GLuint texture = 0; |
| 167 if (textures_.empty()) { |
| 168 // Create a texture object |
| 169 glGenTextures(1, &texture); |
| 170 glBindTexture(GL_TEXTURE_2D, texture); |
| 171 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, kTextureWidth, kTextureHeight, 0, |
| 172 GL_RGBA, GL_UNSIGNED_BYTE, NULL); |
| 173 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 174 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 175 // glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 176 // glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 177 glBindTexture(GL_TEXTURE_2D, 0); |
| 178 } else { |
| 179 texture = textures_.back(); |
| 180 textures_.pop_back(); |
| 181 } |
| 182 |
| 183 if (!rbo_) { |
| 184 // create a renderbuffer object to store depth info |
| 185 glGenRenderbuffers(1, &rbo_); |
| 186 glBindRenderbuffer(GL_RENDERBUFFER, rbo_); |
| 187 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, |
| 188 kTextureWidth, kTextureHeight); |
| 189 glBindRenderbuffer(GL_RENDERBUFFER, 0); |
| 190 } |
| 191 |
| 192 if (!fbo_) { |
| 193 // create a framebuffer object |
| 194 glGenFramebuffers(1, &fbo_); |
| 195 } |
| 196 |
| 197 glBindFramebuffer(GL_FRAMEBUFFER, fbo_); |
| 198 |
| 199 // attach the texture to FBO color attachment point |
| 200 glFramebufferTexture2D(GL_FRAMEBUFFER, |
| 201 GL_COLOR_ATTACHMENT0, |
| 202 GL_TEXTURE_2D, |
| 203 texture, |
| 204 0); |
| 205 |
| 206 // attach the renderbuffer to depth attachment point |
| 207 glFramebufferRenderbuffer(GL_FRAMEBUFFER, |
| 208 GL_DEPTH_ATTACHMENT, |
| 209 GL_RENDERBUFFER, |
| 210 rbo_); |
| 211 |
| 212 // check FBO status |
| 213 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); |
| 214 assert(status == GL_FRAMEBUFFER_COMPLETE); |
| 215 |
| 216 return texture; |
| 217 } |
| 218 |
| 219 pp::ImageData DemoInstance::PrepareImage() { |
| 220 if (images_.empty()) { |
| 221 return pp::ImageData(this, |
| 222 PP_IMAGEDATAFORMAT_RGBA_PREMUL, |
| 223 pp::Size(kImageWidth, kImageHeight), false); |
| 224 } |
| 225 pp::ImageData image = images_.back(); |
| 226 images_.pop_back(); |
| 227 return image; |
| 228 } |
| 229 |
| 230 void DemoInstance::Paint(int32_t result) { |
| 231 if (result != PP_OK || !context_) |
| 232 return; |
| 233 float factor_sin = sin(M_PI / 180 * frame_); |
| 234 float factor_cos = cos(M_PI / 180 * frame_); |
| 235 { |
| 236 // Set the background color layer. |
| 237 if (color_layer_.is_null()) { |
| 238 color_layer_ = compositor_.AddLayer(); |
| 239 assert(!color_layer_.is_null()); |
| 240 static const float transform[16] = { |
| 241 1.0f, 0.0f, 0.0f, 0.0f, |
| 242 0.0f, 1.0f, 0.0f, 0.0f, |
| 243 0.0f, 0.0f, 1.0f, 0.0f, |
| 244 0.0f, 0.0f, 0.0f, 1.0f, |
| 245 }; |
| 246 color_layer_.SetTransform(transform); |
| 247 } |
| 248 color_layer_.SetColor(255 * factor_sin, |
| 249 255 * factor_cos, |
| 250 255 * factor_sin * factor_cos, |
| 251 255, |
| 252 pp::Size(800, 600)); |
| 253 } |
| 254 |
| 255 { |
| 256 // Set the image layer |
| 257 if (image_layer_.is_null()) { |
| 258 image_layer_ = compositor_.AddLayer(); |
| 259 assert(!image_layer_.is_null()); |
| 260 } |
| 261 float x = frame_ % 800; |
| 262 float y = 200 - 200 * factor_sin; |
| 263 const float transform[16] = { |
| 264 fabsf(factor_sin) + 0.2f, 0.0f, 0.0f, 0.0f, |
| 265 0.0f, fabsf(factor_sin) + 0.2f, 0.0f, 0.0f, |
| 266 0.0f, 0.0f, 1.0f, 0.0f, |
| 267 x, y, 0.0f, 1.0f, |
| 268 }; |
| 269 image_layer_.SetTransform(transform); |
| 270 pp::ImageData image = PrepareImage(); |
| 271 uint8_t *p = static_cast<uint8_t*>(image.data()); |
| 272 for (int x = 0; x < kImageWidth; x++) { |
| 273 for (int y = 0; y < kImageHeight; y++) { |
| 274 *(p++) = frame_; |
| 275 *(p++) = frame_ * x; |
| 276 *(p++) = frame_ * y; |
| 277 *(p++) = 255; |
| 278 } |
| 279 } |
| 280 image_layer_.SetImage(image, pp::Size(kImageWidth, kImageHeight), |
| 281 callback_factory_.NewCallback(&DemoInstance::OnImageReleased, image)); |
| 282 } |
| 283 |
| 284 { |
| 285 // Set the stable texture layer |
| 286 if (stable_texture_layer_.is_null()) { |
| 287 stable_texture_layer_ = compositor_.AddLayer(); |
| 288 assert(!stable_texture_layer_.is_null()); |
| 289 stable_texture_layer_.SetPremultipliedAlpha(PP_FALSE); |
| 290 // stable_texture_layer_.SetClipRect(pp::Rect(50, 50, 400, 400)); |
| 291 GLuint texture = PrepareFramebuffer(); |
| 292 cube_->UpdateForTimeDelta(0.02f); |
| 293 cube_->Draw(); |
| 294 stable_texture_layer_.SetTexture(*context_, texture, pp::Size(600, 600), |
| 295 callback_factory_.NewCallback(&DemoInstance::OnTextureReleased, |
| 296 texture)); |
| 297 } |
| 298 float x = 10 - 100 * factor_sin; |
| 299 float y = 10 - 100 * factor_cos; |
| 300 const float transform[16] = { |
| 301 1.0f, 0.0f, 0.0f, 0.0f, |
| 302 0.0f, 1.0f, 0.0f, 0.0f, |
| 303 0.0f, 0.0f, 1.0f, 0.0f, |
| 304 x, y, 0.0f, 1.0f, |
| 305 }; |
| 306 stable_texture_layer_.SetTransform(transform); |
| 307 } |
| 308 |
| 309 |
| 310 if (texture_layer_.is_null()) { |
| 311 texture_layer_ = compositor_.AddLayer(); |
| 312 assert(!texture_layer_.is_null()); |
| 313 static const float transform[16] = { |
| 314 1.0f, 0.0f, 0.0f, 0.0f, |
| 315 0.0f, 1.0f, 0.0f, 0.0f, |
| 316 0.0f, 0.0f, 1.0f, 0.0f, |
| 317 200.0f, 0.0f, 0.0f, 1.0f, |
| 318 }; |
| 319 texture_layer_.SetTransform(transform); |
| 320 texture_layer_.SetPremultipliedAlpha(PP_FALSE); |
| 321 } |
| 322 |
| 323 GLuint texture = PrepareFramebuffer(); |
| 324 cube_->UpdateForTimeDelta(0.02f); |
| 325 cube_->Draw(); |
| 326 texture_layer_.SetTexture(*context_, texture, pp::Size(400, 400), |
| 327 callback_factory_.NewCallback(&DemoInstance::OnTextureReleased, texture)); |
| 328 |
| 329 compositor_.CommitLayers( |
| 330 callback_factory_.NewCallback(&DemoInstance::Paint)); |
| 331 ++frame_; |
| 332 } |
| 333 |
| 334 void DemoInstance::OnTextureReleased(int32_t result, GLuint texture) { |
| 335 if (result == PP_OK) |
| 336 textures_.push_back(texture); |
| 337 } |
| 338 |
| 339 void DemoInstance::OnImageReleased(int32_t result, const pp::ImageData& image) { |
| 340 if (result == PP_OK); |
| 341 images_.push_back(image); |
| 342 } |
| 343 |
| 344 // This object is the global object representing this plugin library as long |
| 345 // as it is loaded. |
| 346 class DemoModule : public pp::Module { |
| 347 public: |
| 348 DemoModule() : Module() {} |
| 349 virtual ~DemoModule() {} |
| 350 |
| 351 virtual pp::Instance* CreateInstance(PP_Instance instance) { |
| 352 return new DemoInstance(instance); |
| 353 } |
| 354 }; |
| 355 |
| 356 } // anonymous namespace |
| 357 |
| 358 namespace pp { |
| 359 // Factory function for your specialization of the Module object. |
| 360 Module* CreateModule() { |
| 361 return new DemoModule(); |
| 362 } |
| 363 } // namespace pp |
OLD | NEW |