yangsu@google.com | a8a42e2 | 2011-06-16 20:49:55 +0000 | [diff] [blame] | 1 | #include "SkDebuggerViews.h" |
| 2 | #include <stdio.h> |
| 3 | |
| 4 | SkContentView::SkContentView(SkEventSinkID clID, SkEventSinkID ipID) : |
| 5 | fDumper(this->getSinkID(), clID, ipID) { |
| 6 | fBGColor = 0xFFDDDDDD; |
| 7 | fAtomsToRead = 0; |
| 8 | fDisplayClip = false; |
| 9 | } |
| 10 | |
| 11 | SkContentView::~SkContentView() { |
| 12 | fAtomBounds.clear(); |
| 13 | fFrameBounds.clear(); |
| 14 | } |
| 15 | |
| 16 | void SkContentView::reinit(const char* filename) { |
| 17 | fFilePath.set(filename); |
| 18 | fAtomsToRead = 0; |
| 19 | this->init(); |
| 20 | } |
| 21 | |
| 22 | bool SkContentView::onEvent(const SkEvent& evt) { |
| 23 | return this->INHERITED::onEvent(evt); |
| 24 | } |
| 25 | |
| 26 | //Read file atom by atom and record attom bounds |
| 27 | void SkContentView::init() { |
| 28 | fDumper.unload(); |
| 29 | fAtomBounds.clear(); |
| 30 | fFrameBounds.clear(); |
| 31 | |
| 32 | SkDumpCanvasM* dumpCanvas = new SkDumpCanvasM(&fDumper); |
| 33 | SkGPipeReader* dumpReader = new SkGPipeReader(dumpCanvas); |
| 34 | |
| 35 | FILE* f = fopen(fFilePath.c_str(), "rb"); |
| 36 | SkASSERT(f != NULL); |
| 37 | fseek(f, 0, SEEK_END); |
| 38 | int fileSize = ftell(f) * sizeof(char); |
| 39 | fseek(f, 0, SEEK_SET); |
| 40 | if (fileSize > 0) { |
| 41 | char* block = (char*)sk_malloc_throw(fileSize); |
| 42 | fread(block, 1, fileSize, f); |
| 43 | int offset = 0; |
| 44 | int frameBound = 0; |
| 45 | size_t bytesRead; |
| 46 | while (offset < fileSize) { |
| 47 | SkGPipeReader::Status s = dumpReader->playback(block + offset, |
| 48 | fileSize - offset, |
| 49 | &bytesRead, true); |
| 50 | SkASSERT(SkGPipeReader::kError_Status != s); |
| 51 | offset += bytesRead; |
| 52 | if (SkGPipeReader::kDone_Status == s) { |
| 53 | fDumper.dump(dumpCanvas,SkDumpCanvasM::kNULL_Verb, |
| 54 | "End of Frame", NULL); |
| 55 | delete dumpReader; |
| 56 | delete dumpCanvas; |
| 57 | dumpCanvas = new SkDumpCanvasM(&fDumper); |
| 58 | dumpReader = new SkGPipeReader(dumpCanvas); |
| 59 | frameBound = offset; |
| 60 | } |
| 61 | fAtomBounds.push_back(offset); |
| 62 | fFrameBounds.push_back(frameBound); |
| 63 | } |
| 64 | sk_free(block); |
| 65 | } |
| 66 | |
| 67 | fclose(f); |
| 68 | |
| 69 | delete dumpReader; |
| 70 | delete dumpCanvas; |
| 71 | |
| 72 | fDumper.load(); |
| 73 | } |
| 74 | |
| 75 | void SkContentView::goToAtom(int atom) { |
| 76 | if (atom != fAtomsToRead) { |
| 77 | fAtomsToRead = atom; |
| 78 | this->inval(NULL); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | void SkContentView::toggleClip() { |
| 83 | fDisplayClip = !fDisplayClip; |
| 84 | this->inval(NULL); |
| 85 | } |
| 86 | |
| 87 | void SkContentView::onDraw(SkCanvas* canvas) { |
| 88 | canvas->drawColor(fBGColor); |
| 89 | |
| 90 | SkAutoCanvasRestore acr(canvas, true); |
| 91 | |
| 92 | int lastFrameBound = fFrameBounds[fAtomsToRead]; |
| 93 | int toBeRead = fAtomBounds[fAtomsToRead] - lastFrameBound; |
| 94 | int firstChunk = (fAtomsToRead > 0) ? fAtomBounds[fAtomsToRead - 1] - |
| 95 | lastFrameBound: 0; |
| 96 | if (toBeRead > 0) { |
| 97 | SkDumpCanvasM* dumpCanvas = new SkDumpCanvasM(&fDumper); |
| 98 | SkGPipeReader* dumpReader = new SkGPipeReader(dumpCanvas); |
| 99 | SkGPipeReader* reader = new SkGPipeReader(canvas); |
| 100 | fDumper.disable(); |
| 101 | |
| 102 | FILE* f = fopen(fFilePath.c_str(), "rb"); |
| 103 | SkASSERT(f != NULL); |
| 104 | fseek(f, lastFrameBound, SEEK_SET); |
| 105 | char* block = (char*)sk_malloc_throw(toBeRead); |
| 106 | fread(block, 1, toBeRead, f); |
| 107 | int offset = 0; |
| 108 | size_t bytesRead; |
| 109 | SkGPipeReader::Status s; |
| 110 | //Read the first chunk |
| 111 | if (offset < firstChunk && firstChunk < toBeRead) { |
| 112 | s = dumpReader->playback(block + offset, firstChunk - offset, NULL, false); |
| 113 | SkASSERT(SkGPipeReader::kError_Status != s); |
| 114 | s = reader->playback(block + offset, firstChunk - offset, &bytesRead, false); |
| 115 | SkASSERT(SkGPipeReader::kError_Status != s); |
| 116 | if (SkGPipeReader::kDone_Status == s){ |
| 117 | delete dumpReader; |
| 118 | delete dumpCanvas; |
| 119 | dumpCanvas = new SkDumpCanvasM(&fDumper); |
| 120 | dumpReader = new SkGPipeReader(dumpCanvas); |
| 121 | delete reader; |
| 122 | reader = new SkGPipeReader(canvas); |
| 123 | } |
| 124 | offset += bytesRead; |
| 125 | } |
| 126 | SkASSERT(offset == firstChunk); |
| 127 | //Then read the current atom |
| 128 | fDumper.enable(); |
| 129 | s = dumpReader->playback(block + offset, toBeRead - offset, NULL, true); |
| 130 | SkASSERT(SkGPipeReader::kError_Status != s); |
| 131 | s = reader->playback(block + offset, toBeRead - offset, &bytesRead, true); |
| 132 | SkASSERT(SkGPipeReader::kError_Status != s); |
| 133 | |
| 134 | sk_free(block); |
| 135 | fclose(f); |
| 136 | |
| 137 | delete reader; |
| 138 | delete dumpReader; |
| 139 | delete dumpCanvas; |
| 140 | |
| 141 | if (fDisplayClip) { |
| 142 | SkPaint p; |
| 143 | p.setColor(0x440000AA); |
| 144 | SkPath path; |
| 145 | canvas->getTotalClip().getBoundaryPath(&path); |
| 146 | canvas->drawPath(path, p); |
| 147 | } |
| 148 | } |
| 149 | this->INHERITED::onDraw(canvas); |
| 150 | } |