Home | History | Annotate | Download | only in gl
      1 /*
      2  * Copyright 2014 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 GrGLPathRendering_DEFINED
      9 #define GrGLPathRendering_DEFINED
     10 
     11 #include "SkRefCnt.h"
     12 #include "GrGpu.h"
     13 #include "GrPathRendering.h"
     14 #include "GrStencilSettings.h"
     15 #include "gl/GrGLTypes.h"
     16 #include "glsl/GrGLSLUtil.h"
     17 
     18 class GrGLNameAllocator;
     19 class GrGLGpu;
     20 class GrStyle;
     21 
     22 /**
     23  * This class wraps the NV_path_rendering extension and manages its various
     24  * API versions. If a method is not present in the GrGLInterface of the GrGLGpu
     25  * (because the driver version is old), it tries to provide a backup
     26  * implementation. But if a backup implementation is not practical, it marks the
     27  * method as not supported.
     28  */
     29 class GrGLPathRendering : public GrPathRendering {
     30 public:
     31     /**
     32      * Create a new GrGLPathRendering object from a given GrGLGpu.
     33      */
     34     GrGLPathRendering(GrGLGpu* gpu);
     35     ~GrGLPathRendering() override;
     36 
     37     // GrPathRendering implementations.
     38     sk_sp<GrPath> createPath(const SkPath&, const GrStyle&) override;
     39     virtual sk_sp<GrPathRange> createPathRange(GrPathRange::PathGenerator*,
     40                                                const GrStyle&) override;
     41 
     42     /* Called when the 3D context state is unknown. */
     43     void resetContext();
     44 
     45     /**
     46      * Called when the context either is about to be lost or is lost. DisconnectType indicates
     47      * whether GPU resources should be cleaned up or abandoned when this is called.
     48      */
     49     void disconnect(GrGpu::DisconnectType);
     50 
     51     bool shouldBindFragmentInputs() const {
     52         return fCaps.bindFragmentInputSupport;
     53     }
     54 
     55     // Functions for "separable shader" texturing support.
     56     void setProgramPathFragmentInputTransform(GrGLuint program, GrGLint location,
     57                                               GrGLenum genMode, GrGLint components,
     58                                               const SkMatrix&);
     59 
     60     /* Sets the projection matrix for path rendering */
     61     void setProjectionMatrix(const SkMatrix& matrix,
     62                              const SkISize& renderTargetSize,
     63                              GrSurfaceOrigin renderTargetOrigin);
     64 
     65     GrGLuint genPaths(GrGLsizei range);
     66     GrGLvoid deletePaths(GrGLuint path, GrGLsizei range);
     67 
     68 protected:
     69     void onStencilPath(const StencilPathArgs&, const GrPath*) override;
     70     void onDrawPath(const GrPipeline&,
     71                     const GrPrimitiveProcessor&,
     72                     const GrStencilSettings&,
     73                     const GrPath*) override;
     74     void onDrawPaths(const GrPipeline&,
     75                      const GrPrimitiveProcessor&,
     76                      const GrStencilSettings&,
     77                      const GrPathRange*,
     78                      const void* indices,
     79                      PathIndexType,
     80                      const float transformValues[],
     81                      PathTransformType,
     82                      int count) override;
     83 private:
     84     /**
     85      * Mark certain functionality as not supported.
     86      */
     87     struct Caps {
     88         bool bindFragmentInputSupport : 1;
     89     };
     90 
     91     void flushPathStencilSettings(const GrStencilSettings&);
     92 
     93     struct MatrixState {
     94         SkMatrix        fViewMatrix;
     95         SkISize         fRenderTargetSize;
     96         GrSurfaceOrigin fRenderTargetOrigin;
     97 
     98         MatrixState() { this->invalidate(); }
     99         void invalidate() {
    100             fViewMatrix = SkMatrix::InvalidMatrix();
    101             fRenderTargetSize.fWidth = -1;
    102             fRenderTargetSize.fHeight = -1;
    103             fRenderTargetOrigin = (GrSurfaceOrigin) -1;
    104         }
    105 
    106         /**
    107          * Gets a matrix that goes from local coordinates to GL normalized device coords.
    108          */
    109         template<int Size> void getRTAdjustedGLMatrix(float* destMatrix) {
    110             SkMatrix combined;
    111             if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) {
    112                 combined.setAll(SkIntToScalar(2) / fRenderTargetSize.fWidth, 0, -SK_Scalar1,
    113                                 0, -SkIntToScalar(2) / fRenderTargetSize.fHeight, SK_Scalar1,
    114                                 0, 0, 1);
    115             } else {
    116                 combined.setAll(SkIntToScalar(2) / fRenderTargetSize.fWidth, 0, -SK_Scalar1,
    117                                 0, SkIntToScalar(2) / fRenderTargetSize.fHeight, -SK_Scalar1,
    118                                 0, 0, 1);
    119             }
    120             combined.preConcat(fViewMatrix);
    121             GrGLSLGetMatrix<Size>(destMatrix, combined);
    122         }
    123     };
    124     GrGLGpu* gpu();
    125 
    126     GrGLuint fFirstPreallocatedPathID;
    127     GrGLsizei fPreallocatedPathCount;
    128     MatrixState fHWProjectionMatrixState;
    129     GrStencilSettings fHWPathStencilSettings;
    130     Caps fCaps;
    131 };
    132 
    133 #endif
    134