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