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 "GrTextureProvider.h" 13 #include "SkAutoPixmapStorage.h" 14 #include "SkBitmap.h" 15 #include "SkDraw.h" 16 #include "SkMatrix.h" 17 #include "SkRasterClip.h" 18 #include "SkRegion.h" 19 #include "SkTypes.h" 20 21 class GrClip; 22 class GrPaint; 23 class GrShape; 24 class GrTextureProvider; 25 class GrStyle; 26 class GrTexture; 27 struct GrUserStencilSettings; 28 29 /** 30 * The GrSWMaskHelper helps generate clip masks using the software rendering 31 * path. It is intended to be used as: 32 * 33 * GrSWMaskHelper helper(context); 34 * helper.init(...); 35 * 36 * draw one or more paths/rects specifying the required boolean ops 37 * 38 * toTexture(); // to get it from the internal bitmap to the GPU 39 * 40 * The result of this process will be the final mask (on the GPU) in the 41 * upper left hand corner of the texture. 42 */ 43 class GrSWMaskHelper : SkNoncopyable { 44 public: GrSWMaskHelper(GrTextureProvider * texProvider)45 GrSWMaskHelper(GrTextureProvider* texProvider) : fTexProvider(texProvider) { } 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 drawRect(const SkRect& rect, SkRegion::Op op, bool antiAlias, uint8_t alpha); 55 56 // Draw a single path into the accumuation bitmap using the specified op 57 void drawShape(const GrShape&, SkRegion::Op op, bool antiAlias, uint8_t alpha); 58 59 // Move the mask generation results from the internal bitmap to the gpu. 60 void toTexture(GrTexture* texture); 61 62 // Convert mask generation results to a signed distance field 63 void toSDF(unsigned char* sdf); 64 65 // Reset the internal bitmap clear(uint8_t alpha)66 void clear(uint8_t alpha) { 67 fPixels.erase(SkColorSetARGB(alpha, 0xFF, 0xFF, 0xFF)); 68 } 69 70 71 enum class TextureType { 72 kExactFit, 73 kApproximateFit 74 }; 75 76 // Canonical usage utility that draws a single path and uploads it 77 // to the GPU. The result is returned. 78 static GrTexture* DrawShapeMaskToTexture(GrTextureProvider*, 79 const GrShape&, 80 const SkIRect& resultBounds, 81 bool antiAlias, 82 TextureType, 83 const SkMatrix* matrix); 84 85 // This utility draws a path mask generated by DrawShapeMaskToTexture using a provided paint. 86 // The rectangle is drawn in device space. The 'viewMatrix' will be used to ensure the correct 87 // local coords are provided to any fragment processors in the paint. 88 static void DrawToTargetWithShapeMask(GrTexture* texture, 89 GrDrawContext*, 90 const GrPaint& paint, 91 const GrUserStencilSettings& userStencilSettings, 92 const GrClip&, 93 const SkMatrix& viewMatrix, 94 const SkIPoint& textureOriginInDeviceSpace, 95 const SkIRect& deviceSpaceRectToDraw); 96 97 private: 98 // Helper function to get a scratch texture suitable for capturing the 99 // result (i.e., right size & format) 100 GrTexture* createTexture(TextureType); 101 102 GrTextureProvider* fTexProvider; 103 SkMatrix fMatrix; 104 SkAutoPixmapStorage fPixels; 105 SkDraw fDraw; 106 SkRasterClip fRasterClip; 107 108 typedef SkNoncopyable INHERITED; 109 }; 110 111 #endif // GrSWMaskHelper_DEFINED 112