BreakHandler.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright 2016 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "tools/skiaserve/urlhandlers/UrlHandler.h"
  8. #include "microhttpd.h"
  9. #include "tools/skiaserve/Request.h"
  10. #include "tools/skiaserve/Response.h"
  11. using namespace Response;
  12. bool BreakHandler::canHandle(const char* method, const char* url) {
  13. static const char* kBasePath = "/break";
  14. return 0 == strcmp(method, MHD_HTTP_METHOD_GET) &&
  15. 0 == strncmp(url, kBasePath, strlen(kBasePath));
  16. }
  17. int BreakHandler::handle(Request* request, MHD_Connection* connection,
  18. const char* url, const char* method,
  19. const char* upload_data, size_t* upload_data_size) {
  20. SkTArray<SkString> commands;
  21. SkStrSplit(url, "/", &commands);
  22. if (!request->hasPicture() || commands.count() != 4) {
  23. return MHD_NO;
  24. }
  25. // /break/<n>/<x>/<y>
  26. int n;
  27. sscanf(commands[1].c_str(), "%d", &n);
  28. int x;
  29. sscanf(commands[2].c_str(), "%d", &x);
  30. int y;
  31. sscanf(commands[3].c_str(), "%d", &y);
  32. int count = request->fDebugCanvas->getSize();
  33. SkASSERT(n < count);
  34. SkCanvas* canvas = request->getCanvas();
  35. canvas->clear(SK_ColorWHITE);
  36. int saveCount = canvas->save();
  37. for (int i = 0; i <= n; ++i) {
  38. request->fDebugCanvas->getDrawCommandAt(i)->execute(canvas);
  39. }
  40. SkColor target = request->getPixel(x, y);
  41. SkDynamicMemoryWStream stream;
  42. SkJSONWriter writer(&stream, SkJSONWriter::Mode::kFast);
  43. writer.beginObject(); // root
  44. writer.appendName("startColor");
  45. DrawCommand::MakeJsonColor(writer, target);
  46. bool changed = false;
  47. for (int i = n + 1; i < n + count; ++i) {
  48. int index = i % count;
  49. if (index == 0) {
  50. // reset canvas for wraparound
  51. canvas->restoreToCount(saveCount);
  52. canvas->clear(SK_ColorWHITE);
  53. saveCount = canvas->save();
  54. }
  55. request->fDebugCanvas->getDrawCommandAt(index)->execute(canvas);
  56. SkColor current = request->getPixel(x, y);
  57. if (current != target) {
  58. writer.appendName("endColor");
  59. DrawCommand::MakeJsonColor(writer, current);
  60. writer.appendS32("endOp", index);
  61. changed = true;
  62. break;
  63. }
  64. }
  65. if (!changed) {
  66. writer.appendName("endColor");
  67. DrawCommand::MakeJsonColor(writer, target);
  68. writer.appendS32("endOp", n);
  69. }
  70. canvas->restoreToCount(saveCount);
  71. writer.endObject(); // root
  72. writer.flush();
  73. return SendData(connection, stream.detachAsData().get(), "application/json");
  74. }