Home | History | Annotate | Download | only in images
      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  */
      8 #include "SkMovie.h"
      9 #include "SkCanvas.h"
     10 #include "SkPaint.h"
     11 
     12 SK_DEFINE_INST_COUNT(SkMovie)
     13 
     14 // 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 
     18 SkMovie::SkMovie()
     19 {
     20     fInfo.fDuration = UNINITIALIZED_MSEC;  // uninitialized
     21     fCurrTime = UNINITIALIZED_MSEC; // uninitialized
     22     fNeedBitmap = true;
     23 }
     24 
     25 void SkMovie::ensureInfo()
     26 {
     27     if (fInfo.fDuration == UNINITIALIZED_MSEC && !this->onGetInfo(&fInfo))
     28         memset(&fInfo, 0, sizeof(fInfo));   // failure
     29 }
     30 
     31 SkMSec SkMovie::duration()
     32 {
     33     this->ensureInfo();
     34     return fInfo.fDuration;
     35 }
     36 
     37 int SkMovie::width()
     38 {
     39     this->ensureInfo();
     40     return fInfo.fWidth;
     41 }
     42 
     43 int SkMovie::height()
     44 {
     45     this->ensureInfo();
     46     return fInfo.fHeight;
     47 }
     48 
     49 int SkMovie::isOpaque()
     50 {
     51     this->ensureInfo();
     52     return fInfo.fIsOpaque;
     53 }
     54 
     55 bool SkMovie::setTime(SkMSec time)
     56 {
     57     SkMSec dur = this->duration();
     58     if (time > dur)
     59         time = dur;
     60 
     61     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 
     71 const 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 
     89 SkMovie* SkMovie::DecodeMemory(const void* data, size_t length) {
     90     SkMemoryStream stream(data, length, false);
     91     return SkMovie::DecodeStream(&stream);
     92 }
     93 
     94 SkMovie* 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 }
    110