Home | History | Annotate | Download | only in gl
      1 
      2 /*
      3  * Copyright 2011 Google Inc.
      4  *
      5  * Use of this source code is governed by a BSD-style license that can be
      6  * found in the LICENSE file.
      7  */
      8 
      9 
     10 
     11 #ifndef GrGpuGL_DEFINED
     12 #define GrGpuGL_DEFINED
     13 
     14 #include "../GrDrawState.h"
     15 #include "../GrGpu.h"
     16 #include "GrGLContextInfo.h"
     17 #include "GrGLIndexBuffer.h"
     18 #include "GrGLIRect.h"
     19 #include "GrGLStencilBuffer.h"
     20 #include "GrGLTexture.h"
     21 #include "GrGLVertexBuffer.h"
     22 
     23 class GrGpuGL : public GrGpu {
     24 public:
     25     virtual ~GrGpuGL();
     26 
     27     const GrGLInterface* glInterface() const {
     28         return fGLContextInfo.interface();
     29     }
     30     GrGLBinding glBinding() const { return fGLContextInfo.binding(); }
     31     GrGLVersion glVersion() const { return fGLContextInfo.version(); }
     32     GrGLSLGeneration glslGeneration() const {
     33         return fGLContextInfo.glslGeneration();
     34     }
     35 
     36     // GrGpu overrides
     37     virtual GrPixelConfig preferredReadPixelsConfig(GrPixelConfig config)
     38                                                             const SK_OVERRIDE;
     39     virtual GrPixelConfig preferredWritePixelsConfig(GrPixelConfig config)
     40                                                             const SK_OVERRIDE;
     41     virtual bool readPixelsWillPayForYFlip(
     42                                     GrRenderTarget* renderTarget,
     43                                     int left, int top,
     44                                     int width, int height,
     45                                     GrPixelConfig config,
     46                                     size_t rowBytes) const SK_OVERRIDE;
     47     virtual bool fullReadPixelsIsFasterThanPartial() const SK_OVERRIDE;
     48 
     49     virtual bool canPreserveReadWriteUnpremulPixels() SK_OVERRIDE;
     50 
     51 protected:
     52     GrGpuGL(const GrGLContextInfo& ctxInfo);
     53 
     54     struct {
     55         size_t                  fVertexOffset;
     56         GrVertexLayout          fVertexLayout;
     57         const GrVertexBuffer*   fVertexBuffer;
     58         const GrIndexBuffer*    fIndexBuffer;
     59         bool                    fArrayPtrsDirty;
     60     } fHWGeometryState;
     61 
     62     struct AAState {
     63         bool fMSAAEnabled;
     64         bool fSmoothLineEnabled;
     65     } fHWAAState;
     66 
     67     enum UnpremulConversion {
     68         kUpOnWrite_DownOnRead_UnpremulConversion,
     69         kDownOnWrite_UpOnRead_UnpremulConversion
     70     } fUnpremulConversion;
     71 
     72     GrDrawState fHWDrawState;
     73     bool        fHWStencilClip;
     74 
     75     // As flush of GL state proceeds it updates fHDrawState
     76     // to reflect the new state. Later parts of the state flush
     77     // may perform cascaded changes but cannot refer to fHWDrawState.
     78     // These code paths can refer to the dirty flags. Subclass should
     79     // call resetDirtyFlags after its flush is complete
     80     struct {
     81         bool fRenderTargetChanged : 1;
     82         int  fTextureChangedMask;
     83     } fDirtyFlags;
     84     GR_STATIC_ASSERT(8 * sizeof(int) >= GrDrawState::kNumStages);
     85 
     86     // clears the dirty flags
     87     void resetDirtyFlags();
     88 
     89     // last scissor / viewport scissor state seen by the GL.
     90     struct {
     91         bool        fScissorEnabled;
     92         GrGLIRect   fScissorRect;
     93         GrGLIRect   fViewportRect;
     94     } fHWBounds;
     95 
     96     const GrGLCaps& glCaps() const { return fGLContextInfo.caps(); }
     97 
     98     // GrGpu overrides
     99     virtual void onResetContext() SK_OVERRIDE;
    100 
    101     virtual GrTexture* onCreateTexture(const GrTextureDesc& desc,
    102                                        const void* srcData,
    103                                        size_t rowBytes);
    104     virtual GrVertexBuffer* onCreateVertexBuffer(uint32_t size,
    105                                                  bool dynamic);
    106     virtual GrIndexBuffer* onCreateIndexBuffer(uint32_t size,
    107                                                bool dynamic);
    108     virtual GrTexture* onCreatePlatformTexture(const GrPlatformTextureDesc& desc) SK_OVERRIDE;
    109     virtual GrRenderTarget* onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) SK_OVERRIDE;
    110     virtual bool createStencilBufferForRenderTarget(GrRenderTarget* rt,
    111                                                     int width, int height);
    112     virtual bool attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
    113                                                    GrRenderTarget* rt);
    114 
    115     virtual void onClear(const GrIRect* rect, GrColor color);
    116 
    117     virtual void onForceRenderTargetFlush();
    118 
    119     virtual bool onReadPixels(GrRenderTarget* target,
    120                               int left, int top,
    121                               int width, int height,
    122                               GrPixelConfig,
    123                               void* buffer,
    124                               size_t rowBytes,
    125                               bool invertY) SK_OVERRIDE;
    126 
    127     virtual void onWriteTexturePixels(GrTexture* texture,
    128                                       int left, int top, int width, int height,
    129                                       GrPixelConfig config, const void* buffer,
    130                                       size_t rowBytes) SK_OVERRIDE;
    131 
    132     virtual void onResolveRenderTarget(GrRenderTarget* target) SK_OVERRIDE;
    133 
    134 
    135     virtual void onGpuDrawIndexed(GrPrimitiveType type,
    136                                   uint32_t startVertex,
    137                                   uint32_t startIndex,
    138                                   uint32_t vertexCount,
    139                                   uint32_t indexCount);
    140     virtual void onGpuDrawNonIndexed(GrPrimitiveType type,
    141                                      uint32_t vertexCount,
    142                                      uint32_t numVertices);
    143     virtual void flushScissor(const GrIRect* rect);
    144     virtual void clearStencil();
    145     virtual void clearStencilClip(const GrIRect& rect, bool insideClip);
    146     virtual int getMaxEdges() const;
    147 
    148     // binds texture unit in GL
    149     void setTextureUnit(int unitIdx);
    150 
    151     // binds appropriate vertex and index buffers, also returns any extra
    152     // extra verts or indices to offset by.
    153     void setBuffers(bool indexed,
    154                     int* extraVertexOffset,
    155                     int* extraIndexOffset);
    156 
    157     // flushes state that is common to fixed and programmable GL
    158     // dither
    159     // line smoothing
    160     // texture binding
    161     // sampler state (filtering, tiling)
    162     // FBO binding
    163     // line width
    164     bool flushGLStateCommon(GrPrimitiveType type);
    165 
    166     // Subclasses should call this to flush the blend state.
    167     // The params should be the final coeffecients to apply
    168     // (after any blending optimizations or dual source blending considerations
    169     // have been accounted for).
    170     void flushBlend(GrPrimitiveType type,
    171                     GrBlendCoeff srcCoeff,
    172                     GrBlendCoeff dstCoeff);
    173 
    174     bool hasExtension(const char* ext) const {
    175         return fGLContextInfo.hasExtension(ext);
    176     }
    177 
    178     const GrGLContextInfo& glContextInfo() const { return fGLContextInfo; }
    179 
    180     // adjusts texture matrix to account for orientation
    181     static void AdjustTextureMatrix(const GrGLTexture* texture,
    182                                     GrSamplerState::SampleMode mode,
    183                                     GrMatrix* matrix);
    184 
    185     // subclass may try to take advantage of identity tex matrices.
    186     // This helper determines if matrix will be identity after all
    187     // adjustments are applied.
    188     static bool TextureMatrixIsIdentity(const GrGLTexture* texture,
    189                                         const GrSamplerState& sampler);
    190 
    191     static bool BlendCoeffReferencesConstant(GrBlendCoeff coeff);
    192 
    193 private:
    194     // Inits GrDrawTarget::Caps, sublcass may enable additional caps.
    195     void initCaps();
    196 
    197     void initFSAASupport();
    198 
    199     // determines valid stencil formats
    200     void initStencilFormats();
    201 
    202     // notify callbacks to update state tracking when related
    203     // objects are bound to GL or deleted outside of the class
    204     void notifyVertexBufferBind(const GrGLVertexBuffer* buffer);
    205     void notifyVertexBufferDelete(const GrGLVertexBuffer* buffer);
    206     void notifyIndexBufferBind(const GrGLIndexBuffer* buffer);
    207     void notifyIndexBufferDelete(const GrGLIndexBuffer* buffer);
    208     void notifyTextureDelete(GrGLTexture* texture);
    209     void notifyRenderTargetDelete(GrRenderTarget* renderTarget);
    210 
    211     void setSpareTextureUnit();
    212 
    213     // bound is region that may be modified and therefore has to be resolved.
    214     // NULL means whole target. Can be an empty rect.
    215     void flushRenderTarget(const GrIRect* bound);
    216     void flushStencil();
    217     void flushAAState(GrPrimitiveType type);
    218 
    219     bool configToGLFormats(GrPixelConfig config,
    220                            bool getSizedInternal,
    221                            GrGLenum* internalFormat,
    222                            GrGLenum* externalFormat,
    223                            GrGLenum* externalType);
    224     // helper for onCreateTexture and writeTexturePixels
    225     bool uploadTexData(const GrGLTexture::Desc& desc,
    226                        bool isNewTexture,
    227                        int left, int top, int width, int height,
    228                        GrPixelConfig dataConfig,
    229                        const void* data,
    230                        size_t rowBytes);
    231 
    232     bool createRenderTargetObjects(int width, int height,
    233                                    GrGLuint texID,
    234                                    GrGLRenderTarget::Desc* desc);
    235 
    236     friend class GrGLVertexBuffer;
    237     friend class GrGLIndexBuffer;
    238     friend class GrGLTexture;
    239     friend class GrGLRenderTarget;
    240 
    241     GrGLContextInfo fGLContextInfo;
    242 
    243     // we want to clear stencil buffers when they are created. We want to clear
    244     // the entire buffer even if it is larger than the color attachment. We
    245     // attach it to this fbo with no color attachment to do the initial clear.
    246     GrGLuint fStencilClearFBO;
    247 
    248     bool fHWBlendDisabled;
    249 
    250     int fActiveTextureUnitIdx;
    251 
    252     // we record what stencil format worked last time to hopefully exit early
    253     // from our loop that tries stencil formats and calls check fb status.
    254     int fLastSuccessfulStencilFmtIdx;
    255 
    256     enum CanPreserveUnpremulRoundtrip {
    257         kUnknown_CanPreserveUnpremulRoundtrip,
    258         kNo_CanPreserveUnpremulRoundtrip,
    259         kYes_CanPreserveUnpremulRoundtrip,
    260     } fCanPreserveUnpremulRoundtrip;
    261 
    262     bool fPrintedCaps;
    263 
    264     typedef GrGpu INHERITED;
    265 };
    266 
    267 #endif
    268 
    269