michael@0: /* michael@0: * Copyright 2012 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #ifndef GrSWMaskHelper_DEFINED michael@0: #define GrSWMaskHelper_DEFINED michael@0: michael@0: #include "GrColor.h" michael@0: #include "GrDrawState.h" michael@0: #include "SkBitmap.h" michael@0: #include "SkDraw.h" michael@0: #include "SkMatrix.h" michael@0: #include "SkRasterClip.h" michael@0: #include "SkRegion.h" michael@0: #include "SkTypes.h" michael@0: michael@0: class GrAutoScratchTexture; michael@0: class GrContext; michael@0: class GrTexture; michael@0: class SkPath; michael@0: class SkStrokeRec; michael@0: class GrDrawTarget; michael@0: michael@0: /** michael@0: * The GrSWMaskHelper helps generate clip masks using the software rendering michael@0: * path. It is intended to be used as: michael@0: * michael@0: * GrSWMaskHelper helper(context); michael@0: * helper.init(...); michael@0: * michael@0: * draw one or more paths/rects specifying the required boolean ops michael@0: * michael@0: * toTexture(); // to get it from the internal bitmap to the GPU michael@0: * michael@0: * The result of this process will be the final mask (on the GPU) in the michael@0: * upper left hand corner of the texture. michael@0: */ michael@0: class GrSWMaskHelper : public SkNoncopyable { michael@0: public: michael@0: GrSWMaskHelper(GrContext* context) michael@0: : fContext(context) { michael@0: } michael@0: michael@0: // set up the internal state in preparation for draws. Since many masks michael@0: // may be accumulated in the helper during creation, "resultBounds" michael@0: // allows the caller to specify the region of interest - to limit the michael@0: // amount of work. michael@0: bool init(const SkIRect& resultBounds, const SkMatrix* matrix); michael@0: michael@0: // Draw a single rect into the accumulation bitmap using the specified op michael@0: void draw(const SkRect& rect, SkRegion::Op op, michael@0: bool antiAlias, uint8_t alpha); michael@0: michael@0: // Draw a single path into the accumuation bitmap using the specified op michael@0: void draw(const SkPath& path, const SkStrokeRec& stroke, SkRegion::Op op, michael@0: bool antiAlias, uint8_t alpha); michael@0: michael@0: // Helper function to get a scratch texture suitable for capturing the michael@0: // result (i.e., right size & format) michael@0: bool getTexture(GrAutoScratchTexture* texture); michael@0: michael@0: // Move the mask generation results from the internal bitmap to the gpu. michael@0: void toTexture(GrTexture* texture); michael@0: michael@0: // Reset the internal bitmap michael@0: void clear(uint8_t alpha) { michael@0: fBM.eraseColor(SkColorSetARGB(alpha, alpha, alpha, alpha)); michael@0: } michael@0: michael@0: // Canonical usage utility that draws a single path and uploads it michael@0: // to the GPU. The result is returned in "result". michael@0: static GrTexture* DrawPathMaskToTexture(GrContext* context, michael@0: const SkPath& path, michael@0: const SkStrokeRec& stroke, michael@0: const SkIRect& resultBounds, michael@0: bool antiAlias, michael@0: SkMatrix* matrix); michael@0: michael@0: // This utility routine is used to add a path's mask to some other draw. michael@0: // The ClipMaskManager uses it to accumulate clip masks while the michael@0: // GrSoftwarePathRenderer uses it to fulfill a drawPath call. michael@0: // It draws with "texture" as a path mask into "target" using "rect" as michael@0: // geometry and the current drawState. The current drawState is altered to michael@0: // accommodate the mask. michael@0: // Note that this method assumes that the GrPaint::kTotalStages slot in michael@0: // the draw state can be used to hold the mask texture stage. michael@0: // This method is really only intended to be used with the michael@0: // output of DrawPathMaskToTexture. michael@0: static void DrawToTargetWithPathMask(GrTexture* texture, michael@0: GrDrawTarget* target, michael@0: const SkIRect& rect); michael@0: michael@0: protected: michael@0: private: michael@0: GrContext* fContext; michael@0: SkMatrix fMatrix; michael@0: SkBitmap fBM; michael@0: SkDraw fDraw; michael@0: SkRasterClip fRasterClip; michael@0: michael@0: typedef SkNoncopyable INHERITED; michael@0: }; michael@0: michael@0: #endif // GrSWMaskHelper_DEFINED