blob: b329dd627e0af065be3dbd60999f57ea93827594 [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
robertphillipsc5035e72016-03-17 06:58:39 -070011#include "SkAutoPixmapStorage.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000012#include "GrColor.h"
egdaniel8dd688b2015-01-22 10:16:09 -080013#include "GrPipelineBuilder.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000014#include "SkBitmap.h"
15#include "SkDraw.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000016#include "SkMatrix.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000017#include "SkRasterClip.h"
18#include "SkRegion.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000019#include "SkTypes.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000020
joshualitt44701df2015-02-23 14:44:57 -080021class GrClip;
robertphillips@google.com58b20212012-06-27 20:44:52 +000022class GrContext;
23class GrTexture;
24class SkPath;
sugoi@google.com5f74cf82012-12-17 21:16:45 +000025class SkStrokeRec;
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000026class GrDrawTarget;
robertphillips@google.com58b20212012-06-27 20:44:52 +000027
28/**
29 * The GrSWMaskHelper helps generate clip masks using the software rendering
robertphillips@google.com366f1c62012-06-29 21:38:47 +000030 * path. It is intended to be used as:
31 *
32 * GrSWMaskHelper helper(context);
33 * helper.init(...);
34 *
35 * draw one or more paths/rects specifying the required boolean ops
36 *
37 * toTexture(); // to get it from the internal bitmap to the GPU
38 *
39 * The result of this process will be the final mask (on the GPU) in the
40 * upper left hand corner of the texture.
robertphillips@google.com58b20212012-06-27 20:44:52 +000041 */
commit-bot@chromium.orge3beb6b2014-04-07 19:34:38 +000042class GrSWMaskHelper : SkNoncopyable {
robertphillips@google.com58b20212012-06-27 20:44:52 +000043public:
robertphillips98377402016-05-13 05:47:23 -070044 GrSWMaskHelper(GrContext* context) : fContext(context) { }
robertphillips@google.com58b20212012-06-27 20:44:52 +000045
robertphillips@google.com366f1c62012-06-29 21:38:47 +000046 // set up the internal state in preparation for draws. Since many masks
rmistry@google.comd6176b02012-08-23 18:14:13 +000047 // may be accumulated in the helper during creation, "resultBounds"
48 // allows the caller to specify the region of interest - to limit the
robertphillips98377402016-05-13 05:47:23 -070049 // amount of work.
50 bool init(const SkIRect& resultBounds, const SkMatrix* matrix);
robertphillips@google.com58b20212012-06-27 20:44:52 +000051
robertphillips@google.com366f1c62012-06-29 21:38:47 +000052 // Draw a single rect into the accumulation bitmap using the specified op
robertphillips98377402016-05-13 05:47:23 -070053 void drawRect(const SkRect& rect, SkRegion::Op op, bool antiAlias, uint8_t alpha);
robertphillips@google.com58b20212012-06-27 20:44:52 +000054
robertphillips@google.com366f1c62012-06-29 21:38:47 +000055 // Draw a single path into the accumuation bitmap using the specified op
robertphillips98377402016-05-13 05:47:23 -070056 void drawPath(const SkPath& path, const GrStyle& style, SkRegion::Op op,
57 bool antiAlias, uint8_t alpha);
robertphillips@google.com58b20212012-06-27 20:44:52 +000058
robertphillips@google.com366f1c62012-06-29 21:38:47 +000059 // Move the mask generation results from the internal bitmap to the gpu.
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +000060 void toTexture(GrTexture* texture);
robertphillips@google.com58b20212012-06-27 20:44:52 +000061
jvanverthfa38a302014-10-06 05:59:05 -070062 // Convert mask generation results to a signed distance field
63 void toSDF(unsigned char* sdf);
halcanary9d524f22016-03-29 09:03:52 -070064
robertphillips@google.com366f1c62012-06-29 21:38:47 +000065 // Reset the internal bitmap
66 void clear(uint8_t alpha) {
reed41e010c2015-06-09 12:16:53 -070067 fPixels.erase(SkColorSetARGB(alpha, 0xFF, 0xFF, 0xFF));
robertphillips@google.com58b20212012-06-27 20:44:52 +000068 }
69
robertphillips@google.com366f1c62012-06-29 21:38:47 +000070 // Canonical usage utility that draws a single path and uploads it
jvanverthfa38a302014-10-06 05:59:05 -070071 // to the GPU. The result is returned.
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000072 static GrTexture* DrawPathMaskToTexture(GrContext* context,
73 const SkPath& path,
bsalomon6663acf2016-05-10 09:14:17 -070074 const GrStyle& style,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000075 const SkIRect& resultBounds,
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000076 bool antiAlias,
joshualitt8059eb92014-12-29 15:10:07 -080077 const SkMatrix* matrix);
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000078
79 // This utility routine is used to add a path's mask to some other draw.
rmistry@google.comd6176b02012-08-23 18:14:13 +000080 // The ClipMaskManager uses it to accumulate clip masks while the
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000081 // GrSoftwarePathRenderer uses it to fulfill a drawPath call.
rmistry@google.comd6176b02012-08-23 18:14:13 +000082 // It draws with "texture" as a path mask into "target" using "rect" as
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000083 // geometry and the current drawState. The current drawState is altered to
84 // accommodate the mask.
rmistry@google.comd6176b02012-08-23 18:14:13 +000085 // Note that this method assumes that the GrPaint::kTotalStages slot in
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000086 // the draw state can be used to hold the mask texture stage.
rmistry@google.comd6176b02012-08-23 18:14:13 +000087 // This method is really only intended to be used with the
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000088 // output of DrawPathMaskToTexture.
89 static void DrawToTargetWithPathMask(GrTexture* texture,
90 GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -080091 GrPipelineBuilder* pipelineBuilder,
cdalton862cff32016-05-12 15:09:48 -070092 const GrClip&,
joshualitt2e3b3e32014-12-09 13:31:14 -080093 GrColor,
joshualitt8059eb92014-12-29 15:10:07 -080094 const SkMatrix& viewMatrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000095 const SkIRect& rect);
robertphillips@google.com366f1c62012-06-29 21:38:47 +000096
robertphillips@google.com58b20212012-06-27 20:44:52 +000097private:
bsalomone3059732014-10-14 11:47:22 -070098 // Helper function to get a scratch texture suitable for capturing the
99 // result (i.e., right size & format)
100 GrTexture* createTexture();
101
robertphillips@google.com58b20212012-06-27 20:44:52 +0000102 GrContext* fContext;
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000103 SkMatrix fMatrix;
reed41e010c2015-06-09 12:16:53 -0700104 SkAutoPixmapStorage fPixels;
robertphillips@google.com58b20212012-06-27 20:44:52 +0000105 SkDraw fDraw;
106 SkRasterClip fRasterClip;
107
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000108 typedef SkNoncopyable INHERITED;
robertphillips@google.com58b20212012-06-27 20:44:52 +0000109};
110
robertphillips@google.comfe659432012-06-28 01:01:53 +0000111#endif // GrSWMaskHelper_DEFINED