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