Home | History | Annotate | Download | only in graphics
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include "Utils.h"
     18 #include "SkUtils.h"
     19 
     20 using namespace android;
     21 
     22 AssetStreamAdaptor::AssetStreamAdaptor(Asset* asset, OwnAsset ownAsset,
     23                                        HasMemoryBase hasMemoryBase)
     24     : fAsset(asset)
     25     , fMemoryBase(kYes_HasMemoryBase == hasMemoryBase ?
     26                   asset->getBuffer(false) : NULL)
     27     , fOwnAsset(ownAsset)
     28 {
     29 }
     30 
     31 AssetStreamAdaptor::~AssetStreamAdaptor() {
     32     if (kYes_OwnAsset == fOwnAsset) {
     33         delete fAsset;
     34     }
     35 }
     36 
     37 bool AssetStreamAdaptor::rewind() {
     38     off64_t pos = fAsset->seek(0, SEEK_SET);
     39     if (pos == (off64_t)-1) {
     40         SkDebugf("----- fAsset->seek(rewind) failed\n");
     41         return false;
     42     }
     43     return true;
     44 }
     45 
     46 size_t AssetStreamAdaptor::getLength() const {
     47     return fAsset->getLength();
     48 }
     49 
     50 bool AssetStreamAdaptor::isAtEnd() const {
     51     return fAsset->getRemainingLength() == 0;
     52 }
     53 
     54 SkStreamRewindable* AssetStreamAdaptor::duplicate() const {
     55     // Cannot create a duplicate, since each AssetStreamAdaptor
     56     // would be modifying the Asset.
     57     //return new AssetStreamAdaptor(fAsset);
     58     return NULL;
     59 }
     60 
     61 size_t AssetStreamAdaptor::read(void* buffer, size_t size) {
     62     ssize_t amount;
     63 
     64     if (NULL == buffer) {
     65         if (0 == size) {
     66             return 0;
     67         }
     68         // asset->seek returns new total offset
     69         // we want to return amount that was skipped
     70 
     71         off64_t oldOffset = fAsset->seek(0, SEEK_CUR);
     72         if (-1 == oldOffset) {
     73             SkDebugf("---- fAsset->seek(oldOffset) failed\n");
     74             return 0;
     75         }
     76         off64_t newOffset = fAsset->seek(size, SEEK_CUR);
     77         if (-1 == newOffset) {
     78             SkDebugf("---- fAsset->seek(%d) failed\n", size);
     79             return 0;
     80         }
     81         amount = newOffset - oldOffset;
     82     } else {
     83         amount = fAsset->read(buffer, size);
     84         if (amount <= 0) {
     85             SkDebugf("---- fAsset->read(%d) returned %d\n", size, amount);
     86         }
     87     }
     88 
     89     if (amount < 0) {
     90         amount = 0;
     91     }
     92     return amount;
     93 }
     94 
     95 SkMemoryStream* android::CopyAssetToStream(Asset* asset) {
     96     if (NULL == asset) {
     97         return NULL;
     98     }
     99 
    100     off64_t size = asset->seek(0, SEEK_SET);
    101     if ((off64_t)-1 == size) {
    102         SkDebugf("---- copyAsset: asset rewind failed\n");
    103         return NULL;
    104     }
    105 
    106     size = asset->getLength();
    107     if (size <= 0) {
    108         SkDebugf("---- copyAsset: asset->getLength() returned %d\n", size);
    109         return NULL;
    110     }
    111 
    112     SkMemoryStream* stream = new SkMemoryStream(size);
    113     void* data = const_cast<void*>(stream->getMemoryBase());
    114     off64_t len = asset->read(data, size);
    115     if (len != size) {
    116         SkDebugf("---- copyAsset: asset->read(%d) returned %d\n", size, len);
    117         delete stream;
    118         stream = NULL;
    119     }
    120     return stream;
    121 }
    122 
    123 jobject android::nullObjectReturn(const char msg[]) {
    124     if (msg) {
    125         SkDebugf("--- %s\n", msg);
    126     }
    127     return NULL;
    128 }
    129