robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | |
Mike Reed | 986480a | 2017-01-13 22:43:16 +0000 | [diff] [blame] | 8 | #include "SkBitmapDevice.h" |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 9 | #include "SkDraw.h" |
reed | e51c356 | 2016-07-19 14:33:20 -0700 | [diff] [blame] | 10 | #include "SkImageFilter.h" |
senorblanco | 900c367 | 2016-04-27 11:31:23 -0700 | [diff] [blame] | 11 | #include "SkImageFilterCache.h" |
mtklein | cd49541 | 2015-11-05 09:46:23 -0800 | [diff] [blame] | 12 | #include "SkMallocPixelRef.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 13 | #include "SkMatrix.h" |
| 14 | #include "SkPaint.h" |
| 15 | #include "SkPath.h" |
reed | 884e97c | 2015-05-26 11:31:54 -0700 | [diff] [blame] | 16 | #include "SkPixelRef.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 17 | #include "SkPixmap.h" |
reed | e51c356 | 2016-07-19 14:33:20 -0700 | [diff] [blame] | 18 | #include "SkRasterClip.h" |
Mike Reed | 356f7c2 | 2017-01-10 11:58:39 -0500 | [diff] [blame] | 19 | #include "SkRasterHandleAllocator.h" |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 20 | #include "SkShader.h" |
reed | e51c356 | 2016-07-19 14:33:20 -0700 | [diff] [blame] | 21 | #include "SkSpecialImage.h" |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 22 | #include "SkSurface.h" |
Florin Malita | 53f77bd | 2017-04-28 13:48:37 -0400 | [diff] [blame] | 23 | #include "SkTLazy.h" |
Mike Reed | 2f6b5a4 | 2017-03-19 15:04:17 -0400 | [diff] [blame] | 24 | #include "SkVertices.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 25 | |
| 26 | class SkColorTable; |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 27 | |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 28 | static bool valid_for_bitmap_device(const SkImageInfo& info, |
| 29 | SkAlphaType* newAlphaType) { |
| 30 | if (info.width() < 0 || info.height() < 0) { |
| 31 | return false; |
| 32 | } |
| 33 | |
| 34 | // TODO: can we stop supporting kUnknown in SkBitmkapDevice? |
| 35 | if (kUnknown_SkColorType == info.colorType()) { |
| 36 | if (newAlphaType) { |
reed | 4497748 | 2015-02-27 10:23:00 -0800 | [diff] [blame] | 37 | *newAlphaType = kUnknown_SkAlphaType; |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 38 | } |
| 39 | return true; |
| 40 | } |
skia.committer@gmail.com | 969588f | 2014-02-16 03:01:56 +0000 | [diff] [blame] | 41 | |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 42 | SkAlphaType canonicalAlphaType = info.alphaType(); |
| 43 | |
| 44 | switch (info.colorType()) { |
| 45 | case kAlpha_8_SkColorType: |
| 46 | break; |
| 47 | case kRGB_565_SkColorType: |
| 48 | canonicalAlphaType = kOpaque_SkAlphaType; |
| 49 | break; |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 50 | case kN32_SkColorType: |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 51 | break; |
reed | a34be68 | 2016-02-15 07:48:35 -0800 | [diff] [blame] | 52 | case kRGBA_F16_SkColorType: |
| 53 | break; |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 54 | default: |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | if (newAlphaType) { |
| 59 | *newAlphaType = canonicalAlphaType; |
| 60 | } |
| 61 | return true; |
| 62 | } |
| 63 | |
robertphillips | 9a53fd7 | 2015-06-22 09:46:59 -0700 | [diff] [blame] | 64 | SkBitmapDevice::SkBitmapDevice(const SkBitmap& bitmap) |
reed | 589a39e | 2016-08-20 07:59:19 -0700 | [diff] [blame] | 65 | : INHERITED(bitmap.info(), SkSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType)) |
reed | 2c9e200 | 2016-07-25 08:05:22 -0700 | [diff] [blame] | 66 | , fBitmap(bitmap) |
Mike Reed | c42a1cd | 2017-02-14 14:25:14 -0500 | [diff] [blame] | 67 | , fRCStack(bitmap.width(), bitmap.height()) |
reed | 2c9e200 | 2016-07-25 08:05:22 -0700 | [diff] [blame] | 68 | { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 69 | SkASSERT(valid_for_bitmap_device(bitmap.info(), nullptr)); |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 70 | } |
| 71 | |
robertphillips | 9a53fd7 | 2015-06-22 09:46:59 -0700 | [diff] [blame] | 72 | SkBitmapDevice* SkBitmapDevice::Create(const SkImageInfo& info) { |
| 73 | return Create(info, SkSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType)); |
| 74 | } |
| 75 | |
Mike Reed | 356f7c2 | 2017-01-10 11:58:39 -0500 | [diff] [blame] | 76 | SkBitmapDevice::SkBitmapDevice(const SkBitmap& bitmap, const SkSurfaceProps& surfaceProps, |
| 77 | SkRasterHandleAllocator::Handle hndl) |
reed | 589a39e | 2016-08-20 07:59:19 -0700 | [diff] [blame] | 78 | : INHERITED(bitmap.info(), surfaceProps) |
reed | 2c9e200 | 2016-07-25 08:05:22 -0700 | [diff] [blame] | 79 | , fBitmap(bitmap) |
Mike Reed | 356f7c2 | 2017-01-10 11:58:39 -0500 | [diff] [blame] | 80 | , fRasterHandle(hndl) |
Mike Reed | c42a1cd | 2017-02-14 14:25:14 -0500 | [diff] [blame] | 81 | , fRCStack(bitmap.width(), bitmap.height()) |
reed | 2c9e200 | 2016-07-25 08:05:22 -0700 | [diff] [blame] | 82 | { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 83 | SkASSERT(valid_for_bitmap_device(bitmap.info(), nullptr)); |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 84 | } |
| 85 | |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 86 | SkBitmapDevice* SkBitmapDevice::Create(const SkImageInfo& origInfo, |
Mike Reed | 356f7c2 | 2017-01-10 11:58:39 -0500 | [diff] [blame] | 87 | const SkSurfaceProps& surfaceProps, |
| 88 | SkRasterHandleAllocator* allocator) { |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 89 | SkAlphaType newAT = origInfo.alphaType(); |
| 90 | if (!valid_for_bitmap_device(origInfo, &newAT)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 91 | return nullptr; |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 92 | } |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 93 | |
Mike Reed | 356f7c2 | 2017-01-10 11:58:39 -0500 | [diff] [blame] | 94 | SkRasterHandleAllocator::Handle hndl = nullptr; |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 95 | const SkImageInfo info = origInfo.makeAlphaType(newAT); |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 96 | SkBitmap bitmap; |
| 97 | |
| 98 | if (kUnknown_SkColorType == info.colorType()) { |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 99 | if (!bitmap.setInfo(info)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 100 | return nullptr; |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 101 | } |
Mike Reed | 356f7c2 | 2017-01-10 11:58:39 -0500 | [diff] [blame] | 102 | } else if (allocator) { |
| 103 | hndl = allocator->allocBitmap(info, &bitmap); |
| 104 | if (!hndl) { |
| 105 | return nullptr; |
| 106 | } |
lsalzman | 04d975a | 2016-01-08 11:20:14 -0800 | [diff] [blame] | 107 | } else if (info.isOpaque()) { |
mtklein | cd49541 | 2015-11-05 09:46:23 -0800 | [diff] [blame] | 108 | // If this bitmap is opaque, we don't have any sensible default color, |
| 109 | // so we just return uninitialized pixels. |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 110 | if (!bitmap.tryAllocPixels(info)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 111 | return nullptr; |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 112 | } |
mtklein | cd49541 | 2015-11-05 09:46:23 -0800 | [diff] [blame] | 113 | } else { |
| 114 | // This bitmap has transparency, so we'll zero the pixels (to transparent). |
Mike Reed | 6b3155c | 2017-04-03 14:41:44 -0400 | [diff] [blame] | 115 | // We use the flag as a faster alloc-then-eraseColor(SK_ColorTRANSPARENT). |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 116 | if (!bitmap.tryAllocPixelsFlags(info, SkBitmap::kZeroPixels_AllocFlag)) { |
mtklein | cd49541 | 2015-11-05 09:46:23 -0800 | [diff] [blame] | 117 | return nullptr; |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
Mike Reed | 356f7c2 | 2017-01-10 11:58:39 -0500 | [diff] [blame] | 121 | return new SkBitmapDevice(bitmap, surfaceProps, hndl); |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void SkBitmapDevice::replaceBitmapBackendForRasterSurface(const SkBitmap& bm) { |
| 125 | SkASSERT(bm.width() == fBitmap.width()); |
| 126 | SkASSERT(bm.height() == fBitmap.height()); |
| 127 | fBitmap = bm; // intent is to use bm's pixelRef (and rowbytes/config) |
reed | 589a39e | 2016-08-20 07:59:19 -0700 | [diff] [blame] | 128 | this->privateResize(fBitmap.info().width(), fBitmap.info().height()); |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 129 | } |
| 130 | |
reed | 76033be | 2015-03-14 10:54:31 -0700 | [diff] [blame] | 131 | SkBaseDevice* SkBitmapDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) { |
robertphillips | 9a53fd7 | 2015-06-22 09:46:59 -0700 | [diff] [blame] | 132 | const SkSurfaceProps surfaceProps(this->surfaceProps().flags(), cinfo.fPixelGeometry); |
Mike Reed | 356f7c2 | 2017-01-10 11:58:39 -0500 | [diff] [blame] | 133 | return SkBitmapDevice::Create(cinfo.fInfo, surfaceProps, cinfo.fAllocator); |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 134 | } |
| 135 | |
reed | 884e97c | 2015-05-26 11:31:54 -0700 | [diff] [blame] | 136 | bool SkBitmapDevice::onAccessPixels(SkPixmap* pmap) { |
reed | b560b5c | 2016-07-24 12:30:34 -0700 | [diff] [blame] | 137 | if (this->onPeekPixels(pmap)) { |
reed | 6e76485 | 2015-06-05 14:11:32 -0700 | [diff] [blame] | 138 | fBitmap.notifyPixelsChanged(); |
| 139 | return true; |
| 140 | } |
| 141 | return false; |
reed | 9572a10 | 2015-05-26 19:22:17 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | bool SkBitmapDevice::onPeekPixels(SkPixmap* pmap) { |
reed | 884e97c | 2015-05-26 11:31:54 -0700 | [diff] [blame] | 145 | const SkImageInfo info = fBitmap.info(); |
| 146 | if (fBitmap.getPixels() && (kUnknown_SkColorType != info.colorType())) { |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 147 | pmap->reset(fBitmap.info(), fBitmap.getPixels(), fBitmap.rowBytes()); |
reed | 884e97c | 2015-05-26 11:31:54 -0700 | [diff] [blame] | 148 | return true; |
reed@google.com | 9c135db | 2014-03-12 18:28:35 +0000 | [diff] [blame] | 149 | } |
reed | 884e97c | 2015-05-26 11:31:54 -0700 | [diff] [blame] | 150 | return false; |
reed@google.com | 9c135db | 2014-03-12 18:28:35 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Mike Reed | 353196f | 2017-07-21 11:01:18 -0400 | [diff] [blame] | 153 | bool SkBitmapDevice::onWritePixels(const SkPixmap& pm, int x, int y) { |
reed@google.com | 0d30c51 | 2014-03-14 14:02:58 +0000 | [diff] [blame] | 154 | // since we don't stop creating un-pixeled devices yet, check for no pixels here |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 155 | if (nullptr == fBitmap.getPixels()) { |
reed@google.com | 0d30c51 | 2014-03-14 14:02:58 +0000 | [diff] [blame] | 156 | return false; |
| 157 | } |
| 158 | |
Mike Reed | 353196f | 2017-07-21 11:01:18 -0400 | [diff] [blame] | 159 | if (fBitmap.writePixels(pm, x, y)) { |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 160 | fBitmap.notifyPixelsChanged(); |
| 161 | return true; |
| 162 | } |
| 163 | return false; |
| 164 | } |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 165 | |
Mike Reed | 353196f | 2017-07-21 11:01:18 -0400 | [diff] [blame] | 166 | bool SkBitmapDevice::onReadPixels(const SkPixmap& pm, int x, int y) { |
| 167 | return fBitmap.readPixels(pm, x, y); |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 168 | } |
| 169 | |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 170 | /////////////////////////////////////////////////////////////////////////////// |
| 171 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 172 | class SkBitmapDevice::BDDraw : public SkDraw { |
Mike Reed | c42a1cd | 2017-02-14 14:25:14 -0500 | [diff] [blame] | 173 | public: |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 174 | BDDraw(SkBitmapDevice* dev) { |
| 175 | // we need fDst to be set, and if we're actually drawing, to dirty the genID |
| 176 | if (!dev->accessPixels(&fDst)) { |
| 177 | // NoDrawDevice uses us (why?) so we have to catch this case w/ no pixels |
| 178 | fDst.reset(dev->imageInfo(), nullptr, 0); |
| 179 | } |
| 180 | fMatrix = &dev->ctm(); |
| 181 | fRC = &dev->fRCStack.rc(); |
Mike Reed | c42a1cd | 2017-02-14 14:25:14 -0500 | [diff] [blame] | 182 | } |
| 183 | }; |
Mike Reed | c42a1cd | 2017-02-14 14:25:14 -0500 | [diff] [blame] | 184 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 185 | void SkBitmapDevice::drawPaint(const SkPaint& paint) { |
| 186 | BDDraw(this).drawPaint(paint); |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 189 | void SkBitmapDevice::drawPoints(SkCanvas::PointMode mode, size_t count, |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 190 | const SkPoint pts[], const SkPaint& paint) { |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 191 | BDDraw(this).drawPoints(mode, count, pts, paint, nullptr); |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 194 | void SkBitmapDevice::drawRect(const SkRect& r, const SkPaint& paint) { |
| 195 | BDDraw(this).drawRect(r, paint); |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 198 | void SkBitmapDevice::drawOval(const SkRect& oval, const SkPaint& paint) { |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 199 | SkPath path; |
| 200 | path.addOval(oval); |
| 201 | // call the VIRTUAL version, so any subclasses who do handle drawPath aren't |
| 202 | // required to override drawOval. |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 203 | this->drawPath(path, paint, nullptr, true); |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 206 | void SkBitmapDevice::drawRRect(const SkRRect& rrect, const SkPaint& paint) { |
robertphillips@google.com | 50a7600 | 2013-11-12 01:16:56 +0000 | [diff] [blame] | 207 | #ifdef SK_IGNORE_BLURRED_RRECT_OPT |
| 208 | SkPath path; |
| 209 | |
| 210 | path.addRRect(rrect); |
| 211 | // call the VIRTUAL version, so any subclasses who do handle drawPath aren't |
| 212 | // required to override drawRRect. |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 213 | this->drawPath(path, paint, nullptr, true); |
robertphillips@google.com | 50a7600 | 2013-11-12 01:16:56 +0000 | [diff] [blame] | 214 | #else |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 215 | BDDraw(this).drawRRect(rrect, paint); |
robertphillips@google.com | 50a7600 | 2013-11-12 01:16:56 +0000 | [diff] [blame] | 216 | #endif |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 219 | void SkBitmapDevice::drawPath(const SkPath& path, |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 220 | const SkPaint& paint, const SkMatrix* prePathMatrix, |
| 221 | bool pathIsMutable) { |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 222 | BDDraw(this).drawPath(path, paint, prePathMatrix, pathIsMutable); |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Hal Canary | b964238 | 2017-06-27 09:58:56 -0400 | [diff] [blame] | 225 | void SkBitmapDevice::drawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y, |
| 226 | const SkPaint& paint) { |
| 227 | SkMatrix matrix = SkMatrix::MakeTrans(x, y); |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 228 | LogDrawScaleFactor(SkMatrix::Concat(this->ctm(), matrix), paint.getFilterQuality()); |
| 229 | BDDraw(this).drawBitmap(bitmap, matrix, nullptr, paint); |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 230 | } |
| 231 | |
fmalita | ab83da7 | 2016-08-26 13:04:14 -0700 | [diff] [blame] | 232 | static inline bool CanApplyDstMatrixAsCTM(const SkMatrix& m, const SkPaint& paint) { |
| 233 | if (!paint.getMaskFilter()) { |
| 234 | return true; |
| 235 | } |
| 236 | |
| 237 | // Some mask filters parameters (sigma) depend on the CTM/scale. |
| 238 | return m.getType() <= SkMatrix::kTranslate_Mask; |
| 239 | } |
| 240 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 241 | void SkBitmapDevice::drawBitmapRect(const SkBitmap& bitmap, |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 242 | const SkRect* src, const SkRect& dst, |
reed | 562fe47 | 2015-07-28 07:35:14 -0700 | [diff] [blame] | 243 | const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) { |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 244 | SkMatrix matrix; |
| 245 | SkRect bitmapBounds, tmpSrc, tmpDst; |
| 246 | SkBitmap tmpBitmap; |
| 247 | |
| 248 | bitmapBounds.isetWH(bitmap.width(), bitmap.height()); |
| 249 | |
| 250 | // Compute matrix from the two rectangles |
| 251 | if (src) { |
| 252 | tmpSrc = *src; |
| 253 | } else { |
| 254 | tmpSrc = bitmapBounds; |
| 255 | } |
| 256 | matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit); |
| 257 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 258 | LogDrawScaleFactor(SkMatrix::Concat(this->ctm(), matrix), paint.getFilterQuality()); |
ericrk | 983294f | 2016-04-18 09:14:00 -0700 | [diff] [blame] | 259 | |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 260 | const SkRect* dstPtr = &dst; |
| 261 | const SkBitmap* bitmapPtr = &bitmap; |
| 262 | |
| 263 | // clip the tmpSrc to the bounds of the bitmap, and recompute dstRect if |
| 264 | // needed (if the src was clipped). No check needed if src==null. |
| 265 | if (src) { |
| 266 | if (!bitmapBounds.contains(*src)) { |
| 267 | if (!tmpSrc.intersect(bitmapBounds)) { |
| 268 | return; // nothing to draw |
| 269 | } |
| 270 | // recompute dst, based on the smaller tmpSrc |
| 271 | matrix.mapRect(&tmpDst, tmpSrc); |
| 272 | dstPtr = &tmpDst; |
| 273 | } |
reed | 27efa23 | 2016-07-25 19:10:10 -0700 | [diff] [blame] | 274 | } |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 275 | |
reed | 27efa23 | 2016-07-25 19:10:10 -0700 | [diff] [blame] | 276 | if (src && !src->contains(bitmapBounds) && |
| 277 | SkCanvas::kFast_SrcRectConstraint == constraint && |
| 278 | paint.getFilterQuality() != kNone_SkFilterQuality) { |
| 279 | // src is smaller than the bounds of the bitmap, and we are filtering, so we don't know |
| 280 | // how much more of the bitmap we need, so we can't use extractSubset or drawBitmap, |
| 281 | // but we must use a shader w/ dst bounds (which can access all of the bitmap needed). |
| 282 | goto USE_SHADER; |
| 283 | } |
| 284 | |
| 285 | if (src) { |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 286 | // since we may need to clamp to the borders of the src rect within |
| 287 | // the bitmap, we extract a subset. |
reed | b07a94f | 2014-11-19 05:03:18 -0800 | [diff] [blame] | 288 | const SkIRect srcIR = tmpSrc.roundOut(); |
bsalomon | 19fe41e | 2016-08-23 14:02:54 -0700 | [diff] [blame] | 289 | if (!bitmap.extractSubset(&tmpBitmap, srcIR)) { |
| 290 | return; |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 291 | } |
| 292 | bitmapPtr = &tmpBitmap; |
| 293 | |
| 294 | // Since we did an extract, we need to adjust the matrix accordingly |
| 295 | SkScalar dx = 0, dy = 0; |
| 296 | if (srcIR.fLeft > 0) { |
| 297 | dx = SkIntToScalar(srcIR.fLeft); |
| 298 | } |
| 299 | if (srcIR.fTop > 0) { |
| 300 | dy = SkIntToScalar(srcIR.fTop); |
| 301 | } |
| 302 | if (dx || dy) { |
| 303 | matrix.preTranslate(dx, dy); |
| 304 | } |
| 305 | |
lsalzman | c0e52f4 | 2017-04-05 21:42:54 -0700 | [diff] [blame] | 306 | #ifdef SK_DRAWBITMAPRECT_FAST_OFFSET |
| 307 | SkRect extractedBitmapBounds = SkRect::MakeXYWH(dx, dy, |
| 308 | SkIntToScalar(bitmapPtr->width()), |
| 309 | SkIntToScalar(bitmapPtr->height())); |
| 310 | #else |
mtklein | 65c74f1 | 2017-03-31 18:36:39 -0700 | [diff] [blame] | 311 | SkRect extractedBitmapBounds; |
| 312 | extractedBitmapBounds.isetWH(bitmapPtr->width(), bitmapPtr->height()); |
lsalzman | c0e52f4 | 2017-04-05 21:42:54 -0700 | [diff] [blame] | 313 | #endif |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 314 | if (extractedBitmapBounds == tmpSrc) { |
| 315 | // no fractional part in src, we can just call drawBitmap |
| 316 | goto USE_DRAWBITMAP; |
| 317 | } |
| 318 | } else { |
| 319 | USE_DRAWBITMAP: |
| 320 | // We can go faster by just calling drawBitmap, which will concat the |
| 321 | // matrix with the CTM, and try to call drawSprite if it can. If not, |
| 322 | // it will make a shader and call drawRect, as we do below. |
fmalita | ab83da7 | 2016-08-26 13:04:14 -0700 | [diff] [blame] | 323 | if (CanApplyDstMatrixAsCTM(matrix, paint)) { |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 324 | BDDraw(this).drawBitmap(*bitmapPtr, matrix, dstPtr, paint); |
fmalita | ab83da7 | 2016-08-26 13:04:14 -0700 | [diff] [blame] | 325 | return; |
| 326 | } |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 327 | } |
| 328 | |
reed | 27efa23 | 2016-07-25 19:10:10 -0700 | [diff] [blame] | 329 | USE_SHADER: |
reed | f77c47b | 2016-08-05 10:15:00 -0700 | [diff] [blame] | 330 | |
Herb Derby | 83e939b | 2017-02-07 14:25:11 -0500 | [diff] [blame] | 331 | // TODO(herb): Move this over to SkArenaAlloc when arena alloc has a facility to return sk_sps. |
reed | f77c47b | 2016-08-05 10:15:00 -0700 | [diff] [blame] | 332 | // Since the shader need only live for our stack-frame, pass in a custom allocator. This |
| 333 | // can save malloc calls, and signals to SkMakeBitmapShader to not try to copy the bitmap |
| 334 | // if its mutable, since that precaution is not needed (give the short lifetime of the shader). |
Herb Derby | bfdc87a | 2017-02-14 15:06:23 +0000 | [diff] [blame] | 335 | |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 336 | // construct a shader, so we can call drawRect with the dst |
reed | f77c47b | 2016-08-05 10:15:00 -0700 | [diff] [blame] | 337 | auto s = SkMakeBitmapShader(*bitmapPtr, SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, |
Herb Derby | bfdc87a | 2017-02-14 15:06:23 +0000 | [diff] [blame] | 338 | &matrix, kNever_SkCopyPixelsMode); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 339 | if (!s) { |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 340 | return; |
| 341 | } |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 342 | |
| 343 | SkPaint paintWithShader(paint); |
| 344 | paintWithShader.setStyle(SkPaint::kFill_Style); |
reed | f77c47b | 2016-08-05 10:15:00 -0700 | [diff] [blame] | 345 | paintWithShader.setShader(s); |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 346 | |
| 347 | // Call ourself, in case the subclass wanted to share this setup code |
| 348 | // but handle the drawRect code themselves. |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 349 | this->drawRect(*dstPtr, paintWithShader); |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Mike Reed | 2f6b5a4 | 2017-03-19 15:04:17 -0400 | [diff] [blame] | 352 | void SkBitmapDevice::drawSprite(const SkBitmap& bitmap, int x, int y, const SkPaint& paint) { |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 353 | BDDraw(this).drawSprite(bitmap, x, y, paint); |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 356 | void SkBitmapDevice::drawText(const void* text, size_t len, |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 357 | SkScalar x, SkScalar y, const SkPaint& paint) { |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 358 | BDDraw(this).drawText((const char*)text, len, x, y, paint, &fSurfaceProps); |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 361 | void SkBitmapDevice::drawPosText(const void* text, size_t len, const SkScalar xpos[], |
| 362 | int scalarsPerPos, const SkPoint& offset, const SkPaint& paint) { |
Mike Reed | 2f6b5a4 | 2017-03-19 15:04:17 -0400 | [diff] [blame] | 363 | BDDraw(this).drawPosText((const char*)text, len, xpos, scalarsPerPos, offset, paint, |
| 364 | &fSurfaceProps); |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Mike Reed | 2f6b5a4 | 2017-03-19 15:04:17 -0400 | [diff] [blame] | 367 | void SkBitmapDevice::drawVertices(const SkVertices* vertices, SkBlendMode bmode, |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 368 | const SkPaint& paint) { |
Mike Reed | 2f6b5a4 | 2017-03-19 15:04:17 -0400 | [diff] [blame] | 369 | BDDraw(this).drawVertices(vertices->mode(), vertices->vertexCount(), vertices->positions(), |
| 370 | vertices->texCoords(), vertices->colors(), bmode, |
| 371 | vertices->indices(), vertices->indexCount(), paint); |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Mike Reed | 2f6b5a4 | 2017-03-19 15:04:17 -0400 | [diff] [blame] | 374 | void SkBitmapDevice::drawDevice(SkBaseDevice* device, int x, int y, const SkPaint& paint) { |
reed | cf5c846 | 2016-07-20 12:28:40 -0700 | [diff] [blame] | 375 | SkASSERT(!paint.getImageFilter()); |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 376 | BDDraw(this).drawSprite(static_cast<SkBitmapDevice*>(device)->fBitmap, x, y, paint); |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 377 | } |
| 378 | |
reed | e51c356 | 2016-07-19 14:33:20 -0700 | [diff] [blame] | 379 | /////////////////////////////////////////////////////////////////////////////// |
| 380 | |
Florin Malita | 87f8071 | 2017-05-10 15:26:44 -0400 | [diff] [blame] | 381 | namespace { |
| 382 | |
| 383 | class SkAutoDeviceClipRestore { |
| 384 | public: |
| 385 | SkAutoDeviceClipRestore(SkBaseDevice* device, const SkIRect& clip) |
| 386 | : fDevice(device) |
| 387 | , fPrevCTM(device->ctm()) { |
| 388 | fDevice->save(); |
| 389 | fDevice->setCTM(SkMatrix::I()); |
| 390 | fDevice->clipRect(SkRect::Make(clip), SkClipOp::kIntersect, false); |
| 391 | fDevice->setCTM(fPrevCTM); |
| 392 | } |
| 393 | |
| 394 | ~SkAutoDeviceClipRestore() { |
| 395 | fDevice->restore(fPrevCTM); |
| 396 | } |
| 397 | |
| 398 | private: |
| 399 | SkBaseDevice* fDevice; |
| 400 | const SkMatrix fPrevCTM; |
| 401 | }; |
| 402 | |
| 403 | } // anonymous ns |
| 404 | |
Florin Malita | 53f77bd | 2017-04-28 13:48:37 -0400 | [diff] [blame] | 405 | void SkBitmapDevice::drawSpecial(SkSpecialImage* src, int x, int y, const SkPaint& origPaint, |
| 406 | SkImage* clipImage, const SkMatrix& clipMatrix) { |
| 407 | SkASSERT(!src->isTextureBacked()); |
reed | e51c356 | 2016-07-19 14:33:20 -0700 | [diff] [blame] | 408 | |
Florin Malita | 53f77bd | 2017-04-28 13:48:37 -0400 | [diff] [blame] | 409 | sk_sp<SkSpecialImage> filteredImage; |
| 410 | SkTCopyOnFirstWrite<SkPaint> paint(origPaint); |
reed | e51c356 | 2016-07-19 14:33:20 -0700 | [diff] [blame] | 411 | |
Florin Malita | 53f77bd | 2017-04-28 13:48:37 -0400 | [diff] [blame] | 412 | if (SkImageFilter* filter = paint->getImageFilter()) { |
reed | e51c356 | 2016-07-19 14:33:20 -0700 | [diff] [blame] | 413 | SkIPoint offset = SkIPoint::Make(0, 0); |
Florin Malita | 53f77bd | 2017-04-28 13:48:37 -0400 | [diff] [blame] | 414 | const SkMatrix matrix = SkMatrix::Concat( |
| 415 | SkMatrix::MakeTrans(SkIntToScalar(-x), SkIntToScalar(-y)), this->ctm()); |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 416 | const SkIRect clipBounds = fRCStack.rc().getBounds().makeOffset(-x, -y); |
Hal Canary | 704cd32 | 2016-11-07 14:13:52 -0500 | [diff] [blame] | 417 | sk_sp<SkImageFilterCache> cache(this->getImageFilterCache()); |
brianosman | 2a75e5d | 2016-09-22 07:15:37 -0700 | [diff] [blame] | 418 | SkImageFilter::OutputProperties outputProperties(fBitmap.colorSpace()); |
| 419 | SkImageFilter::Context ctx(matrix, clipBounds, cache.get(), outputProperties); |
liyuqian | 2add0ff | 2016-10-20 11:04:39 -0700 | [diff] [blame] | 420 | |
Florin Malita | 53f77bd | 2017-04-28 13:48:37 -0400 | [diff] [blame] | 421 | filteredImage = filter->filterImage(src, ctx, &offset); |
| 422 | if (!filteredImage) { |
| 423 | return; |
reed | e51c356 | 2016-07-19 14:33:20 -0700 | [diff] [blame] | 424 | } |
Florin Malita | 53f77bd | 2017-04-28 13:48:37 -0400 | [diff] [blame] | 425 | |
| 426 | src = filteredImage.get(); |
| 427 | paint.writable()->setImageFilter(nullptr); |
| 428 | x += offset.x(); |
| 429 | y += offset.y(); |
reed | e51c356 | 2016-07-19 14:33:20 -0700 | [diff] [blame] | 430 | } |
Florin Malita | 53f77bd | 2017-04-28 13:48:37 -0400 | [diff] [blame] | 431 | |
| 432 | if (!clipImage) { |
| 433 | SkBitmap resultBM; |
| 434 | if (src->getROPixels(&resultBM)) { |
| 435 | this->drawSprite(resultBM, x, y, *paint); |
| 436 | } |
| 437 | return; |
| 438 | } |
| 439 | |
| 440 | // Clip image case. |
| 441 | sk_sp<SkImage> srcImage(src->asImage()); |
| 442 | if (!srcImage) { |
| 443 | return; |
| 444 | } |
| 445 | |
| 446 | const SkMatrix totalMatrix = SkMatrix::Concat(this->ctm(), clipMatrix); |
Florin Malita | 53f77bd | 2017-04-28 13:48:37 -0400 | [diff] [blame] | 447 | SkRect clipBounds; |
| 448 | totalMatrix.mapRect(&clipBounds, SkRect::Make(clipImage->bounds())); |
| 449 | const SkIRect srcBounds = srcImage->bounds().makeOffset(x, y); |
| 450 | |
| 451 | SkIRect maskBounds = fRCStack.rc().getBounds(); |
| 452 | if (!maskBounds.intersect(clipBounds.roundOut()) || !maskBounds.intersect(srcBounds)) { |
| 453 | return; |
| 454 | } |
| 455 | |
Florin Malita | 87f8071 | 2017-05-10 15:26:44 -0400 | [diff] [blame] | 456 | sk_sp<SkImage> mask; |
| 457 | SkMatrix maskMatrix, shaderMatrix; |
| 458 | SkTLazy<SkAutoDeviceClipRestore> autoClipRestore; |
Florin Malita | 53f77bd | 2017-04-28 13:48:37 -0400 | [diff] [blame] | 459 | |
Florin Malita | 87f8071 | 2017-05-10 15:26:44 -0400 | [diff] [blame] | 460 | SkMatrix totalInverse; |
| 461 | if (clipImage->isAlphaOnly() && totalMatrix.invert(&totalInverse)) { |
| 462 | // If the mask is already in A8 format, we can draw it directly |
| 463 | // (while compensating in the shader matrix). |
| 464 | mask = sk_ref_sp(clipImage); |
| 465 | maskMatrix = totalMatrix; |
| 466 | shaderMatrix = SkMatrix::Concat(totalInverse, SkMatrix::MakeTrans(x, y)); |
Florin Malita | 53f77bd | 2017-04-28 13:48:37 -0400 | [diff] [blame] | 467 | |
Florin Malita | 87f8071 | 2017-05-10 15:26:44 -0400 | [diff] [blame] | 468 | // If the mask is not fully contained within the src layer, we must clip. |
| 469 | if (!srcBounds.contains(clipBounds)) { |
| 470 | autoClipRestore.init(this, srcBounds); |
| 471 | } |
| 472 | |
| 473 | maskBounds.offsetTo(0, 0); |
| 474 | } else { |
| 475 | // Otherwise, we convert the mask to A8 explicitly. |
| 476 | sk_sp<SkSurface> surf = SkSurface::MakeRaster(SkImageInfo::MakeA8(maskBounds.width(), |
| 477 | maskBounds.height())); |
| 478 | SkCanvas* canvas = surf->getCanvas(); |
| 479 | canvas->translate(-maskBounds.x(), -maskBounds.y()); |
| 480 | canvas->concat(totalMatrix); |
| 481 | canvas->drawImage(clipImage, 0, 0); |
| 482 | |
| 483 | mask = surf->makeImageSnapshot(); |
| 484 | maskMatrix = SkMatrix::I(); |
| 485 | shaderMatrix = SkMatrix::MakeTrans(x - maskBounds.x(), y - maskBounds.y()); |
| 486 | } |
| 487 | |
| 488 | SkAutoDeviceCTMRestore adctmr(this, maskMatrix); |
| 489 | paint.writable()->setShader(srcImage->makeShader(&shaderMatrix)); |
Florin Malita | 53f77bd | 2017-04-28 13:48:37 -0400 | [diff] [blame] | 490 | this->drawImage(mask.get(), maskBounds.x(), maskBounds.y(), *paint); |
reed | e51c356 | 2016-07-19 14:33:20 -0700 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | sk_sp<SkSpecialImage> SkBitmapDevice::makeSpecial(const SkBitmap& bitmap) { |
| 494 | return SkSpecialImage::MakeFromRaster(bitmap.bounds(), bitmap); |
| 495 | } |
| 496 | |
| 497 | sk_sp<SkSpecialImage> SkBitmapDevice::makeSpecial(const SkImage* image) { |
| 498 | return SkSpecialImage::MakeFromImage(SkIRect::MakeWH(image->width(), image->height()), |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 499 | image->makeNonTextureImage(), fBitmap.colorSpace()); |
reed | e51c356 | 2016-07-19 14:33:20 -0700 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | sk_sp<SkSpecialImage> SkBitmapDevice::snapSpecial() { |
| 503 | return this->makeSpecial(fBitmap); |
| 504 | } |
| 505 | |
| 506 | /////////////////////////////////////////////////////////////////////////////// |
| 507 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 508 | sk_sp<SkSurface> SkBitmapDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) { |
| 509 | return SkSurface::MakeRaster(info, &props); |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 510 | } |
| 511 | |
senorblanco | 900c367 | 2016-04-27 11:31:23 -0700 | [diff] [blame] | 512 | SkImageFilterCache* SkBitmapDevice::getImageFilterCache() { |
| 513 | SkImageFilterCache* cache = SkImageFilterCache::Get(); |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 514 | cache->ref(); |
| 515 | return cache; |
| 516 | } |
| 517 | |
Mike Reed | 7627fa5 | 2017-02-08 10:07:53 -0500 | [diff] [blame] | 518 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 519 | |
reed | b2db898 | 2014-11-13 12:41:02 -0800 | [diff] [blame] | 520 | bool SkBitmapDevice::onShouldDisableLCD(const SkPaint& paint) const { |
commit-bot@chromium.org | cba7378 | 2014-05-29 15:57:47 +0000 | [diff] [blame] | 521 | if (kN32_SkColorType != fBitmap.colorType() || |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 522 | paint.getRasterizer() || |
| 523 | paint.getPathEffect() || |
| 524 | paint.isFakeBoldText() || |
| 525 | paint.getStyle() != SkPaint::kFill_Style || |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 526 | !paint.isSrcOver()) |
reed | b2db898 | 2014-11-13 12:41:02 -0800 | [diff] [blame] | 527 | { |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 528 | return true; |
| 529 | } |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 530 | return false; |
| 531 | } |
Mike Reed | 7627fa5 | 2017-02-08 10:07:53 -0500 | [diff] [blame] | 532 | |
| 533 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 534 | |
| 535 | void SkBitmapDevice::onSave() { |
Mike Reed | c42a1cd | 2017-02-14 14:25:14 -0500 | [diff] [blame] | 536 | fRCStack.save(); |
Mike Reed | 7627fa5 | 2017-02-08 10:07:53 -0500 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | void SkBitmapDevice::onRestore() { |
Mike Reed | c42a1cd | 2017-02-14 14:25:14 -0500 | [diff] [blame] | 540 | fRCStack.restore(); |
Mike Reed | 7627fa5 | 2017-02-08 10:07:53 -0500 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | void SkBitmapDevice::onClipRect(const SkRect& rect, SkClipOp op, bool aa) { |
Mike Reed | c42a1cd | 2017-02-14 14:25:14 -0500 | [diff] [blame] | 544 | fRCStack.clipRect(this->ctm(), rect, op, aa); |
Mike Reed | 7627fa5 | 2017-02-08 10:07:53 -0500 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | void SkBitmapDevice::onClipRRect(const SkRRect& rrect, SkClipOp op, bool aa) { |
Mike Reed | c42a1cd | 2017-02-14 14:25:14 -0500 | [diff] [blame] | 548 | fRCStack.clipRRect(this->ctm(), rrect, op, aa); |
Mike Reed | 7627fa5 | 2017-02-08 10:07:53 -0500 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | void SkBitmapDevice::onClipPath(const SkPath& path, SkClipOp op, bool aa) { |
Mike Reed | c42a1cd | 2017-02-14 14:25:14 -0500 | [diff] [blame] | 552 | fRCStack.clipPath(this->ctm(), path, op, aa); |
Mike Reed | 7627fa5 | 2017-02-08 10:07:53 -0500 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | void SkBitmapDevice::onClipRegion(const SkRegion& rgn, SkClipOp op) { |
Mike Reed | c42a1cd | 2017-02-14 14:25:14 -0500 | [diff] [blame] | 556 | SkIPoint origin = this->getOrigin(); |
| 557 | SkRegion tmp; |
| 558 | const SkRegion* ptr = &rgn; |
| 559 | if (origin.fX | origin.fY) { |
| 560 | // translate from "global/canvas" coordinates to relative to this device |
| 561 | rgn.translate(-origin.fX, -origin.fY, &tmp); |
| 562 | ptr = &tmp; |
| 563 | } |
| 564 | fRCStack.clipRegion(*ptr, op); |
| 565 | } |
| 566 | |
| 567 | void SkBitmapDevice::onSetDeviceClipRestriction(SkIRect* mutableClipRestriction) { |
| 568 | fRCStack.setDeviceClipRestriction(mutableClipRestriction); |
Mike Reed | d519d48 | 2017-02-16 11:04:52 -0500 | [diff] [blame] | 569 | if (!mutableClipRestriction->isEmpty()) { |
| 570 | SkRegion rgn(*mutableClipRestriction); |
| 571 | fRCStack.clipRegion(rgn, SkClipOp::kIntersect); |
| 572 | } |
Mike Reed | c42a1cd | 2017-02-14 14:25:14 -0500 | [diff] [blame] | 573 | } |
| 574 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 575 | bool SkBitmapDevice::onClipIsAA() const { |
| 576 | const SkRasterClip& rc = fRCStack.rc(); |
| 577 | return !rc.isEmpty() && rc.isAA(); |
| 578 | } |
| 579 | |
| 580 | void SkBitmapDevice::onAsRgnClip(SkRegion* rgn) const { |
| 581 | const SkRasterClip& rc = fRCStack.rc(); |
| 582 | if (rc.isAA()) { |
| 583 | rgn->setRect(rc.getBounds()); |
| 584 | } else { |
| 585 | *rgn = rc.bwRgn(); |
| 586 | } |
| 587 | } |
| 588 | |
Mike Reed | c42a1cd | 2017-02-14 14:25:14 -0500 | [diff] [blame] | 589 | void SkBitmapDevice::validateDevBounds(const SkIRect& drawClipBounds) { |
| 590 | #ifdef SK_DEBUG |
| 591 | const SkIRect& stackBounds = fRCStack.rc().getBounds(); |
| 592 | SkASSERT(drawClipBounds == stackBounds); |
| 593 | #endif |
Mike Reed | 7627fa5 | 2017-02-08 10:07:53 -0500 | [diff] [blame] | 594 | } |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 595 | |
| 596 | SkBaseDevice::ClipType SkBitmapDevice::onGetClipType() const { |
| 597 | const SkRasterClip& rc = fRCStack.rc(); |
| 598 | if (rc.isEmpty()) { |
| 599 | return kEmpty_ClipType; |
| 600 | } else if (rc.isRect()) { |
| 601 | return kRect_ClipType; |
| 602 | } else { |
| 603 | return kComplex_ClipType; |
| 604 | } |
| 605 | } |