aboutsummaryrefslogtreecommitdiffhomepage
path: root/experimental/DrawingBoard/SampleDrawingClient.cpp
blob: b86c8c1473697a00f82340f7ed9639710811c63e (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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include "SampleCode.h"
#include "SkView.h"
#include "SkCanvas.h"
#include "SkGPipe.h"
#include "SkSockets.h"
#include "SkNetPipeController.h"
#include "SkCornerPathEffect.h"
#include "SkColorPalette.h"
#include "SkOSMenu.h"


/**
 * Drawing Client
 *
 * A drawing client that allows a user to perform simple brush stokes with
 * a selected color and brush size. The drawing client communicates with a
 * drawing server to send/receive data to/from other clients connected to the
 * same server. The drawing client stores data in fData and fBuffer depending on
 * the data type. Append type means that the drawing data is a completed stroke
 * and Replace type means that the drawing data is in progress and will be
 * replaced by subsequent data. fData and fBuffer are read by a pipe reader and
 * reproduce the drawing. When the client is in a normal state, the data stored
 * on the client and the server should be identical.
 * The drawing client is also able to switch between vector and bitmap drawing.
 * The drawing client also renders the latest drawing stroke locally in order to
 * produce better reponses. This can be disabled by calling
 * controller.disablePlayBack(), which will introduce a lag between the input
 * and the drawing.
 * Note: in order to keep up with the drawing data, the client will try to read
 * a few times each frame in case more than one frame worth of data has been
 * received and render them together. This behavior can be adjusted by tweaking
 * MAX_READ_PER_FRAME or disabled by turning fSync to false
 */

#define MAX_READ_PER_FRAME 5

class DrawingClientView : public SampleView {
public:
    DrawingClientView() {
        fSocket = NULL;
        fTotalBytesRead = 0;
        fPalette = new SkColorPalette;
        fPalette->setSize(100, 300);
        fPalette->setVisibleP(true);
        this->attachChildToFront(fPalette);
        fPalette->unref();
        fBrushSize = SkFloatToScalar(2.5);
        fAA = false;
        fPaletteVisible = true;
        fSync = true;
        fVector = true;
    }
    ~DrawingClientView() {
        if (fSocket) {
            delete fSocket;
        }
        fData.reset();
        fBuffer.reset();
    }

    virtual void requestMenu(SkOSMenu* menu) {
        menu->setTitle("Drawing Client");
        menu->appendTextField("Server IP", "Server IP", this->getSinkID(),
                              "IP address or hostname");
        menu->appendSwitch("Vector", "Vector", this->getSinkID(), fVector);
        menu->appendSlider("Brush Size", "Brush Size", this->getSinkID(), 1.0,
                           100.0, fBrushSize);
        menu->appendSwitch("Anti-Aliasing", "AA", this->getSinkID(), fAA);
        menu->appendSwitch("Show Color Palette", "Palette", this->getSinkID(),
                           fPaletteVisible);
        menu->appendSwitch("Sync", "Sync", this->getSinkID(), fSync);
        menu->appendAction("Clear", this->getSinkID());
    }

protected:

    static void readData(int cid, const void* data, size_t size,
                         SkSocket::DataType type, void* context) {
        DrawingClientView* view = (DrawingClientView*)context;
        view->onRead(cid, data, size, type);
    }

    void onRead(int cid, const void* data, size_t size, SkSocket::DataType type) {
        if (size > 0) {
            fBuffer.reset();
            if (type == SkSocket::kPipeReplace_type)
                fBuffer.append(size, (const char*)data);
            else if (type == SkSocket::kPipeAppend_type)
                fData.append(size, (const char*)data);
            else {
                //other types of data
            }
        }
    }

    bool onQuery(SkEvent* evt) {
        if (SampleCode::TitleQ(*evt)) {
            SampleCode::TitleR(evt, "Drawing Client");
            return true;
        }

        return this->INHERITED::onQuery(evt);
    }

    bool onEvent(const SkEvent& evt) {;
        if (SkOSMenu::FindSliderValue(evt, "Brush Size", &fBrushSize))
            return true;

        SkString s;
        if (SkOSMenu::FindText(evt, "Server IP", &s)) {
            if (NULL != fSocket) {
                delete fSocket;
            }
            fSocket = new SkTCPClient(s.c_str(), 40000);
            fSocket->connectToServer();
            fSocket->suspendWrite();
            SkDebugf("Connecting to %s\n", s.c_str());
            fData.reset();
            fBuffer.reset();
            this->inval(NULL);
            return true;
        }
        if (SkOSMenu::FindSwitchState(evt, "AA", &fAA) ||
            SkOSMenu::FindSwitchState(evt, "Sync", &fSync))
            return true;
        if (SkOSMenu::FindSwitchState(evt, "Vector", &fVector)) {
            this->clearBitmap();
            return true;
        }
        if (SkOSMenu::FindAction(evt, "Clear")) {
            this->clear();
            return true;
        }
        if (SkOSMenu::FindSwitchState(evt, "Palette", &fPaletteVisible)) {
            fPalette->setVisibleP(fPaletteVisible);
            return true;
        }
        return this->INHERITED::onEvent(evt);
    }

    virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) {
        return new Click(this);
    }

    virtual bool onClick(SkView::Click* click) {
        switch (click->fState) {
            case SkView::Click::kDown_State:
                fCurrLine.moveTo(click->fCurr);
                fType = SkSocket::kPipeReplace_type;
                if (fSocket)
                    fSocket->resumeWrite();
                break;
            case SkView::Click::kMoved_State:
                fCurrLine.lineTo(click->fCurr);
                break;
            case SkView::Click::kUp_State:
                fType = SkSocket::kPipeAppend_type;
                break;
            default:
                break;
        }
        return true;
    }

    virtual void onDrawContent(SkCanvas* canvas) {
        if (fSocket) {
            if (fSocket->isConnected()) {
                if (fSync) {
                    int count = 0;
                    while (fSocket->readPacket(readData, this) > 0 &&
                           count < MAX_READ_PER_FRAME)
                        ++count;
                }
                else
                    fSocket->readPacket(readData, this);
            }
            else
                fSocket->connectToServer();
        }
        size_t bytesRead = 0;
        SkGPipeReader::Status status;
        SkCanvas bufferCanvas(fBase);
        SkCanvas* tempCanvas;
        while (fTotalBytesRead < fData.count()) {
            if (fVector)
                tempCanvas = canvas;
            else
                tempCanvas = &bufferCanvas;
            SkGPipeReader reader(tempCanvas);
            status = reader.playback(fData.begin() + fTotalBytesRead,
                                     fData.count() - fTotalBytesRead,
                                     &bytesRead);
            SkASSERT(SkGPipeReader::kError_Status != status);
            fTotalBytesRead += bytesRead;
        }
        if (fVector)
            fTotalBytesRead = 0;
        else
            canvas->drawBitmap(fBase, 0, 0, NULL);

        size_t totalBytesRead = 0;
        while (totalBytesRead < fBuffer.count()) {
            SkGPipeReader reader(canvas);
            status = reader.playback(fBuffer.begin() + totalBytesRead,
                                     fBuffer.count() - totalBytesRead,
                                     &bytesRead);
            SkASSERT(SkGPipeReader::kError_Status != status);
            totalBytesRead += bytesRead;
        }

        SkNetPipeController controller(canvas);
        SkGPipeWriter writer;
        SkCanvas* writerCanvas = writer.startRecording(&controller,
                                                       SkGPipeWriter::kCrossProcess_Flag);

        //controller.disablePlayback();
        SkPaint p;
        p.setColor(fPalette->getColor());
        p.setStyle(SkPaint::kStroke_Style);
        p.setStrokeWidth(fBrushSize);
        p.setStrokeCap(SkPaint::kRound_Cap);
        p.setStrokeJoin(SkPaint::kRound_Join);
        p.setAntiAlias(fAA);
        p.setPathEffect(new SkCornerPathEffect(55))->unref();
        writerCanvas->drawPath(fCurrLine, p);
        writer.endRecording();

        controller.writeToSocket(fSocket, fType);
        if (fType == SkSocket::kPipeAppend_type && fSocket) {
            fSocket->suspendWrite();
            fCurrLine.reset();
        }

        this->inval(NULL);
    }

    virtual void onSizeChange() {
        this->INHERITED::onSizeChange();
        fPalette->setLoc(this->width()-100, 0);
        fBase.setConfig(SkBitmap::kARGB_8888_Config, this->width(), this->height());
        fBase.allocPixels(NULL);
        this->clearBitmap();
    }

private:
    void clear() {
        fData.reset();
        fBuffer.reset();
        fCurrLine.reset();
        fTotalBytesRead = 0;
        this->clearBitmap();
    }
    void clearBitmap() {
        fTotalBytesRead = 0;
        fBase.eraseColor(fBGColor);
    }
    SkTDArray<char>     fData;
    SkTDArray<char>     fBuffer;
    SkBitmap            fBase;
    SkPath              fCurrLine;
    SkTCPClient*        fSocket;
    SkSocket::DataType  fType;
    SkColorPalette*     fPalette;
    bool                fPaletteVisible;
    size_t              fTotalBytesRead;
    SkScalar            fBrushSize;
    bool                fAA;
    bool                fSync;
    bool                fVector;

    typedef SampleView INHERITED;
};


///////////////////////////////////////////////////////////////////////////////

static SkView* MyFactory() { return new DrawingClientView; }
static SkViewRegister reg(MyFactory);