blob: b015b15a4b859f1ab13e00ebea4f01577236193a [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"
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
joshualitt44701df2015-02-23 14:44:57 -080020class GrClip;
robertphillips28a838e2016-06-23 14:07:00 -070021class GrPaint;
bsalomon8acedde2016-06-24 10:42:16 -070022class GrShape;
robertphillips0152d732016-05-20 06:38:43 -070023class GrTextureProvider;
robertphillips28a838e2016-06-23 14:07:00 -070024class GrStyle;
robertphillips@google.com58b20212012-06-27 20:44:52 +000025class GrTexture;
robertphillips28a838e2016-06-23 14:07:00 -070026struct GrUserStencilSettings;
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:
robertphillips0152d732016-05-20 06:38:43 -070044 GrSWMaskHelper(GrTextureProvider* texProvider) : fTexProvider(texProvider) { }
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
bsalomon8acedde2016-06-24 10:42:16 -070056 void drawShape(const GrShape&, SkRegion::Op op, bool antiAlias, uint8_t alpha);
robertphillips@google.com58b20212012-06-27 20:44:52 +000057
robertphillips@google.com366f1c62012-06-29 21:38:47 +000058 // Move the mask generation results from the internal bitmap to the gpu.
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +000059 void toTexture(GrTexture* texture);
robertphillips@google.com58b20212012-06-27 20:44:52 +000060
jvanverthfa38a302014-10-06 05:59:05 -070061 // Convert mask generation results to a signed distance field
62 void toSDF(unsigned char* sdf);
halcanary9d524f22016-03-29 09:03:52 -070063
robertphillips@google.com366f1c62012-06-29 21:38:47 +000064 // Reset the internal bitmap
65 void clear(uint8_t alpha) {
reed41e010c2015-06-09 12:16:53 -070066 fPixels.erase(SkColorSetARGB(alpha, 0xFF, 0xFF, 0xFF));
robertphillips@google.com58b20212012-06-27 20:44:52 +000067 }
68
robertphillips@google.com366f1c62012-06-29 21:38:47 +000069 // Canonical usage utility that draws a single path and uploads it
jvanverthfa38a302014-10-06 05:59:05 -070070 // to the GPU. The result is returned.
bsalomon8acedde2016-06-24 10:42:16 -070071 static GrTexture* DrawShapeMaskToTexture(GrTextureProvider*,
72 const GrShape&,
73 const SkIRect& resultBounds,
74 bool antiAlias,
75 const SkMatrix* matrix);
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000076
bsalomon8acedde2016-06-24 10:42:16 -070077 // This utility routine is used to add a shape's mask to some other draw.
rmistry@google.comd6176b02012-08-23 18:14:13 +000078 // The ClipMaskManager uses it to accumulate clip masks while the
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000079 // GrSoftwarePathRenderer uses it to fulfill a drawPath call.
rmistry@google.comd6176b02012-08-23 18:14:13 +000080 // It draws with "texture" as a path mask into "target" using "rect" as
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000081 // geometry and the current drawState. The current drawState is altered to
82 // accommodate the mask.
rmistry@google.comd6176b02012-08-23 18:14:13 +000083 // Note that this method assumes that the GrPaint::kTotalStages slot in
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000084 // the draw state can be used to hold the mask texture stage.
rmistry@google.comd6176b02012-08-23 18:14:13 +000085 // This method is really only intended to be used with the
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000086 // output of DrawPathMaskToTexture.
bsalomon8acedde2016-06-24 10:42:16 -070087 static void DrawToTargetWithShapeMask(GrTexture* texture,
88 GrDrawContext*,
89 const GrPaint* paint,
90 const GrUserStencilSettings* userStencilSettings,
91 const GrClip&,
92 GrColor,
93 const SkMatrix& viewMatrix,
94 const SkIRect& rect);
robertphillips@google.com366f1c62012-06-29 21:38:47 +000095
robertphillips@google.com58b20212012-06-27 20:44:52 +000096private:
bsalomone3059732014-10-14 11:47:22 -070097 // Helper function to get a scratch texture suitable for capturing the
98 // result (i.e., right size & format)
99 GrTexture* createTexture();
100
robertphillips0152d732016-05-20 06:38:43 -0700101 GrTextureProvider* fTexProvider;
102 SkMatrix fMatrix;
reed41e010c2015-06-09 12:16:53 -0700103 SkAutoPixmapStorage fPixels;
robertphillips0152d732016-05-20 06:38:43 -0700104 SkDraw fDraw;
105 SkRasterClip fRasterClip;
robertphillips@google.com58b20212012-06-27 20:44:52 +0000106
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000107 typedef SkNoncopyable INHERITED;
robertphillips@google.com58b20212012-06-27 20:44:52 +0000108};
109
robertphillips@google.comfe659432012-06-28 01:01:53 +0000110#endif // GrSWMaskHelper_DEFINED