blob: b3ab7fc03c924eda4bbe8ffe8027dfdeb8499cdf [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 "SkDraw.h"
reede51c3562016-07-19 14:33:20 -070010#include "SkImageFilter.h"
senorblanco900c3672016-04-27 11:31:23 -070011#include "SkImageFilterCache.h"
mtkleincd495412015-11-05 09:46:23 -080012#include "SkMallocPixelRef.h"
bungemand3ebb482015-08-05 13:57:49 -070013#include "SkMatrix.h"
14#include "SkPaint.h"
15#include "SkPath.h"
reed884e97c2015-05-26 11:31:54 -070016#include "SkPixelRef.h"
bungemand3ebb482015-08-05 13:57:49 -070017#include "SkPixmap.h"
reede51c3562016-07-19 14:33:20 -070018#include "SkRasterClip.h"
Mike Reed356f7c22017-01-10 11:58:39 -050019#include "SkRasterHandleAllocator.h"
robertphillips@google.com53238bc2013-08-30 13:12:10 +000020#include "SkShader.h"
reede51c3562016-07-19 14:33:20 -070021#include "SkSpecialImage.h"
reed@google.com76f10a32014-02-05 15:32:21 +000022#include "SkSurface.h"
Florin Malita53f77bd2017-04-28 13:48:37 -040023#include "SkTLazy.h"
Mike Reed2f6b5a42017-03-19 15:04:17 -040024#include "SkVertices.h"
bungemand3ebb482015-08-05 13:57:49 -070025
26class SkColorTable;
robertphillips@google.com53238bc2013-08-30 13:12:10 +000027
commit-bot@chromium.org15a14052014-02-16 00:59:25 +000028static bool valid_for_bitmap_device(const SkImageInfo& info,
29 SkAlphaType* newAlphaType) {
30 if (info.width() < 0 || info.height() < 0) {
31 return false;
32 }
33
34 // TODO: can we stop supporting kUnknown in SkBitmkapDevice?
35 if (kUnknown_SkColorType == info.colorType()) {
36 if (newAlphaType) {
reed44977482015-02-27 10:23:00 -080037 *newAlphaType = kUnknown_SkAlphaType;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +000038 }
39 return true;
40 }
skia.committer@gmail.com969588f2014-02-16 03:01:56 +000041
commit-bot@chromium.org15a14052014-02-16 00:59:25 +000042 SkAlphaType canonicalAlphaType = info.alphaType();
43
44 switch (info.colorType()) {
45 case kAlpha_8_SkColorType:
46 break;
47 case kRGB_565_SkColorType:
48 canonicalAlphaType = kOpaque_SkAlphaType;
49 break;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000050 case kN32_SkColorType:
commit-bot@chromium.org15a14052014-02-16 00:59:25 +000051 break;
reeda34be682016-02-15 07:48:35 -080052 case kRGBA_F16_SkColorType:
53 break;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +000054 default:
55 return false;
56 }
57
58 if (newAlphaType) {
59 *newAlphaType = canonicalAlphaType;
60 }
61 return true;
62}
63
robertphillips9a53fd72015-06-22 09:46:59 -070064SkBitmapDevice::SkBitmapDevice(const SkBitmap& bitmap)
reed589a39e2016-08-20 07:59:19 -070065 : INHERITED(bitmap.info(), SkSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType))
reed2c9e2002016-07-25 08:05:22 -070066 , fBitmap(bitmap)
Mike Reedc42a1cd2017-02-14 14:25:14 -050067 , fRCStack(bitmap.width(), bitmap.height())
reed2c9e2002016-07-25 08:05:22 -070068{
halcanary96fcdcc2015-08-27 07:41:13 -070069 SkASSERT(valid_for_bitmap_device(bitmap.info(), nullptr));
robertphillips@google.com53238bc2013-08-30 13:12:10 +000070}
71
robertphillips9a53fd72015-06-22 09:46:59 -070072SkBitmapDevice* SkBitmapDevice::Create(const SkImageInfo& info) {
73 return Create(info, SkSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType));
74}
75
Mike Reed356f7c22017-01-10 11:58:39 -050076SkBitmapDevice::SkBitmapDevice(const SkBitmap& bitmap, const SkSurfaceProps& surfaceProps,
77 SkRasterHandleAllocator::Handle hndl)
reed589a39e2016-08-20 07:59:19 -070078 : INHERITED(bitmap.info(), surfaceProps)
reed2c9e2002016-07-25 08:05:22 -070079 , fBitmap(bitmap)
Mike Reed356f7c22017-01-10 11:58:39 -050080 , fRasterHandle(hndl)
Mike Reedc42a1cd2017-02-14 14:25:14 -050081 , fRCStack(bitmap.width(), bitmap.height())
reed2c9e2002016-07-25 08:05:22 -070082{
halcanary96fcdcc2015-08-27 07:41:13 -070083 SkASSERT(valid_for_bitmap_device(bitmap.info(), nullptr));
robertphillips@google.com53238bc2013-08-30 13:12:10 +000084}
85
commit-bot@chromium.org15a14052014-02-16 00:59:25 +000086SkBitmapDevice* SkBitmapDevice::Create(const SkImageInfo& origInfo,
Mike Reed356f7c22017-01-10 11:58:39 -050087 const SkSurfaceProps& surfaceProps,
88 SkRasterHandleAllocator* allocator) {
reede5ea5002014-09-03 11:54:58 -070089 SkAlphaType newAT = origInfo.alphaType();
90 if (!valid_for_bitmap_device(origInfo, &newAT)) {
halcanary96fcdcc2015-08-27 07:41:13 -070091 return nullptr;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +000092 }
robertphillips@google.com53238bc2013-08-30 13:12:10 +000093
Mike Reed356f7c22017-01-10 11:58:39 -050094 SkRasterHandleAllocator::Handle hndl = nullptr;
reede5ea5002014-09-03 11:54:58 -070095 const SkImageInfo info = origInfo.makeAlphaType(newAT);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +000096 SkBitmap bitmap;
97
98 if (kUnknown_SkColorType == info.colorType()) {
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +000099 if (!bitmap.setInfo(info)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700100 return nullptr;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000101 }
Mike Reed356f7c22017-01-10 11:58:39 -0500102 } else if (allocator) {
103 hndl = allocator->allocBitmap(info, &bitmap);
104 if (!hndl) {
105 return nullptr;
106 }
lsalzman04d975a2016-01-08 11:20:14 -0800107 } else if (info.isOpaque()) {
mtkleincd495412015-11-05 09:46:23 -0800108 // If this bitmap is opaque, we don't have any sensible default color,
109 // so we just return uninitialized pixels.
reed84825042014-09-02 12:50:45 -0700110 if (!bitmap.tryAllocPixels(info)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700111 return nullptr;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000112 }
mtkleincd495412015-11-05 09:46:23 -0800113 } else {
114 // This bitmap has transparency, so we'll zero the pixels (to transparent).
Mike Reed6b3155c2017-04-03 14:41:44 -0400115 // We use the flag as a faster alloc-then-eraseColor(SK_ColorTRANSPARENT).
Mike Reed086a4272017-07-18 10:53:11 -0400116 if (!bitmap.tryAllocPixelsFlags(info, SkBitmap::kZeroPixels_AllocFlag)) {
mtkleincd495412015-11-05 09:46:23 -0800117 return nullptr;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000118 }
119 }
120
Mike Reed356f7c22017-01-10 11:58:39 -0500121 return new SkBitmapDevice(bitmap, surfaceProps, hndl);
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000122}
123
124void SkBitmapDevice::replaceBitmapBackendForRasterSurface(const SkBitmap& bm) {
125 SkASSERT(bm.width() == fBitmap.width());
126 SkASSERT(bm.height() == fBitmap.height());
127 fBitmap = bm; // intent is to use bm's pixelRef (and rowbytes/config)
reed589a39e2016-08-20 07:59:19 -0700128 this->privateResize(fBitmap.info().width(), fBitmap.info().height());
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000129}
130
reed76033be2015-03-14 10:54:31 -0700131SkBaseDevice* SkBitmapDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
robertphillips9a53fd72015-06-22 09:46:59 -0700132 const SkSurfaceProps surfaceProps(this->surfaceProps().flags(), cinfo.fPixelGeometry);
Mike Reed356f7c22017-01-10 11:58:39 -0500133 return SkBitmapDevice::Create(cinfo.fInfo, surfaceProps, cinfo.fAllocator);
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000134}
135
reed884e97c2015-05-26 11:31:54 -0700136bool SkBitmapDevice::onAccessPixels(SkPixmap* pmap) {
reedb560b5c2016-07-24 12:30:34 -0700137 if (this->onPeekPixels(pmap)) {
reed6e764852015-06-05 14:11:32 -0700138 fBitmap.notifyPixelsChanged();
139 return true;
140 }
141 return false;
reed9572a102015-05-26 19:22:17 -0700142}
143
144bool SkBitmapDevice::onPeekPixels(SkPixmap* pmap) {
reed884e97c2015-05-26 11:31:54 -0700145 const SkImageInfo info = fBitmap.info();
146 if (fBitmap.getPixels() && (kUnknown_SkColorType != info.colorType())) {
Mike Reed086a4272017-07-18 10:53:11 -0400147 pmap->reset(fBitmap.info(), fBitmap.getPixels(), fBitmap.rowBytes());
reed884e97c2015-05-26 11:31:54 -0700148 return true;
reed@google.com9c135db2014-03-12 18:28:35 +0000149 }
reed884e97c2015-05-26 11:31:54 -0700150 return false;
reed@google.com9c135db2014-03-12 18:28:35 +0000151}
152
Mike Reed353196f2017-07-21 11:01:18 -0400153bool SkBitmapDevice::onWritePixels(const SkPixmap& pm, int x, int y) {
reed@google.com0d30c512014-03-14 14:02:58 +0000154 // since we don't stop creating un-pixeled devices yet, check for no pixels here
halcanary96fcdcc2015-08-27 07:41:13 -0700155 if (nullptr == fBitmap.getPixels()) {
reed@google.com0d30c512014-03-14 14:02:58 +0000156 return false;
157 }
158
Mike Reed353196f2017-07-21 11:01:18 -0400159 if (fBitmap.writePixels(pm, x, y)) {
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000160 fBitmap.notifyPixelsChanged();
161 return true;
162 }
163 return false;
164}
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000165
Mike Reed353196f2017-07-21 11:01:18 -0400166bool SkBitmapDevice::onReadPixels(const SkPixmap& pm, int x, int y) {
167 return fBitmap.readPixels(pm, x, y);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000168}
169
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000170///////////////////////////////////////////////////////////////////////////////
171
Mike Reeda1361362017-03-07 09:37:29 -0500172class SkBitmapDevice::BDDraw : public SkDraw {
Mike Reedc42a1cd2017-02-14 14:25:14 -0500173public:
Mike Reeda1361362017-03-07 09:37:29 -0500174 BDDraw(SkBitmapDevice* dev) {
175 // we need fDst to be set, and if we're actually drawing, to dirty the genID
176 if (!dev->accessPixels(&fDst)) {
177 // NoDrawDevice uses us (why?) so we have to catch this case w/ no pixels
178 fDst.reset(dev->imageInfo(), nullptr, 0);
179 }
180 fMatrix = &dev->ctm();
181 fRC = &dev->fRCStack.rc();
Mike Reedc42a1cd2017-02-14 14:25:14 -0500182 }
183};
Mike Reedc42a1cd2017-02-14 14:25:14 -0500184
Mike Reeda1361362017-03-07 09:37:29 -0500185void SkBitmapDevice::drawPaint(const SkPaint& paint) {
186 BDDraw(this).drawPaint(paint);
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000187}
188
Mike Reeda1361362017-03-07 09:37:29 -0500189void SkBitmapDevice::drawPoints(SkCanvas::PointMode mode, size_t count,
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000190 const SkPoint pts[], const SkPaint& paint) {
Mike Reeda1361362017-03-07 09:37:29 -0500191 BDDraw(this).drawPoints(mode, count, pts, paint, nullptr);
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000192}
193
Mike Reeda1361362017-03-07 09:37:29 -0500194void SkBitmapDevice::drawRect(const SkRect& r, const SkPaint& paint) {
195 BDDraw(this).drawRect(r, paint);
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000196}
197
Mike Reeda1361362017-03-07 09:37:29 -0500198void SkBitmapDevice::drawOval(const SkRect& oval, const SkPaint& paint) {
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000199 SkPath path;
200 path.addOval(oval);
201 // call the VIRTUAL version, so any subclasses who do handle drawPath aren't
202 // required to override drawOval.
Mike Reeda1361362017-03-07 09:37:29 -0500203 this->drawPath(path, paint, nullptr, true);
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000204}
205
Mike Reeda1361362017-03-07 09:37:29 -0500206void SkBitmapDevice::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
robertphillips@google.com50a76002013-11-12 01:16:56 +0000207#ifdef SK_IGNORE_BLURRED_RRECT_OPT
208 SkPath path;
209
210 path.addRRect(rrect);
211 // call the VIRTUAL version, so any subclasses who do handle drawPath aren't
212 // required to override drawRRect.
Mike Reeda1361362017-03-07 09:37:29 -0500213 this->drawPath(path, paint, nullptr, true);
robertphillips@google.com50a76002013-11-12 01:16:56 +0000214#else
Mike Reeda1361362017-03-07 09:37:29 -0500215 BDDraw(this).drawRRect(rrect, paint);
robertphillips@google.com50a76002013-11-12 01:16:56 +0000216#endif
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000217}
218
Mike Reeda1361362017-03-07 09:37:29 -0500219void SkBitmapDevice::drawPath(const SkPath& path,
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000220 const SkPaint& paint, const SkMatrix* prePathMatrix,
221 bool pathIsMutable) {
Mike Reeda1361362017-03-07 09:37:29 -0500222 BDDraw(this).drawPath(path, paint, prePathMatrix, pathIsMutable);
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000223}
224
Hal Canaryb9642382017-06-27 09:58:56 -0400225void SkBitmapDevice::drawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y,
226 const SkPaint& paint) {
227 SkMatrix matrix = SkMatrix::MakeTrans(x, y);
Mike Reeda1361362017-03-07 09:37:29 -0500228 LogDrawScaleFactor(SkMatrix::Concat(this->ctm(), matrix), paint.getFilterQuality());
229 BDDraw(this).drawBitmap(bitmap, matrix, nullptr, paint);
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000230}
231
fmalitaab83da72016-08-26 13:04:14 -0700232static inline bool CanApplyDstMatrixAsCTM(const SkMatrix& m, const SkPaint& paint) {
233 if (!paint.getMaskFilter()) {
234 return true;
235 }
236
237 // Some mask filters parameters (sigma) depend on the CTM/scale.
238 return m.getType() <= SkMatrix::kTranslate_Mask;
239}
240
Mike Reeda1361362017-03-07 09:37:29 -0500241void SkBitmapDevice::drawBitmapRect(const SkBitmap& bitmap,
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000242 const SkRect* src, const SkRect& dst,
reed562fe472015-07-28 07:35:14 -0700243 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000244 SkMatrix matrix;
245 SkRect bitmapBounds, tmpSrc, tmpDst;
246 SkBitmap tmpBitmap;
247
248 bitmapBounds.isetWH(bitmap.width(), bitmap.height());
249
250 // Compute matrix from the two rectangles
251 if (src) {
252 tmpSrc = *src;
253 } else {
254 tmpSrc = bitmapBounds;
255 }
256 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
257
Mike Reeda1361362017-03-07 09:37:29 -0500258 LogDrawScaleFactor(SkMatrix::Concat(this->ctm(), matrix), paint.getFilterQuality());
ericrk983294f2016-04-18 09:14:00 -0700259
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000260 const SkRect* dstPtr = &dst;
261 const SkBitmap* bitmapPtr = &bitmap;
262
263 // clip the tmpSrc to the bounds of the bitmap, and recompute dstRect if
264 // needed (if the src was clipped). No check needed if src==null.
265 if (src) {
266 if (!bitmapBounds.contains(*src)) {
267 if (!tmpSrc.intersect(bitmapBounds)) {
268 return; // nothing to draw
269 }
270 // recompute dst, based on the smaller tmpSrc
271 matrix.mapRect(&tmpDst, tmpSrc);
272 dstPtr = &tmpDst;
273 }
reed27efa232016-07-25 19:10:10 -0700274 }
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000275
reed27efa232016-07-25 19:10:10 -0700276 if (src && !src->contains(bitmapBounds) &&
277 SkCanvas::kFast_SrcRectConstraint == constraint &&
278 paint.getFilterQuality() != kNone_SkFilterQuality) {
279 // src is smaller than the bounds of the bitmap, and we are filtering, so we don't know
280 // how much more of the bitmap we need, so we can't use extractSubset or drawBitmap,
281 // but we must use a shader w/ dst bounds (which can access all of the bitmap needed).
282 goto USE_SHADER;
283 }
284
285 if (src) {
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000286 // since we may need to clamp to the borders of the src rect within
287 // the bitmap, we extract a subset.
reedb07a94f2014-11-19 05:03:18 -0800288 const SkIRect srcIR = tmpSrc.roundOut();
bsalomon19fe41e2016-08-23 14:02:54 -0700289 if (!bitmap.extractSubset(&tmpBitmap, srcIR)) {
290 return;
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000291 }
292 bitmapPtr = &tmpBitmap;
293
294 // Since we did an extract, we need to adjust the matrix accordingly
295 SkScalar dx = 0, dy = 0;
296 if (srcIR.fLeft > 0) {
297 dx = SkIntToScalar(srcIR.fLeft);
298 }
299 if (srcIR.fTop > 0) {
300 dy = SkIntToScalar(srcIR.fTop);
301 }
302 if (dx || dy) {
303 matrix.preTranslate(dx, dy);
304 }
305
lsalzmanc0e52f42017-04-05 21:42:54 -0700306#ifdef SK_DRAWBITMAPRECT_FAST_OFFSET
307 SkRect extractedBitmapBounds = SkRect::MakeXYWH(dx, dy,
308 SkIntToScalar(bitmapPtr->width()),
309 SkIntToScalar(bitmapPtr->height()));
310#else
mtklein65c74f12017-03-31 18:36:39 -0700311 SkRect extractedBitmapBounds;
312 extractedBitmapBounds.isetWH(bitmapPtr->width(), bitmapPtr->height());
lsalzmanc0e52f42017-04-05 21:42:54 -0700313#endif
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000314 if (extractedBitmapBounds == tmpSrc) {
315 // no fractional part in src, we can just call drawBitmap
316 goto USE_DRAWBITMAP;
317 }
318 } else {
319 USE_DRAWBITMAP:
320 // We can go faster by just calling drawBitmap, which will concat the
321 // matrix with the CTM, and try to call drawSprite if it can. If not,
322 // it will make a shader and call drawRect, as we do below.
fmalitaab83da72016-08-26 13:04:14 -0700323 if (CanApplyDstMatrixAsCTM(matrix, paint)) {
Mike Reeda1361362017-03-07 09:37:29 -0500324 BDDraw(this).drawBitmap(*bitmapPtr, matrix, dstPtr, paint);
fmalitaab83da72016-08-26 13:04:14 -0700325 return;
326 }
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000327 }
328
reed27efa232016-07-25 19:10:10 -0700329 USE_SHADER:
reedf77c47b2016-08-05 10:15:00 -0700330
Herb Derby83e939b2017-02-07 14:25:11 -0500331 // TODO(herb): Move this over to SkArenaAlloc when arena alloc has a facility to return sk_sps.
reedf77c47b2016-08-05 10:15:00 -0700332 // Since the shader need only live for our stack-frame, pass in a custom allocator. This
333 // can save malloc calls, and signals to SkMakeBitmapShader to not try to copy the bitmap
334 // if its mutable, since that precaution is not needed (give the short lifetime of the shader).
Herb Derbybfdc87a2017-02-14 15:06:23 +0000335
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000336 // construct a shader, so we can call drawRect with the dst
reedf77c47b2016-08-05 10:15:00 -0700337 auto s = SkMakeBitmapShader(*bitmapPtr, SkShader::kClamp_TileMode, SkShader::kClamp_TileMode,
Herb Derbybfdc87a2017-02-14 15:06:23 +0000338 &matrix, kNever_SkCopyPixelsMode);
reed8a21c9f2016-03-08 18:50:00 -0800339 if (!s) {
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000340 return;
341 }
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000342
343 SkPaint paintWithShader(paint);
344 paintWithShader.setStyle(SkPaint::kFill_Style);
reedf77c47b2016-08-05 10:15:00 -0700345 paintWithShader.setShader(s);
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000346
347 // Call ourself, in case the subclass wanted to share this setup code
348 // but handle the drawRect code themselves.
Mike Reeda1361362017-03-07 09:37:29 -0500349 this->drawRect(*dstPtr, paintWithShader);
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000350}
351
Mike Reed2f6b5a42017-03-19 15:04:17 -0400352void SkBitmapDevice::drawSprite(const SkBitmap& bitmap, int x, int y, const SkPaint& paint) {
Mike Reeda1361362017-03-07 09:37:29 -0500353 BDDraw(this).drawSprite(bitmap, x, y, paint);
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000354}
355
Mike Reeda1361362017-03-07 09:37:29 -0500356void SkBitmapDevice::drawText(const void* text, size_t len,
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000357 SkScalar x, SkScalar y, const SkPaint& paint) {
Mike Reeda1361362017-03-07 09:37:29 -0500358 BDDraw(this).drawText((const char*)text, len, x, y, paint, &fSurfaceProps);
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000359}
360
Mike Reeda1361362017-03-07 09:37:29 -0500361void SkBitmapDevice::drawPosText(const void* text, size_t len, const SkScalar xpos[],
362 int scalarsPerPos, const SkPoint& offset, const SkPaint& paint) {
Mike Reed2f6b5a42017-03-19 15:04:17 -0400363 BDDraw(this).drawPosText((const char*)text, len, xpos, scalarsPerPos, offset, paint,
364 &fSurfaceProps);
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000365}
366
Mike Reed2f6b5a42017-03-19 15:04:17 -0400367void SkBitmapDevice::drawVertices(const SkVertices* vertices, SkBlendMode bmode,
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000368 const SkPaint& paint) {
Mike Reed2f6b5a42017-03-19 15:04:17 -0400369 BDDraw(this).drawVertices(vertices->mode(), vertices->vertexCount(), vertices->positions(),
370 vertices->texCoords(), vertices->colors(), bmode,
371 vertices->indices(), vertices->indexCount(), paint);
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000372}
373
Mike Reed2f6b5a42017-03-19 15:04:17 -0400374void SkBitmapDevice::drawDevice(SkBaseDevice* device, int x, int y, const SkPaint& paint) {
reedcf5c8462016-07-20 12:28:40 -0700375 SkASSERT(!paint.getImageFilter());
Mike Reeda1361362017-03-07 09:37:29 -0500376 BDDraw(this).drawSprite(static_cast<SkBitmapDevice*>(device)->fBitmap, x, y, paint);
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000377}
378
reede51c3562016-07-19 14:33:20 -0700379///////////////////////////////////////////////////////////////////////////////
380
Florin Malita87f80712017-05-10 15:26:44 -0400381namespace {
382
383class SkAutoDeviceClipRestore {
384public:
385 SkAutoDeviceClipRestore(SkBaseDevice* device, const SkIRect& clip)
386 : fDevice(device)
387 , fPrevCTM(device->ctm()) {
388 fDevice->save();
389 fDevice->setCTM(SkMatrix::I());
390 fDevice->clipRect(SkRect::Make(clip), SkClipOp::kIntersect, false);
391 fDevice->setCTM(fPrevCTM);
392 }
393
394 ~SkAutoDeviceClipRestore() {
395 fDevice->restore(fPrevCTM);
396 }
397
398private:
399 SkBaseDevice* fDevice;
400 const SkMatrix fPrevCTM;
401};
402
403} // anonymous ns
404
Florin Malita53f77bd2017-04-28 13:48:37 -0400405void SkBitmapDevice::drawSpecial(SkSpecialImage* src, int x, int y, const SkPaint& origPaint,
406 SkImage* clipImage, const SkMatrix& clipMatrix) {
407 SkASSERT(!src->isTextureBacked());
reede51c3562016-07-19 14:33:20 -0700408
Florin Malita53f77bd2017-04-28 13:48:37 -0400409 sk_sp<SkSpecialImage> filteredImage;
410 SkTCopyOnFirstWrite<SkPaint> paint(origPaint);
reede51c3562016-07-19 14:33:20 -0700411
Florin Malita53f77bd2017-04-28 13:48:37 -0400412 if (SkImageFilter* filter = paint->getImageFilter()) {
reede51c3562016-07-19 14:33:20 -0700413 SkIPoint offset = SkIPoint::Make(0, 0);
Florin Malita53f77bd2017-04-28 13:48:37 -0400414 const SkMatrix matrix = SkMatrix::Concat(
415 SkMatrix::MakeTrans(SkIntToScalar(-x), SkIntToScalar(-y)), this->ctm());
Mike Reeda1361362017-03-07 09:37:29 -0500416 const SkIRect clipBounds = fRCStack.rc().getBounds().makeOffset(-x, -y);
Hal Canary704cd322016-11-07 14:13:52 -0500417 sk_sp<SkImageFilterCache> cache(this->getImageFilterCache());
brianosman2a75e5d2016-09-22 07:15:37 -0700418 SkImageFilter::OutputProperties outputProperties(fBitmap.colorSpace());
419 SkImageFilter::Context ctx(matrix, clipBounds, cache.get(), outputProperties);
liyuqian2add0ff2016-10-20 11:04:39 -0700420
Florin Malita53f77bd2017-04-28 13:48:37 -0400421 filteredImage = filter->filterImage(src, ctx, &offset);
422 if (!filteredImage) {
423 return;
reede51c3562016-07-19 14:33:20 -0700424 }
Florin Malita53f77bd2017-04-28 13:48:37 -0400425
426 src = filteredImage.get();
427 paint.writable()->setImageFilter(nullptr);
428 x += offset.x();
429 y += offset.y();
reede51c3562016-07-19 14:33:20 -0700430 }
Florin Malita53f77bd2017-04-28 13:48:37 -0400431
432 if (!clipImage) {
433 SkBitmap resultBM;
434 if (src->getROPixels(&resultBM)) {
435 this->drawSprite(resultBM, x, y, *paint);
436 }
437 return;
438 }
439
440 // Clip image case.
441 sk_sp<SkImage> srcImage(src->asImage());
442 if (!srcImage) {
443 return;
444 }
445
446 const SkMatrix totalMatrix = SkMatrix::Concat(this->ctm(), clipMatrix);
Florin Malita53f77bd2017-04-28 13:48:37 -0400447 SkRect clipBounds;
448 totalMatrix.mapRect(&clipBounds, SkRect::Make(clipImage->bounds()));
449 const SkIRect srcBounds = srcImage->bounds().makeOffset(x, y);
450
451 SkIRect maskBounds = fRCStack.rc().getBounds();
452 if (!maskBounds.intersect(clipBounds.roundOut()) || !maskBounds.intersect(srcBounds)) {
453 return;
454 }
455
Florin Malita87f80712017-05-10 15:26:44 -0400456 sk_sp<SkImage> mask;
457 SkMatrix maskMatrix, shaderMatrix;
458 SkTLazy<SkAutoDeviceClipRestore> autoClipRestore;
Florin Malita53f77bd2017-04-28 13:48:37 -0400459
Florin Malita87f80712017-05-10 15:26:44 -0400460 SkMatrix totalInverse;
461 if (clipImage->isAlphaOnly() && totalMatrix.invert(&totalInverse)) {
462 // If the mask is already in A8 format, we can draw it directly
463 // (while compensating in the shader matrix).
464 mask = sk_ref_sp(clipImage);
465 maskMatrix = totalMatrix;
466 shaderMatrix = SkMatrix::Concat(totalInverse, SkMatrix::MakeTrans(x, y));
Florin Malita53f77bd2017-04-28 13:48:37 -0400467
Florin Malita87f80712017-05-10 15:26:44 -0400468 // If the mask is not fully contained within the src layer, we must clip.
469 if (!srcBounds.contains(clipBounds)) {
470 autoClipRestore.init(this, srcBounds);
471 }
472
473 maskBounds.offsetTo(0, 0);
474 } else {
475 // Otherwise, we convert the mask to A8 explicitly.
476 sk_sp<SkSurface> surf = SkSurface::MakeRaster(SkImageInfo::MakeA8(maskBounds.width(),
477 maskBounds.height()));
478 SkCanvas* canvas = surf->getCanvas();
479 canvas->translate(-maskBounds.x(), -maskBounds.y());
480 canvas->concat(totalMatrix);
481 canvas->drawImage(clipImage, 0, 0);
482
483 mask = surf->makeImageSnapshot();
484 maskMatrix = SkMatrix::I();
485 shaderMatrix = SkMatrix::MakeTrans(x - maskBounds.x(), y - maskBounds.y());
486 }
487
488 SkAutoDeviceCTMRestore adctmr(this, maskMatrix);
489 paint.writable()->setShader(srcImage->makeShader(&shaderMatrix));
Florin Malita53f77bd2017-04-28 13:48:37 -0400490 this->drawImage(mask.get(), maskBounds.x(), maskBounds.y(), *paint);
reede51c3562016-07-19 14:33:20 -0700491}
492
493sk_sp<SkSpecialImage> SkBitmapDevice::makeSpecial(const SkBitmap& bitmap) {
494 return SkSpecialImage::MakeFromRaster(bitmap.bounds(), bitmap);
495}
496
497sk_sp<SkSpecialImage> SkBitmapDevice::makeSpecial(const SkImage* image) {
498 return SkSpecialImage::MakeFromImage(SkIRect::MakeWH(image->width(), image->height()),
Brian Osman61624f02016-12-09 14:51:59 -0500499 image->makeNonTextureImage(), fBitmap.colorSpace());
reede51c3562016-07-19 14:33:20 -0700500}
501
502sk_sp<SkSpecialImage> SkBitmapDevice::snapSpecial() {
503 return this->makeSpecial(fBitmap);
504}
505
506///////////////////////////////////////////////////////////////////////////////
507
reede8f30622016-03-23 18:59:25 -0700508sk_sp<SkSurface> SkBitmapDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
509 return SkSurface::MakeRaster(info, &props);
reed@google.com76f10a32014-02-05 15:32:21 +0000510}
511
senorblanco900c3672016-04-27 11:31:23 -0700512SkImageFilterCache* SkBitmapDevice::getImageFilterCache() {
513 SkImageFilterCache* cache = SkImageFilterCache::Get();
senorblanco55b6d8b2014-07-30 11:26:46 -0700514 cache->ref();
515 return cache;
516}
517
Mike Reed7627fa52017-02-08 10:07:53 -0500518///////////////////////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000519
reedb2db8982014-11-13 12:41:02 -0800520bool SkBitmapDevice::onShouldDisableLCD(const SkPaint& paint) const {
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000521 if (kN32_SkColorType != fBitmap.colorType() ||
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000522 paint.getRasterizer() ||
523 paint.getPathEffect() ||
524 paint.isFakeBoldText() ||
525 paint.getStyle() != SkPaint::kFill_Style ||
reed374772b2016-10-05 17:33:02 -0700526 !paint.isSrcOver())
reedb2db8982014-11-13 12:41:02 -0800527 {
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000528 return true;
529 }
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000530 return false;
531}
Mike Reed7627fa52017-02-08 10:07:53 -0500532
533///////////////////////////////////////////////////////////////////////////////////////////////////
534
535void SkBitmapDevice::onSave() {
Mike Reedc42a1cd2017-02-14 14:25:14 -0500536 fRCStack.save();
Mike Reed7627fa52017-02-08 10:07:53 -0500537}
538
539void SkBitmapDevice::onRestore() {
Mike Reedc42a1cd2017-02-14 14:25:14 -0500540 fRCStack.restore();
Mike Reed7627fa52017-02-08 10:07:53 -0500541}
542
543void SkBitmapDevice::onClipRect(const SkRect& rect, SkClipOp op, bool aa) {
Mike Reedc42a1cd2017-02-14 14:25:14 -0500544 fRCStack.clipRect(this->ctm(), rect, op, aa);
Mike Reed7627fa52017-02-08 10:07:53 -0500545}
546
547void SkBitmapDevice::onClipRRect(const SkRRect& rrect, SkClipOp op, bool aa) {
Mike Reedc42a1cd2017-02-14 14:25:14 -0500548 fRCStack.clipRRect(this->ctm(), rrect, op, aa);
Mike Reed7627fa52017-02-08 10:07:53 -0500549}
550
551void SkBitmapDevice::onClipPath(const SkPath& path, SkClipOp op, bool aa) {
Mike Reedc42a1cd2017-02-14 14:25:14 -0500552 fRCStack.clipPath(this->ctm(), path, op, aa);
Mike Reed7627fa52017-02-08 10:07:53 -0500553}
554
555void SkBitmapDevice::onClipRegion(const SkRegion& rgn, SkClipOp op) {
Mike Reedc42a1cd2017-02-14 14:25:14 -0500556 SkIPoint origin = this->getOrigin();
557 SkRegion tmp;
558 const SkRegion* ptr = &rgn;
559 if (origin.fX | origin.fY) {
560 // translate from "global/canvas" coordinates to relative to this device
561 rgn.translate(-origin.fX, -origin.fY, &tmp);
562 ptr = &tmp;
563 }
564 fRCStack.clipRegion(*ptr, op);
565}
566
567void SkBitmapDevice::onSetDeviceClipRestriction(SkIRect* mutableClipRestriction) {
568 fRCStack.setDeviceClipRestriction(mutableClipRestriction);
Mike Reedd519d482017-02-16 11:04:52 -0500569 if (!mutableClipRestriction->isEmpty()) {
570 SkRegion rgn(*mutableClipRestriction);
571 fRCStack.clipRegion(rgn, SkClipOp::kIntersect);
572 }
Mike Reedc42a1cd2017-02-14 14:25:14 -0500573}
574
Mike Reeda1361362017-03-07 09:37:29 -0500575bool SkBitmapDevice::onClipIsAA() const {
576 const SkRasterClip& rc = fRCStack.rc();
577 return !rc.isEmpty() && rc.isAA();
578}
579
580void SkBitmapDevice::onAsRgnClip(SkRegion* rgn) const {
581 const SkRasterClip& rc = fRCStack.rc();
582 if (rc.isAA()) {
583 rgn->setRect(rc.getBounds());
584 } else {
585 *rgn = rc.bwRgn();
586 }
587}
588
Mike Reedc42a1cd2017-02-14 14:25:14 -0500589void SkBitmapDevice::validateDevBounds(const SkIRect& drawClipBounds) {
590#ifdef SK_DEBUG
591 const SkIRect& stackBounds = fRCStack.rc().getBounds();
592 SkASSERT(drawClipBounds == stackBounds);
593#endif
Mike Reed7627fa52017-02-08 10:07:53 -0500594}
Mike Reeda1361362017-03-07 09:37:29 -0500595
596SkBaseDevice::ClipType SkBitmapDevice::onGetClipType() const {
597 const SkRasterClip& rc = fRCStack.rc();
598 if (rc.isEmpty()) {
599 return kEmpty_ClipType;
600 } else if (rc.isRect()) {
601 return kRect_ClipType;
602 } else {
603 return kComplex_ClipType;
604 }
605}