Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 | 1 |
|
borenet
2013/06/17 13:47:48
"svn cp" from platform_tools/nacl/src/nacl_debugge
| |
| 2 /* | 2 /* |
| 3 * Copyright 2013 Google Inc. | 3 * Copyright 2013 Google Inc. |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 #include "ppapi/cpp/completion_callback.h" | 9 #include "ppapi/cpp/completion_callback.h" |
| 10 #include "ppapi/cpp/graphics_2d.h" | 10 #include "ppapi/cpp/graphics_2d.h" |
| 11 #include "ppapi/cpp/image_data.h" | 11 #include "ppapi/cpp/image_data.h" |
| 12 #include "ppapi/cpp/instance.h" | 12 #include "ppapi/cpp/instance.h" |
| 13 #include "ppapi/cpp/module.h" | 13 #include "ppapi/cpp/module.h" |
| 14 #include "ppapi/cpp/point.h" | 14 #include "ppapi/cpp/point.h" |
| 15 #include "ppapi/cpp/rect.h" | 15 #include "ppapi/cpp/rect.h" |
| 16 #include "ppapi/cpp/var.h" | 16 #include "ppapi/cpp/var.h" |
| 17 | 17 |
| 18 #include "SkBase64.h" | |
| 19 #include "SkBitmap.h" | 18 #include "SkBitmap.h" |
| 20 #include "SkCanvas.h" | 19 #include "SkCanvas.h" |
| 21 #include "SkColor.h" | 20 #include "SkColor.h" |
| 22 #include "SkDebugger.h" | |
| 23 #include "SkGraphics.h" | 21 #include "SkGraphics.h" |
| 24 #include "SkStream.h" | 22 #include "SkStream.h" |
| 25 #include "SkString.h" | 23 #include "SkString.h" |
| 26 | 24 |
| 27 class SkiaInstance; | 25 class SkiaInstance; |
| 28 | 26 |
| 29 // Used by SkDebugf | 27 // Used by SkDebugf |
| 30 SkiaInstance* gPluginInstance; | 28 SkiaInstance* gPluginInstance; |
| 31 | 29 |
| 32 void FlushCallback(void* data, int32_t result); | 30 void FlushCallback(void* data, int32_t result); |
| 33 | 31 |
| 32 static void doDraw(SkCanvas* canvas, const SkPaint& paint, const char text[]) { | |
| 33 canvas->drawColor(SK_ColorWHITE); | |
| 34 SkPaint red; | |
| 35 red.setColor(SK_ColorRED); | |
| 36 canvas->drawCircle(150.0, 150.0, 100.0, red); | |
| 37 SkRect bounds; | |
| 38 canvas->getClipBounds(&bounds); | |
| 39 canvas->drawText(text, strlen(text), | |
| 40 bounds.centerX(), bounds.centerY(), | |
| 41 paint); | |
| 42 } | |
| 43 | |
| 34 // Skia's subclass of pp::Instance, our interface with the browser. | 44 // Skia's subclass of pp::Instance, our interface with the browser. |
| 35 class SkiaInstance : public pp::Instance { | 45 class SkiaInstance : public pp::Instance { |
| 36 public: | 46 public: |
| 37 explicit SkiaInstance(PP_Instance instance) | 47 explicit SkiaInstance(PP_Instance instance) |
| 38 : pp::Instance(instance) | 48 : pp::Instance(instance) |
| 39 , fCanvas(NULL) | 49 , fCanvas(NULL) |
| 40 , fPicture(NULL) | |
| 41 , fFlushLoopRunning(false) | 50 , fFlushLoopRunning(false) |
| 42 , fFlushPending(false) | 51 , fFlushPending(false) |
| 43 | |
| 44 { | 52 { |
| 45 gPluginInstance = this; | 53 gPluginInstance = this; |
| 46 SkGraphics::Init(); | 54 SkGraphics::Init(); |
| 47 } | 55 } |
| 48 | 56 |
| 49 virtual ~SkiaInstance() { | 57 virtual ~SkiaInstance() { |
| 50 SkGraphics::Term(); | 58 SkGraphics::Term(); |
| 51 gPluginInstance = NULL; | 59 gPluginInstance = NULL; |
| 52 } | 60 } |
| 53 | 61 |
| 54 virtual void HandleMessage(const pp::Var& var_message) { | 62 virtual void HandleMessage(const pp::Var& var_message) { |
| 55 // Receive a message from javascript. | 63 // Receive a message from javascript. |
| 56 if (var_message.is_string()) { | |
| 57 SkString msg(var_message.AsString().c_str()); | |
| 58 if (msg.startsWith("init")) { | |
| 59 } else if (msg.startsWith("LoadSKP")) { | |
| 60 size_t startIndex = strlen("LoadSKP"); | |
| 61 size_t dataSize = msg.size()/sizeof(char) - startIndex; | |
| 62 SkBase64 decodedData; | |
| 63 decodedData.decode(msg.c_str() + startIndex, dataSize); | |
| 64 size_t decodedSize = 3 * (dataSize / 4); | |
| 65 SkDebugf("Got size: %d\n", decodedSize); | |
| 66 if (!decodedData.getData()) { | |
| 67 SkDebugf("Failed to decode SKP\n"); | |
| 68 return; | |
| 69 } | |
| 70 SkMemoryStream pictureStream(decodedData.getData(), decodedSize) ; | |
| 71 fPicture = new SkPicture(&pictureStream); | |
| 72 if (fPicture->width() == 0 || fPicture->height() == 0) { | |
| 73 SkDebugf("Failed to create SKP.\n"); | |
| 74 return; | |
| 75 } | |
| 76 fDebugger.loadPicture(fPicture); | |
| 77 | |
| 78 // Set up the command list. | |
| 79 SkTArray<SkString>* commands = fDebugger.getDrawCommandsAsString s(); | |
| 80 PostMessage("ClearCommands"); | |
| 81 for (int i = 0; i < commands->count(); ++i) { | |
| 82 SkString addCommand("AddCommand:"); | |
| 83 addCommand.append((*commands)[i]); | |
| 84 PostMessage(addCommand.c_str()); | |
| 85 } | |
| 86 PostMessage("UpdateCommands"); | |
| 87 | |
| 88 // Set the overview text. | |
| 89 SkString overviewText; | |
| 90 fDebugger.getOverviewText(NULL, 0.0, &overviewText, 1); | |
| 91 overviewText.prepend("SetOverview:"); | |
| 92 PostMessage(overviewText.c_str()); | |
| 93 | |
| 94 // Draw the SKP. | |
| 95 if (!fFlushLoopRunning) { | |
| 96 Paint(); | |
| 97 } | |
| 98 } else if (msg.startsWith("CommandSelected:")) { | |
| 99 size_t startIndex = strlen("CommandSelected:"); | |
| 100 int index = atoi(msg.c_str() + startIndex); | |
| 101 fDebugger.setIndex(index); | |
| 102 if (!fFlushLoopRunning) { | |
| 103 Paint(); | |
| 104 } | |
| 105 } else if (msg.startsWith("Rewind")) { | |
| 106 fCanvas->clear(SK_ColorWHITE); | |
| 107 fDebugger.setIndex(0); | |
| 108 if (!fFlushLoopRunning) { | |
| 109 Paint(); | |
| 110 } | |
| 111 } else if (msg.startsWith("StepBack")) { | |
| 112 fCanvas->clear(SK_ColorWHITE); | |
| 113 int currentIndex = fDebugger.index(); | |
| 114 if (currentIndex > 1) { | |
| 115 fDebugger.setIndex(currentIndex - 1); | |
| 116 if (!fFlushLoopRunning) { | |
| 117 Paint(); | |
| 118 } | |
| 119 } | |
| 120 } else if (msg.startsWith("Pause")) { | |
| 121 // TODO(borenet) | |
| 122 } else if (msg.startsWith("StepForward")) { | |
| 123 int currentIndex = fDebugger.index(); | |
| 124 if (currentIndex < fDebugger.getSize() -1) { | |
| 125 fDebugger.setIndex(currentIndex + 1); | |
| 126 if (!fFlushLoopRunning) { | |
| 127 Paint(); | |
| 128 } | |
| 129 } | |
| 130 } else if (msg.startsWith("Play")) { | |
| 131 fDebugger.setIndex(fDebugger.getSize() - 1); | |
| 132 if (!fFlushLoopRunning) { | |
| 133 Paint(); | |
| 134 } | |
| 135 } | |
| 136 } | |
| 137 } | 64 } |
| 138 | 65 |
| 139 void Paint() { | 66 void Paint() { |
| 140 if (!fImage.is_null()) { | 67 if (!fImage.is_null()) { |
| 141 fDebugger.draw(fCanvas); | 68 SkPaint paint; |
| 69 paint.setAntiAlias(true); | |
| 70 paint.setTextSize(SkIntToScalar(30)); | |
| 71 paint.setTextAlign(SkPaint::kCenter_Align); | |
| 72 doDraw(fCanvas, paint, "Hello"); | |
| 73 | |
| 142 fDeviceContext.PaintImageData(fImage, pp::Point(0, 0)); | 74 fDeviceContext.PaintImageData(fImage, pp::Point(0, 0)); |
| 143 if (!fFlushPending) { | 75 if (!fFlushPending) { |
| 144 fFlushPending = true; | 76 fFlushPending = true; |
| 145 fDeviceContext.Flush(pp::CompletionCallback(&FlushCallback, this )); | 77 fDeviceContext.Flush(pp::CompletionCallback(&FlushCallback, this )); |
| 146 } else { | 78 } else { |
| 147 SkDebugf("A flush is pending... Skipping flush.\n"); | 79 SkDebugf("A flush is pending... Skipping flush.\n"); |
| 148 } | 80 } |
| 149 } else { | 81 } else { |
| 150 SkDebugf("No pixels to write to!\n"); | 82 SkDebugf("No pixels to write to!\n"); |
| 151 } | 83 } |
| 152 } | 84 } |
| 153 | 85 |
| 154 virtual void DidChangeView(const pp::Rect& position, const pp::Rect& clip) { | 86 virtual void DidChangeView(const pp::Rect& position, const pp::Rect& clip) { |
| 155 if (position.size().width() == fWidth && | 87 if (position.size().width() == fWidth && |
| 156 position.size().height() == fHeight) { | 88 position.size().height() == fHeight) { |
| 157 return; // We don't care about the position, only the size. | 89 return; // We don't care about the position, only the size. |
| 158 } | 90 } |
| 159 fWidth = position.size().width(); | 91 fWidth = position.size().width(); |
| 160 fHeight = position.size().height(); | 92 fHeight = position.size().height(); |
| 161 | |
| 162 fDeviceContext = pp::Graphics2D(this, pp::Size(fWidth, fHeight), false); | 93 fDeviceContext = pp::Graphics2D(this, pp::Size(fWidth, fHeight), false); |
| 163 if (!BindGraphics(fDeviceContext)) { | 94 if (!BindGraphics(fDeviceContext)) { |
| 164 SkDebugf("Couldn't bind the device context\n"); | 95 SkDebugf("Couldn't bind the device context\n"); |
| 165 return; | 96 return; |
| 166 } | 97 } |
| 167 fImage = pp::ImageData(this, | 98 fImage = pp::ImageData(this, |
| 168 PP_IMAGEDATAFORMAT_BGRA_PREMUL, | 99 PP_IMAGEDATAFORMAT_BGRA_PREMUL, |
| 169 pp::Size(fWidth, fHeight), false); | 100 pp::Size(fWidth, fHeight), false); |
| 170 fBitmap.setConfig(SkBitmap::kARGB_8888_Config, fWidth, fHeight); | 101 fBitmap.setConfig(SkBitmap::kARGB_8888_Config, fWidth, fHeight); |
| 171 fBitmap.setPixels(fImage.data()); | 102 fBitmap.setPixels(fImage.data()); |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 186 } | 117 } |
| 187 | 118 |
| 188 private: | 119 private: |
| 189 pp::Graphics2D fDeviceContext; | 120 pp::Graphics2D fDeviceContext; |
| 190 pp::ImageData fImage; | 121 pp::ImageData fImage; |
| 191 int fWidth; | 122 int fWidth; |
| 192 int fHeight; | 123 int fHeight; |
| 193 | 124 |
| 194 SkBitmap fBitmap; | 125 SkBitmap fBitmap; |
| 195 SkCanvas* fCanvas; | 126 SkCanvas* fCanvas; |
| 196 SkDebugger fDebugger; | |
| 197 SkPicture* fPicture; | |
| 198 | 127 |
| 199 bool fFlushLoopRunning; | 128 bool fFlushLoopRunning; |
| 200 bool fFlushPending; | 129 bool fFlushPending; |
| 201 }; | 130 }; |
| 202 | 131 |
| 203 void FlushCallback(void* data, int32_t result) { | 132 void FlushCallback(void* data, int32_t result) { |
| 204 static_cast<SkiaInstance*>(data)->OnFlush(); | 133 static_cast<SkiaInstance*>(data)->OnFlush(); |
| 205 } | 134 } |
| 206 | 135 |
| 207 class SkiaModule : public pp::Module { | 136 class SkiaModule : public pp::Module { |
| 208 public: | 137 public: |
| 209 SkiaModule() : pp::Module() {} | 138 SkiaModule() : pp::Module() {} |
| 210 virtual ~SkiaModule() {} | 139 virtual ~SkiaModule() {} |
| 211 | 140 |
| 212 virtual pp::Instance* CreateInstance(PP_Instance instance) { | 141 virtual pp::Instance* CreateInstance(PP_Instance instance) { |
| 213 return new SkiaInstance(instance); | 142 return new SkiaInstance(instance); |
| 214 } | 143 } |
| 215 }; | 144 }; |
| 216 | 145 |
| 217 namespace pp { | 146 namespace pp { |
| 218 Module* CreateModule() { | 147 Module* CreateModule() { |
| 219 return new SkiaModule(); | 148 return new SkiaModule(); |
| 220 } | 149 } |
| 221 } // namespace pp | 150 } // namespace pp |
| OLD | NEW |