Home | History | Annotate | Download | only in gpu
      1 /*
      2  * Copyright 2012 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 GrSWMaskHelper_DEFINED
      9 #define GrSWMaskHelper_DEFINED
     10 
     11 #include "GrColor.h"
     12 #include "GrDrawState.h"
     13 #include "SkBitmap.h"
     14 #include "SkDraw.h"
     15 #include "SkMatrix.h"
     16 #include "SkRasterClip.h"
     17 #include "SkRegion.h"
     18 #include "SkTypes.h"
     19 
     20 class GrAutoScratchTexture;
     21 class GrContext;
     22 class GrTexture;
     23 class SkPath;
     24 class SkStrokeRec;
     25 class GrDrawTarget;
     26 
     27 /**
     28  * The GrSWMaskHelper helps generate clip masks using the software rendering
     29  * path. It is intended to be used as:
     30  *
     31  *   GrSWMaskHelper helper(context);
     32  *   helper.init(...);
     33  *
     34  *      draw one or more paths/rects specifying the required boolean ops
     35  *
     36  *   toTexture();   // to get it from the internal bitmap to the GPU
     37  *
     38  * The result of this process will be the final mask (on the GPU) in the
     39  * upper left hand corner of the texture.
     40  */
     41 class GrSWMaskHelper : public SkNoncopyable {
     42 public:
     43     GrSWMaskHelper(GrContext* context)
     44     : fContext(context) {
     45     }
     46 
     47     // set up the internal state in preparation for draws. Since many masks
     48     // may be accumulated in the helper during creation, "resultBounds"
     49     // allows the caller to specify the region of interest - to limit the
     50     // amount of work.
     51     bool init(const SkIRect& resultBounds, const SkMatrix* matrix);
     52 
     53     // Draw a single rect into the accumulation bitmap using the specified op
     54     void draw(const SkRect& rect, SkRegion::Op op,
     55               bool antiAlias, uint8_t alpha);
     56 
     57     // Draw a single path into the accumuation bitmap using the specified op
     58     void draw(const SkPath& path, const SkStrokeRec& stroke, SkRegion::Op op,
     59               bool antiAlias, uint8_t alpha);
     60 
     61     // Helper function to get a scratch texture suitable for capturing the
     62     // result (i.e., right size & format)
     63     bool getTexture(GrAutoScratchTexture* texture);
     64 
     65     // Move the mask generation results from the internal bitmap to the gpu.
     66     void toTexture(GrTexture* texture);
     67 
     68     // Reset the internal bitmap
     69     void clear(uint8_t alpha) {
     70         fBM.eraseColor(SkColorSetARGB(alpha, alpha, alpha, alpha));
     71     }
     72 
     73     // Canonical usage utility that draws a single path and uploads it
     74     // to the GPU. The result is returned in "result".
     75     static GrTexture* DrawPathMaskToTexture(GrContext* context,
     76                                             const SkPath& path,
     77                                             const SkStrokeRec& stroke,
     78                                             const SkIRect& resultBounds,
     79                                             bool antiAlias,
     80                                             SkMatrix* matrix);
     81 
     82     // This utility routine is used to add a path's mask to some other draw.
     83     // The ClipMaskManager uses it to accumulate clip masks while the
     84     // GrSoftwarePathRenderer uses it to fulfill a drawPath call.
     85     // It draws with "texture" as a path mask into "target" using "rect" as
     86     // geometry and the current drawState. The current drawState is altered to
     87     // accommodate the mask.
     88     // Note that this method assumes that the GrPaint::kTotalStages slot in
     89     // the draw state can be used to hold the mask texture stage.
     90     // This method is really only intended to be used with the
     91     // output of DrawPathMaskToTexture.
     92     static void DrawToTargetWithPathMask(GrTexture* texture,
     93                                          GrDrawTarget* target,
     94                                          const SkIRect& rect);
     95 
     96 protected:
     97 private:
     98     GrContext*      fContext;
     99     SkMatrix        fMatrix;
    100     SkBitmap        fBM;
    101     SkDraw          fDraw;
    102     SkRasterClip    fRasterClip;
    103 
    104     typedef SkNoncopyable INHERITED;
    105 };
    106 
    107 #endif // GrSWMaskHelper_DEFINED
    108