blob: 2383c623d089a6f3e776de4f35fef9c8b6151f3a [file] [log] [blame]
bsalomon371bcbc2014-12-01 08:19:34 -08001/*
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
13class GrIndexBufferAllocPool;
14class GrVertexBufferAllocPool;
15class 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 */
23class GrFlushToGpuDrawTarget : public GrClipTarget {
24public:
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
45protected:
46 GrGpu* getGpu() { return fGpu; }
47 const GrGpu* getGpu() const{ return fGpu; }
48
49private:
50 enum {
51 kGeoPoolStatePreAllocCnt = 4,
52 };
53
54 struct GeometryPoolState {
55 const GrVertexBuffer* fPoolVertexBuffer;
56 int fPoolStartVertex;
57 const GrIndexBuffer* fPoolIndexBuffer;
58 int fPoolStartIndex;
59 // caller may conservatively over reserve vertices / indices.
60 // we release unused space back to allocator if possible
61 // can only do this if there isn't an intervening pushGeometrySource()
62 size_t fUsedPoolVertexBytes;
63 size_t fUsedPoolIndexBytes;
64 };
65
66 typedef SkSTArray<kGeoPoolStatePreAllocCnt, GeometryPoolState> GeoPoolStateStack;
67
68 virtual void onReset() = 0;
69
70 virtual void onFlush() = 0;
71
72 void setDrawBuffers(DrawInfo*, size_t stride) SK_OVERRIDE;
73 bool onReserveVertexSpace(size_t vertexSize, int vertexCount, void** vertices) SK_OVERRIDE;
74 bool onReserveIndexSpace(int indexCount, void** indices) SK_OVERRIDE;
75 void releaseReservedVertexSpace() SK_OVERRIDE;
76 void releaseReservedIndexSpace() SK_OVERRIDE;
77 void geometrySourceWillPush() SK_OVERRIDE;
78 void geometrySourceWillPop(const GeometrySrcState& restoredState) SK_OVERRIDE;
79 void willReserveVertexAndIndexSpace(int vertexCount,
80 size_t vertexStride,
81 int indexCount) SK_OVERRIDE;
bsalomoneff2c722014-12-02 09:40:12 -080082 bool onCanCopySurface(const GrSurface* dst,
83 const GrSurface* src,
84 const SkIRect& srcRect,
85 const SkIPoint& dstPoint) SK_OVERRIDE;
86 bool onInitCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) SK_OVERRIDE;
bsalomon371bcbc2014-12-01 08:19:34 -080087
88 GeoPoolStateStack fGeoPoolStateStack;
89 SkAutoTUnref<GrGpu> fGpu;
90 GrVertexBufferAllocPool* fVertexPool;
91 GrIndexBufferAllocPool* fIndexPool;
92 bool fFlushing;
93
94 typedef GrClipTarget INHERITED;
95};
96
97#endif