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