blob: 45ec2b70ae23ecca221f9528b0a918f0d9fcbf0f [file] [log] [blame]
robertphillips@google.com53238bc2013-08-30 13:12:10 +00001/*
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 Reed986480a2017-01-13 22:43:16 +00008#include "SkBitmapDevice.h"
robertphillips@google.com53238bc2013-08-30 13:12:10 +00009#include "SkConfig8888.h"
10#include "SkDraw.h"
reede51c3562016-07-19 14:33:20 -070011#include "SkImageFilter.h"
senorblanco900c3672016-04-27 11:31:23 -070012#include "SkImageFilterCache.h"
mtkleincd495412015-11-05 09:46:23 -080013#include "SkMallocPixelRef.h"
bungemand3ebb482015-08-05 13:57:49 -070014#include "SkMatrix.h"
15#include "SkPaint.h"
16#include "SkPath.h"
reed884e97c2015-05-26 11:31:54 -070017#include "SkPixelRef.h"
bungemand3ebb482015-08-05 13:57:49 -070018#include "SkPixmap.h"
reede51c3562016-07-19 14:33:20 -070019#include "SkRasterClip.h"
Mike Reed356f7c22017-01-10 11:58:39 -050020#include "SkRasterHandleAllocator.h"
robertphillips@google.com53238bc2013-08-30 13:12:10 +000021#include "SkShader.h"
reede51c3562016-07-19 14:33:20 -070022#include "SkSpecialImage.h"
reed@google.com76f10a32014-02-05 15:32:21 +000023#include "SkSurface.h"
bungemand3ebb482015-08-05 13:57:49 -070024
25class SkColorTable;
robertphillips@google.com53238bc2013-08-30 13:12:10 +000026
commit-bot@chromium.org15a14052014-02-16 00:59:25 +000027static 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) {
reed44977482015-02-27 10:23:00 -080036 *newAlphaType = kUnknown_SkAlphaType;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +000037 }
38 return true;
39 }
skia.committer@gmail.com969588f2014-02-16 03:01:56 +000040
commit-bot@chromium.org15a14052014-02-16 00:59:25 +000041 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.org28fcae22014-04-11 17:15:40 +000057 case kN32_SkColorType:
commit-bot@chromium.org15a14052014-02-16 00:59:25 +000058 break;
reeda34be682016-02-15 07:48:35 -080059 case kRGBA_F16_SkColorType:
60 break;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +000061 default:
62 return false;
63 }
64
65 if (newAlphaType) {
66 *newAlphaType = canonicalAlphaType;
67 }
68 return true;
69}
70
robertphillips9a53fd72015-06-22 09:46:59 -070071SkBitmapDevice::SkBitmapDevice(const SkBitmap& bitmap)
reed589a39e2016-08-20 07:59:19 -070072 : INHERITED(bitmap.info(), SkSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType))
reed2c9e2002016-07-25 08:05:22 -070073 , fBitmap(bitmap)
74{
halcanary96fcdcc2015-08-27 07:41:13 -070075 SkASSERT(valid_for_bitmap_device(bitmap.info(), nullptr));
reed2c9e2002016-07-25 08:05:22 -070076 fBitmap.lockPixels();
Mike Reed7627fa52017-02-08 10:07:53 -050077
78 fRCStack.push_back().setRect(SkIRect::MakeWH(bitmap.width(), bitmap.height()));
robertphillips@google.com53238bc2013-08-30 13:12:10 +000079}
80
robertphillips9a53fd72015-06-22 09:46:59 -070081SkBitmapDevice* SkBitmapDevice::Create(const SkImageInfo& info) {
82 return Create(info, SkSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType));
83}
84
Mike Reed356f7c22017-01-10 11:58:39 -050085SkBitmapDevice::SkBitmapDevice(const SkBitmap& bitmap, const SkSurfaceProps& surfaceProps,
86 SkRasterHandleAllocator::Handle hndl)
reed589a39e2016-08-20 07:59:19 -070087 : INHERITED(bitmap.info(), surfaceProps)
reed2c9e2002016-07-25 08:05:22 -070088 , fBitmap(bitmap)
Mike Reed356f7c22017-01-10 11:58:39 -050089 , fRasterHandle(hndl)
reed2c9e2002016-07-25 08:05:22 -070090{
halcanary96fcdcc2015-08-27 07:41:13 -070091 SkASSERT(valid_for_bitmap_device(bitmap.info(), nullptr));
reed2c9e2002016-07-25 08:05:22 -070092 fBitmap.lockPixels();
Mike Reed7627fa52017-02-08 10:07:53 -050093
94 fRCStack.push_back().setRect(SkIRect::MakeWH(bitmap.width(), bitmap.height()));
robertphillips@google.com53238bc2013-08-30 13:12:10 +000095}
96
commit-bot@chromium.org15a14052014-02-16 00:59:25 +000097SkBitmapDevice* SkBitmapDevice::Create(const SkImageInfo& origInfo,
Mike Reed356f7c22017-01-10 11:58:39 -050098 const SkSurfaceProps& surfaceProps,
99 SkRasterHandleAllocator* allocator) {
reede5ea5002014-09-03 11:54:58 -0700100 SkAlphaType newAT = origInfo.alphaType();
101 if (!valid_for_bitmap_device(origInfo, &newAT)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700102 return nullptr;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000103 }
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000104
Mike Reed356f7c22017-01-10 11:58:39 -0500105 SkRasterHandleAllocator::Handle hndl = nullptr;
reede5ea5002014-09-03 11:54:58 -0700106 const SkImageInfo info = origInfo.makeAlphaType(newAT);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000107 SkBitmap bitmap;
108
109 if (kUnknown_SkColorType == info.colorType()) {
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000110 if (!bitmap.setInfo(info)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700111 return nullptr;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000112 }
Mike Reed356f7c22017-01-10 11:58:39 -0500113 } else if (allocator) {
114 hndl = allocator->allocBitmap(info, &bitmap);
115 if (!hndl) {
116 return nullptr;
117 }
lsalzman04d975a2016-01-08 11:20:14 -0800118 } else if (info.isOpaque()) {
mtkleincd495412015-11-05 09:46:23 -0800119 // If this bitmap is opaque, we don't have any sensible default color,
120 // so we just return uninitialized pixels.
reed84825042014-09-02 12:50:45 -0700121 if (!bitmap.tryAllocPixels(info)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700122 return nullptr;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000123 }
mtkleincd495412015-11-05 09:46:23 -0800124 } 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.org15a14052014-02-16 00:59:25 +0000130 }
131 }
132
Mike Reed356f7c22017-01-10 11:58:39 -0500133 return new SkBitmapDevice(bitmap, surfaceProps, hndl);
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000134}
135
mtkleinfeaadee2015-04-08 11:25:48 -0700136void SkBitmapDevice::setNewSize(const SkISize& size) {
137 SkASSERT(!fBitmap.pixelRef());
138 fBitmap.setInfo(fBitmap.info().makeWH(size.fWidth, size.fHeight));
reed589a39e2016-08-20 07:59:19 -0700139 this->privateResize(fBitmap.info().width(), fBitmap.info().height());
mtkleinfeaadee2015-04-08 11:25:48 -0700140}
141
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000142void 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();
reed589a39e2016-08-20 07:59:19 -0700147 this->privateResize(fBitmap.info().width(), fBitmap.info().height());
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000148}
149
reed76033be2015-03-14 10:54:31 -0700150SkBaseDevice* SkBitmapDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
robertphillips9a53fd72015-06-22 09:46:59 -0700151 const SkSurfaceProps surfaceProps(this->surfaceProps().flags(), cinfo.fPixelGeometry);
Mike Reed356f7c22017-01-10 11:58:39 -0500152 return SkBitmapDevice::Create(cinfo.fInfo, surfaceProps, cinfo.fAllocator);
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000153}
154
reed884e97c2015-05-26 11:31:54 -0700155bool SkBitmapDevice::onAccessPixels(SkPixmap* pmap) {
reedb560b5c2016-07-24 12:30:34 -0700156 if (this->onPeekPixels(pmap)) {
reed6e764852015-06-05 14:11:32 -0700157 fBitmap.notifyPixelsChanged();
158 return true;
159 }
160 return false;
reed9572a102015-05-26 19:22:17 -0700161}
162
163bool SkBitmapDevice::onPeekPixels(SkPixmap* pmap) {
reed884e97c2015-05-26 11:31:54 -0700164 const SkImageInfo info = fBitmap.info();
165 if (fBitmap.getPixels() && (kUnknown_SkColorType != info.colorType())) {
halcanary96fcdcc2015-08-27 07:41:13 -0700166 SkColorTable* ctable = nullptr;
reed884e97c2015-05-26 11:31:54 -0700167 pmap->reset(fBitmap.info(), fBitmap.getPixels(), fBitmap.rowBytes(), ctable);
168 return true;
reed@google.com9c135db2014-03-12 18:28:35 +0000169 }
reed884e97c2015-05-26 11:31:54 -0700170 return false;
reed@google.com9c135db2014-03-12 18:28:35 +0000171}
172
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000173bool SkBitmapDevice::onWritePixels(const SkImageInfo& srcInfo, const void* srcPixels,
174 size_t srcRowBytes, int x, int y) {
reed@google.com0d30c512014-03-14 14:02:58 +0000175 // since we don't stop creating un-pixeled devices yet, check for no pixels here
halcanary96fcdcc2015-08-27 07:41:13 -0700176 if (nullptr == fBitmap.getPixels()) {
reed@google.com0d30c512014-03-14 14:02:58 +0000177 return false;
178 }
179
Mike Reed68dd8d02017-01-04 16:34:31 -0500180 if (fBitmap.writePixels(SkPixmap(srcInfo, srcPixels, srcRowBytes), x, y)) {
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000181 fBitmap.notifyPixelsChanged();
182 return true;
183 }
184 return false;
185}
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000186
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000187bool SkBitmapDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
188 int x, int y) {
reedb184f7f2014-07-13 04:32:32 -0700189 return fBitmap.readPixels(dstInfo, dstPixels, dstRowBytes, x, y);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000190}
191
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000192///////////////////////////////////////////////////////////////////////////////
193
194void SkBitmapDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
195 draw.drawPaint(paint);
196}
197
198void SkBitmapDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode, size_t count,
199 const SkPoint pts[], const SkPaint& paint) {
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000200 draw.drawPoints(mode, count, pts, paint);
201}
202
203void SkBitmapDevice::drawRect(const SkDraw& draw, const SkRect& r, const SkPaint& paint) {
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000204 draw.drawRect(r, paint);
205}
206
207void SkBitmapDevice::drawOval(const SkDraw& draw, const SkRect& oval, const SkPaint& paint) {
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000208 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.
halcanary96fcdcc2015-08-27 07:41:13 -0700212 this->drawPath(draw, path, paint, nullptr, true);
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000213}
214
215void SkBitmapDevice::drawRRect(const SkDraw& draw, const SkRRect& rrect, const SkPaint& paint) {
robertphillips@google.com50a76002013-11-12 01:16:56 +0000216#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.
halcanary96fcdcc2015-08-27 07:41:13 -0700222 this->drawPath(draw, path, paint, nullptr, true);
robertphillips@google.com50a76002013-11-12 01:16:56 +0000223#else
scroggo@google.coma8e33a92013-11-08 18:02:53 +0000224 draw.drawRRect(rrect, paint);
robertphillips@google.com50a76002013-11-12 01:16:56 +0000225#endif
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000226}
227
228void SkBitmapDevice::drawPath(const SkDraw& draw, const SkPath& path,
229 const SkPaint& paint, const SkMatrix* prePathMatrix,
230 bool pathIsMutable) {
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000231 draw.drawPath(path, paint, prePathMatrix, pathIsMutable);
232}
233
234void SkBitmapDevice::drawBitmap(const SkDraw& draw, const SkBitmap& bitmap,
235 const SkMatrix& matrix, const SkPaint& paint) {
ericrk983294f2016-04-18 09:14:00 -0700236 LogDrawScaleFactor(SkMatrix::Concat(*draw.fMatrix, matrix), paint.getFilterQuality());
halcanary96fcdcc2015-08-27 07:41:13 -0700237 draw.drawBitmap(bitmap, matrix, nullptr, paint);
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000238}
239
fmalitaab83da72016-08-26 13:04:14 -0700240static 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.com53238bc2013-08-30 13:12:10 +0000249void SkBitmapDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
250 const SkRect* src, const SkRect& dst,
reed562fe472015-07-28 07:35:14 -0700251 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000252 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
ericrk983294f2016-04-18 09:14:00 -0700266 LogDrawScaleFactor(SkMatrix::Concat(*draw.fMatrix, matrix), paint.getFilterQuality());
267
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000268 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 }
reed27efa232016-07-25 19:10:10 -0700282 }
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000283
reed27efa232016-07-25 19:10:10 -0700284 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.com53238bc2013-08-30 13:12:10 +0000294 // since we may need to clamp to the borders of the src rect within
295 // the bitmap, we extract a subset.
reedb07a94f2014-11-19 05:03:18 -0800296 const SkIRect srcIR = tmpSrc.roundOut();
bsalomon19fe41e2016-08-23 14:02:54 -0700297 if (!bitmap.extractSubset(&tmpBitmap, srcIR)) {
298 return;
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000299 }
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.
fmalitaab83da72016-08-26 13:04:14 -0700325 if (CanApplyDstMatrixAsCTM(matrix, paint)) {
326 draw.drawBitmap(*bitmapPtr, matrix, dstPtr, paint);
327 return;
328 }
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000329 }
330
reed27efa232016-07-25 19:10:10 -0700331 USE_SHADER:
reedf77c47b2016-08-05 10:15:00 -0700332
Herb Derby83e939b2017-02-07 14:25:11 -0500333 // TODO(herb): Move this over to SkArenaAlloc when arena alloc has a facility to return sk_sps.
reedf77c47b2016-08-05 10:15:00 -0700334 // 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 Derbybfdc87a2017-02-14 15:06:23 +0000337
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000338 // construct a shader, so we can call drawRect with the dst
reedf77c47b2016-08-05 10:15:00 -0700339 auto s = SkMakeBitmapShader(*bitmapPtr, SkShader::kClamp_TileMode, SkShader::kClamp_TileMode,
Herb Derbybfdc87a2017-02-14 15:06:23 +0000340 &matrix, kNever_SkCopyPixelsMode);
reed8a21c9f2016-03-08 18:50:00 -0800341 if (!s) {
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000342 return;
343 }
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000344
345 SkPaint paintWithShader(paint);
346 paintWithShader.setStyle(SkPaint::kFill_Style);
reedf77c47b2016-08-05 10:15:00 -0700347 paintWithShader.setShader(s);
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000348
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
354void 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
359void 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
364void SkBitmapDevice::drawPosText(const SkDraw& draw, const void* text, size_t len,
fmalita05c4a432014-09-29 06:29:53 -0700365 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.com53238bc2013-08-30 13:12:10 +0000368}
369
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000370void SkBitmapDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
371 int vertexCount,
372 const SkPoint verts[], const SkPoint textures[],
Mike Reedfaba3712016-11-03 14:45:31 -0400373 const SkColor colors[], SkBlendMode bmode,
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000374 const uint16_t indices[], int indexCount,
375 const SkPaint& paint) {
Mike Reed7d954ad2016-10-28 15:42:34 -0400376 draw.drawVertices(vmode, vertexCount, verts, textures, colors, bmode,
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000377 indices, indexCount, paint);
378}
379
380void SkBitmapDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
381 int x, int y, const SkPaint& paint) {
reedcf5c8462016-07-20 12:28:40 -0700382 SkASSERT(!paint.getImageFilter());
reed9572a102015-05-26 19:22:17 -0700383 draw.drawSprite(static_cast<SkBitmapDevice*>(device)->fBitmap, x, y, paint);
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000384}
385
reede51c3562016-07-19 14:33:20 -0700386///////////////////////////////////////////////////////////////////////////////
387
388void 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 Canary704cd322016-11-07 14:13:52 -0500400 sk_sp<SkImageFilterCache> cache(this->getImageFilterCache());
brianosman2a75e5d2016-09-22 07:15:37 -0700401 SkImageFilter::OutputProperties outputProperties(fBitmap.colorSpace());
402 SkImageFilter::Context ctx(matrix, clipBounds, cache.get(), outputProperties);
liyuqian2add0ff2016-10-20 11:04:39 -0700403
reede51c3562016-07-19 14:33:20 -0700404 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
419sk_sp<SkSpecialImage> SkBitmapDevice::makeSpecial(const SkBitmap& bitmap) {
420 return SkSpecialImage::MakeFromRaster(bitmap.bounds(), bitmap);
421}
422
423sk_sp<SkSpecialImage> SkBitmapDevice::makeSpecial(const SkImage* image) {
424 return SkSpecialImage::MakeFromImage(SkIRect::MakeWH(image->width(), image->height()),
Brian Osman61624f02016-12-09 14:51:59 -0500425 image->makeNonTextureImage(), fBitmap.colorSpace());
reede51c3562016-07-19 14:33:20 -0700426}
427
428sk_sp<SkSpecialImage> SkBitmapDevice::snapSpecial() {
429 return this->makeSpecial(fBitmap);
430}
431
432///////////////////////////////////////////////////////////////////////////////
433
reede8f30622016-03-23 18:59:25 -0700434sk_sp<SkSurface> SkBitmapDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
435 return SkSurface::MakeRaster(info, &props);
reed@google.com76f10a32014-02-05 15:32:21 +0000436}
437
senorblanco900c3672016-04-27 11:31:23 -0700438SkImageFilterCache* SkBitmapDevice::getImageFilterCache() {
439 SkImageFilterCache* cache = SkImageFilterCache::Get();
senorblanco55b6d8b2014-07-30 11:26:46 -0700440 cache->ref();
441 return cache;
442}
443
Mike Reed7627fa52017-02-08 10:07:53 -0500444///////////////////////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000445
reedb2db8982014-11-13 12:41:02 -0800446bool SkBitmapDevice::onShouldDisableLCD(const SkPaint& paint) const {
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000447 if (kN32_SkColorType != fBitmap.colorType() ||
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000448 paint.getRasterizer() ||
449 paint.getPathEffect() ||
450 paint.isFakeBoldText() ||
451 paint.getStyle() != SkPaint::kFill_Style ||
reed374772b2016-10-05 17:33:02 -0700452 !paint.isSrcOver())
reedb2db8982014-11-13 12:41:02 -0800453 {
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000454 return true;
455 }
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000456 return false;
457}
Mike Reed7627fa52017-02-08 10:07:53 -0500458
459///////////////////////////////////////////////////////////////////////////////////////////////////
460
461void SkBitmapDevice::onSave() {
462 fRCStack.push_back(fRCStack.back());
463}
464
465void SkBitmapDevice::onRestore() {
466 fRCStack.pop_back();
467}
468
469void 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
474void 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
479void 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
484void SkBitmapDevice::onClipRegion(const SkRegion& rgn, SkClipOp op) {
485 fRCStack.back().op(rgn, (SkRegion::Op)op);
486}