aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/pipe/utils/SamplePipeControllers.h
blob: 3442c49d675dcaaea87ad2d1c2718a5e818da850 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
 * Copyright 2012 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkBitmap.h"
#include "SkChunkAlloc.h"
#include "SkGPipe.h"
#include "SkPicture.h"
#include "SkTDArray.h"

class SkCanvas;
class SkMatrix;

class PipeController : public SkGPipeController {
public:
    PipeController(SkCanvas* target, SkPicture::InstallPixelRefProc proc = NULL);
    virtual ~PipeController();
    void* requestBlock(size_t minRequest, size_t* actual) SK_OVERRIDE;
    void notifyWritten(size_t bytes) SK_OVERRIDE;
protected:
    const void* getData() { return (const char*) fBlock + fBytesWritten; }
    SkGPipeReader fReader;
private:
    void* fBlock;
    size_t fBlockSize;
    size_t fBytesWritten;
    SkGPipeReader::Status fStatus;
};

////////////////////////////////////////////////////////////////////////////////

class TiledPipeController : public PipeController {
public:
    TiledPipeController(const SkBitmap&, SkPicture::InstallPixelRefProc proc = NULL,
                        const SkMatrix* initialMatrix = NULL);
    virtual ~TiledPipeController() {};
    void notifyWritten(size_t bytes) SK_OVERRIDE;
    int numberOfReaders() const SK_OVERRIDE { return NumberOfTiles; }
private:
    enum {
        NumberOfTiles = 10
    };
    SkGPipeReader fReaders[NumberOfTiles - 1];
    SkBitmap fBitmaps[NumberOfTiles];
    typedef PipeController INHERITED;
};

////////////////////////////////////////////////////////////////////////////////

/**
 * Borrowed (and modified) from SkDeferredCanvas.cpp::DeferredPipeController.
 * Allows playing back from multiple threads, but does not do the threading itself.
 */
class ThreadSafePipeController : public SkGPipeController {
public:
    ThreadSafePipeController(int numberOfReaders);
    void* requestBlock(size_t minRequest, size_t* actual) SK_OVERRIDE;
    void notifyWritten(size_t bytes) SK_OVERRIDE;
    int numberOfReaders() const SK_OVERRIDE { return fNumberOfReaders; }

    /**
     * Play the stored drawing commands to the specified canvas. If SkGPipeWriter::startRecording
     * used the flag SkGPipeWriter::kSimultaneousReaders_Flag, this can be called from different
     * threads simultaneously.
     */
    void draw(SkCanvas*);
private:
    enum {
        kMinBlockSize = 4096
    };
    struct PipeBlock {
        PipeBlock(void* block, size_t bytes) { fBlock = block, fBytes = bytes; }
        // Stream of draw commands written by the SkGPipeWriter. Allocated by fAllocator, which will
        // handle freeing it.
        void* fBlock;
        // Number of bytes that were written to fBlock.
        size_t fBytes;
    };
    void* fBlock;
    size_t fBytesWritten;
    SkChunkAlloc fAllocator;
    SkTDArray<PipeBlock> fBlockList;
    int fNumberOfReaders;
};