aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/gpu/GrFlushToGpuDrawTarget.h
blob: fa0ff4aa9cb2096125560382a87bb334d1a939dc (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
 * Copyright 2014 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef GrFlushToGpuDrawTarget_DEFINED
#define GrFlushToGpuDrawTarget_DEFINED

#include "GrDrawTarget.h"

class GrIndexBufferAllocPool;
class GrVertexBufferAllocPool;
class GrGpu;

/**
 * Base class for draw targets that accumulate index and vertex data in buffers for deferred.
 * When draw target clients reserve geometry this subclass will place that geometry into
 * preallocated vertex/index buffers in the order the requests are made (assuming the requests fit
 * in the preallocated buffers).
 */
class GrFlushToGpuDrawTarget : public GrClipTarget {
public:
    GrFlushToGpuDrawTarget(GrGpu*, GrVertexBufferAllocPool*,GrIndexBufferAllocPool*);

    ~GrFlushToGpuDrawTarget() override;

    /**
     * Empties the draw buffer of any queued up draws. This must not be called while inside an
     * unbalanced pushGeometrySource().
     */
    void reset();

    /**
     * This plays any queued up draws to its GrGpu target. It also resets this object (i.e. flushing
     * is destructive). This buffer must not have an active reserved vertex or index source. Any
     * reserved geometry on the target will be finalized because it's geometry source will be pushed
     * before flushing and popped afterwards.
     */
    void flush();

    bool geometryHints(size_t vertexStride, int* vertexCount, int* indexCount) const override;

protected:
    GrGpu* getGpu() { return fGpu; }
    const GrGpu* getGpu() const{ return fGpu; }

    GrVertexBufferAllocPool* getVertexAllocPool() { return fVertexPool; }
    GrIndexBufferAllocPool* getIndexAllocPool() { return fIndexPool; }

    // TODO all of this goes away when batch is everywhere
    enum {
        kGeoPoolStatePreAllocCnt = 4,
    };

    struct GeometryPoolState {
        const GrVertexBuffer*   fPoolVertexBuffer;
        int                     fPoolStartVertex;
        const GrIndexBuffer*    fPoolIndexBuffer;
        int                     fPoolStartIndex;
        // caller may conservatively over reserve vertices / indices.
        // we release unused space back to allocator if possible
        // can only do this if there isn't an intervening pushGeometrySource()
        size_t                  fUsedPoolVertexBytes;
        size_t                  fUsedPoolIndexBytes;
    };

    typedef SkSTArray<kGeoPoolStatePreAllocCnt, GeometryPoolState> GeoPoolStateStack;
    const GeoPoolStateStack& getGeoPoolStateStack() const { return fGeoPoolStateStack; }

    void willReserveVertexAndIndexSpace(int vertexCount,
                                        size_t vertexStride,
                                        int indexCount) override;

private:
    virtual void onReset() = 0;

    virtual void onFlush() = 0;

    void setDrawBuffers(DrawInfo*, size_t stride) override;
    bool onReserveVertexSpace(size_t vertexSize, int vertexCount, void** vertices) override;
    bool onReserveIndexSpace(int indexCount, void** indices) override;
    void releaseReservedVertexSpace() override;
    void releaseReservedIndexSpace() override;
    void geometrySourceWillPush() override;
    void geometrySourceWillPop(const GeometrySrcState& restoredState) override;
    bool onCanCopySurface(const GrSurface* dst,
                          const GrSurface* src,
                          const SkIRect& srcRect,
                          const SkIPoint& dstPoint) override;
    bool onInitCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) override;

    GeoPoolStateStack                   fGeoPoolStateStack;
    SkAutoTUnref<GrGpu>                 fGpu;
    GrVertexBufferAllocPool*            fVertexPool;
    GrIndexBufferAllocPool*             fIndexPool;
    bool                                fFlushing;

    typedef GrClipTarget INHERITED;
};

#endif