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