gfx/skia/trunk/src/gpu/GrInOrderDrawBuffer.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/src/gpu/GrInOrderDrawBuffer.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,254 @@
     1.4 +/*
     1.5 + * Copyright 2011 Google Inc.
     1.6 + *
     1.7 + * Use of this source code is governed by a BSD-style license that can be
     1.8 + * found in the LICENSE file.
     1.9 + */
    1.10 +
    1.11 +#ifndef GrInOrderDrawBuffer_DEFINED
    1.12 +#define GrInOrderDrawBuffer_DEFINED
    1.13 +
    1.14 +#include "GrDrawTarget.h"
    1.15 +#include "GrAllocPool.h"
    1.16 +#include "GrAllocator.h"
    1.17 +#include "GrPath.h"
    1.18 +
    1.19 +#include "SkClipStack.h"
    1.20 +#include "SkTemplates.h"
    1.21 +#include "SkTypes.h"
    1.22 +
    1.23 +class GrGpu;
    1.24 +class GrIndexBufferAllocPool;
    1.25 +class GrVertexBufferAllocPool;
    1.26 +
    1.27 +/**
    1.28 + * GrInOrderDrawBuffer is an implementation of GrDrawTarget that queues up draws for eventual
    1.29 + * playback into a GrGpu. In theory one draw buffer could playback into another. When index or
    1.30 + * vertex buffers are used as geometry sources it is the callers the draw buffer only holds
    1.31 + * references to the buffers. It is the callers responsibility to ensure that the data is still
    1.32 + * valid when the draw buffer is played back into a GrGpu. Similarly, it is the caller's
    1.33 + * responsibility to ensure that all referenced textures, buffers, and render-targets are associated
    1.34 + * in the GrGpu object that the buffer is played back into. The buffer requires VB and IB pools to
    1.35 + * store geometry.
    1.36 + */
    1.37 +class GrInOrderDrawBuffer : public GrDrawTarget {
    1.38 +public:
    1.39 +
    1.40 +    /**
    1.41 +     * Creates a GrInOrderDrawBuffer
    1.42 +     *
    1.43 +     * @param gpu        the gpu object that this draw buffer flushes to.
    1.44 +     * @param vertexPool pool where vertices for queued draws will be saved when
    1.45 +     *                   the vertex source is either reserved or array.
    1.46 +     * @param indexPool  pool where indices for queued draws will be saved when
    1.47 +     *                   the index source is either reserved or array.
    1.48 +     */
    1.49 +    GrInOrderDrawBuffer(GrGpu* gpu,
    1.50 +                        GrVertexBufferAllocPool* vertexPool,
    1.51 +                        GrIndexBufferAllocPool* indexPool);
    1.52 +
    1.53 +    virtual ~GrInOrderDrawBuffer();
    1.54 +
    1.55 +    /**
    1.56 +     * Empties the draw buffer of any queued up draws. This must not be called while inside an
    1.57 +     * unbalanced pushGeometrySource(). The current draw state and clip are preserved.
    1.58 +     */
    1.59 +    void reset();
    1.60 +
    1.61 +    /**
    1.62 +     * This plays the queued up draws to its GrGpu target. It also resets this object (i.e. flushing
    1.63 +     * is destructive). This buffer must not have an active reserved vertex or index source. Any
    1.64 +     * reserved geometry on the target will be finalized because it's geometry source will be pushed
    1.65 +     * before flushing and popped afterwards.
    1.66 +     */
    1.67 +    void flush();
    1.68 +
    1.69 +    // tracking for draws
    1.70 +    virtual DrawToken getCurrentDrawToken() { return DrawToken(this, fDrawID); }
    1.71 +
    1.72 +    // overrides from GrDrawTarget
    1.73 +    virtual bool geometryHints(int* vertexCount,
    1.74 +                               int* indexCount) const SK_OVERRIDE;
    1.75 +    virtual void clear(const SkIRect* rect,
    1.76 +                       GrColor color,
    1.77 +                       bool canIgnoreRect,
    1.78 +                       GrRenderTarget* renderTarget = NULL) SK_OVERRIDE;
    1.79 +
    1.80 +    virtual void initCopySurfaceDstDesc(const GrSurface* src, GrTextureDesc* desc) SK_OVERRIDE;
    1.81 +
    1.82 +protected:
    1.83 +    virtual void clipWillBeSet(const GrClipData* newClip) SK_OVERRIDE;
    1.84 +
    1.85 +private:
    1.86 +    enum Cmd {
    1.87 +        kDraw_Cmd           = 1,
    1.88 +        kStencilPath_Cmd    = 2,
    1.89 +        kSetState_Cmd       = 3,
    1.90 +        kSetClip_Cmd        = 4,
    1.91 +        kClear_Cmd          = 5,
    1.92 +        kCopySurface_Cmd    = 6,
    1.93 +        kDrawPath_Cmd       = 7,
    1.94 +    };
    1.95 +
    1.96 +    class DrawRecord : public DrawInfo {
    1.97 +    public:
    1.98 +        DrawRecord(const DrawInfo& info) : DrawInfo(info) {}
    1.99 +        const GrVertexBuffer*   fVertexBuffer;
   1.100 +        const GrIndexBuffer*    fIndexBuffer;
   1.101 +    };
   1.102 +
   1.103 +    struct StencilPath : public ::SkNoncopyable {
   1.104 +        StencilPath();
   1.105 +
   1.106 +        SkAutoTUnref<const GrPath>  fPath;
   1.107 +        SkPath::FillType            fFill;
   1.108 +    };
   1.109 +
   1.110 +    struct DrawPath : public ::SkNoncopyable {
   1.111 +        DrawPath();
   1.112 +
   1.113 +        SkAutoTUnref<const GrPath>  fPath;
   1.114 +        SkPath::FillType            fFill;
   1.115 +        GrDeviceCoordTexture        fDstCopy;
   1.116 +    };
   1.117 +
   1.118 +    struct Clear : public ::SkNoncopyable {
   1.119 +        Clear() : fRenderTarget(NULL) {}
   1.120 +        ~Clear() { SkSafeUnref(fRenderTarget); }
   1.121 +
   1.122 +        SkIRect         fRect;
   1.123 +        GrColor         fColor;
   1.124 +        bool            fCanIgnoreRect;
   1.125 +        GrRenderTarget* fRenderTarget;
   1.126 +    };
   1.127 +
   1.128 +    struct CopySurface : public ::SkNoncopyable {
   1.129 +        SkAutoTUnref<GrSurface> fDst;
   1.130 +        SkAutoTUnref<GrSurface> fSrc;
   1.131 +        SkIRect                 fSrcRect;
   1.132 +        SkIPoint                fDstPoint;
   1.133 +    };
   1.134 +
   1.135 +    // overrides from GrDrawTarget
   1.136 +    virtual void onDraw(const DrawInfo&) SK_OVERRIDE;
   1.137 +    virtual void onDrawRect(const SkRect& rect,
   1.138 +                            const SkMatrix* matrix,
   1.139 +                            const SkRect* localRect,
   1.140 +                            const SkMatrix* localMatrix) SK_OVERRIDE;
   1.141 +
   1.142 +    virtual void onStencilPath(const GrPath*, SkPath::FillType) SK_OVERRIDE;
   1.143 +    virtual void onDrawPath(const GrPath*, SkPath::FillType,
   1.144 +                            const GrDeviceCoordTexture* dstCopy) SK_OVERRIDE;
   1.145 +
   1.146 +    virtual bool onReserveVertexSpace(size_t vertexSize,
   1.147 +                                      int vertexCount,
   1.148 +                                      void** vertices) SK_OVERRIDE;
   1.149 +    virtual bool onReserveIndexSpace(int indexCount,
   1.150 +                                     void** indices) SK_OVERRIDE;
   1.151 +    virtual void releaseReservedVertexSpace() SK_OVERRIDE;
   1.152 +    virtual void releaseReservedIndexSpace() SK_OVERRIDE;
   1.153 +    virtual void onSetVertexSourceToArray(const void* vertexArray,
   1.154 +                                          int vertexCount) SK_OVERRIDE;
   1.155 +    virtual void onSetIndexSourceToArray(const void* indexArray,
   1.156 +                                         int indexCount) SK_OVERRIDE;
   1.157 +    virtual void releaseVertexArray() SK_OVERRIDE;
   1.158 +    virtual void releaseIndexArray() SK_OVERRIDE;
   1.159 +    virtual void geometrySourceWillPush() SK_OVERRIDE;
   1.160 +    virtual void geometrySourceWillPop(const GeometrySrcState& restoredState) SK_OVERRIDE;
   1.161 +    virtual void willReserveVertexAndIndexSpace(int vertexCount,
   1.162 +                                                int indexCount) SK_OVERRIDE;
   1.163 +    virtual bool onCopySurface(GrSurface* dst,
   1.164 +                               GrSurface* src,
   1.165 +                               const SkIRect& srcRect,
   1.166 +                               const SkIPoint& dstPoint)  SK_OVERRIDE;
   1.167 +    virtual bool onCanCopySurface(GrSurface* dst,
   1.168 +                                  GrSurface* src,
   1.169 +                                  const SkIRect& srcRect,
   1.170 +                                  const SkIPoint& dstPoint) SK_OVERRIDE;
   1.171 +
   1.172 +    bool quickInsideClip(const SkRect& devBounds);
   1.173 +
   1.174 +    virtual void onInstantGpuTraceEvent(const char* marker) SK_OVERRIDE;
   1.175 +    virtual void onPushGpuTraceEvent(const char* marker) SK_OVERRIDE;
   1.176 +    virtual void onPopGpuTraceEvent() SK_OVERRIDE;
   1.177 +
   1.178 +
   1.179 +    // Attempts to concat instances from info onto the previous draw. info must represent an
   1.180 +    // instanced draw. The caller must have already recorded a new draw state and clip if necessary.
   1.181 +    int concatInstancedDraw(const DrawInfo& info);
   1.182 +
   1.183 +    // we lazily record state and clip changes in order to skip clips and states that have no
   1.184 +    // effect.
   1.185 +    bool needsNewState() const;
   1.186 +    bool needsNewClip() const;
   1.187 +
   1.188 +    // these functions record a command
   1.189 +    void            recordState();
   1.190 +    void            recordClip();
   1.191 +    DrawRecord*     recordDraw(const DrawInfo&);
   1.192 +    StencilPath*    recordStencilPath();
   1.193 +    DrawPath*       recordDrawPath();
   1.194 +    Clear*          recordClear();
   1.195 +    CopySurface*    recordCopySurface();
   1.196 +
   1.197 +    // TODO: Use a single allocator for commands and records
   1.198 +    enum {
   1.199 +        kCmdPreallocCnt          = 32,
   1.200 +        kDrawPreallocCnt         = 8,
   1.201 +        kStencilPathPreallocCnt  = 8,
   1.202 +        kDrawPathPreallocCnt     = 8,
   1.203 +        kStatePreallocCnt        = 8,
   1.204 +        kClipPreallocCnt         = 8,
   1.205 +        kClearPreallocCnt        = 4,
   1.206 +        kGeoPoolStatePreAllocCnt = 4,
   1.207 +        kCopySurfacePreallocCnt  = 4,
   1.208 +    };
   1.209 +
   1.210 +    SkSTArray<kCmdPreallocCnt, uint8_t, true>                          fCmds;
   1.211 +    GrSTAllocator<kDrawPreallocCnt, DrawRecord>                        fDraws;
   1.212 +    GrSTAllocator<kStatePreallocCnt, StencilPath>                      fStencilPaths;
   1.213 +    GrSTAllocator<kStatePreallocCnt, DrawPath>                         fDrawPaths;
   1.214 +    GrSTAllocator<kStatePreallocCnt, GrDrawState::DeferredState>       fStates;
   1.215 +    GrSTAllocator<kClearPreallocCnt, Clear>                            fClears;
   1.216 +    GrSTAllocator<kCopySurfacePreallocCnt, CopySurface>                fCopySurfaces;
   1.217 +    GrSTAllocator<kClipPreallocCnt, SkClipStack>                       fClips;
   1.218 +    GrSTAllocator<kClipPreallocCnt, SkIPoint>                          fClipOrigins;
   1.219 +
   1.220 +    GrDrawTarget*                   fDstGpu;
   1.221 +
   1.222 +    bool                            fClipSet;
   1.223 +
   1.224 +    enum ClipProxyState {
   1.225 +        kUnknown_ClipProxyState,
   1.226 +        kValid_ClipProxyState,
   1.227 +        kInvalid_ClipProxyState
   1.228 +    };
   1.229 +    ClipProxyState                  fClipProxyState;
   1.230 +    SkRect                          fClipProxy;
   1.231 +
   1.232 +    GrVertexBufferAllocPool&        fVertexPool;
   1.233 +
   1.234 +    GrIndexBufferAllocPool&         fIndexPool;
   1.235 +
   1.236 +    struct GeometryPoolState {
   1.237 +        const GrVertexBuffer*           fPoolVertexBuffer;
   1.238 +        int                             fPoolStartVertex;
   1.239 +        const GrIndexBuffer*            fPoolIndexBuffer;
   1.240 +        int                             fPoolStartIndex;
   1.241 +        // caller may conservatively over reserve vertices / indices.
   1.242 +        // we release unused space back to allocator if possible
   1.243 +        // can only do this if there isn't an intervening pushGeometrySource()
   1.244 +        size_t                          fUsedPoolVertexBytes;
   1.245 +        size_t                          fUsedPoolIndexBytes;
   1.246 +    };
   1.247 +    SkSTArray<kGeoPoolStatePreAllocCnt, GeometryPoolState> fGeoPoolStateStack;
   1.248 +
   1.249 +    virtual bool       isIssued(uint32_t drawID) { return drawID != fDrawID; }
   1.250 +
   1.251 +    bool                            fFlushing;
   1.252 +    uint32_t                        fDrawID;
   1.253 +
   1.254 +    typedef GrDrawTarget INHERITED;
   1.255 +};
   1.256 +
   1.257 +#endif

mercurial