| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2013 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 <stdio.h> | |
| 6 | |
| 7 #include "ppapi/cpp/graphics_2d.h" | |
| 8 #include "ppapi/cpp/message_loop.h" | |
| 9 #include "ppapi/cpp/size.h" | |
| 10 #include "ppapi/cpp/var.h" | |
| 11 | |
| 12 #include "ppapi/utility/completion_callback_factory.h" | |
| 13 | |
| 14 #include "ppapi_main/ppapi_instance2d.h" | |
| 15 #include "ppapi_main/ppapi_main.h" | |
| 16 | |
| 17 | |
| 18 void* PPAPI_CreateInstance2D(PP_Instance inst, const char *args[]) { | |
| 19 return static_cast<void*>(new PPAPIInstance2D(inst, args)); | |
| 20 } | |
| 21 | |
| 22 | |
| 23 PPAPIInstance2D* PPAPIInstance2D::GetInstance2D() { | |
| 24 return static_cast<PPAPIInstance2D*>(PPAPI_GetInstanceObject()); | |
| 25 } | |
| 26 | |
| 27 | |
| 28 void PPAPIInstance2D::Flushed(int result) { | |
| 29 if (result != 0) { | |
| 30 printf("Flush result=%d.\n", result); | |
| 31 } | |
| 32 | |
| 33 if (is_context_bound_) { | |
| 34 Render(device_context_.pp_resource(), size_.width(), size_.height()); | |
| 35 | |
| 36 int result; | |
| 37 result = device_context_.Flush(callback_factory_.NewCallback( | |
| 38 &PPAPIInstance::Flushed)); | |
| 39 if (result == PP_OK_COMPLETIONPENDING) return; | |
| 40 printf("Failed Flush with %d.\n", result); | |
| 41 } | |
| 42 | |
| 43 // Failed to draw, so add a callback for the future. This could | |
| 44 // an application choice or the browser dealing with an event such as | |
| 45 // fullscreen toggle. We add a delay of 100ms (to prevent burnning CPU | |
| 46 // in cases where the context will not be available for a while. | |
| 47 pp::MessageLoop::GetCurrent().PostWork(callback_factory_.NewCallback( | |
| 48 &PPAPIInstance::Flushed), 100); | |
| 49 } | |
| 50 | |
| 51 void PPAPIInstance2D::BuildContext(int32_t result, const pp::Size& new_size) { | |
| 52 printf("Building context.\n"); | |
| 53 | |
| 54 size_ = new_size; | |
| 55 device_context_ = pp::Graphics2D(this, size_ ,true); | |
| 56 printf("Got Context!\n"); | |
| 57 | |
| 58 is_context_bound_ = BindGraphics(device_context_); | |
| 59 printf("Context is bound=%d\n", is_context_bound_); | |
| 60 | |
| 61 if (is_context_bound_) { | |
| 62 PPAPIBuildContext(size_.width(), size_.height()); | |
| 63 device_context_.Flush(callback_factory_.NewCallback( | |
| 64 &PPAPIInstance::Flushed)); | |
| 65 } else { | |
| 66 fprintf(stderr, "Failed to bind context for %dx%d.\n", size_.width(), | |
| 67 size_.height()); | |
| 68 } | |
| 69 } | |
| 70 | |
| 71 // The default implementation calls the 'C' render function. | |
| 72 void PPAPIInstance2D::Render(PP_Resource ctx, uint32_t width, | |
| 73 uint32_t height) { | |
| 74 PPAPIRender(ctx, width, height); | |
| 75 } | |
| 76 | |
| 77 PPAPIInstance2D::PPAPIInstance2D(PP_Instance instance, const char *args[]) | |
| 78 : PPAPIInstance(instance, args) { | |
| 79 } | |
| 80 | |
| 81 | |
| 82 PPAPIInstance2D::~PPAPIInstance2D() { | |
| 83 if (is_context_bound_) { | |
| 84 is_context_bound_ = false; | |
| 85 // Cleanup code? | |
| 86 } | |
| 87 } | |
| OLD | NEW |