1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/src/pipe/utils/SamplePipeControllers.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,118 @@ 1.4 +/* 1.5 + * Copyright 2012 Google Inc. 1.6 + * 1.7 + * Use of this source code is governed by a BSD-style license that can be 1.8 + * found in the LICENSE file. 1.9 + */ 1.10 + 1.11 +#include "SamplePipeControllers.h" 1.12 + 1.13 +#include "SkBitmapDevice.h" 1.14 +#include "SkCanvas.h" 1.15 +#include "SkGPipe.h" 1.16 +#include "SkMatrix.h" 1.17 + 1.18 +PipeController::PipeController(SkCanvas* target, SkPicture::InstallPixelRefProc proc) 1.19 +:fReader(target) { 1.20 + fBlock = NULL; 1.21 + fBlockSize = fBytesWritten = 0; 1.22 + fReader.setBitmapDecoder(proc); 1.23 +} 1.24 + 1.25 +PipeController::~PipeController() { 1.26 + sk_free(fBlock); 1.27 +} 1.28 + 1.29 +void* PipeController::requestBlock(size_t minRequest, size_t *actual) { 1.30 + sk_free(fBlock); 1.31 + fBlockSize = minRequest * 4; 1.32 + fBlock = sk_malloc_throw(fBlockSize); 1.33 + fBytesWritten = 0; 1.34 + *actual = fBlockSize; 1.35 + return fBlock; 1.36 +} 1.37 + 1.38 +void PipeController::notifyWritten(size_t bytes) { 1.39 + fStatus = fReader.playback(this->getData(), bytes); 1.40 + SkASSERT(SkGPipeReader::kError_Status != fStatus); 1.41 + fBytesWritten += bytes; 1.42 +} 1.43 + 1.44 +//////////////////////////////////////////////////////////////////////////////// 1.45 + 1.46 +TiledPipeController::TiledPipeController(const SkBitmap& bitmap, 1.47 + SkPicture::InstallPixelRefProc proc, 1.48 + const SkMatrix* initial) 1.49 +: INHERITED(NULL, proc) { 1.50 + int32_t top = 0; 1.51 + int32_t bottom; 1.52 + int32_t height = bitmap.height() / NumberOfTiles; 1.53 + SkIRect rect; 1.54 + for (int i = 0; i < NumberOfTiles; i++) { 1.55 + bottom = i + 1 == NumberOfTiles ? bitmap.height() : top + height; 1.56 + rect.setLTRB(0, top, bitmap.width(), bottom); 1.57 + top = bottom; 1.58 + 1.59 + SkDEBUGCODE(bool extracted = )bitmap.extractSubset(&fBitmaps[i], rect); 1.60 + SkASSERT(extracted); 1.61 + SkBaseDevice* device = new SkBitmapDevice(fBitmaps[i]); 1.62 + SkCanvas* canvas = new SkCanvas(device); 1.63 + device->unref(); 1.64 + if (initial != NULL) { 1.65 + canvas->setMatrix(*initial); 1.66 + } 1.67 + canvas->translate(SkIntToScalar(-rect.left()), 1.68 + SkIntToScalar(-rect.top())); 1.69 + if (0 == i) { 1.70 + fReader.setCanvas(canvas); 1.71 + } else { 1.72 + fReaders[i - 1].setCanvas(canvas); 1.73 + fReaders[i - 1].setBitmapDecoder(proc); 1.74 + } 1.75 + canvas->unref(); 1.76 + } 1.77 +} 1.78 + 1.79 +void TiledPipeController::notifyWritten(size_t bytes) { 1.80 + for (int i = 0; i < NumberOfTiles - 1; i++) { 1.81 + fReaders[i].playback(this->getData(), bytes); 1.82 + } 1.83 + this->INHERITED::notifyWritten(bytes); 1.84 +} 1.85 + 1.86 +//////////////////////////////////////////////////////////////////////////////// 1.87 + 1.88 +ThreadSafePipeController::ThreadSafePipeController(int numberOfReaders) 1.89 +: fAllocator(kMinBlockSize) 1.90 +, fNumberOfReaders(numberOfReaders) { 1.91 + fBlock = NULL; 1.92 + fBytesWritten = 0; 1.93 +} 1.94 + 1.95 +void* ThreadSafePipeController::requestBlock(size_t minRequest, size_t *actual) { 1.96 + if (fBlock) { 1.97 + // Save the previous block for later 1.98 + PipeBlock previousBloc(fBlock, fBytesWritten); 1.99 + fBlockList.push(previousBloc); 1.100 + } 1.101 + int32_t blockSize = SkMax32(SkToS32(minRequest), kMinBlockSize); 1.102 + fBlock = fAllocator.allocThrow(blockSize); 1.103 + fBytesWritten = 0; 1.104 + *actual = blockSize; 1.105 + return fBlock; 1.106 +} 1.107 + 1.108 +void ThreadSafePipeController::notifyWritten(size_t bytes) { 1.109 + fBytesWritten += bytes; 1.110 +} 1.111 + 1.112 +void ThreadSafePipeController::draw(SkCanvas* target) { 1.113 + SkGPipeReader reader(target); 1.114 + for (int currentBlock = 0; currentBlock < fBlockList.count(); currentBlock++ ) { 1.115 + reader.playback(fBlockList[currentBlock].fBlock, fBlockList[currentBlock].fBytes); 1.116 + } 1.117 + 1.118 + if (fBlock) { 1.119 + reader.playback(fBlock, fBytesWritten); 1.120 + } 1.121 +}