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 "SkMovie.h"
9#include "SkCanvas.h"
10#include "SkPaint.h"
11
12SK_DEFINE_INST_COUNT(SkMovie)
13
14// We should never see this in normal operation since our time values are
15// 0-based. So we use it as a sentinal.
16#define UNINITIALIZED_MSEC ((SkMSec)-1)
17
18SkMovie::SkMovie()
19{
20    fInfo.fDuration = UNINITIALIZED_MSEC;  // uninitialized
21    fCurrTime = UNINITIALIZED_MSEC; // uninitialized
22    fNeedBitmap = true;
23}
24
25void SkMovie::ensureInfo()
26{
27    if (fInfo.fDuration == UNINITIALIZED_MSEC && !this->onGetInfo(&fInfo))
28        memset(&fInfo, 0, sizeof(fInfo));   // failure
29}
30
31SkMSec SkMovie::duration()
32{
33    this->ensureInfo();
34    return fInfo.fDuration;
35}
36
37int SkMovie::width()
38{
39    this->ensureInfo();
40    return fInfo.fWidth;
41}
42
43int SkMovie::height()
44{
45    this->ensureInfo();
46    return fInfo.fHeight;
47}
48
49int SkMovie::isOpaque()
50{
51    this->ensureInfo();
52    return fInfo.fIsOpaque;
53}
54
55bool SkMovie::setTime(SkMSec time)
56{
57    SkMSec dur = this->duration();
58    if (time > dur)
59        time = dur;
60
61    bool changed = false;
62    if (time != fCurrTime)
63    {
64        fCurrTime = time;
65        changed = this->onSetTime(time);
66        fNeedBitmap |= changed;
67    }
68    return changed;
69}
70
71const SkBitmap& SkMovie::bitmap()
72{
73    if (fCurrTime == UNINITIALIZED_MSEC)    // uninitialized
74        this->setTime(0);
75
76    if (fNeedBitmap)
77    {
78        if (!this->onGetBitmap(&fBitmap))   // failure
79            fBitmap.reset();
80        fNeedBitmap = false;
81    }
82    return fBitmap;
83}
84
85////////////////////////////////////////////////////////////////////
86
87#include "SkStream.h"
88
89SkMovie* SkMovie::DecodeMemory(const void* data, size_t length) {
90    SkMemoryStream stream(data, length, false);
91    return SkMovie::DecodeStream(&stream);
92}
93
94SkMovie* SkMovie::DecodeFile(const char path[]) {
95    SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path));
96    return stream.get() ? SkMovie::DecodeStream(stream) : NULL;
97}
98