blob: ac1b79cd55f02c59eb3d011a2db8866d2940a7f0 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 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.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
reed@google.comac10a2d2010-12-22 21:39:39 +00008#ifndef GrInOrderDrawBuffer_DEFINED
9#define GrInOrderDrawBuffer_DEFINED
10
bsalomon371bcbc2014-12-01 08:19:34 -080011#include "GrFlushToGpuDrawTarget.h"
robertphillips193ea932015-03-03 12:40:49 -080012#include "GrTargetCommands.h"
robertphillips9888b222015-02-27 08:50:34 -080013#include "SkChunkAlloc.h"
robertphillipsdad77942015-03-03 09:28:16 -080014
15/**
16 * GrInOrderDrawBuffer is an implementation of GrDrawTarget that queues up draws for eventual
17 * playback into a GrGpu. In theory one draw buffer could playback into another. When index or
18 * vertex buffers are used as geometry sources it is the callers the draw buffer only holds
19 * references to the buffers. It is the callers responsibility to ensure that the data is still
20 * valid when the draw buffer is played back into a GrGpu. Similarly, it is the caller's
21 * responsibility to ensure that all referenced textures, buffers, and render-targets are associated
22 * in the GrGpu object that the buffer is played back into. The buffer requires VB and IB pools to
23 * store geometry.
24 */
25class GrInOrderDrawBuffer : public GrFlushToGpuDrawTarget {
26public:
27
28 /**
29 * Creates a GrInOrderDrawBuffer
30 *
31 * @param gpu the gpu object that this draw buffer flushes to.
32 * @param vertexPool pool where vertices for queued draws will be saved when
33 * the vertex source is either reserved or array.
34 * @param indexPool pool where indices for queued draws will be saved when
35 * the index source is either reserved or array.
36 */
37 GrInOrderDrawBuffer(GrGpu* gpu,
38 GrVertexBufferAllocPool* vertexPool,
39 GrIndexBufferAllocPool* indexPool);
40
mtklein36352bf2015-03-25 18:17:31 -070041 ~GrInOrderDrawBuffer() override;
robertphillipsdad77942015-03-03 09:28:16 -080042
43 // tracking for draws
mtklein36352bf2015-03-25 18:17:31 -070044 DrawToken getCurrentDrawToken() override { return DrawToken(this, fDrawID); }
robertphillipsdad77942015-03-03 09:28:16 -080045
46 void clearStencilClip(const SkIRect& rect,
47 bool insideClip,
mtklein36352bf2015-03-25 18:17:31 -070048 GrRenderTarget* renderTarget) override;
robertphillipsdad77942015-03-03 09:28:16 -080049
mtklein36352bf2015-03-25 18:17:31 -070050 void discard(GrRenderTarget*) override;
robertphillipsdad77942015-03-03 09:28:16 -080051
52protected:
53 void willReserveVertexAndIndexSpace(int vertexCount,
54 size_t vertexStride,
mtklein36352bf2015-03-25 18:17:31 -070055 int indexCount) override;
robertphillipsdad77942015-03-03 09:28:16 -080056
57 void appendIndicesAndTransforms(const void* indexValues, PathIndexType indexType,
58 const float* transformValues, PathTransformType transformType,
59 int count, char** indicesLocation, float** xformsLocation) {
60 int indexBytes = GrPathRange::PathIndexSizeInBytes(indexType);
61 *indicesLocation = (char*) fPathIndexBuffer.alloc(count * indexBytes,
62 SkChunkAlloc::kThrow_AllocFailType);
63 SkASSERT(SkIsAlign4((uintptr_t)*indicesLocation));
64 memcpy(*indicesLocation, reinterpret_cast<const char*>(indexValues), count * indexBytes);
65
66 const int xformBytes = GrPathRendering::PathTransformSize(transformType) * sizeof(float);
67 *xformsLocation = NULL;
68
69 if (0 != xformBytes) {
70 *xformsLocation = (float*) fPathTransformBuffer.alloc(count * xformBytes,
71 SkChunkAlloc::kThrow_AllocFailType);
72 SkASSERT(SkIsAlign4((uintptr_t)*xformsLocation));
73 memcpy(*xformsLocation, transformValues, count * xformBytes);
74 }
75 }
76
77 bool canConcatToIndexBuffer(const GrIndexBuffer** ib) {
78 const GrDrawTarget::GeometrySrcState& geomSrc = this->getGeomSrc();
79
80 // we only attempt to concat when reserved verts are used with a client-specified
81 // index buffer. To make this work with client-specified VBs we'd need to know if the VB
82 // was updated between draws.
83 if (kReserved_GeometrySrcType != geomSrc.fVertexSrc ||
84 kBuffer_GeometrySrcType != geomSrc.fIndexSrc) {
85 return false;
86 }
87
88 *ib = geomSrc.fIndexBuffer;
89 return true;
90 }
91
92private:
93 friend class GrTargetCommands;
94
mtklein36352bf2015-03-25 18:17:31 -070095 void onReset() override;
96 void onFlush() override;
bsalomon371bcbc2014-12-01 08:19:34 -080097
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000098 // overrides from GrDrawTarget
mtklein36352bf2015-03-25 18:17:31 -070099 void onDraw(const GrGeometryProcessor*, const DrawInfo&, const PipelineInfo&) override;
100 void onDrawBatch(GrBatch*, const PipelineInfo&) override;
egdaniel8dd688b2015-01-22 10:16:09 -0800101 void onDrawRect(GrPipelineBuilder*,
joshualitt2e3b3e32014-12-09 13:31:14 -0800102 GrColor,
joshualitt8059eb92014-12-29 15:10:07 -0800103 const SkMatrix& viewMatrix,
bsalomonae59b772014-11-19 08:23:49 -0800104 const SkRect& rect,
105 const SkRect* localRect,
mtklein36352bf2015-03-25 18:17:31 -0700106 const SkMatrix* localMatrix) override;
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000107
egdaniel8dd688b2015-01-22 10:16:09 -0800108 void onStencilPath(const GrPipelineBuilder&,
joshualitt56995b52014-12-11 15:44:02 -0800109 const GrPathProcessor*,
bsalomonae59b772014-11-19 08:23:49 -0800110 const GrPath*,
bsalomon3e791242014-12-17 13:43:13 -0800111 const GrScissorState&,
mtklein36352bf2015-03-25 18:17:31 -0700112 const GrStencilSettings&) override;
egdaniele36914c2015-02-13 09:00:33 -0800113 void onDrawPath(const GrPathProcessor*,
bsalomonae59b772014-11-19 08:23:49 -0800114 const GrPath*,
bsalomonae59b772014-11-19 08:23:49 -0800115 const GrStencilSettings&,
mtklein36352bf2015-03-25 18:17:31 -0700116 const PipelineInfo&) override;
egdaniele36914c2015-02-13 09:00:33 -0800117 void onDrawPaths(const GrPathProcessor*,
bsalomonae59b772014-11-19 08:23:49 -0800118 const GrPathRange*,
cdalton55b24af2014-11-25 11:00:56 -0800119 const void* indices,
120 PathIndexType,
121 const float transformValues[],
bsalomonae59b772014-11-19 08:23:49 -0800122 PathTransformType,
cdalton55b24af2014-11-25 11:00:56 -0800123 int count,
bsalomonae59b772014-11-19 08:23:49 -0800124 const GrStencilSettings&,
mtklein36352bf2015-03-25 18:17:31 -0700125 const PipelineInfo&) override;
bsalomonae59b772014-11-19 08:23:49 -0800126 void onClear(const SkIRect* rect,
127 GrColor color,
128 bool canIgnoreRect,
mtklein36352bf2015-03-25 18:17:31 -0700129 GrRenderTarget* renderTarget) override;
bsalomonf90a02b2014-11-26 12:28:00 -0800130 bool onCopySurface(GrSurface* dst,
131 GrSurface* src,
132 const SkIRect& srcRect,
mtklein36352bf2015-03-25 18:17:31 -0700133 const SkIPoint& dstPoint) override;
bsalomon@google.com116ad842013-04-09 15:38:19 +0000134
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000135 // Attempts to concat instances from info onto the previous draw. info must represent an
136 // instanced draw. The caller must have already recorded a new draw state and clip if necessary.
egdaniele36914c2015-02-13 09:00:33 -0800137 int concatInstancedDraw(const DrawInfo&);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000138
bsalomon838f62d2014-08-05 07:15:57 -0700139 // We lazily record clip changes in order to skip clips that have no effect.
cdalton6819df32014-10-15 13:43:48 -0700140 void recordClipIfNecessary();
robertphillips7f966f42015-03-02 06:40:12 -0800141 // Records any trace markers for a command
robertphillipsdad77942015-03-03 09:28:16 -0800142 void recordTraceMarkersIfNecessary(GrTargetCommands::Cmd*);
robertphillips7f966f42015-03-02 06:40:12 -0800143 SkString getCmdString(int index) const {
144 SkASSERT(index < fGpuCmdMarkers.count());
145 return fGpuCmdMarkers[index].toString();
146 }
mtklein36352bf2015-03-25 18:17:31 -0700147 bool isIssued(uint32_t drawID) override { return drawID != fDrawID; }
bsalomon@google.com934c5702012-03-20 21:17:58 +0000148
bsalomon@google.com116ad842013-04-09 15:38:19 +0000149 // TODO: Use a single allocator for commands and records
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000150 enum {
cdalton55b24af2014-11-25 11:00:56 -0800151 kPathIdxBufferMinReserve = 2 * 64, // 64 uint16_t's
152 kPathXformBufferMinReserve = 2 * 64, // 64 two-float transforms
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000153 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000154
robertphillipsdad77942015-03-03 09:28:16 -0800155 GrTargetCommands fCommands;
bsalomonae59b772014-11-19 08:23:49 -0800156 SkTArray<GrTraceMarkerSet, false> fGpuCmdMarkers;
robertphillips9888b222015-02-27 08:50:34 -0800157 SkChunkAlloc fPathIndexBuffer;
158 SkChunkAlloc fPathTransformBuffer;
bsalomonae59b772014-11-19 08:23:49 -0800159 uint32_t fDrawID;
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000160
bsalomon371bcbc2014-12-01 08:19:34 -0800161 typedef GrFlushToGpuDrawTarget INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000162};
163
164#endif