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