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