blob: 059c1b3ed20adbbf1ee39b00cf40b7493f25b0d0 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2010 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 GrDrawTarget_DEFINED
9#define GrDrawTarget_DEFINED
10
joshualitt44701df2015-02-23 14:44:57 -080011#include "GrClip.h"
joshualitt6db519c2014-10-29 08:48:18 -070012#include "GrClipMaskManager.h"
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +000013#include "GrContext.h"
joshualitt8072caa2015-02-12 14:20:52 -080014#include "GrPathProcessor.h"
15#include "GrPrimitiveProcessor.h"
bsalomon@google.com934c5702012-03-20 21:17:58 +000016#include "GrIndexBuffer.h"
kkinnunenccdaa042014-08-20 01:36:23 -070017#include "GrPathRendering.h"
egdaniel8dd688b2015-01-22 10:16:09 -080018#include "GrPipelineBuilder.h"
bsalomona387a112015-08-11 14:47:42 -070019#include "GrPipeline.h"
joshualitt7eb8c7b2014-11-18 14:24:27 -080020#include "GrVertexBuffer.h"
bsalomon6a44c6a2015-05-26 09:49:05 -070021#include "GrXferProcessor.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000022
bsalomon@google.com8d67c072012-12-13 20:38:14 +000023#include "SkClipStack.h"
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +000024#include "SkMatrix.h"
sugoi@google.com12b4e272012-12-06 20:13:11 +000025#include "SkPath.h"
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +000026#include "SkStrokeRec.h"
robertphillips@google.coma2d71482012-08-01 20:08:47 +000027#include "SkTArray.h"
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +000028#include "SkTLazy.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000029#include "SkTypes.h"
bsalomon@google.com8d67c072012-12-13 20:38:14 +000030#include "SkXfermode.h"
Scroggo97c88c22011-05-11 14:05:25 +000031
bsalomon53469832015-08-18 09:20:09 -070032class GrBatch;
joshualitt44701df2015-02-23 14:44:57 -080033class GrClip;
bsalomon4b91f762015-05-19 09:29:46 -070034class GrCaps;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000035class GrPath;
bsalomonabd30f52015-08-13 13:34:48 -070036class GrDrawBatch;
bsalomon1fcc01c2015-09-09 09:48:06 -070037class GrDrawPathBatchBase;
38class GrPathRangeDraw;
sugoi@google.com12b4e272012-12-06 20:13:11 +000039
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +000040class GrDrawTarget : public SkRefCnt {
bsalomon@google.comf6601872012-08-28 21:11:35 +000041public:
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000042 // The context may not be fully constructed and should not be used during GrDrawTarget
43 // construction.
bsalomon4061b122015-05-29 10:26:19 -070044 GrDrawTarget(GrGpu* gpu, GrResourceProvider*);
bsalomona73239a2015-04-28 13:35:17 -070045
bsalomon512be532015-09-10 10:42:55 -070046 ~GrDrawTarget() override;
reed@google.comac10a2d2010-12-22 21:39:39 +000047
48 /**
bsalomona73239a2015-04-28 13:35:17 -070049 * Empties the draw buffer of any queued up draws.
50 */
bsalomon512be532015-09-10 10:42:55 -070051 void reset();
bsalomona73239a2015-04-28 13:35:17 -070052
53 /**
54 * This plays any queued up draws to its GrGpu target. It also resets this object (i.e. flushing
55 * is destructive).
56 */
57 void flush();
58
59 /**
bsalomon@google.com18c9c192011-09-22 21:01:31 +000060 * Gets the capabilities of the draw target.
61 */
bsalomon4061b122015-05-29 10:26:19 -070062 const GrCaps* caps() const { return fCaps; }
bsalomon@google.com18c9c192011-09-22 21:01:31 +000063
bsalomonabd30f52015-08-13 13:34:48 -070064 void drawBatch(const GrPipelineBuilder&, GrDrawBatch*);
joshualitt4d8da812015-01-28 12:53:54 -080065
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000066 /**
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000067 * Draws path into the stencil buffer. The fill must be either even/odd or
68 * winding (not inverse or hairline). It will respect the HW antialias flag
egdaniel8dd688b2015-01-22 10:16:09 -080069 * on the GrPipelineBuilder (if possible in the 3D API). Note, we will never have an inverse
70 * fill with stencil path
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000071 */
joshualittf2384692015-09-10 11:00:51 -070072 void stencilPath(const GrPipelineBuilder&, const SkMatrix& viewMatrix, const GrPath*,
egdaniel8dd688b2015-01-22 10:16:09 -080073 GrPathRendering::FillType);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000074
75 /**
commit-bot@chromium.org32184d82013-10-09 15:14:18 +000076 * Draws a path. Fill must not be a hairline. It will respect the HW
egdaniel8dd688b2015-01-22 10:16:09 -080077 * antialias flag on the GrPipelineBuilder (if possible in the 3D API).
bsalomon1fcc01c2015-09-09 09:48:06 -070078 *
79 * TODO: Remove this function and construct the batch outside GrDrawTarget.
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +000080 */
joshualittf2384692015-09-10 11:00:51 -070081 void drawPath(const GrPipelineBuilder&, const SkMatrix& viewMatrix, GrColor color,
82 const GrPath*, GrPathRendering::FillType);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +000083
84 /**
cdalton55b24af2014-11-25 11:00:56 -080085 * Draws the aggregate path from combining multiple. Note that this will not
86 * always be equivalent to back-to-back calls to drawPath(). It will respect
egdaniel8dd688b2015-01-22 10:16:09 -080087 * the HW antialias flag on the GrPipelineBuilder (if possible in the 3D API).
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +000088 *
bsalomon1fcc01c2015-09-09 09:48:06 -070089 * TODO: Remove this function and construct the batch outside GrDrawTarget.
90 *
91 * @param draw The range, transforms, and indices for the draw.
92 * This object must only be drawn once. The draw
93 * may modify its contents.
cdaltonb85a0aa2014-07-21 15:32:44 -070094 * @param fill Fill type for drawing all the paths
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +000095 */
bsalomon1fcc01c2015-09-09 09:48:06 -070096 void drawPathsFromRange(const GrPipelineBuilder&,
joshualittf2384692015-09-10 11:00:51 -070097 const SkMatrix& viewMatrix,
98 const SkMatrix& localMatrix,
99 GrColor color,
bsalomon1fcc01c2015-09-09 09:48:06 -0700100 GrPathRangeDraw* draw,
101 GrPathRendering::FillType fill);
cdaltonb85a0aa2014-07-21 15:32:44 -0700102
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000103 /**
bsalomon72e3ae42015-04-28 08:08:46 -0700104 * Helper function for drawing rects.
skia.committer@gmail.com044679e2013-02-15 07:16:57 +0000105 *
bsalomon@google.comc7818882013-03-20 19:19:53 +0000106 * @param rect the rect to draw
bsalomon@google.comc7818882013-03-20 19:19:53 +0000107 * @param localRect optional rect that specifies local coords to map onto
halcanary96fcdcc2015-08-27 07:41:13 -0700108 * rect. If nullptr then rect serves as the local coords.
joshualitt8fc6c2d2014-12-22 15:27:05 -0800109 * @param localMatrix Optional local matrix. The local coordinates are specified by localRect,
halcanary96fcdcc2015-08-27 07:41:13 -0700110 * or if it is nullptr by rect. This matrix applies to the coordinate implied by
joshualitt8fc6c2d2014-12-22 15:27:05 -0800111 * that rectangle before it is input to GrCoordTransforms that read local
112 * coordinates
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000113 */
joshualittd2b23e02015-08-21 10:53:34 -0700114 void drawNonAARect(const GrPipelineBuilder& pipelineBuilder,
115 GrColor color,
116 const SkMatrix& viewMatrix,
117 const SkRect& rect);
joshualittb6b513b2015-08-21 10:25:18 -0700118
joshualittd2b23e02015-08-21 10:53:34 -0700119 void drawNonAARect(const GrPipelineBuilder& pipelineBuilder,
120 GrColor color,
121 const SkMatrix& viewMatrix,
122 const SkRect& rect,
123 const SkMatrix& localMatrix);
joshualittb6b513b2015-08-21 10:25:18 -0700124
joshualittd2b23e02015-08-21 10:53:34 -0700125 void drawNonAARect(const GrPipelineBuilder& pipelineBuilder,
126 GrColor color,
127 const SkMatrix& viewMatrix,
128 const SkRect& rect,
129 const SkRect& localRect);
jvanverth@google.com39768252013-02-14 15:25:44 +0000130
joshualittd2b23e02015-08-21 10:53:34 -0700131 void drawNonAARect(const GrPipelineBuilder& ds,
132 GrColor color,
133 const SkMatrix& viewM,
134 const SkIRect& irect) {
reed@google.com44699382013-10-31 17:28:30 +0000135 SkRect rect = SkRect::Make(irect);
joshualittd2b23e02015-08-21 10:53:34 -0700136 this->drawNonAARect(ds, color, viewM, rect);
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000137 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000138
joshualitt1c735482015-07-13 08:08:25 -0700139 void drawAARect(const GrPipelineBuilder& pipelineBuilder,
robertphillipsea461502015-05-26 11:38:03 -0700140 GrColor color,
141 const SkMatrix& viewMatrix,
142 const SkRect& rect,
143 const SkRect& devRect);
bsalomon@google.com934c5702012-03-20 21:17:58 +0000144
145 /**
egdaniel8dd688b2015-01-22 10:16:09 -0800146 * Clear the passed in render target. Ignores the GrPipelineBuilder and clip. Clears the whole
halcanary96fcdcc2015-08-27 07:41:13 -0700147 * thing if rect is nullptr, otherwise just the rect. If canIgnoreRect is set then the entire
egdaniel8dd688b2015-01-22 10:16:09 -0800148 * render target can be optionally cleared.
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000149 */
joshualitt9853cce2014-11-17 14:22:48 -0800150 void clear(const SkIRect* rect,
151 GrColor color,
152 bool canIgnoreRect,
bsalomon63b21962014-11-05 07:05:34 -0800153 GrRenderTarget* renderTarget);
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000154
bsalomon53469832015-08-18 09:20:09 -0700155 /** Discards the contents render target. */
156 void discard(GrRenderTarget*);
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000157
158 /**
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000159 * Copies a pixel rectangle from one surface to another. This call may finalize
160 * reserved vertex/index data (as though a draw call was made). The src pixels
161 * copied are specified by srcRect. They are copied to a rect of the same
162 * size in dst with top left at dstPoint. If the src rect is clipped by the
163 * src bounds then pixel values in the dst rect corresponding to area clipped
bsalomon6df86402015-06-01 10:41:49 -0700164 * by the src rect are not overwritten. This method is not guaranteed to succeed
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000165 * depending on the type of surface, configs, etc, and the backend-specific
bsalomon6df86402015-06-01 10:41:49 -0700166 * limitations.
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000167 */
bsalomon6df86402015-06-01 10:41:49 -0700168 void copySurface(GrSurface* dst,
bsalomonf90a02b2014-11-26 12:28:00 -0800169 GrSurface* src,
170 const SkIRect& srcRect,
171 const SkIPoint& dstPoint);
bsalomon@google.com116ad842013-04-09 15:38:19 +0000172 /**
robertphillips@google.comff175842012-05-14 19:31:39 +0000173 * Release any resources that are cached but not currently in use. This
174 * is intended to give an application some recourse when resources are low.
175 */
176 virtual void purgeResources() {};
177
bsalomon4061b122015-05-29 10:26:19 -0700178 bool programUnitTest(GrContext* owner, int maxStages);
joshualitt2c93efe2014-11-06 12:57:13 -0800179
bsalomona387a112015-08-11 14:47:42 -0700180 struct PipelineInfo {
181 PipelineInfo(const GrPipelineBuilder* pipelineBuilder, const GrScissorState* scissor,
bsalomonabd30f52015-08-13 13:34:48 -0700182 const GrDrawBatch* batch, const SkRect* devBounds,
bsalomona387a112015-08-11 14:47:42 -0700183 GrDrawTarget* target);
184
185 bool valid() const { return SkToBool(fArgs.fPipelineBuilder); }
186
187 const GrPipeline::CreateArgs& pipelineCreateArgs() const {
188 SkASSERT(this->valid());
189 return fArgs;
190 }
191
bsalomona387a112015-08-11 14:47:42 -0700192 private:
193 GrPipeline::CreateArgs fArgs;
194 };
195
reed@google.comac10a2d2010-12-22 21:39:39 +0000196protected:
bsalomon4061b122015-05-29 10:26:19 -0700197 GrGpu* getGpu() { return fGpu; }
198 const GrGpu* getGpu() const { return fGpu; }
bsalomona73239a2015-04-28 13:35:17 -0700199
joshualitt65171342014-10-09 07:25:36 -0700200 // Makes a copy of the dst if it is necessary for the draw. Returns false if a copy is required
201 // but couldn't be made. Otherwise, returns true. This method needs to be protected because it
202 // needs to be accessed by GLPrograms to setup a correct drawstate
bsalomon50785a32015-02-06 07:02:37 -0800203 bool setupDstReadIfNecessary(const GrPipelineBuilder&,
egdaniele36914c2015-02-13 09:00:33 -0800204 const GrProcOptInfo& colorPOI,
205 const GrProcOptInfo& coveragePOI,
bsalomon6a44c6a2015-05-26 09:49:05 -0700206 GrXferProcessor::DstTexture*,
joshualitt9853cce2014-11-17 14:22:48 -0800207 const SkRect* drawBounds);
joshualitt65171342014-10-09 07:25:36 -0700208
bsalomon512be532015-09-10 10:42:55 -0700209 void recordBatch(GrBatch*);
bsalomon5ea03632015-08-18 10:33:30 -0700210
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000211private:
bsalomon512be532015-09-10 10:42:55 -0700212 SkSTArray<256, SkAutoTUnref<GrBatch>, true> fBatches;
bsalomonf90a02b2014-11-26 12:28:00 -0800213
bsalomon1fcc01c2015-09-09 09:48:06 -0700214 void drawPathBatch(const GrPipelineBuilder& pipelineBuilder, GrDrawPathBatchBase* batch,
215 GrPathRendering::FillType fill);
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000216 // Check to see if this set of draw commands has been sent out
joshualitt9853cce2014-11-17 14:22:48 -0800217 void getPathStencilSettingsForFilltype(GrPathRendering::FillType,
egdaniel8dc7c3a2015-04-16 11:22:42 -0700218 const GrStencilAttachment*,
joshualitt9853cce2014-11-17 14:22:48 -0800219 GrStencilSettings*);
joshualitta7024152014-11-03 14:16:35 -0800220 virtual GrClipMaskManager* clipMaskManager() = 0;
joshualitt1c735482015-07-13 08:08:25 -0700221 virtual bool setupClip(const GrPipelineBuilder&,
joshualitt4421a4c2015-07-13 09:36:41 -0700222 GrPipelineBuilder::AutoRestoreFragmentProcessorState*,
bsalomon6be6f7c2015-02-26 13:05:21 -0800223 GrPipelineBuilder::AutoRestoreStencil*,
224 GrScissorState*,
joshualitt8059eb92014-12-29 15:10:07 -0800225 const SkRect* devBounds) = 0;
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000226
bsalomon4061b122015-05-29 10:26:19 -0700227 GrGpu* fGpu;
228 const GrCaps* fCaps;
229 GrResourceProvider* fResourceProvider;
bsalomon4061b122015-05-29 10:26:19 -0700230 bool fFlushing;
bsalomon512be532015-09-10 10:42:55 -0700231 GrBatchToken fLastFlushToken;
reed@google.comfa35e3d2012-06-26 20:16:17 +0000232
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000233 typedef SkRefCnt INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000234};
235
joshualitt329bf482014-10-29 12:31:28 -0700236/*
237 * This class is JUST for clip mask manager. Everyone else should just use draw target above.
238 */
joshualitt6db519c2014-10-29 08:48:18 -0700239class GrClipTarget : public GrDrawTarget {
240public:
bsalomonedd77a12015-05-29 09:45:57 -0700241 GrClipTarget(GrContext*);
joshualitt329bf482014-10-29 12:31:28 -0700242
243 /* Clip mask manager needs access to the context.
244 * TODO we only need a very small subset of context in the CMM.
245 */
bsalomon4061b122015-05-29 10:26:19 -0700246 GrContext* getContext() { return fContext; }
247 const GrContext* getContext() const { return fContext; }
joshualitt329bf482014-10-29 12:31:28 -0700248
joshualitt6db519c2014-10-29 08:48:18 -0700249 /**
250 * Clip Mask Manager(and no one else) needs to clear private stencil bits.
251 * ClipTarget subclass sets clip bit in the stencil buffer. The subclass
252 * is free to clear the remaining bits to zero if masked clears are more
253 * expensive than clearing all bits.
254 */
bsalomon5ea03632015-08-18 10:33:30 -0700255 void clearStencilClip(const SkIRect&, bool insideClip, GrRenderTarget*);
joshualitt6db519c2014-10-29 08:48:18 -0700256
joshualitt3bdd7dc2014-10-31 08:27:39 -0700257 /**
258 * Release any resources that are cached but not currently in use. This
259 * is intended to give an application some recourse when resources are low.
260 */
bsalomonedd77a12015-05-29 09:45:57 -0700261 void purgeResources() override;
joshualitt3bdd7dc2014-10-31 08:27:39 -0700262
joshualitt329bf482014-10-29 12:31:28 -0700263protected:
bsalomonedd77a12015-05-29 09:45:57 -0700264 SkAutoTDelete<GrClipMaskManager> fClipMaskManager;
bsalomon4061b122015-05-29 10:26:19 -0700265 GrContext* fContext;
joshualitt329bf482014-10-29 12:31:28 -0700266
joshualitt6db519c2014-10-29 08:48:18 -0700267private:
bsalomonedd77a12015-05-29 09:45:57 -0700268 GrClipMaskManager* clipMaskManager() override { return fClipMaskManager; }
joshualitt329bf482014-10-29 12:31:28 -0700269
joshualitt1c735482015-07-13 08:08:25 -0700270 bool setupClip(const GrPipelineBuilder&,
joshualitt4421a4c2015-07-13 09:36:41 -0700271 GrPipelineBuilder::AutoRestoreFragmentProcessorState*,
joshualitt5e6ba212015-07-13 07:35:05 -0700272 GrPipelineBuilder::AutoRestoreStencil*,
joshualitt5e6ba212015-07-13 07:35:05 -0700273 GrScissorState* scissorState,
274 const SkRect* devBounds) override;
joshualitt2c93efe2014-11-06 12:57:13 -0800275
joshualitt6db519c2014-10-29 08:48:18 -0700276 typedef GrDrawTarget INHERITED;
277};
278
reed@google.comac10a2d2010-12-22 21:39:39 +0000279#endif