GrGpu.h revision 9954bc38c498f6b9e9d8c0bcc5cd00d45bfc6e23
1/*
2 * 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.
6 */
7
8#ifndef GrGpu_DEFINED
9#define GrGpu_DEFINED
10
11#include "GrDrawTarget.h"
12#include "GrPathRendering.h"
13#include "GrProgramDesc.h"
14#include "SkPath.h"
15
16class GrContext;
17class GrIndexBufferAllocPool;
18class GrPath;
19class GrPathRange;
20class GrPathRenderer;
21class GrPathRendererChain;
22class GrPipeline;
23class GrPrimitiveProcessor;
24class GrStencilAttachment;
25class GrVertexBufferAllocPool;
26
27class GrGpu : public SkRefCnt {
28public:
29    /**
30     * Create an instance of GrGpu that matches the specified backend. If the requested backend is
31     * not supported (at compile-time or run-time) this returns NULL. The context will not be
32     * fully constructed and should not be used by GrGpu until after this function returns.
33     */
34    static GrGpu* Create(GrBackend, GrBackendContext, GrContext* context);
35
36    ////////////////////////////////////////////////////////////////////////////
37
38    GrGpu(GrContext* context);
39    ~GrGpu() override;
40
41    GrContext* getContext() { return fContext; }
42    const GrContext* getContext() const { return fContext; }
43
44    /**
45     * Gets the capabilities of the draw target.
46     */
47    const GrDrawTargetCaps* caps() const { return fCaps.get(); }
48
49    GrPathRendering* pathRendering() { return fPathRendering.get(); }
50
51    // Called by GrContext when the underlying backend context has been destroyed.
52    // GrGpu should use this to ensure that no backend API calls will be made from
53    // here onward, including in its destructor. Subclasses should call
54    // INHERITED::contextAbandoned() if they override this.
55    virtual void contextAbandoned();
56
57    /**
58     * The GrGpu object normally assumes that no outsider is setting state
59     * within the underlying 3D API's context/device/whatever. This call informs
60     * the GrGpu that the state was modified and it shouldn't make assumptions
61     * about the state.
62     */
63    void markContextDirty(uint32_t state = kAll_GrBackendState) { fResetBits |= state; }
64
65    /**
66     * Creates a texture object. If kRenderTarget_GrSurfaceFlag the texture can
67     * be used as a render target by calling GrTexture::asRenderTarget(). Not all
68     * pixel configs can be used as render targets. Support for configs as textures
69     * or render targets can be checked using GrDrawTargetCaps.
70     *
71     * @param desc        describes the texture to be created.
72     * @param budgeted    does this texture count against the resource cache budget?
73     * @param srcData     texel data to load texture. Begins with full-size
74     *                    palette data for paletted textures. For compressed
75     *                    formats it contains the compressed pixel data. Otherwise,
76     *                    it contains width*height texels. If NULL texture data
77     *                    is uninitialized.
78     * @param rowBytes    the number of bytes between consecutive rows. Zero
79     *                    means rows are tightly packed. This field is ignored
80     *                    for compressed formats.
81     *
82     * @return    The texture object if successful, otherwise NULL.
83     */
84    GrTexture* createTexture(const GrSurfaceDesc& desc, bool budgeted,
85                             const void* srcData, size_t rowBytes);
86
87    /**
88     * Implements GrContext::wrapBackendTexture
89     */
90    GrTexture* wrapBackendTexture(const GrBackendTextureDesc&);
91
92    /**
93     * Implements GrContext::wrapBackendTexture
94     */
95    GrRenderTarget* wrapBackendRenderTarget(const GrBackendRenderTargetDesc&);
96
97    /**
98     * Creates a vertex buffer.
99     *
100     * @param size    size in bytes of the vertex buffer
101     * @param dynamic hints whether the data will be frequently changed
102     *                by either GrVertexBuffer::map() or
103     *                GrVertexBuffer::updateData().
104     *
105     * @return    The vertex buffer if successful, otherwise NULL.
106     */
107    GrVertexBuffer* createVertexBuffer(size_t size, bool dynamic);
108
109    /**
110     * Creates an index buffer.
111     *
112     * @param size    size in bytes of the index buffer
113     * @param dynamic hints whether the data will be frequently changed
114     *                by either GrIndexBuffer::map() or
115     *                GrIndexBuffer::updateData().
116     *
117     * @return The index buffer if successful, otherwise NULL.
118     */
119    GrIndexBuffer* createIndexBuffer(size_t size, bool dynamic);
120
121    /**
122     * Creates an index buffer for instance drawing with a specific pattern.
123     *
124     * @param pattern     the pattern to repeat
125     * @param patternSize size in bytes of the pattern
126     * @param reps        number of times to repeat the pattern
127     * @param vertCount   number of vertices the pattern references
128     * @param dynamic     hints whether the data will be frequently changed
129     *                    by either GrIndexBuffer::map() or
130     *                    GrIndexBuffer::updateData().
131     *
132     * @return The index buffer if successful, otherwise NULL.
133     */
134    GrIndexBuffer* createInstancedIndexBuffer(const uint16_t* pattern,
135                                              int patternSize,
136                                              int reps,
137                                              int vertCount,
138                                              bool isDynamic = false);
139
140    /**
141     * Returns an index buffer that can be used to render quads.
142     * Six indices per quad: 0, 1, 2, 0, 2, 3, etc.
143     * The max number of quads can be queried using GrIndexBuffer::maxQuads().
144     * Draw with kTriangles_GrPrimitiveType
145     * @ return the quad index buffer
146     */
147    const GrIndexBuffer* getQuadIndexBuffer() const;
148
149    /**
150     * Resolves MSAA.
151     */
152    void resolveRenderTarget(GrRenderTarget* target);
153
154    /**
155     * Gets a preferred 8888 config to use for writing/reading pixel data to/from a surface with
156     * config surfaceConfig. The returned config must have at least as many bits per channel as the
157     * readConfig or writeConfig param.
158     */
159    virtual GrPixelConfig preferredReadPixelsConfig(GrPixelConfig readConfig,
160                                                    GrPixelConfig surfaceConfig) const {
161        return readConfig;
162    }
163    virtual GrPixelConfig preferredWritePixelsConfig(GrPixelConfig writeConfig,
164                                                     GrPixelConfig surfaceConfig) const {
165        return writeConfig;
166    }
167
168    /**
169     * Called before uploading writing pixels to a GrTexture when the src pixel config doesn't
170     * match the texture's config.
171     */
172    virtual bool canWriteTexturePixels(const GrTexture*, GrPixelConfig srcConfig) const = 0;
173
174    /**
175     * OpenGL's readPixels returns the result bottom-to-top while the skia
176     * API is top-to-bottom. Thus we have to do a y-axis flip. The obvious
177     * solution is to have the subclass do the flip using either the CPU or GPU.
178     * However, the caller (GrContext) may have transformations to apply and can
179     * simply fold in the y-flip for free. On the other hand, the subclass may
180     * be able to do it for free itself. For example, the subclass may have to
181     * do memcpys to handle rowBytes that aren't tight. It could do the y-flip
182     * concurrently.
183     *
184     * This function returns true if a y-flip is required to put the pixels in
185     * top-to-bottom order and the subclass cannot do it for free.
186     *
187     * See read pixels for the params
188     * @return true if calling readPixels with the same set of params will
189     *              produce bottom-to-top data
190     */
191     virtual bool readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
192                                            int left, int top,
193                                            int width, int height,
194                                            GrPixelConfig config,
195                                            size_t rowBytes) const = 0;
196     /**
197      * This should return true if reading a NxM rectangle of pixels from a
198      * render target is faster if the target has dimensons N and M and the read
199      * rectangle has its top-left at 0,0.
200      */
201     virtual bool fullReadPixelsIsFasterThanPartial() const { return false; };
202
203    /**
204     * Reads a rectangle of pixels from a render target.
205     *
206     * @param renderTarget  the render target to read from. NULL means the
207     *                      current render target.
208     * @param left          left edge of the rectangle to read (inclusive)
209     * @param top           top edge of the rectangle to read (inclusive)
210     * @param width         width of rectangle to read in pixels.
211     * @param height        height of rectangle to read in pixels.
212     * @param config        the pixel config of the destination buffer
213     * @param buffer        memory to read the rectangle into.
214     * @param rowBytes      the number of bytes between consecutive rows. Zero
215     *                      means rows are tightly packed.
216     * @param invertY       buffer should be populated bottom-to-top as opposed
217     *                      to top-to-bottom (skia's usual order)
218     *
219     * @return true if the read succeeded, false if not. The read can fail
220     *              because of a unsupported pixel config or because no render
221     *              target is currently set.
222     */
223    bool readPixels(GrRenderTarget* renderTarget,
224                    int left, int top, int width, int height,
225                    GrPixelConfig config, void* buffer, size_t rowBytes);
226
227    /**
228     * Updates the pixels in a rectangle of a texture.
229     *
230     * @param left          left edge of the rectangle to write (inclusive)
231     * @param top           top edge of the rectangle to write (inclusive)
232     * @param width         width of rectangle to write in pixels.
233     * @param height        height of rectangle to write in pixels.
234     * @param config        the pixel config of the source buffer
235     * @param buffer        memory to read pixels from
236     * @param rowBytes      number of bytes between consecutive rows. Zero
237     *                      means rows are tightly packed.
238     */
239    bool writeTexturePixels(GrTexture* texture,
240                            int left, int top, int width, int height,
241                            GrPixelConfig config, const void* buffer,
242                            size_t rowBytes);
243
244    /**
245     * Clear the passed in render target. Ignores the draw state and clip. Clears the whole thing if
246     * rect is NULL, otherwise just the rect. If canIgnoreRect is set then the entire render target
247     * can be optionally cleared.
248     */
249    void clear(const SkIRect* rect, GrColor color, bool canIgnoreRect,GrRenderTarget* renderTarget);
250
251
252    void clearStencilClip(const SkIRect& rect, bool insideClip, GrRenderTarget* renderTarget);
253
254    /**
255     * Discards the contents render target. NULL indicates that the current render target should
256     * be discarded.
257     **/
258    virtual void discard(GrRenderTarget* = NULL) = 0;
259
260    /**
261     * This is can be called before allocating a texture to be a dst for copySurface. It will
262     * populate the origin, config, and flags fields of the desc such that copySurface can
263     * efficiently succeed. It should only succeed if it can allow copySurface to perform a copy
264     * that would be more effecient than drawing the src to a dst render target.
265     */
266    virtual bool initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) = 0;
267
268    // After the client interacts directly with the 3D context state the GrGpu
269    // must resync its internal state and assumptions about 3D context state.
270    // Each time this occurs the GrGpu bumps a timestamp.
271    // state of the 3D context
272    // At 10 resets / frame and 60fps a 64bit timestamp will overflow in about
273    // a billion years.
274    typedef uint64_t ResetTimestamp;
275
276    // This timestamp is always older than the current timestamp
277    static const ResetTimestamp kExpiredTimestamp = 0;
278    // Returns a timestamp based on the number of times the context was reset.
279    // This timestamp can be used to lazily detect when cached 3D context state
280    // is dirty.
281    ResetTimestamp getResetTimestamp() const { return fResetTimestamp; }
282
283    virtual void buildProgramDesc(GrProgramDesc*,
284                                  const GrPrimitiveProcessor&,
285                                  const GrPipeline&,
286                                  const GrBatchTracker&) const = 0;
287
288    // Called to determine whether a copySurface call would succeed or not. Derived
289    // classes must keep this consistent with their implementation of onCopySurface(). Fallbacks
290    // to issuing a draw from the src to dst take place at the GrDrawTarget level and this function
291    // should only return true if a faster copy path exists. The rect and point are pre-clipped. The
292    // src rect and implied dst rect are guaranteed to be within the src/dst bounds and non-empty.
293    virtual bool canCopySurface(const GrSurface* dst,
294                                const GrSurface* src,
295                                const SkIRect& srcRect,
296                                const SkIPoint& dstPoint) = 0;
297
298    // Called to perform a surface to surface copy. Fallbacks to issuing a draw from the src to dst
299    // take place at the GrDrawTarget level and this function implement faster copy paths. The rect
300    // and point are pre-clipped. The src rect and implied dst rect are guaranteed to be within the
301    // src/dst bounds and non-empty.
302    virtual bool copySurface(GrSurface* dst,
303                             GrSurface* src,
304                             const SkIRect& srcRect,
305                             const SkIPoint& dstPoint) = 0;
306
307    // Called before certain draws in order to guarantee coherent results from dst reads.
308    virtual void xferBarrier(GrXferBarrierType) = 0;
309
310    struct DrawArgs {
311        typedef GrDrawTarget::DrawInfo DrawInfo;
312        DrawArgs(const GrPrimitiveProcessor* primProc,
313                 const GrPipeline* pipeline,
314                 const GrProgramDesc* desc,
315                 const GrBatchTracker* batchTracker)
316            : fPrimitiveProcessor(primProc)
317            , fPipeline(pipeline)
318            , fDesc(desc)
319            , fBatchTracker(batchTracker) {
320            SkASSERT(primProc && pipeline && desc && batchTracker);
321        }
322        const GrPrimitiveProcessor* fPrimitiveProcessor;
323        const GrPipeline* fPipeline;
324        const GrProgramDesc* fDesc;
325        const GrBatchTracker* fBatchTracker;
326    };
327
328    void draw(const DrawArgs&, const GrDrawTarget::DrawInfo&);
329
330    /** None of these params are optional, pointers used just to avoid making copies. */
331    struct StencilPathState {
332        bool fUseHWAA;
333        GrRenderTarget* fRenderTarget;
334        const SkMatrix* fViewMatrix;
335        const GrStencilSettings* fStencil;
336        const GrScissorState* fScissor;
337    };
338
339    void stencilPath(const GrPath*, const StencilPathState&);
340
341    void drawPath(const DrawArgs&, const GrPath*, const GrStencilSettings&);
342    void drawPaths(const DrawArgs&,
343                   const GrPathRange*,
344                   const void* indices,
345                   GrDrawTarget::PathIndexType,
346                   const float transformValues[],
347                   GrDrawTarget::PathTransformType,
348                   int count,
349                   const GrStencilSettings&);
350
351    ///////////////////////////////////////////////////////////////////////////
352    // Debugging and Stats
353
354    class Stats {
355    public:
356#if GR_GPU_STATS
357        Stats() { this->reset(); }
358
359        void reset() {
360            fRenderTargetBinds = 0;
361            fShaderCompilations = 0;
362            fTextureCreates = 0;
363            fTextureUploads = 0;
364            fStencilAttachmentCreates = 0;
365        }
366
367        int renderTargetBinds() const { return fRenderTargetBinds; }
368        void incRenderTargetBinds() { fRenderTargetBinds++; }
369        int shaderCompilations() const { return fShaderCompilations; }
370        void incShaderCompilations() { fShaderCompilations++; }
371        int textureCreates() const { return fTextureCreates; }
372        void incTextureCreates() { fTextureCreates++; }
373        int textureUploads() const { return fTextureUploads; }
374        void incTextureUploads() { fTextureUploads++; }
375        void incStencilAttachmentCreates() { fStencilAttachmentCreates++; }
376        void dump(SkString*);
377
378    private:
379        int fRenderTargetBinds;
380        int fShaderCompilations;
381        int fTextureCreates;
382        int fTextureUploads;
383        int fStencilAttachmentCreates;
384#else
385        void dump(SkString*) {};
386        void incRenderTargetBinds() {}
387        void incShaderCompilations() {}
388        void incTextureCreates() {}
389        void incTextureUploads() {}
390        void incStencilAttachmentCreates() {}
391#endif
392    };
393
394    Stats* stats() { return &fStats; }
395
396    /**
397     * Called at start and end of gpu trace marking
398     * GR_CREATE_GPU_TRACE_MARKER(marker_str, target) will automatically call these at the start
399     * and end of a code block respectively
400     */
401    void addGpuTraceMarker(const GrGpuTraceMarker* marker);
402    void removeGpuTraceMarker(const GrGpuTraceMarker* marker);
403
404    /**
405     * Takes the current active set of markers and stores them for later use. Any current marker
406     * in the active set is removed from the active set and the targets remove function is called.
407     * These functions do not work as a stack so you cannot call save a second time before calling
408     * restore. Also, it is assumed that when restore is called the current active set of markers
409     * is empty. When the stored markers are added back into the active set, the targets add marker
410     * is called.
411     */
412    void saveActiveTraceMarkers();
413    void restoreActiveTraceMarkers();
414
415    // Given a rt, find or create a stencil buffer and attach it
416    bool attachStencilAttachmentToRenderTarget(GrRenderTarget* target);
417
418protected:
419    // Functions used to map clip-respecting stencil tests into normal
420    // stencil funcs supported by GPUs.
421    static GrStencilFunc ConvertStencilFunc(bool stencilInClip,
422                                            GrStencilFunc func);
423    static void ConvertStencilFuncAndMask(GrStencilFunc func,
424                                          bool clipInStencil,
425                                          unsigned int clipBit,
426                                          unsigned int userBits,
427                                          unsigned int* ref,
428                                          unsigned int* mask);
429
430    const GrTraceMarkerSet& getActiveTraceMarkers() const { return fActiveTraceMarkers; }
431
432    Stats                                   fStats;
433    SkAutoTDelete<GrPathRendering>          fPathRendering;
434    // Subclass must initialize this in its constructor.
435    SkAutoTUnref<const GrDrawTargetCaps>    fCaps;
436
437private:
438    // called when the 3D context state is unknown. Subclass should emit any
439    // assumed 3D context state and dirty any state cache.
440    virtual void onResetContext(uint32_t resetBits) = 0;
441
442    // overridden by backend-specific derived class to create objects.
443    // Texture size and sample size will have already been validated in base class before
444    // onCreateTexture/CompressedTexture are called.
445    virtual GrTexture* onCreateTexture(const GrSurfaceDesc& desc,
446                                       GrGpuResource::LifeCycle lifeCycle,
447                                       const void* srcData, size_t rowBytes) = 0;
448    virtual GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc,
449                                                 GrGpuResource::LifeCycle lifeCycle,
450                                                 const void* srcData) = 0;
451    virtual GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&) = 0;
452    virtual GrRenderTarget* onWrapBackendRenderTarget(const GrBackendRenderTargetDesc&) = 0;
453    virtual GrVertexBuffer* onCreateVertexBuffer(size_t size, bool dynamic) = 0;
454    virtual GrIndexBuffer* onCreateIndexBuffer(size_t size, bool dynamic) = 0;
455
456    // overridden by backend-specific derived class to perform the clear.
457    virtual void onClear(GrRenderTarget*, const SkIRect* rect, GrColor color,
458                         bool canIgnoreRect) = 0;
459
460
461    // Overridden by backend specific classes to perform a clear of the stencil clip bits.  This is
462    // ONLY used by the the clip target
463    virtual void onClearStencilClip(GrRenderTarget*, const SkIRect& rect, bool insideClip) = 0;
464
465    // overridden by backend-specific derived class to perform the draw call.
466    virtual void onDraw(const DrawArgs&, const GrDrawTarget::DrawInfo&) = 0;
467    virtual void onStencilPath(const GrPath*, const StencilPathState&) = 0;
468
469    virtual void onDrawPath(const DrawArgs&, const GrPath*, const GrStencilSettings&) = 0;
470    virtual void onDrawPaths(const DrawArgs&,
471                             const GrPathRange*,
472                             const void* indices,
473                             GrDrawTarget::PathIndexType,
474                             const float transformValues[],
475                             GrDrawTarget::PathTransformType,
476                             int count,
477                             const GrStencilSettings&) = 0;
478
479    // overridden by backend-specific derived class to perform the read pixels.
480    virtual bool onReadPixels(GrRenderTarget* target,
481                              int left, int top, int width, int height,
482                              GrPixelConfig,
483                              void* buffer,
484                              size_t rowBytes) = 0;
485
486    // overridden by backend-specific derived class to perform the texture update
487    virtual bool onWriteTexturePixels(GrTexture* texture,
488                                      int left, int top, int width, int height,
489                                      GrPixelConfig config, const void* buffer,
490                                      size_t rowBytes) = 0;
491
492    // overridden by backend-specific derived class to perform the resolve
493    virtual void onResolveRenderTarget(GrRenderTarget* target) = 0;
494
495    // width and height may be larger than rt (if underlying API allows it).
496    // Should attach the SB to the RT. Returns false if compatible sb could
497    // not be created.
498    virtual bool createStencilAttachmentForRenderTarget(GrRenderTarget*, int width, int height) = 0;
499
500    // attaches an existing SB to an existing RT.
501    virtual bool attachStencilAttachmentToRenderTarget(GrStencilAttachment*, GrRenderTarget*) = 0;
502
503    // clears target's entire stencil buffer to 0
504    virtual void clearStencil(GrRenderTarget* target) = 0;
505
506    virtual void didAddGpuTraceMarker() = 0;
507    virtual void didRemoveGpuTraceMarker() = 0;
508
509    void resetContext() {
510        this->onResetContext(fResetBits);
511        fResetBits = 0;
512        ++fResetTimestamp;
513    }
514
515    void handleDirtyContext() {
516        if (fResetBits) {
517            this->resetContext();
518        }
519    }
520
521    ResetTimestamp                                                      fResetTimestamp;
522    uint32_t                                                            fResetBits;
523    // these are mutable so they can be created on-demand
524    mutable GrIndexBuffer*                                              fQuadIndexBuffer;
525    // To keep track that we always have at least as many debug marker adds as removes
526    int                                                                 fGpuTraceMarkerCount;
527    GrTraceMarkerSet                                                    fActiveTraceMarkers;
528    GrTraceMarkerSet                                                    fStoredTraceMarkers;
529    // The context owns us, not vice-versa, so this ptr is not ref'ed by Gpu.
530    GrContext*                                                          fContext;
531
532    typedef SkRefCnt INHERITED;
533};
534
535#endif
536