blob: 89d8842aa723b1121c8769bad7db6cc0e0984932 [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 "GrTextureProvider.h"
14#include "SkAutoPixmapStorage.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000015#include "SkBitmap.h"
16#include "SkDraw.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000017#include "SkMatrix.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000018#include "SkRasterClip.h"
19#include "SkRegion.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000020#include "SkTypes.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000021
joshualitt44701df2015-02-23 14:44:57 -080022class GrClip;
robertphillips28a838e2016-06-23 14:07:00 -070023class GrPaint;
bsalomon8acedde2016-06-24 10:42:16 -070024class GrShape;
robertphillips0152d732016-05-20 06:38:43 -070025class GrTextureProvider;
robertphillips28a838e2016-06-23 14:07:00 -070026class GrStyle;
robertphillips@google.com58b20212012-06-27 20:44:52 +000027class GrTexture;
robertphillips28a838e2016-06-23 14:07:00 -070028struct GrUserStencilSettings;
robertphillips@google.com58b20212012-06-27 20:44:52 +000029
30/**
31 * The GrSWMaskHelper helps generate clip masks using the software rendering
robertphillips@google.com366f1c62012-06-29 21:38:47 +000032 * path. It is intended to be used as:
33 *
34 * GrSWMaskHelper helper(context);
35 * helper.init(...);
36 *
37 * draw one or more paths/rects specifying the required boolean ops
38 *
39 * toTexture(); // to get it from the internal bitmap to the GPU
40 *
41 * The result of this process will be the final mask (on the GPU) in the
42 * upper left hand corner of the texture.
robertphillips@google.com58b20212012-06-27 20:44:52 +000043 */
commit-bot@chromium.orge3beb6b2014-04-07 19:34:38 +000044class GrSWMaskHelper : SkNoncopyable {
robertphillips@google.com58b20212012-06-27 20:44:52 +000045public:
Robert Phillipse305cc1f2016-12-14 12:19:05 -050046 GrSWMaskHelper() { }
robertphillips@google.com58b20212012-06-27 20:44:52 +000047
robertphillips@google.com366f1c62012-06-29 21:38:47 +000048 // set up the internal state in preparation for draws. Since many masks
rmistry@google.comd6176b02012-08-23 18:14:13 +000049 // may be accumulated in the helper during creation, "resultBounds"
50 // allows the caller to specify the region of interest - to limit the
robertphillips98377402016-05-13 05:47:23 -070051 // amount of work.
52 bool init(const SkIRect& resultBounds, const SkMatrix* matrix);
robertphillips@google.com58b20212012-06-27 20:44:52 +000053
robertphillips@google.com366f1c62012-06-29 21:38:47 +000054 // Draw a single rect into the accumulation bitmap using the specified op
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050055 void drawRect(const SkRect& rect, SkRegion::Op op, GrAA, 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
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050058 void drawShape(const GrShape&, SkRegion::Op op, GrAA, uint8_t alpha);
robertphillips@google.com58b20212012-06-27 20:44:52 +000059
Robert Phillipse305cc1f2016-12-14 12:19:05 -050060 sk_sp<GrTextureProxy> toTexture(GrContext*, SkBackingFit fit);
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.
Robert Phillipse305cc1f2016-12-14 12:19:05 -050072 static sk_sp<GrTexture> DrawShapeMaskToTexture(GrContext*,
73 const GrShape&,
74 const SkIRect& resultBounds,
75 GrAA,
76 SkBackingFit,
77 const SkMatrix* matrix);
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000078
bsalomon39ef7fb2016-09-21 11:16:05 -070079 // This utility draws a path mask generated by DrawShapeMaskToTexture using a provided paint.
80 // The rectangle is drawn in device space. The 'viewMatrix' will be used to ensure the correct
81 // local coords are provided to any fragment processors in the paint.
bsalomon8acedde2016-06-24 10:42:16 -070082 static void DrawToTargetWithShapeMask(GrTexture* texture,
Brian Osman11052242016-10-27 14:47:55 -040083 GrRenderTargetContext*,
Brian Salomon82f44312017-01-11 13:42:54 -050084 GrPaint&& paint,
robertphillipsd2b6d642016-07-21 08:55:08 -070085 const GrUserStencilSettings& userStencilSettings,
bsalomon8acedde2016-06-24 10:42:16 -070086 const GrClip&,
bsalomon8acedde2016-06-24 10:42:16 -070087 const SkMatrix& viewMatrix,
bsalomon39ef7fb2016-09-21 11:16:05 -070088 const SkIPoint& textureOriginInDeviceSpace,
89 const SkIRect& deviceSpaceRectToDraw);
robertphillips@google.com366f1c62012-06-29 21:38:47 +000090
robertphillips@google.com58b20212012-06-27 20:44:52 +000091private:
robertphillips0152d732016-05-20 06:38:43 -070092 SkMatrix fMatrix;
reed41e010c2015-06-09 12:16:53 -070093 SkAutoPixmapStorage fPixels;
robertphillips0152d732016-05-20 06:38:43 -070094 SkDraw fDraw;
95 SkRasterClip fRasterClip;
robertphillips@google.com58b20212012-06-27 20:44:52 +000096
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000097 typedef SkNoncopyable INHERITED;
robertphillips@google.com58b20212012-06-27 20:44:52 +000098};
99
robertphillips@google.comfe659432012-06-28 01:01:53 +0000100#endif // GrSWMaskHelper_DEFINED