blob: 3c5521fb818de4cf6885408bbb032e931104e698 [file] [log] [blame]
reed@google.come36707a2011-10-04 21:38:55 +00001/*
2 * Copyright 2011 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 SkAAClip_DEFINED
9#define SkAAClip_DEFINED
10
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkRegion.h"
12#include "src/core/SkAutoMalloc.h"
13#include "src/core/SkBlitter.h"
reed@google.come36707a2011-10-04 21:38:55 +000014
reed@google.come36707a2011-10-04 21:38:55 +000015class SkAAClip {
16public:
17 SkAAClip();
18 SkAAClip(const SkAAClip&);
19 ~SkAAClip();
20
21 SkAAClip& operator=(const SkAAClip&);
22 friend bool operator==(const SkAAClip&, const SkAAClip&);
23 friend bool operator!=(const SkAAClip& a, const SkAAClip& b) {
24 return !(a == b);
25 }
26
27 void swap(SkAAClip&);
28
halcanary96fcdcc2015-08-27 07:41:13 -070029 bool isEmpty() const { return nullptr == fRunHead; }
reed@google.come36707a2011-10-04 21:38:55 +000030 const SkIRect& getBounds() const { return fBounds; }
31
reed202ab2a2014-08-07 11:48:10 -070032 // Returns true iff the clip is not empty, and is just a hard-edged rect (no partial alpha).
33 // If true, getBounds() can be used in place of this clip.
34 bool isRect() const;
35
reed@google.come36707a2011-10-04 21:38:55 +000036 bool setEmpty();
37 bool setRect(const SkIRect&);
reed@google.comf3c1da12011-10-10 19:35:47 +000038 bool setRect(const SkRect&, bool doAA = true);
halcanary96fcdcc2015-08-27 07:41:13 -070039 bool setPath(const SkPath&, const SkRegion* clip = nullptr, bool doAA = true);
reed@google.comf3c1da12011-10-10 19:35:47 +000040 bool setRegion(const SkRegion&);
reed@google.com32287892011-10-05 16:27:44 +000041 bool set(const SkAAClip&);
reed@google.come36707a2011-10-04 21:38:55 +000042
43 bool op(const SkAAClip&, const SkAAClip&, SkRegion::Op);
44
reed@google.com47ac84e2011-10-06 13:11:25 +000045 // Helpers for op()
46 bool op(const SkIRect&, SkRegion::Op);
reed@google.com00177082011-10-12 14:34:30 +000047 bool op(const SkRect&, SkRegion::Op, bool doAA);
reed@google.com47ac84e2011-10-06 13:11:25 +000048 bool op(const SkAAClip&, SkRegion::Op);
49
reed@google.com34f7e472011-10-13 15:11:59 +000050 bool translate(int dx, int dy, SkAAClip* dst) const;
51 bool translate(int dx, int dy) {
52 return this->translate(dx, dy, this);
53 }
reed@google.com1c04bf92011-10-10 12:57:12 +000054
reed@google.com32287892011-10-05 16:27:44 +000055 /**
56 * Allocates a mask the size of the aaclip, and expands its data into
57 * the mask, using kA8_Format
58 */
59 void copyToMask(SkMask*) const;
60
reed@google.come36707a2011-10-04 21:38:55 +000061 // called internally
rmistry@google.comfbfcd562012-08-23 18:09:54 +000062
reed@google.come36707a2011-10-04 21:38:55 +000063 bool quickContains(int left, int top, int right, int bottom) const;
reed@google.com045e62d2011-10-24 12:19:46 +000064 bool quickContains(const SkIRect& r) const {
65 return this->quickContains(r.fLeft, r.fTop, r.fRight, r.fBottom);
66 }
67
halcanary96fcdcc2015-08-27 07:41:13 -070068 const uint8_t* findRow(int y, int* lastYForRow = nullptr) const;
69 const uint8_t* findX(const uint8_t data[], int x, int* initialCount = nullptr) const;
reed@google.come36707a2011-10-04 21:38:55 +000070
reed@google.com32287892011-10-05 16:27:44 +000071 class Iter;
reed@google.come36707a2011-10-04 21:38:55 +000072 struct RunHead;
73 struct YOffset;
reed@google.com32287892011-10-05 16:27:44 +000074 class Builder;
reed@google.come36707a2011-10-04 21:38:55 +000075
reed@google.com045e62d2011-10-24 12:19:46 +000076#ifdef SK_DEBUG
77 void validate() const;
humper6d42d9c2014-08-08 11:45:46 -070078 void debug(bool compress_y=false) const;
reed@google.com045e62d2011-10-24 12:19:46 +000079#else
80 void validate() const {}
humper6d42d9c2014-08-08 11:45:46 -070081 void debug(bool compress_y=false) const {}
reed@google.com045e62d2011-10-24 12:19:46 +000082#endif
83
reed@google.com32287892011-10-05 16:27:44 +000084private:
reed@google.come36707a2011-10-04 21:38:55 +000085 SkIRect fBounds;
86 RunHead* fRunHead;
87
88 void freeRuns();
reed@google.com045e62d2011-10-24 12:19:46 +000089 bool trimBounds();
reed@google.comc9041912011-10-27 16:58:46 +000090 bool trimTopBottom();
91 bool trimLeftRight();
reed@google.come36707a2011-10-04 21:38:55 +000092
reed@google.come36707a2011-10-04 21:38:55 +000093 friend class Builder;
94 class BuilderBlitter;
95 friend class BuilderBlitter;
96};
97
98///////////////////////////////////////////////////////////////////////////////
99
100class SkAAClipBlitter : public SkBlitter {
101public:
halcanary96fcdcc2015-08-27 07:41:13 -0700102 SkAAClipBlitter() : fScanlineScratch(nullptr) {}
Brian Salomond3b65972017-03-22 12:05:03 -0400103 ~SkAAClipBlitter() override;
reed@google.come36707a2011-10-04 21:38:55 +0000104
105 void init(SkBlitter* blitter, const SkAAClip* aaclip) {
106 SkASSERT(aaclip && !aaclip->isEmpty());
107 fBlitter = blitter;
108 fAAClip = aaclip;
109 fAAClipBounds = aaclip->getBounds();
110 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000111
mtklein36352bf2015-03-25 18:17:31 -0700112 void blitH(int x, int y, int width) override;
reed41e010c2015-06-09 12:16:53 -0700113 void blitAntiH(int x, int y, const SkAlpha[], const int16_t runs[]) override;
mtklein36352bf2015-03-25 18:17:31 -0700114 void blitV(int x, int y, int height, SkAlpha alpha) override;
115 void blitRect(int x, int y, int width, int height) override;
116 void blitMask(const SkMask&, const SkIRect& clip) override;
reed41e010c2015-06-09 12:16:53 -0700117 const SkPixmap* justAnOpaqueColor(uint32_t* value) override;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000118
reed@google.come36707a2011-10-04 21:38:55 +0000119private:
120 SkBlitter* fBlitter;
121 const SkAAClip* fAAClip;
122 SkIRect fAAClipBounds;
123
reed@google.com045e62d2011-10-24 12:19:46 +0000124 // point into fScanlineScratch
reed@google.come36707a2011-10-04 21:38:55 +0000125 int16_t* fRuns;
reed@google.com045e62d2011-10-24 12:19:46 +0000126 SkAlpha* fAA;
127
128 enum {
129 kSize = 32 * 32
130 };
131 SkAutoSMalloc<kSize> fGrayMaskScratch; // used for blitMask
132 void* fScanlineScratch; // enough for a mask at 32bit, or runs+aa
reed@google.come36707a2011-10-04 21:38:55 +0000133
134 void ensureRunsAndAA();
135};
136
137#endif