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 #include "SkData.h"
     20 
     21 using namespace android;
     22 
     23 AssetStreamAdaptor::AssetStreamAdaptor(Asset* asset)
     24     : fAsset(asset)
     25 {
     26 }
     27 
     28 bool AssetStreamAdaptor::rewind() {
     29     off64_t pos = fAsset->seek(0, SEEK_SET);
     30     if (pos == (off64_t)-1) {
     31         SkDebugf("----- fAsset->seek(rewind) failed\n");
     32         return false;
     33     }
     34     return true;
     35 }
     36 
     37 size_t AssetStreamAdaptor::getLength() const {
     38     return fAsset->getLength();
     39 }
     40 
     41 bool AssetStreamAdaptor::isAtEnd() const {
     42     return fAsset->getRemainingLength() == 0;
     43 }
     44 
     45 SkStreamRewindable* AssetStreamAdaptor::onDuplicate() const {
     46     // Cannot create a duplicate, since each AssetStreamAdaptor
     47     // would be modifying the Asset.
     48     //return new AssetStreamAdaptor(fAsset);
     49     return NULL;
     50 }
     51 
     52 bool AssetStreamAdaptor::hasPosition() const {
     53     return fAsset->seek(0, SEEK_CUR) != -1;
     54 }
     55 
     56 size_t AssetStreamAdaptor::getPosition() const {
     57     const off64_t offset = fAsset->seek(0, SEEK_CUR);
     58     if (offset == -1) {
     59         SkDebugf("---- fAsset->seek(0, SEEK_CUR) failed\n");
     60         return 0;
     61     }
     62 
     63     return offset;
     64 }
     65 
     66 bool AssetStreamAdaptor::seek(size_t position) {
     67     if (fAsset->seek(position, SEEK_SET) == -1) {
     68         SkDebugf("---- fAsset->seek(0, SEEK_SET) failed\n");
     69         return false;
     70     }
     71 
     72     return true;
     73 }
     74 
     75 bool AssetStreamAdaptor::move(long offset) {
     76     if (fAsset->seek(offset, SEEK_CUR) == -1) {
     77         SkDebugf("---- fAsset->seek(%i, SEEK_CUR) failed\n", offset);
     78         return false;
     79     }
     80 
     81     return true;
     82 }
     83 
     84 size_t AssetStreamAdaptor::read(void* buffer, size_t size) {
     85     ssize_t amount;
     86 
     87     if (NULL == buffer) {
     88         if (0 == size) {
     89             return 0;
     90         }
     91         // asset->seek returns new total offset
     92         // we want to return amount that was skipped
     93 
     94         off64_t oldOffset = fAsset->seek(0, SEEK_CUR);
     95         if (-1 == oldOffset) {
     96             SkDebugf("---- fAsset->seek(oldOffset) failed\n");
     97             return 0;
     98         }
     99         off64_t newOffset = fAsset->seek(size, SEEK_CUR);
    100         if (-1 == newOffset) {
    101             SkDebugf("---- fAsset->seek(%d) failed\n", size);
    102             return 0;
    103         }
    104         amount = newOffset - oldOffset;
    105     } else {
    106         amount = fAsset->read(buffer, size);
    107     }
    108 
    109     if (amount < 0) {
    110         amount = 0;
    111     }
    112     return amount;
    113 }
    114 
    115 SkMemoryStream* android::CopyAssetToStream(Asset* asset) {
    116     if (NULL == asset) {
    117         return NULL;
    118     }
    119 
    120     const off64_t seekReturnVal = asset->seek(0, SEEK_SET);
    121     if ((off64_t)-1 == seekReturnVal) {
    122         SkDebugf("---- copyAsset: asset rewind failed\n");
    123         return NULL;
    124     }
    125 
    126     const off64_t size = asset->getLength();
    127     if (size <= 0) {
    128         SkDebugf("---- copyAsset: asset->getLength() returned %d\n", size);
    129         return NULL;
    130     }
    131 
    132     sk_sp<SkData> data(SkData::MakeUninitialized(size));
    133     const off64_t len = asset->read(data->writable_data(), size);
    134     if (len != size) {
    135         SkDebugf("---- copyAsset: asset->read(%d) returned %d\n", size, len);
    136         return NULL;
    137     }
    138 
    139     return new SkMemoryStream(std::move(data));
    140 }
    141 
    142 jobject android::nullObjectReturn(const char msg[]) {
    143     if (msg) {
    144         SkDebugf("--- %s\n", msg);
    145     }
    146     return NULL;
    147 }
    148 
    149 bool android::isSeekable(int descriptor) {
    150     return ::lseek64(descriptor, 0, SEEK_CUR) != -1;
    151 }
    152 
    153 JNIEnv* android::get_env_or_die(JavaVM* jvm) {
    154     JNIEnv* env;
    155     if (jvm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
    156         LOG_ALWAYS_FATAL("Failed to get JNIEnv for JavaVM: %p", jvm);
    157     }
    158     return env;
    159 }
    160