LCOV - code coverage report
Current view: top level - gfx/skia/skia/src/core - SkWriteBuffer.cpp (source / functions) Hit Total Coverage
Test: output.info Lines: 7 158 4.4 %
Date: 2017-07-14 16:53:18 Functions: 2 34 5.9 %
Legend: Lines: hit not hit

          Line data    Source code
       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          63 : SkBinaryWriteBuffer::SkBinaryWriteBuffer(uint32_t flags)
      20             :     : fFlags(flags)
      21             :     , fFactorySet(nullptr)
      22          63 :     , fTFSet(nullptr) {
      23          63 : }
      24             : 
      25           0 : SkBinaryWriteBuffer::SkBinaryWriteBuffer(void* storage, size_t storageSize, uint32_t flags)
      26             :     : fFlags(flags)
      27             :     , fFactorySet(nullptr)
      28             :     , fWriter(storage, storageSize)
      29           0 :     , fTFSet(nullptr) {
      30           0 : }
      31             : 
      32         126 : SkBinaryWriteBuffer::~SkBinaryWriteBuffer() {
      33          63 :     SkSafeUnref(fFactorySet);
      34          63 :     SkSafeUnref(fTFSet);
      35          63 : }
      36             : 
      37           0 : void SkBinaryWriteBuffer::writeByteArray(const void* data, size_t size) {
      38           0 :     fWriter.write32(SkToU32(size));
      39           0 :     fWriter.writePad(data, size);
      40           0 : }
      41             : 
      42           0 : void SkBinaryWriteBuffer::writeBool(bool value) {
      43           0 :     fWriter.writeBool(value);
      44           0 : }
      45             : 
      46           0 : void SkBinaryWriteBuffer::writeScalar(SkScalar value) {
      47           0 :     fWriter.writeScalar(value);
      48           0 : }
      49             : 
      50           0 : void SkBinaryWriteBuffer::writeScalarArray(const SkScalar* value, uint32_t count) {
      51           0 :     fWriter.write32(count);
      52           0 :     fWriter.write(value, count * sizeof(SkScalar));
      53           0 : }
      54             : 
      55           0 : void SkBinaryWriteBuffer::writeInt(int32_t value) {
      56           0 :     fWriter.write32(value);
      57           0 : }
      58             : 
      59           0 : void SkBinaryWriteBuffer::writeIntArray(const int32_t* value, uint32_t count) {
      60           0 :     fWriter.write32(count);
      61           0 :     fWriter.write(value, count * sizeof(int32_t));
      62           0 : }
      63             : 
      64           0 : void SkBinaryWriteBuffer::writeUInt(uint32_t value) {
      65           0 :     fWriter.write32(value);
      66           0 : }
      67             : 
      68           0 : void SkBinaryWriteBuffer::writeString(const char* value) {
      69           0 :     fWriter.writeString(value);
      70           0 : }
      71             : 
      72           0 : void SkBinaryWriteBuffer::writeColor(SkColor color) {
      73           0 :     fWriter.write32(color);
      74           0 : }
      75             : 
      76           0 : void SkBinaryWriteBuffer::writeColorArray(const SkColor* color, uint32_t count) {
      77           0 :     fWriter.write32(count);
      78           0 :     fWriter.write(color, count * sizeof(SkColor));
      79           0 : }
      80             : 
      81           0 : void SkBinaryWriteBuffer::writeColor4f(const SkColor4f& color) {
      82           0 :     fWriter.write(&color, sizeof(SkColor4f));
      83           0 : }
      84             : 
      85           0 : void SkBinaryWriteBuffer::writeColor4fArray(const SkColor4f* color, uint32_t count) {
      86           0 :     fWriter.write32(count);
      87           0 :     fWriter.write(color, count * sizeof(SkColor4f));
      88           0 : }
      89             : 
      90           0 : void SkBinaryWriteBuffer::writePoint(const SkPoint& point) {
      91           0 :     fWriter.writeScalar(point.fX);
      92           0 :     fWriter.writeScalar(point.fY);
      93           0 : }
      94             : 
      95           0 : void SkBinaryWriteBuffer::writePointArray(const SkPoint* point, uint32_t count) {
      96           0 :     fWriter.write32(count);
      97           0 :     fWriter.write(point, count * sizeof(SkPoint));
      98           0 : }
      99             : 
     100           0 : void SkBinaryWriteBuffer::writeMatrix(const SkMatrix& matrix) {
     101           0 :     fWriter.writeMatrix(matrix);
     102           0 : }
     103             : 
     104           0 : void SkBinaryWriteBuffer::writeIRect(const SkIRect& rect) {
     105           0 :     fWriter.write(&rect, sizeof(SkIRect));
     106           0 : }
     107             : 
     108           0 : void SkBinaryWriteBuffer::writeRect(const SkRect& rect) {
     109           0 :     fWriter.writeRect(rect);
     110           0 : }
     111             : 
     112           0 : void SkBinaryWriteBuffer::writeRegion(const SkRegion& region) {
     113           0 :     fWriter.writeRegion(region);
     114           0 : }
     115             : 
     116           0 : void SkBinaryWriteBuffer::writePath(const SkPath& path) {
     117           0 :     fWriter.writePath(path);
     118           0 : }
     119             : 
     120           0 : size_t SkBinaryWriteBuffer::writeStream(SkStream* stream, size_t length) {
     121           0 :     fWriter.write32(SkToU32(length));
     122           0 :     size_t bytesWritten = fWriter.readFromStream(stream, length);
     123           0 :     if (bytesWritten < length) {
     124           0 :         fWriter.reservePad(length - bytesWritten);
     125             :     }
     126           0 :     return bytesWritten;
     127             : }
     128             : 
     129           0 : bool SkBinaryWriteBuffer::writeToStream(SkWStream* stream) {
     130           0 :     return fWriter.writeToStream(stream);
     131             : }
     132             : 
     133           0 : static void write_encoded_bitmap(SkBinaryWriteBuffer* buffer, SkData* data,
     134             :                                  const SkIPoint& origin) {
     135           0 :     buffer->writeDataAsByteArray(data);
     136           0 :     buffer->write32(origin.fX);
     137           0 :     buffer->write32(origin.fY);
     138           0 : }
     139             : 
     140           0 : 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           0 :     this->writeInt(bitmap.width());
     144           0 :     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           0 :     this->writeBool(false);
     155             : 
     156             :     // see if the caller wants to manually encode
     157           0 :     SkAutoPixmapUnlock result;
     158           0 :     if (fPixelSerializer && bitmap.requestLock(&result)) {
     159           0 :         sk_sp<SkData> data(fPixelSerializer->encode(result.pixmap()));
     160           0 :         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           0 :             write_encoded_bitmap(this, data.get(), SkIPoint::Make(0, 0));
     164           0 :             return;
     165             :         }
     166             :     }
     167             : 
     168           0 :     this->writeUInt(0); // signal raw pixels
     169           0 :     SkBitmap::WriteRawPixels(this, bitmap);
     170             : }
     171             : 
     172           0 : void SkBinaryWriteBuffer::writeImage(const SkImage* image) {
     173           0 :     if (fDeduper) {
     174           0 :         this->write32(fDeduper->findOrDefineImage(const_cast<SkImage*>(image)));
     175           0 :         return;
     176             :     }
     177             : 
     178           0 :     this->writeInt(image->width());
     179           0 :     this->writeInt(image->height());
     180             : 
     181           0 :     sk_sp<SkData> encoded(image->encode(this->getPixelSerializer()));
     182           0 :     if (encoded && encoded->size() > 0) {
     183           0 :         write_encoded_bitmap(this, encoded.get(), SkIPoint::Make(0, 0));
     184           0 :         return;
     185             :     }
     186             : 
     187           0 :     SkBitmap bm;
     188           0 :     if (image->asLegacyBitmap(&bm, SkImage::kRO_LegacyBitmapMode)) {
     189           0 :         this->writeUInt(1);  // signal raw pixels.
     190           0 :         SkBitmap::WriteRawPixels(this, bm);
     191           0 :         return;
     192             :     }
     193             : 
     194           0 :     this->writeUInt(0); // signal no pixels (in place of the size of the encoded data)
     195             : }
     196             : 
     197           0 : void SkBinaryWriteBuffer::writeTypeface(SkTypeface* obj) {
     198           0 :     if (fDeduper) {
     199           0 :         this->write32(fDeduper->findOrDefineTypeface(obj));
     200           0 :         return;
     201             :     }
     202             : 
     203           0 :     if (nullptr == obj || nullptr == fTFSet) {
     204           0 :         fWriter.write32(0);
     205             :     } else {
     206           0 :         fWriter.write32(fTFSet->add(obj));
     207             :     }
     208             : }
     209             : 
     210           0 : void SkBinaryWriteBuffer::writePaint(const SkPaint& paint) {
     211           0 :     paint.flatten(*this);
     212           0 : }
     213             : 
     214           0 : SkFactorySet* SkBinaryWriteBuffer::setFactoryRecorder(SkFactorySet* rec) {
     215           0 :     SkRefCnt_SafeAssign(fFactorySet, rec);
     216           0 :     return rec;
     217             : }
     218             : 
     219           0 : SkRefCntSet* SkBinaryWriteBuffer::setTypefaceRecorder(SkRefCntSet* rec) {
     220           0 :     SkRefCnt_SafeAssign(fTFSet, rec);
     221           0 :     return rec;
     222             : }
     223             : 
     224           0 : void SkBinaryWriteBuffer::setPixelSerializer(sk_sp<SkPixelSerializer> serializer) {
     225           0 :     fPixelSerializer = std::move(serializer);
     226           0 : }
     227             : 
     228           0 : void SkBinaryWriteBuffer::writeFlattenable(const SkFlattenable* flattenable) {
     229           0 :     if (nullptr == flattenable) {
     230           0 :         this->write32(0);
     231           0 :         return;
     232             :     }
     233             : 
     234           0 :     if (fDeduper) {
     235           0 :         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           0 :         if (fFactorySet) {
     249           0 :             SkFlattenable::Factory factory = flattenable->getFactory();
     250           0 :             SkASSERT(factory);
     251           0 :             this->write32(fFactorySet->add(factory));
     252             :         } else {
     253           0 :             const char* name = flattenable->getTypeName();
     254           0 :             SkASSERT(name);
     255           0 :             SkString key(name);
     256           0 :             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           0 :                 SkASSERT(0 == *indexPtr >> 24);
     264           0 :                 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           0 :                 SkASSERT(strcmp("", name));
     269           0 :                 this->writeString(name);
     270             : 
     271             :                 // Add key to dictionary.
     272           0 :                 fFlattenableDict.set(key, fFlattenableDict.count() + 1);
     273             :             }
     274             :         }
     275             :     }
     276             : 
     277             :     // make room for the size of the flattened object
     278           0 :     (void)fWriter.reserve(sizeof(uint32_t));
     279             :     // record the current size, so we can subtract after the object writes.
     280           0 :     size_t offset = fWriter.bytesWritten();
     281             :     // now flatten the object
     282           0 :     flattenable->flatten(*this);
     283           0 :     size_t objSize = fWriter.bytesWritten() - offset;
     284             :     // record the obj's size
     285           0 :     fWriter.overwriteTAt(offset - sizeof(uint32_t), SkToU32(objSize));
     286             : }

Generated by: LCOV version 1.13