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 
Movie()15 Movie::Movie()
16 {
17     fInfo.fDuration = UNINITIALIZED_MSEC;  // uninitialized
18     fCurrTime = UNINITIALIZED_MSEC; // uninitialized
19     fNeedBitmap = true;
20 }
21 
ensureInfo()22 void Movie::ensureInfo()
23 {
24     if (fInfo.fDuration == UNINITIALIZED_MSEC && !this->onGetInfo(&fInfo))
25         memset(&fInfo, 0, sizeof(fInfo));   // failure
26 }
27 
duration()28 SkMSec Movie::duration()
29 {
30     this->ensureInfo();
31     return fInfo.fDuration;
32 }
33 
width()34 int Movie::width()
35 {
36     this->ensureInfo();
37     return fInfo.fWidth;
38 }
39 
height()40 int Movie::height()
41 {
42     this->ensureInfo();
43     return fInfo.fHeight;
44 }
45 
isOpaque()46 int Movie::isOpaque()
47 {
48     this->ensureInfo();
49     return fInfo.fIsOpaque;
50 }
51 
setTime(SkMSec time)52 bool 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 
bitmap()68 const 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 
DecodeMemory(const void * data,size_t length)86 Movie* Movie::DecodeMemory(const void* data, size_t length) {
87     SkMemoryStream stream(data, length, false);
88     return Movie::DecodeStream(&stream);
89 }
90 
DecodeFile(const char path[])91 Movie* Movie::DecodeFile(const char path[]) {
92     std::unique_ptr<SkStreamRewindable> stream = SkStream::MakeFromFile(path);
93     return stream ? Movie::DecodeStream(stream.get()) : nullptr;
94 }
95