blob: 6542b588915d657112396209a3a1e2a74356360a [file] [log] [blame]
yangsu@google.comf3493f02011-08-08 15:12:05 +00001#include "SampleCode.h"
2#include "SkView.h"
3#include "SkCanvas.h"
4#include "SkGPipe.h"
5#include "SkSockets.h"
6#include "SkNetPipeController.h"
7#include "SkCornerPathEffect.h"
8#include "SkOSMenu.h"
9#include <map>
10class DrawingServerView : public SampleView {
11public:
12 DrawingServerView(){
13 fServer = new SkTCPServer(40000);
14 fServer->suspendWrite();
15 fTotalBytesRead = fTotalBytesWritten = 0;
16 fVector = true;
17 }
18 ~DrawingServerView() {
19 delete fServer;
20 fData.reset();
21 fBuffer.reset();
22 fClientMap.clear();
23 }
24
25 virtual void requestMenu(SkOSMenu* menu) {
26 menu->setTitle("Drawing Server");
27 menu->appendAction("Clear", this->getSinkID());
28 menu->appendSwitch("Vector", "Vector", this->getSinkID(), fVector);
29 }
30
31protected:
32 static void readData(int cid, const void* data, size_t size,
33 SkSocket::DataType type, void* context) {
34 DrawingServerView* view = (DrawingServerView*)context;
35 view->onRead(cid, data, size, type);
36 }
37
38 void onRead(int cid, const void* data, size_t size, SkSocket::DataType type) {
39 if (NULL == data && size <= 0)
40 return;
41
42 ClientState* cs;
43 std::map<int, ClientState*>::iterator it = fClientMap.find(cid);
44 if (it == fClientMap.end()) { //New client
45 cs = new ClientState;
46 cs->bufferBase = 0;
47 cs->bufferSize = 0;
48 fClientMap[cid] = cs;
49 }
50 else {
51 cs = it->second;
52 }
53
54 if (type == SkSocket::kPipeReplace_type) {
55 fBuffer.remove(cs->bufferBase, cs->bufferSize);
56
57 for (it = fClientMap.begin(); it != fClientMap.end(); ++it) {
58 if (cid == it->first)
59 continue;
60 else {
61 if (it->second->bufferBase > cs->bufferBase) {
62 it->second->bufferBase -= cs->bufferSize;
63 SkASSERT(it->second->bufferBase >= 0);
64 }
65 }
66 }
67
68 cs->bufferBase = fBuffer.count();
69 cs->bufferSize = size;
70 fBuffer.append(size, (const char*)data);
71 }
72 else if (type == SkSocket::kPipeAppend_type) {
73 fData.append(size, (const char*)data);
74 fServer->resumeWrite();
75 fServer->writePacket(fData.begin() + fTotalBytesWritten,
76 fData.count() - fTotalBytesWritten,
77 SkSocket::kPipeAppend_type);
78 fTotalBytesWritten = fData.count();
79 fServer->suspendWrite();
80 //this->clearBitmap();
81 }
82 else {
83 //other types of data
84 }
85 }
86
87 // overrides from SkEventSink
88 bool onQuery(SkEvent* evt) {
89 if (SampleCode::TitleQ(*evt)) {
90 SampleCode::TitleR(evt, "Drawing Server");
91 return true;
92 }
93 return this->INHERITED::onQuery(evt);
94 }
95
96 bool onEvent(const SkEvent& evt) {
97 if (SkOSMenu::FindAction(&evt, "Clear")) {
98 this->clear();
99 return true;
100 }
101 if (SkOSMenu::FindSwitchState(&evt, "Vector", &fVector)) {
102 this->clearBitmap();
103 return true;
104 }
105 return this->INHERITED::onEvent(evt);
106 }
107
108
109 virtual void onDrawContent(SkCanvas* canvas) {
110 if (fCurrMatrix != canvas->getTotalMatrix()) {
111 fTotalBytesRead = 0;
112 fCurrMatrix = canvas->getTotalMatrix();
113 }
114
115 fServer->acceptConnections();
116 if (fServer->readPacket(readData, this) > 0) {
117 fServer->resumeWrite();
118 }
119 else {
120 fServer->suspendWrite();
121 }
122
123 size_t bytesRead;
124 SkCanvas bufferCanvas(fBase);
125 SkCanvas* tempCanvas;
126 while (fTotalBytesRead < fData.count()) {
127 if (fVector)
128 tempCanvas = canvas;
129 else
130 tempCanvas = &bufferCanvas;
131 SkGPipeReader reader(tempCanvas);
132 SkGPipeReader::Status stat = reader.playback(fData.begin() + fTotalBytesRead,
133 fData.count() - fTotalBytesRead,
134 &bytesRead);
135 SkASSERT(SkGPipeReader::kError_Status != stat);
136 fTotalBytesRead += bytesRead;
137
138 if (SkGPipeReader::kDone_Status == stat) {}
139 }
140 if (fVector)
141 fTotalBytesRead = 0;
142 else
143 canvas->drawBitmap(fBase, 0, 0, NULL);
144
145 size_t totalBytesRead = 0;
146 while (totalBytesRead < fBuffer.count()) {
147 SkGPipeReader reader(canvas);
148 reader.playback(fBuffer.begin() + totalBytesRead,
149 fBuffer.count() - totalBytesRead,
150 &bytesRead);
151 totalBytesRead += bytesRead;
152 }
153
154 fServer->writePacket(fBuffer.begin(), fBuffer.count(),
155 SkSocket::kPipeReplace_type);
156
157 this->inval(NULL);
158 }
159
160 virtual void onSizeChange() {
161 this->INHERITED::onSizeChange();
162 fBase.setConfig(SkBitmap::kARGB_8888_Config, this->width(), this->height());
163 fBase.allocPixels(NULL);
164 this->clearBitmap();
165 }
166
167private:
168 void clear() {
169 fData.reset();
170 fBuffer.reset();
171 fTotalBytesRead = fTotalBytesWritten = 0;
172 fClientMap.clear();
173 this->clearBitmap();
174 }
175 void clearBitmap() {
176 fTotalBytesRead = 0;
177 fBase.eraseColor(fBGColor);
178 }
179
180 struct ClientState {
181 int bufferBase;
182 int bufferSize;
183 };
184 std::map<int, ClientState*> fClientMap;
185 SkTDArray<char> fData;
186 SkTDArray<char> fBuffer;
187 size_t fTotalBytesRead;
188 size_t fTotalBytesWritten;
189 SkMatrix fCurrMatrix;
190 SkBitmap fBase;
191 bool fVector;
192 SkTCPServer* fServer;
193 typedef SampleView INHERITED;
194};
195
196
197///////////////////////////////////////////////////////////////////////////////
198
199static SkView* MyFactory() { return new DrawingServerView; }
200static SkViewRegister reg(MyFactory);