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 */
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"
15ReaderView::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
26void ReaderView::draw(SkCanvas* canvas) {
27    canvas->drawColor(fBGColor);
28
29    SkAutoCanvasRestore acr(canvas, true);
30
31    //Create a temporary canvas and reader object that draws into the back
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);
36
37    //The file specified by FILE_PATH MUST exist
38    FILE* f = fopen(FILE_PATH, "rb");
39    SkASSERT(f != NULL);
40
41    fseek(f, 0, SEEK_END);
42    int size = ftell(f) * sizeof(char);
43    if (size <= fFilePos) {
44        fFilePos = 0;
45    }
46
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);
53
54        size_t bytesRead;
55        SkGPipeReader::Status fStatus = reader.playback(block, toBeRead, &bytesRead);
56        SkASSERT(SkGPipeReader::kError_Status != fStatus);
57        SkASSERT(toBeRead >= bytesRead);
58
59        //if the reader reaches a done verb, a frame is complete.
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    }
69
70    fclose(f);
71
72    //the front bitmap is always drawn
73    canvas->drawBitmap(fBufferBitmaps[fFront], 0, 0, NULL);
74    this->inval(NULL);
75}
76