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