blob: b274e84c66d7164f9ea8151910c56ad2638ea66d [file] [log] [blame]
robertphillips@google.com58b20212012-06-27 20:44:52 +00001/*
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"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000012#include "GrDrawState.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000013#include "SkBitmap.h"
14#include "SkDraw.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000015#include "SkMatrix.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000016#include "SkRasterClip.h"
17#include "SkRegion.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000018#include "SkTypes.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000019
20class GrAutoScratchTexture;
21class GrContext;
22class GrTexture;
23class SkPath;
sugoi@google.com5f74cf82012-12-17 21:16:45 +000024class SkStrokeRec;
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000025class GrDrawTarget;
robertphillips@google.com58b20212012-06-27 20:44:52 +000026
27/**
28 * The GrSWMaskHelper helps generate clip masks using the software rendering
robertphillips@google.com366f1c62012-06-29 21:38:47 +000029 * 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.
robertphillips@google.com58b20212012-06-27 20:44:52 +000040 */
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000041class GrSWMaskHelper : public SkNoncopyable {
robertphillips@google.com58b20212012-06-27 20:44:52 +000042public:
rmistry@google.comd6176b02012-08-23 18:14:13 +000043 GrSWMaskHelper(GrContext* context)
robertphillips@google.com58b20212012-06-27 20:44:52 +000044 : fContext(context) {
45 }
46
robertphillips@google.com366f1c62012-06-29 21:38:47 +000047 // set up the internal state in preparation for draws. Since many masks
rmistry@google.comd6176b02012-08-23 18:14:13 +000048 // may be accumulated in the helper during creation, "resultBounds"
49 // allows the caller to specify the region of interest - to limit the
robertphillips@google.com366f1c62012-06-29 21:38:47 +000050 // amount of work.
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000051 bool init(const SkIRect& resultBounds, const SkMatrix* matrix);
robertphillips@google.com58b20212012-06-27 20:44:52 +000052
robertphillips@google.com366f1c62012-06-29 21:38:47 +000053 // Draw a single rect into the accumulation bitmap using the specified op
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000054 void draw(const SkRect& rect, SkRegion::Op op,
robertphillips@google.com366f1c62012-06-29 21:38:47 +000055 bool antiAlias, uint8_t alpha);
robertphillips@google.com58b20212012-06-27 20:44:52 +000056
robertphillips@google.com366f1c62012-06-29 21:38:47 +000057 // Draw a single path into the accumuation bitmap using the specified op
sugoi@google.com5f74cf82012-12-17 21:16:45 +000058 void draw(const SkPath& path, const SkStrokeRec& stroke, SkRegion::Op op,
sugoi@google.com12b4e272012-12-06 20:13:11 +000059 bool antiAlias, uint8_t alpha);
robertphillips@google.com58b20212012-06-27 20:44:52 +000060
robertphillips@google.com366f1c62012-06-29 21:38:47 +000061 // Helper function to get a scratch texture suitable for capturing the
62 // result (i.e., right size & format)
63 bool getTexture(GrAutoScratchTexture* texture);
robertphillips@google.com58b20212012-06-27 20:44:52 +000064
robertphillips@google.com366f1c62012-06-29 21:38:47 +000065 // Move the mask generation results from the internal bitmap to the gpu.
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +000066 void toTexture(GrTexture* texture);
robertphillips@google.com58b20212012-06-27 20:44:52 +000067
robertphillips@google.com366f1c62012-06-29 21:38:47 +000068 // Reset the internal bitmap
69 void clear(uint8_t alpha) {
70 fBM.eraseColor(SkColorSetARGB(alpha, alpha, alpha, alpha));
robertphillips@google.com58b20212012-06-27 20:44:52 +000071 }
72
robertphillips@google.com366f1c62012-06-29 21:38:47 +000073 // Canonical usage utility that draws a single path and uploads it
74 // to the GPU. The result is returned in "result".
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000075 static GrTexture* DrawPathMaskToTexture(GrContext* context,
76 const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +000077 const SkStrokeRec& stroke,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000078 const SkIRect& resultBounds,
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000079 bool antiAlias,
bsalomon@google.comb9086a02012-11-01 18:02:54 +000080 SkMatrix* matrix);
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000081
82 // This utility routine is used to add a path's mask to some other draw.
rmistry@google.comd6176b02012-08-23 18:14:13 +000083 // The ClipMaskManager uses it to accumulate clip masks while the
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000084 // GrSoftwarePathRenderer uses it to fulfill a drawPath call.
rmistry@google.comd6176b02012-08-23 18:14:13 +000085 // It draws with "texture" as a path mask into "target" using "rect" as
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000086 // geometry and the current drawState. The current drawState is altered to
87 // accommodate the mask.
rmistry@google.comd6176b02012-08-23 18:14:13 +000088 // Note that this method assumes that the GrPaint::kTotalStages slot in
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000089 // the draw state can be used to hold the mask texture stage.
rmistry@google.comd6176b02012-08-23 18:14:13 +000090 // This method is really only intended to be used with the
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000091 // output of DrawPathMaskToTexture.
92 static void DrawToTargetWithPathMask(GrTexture* texture,
93 GrDrawTarget* target,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000094 const SkIRect& rect);
robertphillips@google.com366f1c62012-06-29 21:38:47 +000095
robertphillips@google.com58b20212012-06-27 20:44:52 +000096protected:
97private:
98 GrContext* fContext;
bsalomon@google.comb9086a02012-11-01 18:02:54 +000099 SkMatrix fMatrix;
robertphillips@google.com58b20212012-06-27 20:44:52 +0000100 SkBitmap fBM;
101 SkDraw fDraw;
102 SkRasterClip fRasterClip;
103
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000104 typedef SkNoncopyable INHERITED;
robertphillips@google.com58b20212012-06-27 20:44:52 +0000105};
106
robertphillips@google.comfe659432012-06-28 01:01:53 +0000107#endif // GrSWMaskHelper_DEFINED