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