OLD | NEW |
1 #include <cstdio> | 1 #include <cstdio> |
2 #include <string> | 2 #include <string> |
3 | 3 |
4 #include "ppapi/cpp/completion_callback.h" | 4 #include "ppapi/cpp/completion_callback.h" |
5 #include "ppapi/cpp/graphics_2d.h" | 5 #include "ppapi/cpp/graphics_2d.h" |
6 #include "ppapi/cpp/image_data.h" | 6 #include "ppapi/cpp/image_data.h" |
7 #include "ppapi/cpp/instance.h" | 7 #include "ppapi/cpp/instance.h" |
8 #include "ppapi/cpp/module.h" | 8 #include "ppapi/cpp/module.h" |
| 9 #include "ppapi/cpp/point.h" |
| 10 #include "ppapi/cpp/rect.h" |
9 #include "ppapi/cpp/var.h" | 11 #include "ppapi/cpp/var.h" |
10 | 12 |
| 13 #include "SkBase64.h" |
| 14 #include "SkBitmap.h" |
11 #include "SkCanvas.h" | 15 #include "SkCanvas.h" |
12 #include "SkBitmap.h" | 16 #include "SkColor.h" |
| 17 #include "SkDebugger.h" |
| 18 #include "SkGraphics.h" |
| 19 #include "SkStream.h" |
13 #include "SkString.h" | 20 #include "SkString.h" |
14 #include "SkThreadUtils.h" | |
15 | 21 |
16 class SkiaInstance; | 22 class SkiaInstance; |
17 | 23 |
18 // Used by SkDebugf | 24 // Used by SkDebugf |
19 SkiaInstance* gPluginInstance; | 25 SkiaInstance* gPluginInstance; |
20 | 26 |
21 // Main entry point for the app we're linked into | 27 void FlushCallback(void* data, int32_t result); |
22 extern int tool_main(int, char**); | |
23 | |
24 // Tokenize a command line and store it in argc and argv. | |
25 void SkStringToProgramArgs(const SkString commandLine, int* argc, char*** argv)
{ | |
26 int numBreaks = 0; | |
27 const char* commandChars = commandLine.c_str(); | |
28 for (int i = 0; i < strlen(commandChars); i++) { | |
29 if (isspace(commandChars[i])) { | |
30 numBreaks++; | |
31 } | |
32 } | |
33 int numArgs; | |
34 if (strlen(commandChars) > 0) { | |
35 numArgs = numBreaks + 1; | |
36 } else { | |
37 numArgs = 0; | |
38 } | |
39 *argc = numArgs; | |
40 *argv = new char*[numArgs + 1]; | |
41 (*argv)[numArgs] = NULL; | |
42 char* start = (char*) commandChars; | |
43 int length = 0; | |
44 int argIndex = 0; | |
45 for (int i = 0; i < strlen(commandChars) + 1; i++) { | |
46 if (isspace(commandChars[i]) || '\0' == commandChars[i]) { | |
47 if (length > 0) { | |
48 char* argument = new char[length + 1]; | |
49 memcpy(argument, start, length); | |
50 argument[length] = '\0'; | |
51 (*argv)[argIndex++] = argument; | |
52 } | |
53 start = (char*) commandChars + i + 1; | |
54 length = 0; | |
55 } else { | |
56 length++; | |
57 } | |
58 } | |
59 } | |
60 | |
61 // Run the program with the given command line. | |
62 void RunProgram(const SkString& commandLine) { | |
63 int argc; | |
64 char** argv; | |
65 SkStringToProgramArgs(commandLine, &argc, &argv); | |
66 tool_main(argc, argv); | |
67 } | |
68 | |
69 | 28 |
70 // Skia's subclass of pp::Instance, our interface with the browser. | 29 // Skia's subclass of pp::Instance, our interface with the browser. |
71 class SkiaInstance : public pp::Instance { | 30 class SkiaInstance : public pp::Instance { |
72 public: | 31 public: |
73 explicit SkiaInstance(PP_Instance instance) : pp::Instance(instance) { | 32 explicit SkiaInstance(PP_Instance instance) |
| 33 : pp::Instance(instance) |
| 34 , fBitmap() |
| 35 , fCanvas(NULL) |
| 36 , fDebugger() |
| 37 , fImage() |
| 38 , fFlushLoopRunning(false) |
| 39 , fFlushPending(false) |
| 40 , fPicture(NULL) |
| 41 { |
74 gPluginInstance = this; | 42 gPluginInstance = this; |
| 43 SkGraphics::Init(); |
75 } | 44 } |
76 | 45 |
77 virtual ~SkiaInstance() { | 46 virtual ~SkiaInstance() { |
| 47 SkGraphics::Term(); |
78 gPluginInstance = NULL; | 48 gPluginInstance = NULL; |
79 } | 49 } |
80 | 50 |
81 virtual void HandleMessage(const pp::Var& var_message) { | 51 virtual void HandleMessage(const pp::Var& var_message) { |
82 // Receive a message from javascript. | 52 // Receive a message from javascript. |
83 if (var_message.is_string()) { | 53 if (var_message.is_string()) { |
84 SkString msg(var_message.AsString().c_str()); | 54 SkString msg(var_message.AsString().c_str()); |
85 if (msg.startsWith("init")) { | 55 if (msg.startsWith("init")) { |
86 RunProgram(msg); | 56 } else if (msg.startsWith("LoadSKP")) { |
| 57 size_t startIndex = strlen("LoadSKP"); |
| 58 size_t dataSize = msg.size()/sizeof(char) - startIndex; |
| 59 SkBase64 decodedData; |
| 60 decodedData.decode(msg.c_str() + startIndex, dataSize); |
| 61 size_t decodedSize = 3 * (dataSize / 4); |
| 62 SkDebugf("Got size: %d\n", decodedSize); |
| 63 if (!decodedData.getData()) { |
| 64 SkDebugf("Failed to decode SKP\n"); |
| 65 return; |
| 66 } |
| 67 SkMemoryStream pictureStream(decodedData.getData(), decodedSize)
; |
| 68 fPicture = new SkPicture(&pictureStream); |
| 69 if (fPicture->width() == 0 || fPicture->height() == 0) { |
| 70 SkDebugf("Failed to create SKP.\n"); |
| 71 return; |
| 72 } |
| 73 fDebugger.loadPicture(fPicture); |
| 74 SkTArray<SkString>* commands = fDebugger.getDrawCommandsAsString
s(); |
| 75 PostMessage("ClearCommands"); |
| 76 for (int i = 0; i < commands->count(); ++i) { |
| 77 SkString message("AddCommand:"); |
| 78 message.append((*commands)[i]); |
| 79 PostMessage(message.c_str()); |
| 80 } |
| 81 if (!fFlushLoopRunning) { |
| 82 Paint(); |
| 83 } |
| 84 } else if (msg.startsWith("CommandSelected:")) { |
| 85 size_t startIndex = strlen("CommandSelected:"); |
| 86 int index = atoi(msg.c_str() + startIndex); |
| 87 fDebugger.setIndex(index); |
| 88 if (!fFlushLoopRunning) { |
| 89 Paint(); |
| 90 } |
| 91 } else if (msg.startsWith("Rewind")) { |
| 92 fCanvas->clear(SK_ColorWHITE); |
| 93 fDebugger.setIndex(0); |
| 94 if (!fFlushLoopRunning) { |
| 95 Paint(); |
| 96 } |
| 97 } else if (msg.startsWith("StepBack")) { |
| 98 fCanvas->clear(SK_ColorWHITE); |
| 99 int currentIndex = fDebugger.index(); |
| 100 if (currentIndex > 1) { |
| 101 fDebugger.setIndex(currentIndex - 1); |
| 102 if (!fFlushLoopRunning) { |
| 103 Paint(); |
| 104 } |
| 105 } |
| 106 } else if (msg.startsWith("Pause")) { |
| 107 // TODO(borenet) |
| 108 } else if (msg.startsWith("StepForward")) { |
| 109 int currentIndex = fDebugger.index(); |
| 110 if (currentIndex < fDebugger.getSize() -1) { |
| 111 fDebugger.setIndex(currentIndex + 1); |
| 112 if (!fFlushLoopRunning) { |
| 113 Paint(); |
| 114 } |
| 115 } |
| 116 } else if (msg.startsWith("Play")) { |
| 117 fDebugger.setIndex(fDebugger.getSize() - 1); |
| 118 if (!fFlushLoopRunning) { |
| 119 Paint(); |
| 120 } |
87 } | 121 } |
88 } | 122 } |
89 } | 123 } |
| 124 |
| 125 void Paint() { |
| 126 if (!fImage.is_null()) { |
| 127 fDebugger.draw(fCanvas); |
| 128 fDeviceContext.PaintImageData(fImage, pp::Point(0, 0)); |
| 129 if (!fFlushPending) { |
| 130 fFlushPending = true; |
| 131 fDeviceContext.Flush(pp::CompletionCallback(&FlushCallback, this
)); |
| 132 } else { |
| 133 SkDebugf("A flush is pending... Skipping flush.\n"); |
| 134 } |
| 135 } else { |
| 136 SkDebugf("No pixels to write to!\n"); |
| 137 } |
| 138 } |
| 139 |
| 140 virtual void DidChangeView(const pp::Rect& position, const pp::Rect& clip) { |
| 141 if (position.size().width() == fWidth && |
| 142 position.size().height() == fHeight) { |
| 143 return; // We don't care about the position, only the size. |
| 144 } |
| 145 fWidth = position.size().width(); |
| 146 fHeight = position.size().height(); |
| 147 |
| 148 fDeviceContext = pp::Graphics2D(this, pp::Size(fWidth, fHeight), false); |
| 149 if (!BindGraphics(fDeviceContext)) { |
| 150 SkDebugf("Couldn't bind the device context\n"); |
| 151 return; |
| 152 } |
| 153 fImage = pp::ImageData(this, |
| 154 PP_IMAGEDATAFORMAT_BGRA_PREMUL, |
| 155 pp::Size(fWidth, fHeight), false); |
| 156 fBitmap.setConfig(SkBitmap::kARGB_8888_Config, fWidth, fHeight); |
| 157 fBitmap.setPixels(fImage.data()); |
| 158 if (fCanvas) { |
| 159 delete fCanvas; |
| 160 } |
| 161 fCanvas = new SkCanvas(fBitmap); |
| 162 fCanvas->clear(SK_ColorWHITE); |
| 163 if (!fFlushLoopRunning) { |
| 164 Paint(); |
| 165 } |
| 166 } |
| 167 |
| 168 void OnFlush() { |
| 169 fFlushLoopRunning = true; |
| 170 fFlushPending = false; |
| 171 Paint(); |
| 172 } |
| 173 |
| 174 private: |
| 175 pp::Graphics2D fDeviceContext; |
| 176 pp::ImageData fImage; |
| 177 int fWidth; |
| 178 int fHeight; |
| 179 |
| 180 SkBitmap fBitmap; |
| 181 SkCanvas* fCanvas; |
| 182 SkDebugger fDebugger; |
| 183 SkPicture* fPicture; |
| 184 |
| 185 bool fFlushLoopRunning; |
| 186 bool fFlushPending; |
90 }; | 187 }; |
91 | 188 |
| 189 void FlushCallback(void* data, int32_t result) { |
| 190 static_cast<SkiaInstance*>(data)->OnFlush(); |
| 191 } |
| 192 |
92 class SkiaModule : public pp::Module { | 193 class SkiaModule : public pp::Module { |
93 public: | 194 public: |
94 SkiaModule() : pp::Module() {} | 195 SkiaModule() : pp::Module() {} |
95 virtual ~SkiaModule() {} | 196 virtual ~SkiaModule() {} |
96 | 197 |
97 virtual pp::Instance* CreateInstance(PP_Instance instance) { | 198 virtual pp::Instance* CreateInstance(PP_Instance instance) { |
98 return new SkiaInstance(instance); | 199 return new SkiaInstance(instance); |
99 } | 200 } |
100 }; | 201 }; |
101 | 202 |
102 namespace pp { | 203 namespace pp { |
103 Module* CreateModule() { | 204 Module* CreateModule() { |
104 return new SkiaModule(); | 205 return new SkiaModule(); |
105 } | 206 } |
106 } // namespace pp | 207 } // namespace pp |
107 | |
108 /////////////////////////////////////////// | |
109 ////////////// SkThread impl ////////////// | |
110 /////////////////////////////////////////// | |
111 | |
112 bool SkThread::setProcessorAffinity(unsigned int processor) { | |
113 return false; | |
114 } | |
OLD | NEW |