4c72f93572d1054721b85cb1ee796ed80b10fab1
[platform/upstream/libSkiaSharp.git] / src / gpu / GrFlushToGpuDrawTarget.h
1 /*
2  * Copyright 2014 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 #ifndef GrFlushToGpuDrawTarget_DEFINED
9 #define GrFlushToGpuDrawTarget_DEFINED
10
11 #include "GrDrawTarget.h"
12
13 class GrIndexBufferAllocPool;
14 class GrVertexBufferAllocPool;
15 class GrGpu;
16
17 /**
18  * Base class for draw targets that accumulate index and vertex data in buffers for deferred.
19  * When draw target clients reserve geometry this subclass will place that geometry into
20  * preallocated vertex/index buffers in the order the requests are made (assuming the requests fit
21  * in the preallocated buffers).
22  */
23 class GrFlushToGpuDrawTarget : public GrClipTarget {
24 public:
25     GrFlushToGpuDrawTarget(GrGpu*, GrVertexBufferAllocPool*,GrIndexBufferAllocPool*);
26
27     ~GrFlushToGpuDrawTarget() SK_OVERRIDE;
28
29     /**
30      * Empties the draw buffer of any queued up draws. This must not be called while inside an
31      * unbalanced pushGeometrySource().
32      */
33     void reset();
34
35     /**
36      * This plays any queued up draws to its GrGpu target. It also resets this object (i.e. flushing
37      * is destructive). This buffer must not have an active reserved vertex or index source. Any
38      * reserved geometry on the target will be finalized because it's geometry source will be pushed
39      * before flushing and popped afterwards.
40      */
41     void flush();
42
43     bool geometryHints(size_t vertexStride, int* vertexCount, int* indexCount) const SK_OVERRIDE;
44
45 protected:
46     GrGpu* getGpu() { return fGpu; }
47     const GrGpu* getGpu() const{ return fGpu; }
48
49     GrVertexBufferAllocPool* getVertexAllocPool() { return fVertexPool; }
50     GrIndexBufferAllocPool* getIndexAllocPool() { return fIndexPool; }
51
52     // TODO all of this goes away when batch is everywhere
53     enum {
54         kGeoPoolStatePreAllocCnt = 4,
55     };
56
57     struct GeometryPoolState {
58         const GrVertexBuffer*   fPoolVertexBuffer;
59         int                     fPoolStartVertex;
60         const GrIndexBuffer*    fPoolIndexBuffer;
61         int                     fPoolStartIndex;
62         // caller may conservatively over reserve vertices / indices.
63         // we release unused space back to allocator if possible
64         // can only do this if there isn't an intervening pushGeometrySource()
65         size_t                  fUsedPoolVertexBytes;
66         size_t                  fUsedPoolIndexBytes;
67     };
68
69     typedef SkSTArray<kGeoPoolStatePreAllocCnt, GeometryPoolState> GeoPoolStateStack;
70     const GeoPoolStateStack& getGeoPoolStateStack() const { return fGeoPoolStateStack; }
71
72     void willReserveVertexAndIndexSpace(int vertexCount,
73                                         size_t vertexStride,
74                                         int indexCount) SK_OVERRIDE;
75
76 private:
77     virtual void onReset() = 0;
78
79     virtual void onFlush() = 0;
80
81     void setDrawBuffers(DrawInfo*, size_t stride) SK_OVERRIDE;
82     bool onReserveVertexSpace(size_t vertexSize, int vertexCount, void** vertices) SK_OVERRIDE;
83     bool onReserveIndexSpace(int indexCount, void** indices) SK_OVERRIDE;
84     void releaseReservedVertexSpace() SK_OVERRIDE;
85     void releaseReservedIndexSpace() SK_OVERRIDE;
86     void geometrySourceWillPush() SK_OVERRIDE;
87     void geometrySourceWillPop(const GeometrySrcState& restoredState) SK_OVERRIDE;
88     bool onCanCopySurface(const GrSurface* dst,
89                           const GrSurface* src,
90                           const SkIRect& srcRect,
91                           const SkIPoint& dstPoint) SK_OVERRIDE;
92     bool onInitCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) SK_OVERRIDE;
93
94     GeoPoolStateStack                   fGeoPoolStateStack;
95     SkAutoTUnref<GrGpu>                 fGpu;
96     GrVertexBufferAllocPool*            fVertexPool;
97     GrIndexBufferAllocPool*             fIndexPool;
98     bool                                fFlushing;
99
100     typedef GrClipTarget INHERITED;
101 };
102
103 #endif