| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "GrCaps.h" | 8 #include "GrCaps.h" |
| 9 #include "GrContextFactory.h" | 9 #include "GrContextFactory.h" |
| 10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
| 11 #include "SkCommandLineFlags.h" | 11 #include "SkCommandLineFlags.h" |
| 12 #include "SkDebugCanvas.h" | 12 #include "SkDebugCanvas.h" |
| 13 #include "SkJSONCanvas.h" | 13 #include "SkJSONCanvas.h" |
| 14 #include "SkJSONCPP.h" |
| 14 #include "SkPicture.h" | 15 #include "SkPicture.h" |
| 15 #include "SkPictureRecorder.h" | 16 #include "SkPictureRecorder.h" |
| 16 #include "SkPixelSerializer.h" | 17 #include "SkPixelSerializer.h" |
| 17 #include "SkStream.h" | 18 #include "SkStream.h" |
| 18 #include "SkSurface.h" | 19 #include "SkSurface.h" |
| 19 | 20 |
| 20 #include <sys/socket.h> | 21 #include <sys/socket.h> |
| 21 #include <microhttpd.h> | 22 #include <microhttpd.h> |
| 22 | 23 |
| 23 // To get image decoders linked in we have to do the below magic | 24 // To get image decoders linked in we have to do the below magic |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 GrContextOptions grContextOpts; | 111 GrContextOptions grContextOpts; |
| 111 SkAutoTDelete<GrContextFactory> factory(new GrContextFactory(grContextOpts))
; | 112 SkAutoTDelete<GrContextFactory> factory(new GrContextFactory(grContextOpts))
; |
| 112 SkAutoTUnref<SkSurface> surface(setupSurface(factory.get())); | 113 SkAutoTUnref<SkSurface> surface(setupSurface(factory.get())); |
| 113 | 114 |
| 114 SkASSERT(debugCanvas); | 115 SkASSERT(debugCanvas); |
| 115 SkCanvas* canvas = surface->getCanvas(); | 116 SkCanvas* canvas = surface->getCanvas(); |
| 116 debugCanvas->drawTo(canvas, n); | 117 debugCanvas->drawTo(canvas, n); |
| 117 return writeCanvasToPng(canvas); | 118 return writeCanvasToPng(canvas); |
| 118 } | 119 } |
| 119 | 120 |
| 121 SkSurface* setupCpuSurface() { |
| 122 SkImageInfo info = SkImageInfo::Make(kImageWidth, kImageHeight, kN32_SkColor
Type, |
| 123 kPremul_SkAlphaType); |
| 124 return SkSurface::NewRaster(info); |
| 125 } |
| 126 |
| 120 static const size_t kBufferSize = 1024; | 127 static const size_t kBufferSize = 1024; |
| 121 | 128 |
| 122 static int process_upload_data(void* cls, enum MHD_ValueKind kind, | 129 static int process_upload_data(void* cls, enum MHD_ValueKind kind, |
| 123 const char* key, const char* filename, | 130 const char* key, const char* filename, |
| 124 const char* content_type, const char* transfer_en
coding, | 131 const char* content_type, const char* transfer_en
coding, |
| 125 const char* data, uint64_t off, size_t size) { | 132 const char* data, uint64_t off, size_t size) { |
| 126 struct UploadContext* uc = reinterpret_cast<UploadContext*>(cls); | 133 struct UploadContext* uc = reinterpret_cast<UploadContext*>(cls); |
| 127 | 134 |
| 128 if (0 != size) { | 135 if (0 != size) { |
| 129 uc->fStream.write(data, size); | 136 uc->fStream.write(data, size); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 | 188 |
| 182 class UrlHandler { | 189 class UrlHandler { |
| 183 public: | 190 public: |
| 184 virtual ~UrlHandler() {} | 191 virtual ~UrlHandler() {} |
| 185 virtual bool canHandle(const char* method, const char* url) = 0; | 192 virtual bool canHandle(const char* method, const char* url) = 0; |
| 186 virtual int handle(Request* request, MHD_Connection* connection, | 193 virtual int handle(Request* request, MHD_Connection* connection, |
| 187 const char* url, const char* method, | 194 const char* url, const char* method, |
| 188 const char* upload_data, size_t* upload_data_size) = 0; | 195 const char* upload_data, size_t* upload_data_size) = 0; |
| 189 }; | 196 }; |
| 190 | 197 |
| 191 class InfoHandler : public UrlHandler { | 198 class CmdHandler : public UrlHandler { |
| 192 public: | 199 public: |
| 193 bool canHandle(const char* method, const char* url) override { | 200 bool canHandle(const char* method, const char* url) override { |
| 194 const char* kBasePath = "/cmd"; | 201 const char* kBasePath = "/cmd"; |
| 195 return 0 == strncmp(url, kBasePath, strlen(kBasePath)); | 202 return 0 == strncmp(url, kBasePath, strlen(kBasePath)); |
| 196 } | 203 } |
| 197 | 204 |
| 198 int handle(Request* request, MHD_Connection* connection, | 205 int handle(Request* request, MHD_Connection* connection, |
| 199 const char* url, const char* method, | 206 const char* url, const char* method, |
| 200 const char* upload_data, size_t* upload_data_size) override { | 207 const char* upload_data, size_t* upload_data_size) override { |
| 201 SkTArray<SkString> commands; | 208 SkTArray<SkString> commands; |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 349 picture->serialize(&outStream, serializer); | 356 picture->serialize(&outStream, serializer); |
| 350 | 357 |
| 351 SkAutoTUnref<SkData> data(outStream.copyToData()); | 358 SkAutoTUnref<SkData> data(outStream.copyToData()); |
| 352 | 359 |
| 353 // TODO fancier name handling | 360 // TODO fancier name handling |
| 354 return SendData(connection, data, "application/octet-stream", true, | 361 return SendData(connection, data, "application/octet-stream", true, |
| 355 "attachment; filename=something.skp;"); | 362 "attachment; filename=something.skp;"); |
| 356 } | 363 } |
| 357 }; | 364 }; |
| 358 | 365 |
| 366 class InfoHandler : public UrlHandler { |
| 367 public: |
| 368 bool canHandle(const char* method, const char* url) override { |
| 369 const char* kBaseName = "/info"; |
| 370 return 0 == strcmp(method, MHD_HTTP_METHOD_GET) && |
| 371 0 == strncmp(url, kBaseName, strlen(kBaseName)); |
| 372 } |
| 373 |
| 374 int handle(Request* request, MHD_Connection* connection, |
| 375 const char* url, const char* method, |
| 376 const char* upload_data, size_t* upload_data_size) override { |
| 377 SkTArray<SkString> commands; |
| 378 SkStrSplit(url, "/", &commands); |
| 379 |
| 380 if (!request->fPicture.get() || commands.count() > 2) { |
| 381 return MHD_NO; |
| 382 } |
| 383 |
| 384 // drawTo |
| 385 SkAutoTUnref<SkSurface> surface(setupCpuSurface()); |
| 386 SkCanvas* canvas = surface->getCanvas(); |
| 387 |
| 388 int n; |
| 389 // /info or /info/N |
| 390 if (commands.count() == 1) { |
| 391 n = request->fDebugCanvas->getSize() - 1; |
| 392 } else { |
| 393 sscanf(commands[1].c_str(), "%d", &n); |
| 394 } |
| 395 |
| 396 // TODO this is really slow and we should cache the matrix and clip |
| 397 request->fDebugCanvas->drawTo(canvas, n); |
| 398 |
| 399 // make some json |
| 400 SkMatrix vm = request->fDebugCanvas->getCurrentMatrix(); |
| 401 SkIRect clip = request->fDebugCanvas->getCurrentClip(); |
| 402 Json::Value info(Json::objectValue); |
| 403 info["ViewMatrix"] = SkJSONCanvas::MakeMatrix(vm); |
| 404 info["ClipRect"] = SkJSONCanvas::MakeIRect(clip); |
| 405 |
| 406 std::string json = Json::FastWriter().write(info); |
| 407 |
| 408 // We don't want the null terminator so strlen is correct |
| 409 SkAutoTUnref<SkData> data(SkData::NewWithCopy(json.c_str(), strlen(json.
c_str()))); |
| 410 return SendData(connection, data, "application/json"); |
| 411 } |
| 412 }; |
| 413 |
| 414 |
| 359 class RootHandler : public UrlHandler { | 415 class RootHandler : public UrlHandler { |
| 360 public: | 416 public: |
| 361 bool canHandle(const char* method, const char* url) override { | 417 bool canHandle(const char* method, const char* url) override { |
| 362 return 0 == strcmp(method, MHD_HTTP_METHOD_GET) && | 418 return 0 == strcmp(method, MHD_HTTP_METHOD_GET) && |
| 363 0 == strcmp(url, "/"); | 419 0 == strcmp(url, "/"); |
| 364 } | 420 } |
| 365 | 421 |
| 366 int handle(Request* request, MHD_Connection* connection, | 422 int handle(Request* request, MHD_Connection* connection, |
| 367 const char* url, const char* method, | 423 const char* url, const char* method, |
| 368 const char* upload_data, size_t* upload_data_size) override { | 424 const char* upload_data, size_t* upload_data_size) override { |
| 369 return SendTemplate(connection); | 425 return SendTemplate(connection); |
| 370 } | 426 } |
| 371 }; | 427 }; |
| 372 | 428 |
| 373 class UrlManager { | 429 class UrlManager { |
| 374 public: | 430 public: |
| 375 UrlManager() { | 431 UrlManager() { |
| 376 // Register handlers | 432 // Register handlers |
| 377 fHandlers.push_back(new RootHandler); | 433 fHandlers.push_back(new RootHandler); |
| 378 fHandlers.push_back(new PostHandler); | 434 fHandlers.push_back(new PostHandler); |
| 379 fHandlers.push_back(new ImgHandler); | 435 fHandlers.push_back(new ImgHandler); |
| 436 fHandlers.push_back(new CmdHandler); |
| 380 fHandlers.push_back(new InfoHandler); | 437 fHandlers.push_back(new InfoHandler); |
| 381 fHandlers.push_back(new DownloadHandler); | 438 fHandlers.push_back(new DownloadHandler); |
| 382 } | 439 } |
| 383 | 440 |
| 384 ~UrlManager() { | 441 ~UrlManager() { |
| 385 for (int i = 0; i < fHandlers.count(); i++) { delete fHandlers[i]; } | 442 for (int i = 0; i < fHandlers.count(); i++) { delete fHandlers[i]; } |
| 386 } | 443 } |
| 387 | 444 |
| 388 // This is clearly not efficient for a large number of urls and handlers | 445 // This is clearly not efficient for a large number of urls and handlers |
| 389 int invoke(Request* request, MHD_Connection* connection, const char* url, co
nst char* method, | 446 int invoke(Request* request, MHD_Connection* connection, const char* url, co
nst char* method, |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 433 MHD_stop_daemon(daemon); | 490 MHD_stop_daemon(daemon); |
| 434 return 0; | 491 return 0; |
| 435 } | 492 } |
| 436 | 493 |
| 437 #if !defined SK_BUILD_FOR_IOS | 494 #if !defined SK_BUILD_FOR_IOS |
| 438 int main(int argc, char** argv) { | 495 int main(int argc, char** argv) { |
| 439 SkCommandLineFlags::Parse(argc, argv); | 496 SkCommandLineFlags::Parse(argc, argv); |
| 440 return skiaserve_main(); | 497 return skiaserve_main(); |
| 441 } | 498 } |
| 442 #endif | 499 #endif |
| OLD | NEW |