| 152 | } |
| 153 | |
| 154 | void SkWriteBuffer::writeBitmap(const SkBitmap& bitmap) { |
| 155 | // Record the width and height. This way if readBitmap fails a dummy bitmap can be drawn at the |
| 156 | // right size. |
| 157 | this->writeInt(bitmap.width()); |
| 158 | this->writeInt(bitmap.height()); |
| 159 | |
| 160 | // Record information about the bitmap in one of three ways, in order of priority: |
| 161 | // 1. If there is an SkBitmapHeap, store it in the heap. The client can avoid serializing the |
| 162 | // bitmap entirely or serialize it later as desired. A boolean value of true will be written |
| 163 | // to the stream to signify that a heap was used. |
| 164 | // 2. If there is a function for encoding bitmaps, use it to write an encoded version of the |
| 165 | // bitmap. After writing a boolean value of false, signifying that a heap was not used, write |
| 166 | // the size of the encoded data. A non-zero size signifies that encoded data was written. |
| 167 | // 3. Call SkBitmap::flatten. After writing a boolean value of false, signifying that a heap was |
| 168 | // not used, write a zero to signify that the data was not encoded. |
| 169 | bool useBitmapHeap = fBitmapHeap != NULL; |
| 170 | // Write a bool: true if the SkBitmapHeap is to be used, in which case the reader must use an |
| 171 | // SkBitmapHeapReader to read the SkBitmap. False if the bitmap was serialized another way. |
| 172 | this->writeBool(useBitmapHeap); |
| 173 | if (useBitmapHeap) { |
| 174 | SkASSERT(NULL == fBitmapEncoder); |
| 175 | int32_t slot = fBitmapHeap->insert(bitmap); |
| 176 | fWriter.write32(slot); |
| 177 | // crbug.com/155875 |
| 178 | // The generation ID is not required information. We write it to prevent collisions |
| 179 | // in SkFlatDictionary. It is possible to get a collision when a previously |
| 180 | // unflattened (i.e. stale) instance of a similar flattenable is in the dictionary |
| 181 | // and the instance currently being written is re-using the same slot from the |
| 182 | // bitmap heap. |
| 183 | fWriter.write32(bitmap.getGenerationID()); |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | // see if the pixelref already has an encoded version |
| 188 | if (bitmap.pixelRef()) { |
| 189 | SkAutoDataUnref data(bitmap.pixelRef()->refEncodedData()); |
| 190 | if (data.get() != NULL) { |
| 191 | write_encoded_bitmap(this, data, bitmap.pixelRefOrigin()); |
| 192 | return; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | // see if the caller wants to manually encode |
| 197 | if (fBitmapEncoder != NULL) { |
| 198 | SkASSERT(NULL == fBitmapHeap); |
| 199 | size_t offset = 0; // this parameter is deprecated/ignored |
| 200 | // if we have to "encode" the bitmap, then we assume there is no |
| 201 | // offset to share, since we are effectively creating a new pixelref |
| 202 | SkAutoDataUnref data(fBitmapEncoder(&offset, bitmap)); |
| 203 | if (data.get() != NULL) { |
| 204 | write_encoded_bitmap(this, data, SkIPoint::Make(0, 0)); |
| 205 | return; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | this->writeUInt(0); // signal raw pixels |
| 210 | SkBitmap::WriteRawPixels(this, bitmap); |
| 211 | } |
no test coverage detected