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     SkASSERT(false);
     56     // Cannot create a duplicate, since each AssetStreamAdaptor
     57     // would be modifying the Asset.
     58     //return new AssetStreamAdaptor(fAsset);
     59     return NULL;
     60 }
     61 
     62 size_t AssetStreamAdaptor::read(void* buffer, size_t size) {
     63     ssize_t amount;
     64 
     65     if (NULL == buffer) {
     66         if (0 == size) {
     67             return 0;
     68         }
     69         // asset->seek returns new total offset
     70         // we want to return amount that was skipped
     71 
     72         off64_t oldOffset = fAsset->seek(0, SEEK_CUR);
     73         if (-1 == oldOffset) {
     74             SkDebugf("---- fAsset->seek(oldOffset) failed\n");
     75             return 0;
     76         }
     77         off64_t newOffset = fAsset->seek(size, SEEK_CUR);
     78         if (-1 == newOffset) {
     79             SkDebugf("---- fAsset->seek(%d) failed\n", size);
     80             return 0;
     81         }
     82         amount = newOffset - oldOffset;
     83     } else {
     84         amount = fAsset->read(buffer, size);
     85         if (amount <= 0) {
     86             SkDebugf("---- fAsset->read(%d) returned %d\n", size, amount);
     87         }
     88     }
     89 
     90     if (amount < 0) {
     91         amount = 0;
     92     }
     93     return amount;
     94 }
     95 
     96 SkMemoryStream* android::CopyAssetToStream(Asset* asset) {
     97     if (NULL == asset) {
     98         return NULL;
     99     }
    100 
    101     off64_t size = asset->seek(0, SEEK_SET);
    102     if ((off64_t)-1 == size) {
    103         SkDebugf("---- copyAsset: asset rewind failed\n");
    104         return NULL;
    105     }
    106 
    107     size = asset->getLength();
    108     if (size <= 0) {
    109         SkDebugf("---- copyAsset: asset->getLength() returned %d\n", size);
    110         return NULL;
    111     }
    112 
    113     SkMemoryStream* stream = new SkMemoryStream(size);
    114     void* data = const_cast<void*>(stream->getMemoryBase());
    115     off64_t len = asset->read(data, size);
    116     if (len != size) {
    117         SkDebugf("---- copyAsset: asset->read(%d) returned %d\n", size, len);
    118         delete stream;
    119         stream = NULL;
    120     }
    121     return stream;
    122 }
    123 
    124 jobject android::nullObjectReturn(const char msg[]) {
    125     if (msg) {
    126         SkDebugf("--- %s\n", msg);
    127     }
    128     return NULL;
    129 }
    130