blob: 62c1bbf292457a0d8e4842b37d9174f94bf787bb [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"
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050012#include "GrRenderTargetContext.h"
bsalomon39ef7fb2016-09-21 11:16:05 -070013#include "SkAutoPixmapStorage.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;
robertphillips28a838e2016-06-23 14:07:00 -070022class GrPaint;
bsalomon8acedde2016-06-24 10:42:16 -070023class GrShape;
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:
Robert Phillipse305cc1f2016-12-14 12:19:05 -050044 GrSWMaskHelper() { }
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
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050053 void drawRect(const SkRect& rect, SkRegion::Op op, GrAA, 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
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050056 void drawShape(const GrShape&, SkRegion::Op op, GrAA, uint8_t alpha);
robertphillips@google.com58b20212012-06-27 20:44:52 +000057
Robert Phillipsd3749482017-03-14 09:17:43 -040058 sk_sp<GrTextureProxy> toTextureProxy(GrContext*, SkBackingFit fit);
robertphillips@google.com58b20212012-06-27 20:44:52 +000059
jvanverthfa38a302014-10-06 05:59:05 -070060 // Convert mask generation results to a signed distance field
61 void toSDF(unsigned char* sdf);
halcanary9d524f22016-03-29 09:03:52 -070062
robertphillips@google.com366f1c62012-06-29 21:38:47 +000063 // Reset the internal bitmap
64 void clear(uint8_t alpha) {
reed41e010c2015-06-09 12:16:53 -070065 fPixels.erase(SkColorSetARGB(alpha, 0xFF, 0xFF, 0xFF));
robertphillips@google.com58b20212012-06-27 20:44:52 +000066 }
67
robertphillips@google.com366f1c62012-06-29 21:38:47 +000068 // Canonical usage utility that draws a single path and uploads it
jvanverthfa38a302014-10-06 05:59:05 -070069 // to the GPU. The result is returned.
Robert Phillipsd3749482017-03-14 09:17:43 -040070 static sk_sp<GrTextureProxy> DrawShapeMaskToTexture(GrContext*,
71 const GrShape&,
72 const SkIRect& resultBounds,
73 GrAA,
74 SkBackingFit,
75 const SkMatrix* matrix);
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000076
bsalomon39ef7fb2016-09-21 11:16:05 -070077 // This utility draws a path mask generated by DrawShapeMaskToTexture using a provided paint.
78 // The rectangle is drawn in device space. The 'viewMatrix' will be used to ensure the correct
79 // local coords are provided to any fragment processors in the paint.
Robert Phillips296b1cc2017-03-15 10:42:12 -040080 static void DrawToTargetWithShapeMask(sk_sp<GrTextureProxy>,
Brian Osman11052242016-10-27 14:47:55 -040081 GrRenderTargetContext*,
Brian Salomon82f44312017-01-11 13:42:54 -050082 GrPaint&& paint,
robertphillipsd2b6d642016-07-21 08:55:08 -070083 const GrUserStencilSettings& userStencilSettings,
bsalomon8acedde2016-06-24 10:42:16 -070084 const GrClip&,
bsalomon8acedde2016-06-24 10:42:16 -070085 const SkMatrix& viewMatrix,
bsalomon39ef7fb2016-09-21 11:16:05 -070086 const SkIPoint& textureOriginInDeviceSpace,
87 const SkIRect& deviceSpaceRectToDraw);
robertphillips@google.com366f1c62012-06-29 21:38:47 +000088
robertphillips@google.com58b20212012-06-27 20:44:52 +000089private:
robertphillips0152d732016-05-20 06:38:43 -070090 SkMatrix fMatrix;
reed41e010c2015-06-09 12:16:53 -070091 SkAutoPixmapStorage fPixels;
robertphillips0152d732016-05-20 06:38:43 -070092 SkDraw fDraw;
93 SkRasterClip fRasterClip;
robertphillips@google.com58b20212012-06-27 20:44:52 +000094
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000095 typedef SkNoncopyable INHERITED;
robertphillips@google.com58b20212012-06-27 20:44:52 +000096};
97
robertphillips@google.comfe659432012-06-28 01:01:53 +000098#endif // GrSWMaskHelper_DEFINED