| scroggo@google.com | 72c9672 | 2012-06-06 21:07:10 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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 | |
| 8 | #include "SamplePipeControllers.h" |
| scroggo@google.com | b073d92 | 2012-06-08 15:35:03 +0000 | [diff] [blame] | 9 | |
| robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 10 | #include "SkBitmapDevice.h" |
| scroggo@google.com | 72c9672 | 2012-06-06 21:07:10 +0000 | [diff] [blame] | 11 | #include "SkCanvas.h" |
| scroggo@google.com | 72c9672 | 2012-06-06 21:07:10 +0000 | [diff] [blame] | 12 | #include "SkGPipe.h" |
| scroggo@google.com | b073d92 | 2012-06-08 15:35:03 +0000 | [diff] [blame] | 13 | #include "SkMatrix.h" |
| scroggo@google.com | 72c9672 | 2012-06-06 21:07:10 +0000 | [diff] [blame] | 14 | |
| scroggo@google.com | 74b7ffd | 2013-04-30 02:32:41 +0000 | [diff] [blame] | 15 | PipeController::PipeController(SkCanvas* target, SkPicture::InstallPixelRefProc proc) |
| commit-bot@chromium.org | 2d91eff | 2014-05-07 15:27:10 +0000 | [diff] [blame] | 16 | : fReader(target), fBlockSize(0), fBytesWritten(0) { |
| scroggo@google.com | 74b7ffd | 2013-04-30 02:32:41 +0000 | [diff] [blame] | 17 | fReader.setBitmapDecoder(proc); |
| scroggo@google.com | 72c9672 | 2012-06-06 21:07:10 +0000 | [diff] [blame] | 18 | } |
| 19 | |
| commit-bot@chromium.org | 2d91eff | 2014-05-07 15:27:10 +0000 | [diff] [blame] | 20 | void* PipeController::requestBlock(size_t minRequest, size_t* actual) { |
| 21 | fBlockSize = minRequest; |
| 22 | fBlock.reset(fBlockSize); |
| scroggo@google.com | 72c9672 | 2012-06-06 21:07:10 +0000 | [diff] [blame] | 23 | fBytesWritten = 0; |
| 24 | *actual = fBlockSize; |
| commit-bot@chromium.org | 2d91eff | 2014-05-07 15:27:10 +0000 | [diff] [blame] | 25 | return fBlock.get(); |
| scroggo@google.com | 72c9672 | 2012-06-06 21:07:10 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | void PipeController::notifyWritten(size_t bytes) { |
| 29 | fStatus = fReader.playback(this->getData(), bytes); |
| 30 | SkASSERT(SkGPipeReader::kError_Status != fStatus); |
| 31 | fBytesWritten += bytes; |
| 32 | } |
| 33 | |
| 34 | //////////////////////////////////////////////////////////////////////////////// |
| 35 | |
| scroggo@google.com | b073d92 | 2012-06-08 15:35:03 +0000 | [diff] [blame] | 36 | TiledPipeController::TiledPipeController(const SkBitmap& bitmap, |
| scroggo@google.com | 74b7ffd | 2013-04-30 02:32:41 +0000 | [diff] [blame] | 37 | SkPicture::InstallPixelRefProc proc, |
| scroggo@google.com | b073d92 | 2012-06-08 15:35:03 +0000 | [diff] [blame] | 38 | const SkMatrix* initial) |
| scroggo@google.com | 74b7ffd | 2013-04-30 02:32:41 +0000 | [diff] [blame] | 39 | : INHERITED(NULL, proc) { |
| scroggo@google.com | 72c9672 | 2012-06-06 21:07:10 +0000 | [diff] [blame] | 40 | int32_t top = 0; |
| 41 | int32_t bottom; |
| 42 | int32_t height = bitmap.height() / NumberOfTiles; |
| 43 | SkIRect rect; |
| 44 | for (int i = 0; i < NumberOfTiles; i++) { |
| 45 | bottom = i + 1 == NumberOfTiles ? bitmap.height() : top + height; |
| 46 | rect.setLTRB(0, top, bitmap.width(), bottom); |
| 47 | top = bottom; |
| 48 | |
| humper@google.com | 0e51577 | 2013-01-07 19:54:40 +0000 | [diff] [blame] | 49 | SkDEBUGCODE(bool extracted = )bitmap.extractSubset(&fBitmaps[i], rect); |
| scroggo@google.com | 72c9672 | 2012-06-06 21:07:10 +0000 | [diff] [blame] | 50 | SkASSERT(extracted); |
| robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 51 | SkBaseDevice* device = new SkBitmapDevice(fBitmaps[i]); |
| scroggo@google.com | 72c9672 | 2012-06-06 21:07:10 +0000 | [diff] [blame] | 52 | SkCanvas* canvas = new SkCanvas(device); |
| 53 | device->unref(); |
| scroggo@google.com | b073d92 | 2012-06-08 15:35:03 +0000 | [diff] [blame] | 54 | if (initial != NULL) { |
| 55 | canvas->setMatrix(*initial); |
| 56 | } |
| scroggo@google.com | 72c9672 | 2012-06-06 21:07:10 +0000 | [diff] [blame] | 57 | canvas->translate(SkIntToScalar(-rect.left()), |
| 58 | SkIntToScalar(-rect.top())); |
| 59 | if (0 == i) { |
| 60 | fReader.setCanvas(canvas); |
| 61 | } else { |
| 62 | fReaders[i - 1].setCanvas(canvas); |
| scroggo@google.com | 74b7ffd | 2013-04-30 02:32:41 +0000 | [diff] [blame] | 63 | fReaders[i - 1].setBitmapDecoder(proc); |
| scroggo@google.com | 72c9672 | 2012-06-06 21:07:10 +0000 | [diff] [blame] | 64 | } |
| 65 | canvas->unref(); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | void TiledPipeController::notifyWritten(size_t bytes) { |
| 70 | for (int i = 0; i < NumberOfTiles - 1; i++) { |
| 71 | fReaders[i].playback(this->getData(), bytes); |
| 72 | } |
| 73 | this->INHERITED::notifyWritten(bytes); |
| 74 | } |
| scroggo@google.com | 58b4ead | 2012-08-31 16:15:22 +0000 | [diff] [blame] | 75 | |
| 76 | //////////////////////////////////////////////////////////////////////////////// |
| 77 | |
| scroggo@google.com | 8e073ba | 2012-08-31 16:25:46 +0000 | [diff] [blame] | 78 | ThreadSafePipeController::ThreadSafePipeController(int numberOfReaders) |
| scroggo@google.com | 58b4ead | 2012-08-31 16:15:22 +0000 | [diff] [blame] | 79 | : fAllocator(kMinBlockSize) |
| 80 | , fNumberOfReaders(numberOfReaders) { |
| 81 | fBlock = NULL; |
| 82 | fBytesWritten = 0; |
| 83 | } |
| 84 | |
| scroggo@google.com | 8e073ba | 2012-08-31 16:25:46 +0000 | [diff] [blame] | 85 | void* ThreadSafePipeController::requestBlock(size_t minRequest, size_t *actual) { |
| scroggo@google.com | 58b4ead | 2012-08-31 16:15:22 +0000 | [diff] [blame] | 86 | if (fBlock) { |
| 87 | // Save the previous block for later |
| 88 | PipeBlock previousBloc(fBlock, fBytesWritten); |
| 89 | fBlockList.push(previousBloc); |
| 90 | } |
| scroggo@google.com | e5f4824 | 2013-02-25 21:47:41 +0000 | [diff] [blame] | 91 | int32_t blockSize = SkMax32(SkToS32(minRequest), kMinBlockSize); |
| scroggo@google.com | 58b4ead | 2012-08-31 16:15:22 +0000 | [diff] [blame] | 92 | fBlock = fAllocator.allocThrow(blockSize); |
| 93 | fBytesWritten = 0; |
| 94 | *actual = blockSize; |
| 95 | return fBlock; |
| 96 | } |
| 97 | |
| scroggo@google.com | 8e073ba | 2012-08-31 16:25:46 +0000 | [diff] [blame] | 98 | void ThreadSafePipeController::notifyWritten(size_t bytes) { |
| scroggo@google.com | 58b4ead | 2012-08-31 16:15:22 +0000 | [diff] [blame] | 99 | fBytesWritten += bytes; |
| 100 | } |
| 101 | |
| scroggo@google.com | bcdf2ec | 2012-09-20 14:42:33 +0000 | [diff] [blame] | 102 | void ThreadSafePipeController::draw(SkCanvas* target) { |
| scroggo@google.com | 58b4ead | 2012-08-31 16:15:22 +0000 | [diff] [blame] | 103 | SkGPipeReader reader(target); |
| 104 | for (int currentBlock = 0; currentBlock < fBlockList.count(); currentBlock++ ) { |
| 105 | reader.playback(fBlockList[currentBlock].fBlock, fBlockList[currentBlock].fBytes); |
| 106 | } |
| 107 | |
| 108 | if (fBlock) { |
| 109 | reader.playback(fBlock, fBytesWritten); |
| 110 | } |
| 111 | } |