epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SkMovie.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkPaint.h" |
| 11 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 12 | // We should never see this in normal operation since our time values are |
| 13 | // 0-based. So we use it as a sentinal. |
| 14 | #define UNINITIALIZED_MSEC ((SkMSec)-1) |
| 15 | |
| 16 | SkMovie::SkMovie() |
| 17 | { |
| 18 | fInfo.fDuration = UNINITIALIZED_MSEC; // uninitialized |
| 19 | fCurrTime = UNINITIALIZED_MSEC; // uninitialized |
| 20 | fNeedBitmap = true; |
| 21 | } |
| 22 | |
| 23 | void SkMovie::ensureInfo() |
| 24 | { |
| 25 | if (fInfo.fDuration == UNINITIALIZED_MSEC && !this->onGetInfo(&fInfo)) |
| 26 | memset(&fInfo, 0, sizeof(fInfo)); // failure |
| 27 | } |
| 28 | |
| 29 | SkMSec SkMovie::duration() |
| 30 | { |
| 31 | this->ensureInfo(); |
| 32 | return fInfo.fDuration; |
| 33 | } |
| 34 | |
| 35 | int SkMovie::width() |
| 36 | { |
| 37 | this->ensureInfo(); |
| 38 | return fInfo.fWidth; |
| 39 | } |
| 40 | |
| 41 | int SkMovie::height() |
| 42 | { |
| 43 | this->ensureInfo(); |
| 44 | return fInfo.fHeight; |
| 45 | } |
| 46 | |
| 47 | int SkMovie::isOpaque() |
| 48 | { |
| 49 | this->ensureInfo(); |
| 50 | return fInfo.fIsOpaque; |
| 51 | } |
| 52 | |
| 53 | bool SkMovie::setTime(SkMSec time) |
| 54 | { |
| 55 | SkMSec dur = this->duration(); |
| 56 | if (time > dur) |
| 57 | time = dur; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 58 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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 | |
| 69 | const SkBitmap& SkMovie::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 | |
| 85 | #include "SkStream.h" |
| 86 | |
| 87 | SkMovie* SkMovie::DecodeMemory(const void* data, size_t length) { |
| 88 | SkMemoryStream stream(data, length, false); |
| 89 | return SkMovie::DecodeStream(&stream); |
| 90 | } |
| 91 | |
mike@reedtribe.org | f381162 | 2013-03-19 02:18:33 +0000 | [diff] [blame] | 92 | SkMovie* SkMovie::DecodeFile(const char path[]) { |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame^] | 93 | SkAutoTDelete<SkStreamRewindable> stream(SkStream::NewFromFile(path)); |
mike@reedtribe.org | f381162 | 2013-03-19 02:18:33 +0000 | [diff] [blame] | 94 | return stream.get() ? SkMovie::DecodeStream(stream) : NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 95 | } |