1/*
2 * Copyright 2011 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#include "SkNetPipeController.h"
8
9SkNetPipeController::SkNetPipeController(SkCanvas* target) : fReader(target) {
10    fBlock = NULL;
11    fBlockSize = fBytesWritten = 0;
12    fPlayback = true;
13    fStatus = SkGPipeReader::kDone_Status;
14    fTotalWritten = 0;
15    fAtomsWritten = 0;
16}
17SkNetPipeController::~SkNetPipeController() {
18    sk_free(fBlock);
19}
20
21int SkNetPipeController::writeToSocket(SkSocket* sockfd, SkSocket::DataType type) {
22    if (NULL != sockfd && fTotalWritten > 4)
23        return sockfd->writePacket(fBlock, fBytesWritten, type);
24    else
25        return -1;
26}
27
28void* SkNetPipeController::requestBlock(size_t minRequest, size_t* actual) {
29    sk_free(fBlock);
30
31    fBlockSize = minRequest * 4;
32    fBlock = sk_malloc_throw(fBlockSize);
33    fBytesWritten = 0;
34    *actual = fBlockSize;
35    return fBlock;
36}
37
38void SkNetPipeController::notifyWritten(size_t bytes) {
39    SkASSERT(fBytesWritten + bytes <= fBlockSize);
40
41    if (fPlayback) {
42        fStatus = fReader.playback((const char*)fBlock + fBytesWritten, bytes);
43    }
44
45    SkASSERT(SkGPipeReader::kError_Status != fStatus);
46    fBytesWritten += bytes;
47    fTotalWritten += bytes;
48
49    fAtomsWritten += 1;
50}
51