aboutsummaryrefslogtreecommitdiffhomepage
path: root/experimental/DrawingBoard/SampleDrawingServer.cpp
blob: c79f8d3470f960f3d74cee193e30d09e2225d5c9 (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
#include "SampleCode.h"
#include "SkView.h"
#include "SkCanvas.h"
#include "SkGPipe.h"
#include "SkSockets.h"
#include "SkNetPipeController.h"
#include "SkCornerPathEffect.h"
#include "SkOSMenu.h"
#include <map>

/**
 * Drawing Server
 *
 * This simple drawing server can accept connections from multiple drawing 
 * clients simultaneously. It accumulates drawing data from each client each 
 * frame, stores it in the appropriate place, and then broadcasts incremental
 * changes back to all the clients. Each logical packet, meaning one brush
 * stoke in this case can be of two types, append and replace. Append types are
 * completed strokes ready to be stored in the fData queue and will no longer be
 * modified. Replace types are drawing operations that are still in progress on 
 * the client side, so they are appended to fBuffer. The location and size of 
 * the buffered data for each client is stored in a map and updated properly.
 * Each time a new replace drawing call is received from a client, its previous
 * buffered data is discarded.
 * Since the Server keeps all the complete drawing data and the latest buffered
 * data, it's able to switch between vector and bitmap drawing
 */

class DrawingServerView : public SampleView {
public:
	DrawingServerView(){
        fServer = new SkTCPServer(40000);
        fServer->suspendWrite();
        fTotalBytesRead = fTotalBytesWritten = 0;
        fVector = true;
    }
    ~DrawingServerView() {
        delete fServer;
        fData.reset();
        fBuffer.reset();
        fClientMap.clear();
    }
    
    virtual void requestMenu(SkOSMenu* menu) {
        menu->setTitle("Drawing Server");
        menu->appendAction("Clear", this->getSinkID());
        menu->appendSwitch("Vector", "Vector", this->getSinkID(), fVector);
    }
    
protected:
    static void readData(int cid, const void* data, size_t size, 
                         SkSocket::DataType type, void* context) {
        DrawingServerView* view = (DrawingServerView*)context;
        view->onRead(cid, data, size, type);
    }
    
    void onRead(int cid, const void* data, size_t size, SkSocket::DataType type) {
        if (NULL == data && size <= 0)
            return;
        
        ClientState* cs;
        std::map<int, ClientState*>::iterator it = fClientMap.find(cid);
        if (it == fClientMap.end()) { //New client
            cs = new ClientState;
            cs->bufferBase = 0;
            cs->bufferSize = 0;
            fClientMap[cid] = cs;
        }
        else {
            cs = it->second;
        }
        
        if (type == SkSocket::kPipeReplace_type) {
            fBuffer.remove(cs->bufferBase, cs->bufferSize);
            
            for (it = fClientMap.begin(); it != fClientMap.end(); ++it) {
                if (cid == it->first)
                    continue;
                else {
                    if (it->second->bufferBase > cs->bufferBase) {
                        it->second->bufferBase -= cs->bufferSize;
                        SkASSERT(it->second->bufferBase >= 0);
                    }
                }
            }
            
            cs->bufferBase = fBuffer.count();
            cs->bufferSize = size;
            fBuffer.append(size, (const char*)data);
        }
        else if (type == SkSocket::kPipeAppend_type) {
            fData.append(size, (const char*)data);
            fServer->resumeWrite();
            fServer->writePacket(fData.begin() + fTotalBytesWritten,
                                 fData.count() - fTotalBytesWritten,
                                 SkSocket::kPipeAppend_type);
            fTotalBytesWritten = fData.count();
            fServer->suspendWrite();
        }
        else {
            //other types of data
        }
    }
    
    bool onQuery(SkEvent* evt) {
        if (SampleCode::TitleQ(*evt)) {
            SampleCode::TitleR(evt, "Drawing Server");
            return true;
        }
        return this->INHERITED::onQuery(evt);
    }
    
    bool onEvent(const SkEvent& evt) {
        if (SkOSMenu::FindAction(evt, "Clear")) {
            this->clear();
            return true;
        }
        if (SkOSMenu::FindSwitchState(evt, "Vector", &fVector)) {
            this->clearBitmap();
            return true;
        }
        return this->INHERITED::onEvent(evt);
    }
    
    
    virtual void onDrawContent(SkCanvas* canvas) {
        if (fCurrMatrix != canvas->getTotalMatrix()) {
            fTotalBytesRead = 0;
            fCurrMatrix = canvas->getTotalMatrix();
        }

        fServer->acceptConnections();
        if (fServer->readPacket(readData, this) > 0) {
            fServer->resumeWrite();
        }
        else {
            fServer->suspendWrite();
        }
        
        size_t bytesRead;
        SkGPipeReader::Status stat;
        SkCanvas bufferCanvas(fBase);
        SkCanvas* tempCanvas;
        while (fTotalBytesRead < fData.count()) {
            if (fVector) {
                tempCanvas = canvas;
            } else {
                tempCanvas = &bufferCanvas;
            }
            SkGPipeReader reader(tempCanvas);
            stat = reader.playback(fData.begin() + fTotalBytesRead,
                                   fData.count() - fTotalBytesRead,
                                   &bytesRead);
            SkASSERT(SkGPipeReader::kError_Status != stat);
            fTotalBytesRead += bytesRead;
        }
        if (fVector) {
            fTotalBytesRead = 0;
        } else {
            canvas->drawBitmap(fBase, 0, 0, NULL);
        }
        
        size_t totalBytesRead = 0;
        while (totalBytesRead < fBuffer.count()) {
            SkGPipeReader reader(canvas);
            stat = reader.playback(fBuffer.begin() + totalBytesRead,
                                   fBuffer.count() - totalBytesRead,
                                   &bytesRead);
            SkASSERT(SkGPipeReader::kError_Status != stat);
            totalBytesRead += bytesRead;
        }
        
        fServer->writePacket(fBuffer.begin(), fBuffer.count(), 
                             SkSocket::kPipeReplace_type);

        this->inval(NULL);
    }
    
    virtual void onSizeChange() {
        this->INHERITED::onSizeChange();
        fBase.setConfig(SkBitmap::kARGB_8888_Config, 
                        this->width(), 
                        this->height());
        fBase.allocPixels(NULL);
        this->clearBitmap();
    }
    
private:
    void clear() {
        fData.reset();
        fBuffer.reset();
        fTotalBytesRead = fTotalBytesWritten = 0;
        fClientMap.clear();
        this->clearBitmap();
    }
    void clearBitmap() {
        fTotalBytesRead = 0;
        fBase.eraseColor(fBGColor);
    }
    
    struct ClientState {
        int bufferBase;
        int bufferSize;
    };
    
    std::map<int, ClientState*> fClientMap;
    SkTDArray<char>             fData;
    SkTDArray<char>             fBuffer;
    size_t                      fTotalBytesRead;
    size_t                      fTotalBytesWritten;
    SkMatrix                    fCurrMatrix;
    SkBitmap                    fBase;
    bool                        fVector;
    SkTCPServer*                fServer;
    typedef SampleView INHERITED;
};


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

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