blob: afdc7e92df19096e95c86b8fc5378c0df78327be [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
849 // can we cheat, and treat a thin stroke as a hairline w/ coverage
850 // if we can, we draw lots faster (raster device does this same test)
851 SkScalar hairlineCoverage;
852 bool doHairLine = SkDrawTreatAsHairline(paint, fContext->getMatrix(), &hairlineCoverage);
853 if (doHairLine) {
854 grPaint.setCoverage(SkScalarRoundToInt(hairlineCoverage * grPaint.getCoverage()));
855 }
856
857 // If we have a prematrix, apply it to the path, optimizing for the case
858 // where the original path can in fact be modified in place (even though
859 // its parameter type is const).
860 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
861 SkPath tmpPath, effectPath;
862
863 if (prePathMatrix) {
864 SkPath* result = pathPtr;
865
866 if (!pathIsMutable) {
867 result = &tmpPath;
868 pathIsMutable = true;
869 }
870 // should I push prePathMatrix on our MV stack temporarily, instead
871 // of applying it here? See SkDraw.cpp
872 pathPtr->transform(*prePathMatrix, result);
873 pathPtr = result;
874 }
875 // at this point we're done with prePathMatrix
876 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
877
878 SkStrokeRec stroke(paint);
879 SkPathEffect* pathEffect = paint.getPathEffect();
880 const SkRect* cullRect = NULL; // TODO: what is our bounds?
881 if (pathEffect && pathEffect->filterPath(&effectPath, *pathPtr, &stroke,
882 cullRect)) {
883 pathPtr = &effectPath;
884 }
885
886 if (!pathEffect && doHairLine) {
887 stroke.setHairlineStyle();
888 }
889
890 if (paint.getMaskFilter()) {
891 if (!stroke.isHairlineStyle()) {
892 if (stroke.applyToPath(&tmpPath, *pathPtr)) {
893 pathPtr = &tmpPath;
894 pathIsMutable = true;
895 stroke.setFillStyle();
896 }
897 }
898
899 // avoid possibly allocating a new path in transform if we can
900 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
901
902 // transform the path into device space
903 pathPtr->transform(fContext->getMatrix(), devPathPtr);
904
905 SkRect maskRect;
906 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
907 draw.fClip->getBounds(),
908 fContext->getMatrix(),
909 &maskRect)) {
910 SkIRect finalIRect;
911 maskRect.roundOut(&finalIRect);
912 if (draw.fClip->quickReject(finalIRect)) {
913 // clipped out
914 return;
915 }
916 if (NULL != draw.fBounder && !draw.fBounder->doIRect(finalIRect)) {
917 // nothing to draw
918 return;
919 }
920
921 GrAutoScratchTexture mask;
922
923 if (create_mask_GPU(fContext, maskRect, *devPathPtr, stroke,
924 grPaint.isAntiAlias(), &mask)) {
925 GrTexture* filtered;
926
927 if (paint.getMaskFilter()->filterMaskGPU(mask.texture(), maskRect, &filtered, true)) {
928 // filterMaskGPU gives us ownership of a ref to the result
929 SkAutoTUnref<GrTexture> atu(filtered);
930
931 // If the scratch texture that we used as the filter src also holds the filter
932 // result then we must detach so that this texture isn't recycled for a later
933 // draw.
934 if (filtered == mask.texture()) {
935 mask.detach();
936 filtered->unref(); // detach transfers GrAutoScratchTexture's ref to us.
937 }
938
939 if (draw_mask(fContext, maskRect, &grPaint, filtered)) {
940 // This path is completely drawn
941 return;
942 }
943 }
944 }
945 }
946
947 // draw the mask on the CPU - this is a fallthrough path in case the
948 // GPU path fails
949 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
950 SkPaint::kFill_Style;
951 draw_with_mask_filter(fContext, *devPathPtr, paint.getMaskFilter(),
952 *draw.fClip, draw.fBounder, &grPaint, style);
953 return;
954 }
955
956 fContext->drawPath(grPaint, *pathPtr, stroke);
957}
958
959static const int kBmpSmallTileSize = 1 << 10;
960
961static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
962 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
963 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
964 return tilesX * tilesY;
965}
966
967static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
968 if (maxTileSize <= kBmpSmallTileSize) {
969 return maxTileSize;
970 }
971
972 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
973 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
974
975 maxTileTotalTileSize *= maxTileSize * maxTileSize;
976 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
977
978 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
979 return kBmpSmallTileSize;
980 } else {
981 return maxTileSize;
982 }
983}
984
985// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
986// pixels from the bitmap are necessary.
987static void determine_clipped_src_rect(const GrContext* context,
988 const SkBitmap& bitmap,
989 const SkRect* srcRectPtr,
990 SkIRect* clippedSrcIRect) {
991 const GrClipData* clip = context->getClip();
992 clip->getConservativeBounds(context->getRenderTarget(), clippedSrcIRect, NULL);
993 SkMatrix inv;
994 if (!context->getMatrix().invert(&inv)) {
995 clippedSrcIRect->setEmpty();
996 return;
997 }
998 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
999 inv.mapRect(&clippedSrcRect);
1000 if (NULL != srcRectPtr) {
1001 if (!clippedSrcRect.intersect(*srcRectPtr)) {
1002 clippedSrcIRect->setEmpty();
1003 return;
1004 }
1005 }
1006 clippedSrcRect.roundOut(clippedSrcIRect);
1007 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1008 if (!clippedSrcIRect->intersect(bmpBounds)) {
1009 clippedSrcIRect->setEmpty();
1010 }
1011}
1012
1013bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
1014 const GrTextureParams& params,
1015 const SkRect* srcRectPtr,
1016 int maxTileSize,
1017 int* tileSize,
1018 SkIRect* clippedSrcRect) const {
1019 // if bitmap is explictly texture backed then just use the texture
1020 if (NULL != bitmap.getTexture()) {
1021 return false;
1022 }
1023
1024 // if it's larger than the max tile size, then we have no choice but tiling.
1025 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
1026 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
1027 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
1028 return true;
1029 }
1030
1031 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
1032 return false;
1033 }
1034
1035 // if the entire texture is already in our cache then no reason to tile it
1036 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
1037 return false;
1038 }
1039
1040 // At this point we know we could do the draw by uploading the entire bitmap
1041 // as a texture. However, if the texture would be large compared to the
1042 // cache size and we don't require most of it for this draw then tile to
1043 // reduce the amount of upload and cache spill.
1044
1045 // assumption here is that sw bitmap size is a good proxy for its size as
1046 // a texture
1047 size_t bmpSize = bitmap.getSize();
1048 size_t cacheSize;
1049 fContext->getTextureCacheLimits(NULL, &cacheSize);
1050 if (bmpSize < cacheSize / 2) {
1051 return false;
1052 }
1053
1054 // Figure out how much of the src we will need based on the src rect and clipping.
1055 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
1056 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
1057 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
1058 kBmpSmallTileSize * kBmpSmallTileSize;
1059
1060 return usedTileBytes < 2 * bmpSize;
1061}
1062
1063void SkGpuDevice::drawBitmap(const SkDraw& draw,
1064 const SkBitmap& bitmap,
1065 const SkMatrix& m,
1066 const SkPaint& paint) {
1067 // We cannot call drawBitmapRect here since 'm' could be anything
1068 this->drawBitmapCommon(draw, bitmap, NULL, m, paint,
1069 SkCanvas::kNone_DrawBitmapRectFlag);
1070}
1071
1072// This method outsets 'iRect' by 1 all around and then clamps its extents to
1073// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
1074// of 'iRect' for all possible outsets/clamps.
1075static inline void clamped_unit_outset_with_offset(SkIRect* iRect, SkPoint* offset,
1076 const SkIRect& clamp) {
1077 iRect->outset(1, 1);
1078
1079 if (iRect->fLeft < clamp.fLeft) {
1080 iRect->fLeft = clamp.fLeft;
1081 } else {
1082 offset->fX -= SK_Scalar1;
1083 }
1084 if (iRect->fTop < clamp.fTop) {
1085 iRect->fTop = clamp.fTop;
1086 } else {
1087 offset->fY -= SK_Scalar1;
1088 }
1089
1090 if (iRect->fRight > clamp.fRight) {
1091 iRect->fRight = clamp.fRight;
1092 }
1093 if (iRect->fBottom > clamp.fBottom) {
1094 iRect->fBottom = clamp.fBottom;
1095 }
1096}
1097
1098void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1099 const SkBitmap& bitmap,
1100 const SkRect* srcRectPtr,
1101 const SkMatrix& m,
1102 const SkPaint& paint,
1103 SkCanvas::DrawBitmapRectFlags flags) {
1104 CHECK_SHOULD_DRAW(draw, false);
1105
1106 SkRect srcRect;
1107 if (NULL == srcRectPtr) {
1108 srcRect.set(0, 0, SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()));
1109 } else {
1110 srcRect = *srcRectPtr;
1111 }
1112
1113 if (paint.getMaskFilter()){
1114 // Convert the bitmap to a shader so that the rect can be drawn
1115 // through drawRect, which supports mask filters.
1116 SkMatrix newM(m);
1117 SkBitmap tmp; // subset of bitmap, if necessary
1118 const SkBitmap* bitmapPtr = &bitmap;
1119 if (NULL != srcRectPtr) {
1120 SkIRect iSrc;
1121 srcRect.roundOut(&iSrc);
1122
1123 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
1124 SkIntToScalar(iSrc.fTop));
1125
1126 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1127 // In bleed mode we want to expand the src rect on all sides
1128 // but stay within the bitmap bounds
1129 SkIRect iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1130 clamped_unit_outset_with_offset(&iSrc, &offset, iClampRect);
1131 }
1132
1133 if (!bitmap.extractSubset(&tmp, iSrc)) {
1134 return; // extraction failed
1135 }
1136 bitmapPtr = &tmp;
1137 srcRect.offset(-offset.fX, -offset.fY);
1138 // The source rect has changed so update the matrix
1139 newM.preTranslate(offset.fX, offset.fY);
1140 }
1141
1142 SkPaint paintWithTexture(paint);
1143 paintWithTexture.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
1144 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
1145
1146 // Transform 'newM' needs to be concatenated to the current matrix,
1147 // rather than transforming the primitive directly, so that 'newM' will
1148 // also affect the behavior of the mask filter.
1149 SkMatrix drawMatrix;
1150 drawMatrix.setConcat(fContext->getMatrix(), newM);
1151 SkDraw transformedDraw(draw);
1152 transformedDraw.fMatrix = &drawMatrix;
1153
1154 this->drawRect(transformedDraw, srcRect, paintWithTexture);
1155
1156 return;
1157 }
1158
1159 fContext->concatMatrix(m);
1160
1161 GrTextureParams params;
1162 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel();
1163 GrTextureParams::FilterMode textureFilterMode;
1164 switch(paintFilterLevel) {
1165 case SkPaint::kNone_FilterLevel:
1166 textureFilterMode = GrTextureParams::kNone_FilterMode;
1167 break;
1168 case SkPaint::kLow_FilterLevel:
1169 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1170 break;
1171 case SkPaint::kMedium_FilterLevel:
1172 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1173 break;
1174 case SkPaint::kHigh_FilterLevel:
1175 // Fall back to mips for now
1176 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1177 break;
1178 default:
1179 SkErrorInternals::SetError( kInvalidPaint_SkError,
1180 "Sorry, I don't understand the filtering "
1181 "mode you asked for. Falling back to "
1182 "MIPMaps.");
1183 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1184 break;
1185
1186 }
1187
1188 params.setFilterMode(textureFilterMode);
1189
1190 int maxTileSize = fContext->getMaxTextureSize();
1191 if (SkPaint::kNone_FilterLevel != paint.getFilterLevel()) {
1192 // We may need a skosh more room if we have to bump out the tile
1193 // by 1 pixel all around
1194 maxTileSize -= 2;
1195 }
1196 int tileSize;
1197
1198 SkIRect clippedSrcRect;
1199 if (this->shouldTileBitmap(bitmap, params, srcRectPtr, maxTileSize, &tileSize,
1200 &clippedSrcRect)) {
1201 this->drawTiledBitmap(bitmap, srcRect, clippedSrcRect, params, paint, flags, tileSize);
1202 } else {
1203 // take the simple case
1204 this->internalDrawBitmap(bitmap, srcRect, params, paint, flags);
1205 }
1206}
1207
1208// Break 'bitmap' into several tiles to draw it since it has already
1209// been determined to be too large to fit in VRAM
1210void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
1211 const SkRect& srcRect,
1212 const SkIRect& clippedSrcIRect,
1213 const GrTextureParams& params,
1214 const SkPaint& paint,
1215 SkCanvas::DrawBitmapRectFlags flags,
1216 int tileSize) {
1217 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1218
1219 int nx = bitmap.width() / tileSize;
1220 int ny = bitmap.height() / tileSize;
1221 for (int x = 0; x <= nx; x++) {
1222 for (int y = 0; y <= ny; y++) {
1223 SkRect tileR;
1224 tileR.set(SkIntToScalar(x * tileSize),
1225 SkIntToScalar(y * tileSize),
1226 SkIntToScalar((x + 1) * tileSize),
1227 SkIntToScalar((y + 1) * tileSize));
1228
1229 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1230 continue;
1231 }
1232
1233 if (!tileR.intersect(srcRect)) {
1234 continue;
1235 }
1236
1237 SkBitmap tmpB;
1238 SkIRect iTileR;
1239 tileR.roundOut(&iTileR);
1240 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1241 SkIntToScalar(iTileR.fTop));
1242
1243 if (SkPaint::kNone_FilterLevel != paint.getFilterLevel()) {
1244 SkIRect iClampRect;
1245
1246 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1247 // In bleed mode we want to always expand the tile on all edges
1248 // but stay within the bitmap bounds
1249 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1250 } else {
1251 // In texture-domain/clamp mode we only want to expand the
1252 // tile on edges interior to "srcRect" (i.e., we want to
1253 // not bleed across the original clamped edges)
1254 srcRect.roundOut(&iClampRect);
1255 }
1256
1257 clamped_unit_outset_with_offset(&iTileR, &offset, iClampRect);
1258 }
1259
1260 if (bitmap.extractSubset(&tmpB, iTileR)) {
1261 // now offset it to make it "local" to our tmp bitmap
1262 tileR.offset(-offset.fX, -offset.fY);
1263 SkMatrix tmpM;
1264 tmpM.setTranslate(offset.fX, offset.fY);
1265 GrContext::AutoMatrix am;
1266 am.setPreConcat(fContext, tmpM);
1267 this->internalDrawBitmap(tmpB, tileR, params, paint, flags);
1268 }
1269 }
1270 }
1271}
1272
1273static bool has_aligned_samples(const SkRect& srcRect,
1274 const SkRect& transformedRect) {
1275 // detect pixel disalignment
1276 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1277 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1278 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1279 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1280 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1281 COLOR_BLEED_TOLERANCE &&
1282 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1283 COLOR_BLEED_TOLERANCE) {
1284 return true;
1285 }
1286 return false;
1287}
1288
1289static bool may_color_bleed(const SkRect& srcRect,
1290 const SkRect& transformedRect,
1291 const SkMatrix& m) {
1292 // Only gets called if has_aligned_samples returned false.
1293 // So we can assume that sampling is axis aligned but not texel aligned.
1294 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
1295 SkRect innerSrcRect(srcRect), innerTransformedRect,
1296 outerTransformedRect(transformedRect);
1297 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1298 m.mapRect(&innerTransformedRect, innerSrcRect);
1299
1300 // The gap between outerTransformedRect and innerTransformedRect
1301 // represents the projection of the source border area, which is
1302 // problematic for color bleeding. We must check whether any
1303 // destination pixels sample the border area.
1304 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1305 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1306 SkIRect outer, inner;
1307 outerTransformedRect.round(&outer);
1308 innerTransformedRect.round(&inner);
1309 // If the inner and outer rects round to the same result, it means the
1310 // border does not overlap any pixel centers. Yay!
1311 return inner != outer;
1312}
1313
1314
1315/*
1316 * This is called by drawBitmap(), which has to handle images that may be too
1317 * large to be represented by a single texture.
1318 *
1319 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1320 * and that non-texture portion of the GrPaint has already been setup.
1321 */
1322void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
1323 const SkRect& srcRect,
1324 const GrTextureParams& params,
1325 const SkPaint& paint,
1326 SkCanvas::DrawBitmapRectFlags flags) {
1327 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1328 bitmap.height() <= fContext->getMaxTextureSize());
1329
1330 GrTexture* texture;
1331 SkAutoCachedTexture act(this, bitmap, &params, &texture);
1332 if (NULL == texture) {
1333 return;
1334 }
1335
1336 SkRect dstRect(srcRect);
1337 SkRect paintRect;
1338 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1339 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1340 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1341 SkScalarMul(srcRect.fTop, hInv),
1342 SkScalarMul(srcRect.fRight, wInv),
1343 SkScalarMul(srcRect.fBottom, hInv));
1344
1345 bool needsTextureDomain = false;
1346 if (!(flags & SkCanvas::kBleed_DrawBitmapRectFlag) &&
1347 params.filterMode() != GrTextureParams::kNone_FilterMode) {
1348 // Need texture domain if drawing a sub rect.
1349 needsTextureDomain = srcRect.width() < bitmap.width() ||
1350 srcRect.height() < bitmap.height();
1351 if (needsTextureDomain && fContext->getMatrix().rectStaysRect()) {
1352 const SkMatrix& matrix = fContext->getMatrix();
1353 // sampling is axis-aligned
1354 SkRect transformedRect;
1355 matrix.mapRect(&transformedRect, srcRect);
1356
1357 if (has_aligned_samples(srcRect, transformedRect)) {
1358 // We could also turn off filtering here (but we already did a cache lookup with
1359 // params).
1360 needsTextureDomain = false;
1361 } else {
1362 needsTextureDomain = may_color_bleed(srcRect, transformedRect, matrix);
1363 }
1364 }
1365 }
1366
1367 SkRect textureDomain = SkRect::MakeEmpty();
1368 SkAutoTUnref<GrEffectRef> effect;
1369 if (needsTextureDomain) {
1370 // Use a constrained texture domain to avoid color bleeding
1371 SkScalar left, top, right, bottom;
1372 if (srcRect.width() > SK_Scalar1) {
1373 SkScalar border = SK_ScalarHalf / texture->width();
1374 left = paintRect.left() + border;
1375 right = paintRect.right() - border;
1376 } else {
1377 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1378 }
1379 if (srcRect.height() > SK_Scalar1) {
1380 SkScalar border = SK_ScalarHalf / texture->height();
1381 top = paintRect.top() + border;
1382 bottom = paintRect.bottom() - border;
1383 } else {
1384 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1385 }
1386 textureDomain.setLTRB(left, top, right, bottom);
1387 effect.reset(GrTextureDomainEffect::Create(texture,
1388 SkMatrix::I(),
1389 textureDomain,
1390 GrTextureDomainEffect::kClamp_WrapMode,
1391 params.filterMode()));
1392 } else {
1393 effect.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
1394 }
1395
1396 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1397 // the rest from the SkPaint.
1398 GrPaint grPaint;
1399 grPaint.addColorEffect(effect);
1400 bool alphaOnly = !(SkBitmap::kA8_Config == bitmap.config());
1401 if (!skPaint2GrPaintNoShader(this, paint, alphaOnly, false, &grPaint)) {
1402 return;
1403 }
1404
1405 fContext->drawRectToRect(grPaint, dstRect, paintRect, NULL);
1406}
1407
1408static bool filter_texture(SkBaseDevice* device, GrContext* context,
1409 GrTexture* texture, SkImageFilter* filter,
1410 int w, int h, const SkMatrix& ctm, SkBitmap* result,
1411 SkIPoint* offset) {
1412 SkASSERT(filter);
1413 SkDeviceImageFilterProxy proxy(device);
1414
1415 if (filter->canFilterImageGPU()) {
1416 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1417 // filter. Also set the clip wide open and the matrix to identity.
1418 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
1419 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctm, result, offset);
1420 } else {
1421 return false;
1422 }
1423}
1424
1425void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1426 int left, int top, const SkPaint& paint) {
1427 // drawSprite is defined to be in device coords.
1428 CHECK_SHOULD_DRAW(draw, true);
1429
1430 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1431 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1432 return;
1433 }
1434
1435 int w = bitmap.width();
1436 int h = bitmap.height();
1437
1438 GrTexture* texture;
1439 // draw sprite uses the default texture params
1440 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
1441
1442 SkImageFilter* filter = paint.getImageFilter();
1443 SkIPoint offset = SkIPoint::Make(left, top);
1444 // This bitmap will own the filtered result as a texture.
1445 SkBitmap filteredBitmap;
1446
1447 if (NULL != filter) {
1448 SkMatrix matrix(*draw.fMatrix);
1449 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
1450 if (filter_texture(this, fContext, texture, filter, w, h, matrix, &filteredBitmap,
1451 &offset)) {
1452 texture = (GrTexture*) filteredBitmap.getTexture();
1453 w = filteredBitmap.width();
1454 h = filteredBitmap.height();
1455 } else {
1456 return;
1457 }
1458 }
1459
1460 GrPaint grPaint;
1461 grPaint.addColorTextureEffect(texture, SkMatrix::I());
1462
1463 if(!skPaint2GrPaintNoShader(this, paint, true, false, &grPaint)) {
1464 return;
1465 }
1466
1467 fContext->drawRectToRect(grPaint,
1468 SkRect::MakeXYWH(SkIntToScalar(offset.fX),
1469 SkIntToScalar(offset.fY),
1470 SkIntToScalar(w),
1471 SkIntToScalar(h)),
1472 SkRect::MakeXYWH(0,
1473 0,
1474 SK_Scalar1 * w / texture->width(),
1475 SK_Scalar1 * h / texture->height()));
1476}
1477
1478void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
1479 const SkRect* src, const SkRect& dst,
1480 const SkPaint& paint,
1481 SkCanvas::DrawBitmapRectFlags flags) {
1482 SkMatrix matrix;
1483 SkRect bitmapBounds, tmpSrc;
1484
1485 bitmapBounds.set(0, 0,
1486 SkIntToScalar(bitmap.width()),
1487 SkIntToScalar(bitmap.height()));
1488
1489 // Compute matrix from the two rectangles
1490 if (NULL != src) {
1491 tmpSrc = *src;
1492 } else {
1493 tmpSrc = bitmapBounds;
1494 }
1495 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1496
1497 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
1498 if (NULL != src) {
1499 if (!bitmapBounds.contains(tmpSrc)) {
1500 if (!tmpSrc.intersect(bitmapBounds)) {
1501 return; // nothing to draw
1502 }
1503 }
1504 }
1505
1506 this->drawBitmapCommon(draw, bitmap, &tmpSrc, matrix, paint, flags);
1507}
1508
1509void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1510 int x, int y, const SkPaint& paint) {
1511 // clear of the source device must occur before CHECK_SHOULD_DRAW
1512 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1513 if (dev->fNeedClear) {
1514 // TODO: could check here whether we really need to draw at all
1515 dev->clear(0x0);
1516 }
1517
1518 // drawDevice is defined to be in device coords.
1519 CHECK_SHOULD_DRAW(draw, true);
1520
1521 GrRenderTarget* devRT = dev->accessRenderTarget();
1522 GrTexture* devTex;
1523 if (NULL == (devTex = devRT->asTexture())) {
1524 return;
1525 }
1526
1527 const SkBitmap& bm = dev->accessBitmap(false);
1528 int w = bm.width();
1529 int h = bm.height();
1530
1531 SkImageFilter* filter = paint.getImageFilter();
1532 // This bitmap will own the filtered result as a texture.
1533 SkBitmap filteredBitmap;
1534
1535 if (NULL != filter) {
1536 SkIPoint offset = SkIPoint::Make(0, 0);
1537 SkMatrix matrix(*draw.fMatrix);
1538 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
1539 if (filter_texture(this, fContext, devTex, filter, w, h, matrix, &filteredBitmap,
1540 &offset)) {
1541 devTex = filteredBitmap.getTexture();
1542 w = filteredBitmap.width();
1543 h = filteredBitmap.height();
1544 x += offset.fX;
1545 y += offset.fY;
1546 } else {
1547 return;
1548 }
1549 }
1550
1551 GrPaint grPaint;
1552 grPaint.addColorTextureEffect(devTex, SkMatrix::I());
1553
1554 if (!skPaint2GrPaintNoShader(this, paint, true, false, &grPaint)) {
1555 return;
1556 }
1557
1558 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1559 SkIntToScalar(y),
1560 SkIntToScalar(w),
1561 SkIntToScalar(h));
1562
1563 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1564 // scratch texture).
1565 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1566 SK_Scalar1 * h / devTex->height());
1567
1568 fContext->drawRectToRect(grPaint, dstRect, srcRect);
1569}
1570
1571bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
1572 return filter->canFilterImageGPU();
1573}
1574
1575bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1576 const SkMatrix& ctm,
1577 SkBitmap* result, SkIPoint* offset) {
1578 // want explicitly our impl, so guard against a subclass of us overriding it
1579 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1580 return false;
1581 }
1582
1583 SkAutoLockPixels alp(src, !src.getTexture());
1584 if (!src.getTexture() && !src.readyToDraw()) {
1585 return false;
1586 }
1587
1588 GrTexture* texture;
1589 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1590 // must be pushed upstack.
1591 SkAutoCachedTexture act(this, src, NULL, &texture);
1592
1593 return filter_texture(this, fContext, texture, filter, src.width(), src.height(), ctm, result,
1594 offset);
1595}
1596
1597///////////////////////////////////////////////////////////////////////////////
1598
1599// must be in SkCanvas::VertexMode order
1600static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1601 kTriangles_GrPrimitiveType,
1602 kTriangleStrip_GrPrimitiveType,
1603 kTriangleFan_GrPrimitiveType,
1604};
1605
1606void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1607 int vertexCount, const SkPoint vertices[],
1608 const SkPoint texs[], const SkColor colors[],
1609 SkXfermode* xmode,
1610 const uint16_t indices[], int indexCount,
1611 const SkPaint& paint) {
1612 CHECK_SHOULD_DRAW(draw, false);
1613
1614 GrPaint grPaint;
1615 // we ignore the shader if texs is null.
1616 if (NULL == texs) {
1617 if (!skPaint2GrPaintNoShader(this, paint, false, NULL == colors, &grPaint)) {
1618 return;
1619 }
1620 } else {
1621 if (!skPaint2GrPaintShader(this, paint, NULL == colors, &grPaint)) {
1622 return;
1623 }
1624 }
1625
1626 if (NULL != xmode && NULL != texs && NULL != colors) {
1627 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1628 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1629#if 0
1630 return
1631#endif
1632 }
1633 }
1634
1635 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1636 if (NULL != colors) {
1637 // need to convert byte order and from non-PM to PM
1638 convertedColors.reset(vertexCount);
1639 for (int i = 0; i < vertexCount; ++i) {
1640 convertedColors[i] = SkColor2GrColor(colors[i]);
1641 }
1642 colors = convertedColors.get();
1643 }
1644 fContext->drawVertices(grPaint,
1645 gVertexMode2PrimitiveType[vmode],
1646 vertexCount,
1647 (GrPoint*) vertices,
1648 (GrPoint*) texs,
1649 colors,
1650 indices,
1651 indexCount);
1652}
1653
1654///////////////////////////////////////////////////////////////////////////////
1655
1656static void GlyphCacheAuxProc(void* data) {
1657 GrFontScaler* scaler = (GrFontScaler*)data;
1658 SkSafeUnref(scaler);
1659}
1660
1661static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1662 void* auxData;
1663 GrFontScaler* scaler = NULL;
1664 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1665 scaler = (GrFontScaler*)auxData;
1666 }
1667 if (NULL == scaler) {
1668 scaler = SkNEW_ARGS(SkGrFontScaler, (cache));
1669 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1670 }
1671 return scaler;
1672}
1673
1674static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1675 SkFixed fx, SkFixed fy,
1676 const SkGlyph& glyph) {
1677 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1678
1679 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
1680
1681 if (NULL == procs->fFontScaler) {
1682 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1683 }
1684
1685 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1686 glyph.getSubXFixed(),
1687 glyph.getSubYFixed()),
1688 SkFixedFloorToFixed(fx),
1689 SkFixedFloorToFixed(fy),
1690 procs->fFontScaler);
1691}
1692
1693SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
1694
1695 // deferred allocation
1696 if (NULL == fDrawProcs) {
1697 fDrawProcs = SkNEW(GrSkDrawProcs);
1698 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1699 fDrawProcs->fContext = fContext;
1700#if SK_DISTANCEFIELD_FONTS
1701 fDrawProcs->fFlags = 0;
1702 fDrawProcs->fFlags |= SkDrawProcs::kSkipBakedGlyphTransform_Flag;
1703 fDrawProcs->fFlags |= SkDrawProcs::kUseScaledGlyphs_Flag;
1704#endif
1705 }
1706
1707 // init our (and GL's) state
1708 fDrawProcs->fTextContext = context;
1709 fDrawProcs->fFontScaler = NULL;
1710 return fDrawProcs;
1711}
1712
1713void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1714 size_t byteLength, SkScalar x, SkScalar y,
1715 const SkPaint& paint) {
1716 CHECK_SHOULD_DRAW(draw, false);
1717
1718 if (fContext->getMatrix().hasPerspective()) {
1719 // this guy will just call our drawPath()
1720 draw.drawText((const char*)text, byteLength, x, y, paint);
1721 } else {
1722 SkDraw myDraw(draw);
1723
1724 GrPaint grPaint;
1725 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
1726 return;
1727 }
1728#if SK_DISTANCEFIELD_FONTS
1729 GrDistanceFieldTextContext context(fContext, grPaint, paint.getColor(),
1730 paint.getTextSize());
1731#else
1732 GrBitmapTextContext context(fContext, grPaint, paint.getColor());
1733#endif
1734 myDraw.fProcs = this->initDrawForText(&context);
1735 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1736 }
1737}
1738
1739void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1740 size_t byteLength, const SkScalar pos[],
1741 SkScalar constY, int scalarsPerPos,
1742 const SkPaint& paint) {
1743 CHECK_SHOULD_DRAW(draw, false);
1744
1745 if (fContext->getMatrix().hasPerspective()) {
1746 // this guy will just call our drawPath()
1747 draw.drawPosText((const char*)text, byteLength, pos, constY,
1748 scalarsPerPos, paint);
1749 } else {
1750 SkDraw myDraw(draw);
1751
1752 GrPaint grPaint;
1753 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
1754 return;
1755 }
1756#if SK_DISTANCEFIELD_FONTS
1757 GrDistanceFieldTextContext context(fContext, grPaint, paint.getColor(),
1758 paint.getTextSize()/SkDrawProcs::kBaseDFFontSize);
1759#else
1760 GrBitmapTextContext context(fContext, grPaint, paint.getColor());
1761#endif
1762 myDraw.fProcs = this->initDrawForText(&context);
1763 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1764 scalarsPerPos, paint);
1765 }
1766}
1767
1768void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1769 size_t len, const SkPath& path,
1770 const SkMatrix* m, const SkPaint& paint) {
1771 CHECK_SHOULD_DRAW(draw, false);
1772
1773 SkASSERT(draw.fDevice == this);
1774 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1775}
1776
1777///////////////////////////////////////////////////////////////////////////////
1778
1779bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1780 if (!paint.isLCDRenderText()) {
1781 // we're cool with the paint as is
1782 return false;
1783 }
1784
1785 if (paint.getShader() ||
1786 paint.getXfermode() || // unless its srcover
1787 paint.getMaskFilter() ||
1788 paint.getRasterizer() ||
1789 paint.getColorFilter() ||
1790 paint.getPathEffect() ||
1791 paint.isFakeBoldText() ||
1792 paint.getStyle() != SkPaint::kFill_Style) {
1793 // turn off lcd
1794 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1795 flags->fHinting = paint.getHinting();
1796 return true;
1797 }
1798 // we're cool with the paint as is
1799 return false;
1800}
1801
1802void SkGpuDevice::flush() {
1803 DO_DEFERRED_CLEAR();
1804 fContext->resolveRenderTarget(fRenderTarget);
1805}
1806
1807///////////////////////////////////////////////////////////////////////////////
1808
1809SkBaseDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1810 int width, int height,
1811 bool isOpaque,
1812 Usage usage) {
1813 GrTextureDesc desc;
1814 desc.fConfig = fRenderTarget->config();
1815 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1816 desc.fWidth = width;
1817 desc.fHeight = height;
1818 desc.fSampleCnt = fRenderTarget->numSamples();
1819
1820 SkAutoTUnref<GrTexture> texture;
1821 // Skia's convention is to only clear a device if it is non-opaque.
1822 bool needClear = !isOpaque;
1823
1824#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1825 // layers are never draw in repeat modes, so we can request an approx
1826 // match and ignore any padding.
1827 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == usage) ?
1828 GrContext::kApprox_ScratchTexMatch :
1829 GrContext::kExact_ScratchTexMatch;
1830 texture.reset(fContext->lockAndRefScratchTexture(desc, match));
1831#else
1832 texture.reset(fContext->createUncachedTexture(desc, NULL, 0));
1833#endif
1834 if (NULL != texture.get()) {
1835 return SkNEW_ARGS(SkGpuDevice,(fContext, texture, needClear));
1836 } else {
1837 GrPrintf("---- failed to create compatible device texture [%d %d]\n", width, height);
1838 return NULL;
1839 }
1840}
1841
1842SkGpuDevice::SkGpuDevice(GrContext* context,
1843 GrTexture* texture,
1844 bool needClear)
1845 : SkBitmapDevice(make_bitmap(context, texture->asRenderTarget())) {
1846
1847 SkASSERT(texture && texture->asRenderTarget());
1848 // This constructor is called from onCreateCompatibleDevice. It has locked the RT in the texture
1849 // cache. We pass true for the third argument so that it will get unlocked.
1850 this->initFromRenderTarget(context, texture->asRenderTarget(), true);
1851 fNeedClear = needClear;
1852}