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 "SkConfig8888.h" |
| 10 | #include "SkDraw.h" |
reed | e51c356 | 2016-07-19 14:33:20 -0700 | [diff] [blame] | 11 | #include "SkImageFilter.h" |
senorblanco | 900c367 | 2016-04-27 11:31:23 -0700 | [diff] [blame] | 12 | #include "SkImageFilterCache.h" |
mtklein | cd49541 | 2015-11-05 09:46:23 -0800 | [diff] [blame] | 13 | #include "SkMallocPixelRef.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 14 | #include "SkMatrix.h" |
| 15 | #include "SkPaint.h" |
| 16 | #include "SkPath.h" |
reed | 884e97c | 2015-05-26 11:31:54 -0700 | [diff] [blame] | 17 | #include "SkPixelRef.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 18 | #include "SkPixmap.h" |
reed | e51c356 | 2016-07-19 14:33:20 -0700 | [diff] [blame] | 19 | #include "SkRasterClip.h" |
Mike Reed | 356f7c2 | 2017-01-10 11:58:39 -0500 | [diff] [blame] | 20 | #include "SkRasterHandleAllocator.h" |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 21 | #include "SkShader.h" |
reed | e51c356 | 2016-07-19 14:33:20 -0700 | [diff] [blame] | 22 | #include "SkSpecialImage.h" |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 23 | #include "SkSurface.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 24 | |
| 25 | class SkColorTable; |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 26 | |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 27 | static bool valid_for_bitmap_device(const SkImageInfo& info, |
| 28 | SkAlphaType* newAlphaType) { |
| 29 | if (info.width() < 0 || info.height() < 0) { |
| 30 | return false; |
| 31 | } |
| 32 | |
| 33 | // TODO: can we stop supporting kUnknown in SkBitmkapDevice? |
| 34 | if (kUnknown_SkColorType == info.colorType()) { |
| 35 | if (newAlphaType) { |
reed | 4497748 | 2015-02-27 10:23:00 -0800 | [diff] [blame] | 36 | *newAlphaType = kUnknown_SkAlphaType; |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 37 | } |
| 38 | return true; |
| 39 | } |
skia.committer@gmail.com | 969588f | 2014-02-16 03:01:56 +0000 | [diff] [blame] | 40 | |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 41 | switch (info.alphaType()) { |
| 42 | case kPremul_SkAlphaType: |
| 43 | case kOpaque_SkAlphaType: |
| 44 | break; |
| 45 | default: |
| 46 | return false; |
| 47 | } |
| 48 | |
| 49 | SkAlphaType canonicalAlphaType = info.alphaType(); |
| 50 | |
| 51 | switch (info.colorType()) { |
| 52 | case kAlpha_8_SkColorType: |
| 53 | break; |
| 54 | case kRGB_565_SkColorType: |
| 55 | canonicalAlphaType = kOpaque_SkAlphaType; |
| 56 | break; |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 57 | case kN32_SkColorType: |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 58 | break; |
reed | a34be68 | 2016-02-15 07:48:35 -0800 | [diff] [blame] | 59 | case kRGBA_F16_SkColorType: |
| 60 | break; |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 61 | default: |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | if (newAlphaType) { |
| 66 | *newAlphaType = canonicalAlphaType; |
| 67 | } |
| 68 | return true; |
| 69 | } |
| 70 | |
robertphillips | 9a53fd7 | 2015-06-22 09:46:59 -0700 | [diff] [blame] | 71 | SkBitmapDevice::SkBitmapDevice(const SkBitmap& bitmap) |
reed | 589a39e | 2016-08-20 07:59:19 -0700 | [diff] [blame] | 72 | : INHERITED(bitmap.info(), SkSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType)) |
reed | 2c9e200 | 2016-07-25 08:05:22 -0700 | [diff] [blame] | 73 | , fBitmap(bitmap) |
| 74 | { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 75 | SkASSERT(valid_for_bitmap_device(bitmap.info(), nullptr)); |
reed | 2c9e200 | 2016-07-25 08:05:22 -0700 | [diff] [blame] | 76 | fBitmap.lockPixels(); |
Mike Reed | 7627fa5 | 2017-02-08 10:07:53 -0500 | [diff] [blame] | 77 | |
| 78 | fRCStack.push_back().setRect(SkIRect::MakeWH(bitmap.width(), bitmap.height())); |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 79 | } |
| 80 | |
robertphillips | 9a53fd7 | 2015-06-22 09:46:59 -0700 | [diff] [blame] | 81 | SkBitmapDevice* SkBitmapDevice::Create(const SkImageInfo& info) { |
| 82 | return Create(info, SkSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType)); |
| 83 | } |
| 84 | |
Mike Reed | 356f7c2 | 2017-01-10 11:58:39 -0500 | [diff] [blame] | 85 | SkBitmapDevice::SkBitmapDevice(const SkBitmap& bitmap, const SkSurfaceProps& surfaceProps, |
| 86 | SkRasterHandleAllocator::Handle hndl) |
reed | 589a39e | 2016-08-20 07:59:19 -0700 | [diff] [blame] | 87 | : INHERITED(bitmap.info(), surfaceProps) |
reed | 2c9e200 | 2016-07-25 08:05:22 -0700 | [diff] [blame] | 88 | , fBitmap(bitmap) |
Mike Reed | 356f7c2 | 2017-01-10 11:58:39 -0500 | [diff] [blame] | 89 | , fRasterHandle(hndl) |
reed | 2c9e200 | 2016-07-25 08:05:22 -0700 | [diff] [blame] | 90 | { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 91 | SkASSERT(valid_for_bitmap_device(bitmap.info(), nullptr)); |
reed | 2c9e200 | 2016-07-25 08:05:22 -0700 | [diff] [blame] | 92 | fBitmap.lockPixels(); |
Mike Reed | 7627fa5 | 2017-02-08 10:07:53 -0500 | [diff] [blame] | 93 | |
| 94 | fRCStack.push_back().setRect(SkIRect::MakeWH(bitmap.width(), bitmap.height())); |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 95 | } |
| 96 | |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 97 | SkBitmapDevice* SkBitmapDevice::Create(const SkImageInfo& origInfo, |
Mike Reed | 356f7c2 | 2017-01-10 11:58:39 -0500 | [diff] [blame] | 98 | const SkSurfaceProps& surfaceProps, |
| 99 | SkRasterHandleAllocator* allocator) { |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 100 | SkAlphaType newAT = origInfo.alphaType(); |
| 101 | if (!valid_for_bitmap_device(origInfo, &newAT)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 102 | return nullptr; |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 103 | } |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 104 | |
Mike Reed | 356f7c2 | 2017-01-10 11:58:39 -0500 | [diff] [blame] | 105 | SkRasterHandleAllocator::Handle hndl = nullptr; |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 106 | const SkImageInfo info = origInfo.makeAlphaType(newAT); |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 107 | SkBitmap bitmap; |
| 108 | |
| 109 | if (kUnknown_SkColorType == info.colorType()) { |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 110 | if (!bitmap.setInfo(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 | } |
Mike Reed | 356f7c2 | 2017-01-10 11:58:39 -0500 | [diff] [blame] | 113 | } else if (allocator) { |
| 114 | hndl = allocator->allocBitmap(info, &bitmap); |
| 115 | if (!hndl) { |
| 116 | return nullptr; |
| 117 | } |
lsalzman | 04d975a | 2016-01-08 11:20:14 -0800 | [diff] [blame] | 118 | } else if (info.isOpaque()) { |
mtklein | cd49541 | 2015-11-05 09:46:23 -0800 | [diff] [blame] | 119 | // If this bitmap is opaque, we don't have any sensible default color, |
| 120 | // so we just return uninitialized pixels. |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 121 | if (!bitmap.tryAllocPixels(info)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 122 | return nullptr; |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 123 | } |
mtklein | cd49541 | 2015-11-05 09:46:23 -0800 | [diff] [blame] | 124 | } else { |
| 125 | // This bitmap has transparency, so we'll zero the pixels (to transparent). |
| 126 | // We use a ZeroedPRFactory as a faster alloc-then-eraseColor(SK_ColorTRANSPARENT). |
| 127 | SkMallocPixelRef::ZeroedPRFactory factory; |
| 128 | if (!bitmap.tryAllocPixels(info, &factory, nullptr/*color table*/)) { |
| 129 | return nullptr; |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
Mike Reed | 356f7c2 | 2017-01-10 11:58:39 -0500 | [diff] [blame] | 133 | return new SkBitmapDevice(bitmap, surfaceProps, hndl); |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 134 | } |
| 135 | |
mtklein | feaadee | 2015-04-08 11:25:48 -0700 | [diff] [blame] | 136 | void SkBitmapDevice::setNewSize(const SkISize& size) { |
| 137 | SkASSERT(!fBitmap.pixelRef()); |
| 138 | fBitmap.setInfo(fBitmap.info().makeWH(size.fWidth, size.fHeight)); |
reed | 589a39e | 2016-08-20 07:59:19 -0700 | [diff] [blame] | 139 | this->privateResize(fBitmap.info().width(), fBitmap.info().height()); |
mtklein | feaadee | 2015-04-08 11:25:48 -0700 | [diff] [blame] | 140 | } |
| 141 | |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 142 | void SkBitmapDevice::replaceBitmapBackendForRasterSurface(const SkBitmap& bm) { |
| 143 | SkASSERT(bm.width() == fBitmap.width()); |
| 144 | SkASSERT(bm.height() == fBitmap.height()); |
| 145 | fBitmap = bm; // intent is to use bm's pixelRef (and rowbytes/config) |
| 146 | fBitmap.lockPixels(); |
reed | 589a39e | 2016-08-20 07:59:19 -0700 | [diff] [blame] | 147 | this->privateResize(fBitmap.info().width(), fBitmap.info().height()); |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 148 | } |
| 149 | |
reed | 76033be | 2015-03-14 10:54:31 -0700 | [diff] [blame] | 150 | SkBaseDevice* SkBitmapDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) { |
robertphillips | 9a53fd7 | 2015-06-22 09:46:59 -0700 | [diff] [blame] | 151 | const SkSurfaceProps surfaceProps(this->surfaceProps().flags(), cinfo.fPixelGeometry); |
Mike Reed | 356f7c2 | 2017-01-10 11:58:39 -0500 | [diff] [blame] | 152 | return SkBitmapDevice::Create(cinfo.fInfo, surfaceProps, cinfo.fAllocator); |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 153 | } |
| 154 | |
reed | 884e97c | 2015-05-26 11:31:54 -0700 | [diff] [blame] | 155 | bool SkBitmapDevice::onAccessPixels(SkPixmap* pmap) { |
reed | b560b5c | 2016-07-24 12:30:34 -0700 | [diff] [blame] | 156 | if (this->onPeekPixels(pmap)) { |
reed | 6e76485 | 2015-06-05 14:11:32 -0700 | [diff] [blame] | 157 | fBitmap.notifyPixelsChanged(); |
| 158 | return true; |
| 159 | } |
| 160 | return false; |
reed | 9572a10 | 2015-05-26 19:22:17 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | bool SkBitmapDevice::onPeekPixels(SkPixmap* pmap) { |
reed | 884e97c | 2015-05-26 11:31:54 -0700 | [diff] [blame] | 164 | const SkImageInfo info = fBitmap.info(); |
| 165 | if (fBitmap.getPixels() && (kUnknown_SkColorType != info.colorType())) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 166 | SkColorTable* ctable = nullptr; |
reed | 884e97c | 2015-05-26 11:31:54 -0700 | [diff] [blame] | 167 | pmap->reset(fBitmap.info(), fBitmap.getPixels(), fBitmap.rowBytes(), ctable); |
| 168 | return true; |
reed@google.com | 9c135db | 2014-03-12 18:28:35 +0000 | [diff] [blame] | 169 | } |
reed | 884e97c | 2015-05-26 11:31:54 -0700 | [diff] [blame] | 170 | return false; |
reed@google.com | 9c135db | 2014-03-12 18:28:35 +0000 | [diff] [blame] | 171 | } |
| 172 | |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 173 | bool SkBitmapDevice::onWritePixels(const SkImageInfo& srcInfo, const void* srcPixels, |
| 174 | size_t srcRowBytes, int x, int y) { |
reed@google.com | 0d30c51 | 2014-03-14 14:02:58 +0000 | [diff] [blame] | 175 | // 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] | 176 | if (nullptr == fBitmap.getPixels()) { |
reed@google.com | 0d30c51 | 2014-03-14 14:02:58 +0000 | [diff] [blame] | 177 | return false; |
| 178 | } |
| 179 | |
Mike Reed | 68dd8d0 | 2017-01-04 16:34:31 -0500 | [diff] [blame] | 180 | if (fBitmap.writePixels(SkPixmap(srcInfo, srcPixels, srcRowBytes), x, y)) { |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 181 | fBitmap.notifyPixelsChanged(); |
| 182 | return true; |
| 183 | } |
| 184 | return false; |
| 185 | } |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 186 | |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 187 | bool SkBitmapDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, |
| 188 | int x, int y) { |
reed | b184f7f | 2014-07-13 04:32:32 -0700 | [diff] [blame] | 189 | return fBitmap.readPixels(dstInfo, dstPixels, dstRowBytes, x, y); |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 190 | } |
| 191 | |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 192 | /////////////////////////////////////////////////////////////////////////////// |
| 193 | |
| 194 | void SkBitmapDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) { |
| 195 | draw.drawPaint(paint); |
| 196 | } |
| 197 | |
| 198 | void SkBitmapDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode, size_t count, |
| 199 | const SkPoint pts[], const SkPaint& paint) { |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 200 | draw.drawPoints(mode, count, pts, paint); |
| 201 | } |
| 202 | |
| 203 | void SkBitmapDevice::drawRect(const SkDraw& draw, const SkRect& r, const SkPaint& paint) { |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 204 | draw.drawRect(r, paint); |
| 205 | } |
| 206 | |
| 207 | void SkBitmapDevice::drawOval(const SkDraw& draw, const SkRect& oval, const SkPaint& paint) { |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 208 | SkPath path; |
| 209 | path.addOval(oval); |
| 210 | // call the VIRTUAL version, so any subclasses who do handle drawPath aren't |
| 211 | // required to override drawOval. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 212 | this->drawPath(draw, path, paint, nullptr, true); |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | void SkBitmapDevice::drawRRect(const SkDraw& draw, const SkRRect& rrect, const SkPaint& paint) { |
robertphillips@google.com | 50a7600 | 2013-11-12 01:16:56 +0000 | [diff] [blame] | 216 | #ifdef SK_IGNORE_BLURRED_RRECT_OPT |
| 217 | SkPath path; |
| 218 | |
| 219 | path.addRRect(rrect); |
| 220 | // call the VIRTUAL version, so any subclasses who do handle drawPath aren't |
| 221 | // required to override drawRRect. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 222 | this->drawPath(draw, path, paint, nullptr, true); |
robertphillips@google.com | 50a7600 | 2013-11-12 01:16:56 +0000 | [diff] [blame] | 223 | #else |
scroggo@google.com | a8e33a9 | 2013-11-08 18:02:53 +0000 | [diff] [blame] | 224 | draw.drawRRect(rrect, paint); |
robertphillips@google.com | 50a7600 | 2013-11-12 01:16:56 +0000 | [diff] [blame] | 225 | #endif |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | void SkBitmapDevice::drawPath(const SkDraw& draw, const SkPath& path, |
| 229 | const SkPaint& paint, const SkMatrix* prePathMatrix, |
| 230 | bool pathIsMutable) { |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 231 | draw.drawPath(path, paint, prePathMatrix, pathIsMutable); |
| 232 | } |
| 233 | |
| 234 | void SkBitmapDevice::drawBitmap(const SkDraw& draw, const SkBitmap& bitmap, |
| 235 | const SkMatrix& matrix, const SkPaint& paint) { |
ericrk | 983294f | 2016-04-18 09:14:00 -0700 | [diff] [blame] | 236 | LogDrawScaleFactor(SkMatrix::Concat(*draw.fMatrix, matrix), paint.getFilterQuality()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 237 | draw.drawBitmap(bitmap, matrix, nullptr, paint); |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 238 | } |
| 239 | |
fmalita | ab83da7 | 2016-08-26 13:04:14 -0700 | [diff] [blame] | 240 | static inline bool CanApplyDstMatrixAsCTM(const SkMatrix& m, const SkPaint& paint) { |
| 241 | if (!paint.getMaskFilter()) { |
| 242 | return true; |
| 243 | } |
| 244 | |
| 245 | // Some mask filters parameters (sigma) depend on the CTM/scale. |
| 246 | return m.getType() <= SkMatrix::kTranslate_Mask; |
| 247 | } |
| 248 | |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 249 | void SkBitmapDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap, |
| 250 | const SkRect* src, const SkRect& dst, |
reed | 562fe47 | 2015-07-28 07:35:14 -0700 | [diff] [blame] | 251 | const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) { |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 252 | SkMatrix matrix; |
| 253 | SkRect bitmapBounds, tmpSrc, tmpDst; |
| 254 | SkBitmap tmpBitmap; |
| 255 | |
| 256 | bitmapBounds.isetWH(bitmap.width(), bitmap.height()); |
| 257 | |
| 258 | // Compute matrix from the two rectangles |
| 259 | if (src) { |
| 260 | tmpSrc = *src; |
| 261 | } else { |
| 262 | tmpSrc = bitmapBounds; |
| 263 | } |
| 264 | matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit); |
| 265 | |
ericrk | 983294f | 2016-04-18 09:14:00 -0700 | [diff] [blame] | 266 | LogDrawScaleFactor(SkMatrix::Concat(*draw.fMatrix, matrix), paint.getFilterQuality()); |
| 267 | |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 268 | const SkRect* dstPtr = &dst; |
| 269 | const SkBitmap* bitmapPtr = &bitmap; |
| 270 | |
| 271 | // clip the tmpSrc to the bounds of the bitmap, and recompute dstRect if |
| 272 | // needed (if the src was clipped). No check needed if src==null. |
| 273 | if (src) { |
| 274 | if (!bitmapBounds.contains(*src)) { |
| 275 | if (!tmpSrc.intersect(bitmapBounds)) { |
| 276 | return; // nothing to draw |
| 277 | } |
| 278 | // recompute dst, based on the smaller tmpSrc |
| 279 | matrix.mapRect(&tmpDst, tmpSrc); |
| 280 | dstPtr = &tmpDst; |
| 281 | } |
reed | 27efa23 | 2016-07-25 19:10:10 -0700 | [diff] [blame] | 282 | } |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 283 | |
reed | 27efa23 | 2016-07-25 19:10:10 -0700 | [diff] [blame] | 284 | if (src && !src->contains(bitmapBounds) && |
| 285 | SkCanvas::kFast_SrcRectConstraint == constraint && |
| 286 | paint.getFilterQuality() != kNone_SkFilterQuality) { |
| 287 | // src is smaller than the bounds of the bitmap, and we are filtering, so we don't know |
| 288 | // how much more of the bitmap we need, so we can't use extractSubset or drawBitmap, |
| 289 | // but we must use a shader w/ dst bounds (which can access all of the bitmap needed). |
| 290 | goto USE_SHADER; |
| 291 | } |
| 292 | |
| 293 | if (src) { |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 294 | // since we may need to clamp to the borders of the src rect within |
| 295 | // the bitmap, we extract a subset. |
reed | b07a94f | 2014-11-19 05:03:18 -0800 | [diff] [blame] | 296 | const SkIRect srcIR = tmpSrc.roundOut(); |
bsalomon | 19fe41e | 2016-08-23 14:02:54 -0700 | [diff] [blame] | 297 | if (!bitmap.extractSubset(&tmpBitmap, srcIR)) { |
| 298 | return; |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 299 | } |
| 300 | bitmapPtr = &tmpBitmap; |
| 301 | |
| 302 | // Since we did an extract, we need to adjust the matrix accordingly |
| 303 | SkScalar dx = 0, dy = 0; |
| 304 | if (srcIR.fLeft > 0) { |
| 305 | dx = SkIntToScalar(srcIR.fLeft); |
| 306 | } |
| 307 | if (srcIR.fTop > 0) { |
| 308 | dy = SkIntToScalar(srcIR.fTop); |
| 309 | } |
| 310 | if (dx || dy) { |
| 311 | matrix.preTranslate(dx, dy); |
| 312 | } |
| 313 | |
| 314 | SkRect extractedBitmapBounds; |
| 315 | extractedBitmapBounds.isetWH(bitmapPtr->width(), bitmapPtr->height()); |
| 316 | if (extractedBitmapBounds == tmpSrc) { |
| 317 | // no fractional part in src, we can just call drawBitmap |
| 318 | goto USE_DRAWBITMAP; |
| 319 | } |
| 320 | } else { |
| 321 | USE_DRAWBITMAP: |
| 322 | // We can go faster by just calling drawBitmap, which will concat the |
| 323 | // matrix with the CTM, and try to call drawSprite if it can. If not, |
| 324 | // it will make a shader and call drawRect, as we do below. |
fmalita | ab83da7 | 2016-08-26 13:04:14 -0700 | [diff] [blame] | 325 | if (CanApplyDstMatrixAsCTM(matrix, paint)) { |
| 326 | draw.drawBitmap(*bitmapPtr, matrix, dstPtr, paint); |
| 327 | return; |
| 328 | } |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 329 | } |
| 330 | |
reed | 27efa23 | 2016-07-25 19:10:10 -0700 | [diff] [blame] | 331 | USE_SHADER: |
reed | f77c47b | 2016-08-05 10:15:00 -0700 | [diff] [blame] | 332 | |
Herb Derby | 83e939b | 2017-02-07 14:25:11 -0500 | [diff] [blame] | 333 | // 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] | 334 | // Since the shader need only live for our stack-frame, pass in a custom allocator. This |
| 335 | // can save malloc calls, and signals to SkMakeBitmapShader to not try to copy the bitmap |
| 336 | // 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^] | 337 | |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 338 | // construct a shader, so we can call drawRect with the dst |
reed | f77c47b | 2016-08-05 10:15:00 -0700 | [diff] [blame] | 339 | auto s = SkMakeBitmapShader(*bitmapPtr, SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, |
Herb Derby | bfdc87a | 2017-02-14 15:06:23 +0000 | [diff] [blame^] | 340 | &matrix, kNever_SkCopyPixelsMode); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 341 | if (!s) { |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 342 | return; |
| 343 | } |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 344 | |
| 345 | SkPaint paintWithShader(paint); |
| 346 | paintWithShader.setStyle(SkPaint::kFill_Style); |
reed | f77c47b | 2016-08-05 10:15:00 -0700 | [diff] [blame] | 347 | paintWithShader.setShader(s); |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 348 | |
| 349 | // Call ourself, in case the subclass wanted to share this setup code |
| 350 | // but handle the drawRect code themselves. |
| 351 | this->drawRect(draw, *dstPtr, paintWithShader); |
| 352 | } |
| 353 | |
| 354 | void SkBitmapDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap, |
| 355 | int x, int y, const SkPaint& paint) { |
| 356 | draw.drawSprite(bitmap, x, y, paint); |
| 357 | } |
| 358 | |
| 359 | void SkBitmapDevice::drawText(const SkDraw& draw, const void* text, size_t len, |
| 360 | SkScalar x, SkScalar y, const SkPaint& paint) { |
| 361 | draw.drawText((const char*)text, len, x, y, paint); |
| 362 | } |
| 363 | |
| 364 | void SkBitmapDevice::drawPosText(const SkDraw& draw, const void* text, size_t len, |
fmalita | 05c4a43 | 2014-09-29 06:29:53 -0700 | [diff] [blame] | 365 | const SkScalar xpos[], int scalarsPerPos, |
| 366 | const SkPoint& offset, const SkPaint& paint) { |
| 367 | draw.drawPosText((const char*)text, len, xpos, scalarsPerPos, offset, paint); |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 368 | } |
| 369 | |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 370 | void SkBitmapDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode, |
| 371 | int vertexCount, |
| 372 | const SkPoint verts[], const SkPoint textures[], |
Mike Reed | faba371 | 2016-11-03 14:45:31 -0400 | [diff] [blame] | 373 | const SkColor colors[], SkBlendMode bmode, |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 374 | const uint16_t indices[], int indexCount, |
| 375 | const SkPaint& paint) { |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 376 | draw.drawVertices(vmode, vertexCount, verts, textures, colors, bmode, |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 377 | indices, indexCount, paint); |
| 378 | } |
| 379 | |
| 380 | void SkBitmapDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device, |
| 381 | int x, int y, const SkPaint& paint) { |
reed | cf5c846 | 2016-07-20 12:28:40 -0700 | [diff] [blame] | 382 | SkASSERT(!paint.getImageFilter()); |
reed | 9572a10 | 2015-05-26 19:22:17 -0700 | [diff] [blame] | 383 | draw.drawSprite(static_cast<SkBitmapDevice*>(device)->fBitmap, x, y, paint); |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 384 | } |
| 385 | |
reed | e51c356 | 2016-07-19 14:33:20 -0700 | [diff] [blame] | 386 | /////////////////////////////////////////////////////////////////////////////// |
| 387 | |
| 388 | void SkBitmapDevice::drawSpecial(const SkDraw& draw, SkSpecialImage* srcImg, int x, int y, |
| 389 | const SkPaint& paint) { |
| 390 | SkASSERT(!srcImg->isTextureBacked()); |
| 391 | |
| 392 | SkBitmap resultBM; |
| 393 | |
| 394 | SkImageFilter* filter = paint.getImageFilter(); |
| 395 | if (filter) { |
| 396 | SkIPoint offset = SkIPoint::Make(0, 0); |
| 397 | SkMatrix matrix = *draw.fMatrix; |
| 398 | matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y)); |
| 399 | const SkIRect clipBounds = draw.fRC->getBounds().makeOffset(-x, -y); |
Hal Canary | 704cd32 | 2016-11-07 14:13:52 -0500 | [diff] [blame] | 400 | sk_sp<SkImageFilterCache> cache(this->getImageFilterCache()); |
brianosman | 2a75e5d | 2016-09-22 07:15:37 -0700 | [diff] [blame] | 401 | SkImageFilter::OutputProperties outputProperties(fBitmap.colorSpace()); |
| 402 | SkImageFilter::Context ctx(matrix, clipBounds, cache.get(), outputProperties); |
liyuqian | 2add0ff | 2016-10-20 11:04:39 -0700 | [diff] [blame] | 403 | |
reed | e51c356 | 2016-07-19 14:33:20 -0700 | [diff] [blame] | 404 | sk_sp<SkSpecialImage> resultImg(filter->filterImage(srcImg, ctx, &offset)); |
| 405 | if (resultImg) { |
| 406 | SkPaint tmpUnfiltered(paint); |
| 407 | tmpUnfiltered.setImageFilter(nullptr); |
| 408 | if (resultImg->getROPixels(&resultBM)) { |
| 409 | this->drawSprite(draw, resultBM, x + offset.x(), y + offset.y(), tmpUnfiltered); |
| 410 | } |
| 411 | } |
| 412 | } else { |
| 413 | if (srcImg->getROPixels(&resultBM)) { |
| 414 | this->drawSprite(draw, resultBM, x, y, paint); |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | sk_sp<SkSpecialImage> SkBitmapDevice::makeSpecial(const SkBitmap& bitmap) { |
| 420 | return SkSpecialImage::MakeFromRaster(bitmap.bounds(), bitmap); |
| 421 | } |
| 422 | |
| 423 | sk_sp<SkSpecialImage> SkBitmapDevice::makeSpecial(const SkImage* image) { |
| 424 | return SkSpecialImage::MakeFromImage(SkIRect::MakeWH(image->width(), image->height()), |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 425 | image->makeNonTextureImage(), fBitmap.colorSpace()); |
reed | e51c356 | 2016-07-19 14:33:20 -0700 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | sk_sp<SkSpecialImage> SkBitmapDevice::snapSpecial() { |
| 429 | return this->makeSpecial(fBitmap); |
| 430 | } |
| 431 | |
| 432 | /////////////////////////////////////////////////////////////////////////////// |
| 433 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 434 | sk_sp<SkSurface> SkBitmapDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) { |
| 435 | return SkSurface::MakeRaster(info, &props); |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 436 | } |
| 437 | |
senorblanco | 900c367 | 2016-04-27 11:31:23 -0700 | [diff] [blame] | 438 | SkImageFilterCache* SkBitmapDevice::getImageFilterCache() { |
| 439 | SkImageFilterCache* cache = SkImageFilterCache::Get(); |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 440 | cache->ref(); |
| 441 | return cache; |
| 442 | } |
| 443 | |
Mike Reed | 7627fa5 | 2017-02-08 10:07:53 -0500 | [diff] [blame] | 444 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 445 | |
reed | b2db898 | 2014-11-13 12:41:02 -0800 | [diff] [blame] | 446 | bool SkBitmapDevice::onShouldDisableLCD(const SkPaint& paint) const { |
commit-bot@chromium.org | cba7378 | 2014-05-29 15:57:47 +0000 | [diff] [blame] | 447 | if (kN32_SkColorType != fBitmap.colorType() || |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 448 | paint.getRasterizer() || |
| 449 | paint.getPathEffect() || |
| 450 | paint.isFakeBoldText() || |
| 451 | paint.getStyle() != SkPaint::kFill_Style || |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 452 | !paint.isSrcOver()) |
reed | b2db898 | 2014-11-13 12:41:02 -0800 | [diff] [blame] | 453 | { |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 454 | return true; |
| 455 | } |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 456 | return false; |
| 457 | } |
Mike Reed | 7627fa5 | 2017-02-08 10:07:53 -0500 | [diff] [blame] | 458 | |
| 459 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 460 | |
| 461 | void SkBitmapDevice::onSave() { |
| 462 | fRCStack.push_back(fRCStack.back()); |
| 463 | } |
| 464 | |
| 465 | void SkBitmapDevice::onRestore() { |
| 466 | fRCStack.pop_back(); |
| 467 | } |
| 468 | |
| 469 | void SkBitmapDevice::onClipRect(const SkRect& rect, SkClipOp op, bool aa) { |
| 470 | fRCStack.back().op(rect, this->ctm(), SkIRect::MakeWH(this->width(), this->height()), |
| 471 | (SkRegion::Op)op, aa); |
| 472 | } |
| 473 | |
| 474 | void SkBitmapDevice::onClipRRect(const SkRRect& rrect, SkClipOp op, bool aa) { |
| 475 | fRCStack.back().op(rrect, this->ctm(), SkIRect::MakeWH(this->width(), this->height()), |
| 476 | (SkRegion::Op)op, aa); |
| 477 | } |
| 478 | |
| 479 | void SkBitmapDevice::onClipPath(const SkPath& path, SkClipOp op, bool aa) { |
| 480 | fRCStack.back().op(path, this->ctm(), SkIRect::MakeWH(this->width(), this->height()), |
| 481 | (SkRegion::Op)op, aa); |
| 482 | } |
| 483 | |
| 484 | void SkBitmapDevice::onClipRegion(const SkRegion& rgn, SkClipOp op) { |
| 485 | fRCStack.back().op(rgn, (SkRegion::Op)op); |
| 486 | } |