aboutsummaryrefslogtreecommitdiffhomepage
path: root/platform_tools/nacl/src/nacl_sample.cpp
blob: aca74f4247010378f63ef8cb9d29644edf281803 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <cstdio>
#include <string>

#include "ppapi/cpp/completion_callback.h"
#include "ppapi/cpp/graphics_2d.h"
#include "ppapi/cpp/image_data.h"
#include "ppapi/cpp/instance.h"
#include "ppapi/cpp/module.h"
#include "ppapi/cpp/var.h"

#include "SampleApp.h"
#include "SkApplication.h"
#include "SkCanvas.h"
#include "SkBitmap.h"
#include "SkEvent.h"
#include "SkWindow.h"

class SkiaInstance;

namespace {
void FlushCallback(void* data, int32_t result);
}

SkiaInstance* gPluginInstance;
extern int main(int, char**);

class SkiaInstance : public pp::Instance {
 public:
    explicit SkiaInstance(PP_Instance instance) : pp::Instance(instance),
                                                  fFlushPending(false),
                                                  fGraphics2dContext(NULL),
                                                  fPixelBuffer(NULL)
    {
        gPluginInstance = this;
        application_init();
        char* commandName = "SampleApp";
        fWindow = new SampleWindow(NULL, 0, &commandName, NULL);
    }

    virtual ~SkiaInstance() {
        gPluginInstance = NULL;
        delete fWindow;
        application_term();
    }

    virtual void HandleMessage(const pp::Var& var_message) {
        // Receive a message from javascript.  Right now this just signals us to
        // get started.
        uint32_t width = 500;
        uint32_t height = 500;
        char buffer[2048];
        sprintf(buffer, "SetSize:%d,%d", width, height);
        PostMessage(buffer);
    }

    virtual void DidChangeView(const pp::Rect& position,
                               const pp::Rect& clip) {
        if (position.size().width() == width() &&
            position.size().height() == height()) {
            return;  // Size didn't change, no need to update anything.
        }
        // Create a new device context with the new size.
        DestroyContext();
        CreateContext(position.size());
        // Delete the old pixel buffer and create a new one.
        delete fPixelBuffer;
        fPixelBuffer = NULL;
        if (fGraphics2dContext != NULL) {
            fPixelBuffer = new pp::ImageData(this,
                                              PP_IMAGEDATAFORMAT_BGRA_PREMUL,
                                              fGraphics2dContext->size(),
                                              false);
            fWindow->resize(position.size().width(), position.size().height());
            fWindow->update(NULL);
            paint();
        }
    }

    // Indicate whether a flush is pending.  This can only be called from the
    // main thread; it is not thread safe.
    bool flush_pending() const {
        return fFlushPending;
    }
    void set_flush_pending(bool flag) {
        fFlushPending = flag;
    }

  void paint() {
    if (fPixelBuffer) {
      // Draw some stuff.  TODO(borenet): Actually have SampleApp draw into
      // the plugin area.
      uint32_t w = fPixelBuffer->size().width();
      uint32_t h = fPixelBuffer->size().height();
      uint32_t* data = (uint32_t*) fPixelBuffer->data();
      // Create a bitmap using the fPixelBuffer pixels
      SkBitmap bitmap;
      bitmap.setConfig(SkBitmap::kARGB_8888_Config, w, h);
      bitmap.setPixels(data);
      // Create a canvas with the bitmap as the backend
      SkCanvas canvas(bitmap);

      canvas.drawColor(SK_ColorBLUE);
      SkRect rect = SkRect::MakeXYWH(10, 10, 80, 80);
      SkPaint rect_paint;
      rect_paint.setStyle(SkPaint::kFill_Style);
      rect_paint.setColor(SK_ColorRED);
      canvas.drawRect(rect, rect_paint);

      FlushPixelBuffer();
    }
  }

private:
    int width() const {
        return fPixelBuffer ? fPixelBuffer->size().width() : 0;
    }

    int height() const {
        return fPixelBuffer ? fPixelBuffer->size().height() : 0;
    }

    bool IsContextValid() const {
        return fGraphics2dContext != NULL;
    }

    void CreateContext(const pp::Size& size) {
        if (IsContextValid())
            return;
        fGraphics2dContext = new pp::Graphics2D(this, size, false);
        if (!BindGraphics(*fGraphics2dContext)) {
            SkDebugf("Couldn't bind the device context");
        }
    }

    void DestroyContext() {
        if (!IsContextValid())
            return;
        delete fGraphics2dContext;
        fGraphics2dContext = NULL;
    }

    void FlushPixelBuffer() {
        if (!IsContextValid())
            return;
        // Note that the pixel lock is held while the buffer is copied into the
        // device context and then flushed.
        fGraphics2dContext->PaintImageData(*fPixelBuffer, pp::Point());
        if (flush_pending())
            return;
        set_flush_pending(true);
        fGraphics2dContext->Flush(pp::CompletionCallback(&FlushCallback, this));
    }

    bool fFlushPending;
    pp::Graphics2D* fGraphics2dContext;
    pp::ImageData* fPixelBuffer;
    SampleWindow* fWindow;
};

class SkiaModule : public pp::Module {
public:
    SkiaModule() : pp::Module() {}
    virtual ~SkiaModule() {}

    virtual pp::Instance* CreateInstance(PP_Instance instance) {
        gPluginInstance = new SkiaInstance(instance);
        return gPluginInstance;
    }
};

namespace {
void FlushCallback(void* data, int32_t result) {
    static_cast<SkiaInstance*>(data)->set_flush_pending(false);
}
}

namespace pp {
Module* CreateModule() {
    return new SkiaModule();
}
}  // namespace pp


///////////////////////////////////////////
///////////// SkOSWindow impl /////////////
///////////////////////////////////////////

void SkOSWindow::onSetTitle(const char title[])
{
    char buffer[2048];
    sprintf(buffer, "SetTitle:%s", title);
    gPluginInstance->PostMessage(buffer);
}

void SkOSWindow::onHandleInval(const SkIRect& rect)
{
    gPluginInstance->paint();
}

void SkOSWindow::onPDFSaved(const char title[], const char desc[],
                            const char path[]) {
}

///////////////////////////////////////////
/////////////// SkEvent impl //////////////
///////////////////////////////////////////

void SkEvent::SignalQueueTimer(SkMSec ms) {
}

void SkEvent::SignalNonEmptyQueue() {
}