• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "SkBitmap.h"
9 #include "SkChunkAlloc.h"
10 #include "SkGPipe.h"
11 #include "SkTDArray.h"
12 
13 class SkCanvas;
14 class SkMatrix;
15 
16 class PipeController : public SkGPipeController {
17 public:
18     PipeController(SkCanvas* target);
19     virtual ~PipeController();
20     virtual void* requestBlock(size_t minRequest, size_t* actual) SK_OVERRIDE;
21     virtual void notifyWritten(size_t bytes) SK_OVERRIDE;
22 protected:
getData()23     const void* getData() { return (const char*) fBlock + fBytesWritten; }
24     SkGPipeReader fReader;
25 private:
26     void* fBlock;
27     size_t fBlockSize;
28     size_t fBytesWritten;
29     SkGPipeReader::Status fStatus;
30 };
31 
32 ////////////////////////////////////////////////////////////////////////////////
33 
34 class TiledPipeController : public PipeController {
35 public:
36     TiledPipeController(const SkBitmap&, const SkMatrix* initialMatrix = NULL);
~TiledPipeController()37     virtual ~TiledPipeController() {};
38     virtual void notifyWritten(size_t bytes) SK_OVERRIDE;
numberOfReaders()39     virtual int numberOfReaders() const SK_OVERRIDE { return NumberOfTiles; }
40 private:
41     enum {
42         NumberOfTiles = 10
43     };
44     SkGPipeReader fReaders[NumberOfTiles - 1];
45     SkBitmap fBitmaps[NumberOfTiles];
46     typedef PipeController INHERITED;
47 };
48 
49 ////////////////////////////////////////////////////////////////////////////////
50 
51 /**
52  * Borrowed (and modified) from SkDeferredCanvas.cpp::DeferredPipeController.
53  * Allows playing back from multiple threads, but does not do the threading itself.
54  */
55 class ThreadSafePipeController : public SkGPipeController {
56 public:
57     ThreadSafePipeController(int numberOfReaders);
58     virtual void* requestBlock(size_t minRequest, size_t* actual) SK_OVERRIDE;
59     virtual void notifyWritten(size_t bytes) SK_OVERRIDE;
numberOfReaders()60     virtual int numberOfReaders() const SK_OVERRIDE { return fNumberOfReaders; }
61 
62     /**
63      * Play the stored drawing commands to the specified canvas. If SkGPipeWriter::startRecording
64      * used the flag SkGPipeWriter::kSimultaneousReaders_Flag, this can be called from different
65      * threads simultaneously.
66      */
67     void draw(SkCanvas*);
68 private:
69     enum {
70         kMinBlockSize = 4096
71     };
72     struct PipeBlock {
PipeBlockPipeBlock73         PipeBlock(void* block, size_t bytes) { fBlock = block, fBytes = bytes; }
74         // Stream of draw commands written by the SkGPipeWriter. Allocated by fAllocator, which will
75         // handle freeing it.
76         void* fBlock;
77         // Number of bytes that were written to fBlock.
78         size_t fBytes;
79     };
80     void* fBlock;
81     size_t fBytesWritten;
82     SkChunkAlloc fAllocator;
83     SkTDArray<PipeBlock> fBlockList;
84     int fNumberOfReaders;
85 };
86