Home | History | Annotate | Download | only in gpu
      1 /*
      2 * Copyright 2016 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 GrGpuCommandBuffer_DEFINED
      9 #define GrGpuCommandBuffer_DEFINED
     10 
     11 #include "GrColor.h"
     12 #include "GrPipeline.h"
     13 #include "ops/GrDrawOp.h"
     14 
     15 class GrOpFlushState;
     16 class GrFixedClip;
     17 class GrGpu;
     18 class GrMesh;
     19 class GrPipeline;
     20 class GrPrimitiveProcessor;
     21 class GrRenderTarget;
     22 struct SkIRect;
     23 struct SkRect;
     24 
     25 class GrGpuRTCommandBuffer;
     26 
     27 class GrGpuCommandBuffer {
     28 public:
     29     virtual ~GrGpuCommandBuffer() {}
     30 
     31     // Copy src into current surface owned by either a GrGpuTextureCommandBuffer or
     32     // GrGpuRenderTargetCommandBuffer.
     33     virtual void copy(GrSurface* src, GrSurfaceOrigin srcOrigin,
     34                       const SkIRect& srcRect, const SkIPoint& dstPoint) = 0;
     35 
     36     virtual void insertEventMarker(const char*) = 0;
     37 
     38     virtual GrGpuRTCommandBuffer* asRTCommandBuffer() { return nullptr; }
     39 
     40     // Sends the command buffer off to the GPU object to execute the commands built up in the
     41     // buffer. The gpu object is allowed to defer execution of the commands until it is flushed.
     42     virtual void submit() = 0;
     43 
     44 protected:
     45     GrGpuCommandBuffer(GrSurfaceOrigin origin) : fOrigin(origin) {}
     46 
     47     GrSurfaceOrigin fOrigin;
     48 };
     49 
     50 class GrGpuTextureCommandBuffer : public GrGpuCommandBuffer{
     51 public:
     52     virtual ~GrGpuTextureCommandBuffer() {}
     53 
     54     virtual void submit() = 0;
     55 
     56 protected:
     57     GrGpuTextureCommandBuffer(GrTexture* texture, GrSurfaceOrigin origin)
     58             : INHERITED(origin)
     59             , fTexture(texture) {}
     60 
     61     GrTexture* fTexture;
     62 
     63 private:
     64     typedef GrGpuCommandBuffer INHERITED;
     65 };
     66 
     67 /**
     68  * The GrGpuRenderTargetCommandBuffer is a series of commands (draws, clears, and discards), which
     69  * all target the same render target. It is possible that these commands execute immediately (GL),
     70  * or get buffered up for later execution (Vulkan). GrOps will execute their draw commands into a
     71  * GrGpuCommandBuffer.
     72  */
     73 class GrGpuRTCommandBuffer : public GrGpuCommandBuffer {
     74 public:
     75     struct LoadAndStoreInfo {
     76         GrLoadOp  fLoadOp;
     77         GrStoreOp fStoreOp;
     78         GrColor   fClearColor;
     79     };
     80 
     81     // Load-time clears of the stencil buffer are always to 0 so we don't store
     82     // an 'fStencilClearValue'
     83     struct StencilLoadAndStoreInfo {
     84         GrLoadOp  fLoadOp;
     85         GrStoreOp fStoreOp;
     86     };
     87 
     88     virtual ~GrGpuRTCommandBuffer() {}
     89 
     90     GrGpuRTCommandBuffer* asRTCommandBuffer() { return this; }
     91 
     92     virtual void begin() = 0;
     93     // Signals the end of recording to the command buffer and that it can now be submitted.
     94     virtual void end() = 0;
     95 
     96     // We pass in an array of meshCount GrMesh to the draw. The backend should loop over each
     97     // GrMesh object and emit a draw for it. Each draw will use the same GrPipeline and
     98     // GrPrimitiveProcessor. This may fail if the draw would exceed any resource limits (e.g.
     99     // number of vertex attributes is too large).
    100     bool draw(const GrPipeline&,
    101               const GrPrimitiveProcessor&,
    102               const GrMesh[],
    103               const GrPipeline::DynamicState[],
    104               int meshCount,
    105               const SkRect& bounds);
    106 
    107     // Performs an upload of vertex data in the middle of a set of a set of draws
    108     virtual void inlineUpload(GrOpFlushState*, GrDeferredTextureUploadFn&) = 0;
    109 
    110     /**
    111      * Clear the owned render target. Ignores the draw state and clip.
    112      */
    113     void clear(const GrFixedClip&, GrColor);
    114 
    115     void clearStencilClip(const GrFixedClip&, bool insideStencilMask);
    116 
    117     /**
    118      * Discards the contents render target.
    119      */
    120     // TODO: This should be removed in the future to favor using the load and store ops for discard
    121     virtual void discard() = 0;
    122 
    123 protected:
    124     GrGpuRTCommandBuffer(GrRenderTarget* rt, GrSurfaceOrigin origin)
    125             : INHERITED(origin)
    126             , fRenderTarget(rt) {
    127     }
    128 
    129     GrRenderTarget* fRenderTarget;
    130 
    131 private:
    132     virtual GrGpu* gpu() = 0;
    133 
    134     // overridden by backend-specific derived class to perform the draw call.
    135     virtual void onDraw(const GrPipeline&,
    136                         const GrPrimitiveProcessor&,
    137                         const GrMesh[],
    138                         const GrPipeline::DynamicState[],
    139                         int meshCount,
    140                         const SkRect& bounds) = 0;
    141 
    142     // overridden by backend-specific derived class to perform the clear.
    143     virtual void onClear(const GrFixedClip&, GrColor) = 0;
    144 
    145     virtual void onClearStencilClip(const GrFixedClip&, bool insideStencilMask) = 0;
    146 
    147     typedef GrGpuCommandBuffer INHERITED;
    148 };
    149 
    150 #endif
    151