Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(144)

Side by Side Diff: src/core/SkStream.cpp

Issue 15298009: Change SkStream. (Closed) Base URL: http://skia.googlecode.com/svn/trunk/
Patch Set: Adjustments Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/core/SkFDStream.cpp ('k') | src/ports/SkFontHost_fontconfig.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
11 #include "SkData.h" 11 #include "SkData.h"
12 #include "SkFixed.h" 12 #include "SkFixed.h"
13 #include "SkString.h" 13 #include "SkString.h"
14 #include "SkOSFile.h" 14 #include "SkOSFile.h"
15 15
16 SK_DEFINE_INST_COUNT(SkStream) 16 SK_DEFINE_INST_COUNT(SkStream)
17 SK_DEFINE_INST_COUNT(SkWStream) 17 SK_DEFINE_INST_COUNT(SkWStream)
18 SK_DEFINE_INST_COUNT(SkFILEStream) 18 SK_DEFINE_INST_COUNT(SkFILEStream)
19 SK_DEFINE_INST_COUNT(SkFDStream)
20 SK_DEFINE_INST_COUNT(SkMemoryStream) 19 SK_DEFINE_INST_COUNT(SkMemoryStream)
21 SK_DEFINE_INST_COUNT(SkBufferStream)
22 SK_DEFINE_INST_COUNT(SkFILEWStream) 20 SK_DEFINE_INST_COUNT(SkFILEWStream)
23 SK_DEFINE_INST_COUNT(SkMemoryWStream) 21 SK_DEFINE_INST_COUNT(SkMemoryWStream)
24 SK_DEFINE_INST_COUNT(SkDynamicMemoryWStream) 22 SK_DEFINE_INST_COUNT(SkDynamicMemoryWStream)
25 SK_DEFINE_INST_COUNT(SkDebugWStream) 23 SK_DEFINE_INST_COUNT(SkDebugWStream)
26 24
27 /////////////////////////////////////////////////////////////////////////////// 25 ///////////////////////////////////////////////////////////////////////////////
28 26
29 const char* SkStream::getFileName()
30 {
31 // override in subclass if you represent a file
32 return NULL;
33 }
34
35 const void* SkStream::getMemoryBase()
36 {
37 // override in subclass if you represent a memory block
38 return NULL;
39 }
40
41 size_t SkStream::skip(size_t size)
42 {
43 /* Check for size == 0, and just return 0. If we passed that
44 to read(), it would interpret it as a request for the entire
45 size of the stream.
46 */
47 return size ? this->read(NULL, size) : 0;
48 }
49 27
50 int8_t SkStream::readS8() { 28 int8_t SkStream::readS8() {
51 int8_t value; 29 int8_t value;
52 SkDEBUGCODE(size_t len =) this->read(&value, 1); 30 SkDEBUGCODE(size_t len =) this->read(&value, 1);
53 SkASSERT(1 == len); 31 SkASSERT(1 == len);
54 return value; 32 return value;
55 } 33 }
56 34
57 int16_t SkStream::readS16() { 35 int16_t SkStream::readS16() {
58 int16_t value; 36 int16_t value;
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
214 this->write32(data->size()); 192 this->write32(data->size());
215 this->write(data->data(), data->size()); 193 this->write(data->data(), data->size());
216 } else { 194 } else {
217 this->write32(0); 195 this->write32(0);
218 } 196 }
219 return true; 197 return true;
220 } 198 }
221 199
222 /////////////////////////////////////////////////////////////////////////////// 200 ///////////////////////////////////////////////////////////////////////////////
223 201
224 SkFILEStream::SkFILEStream(const char file[]) : fName(file) { 202 SkFILEStream::SkFILEStream(const char file[]) : fName(file), fOwnership(kCallerP asses_Ownership) {
225 fFILE = file ? sk_fopen(fName.c_str(), kRead_SkFILE_Flag) : NULL; 203 fFILE = file ? sk_fopen(fName.c_str(), kRead_SkFILE_Flag) : NULL;
226 } 204 }
227 205
206 SkFILEStream::SkFILEStream(FILE* file, Ownership ownership)
207 : fFILE((SkFILE*)file)
208 , fOwnership(ownership) {
209 }
210
228 SkFILEStream::~SkFILEStream() { 211 SkFILEStream::~SkFILEStream() {
229 if (fFILE) { 212 if (fFILE && fOwnership != kCallerRetains_Ownership) {
230 sk_fclose(fFILE); 213 sk_fclose(fFILE);
231 } 214 }
232 } 215 }
233 216
234 void SkFILEStream::setPath(const char path[]) { 217 void SkFILEStream::setPath(const char path[]) {
235 fName.set(path); 218 fName.set(path);
236 if (fFILE) { 219 if (fFILE) {
237 sk_fclose(fFILE); 220 sk_fclose(fFILE);
238 fFILE = NULL; 221 fFILE = NULL;
239 } 222 }
240 if (path) { 223 if (path) {
241 fFILE = sk_fopen(fName.c_str(), kRead_SkFILE_Flag); 224 fFILE = sk_fopen(fName.c_str(), kRead_SkFILE_Flag);
242 } 225 }
243 } 226 }
244 227
245 const char* SkFILEStream::getFileName() { 228 size_t SkFILEStream::read(void* buffer, size_t size) {
246 return fName.c_str(); 229 if (fFILE) {
230 return sk_fread(buffer, size, fFILE);
231 }
232 return 0;
247 } 233 }
248 234
249 bool SkFILEStream::rewind() { 235 bool SkFILEStream::rewind() {
250 if (fFILE) { 236 if (fFILE) {
251 if (sk_frewind(fFILE)) { 237 if (sk_frewind(fFILE)) {
252 return true; 238 return true;
253 } 239 }
254 // we hit an error 240 // we hit an error
255 sk_fclose(fFILE); 241 sk_fclose(fFILE);
256 fFILE = NULL; 242 fFILE = NULL;
257 } 243 }
258 return false; 244 return false;
259 } 245 }
260 246
261 size_t SkFILEStream::read(void* buffer, size_t size) { 247 SkStreamAsset* SkFILEStream::duplicate() const {
262 if (fFILE) { 248 if (NULL == fFILE) {
263 if (buffer == NULL && size == 0) { // special signature, they want the total size 249 return new SkMemoryStream();
264 return sk_fgetsize(fFILE); 250 }
265 } else { 251
266 return sk_fread(buffer, size, fFILE); 252 if (NULL != fData.get()) {
253 return new SkMemoryStream(fData);
254 }
255
256 if (!fName.isEmpty()) {
257 SkAutoTUnref<SkFILEStream> that(new SkFILEStream(fName.c_str()));
258 if (sk_fidentical(that->fFILE, this->fFILE)) {
259 return that.detach();
267 } 260 }
268 } 261 }
269 return 0; 262
263 fData.reset(SkData::NewFromFILE(fFILE));
264 if (NULL == fData.get()) {
265 return NULL;
266 }
267 return new SkMemoryStream(fData);
268 }
269
270 bool SkFILEStream::seek(size_t position) {
271 return sk_fseek(fFILE, position);
272 }
273
274 bool SkFILEStream::move(long offset) {
275 return sk_fmove(fFILE, offset);
276 }
277
278 SkStreamAsset* SkFILEStream::fork() const {
279 SkAutoTUnref<SkStreamAsset> that(this->duplicate());
280 that->seek(sk_ftell(fFILE));
281 return that.detach();
282 }
283
284 size_t SkFILEStream::getLength() const {
285 return sk_fgetsize(fFILE);
286 }
287
288 const void* SkFILEStream::getMemoryBase() {
289 if (NULL == fData.get()) {
290 return NULL;
291 }
292 return fData->data();
270 } 293 }
271 294
272 /////////////////////////////////////////////////////////////////////////////// 295 ///////////////////////////////////////////////////////////////////////////////
273 296
274 static SkData* newFromParams(const void* src, size_t size, bool copyData) { 297 static SkData* newFromParams(const void* src, size_t size, bool copyData) {
275 if (copyData) { 298 if (copyData) {
276 return SkData::NewWithCopy(src, size); 299 return SkData::NewWithCopy(src, size);
277 } else { 300 } else {
278 return SkData::NewWithProc(src, size, NULL, NULL); 301 return SkData::NewWithProc(src, size, NULL, NULL);
279 } 302 }
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
334 fData->ref(); 357 fData->ref();
335 } 358 }
336 return data; 359 return data;
337 } 360 }
338 361
339 void SkMemoryStream::skipToAlign4() { 362 void SkMemoryStream::skipToAlign4() {
340 // cast to remove unary-minus warning 363 // cast to remove unary-minus warning
341 fOffset += -(int)fOffset & 0x03; 364 fOffset += -(int)fOffset & 0x03;
342 } 365 }
343 366
344 bool SkMemoryStream::rewind() {
345 fOffset = 0;
346 return true;
347 }
348
349 size_t SkMemoryStream::read(void* buffer, size_t size) { 367 size_t SkMemoryStream::read(void* buffer, size_t size) {
350 size_t dataSize = fData->size(); 368 size_t dataSize = fData->size();
351 369
352 if (buffer == NULL && size == 0) // special signature, they want the tota l size 370 if (buffer == NULL && size == 0) // special signature, they want the tota l size
353 return dataSize; 371 return dataSize;
354 372
355 // if buffer is NULL, seek ahead by size 373 // if buffer is NULL, seek ahead by size
356 374
357 if (size == 0) { 375 if (size == 0) {
358 return 0; 376 return 0;
359 } 377 }
360 if (size > dataSize - fOffset) { 378 if (size > dataSize - fOffset) {
361 size = dataSize - fOffset; 379 size = dataSize - fOffset;
362 } 380 }
363 if (buffer) { 381 if (buffer) {
364 memcpy(buffer, fData->bytes() + fOffset, size); 382 memcpy(buffer, fData->bytes() + fOffset, size);
365 } 383 }
366 fOffset += size; 384 fOffset += size;
367 return size; 385 return size;
368 } 386 }
369 387
388 bool SkMemoryStream::rewind() {
389 fOffset = 0;
390 return true;
391 }
392
393 SkMemoryStream* SkMemoryStream::duplicate() const {
394 return SkNEW_ARGS(SkMemoryStream, (fData));
395 }
396
397 bool SkMemoryStream::seek(size_t position) {
398 fOffset = position > fData->size()
399 ? fData->size()
400 : position;
401 return true;
402 }
403
404 bool SkMemoryStream::move(long offset) {
405 return this->seek(fOffset + offset);
406 }
407
408 SkMemoryStream* SkMemoryStream::fork() const {
409 SkAutoTUnref<SkMemoryStream> that(this->duplicate());
410 that->fOffset = this->fOffset;
411 return that.detach();
412 }
413
414 size_t SkMemoryStream::getLength() const {
415 return fData->size();
416 }
417
370 const void* SkMemoryStream::getMemoryBase() { 418 const void* SkMemoryStream::getMemoryBase() {
371 return fData->data(); 419 return fData->data();
372 } 420 }
373 421
374 const void* SkMemoryStream::getAtPos() { 422 const void* SkMemoryStream::getAtPos() {
375 return fData->bytes() + fOffset; 423 return fData->bytes() + fOffset;
376 } 424 }
377 425
378 size_t SkMemoryStream::seek(size_t offset) {
379 if (offset > fData->size()) {
380 offset = fData->size();
381 }
382 fOffset = offset;
383 return offset;
384 }
385
386 ///////////////////////////////////////////////////////////////////////////////
387
388 SkBufferStream::SkBufferStream(SkStream* proxy, size_t bufferSize)
389 : fProxy(proxy)
390 {
391 SkASSERT(proxy != NULL);
392 proxy->ref();
393 this->init(NULL, bufferSize);
394 }
395
396 SkBufferStream::SkBufferStream(SkStream* proxy, void* buffer, size_t bufferSize)
397 : fProxy(proxy)
398 {
399 SkASSERT(proxy != NULL);
400 SkASSERT(buffer == NULL || bufferSize != 0); // init(addr, 0) makes no se nse, we must know how big their buffer is
401 proxy->ref();
402 this->init(buffer, bufferSize);
403 }
404
405 void SkBufferStream::init(void* buffer, size_t bufferSize)
406 {
407 if (bufferSize == 0)
408 bufferSize = kDefaultBufferSize;
409
410 fOrigBufferSize = bufferSize;
411 fBufferSize = bufferSize;
412 fBufferOffset = bufferSize; // to trigger a reload on the first read()
413
414 if (buffer == NULL)
415 {
416 fBuffer = (char*)sk_malloc_throw(fBufferSize);
417 fWeOwnTheBuffer = true;
418 }
419 else
420 {
421 fBuffer = (char*)buffer;
422 fWeOwnTheBuffer = false;
423 }
424 }
425
426 SkBufferStream::~SkBufferStream()
427 {
428 fProxy->unref();
429 if (fWeOwnTheBuffer)
430 sk_free(fBuffer);
431 }
432
433 bool SkBufferStream::rewind()
434 {
435 fBufferOffset = fBufferSize = fOrigBufferSize;
436 return fProxy->rewind();
437 }
438
439 const char* SkBufferStream::getFileName()
440 {
441 return fProxy->getFileName();
442 }
443
444 #ifdef SK_DEBUG
445 // #define SK_TRACE_BUFFERSTREAM
446 #endif
447
448 size_t SkBufferStream::read(void* buffer, size_t size) {
449 #ifdef SK_TRACE_BUFFERSTREAM
450 SkDebugf("Request %d", size);
451 #endif
452
453 if (buffer == NULL && size == 0) {
454 return fProxy->read(buffer, size); // requesting total size
455 }
456
457 if (0 == size) {
458 return 0;
459 }
460
461 // skip size bytes
462 if (NULL == buffer) {
463 size_t remaining = fBufferSize - fBufferOffset;
464 if (remaining >= size) {
465 fBufferOffset += size;
466 return size;
467 }
468 // if we get here, we are being asked to skip beyond our current buffer
469 // so reset our offset to force a read next time, and skip the diff
470 // in our proxy
471 fBufferOffset = fOrigBufferSize;
472 return remaining + fProxy->read(NULL, size - remaining);
473 }
474
475 size_t s = size;
476 size_t actuallyRead = 0;
477
478 // flush what we can from our fBuffer
479 if (fBufferOffset < fBufferSize)
480 {
481 if (s > fBufferSize - fBufferOffset)
482 s = fBufferSize - fBufferOffset;
483 memcpy(buffer, fBuffer + fBufferOffset, s);
484 #ifdef SK_TRACE_BUFFERSTREAM
485 SkDebugf(" flush %d", s);
486 #endif
487 size -= s;
488 fBufferOffset += s;
489 buffer = (char*)buffer + s;
490 actuallyRead = s;
491 }
492
493 // check if there is more to read
494 if (size)
495 {
496 SkASSERT(fBufferOffset >= fBufferSize); // need to refill our fBuffer
497
498 if (size < fBufferSize) // lets try to read more than the request
499 {
500 s = fProxy->read(fBuffer, fBufferSize);
501 #ifdef SK_TRACE_BUFFERSTREAM
502 SkDebugf(" read %d into fBuffer", s);
503 #endif
504 if (size > s) // they asked for too much
505 size = s;
506 if (size)
507 {
508 memcpy(buffer, fBuffer, size);
509 actuallyRead += size;
510 #ifdef SK_TRACE_BUFFERSTREAM
511 SkDebugf(" memcpy %d into dst", size);
512 #endif
513 }
514
515 fBufferOffset = size;
516 fBufferSize = s; // record the (possibly smaller) size for th e buffer
517 }
518 else // just do a direct read
519 {
520 actuallyRead += fProxy->read(buffer, size);
521 #ifdef SK_TRACE_BUFFERSTREAM
522 SkDebugf(" direct read %d", size);
523 #endif
524 }
525 }
526 #ifdef SK_TRACE_BUFFERSTREAM
527 SkDebugf("\n");
528 #endif
529 return actuallyRead;
530 }
531
532 const void* SkBufferStream::getMemoryBase()
533 {
534 return fProxy->getMemoryBase();
535 }
536
537 //////////////////////////////////////////////////////////////////////////////// ///////////////////////// 426 //////////////////////////////////////////////////////////////////////////////// /////////////////////////
538 //////////////////////////////////////////////////////////////////////////////// ///////////////////////// 427 //////////////////////////////////////////////////////////////////////////////// /////////////////////////
539 428
540 SkFILEWStream::SkFILEWStream(const char path[]) 429 SkFILEWStream::SkFILEWStream(const char path[])
541 { 430 {
542 fFILE = sk_fopen(path, kWrite_SkFILE_Flag); 431 fFILE = sk_fopen(path, kWrite_SkFILE_Flag);
543 } 432 }
544 433
545 SkFILEWStream::~SkFILEWStream() 434 SkFILEWStream::~SkFILEWStream()
546 { 435 {
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after
793 SkFILE* file = sk_fopen(path, kRead_SkFILE_Flag); 682 SkFILE* file = sk_fopen(path, kRead_SkFILE_Flag);
794 if (NULL == file) { 683 if (NULL == file) {
795 return NULL; 684 return NULL;
796 } 685 }
797 686
798 SkData* data = SkData::NewFromFILE(file); 687 SkData* data = SkData::NewFromFILE(file);
799 sk_fclose(file); 688 sk_fclose(file);
800 return data; 689 return data;
801 } 690 }
802 691
803 SkStream* SkStream::NewFromFile(const char path[]) { 692 SkStreamAsset* SkStream::NewFromFile(const char path[]) {
804 SkAutoTUnref<SkData> data(mmap_filename(path)); 693 SkAutoTUnref<SkData> data(mmap_filename(path));
805 if (data.get()) { 694 if (data.get()) {
806 return SkNEW_ARGS(SkMemoryStream, (data.get())); 695 return SkNEW_ARGS(SkMemoryStream, (data.get()));
807 } 696 }
808 697
809 // If we get here, then our attempt at using mmap failed, so try normal 698 // If we get here, then our attempt at using mmap failed, so try normal
810 // file access. 699 // file access.
811 SkFILEStream* stream = SkNEW_ARGS(SkFILEStream, (path)); 700 SkFILEStream* stream = SkNEW_ARGS(SkFILEStream, (path));
812 if (!stream->isValid()) { 701 if (!stream->isValid()) {
813 stream->unref(); 702 stream->unref();
814 stream = NULL; 703 stream = NULL;
815 } 704 }
816 return stream; 705 return stream;
817 } 706 }
OLDNEW
« no previous file with comments | « src/core/SkFDStream.cpp ('k') | src/ports/SkFontHost_fontconfig.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698