summaryrefslogtreecommitdiffstats
path: root/native_client_sdk/src/libraries/ppapi_main/ppapi_instance2d.cc
blob: f6e54be62e8fd64a544177c321dea21df62c596b (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
// Copyright (c) 2013 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 <stdio.h>

#include "ppapi/cpp/graphics_2d.h"
#include "ppapi/cpp/message_loop.h"
#include "ppapi/cpp/size.h"
#include "ppapi/cpp/var.h"

#include "ppapi/utility/completion_callback_factory.h"

#include "ppapi_main/ppapi_instance2d.h"
#include "ppapi_main/ppapi_main.h"


void* PPAPI_CreateInstance2D(PP_Instance inst, const char *args[]) {
  return static_cast<void*>(new PPAPIInstance2D(inst, args));
}


PPAPIInstance2D* PPAPIInstance2D::GetInstance2D() {
  return static_cast<PPAPIInstance2D*>(PPAPI_GetInstanceObject());
}


void PPAPIInstance2D::Flushed(int result) {
  if (result != 0) {
    printf("Flush result=%d.\n", result);
  }

  if (is_context_bound_) {
    Render(device_context_.pp_resource(), size_.width(), size_.height());

    int result;
    result = device_context_.Flush(callback_factory_.NewCallback(
                                         &PPAPIInstance::Flushed));
    if (result == PP_OK_COMPLETIONPENDING) return;
    printf("Failed Flush with %d.\n", result);
  }

  // Failed to draw, so add a callback for the future.  This could
  // an application choice or the browser dealing with an event such as
  // fullscreen toggle.  We add a delay of 100ms (to prevent burnning CPU
  // in cases where the context will not be available for a while.
  pp::MessageLoop::GetCurrent().PostWork(callback_factory_.NewCallback(
                                         &PPAPIInstance::Flushed), 100);
}

void PPAPIInstance2D::BuildContext(int32_t result, const pp::Size& new_size) {
  printf("Building context.\n");

  size_ = new_size;
  device_context_ = pp::Graphics2D(this, size_ ,true);
  printf("Got Context!\n");

  is_context_bound_ = BindGraphics(device_context_);
  printf("Context is bound=%d\n", is_context_bound_);

  if (is_context_bound_) {
    PPAPIBuildContext(size_.width(), size_.height());
    device_context_.Flush(callback_factory_.NewCallback(
                          &PPAPIInstance::Flushed));
  } else {
    fprintf(stderr, "Failed to bind context for %dx%d.\n", size_.width(),
            size_.height());
  }
}

// The default implementation calls the 'C' render function.
void PPAPIInstance2D::Render(PP_Resource ctx, uint32_t width,
                             uint32_t height) {
  PPAPIRender(ctx, width, height);
}

PPAPIInstance2D::PPAPIInstance2D(PP_Instance instance, const char *args[])
    : PPAPIInstance(instance, args) {
}


PPAPIInstance2D::~PPAPIInstance2D() {
  if (is_context_bound_) {
    is_context_bound_ = false;
    // Cleanup code?
  }
}