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