2011-07-28 14:26:00 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2011-06-17 18:31:01 +00:00
|
|
|
#include "ReaderView.h"
|
|
|
|
#include "SkGPipe.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#define FILE_PATH "/Users/yangsu/Code/test/test.a"
|
|
|
|
ReaderView::ReaderView() {
|
|
|
|
fBGColor = 0xFFDDDDDD;
|
|
|
|
fFilePos = 0;
|
|
|
|
fBufferBitmaps[0].setConfig(SkBitmap::kARGB_8888_Config, 640, 480);
|
|
|
|
fBufferBitmaps[0].allocPixels(NULL);
|
|
|
|
fBufferBitmaps[1].setConfig(SkBitmap::kARGB_8888_Config, 640, 480);
|
|
|
|
fBufferBitmaps[1].allocPixels(NULL);
|
|
|
|
fFront = 0;
|
|
|
|
fBack = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReaderView::draw(SkCanvas* canvas) {
|
|
|
|
canvas->drawColor(fBGColor);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-06-17 18:31:01 +00:00
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
|
|
|
//Create a temporary canvas and reader object that draws into the back
|
2011-06-17 18:31:01 +00:00
|
|
|
//bitmap so that the incremental changes or incomplete reads are not shown
|
|
|
|
//on screen
|
|
|
|
SkCanvas bufferCanvas(fBufferBitmaps[fBack]);
|
|
|
|
SkGPipeReader reader(&bufferCanvas);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-06-17 18:31:01 +00:00
|
|
|
//The file specified by FILE_PATH MUST exist
|
|
|
|
FILE* f = fopen(FILE_PATH, "rb");
|
|
|
|
SkASSERT(f != NULL);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-06-17 18:31:01 +00:00
|
|
|
fseek(f, 0, SEEK_END);
|
|
|
|
int size = ftell(f) * sizeof(char);
|
|
|
|
if (size <= fFilePos) {
|
|
|
|
fFilePos = 0;
|
|
|
|
}
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-06-17 18:31:01 +00:00
|
|
|
//Resume from the last read location
|
|
|
|
fseek(f, fFilePos, SEEK_SET);
|
|
|
|
int toBeRead = size - fFilePos;
|
|
|
|
if (size > 0 && toBeRead > 0) {
|
|
|
|
void* block = sk_malloc_throw(toBeRead);
|
|
|
|
fread(block, 1, toBeRead, f);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-06-17 18:31:01 +00:00
|
|
|
size_t bytesRead;
|
|
|
|
SkGPipeReader::Status fStatus = reader.playback(block, toBeRead, &bytesRead);
|
|
|
|
SkASSERT(SkGPipeReader::kError_Status != fStatus);
|
|
|
|
SkASSERT(toBeRead >= bytesRead);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
|
|
|
//if the reader reaches a done verb, a frame is complete.
|
2011-06-17 18:31:01 +00:00
|
|
|
//Update the file location and swap the front and back bitmaps to show
|
|
|
|
//the new frame
|
|
|
|
if (SkGPipeReader::kDone_Status == fStatus) {
|
|
|
|
fFilePos += bytesRead;
|
|
|
|
fFront = fFront ^ 0x1;
|
|
|
|
fBack = fBack ^ 0x1;
|
|
|
|
}
|
|
|
|
sk_free(block);
|
|
|
|
}
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-06-17 18:31:01 +00:00
|
|
|
fclose(f);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-06-17 18:31:01 +00:00
|
|
|
//the front bitmap is always drawn
|
|
|
|
canvas->drawBitmap(fBufferBitmaps[fFront], 0, 0, NULL);
|
|
|
|
this->inval(NULL);
|
2012-08-14 20:27:54 +00:00
|
|
|
}
|
|
|
|
|