yangsu@google.com | 0264276 | 2011-06-17 18:31:01 +0000 | [diff] [blame^] | 1 | #include "ReaderView.h" |
| 2 | #include "SkGPipe.h" |
| 3 | #include "SkCanvas.h" |
| 4 | |
| 5 | #include <stdio.h> |
| 6 | |
| 7 | #define FILE_PATH "/Users/yangsu/Code/test/test.a" |
| 8 | ReaderView::ReaderView() { |
| 9 | fBGColor = 0xFFDDDDDD; |
| 10 | fFilePos = 0; |
| 11 | fBufferBitmaps[0].setConfig(SkBitmap::kARGB_8888_Config, 640, 480); |
| 12 | fBufferBitmaps[0].allocPixels(NULL); |
| 13 | fBufferBitmaps[1].setConfig(SkBitmap::kARGB_8888_Config, 640, 480); |
| 14 | fBufferBitmaps[1].allocPixels(NULL); |
| 15 | fFront = 0; |
| 16 | fBack = 1; |
| 17 | } |
| 18 | |
| 19 | void ReaderView::draw(SkCanvas* canvas) { |
| 20 | canvas->drawColor(fBGColor); |
| 21 | |
| 22 | SkAutoCanvasRestore acr(canvas, true); |
| 23 | |
| 24 | //Create a temporary canvas and reader object that draws into the back |
| 25 | //bitmap so that the incremental changes or incomplete reads are not shown |
| 26 | //on screen |
| 27 | SkCanvas bufferCanvas(fBufferBitmaps[fBack]); |
| 28 | SkGPipeReader reader(&bufferCanvas); |
| 29 | |
| 30 | //The file specified by FILE_PATH MUST exist |
| 31 | FILE* f = fopen(FILE_PATH, "rb"); |
| 32 | SkASSERT(f != NULL); |
| 33 | |
| 34 | fseek(f, 0, SEEK_END); |
| 35 | int size = ftell(f) * sizeof(char); |
| 36 | if (size <= fFilePos) { |
| 37 | fFilePos = 0; |
| 38 | } |
| 39 | |
| 40 | //Resume from the last read location |
| 41 | fseek(f, fFilePos, SEEK_SET); |
| 42 | int toBeRead = size - fFilePos; |
| 43 | if (size > 0 && toBeRead > 0) { |
| 44 | void* block = sk_malloc_throw(toBeRead); |
| 45 | fread(block, 1, toBeRead, f); |
| 46 | |
| 47 | size_t bytesRead; |
| 48 | SkGPipeReader::Status fStatus = reader.playback(block, toBeRead, &bytesRead); |
| 49 | SkASSERT(SkGPipeReader::kError_Status != fStatus); |
| 50 | SkASSERT(toBeRead >= bytesRead); |
| 51 | |
| 52 | //if the reader reaches a done verb, a frame is complete. |
| 53 | //Update the file location and swap the front and back bitmaps to show |
| 54 | //the new frame |
| 55 | if (SkGPipeReader::kDone_Status == fStatus) { |
| 56 | fFilePos += bytesRead; |
| 57 | fFront = fFront ^ 0x1; |
| 58 | fBack = fBack ^ 0x1; |
| 59 | } |
| 60 | sk_free(block); |
| 61 | } |
| 62 | |
| 63 | fclose(f); |
| 64 | |
| 65 | //the front bitmap is always drawn |
| 66 | canvas->drawBitmap(fBufferBitmaps[fFront], 0, 0, NULL); |
| 67 | this->inval(NULL); |
| 68 | } |