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