blob: fd387acb90f9ac29e216f584bf15c766a3b6a1e1 [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"
Mike Reed2f6b5a42017-03-19 15:04:17 -040023#include "SkVertices.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)
Mike Reedc42a1cd2017-02-14 14:25:14 -050074 , fRCStack(bitmap.width(), bitmap.height())
reed2c9e2002016-07-25 08:05:22 -070075{
halcanary96fcdcc2015-08-27 07:41:13 -070076 SkASSERT(valid_for_bitmap_device(bitmap.info(), nullptr));
reed2c9e2002016-07-25 08:05:22 -070077 fBitmap.lockPixels();
robertphillips@google.com53238bc2013-08-30 13:12:10 +000078}
79
robertphillips9a53fd72015-06-22 09:46:59 -070080SkBitmapDevice* SkBitmapDevice::Create(const SkImageInfo& info) {
81 return Create(info, SkSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType));
82}
83
Mike Reed356f7c22017-01-10 11:58:39 -050084SkBitmapDevice::SkBitmapDevice(const SkBitmap& bitmap, const SkSurfaceProps& surfaceProps,
85 SkRasterHandleAllocator::Handle hndl)
reed589a39e2016-08-20 07:59:19 -070086 : INHERITED(bitmap.info(), surfaceProps)
reed2c9e2002016-07-25 08:05:22 -070087 , fBitmap(bitmap)
Mike Reed356f7c22017-01-10 11:58:39 -050088 , fRasterHandle(hndl)
Mike Reedc42a1cd2017-02-14 14:25:14 -050089 , fRCStack(bitmap.width(), bitmap.height())
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();
robertphillips@google.com53238bc2013-08-30 13:12:10 +000093}
94
commit-bot@chromium.org15a14052014-02-16 00:59:25 +000095SkBitmapDevice* SkBitmapDevice::Create(const SkImageInfo& origInfo,
Mike Reed356f7c22017-01-10 11:58:39 -050096 const SkSurfaceProps& surfaceProps,
97 SkRasterHandleAllocator* allocator) {
reede5ea5002014-09-03 11:54:58 -070098 SkAlphaType newAT = origInfo.alphaType();
99 if (!valid_for_bitmap_device(origInfo, &newAT)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700100 return nullptr;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000101 }
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000102
Mike Reed356f7c22017-01-10 11:58:39 -0500103 SkRasterHandleAllocator::Handle hndl = nullptr;
reede5ea5002014-09-03 11:54:58 -0700104 const SkImageInfo info = origInfo.makeAlphaType(newAT);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000105 SkBitmap bitmap;
106
107 if (kUnknown_SkColorType == info.colorType()) {
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000108 if (!bitmap.setInfo(info)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700109 return nullptr;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000110 }
Mike Reed356f7c22017-01-10 11:58:39 -0500111 } else if (allocator) {
112 hndl = allocator->allocBitmap(info, &bitmap);
113 if (!hndl) {
114 return nullptr;
115 }
lsalzman04d975a2016-01-08 11:20:14 -0800116 } else if (info.isOpaque()) {
mtkleincd495412015-11-05 09:46:23 -0800117 // If this bitmap is opaque, we don't have any sensible default color,
118 // so we just return uninitialized pixels.
reed84825042014-09-02 12:50:45 -0700119 if (!bitmap.tryAllocPixels(info)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700120 return nullptr;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000121 }
mtkleincd495412015-11-05 09:46:23 -0800122 } else {
123 // This bitmap has transparency, so we'll zero the pixels (to transparent).
Mike Reed6b3155c2017-04-03 14:41:44 -0400124 // We use the flag as a faster alloc-then-eraseColor(SK_ColorTRANSPARENT).
125 if (!bitmap.tryAllocPixels(info, nullptr/*colortable*/, SkBitmap::kZeroPixels_AllocFlag)) {
mtkleincd495412015-11-05 09:46:23 -0800126 return nullptr;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000127 }
128 }
129
Mike Reed356f7c22017-01-10 11:58:39 -0500130 return new SkBitmapDevice(bitmap, surfaceProps, hndl);
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000131}
132
133void SkBitmapDevice::replaceBitmapBackendForRasterSurface(const SkBitmap& bm) {
134 SkASSERT(bm.width() == fBitmap.width());
135 SkASSERT(bm.height() == fBitmap.height());
136 fBitmap = bm; // intent is to use bm's pixelRef (and rowbytes/config)
137 fBitmap.lockPixels();
reed589a39e2016-08-20 07:59:19 -0700138 this->privateResize(fBitmap.info().width(), fBitmap.info().height());
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000139}
140
reed76033be2015-03-14 10:54:31 -0700141SkBaseDevice* SkBitmapDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
robertphillips9a53fd72015-06-22 09:46:59 -0700142 const SkSurfaceProps surfaceProps(this->surfaceProps().flags(), cinfo.fPixelGeometry);
Mike Reed356f7c22017-01-10 11:58:39 -0500143 return SkBitmapDevice::Create(cinfo.fInfo, surfaceProps, cinfo.fAllocator);
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000144}
145
reed884e97c2015-05-26 11:31:54 -0700146bool SkBitmapDevice::onAccessPixels(SkPixmap* pmap) {
reedb560b5c2016-07-24 12:30:34 -0700147 if (this->onPeekPixels(pmap)) {
reed6e764852015-06-05 14:11:32 -0700148 fBitmap.notifyPixelsChanged();
149 return true;
150 }
151 return false;
reed9572a102015-05-26 19:22:17 -0700152}
153
154bool SkBitmapDevice::onPeekPixels(SkPixmap* pmap) {
reed884e97c2015-05-26 11:31:54 -0700155 const SkImageInfo info = fBitmap.info();
156 if (fBitmap.getPixels() && (kUnknown_SkColorType != info.colorType())) {
halcanary96fcdcc2015-08-27 07:41:13 -0700157 SkColorTable* ctable = nullptr;
reed884e97c2015-05-26 11:31:54 -0700158 pmap->reset(fBitmap.info(), fBitmap.getPixels(), fBitmap.rowBytes(), ctable);
159 return true;
reed@google.com9c135db2014-03-12 18:28:35 +0000160 }
reed884e97c2015-05-26 11:31:54 -0700161 return false;
reed@google.com9c135db2014-03-12 18:28:35 +0000162}
163
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000164bool SkBitmapDevice::onWritePixels(const SkImageInfo& srcInfo, const void* srcPixels,
165 size_t srcRowBytes, int x, int y) {
reed@google.com0d30c512014-03-14 14:02:58 +0000166 // since we don't stop creating un-pixeled devices yet, check for no pixels here
halcanary96fcdcc2015-08-27 07:41:13 -0700167 if (nullptr == fBitmap.getPixels()) {
reed@google.com0d30c512014-03-14 14:02:58 +0000168 return false;
169 }
170
Mike Reed68dd8d02017-01-04 16:34:31 -0500171 if (fBitmap.writePixels(SkPixmap(srcInfo, srcPixels, srcRowBytes), x, y)) {
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000172 fBitmap.notifyPixelsChanged();
173 return true;
174 }
175 return false;
176}
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000177
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000178bool SkBitmapDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
179 int x, int y) {
reedb184f7f2014-07-13 04:32:32 -0700180 return fBitmap.readPixels(dstInfo, dstPixels, dstRowBytes, x, y);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000181}
182
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000183///////////////////////////////////////////////////////////////////////////////
184
Mike Reeda1361362017-03-07 09:37:29 -0500185class SkBitmapDevice::BDDraw : public SkDraw {
Mike Reedc42a1cd2017-02-14 14:25:14 -0500186public:
Mike Reeda1361362017-03-07 09:37:29 -0500187 BDDraw(SkBitmapDevice* dev) {
188 // we need fDst to be set, and if we're actually drawing, to dirty the genID
189 if (!dev->accessPixels(&fDst)) {
190 // NoDrawDevice uses us (why?) so we have to catch this case w/ no pixels
191 fDst.reset(dev->imageInfo(), nullptr, 0);
192 }
193 fMatrix = &dev->ctm();
194 fRC = &dev->fRCStack.rc();
Mike Reedc42a1cd2017-02-14 14:25:14 -0500195 }
196};
Mike Reedc42a1cd2017-02-14 14:25:14 -0500197
Mike Reeda1361362017-03-07 09:37:29 -0500198void SkBitmapDevice::drawPaint(const SkPaint& paint) {
199 BDDraw(this).drawPaint(paint);
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000200}
201
Mike Reeda1361362017-03-07 09:37:29 -0500202void SkBitmapDevice::drawPoints(SkCanvas::PointMode mode, size_t count,
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000203 const SkPoint pts[], const SkPaint& paint) {
Mike Reeda1361362017-03-07 09:37:29 -0500204 BDDraw(this).drawPoints(mode, count, pts, paint, nullptr);
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000205}
206
Mike Reeda1361362017-03-07 09:37:29 -0500207void SkBitmapDevice::drawRect(const SkRect& r, const SkPaint& paint) {
208 BDDraw(this).drawRect(r, paint);
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000209}
210
Mike Reeda1361362017-03-07 09:37:29 -0500211void SkBitmapDevice::drawOval(const SkRect& oval, const SkPaint& paint) {
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000212 SkPath path;
213 path.addOval(oval);
214 // call the VIRTUAL version, so any subclasses who do handle drawPath aren't
215 // required to override drawOval.
Mike Reeda1361362017-03-07 09:37:29 -0500216 this->drawPath(path, paint, nullptr, true);
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000217}
218
Mike Reeda1361362017-03-07 09:37:29 -0500219void SkBitmapDevice::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
robertphillips@google.com50a76002013-11-12 01:16:56 +0000220#ifdef SK_IGNORE_BLURRED_RRECT_OPT
221 SkPath path;
222
223 path.addRRect(rrect);
224 // call the VIRTUAL version, so any subclasses who do handle drawPath aren't
225 // required to override drawRRect.
Mike Reeda1361362017-03-07 09:37:29 -0500226 this->drawPath(path, paint, nullptr, true);
robertphillips@google.com50a76002013-11-12 01:16:56 +0000227#else
Mike Reeda1361362017-03-07 09:37:29 -0500228 BDDraw(this).drawRRect(rrect, paint);
robertphillips@google.com50a76002013-11-12 01:16:56 +0000229#endif
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000230}
231
Mike Reeda1361362017-03-07 09:37:29 -0500232void SkBitmapDevice::drawPath(const SkPath& path,
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000233 const SkPaint& paint, const SkMatrix* prePathMatrix,
234 bool pathIsMutable) {
Mike Reeda1361362017-03-07 09:37:29 -0500235 BDDraw(this).drawPath(path, paint, prePathMatrix, pathIsMutable);
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000236}
237
Mike Reeda1361362017-03-07 09:37:29 -0500238void SkBitmapDevice::drawBitmap(const SkBitmap& bitmap,
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000239 const SkMatrix& matrix, const SkPaint& paint) {
Mike Reeda1361362017-03-07 09:37:29 -0500240 LogDrawScaleFactor(SkMatrix::Concat(this->ctm(), matrix), paint.getFilterQuality());
241 BDDraw(this).drawBitmap(bitmap, matrix, nullptr, paint);
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000242}
243
fmalitaab83da72016-08-26 13:04:14 -0700244static inline bool CanApplyDstMatrixAsCTM(const SkMatrix& m, const SkPaint& paint) {
245 if (!paint.getMaskFilter()) {
246 return true;
247 }
248
249 // Some mask filters parameters (sigma) depend on the CTM/scale.
250 return m.getType() <= SkMatrix::kTranslate_Mask;
251}
252
Mike Reeda1361362017-03-07 09:37:29 -0500253void SkBitmapDevice::drawBitmapRect(const SkBitmap& bitmap,
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000254 const SkRect* src, const SkRect& dst,
reed562fe472015-07-28 07:35:14 -0700255 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000256 SkMatrix matrix;
257 SkRect bitmapBounds, tmpSrc, tmpDst;
258 SkBitmap tmpBitmap;
259
260 bitmapBounds.isetWH(bitmap.width(), bitmap.height());
261
262 // Compute matrix from the two rectangles
263 if (src) {
264 tmpSrc = *src;
265 } else {
266 tmpSrc = bitmapBounds;
267 }
268 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
269
Mike Reeda1361362017-03-07 09:37:29 -0500270 LogDrawScaleFactor(SkMatrix::Concat(this->ctm(), matrix), paint.getFilterQuality());
ericrk983294f2016-04-18 09:14:00 -0700271
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000272 const SkRect* dstPtr = &dst;
273 const SkBitmap* bitmapPtr = &bitmap;
274
275 // clip the tmpSrc to the bounds of the bitmap, and recompute dstRect if
276 // needed (if the src was clipped). No check needed if src==null.
277 if (src) {
278 if (!bitmapBounds.contains(*src)) {
279 if (!tmpSrc.intersect(bitmapBounds)) {
280 return; // nothing to draw
281 }
282 // recompute dst, based on the smaller tmpSrc
283 matrix.mapRect(&tmpDst, tmpSrc);
284 dstPtr = &tmpDst;
285 }
reed27efa232016-07-25 19:10:10 -0700286 }
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000287
reed27efa232016-07-25 19:10:10 -0700288 if (src && !src->contains(bitmapBounds) &&
289 SkCanvas::kFast_SrcRectConstraint == constraint &&
290 paint.getFilterQuality() != kNone_SkFilterQuality) {
291 // src is smaller than the bounds of the bitmap, and we are filtering, so we don't know
292 // how much more of the bitmap we need, so we can't use extractSubset or drawBitmap,
293 // but we must use a shader w/ dst bounds (which can access all of the bitmap needed).
294 goto USE_SHADER;
295 }
296
297 if (src) {
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000298 // since we may need to clamp to the borders of the src rect within
299 // the bitmap, we extract a subset.
reedb07a94f2014-11-19 05:03:18 -0800300 const SkIRect srcIR = tmpSrc.roundOut();
bsalomon19fe41e2016-08-23 14:02:54 -0700301 if (!bitmap.extractSubset(&tmpBitmap, srcIR)) {
302 return;
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000303 }
304 bitmapPtr = &tmpBitmap;
305
306 // Since we did an extract, we need to adjust the matrix accordingly
307 SkScalar dx = 0, dy = 0;
308 if (srcIR.fLeft > 0) {
309 dx = SkIntToScalar(srcIR.fLeft);
310 }
311 if (srcIR.fTop > 0) {
312 dy = SkIntToScalar(srcIR.fTop);
313 }
314 if (dx || dy) {
315 matrix.preTranslate(dx, dy);
316 }
317
lsalzmanc0e52f42017-04-05 21:42:54 -0700318#ifdef SK_DRAWBITMAPRECT_FAST_OFFSET
319 SkRect extractedBitmapBounds = SkRect::MakeXYWH(dx, dy,
320 SkIntToScalar(bitmapPtr->width()),
321 SkIntToScalar(bitmapPtr->height()));
322#else
mtklein65c74f12017-03-31 18:36:39 -0700323 SkRect extractedBitmapBounds;
324 extractedBitmapBounds.isetWH(bitmapPtr->width(), bitmapPtr->height());
lsalzmanc0e52f42017-04-05 21:42:54 -0700325#endif
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000326 if (extractedBitmapBounds == tmpSrc) {
327 // no fractional part in src, we can just call drawBitmap
328 goto USE_DRAWBITMAP;
329 }
330 } else {
331 USE_DRAWBITMAP:
332 // We can go faster by just calling drawBitmap, which will concat the
333 // matrix with the CTM, and try to call drawSprite if it can. If not,
334 // it will make a shader and call drawRect, as we do below.
fmalitaab83da72016-08-26 13:04:14 -0700335 if (CanApplyDstMatrixAsCTM(matrix, paint)) {
Mike Reeda1361362017-03-07 09:37:29 -0500336 BDDraw(this).drawBitmap(*bitmapPtr, matrix, dstPtr, paint);
fmalitaab83da72016-08-26 13:04:14 -0700337 return;
338 }
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000339 }
340
reed27efa232016-07-25 19:10:10 -0700341 USE_SHADER:
reedf77c47b2016-08-05 10:15:00 -0700342
Herb Derby83e939b2017-02-07 14:25:11 -0500343 // TODO(herb): Move this over to SkArenaAlloc when arena alloc has a facility to return sk_sps.
reedf77c47b2016-08-05 10:15:00 -0700344 // Since the shader need only live for our stack-frame, pass in a custom allocator. This
345 // can save malloc calls, and signals to SkMakeBitmapShader to not try to copy the bitmap
346 // if its mutable, since that precaution is not needed (give the short lifetime of the shader).
Herb Derbybfdc87a2017-02-14 15:06:23 +0000347
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000348 // construct a shader, so we can call drawRect with the dst
reedf77c47b2016-08-05 10:15:00 -0700349 auto s = SkMakeBitmapShader(*bitmapPtr, SkShader::kClamp_TileMode, SkShader::kClamp_TileMode,
Herb Derbybfdc87a2017-02-14 15:06:23 +0000350 &matrix, kNever_SkCopyPixelsMode);
reed8a21c9f2016-03-08 18:50:00 -0800351 if (!s) {
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000352 return;
353 }
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000354
355 SkPaint paintWithShader(paint);
356 paintWithShader.setStyle(SkPaint::kFill_Style);
reedf77c47b2016-08-05 10:15:00 -0700357 paintWithShader.setShader(s);
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000358
359 // Call ourself, in case the subclass wanted to share this setup code
360 // but handle the drawRect code themselves.
Mike Reeda1361362017-03-07 09:37:29 -0500361 this->drawRect(*dstPtr, paintWithShader);
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000362}
363
Mike Reed2f6b5a42017-03-19 15:04:17 -0400364void SkBitmapDevice::drawSprite(const SkBitmap& bitmap, int x, int y, const SkPaint& paint) {
Mike Reeda1361362017-03-07 09:37:29 -0500365 BDDraw(this).drawSprite(bitmap, x, y, paint);
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000366}
367
Mike Reeda1361362017-03-07 09:37:29 -0500368void SkBitmapDevice::drawText(const void* text, size_t len,
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000369 SkScalar x, SkScalar y, const SkPaint& paint) {
Mike Reeda1361362017-03-07 09:37:29 -0500370 BDDraw(this).drawText((const char*)text, len, x, y, paint, &fSurfaceProps);
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000371}
372
Mike Reeda1361362017-03-07 09:37:29 -0500373void SkBitmapDevice::drawPosText(const void* text, size_t len, const SkScalar xpos[],
374 int scalarsPerPos, const SkPoint& offset, const SkPaint& paint) {
Mike Reed2f6b5a42017-03-19 15:04:17 -0400375 BDDraw(this).drawPosText((const char*)text, len, xpos, scalarsPerPos, offset, paint,
376 &fSurfaceProps);
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000377}
378
Mike Reed2f6b5a42017-03-19 15:04:17 -0400379void SkBitmapDevice::drawVertices(const SkVertices* vertices, SkBlendMode bmode,
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000380 const SkPaint& paint) {
Mike Reed2f6b5a42017-03-19 15:04:17 -0400381 BDDraw(this).drawVertices(vertices->mode(), vertices->vertexCount(), vertices->positions(),
382 vertices->texCoords(), vertices->colors(), bmode,
383 vertices->indices(), vertices->indexCount(), paint);
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000384}
385
Mike Reed2f6b5a42017-03-19 15:04:17 -0400386void SkBitmapDevice::drawDevice(SkBaseDevice* device, int x, int y, const SkPaint& paint) {
reedcf5c8462016-07-20 12:28:40 -0700387 SkASSERT(!paint.getImageFilter());
Mike Reeda1361362017-03-07 09:37:29 -0500388 BDDraw(this).drawSprite(static_cast<SkBitmapDevice*>(device)->fBitmap, x, y, paint);
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000389}
390
reede51c3562016-07-19 14:33:20 -0700391///////////////////////////////////////////////////////////////////////////////
392
Mike Reeda1361362017-03-07 09:37:29 -0500393void SkBitmapDevice::drawSpecial(SkSpecialImage* srcImg, int x, int y,
reede51c3562016-07-19 14:33:20 -0700394 const SkPaint& paint) {
395 SkASSERT(!srcImg->isTextureBacked());
396
397 SkBitmap resultBM;
398
399 SkImageFilter* filter = paint.getImageFilter();
400 if (filter) {
401 SkIPoint offset = SkIPoint::Make(0, 0);
Mike Reeda1361362017-03-07 09:37:29 -0500402 SkMatrix matrix = this->ctm();
reede51c3562016-07-19 14:33:20 -0700403 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
Mike Reeda1361362017-03-07 09:37:29 -0500404 const SkIRect clipBounds = fRCStack.rc().getBounds().makeOffset(-x, -y);
Hal Canary704cd322016-11-07 14:13:52 -0500405 sk_sp<SkImageFilterCache> cache(this->getImageFilterCache());
brianosman2a75e5d2016-09-22 07:15:37 -0700406 SkImageFilter::OutputProperties outputProperties(fBitmap.colorSpace());
407 SkImageFilter::Context ctx(matrix, clipBounds, cache.get(), outputProperties);
liyuqian2add0ff2016-10-20 11:04:39 -0700408
reede51c3562016-07-19 14:33:20 -0700409 sk_sp<SkSpecialImage> resultImg(filter->filterImage(srcImg, ctx, &offset));
410 if (resultImg) {
411 SkPaint tmpUnfiltered(paint);
412 tmpUnfiltered.setImageFilter(nullptr);
413 if (resultImg->getROPixels(&resultBM)) {
Mike Reeda1361362017-03-07 09:37:29 -0500414 this->drawSprite(resultBM, x + offset.x(), y + offset.y(), tmpUnfiltered);
reede51c3562016-07-19 14:33:20 -0700415 }
416 }
417 } else {
418 if (srcImg->getROPixels(&resultBM)) {
Mike Reeda1361362017-03-07 09:37:29 -0500419 this->drawSprite(resultBM, x, y, paint);
reede51c3562016-07-19 14:33:20 -0700420 }
421 }
422}
423
424sk_sp<SkSpecialImage> SkBitmapDevice::makeSpecial(const SkBitmap& bitmap) {
425 return SkSpecialImage::MakeFromRaster(bitmap.bounds(), bitmap);
426}
427
428sk_sp<SkSpecialImage> SkBitmapDevice::makeSpecial(const SkImage* image) {
429 return SkSpecialImage::MakeFromImage(SkIRect::MakeWH(image->width(), image->height()),
Brian Osman61624f02016-12-09 14:51:59 -0500430 image->makeNonTextureImage(), fBitmap.colorSpace());
reede51c3562016-07-19 14:33:20 -0700431}
432
433sk_sp<SkSpecialImage> SkBitmapDevice::snapSpecial() {
434 return this->makeSpecial(fBitmap);
435}
436
437///////////////////////////////////////////////////////////////////////////////
438
reede8f30622016-03-23 18:59:25 -0700439sk_sp<SkSurface> SkBitmapDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
440 return SkSurface::MakeRaster(info, &props);
reed@google.com76f10a32014-02-05 15:32:21 +0000441}
442
senorblanco900c3672016-04-27 11:31:23 -0700443SkImageFilterCache* SkBitmapDevice::getImageFilterCache() {
444 SkImageFilterCache* cache = SkImageFilterCache::Get();
senorblanco55b6d8b2014-07-30 11:26:46 -0700445 cache->ref();
446 return cache;
447}
448
Mike Reed7627fa52017-02-08 10:07:53 -0500449///////////////////////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000450
reedb2db8982014-11-13 12:41:02 -0800451bool SkBitmapDevice::onShouldDisableLCD(const SkPaint& paint) const {
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000452 if (kN32_SkColorType != fBitmap.colorType() ||
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000453 paint.getRasterizer() ||
454 paint.getPathEffect() ||
455 paint.isFakeBoldText() ||
456 paint.getStyle() != SkPaint::kFill_Style ||
reed374772b2016-10-05 17:33:02 -0700457 !paint.isSrcOver())
reedb2db8982014-11-13 12:41:02 -0800458 {
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000459 return true;
460 }
robertphillips@google.com53238bc2013-08-30 13:12:10 +0000461 return false;
462}
Mike Reed7627fa52017-02-08 10:07:53 -0500463
464///////////////////////////////////////////////////////////////////////////////////////////////////
465
466void SkBitmapDevice::onSave() {
Mike Reedc42a1cd2017-02-14 14:25:14 -0500467 fRCStack.save();
Mike Reed7627fa52017-02-08 10:07:53 -0500468}
469
470void SkBitmapDevice::onRestore() {
Mike Reedc42a1cd2017-02-14 14:25:14 -0500471 fRCStack.restore();
Mike Reed7627fa52017-02-08 10:07:53 -0500472}
473
474void SkBitmapDevice::onClipRect(const SkRect& rect, SkClipOp op, bool aa) {
Mike Reedc42a1cd2017-02-14 14:25:14 -0500475 fRCStack.clipRect(this->ctm(), rect, op, aa);
Mike Reed7627fa52017-02-08 10:07:53 -0500476}
477
478void SkBitmapDevice::onClipRRect(const SkRRect& rrect, SkClipOp op, bool aa) {
Mike Reedc42a1cd2017-02-14 14:25:14 -0500479 fRCStack.clipRRect(this->ctm(), rrect, op, aa);
Mike Reed7627fa52017-02-08 10:07:53 -0500480}
481
482void SkBitmapDevice::onClipPath(const SkPath& path, SkClipOp op, bool aa) {
Mike Reedc42a1cd2017-02-14 14:25:14 -0500483 fRCStack.clipPath(this->ctm(), path, op, aa);
Mike Reed7627fa52017-02-08 10:07:53 -0500484}
485
486void SkBitmapDevice::onClipRegion(const SkRegion& rgn, SkClipOp op) {
Mike Reedc42a1cd2017-02-14 14:25:14 -0500487 SkIPoint origin = this->getOrigin();
488 SkRegion tmp;
489 const SkRegion* ptr = &rgn;
490 if (origin.fX | origin.fY) {
491 // translate from "global/canvas" coordinates to relative to this device
492 rgn.translate(-origin.fX, -origin.fY, &tmp);
493 ptr = &tmp;
494 }
495 fRCStack.clipRegion(*ptr, op);
496}
497
498void SkBitmapDevice::onSetDeviceClipRestriction(SkIRect* mutableClipRestriction) {
499 fRCStack.setDeviceClipRestriction(mutableClipRestriction);
Mike Reedd519d482017-02-16 11:04:52 -0500500 if (!mutableClipRestriction->isEmpty()) {
501 SkRegion rgn(*mutableClipRestriction);
502 fRCStack.clipRegion(rgn, SkClipOp::kIntersect);
503 }
Mike Reedc42a1cd2017-02-14 14:25:14 -0500504}
505
Mike Reeda1361362017-03-07 09:37:29 -0500506bool SkBitmapDevice::onClipIsAA() const {
507 const SkRasterClip& rc = fRCStack.rc();
508 return !rc.isEmpty() && rc.isAA();
509}
510
511void SkBitmapDevice::onAsRgnClip(SkRegion* rgn) const {
512 const SkRasterClip& rc = fRCStack.rc();
513 if (rc.isAA()) {
514 rgn->setRect(rc.getBounds());
515 } else {
516 *rgn = rc.bwRgn();
517 }
518}
519
Mike Reedc42a1cd2017-02-14 14:25:14 -0500520void SkBitmapDevice::validateDevBounds(const SkIRect& drawClipBounds) {
521#ifdef SK_DEBUG
522 const SkIRect& stackBounds = fRCStack.rc().getBounds();
523 SkASSERT(drawClipBounds == stackBounds);
524#endif
Mike Reed7627fa52017-02-08 10:07:53 -0500525}
Mike Reeda1361362017-03-07 09:37:29 -0500526
527SkBaseDevice::ClipType SkBitmapDevice::onGetClipType() const {
528 const SkRasterClip& rc = fRCStack.rc();
529 if (rc.isEmpty()) {
530 return kEmpty_ClipType;
531 } else if (rc.isRect()) {
532 return kRect_ClipType;
533 } else {
534 return kComplex_ClipType;
535 }
536}