Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright 2012 Google Inc. |
michael@0 | 3 | * |
michael@0 | 4 | * Use of this source code is governed by a BSD-style license that can be |
michael@0 | 5 | * found in the LICENSE file. |
michael@0 | 6 | */ |
michael@0 | 7 | |
michael@0 | 8 | #include "SamplePipeControllers.h" |
michael@0 | 9 | |
michael@0 | 10 | #include "SkBitmapDevice.h" |
michael@0 | 11 | #include "SkCanvas.h" |
michael@0 | 12 | #include "SkGPipe.h" |
michael@0 | 13 | #include "SkMatrix.h" |
michael@0 | 14 | |
michael@0 | 15 | PipeController::PipeController(SkCanvas* target, SkPicture::InstallPixelRefProc proc) |
michael@0 | 16 | :fReader(target) { |
michael@0 | 17 | fBlock = NULL; |
michael@0 | 18 | fBlockSize = fBytesWritten = 0; |
michael@0 | 19 | fReader.setBitmapDecoder(proc); |
michael@0 | 20 | } |
michael@0 | 21 | |
michael@0 | 22 | PipeController::~PipeController() { |
michael@0 | 23 | sk_free(fBlock); |
michael@0 | 24 | } |
michael@0 | 25 | |
michael@0 | 26 | void* PipeController::requestBlock(size_t minRequest, size_t *actual) { |
michael@0 | 27 | sk_free(fBlock); |
michael@0 | 28 | fBlockSize = minRequest * 4; |
michael@0 | 29 | fBlock = sk_malloc_throw(fBlockSize); |
michael@0 | 30 | fBytesWritten = 0; |
michael@0 | 31 | *actual = fBlockSize; |
michael@0 | 32 | return fBlock; |
michael@0 | 33 | } |
michael@0 | 34 | |
michael@0 | 35 | void PipeController::notifyWritten(size_t bytes) { |
michael@0 | 36 | fStatus = fReader.playback(this->getData(), bytes); |
michael@0 | 37 | SkASSERT(SkGPipeReader::kError_Status != fStatus); |
michael@0 | 38 | fBytesWritten += bytes; |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 42 | |
michael@0 | 43 | TiledPipeController::TiledPipeController(const SkBitmap& bitmap, |
michael@0 | 44 | SkPicture::InstallPixelRefProc proc, |
michael@0 | 45 | const SkMatrix* initial) |
michael@0 | 46 | : INHERITED(NULL, proc) { |
michael@0 | 47 | int32_t top = 0; |
michael@0 | 48 | int32_t bottom; |
michael@0 | 49 | int32_t height = bitmap.height() / NumberOfTiles; |
michael@0 | 50 | SkIRect rect; |
michael@0 | 51 | for (int i = 0; i < NumberOfTiles; i++) { |
michael@0 | 52 | bottom = i + 1 == NumberOfTiles ? bitmap.height() : top + height; |
michael@0 | 53 | rect.setLTRB(0, top, bitmap.width(), bottom); |
michael@0 | 54 | top = bottom; |
michael@0 | 55 | |
michael@0 | 56 | SkDEBUGCODE(bool extracted = )bitmap.extractSubset(&fBitmaps[i], rect); |
michael@0 | 57 | SkASSERT(extracted); |
michael@0 | 58 | SkBaseDevice* device = new SkBitmapDevice(fBitmaps[i]); |
michael@0 | 59 | SkCanvas* canvas = new SkCanvas(device); |
michael@0 | 60 | device->unref(); |
michael@0 | 61 | if (initial != NULL) { |
michael@0 | 62 | canvas->setMatrix(*initial); |
michael@0 | 63 | } |
michael@0 | 64 | canvas->translate(SkIntToScalar(-rect.left()), |
michael@0 | 65 | SkIntToScalar(-rect.top())); |
michael@0 | 66 | if (0 == i) { |
michael@0 | 67 | fReader.setCanvas(canvas); |
michael@0 | 68 | } else { |
michael@0 | 69 | fReaders[i - 1].setCanvas(canvas); |
michael@0 | 70 | fReaders[i - 1].setBitmapDecoder(proc); |
michael@0 | 71 | } |
michael@0 | 72 | canvas->unref(); |
michael@0 | 73 | } |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | void TiledPipeController::notifyWritten(size_t bytes) { |
michael@0 | 77 | for (int i = 0; i < NumberOfTiles - 1; i++) { |
michael@0 | 78 | fReaders[i].playback(this->getData(), bytes); |
michael@0 | 79 | } |
michael@0 | 80 | this->INHERITED::notifyWritten(bytes); |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 84 | |
michael@0 | 85 | ThreadSafePipeController::ThreadSafePipeController(int numberOfReaders) |
michael@0 | 86 | : fAllocator(kMinBlockSize) |
michael@0 | 87 | , fNumberOfReaders(numberOfReaders) { |
michael@0 | 88 | fBlock = NULL; |
michael@0 | 89 | fBytesWritten = 0; |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | void* ThreadSafePipeController::requestBlock(size_t minRequest, size_t *actual) { |
michael@0 | 93 | if (fBlock) { |
michael@0 | 94 | // Save the previous block for later |
michael@0 | 95 | PipeBlock previousBloc(fBlock, fBytesWritten); |
michael@0 | 96 | fBlockList.push(previousBloc); |
michael@0 | 97 | } |
michael@0 | 98 | int32_t blockSize = SkMax32(SkToS32(minRequest), kMinBlockSize); |
michael@0 | 99 | fBlock = fAllocator.allocThrow(blockSize); |
michael@0 | 100 | fBytesWritten = 0; |
michael@0 | 101 | *actual = blockSize; |
michael@0 | 102 | return fBlock; |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | void ThreadSafePipeController::notifyWritten(size_t bytes) { |
michael@0 | 106 | fBytesWritten += bytes; |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | void ThreadSafePipeController::draw(SkCanvas* target) { |
michael@0 | 110 | SkGPipeReader reader(target); |
michael@0 | 111 | for (int currentBlock = 0; currentBlock < fBlockList.count(); currentBlock++ ) { |
michael@0 | 112 | reader.playback(fBlockList[currentBlock].fBlock, fBlockList[currentBlock].fBytes); |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | if (fBlock) { |
michael@0 | 116 | reader.playback(fBlock, fBytesWritten); |
michael@0 | 117 | } |
michael@0 | 118 | } |