Plumb dst color space in many places, rather than "mode"
[platform/upstream/libSkiaSharp.git] / include / gpu / GrRenderTargetContext.h
1 /*
2  * Copyright 2015 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 GrRenderTargetContext_DEFINED
9 #define GrRenderTargetContext_DEFINED
10
11 #include "GrColor.h"
12 #include "GrContext.h"
13 #include "GrPaint.h"
14 #include "GrSurfaceContext.h"
15 #include "SkRefCnt.h"
16 #include "SkSurfaceProps.h"
17 #include "../private/GrInstancedPipelineInfo.h"
18 #include "../private/GrRenderTargetProxy.h"
19
20 class GrClip;
21 class GrDrawingManager;
22 class GrDrawOp;
23 class GrFixedClip;
24 class GrPipelineBuilder;
25 class GrRenderTarget;
26 class GrRenderTargetContextPriv;
27 class GrRenderTargetOpList;
28 class GrStyle;
29 class GrSurface;
30 class GrTextureProxy;
31 struct GrUserStencilSettings;
32 class SkDrawFilter;
33 struct SkIPoint;
34 struct SkIRect;
35 class SkLatticeIter;
36 class SkMatrix;
37 class SkPaint;
38 class SkPath;
39 struct SkPoint;
40 struct SkRect;
41 class SkRegion;
42 class SkRRect;
43 struct SkRSXform;
44 class SkTextBlob;
45
46 /**
47  * A helper object to orchestrate commands (draws, etc...) for GrSurfaces that are GrRenderTargets.
48  */
49 class SK_API GrRenderTargetContext : public GrSurfaceContext {
50 public:
51     ~GrRenderTargetContext() override;
52
53     bool copySurface(GrSurface* src, const SkIRect& srcRect, const SkIPoint& dstPoint) override;
54
55     // TODO: it is odd that we need both the SkPaint in the following 3 methods.
56     // We should extract the text parameters from SkPaint and pass them separately
57     // akin to GrStyle (GrTextInfo?)
58     virtual void drawText(const GrClip&,  const GrPaint&, const SkPaint&,
59                           const SkMatrix& viewMatrix, const char text[], size_t byteLength,
60                           SkScalar x, SkScalar y, const SkIRect& clipBounds);
61     virtual void drawPosText(const GrClip&, const GrPaint&, const SkPaint&,
62                              const SkMatrix& viewMatrix, const char text[], size_t byteLength,
63                              const SkScalar pos[], int scalarsPerPosition,
64                              const SkPoint& offset, const SkIRect& clipBounds);
65     virtual void drawTextBlob(const GrClip&, const SkPaint&,
66                               const SkMatrix& viewMatrix, const SkTextBlob*,
67                               SkScalar x, SkScalar y,
68                               SkDrawFilter*, const SkIRect& clipBounds);
69
70     /**
71      * Provides a perfomance hint that the render target's contents are allowed
72      * to become undefined.
73      */
74     void discard();
75
76     /**
77      * Clear the entire or rect of the render target, ignoring any clips.
78      * @param rect  the rect to clear or the whole thing if rect is NULL.
79      * @param color the color to clear to.
80      * @param canIgnoreRect allows partial clears to be converted to whole
81      *                      clears on platforms for which that is cheap
82      */
83     void clear(const SkIRect* rect, GrColor color, bool canIgnoreRect);
84
85     /**
86      *  Draw everywhere (respecting the clip) with the paint.
87      */
88     void drawPaint(const GrClip&, const GrPaint&, const SkMatrix& viewMatrix);
89
90     /**
91      *  Draw the rect using a paint.
92      *  @param paint        describes how to color pixels.
93      *  @param viewMatrix   transformation matrix
94      *  @param style        The style to apply. Null means fill. Currently path effects are not
95      *                      allowed.
96      *  The rects coords are used to access the paint (through texture matrix)
97      */
98     void drawRect(const GrClip&,
99                   const GrPaint& paint,
100                   const SkMatrix& viewMatrix,
101                   const SkRect&,
102                   const GrStyle* style  = nullptr);
103
104     /**
105      * Maps a rectangle of shader coordinates to a rectangle and fills that rectangle.
106      *
107      * @param paint         describes how to color pixels.
108      * @param viewMatrix    transformation matrix which applies to rectToDraw
109      * @param rectToDraw    the rectangle to draw
110      * @param localRect     the rectangle of shader coordinates applied to rectToDraw
111      */
112     void fillRectToRect(const GrClip&,
113                         const GrPaint& paint,
114                         const SkMatrix& viewMatrix,
115                         const SkRect& rectToDraw,
116                         const SkRect& localRect);
117
118     /**
119      * Fills a rect with a paint and a localMatrix.
120      */
121     void fillRectWithLocalMatrix(const GrClip& clip,
122                                  const GrPaint& paint,
123                                  const SkMatrix& viewMatrix,
124                                  const SkRect& rect,
125                                  const SkMatrix& localMatrix);
126
127     /**
128      *  Draw a roundrect using a paint.
129      *
130      *  @param paint        describes how to color pixels.
131      *  @param viewMatrix   transformation matrix
132      *  @param rrect        the roundrect to draw
133      *  @param style        style to apply to the rrect. Currently path effects are not allowed.
134      */
135     void drawRRect(const GrClip&,
136                    const GrPaint&,
137                    const SkMatrix& viewMatrix,
138                    const SkRRect& rrect,
139                    const GrStyle& style);
140
141     /**
142      *  Draw a roundrect using a paint and a shadow shader. This is separate from drawRRect
143      *  because it uses different underlying geometry and GeometryProcessor
144      *
145      *  @param paint        describes how to color pixels.
146      *  @param viewMatrix   transformation matrix
147      *  @param rrect        the roundrect to draw
148      *  @param blurRadius   amount of shadow blur to apply (in device space)
149      *  @param style        style to apply to the rrect. Currently path effects are not allowed.
150      */
151     void drawShadowRRect(const GrClip&,
152                          const GrPaint&,
153                          const SkMatrix& viewMatrix,
154                          const SkRRect& rrect,
155                          SkScalar blurRadius,
156                          const GrStyle& style);
157
158     /**
159      *  Shortcut for drawing an SkPath consisting of nested rrects using a paint.
160      *  Does not support stroking. The result is undefined if outer does not contain
161      *  inner.
162      *
163      *  @param paint        describes how to color pixels.
164      *  @param viewMatrix   transformation matrix
165      *  @param outer        the outer roundrect
166      *  @param inner        the inner roundrect
167      */
168     void drawDRRect(const GrClip&,
169                     const GrPaint&,
170                     const SkMatrix& viewMatrix,
171                     const SkRRect& outer,
172                     const SkRRect& inner);
173
174     /**
175      * Draws a path.
176      *
177      * @param paint         describes how to color pixels.
178      * @param viewMatrix    transformation matrix
179      * @param path          the path to draw
180      * @param style         style to apply to the path.
181      */
182     void drawPath(const GrClip&,
183                   const GrPaint&,
184                   const SkMatrix& viewMatrix,
185                   const SkPath&,
186                   const GrStyle& style);
187
188     /**
189      * Draws vertices with a paint.
190      *
191      * @param   paint           describes how to color pixels.
192      * @param   viewMatrix      transformation matrix
193      * @param   primitiveType   primitives type to draw.
194      * @param   vertexCount     number of vertices.
195      * @param   positions       array of vertex positions, required.
196      * @param   texCoords       optional array of texture coordinates used
197      *                          to access the paint.
198      * @param   colors          optional array of per-vertex colors, supercedes
199      *                          the paint's color field.
200      * @param   indices         optional array of indices. If NULL vertices
201      *                          are drawn non-indexed.
202      * @param   indexCount      if indices is non-null then this is the
203      *                          number of indices.
204      */
205     void drawVertices(const GrClip&,
206                       const GrPaint& paint,
207                       const SkMatrix& viewMatrix,
208                       GrPrimitiveType primitiveType,
209                       int vertexCount,
210                       const SkPoint positions[],
211                       const SkPoint texs[],
212                       const GrColor colors[],
213                       const uint16_t indices[],
214                       int indexCount);
215
216     /**
217      * Draws textured sprites from an atlas with a paint.
218      *
219      * @param   paint           describes how to color pixels.
220      * @param   viewMatrix      transformation matrix
221      * @param   spriteCount     number of sprites.
222      * @param   xform           array of compressed transformation data, required.
223      * @param   texRect         array of texture rectangles used to access the paint.
224      * @param   colors          optional array of per-sprite colors, supercedes
225      *                          the paint's color field.
226      */
227     void drawAtlas(const GrClip&,
228                    const GrPaint& paint,
229                    const SkMatrix& viewMatrix,
230                    int spriteCount,
231                    const SkRSXform xform[],
232                    const SkRect texRect[],
233                    const SkColor colors[]);
234
235     /**
236      * Draws a region.
237      *
238      * @param paint         describes how to color pixels
239      * @param viewMatrix    transformation matrix
240      * @param region        the region to be drawn
241      * @param style         style to apply to the region
242      */
243     void drawRegion(const GrClip&,
244                     const GrPaint& paint,
245                     const SkMatrix& viewMatrix,
246                     const SkRegion& region,
247                     const GrStyle& style);
248
249     /**
250      * Draws an oval.
251      *
252      * @param paint         describes how to color pixels.
253      * @param viewMatrix    transformation matrix
254      * @param oval          the bounding rect of the oval.
255      * @param style         style to apply to the oval. Currently path effects are not allowed.
256      */
257     void drawOval(const GrClip&,
258                   const GrPaint& paint,
259                   const SkMatrix& viewMatrix,
260                   const SkRect& oval,
261                   const GrStyle& style);
262    /**
263     * Draws a partial arc of an oval.
264     *
265     * @param paint         describes how to color pixels.
266     * @param viewMatrix    transformation matrix.
267     * @param oval          the bounding rect of the oval.
268     * @param startAngle    starting angle in degrees.
269     * @param sweepAngle    angle to sweep in degrees. Must be in (-360, 360)
270     * @param useCenter     true means that the implied path begins at the oval center, connects as a
271     *                      line to the point indicated by the start contains the arc indicated by
272     *                      the sweep angle. If false the line beginning at the center point is
273     *                      omitted.
274     * @param style         style to apply to the oval.
275     */
276     void drawArc(const GrClip&,
277                  const GrPaint& paint,
278                  const SkMatrix& viewMatrix,
279                  const SkRect& oval,
280                  SkScalar startAngle,
281                  SkScalar sweepAngle,
282                  bool useCenter,
283                  const GrStyle& style);
284
285     /**
286      * Draw the image as a set of rects, specified by |iter|.
287      */
288     void drawImageLattice(const GrClip&,
289                           const GrPaint& paint,
290                           const SkMatrix& viewMatrix,
291                           int imageWidth,
292                           int imageHeight,
293                           std::unique_ptr<SkLatticeIter> iter,
294                           const SkRect& dst);
295
296     /**
297      * After this returns any pending surface IO will be issued to the backend 3D API and
298      * if the surface has MSAA it will be resolved.
299      */
300     void prepareForExternalIO();
301
302     /**
303      * Reads a rectangle of pixels from the render target context.
304      * @param dstInfo       image info for the destination
305      * @param dstBuffer     destination pixels for the read
306      * @param dstRowBytes   bytes in a row of 'dstBuffer'
307      * @param x             x offset w/in the render target context from which to read
308      * @param y             y offset w/in the render target context from which to read
309      *
310      * @return true if the read succeeded, false if not. The read can fail because of an
311      *              unsupported pixel config.
312      */
313     bool readPixels(const SkImageInfo& dstInfo, void* dstBuffer, size_t dstRowBytes, int x, int y);
314
315     /**
316      * Writes a rectangle of pixels [srcInfo, srcBuffer, srcRowbytes] into the 
317      * renderTargetContext at the specified position.
318      * @param srcInfo       image info for the source pixels
319      * @param srcBuffer     source for the write
320      * @param srcRowBytes   bytes in a row of 'srcBuffer'
321      * @param x             x offset w/in the render target context at which to write
322      * @param y             y offset w/in the render target context at which to write
323      *
324      * @return true if the write succeeded, false if not. The write can fail because of an
325      *              unsupported pixel config.
326      */
327     bool writePixels(const SkImageInfo& srcInfo, const void* srcBuffer, size_t srcRowBytes,
328                      int x, int y);
329
330     bool isStencilBufferMultisampled() const {
331         return fRenderTargetProxy->isStencilBufferMultisampled();
332     }
333     bool isUnifiedMultisampled() const { return fRenderTargetProxy->isUnifiedMultisampled(); }
334     bool hasMixedSamples() const { return fRenderTargetProxy->isMixedSampled(); }
335
336     bool mustUseHWAA(const GrPaint& paint) const {
337         return paint.isAntiAlias() && fRenderTargetProxy->isUnifiedMultisampled();
338     }
339
340     const GrCaps* caps() const { return fContext->caps(); }
341     const GrSurfaceDesc& desc() const { return fRenderTargetProxy->desc(); }
342     int width() const { return fRenderTargetProxy->width(); }
343     int height() const { return fRenderTargetProxy->height(); }
344     GrPixelConfig config() const { return fRenderTargetProxy->config(); }
345     int numColorSamples() const { return fRenderTargetProxy->numColorSamples(); }
346     bool isGammaCorrect() const { return SkToBool(fColorSpace.get()); }
347     const SkSurfaceProps& surfaceProps() const { return fSurfaceProps; }
348     SkColorSpace* getColorSpace() const { return fColorSpace.get(); }
349     GrColorSpaceXform* getColorXformFromSRGB() const { return fColorXformFromSRGB.get(); }
350     GrSurfaceOrigin origin() const { return fRenderTargetProxy->origin(); }
351
352     bool wasAbandoned() const;
353
354     GrRenderTarget* instantiate();
355
356     GrRenderTarget* accessRenderTarget() {
357         // TODO: usage of this entry point needs to be reduced and potentially eliminated
358         // since it ends the deferral of the GrRenderTarget's allocation
359         return fRenderTargetProxy->instantiate(fContext->textureProvider());
360     }
361
362     GrTextureProxy* asDeferredTexture();
363
364     sk_sp<GrTexture> asTexture() {
365         if (!this->accessRenderTarget()) {
366             return nullptr;
367         }
368
369         // TODO: usage of this entry point needs to be reduced and potentially eliminated
370         // since it ends the deferral of the GrRenderTarget's allocation
371         // It's usage should migrate to asDeferredTexture
372         return sk_ref_sp(this->accessRenderTarget()->asTexture());
373     }
374
375     // Provides access to functions that aren't part of the public API.
376     GrRenderTargetContextPriv priv();
377     const GrRenderTargetContextPriv priv() const;
378
379     bool isWrapped_ForTesting() const;
380
381 protected:
382     GrRenderTargetContext(GrContext*, GrDrawingManager*, sk_sp<GrRenderTargetProxy>,
383                           sk_sp<SkColorSpace>, const SkSurfaceProps* surfaceProps, GrAuditTrail*,
384                           GrSingleOwner*);
385
386     GrDrawingManager* drawingManager() { return fDrawingManager; }
387
388     SkDEBUGCODE(void validate() const;)
389
390 private:
391     friend class GrAtlasTextBlob; // for access to addDrawOp
392     friend class GrStencilAndCoverTextContext; // for access to addDrawOp
393
394     friend class GrDrawingManager; // for ctor
395     friend class GrRenderTargetContextPriv;
396     friend class GrTestTarget;  // for access to getOpList
397     friend class GrSWMaskHelper;                 // for access to addDrawOp
398
399     // All the path renderers currently make their own ops
400     friend class GrSoftwarePathRenderer;         // for access to addDrawOp
401     friend class GrAAConvexPathRenderer;         // for access to addDrawOp
402     friend class GrDashLinePathRenderer;         // for access to addDrawOp
403     friend class GrAAHairLinePathRenderer;       // for access to addDrawOp
404     friend class GrAALinearizingConvexPathRenderer;  // for access to addDrawOp
405     friend class GrAADistanceFieldPathRenderer;  // for access to addDrawOp
406     friend class GrDefaultPathRenderer;          // for access to addDrawOp
407     friend class GrPLSPathRenderer;              // for access to addDrawOp
408     friend class GrMSAAPathRenderer;             // for access to addDrawOp
409     friend class GrStencilAndCoverPathRenderer;  // for access to addDrawOp
410     friend class GrTessellatingPathRenderer;     // for access to addDrawOp
411
412     void internalClear(const GrFixedClip&, const GrColor, bool canIgnoreClip);
413
414     bool drawFilledDRRect(const GrClip& clip,
415                           const GrPaint& paint,
416                           const SkMatrix& viewMatrix,
417                           const SkRRect& origOuter,
418                           const SkRRect& origInner);
419
420     bool drawFilledRect(const GrClip& clip,
421                         const GrPaint& paint,
422                         const SkMatrix& viewMatrix,
423                         const SkRect& rect,
424                         const GrUserStencilSettings* ss);
425
426     void drawNonAAFilledRect(const GrClip&,
427                              const GrPaint&,
428                              const SkMatrix& viewMatrix,
429                              const SkRect& rect,
430                              const SkRect* localRect,
431                              const SkMatrix* localMatrix,
432                              const GrUserStencilSettings* ss,
433                              bool useHWAA);
434
435     void internalDrawPath(const GrClip& clip,
436                           const GrPaint& paint,
437                           const SkMatrix& viewMatrix,
438                           const SkPath& path,
439                           const GrStyle& style);
440
441     // This entry point allows the GrTextContext-derived classes to add their ops to the GrOpList.
442     void addDrawOp(const GrPipelineBuilder&, const GrClip&, GrDrawOp*);
443
444     GrRenderTargetOpList* getOpList();
445
446     GrDrawingManager*                 fDrawingManager;
447     sk_sp<GrRenderTargetProxy>        fRenderTargetProxy;
448
449     // In MDB-mode the GrOpList can be closed by some other renderTargetContext that has picked
450     // it up. For this reason, the GrOpList should only ever be accessed via 'getOpList'.
451     GrRenderTargetOpList*             fOpList;
452     GrInstancedPipelineInfo           fInstancedPipelineInfo;
453
454     sk_sp<SkColorSpace>               fColorSpace;
455     sk_sp<GrColorSpaceXform>          fColorXformFromSRGB;
456     SkSurfaceProps                    fSurfaceProps;
457 };
458
459 #endif