blob: 78a7a21e28407fbd37cc8280c04e78f9e106b603 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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.com02642762011-06-17 18:31:01 +00008#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);
rmistry@google.comd6176b02012-08-23 18:14:13 +000028
yangsu@google.com02642762011-06-17 18:31:01 +000029 SkAutoCanvasRestore acr(canvas, true);
rmistry@google.comd6176b02012-08-23 18:14:13 +000030
31 //Create a temporary canvas and reader object that draws into the back
yangsu@google.com02642762011-06-17 18:31:01 +000032 //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.comd6176b02012-08-23 18:14:13 +000036
yangsu@google.com02642762011-06-17 18:31:01 +000037 //The file specified by FILE_PATH MUST exist
38 FILE* f = fopen(FILE_PATH, "rb");
39 SkASSERT(f != NULL);
rmistry@google.comd6176b02012-08-23 18:14:13 +000040
yangsu@google.com02642762011-06-17 18:31:01 +000041 fseek(f, 0, SEEK_END);
42 int size = ftell(f) * sizeof(char);
43 if (size <= fFilePos) {
44 fFilePos = 0;
45 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000046
yangsu@google.com02642762011-06-17 18:31:01 +000047 //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.comd6176b02012-08-23 18:14:13 +000053
yangsu@google.com02642762011-06-17 18:31:01 +000054 size_t bytesRead;
55 SkGPipeReader::Status fStatus = reader.playback(block, toBeRead, &bytesRead);
56 SkASSERT(SkGPipeReader::kError_Status != fStatus);
57 SkASSERT(toBeRead >= bytesRead);
rmistry@google.comd6176b02012-08-23 18:14:13 +000058
59 //if the reader reaches a done verb, a frame is complete.
yangsu@google.com02642762011-06-17 18:31:01 +000060 //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.comd6176b02012-08-23 18:14:13 +000069
yangsu@google.com02642762011-06-17 18:31:01 +000070 fclose(f);
rmistry@google.comd6176b02012-08-23 18:14:13 +000071
yangsu@google.com02642762011-06-17 18:31:01 +000072 //the front bitmap is always drawn
73 canvas->drawBitmap(fBufferBitmaps[fFront], 0, 0, NULL);
74 this->inval(NULL);
rmistry@google.combda03db2012-08-14 20:27:54 +000075}
76