epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 8 | #include "SkNetPipeController.h" |
| 9 | |
| 10 | SkNetPipeController::SkNetPipeController(SkCanvas* target) : fReader(target) { |
| 11 | fBlock = NULL; |
| 12 | fBlockSize = fBytesWritten = 0; |
| 13 | fPlayback = true; |
| 14 | fStatus = SkGPipeReader::kDone_Status; |
| 15 | fTotalWritten = 0; |
| 16 | fAtomsWritten = 0; |
| 17 | } |
| 18 | SkNetPipeController::~SkNetPipeController() { |
| 19 | sk_free(fBlock); |
| 20 | } |
| 21 | |
| 22 | int SkNetPipeController::writeToSocket(SkSocket* sockfd, SkSocket::DataType type) { |
| 23 | if (NULL != sockfd && fTotalWritten > 4) |
| 24 | return sockfd->writePacket(fBlock, fBytesWritten, type); |
| 25 | else |
| 26 | return -1; |
| 27 | } |
| 28 | |
| 29 | void* SkNetPipeController::requestBlock(size_t minRequest, size_t* actual) { |
| 30 | sk_free(fBlock); |
| 31 | |
| 32 | fBlockSize = minRequest * 4; |
| 33 | fBlock = sk_malloc_throw(fBlockSize); |
| 34 | fBytesWritten = 0; |
| 35 | *actual = fBlockSize; |
| 36 | return fBlock; |
| 37 | } |
| 38 | |
| 39 | void SkNetPipeController::notifyWritten(size_t bytes) { |
| 40 | SkASSERT(fBytesWritten + bytes <= fBlockSize); |
| 41 | |
| 42 | if (fPlayback) { |
| 43 | fStatus = fReader.playback((const char*)fBlock + fBytesWritten, bytes); |
| 44 | } |
| 45 | |
| 46 | SkASSERT(SkGPipeReader::kError_Status != fStatus); |
| 47 | fBytesWritten += bytes; |
| 48 | fTotalWritten += bytes; |
| 49 | |
| 50 | fAtomsWritten += 1; |
| 51 | } |