Chromium Code Reviews| 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 request->fDebugCanvas->drawTo(canvas, n); | |
|
jcgregorio
2016/02/03 19:59:53
Yeah, I think caching the matrix and clip for /inf
joshualitt
2016/02/04 13:41:37
Acknowledged.
| |
| 397 | |
| 398 // make some json | |
| 399 SkMatrix vm = request->fDebugCanvas->getCurrentMatrix(); | |
| 400 SkIRect clip = request->fDebugCanvas->getCurrentClip(); | |
| 401 Json::Value info(Json::objectValue); | |
| 402 info["ViewMatrix"] = SkJSONCanvas::MakeMatrix(vm); | |
| 403 info["ClipRect"] = SkJSONCanvas::MakeIRect(clip); | |
| 404 | |
| 405 SkAutoTUnref<SkData> data(SkData::NewWithCString(Json::FastWriter().writ e(info).c_str())); | |
| 406 return SendData(connection, data, "application/json"); | |
| 407 } | |
| 408 }; | |
| 409 | |
| 410 | |
| 359 class RootHandler : public UrlHandler { | 411 class RootHandler : public UrlHandler { |
| 360 public: | 412 public: |
| 361 bool canHandle(const char* method, const char* url) override { | 413 bool canHandle(const char* method, const char* url) override { |
| 362 return 0 == strcmp(method, MHD_HTTP_METHOD_GET) && | 414 return 0 == strcmp(method, MHD_HTTP_METHOD_GET) && |
| 363 0 == strcmp(url, "/"); | 415 0 == strcmp(url, "/"); |
| 364 } | 416 } |
| 365 | 417 |
| 366 int handle(Request* request, MHD_Connection* connection, | 418 int handle(Request* request, MHD_Connection* connection, |
| 367 const char* url, const char* method, | 419 const char* url, const char* method, |
| 368 const char* upload_data, size_t* upload_data_size) override { | 420 const char* upload_data, size_t* upload_data_size) override { |
| 369 return SendTemplate(connection); | 421 return SendTemplate(connection); |
| 370 } | 422 } |
| 371 }; | 423 }; |
| 372 | 424 |
| 373 class UrlManager { | 425 class UrlManager { |
| 374 public: | 426 public: |
| 375 UrlManager() { | 427 UrlManager() { |
| 376 // Register handlers | 428 // Register handlers |
| 377 fHandlers.push_back(new RootHandler); | 429 fHandlers.push_back(new RootHandler); |
| 378 fHandlers.push_back(new PostHandler); | 430 fHandlers.push_back(new PostHandler); |
| 379 fHandlers.push_back(new ImgHandler); | 431 fHandlers.push_back(new ImgHandler); |
| 432 fHandlers.push_back(new CmdHandler); | |
| 380 fHandlers.push_back(new InfoHandler); | 433 fHandlers.push_back(new InfoHandler); |
| 381 fHandlers.push_back(new DownloadHandler); | 434 fHandlers.push_back(new DownloadHandler); |
| 382 } | 435 } |
| 383 | 436 |
| 384 ~UrlManager() { | 437 ~UrlManager() { |
| 385 for (int i = 0; i < fHandlers.count(); i++) { delete fHandlers[i]; } | 438 for (int i = 0; i < fHandlers.count(); i++) { delete fHandlers[i]; } |
| 386 } | 439 } |
| 387 | 440 |
| 388 // This is clearly not efficient for a large number of urls and handlers | 441 // 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, | 442 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); | 486 MHD_stop_daemon(daemon); |
| 434 return 0; | 487 return 0; |
| 435 } | 488 } |
| 436 | 489 |
| 437 #if !defined SK_BUILD_FOR_IOS | 490 #if !defined SK_BUILD_FOR_IOS |
| 438 int main(int argc, char** argv) { | 491 int main(int argc, char** argv) { |
| 439 SkCommandLineFlags::Parse(argc, argv); | 492 SkCommandLineFlags::Parse(argc, argv); |
| 440 return skiaserve_main(); | 493 return skiaserve_main(); |
| 441 } | 494 } |
| 442 #endif | 495 #endif |
| OLD | NEW |