Home | History | Annotate | Download | only in core
      1 
      2 /*
      3  * Copyright 2012 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 
      9 #include "SkOrderedReadBuffer.h"
     10 #include "SkStream.h"
     11 #include "SkTypeface.h"
     12 
     13 SkOrderedReadBuffer::SkOrderedReadBuffer() : INHERITED() {
     14     fMemoryPtr = NULL;
     15 
     16     fBitmapStorage = NULL;
     17     fTFArray = NULL;
     18     fTFCount = 0;
     19 
     20     fFactoryTDArray = NULL;
     21     fFactoryArray = NULL;
     22     fFactoryCount = 0;
     23     fBitmapDecoder = NULL;
     24 }
     25 
     26 SkOrderedReadBuffer::SkOrderedReadBuffer(const void* data, size_t size) : INHERITED()  {
     27     fReader.setMemory(data, size);
     28     fMemoryPtr = NULL;
     29 
     30     fBitmapStorage = NULL;
     31     fTFArray = NULL;
     32     fTFCount = 0;
     33 
     34     fFactoryTDArray = NULL;
     35     fFactoryArray = NULL;
     36     fFactoryCount = 0;
     37     fBitmapDecoder = NULL;
     38 }
     39 
     40 SkOrderedReadBuffer::SkOrderedReadBuffer(SkStream* stream) {
     41     const size_t length = stream->getLength();
     42     fMemoryPtr = sk_malloc_throw(length);
     43     stream->read(fMemoryPtr, length);
     44     fReader.setMemory(fMemoryPtr, length);
     45 
     46     fBitmapStorage = NULL;
     47     fTFArray = NULL;
     48     fTFCount = 0;
     49 
     50     fFactoryTDArray = NULL;
     51     fFactoryArray = NULL;
     52     fFactoryCount = 0;
     53     fBitmapDecoder = NULL;
     54 }
     55 
     56 SkOrderedReadBuffer::~SkOrderedReadBuffer() {
     57     sk_free(fMemoryPtr);
     58     SkSafeUnref(fBitmapStorage);
     59 }
     60 
     61 bool SkOrderedReadBuffer::readBool() {
     62     return fReader.readBool();
     63 }
     64 
     65 SkColor SkOrderedReadBuffer::readColor() {
     66     return fReader.readInt();
     67 }
     68 
     69 SkFixed SkOrderedReadBuffer::readFixed() {
     70     return fReader.readS32();
     71 }
     72 
     73 int32_t SkOrderedReadBuffer::readInt() {
     74     return fReader.readInt();
     75 }
     76 
     77 SkScalar SkOrderedReadBuffer::readScalar() {
     78     return fReader.readScalar();
     79 }
     80 
     81 uint32_t SkOrderedReadBuffer::readUInt() {
     82     return fReader.readU32();
     83 }
     84 
     85 int32_t SkOrderedReadBuffer::read32() {
     86     return fReader.readInt();
     87 }
     88 
     89 char* SkOrderedReadBuffer::readString() {
     90     const char* string = fReader.readString();
     91     const int32_t length = strlen(string);
     92     char* value = (char*)sk_malloc_throw(length + 1);
     93     strcpy(value, string);
     94     return value;
     95 }
     96 
     97 void* SkOrderedReadBuffer::readEncodedString(size_t* length, SkPaint::TextEncoding encoding) {
     98     SkDEBUGCODE(int32_t encodingType = ) fReader.readInt();
     99     SkASSERT(encodingType == encoding);
    100     *length =  fReader.readInt();
    101     void* data = sk_malloc_throw(*length);
    102     memcpy(data, fReader.skip(SkAlign4(*length)), *length);
    103     return data;
    104 }
    105 
    106 void SkOrderedReadBuffer::readPoint(SkPoint* point) {
    107     point->fX = fReader.readScalar();
    108     point->fY = fReader.readScalar();
    109 }
    110 
    111 void SkOrderedReadBuffer::readMatrix(SkMatrix* matrix) {
    112     fReader.readMatrix(matrix);
    113 }
    114 
    115 void SkOrderedReadBuffer::readIRect(SkIRect* rect) {
    116     memcpy(rect, fReader.skip(sizeof(SkIRect)), sizeof(SkIRect));
    117 }
    118 
    119 void SkOrderedReadBuffer::readRect(SkRect* rect) {
    120     memcpy(rect, fReader.skip(sizeof(SkRect)), sizeof(SkRect));
    121 }
    122 
    123 void SkOrderedReadBuffer::readRegion(SkRegion* region) {
    124     fReader.readRegion(region);
    125 }
    126 
    127 void SkOrderedReadBuffer::readPath(SkPath* path) {
    128     fReader.readPath(path);
    129 }
    130 
    131 uint32_t SkOrderedReadBuffer::readByteArray(void* value) {
    132     const uint32_t length = fReader.readU32();
    133     memcpy(value, fReader.skip(SkAlign4(length)), length);
    134     return length;
    135 }
    136 
    137 uint32_t SkOrderedReadBuffer::readColorArray(SkColor* colors) {
    138     const uint32_t count = fReader.readU32();
    139     const uint32_t byteLength = count * sizeof(SkColor);
    140     memcpy(colors, fReader.skip(SkAlign4(byteLength)), byteLength);
    141     return count;
    142 }
    143 
    144 uint32_t SkOrderedReadBuffer::readIntArray(int32_t* values) {
    145     const uint32_t count = fReader.readU32();
    146     const uint32_t byteLength = count * sizeof(int32_t);
    147     memcpy(values, fReader.skip(SkAlign4(byteLength)), byteLength);
    148     return count;
    149 }
    150 
    151 uint32_t SkOrderedReadBuffer::readPointArray(SkPoint* points) {
    152     const uint32_t count = fReader.readU32();
    153     const uint32_t byteLength = count * sizeof(SkPoint);
    154     memcpy(points, fReader.skip(SkAlign4(byteLength)), byteLength);
    155     return count;
    156 }
    157 
    158 uint32_t SkOrderedReadBuffer::readScalarArray(SkScalar* values) {
    159     const uint32_t count = fReader.readU32();
    160     const uint32_t byteLength = count * sizeof(SkScalar);
    161     memcpy(values, fReader.skip(SkAlign4(byteLength)), byteLength);
    162     return count;
    163 }
    164 
    165 uint32_t SkOrderedReadBuffer::getArrayCount() {
    166     return *(uint32_t*)fReader.peek();
    167 }
    168 
    169 void SkOrderedReadBuffer::readBitmap(SkBitmap* bitmap) {
    170     size_t length = this->readUInt();
    171     if (length > 0) {
    172         // Bitmap was encoded.
    173         SkMemoryStream stream(const_cast<void*>(this->skip(length)), length, false);
    174         if (fBitmapDecoder != NULL && fBitmapDecoder(&stream, bitmap)) {
    175             // Skip the width and height, which were written in case of failure.
    176             fReader.skip(2 * sizeof(int));
    177         } else {
    178             // This bitmap was encoded when written, but we are unable to decode, possibly due to
    179             // not having a decoder. Use a placeholder bitmap.
    180             SkDebugf("Could not decode bitmap. Resulting bitmap will be red.\n");
    181             int width = this->readInt();
    182             int height = this->readInt();
    183             bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height);
    184             bitmap->allocPixels();
    185             bitmap->eraseColor(SK_ColorRED);
    186         }
    187     } else {
    188         if (fBitmapStorage) {
    189             const uint32_t index = fReader.readU32();
    190             fReader.readU32(); // bitmap generation ID (see SkOrderedWriteBuffer::writeBitmap)
    191             *bitmap = *fBitmapStorage->getBitmap(index);
    192             fBitmapStorage->releaseRef(index);
    193         } else {
    194             bitmap->unflatten(*this);
    195         }
    196     }
    197 }
    198 
    199 SkTypeface* SkOrderedReadBuffer::readTypeface() {
    200 
    201     uint32_t index = fReader.readU32();
    202     if (0 == index || index > (unsigned)fTFCount) {
    203         if (index) {
    204             SkDebugf("====== typeface index %d\n", index);
    205         }
    206         return NULL;
    207     } else {
    208         SkASSERT(fTFArray);
    209         return fTFArray[index - 1];
    210     }
    211 }
    212 
    213 SkFlattenable* SkOrderedReadBuffer::readFlattenable() {
    214     SkFlattenable::Factory factory = NULL;
    215 
    216     if (fFactoryCount > 0) {
    217         int32_t index = fReader.readU32();
    218         if (0 == index) {
    219             return NULL; // writer failed to give us the flattenable
    220         }
    221         index -= 1;     // we stored the index-base-1
    222         SkASSERT(index < fFactoryCount);
    223         factory = fFactoryArray[index];
    224     } else if (fFactoryTDArray) {
    225         int32_t index = fReader.readU32();
    226         if (0 == index) {
    227             return NULL; // writer failed to give us the flattenable
    228         }
    229         index -= 1;     // we stored the index-base-1
    230         factory = (*fFactoryTDArray)[index];
    231     } else {
    232         factory = (SkFlattenable::Factory)readFunctionPtr();
    233         if (NULL == factory) {
    234             return NULL; // writer failed to give us the flattenable
    235         }
    236     }
    237 
    238     // if we get here, factory may still be null, but if that is the case, the
    239     // failure was ours, not the writer.
    240     SkFlattenable* obj = NULL;
    241     uint32_t sizeRecorded = fReader.readU32();
    242     if (factory) {
    243         uint32_t offset = fReader.offset();
    244         obj = (*factory)(*this);
    245         // check that we read the amount we expected
    246         uint32_t sizeRead = fReader.offset() - offset;
    247         if (sizeRecorded != sizeRead) {
    248             // we could try to fix up the offset...
    249             sk_throw();
    250         }
    251     } else {
    252         // we must skip the remaining data
    253         fReader.skip(sizeRecorded);
    254     }
    255     return obj;
    256 }
    257