blob: fe036febe2499311d297ee7a1abb4b33eba5dbed [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.org907fbd52013-12-09 17:03:02 +000011#include "effects/GrTextureDomain.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000012#include "effects/GrSimpleTextureEffect.h"
13
14#include "GrContext.h"
15#include "GrBitmapTextContext.h"
16#if SK_DISTANCEFIELD_FONTS
17#include "GrDistanceFieldTextContext.h"
18#endif
19
20#include "SkGrTexturePixelRef.h"
21
22#include "SkColorFilter.h"
23#include "SkDeviceImageFilterProxy.h"
24#include "SkDrawProcs.h"
25#include "SkGlyphCache.h"
26#include "SkImageFilter.h"
27#include "SkPathEffect.h"
28#include "SkRRect.h"
29#include "SkStroke.h"
30#include "SkUtils.h"
31#include "SkErrorInternals.h"
32
33#define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
34
35#if 0
36 extern bool (*gShouldDrawProc)();
37 #define CHECK_SHOULD_DRAW(draw, forceI) \
38 do { \
39 if (gShouldDrawProc && !gShouldDrawProc()) return; \
40 this->prepareDraw(draw, forceI); \
41 } while (0)
42#else
43 #define CHECK_SHOULD_DRAW(draw, forceI) this->prepareDraw(draw, forceI)
44#endif
45
46// This constant represents the screen alignment criterion in texels for
47// requiring texture domain clamping to prevent color bleeding when drawing
48// a sub region of a larger source image.
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000049#define COLOR_BLEED_TOLERANCE 0.001f
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000050
51#define DO_DEFERRED_CLEAR() \
52 do { \
53 if (fNeedClear) { \
54 this->clear(SK_ColorTRANSPARENT); \
55 } \
56 } while (false) \
57
58///////////////////////////////////////////////////////////////////////////////
59
60#define CHECK_FOR_ANNOTATION(paint) \
61 do { if (paint.getAnnotation()) { return; } } while (0)
62
63///////////////////////////////////////////////////////////////////////////////
64
65
66class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
67public:
68 SkAutoCachedTexture()
69 : fDevice(NULL)
70 , fTexture(NULL) {
71 }
72
73 SkAutoCachedTexture(SkGpuDevice* device,
74 const SkBitmap& bitmap,
75 const GrTextureParams* params,
76 GrTexture** texture)
77 : fDevice(NULL)
78 , fTexture(NULL) {
79 SkASSERT(NULL != texture);
80 *texture = this->set(device, bitmap, params);
81 }
82
83 ~SkAutoCachedTexture() {
84 if (NULL != fTexture) {
85 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
86 }
87 }
88
89 GrTexture* set(SkGpuDevice* device,
90 const SkBitmap& bitmap,
91 const GrTextureParams* params) {
92 if (NULL != fTexture) {
93 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
94 fTexture = NULL;
95 }
96 fDevice = device;
97 GrTexture* result = (GrTexture*)bitmap.getTexture();
98 if (NULL == result) {
99 // Cannot return the native texture so look it up in our cache
100 fTexture = GrLockAndRefCachedBitmapTexture(device->context(), bitmap, params);
101 result = fTexture;
102 }
103 return result;
104 }
105
106private:
107 SkGpuDevice* fDevice;
108 GrTexture* fTexture;
109};
110
111///////////////////////////////////////////////////////////////////////////////
112
113struct GrSkDrawProcs : public SkDrawProcs {
114public:
115 GrContext* fContext;
116 GrTextContext* fTextContext;
117 GrFontScaler* fFontScaler; // cached in the skia glyphcache
118};
119
120///////////////////////////////////////////////////////////////////////////////
121
122static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
123 switch (config) {
124 case kAlpha_8_GrPixelConfig:
125 *isOpaque = false;
126 return SkBitmap::kA8_Config;
127 case kRGB_565_GrPixelConfig:
128 *isOpaque = true;
129 return SkBitmap::kRGB_565_Config;
130 case kRGBA_4444_GrPixelConfig:
131 *isOpaque = false;
132 return SkBitmap::kARGB_4444_Config;
133 case kSkia8888_GrPixelConfig:
134 // we don't currently have a way of knowing whether
135 // a 8888 is opaque based on the config.
136 *isOpaque = false;
137 return SkBitmap::kARGB_8888_Config;
138 default:
139 *isOpaque = false;
140 return SkBitmap::kNo_Config;
141 }
142}
143
144/*
145 * GrRenderTarget does not know its opaqueness, only its config, so we have
146 * to make conservative guesses when we return an "equivalent" bitmap.
147 */
148static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
149 bool isOpaque;
150 SkBitmap::Config config = grConfig2skConfig(renderTarget->config(), &isOpaque);
151
152 SkBitmap bitmap;
153 bitmap.setConfig(config, renderTarget->width(), renderTarget->height(), 0,
154 isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
155 return bitmap;
156}
157
commit-bot@chromium.org3e7f3852013-12-06 22:59:02 +0000158/*
159 * Calling SkBitmapDevice with individual params asks it to allocate pixel memory.
160 * We never want that, so we always need to call it with a bitmap argument
161 * (which says take my allocate (or lack thereof)).
162 *
163 * This is a REALLY good reason to finish the clean-up of SkBaseDevice, and have
164 * SkGpuDevice inherit from that instead of SkBitmapDevice.
165 */
166static SkBitmap make_bitmap(SkBitmap::Config config, int width, int height, bool isOpaque) {
167 SkBitmap bm;
168 bm.setConfig(config, width, height, isOpaque);
169 return bm;
170}
171
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000172SkGpuDevice* SkGpuDevice::Create(GrSurface* surface) {
173 SkASSERT(NULL != surface);
174 if (NULL == surface->asRenderTarget() || NULL == surface->getContext()) {
175 return NULL;
176 }
177 if (surface->asTexture()) {
178 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asTexture()));
179 } else {
180 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asRenderTarget()));
181 }
182}
183
184SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
185 : SkBitmapDevice(make_bitmap(context, texture->asRenderTarget())) {
186 this->initFromRenderTarget(context, texture->asRenderTarget(), false);
187}
188
189SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
190 : SkBitmapDevice(make_bitmap(context, renderTarget)) {
191 this->initFromRenderTarget(context, renderTarget, false);
192}
193
194void SkGpuDevice::initFromRenderTarget(GrContext* context,
195 GrRenderTarget* renderTarget,
196 bool cached) {
197 fDrawProcs = NULL;
198
199 fContext = context;
200 fContext->ref();
201
202 fRenderTarget = NULL;
203 fNeedClear = false;
204
205 SkASSERT(NULL != renderTarget);
206 fRenderTarget = renderTarget;
207 fRenderTarget->ref();
208
209 // Hold onto to the texture in the pixel ref (if there is one) because the texture holds a ref
210 // on the RT but not vice-versa.
211 // TODO: Remove this trickery once we figure out how to make SkGrPixelRef do this without
212 // busting chrome (for a currently unknown reason).
213 GrSurface* surface = fRenderTarget->asTexture();
214 if (NULL == surface) {
215 surface = fRenderTarget;
216 }
reed@google.combf790232013-12-13 19:45:58 +0000217
218 SkImageInfo info;
219 surface->asImageInfo(&info);
220 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (info, surface, cached));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000221
222 this->setPixelRef(pr, 0)->unref();
223}
224
225SkGpuDevice::SkGpuDevice(GrContext* context,
226 SkBitmap::Config config,
227 int width,
228 int height,
229 int sampleCount)
reed@google.combf790232013-12-13 19:45:58 +0000230 : SkBitmapDevice(make_bitmap(config, width, height, false /*isOpaque*/))
231{
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000232 fDrawProcs = NULL;
233
234 fContext = context;
235 fContext->ref();
236
237 fRenderTarget = NULL;
238 fNeedClear = false;
239
240 if (config != SkBitmap::kRGB_565_Config) {
241 config = SkBitmap::kARGB_8888_Config;
242 }
243
244 GrTextureDesc desc;
245 desc.fFlags = kRenderTarget_GrTextureFlagBit;
246 desc.fWidth = width;
247 desc.fHeight = height;
248 desc.fConfig = SkBitmapConfig2GrPixelConfig(config);
249 desc.fSampleCnt = sampleCount;
250
reed@google.combf790232013-12-13 19:45:58 +0000251 SkImageInfo info;
252 if (!GrPixelConfig2ColorType(desc.fConfig, &info.fColorType)) {
253 sk_throw();
254 }
255 info.fWidth = width;
256 info.fHeight = height;
257 info.fAlphaType = kPremul_SkAlphaType;
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +0000258
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000259 SkAutoTUnref<GrTexture> texture(fContext->createUncachedTexture(desc, NULL, 0));
260
261 if (NULL != texture) {
262 fRenderTarget = texture->asRenderTarget();
263 fRenderTarget->ref();
264
265 SkASSERT(NULL != fRenderTarget);
266
267 // wrap the bitmap with a pixelref to expose our texture
reed@google.combf790232013-12-13 19:45:58 +0000268 SkGrPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (info, texture));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000269 this->setPixelRef(pr, 0)->unref();
270 } else {
271 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
272 width, height);
273 SkASSERT(false);
274 }
275}
276
277SkGpuDevice::~SkGpuDevice() {
278 if (fDrawProcs) {
279 delete fDrawProcs;
280 }
281
282 // The GrContext takes a ref on the target. We don't want to cause the render
283 // target to be unnecessarily kept alive.
284 if (fContext->getRenderTarget() == fRenderTarget) {
285 fContext->setRenderTarget(NULL);
286 }
287
288 if (fContext->getClip() == &fClipData) {
289 fContext->setClip(NULL);
290 }
291
292 SkSafeUnref(fRenderTarget);
293 fContext->unref();
294}
295
296///////////////////////////////////////////////////////////////////////////////
297
298void SkGpuDevice::makeRenderTargetCurrent() {
299 DO_DEFERRED_CLEAR();
300 fContext->setRenderTarget(fRenderTarget);
301}
302
303///////////////////////////////////////////////////////////////////////////////
304
305namespace {
306GrPixelConfig config8888_to_grconfig_and_flags(SkCanvas::Config8888 config8888, uint32_t* flags) {
307 switch (config8888) {
308 case SkCanvas::kNative_Premul_Config8888:
309 *flags = 0;
310 return kSkia8888_GrPixelConfig;
311 case SkCanvas::kNative_Unpremul_Config8888:
312 *flags = GrContext::kUnpremul_PixelOpsFlag;
313 return kSkia8888_GrPixelConfig;
314 case SkCanvas::kBGRA_Premul_Config8888:
315 *flags = 0;
316 return kBGRA_8888_GrPixelConfig;
317 case SkCanvas::kBGRA_Unpremul_Config8888:
318 *flags = GrContext::kUnpremul_PixelOpsFlag;
319 return kBGRA_8888_GrPixelConfig;
320 case SkCanvas::kRGBA_Premul_Config8888:
321 *flags = 0;
322 return kRGBA_8888_GrPixelConfig;
323 case SkCanvas::kRGBA_Unpremul_Config8888:
324 *flags = GrContext::kUnpremul_PixelOpsFlag;
325 return kRGBA_8888_GrPixelConfig;
326 default:
327 GrCrash("Unexpected Config8888.");
328 *flags = 0; // suppress warning
329 return kSkia8888_GrPixelConfig;
330 }
331}
332}
333
334bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
335 int x, int y,
336 SkCanvas::Config8888 config8888) {
337 DO_DEFERRED_CLEAR();
338 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
339 SkASSERT(!bitmap.isNull());
340 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
341
342 SkAutoLockPixels alp(bitmap);
343 GrPixelConfig config;
344 uint32_t flags;
345 config = config8888_to_grconfig_and_flags(config8888, &flags);
346 return fContext->readRenderTargetPixels(fRenderTarget,
347 x, y,
348 bitmap.width(),
349 bitmap.height(),
350 config,
351 bitmap.getPixels(),
352 bitmap.rowBytes(),
353 flags);
354}
355
356void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y,
357 SkCanvas::Config8888 config8888) {
358 SkAutoLockPixels alp(bitmap);
359 if (!bitmap.readyToDraw()) {
360 return;
361 }
362
363 GrPixelConfig config;
364 uint32_t flags;
365 if (SkBitmap::kARGB_8888_Config == bitmap.config()) {
366 config = config8888_to_grconfig_and_flags(config8888, &flags);
367 } else {
368 flags = 0;
369 config= SkBitmapConfig2GrPixelConfig(bitmap.config());
370 }
371
372 fRenderTarget->writePixels(x, y, bitmap.width(), bitmap.height(),
373 config, bitmap.getPixels(), bitmap.rowBytes(), flags);
374}
375
376void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
377 INHERITED::onAttachToCanvas(canvas);
378
379 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
380 fClipData.fClipStack = canvas->getClipStack();
381}
382
383void SkGpuDevice::onDetachFromCanvas() {
384 INHERITED::onDetachFromCanvas();
385 fClipData.fClipStack = NULL;
386}
387
388// call this every draw call, to ensure that the context reflects our state,
389// and not the state from some other canvas/device
390void SkGpuDevice::prepareDraw(const SkDraw& draw, bool forceIdentity) {
391 SkASSERT(NULL != fClipData.fClipStack);
392
393 fContext->setRenderTarget(fRenderTarget);
394
395 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
396
397 if (forceIdentity) {
398 fContext->setIdentityMatrix();
399 } else {
400 fContext->setMatrix(*draw.fMatrix);
401 }
402 fClipData.fOrigin = this->getOrigin();
403
404 fContext->setClip(&fClipData);
405
406 DO_DEFERRED_CLEAR();
407}
408
409GrRenderTarget* SkGpuDevice::accessRenderTarget() {
410 DO_DEFERRED_CLEAR();
411 return fRenderTarget;
412}
413
414///////////////////////////////////////////////////////////////////////////////
415
416SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
417SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
418SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
419SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
420SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
421 shader_type_mismatch);
422SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
423 shader_type_mismatch);
424SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
425SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
426
427namespace {
428
429// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
430// justAlpha indicates that skPaint's alpha should be used rather than the color
431// Callers may subsequently modify the GrPaint. Setting constantColor indicates
432// that the final paint will draw the same color at every pixel. This allows
433// an optimization where the the color filter can be applied to the skPaint's
434// color once while converting to GrPaint and then ignored.
435inline bool skPaint2GrPaintNoShader(SkGpuDevice* dev,
436 const SkPaint& skPaint,
437 bool justAlpha,
438 bool constantColor,
439 GrPaint* grPaint) {
440
441 grPaint->setDither(skPaint.isDither());
442 grPaint->setAntiAlias(skPaint.isAntiAlias());
443
444 SkXfermode::Coeff sm;
445 SkXfermode::Coeff dm;
446
447 SkXfermode* mode = skPaint.getXfermode();
448 GrEffectRef* xferEffect = NULL;
449 if (SkXfermode::AsNewEffectOrCoeff(mode, &xferEffect, &sm, &dm)) {
450 if (NULL != xferEffect) {
451 grPaint->addColorEffect(xferEffect)->unref();
452 sm = SkXfermode::kOne_Coeff;
453 dm = SkXfermode::kZero_Coeff;
454 }
455 } else {
456 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
457#if 0
458 return false;
459#else
460 // Fall back to src-over
461 sm = SkXfermode::kOne_Coeff;
462 dm = SkXfermode::kISA_Coeff;
463#endif
464 }
465 grPaint->setBlendFunc(sk_blend_to_grblend(sm), sk_blend_to_grblend(dm));
466
467 if (justAlpha) {
468 uint8_t alpha = skPaint.getAlpha();
469 grPaint->setColor(GrColorPackRGBA(alpha, alpha, alpha, alpha));
470 // justAlpha is currently set to true only if there is a texture,
471 // so constantColor should not also be true.
472 SkASSERT(!constantColor);
473 } else {
474 grPaint->setColor(SkColor2GrColor(skPaint.getColor()));
475 }
476
477 SkColorFilter* colorFilter = skPaint.getColorFilter();
478 if (NULL != colorFilter) {
479 // if the source color is a constant then apply the filter here once rather than per pixel
480 // in a shader.
481 if (constantColor) {
482 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
483 grPaint->setColor(SkColor2GrColor(filtered));
484 } else {
485 SkAutoTUnref<GrEffectRef> effect(colorFilter->asNewEffect(dev->context()));
486 if (NULL != effect.get()) {
487 grPaint->addColorEffect(effect);
488 }
489 }
490 }
491
492 return true;
493}
494
495// This function is similar to skPaint2GrPaintNoShader but also converts
496// skPaint's shader to a GrTexture/GrEffectStage if possible. The texture to
497// be used is set on grPaint and returned in param act. constantColor has the
498// same meaning as in skPaint2GrPaintNoShader.
499inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
500 const SkPaint& skPaint,
501 bool constantColor,
502 GrPaint* grPaint) {
503 SkShader* shader = skPaint.getShader();
504 if (NULL == shader) {
505 return skPaint2GrPaintNoShader(dev, skPaint, false, constantColor, grPaint);
506 }
507
508 // SkShader::asNewEffect() may do offscreen rendering. Setup default drawing state
509 // Also require shader to set the render target .
510 GrContext::AutoWideOpenIdentityDraw awo(dev->context(), NULL);
511 GrContext::AutoRenderTarget(dev->context(), NULL);
512
513 // setup the shader as the first color effect on the paint
514 SkAutoTUnref<GrEffectRef> effect(shader->asNewEffect(dev->context(), skPaint));
515 if (NULL != effect.get()) {
516 grPaint->addColorEffect(effect);
517 // Now setup the rest of the paint.
518 return skPaint2GrPaintNoShader(dev, skPaint, true, false, grPaint);
519 } else {
520 // We still don't have SkColorShader::asNewEffect() implemented.
521 SkShader::GradientInfo info;
522 SkColor color;
523
524 info.fColors = &color;
525 info.fColorOffsets = NULL;
526 info.fColorCount = 1;
527 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
528 SkPaint copy(skPaint);
529 copy.setShader(NULL);
530 // modulate the paint alpha by the shader's solid color alpha
531 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
532 copy.setColor(SkColorSetA(color, newA));
533 return skPaint2GrPaintNoShader(dev, copy, false, constantColor, grPaint);
534 } else {
535 return false;
536 }
537 }
538}
539}
540
541///////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000542
543SkBitmap::Config SkGpuDevice::config() const {
544 if (NULL == fRenderTarget) {
545 return SkBitmap::kNo_Config;
546 }
547
548 bool isOpaque;
549 return grConfig2skConfig(fRenderTarget->config(), &isOpaque);
550}
551
552void SkGpuDevice::clear(SkColor color) {
553 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
554 fContext->clear(&rect, SkColor2GrColor(color), true, fRenderTarget);
555 fNeedClear = false;
556}
557
558void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
559 CHECK_SHOULD_DRAW(draw, false);
560
561 GrPaint grPaint;
562 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
563 return;
564 }
565
566 fContext->drawPaint(grPaint);
567}
568
569// must be in SkCanvas::PointMode order
570static const GrPrimitiveType gPointMode2PrimtiveType[] = {
571 kPoints_GrPrimitiveType,
572 kLines_GrPrimitiveType,
573 kLineStrip_GrPrimitiveType
574};
575
576void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
577 size_t count, const SkPoint pts[], const SkPaint& paint) {
578 CHECK_FOR_ANNOTATION(paint);
579 CHECK_SHOULD_DRAW(draw, false);
580
581 SkScalar width = paint.getStrokeWidth();
582 if (width < 0) {
583 return;
584 }
585
586 // we only handle hairlines and paints without path effects or mask filters,
587 // else we let the SkDraw call our drawPath()
588 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
589 draw.drawPoints(mode, count, pts, paint, true);
590 return;
591 }
592
593 GrPaint grPaint;
594 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
595 return;
596 }
597
598 fContext->drawVertices(grPaint,
599 gPointMode2PrimtiveType[mode],
robertphillips@google.coma4662862013-11-21 14:24:16 +0000600 SkToS32(count),
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000601 (GrPoint*)pts,
602 NULL,
603 NULL,
604 NULL,
605 0);
606}
607
608///////////////////////////////////////////////////////////////////////////////
609
610void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
611 const SkPaint& paint) {
612 CHECK_FOR_ANNOTATION(paint);
613 CHECK_SHOULD_DRAW(draw, false);
614
615 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
616 SkScalar width = paint.getStrokeWidth();
617
618 /*
619 We have special code for hairline strokes, miter-strokes, bevel-stroke
620 and fills. Anything else we just call our path code.
621 */
622 bool usePath = doStroke && width > 0 &&
623 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
624 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
625 // another two reasons we might need to call drawPath...
626 if (paint.getMaskFilter() || paint.getPathEffect()) {
627 usePath = true;
628 }
629 if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) {
630#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
631 if (doStroke) {
632#endif
633 usePath = true;
634#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
635 } else {
636 usePath = !fContext->getMatrix().preservesRightAngles();
637 }
638#endif
639 }
640 // until we can both stroke and fill rectangles
641 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
642 usePath = true;
643 }
644
645 if (usePath) {
646 SkPath path;
647 path.addRect(rect);
648 this->drawPath(draw, path, paint, NULL, true);
649 return;
650 }
651
652 GrPaint grPaint;
653 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
654 return;
655 }
656
657 if (!doStroke) {
658 fContext->drawRect(grPaint, rect);
659 } else {
660 SkStrokeRec stroke(paint);
661 fContext->drawRect(grPaint, rect, &stroke);
662 }
663}
664
665///////////////////////////////////////////////////////////////////////////////
666
667void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
668 const SkPaint& paint) {
669 CHECK_FOR_ANNOTATION(paint);
670 CHECK_SHOULD_DRAW(draw, false);
671
672 bool usePath = !rect.isSimple();
673 // another two reasons we might need to call drawPath...
674 if (paint.getMaskFilter() || paint.getPathEffect()) {
675 usePath = true;
676 }
677 // until we can rotate rrects...
678 if (!usePath && !fContext->getMatrix().rectStaysRect()) {
679 usePath = true;
680 }
681
682 if (usePath) {
683 SkPath path;
684 path.addRRect(rect);
685 this->drawPath(draw, path, paint, NULL, true);
686 return;
687 }
688
689 GrPaint grPaint;
690 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
691 return;
692 }
693
694 SkStrokeRec stroke(paint);
695 fContext->drawRRect(grPaint, rect, stroke);
696}
697
698///////////////////////////////////////////////////////////////////////////////
699
700void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
701 const SkPaint& paint) {
702 CHECK_FOR_ANNOTATION(paint);
703 CHECK_SHOULD_DRAW(draw, false);
704
705 bool usePath = false;
706 // some basic reasons we might need to call drawPath...
707 if (paint.getMaskFilter() || paint.getPathEffect()) {
708 usePath = true;
709 }
710
711 if (usePath) {
712 SkPath path;
713 path.addOval(oval);
714 this->drawPath(draw, path, paint, NULL, true);
715 return;
716 }
717
718 GrPaint grPaint;
719 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
720 return;
721 }
722 SkStrokeRec stroke(paint);
723
724 fContext->drawOval(grPaint, oval, stroke);
725}
726
727#include "SkMaskFilter.h"
728#include "SkBounder.h"
729
730///////////////////////////////////////////////////////////////////////////////
731
732// helpers for applying mask filters
733namespace {
734
735// Draw a mask using the supplied paint. Since the coverage/geometry
736// is already burnt into the mask this boils down to a rect draw.
737// Return true if the mask was successfully drawn.
738bool draw_mask(GrContext* context, const SkRect& maskRect,
739 GrPaint* grp, GrTexture* mask) {
740 GrContext::AutoMatrix am;
741 if (!am.setIdentity(context, grp)) {
742 return false;
743 }
744
745 SkMatrix matrix;
746 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
747 matrix.postIDiv(mask->width(), mask->height());
748
749 grp->addCoverageEffect(GrSimpleTextureEffect::Create(mask, matrix))->unref();
750 context->drawRect(*grp, maskRect);
751 return true;
752}
753
754bool draw_with_mask_filter(GrContext* context, const SkPath& devPath,
755 SkMaskFilter* filter, const SkRegion& clip, SkBounder* bounder,
756 GrPaint* grp, SkPaint::Style style) {
757 SkMask srcM, dstM;
758
759 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
760 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
761 return false;
762 }
763 SkAutoMaskFreeImage autoSrc(srcM.fImage);
764
765 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
766 return false;
767 }
768 // this will free-up dstM when we're done (allocated in filterMask())
769 SkAutoMaskFreeImage autoDst(dstM.fImage);
770
771 if (clip.quickReject(dstM.fBounds)) {
772 return false;
773 }
774 if (bounder && !bounder->doIRect(dstM.fBounds)) {
775 return false;
776 }
777
778 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
779 // the current clip (and identity matrix) and GrPaint settings
780 GrTextureDesc desc;
781 desc.fWidth = dstM.fBounds.width();
782 desc.fHeight = dstM.fBounds.height();
783 desc.fConfig = kAlpha_8_GrPixelConfig;
784
785 GrAutoScratchTexture ast(context, desc);
786 GrTexture* texture = ast.texture();
787
788 if (NULL == texture) {
789 return false;
790 }
791 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
792 dstM.fImage, dstM.fRowBytes);
793
794 SkRect maskRect = SkRect::Make(dstM.fBounds);
795
796 return draw_mask(context, maskRect, grp, texture);
797}
798
799// Create a mask of 'devPath' and place the result in 'mask'. Return true on
800// success; false otherwise.
801bool create_mask_GPU(GrContext* context,
802 const SkRect& maskRect,
803 const SkPath& devPath,
804 const SkStrokeRec& stroke,
805 bool doAA,
806 GrAutoScratchTexture* mask) {
807 GrTextureDesc desc;
808 desc.fFlags = kRenderTarget_GrTextureFlagBit;
809 desc.fWidth = SkScalarCeilToInt(maskRect.width());
810 desc.fHeight = SkScalarCeilToInt(maskRect.height());
811 // We actually only need A8, but it often isn't supported as a
812 // render target so default to RGBA_8888
813 desc.fConfig = kRGBA_8888_GrPixelConfig;
814 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
815 desc.fConfig = kAlpha_8_GrPixelConfig;
816 }
817
818 mask->set(context, desc);
819 if (NULL == mask->texture()) {
820 return false;
821 }
822
823 GrTexture* maskTexture = mask->texture();
824 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
825
826 GrContext::AutoRenderTarget art(context, maskTexture->asRenderTarget());
827 GrContext::AutoClip ac(context, clipRect);
828
829 context->clear(NULL, 0x0, true);
830
831 GrPaint tempPaint;
832 if (doAA) {
833 tempPaint.setAntiAlias(true);
834 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
835 // blend coeff of zero requires dual source blending support in order
836 // to properly blend partially covered pixels. This means the AA
837 // code path may not be taken. So we use a dst blend coeff of ISA. We
838 // could special case AA draws to a dst surface with known alpha=0 to
839 // use a zero dst coeff when dual source blending isn't available.
840 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
841 }
842
843 GrContext::AutoMatrix am;
844
845 // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint.
846 SkMatrix translate;
847 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
848 am.set(context, translate);
849 context->drawPath(tempPaint, devPath, stroke);
850 return true;
851}
852
853SkBitmap wrap_texture(GrTexture* texture) {
reed@google.combf790232013-12-13 19:45:58 +0000854 SkImageInfo info;
855 texture->asImageInfo(&info);
856
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000857 SkBitmap result;
reed@google.combf790232013-12-13 19:45:58 +0000858 result.setConfig(info);
859 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000860 return result;
861}
862
863};
864
865void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
866 const SkPaint& paint, const SkMatrix* prePathMatrix,
867 bool pathIsMutable) {
868 CHECK_FOR_ANNOTATION(paint);
869 CHECK_SHOULD_DRAW(draw, false);
870
871 GrPaint grPaint;
872 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
873 return;
874 }
875
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000876 // If we have a prematrix, apply it to the path, optimizing for the case
877 // where the original path can in fact be modified in place (even though
878 // its parameter type is const).
879 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
880 SkPath tmpPath, effectPath;
881
882 if (prePathMatrix) {
883 SkPath* result = pathPtr;
884
885 if (!pathIsMutable) {
886 result = &tmpPath;
887 pathIsMutable = true;
888 }
889 // should I push prePathMatrix on our MV stack temporarily, instead
890 // of applying it here? See SkDraw.cpp
891 pathPtr->transform(*prePathMatrix, result);
892 pathPtr = result;
893 }
894 // at this point we're done with prePathMatrix
895 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
896
897 SkStrokeRec stroke(paint);
898 SkPathEffect* pathEffect = paint.getPathEffect();
899 const SkRect* cullRect = NULL; // TODO: what is our bounds?
900 if (pathEffect && pathEffect->filterPath(&effectPath, *pathPtr, &stroke,
901 cullRect)) {
902 pathPtr = &effectPath;
903 }
904
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000905 if (paint.getMaskFilter()) {
906 if (!stroke.isHairlineStyle()) {
907 if (stroke.applyToPath(&tmpPath, *pathPtr)) {
908 pathPtr = &tmpPath;
909 pathIsMutable = true;
910 stroke.setFillStyle();
911 }
912 }
913
914 // avoid possibly allocating a new path in transform if we can
915 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
916
917 // transform the path into device space
918 pathPtr->transform(fContext->getMatrix(), devPathPtr);
919
920 SkRect maskRect;
921 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
922 draw.fClip->getBounds(),
923 fContext->getMatrix(),
924 &maskRect)) {
925 SkIRect finalIRect;
926 maskRect.roundOut(&finalIRect);
927 if (draw.fClip->quickReject(finalIRect)) {
928 // clipped out
929 return;
930 }
931 if (NULL != draw.fBounder && !draw.fBounder->doIRect(finalIRect)) {
932 // nothing to draw
933 return;
934 }
935
936 GrAutoScratchTexture mask;
937
938 if (create_mask_GPU(fContext, maskRect, *devPathPtr, stroke,
939 grPaint.isAntiAlias(), &mask)) {
940 GrTexture* filtered;
941
942 if (paint.getMaskFilter()->filterMaskGPU(mask.texture(), maskRect, &filtered, true)) {
943 // filterMaskGPU gives us ownership of a ref to the result
944 SkAutoTUnref<GrTexture> atu(filtered);
945
946 // If the scratch texture that we used as the filter src also holds the filter
947 // result then we must detach so that this texture isn't recycled for a later
948 // draw.
949 if (filtered == mask.texture()) {
950 mask.detach();
951 filtered->unref(); // detach transfers GrAutoScratchTexture's ref to us.
952 }
953
954 if (draw_mask(fContext, maskRect, &grPaint, filtered)) {
955 // This path is completely drawn
956 return;
957 }
958 }
959 }
960 }
961
962 // draw the mask on the CPU - this is a fallthrough path in case the
963 // GPU path fails
964 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
965 SkPaint::kFill_Style;
966 draw_with_mask_filter(fContext, *devPathPtr, paint.getMaskFilter(),
967 *draw.fClip, draw.fBounder, &grPaint, style);
968 return;
969 }
970
971 fContext->drawPath(grPaint, *pathPtr, stroke);
972}
973
974static const int kBmpSmallTileSize = 1 << 10;
975
976static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
977 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
978 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
979 return tilesX * tilesY;
980}
981
982static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
983 if (maxTileSize <= kBmpSmallTileSize) {
984 return maxTileSize;
985 }
986
987 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
988 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
989
990 maxTileTotalTileSize *= maxTileSize * maxTileSize;
991 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
992
993 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
994 return kBmpSmallTileSize;
995 } else {
996 return maxTileSize;
997 }
998}
999
1000// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
1001// pixels from the bitmap are necessary.
1002static void determine_clipped_src_rect(const GrContext* context,
1003 const SkBitmap& bitmap,
1004 const SkRect* srcRectPtr,
1005 SkIRect* clippedSrcIRect) {
1006 const GrClipData* clip = context->getClip();
1007 clip->getConservativeBounds(context->getRenderTarget(), clippedSrcIRect, NULL);
1008 SkMatrix inv;
1009 if (!context->getMatrix().invert(&inv)) {
1010 clippedSrcIRect->setEmpty();
1011 return;
1012 }
1013 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
1014 inv.mapRect(&clippedSrcRect);
1015 if (NULL != srcRectPtr) {
1016 if (!clippedSrcRect.intersect(*srcRectPtr)) {
1017 clippedSrcIRect->setEmpty();
1018 return;
1019 }
1020 }
1021 clippedSrcRect.roundOut(clippedSrcIRect);
1022 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1023 if (!clippedSrcIRect->intersect(bmpBounds)) {
1024 clippedSrcIRect->setEmpty();
1025 }
1026}
1027
1028bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
1029 const GrTextureParams& params,
1030 const SkRect* srcRectPtr,
1031 int maxTileSize,
1032 int* tileSize,
1033 SkIRect* clippedSrcRect) const {
1034 // if bitmap is explictly texture backed then just use the texture
1035 if (NULL != bitmap.getTexture()) {
1036 return false;
1037 }
1038
1039 // if it's larger than the max tile size, then we have no choice but tiling.
1040 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
1041 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
1042 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
1043 return true;
1044 }
1045
1046 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
1047 return false;
1048 }
1049
1050 // if the entire texture is already in our cache then no reason to tile it
1051 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
1052 return false;
1053 }
1054
1055 // At this point we know we could do the draw by uploading the entire bitmap
1056 // as a texture. However, if the texture would be large compared to the
1057 // cache size and we don't require most of it for this draw then tile to
1058 // reduce the amount of upload and cache spill.
1059
1060 // assumption here is that sw bitmap size is a good proxy for its size as
1061 // a texture
1062 size_t bmpSize = bitmap.getSize();
1063 size_t cacheSize;
1064 fContext->getTextureCacheLimits(NULL, &cacheSize);
1065 if (bmpSize < cacheSize / 2) {
1066 return false;
1067 }
1068
1069 // Figure out how much of the src we will need based on the src rect and clipping.
1070 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
1071 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
1072 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
1073 kBmpSmallTileSize * kBmpSmallTileSize;
1074
1075 return usedTileBytes < 2 * bmpSize;
1076}
1077
1078void SkGpuDevice::drawBitmap(const SkDraw& draw,
1079 const SkBitmap& bitmap,
1080 const SkMatrix& m,
1081 const SkPaint& paint) {
1082 // We cannot call drawBitmapRect here since 'm' could be anything
1083 this->drawBitmapCommon(draw, bitmap, NULL, m, paint,
1084 SkCanvas::kNone_DrawBitmapRectFlag);
1085}
1086
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001087// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001088// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
1089// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001090static inline void clamped_outset_with_offset(SkIRect* iRect,
1091 int outset,
1092 SkPoint* offset,
1093 const SkIRect& clamp) {
1094 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001095
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001096 int leftClampDelta = clamp.fLeft - iRect->fLeft;
1097 if (leftClampDelta > 0) {
1098 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001099 iRect->fLeft = clamp.fLeft;
1100 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001101 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001102 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001103
1104 int topClampDelta = clamp.fTop - iRect->fTop;
1105 if (topClampDelta > 0) {
1106 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001107 iRect->fTop = clamp.fTop;
1108 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001109 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001110 }
1111
1112 if (iRect->fRight > clamp.fRight) {
1113 iRect->fRight = clamp.fRight;
1114 }
1115 if (iRect->fBottom > clamp.fBottom) {
1116 iRect->fBottom = clamp.fBottom;
1117 }
1118}
1119
1120void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1121 const SkBitmap& bitmap,
1122 const SkRect* srcRectPtr,
1123 const SkMatrix& m,
1124 const SkPaint& paint,
1125 SkCanvas::DrawBitmapRectFlags flags) {
1126 CHECK_SHOULD_DRAW(draw, false);
1127
1128 SkRect srcRect;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001129 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
1130 // in the (easier) bleed case, so update flags.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001131 if (NULL == srcRectPtr) {
1132 srcRect.set(0, 0, SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()));
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001133 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001134 } else {
1135 srcRect = *srcRectPtr;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001136 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
1137 srcRect.fRight >= bitmap.width() && srcRect.fBottom >= bitmap.height()) {
1138 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
1139 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001140 }
1141
1142 if (paint.getMaskFilter()){
1143 // Convert the bitmap to a shader so that the rect can be drawn
1144 // through drawRect, which supports mask filters.
1145 SkMatrix newM(m);
1146 SkBitmap tmp; // subset of bitmap, if necessary
1147 const SkBitmap* bitmapPtr = &bitmap;
1148 if (NULL != srcRectPtr) {
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001149 // In bleed mode we position and trim the bitmap based on the src rect which is
1150 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
1151 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
1152 // compensate.
1153 if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) {
1154 SkIRect iSrc;
1155 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001156
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001157 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
1158 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001159
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001160 if (!bitmap.extractSubset(&tmp, iSrc)) {
1161 return; // extraction failed
1162 }
1163 bitmapPtr = &tmp;
1164 srcRect.offset(-offset.fX, -offset.fY);
1165 // The source rect has changed so update the matrix
1166 newM.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001167 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001168 }
1169
1170 SkPaint paintWithTexture(paint);
1171 paintWithTexture.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
1172 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
1173
1174 // Transform 'newM' needs to be concatenated to the current matrix,
1175 // rather than transforming the primitive directly, so that 'newM' will
1176 // also affect the behavior of the mask filter.
1177 SkMatrix drawMatrix;
1178 drawMatrix.setConcat(fContext->getMatrix(), newM);
1179 SkDraw transformedDraw(draw);
1180 transformedDraw.fMatrix = &drawMatrix;
1181
1182 this->drawRect(transformedDraw, srcRect, paintWithTexture);
1183
1184 return;
1185 }
1186
1187 fContext->concatMatrix(m);
1188
1189 GrTextureParams params;
1190 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel();
1191 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001192
1193 int tileFilterPad;
1194 bool doBicubic = false;
1195
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001196 switch(paintFilterLevel) {
1197 case SkPaint::kNone_FilterLevel:
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001198 tileFilterPad = 0;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001199 textureFilterMode = GrTextureParams::kNone_FilterMode;
1200 break;
1201 case SkPaint::kLow_FilterLevel:
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001202 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001203 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1204 break;
1205 case SkPaint::kMedium_FilterLevel:
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001206 tileFilterPad = 1;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001207 if (fContext->getMatrix().getMinStretch() < SK_Scalar1) {
1208 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1209 } else {
1210 // Don't trigger MIP level generation unnecessarily.
1211 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1212 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001213 break;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001214 case SkPaint::kHigh_FilterLevel:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001215 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001216 if (fContext->getMatrix().getMinStretch() >= SK_Scalar1) {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001217 // We will install an effect that does the filtering in the shader.
1218 textureFilterMode = GrTextureParams::kNone_FilterMode;
1219 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1220 doBicubic = true;
1221 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001222 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1223 tileFilterPad = 1;
1224 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001225 break;
1226 default:
1227 SkErrorInternals::SetError( kInvalidPaint_SkError,
1228 "Sorry, I don't understand the filtering "
1229 "mode you asked for. Falling back to "
1230 "MIPMaps.");
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001231 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001232 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1233 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001234 }
1235
1236 params.setFilterMode(textureFilterMode);
1237
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001238 int maxTileSize = fContext->getMaxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001239 int tileSize;
1240
1241 SkIRect clippedSrcRect;
1242 if (this->shouldTileBitmap(bitmap, params, srcRectPtr, maxTileSize, &tileSize,
1243 &clippedSrcRect)) {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001244 this->drawTiledBitmap(bitmap, srcRect, clippedSrcRect, params, paint, flags, tileSize,
1245 doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001246 } else {
1247 // take the simple case
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001248 this->internalDrawBitmap(bitmap, srcRect, params, paint, flags, doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001249 }
1250}
1251
1252// Break 'bitmap' into several tiles to draw it since it has already
1253// been determined to be too large to fit in VRAM
1254void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
1255 const SkRect& srcRect,
1256 const SkIRect& clippedSrcIRect,
1257 const GrTextureParams& params,
1258 const SkPaint& paint,
1259 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001260 int tileSize,
1261 bool bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001262 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1263
1264 int nx = bitmap.width() / tileSize;
1265 int ny = bitmap.height() / tileSize;
1266 for (int x = 0; x <= nx; x++) {
1267 for (int y = 0; y <= ny; y++) {
1268 SkRect tileR;
1269 tileR.set(SkIntToScalar(x * tileSize),
1270 SkIntToScalar(y * tileSize),
1271 SkIntToScalar((x + 1) * tileSize),
1272 SkIntToScalar((y + 1) * tileSize));
1273
1274 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1275 continue;
1276 }
1277
1278 if (!tileR.intersect(srcRect)) {
1279 continue;
1280 }
1281
1282 SkBitmap tmpB;
1283 SkIRect iTileR;
1284 tileR.roundOut(&iTileR);
1285 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1286 SkIntToScalar(iTileR.fTop));
1287
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001288 if (SkPaint::kNone_FilterLevel != paint.getFilterLevel() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001289 SkIRect iClampRect;
1290
1291 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1292 // In bleed mode we want to always expand the tile on all edges
1293 // but stay within the bitmap bounds
1294 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1295 } else {
1296 // In texture-domain/clamp mode we only want to expand the
1297 // tile on edges interior to "srcRect" (i.e., we want to
1298 // not bleed across the original clamped edges)
1299 srcRect.roundOut(&iClampRect);
1300 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001301 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1302 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001303 }
1304
1305 if (bitmap.extractSubset(&tmpB, iTileR)) {
1306 // now offset it to make it "local" to our tmp bitmap
1307 tileR.offset(-offset.fX, -offset.fY);
1308 SkMatrix tmpM;
1309 tmpM.setTranslate(offset.fX, offset.fY);
1310 GrContext::AutoMatrix am;
1311 am.setPreConcat(fContext, tmpM);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001312 this->internalDrawBitmap(tmpB, tileR, params, paint, flags, bicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001313 }
1314 }
1315 }
1316}
1317
1318static bool has_aligned_samples(const SkRect& srcRect,
1319 const SkRect& transformedRect) {
1320 // detect pixel disalignment
1321 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1322 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1323 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1324 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1325 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1326 COLOR_BLEED_TOLERANCE &&
1327 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1328 COLOR_BLEED_TOLERANCE) {
1329 return true;
1330 }
1331 return false;
1332}
1333
1334static bool may_color_bleed(const SkRect& srcRect,
1335 const SkRect& transformedRect,
1336 const SkMatrix& m) {
1337 // Only gets called if has_aligned_samples returned false.
1338 // So we can assume that sampling is axis aligned but not texel aligned.
1339 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
1340 SkRect innerSrcRect(srcRect), innerTransformedRect,
1341 outerTransformedRect(transformedRect);
1342 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1343 m.mapRect(&innerTransformedRect, innerSrcRect);
1344
1345 // The gap between outerTransformedRect and innerTransformedRect
1346 // represents the projection of the source border area, which is
1347 // problematic for color bleeding. We must check whether any
1348 // destination pixels sample the border area.
1349 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1350 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1351 SkIRect outer, inner;
1352 outerTransformedRect.round(&outer);
1353 innerTransformedRect.round(&inner);
1354 // If the inner and outer rects round to the same result, it means the
1355 // border does not overlap any pixel centers. Yay!
1356 return inner != outer;
1357}
1358
1359
1360/*
1361 * This is called by drawBitmap(), which has to handle images that may be too
1362 * large to be represented by a single texture.
1363 *
1364 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1365 * and that non-texture portion of the GrPaint has already been setup.
1366 */
1367void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
1368 const SkRect& srcRect,
1369 const GrTextureParams& params,
1370 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001371 SkCanvas::DrawBitmapRectFlags flags,
1372 bool bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001373 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1374 bitmap.height() <= fContext->getMaxTextureSize());
1375
1376 GrTexture* texture;
1377 SkAutoCachedTexture act(this, bitmap, &params, &texture);
1378 if (NULL == texture) {
1379 return;
1380 }
1381
1382 SkRect dstRect(srcRect);
1383 SkRect paintRect;
1384 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1385 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1386 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1387 SkScalarMul(srcRect.fTop, hInv),
1388 SkScalarMul(srcRect.fRight, wInv),
1389 SkScalarMul(srcRect.fBottom, hInv));
1390
1391 bool needsTextureDomain = false;
1392 if (!(flags & SkCanvas::kBleed_DrawBitmapRectFlag) &&
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001393 (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode)) {
1394 // Need texture domain if drawing a sub rect
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001395 needsTextureDomain = srcRect.width() < bitmap.width() ||
1396 srcRect.height() < bitmap.height();
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001397 if (!bicubic && needsTextureDomain && fContext->getMatrix().rectStaysRect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001398 const SkMatrix& matrix = fContext->getMatrix();
1399 // sampling is axis-aligned
1400 SkRect transformedRect;
1401 matrix.mapRect(&transformedRect, srcRect);
1402
1403 if (has_aligned_samples(srcRect, transformedRect)) {
1404 // We could also turn off filtering here (but we already did a cache lookup with
1405 // params).
1406 needsTextureDomain = false;
1407 } else {
1408 needsTextureDomain = may_color_bleed(srcRect, transformedRect, matrix);
1409 }
1410 }
1411 }
1412
1413 SkRect textureDomain = SkRect::MakeEmpty();
1414 SkAutoTUnref<GrEffectRef> effect;
1415 if (needsTextureDomain) {
1416 // Use a constrained texture domain to avoid color bleeding
1417 SkScalar left, top, right, bottom;
1418 if (srcRect.width() > SK_Scalar1) {
1419 SkScalar border = SK_ScalarHalf / texture->width();
1420 left = paintRect.left() + border;
1421 right = paintRect.right() - border;
1422 } else {
1423 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1424 }
1425 if (srcRect.height() > SK_Scalar1) {
1426 SkScalar border = SK_ScalarHalf / texture->height();
1427 top = paintRect.top() + border;
1428 bottom = paintRect.bottom() - border;
1429 } else {
1430 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1431 }
1432 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001433 if (bicubic) {
1434 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
1435 } else {
1436 effect.reset(GrTextureDomainEffect::Create(texture,
1437 SkMatrix::I(),
1438 textureDomain,
1439 GrTextureDomain::kClamp_Mode,
1440 params.filterMode()));
1441 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001442 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001443 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1444 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
1445 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001446 } else {
1447 effect.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
1448 }
1449
1450 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1451 // the rest from the SkPaint.
1452 GrPaint grPaint;
1453 grPaint.addColorEffect(effect);
1454 bool alphaOnly = !(SkBitmap::kA8_Config == bitmap.config());
1455 if (!skPaint2GrPaintNoShader(this, paint, alphaOnly, false, &grPaint)) {
1456 return;
1457 }
1458
1459 fContext->drawRectToRect(grPaint, dstRect, paintRect, NULL);
1460}
1461
1462static bool filter_texture(SkBaseDevice* device, GrContext* context,
1463 GrTexture* texture, SkImageFilter* filter,
1464 int w, int h, const SkMatrix& ctm, SkBitmap* result,
1465 SkIPoint* offset) {
1466 SkASSERT(filter);
1467 SkDeviceImageFilterProxy proxy(device);
1468
1469 if (filter->canFilterImageGPU()) {
1470 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1471 // filter. Also set the clip wide open and the matrix to identity.
1472 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
1473 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctm, result, offset);
1474 } else {
1475 return false;
1476 }
1477}
1478
1479void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1480 int left, int top, const SkPaint& paint) {
1481 // drawSprite is defined to be in device coords.
1482 CHECK_SHOULD_DRAW(draw, true);
1483
1484 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1485 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1486 return;
1487 }
1488
1489 int w = bitmap.width();
1490 int h = bitmap.height();
1491
1492 GrTexture* texture;
1493 // draw sprite uses the default texture params
1494 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
1495
1496 SkImageFilter* filter = paint.getImageFilter();
1497 SkIPoint offset = SkIPoint::Make(left, top);
1498 // This bitmap will own the filtered result as a texture.
1499 SkBitmap filteredBitmap;
1500
1501 if (NULL != filter) {
1502 SkMatrix matrix(*draw.fMatrix);
1503 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
1504 if (filter_texture(this, fContext, texture, filter, w, h, matrix, &filteredBitmap,
1505 &offset)) {
1506 texture = (GrTexture*) filteredBitmap.getTexture();
1507 w = filteredBitmap.width();
1508 h = filteredBitmap.height();
1509 } else {
1510 return;
1511 }
1512 }
1513
1514 GrPaint grPaint;
1515 grPaint.addColorTextureEffect(texture, SkMatrix::I());
1516
1517 if(!skPaint2GrPaintNoShader(this, paint, true, false, &grPaint)) {
1518 return;
1519 }
1520
1521 fContext->drawRectToRect(grPaint,
1522 SkRect::MakeXYWH(SkIntToScalar(offset.fX),
1523 SkIntToScalar(offset.fY),
1524 SkIntToScalar(w),
1525 SkIntToScalar(h)),
1526 SkRect::MakeXYWH(0,
1527 0,
1528 SK_Scalar1 * w / texture->width(),
1529 SK_Scalar1 * h / texture->height()));
1530}
1531
1532void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
1533 const SkRect* src, const SkRect& dst,
1534 const SkPaint& paint,
1535 SkCanvas::DrawBitmapRectFlags flags) {
1536 SkMatrix matrix;
1537 SkRect bitmapBounds, tmpSrc;
1538
1539 bitmapBounds.set(0, 0,
1540 SkIntToScalar(bitmap.width()),
1541 SkIntToScalar(bitmap.height()));
1542
1543 // Compute matrix from the two rectangles
1544 if (NULL != src) {
1545 tmpSrc = *src;
1546 } else {
1547 tmpSrc = bitmapBounds;
1548 }
1549 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1550
1551 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
1552 if (NULL != src) {
1553 if (!bitmapBounds.contains(tmpSrc)) {
1554 if (!tmpSrc.intersect(bitmapBounds)) {
1555 return; // nothing to draw
1556 }
1557 }
1558 }
1559
1560 this->drawBitmapCommon(draw, bitmap, &tmpSrc, matrix, paint, flags);
1561}
1562
1563void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1564 int x, int y, const SkPaint& paint) {
1565 // clear of the source device must occur before CHECK_SHOULD_DRAW
1566 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1567 if (dev->fNeedClear) {
1568 // TODO: could check here whether we really need to draw at all
1569 dev->clear(0x0);
1570 }
1571
1572 // drawDevice is defined to be in device coords.
1573 CHECK_SHOULD_DRAW(draw, true);
1574
1575 GrRenderTarget* devRT = dev->accessRenderTarget();
1576 GrTexture* devTex;
1577 if (NULL == (devTex = devRT->asTexture())) {
1578 return;
1579 }
1580
1581 const SkBitmap& bm = dev->accessBitmap(false);
1582 int w = bm.width();
1583 int h = bm.height();
1584
1585 SkImageFilter* filter = paint.getImageFilter();
1586 // This bitmap will own the filtered result as a texture.
1587 SkBitmap filteredBitmap;
1588
1589 if (NULL != filter) {
1590 SkIPoint offset = SkIPoint::Make(0, 0);
1591 SkMatrix matrix(*draw.fMatrix);
1592 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
1593 if (filter_texture(this, fContext, devTex, filter, w, h, matrix, &filteredBitmap,
1594 &offset)) {
1595 devTex = filteredBitmap.getTexture();
1596 w = filteredBitmap.width();
1597 h = filteredBitmap.height();
1598 x += offset.fX;
1599 y += offset.fY;
1600 } else {
1601 return;
1602 }
1603 }
1604
1605 GrPaint grPaint;
1606 grPaint.addColorTextureEffect(devTex, SkMatrix::I());
1607
1608 if (!skPaint2GrPaintNoShader(this, paint, true, false, &grPaint)) {
1609 return;
1610 }
1611
1612 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1613 SkIntToScalar(y),
1614 SkIntToScalar(w),
1615 SkIntToScalar(h));
1616
1617 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1618 // scratch texture).
1619 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1620 SK_Scalar1 * h / devTex->height());
1621
1622 fContext->drawRectToRect(grPaint, dstRect, srcRect);
1623}
1624
1625bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
1626 return filter->canFilterImageGPU();
1627}
1628
1629bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1630 const SkMatrix& ctm,
1631 SkBitmap* result, SkIPoint* offset) {
1632 // want explicitly our impl, so guard against a subclass of us overriding it
1633 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1634 return false;
1635 }
1636
1637 SkAutoLockPixels alp(src, !src.getTexture());
1638 if (!src.getTexture() && !src.readyToDraw()) {
1639 return false;
1640 }
1641
1642 GrTexture* texture;
1643 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1644 // must be pushed upstack.
1645 SkAutoCachedTexture act(this, src, NULL, &texture);
1646
1647 return filter_texture(this, fContext, texture, filter, src.width(), src.height(), ctm, result,
1648 offset);
1649}
1650
1651///////////////////////////////////////////////////////////////////////////////
1652
1653// must be in SkCanvas::VertexMode order
1654static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1655 kTriangles_GrPrimitiveType,
1656 kTriangleStrip_GrPrimitiveType,
1657 kTriangleFan_GrPrimitiveType,
1658};
1659
1660void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1661 int vertexCount, const SkPoint vertices[],
1662 const SkPoint texs[], const SkColor colors[],
1663 SkXfermode* xmode,
1664 const uint16_t indices[], int indexCount,
1665 const SkPaint& paint) {
1666 CHECK_SHOULD_DRAW(draw, false);
1667
1668 GrPaint grPaint;
1669 // we ignore the shader if texs is null.
1670 if (NULL == texs) {
1671 if (!skPaint2GrPaintNoShader(this, paint, false, NULL == colors, &grPaint)) {
1672 return;
1673 }
1674 } else {
1675 if (!skPaint2GrPaintShader(this, paint, NULL == colors, &grPaint)) {
1676 return;
1677 }
1678 }
1679
1680 if (NULL != xmode && NULL != texs && NULL != colors) {
1681 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1682 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1683#if 0
1684 return
1685#endif
1686 }
1687 }
1688
1689 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1690 if (NULL != colors) {
1691 // need to convert byte order and from non-PM to PM
1692 convertedColors.reset(vertexCount);
1693 for (int i = 0; i < vertexCount; ++i) {
1694 convertedColors[i] = SkColor2GrColor(colors[i]);
1695 }
1696 colors = convertedColors.get();
1697 }
1698 fContext->drawVertices(grPaint,
1699 gVertexMode2PrimitiveType[vmode],
1700 vertexCount,
1701 (GrPoint*) vertices,
1702 (GrPoint*) texs,
1703 colors,
1704 indices,
1705 indexCount);
1706}
1707
1708///////////////////////////////////////////////////////////////////////////////
1709
1710static void GlyphCacheAuxProc(void* data) {
1711 GrFontScaler* scaler = (GrFontScaler*)data;
1712 SkSafeUnref(scaler);
1713}
1714
1715static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1716 void* auxData;
1717 GrFontScaler* scaler = NULL;
1718 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1719 scaler = (GrFontScaler*)auxData;
1720 }
1721 if (NULL == scaler) {
1722 scaler = SkNEW_ARGS(SkGrFontScaler, (cache));
1723 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1724 }
1725 return scaler;
1726}
1727
1728static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1729 SkFixed fx, SkFixed fy,
1730 const SkGlyph& glyph) {
1731 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1732
1733 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
1734
1735 if (NULL == procs->fFontScaler) {
1736 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1737 }
1738
1739 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1740 glyph.getSubXFixed(),
1741 glyph.getSubYFixed()),
1742 SkFixedFloorToFixed(fx),
1743 SkFixedFloorToFixed(fy),
1744 procs->fFontScaler);
1745}
1746
1747SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
1748
1749 // deferred allocation
1750 if (NULL == fDrawProcs) {
1751 fDrawProcs = SkNEW(GrSkDrawProcs);
1752 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1753 fDrawProcs->fContext = fContext;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001754 }
1755
1756 // init our (and GL's) state
1757 fDrawProcs->fTextContext = context;
1758 fDrawProcs->fFontScaler = NULL;
1759 return fDrawProcs;
1760}
1761
1762void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1763 size_t byteLength, SkScalar x, SkScalar y,
1764 const SkPaint& paint) {
1765 CHECK_SHOULD_DRAW(draw, false);
1766
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001767 if (SkDraw::ShouldDrawTextAsPaths(paint, fContext->getMatrix())) {
1768 draw.drawText_asPaths((const char*)text, byteLength, x, y, paint);
1769#if SK_DISTANCEFIELD_FONTS
1770 } else if (!paint.getRasterizer()) {
1771 GrPaint grPaint;
1772 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
1773 return;
1774 }
1775
1776 SkDEBUGCODE(this->validate();)
1777
1778 GrDistanceFieldTextContext context(fContext, grPaint, paint);
1779
1780 SkAutoGlyphCache autoCache(context.getSkPaint(), &this->fLeakyProperties, NULL);
1781 SkGlyphCache* cache = autoCache.getCache();
1782 GrFontScaler* fontScaler = get_gr_font_scaler(cache);
1783
1784 context.drawText((const char *)text, byteLength, x, y, cache, fontScaler);
1785#endif
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001786 } else {
1787 SkDraw myDraw(draw);
1788
1789 GrPaint grPaint;
1790 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
1791 return;
1792 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001793
1794 GrBitmapTextContext context(fContext, grPaint, paint.getColor());
1795 myDraw.fProcs = this->initDrawForText(&context);
1796 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001797 }
1798}
1799
1800void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1801 size_t byteLength, const SkScalar pos[],
1802 SkScalar constY, int scalarsPerPos,
1803 const SkPaint& paint) {
1804 CHECK_SHOULD_DRAW(draw, false);
1805
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001806 if (SkDraw::ShouldDrawTextAsPaths(paint, fContext->getMatrix())) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001807 // this guy will just call our drawPath()
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001808 draw.drawPosText_asPaths((const char*)text, byteLength, pos, constY,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001809 scalarsPerPos, paint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001810#if SK_DISTANCEFIELD_FONTS
1811 } else if (!paint.getRasterizer()) {
1812 GrPaint grPaint;
1813 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
1814 return;
1815 }
1816
1817 SkDEBUGCODE(this->validate();)
1818
1819 GrDistanceFieldTextContext context(fContext, grPaint, paint);
1820
1821 SkAutoGlyphCache autoCache(context.getSkPaint(), &this->fLeakyProperties, NULL);
1822 SkGlyphCache* cache = autoCache.getCache();
1823 GrFontScaler* fontScaler = get_gr_font_scaler(cache);
1824
1825 context.drawPosText((const char *)text, byteLength, pos, constY, scalarsPerPos,
1826 cache, fontScaler);
1827#endif
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001828 } else {
1829 SkDraw myDraw(draw);
1830
1831 GrPaint grPaint;
1832 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
1833 return;
1834 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001835 GrBitmapTextContext context(fContext, grPaint, paint.getColor());
1836 myDraw.fProcs = this->initDrawForText(&context);
1837 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1838 scalarsPerPos, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001839 }
1840}
1841
1842void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1843 size_t len, const SkPath& path,
1844 const SkMatrix* m, const SkPaint& paint) {
1845 CHECK_SHOULD_DRAW(draw, false);
1846
1847 SkASSERT(draw.fDevice == this);
1848 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1849}
1850
1851///////////////////////////////////////////////////////////////////////////////
1852
1853bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1854 if (!paint.isLCDRenderText()) {
1855 // we're cool with the paint as is
1856 return false;
1857 }
1858
1859 if (paint.getShader() ||
1860 paint.getXfermode() || // unless its srcover
1861 paint.getMaskFilter() ||
1862 paint.getRasterizer() ||
1863 paint.getColorFilter() ||
1864 paint.getPathEffect() ||
1865 paint.isFakeBoldText() ||
1866 paint.getStyle() != SkPaint::kFill_Style) {
1867 // turn off lcd
1868 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1869 flags->fHinting = paint.getHinting();
1870 return true;
1871 }
1872 // we're cool with the paint as is
1873 return false;
1874}
1875
1876void SkGpuDevice::flush() {
1877 DO_DEFERRED_CLEAR();
1878 fContext->resolveRenderTarget(fRenderTarget);
1879}
1880
1881///////////////////////////////////////////////////////////////////////////////
1882
1883SkBaseDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1884 int width, int height,
1885 bool isOpaque,
1886 Usage usage) {
1887 GrTextureDesc desc;
1888 desc.fConfig = fRenderTarget->config();
1889 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1890 desc.fWidth = width;
1891 desc.fHeight = height;
1892 desc.fSampleCnt = fRenderTarget->numSamples();
1893
1894 SkAutoTUnref<GrTexture> texture;
1895 // Skia's convention is to only clear a device if it is non-opaque.
1896 bool needClear = !isOpaque;
1897
1898#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1899 // layers are never draw in repeat modes, so we can request an approx
1900 // match and ignore any padding.
1901 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == usage) ?
1902 GrContext::kApprox_ScratchTexMatch :
1903 GrContext::kExact_ScratchTexMatch;
1904 texture.reset(fContext->lockAndRefScratchTexture(desc, match));
1905#else
1906 texture.reset(fContext->createUncachedTexture(desc, NULL, 0));
1907#endif
1908 if (NULL != texture.get()) {
1909 return SkNEW_ARGS(SkGpuDevice,(fContext, texture, needClear));
1910 } else {
1911 GrPrintf("---- failed to create compatible device texture [%d %d]\n", width, height);
1912 return NULL;
1913 }
1914}
1915
1916SkGpuDevice::SkGpuDevice(GrContext* context,
1917 GrTexture* texture,
1918 bool needClear)
1919 : SkBitmapDevice(make_bitmap(context, texture->asRenderTarget())) {
1920
1921 SkASSERT(texture && texture->asRenderTarget());
1922 // This constructor is called from onCreateCompatibleDevice. It has locked the RT in the texture
1923 // cache. We pass true for the third argument so that it will get unlocked.
1924 this->initFromRenderTarget(context, texture->asRenderTarget(), true);
1925 fNeedClear = needClear;
1926}