blob: ea4a76c6a19b47756b481cf178a9daa11624bfc3 [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 */
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SkMovie.h"
9#include "SkCanvas.h"
10#include "SkPaint.h"
11
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000012SK_DEFINE_INST_COUNT(SkMovie)
13
reed@android.com8a1c16f2008-12-17 15:59:43 +000014// 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;
rmistry@google.comd6176b02012-08-23 18:14:13 +000060
reed@android.com8a1c16f2008-12-17 15:59:43 +000061 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{
96 SkMovie* movie = NULL;
97
98 SkFILEStream stream(path);
99 if (stream.isValid()) {
100 movie = SkMovie::DecodeStream(&stream);
101 }
102#ifdef SK_DEBUG
103 else {
104 SkDebugf("Movie file not found <%s>\n", path);
105 }
106#endif
107
108 return movie;
109}