Home | History | Annotate | Download | only in core
      1 /*
      2  * Copyright 2012 Google Inc.
      3  *
      4  * Use of this source code is governed by a BSD-style license that can be
      5  * found in the LICENSE file.
      6  */
      7 
      8 #include "SkWriteBuffer.h"
      9 #include "SkBitmap.h"
     10 #include "SkData.h"
     11 #include "SkDeduper.h"
     12 #include "SkPixelRef.h"
     13 #include "SkPtrRecorder.h"
     14 #include "SkStream.h"
     15 #include "SkTypeface.h"
     16 
     17 ///////////////////////////////////////////////////////////////////////////////////////////////////
     18 
     19 SkBinaryWriteBuffer::SkBinaryWriteBuffer(uint32_t flags)
     20     : fFlags(flags)
     21     , fFactorySet(nullptr)
     22     , fTFSet(nullptr) {
     23 }
     24 
     25 SkBinaryWriteBuffer::SkBinaryWriteBuffer(void* storage, size_t storageSize, uint32_t flags)
     26     : fFlags(flags)
     27     , fFactorySet(nullptr)
     28     , fWriter(storage, storageSize)
     29     , fTFSet(nullptr) {
     30 }
     31 
     32 SkBinaryWriteBuffer::~SkBinaryWriteBuffer() {
     33     SkSafeUnref(fFactorySet);
     34     SkSafeUnref(fTFSet);
     35 }
     36 
     37 void SkBinaryWriteBuffer::writeByteArray(const void* data, size_t size) {
     38     fWriter.write32(SkToU32(size));
     39     fWriter.writePad(data, size);
     40 }
     41 
     42 void SkBinaryWriteBuffer::writeBool(bool value) {
     43     fWriter.writeBool(value);
     44 }
     45 
     46 void SkBinaryWriteBuffer::writeScalar(SkScalar value) {
     47     fWriter.writeScalar(value);
     48 }
     49 
     50 void SkBinaryWriteBuffer::writeScalarArray(const SkScalar* value, uint32_t count) {
     51     fWriter.write32(count);
     52     fWriter.write(value, count * sizeof(SkScalar));
     53 }
     54 
     55 void SkBinaryWriteBuffer::writeInt(int32_t value) {
     56     fWriter.write32(value);
     57 }
     58 
     59 void SkBinaryWriteBuffer::writeIntArray(const int32_t* value, uint32_t count) {
     60     fWriter.write32(count);
     61     fWriter.write(value, count * sizeof(int32_t));
     62 }
     63 
     64 void SkBinaryWriteBuffer::writeUInt(uint32_t value) {
     65     fWriter.write32(value);
     66 }
     67 
     68 void SkBinaryWriteBuffer::writeString(const char* value) {
     69     fWriter.writeString(value);
     70 }
     71 
     72 void SkBinaryWriteBuffer::writeColor(SkColor color) {
     73     fWriter.write32(color);
     74 }
     75 
     76 void SkBinaryWriteBuffer::writeColorArray(const SkColor* color, uint32_t count) {
     77     fWriter.write32(count);
     78     fWriter.write(color, count * sizeof(SkColor));
     79 }
     80 
     81 void SkBinaryWriteBuffer::writeColor4f(const SkColor4f& color) {
     82     fWriter.write(&color, sizeof(SkColor4f));
     83 }
     84 
     85 void SkBinaryWriteBuffer::writeColor4fArray(const SkColor4f* color, uint32_t count) {
     86     fWriter.write32(count);
     87     fWriter.write(color, count * sizeof(SkColor4f));
     88 }
     89 
     90 void SkBinaryWriteBuffer::writePoint(const SkPoint& point) {
     91     fWriter.writeScalar(point.fX);
     92     fWriter.writeScalar(point.fY);
     93 }
     94 
     95 void SkBinaryWriteBuffer::writePointArray(const SkPoint* point, uint32_t count) {
     96     fWriter.write32(count);
     97     fWriter.write(point, count * sizeof(SkPoint));
     98 }
     99 
    100 void SkBinaryWriteBuffer::writeMatrix(const SkMatrix& matrix) {
    101     fWriter.writeMatrix(matrix);
    102 }
    103 
    104 void SkBinaryWriteBuffer::writeIRect(const SkIRect& rect) {
    105     fWriter.write(&rect, sizeof(SkIRect));
    106 }
    107 
    108 void SkBinaryWriteBuffer::writeRect(const SkRect& rect) {
    109     fWriter.writeRect(rect);
    110 }
    111 
    112 void SkBinaryWriteBuffer::writeRegion(const SkRegion& region) {
    113     fWriter.writeRegion(region);
    114 }
    115 
    116 void SkBinaryWriteBuffer::writePath(const SkPath& path) {
    117     fWriter.writePath(path);
    118 }
    119 
    120 size_t SkBinaryWriteBuffer::writeStream(SkStream* stream, size_t length) {
    121     fWriter.write32(SkToU32(length));
    122     size_t bytesWritten = fWriter.readFromStream(stream, length);
    123     if (bytesWritten < length) {
    124         fWriter.reservePad(length - bytesWritten);
    125     }
    126     return bytesWritten;
    127 }
    128 
    129 bool SkBinaryWriteBuffer::writeToStream(SkWStream* stream) {
    130     return fWriter.writeToStream(stream);
    131 }
    132 
    133 static void write_encoded_bitmap(SkBinaryWriteBuffer* buffer, SkData* data,
    134                                  const SkIPoint& origin) {
    135     buffer->writeDataAsByteArray(data);
    136     buffer->write32(origin.fX);
    137     buffer->write32(origin.fY);
    138 }
    139 
    140 void SkBinaryWriteBuffer::writeBitmap(const SkBitmap& bitmap) {
    141     // Record the width and height. This way if readBitmap fails a dummy bitmap can be drawn at the
    142     // right size.
    143     this->writeInt(bitmap.width());
    144     this->writeInt(bitmap.height());
    145 
    146     // Record information about the bitmap in one of two ways, in order of priority:
    147     // 1. If there is a function for encoding bitmaps, use it to write an encoded version of the
    148     //    bitmap. After writing a boolean value of false, signifying that a heap was not used, write
    149     //    the size of the encoded data. A non-zero size signifies that encoded data was written.
    150     // 2. Call SkBitmap::flatten. After writing a boolean value of false, signifying that a heap was
    151     //    not used, write a zero to signify that the data was not encoded.
    152 
    153     // Write a bool to indicate that we did not use an SkBitmapHeap. That feature is deprecated.
    154     this->writeBool(false);
    155 
    156     // see if the caller wants to manually encode
    157     SkAutoPixmapUnlock result;
    158     if (fPixelSerializer && bitmap.requestLock(&result)) {
    159         sk_sp<SkData> data(fPixelSerializer->encode(result.pixmap()));
    160         if (data) {
    161             // if we have to "encode" the bitmap, then we assume there is no
    162             // offset to share, since we are effectively creating a new pixelref
    163             write_encoded_bitmap(this, data.get(), SkIPoint::Make(0, 0));
    164             return;
    165         }
    166     }
    167 
    168     this->writeUInt(0); // signal raw pixels
    169     SkBitmap::WriteRawPixels(this, bitmap);
    170 }
    171 
    172 void SkBinaryWriteBuffer::writeImage(const SkImage* image) {
    173     if (fDeduper) {
    174         this->write32(fDeduper->findOrDefineImage(const_cast<SkImage*>(image)));
    175         return;
    176     }
    177 
    178     this->writeInt(image->width());
    179     this->writeInt(image->height());
    180 
    181     sk_sp<SkData> encoded(image->encode(this->getPixelSerializer()));
    182     if (encoded && encoded->size() > 0) {
    183         write_encoded_bitmap(this, encoded.get(), SkIPoint::Make(0, 0));
    184         return;
    185     }
    186 
    187     SkBitmap bm;
    188     if (image->asLegacyBitmap(&bm, SkImage::kRO_LegacyBitmapMode)) {
    189         this->writeUInt(1);  // signal raw pixels.
    190         SkBitmap::WriteRawPixels(this, bm);
    191         return;
    192     }
    193 
    194     this->writeUInt(0); // signal no pixels (in place of the size of the encoded data)
    195 }
    196 
    197 void SkBinaryWriteBuffer::writeTypeface(SkTypeface* obj) {
    198     if (fDeduper) {
    199         this->write32(fDeduper->findOrDefineTypeface(obj));
    200         return;
    201     }
    202 
    203     if (nullptr == obj || nullptr == fTFSet) {
    204         fWriter.write32(0);
    205     } else {
    206         fWriter.write32(fTFSet->add(obj));
    207     }
    208 }
    209 
    210 void SkBinaryWriteBuffer::writePaint(const SkPaint& paint) {
    211     paint.flatten(*this);
    212 }
    213 
    214 SkFactorySet* SkBinaryWriteBuffer::setFactoryRecorder(SkFactorySet* rec) {
    215     SkRefCnt_SafeAssign(fFactorySet, rec);
    216     return rec;
    217 }
    218 
    219 SkRefCntSet* SkBinaryWriteBuffer::setTypefaceRecorder(SkRefCntSet* rec) {
    220     SkRefCnt_SafeAssign(fTFSet, rec);
    221     return rec;
    222 }
    223 
    224 void SkBinaryWriteBuffer::setPixelSerializer(sk_sp<SkPixelSerializer> serializer) {
    225     fPixelSerializer = std::move(serializer);
    226 }
    227 
    228 void SkBinaryWriteBuffer::writeFlattenable(const SkFlattenable* flattenable) {
    229     if (nullptr == flattenable) {
    230         this->write32(0);
    231         return;
    232     }
    233 
    234     if (fDeduper) {
    235         this->write32(fDeduper->findOrDefineFactory(const_cast<SkFlattenable*>(flattenable)));
    236     } else {
    237         /*
    238          *  We can write 1 of 2 versions of the flattenable:
    239          *  1.  index into fFactorySet : This assumes the writer will later
    240          *      resolve the function-ptrs into strings for its reader. SkPicture
    241          *      does exactly this, by writing a table of names (matching the indices)
    242          *      up front in its serialized form.
    243          *  2.  string name of the flattenable or index into fFlattenableDict:  We
    244          *      store the string to allow the reader to specify its own factories
    245          *      after write time.  In order to improve compression, if we have
    246          *      already written the string, we write its index instead.
    247          */
    248         if (fFactorySet) {
    249             SkFlattenable::Factory factory = flattenable->getFactory();
    250             SkASSERT(factory);
    251             this->write32(fFactorySet->add(factory));
    252         } else {
    253             const char* name = flattenable->getTypeName();
    254             SkASSERT(name);
    255             SkString key(name);
    256             if (uint32_t* indexPtr = fFlattenableDict.find(key)) {
    257                 // We will write the index as a 32-bit int.  We want the first byte
    258                 // that we send to be zero - this will act as a sentinel that we
    259                 // have an index (not a string).  This means that we will send the
    260                 // the index shifted left by 8.  The remaining 24-bits should be
    261                 // plenty to store the index.  Note that this strategy depends on
    262                 // being little endian.
    263                 SkASSERT(0 == *indexPtr >> 24);
    264                 this->write32(*indexPtr << 8);
    265             } else {
    266                 // Otherwise write the string.  Clients should not use the empty
    267                 // string as a name, or we will have a problem.
    268                 SkASSERT(strcmp("", name));
    269                 this->writeString(name);
    270 
    271                 // Add key to dictionary.
    272                 fFlattenableDict.set(key, fFlattenableDict.count() + 1);
    273             }
    274         }
    275     }
    276 
    277     // make room for the size of the flattened object
    278     (void)fWriter.reserve(sizeof(uint32_t));
    279     // record the current size, so we can subtract after the object writes.
    280     size_t offset = fWriter.bytesWritten();
    281     // now flatten the object
    282     flattenable->flatten(*this);
    283     size_t objSize = fWriter.bytesWritten() - offset;
    284     // record the obj's size
    285     fWriter.overwriteTAt(offset - sizeof(uint32_t), SkToU32(objSize));
    286 }
    287