OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2006 The Android Open Source Project | 3 * Copyright 2006 The Android Open Source Project |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 #include "SkStream.h" | 10 #include "SkStream.h" |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 return false; | 174 return false; |
175 } | 175 } |
176 length -= n; | 176 length -= n; |
177 } | 177 } |
178 return true; | 178 return true; |
179 } | 179 } |
180 | 180 |
181 /////////////////////////////////////////////////////////////////////////////// | 181 /////////////////////////////////////////////////////////////////////////////// |
182 | 182 |
183 SkFILEStream::SkFILEStream(const char file[]) : fName(file), fOwnership(kCallerP
asses_Ownership) { | 183 SkFILEStream::SkFILEStream(const char file[]) : fName(file), fOwnership(kCallerP
asses_Ownership) { |
184 fFILE = file ? sk_fopen(fName.c_str(), kRead_SkFILE_Flag) : NULL; | 184 fFILE = file ? sk_fopen(fName.c_str(), kRead_SkFILE_Flag) : nullptr; |
185 } | 185 } |
186 | 186 |
187 SkFILEStream::SkFILEStream(FILE* file, Ownership ownership) | 187 SkFILEStream::SkFILEStream(FILE* file, Ownership ownership) |
188 : fFILE((SkFILE*)file) | 188 : fFILE((SkFILE*)file) |
189 , fOwnership(ownership) { | 189 , fOwnership(ownership) { |
190 } | 190 } |
191 | 191 |
192 SkFILEStream::~SkFILEStream() { | 192 SkFILEStream::~SkFILEStream() { |
193 if (fFILE && fOwnership != kCallerRetains_Ownership) { | 193 if (fFILE && fOwnership != kCallerRetains_Ownership) { |
194 sk_fclose(fFILE); | 194 sk_fclose(fFILE); |
195 } | 195 } |
196 } | 196 } |
197 | 197 |
198 void SkFILEStream::setPath(const char path[]) { | 198 void SkFILEStream::setPath(const char path[]) { |
199 fName.set(path); | 199 fName.set(path); |
200 if (fFILE) { | 200 if (fFILE) { |
201 sk_fclose(fFILE); | 201 sk_fclose(fFILE); |
202 fFILE = NULL; | 202 fFILE = nullptr; |
203 } | 203 } |
204 if (path) { | 204 if (path) { |
205 fFILE = sk_fopen(fName.c_str(), kRead_SkFILE_Flag); | 205 fFILE = sk_fopen(fName.c_str(), kRead_SkFILE_Flag); |
206 } | 206 } |
207 } | 207 } |
208 | 208 |
209 size_t SkFILEStream::read(void* buffer, size_t size) { | 209 size_t SkFILEStream::read(void* buffer, size_t size) { |
210 if (fFILE) { | 210 if (fFILE) { |
211 return sk_fread(buffer, size, fFILE); | 211 return sk_fread(buffer, size, fFILE); |
212 } | 212 } |
213 return 0; | 213 return 0; |
214 } | 214 } |
215 | 215 |
216 bool SkFILEStream::isAtEnd() const { | 216 bool SkFILEStream::isAtEnd() const { |
217 return sk_feof(fFILE); | 217 return sk_feof(fFILE); |
218 } | 218 } |
219 | 219 |
220 bool SkFILEStream::rewind() { | 220 bool SkFILEStream::rewind() { |
221 if (fFILE) { | 221 if (fFILE) { |
222 if (sk_frewind(fFILE)) { | 222 if (sk_frewind(fFILE)) { |
223 return true; | 223 return true; |
224 } | 224 } |
225 // we hit an error | 225 // we hit an error |
226 sk_fclose(fFILE); | 226 sk_fclose(fFILE); |
227 fFILE = NULL; | 227 fFILE = nullptr; |
228 } | 228 } |
229 return false; | 229 return false; |
230 } | 230 } |
231 | 231 |
232 SkStreamAsset* SkFILEStream::duplicate() const { | 232 SkStreamAsset* SkFILEStream::duplicate() const { |
233 if (NULL == fFILE) { | 233 if (nullptr == fFILE) { |
234 return new SkMemoryStream(); | 234 return new SkMemoryStream(); |
235 } | 235 } |
236 | 236 |
237 if (fData.get()) { | 237 if (fData.get()) { |
238 return new SkMemoryStream(fData); | 238 return new SkMemoryStream(fData); |
239 } | 239 } |
240 | 240 |
241 if (!fName.isEmpty()) { | 241 if (!fName.isEmpty()) { |
242 SkAutoTDelete<SkFILEStream> that(new SkFILEStream(fName.c_str())); | 242 SkAutoTDelete<SkFILEStream> that(new SkFILEStream(fName.c_str())); |
243 if (sk_fidentical(that->fFILE, this->fFILE)) { | 243 if (sk_fidentical(that->fFILE, this->fFILE)) { |
244 return that.detach(); | 244 return that.detach(); |
245 } | 245 } |
246 } | 246 } |
247 | 247 |
248 fData.reset(SkData::NewFromFILE(fFILE)); | 248 fData.reset(SkData::NewFromFILE(fFILE)); |
249 if (NULL == fData.get()) { | 249 if (nullptr == fData.get()) { |
250 return NULL; | 250 return nullptr; |
251 } | 251 } |
252 return new SkMemoryStream(fData); | 252 return new SkMemoryStream(fData); |
253 } | 253 } |
254 | 254 |
255 size_t SkFILEStream::getPosition() const { | 255 size_t SkFILEStream::getPosition() const { |
256 return sk_ftell(fFILE); | 256 return sk_ftell(fFILE); |
257 } | 257 } |
258 | 258 |
259 bool SkFILEStream::seek(size_t position) { | 259 bool SkFILEStream::seek(size_t position) { |
260 return sk_fseek(fFILE, position); | 260 return sk_fseek(fFILE, position); |
261 } | 261 } |
262 | 262 |
263 bool SkFILEStream::move(long offset) { | 263 bool SkFILEStream::move(long offset) { |
264 return sk_fmove(fFILE, offset); | 264 return sk_fmove(fFILE, offset); |
265 } | 265 } |
266 | 266 |
267 SkStreamAsset* SkFILEStream::fork() const { | 267 SkStreamAsset* SkFILEStream::fork() const { |
268 SkAutoTDelete<SkStreamAsset> that(this->duplicate()); | 268 SkAutoTDelete<SkStreamAsset> that(this->duplicate()); |
269 that->seek(this->getPosition()); | 269 that->seek(this->getPosition()); |
270 return that.detach(); | 270 return that.detach(); |
271 } | 271 } |
272 | 272 |
273 size_t SkFILEStream::getLength() const { | 273 size_t SkFILEStream::getLength() const { |
274 return sk_fgetsize(fFILE); | 274 return sk_fgetsize(fFILE); |
275 } | 275 } |
276 | 276 |
277 const void* SkFILEStream::getMemoryBase() { | 277 const void* SkFILEStream::getMemoryBase() { |
278 if (NULL == fData.get()) { | 278 if (nullptr == fData.get()) { |
279 return NULL; | 279 return nullptr; |
280 } | 280 } |
281 return fData->data(); | 281 return fData->data(); |
282 } | 282 } |
283 | 283 |
284 /////////////////////////////////////////////////////////////////////////////// | 284 /////////////////////////////////////////////////////////////////////////////// |
285 | 285 |
286 static SkData* newFromParams(const void* src, size_t size, bool copyData) { | 286 static SkData* newFromParams(const void* src, size_t size, bool copyData) { |
287 if (copyData) { | 287 if (copyData) { |
288 return SkData::NewWithCopy(src, size); | 288 return SkData::NewWithCopy(src, size); |
289 } else { | 289 } else { |
(...skipping 10 matching lines...) Expand all Loading... |
300 fData = SkData::NewUninitialized(size); | 300 fData = SkData::NewUninitialized(size); |
301 fOffset = 0; | 301 fOffset = 0; |
302 } | 302 } |
303 | 303 |
304 SkMemoryStream::SkMemoryStream(const void* src, size_t size, bool copyData) { | 304 SkMemoryStream::SkMemoryStream(const void* src, size_t size, bool copyData) { |
305 fData = newFromParams(src, size, copyData); | 305 fData = newFromParams(src, size, copyData); |
306 fOffset = 0; | 306 fOffset = 0; |
307 } | 307 } |
308 | 308 |
309 SkMemoryStream::SkMemoryStream(SkData* data) { | 309 SkMemoryStream::SkMemoryStream(SkData* data) { |
310 if (NULL == data) { | 310 if (nullptr == data) { |
311 fData = SkData::NewEmpty(); | 311 fData = SkData::NewEmpty(); |
312 } else { | 312 } else { |
313 fData = data; | 313 fData = data; |
314 fData->ref(); | 314 fData->ref(); |
315 } | 315 } |
316 fOffset = 0; | 316 fOffset = 0; |
317 } | 317 } |
318 | 318 |
319 SkMemoryStream::~SkMemoryStream() { | 319 SkMemoryStream::~SkMemoryStream() { |
320 fData->unref(); | 320 fData->unref(); |
(...skipping 11 matching lines...) Expand all Loading... |
332 fOffset = 0; | 332 fOffset = 0; |
333 } | 333 } |
334 | 334 |
335 SkData* SkMemoryStream::copyToData() const { | 335 SkData* SkMemoryStream::copyToData() const { |
336 fData->ref(); | 336 fData->ref(); |
337 return fData; | 337 return fData; |
338 } | 338 } |
339 | 339 |
340 SkData* SkMemoryStream::setData(SkData* data) { | 340 SkData* SkMemoryStream::setData(SkData* data) { |
341 fData->unref(); | 341 fData->unref(); |
342 if (NULL == data) { | 342 if (nullptr == data) { |
343 fData = SkData::NewEmpty(); | 343 fData = SkData::NewEmpty(); |
344 } else { | 344 } else { |
345 fData = data; | 345 fData = data; |
346 fData->ref(); | 346 fData->ref(); |
347 } | 347 } |
348 fOffset = 0; | 348 fOffset = 0; |
349 return data; | 349 return data; |
350 } | 350 } |
351 | 351 |
352 void SkMemoryStream::skipToAlign4() { | 352 void SkMemoryStream::skipToAlign4() { |
353 // cast to remove unary-minus warning | 353 // cast to remove unary-minus warning |
354 fOffset += -(int)fOffset & 0x03; | 354 fOffset += -(int)fOffset & 0x03; |
355 } | 355 } |
356 | 356 |
357 size_t SkMemoryStream::read(void* buffer, size_t size) { | 357 size_t SkMemoryStream::read(void* buffer, size_t size) { |
358 size_t dataSize = fData->size(); | 358 size_t dataSize = fData->size(); |
359 | 359 |
360 if (size > dataSize - fOffset) { | 360 if (size > dataSize - fOffset) { |
361 size = dataSize - fOffset; | 361 size = dataSize - fOffset; |
362 } | 362 } |
363 if (buffer) { | 363 if (buffer) { |
364 memcpy(buffer, fData->bytes() + fOffset, size); | 364 memcpy(buffer, fData->bytes() + fOffset, size); |
365 } | 365 } |
366 fOffset += size; | 366 fOffset += size; |
367 return size; | 367 return size; |
368 } | 368 } |
369 | 369 |
370 bool SkMemoryStream::peek(void* buffer, size_t size) const { | 370 bool SkMemoryStream::peek(void* buffer, size_t size) const { |
371 SkASSERT(buffer != NULL); | 371 SkASSERT(buffer != nullptr); |
372 const size_t position = fOffset; | 372 const size_t position = fOffset; |
373 if (size > fData->size() - position) { | 373 if (size > fData->size() - position) { |
374 // The stream is not large enough to satisfy this request. | 374 // The stream is not large enough to satisfy this request. |
375 return false; | 375 return false; |
376 } | 376 } |
377 SkMemoryStream* nonConstThis = const_cast<SkMemoryStream*>(this); | 377 SkMemoryStream* nonConstThis = const_cast<SkMemoryStream*>(this); |
378 SkDEBUGCODE(const size_t bytesRead =) nonConstThis->read(buffer, size); | 378 SkDEBUGCODE(const size_t bytesRead =) nonConstThis->read(buffer, size); |
379 SkASSERT(bytesRead == size); | 379 SkASSERT(bytesRead == size); |
380 nonConstThis->fOffset = position; | 380 nonConstThis->fOffset = position; |
381 return true; | 381 return true; |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
439 sk_fclose(fFILE); | 439 sk_fclose(fFILE); |
440 } | 440 } |
441 } | 441 } |
442 | 442 |
443 size_t SkFILEWStream::bytesWritten() const { | 443 size_t SkFILEWStream::bytesWritten() const { |
444 return sk_ftell(fFILE); | 444 return sk_ftell(fFILE); |
445 } | 445 } |
446 | 446 |
447 bool SkFILEWStream::write(const void* buffer, size_t size) | 447 bool SkFILEWStream::write(const void* buffer, size_t size) |
448 { | 448 { |
449 if (fFILE == NULL) { | 449 if (fFILE == nullptr) { |
450 return false; | 450 return false; |
451 } | 451 } |
452 | 452 |
453 if (sk_fwrite(buffer, size, fFILE) != size) | 453 if (sk_fwrite(buffer, size, fFILE) != size) |
454 { | 454 { |
455 SkDEBUGCODE(SkDebugf("SkFILEWStream failed writing %d bytes\n", size);) | 455 SkDEBUGCODE(SkDebugf("SkFILEWStream failed writing %d bytes\n", size);) |
456 sk_fclose(fFILE); | 456 sk_fclose(fFILE); |
457 fFILE = NULL; | 457 fFILE = nullptr; |
458 return false; | 458 return false; |
459 } | 459 } |
460 return true; | 460 return true; |
461 } | 461 } |
462 | 462 |
463 void SkFILEWStream::flush() | 463 void SkFILEWStream::flush() |
464 { | 464 { |
465 if (fFILE) { | 465 if (fFILE) { |
466 sk_fflush(fFILE); | 466 sk_fflush(fFILE); |
467 } | 467 } |
(...skipping 25 matching lines...) Expand all Loading... |
493 char* fCurr; | 493 char* fCurr; |
494 char* fStop; | 494 char* fStop; |
495 | 495 |
496 const char* start() const { return (const char*)(this + 1); } | 496 const char* start() const { return (const char*)(this + 1); } |
497 char* start() { return (char*)(this + 1); } | 497 char* start() { return (char*)(this + 1); } |
498 size_t avail() const { return fStop - fCurr; } | 498 size_t avail() const { return fStop - fCurr; } |
499 size_t written() const { return fCurr - this->start(); } | 499 size_t written() const { return fCurr - this->start(); } |
500 | 500 |
501 void init(size_t size) | 501 void init(size_t size) |
502 { | 502 { |
503 fNext = NULL; | 503 fNext = nullptr; |
504 fCurr = this->start(); | 504 fCurr = this->start(); |
505 fStop = this->start() + size; | 505 fStop = this->start() + size; |
506 } | 506 } |
507 | 507 |
508 const void* append(const void* data, size_t size) | 508 const void* append(const void* data, size_t size) |
509 { | 509 { |
510 SkASSERT((size_t)(fStop - fCurr) >= size); | 510 SkASSERT((size_t)(fStop - fCurr) >= size); |
511 memcpy(fCurr, data, size); | 511 memcpy(fCurr, data, size); |
512 fCurr += size; | 512 fCurr += size; |
513 return (const void*)((const char*)data + size); | 513 return (const void*)((const char*)data + size); |
514 } | 514 } |
515 }; | 515 }; |
516 | 516 |
517 SkDynamicMemoryWStream::SkDynamicMemoryWStream() | 517 SkDynamicMemoryWStream::SkDynamicMemoryWStream() |
518 : fHead(NULL), fTail(NULL), fBytesWritten(0), fCopy(NULL) | 518 : fHead(nullptr), fTail(nullptr), fBytesWritten(0), fCopy(nullptr) |
519 { | 519 { |
520 } | 520 } |
521 | 521 |
522 SkDynamicMemoryWStream::~SkDynamicMemoryWStream() | 522 SkDynamicMemoryWStream::~SkDynamicMemoryWStream() |
523 { | 523 { |
524 reset(); | 524 reset(); |
525 } | 525 } |
526 | 526 |
527 void SkDynamicMemoryWStream::reset() | 527 void SkDynamicMemoryWStream::reset() |
528 { | 528 { |
529 this->invalidateCopy(); | 529 this->invalidateCopy(); |
530 | 530 |
531 Block* block = fHead; | 531 Block* block = fHead; |
532 | 532 |
533 while (block != NULL) { | 533 while (block != nullptr) { |
534 Block* next = block->fNext; | 534 Block* next = block->fNext; |
535 sk_free(block); | 535 sk_free(block); |
536 block = next; | 536 block = next; |
537 } | 537 } |
538 fHead = fTail = NULL; | 538 fHead = fTail = nullptr; |
539 fBytesWritten = 0; | 539 fBytesWritten = 0; |
540 } | 540 } |
541 | 541 |
542 bool SkDynamicMemoryWStream::write(const void* buffer, size_t count) | 542 bool SkDynamicMemoryWStream::write(const void* buffer, size_t count) |
543 { | 543 { |
544 if (count > 0) { | 544 if (count > 0) { |
545 this->invalidateCopy(); | 545 this->invalidateCopy(); |
546 | 546 |
547 fBytesWritten += count; | 547 fBytesWritten += count; |
548 | 548 |
549 size_t size; | 549 size_t size; |
550 | 550 |
551 if (fTail != NULL && fTail->avail() > 0) { | 551 if (fTail != nullptr && fTail->avail() > 0) { |
552 size = SkTMin(fTail->avail(), count); | 552 size = SkTMin(fTail->avail(), count); |
553 buffer = fTail->append(buffer, size); | 553 buffer = fTail->append(buffer, size); |
554 SkASSERT(count >= size); | 554 SkASSERT(count >= size); |
555 count -= size; | 555 count -= size; |
556 if (count == 0) | 556 if (count == 0) |
557 return true; | 557 return true; |
558 } | 558 } |
559 | 559 |
560 size = SkTMax<size_t>(count, SkDynamicMemoryWStream_MinBlockSize); | 560 size = SkTMax<size_t>(count, SkDynamicMemoryWStream_MinBlockSize); |
561 Block* block = (Block*)sk_malloc_throw(sizeof(Block) + size); | 561 Block* block = (Block*)sk_malloc_throw(sizeof(Block) + size); |
562 block->init(size); | 562 block->init(size); |
563 block->append(buffer, count); | 563 block->append(buffer, count); |
564 | 564 |
565 if (fTail != NULL) | 565 if (fTail != nullptr) |
566 fTail->fNext = block; | 566 fTail->fNext = block; |
567 else | 567 else |
568 fHead = fTail = block; | 568 fHead = fTail = block; |
569 fTail = block; | 569 fTail = block; |
570 } | 570 } |
571 return true; | 571 return true; |
572 } | 572 } |
573 | 573 |
574 bool SkDynamicMemoryWStream::write(const void* buffer, size_t offset, size_t cou
nt) | 574 bool SkDynamicMemoryWStream::write(const void* buffer, size_t offset, size_t cou
nt) |
575 { | 575 { |
576 if (offset + count > fBytesWritten) { | 576 if (offset + count > fBytesWritten) { |
577 return false; // test does not partially modify | 577 return false; // test does not partially modify |
578 } | 578 } |
579 | 579 |
580 this->invalidateCopy(); | 580 this->invalidateCopy(); |
581 | 581 |
582 Block* block = fHead; | 582 Block* block = fHead; |
583 while (block != NULL) { | 583 while (block != nullptr) { |
584 size_t size = block->written(); | 584 size_t size = block->written(); |
585 if (offset < size) { | 585 if (offset < size) { |
586 size_t part = offset + count > size ? size - offset : count; | 586 size_t part = offset + count > size ? size - offset : count; |
587 memcpy(block->start() + offset, buffer, part); | 587 memcpy(block->start() + offset, buffer, part); |
588 if (count <= part) | 588 if (count <= part) |
589 return true; | 589 return true; |
590 count -= part; | 590 count -= part; |
591 buffer = (const void*) ((char* ) buffer + part); | 591 buffer = (const void*) ((char* ) buffer + part); |
592 } | 592 } |
593 offset = offset > size ? offset - size : 0; | 593 offset = offset > size ? offset - size : 0; |
594 block = block->fNext; | 594 block = block->fNext; |
595 } | 595 } |
596 return false; | 596 return false; |
597 } | 597 } |
598 | 598 |
599 bool SkDynamicMemoryWStream::read(void* buffer, size_t offset, size_t count) | 599 bool SkDynamicMemoryWStream::read(void* buffer, size_t offset, size_t count) |
600 { | 600 { |
601 if (offset + count > fBytesWritten) | 601 if (offset + count > fBytesWritten) |
602 return false; // test does not partially modify | 602 return false; // test does not partially modify |
603 Block* block = fHead; | 603 Block* block = fHead; |
604 while (block != NULL) { | 604 while (block != nullptr) { |
605 size_t size = block->written(); | 605 size_t size = block->written(); |
606 if (offset < size) { | 606 if (offset < size) { |
607 size_t part = offset + count > size ? size - offset : count; | 607 size_t part = offset + count > size ? size - offset : count; |
608 memcpy(buffer, block->start() + offset, part); | 608 memcpy(buffer, block->start() + offset, part); |
609 if (count <= part) | 609 if (count <= part) |
610 return true; | 610 return true; |
611 count -= part; | 611 count -= part; |
612 buffer = (void*) ((char* ) buffer + part); | 612 buffer = (void*) ((char* ) buffer + part); |
613 } | 613 } |
614 offset = offset > size ? offset - size : 0; | 614 offset = offset > size ? offset - size : 0; |
615 block = block->fNext; | 615 block = block->fNext; |
616 } | 616 } |
617 return false; | 617 return false; |
618 } | 618 } |
619 | 619 |
620 void SkDynamicMemoryWStream::copyTo(void* dst) const | 620 void SkDynamicMemoryWStream::copyTo(void* dst) const |
621 { | 621 { |
622 if (fCopy) { | 622 if (fCopy) { |
623 memcpy(dst, fCopy->data(), fBytesWritten); | 623 memcpy(dst, fCopy->data(), fBytesWritten); |
624 } else { | 624 } else { |
625 Block* block = fHead; | 625 Block* block = fHead; |
626 | 626 |
627 while (block != NULL) { | 627 while (block != nullptr) { |
628 size_t size = block->written(); | 628 size_t size = block->written(); |
629 memcpy(dst, block->start(), size); | 629 memcpy(dst, block->start(), size); |
630 dst = (void*)((char*)dst + size); | 630 dst = (void*)((char*)dst + size); |
631 block = block->fNext; | 631 block = block->fNext; |
632 } | 632 } |
633 } | 633 } |
634 } | 634 } |
635 | 635 |
636 void SkDynamicMemoryWStream::writeToStream(SkWStream* dst) const { | 636 void SkDynamicMemoryWStream::writeToStream(SkWStream* dst) const { |
637 for (Block* block = fHead; block != NULL; block = block->fNext) { | 637 for (Block* block = fHead; block != nullptr; block = block->fNext) { |
638 dst->write(block->start(), block->written()); | 638 dst->write(block->start(), block->written()); |
639 } | 639 } |
640 } | 640 } |
641 | 641 |
642 void SkDynamicMemoryWStream::padToAlign4() | 642 void SkDynamicMemoryWStream::padToAlign4() |
643 { | 643 { |
644 // cast to remove unary-minus warning | 644 // cast to remove unary-minus warning |
645 int padBytes = -(int)fBytesWritten & 0x03; | 645 int padBytes = -(int)fBytesWritten & 0x03; |
646 if (padBytes == 0) | 646 if (padBytes == 0) |
647 return; | 647 return; |
648 int zero = 0; | 648 int zero = 0; |
649 write(&zero, padBytes); | 649 write(&zero, padBytes); |
650 } | 650 } |
651 | 651 |
652 SkData* SkDynamicMemoryWStream::copyToData() const { | 652 SkData* SkDynamicMemoryWStream::copyToData() const { |
653 if (NULL == fCopy) { | 653 if (nullptr == fCopy) { |
654 SkData* data = SkData::NewUninitialized(fBytesWritten); | 654 SkData* data = SkData::NewUninitialized(fBytesWritten); |
655 // be sure to call copyTo() before we assign to fCopy | 655 // be sure to call copyTo() before we assign to fCopy |
656 this->copyTo(data->writable_data()); | 656 this->copyTo(data->writable_data()); |
657 fCopy = data; | 657 fCopy = data; |
658 } | 658 } |
659 return SkRef(fCopy); | 659 return SkRef(fCopy); |
660 } | 660 } |
661 | 661 |
662 void SkDynamicMemoryWStream::invalidateCopy() { | 662 void SkDynamicMemoryWStream::invalidateCopy() { |
663 if (fCopy) { | 663 if (fCopy) { |
664 fCopy->unref(); | 664 fCopy->unref(); |
665 fCopy = NULL; | 665 fCopy = nullptr; |
666 } | 666 } |
667 } | 667 } |
668 | 668 |
669 class SkBlockMemoryRefCnt : public SkRefCnt { | 669 class SkBlockMemoryRefCnt : public SkRefCnt { |
670 public: | 670 public: |
671 explicit SkBlockMemoryRefCnt(SkDynamicMemoryWStream::Block* head) : fHead(he
ad) { } | 671 explicit SkBlockMemoryRefCnt(SkDynamicMemoryWStream::Block* head) : fHead(he
ad) { } |
672 | 672 |
673 virtual ~SkBlockMemoryRefCnt() { | 673 virtual ~SkBlockMemoryRefCnt() { |
674 SkDynamicMemoryWStream::Block* block = fHead; | 674 SkDynamicMemoryWStream::Block* block = fHead; |
675 while (block != NULL) { | 675 while (block != nullptr) { |
676 SkDynamicMemoryWStream::Block* next = block->fNext; | 676 SkDynamicMemoryWStream::Block* next = block->fNext; |
677 sk_free(block); | 677 sk_free(block); |
678 block = next; | 678 block = next; |
679 } | 679 } |
680 } | 680 } |
681 | 681 |
682 SkDynamicMemoryWStream::Block* const fHead; | 682 SkDynamicMemoryWStream::Block* const fHead; |
683 }; | 683 }; |
684 | 684 |
685 class SkBlockMemoryStream : public SkStreamAsset { | 685 class SkBlockMemoryStream : public SkStreamAsset { |
686 public: | 686 public: |
687 SkBlockMemoryStream(SkDynamicMemoryWStream::Block* head, size_t size) | 687 SkBlockMemoryStream(SkDynamicMemoryWStream::Block* head, size_t size) |
688 : fBlockMemory(new SkBlockMemoryRefCnt(head)) | 688 : fBlockMemory(new SkBlockMemoryRefCnt(head)) |
689 , fCurrent(head) | 689 , fCurrent(head) |
690 , fSize(size) | 690 , fSize(size) |
691 , fOffset(0) | 691 , fOffset(0) |
692 , fCurrentOffset(0) {} | 692 , fCurrentOffset(0) {} |
693 | 693 |
694 SkBlockMemoryStream(SkBlockMemoryRefCnt* headRef, size_t size) | 694 SkBlockMemoryStream(SkBlockMemoryRefCnt* headRef, size_t size) |
695 : fBlockMemory(SkRef(headRef)), fCurrent(fBlockMemory->fHead) | 695 : fBlockMemory(SkRef(headRef)), fCurrent(fBlockMemory->fHead) |
696 , fSize(size) , fOffset(0), fCurrentOffset(0) { } | 696 , fSize(size) , fOffset(0), fCurrentOffset(0) { } |
697 | 697 |
698 size_t read(void* buffer, size_t rawCount) override { | 698 size_t read(void* buffer, size_t rawCount) override { |
699 size_t count = rawCount; | 699 size_t count = rawCount; |
700 if (fOffset + count > fSize) { | 700 if (fOffset + count > fSize) { |
701 count = fSize - fOffset; | 701 count = fSize - fOffset; |
702 } | 702 } |
703 size_t bytesLeftToRead = count; | 703 size_t bytesLeftToRead = count; |
704 while (fCurrent != NULL) { | 704 while (fCurrent != nullptr) { |
705 size_t bytesLeftInCurrent = fCurrent->written() - fCurrentOffset; | 705 size_t bytesLeftInCurrent = fCurrent->written() - fCurrentOffset; |
706 size_t bytesFromCurrent = SkTMin(bytesLeftToRead, bytesLeftInCurrent
); | 706 size_t bytesFromCurrent = SkTMin(bytesLeftToRead, bytesLeftInCurrent
); |
707 if (buffer) { | 707 if (buffer) { |
708 memcpy(buffer, fCurrent->start() + fCurrentOffset, bytesFromCurr
ent); | 708 memcpy(buffer, fCurrent->start() + fCurrentOffset, bytesFromCurr
ent); |
709 buffer = SkTAddOffset<void>(buffer, bytesFromCurrent); | 709 buffer = SkTAddOffset<void>(buffer, bytesFromCurrent); |
710 } | 710 } |
711 if (bytesLeftToRead <= bytesFromCurrent) { | 711 if (bytesLeftToRead <= bytesFromCurrent) { |
712 fCurrentOffset += bytesFromCurrent; | 712 fCurrentOffset += bytesFromCurrent; |
713 fOffset += count; | 713 fOffset += count; |
714 return count; | 714 return count; |
715 } | 715 } |
716 bytesLeftToRead -= bytesFromCurrent; | 716 bytesLeftToRead -= bytesFromCurrent; |
717 fCurrent = fCurrent->fNext; | 717 fCurrent = fCurrent->fNext; |
718 fCurrentOffset = 0; | 718 fCurrentOffset = 0; |
719 } | 719 } |
720 SkASSERT(false); | 720 SkASSERT(false); |
721 return 0; | 721 return 0; |
722 } | 722 } |
723 | 723 |
724 bool isAtEnd() const override { | 724 bool isAtEnd() const override { |
725 return fOffset == fSize; | 725 return fOffset == fSize; |
726 } | 726 } |
727 | 727 |
728 bool peek(void* buff, size_t size) const override { | 728 bool peek(void* buff, size_t size) const override { |
729 SkASSERT(buff != NULL); | 729 SkASSERT(buff != nullptr); |
730 if (fOffset + size > fSize) { | 730 if (fOffset + size > fSize) { |
731 return false; | 731 return false; |
732 } | 732 } |
733 char* buffer = static_cast<char*>(buff); | 733 char* buffer = static_cast<char*>(buff); |
734 const SkDynamicMemoryWStream::Block* current = fCurrent; | 734 const SkDynamicMemoryWStream::Block* current = fCurrent; |
735 size_t currentOffset = fCurrentOffset; | 735 size_t currentOffset = fCurrentOffset; |
736 while (size) { | 736 while (size) { |
737 SkASSERT(current); | 737 SkASSERT(current); |
738 size_t bytesFromCurrent = | 738 size_t bytesFromCurrent = |
739 SkTMin(current->written() - currentOffset, size); | 739 SkTMin(current->written() - currentOffset, size); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
788 that->fOffset = this->fOffset; | 788 that->fOffset = this->fOffset; |
789 that->fCurrentOffset = this->fCurrentOffset; | 789 that->fCurrentOffset = this->fCurrentOffset; |
790 return that.detach(); | 790 return that.detach(); |
791 } | 791 } |
792 | 792 |
793 size_t getLength() const override { | 793 size_t getLength() const override { |
794 return fSize; | 794 return fSize; |
795 } | 795 } |
796 | 796 |
797 const void* getMemoryBase() override { | 797 const void* getMemoryBase() override { |
798 if (NULL == fBlockMemory->fHead->fNext) { | 798 if (nullptr == fBlockMemory->fHead->fNext) { |
799 return fBlockMemory->fHead->start(); | 799 return fBlockMemory->fHead->start(); |
800 } | 800 } |
801 return NULL; | 801 return nullptr; |
802 } | 802 } |
803 | 803 |
804 private: | 804 private: |
805 SkAutoTUnref<SkBlockMemoryRefCnt> const fBlockMemory; | 805 SkAutoTUnref<SkBlockMemoryRefCnt> const fBlockMemory; |
806 SkDynamicMemoryWStream::Block const * fCurrent; | 806 SkDynamicMemoryWStream::Block const * fCurrent; |
807 size_t const fSize; | 807 size_t const fSize; |
808 size_t fOffset; | 808 size_t fOffset; |
809 size_t fCurrentOffset; | 809 size_t fCurrentOffset; |
810 }; | 810 }; |
811 | 811 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
843 #endif | 843 #endif |
844 return true; | 844 return true; |
845 } | 845 } |
846 | 846 |
847 /////////////////////////////////////////////////////////////////////////////// | 847 /////////////////////////////////////////////////////////////////////////////// |
848 /////////////////////////////////////////////////////////////////////////////// | 848 /////////////////////////////////////////////////////////////////////////////// |
849 | 849 |
850 | 850 |
851 static SkData* mmap_filename(const char path[]) { | 851 static SkData* mmap_filename(const char path[]) { |
852 SkFILE* file = sk_fopen(path, kRead_SkFILE_Flag); | 852 SkFILE* file = sk_fopen(path, kRead_SkFILE_Flag); |
853 if (NULL == file) { | 853 if (nullptr == file) { |
854 return NULL; | 854 return nullptr; |
855 } | 855 } |
856 | 856 |
857 SkData* data = SkData::NewFromFILE(file); | 857 SkData* data = SkData::NewFromFILE(file); |
858 sk_fclose(file); | 858 sk_fclose(file); |
859 return data; | 859 return data; |
860 } | 860 } |
861 | 861 |
862 SkStreamAsset* SkStream::NewFromFile(const char path[]) { | 862 SkStreamAsset* SkStream::NewFromFile(const char path[]) { |
863 SkAutoTUnref<SkData> data(mmap_filename(path)); | 863 SkAutoTUnref<SkData> data(mmap_filename(path)); |
864 if (data.get()) { | 864 if (data.get()) { |
865 return new SkMemoryStream(data.get()); | 865 return new SkMemoryStream(data.get()); |
866 } | 866 } |
867 | 867 |
868 // If we get here, then our attempt at using mmap failed, so try normal | 868 // If we get here, then our attempt at using mmap failed, so try normal |
869 // file access. | 869 // file access. |
870 SkFILEStream* stream = new SkFILEStream(path); | 870 SkFILEStream* stream = new SkFILEStream(path); |
871 if (!stream->isValid()) { | 871 if (!stream->isValid()) { |
872 delete stream; | 872 delete stream; |
873 stream = NULL; | 873 stream = nullptr; |
874 } | 874 } |
875 return stream; | 875 return stream; |
876 } | 876 } |
877 | 877 |
878 // Declared in SkStreamPriv.h: | 878 // Declared in SkStreamPriv.h: |
879 size_t SkCopyStreamToStorage(SkAutoMalloc* storage, SkStream* stream) { | 879 size_t SkCopyStreamToStorage(SkAutoMalloc* storage, SkStream* stream) { |
880 SkASSERT(storage != NULL); | 880 SkASSERT(storage != nullptr); |
881 SkASSERT(stream != NULL); | 881 SkASSERT(stream != nullptr); |
882 | 882 |
883 if (stream->hasLength()) { | 883 if (stream->hasLength()) { |
884 const size_t length = stream->getLength(); | 884 const size_t length = stream->getLength(); |
885 void* dst = storage->reset(length); | 885 void* dst = storage->reset(length); |
886 if (stream->read(dst, length) != length) { | 886 if (stream->read(dst, length) != length) { |
887 return 0; | 887 return 0; |
888 } | 888 } |
889 return length; | 889 return length; |
890 } | 890 } |
891 | 891 |
892 SkDynamicMemoryWStream tempStream; | 892 SkDynamicMemoryWStream tempStream; |
893 // Arbitrary buffer size. | 893 // Arbitrary buffer size. |
894 const size_t bufferSize = 256 * 1024; // 256KB | 894 const size_t bufferSize = 256 * 1024; // 256KB |
895 char buffer[bufferSize]; | 895 char buffer[bufferSize]; |
896 SkDEBUGCODE(size_t debugLength = 0;) | 896 SkDEBUGCODE(size_t debugLength = 0;) |
897 do { | 897 do { |
898 size_t bytesRead = stream->read(buffer, bufferSize); | 898 size_t bytesRead = stream->read(buffer, bufferSize); |
899 tempStream.write(buffer, bytesRead); | 899 tempStream.write(buffer, bytesRead); |
900 SkDEBUGCODE(debugLength += bytesRead); | 900 SkDEBUGCODE(debugLength += bytesRead); |
901 SkASSERT(tempStream.bytesWritten() == debugLength); | 901 SkASSERT(tempStream.bytesWritten() == debugLength); |
902 } while (!stream->isAtEnd()); | 902 } while (!stream->isAtEnd()); |
903 const size_t length = tempStream.bytesWritten(); | 903 const size_t length = tempStream.bytesWritten(); |
904 void* dst = storage->reset(length); | 904 void* dst = storage->reset(length); |
905 tempStream.copyTo(dst); | 905 tempStream.copyTo(dst); |
906 return length; | 906 return length; |
907 } | 907 } |
908 | 908 |
909 // Declared in SkStreamPriv.h: | 909 // Declared in SkStreamPriv.h: |
910 SkData* SkCopyStreamToData(SkStream* stream) { | 910 SkData* SkCopyStreamToData(SkStream* stream) { |
911 SkASSERT(stream != NULL); | 911 SkASSERT(stream != nullptr); |
912 | 912 |
913 if (stream->hasLength()) { | 913 if (stream->hasLength()) { |
914 return SkData::NewFromStream(stream, stream->getLength()); | 914 return SkData::NewFromStream(stream, stream->getLength()); |
915 } | 915 } |
916 | 916 |
917 SkDynamicMemoryWStream tempStream; | 917 SkDynamicMemoryWStream tempStream; |
918 const size_t bufferSize = 4096; | 918 const size_t bufferSize = 4096; |
919 char buffer[bufferSize]; | 919 char buffer[bufferSize]; |
920 do { | 920 do { |
921 size_t bytesRead = stream->read(buffer, bufferSize); | 921 size_t bytesRead = stream->read(buffer, bufferSize); |
922 tempStream.write(buffer, bytesRead); | 922 tempStream.write(buffer, bytesRead); |
923 } while (!stream->isAtEnd()); | 923 } while (!stream->isAtEnd()); |
924 return tempStream.copyToData(); | 924 return tempStream.copyToData(); |
925 } | 925 } |
926 | 926 |
927 SkStreamRewindable* SkStreamRewindableFromSkStream(SkStream* stream) { | 927 SkStreamRewindable* SkStreamRewindableFromSkStream(SkStream* stream) { |
928 if (!stream) { | 928 if (!stream) { |
929 return NULL; | 929 return nullptr; |
930 } | 930 } |
931 SkAutoTDelete<SkStreamRewindable> dupStream(stream->duplicate()); | 931 SkAutoTDelete<SkStreamRewindable> dupStream(stream->duplicate()); |
932 if (dupStream) { | 932 if (dupStream) { |
933 return dupStream.detach(); | 933 return dupStream.detach(); |
934 } | 934 } |
935 stream->rewind(); | 935 stream->rewind(); |
936 if (stream->hasLength()) { | 936 if (stream->hasLength()) { |
937 size_t length = stream->getLength(); | 937 size_t length = stream->getLength(); |
938 if (stream->hasPosition()) { // If stream has length, but can't rewind. | 938 if (stream->hasPosition()) { // If stream has length, but can't rewind. |
939 length -= stream->getPosition(); | 939 length -= stream->getPosition(); |
(...skipping 26 matching lines...) Expand all Loading... |
966 while (true) { | 966 while (true) { |
967 count = input->read(scratch, sizeof(scratch)); | 967 count = input->read(scratch, sizeof(scratch)); |
968 if (0 == count) { | 968 if (0 == count) { |
969 return true; | 969 return true; |
970 } | 970 } |
971 if (!out->write(scratch, count)) { | 971 if (!out->write(scratch, count)) { |
972 return false; | 972 return false; |
973 } | 973 } |
974 } | 974 } |
975 } | 975 } |
OLD | NEW |