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 | |
| 8 | #include "SkBitmapDevice.h" |
| 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" |
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" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 23 | #include "SkXfermode.h" |
| 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) |
| 72 | : INHERITED(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(); |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 77 | } |
| 78 | |
robertphillips | 9a53fd7 | 2015-06-22 09:46:59 -0700 | [diff] [blame] | 79 | SkBitmapDevice* SkBitmapDevice::Create(const SkImageInfo& info) { |
| 80 | return Create(info, SkSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType)); |
| 81 | } |
| 82 | |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 83 | SkBitmapDevice::SkBitmapDevice(const SkBitmap& bitmap, const SkSurfaceProps& surfaceProps) |
robertphillips | 9a53fd7 | 2015-06-22 09:46:59 -0700 | [diff] [blame] | 84 | : INHERITED(surfaceProps) |
reed | 2c9e200 | 2016-07-25 08:05:22 -0700 | [diff] [blame^] | 85 | , fBitmap(bitmap) |
| 86 | { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 87 | SkASSERT(valid_for_bitmap_device(bitmap.info(), nullptr)); |
reed | 2c9e200 | 2016-07-25 08:05:22 -0700 | [diff] [blame^] | 88 | fBitmap.lockPixels(); |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 89 | } |
| 90 | |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 91 | SkBitmapDevice* SkBitmapDevice::Create(const SkImageInfo& origInfo, |
robertphillips | 9a53fd7 | 2015-06-22 09:46:59 -0700 | [diff] [blame] | 92 | const SkSurfaceProps& surfaceProps) { |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 93 | SkAlphaType newAT = origInfo.alphaType(); |
| 94 | if (!valid_for_bitmap_device(origInfo, &newAT)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 95 | return nullptr; |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 96 | } |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 97 | |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 98 | const SkImageInfo info = origInfo.makeAlphaType(newAT); |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 99 | SkBitmap bitmap; |
| 100 | |
| 101 | if (kUnknown_SkColorType == info.colorType()) { |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 102 | if (!bitmap.setInfo(info)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 103 | return nullptr; |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 104 | } |
lsalzman | 04d975a | 2016-01-08 11:20:14 -0800 | [diff] [blame] | 105 | } else if (info.isOpaque()) { |
mtklein | cd49541 | 2015-11-05 09:46:23 -0800 | [diff] [blame] | 106 | // If this bitmap is opaque, we don't have any sensible default color, |
| 107 | // so we just return uninitialized pixels. |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 108 | if (!bitmap.tryAllocPixels(info)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 109 | return nullptr; |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 110 | } |
mtklein | cd49541 | 2015-11-05 09:46:23 -0800 | [diff] [blame] | 111 | } else { |
| 112 | // This bitmap has transparency, so we'll zero the pixels (to transparent). |
| 113 | // We use a ZeroedPRFactory as a faster alloc-then-eraseColor(SK_ColorTRANSPARENT). |
| 114 | SkMallocPixelRef::ZeroedPRFactory factory; |
| 115 | if (!bitmap.tryAllocPixels(info, &factory, nullptr/*color table*/)) { |
| 116 | return nullptr; |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 120 | return new SkBitmapDevice(bitmap, surfaceProps); |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 121 | } |
| 122 | |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 123 | SkImageInfo SkBitmapDevice::imageInfo() const { |
| 124 | return fBitmap.info(); |
| 125 | } |
| 126 | |
mtklein | feaadee | 2015-04-08 11:25:48 -0700 | [diff] [blame] | 127 | void SkBitmapDevice::setNewSize(const SkISize& size) { |
| 128 | SkASSERT(!fBitmap.pixelRef()); |
| 129 | fBitmap.setInfo(fBitmap.info().makeWH(size.fWidth, size.fHeight)); |
| 130 | } |
| 131 | |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 132 | void SkBitmapDevice::replaceBitmapBackendForRasterSurface(const SkBitmap& bm) { |
| 133 | SkASSERT(bm.width() == fBitmap.width()); |
| 134 | SkASSERT(bm.height() == fBitmap.height()); |
| 135 | fBitmap = bm; // intent is to use bm's pixelRef (and rowbytes/config) |
| 136 | fBitmap.lockPixels(); |
| 137 | } |
| 138 | |
reed | 76033be | 2015-03-14 10:54:31 -0700 | [diff] [blame] | 139 | SkBaseDevice* SkBitmapDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) { |
robertphillips | 9a53fd7 | 2015-06-22 09:46:59 -0700 | [diff] [blame] | 140 | const SkSurfaceProps surfaceProps(this->surfaceProps().flags(), cinfo.fPixelGeometry); |
| 141 | return SkBitmapDevice::Create(cinfo.fInfo, surfaceProps); |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 142 | } |
| 143 | |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 144 | const SkBitmap& SkBitmapDevice::onAccessBitmap() { |
| 145 | return fBitmap; |
| 146 | } |
| 147 | |
reed | 884e97c | 2015-05-26 11:31:54 -0700 | [diff] [blame] | 148 | bool SkBitmapDevice::onAccessPixels(SkPixmap* pmap) { |
reed | b560b5c | 2016-07-24 12:30:34 -0700 | [diff] [blame] | 149 | if (this->onPeekPixels(pmap)) { |
reed | 6e76485 | 2015-06-05 14:11:32 -0700 | [diff] [blame] | 150 | fBitmap.notifyPixelsChanged(); |
| 151 | return true; |
| 152 | } |
| 153 | return false; |
reed | 9572a10 | 2015-05-26 19:22:17 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | bool SkBitmapDevice::onPeekPixels(SkPixmap* pmap) { |
reed | 884e97c | 2015-05-26 11:31:54 -0700 | [diff] [blame] | 157 | const SkImageInfo info = fBitmap.info(); |
| 158 | if (fBitmap.getPixels() && (kUnknown_SkColorType != info.colorType())) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 159 | SkColorTable* ctable = nullptr; |
reed | 884e97c | 2015-05-26 11:31:54 -0700 | [diff] [blame] | 160 | pmap->reset(fBitmap.info(), fBitmap.getPixels(), fBitmap.rowBytes(), ctable); |
| 161 | return true; |
reed@google.com | 9c135db | 2014-03-12 18:28:35 +0000 | [diff] [blame] | 162 | } |
reed | 884e97c | 2015-05-26 11:31:54 -0700 | [diff] [blame] | 163 | return false; |
reed@google.com | 9c135db | 2014-03-12 18:28:35 +0000 | [diff] [blame] | 164 | } |
| 165 | |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 166 | bool SkBitmapDevice::onWritePixels(const SkImageInfo& srcInfo, const void* srcPixels, |
| 167 | size_t srcRowBytes, int x, int y) { |
reed@google.com | 0d30c51 | 2014-03-14 14:02:58 +0000 | [diff] [blame] | 168 | // 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] | 169 | if (nullptr == fBitmap.getPixels()) { |
reed@google.com | 0d30c51 | 2014-03-14 14:02:58 +0000 | [diff] [blame] | 170 | return false; |
| 171 | } |
| 172 | |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 173 | const SkImageInfo dstInfo = fBitmap.info().makeWH(srcInfo.width(), srcInfo.height()); |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 174 | |
| 175 | void* dstPixels = fBitmap.getAddr(x, y); |
| 176 | size_t dstRowBytes = fBitmap.rowBytes(); |
| 177 | |
reed | b184f7f | 2014-07-13 04:32:32 -0700 | [diff] [blame] | 178 | if (SkPixelInfo::CopyPixels(dstInfo, dstPixels, dstRowBytes, srcInfo, srcPixels, srcRowBytes)) { |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 179 | fBitmap.notifyPixelsChanged(); |
| 180 | return true; |
| 181 | } |
| 182 | return false; |
| 183 | } |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 184 | |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 185 | bool SkBitmapDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, |
| 186 | int x, int y) { |
reed | b184f7f | 2014-07-13 04:32:32 -0700 | [diff] [blame] | 187 | return fBitmap.readPixels(dstInfo, dstPixels, dstRowBytes, x, y); |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 188 | } |
| 189 | |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 190 | /////////////////////////////////////////////////////////////////////////////// |
| 191 | |
| 192 | void SkBitmapDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) { |
| 193 | draw.drawPaint(paint); |
| 194 | } |
| 195 | |
| 196 | void SkBitmapDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode, size_t count, |
| 197 | const SkPoint pts[], const SkPaint& paint) { |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 198 | draw.drawPoints(mode, count, pts, paint); |
| 199 | } |
| 200 | |
| 201 | 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] | 202 | draw.drawRect(r, paint); |
| 203 | } |
| 204 | |
| 205 | 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] | 206 | SkPath path; |
| 207 | path.addOval(oval); |
| 208 | // call the VIRTUAL version, so any subclasses who do handle drawPath aren't |
| 209 | // required to override drawOval. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 210 | this->drawPath(draw, path, paint, nullptr, true); |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | 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] | 214 | #ifdef SK_IGNORE_BLURRED_RRECT_OPT |
| 215 | SkPath path; |
| 216 | |
| 217 | path.addRRect(rrect); |
| 218 | // call the VIRTUAL version, so any subclasses who do handle drawPath aren't |
| 219 | // required to override drawRRect. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 220 | this->drawPath(draw, path, paint, nullptr, true); |
robertphillips@google.com | 50a7600 | 2013-11-12 01:16:56 +0000 | [diff] [blame] | 221 | #else |
scroggo@google.com | a8e33a9 | 2013-11-08 18:02:53 +0000 | [diff] [blame] | 222 | draw.drawRRect(rrect, paint); |
robertphillips@google.com | 50a7600 | 2013-11-12 01:16:56 +0000 | [diff] [blame] | 223 | #endif |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | void SkBitmapDevice::drawPath(const SkDraw& draw, const SkPath& path, |
| 227 | const SkPaint& paint, const SkMatrix* prePathMatrix, |
| 228 | bool pathIsMutable) { |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 229 | draw.drawPath(path, paint, prePathMatrix, pathIsMutable); |
| 230 | } |
| 231 | |
| 232 | void SkBitmapDevice::drawBitmap(const SkDraw& draw, const SkBitmap& bitmap, |
| 233 | const SkMatrix& matrix, const SkPaint& paint) { |
ericrk | 983294f | 2016-04-18 09:14:00 -0700 | [diff] [blame] | 234 | LogDrawScaleFactor(SkMatrix::Concat(*draw.fMatrix, matrix), paint.getFilterQuality()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 235 | draw.drawBitmap(bitmap, matrix, nullptr, paint); |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | void SkBitmapDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap, |
| 239 | const SkRect* src, const SkRect& dst, |
reed | 562fe47 | 2015-07-28 07:35:14 -0700 | [diff] [blame] | 240 | const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) { |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 241 | SkMatrix matrix; |
| 242 | SkRect bitmapBounds, tmpSrc, tmpDst; |
| 243 | SkBitmap tmpBitmap; |
| 244 | |
| 245 | bitmapBounds.isetWH(bitmap.width(), bitmap.height()); |
| 246 | |
| 247 | // Compute matrix from the two rectangles |
| 248 | if (src) { |
| 249 | tmpSrc = *src; |
| 250 | } else { |
| 251 | tmpSrc = bitmapBounds; |
| 252 | } |
| 253 | matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit); |
| 254 | |
ericrk | 983294f | 2016-04-18 09:14:00 -0700 | [diff] [blame] | 255 | LogDrawScaleFactor(SkMatrix::Concat(*draw.fMatrix, matrix), paint.getFilterQuality()); |
| 256 | |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 257 | const SkRect* dstPtr = &dst; |
| 258 | const SkBitmap* bitmapPtr = &bitmap; |
| 259 | |
| 260 | // clip the tmpSrc to the bounds of the bitmap, and recompute dstRect if |
| 261 | // needed (if the src was clipped). No check needed if src==null. |
| 262 | if (src) { |
| 263 | if (!bitmapBounds.contains(*src)) { |
| 264 | if (!tmpSrc.intersect(bitmapBounds)) { |
| 265 | return; // nothing to draw |
| 266 | } |
| 267 | // recompute dst, based on the smaller tmpSrc |
| 268 | matrix.mapRect(&tmpDst, tmpSrc); |
| 269 | dstPtr = &tmpDst; |
| 270 | } |
| 271 | |
| 272 | // since we may need to clamp to the borders of the src rect within |
| 273 | // the bitmap, we extract a subset. |
reed | b07a94f | 2014-11-19 05:03:18 -0800 | [diff] [blame] | 274 | const SkIRect srcIR = tmpSrc.roundOut(); |
piotaixr | 0e97705 | 2014-09-17 16:24:04 -0700 | [diff] [blame] | 275 | if(bitmap.pixelRef()->getTexture()) { |
| 276 | // Accelerated source canvas, don't use extractSubset but readPixels to get the subset. |
| 277 | // This way, the pixels are copied in CPU memory instead of GPU memory. |
bsalomon | 9d22fd6 | 2016-01-11 11:14:17 -0800 | [diff] [blame] | 278 | bitmap.pixelRef()->readPixels(&tmpBitmap, kN32_SkColorType, &srcIR); |
piotaixr | 0e97705 | 2014-09-17 16:24:04 -0700 | [diff] [blame] | 279 | } else { |
| 280 | if (!bitmap.extractSubset(&tmpBitmap, srcIR)) { |
| 281 | return; |
| 282 | } |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 283 | } |
| 284 | bitmapPtr = &tmpBitmap; |
| 285 | |
| 286 | // Since we did an extract, we need to adjust the matrix accordingly |
| 287 | SkScalar dx = 0, dy = 0; |
| 288 | if (srcIR.fLeft > 0) { |
| 289 | dx = SkIntToScalar(srcIR.fLeft); |
| 290 | } |
| 291 | if (srcIR.fTop > 0) { |
| 292 | dy = SkIntToScalar(srcIR.fTop); |
| 293 | } |
| 294 | if (dx || dy) { |
| 295 | matrix.preTranslate(dx, dy); |
| 296 | } |
| 297 | |
| 298 | SkRect extractedBitmapBounds; |
| 299 | extractedBitmapBounds.isetWH(bitmapPtr->width(), bitmapPtr->height()); |
| 300 | if (extractedBitmapBounds == tmpSrc) { |
| 301 | // no fractional part in src, we can just call drawBitmap |
| 302 | goto USE_DRAWBITMAP; |
| 303 | } |
| 304 | } else { |
| 305 | USE_DRAWBITMAP: |
| 306 | // We can go faster by just calling drawBitmap, which will concat the |
| 307 | // matrix with the CTM, and try to call drawSprite if it can. If not, |
| 308 | // it will make a shader and call drawRect, as we do below. |
reed | 0393912 | 2014-12-15 13:42:51 -0800 | [diff] [blame] | 309 | draw.drawBitmap(*bitmapPtr, matrix, dstPtr, paint); |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 310 | return; |
| 311 | } |
| 312 | |
| 313 | // construct a shader, so we can call drawRect with the dst |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 314 | auto s = SkShader::MakeBitmapShader(*bitmapPtr, SkShader::kClamp_TileMode, |
| 315 | SkShader::kClamp_TileMode, &matrix); |
| 316 | if (!s) { |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 317 | return; |
| 318 | } |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 319 | |
| 320 | SkPaint paintWithShader(paint); |
| 321 | paintWithShader.setStyle(SkPaint::kFill_Style); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 322 | paintWithShader.setShader(std::move(s)); |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 323 | |
| 324 | // Call ourself, in case the subclass wanted to share this setup code |
| 325 | // but handle the drawRect code themselves. |
| 326 | this->drawRect(draw, *dstPtr, paintWithShader); |
| 327 | } |
| 328 | |
| 329 | void SkBitmapDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap, |
| 330 | int x, int y, const SkPaint& paint) { |
| 331 | draw.drawSprite(bitmap, x, y, paint); |
| 332 | } |
| 333 | |
| 334 | void SkBitmapDevice::drawText(const SkDraw& draw, const void* text, size_t len, |
| 335 | SkScalar x, SkScalar y, const SkPaint& paint) { |
| 336 | draw.drawText((const char*)text, len, x, y, paint); |
| 337 | } |
| 338 | |
| 339 | void SkBitmapDevice::drawPosText(const SkDraw& draw, const void* text, size_t len, |
fmalita | 05c4a43 | 2014-09-29 06:29:53 -0700 | [diff] [blame] | 340 | const SkScalar xpos[], int scalarsPerPos, |
| 341 | const SkPoint& offset, const SkPaint& paint) { |
| 342 | draw.drawPosText((const char*)text, len, xpos, scalarsPerPos, offset, paint); |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 343 | } |
| 344 | |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 345 | void SkBitmapDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode, |
| 346 | int vertexCount, |
| 347 | const SkPoint verts[], const SkPoint textures[], |
| 348 | const SkColor colors[], SkXfermode* xmode, |
| 349 | const uint16_t indices[], int indexCount, |
| 350 | const SkPaint& paint) { |
| 351 | draw.drawVertices(vmode, vertexCount, verts, textures, colors, xmode, |
| 352 | indices, indexCount, paint); |
| 353 | } |
| 354 | |
| 355 | void SkBitmapDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device, |
| 356 | int x, int y, const SkPaint& paint) { |
reed | cf5c846 | 2016-07-20 12:28:40 -0700 | [diff] [blame] | 357 | SkASSERT(!paint.getImageFilter()); |
reed | 9572a10 | 2015-05-26 19:22:17 -0700 | [diff] [blame] | 358 | draw.drawSprite(static_cast<SkBitmapDevice*>(device)->fBitmap, x, y, paint); |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 359 | } |
| 360 | |
reed | e51c356 | 2016-07-19 14:33:20 -0700 | [diff] [blame] | 361 | /////////////////////////////////////////////////////////////////////////////// |
| 362 | |
| 363 | void SkBitmapDevice::drawSpecial(const SkDraw& draw, SkSpecialImage* srcImg, int x, int y, |
| 364 | const SkPaint& paint) { |
| 365 | SkASSERT(!srcImg->isTextureBacked()); |
| 366 | |
| 367 | SkBitmap resultBM; |
| 368 | |
| 369 | SkImageFilter* filter = paint.getImageFilter(); |
| 370 | if (filter) { |
| 371 | SkIPoint offset = SkIPoint::Make(0, 0); |
| 372 | SkMatrix matrix = *draw.fMatrix; |
| 373 | matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y)); |
| 374 | const SkIRect clipBounds = draw.fRC->getBounds().makeOffset(-x, -y); |
| 375 | SkAutoTUnref<SkImageFilterCache> cache(this->getImageFilterCache()); |
| 376 | SkImageFilter::Context ctx(matrix, clipBounds, cache.get()); |
| 377 | |
| 378 | sk_sp<SkSpecialImage> resultImg(filter->filterImage(srcImg, ctx, &offset)); |
| 379 | if (resultImg) { |
| 380 | SkPaint tmpUnfiltered(paint); |
| 381 | tmpUnfiltered.setImageFilter(nullptr); |
| 382 | if (resultImg->getROPixels(&resultBM)) { |
| 383 | this->drawSprite(draw, resultBM, x + offset.x(), y + offset.y(), tmpUnfiltered); |
| 384 | } |
| 385 | } |
| 386 | } else { |
| 387 | if (srcImg->getROPixels(&resultBM)) { |
| 388 | this->drawSprite(draw, resultBM, x, y, paint); |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | sk_sp<SkSpecialImage> SkBitmapDevice::makeSpecial(const SkBitmap& bitmap) { |
| 394 | return SkSpecialImage::MakeFromRaster(bitmap.bounds(), bitmap); |
| 395 | } |
| 396 | |
| 397 | sk_sp<SkSpecialImage> SkBitmapDevice::makeSpecial(const SkImage* image) { |
| 398 | return SkSpecialImage::MakeFromImage(SkIRect::MakeWH(image->width(), image->height()), |
| 399 | image->makeNonTextureImage()); |
| 400 | } |
| 401 | |
| 402 | sk_sp<SkSpecialImage> SkBitmapDevice::snapSpecial() { |
| 403 | return this->makeSpecial(fBitmap); |
| 404 | } |
| 405 | |
| 406 | /////////////////////////////////////////////////////////////////////////////// |
| 407 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 408 | sk_sp<SkSurface> SkBitmapDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) { |
| 409 | return SkSurface::MakeRaster(info, &props); |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 410 | } |
| 411 | |
senorblanco | 900c367 | 2016-04-27 11:31:23 -0700 | [diff] [blame] | 412 | SkImageFilterCache* SkBitmapDevice::getImageFilterCache() { |
| 413 | SkImageFilterCache* cache = SkImageFilterCache::Get(); |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 414 | cache->ref(); |
| 415 | return cache; |
| 416 | } |
| 417 | |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 418 | /////////////////////////////////////////////////////////////////////////////// |
| 419 | |
reed | b2db898 | 2014-11-13 12:41:02 -0800 | [diff] [blame] | 420 | bool SkBitmapDevice::onShouldDisableLCD(const SkPaint& paint) const { |
commit-bot@chromium.org | cba7378 | 2014-05-29 15:57:47 +0000 | [diff] [blame] | 421 | if (kN32_SkColorType != fBitmap.colorType() || |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 422 | paint.getRasterizer() || |
| 423 | paint.getPathEffect() || |
| 424 | paint.isFakeBoldText() || |
| 425 | paint.getStyle() != SkPaint::kFill_Style || |
reed | b2db898 | 2014-11-13 12:41:02 -0800 | [diff] [blame] | 426 | !SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrcOver_Mode)) |
| 427 | { |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 428 | return true; |
| 429 | } |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 430 | return false; |
| 431 | } |