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