blob: b06046a557f9a18c1d9b407b8d154b06503d57cb [file] [log] [blame]
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001/*
2 * Copyright 2011 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 "SkGpuDevice.h"
9
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +000010#include "effects/GrBicubicEffect.h"
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000011#include "effects/GrDashingEffect.h"
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000012#include "effects/GrTextureDomain.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000013#include "effects/GrSimpleTextureEffect.h"
14
15#include "GrContext.h"
16#include "GrBitmapTextContext.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000017#include "GrDistanceFieldTextContext.h"
robertphillips@google.come930a072014-04-03 00:34:27 +000018#include "GrLayerCache.h"
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +000019#include "GrPictureUtils.h"
egdanield58a0ba2014-06-11 10:30:05 -070020#include "GrStrokeInfo.h"
egdanielbbcb38d2014-06-19 10:19:29 -070021#include "GrTracing.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000022
23#include "SkGrTexturePixelRef.h"
24
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000025#include "SkDeviceImageFilterProxy.h"
26#include "SkDrawProcs.h"
27#include "SkGlyphCache.h"
28#include "SkImageFilter.h"
commit-bot@chromium.org82139702014-03-10 22:53:20 +000029#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000030#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000031#include "SkPicture.h"
robertphillipsdb539902014-07-01 08:47:04 -070032#include "SkPictureData.h"
robertphillips1ad00e42014-07-08 08:28:08 -070033#include "SkPictureRangePlayback.h"
robertphillipsc26d9912014-07-10 07:21:27 -070034#include "SkPictureReplacementPlayback.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000035#include "SkRRect.h"
36#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000037#include "SkSurface.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000038#include "SkTLazy.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000039#include "SkUtils.h"
commit-bot@chromium.org559a8832014-05-30 10:08:22 +000040#include "SkVertState.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000041#include "SkErrorInternals.h"
42
43#define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
44
45#if 0
46 extern bool (*gShouldDrawProc)();
47 #define CHECK_SHOULD_DRAW(draw, forceI) \
48 do { \
49 if (gShouldDrawProc && !gShouldDrawProc()) return; \
50 this->prepareDraw(draw, forceI); \
51 } while (0)
52#else
53 #define CHECK_SHOULD_DRAW(draw, forceI) this->prepareDraw(draw, forceI)
54#endif
55
56// This constant represents the screen alignment criterion in texels for
57// requiring texture domain clamping to prevent color bleeding when drawing
58// a sub region of a larger source image.
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000059#define COLOR_BLEED_TOLERANCE 0.001f
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000060
61#define DO_DEFERRED_CLEAR() \
62 do { \
63 if (fNeedClear) { \
64 this->clear(SK_ColorTRANSPARENT); \
65 } \
66 } while (false) \
67
68///////////////////////////////////////////////////////////////////////////////
69
70#define CHECK_FOR_ANNOTATION(paint) \
71 do { if (paint.getAnnotation()) { return; } } while (0)
72
73///////////////////////////////////////////////////////////////////////////////
74
75
76class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
77public:
78 SkAutoCachedTexture()
79 : fDevice(NULL)
80 , fTexture(NULL) {
81 }
82
83 SkAutoCachedTexture(SkGpuDevice* device,
84 const SkBitmap& bitmap,
85 const GrTextureParams* params,
86 GrTexture** texture)
87 : fDevice(NULL)
88 , fTexture(NULL) {
89 SkASSERT(NULL != texture);
90 *texture = this->set(device, bitmap, params);
91 }
92
93 ~SkAutoCachedTexture() {
94 if (NULL != fTexture) {
95 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
96 }
97 }
98
99 GrTexture* set(SkGpuDevice* device,
100 const SkBitmap& bitmap,
101 const GrTextureParams* params) {
102 if (NULL != fTexture) {
103 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
104 fTexture = NULL;
105 }
106 fDevice = device;
107 GrTexture* result = (GrTexture*)bitmap.getTexture();
108 if (NULL == result) {
109 // Cannot return the native texture so look it up in our cache
110 fTexture = GrLockAndRefCachedBitmapTexture(device->context(), bitmap, params);
111 result = fTexture;
112 }
113 return result;
114 }
115
116private:
117 SkGpuDevice* fDevice;
118 GrTexture* fTexture;
119};
120
121///////////////////////////////////////////////////////////////////////////////
122
123struct GrSkDrawProcs : public SkDrawProcs {
124public:
125 GrContext* fContext;
126 GrTextContext* fTextContext;
127 GrFontScaler* fFontScaler; // cached in the skia glyphcache
128};
129
130///////////////////////////////////////////////////////////////////////////////
131
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000132SkGpuDevice* SkGpuDevice::Create(GrSurface* surface, unsigned flags) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000133 SkASSERT(NULL != surface);
134 if (NULL == surface->asRenderTarget() || NULL == surface->getContext()) {
135 return NULL;
136 }
137 if (surface->asTexture()) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000138 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asTexture(), flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000139 } else {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000140 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asRenderTarget(), flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000141 }
142}
143
reed89443ab2014-06-27 11:34:19 -0700144SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture, unsigned flags) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000145 this->initFromRenderTarget(context, texture->asRenderTarget(), flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000146}
147
reed89443ab2014-06-27 11:34:19 -0700148SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget, unsigned flags) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000149 this->initFromRenderTarget(context, renderTarget, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000150}
151
152void SkGpuDevice::initFromRenderTarget(GrContext* context,
153 GrRenderTarget* renderTarget,
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000154 unsigned flags) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000155 fDrawProcs = NULL;
156
157 fContext = context;
158 fContext->ref();
159
160 fRenderTarget = NULL;
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000161 fNeedClear = flags & kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000162
163 SkASSERT(NULL != renderTarget);
164 fRenderTarget = renderTarget;
165 fRenderTarget->ref();
166
167 // Hold onto to the texture in the pixel ref (if there is one) because the texture holds a ref
168 // on the RT but not vice-versa.
169 // TODO: Remove this trickery once we figure out how to make SkGrPixelRef do this without
170 // busting chrome (for a currently unknown reason).
171 GrSurface* surface = fRenderTarget->asTexture();
172 if (NULL == surface) {
173 surface = fRenderTarget;
174 }
reed@google.combf790232013-12-13 19:45:58 +0000175
reed6c225732014-06-09 19:52:07 -0700176 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef,
177 (surface->info(), surface, SkToBool(flags & kCached_Flag)));
reed89443ab2014-06-27 11:34:19 -0700178 fLegacyBitmap.setInfo(surface->info());
179 fLegacyBitmap.setPixelRef(pr)->unref();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700180
181 bool useDFFonts = !!(flags & kDFFonts_Flag);
182 fMainTextContext = fContext->createTextContext(fRenderTarget, fLeakyProperties, useDFFonts);
183 fFallbackTextContext = SkNEW_ARGS(GrBitmapTextContext, (fContext, fLeakyProperties));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000184}
185
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000186SkGpuDevice* SkGpuDevice::Create(GrContext* context, const SkImageInfo& origInfo,
187 int sampleCount) {
188 if (kUnknown_SkColorType == origInfo.colorType() ||
189 origInfo.width() < 0 || origInfo.height() < 0) {
190 return NULL;
191 }
192
193 SkImageInfo info = origInfo;
194 // TODO: perhas we can loosen this check now that colortype is more detailed
195 // e.g. can we support both RGBA and BGRA here?
196 if (kRGB_565_SkColorType == info.colorType()) {
197 info.fAlphaType = kOpaque_SkAlphaType; // force this setting
198 } else {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000199 info.fColorType = kN32_SkColorType;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000200 if (kOpaque_SkAlphaType != info.alphaType()) {
201 info.fAlphaType = kPremul_SkAlphaType; // force this setting
202 }
203 }
204
205 GrTextureDesc desc;
206 desc.fFlags = kRenderTarget_GrTextureFlagBit;
207 desc.fWidth = info.width();
208 desc.fHeight = info.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000209 desc.fConfig = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000210 desc.fSampleCnt = sampleCount;
211
212 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0));
213 if (!texture.get()) {
214 return NULL;
215 }
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000216
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000217 return SkNEW_ARGS(SkGpuDevice, (context, texture.get()));
218}
219
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000220SkGpuDevice::~SkGpuDevice() {
221 if (fDrawProcs) {
222 delete fDrawProcs;
223 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000224
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000225 delete fMainTextContext;
226 delete fFallbackTextContext;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000227
228 // The GrContext takes a ref on the target. We don't want to cause the render
229 // target to be unnecessarily kept alive.
230 if (fContext->getRenderTarget() == fRenderTarget) {
231 fContext->setRenderTarget(NULL);
232 }
233
234 if (fContext->getClip() == &fClipData) {
235 fContext->setClip(NULL);
236 }
237
238 SkSafeUnref(fRenderTarget);
239 fContext->unref();
240}
241
242///////////////////////////////////////////////////////////////////////////////
243
244void SkGpuDevice::makeRenderTargetCurrent() {
245 DO_DEFERRED_CLEAR();
246 fContext->setRenderTarget(fRenderTarget);
247}
248
249///////////////////////////////////////////////////////////////////////////////
250
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000251bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
252 int x, int y) {
253 DO_DEFERRED_CLEAR();
254
255 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000256 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000257 if (kUnknown_GrPixelConfig == config) {
258 return false;
259 }
260
261 uint32_t flags = 0;
262 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
263 flags = GrContext::kUnpremul_PixelOpsFlag;
264 }
265 return fContext->readRenderTargetPixels(fRenderTarget, x, y, dstInfo.width(), dstInfo.height(),
266 config, dstPixels, dstRowBytes, flags);
267}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000268
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000269bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
270 int x, int y) {
271 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000272 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000273 if (kUnknown_GrPixelConfig == config) {
274 return false;
275 }
276 uint32_t flags = 0;
277 if (kUnpremul_SkAlphaType == info.alphaType()) {
278 flags = GrContext::kUnpremul_PixelOpsFlag;
279 }
280 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
281
282 // need to bump our genID for compatibility with clients that "know" we have a bitmap
reed89443ab2014-06-27 11:34:19 -0700283 fLegacyBitmap.notifyPixelsChanged();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000284
285 return true;
286}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000287
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000288const SkBitmap& SkGpuDevice::onAccessBitmap() {
289 DO_DEFERRED_CLEAR();
reed89443ab2014-06-27 11:34:19 -0700290 return fLegacyBitmap;
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000291}
292
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000293void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
294 INHERITED::onAttachToCanvas(canvas);
295
296 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
297 fClipData.fClipStack = canvas->getClipStack();
298}
299
300void SkGpuDevice::onDetachFromCanvas() {
301 INHERITED::onDetachFromCanvas();
302 fClipData.fClipStack = NULL;
303}
304
305// call this every draw call, to ensure that the context reflects our state,
306// and not the state from some other canvas/device
307void SkGpuDevice::prepareDraw(const SkDraw& draw, bool forceIdentity) {
308 SkASSERT(NULL != fClipData.fClipStack);
309
310 fContext->setRenderTarget(fRenderTarget);
311
312 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
313
314 if (forceIdentity) {
315 fContext->setIdentityMatrix();
316 } else {
317 fContext->setMatrix(*draw.fMatrix);
318 }
319 fClipData.fOrigin = this->getOrigin();
320
321 fContext->setClip(&fClipData);
322
323 DO_DEFERRED_CLEAR();
324}
325
326GrRenderTarget* SkGpuDevice::accessRenderTarget() {
327 DO_DEFERRED_CLEAR();
328 return fRenderTarget;
329}
330
331///////////////////////////////////////////////////////////////////////////////
332
333SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
334SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
335SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
336SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
337SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
338 shader_type_mismatch);
339SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
340 shader_type_mismatch);
341SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
342SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
343
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000344///////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000345
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000346void SkGpuDevice::clear(SkColor color) {
egdanield78a1682014-07-09 10:41:26 -0700347 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::clear", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000348 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
349 fContext->clear(&rect, SkColor2GrColor(color), true, fRenderTarget);
350 fNeedClear = false;
351}
352
353void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
354 CHECK_SHOULD_DRAW(draw, false);
egdanield78a1682014-07-09 10:41:26 -0700355 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPaint", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000356
357 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000358 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000359
360 fContext->drawPaint(grPaint);
361}
362
363// must be in SkCanvas::PointMode order
364static const GrPrimitiveType gPointMode2PrimtiveType[] = {
365 kPoints_GrPrimitiveType,
366 kLines_GrPrimitiveType,
367 kLineStrip_GrPrimitiveType
368};
369
370void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
371 size_t count, const SkPoint pts[], const SkPaint& paint) {
372 CHECK_FOR_ANNOTATION(paint);
373 CHECK_SHOULD_DRAW(draw, false);
374
375 SkScalar width = paint.getStrokeWidth();
376 if (width < 0) {
377 return;
378 }
379
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000380 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
egdaniele61c4112014-06-12 10:24:21 -0700381 GrStrokeInfo strokeInfo(paint, SkPaint::kStroke_Style);
382 GrPaint grPaint;
383 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
384 SkPath path;
385 path.moveTo(pts[0]);
386 path.lineTo(pts[1]);
387 fContext->drawPath(grPaint, path, strokeInfo);
388 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000389 }
390
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000391 // we only handle hairlines and paints without path effects or mask filters,
392 // else we let the SkDraw call our drawPath()
393 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
394 draw.drawPoints(mode, count, pts, paint, true);
395 return;
396 }
397
398 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000399 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000400
401 fContext->drawVertices(grPaint,
402 gPointMode2PrimtiveType[mode],
robertphillips@google.coma4662862013-11-21 14:24:16 +0000403 SkToS32(count),
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000404 (SkPoint*)pts,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000405 NULL,
406 NULL,
407 NULL,
408 0);
409}
410
411///////////////////////////////////////////////////////////////////////////////
412
413void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
414 const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -0700415 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRect", fContext);
416
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000417 CHECK_FOR_ANNOTATION(paint);
418 CHECK_SHOULD_DRAW(draw, false);
419
420 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
421 SkScalar width = paint.getStrokeWidth();
422
423 /*
424 We have special code for hairline strokes, miter-strokes, bevel-stroke
425 and fills. Anything else we just call our path code.
426 */
427 bool usePath = doStroke && width > 0 &&
428 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
429 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
430 // another two reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700431
432 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000433 usePath = true;
434 }
egdanield58a0ba2014-06-11 10:30:05 -0700435
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000436 if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) {
437#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
438 if (doStroke) {
439#endif
440 usePath = true;
441#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
442 } else {
443 usePath = !fContext->getMatrix().preservesRightAngles();
444 }
445#endif
446 }
447 // until we can both stroke and fill rectangles
448 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
449 usePath = true;
450 }
451
egdanield58a0ba2014-06-11 10:30:05 -0700452 GrStrokeInfo strokeInfo(paint);
453
454 const SkPathEffect* pe = paint.getPathEffect();
455 if (!usePath && NULL != pe && !strokeInfo.isDashed()) {
456 usePath = true;
457 }
458
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000459 if (usePath) {
460 SkPath path;
461 path.addRect(rect);
462 this->drawPath(draw, path, paint, NULL, true);
463 return;
464 }
465
466 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000467 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
Mike Klein744fb732014-06-23 15:13:26 -0400468
egdanield58a0ba2014-06-11 10:30:05 -0700469 fContext->drawRect(grPaint, rect, &strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000470}
471
472///////////////////////////////////////////////////////////////////////////////
473
474void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
475 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700476 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRRect", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000477 CHECK_FOR_ANNOTATION(paint);
478 CHECK_SHOULD_DRAW(draw, false);
479
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000480 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000481 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
Mike Klein744fb732014-06-23 15:13:26 -0400482
egdanield58a0ba2014-06-11 10:30:05 -0700483 GrStrokeInfo strokeInfo(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000484 if (paint.getMaskFilter()) {
485 // try to hit the fast path for drawing filtered round rects
486
487 SkRRect devRRect;
488 if (rect.transform(fContext->getMatrix(), &devRRect)) {
489 if (devRRect.allCornersCircular()) {
490 SkRect maskRect;
491 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(),
492 draw.fClip->getBounds(),
493 fContext->getMatrix(),
494 &maskRect)) {
495 SkIRect finalIRect;
496 maskRect.roundOut(&finalIRect);
497 if (draw.fClip->quickReject(finalIRect)) {
498 // clipped out
499 return;
500 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000501 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext, &grPaint,
egdanield58a0ba2014-06-11 10:30:05 -0700502 strokeInfo.getStrokeRec(),
503 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000504 return;
505 }
506 }
507
508 }
509 }
510
511 }
512
egdanield58a0ba2014-06-11 10:30:05 -0700513 bool usePath = false;
514
515 if (paint.getMaskFilter()) {
516 usePath = true;
517 } else {
518 const SkPathEffect* pe = paint.getPathEffect();
519 if (NULL != pe && !strokeInfo.isDashed()) {
520 usePath = true;
521 }
522 }
523
524
525 if (usePath) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000526 SkPath path;
527 path.addRRect(rect);
528 this->drawPath(draw, path, paint, NULL, true);
529 return;
530 }
Mike Klein744fb732014-06-23 15:13:26 -0400531
egdanield58a0ba2014-06-11 10:30:05 -0700532 fContext->drawRRect(grPaint, rect, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000533}
534
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000535void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
536 const SkRRect& inner, const SkPaint& paint) {
537 SkStrokeRec stroke(paint);
538 if (stroke.isFillStyle()) {
539
540 CHECK_FOR_ANNOTATION(paint);
541 CHECK_SHOULD_DRAW(draw, false);
542
543 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000544 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000545
546 if (NULL == paint.getMaskFilter() && NULL == paint.getPathEffect()) {
547 fContext->drawDRRect(grPaint, outer, inner);
548 return;
549 }
550 }
551
552 SkPath path;
553 path.addRRect(outer);
554 path.addRRect(inner);
555 path.setFillType(SkPath::kEvenOdd_FillType);
556
557 this->drawPath(draw, path, paint, NULL, true);
558}
559
560
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000561/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000562
563void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
564 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700565 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawOval", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000566 CHECK_FOR_ANNOTATION(paint);
567 CHECK_SHOULD_DRAW(draw, false);
568
egdanield58a0ba2014-06-11 10:30:05 -0700569 GrStrokeInfo strokeInfo(paint);
570
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000571 bool usePath = false;
572 // some basic reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700573 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000574 usePath = true;
egdanield58a0ba2014-06-11 10:30:05 -0700575 } else {
576 const SkPathEffect* pe = paint.getPathEffect();
577 if (NULL != pe && !strokeInfo.isDashed()) {
578 usePath = true;
579 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000580 }
581
582 if (usePath) {
583 SkPath path;
584 path.addOval(oval);
585 this->drawPath(draw, path, paint, NULL, true);
586 return;
587 }
588
589 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000590 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000591
egdanield58a0ba2014-06-11 10:30:05 -0700592 fContext->drawOval(grPaint, oval, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000593}
594
595#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000596
597///////////////////////////////////////////////////////////////////////////////
598
599// helpers for applying mask filters
600namespace {
601
602// Draw a mask using the supplied paint. Since the coverage/geometry
603// is already burnt into the mask this boils down to a rect draw.
604// Return true if the mask was successfully drawn.
605bool draw_mask(GrContext* context, const SkRect& maskRect,
606 GrPaint* grp, GrTexture* mask) {
607 GrContext::AutoMatrix am;
608 if (!am.setIdentity(context, grp)) {
609 return false;
610 }
611
612 SkMatrix matrix;
613 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
614 matrix.postIDiv(mask->width(), mask->height());
615
616 grp->addCoverageEffect(GrSimpleTextureEffect::Create(mask, matrix))->unref();
617 context->drawRect(*grp, maskRect);
618 return true;
619}
620
621bool draw_with_mask_filter(GrContext* context, const SkPath& devPath,
reed868074b2014-06-03 10:53:59 -0700622 SkMaskFilter* filter, const SkRegion& clip,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000623 GrPaint* grp, SkPaint::Style style) {
624 SkMask srcM, dstM;
625
626 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
627 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
628 return false;
629 }
630 SkAutoMaskFreeImage autoSrc(srcM.fImage);
631
632 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
633 return false;
634 }
635 // this will free-up dstM when we're done (allocated in filterMask())
636 SkAutoMaskFreeImage autoDst(dstM.fImage);
637
638 if (clip.quickReject(dstM.fBounds)) {
639 return false;
640 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000641
642 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
643 // the current clip (and identity matrix) and GrPaint settings
644 GrTextureDesc desc;
645 desc.fWidth = dstM.fBounds.width();
646 desc.fHeight = dstM.fBounds.height();
647 desc.fConfig = kAlpha_8_GrPixelConfig;
648
649 GrAutoScratchTexture ast(context, desc);
650 GrTexture* texture = ast.texture();
651
652 if (NULL == texture) {
653 return false;
654 }
655 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
656 dstM.fImage, dstM.fRowBytes);
657
658 SkRect maskRect = SkRect::Make(dstM.fBounds);
659
660 return draw_mask(context, maskRect, grp, texture);
661}
662
663// Create a mask of 'devPath' and place the result in 'mask'. Return true on
664// success; false otherwise.
665bool create_mask_GPU(GrContext* context,
666 const SkRect& maskRect,
667 const SkPath& devPath,
egdanield58a0ba2014-06-11 10:30:05 -0700668 const GrStrokeInfo& strokeInfo,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000669 bool doAA,
670 GrAutoScratchTexture* mask) {
671 GrTextureDesc desc;
672 desc.fFlags = kRenderTarget_GrTextureFlagBit;
673 desc.fWidth = SkScalarCeilToInt(maskRect.width());
674 desc.fHeight = SkScalarCeilToInt(maskRect.height());
675 // We actually only need A8, but it often isn't supported as a
676 // render target so default to RGBA_8888
677 desc.fConfig = kRGBA_8888_GrPixelConfig;
678 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
679 desc.fConfig = kAlpha_8_GrPixelConfig;
680 }
681
682 mask->set(context, desc);
683 if (NULL == mask->texture()) {
684 return false;
685 }
686
687 GrTexture* maskTexture = mask->texture();
688 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
689
690 GrContext::AutoRenderTarget art(context, maskTexture->asRenderTarget());
691 GrContext::AutoClip ac(context, clipRect);
692
693 context->clear(NULL, 0x0, true);
694
695 GrPaint tempPaint;
696 if (doAA) {
697 tempPaint.setAntiAlias(true);
698 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
699 // blend coeff of zero requires dual source blending support in order
700 // to properly blend partially covered pixels. This means the AA
701 // code path may not be taken. So we use a dst blend coeff of ISA. We
702 // could special case AA draws to a dst surface with known alpha=0 to
703 // use a zero dst coeff when dual source blending isn't available.
704 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
705 }
706
707 GrContext::AutoMatrix am;
708
709 // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint.
710 SkMatrix translate;
711 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
712 am.set(context, translate);
egdanield58a0ba2014-06-11 10:30:05 -0700713 context->drawPath(tempPaint, devPath, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000714 return true;
715}
716
717SkBitmap wrap_texture(GrTexture* texture) {
718 SkBitmap result;
reed6c225732014-06-09 19:52:07 -0700719 result.setInfo(texture->info());
720 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (result.info(), texture)))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000721 return result;
722}
723
724};
725
726void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
727 const SkPaint& paint, const SkMatrix* prePathMatrix,
728 bool pathIsMutable) {
729 CHECK_FOR_ANNOTATION(paint);
730 CHECK_SHOULD_DRAW(draw, false);
egdanield78a1682014-07-09 10:41:26 -0700731 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPath", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000732
733 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000734 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000735
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000736 // If we have a prematrix, apply it to the path, optimizing for the case
737 // where the original path can in fact be modified in place (even though
738 // its parameter type is const).
739 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000740 SkTLazy<SkPath> tmpPath;
741 SkTLazy<SkPath> effectPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000742
743 if (prePathMatrix) {
744 SkPath* result = pathPtr;
745
746 if (!pathIsMutable) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000747 result = tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000748 pathIsMutable = true;
749 }
750 // should I push prePathMatrix on our MV stack temporarily, instead
751 // of applying it here? See SkDraw.cpp
752 pathPtr->transform(*prePathMatrix, result);
753 pathPtr = result;
754 }
755 // at this point we're done with prePathMatrix
756 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
757
egdanield58a0ba2014-06-11 10:30:05 -0700758 GrStrokeInfo strokeInfo(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000759 SkPathEffect* pathEffect = paint.getPathEffect();
760 const SkRect* cullRect = NULL; // TODO: what is our bounds?
egdanield58a0ba2014-06-11 10:30:05 -0700761 SkStrokeRec* strokePtr = strokeInfo.getStrokeRecPtr();
762 if (pathEffect && pathEffect->filterPath(effectPath.init(), *pathPtr, strokePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000763 cullRect)) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000764 pathPtr = effectPath.get();
765 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700766 strokeInfo.removeDash();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000767 }
768
egdanield58a0ba2014-06-11 10:30:05 -0700769 const SkStrokeRec& stroke = strokeInfo.getStrokeRec();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000770 if (paint.getMaskFilter()) {
771 if (!stroke.isHairlineStyle()) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000772 SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init();
773 if (stroke.applyToPath(strokedPath, *pathPtr)) {
774 pathPtr = strokedPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000775 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700776 strokeInfo.setFillStyle();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000777 }
778 }
779
780 // avoid possibly allocating a new path in transform if we can
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000781 SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000782
783 // transform the path into device space
784 pathPtr->transform(fContext->getMatrix(), devPathPtr);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000785
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000786 SkRect maskRect;
787 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
788 draw.fClip->getBounds(),
789 fContext->getMatrix(),
790 &maskRect)) {
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000791 // The context's matrix may change while creating the mask, so save the CTM here to
792 // pass to filterMaskGPU.
793 const SkMatrix ctm = fContext->getMatrix();
794
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000795 SkIRect finalIRect;
796 maskRect.roundOut(&finalIRect);
797 if (draw.fClip->quickReject(finalIRect)) {
798 // clipped out
799 return;
800 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000801
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000802 if (paint.getMaskFilter()->directFilterMaskGPU(fContext, &grPaint,
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000803 stroke, *devPathPtr)) {
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000804 // the mask filter was able to draw itself directly, so there's nothing
805 // left to do.
806 return;
807 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000808
809 GrAutoScratchTexture mask;
810
egdanield58a0ba2014-06-11 10:30:05 -0700811 if (create_mask_GPU(fContext, maskRect, *devPathPtr, strokeInfo,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000812 grPaint.isAntiAlias(), &mask)) {
813 GrTexture* filtered;
814
commit-bot@chromium.org41bf9302014-01-08 22:25:53 +0000815 if (paint.getMaskFilter()->filterMaskGPU(mask.texture(),
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000816 ctm, maskRect, &filtered, true)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000817 // filterMaskGPU gives us ownership of a ref to the result
818 SkAutoTUnref<GrTexture> atu(filtered);
819
820 // If the scratch texture that we used as the filter src also holds the filter
821 // result then we must detach so that this texture isn't recycled for a later
822 // draw.
823 if (filtered == mask.texture()) {
824 mask.detach();
825 filtered->unref(); // detach transfers GrAutoScratchTexture's ref to us.
826 }
827
828 if (draw_mask(fContext, maskRect, &grPaint, filtered)) {
829 // This path is completely drawn
830 return;
831 }
832 }
833 }
834 }
835
836 // draw the mask on the CPU - this is a fallthrough path in case the
837 // GPU path fails
838 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
839 SkPaint::kFill_Style;
egdanield58a0ba2014-06-11 10:30:05 -0700840 draw_with_mask_filter(fContext, *devPathPtr, paint.getMaskFilter(),
841 *draw.fClip, &grPaint, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000842 return;
843 }
844
egdanield58a0ba2014-06-11 10:30:05 -0700845 fContext->drawPath(grPaint, *pathPtr, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000846}
847
848static const int kBmpSmallTileSize = 1 << 10;
849
850static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
851 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
852 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
853 return tilesX * tilesY;
854}
855
856static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
857 if (maxTileSize <= kBmpSmallTileSize) {
858 return maxTileSize;
859 }
860
861 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
862 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
863
864 maxTileTotalTileSize *= maxTileSize * maxTileSize;
865 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
866
867 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
868 return kBmpSmallTileSize;
869 } else {
870 return maxTileSize;
871 }
872}
873
874// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
875// pixels from the bitmap are necessary.
876static void determine_clipped_src_rect(const GrContext* context,
877 const SkBitmap& bitmap,
878 const SkRect* srcRectPtr,
879 SkIRect* clippedSrcIRect) {
880 const GrClipData* clip = context->getClip();
881 clip->getConservativeBounds(context->getRenderTarget(), clippedSrcIRect, NULL);
882 SkMatrix inv;
883 if (!context->getMatrix().invert(&inv)) {
884 clippedSrcIRect->setEmpty();
885 return;
886 }
887 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
888 inv.mapRect(&clippedSrcRect);
889 if (NULL != srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000890 // we've setup src space 0,0 to map to the top left of the src rect.
891 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000892 if (!clippedSrcRect.intersect(*srcRectPtr)) {
893 clippedSrcIRect->setEmpty();
894 return;
895 }
896 }
897 clippedSrcRect.roundOut(clippedSrcIRect);
898 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
899 if (!clippedSrcIRect->intersect(bmpBounds)) {
900 clippedSrcIRect->setEmpty();
901 }
902}
903
904bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
905 const GrTextureParams& params,
906 const SkRect* srcRectPtr,
907 int maxTileSize,
908 int* tileSize,
909 SkIRect* clippedSrcRect) const {
910 // if bitmap is explictly texture backed then just use the texture
911 if (NULL != bitmap.getTexture()) {
912 return false;
913 }
914
915 // if it's larger than the max tile size, then we have no choice but tiling.
916 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
917 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
918 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
919 return true;
920 }
921
922 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
923 return false;
924 }
925
926 // if the entire texture is already in our cache then no reason to tile it
927 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
928 return false;
929 }
930
931 // At this point we know we could do the draw by uploading the entire bitmap
932 // as a texture. However, if the texture would be large compared to the
933 // cache size and we don't require most of it for this draw then tile to
934 // reduce the amount of upload and cache spill.
935
936 // assumption here is that sw bitmap size is a good proxy for its size as
937 // a texture
938 size_t bmpSize = bitmap.getSize();
939 size_t cacheSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000940 fContext->getResourceCacheLimits(NULL, &cacheSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000941 if (bmpSize < cacheSize / 2) {
942 return false;
943 }
944
945 // Figure out how much of the src we will need based on the src rect and clipping.
946 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
947 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
948 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
949 kBmpSmallTileSize * kBmpSmallTileSize;
950
951 return usedTileBytes < 2 * bmpSize;
952}
953
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000954void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000955 const SkBitmap& bitmap,
956 const SkMatrix& m,
957 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000958 SkMatrix concat;
959 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
960 if (!m.isIdentity()) {
961 concat.setConcat(*draw->fMatrix, m);
962 draw.writable()->fMatrix = &concat;
963 }
964 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000965}
966
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000967// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000968// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
969// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000970static inline void clamped_outset_with_offset(SkIRect* iRect,
971 int outset,
972 SkPoint* offset,
973 const SkIRect& clamp) {
974 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000975
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000976 int leftClampDelta = clamp.fLeft - iRect->fLeft;
977 if (leftClampDelta > 0) {
978 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000979 iRect->fLeft = clamp.fLeft;
980 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000981 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000982 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000983
984 int topClampDelta = clamp.fTop - iRect->fTop;
985 if (topClampDelta > 0) {
986 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000987 iRect->fTop = clamp.fTop;
988 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000989 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000990 }
991
992 if (iRect->fRight > clamp.fRight) {
993 iRect->fRight = clamp.fRight;
994 }
995 if (iRect->fBottom > clamp.fBottom) {
996 iRect->fBottom = clamp.fBottom;
997 }
998}
999
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001000static bool has_aligned_samples(const SkRect& srcRect,
1001 const SkRect& transformedRect) {
1002 // detect pixel disalignment
1003 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1004 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1005 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1006 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1007 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1008 COLOR_BLEED_TOLERANCE &&
1009 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1010 COLOR_BLEED_TOLERANCE) {
1011 return true;
1012 }
1013 return false;
1014}
1015
1016static bool may_color_bleed(const SkRect& srcRect,
1017 const SkRect& transformedRect,
1018 const SkMatrix& m) {
1019 // Only gets called if has_aligned_samples returned false.
1020 // So we can assume that sampling is axis aligned but not texel aligned.
1021 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
1022 SkRect innerSrcRect(srcRect), innerTransformedRect,
1023 outerTransformedRect(transformedRect);
1024 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1025 m.mapRect(&innerTransformedRect, innerSrcRect);
1026
1027 // The gap between outerTransformedRect and innerTransformedRect
1028 // represents the projection of the source border area, which is
1029 // problematic for color bleeding. We must check whether any
1030 // destination pixels sample the border area.
1031 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1032 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1033 SkIRect outer, inner;
1034 outerTransformedRect.round(&outer);
1035 innerTransformedRect.round(&inner);
1036 // If the inner and outer rects round to the same result, it means the
1037 // border does not overlap any pixel centers. Yay!
1038 return inner != outer;
1039}
1040
1041static bool needs_texture_domain(const SkBitmap& bitmap,
1042 const SkRect& srcRect,
1043 GrTextureParams &params,
1044 const SkMatrix& contextMatrix,
1045 bool bicubic) {
1046 bool needsTextureDomain = false;
1047
1048 if (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode) {
1049 // Need texture domain if drawing a sub rect
1050 needsTextureDomain = srcRect.width() < bitmap.width() ||
1051 srcRect.height() < bitmap.height();
1052 if (!bicubic && needsTextureDomain && contextMatrix.rectStaysRect()) {
1053 // sampling is axis-aligned
1054 SkRect transformedRect;
1055 contextMatrix.mapRect(&transformedRect, srcRect);
1056
1057 if (has_aligned_samples(srcRect, transformedRect)) {
1058 params.setFilterMode(GrTextureParams::kNone_FilterMode);
1059 needsTextureDomain = false;
1060 } else {
1061 needsTextureDomain = may_color_bleed(srcRect, transformedRect, contextMatrix);
1062 }
1063 }
1064 }
1065 return needsTextureDomain;
1066}
1067
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001068void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1069 const SkBitmap& bitmap,
1070 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001071 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001072 const SkPaint& paint,
1073 SkCanvas::DrawBitmapRectFlags flags) {
1074 CHECK_SHOULD_DRAW(draw, false);
1075
1076 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001077 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001078 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
1079 // in the (easier) bleed case, so update flags.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001080 if (NULL == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001081 SkScalar w = SkIntToScalar(bitmap.width());
1082 SkScalar h = SkIntToScalar(bitmap.height());
1083 dstSize.fWidth = w;
1084 dstSize.fHeight = h;
1085 srcRect.set(0, 0, w, h);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001086 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001087 } else {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001088 SkASSERT(NULL != dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001089 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001090 dstSize = *dstSizePtr;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001091 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
1092 srcRect.fRight >= bitmap.width() && srcRect.fBottom >= bitmap.height()) {
1093 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
1094 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001095 }
1096
1097 if (paint.getMaskFilter()){
1098 // Convert the bitmap to a shader so that the rect can be drawn
1099 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001100 SkBitmap tmp; // subset of bitmap, if necessary
1101 const SkBitmap* bitmapPtr = &bitmap;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001102 SkMatrix localM;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001103 if (NULL != srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001104 localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
1105 localM.postScale(dstSize.fWidth / srcRectPtr->width(),
1106 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001107 // In bleed mode we position and trim the bitmap based on the src rect which is
1108 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
1109 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
1110 // compensate.
1111 if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) {
1112 SkIRect iSrc;
1113 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001114
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001115 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
1116 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001117
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001118 if (!bitmap.extractSubset(&tmp, iSrc)) {
1119 return; // extraction failed
1120 }
1121 bitmapPtr = &tmp;
1122 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001123
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001124 // The source rect has changed so update the matrix
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001125 localM.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001126 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001127 } else {
1128 localM.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001129 }
1130
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001131 SkPaint paintWithShader(paint);
1132 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +00001133 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &localM))->unref();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001134 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1135 this->drawRect(draw, dstRect, paintWithShader);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001136
1137 return;
1138 }
1139
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001140 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
1141 // the view matrix rather than a local matrix.
1142 SkMatrix m;
1143 m.setScale(dstSize.fWidth / srcRect.width(),
1144 dstSize.fHeight / srcRect.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001145 fContext->concatMatrix(m);
1146
1147 GrTextureParams params;
1148 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel();
1149 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001150
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001151 bool doBicubic = false;
1152
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001153 switch(paintFilterLevel) {
1154 case SkPaint::kNone_FilterLevel:
1155 textureFilterMode = GrTextureParams::kNone_FilterMode;
1156 break;
1157 case SkPaint::kLow_FilterLevel:
1158 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1159 break;
1160 case SkPaint::kMedium_FilterLevel:
commit-bot@chromium.org18786512014-05-20 14:53:45 +00001161 if (fContext->getMatrix().getMinScale() < SK_Scalar1) {
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001162 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1163 } else {
1164 // Don't trigger MIP level generation unnecessarily.
1165 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1166 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001167 break;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001168 case SkPaint::kHigh_FilterLevel:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001169 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001170 doBicubic =
1171 GrBicubicEffect::ShouldUseBicubic(fContext->getMatrix(), &textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001172 break;
1173 default:
1174 SkErrorInternals::SetError( kInvalidPaint_SkError,
1175 "Sorry, I don't understand the filtering "
1176 "mode you asked for. Falling back to "
1177 "MIPMaps.");
1178 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1179 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001180 }
1181
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001182 int tileFilterPad;
1183 if (doBicubic) {
1184 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1185 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1186 tileFilterPad = 0;
1187 } else {
1188 tileFilterPad = 1;
1189 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001190 params.setFilterMode(textureFilterMode);
1191
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001192 int maxTileSize = fContext->getMaxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001193 int tileSize;
1194
1195 SkIRect clippedSrcRect;
1196 if (this->shouldTileBitmap(bitmap, params, srcRectPtr, maxTileSize, &tileSize,
1197 &clippedSrcRect)) {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001198 this->drawTiledBitmap(bitmap, srcRect, clippedSrcRect, params, paint, flags, tileSize,
1199 doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001200 } else {
1201 // take the simple case
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001202 bool needsTextureDomain = needs_texture_domain(bitmap,
1203 srcRect,
1204 params,
1205 fContext->getMatrix(),
1206 doBicubic);
1207 this->internalDrawBitmap(bitmap,
1208 srcRect,
1209 params,
1210 paint,
1211 flags,
1212 doBicubic,
1213 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001214 }
1215}
1216
1217// Break 'bitmap' into several tiles to draw it since it has already
1218// been determined to be too large to fit in VRAM
1219void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
1220 const SkRect& srcRect,
1221 const SkIRect& clippedSrcIRect,
1222 const GrTextureParams& params,
1223 const SkPaint& paint,
1224 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001225 int tileSize,
1226 bool bicubic) {
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +00001227 // The following pixel lock is technically redundant, but it is desirable
1228 // to lock outside of the tile loop to prevent redecoding the whole image
1229 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1230 // is larger than the limit of the discardable memory pool.
1231 SkAutoLockPixels alp(bitmap);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001232 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1233
1234 int nx = bitmap.width() / tileSize;
1235 int ny = bitmap.height() / tileSize;
1236 for (int x = 0; x <= nx; x++) {
1237 for (int y = 0; y <= ny; y++) {
1238 SkRect tileR;
1239 tileR.set(SkIntToScalar(x * tileSize),
1240 SkIntToScalar(y * tileSize),
1241 SkIntToScalar((x + 1) * tileSize),
1242 SkIntToScalar((y + 1) * tileSize));
1243
1244 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1245 continue;
1246 }
1247
1248 if (!tileR.intersect(srcRect)) {
1249 continue;
1250 }
1251
1252 SkBitmap tmpB;
1253 SkIRect iTileR;
1254 tileR.roundOut(&iTileR);
1255 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1256 SkIntToScalar(iTileR.fTop));
1257
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001258 // Adjust the context matrix to draw at the right x,y in device space
1259 SkMatrix tmpM;
1260 GrContext::AutoMatrix am;
1261 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
1262 am.setPreConcat(fContext, tmpM);
1263
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001264 if (SkPaint::kNone_FilterLevel != paint.getFilterLevel() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001265 SkIRect iClampRect;
1266
1267 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1268 // In bleed mode we want to always expand the tile on all edges
1269 // but stay within the bitmap bounds
1270 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1271 } else {
1272 // In texture-domain/clamp mode we only want to expand the
1273 // tile on edges interior to "srcRect" (i.e., we want to
1274 // not bleed across the original clamped edges)
1275 srcRect.roundOut(&iClampRect);
1276 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001277 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1278 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001279 }
1280
1281 if (bitmap.extractSubset(&tmpB, iTileR)) {
1282 // now offset it to make it "local" to our tmp bitmap
1283 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001284 GrTextureParams paramsTemp = params;
1285 bool needsTextureDomain = needs_texture_domain(bitmap,
1286 srcRect,
1287 paramsTemp,
1288 fContext->getMatrix(),
1289 bicubic);
1290 this->internalDrawBitmap(tmpB,
1291 tileR,
1292 paramsTemp,
1293 paint,
1294 flags,
1295 bicubic,
1296 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001297 }
1298 }
1299 }
1300}
1301
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001302
1303/*
1304 * This is called by drawBitmap(), which has to handle images that may be too
1305 * large to be represented by a single texture.
1306 *
1307 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1308 * and that non-texture portion of the GrPaint has already been setup.
1309 */
1310void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
1311 const SkRect& srcRect,
1312 const GrTextureParams& params,
1313 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001314 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001315 bool bicubic,
1316 bool needsTextureDomain) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001317 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1318 bitmap.height() <= fContext->getMaxTextureSize());
1319
1320 GrTexture* texture;
1321 SkAutoCachedTexture act(this, bitmap, &params, &texture);
1322 if (NULL == texture) {
1323 return;
1324 }
1325
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001326 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001327 SkRect paintRect;
1328 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1329 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1330 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1331 SkScalarMul(srcRect.fTop, hInv),
1332 SkScalarMul(srcRect.fRight, wInv),
1333 SkScalarMul(srcRect.fBottom, hInv));
1334
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001335 SkRect textureDomain = SkRect::MakeEmpty();
bsalomon83d081a2014-07-08 09:56:10 -07001336 SkAutoTUnref<GrEffect> effect;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001337 if (needsTextureDomain && !(flags & SkCanvas::kBleed_DrawBitmapRectFlag)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001338 // Use a constrained texture domain to avoid color bleeding
1339 SkScalar left, top, right, bottom;
1340 if (srcRect.width() > SK_Scalar1) {
1341 SkScalar border = SK_ScalarHalf / texture->width();
1342 left = paintRect.left() + border;
1343 right = paintRect.right() - border;
1344 } else {
1345 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1346 }
1347 if (srcRect.height() > SK_Scalar1) {
1348 SkScalar border = SK_ScalarHalf / texture->height();
1349 top = paintRect.top() + border;
1350 bottom = paintRect.bottom() - border;
1351 } else {
1352 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1353 }
1354 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001355 if (bicubic) {
1356 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
1357 } else {
1358 effect.reset(GrTextureDomainEffect::Create(texture,
1359 SkMatrix::I(),
1360 textureDomain,
1361 GrTextureDomain::kClamp_Mode,
1362 params.filterMode()));
1363 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001364 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001365 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1366 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
1367 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001368 } else {
1369 effect.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
1370 }
1371
1372 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1373 // the rest from the SkPaint.
1374 GrPaint grPaint;
1375 grPaint.addColorEffect(effect);
reed0689d7b2014-06-14 05:30:20 -07001376 bool alphaOnly = !(kAlpha_8_SkColorType == bitmap.colorType());
bsalomon83d081a2014-07-08 09:56:10 -07001377 GrColor paintColor = (alphaOnly) ? SkColor2GrColorJustAlpha(paint.getColor()) :
1378 SkColor2GrColor(paint.getColor());
1379 SkPaint2GrPaintNoShader(this->context(), paint, paintColor, false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001380
1381 fContext->drawRectToRect(grPaint, dstRect, paintRect, NULL);
1382}
1383
1384static bool filter_texture(SkBaseDevice* device, GrContext* context,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001385 GrTexture* texture, const SkImageFilter* filter,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001386 int w, int h, const SkImageFilter::Context& ctx,
1387 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001388 SkASSERT(filter);
1389 SkDeviceImageFilterProxy proxy(device);
1390
1391 if (filter->canFilterImageGPU()) {
1392 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1393 // filter. Also set the clip wide open and the matrix to identity.
1394 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001395 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001396 } else {
1397 return false;
1398 }
1399}
1400
1401void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1402 int left, int top, const SkPaint& paint) {
1403 // drawSprite is defined to be in device coords.
1404 CHECK_SHOULD_DRAW(draw, true);
1405
1406 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1407 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1408 return;
1409 }
1410
1411 int w = bitmap.width();
1412 int h = bitmap.height();
1413
1414 GrTexture* texture;
1415 // draw sprite uses the default texture params
1416 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
1417
1418 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001419 // This bitmap will own the filtered result as a texture.
1420 SkBitmap filteredBitmap;
1421
1422 if (NULL != filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001423 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001424 SkMatrix matrix(*draw.fMatrix);
1425 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001426 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001427 SkImageFilter::Cache* cache = SkImageFilter::Cache::Create();
1428 SkAutoUnref aur(cache);
1429 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001430 if (filter_texture(this, fContext, texture, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001431 &offset)) {
1432 texture = (GrTexture*) filteredBitmap.getTexture();
1433 w = filteredBitmap.width();
1434 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001435 left += offset.x();
1436 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001437 } else {
1438 return;
1439 }
1440 }
1441
1442 GrPaint grPaint;
1443 grPaint.addColorTextureEffect(texture, SkMatrix::I());
1444
dandov9de5b512014-06-10 14:38:28 -07001445 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColorJustAlpha(paint.getColor()),
1446 false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001447
1448 fContext->drawRectToRect(grPaint,
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001449 SkRect::MakeXYWH(SkIntToScalar(left),
1450 SkIntToScalar(top),
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001451 SkIntToScalar(w),
1452 SkIntToScalar(h)),
1453 SkRect::MakeXYWH(0,
1454 0,
1455 SK_Scalar1 * w / texture->width(),
1456 SK_Scalar1 * h / texture->height()));
1457}
1458
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001459void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001460 const SkRect* src, const SkRect& dst,
1461 const SkPaint& paint,
1462 SkCanvas::DrawBitmapRectFlags flags) {
1463 SkMatrix matrix;
1464 SkRect bitmapBounds, tmpSrc;
1465
1466 bitmapBounds.set(0, 0,
1467 SkIntToScalar(bitmap.width()),
1468 SkIntToScalar(bitmap.height()));
1469
1470 // Compute matrix from the two rectangles
1471 if (NULL != src) {
1472 tmpSrc = *src;
1473 } else {
1474 tmpSrc = bitmapBounds;
1475 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001476
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001477 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1478
1479 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
1480 if (NULL != src) {
1481 if (!bitmapBounds.contains(tmpSrc)) {
1482 if (!tmpSrc.intersect(bitmapBounds)) {
1483 return; // nothing to draw
1484 }
1485 }
1486 }
1487
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001488 SkRect tmpDst;
1489 matrix.mapRect(&tmpDst, tmpSrc);
1490
1491 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1492 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1493 // Translate so that tempDst's top left is at the origin.
1494 matrix = *origDraw.fMatrix;
1495 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1496 draw.writable()->fMatrix = &matrix;
1497 }
1498 SkSize dstSize;
1499 dstSize.fWidth = tmpDst.width();
1500 dstSize.fHeight = tmpDst.height();
1501
1502 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001503}
1504
1505void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1506 int x, int y, const SkPaint& paint) {
1507 // clear of the source device must occur before CHECK_SHOULD_DRAW
egdanield78a1682014-07-09 10:41:26 -07001508 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawDevice", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001509 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1510 if (dev->fNeedClear) {
1511 // TODO: could check here whether we really need to draw at all
1512 dev->clear(0x0);
1513 }
1514
1515 // drawDevice is defined to be in device coords.
1516 CHECK_SHOULD_DRAW(draw, true);
1517
1518 GrRenderTarget* devRT = dev->accessRenderTarget();
1519 GrTexture* devTex;
1520 if (NULL == (devTex = devRT->asTexture())) {
1521 return;
1522 }
1523
1524 const SkBitmap& bm = dev->accessBitmap(false);
1525 int w = bm.width();
1526 int h = bm.height();
1527
1528 SkImageFilter* filter = paint.getImageFilter();
1529 // This bitmap will own the filtered result as a texture.
1530 SkBitmap filteredBitmap;
1531
1532 if (NULL != filter) {
1533 SkIPoint offset = SkIPoint::Make(0, 0);
1534 SkMatrix matrix(*draw.fMatrix);
1535 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001536 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001537 SkImageFilter::Cache* cache = SkImageFilter::Cache::Create();
1538 SkAutoUnref aur(cache);
1539 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001540 if (filter_texture(this, fContext, devTex, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001541 &offset)) {
1542 devTex = filteredBitmap.getTexture();
1543 w = filteredBitmap.width();
1544 h = filteredBitmap.height();
1545 x += offset.fX;
1546 y += offset.fY;
1547 } else {
1548 return;
1549 }
1550 }
1551
1552 GrPaint grPaint;
1553 grPaint.addColorTextureEffect(devTex, SkMatrix::I());
1554
dandov9de5b512014-06-10 14:38:28 -07001555 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColorJustAlpha(paint.getColor()),
1556 false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001557
1558 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1559 SkIntToScalar(y),
1560 SkIntToScalar(w),
1561 SkIntToScalar(h));
1562
1563 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1564 // scratch texture).
1565 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1566 SK_Scalar1 * h / devTex->height());
1567
1568 fContext->drawRectToRect(grPaint, dstRect, srcRect);
1569}
1570
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001571bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001572 return filter->canFilterImageGPU();
1573}
1574
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001575bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001576 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001577 SkBitmap* result, SkIPoint* offset) {
1578 // want explicitly our impl, so guard against a subclass of us overriding it
1579 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1580 return false;
1581 }
1582
1583 SkAutoLockPixels alp(src, !src.getTexture());
1584 if (!src.getTexture() && !src.readyToDraw()) {
1585 return false;
1586 }
1587
1588 GrTexture* texture;
1589 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1590 // must be pushed upstack.
1591 SkAutoCachedTexture act(this, src, NULL, &texture);
1592
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001593 return filter_texture(this, fContext, texture, filter, src.width(), src.height(), ctx,
1594 result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001595}
1596
1597///////////////////////////////////////////////////////////////////////////////
1598
1599// must be in SkCanvas::VertexMode order
1600static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1601 kTriangles_GrPrimitiveType,
1602 kTriangleStrip_GrPrimitiveType,
1603 kTriangleFan_GrPrimitiveType,
1604};
1605
1606void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1607 int vertexCount, const SkPoint vertices[],
1608 const SkPoint texs[], const SkColor colors[],
1609 SkXfermode* xmode,
1610 const uint16_t indices[], int indexCount,
1611 const SkPaint& paint) {
1612 CHECK_SHOULD_DRAW(draw, false);
1613
egdanield78a1682014-07-09 10:41:26 -07001614 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawVertices", fContext);
dandov32a311b2014-07-15 19:46:26 -07001615
1616 const uint16_t* outIndices;
1617 SkAutoTDeleteArray<uint16_t> outAlloc(NULL);
1618 GrPrimitiveType primType;
1619 GrPaint grPaint;
1620
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001621 // If both textures and vertex-colors are NULL, strokes hairlines with the paint's color.
1622 if ((NULL == texs || NULL == paint.getShader()) && NULL == colors) {
dandov32a311b2014-07-15 19:46:26 -07001623
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001624 texs = NULL;
dandov32a311b2014-07-15 19:46:26 -07001625
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001626 SkPaint copy(paint);
1627 copy.setStyle(SkPaint::kStroke_Style);
1628 copy.setStrokeWidth(0);
dandov32a311b2014-07-15 19:46:26 -07001629
1630 // we ignore the shader if texs is null.
1631 SkPaint2GrPaintNoShader(this->context(), copy, SkColor2GrColor(copy.getColor()),
1632 NULL == colors, &grPaint);
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001633
dandov32a311b2014-07-15 19:46:26 -07001634 primType = kLines_GrPrimitiveType;
1635 int triangleCount = 0;
1636 switch (vmode) {
1637 case SkCanvas::kTriangles_VertexMode:
1638 triangleCount = indexCount / 3;
1639 break;
1640 case SkCanvas::kTriangleStrip_VertexMode:
1641 case SkCanvas::kTriangleFan_VertexMode:
1642 triangleCount = indexCount - 2;
1643 break;
1644 }
1645
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001646 VertState state(vertexCount, indices, indexCount);
1647 VertState::Proc vertProc = state.chooseProc(vmode);
dandov32a311b2014-07-15 19:46:26 -07001648
1649 //number of indices for lines per triangle with kLines
1650 indexCount = triangleCount * 6;
1651
1652 outAlloc.reset(SkNEW_ARRAY(uint16_t, indexCount));
1653 outIndices = outAlloc.get();
1654 uint16_t* auxIndices = outAlloc.get();
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001655 int i = 0;
1656 while (vertProc(&state)) {
dandov32a311b2014-07-15 19:46:26 -07001657 auxIndices[i] = state.f0;
1658 auxIndices[i + 1] = state.f1;
1659 auxIndices[i + 2] = state.f1;
1660 auxIndices[i + 3] = state.f2;
1661 auxIndices[i + 4] = state.f2;
1662 auxIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001663 i += 6;
1664 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001665 } else {
dandov32a311b2014-07-15 19:46:26 -07001666 outIndices = indices;
1667 primType = gVertexMode2PrimitiveType[vmode];
1668
1669 if (NULL == texs || NULL == paint.getShader()) {
1670 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColor(paint.getColor()),
1671 NULL == colors, &grPaint);
1672 } else {
1673 SkPaint2GrPaintShader(this->context(), paint, NULL == colors, &grPaint);
1674 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001675 }
1676
mtklein2583b622014-06-04 08:20:41 -07001677#if 0
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001678 if (NULL != xmode && NULL != texs && NULL != colors) {
1679 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1680 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
mtklein2583b622014-06-04 08:20:41 -07001681 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001682 }
1683 }
mtklein2583b622014-06-04 08:20:41 -07001684#endif
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001685
1686 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1687 if (NULL != colors) {
1688 // need to convert byte order and from non-PM to PM
1689 convertedColors.reset(vertexCount);
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001690 SkColor color;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001691 for (int i = 0; i < vertexCount; ++i) {
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001692 color = colors[i];
1693 if (paint.getAlpha() != 255) {
1694 color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paint.getAlpha()));
1695 }
1696 convertedColors[i] = SkColor2GrColor(color);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001697 }
1698 colors = convertedColors.get();
1699 }
1700 fContext->drawVertices(grPaint,
dandov32a311b2014-07-15 19:46:26 -07001701 primType,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001702 vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001703 vertices,
1704 texs,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001705 colors,
dandov32a311b2014-07-15 19:46:26 -07001706 outIndices,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001707 indexCount);
1708}
1709
1710///////////////////////////////////////////////////////////////////////////////
1711
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001712void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1713 size_t byteLength, SkScalar x, SkScalar y,
1714 const SkPaint& paint) {
1715 CHECK_SHOULD_DRAW(draw, false);
egdanield78a1682014-07-09 10:41:26 -07001716 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001717
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001718 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001719 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001720 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001721
1722 SkDEBUGCODE(this->validate();)
1723
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001724 fMainTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
1725 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001726 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001727 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001728
1729 SkDEBUGCODE(this->validate();)
1730
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001731 fFallbackTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001732 } else {
1733 // this guy will just call our drawPath()
1734 draw.drawText_asPaths((const char*)text, byteLength, x, y, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001735 }
1736}
1737
1738void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1739 size_t byteLength, const SkScalar pos[],
1740 SkScalar constY, int scalarsPerPos,
1741 const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -07001742 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPosText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001743 CHECK_SHOULD_DRAW(draw, false);
1744
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001745 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001746 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001747 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001748
1749 SkDEBUGCODE(this->validate();)
1750
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001751 fMainTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001752 constY, scalarsPerPos);
1753 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001754 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001755 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001756
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001757 SkDEBUGCODE(this->validate();)
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001758
1759 fFallbackTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001760 constY, scalarsPerPos);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001761 } else {
1762 draw.drawPosText_asPaths((const char*)text, byteLength, pos, constY,
1763 scalarsPerPos, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001764 }
1765}
1766
1767void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1768 size_t len, const SkPath& path,
1769 const SkMatrix* m, const SkPaint& paint) {
1770 CHECK_SHOULD_DRAW(draw, false);
1771
1772 SkASSERT(draw.fDevice == this);
1773 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1774}
1775
1776///////////////////////////////////////////////////////////////////////////////
1777
1778bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1779 if (!paint.isLCDRenderText()) {
1780 // we're cool with the paint as is
1781 return false;
1782 }
1783
1784 if (paint.getShader() ||
1785 paint.getXfermode() || // unless its srcover
1786 paint.getMaskFilter() ||
1787 paint.getRasterizer() ||
1788 paint.getColorFilter() ||
1789 paint.getPathEffect() ||
1790 paint.isFakeBoldText() ||
1791 paint.getStyle() != SkPaint::kFill_Style) {
1792 // turn off lcd
1793 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1794 flags->fHinting = paint.getHinting();
1795 return true;
1796 }
1797 // we're cool with the paint as is
1798 return false;
1799}
1800
1801void SkGpuDevice::flush() {
1802 DO_DEFERRED_CLEAR();
1803 fContext->resolveRenderTarget(fRenderTarget);
1804}
1805
1806///////////////////////////////////////////////////////////////////////////////
1807
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001808SkBaseDevice* SkGpuDevice::onCreateDevice(const SkImageInfo& info, Usage usage) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001809 GrTextureDesc desc;
1810 desc.fConfig = fRenderTarget->config();
1811 desc.fFlags = kRenderTarget_GrTextureFlagBit;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001812 desc.fWidth = info.width();
1813 desc.fHeight = info.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001814 desc.fSampleCnt = fRenderTarget->numSamples();
1815
1816 SkAutoTUnref<GrTexture> texture;
1817 // Skia's convention is to only clear a device if it is non-opaque.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001818 unsigned flags = info.isOpaque() ? 0 : kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001819
1820#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1821 // layers are never draw in repeat modes, so we can request an approx
1822 // match and ignore any padding.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001823 flags |= kCached_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001824 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == usage) ?
1825 GrContext::kApprox_ScratchTexMatch :
1826 GrContext::kExact_ScratchTexMatch;
1827 texture.reset(fContext->lockAndRefScratchTexture(desc, match));
1828#else
1829 texture.reset(fContext->createUncachedTexture(desc, NULL, 0));
1830#endif
1831 if (NULL != texture.get()) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001832 return SkGpuDevice::Create(texture, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001833 } else {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001834 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1835 info.width(), info.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001836 return NULL;
1837 }
1838}
1839
reed@google.com76f10a32014-02-05 15:32:21 +00001840SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info) {
1841 return SkSurface::NewRenderTarget(fContext, info, fRenderTarget->numSamples());
1842}
1843
robertphillips9b14f262014-06-04 05:40:44 -07001844void SkGpuDevice::EXPERIMENTAL_optimize(const SkPicture* picture) {
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001845 SkPicture::AccelData::Key key = GPUAccelData::ComputeAccelDataKey();
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001846
commit-bot@chromium.org8ec8bab2014-05-14 13:11:48 +00001847 const SkPicture::AccelData* existing = picture->EXPERIMENTAL_getAccelData(key);
1848 if (NULL != existing) {
1849 return;
1850 }
1851
commit-bot@chromium.org8fd93822014-05-06 13:43:22 +00001852 SkAutoTUnref<GPUAccelData> data(SkNEW_ARGS(GPUAccelData, (key)));
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001853
1854 picture->EXPERIMENTAL_addAccelData(data);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001855
1856 GatherGPUInfo(picture, data);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001857}
1858
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001859static void wrap_texture(GrTexture* texture, int width, int height, SkBitmap* result) {
1860 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001861 result->setInfo(info);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001862 result->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref();
1863}
1864
robertphillips9b14f262014-06-04 05:40:44 -07001865void SkGpuDevice::EXPERIMENTAL_purge(const SkPicture* picture) {
robertphillips952841b2014-06-30 08:26:50 -07001866 fContext->getLayerCache()->purge(picture);
commit-bot@chromium.orgc8733292014-04-11 15:54:14 +00001867}
1868
robertphillips9b14f262014-06-04 05:40:44 -07001869bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* canvas, const SkPicture* picture) {
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001870
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001871 SkPicture::AccelData::Key key = GPUAccelData::ComputeAccelDataKey();
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001872
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001873 const SkPicture::AccelData* data = picture->EXPERIMENTAL_getAccelData(key);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001874 if (NULL == data) {
1875 return false;
1876 }
1877
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001878 const GPUAccelData *gpuData = static_cast<const GPUAccelData*>(data);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001879
commit-bot@chromium.org8ec8bab2014-05-14 13:11:48 +00001880 if (0 == gpuData->numSaveLayers()) {
1881 return false;
1882 }
1883
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001884 SkAutoTArray<bool> pullForward(gpuData->numSaveLayers());
1885 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1886 pullForward[i] = false;
1887 }
1888
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001889 SkRect clipBounds;
1890 if (!canvas->getClipBounds(&clipBounds)) {
1891 return true;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001892 }
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001893 SkIRect query;
1894 clipBounds.roundOut(&query);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001895
robertphillipsce4dd3d2014-07-07 13:46:35 -07001896 SkAutoTDelete<const SkPicture::OperationList> ops(picture->EXPERIMENTAL_getActiveOps(query));
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001897
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001898 // This code pre-renders the entire layer since it will be cached and potentially
1899 // reused with different clips (e.g., in different tiles). Because of this the
1900 // clip will not be limiting the size of the pre-rendered layer. kSaveLayerMaxSize
1901 // is used to limit which clips are pre-rendered.
1902 static const int kSaveLayerMaxSize = 256;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001903
robertphillipsce4dd3d2014-07-07 13:46:35 -07001904 if (NULL != ops.get()) {
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001905 // In this case the picture has been generated with a BBH so we use
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001906 // the BBH to limit the pre-rendering to just the layers needed to cover
1907 // the region being drawn
robertphillipsce4dd3d2014-07-07 13:46:35 -07001908 for (int i = 0; i < ops->numOps(); ++i) {
1909 uint32_t offset = ops->offset(i);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001910
1911 // For now we're saving all the layers in the GPUAccelData so they
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001912 // can be nested. Additionally, the nested layers appear before
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001913 // their parent in the list.
1914 for (int j = 0 ; j < gpuData->numSaveLayers(); ++j) {
1915 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(j);
1916
1917 if (pullForward[j]) {
1918 continue; // already pulling forward
1919 }
1920
1921 if (offset < info.fSaveLayerOpID || offset > info.fRestoreOpID) {
1922 continue; // the op isn't in this range
1923 }
1924
1925 // TODO: once this code is more stable unsuitable layers can
1926 // just be omitted during the optimization stage
1927 if (!info.fValid ||
1928 kSaveLayerMaxSize < info.fSize.fWidth ||
1929 kSaveLayerMaxSize < info.fSize.fHeight ||
1930 info.fIsNested) {
1931 continue; // this layer is unsuitable
1932 }
1933
1934 pullForward[j] = true;
1935 }
1936 }
1937 } else {
1938 // In this case there is no BBH associated with the picture. Pre-render
commit-bot@chromium.orgf97d65d2014-05-08 23:24:05 +00001939 // all the layers that intersect the drawn region
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001940 for (int j = 0; j < gpuData->numSaveLayers(); ++j) {
1941 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(j);
1942
commit-bot@chromium.orgf97d65d2014-05-08 23:24:05 +00001943 SkIRect layerRect = SkIRect::MakeXYWH(info.fOffset.fX,
1944 info.fOffset.fY,
1945 info.fSize.fWidth,
1946 info.fSize.fHeight);
1947
1948 if (!SkIRect::Intersects(query, layerRect)) {
1949 continue;
1950 }
1951
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001952 // TODO: once this code is more stable unsuitable layers can
1953 // just be omitted during the optimization stage
1954 if (!info.fValid ||
1955 kSaveLayerMaxSize < info.fSize.fWidth ||
1956 kSaveLayerMaxSize < info.fSize.fHeight ||
1957 info.fIsNested) {
1958 continue;
1959 }
1960
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001961 pullForward[j] = true;
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001962 }
1963 }
1964
robertphillipsc26d9912014-07-10 07:21:27 -07001965 SkPictureReplacementPlayback::PlaybackReplacements replacements;
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001966
robertphillips4ec84da2014-06-24 13:10:43 -07001967 // Generate the layer and/or ensure it is locked
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001968 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1969 if (pullForward[i]) {
1970 GrCachedLayer* layer = fContext->getLayerCache()->findLayerOrCreate(picture, i);
1971
1972 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(i);
1973
robertphillipsc26d9912014-07-10 07:21:27 -07001974 SkPictureReplacementPlayback::PlaybackReplacements::ReplacementInfo* layerInfo =
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001975 replacements.push();
robertphillips4ec84da2014-06-24 13:10:43 -07001976 layerInfo->fStart = info.fSaveLayerOpID;
1977 layerInfo->fStop = info.fRestoreOpID;
1978 layerInfo->fPos = info.fOffset;
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001979
robertphillips4ec84da2014-06-24 13:10:43 -07001980 GrTextureDesc desc;
1981 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1982 desc.fWidth = info.fSize.fWidth;
1983 desc.fHeight = info.fSize.fHeight;
1984 desc.fConfig = kSkia8888_GrPixelConfig;
1985 // TODO: need to deal with sample count
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001986
robertphillips4ec84da2014-06-24 13:10:43 -07001987 bool needsRendering = !fContext->getLayerCache()->lock(layer, desc);
robertphillips952841b2014-06-30 08:26:50 -07001988 if (NULL == layer->texture()) {
robertphillips4ec84da2014-06-24 13:10:43 -07001989 continue;
1990 }
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001991
robertphillips4ec84da2014-06-24 13:10:43 -07001992 layerInfo->fBM = SkNEW(SkBitmap); // fBM is allocated so ReplacementInfo can be POD
robertphillips952841b2014-06-30 08:26:50 -07001993 wrap_texture(layer->texture(),
robertphillips21048b52014-07-15 19:46:35 -07001994 !layer->isAtlased() ? desc.fWidth : layer->texture()->width(),
1995 !layer->isAtlased() ? desc.fHeight : layer->texture()->height(),
robertphillips952841b2014-06-30 08:26:50 -07001996 layerInfo->fBM);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001997
robertphillips4ec84da2014-06-24 13:10:43 -07001998 SkASSERT(info.fPaint);
1999 layerInfo->fPaint = info.fPaint;
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002000
robertphillips21048b52014-07-15 19:46:35 -07002001 layerInfo->fSrcRect = SkIRect::MakeXYWH(layer->rect().fLeft,
2002 layer->rect().fTop,
2003 layer->rect().width(),
2004 layer->rect().height());
2005
robertphillips952841b2014-06-30 08:26:50 -07002006
robertphillips4ec84da2014-06-24 13:10:43 -07002007 if (needsRendering) {
2008 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTargetDirect(
robertphillips952841b2014-06-30 08:26:50 -07002009 layer->texture()->asRenderTarget(),
2010 SkSurface::kStandard_TextRenderMode,
2011 SkSurface::kDontClear_RenderTargetFlag));
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002012
robertphillips4ec84da2014-06-24 13:10:43 -07002013 SkCanvas* canvas = surface->getCanvas();
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002014
robertphillips21048b52014-07-15 19:46:35 -07002015 // Add a rect clip to make sure the rendering doesn't
2016 // extend beyond the boundaries of the atlased sub-rect
2017 SkRect bound = SkRect::Make(layerInfo->fSrcRect);
2018 canvas->clipRect(bound);
2019
2020 if (layer->isAtlased()) {
robertphillips952841b2014-06-30 08:26:50 -07002021 // Since 'clear' doesn't respect the clip we need to draw a rect
2022 // TODO: ensure none of the atlased layers contain a clear call!
2023 SkPaint paint;
2024 paint.setColor(SK_ColorTRANSPARENT);
2025 canvas->drawRect(bound, paint);
2026 } else {
2027 canvas->clear(SK_ColorTRANSPARENT);
2028 }
2029
robertphillips21048b52014-07-15 19:46:35 -07002030 // info.fCTM maps the layer's top/left to the origin.
2031 // If this layer is atlased the top/left corner needs
2032 // to be offset to some arbitrary location in the backing
2033 // texture.
2034 canvas->translate(bound.fLeft, bound.fTop);
2035 canvas->concat(info.fCTM);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002036
robertphillips1ad00e42014-07-08 08:28:08 -07002037 SkPictureRangePlayback rangePlayback(picture,
2038 info.fSaveLayerOpID,
2039 info.fRestoreOpID);
2040 rangePlayback.draw(canvas, NULL);
robertphillips952841b2014-06-30 08:26:50 -07002041
robertphillips4ec84da2014-06-24 13:10:43 -07002042 canvas->flush();
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00002043 }
2044 }
2045 }
2046
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002047 // Playback using new layers
robertphillipsc26d9912014-07-10 07:21:27 -07002048 SkPictureReplacementPlayback playback(picture, &replacements, ops.get());
robertphillipsce4dd3d2014-07-07 13:46:35 -07002049
robertphillipsce4dd3d2014-07-07 13:46:35 -07002050 playback.draw(canvas, NULL);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002051
robertphillips4ec84da2014-06-24 13:10:43 -07002052 // unlock the layers
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002053 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
robertphillips4ec84da2014-06-24 13:10:43 -07002054 GrCachedLayer* layer = fContext->getLayerCache()->findLayer(picture, i);
2055 fContext->getLayerCache()->unlock(layer);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002056 }
2057
2058 return true;
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00002059}