blob: ac4c02ac0a9c5adf1e94ed63aa6ed53fb8caa6e9 [file] [log] [blame]
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkGpuDevice.h"
9
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +000010#include "effects/GrBicubicEffect.h"
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000011#include "effects/GrTextureDomain.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000012#include "effects/GrSimpleTextureEffect.h"
13
14#include "GrContext.h"
15#include "GrBitmapTextContext.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000016#include "GrDistanceFieldTextContext.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000017
18#include "SkGrTexturePixelRef.h"
19
commit-bot@chromium.org82139702014-03-10 22:53:20 +000020#include "SkBounder.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000021#include "SkColorFilter.h"
22#include "SkDeviceImageFilterProxy.h"
23#include "SkDrawProcs.h"
24#include "SkGlyphCache.h"
25#include "SkImageFilter.h"
commit-bot@chromium.org82139702014-03-10 22:53:20 +000026#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000027#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000028#include "SkPicture.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000029#include "SkRRect.h"
30#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000031#include "SkSurface.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000032#include "SkTLazy.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000033#include "SkUtils.h"
34#include "SkErrorInternals.h"
35
36#define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
37
38#if 0
39 extern bool (*gShouldDrawProc)();
40 #define CHECK_SHOULD_DRAW(draw, forceI) \
41 do { \
42 if (gShouldDrawProc && !gShouldDrawProc()) return; \
43 this->prepareDraw(draw, forceI); \
44 } while (0)
45#else
46 #define CHECK_SHOULD_DRAW(draw, forceI) this->prepareDraw(draw, forceI)
47#endif
48
49// This constant represents the screen alignment criterion in texels for
50// requiring texture domain clamping to prevent color bleeding when drawing
51// a sub region of a larger source image.
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000052#define COLOR_BLEED_TOLERANCE 0.001f
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000053
54#define DO_DEFERRED_CLEAR() \
55 do { \
56 if (fNeedClear) { \
57 this->clear(SK_ColorTRANSPARENT); \
58 } \
59 } while (false) \
60
61///////////////////////////////////////////////////////////////////////////////
62
63#define CHECK_FOR_ANNOTATION(paint) \
64 do { if (paint.getAnnotation()) { return; } } while (0)
65
66///////////////////////////////////////////////////////////////////////////////
67
68
69class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
70public:
71 SkAutoCachedTexture()
72 : fDevice(NULL)
73 , fTexture(NULL) {
74 }
75
76 SkAutoCachedTexture(SkGpuDevice* device,
77 const SkBitmap& bitmap,
78 const GrTextureParams* params,
79 GrTexture** texture)
80 : fDevice(NULL)
81 , fTexture(NULL) {
82 SkASSERT(NULL != texture);
83 *texture = this->set(device, bitmap, params);
84 }
85
86 ~SkAutoCachedTexture() {
87 if (NULL != fTexture) {
88 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
89 }
90 }
91
92 GrTexture* set(SkGpuDevice* device,
93 const SkBitmap& bitmap,
94 const GrTextureParams* params) {
95 if (NULL != fTexture) {
96 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
97 fTexture = NULL;
98 }
99 fDevice = device;
100 GrTexture* result = (GrTexture*)bitmap.getTexture();
101 if (NULL == result) {
102 // Cannot return the native texture so look it up in our cache
103 fTexture = GrLockAndRefCachedBitmapTexture(device->context(), bitmap, params);
104 result = fTexture;
105 }
106 return result;
107 }
108
109private:
110 SkGpuDevice* fDevice;
111 GrTexture* fTexture;
112};
113
114///////////////////////////////////////////////////////////////////////////////
115
116struct GrSkDrawProcs : public SkDrawProcs {
117public:
118 GrContext* fContext;
119 GrTextContext* fTextContext;
120 GrFontScaler* fFontScaler; // cached in the skia glyphcache
121};
122
123///////////////////////////////////////////////////////////////////////////////
124
125static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
126 switch (config) {
127 case kAlpha_8_GrPixelConfig:
128 *isOpaque = false;
129 return SkBitmap::kA8_Config;
130 case kRGB_565_GrPixelConfig:
131 *isOpaque = true;
132 return SkBitmap::kRGB_565_Config;
133 case kRGBA_4444_GrPixelConfig:
134 *isOpaque = false;
135 return SkBitmap::kARGB_4444_Config;
136 case kSkia8888_GrPixelConfig:
137 // we don't currently have a way of knowing whether
138 // a 8888 is opaque based on the config.
139 *isOpaque = false;
140 return SkBitmap::kARGB_8888_Config;
141 default:
142 *isOpaque = false;
143 return SkBitmap::kNo_Config;
144 }
145}
146
147/*
148 * GrRenderTarget does not know its opaqueness, only its config, so we have
149 * to make conservative guesses when we return an "equivalent" bitmap.
150 */
151static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
152 bool isOpaque;
153 SkBitmap::Config config = grConfig2skConfig(renderTarget->config(), &isOpaque);
154
155 SkBitmap bitmap;
156 bitmap.setConfig(config, renderTarget->width(), renderTarget->height(), 0,
157 isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
158 return bitmap;
159}
160
161SkGpuDevice* SkGpuDevice::Create(GrSurface* surface) {
162 SkASSERT(NULL != surface);
163 if (NULL == surface->asRenderTarget() || NULL == surface->getContext()) {
164 return NULL;
165 }
166 if (surface->asTexture()) {
167 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asTexture()));
168 } else {
169 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asRenderTarget()));
170 }
171}
172
173SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
174 : SkBitmapDevice(make_bitmap(context, texture->asRenderTarget())) {
175 this->initFromRenderTarget(context, texture->asRenderTarget(), false);
176}
177
178SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
179 : SkBitmapDevice(make_bitmap(context, renderTarget)) {
180 this->initFromRenderTarget(context, renderTarget, false);
181}
182
183void SkGpuDevice::initFromRenderTarget(GrContext* context,
184 GrRenderTarget* renderTarget,
185 bool cached) {
186 fDrawProcs = NULL;
187
188 fContext = context;
189 fContext->ref();
190
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000191 fMainTextContext = SkNEW_ARGS(GrDistanceFieldTextContext, (fContext, fLeakyProperties));
192 fFallbackTextContext = SkNEW_ARGS(GrBitmapTextContext, (fContext, fLeakyProperties));
commit-bot@chromium.orgcc40f062014-01-24 14:38:27 +0000193
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000194 fRenderTarget = NULL;
195 fNeedClear = false;
196
197 SkASSERT(NULL != renderTarget);
198 fRenderTarget = renderTarget;
199 fRenderTarget->ref();
200
201 // Hold onto to the texture in the pixel ref (if there is one) because the texture holds a ref
202 // on the RT but not vice-versa.
203 // TODO: Remove this trickery once we figure out how to make SkGrPixelRef do this without
204 // busting chrome (for a currently unknown reason).
205 GrSurface* surface = fRenderTarget->asTexture();
206 if (NULL == surface) {
207 surface = fRenderTarget;
208 }
reed@google.combf790232013-12-13 19:45:58 +0000209
210 SkImageInfo info;
211 surface->asImageInfo(&info);
212 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (info, surface, cached));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000213
reed@google.com672588b2014-01-08 15:42:01 +0000214 this->setPixelRef(pr)->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000215}
216
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000217SkGpuDevice* SkGpuDevice::Create(GrContext* context, const SkImageInfo& origInfo,
218 int sampleCount) {
219 if (kUnknown_SkColorType == origInfo.colorType() ||
220 origInfo.width() < 0 || origInfo.height() < 0) {
221 return NULL;
222 }
223
224 SkImageInfo info = origInfo;
225 // TODO: perhas we can loosen this check now that colortype is more detailed
226 // e.g. can we support both RGBA and BGRA here?
227 if (kRGB_565_SkColorType == info.colorType()) {
228 info.fAlphaType = kOpaque_SkAlphaType; // force this setting
229 } else {
230 info.fColorType = kPMColor_SkColorType;
231 if (kOpaque_SkAlphaType != info.alphaType()) {
232 info.fAlphaType = kPremul_SkAlphaType; // force this setting
233 }
234 }
235
236 GrTextureDesc desc;
237 desc.fFlags = kRenderTarget_GrTextureFlagBit;
238 desc.fWidth = info.width();
239 desc.fHeight = info.height();
240 desc.fConfig = SkImageInfo2GrPixelConfig(info.colorType(), info.alphaType());
241 desc.fSampleCnt = sampleCount;
242
243 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0));
244 if (!texture.get()) {
245 return NULL;
246 }
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000247
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000248 return SkNEW_ARGS(SkGpuDevice, (context, texture.get()));
249}
250
251#ifdef SK_SUPPORT_LEGACY_COMPATIBLEDEVICE_CONFIG
252static SkBitmap make_bitmap(SkBitmap::Config config, int width, int height) {
253 SkBitmap bm;
254 bm.setConfig(SkImageInfo::Make(width, height,
255 SkBitmapConfigToColorType(config),
256 kPremul_SkAlphaType));
257 return bm;
258}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000259SkGpuDevice::SkGpuDevice(GrContext* context,
260 SkBitmap::Config config,
261 int width,
262 int height,
263 int sampleCount)
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000264 : SkBitmapDevice(make_bitmap(config, width, height))
reed@google.combf790232013-12-13 19:45:58 +0000265{
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000266 fDrawProcs = NULL;
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000267
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000268 fContext = context;
269 fContext->ref();
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000270
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000271 fMainTextContext = SkNEW_ARGS(GrDistanceFieldTextContext, (fContext, fLeakyProperties));
272 fFallbackTextContext = SkNEW_ARGS(GrBitmapTextContext, (fContext, fLeakyProperties));
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000273
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000274 fRenderTarget = NULL;
275 fNeedClear = false;
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000276
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000277 if (config != SkBitmap::kRGB_565_Config) {
278 config = SkBitmap::kARGB_8888_Config;
279 }
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000280
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000281 GrTextureDesc desc;
282 desc.fFlags = kRenderTarget_GrTextureFlagBit;
283 desc.fWidth = width;
284 desc.fHeight = height;
285 desc.fConfig = SkBitmapConfig2GrPixelConfig(config);
286 desc.fSampleCnt = sampleCount;
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000287
reed@google.combf790232013-12-13 19:45:58 +0000288 SkImageInfo info;
289 if (!GrPixelConfig2ColorType(desc.fConfig, &info.fColorType)) {
290 sk_throw();
291 }
292 info.fWidth = width;
293 info.fHeight = height;
294 info.fAlphaType = kPremul_SkAlphaType;
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000295
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000296 SkAutoTUnref<GrTexture> texture(fContext->createUncachedTexture(desc, NULL, 0));
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000297
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000298 if (NULL != texture) {
299 fRenderTarget = texture->asRenderTarget();
300 fRenderTarget->ref();
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000301
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000302 SkASSERT(NULL != fRenderTarget);
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000303
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000304 // wrap the bitmap with a pixelref to expose our texture
reed@google.combf790232013-12-13 19:45:58 +0000305 SkGrPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (info, texture));
reed@google.com672588b2014-01-08 15:42:01 +0000306 this->setPixelRef(pr)->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000307 } else {
308 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
309 width, height);
310 SkASSERT(false);
311 }
312}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000313#endif
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000314
315SkGpuDevice::~SkGpuDevice() {
316 if (fDrawProcs) {
317 delete fDrawProcs;
318 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000319
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000320 delete fMainTextContext;
321 delete fFallbackTextContext;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000322
323 // The GrContext takes a ref on the target. We don't want to cause the render
324 // target to be unnecessarily kept alive.
325 if (fContext->getRenderTarget() == fRenderTarget) {
326 fContext->setRenderTarget(NULL);
327 }
328
329 if (fContext->getClip() == &fClipData) {
330 fContext->setClip(NULL);
331 }
332
333 SkSafeUnref(fRenderTarget);
334 fContext->unref();
335}
336
337///////////////////////////////////////////////////////////////////////////////
338
339void SkGpuDevice::makeRenderTargetCurrent() {
340 DO_DEFERRED_CLEAR();
341 fContext->setRenderTarget(fRenderTarget);
342}
343
344///////////////////////////////////////////////////////////////////////////////
345
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000346#ifdef SK_SUPPORT_LEGACY_READPIXELSCONFIG
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000347namespace {
348GrPixelConfig config8888_to_grconfig_and_flags(SkCanvas::Config8888 config8888, uint32_t* flags) {
349 switch (config8888) {
350 case SkCanvas::kNative_Premul_Config8888:
351 *flags = 0;
352 return kSkia8888_GrPixelConfig;
353 case SkCanvas::kNative_Unpremul_Config8888:
354 *flags = GrContext::kUnpremul_PixelOpsFlag;
355 return kSkia8888_GrPixelConfig;
356 case SkCanvas::kBGRA_Premul_Config8888:
357 *flags = 0;
358 return kBGRA_8888_GrPixelConfig;
359 case SkCanvas::kBGRA_Unpremul_Config8888:
360 *flags = GrContext::kUnpremul_PixelOpsFlag;
361 return kBGRA_8888_GrPixelConfig;
362 case SkCanvas::kRGBA_Premul_Config8888:
363 *flags = 0;
364 return kRGBA_8888_GrPixelConfig;
365 case SkCanvas::kRGBA_Unpremul_Config8888:
366 *flags = GrContext::kUnpremul_PixelOpsFlag;
367 return kRGBA_8888_GrPixelConfig;
368 default:
369 GrCrash("Unexpected Config8888.");
370 *flags = 0; // suppress warning
371 return kSkia8888_GrPixelConfig;
372 }
373}
374}
375
376bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
377 int x, int y,
378 SkCanvas::Config8888 config8888) {
379 DO_DEFERRED_CLEAR();
380 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
381 SkASSERT(!bitmap.isNull());
382 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000383
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000384 SkAutoLockPixels alp(bitmap);
385 GrPixelConfig config;
386 uint32_t flags;
387 config = config8888_to_grconfig_and_flags(config8888, &flags);
388 return fContext->readRenderTargetPixels(fRenderTarget,
389 x, y,
390 bitmap.width(),
391 bitmap.height(),
392 config,
393 bitmap.getPixels(),
394 bitmap.rowBytes(),
395 flags);
396}
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000397#endif
398
399bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
400 int x, int y) {
401 DO_DEFERRED_CLEAR();
402
403 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
404 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo.colorType(), dstInfo.alphaType());
405 if (kUnknown_GrPixelConfig == config) {
406 return false;
407 }
408
409 uint32_t flags = 0;
410 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
411 flags = GrContext::kUnpremul_PixelOpsFlag;
412 }
413 return fContext->readRenderTargetPixels(fRenderTarget, x, y, dstInfo.width(), dstInfo.height(),
414 config, dstPixels, dstRowBytes, flags);
415}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000416
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000417bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
418 int x, int y) {
419 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
420 GrPixelConfig config = SkImageInfo2GrPixelConfig(info.colorType(), info.alphaType());
421 if (kUnknown_GrPixelConfig == config) {
422 return false;
423 }
424 uint32_t flags = 0;
425 if (kUnpremul_SkAlphaType == info.alphaType()) {
426 flags = GrContext::kUnpremul_PixelOpsFlag;
427 }
428 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
429
430 // need to bump our genID for compatibility with clients that "know" we have a bitmap
431 this->onAccessBitmap().notifyPixelsChanged();
432
433 return true;
434}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000435
436void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
437 INHERITED::onAttachToCanvas(canvas);
438
439 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
440 fClipData.fClipStack = canvas->getClipStack();
441}
442
443void SkGpuDevice::onDetachFromCanvas() {
444 INHERITED::onDetachFromCanvas();
445 fClipData.fClipStack = NULL;
446}
447
448// call this every draw call, to ensure that the context reflects our state,
449// and not the state from some other canvas/device
450void SkGpuDevice::prepareDraw(const SkDraw& draw, bool forceIdentity) {
451 SkASSERT(NULL != fClipData.fClipStack);
452
453 fContext->setRenderTarget(fRenderTarget);
454
455 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
456
457 if (forceIdentity) {
458 fContext->setIdentityMatrix();
459 } else {
460 fContext->setMatrix(*draw.fMatrix);
461 }
462 fClipData.fOrigin = this->getOrigin();
463
464 fContext->setClip(&fClipData);
465
466 DO_DEFERRED_CLEAR();
467}
468
469GrRenderTarget* SkGpuDevice::accessRenderTarget() {
470 DO_DEFERRED_CLEAR();
471 return fRenderTarget;
472}
473
474///////////////////////////////////////////////////////////////////////////////
475
476SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
477SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
478SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
479SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
480SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
481 shader_type_mismatch);
482SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
483 shader_type_mismatch);
484SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
485SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
486
487namespace {
488
489// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
490// justAlpha indicates that skPaint's alpha should be used rather than the color
491// Callers may subsequently modify the GrPaint. Setting constantColor indicates
492// that the final paint will draw the same color at every pixel. This allows
493// an optimization where the the color filter can be applied to the skPaint's
494// color once while converting to GrPaint and then ignored.
495inline bool skPaint2GrPaintNoShader(SkGpuDevice* dev,
496 const SkPaint& skPaint,
497 bool justAlpha,
498 bool constantColor,
499 GrPaint* grPaint) {
500
501 grPaint->setDither(skPaint.isDither());
502 grPaint->setAntiAlias(skPaint.isAntiAlias());
503
504 SkXfermode::Coeff sm;
505 SkXfermode::Coeff dm;
506
507 SkXfermode* mode = skPaint.getXfermode();
508 GrEffectRef* xferEffect = NULL;
509 if (SkXfermode::AsNewEffectOrCoeff(mode, &xferEffect, &sm, &dm)) {
510 if (NULL != xferEffect) {
511 grPaint->addColorEffect(xferEffect)->unref();
512 sm = SkXfermode::kOne_Coeff;
513 dm = SkXfermode::kZero_Coeff;
514 }
515 } else {
516 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
517#if 0
518 return false;
519#else
520 // Fall back to src-over
521 sm = SkXfermode::kOne_Coeff;
522 dm = SkXfermode::kISA_Coeff;
523#endif
524 }
525 grPaint->setBlendFunc(sk_blend_to_grblend(sm), sk_blend_to_grblend(dm));
526
527 if (justAlpha) {
528 uint8_t alpha = skPaint.getAlpha();
529 grPaint->setColor(GrColorPackRGBA(alpha, alpha, alpha, alpha));
530 // justAlpha is currently set to true only if there is a texture,
531 // so constantColor should not also be true.
532 SkASSERT(!constantColor);
533 } else {
534 grPaint->setColor(SkColor2GrColor(skPaint.getColor()));
535 }
536
537 SkColorFilter* colorFilter = skPaint.getColorFilter();
538 if (NULL != colorFilter) {
539 // if the source color is a constant then apply the filter here once rather than per pixel
540 // in a shader.
541 if (constantColor) {
542 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
543 grPaint->setColor(SkColor2GrColor(filtered));
544 } else {
545 SkAutoTUnref<GrEffectRef> effect(colorFilter->asNewEffect(dev->context()));
546 if (NULL != effect.get()) {
547 grPaint->addColorEffect(effect);
548 }
549 }
550 }
551
552 return true;
553}
554
555// This function is similar to skPaint2GrPaintNoShader but also converts
556// skPaint's shader to a GrTexture/GrEffectStage if possible. The texture to
557// be used is set on grPaint and returned in param act. constantColor has the
558// same meaning as in skPaint2GrPaintNoShader.
559inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
560 const SkPaint& skPaint,
561 bool constantColor,
562 GrPaint* grPaint) {
563 SkShader* shader = skPaint.getShader();
564 if (NULL == shader) {
565 return skPaint2GrPaintNoShader(dev, skPaint, false, constantColor, grPaint);
566 }
567
commit-bot@chromium.org60770572014-01-13 15:57:05 +0000568 // SkShader::asNewEffect() may do offscreen rendering. Setup default drawing state and require
569 // the shader to set a render target .
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000570 GrContext::AutoWideOpenIdentityDraw awo(dev->context(), NULL);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000571
572 // setup the shader as the first color effect on the paint
573 SkAutoTUnref<GrEffectRef> effect(shader->asNewEffect(dev->context(), skPaint));
574 if (NULL != effect.get()) {
575 grPaint->addColorEffect(effect);
576 // Now setup the rest of the paint.
577 return skPaint2GrPaintNoShader(dev, skPaint, true, false, grPaint);
578 } else {
579 // We still don't have SkColorShader::asNewEffect() implemented.
580 SkShader::GradientInfo info;
581 SkColor color;
582
583 info.fColors = &color;
584 info.fColorOffsets = NULL;
585 info.fColorCount = 1;
586 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
587 SkPaint copy(skPaint);
588 copy.setShader(NULL);
589 // modulate the paint alpha by the shader's solid color alpha
590 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
591 copy.setColor(SkColorSetA(color, newA));
592 return skPaint2GrPaintNoShader(dev, copy, false, constantColor, grPaint);
593 } else {
594 return false;
595 }
596 }
597}
598}
599
600///////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000601
602SkBitmap::Config SkGpuDevice::config() const {
603 if (NULL == fRenderTarget) {
604 return SkBitmap::kNo_Config;
605 }
606
607 bool isOpaque;
608 return grConfig2skConfig(fRenderTarget->config(), &isOpaque);
609}
610
611void SkGpuDevice::clear(SkColor color) {
612 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
613 fContext->clear(&rect, SkColor2GrColor(color), true, fRenderTarget);
614 fNeedClear = false;
615}
616
617void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
618 CHECK_SHOULD_DRAW(draw, false);
619
620 GrPaint grPaint;
621 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
622 return;
623 }
624
625 fContext->drawPaint(grPaint);
626}
627
628// must be in SkCanvas::PointMode order
629static const GrPrimitiveType gPointMode2PrimtiveType[] = {
630 kPoints_GrPrimitiveType,
631 kLines_GrPrimitiveType,
632 kLineStrip_GrPrimitiveType
633};
634
635void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
636 size_t count, const SkPoint pts[], const SkPaint& paint) {
637 CHECK_FOR_ANNOTATION(paint);
638 CHECK_SHOULD_DRAW(draw, false);
639
640 SkScalar width = paint.getStrokeWidth();
641 if (width < 0) {
642 return;
643 }
644
645 // we only handle hairlines and paints without path effects or mask filters,
646 // else we let the SkDraw call our drawPath()
647 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
648 draw.drawPoints(mode, count, pts, paint, true);
649 return;
650 }
651
652 GrPaint grPaint;
653 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
654 return;
655 }
656
657 fContext->drawVertices(grPaint,
658 gPointMode2PrimtiveType[mode],
robertphillips@google.coma4662862013-11-21 14:24:16 +0000659 SkToS32(count),
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000660 (GrPoint*)pts,
661 NULL,
662 NULL,
663 NULL,
664 0);
665}
666
667///////////////////////////////////////////////////////////////////////////////
668
669void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
670 const SkPaint& paint) {
671 CHECK_FOR_ANNOTATION(paint);
672 CHECK_SHOULD_DRAW(draw, false);
673
674 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
675 SkScalar width = paint.getStrokeWidth();
676
677 /*
678 We have special code for hairline strokes, miter-strokes, bevel-stroke
679 and fills. Anything else we just call our path code.
680 */
681 bool usePath = doStroke && width > 0 &&
682 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
683 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
684 // another two reasons we might need to call drawPath...
685 if (paint.getMaskFilter() || paint.getPathEffect()) {
686 usePath = true;
687 }
688 if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) {
689#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
690 if (doStroke) {
691#endif
692 usePath = true;
693#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
694 } else {
695 usePath = !fContext->getMatrix().preservesRightAngles();
696 }
697#endif
698 }
699 // until we can both stroke and fill rectangles
700 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
701 usePath = true;
702 }
703
704 if (usePath) {
705 SkPath path;
706 path.addRect(rect);
707 this->drawPath(draw, path, paint, NULL, true);
708 return;
709 }
710
711 GrPaint grPaint;
712 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
713 return;
714 }
715
716 if (!doStroke) {
717 fContext->drawRect(grPaint, rect);
718 } else {
719 SkStrokeRec stroke(paint);
720 fContext->drawRect(grPaint, rect, &stroke);
721 }
722}
723
724///////////////////////////////////////////////////////////////////////////////
725
726void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
727 const SkPaint& paint) {
728 CHECK_FOR_ANNOTATION(paint);
729 CHECK_SHOULD_DRAW(draw, false);
730
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000731 GrPaint grPaint;
732 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
733 return;
734 }
735
736 SkStrokeRec stroke(paint);
737 if (paint.getMaskFilter()) {
738 // try to hit the fast path for drawing filtered round rects
739
740 SkRRect devRRect;
741 if (rect.transform(fContext->getMatrix(), &devRRect)) {
742 if (devRRect.allCornersCircular()) {
743 SkRect maskRect;
744 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(),
745 draw.fClip->getBounds(),
746 fContext->getMatrix(),
747 &maskRect)) {
748 SkIRect finalIRect;
749 maskRect.roundOut(&finalIRect);
750 if (draw.fClip->quickReject(finalIRect)) {
751 // clipped out
752 return;
753 }
754 if (NULL != draw.fBounder && !draw.fBounder->doIRect(finalIRect)) {
755 // nothing to draw
756 return;
757 }
758 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext, &grPaint,
759 stroke, devRRect)) {
760 return;
761 }
762 }
763
764 }
765 }
766
767 }
768
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000769 bool usePath = !rect.isSimple();
770 // another two reasons we might need to call drawPath...
771 if (paint.getMaskFilter() || paint.getPathEffect()) {
772 usePath = true;
773 }
774 // until we can rotate rrects...
775 if (!usePath && !fContext->getMatrix().rectStaysRect()) {
776 usePath = true;
777 }
778
779 if (usePath) {
780 SkPath path;
781 path.addRRect(rect);
782 this->drawPath(draw, path, paint, NULL, true);
783 return;
784 }
785
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000786 fContext->drawRRect(grPaint, rect, stroke);
787}
788
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000789/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000790
791void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
792 const SkPaint& paint) {
793 CHECK_FOR_ANNOTATION(paint);
794 CHECK_SHOULD_DRAW(draw, false);
795
796 bool usePath = false;
797 // some basic reasons we might need to call drawPath...
798 if (paint.getMaskFilter() || paint.getPathEffect()) {
799 usePath = true;
800 }
801
802 if (usePath) {
803 SkPath path;
804 path.addOval(oval);
805 this->drawPath(draw, path, paint, NULL, true);
806 return;
807 }
808
809 GrPaint grPaint;
810 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
811 return;
812 }
813 SkStrokeRec stroke(paint);
814
815 fContext->drawOval(grPaint, oval, stroke);
816}
817
818#include "SkMaskFilter.h"
819#include "SkBounder.h"
820
821///////////////////////////////////////////////////////////////////////////////
822
823// helpers for applying mask filters
824namespace {
825
826// Draw a mask using the supplied paint. Since the coverage/geometry
827// is already burnt into the mask this boils down to a rect draw.
828// Return true if the mask was successfully drawn.
829bool draw_mask(GrContext* context, const SkRect& maskRect,
830 GrPaint* grp, GrTexture* mask) {
831 GrContext::AutoMatrix am;
832 if (!am.setIdentity(context, grp)) {
833 return false;
834 }
835
836 SkMatrix matrix;
837 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
838 matrix.postIDiv(mask->width(), mask->height());
839
840 grp->addCoverageEffect(GrSimpleTextureEffect::Create(mask, matrix))->unref();
841 context->drawRect(*grp, maskRect);
842 return true;
843}
844
845bool draw_with_mask_filter(GrContext* context, const SkPath& devPath,
846 SkMaskFilter* filter, const SkRegion& clip, SkBounder* bounder,
847 GrPaint* grp, SkPaint::Style style) {
848 SkMask srcM, dstM;
849
850 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
851 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
852 return false;
853 }
854 SkAutoMaskFreeImage autoSrc(srcM.fImage);
855
856 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
857 return false;
858 }
859 // this will free-up dstM when we're done (allocated in filterMask())
860 SkAutoMaskFreeImage autoDst(dstM.fImage);
861
862 if (clip.quickReject(dstM.fBounds)) {
863 return false;
864 }
865 if (bounder && !bounder->doIRect(dstM.fBounds)) {
866 return false;
867 }
868
869 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
870 // the current clip (and identity matrix) and GrPaint settings
871 GrTextureDesc desc;
872 desc.fWidth = dstM.fBounds.width();
873 desc.fHeight = dstM.fBounds.height();
874 desc.fConfig = kAlpha_8_GrPixelConfig;
875
876 GrAutoScratchTexture ast(context, desc);
877 GrTexture* texture = ast.texture();
878
879 if (NULL == texture) {
880 return false;
881 }
882 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
883 dstM.fImage, dstM.fRowBytes);
884
885 SkRect maskRect = SkRect::Make(dstM.fBounds);
886
887 return draw_mask(context, maskRect, grp, texture);
888}
889
890// Create a mask of 'devPath' and place the result in 'mask'. Return true on
891// success; false otherwise.
892bool create_mask_GPU(GrContext* context,
893 const SkRect& maskRect,
894 const SkPath& devPath,
895 const SkStrokeRec& stroke,
896 bool doAA,
897 GrAutoScratchTexture* mask) {
898 GrTextureDesc desc;
899 desc.fFlags = kRenderTarget_GrTextureFlagBit;
900 desc.fWidth = SkScalarCeilToInt(maskRect.width());
901 desc.fHeight = SkScalarCeilToInt(maskRect.height());
902 // We actually only need A8, but it often isn't supported as a
903 // render target so default to RGBA_8888
904 desc.fConfig = kRGBA_8888_GrPixelConfig;
905 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
906 desc.fConfig = kAlpha_8_GrPixelConfig;
907 }
908
909 mask->set(context, desc);
910 if (NULL == mask->texture()) {
911 return false;
912 }
913
914 GrTexture* maskTexture = mask->texture();
915 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
916
917 GrContext::AutoRenderTarget art(context, maskTexture->asRenderTarget());
918 GrContext::AutoClip ac(context, clipRect);
919
920 context->clear(NULL, 0x0, true);
921
922 GrPaint tempPaint;
923 if (doAA) {
924 tempPaint.setAntiAlias(true);
925 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
926 // blend coeff of zero requires dual source blending support in order
927 // to properly blend partially covered pixels. This means the AA
928 // code path may not be taken. So we use a dst blend coeff of ISA. We
929 // could special case AA draws to a dst surface with known alpha=0 to
930 // use a zero dst coeff when dual source blending isn't available.
931 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
932 }
933
934 GrContext::AutoMatrix am;
935
936 // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint.
937 SkMatrix translate;
938 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
939 am.set(context, translate);
940 context->drawPath(tempPaint, devPath, stroke);
941 return true;
942}
943
944SkBitmap wrap_texture(GrTexture* texture) {
reed@google.combf790232013-12-13 19:45:58 +0000945 SkImageInfo info;
946 texture->asImageInfo(&info);
947
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000948 SkBitmap result;
reed@google.combf790232013-12-13 19:45:58 +0000949 result.setConfig(info);
950 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000951 return result;
952}
953
954};
955
956void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
957 const SkPaint& paint, const SkMatrix* prePathMatrix,
958 bool pathIsMutable) {
959 CHECK_FOR_ANNOTATION(paint);
960 CHECK_SHOULD_DRAW(draw, false);
961
962 GrPaint grPaint;
963 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
964 return;
965 }
966
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000967 // If we have a prematrix, apply it to the path, optimizing for the case
968 // where the original path can in fact be modified in place (even though
969 // its parameter type is const).
970 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000971 SkTLazy<SkPath> tmpPath;
972 SkTLazy<SkPath> effectPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000973
974 if (prePathMatrix) {
975 SkPath* result = pathPtr;
976
977 if (!pathIsMutable) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000978 result = tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000979 pathIsMutable = true;
980 }
981 // should I push prePathMatrix on our MV stack temporarily, instead
982 // of applying it here? See SkDraw.cpp
983 pathPtr->transform(*prePathMatrix, result);
984 pathPtr = result;
985 }
986 // at this point we're done with prePathMatrix
987 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
988
989 SkStrokeRec stroke(paint);
990 SkPathEffect* pathEffect = paint.getPathEffect();
991 const SkRect* cullRect = NULL; // TODO: what is our bounds?
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000992 if (pathEffect && pathEffect->filterPath(effectPath.init(), *pathPtr, &stroke,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000993 cullRect)) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000994 pathPtr = effectPath.get();
995 pathIsMutable = true;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000996 }
997
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000998 if (paint.getMaskFilter()) {
999 if (!stroke.isHairlineStyle()) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +00001000 SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init();
1001 if (stroke.applyToPath(strokedPath, *pathPtr)) {
1002 pathPtr = strokedPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001003 pathIsMutable = true;
1004 stroke.setFillStyle();
1005 }
1006 }
1007
1008 // avoid possibly allocating a new path in transform if we can
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +00001009 SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001010
1011 // transform the path into device space
1012 pathPtr->transform(fContext->getMatrix(), devPathPtr);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001013
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001014 SkRect maskRect;
1015 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
1016 draw.fClip->getBounds(),
1017 fContext->getMatrix(),
1018 &maskRect)) {
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +00001019 // The context's matrix may change while creating the mask, so save the CTM here to
1020 // pass to filterMaskGPU.
1021 const SkMatrix ctm = fContext->getMatrix();
1022
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001023 SkIRect finalIRect;
1024 maskRect.roundOut(&finalIRect);
1025 if (draw.fClip->quickReject(finalIRect)) {
1026 // clipped out
1027 return;
1028 }
1029 if (NULL != draw.fBounder && !draw.fBounder->doIRect(finalIRect)) {
1030 // nothing to draw
1031 return;
1032 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001033
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +00001034 if (paint.getMaskFilter()->directFilterMaskGPU(fContext, &grPaint,
commit-bot@chromium.org82139702014-03-10 22:53:20 +00001035 stroke, *devPathPtr)) {
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +00001036 // the mask filter was able to draw itself directly, so there's nothing
1037 // left to do.
1038 return;
1039 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001040
1041 GrAutoScratchTexture mask;
1042
1043 if (create_mask_GPU(fContext, maskRect, *devPathPtr, stroke,
1044 grPaint.isAntiAlias(), &mask)) {
1045 GrTexture* filtered;
1046
commit-bot@chromium.org41bf9302014-01-08 22:25:53 +00001047 if (paint.getMaskFilter()->filterMaskGPU(mask.texture(),
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +00001048 ctm, maskRect, &filtered, true)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001049 // filterMaskGPU gives us ownership of a ref to the result
1050 SkAutoTUnref<GrTexture> atu(filtered);
1051
1052 // If the scratch texture that we used as the filter src also holds the filter
1053 // result then we must detach so that this texture isn't recycled for a later
1054 // draw.
1055 if (filtered == mask.texture()) {
1056 mask.detach();
1057 filtered->unref(); // detach transfers GrAutoScratchTexture's ref to us.
1058 }
1059
1060 if (draw_mask(fContext, maskRect, &grPaint, filtered)) {
1061 // This path is completely drawn
1062 return;
1063 }
1064 }
1065 }
1066 }
1067
1068 // draw the mask on the CPU - this is a fallthrough path in case the
1069 // GPU path fails
1070 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
1071 SkPaint::kFill_Style;
1072 draw_with_mask_filter(fContext, *devPathPtr, paint.getMaskFilter(),
1073 *draw.fClip, draw.fBounder, &grPaint, style);
1074 return;
1075 }
1076
1077 fContext->drawPath(grPaint, *pathPtr, stroke);
1078}
1079
1080static const int kBmpSmallTileSize = 1 << 10;
1081
1082static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
1083 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
1084 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
1085 return tilesX * tilesY;
1086}
1087
1088static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
1089 if (maxTileSize <= kBmpSmallTileSize) {
1090 return maxTileSize;
1091 }
1092
1093 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
1094 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
1095
1096 maxTileTotalTileSize *= maxTileSize * maxTileSize;
1097 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
1098
1099 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
1100 return kBmpSmallTileSize;
1101 } else {
1102 return maxTileSize;
1103 }
1104}
1105
1106// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
1107// pixels from the bitmap are necessary.
1108static void determine_clipped_src_rect(const GrContext* context,
1109 const SkBitmap& bitmap,
1110 const SkRect* srcRectPtr,
1111 SkIRect* clippedSrcIRect) {
1112 const GrClipData* clip = context->getClip();
1113 clip->getConservativeBounds(context->getRenderTarget(), clippedSrcIRect, NULL);
1114 SkMatrix inv;
1115 if (!context->getMatrix().invert(&inv)) {
1116 clippedSrcIRect->setEmpty();
1117 return;
1118 }
1119 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
1120 inv.mapRect(&clippedSrcRect);
1121 if (NULL != srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001122 // we've setup src space 0,0 to map to the top left of the src rect.
1123 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001124 if (!clippedSrcRect.intersect(*srcRectPtr)) {
1125 clippedSrcIRect->setEmpty();
1126 return;
1127 }
1128 }
1129 clippedSrcRect.roundOut(clippedSrcIRect);
1130 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1131 if (!clippedSrcIRect->intersect(bmpBounds)) {
1132 clippedSrcIRect->setEmpty();
1133 }
1134}
1135
1136bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
1137 const GrTextureParams& params,
1138 const SkRect* srcRectPtr,
1139 int maxTileSize,
1140 int* tileSize,
1141 SkIRect* clippedSrcRect) const {
1142 // if bitmap is explictly texture backed then just use the texture
1143 if (NULL != bitmap.getTexture()) {
1144 return false;
1145 }
1146
1147 // if it's larger than the max tile size, then we have no choice but tiling.
1148 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
1149 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
1150 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
1151 return true;
1152 }
1153
1154 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
1155 return false;
1156 }
1157
1158 // if the entire texture is already in our cache then no reason to tile it
1159 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
1160 return false;
1161 }
1162
1163 // At this point we know we could do the draw by uploading the entire bitmap
1164 // as a texture. However, if the texture would be large compared to the
1165 // cache size and we don't require most of it for this draw then tile to
1166 // reduce the amount of upload and cache spill.
1167
1168 // assumption here is that sw bitmap size is a good proxy for its size as
1169 // a texture
1170 size_t bmpSize = bitmap.getSize();
1171 size_t cacheSize;
1172 fContext->getTextureCacheLimits(NULL, &cacheSize);
1173 if (bmpSize < cacheSize / 2) {
1174 return false;
1175 }
1176
1177 // Figure out how much of the src we will need based on the src rect and clipping.
1178 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
1179 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
1180 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
1181 kBmpSmallTileSize * kBmpSmallTileSize;
1182
1183 return usedTileBytes < 2 * bmpSize;
1184}
1185
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001186void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001187 const SkBitmap& bitmap,
1188 const SkMatrix& m,
1189 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001190 SkMatrix concat;
1191 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1192 if (!m.isIdentity()) {
1193 concat.setConcat(*draw->fMatrix, m);
1194 draw.writable()->fMatrix = &concat;
1195 }
1196 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001197}
1198
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001199// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001200// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
1201// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001202static inline void clamped_outset_with_offset(SkIRect* iRect,
1203 int outset,
1204 SkPoint* offset,
1205 const SkIRect& clamp) {
1206 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001207
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001208 int leftClampDelta = clamp.fLeft - iRect->fLeft;
1209 if (leftClampDelta > 0) {
1210 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001211 iRect->fLeft = clamp.fLeft;
1212 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001213 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001214 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001215
1216 int topClampDelta = clamp.fTop - iRect->fTop;
1217 if (topClampDelta > 0) {
1218 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001219 iRect->fTop = clamp.fTop;
1220 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001221 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001222 }
1223
1224 if (iRect->fRight > clamp.fRight) {
1225 iRect->fRight = clamp.fRight;
1226 }
1227 if (iRect->fBottom > clamp.fBottom) {
1228 iRect->fBottom = clamp.fBottom;
1229 }
1230}
1231
1232void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1233 const SkBitmap& bitmap,
1234 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001235 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001236 const SkPaint& paint,
1237 SkCanvas::DrawBitmapRectFlags flags) {
1238 CHECK_SHOULD_DRAW(draw, false);
1239
1240 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001241 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001242 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
1243 // in the (easier) bleed case, so update flags.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001244 if (NULL == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001245 SkScalar w = SkIntToScalar(bitmap.width());
1246 SkScalar h = SkIntToScalar(bitmap.height());
1247 dstSize.fWidth = w;
1248 dstSize.fHeight = h;
1249 srcRect.set(0, 0, w, h);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001250 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001251 } else {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001252 SkASSERT(NULL != dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001253 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001254 dstSize = *dstSizePtr;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001255 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
1256 srcRect.fRight >= bitmap.width() && srcRect.fBottom >= bitmap.height()) {
1257 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
1258 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001259 }
1260
1261 if (paint.getMaskFilter()){
1262 // Convert the bitmap to a shader so that the rect can be drawn
1263 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001264 SkBitmap tmp; // subset of bitmap, if necessary
1265 const SkBitmap* bitmapPtr = &bitmap;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001266 SkMatrix localM;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001267 if (NULL != srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001268 localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
1269 localM.postScale(dstSize.fWidth / srcRectPtr->width(),
1270 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001271 // In bleed mode we position and trim the bitmap based on the src rect which is
1272 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
1273 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
1274 // compensate.
1275 if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) {
1276 SkIRect iSrc;
1277 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001278
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001279 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
1280 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001281
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001282 if (!bitmap.extractSubset(&tmp, iSrc)) {
1283 return; // extraction failed
1284 }
1285 bitmapPtr = &tmp;
1286 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001287
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001288 // The source rect has changed so update the matrix
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001289 localM.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001290 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001291 } else {
1292 localM.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001293 }
1294
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001295 SkPaint paintWithShader(paint);
1296 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001297 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001298 paintWithShader.getShader()->setLocalMatrix(localM);
1299 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1300 this->drawRect(draw, dstRect, paintWithShader);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001301
1302 return;
1303 }
1304
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001305 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
1306 // the view matrix rather than a local matrix.
1307 SkMatrix m;
1308 m.setScale(dstSize.fWidth / srcRect.width(),
1309 dstSize.fHeight / srcRect.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001310 fContext->concatMatrix(m);
1311
1312 GrTextureParams params;
1313 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel();
1314 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001315
1316 int tileFilterPad;
1317 bool doBicubic = false;
1318
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001319 switch(paintFilterLevel) {
1320 case SkPaint::kNone_FilterLevel:
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001321 tileFilterPad = 0;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001322 textureFilterMode = GrTextureParams::kNone_FilterMode;
1323 break;
1324 case SkPaint::kLow_FilterLevel:
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001325 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001326 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1327 break;
1328 case SkPaint::kMedium_FilterLevel:
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001329 tileFilterPad = 1;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001330 if (fContext->getMatrix().getMinStretch() < SK_Scalar1) {
1331 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1332 } else {
1333 // Don't trigger MIP level generation unnecessarily.
1334 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1335 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001336 break;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001337 case SkPaint::kHigh_FilterLevel:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001338 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001339 if (fContext->getMatrix().getMinStretch() >= SK_Scalar1) {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001340 // We will install an effect that does the filtering in the shader.
1341 textureFilterMode = GrTextureParams::kNone_FilterMode;
1342 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1343 doBicubic = true;
1344 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001345 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1346 tileFilterPad = 1;
1347 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001348 break;
1349 default:
1350 SkErrorInternals::SetError( kInvalidPaint_SkError,
1351 "Sorry, I don't understand the filtering "
1352 "mode you asked for. Falling back to "
1353 "MIPMaps.");
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001354 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001355 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1356 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001357 }
1358
1359 params.setFilterMode(textureFilterMode);
1360
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001361 int maxTileSize = fContext->getMaxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001362 int tileSize;
1363
1364 SkIRect clippedSrcRect;
1365 if (this->shouldTileBitmap(bitmap, params, srcRectPtr, maxTileSize, &tileSize,
1366 &clippedSrcRect)) {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001367 this->drawTiledBitmap(bitmap, srcRect, clippedSrcRect, params, paint, flags, tileSize,
1368 doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001369 } else {
1370 // take the simple case
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001371 this->internalDrawBitmap(bitmap, srcRect, params, paint, flags, doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001372 }
1373}
1374
1375// Break 'bitmap' into several tiles to draw it since it has already
1376// been determined to be too large to fit in VRAM
1377void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
1378 const SkRect& srcRect,
1379 const SkIRect& clippedSrcIRect,
1380 const GrTextureParams& params,
1381 const SkPaint& paint,
1382 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001383 int tileSize,
1384 bool bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001385 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1386
1387 int nx = bitmap.width() / tileSize;
1388 int ny = bitmap.height() / tileSize;
1389 for (int x = 0; x <= nx; x++) {
1390 for (int y = 0; y <= ny; y++) {
1391 SkRect tileR;
1392 tileR.set(SkIntToScalar(x * tileSize),
1393 SkIntToScalar(y * tileSize),
1394 SkIntToScalar((x + 1) * tileSize),
1395 SkIntToScalar((y + 1) * tileSize));
1396
1397 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1398 continue;
1399 }
1400
1401 if (!tileR.intersect(srcRect)) {
1402 continue;
1403 }
1404
1405 SkBitmap tmpB;
1406 SkIRect iTileR;
1407 tileR.roundOut(&iTileR);
1408 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1409 SkIntToScalar(iTileR.fTop));
1410
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001411 // Adjust the context matrix to draw at the right x,y in device space
1412 SkMatrix tmpM;
1413 GrContext::AutoMatrix am;
1414 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
1415 am.setPreConcat(fContext, tmpM);
1416
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001417 if (SkPaint::kNone_FilterLevel != paint.getFilterLevel() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001418 SkIRect iClampRect;
1419
1420 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1421 // In bleed mode we want to always expand the tile on all edges
1422 // but stay within the bitmap bounds
1423 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1424 } else {
1425 // In texture-domain/clamp mode we only want to expand the
1426 // tile on edges interior to "srcRect" (i.e., we want to
1427 // not bleed across the original clamped edges)
1428 srcRect.roundOut(&iClampRect);
1429 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001430 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1431 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001432 }
1433
1434 if (bitmap.extractSubset(&tmpB, iTileR)) {
1435 // now offset it to make it "local" to our tmp bitmap
1436 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001437
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001438 this->internalDrawBitmap(tmpB, tileR, params, paint, flags, bicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001439 }
1440 }
1441 }
1442}
1443
1444static bool has_aligned_samples(const SkRect& srcRect,
1445 const SkRect& transformedRect) {
1446 // detect pixel disalignment
1447 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1448 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1449 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1450 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1451 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1452 COLOR_BLEED_TOLERANCE &&
1453 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1454 COLOR_BLEED_TOLERANCE) {
1455 return true;
1456 }
1457 return false;
1458}
1459
1460static bool may_color_bleed(const SkRect& srcRect,
1461 const SkRect& transformedRect,
1462 const SkMatrix& m) {
1463 // Only gets called if has_aligned_samples returned false.
1464 // So we can assume that sampling is axis aligned but not texel aligned.
1465 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
1466 SkRect innerSrcRect(srcRect), innerTransformedRect,
1467 outerTransformedRect(transformedRect);
1468 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1469 m.mapRect(&innerTransformedRect, innerSrcRect);
1470
1471 // The gap between outerTransformedRect and innerTransformedRect
1472 // represents the projection of the source border area, which is
1473 // problematic for color bleeding. We must check whether any
1474 // destination pixels sample the border area.
1475 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1476 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1477 SkIRect outer, inner;
1478 outerTransformedRect.round(&outer);
1479 innerTransformedRect.round(&inner);
1480 // If the inner and outer rects round to the same result, it means the
1481 // border does not overlap any pixel centers. Yay!
1482 return inner != outer;
1483}
1484
1485
1486/*
1487 * This is called by drawBitmap(), which has to handle images that may be too
1488 * large to be represented by a single texture.
1489 *
1490 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1491 * and that non-texture portion of the GrPaint has already been setup.
1492 */
1493void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
1494 const SkRect& srcRect,
1495 const GrTextureParams& params,
1496 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001497 SkCanvas::DrawBitmapRectFlags flags,
1498 bool bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001499 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1500 bitmap.height() <= fContext->getMaxTextureSize());
1501
1502 GrTexture* texture;
1503 SkAutoCachedTexture act(this, bitmap, &params, &texture);
1504 if (NULL == texture) {
1505 return;
1506 }
1507
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001508 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001509 SkRect paintRect;
1510 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1511 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1512 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1513 SkScalarMul(srcRect.fTop, hInv),
1514 SkScalarMul(srcRect.fRight, wInv),
1515 SkScalarMul(srcRect.fBottom, hInv));
1516
1517 bool needsTextureDomain = false;
1518 if (!(flags & SkCanvas::kBleed_DrawBitmapRectFlag) &&
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001519 (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode)) {
1520 // Need texture domain if drawing a sub rect
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001521 needsTextureDomain = srcRect.width() < bitmap.width() ||
1522 srcRect.height() < bitmap.height();
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001523 if (!bicubic && needsTextureDomain && fContext->getMatrix().rectStaysRect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001524 const SkMatrix& matrix = fContext->getMatrix();
1525 // sampling is axis-aligned
1526 SkRect transformedRect;
1527 matrix.mapRect(&transformedRect, srcRect);
1528
1529 if (has_aligned_samples(srcRect, transformedRect)) {
1530 // We could also turn off filtering here (but we already did a cache lookup with
1531 // params).
1532 needsTextureDomain = false;
1533 } else {
1534 needsTextureDomain = may_color_bleed(srcRect, transformedRect, matrix);
1535 }
1536 }
1537 }
1538
1539 SkRect textureDomain = SkRect::MakeEmpty();
1540 SkAutoTUnref<GrEffectRef> effect;
1541 if (needsTextureDomain) {
1542 // Use a constrained texture domain to avoid color bleeding
1543 SkScalar left, top, right, bottom;
1544 if (srcRect.width() > SK_Scalar1) {
1545 SkScalar border = SK_ScalarHalf / texture->width();
1546 left = paintRect.left() + border;
1547 right = paintRect.right() - border;
1548 } else {
1549 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1550 }
1551 if (srcRect.height() > SK_Scalar1) {
1552 SkScalar border = SK_ScalarHalf / texture->height();
1553 top = paintRect.top() + border;
1554 bottom = paintRect.bottom() - border;
1555 } else {
1556 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1557 }
1558 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001559 if (bicubic) {
1560 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
1561 } else {
1562 effect.reset(GrTextureDomainEffect::Create(texture,
1563 SkMatrix::I(),
1564 textureDomain,
1565 GrTextureDomain::kClamp_Mode,
1566 params.filterMode()));
1567 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001568 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001569 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1570 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
1571 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001572 } else {
1573 effect.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
1574 }
1575
1576 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1577 // the rest from the SkPaint.
1578 GrPaint grPaint;
1579 grPaint.addColorEffect(effect);
1580 bool alphaOnly = !(SkBitmap::kA8_Config == bitmap.config());
1581 if (!skPaint2GrPaintNoShader(this, paint, alphaOnly, false, &grPaint)) {
1582 return;
1583 }
1584
1585 fContext->drawRectToRect(grPaint, dstRect, paintRect, NULL);
1586}
1587
1588static bool filter_texture(SkBaseDevice* device, GrContext* context,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001589 GrTexture* texture, const SkImageFilter* filter,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001590 int w, int h, const SkImageFilter::Context& ctx,
1591 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001592 SkASSERT(filter);
1593 SkDeviceImageFilterProxy proxy(device);
1594
1595 if (filter->canFilterImageGPU()) {
1596 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1597 // filter. Also set the clip wide open and the matrix to identity.
1598 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001599 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001600 } else {
1601 return false;
1602 }
1603}
1604
1605void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1606 int left, int top, const SkPaint& paint) {
1607 // drawSprite is defined to be in device coords.
1608 CHECK_SHOULD_DRAW(draw, true);
1609
1610 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1611 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1612 return;
1613 }
1614
1615 int w = bitmap.width();
1616 int h = bitmap.height();
1617
1618 GrTexture* texture;
1619 // draw sprite uses the default texture params
1620 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
1621
1622 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001623 // This bitmap will own the filtered result as a texture.
1624 SkBitmap filteredBitmap;
1625
1626 if (NULL != filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001627 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001628 SkMatrix matrix(*draw.fMatrix);
1629 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001630 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1631 SkImageFilter::Context ctx(matrix, clipBounds);
1632 if (filter_texture(this, fContext, texture, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001633 &offset)) {
1634 texture = (GrTexture*) filteredBitmap.getTexture();
1635 w = filteredBitmap.width();
1636 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001637 left += offset.x();
1638 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001639 } else {
1640 return;
1641 }
1642 }
1643
1644 GrPaint grPaint;
1645 grPaint.addColorTextureEffect(texture, SkMatrix::I());
1646
1647 if(!skPaint2GrPaintNoShader(this, paint, true, false, &grPaint)) {
1648 return;
1649 }
1650
1651 fContext->drawRectToRect(grPaint,
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001652 SkRect::MakeXYWH(SkIntToScalar(left),
1653 SkIntToScalar(top),
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001654 SkIntToScalar(w),
1655 SkIntToScalar(h)),
1656 SkRect::MakeXYWH(0,
1657 0,
1658 SK_Scalar1 * w / texture->width(),
1659 SK_Scalar1 * h / texture->height()));
1660}
1661
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001662void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001663 const SkRect* src, const SkRect& dst,
1664 const SkPaint& paint,
1665 SkCanvas::DrawBitmapRectFlags flags) {
1666 SkMatrix matrix;
1667 SkRect bitmapBounds, tmpSrc;
1668
1669 bitmapBounds.set(0, 0,
1670 SkIntToScalar(bitmap.width()),
1671 SkIntToScalar(bitmap.height()));
1672
1673 // Compute matrix from the two rectangles
1674 if (NULL != src) {
1675 tmpSrc = *src;
1676 } else {
1677 tmpSrc = bitmapBounds;
1678 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001679
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001680 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1681
1682 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
1683 if (NULL != src) {
1684 if (!bitmapBounds.contains(tmpSrc)) {
1685 if (!tmpSrc.intersect(bitmapBounds)) {
1686 return; // nothing to draw
1687 }
1688 }
1689 }
1690
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001691 SkRect tmpDst;
1692 matrix.mapRect(&tmpDst, tmpSrc);
1693
1694 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1695 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1696 // Translate so that tempDst's top left is at the origin.
1697 matrix = *origDraw.fMatrix;
1698 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1699 draw.writable()->fMatrix = &matrix;
1700 }
1701 SkSize dstSize;
1702 dstSize.fWidth = tmpDst.width();
1703 dstSize.fHeight = tmpDst.height();
1704
1705 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001706}
1707
1708void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1709 int x, int y, const SkPaint& paint) {
1710 // clear of the source device must occur before CHECK_SHOULD_DRAW
1711 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1712 if (dev->fNeedClear) {
1713 // TODO: could check here whether we really need to draw at all
1714 dev->clear(0x0);
1715 }
1716
1717 // drawDevice is defined to be in device coords.
1718 CHECK_SHOULD_DRAW(draw, true);
1719
1720 GrRenderTarget* devRT = dev->accessRenderTarget();
1721 GrTexture* devTex;
1722 if (NULL == (devTex = devRT->asTexture())) {
1723 return;
1724 }
1725
1726 const SkBitmap& bm = dev->accessBitmap(false);
1727 int w = bm.width();
1728 int h = bm.height();
1729
1730 SkImageFilter* filter = paint.getImageFilter();
1731 // This bitmap will own the filtered result as a texture.
1732 SkBitmap filteredBitmap;
1733
1734 if (NULL != filter) {
1735 SkIPoint offset = SkIPoint::Make(0, 0);
1736 SkMatrix matrix(*draw.fMatrix);
1737 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001738 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
1739 SkImageFilter::Context ctx(matrix, clipBounds);
1740 if (filter_texture(this, fContext, devTex, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001741 &offset)) {
1742 devTex = filteredBitmap.getTexture();
1743 w = filteredBitmap.width();
1744 h = filteredBitmap.height();
1745 x += offset.fX;
1746 y += offset.fY;
1747 } else {
1748 return;
1749 }
1750 }
1751
1752 GrPaint grPaint;
1753 grPaint.addColorTextureEffect(devTex, SkMatrix::I());
1754
1755 if (!skPaint2GrPaintNoShader(this, paint, true, false, &grPaint)) {
1756 return;
1757 }
1758
1759 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1760 SkIntToScalar(y),
1761 SkIntToScalar(w),
1762 SkIntToScalar(h));
1763
1764 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1765 // scratch texture).
1766 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1767 SK_Scalar1 * h / devTex->height());
1768
1769 fContext->drawRectToRect(grPaint, dstRect, srcRect);
1770}
1771
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001772bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001773 return filter->canFilterImageGPU();
1774}
1775
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001776bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001777 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001778 SkBitmap* result, SkIPoint* offset) {
1779 // want explicitly our impl, so guard against a subclass of us overriding it
1780 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1781 return false;
1782 }
1783
1784 SkAutoLockPixels alp(src, !src.getTexture());
1785 if (!src.getTexture() && !src.readyToDraw()) {
1786 return false;
1787 }
1788
1789 GrTexture* texture;
1790 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1791 // must be pushed upstack.
1792 SkAutoCachedTexture act(this, src, NULL, &texture);
1793
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001794 return filter_texture(this, fContext, texture, filter, src.width(), src.height(), ctx,
1795 result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001796}
1797
1798///////////////////////////////////////////////////////////////////////////////
1799
1800// must be in SkCanvas::VertexMode order
1801static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1802 kTriangles_GrPrimitiveType,
1803 kTriangleStrip_GrPrimitiveType,
1804 kTriangleFan_GrPrimitiveType,
1805};
1806
1807void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1808 int vertexCount, const SkPoint vertices[],
1809 const SkPoint texs[], const SkColor colors[],
1810 SkXfermode* xmode,
1811 const uint16_t indices[], int indexCount,
1812 const SkPaint& paint) {
1813 CHECK_SHOULD_DRAW(draw, false);
1814
1815 GrPaint grPaint;
1816 // we ignore the shader if texs is null.
1817 if (NULL == texs) {
1818 if (!skPaint2GrPaintNoShader(this, paint, false, NULL == colors, &grPaint)) {
1819 return;
1820 }
1821 } else {
1822 if (!skPaint2GrPaintShader(this, paint, NULL == colors, &grPaint)) {
1823 return;
1824 }
1825 }
1826
1827 if (NULL != xmode && NULL != texs && NULL != colors) {
1828 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1829 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1830#if 0
1831 return
1832#endif
1833 }
1834 }
1835
1836 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1837 if (NULL != colors) {
1838 // need to convert byte order and from non-PM to PM
1839 convertedColors.reset(vertexCount);
1840 for (int i = 0; i < vertexCount; ++i) {
1841 convertedColors[i] = SkColor2GrColor(colors[i]);
1842 }
1843 colors = convertedColors.get();
1844 }
1845 fContext->drawVertices(grPaint,
1846 gVertexMode2PrimitiveType[vmode],
1847 vertexCount,
1848 (GrPoint*) vertices,
1849 (GrPoint*) texs,
1850 colors,
1851 indices,
1852 indexCount);
1853}
1854
1855///////////////////////////////////////////////////////////////////////////////
1856
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001857void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1858 size_t byteLength, SkScalar x, SkScalar y,
1859 const SkPaint& paint) {
1860 CHECK_SHOULD_DRAW(draw, false);
1861
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001862 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001863 GrPaint grPaint;
1864 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
1865 return;
1866 }
1867
1868 SkDEBUGCODE(this->validate();)
1869
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001870 fMainTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
1871 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001872 GrPaint grPaint;
1873 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
1874 return;
1875 }
1876
1877 SkDEBUGCODE(this->validate();)
1878
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001879 fFallbackTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001880 } else {
1881 // this guy will just call our drawPath()
1882 draw.drawText_asPaths((const char*)text, byteLength, x, y, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001883 }
1884}
1885
1886void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1887 size_t byteLength, const SkScalar pos[],
1888 SkScalar constY, int scalarsPerPos,
1889 const SkPaint& paint) {
1890 CHECK_SHOULD_DRAW(draw, false);
1891
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001892 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001893 GrPaint grPaint;
1894 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
1895 return;
1896 }
1897
1898 SkDEBUGCODE(this->validate();)
1899
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001900 fMainTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001901 constY, scalarsPerPos);
1902 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001903 GrPaint grPaint;
1904 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
1905 return;
1906 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001907
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001908 SkDEBUGCODE(this->validate();)
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001909
1910 fFallbackTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001911 constY, scalarsPerPos);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001912 } else {
1913 draw.drawPosText_asPaths((const char*)text, byteLength, pos, constY,
1914 scalarsPerPos, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001915 }
1916}
1917
1918void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1919 size_t len, const SkPath& path,
1920 const SkMatrix* m, const SkPaint& paint) {
1921 CHECK_SHOULD_DRAW(draw, false);
1922
1923 SkASSERT(draw.fDevice == this);
1924 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1925}
1926
1927///////////////////////////////////////////////////////////////////////////////
1928
1929bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1930 if (!paint.isLCDRenderText()) {
1931 // we're cool with the paint as is
1932 return false;
1933 }
1934
1935 if (paint.getShader() ||
1936 paint.getXfermode() || // unless its srcover
1937 paint.getMaskFilter() ||
1938 paint.getRasterizer() ||
1939 paint.getColorFilter() ||
1940 paint.getPathEffect() ||
1941 paint.isFakeBoldText() ||
1942 paint.getStyle() != SkPaint::kFill_Style) {
1943 // turn off lcd
1944 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1945 flags->fHinting = paint.getHinting();
1946 return true;
1947 }
1948 // we're cool with the paint as is
1949 return false;
1950}
1951
1952void SkGpuDevice::flush() {
1953 DO_DEFERRED_CLEAR();
1954 fContext->resolveRenderTarget(fRenderTarget);
1955}
1956
1957///////////////////////////////////////////////////////////////////////////////
1958
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001959SkBaseDevice* SkGpuDevice::onCreateDevice(const SkImageInfo& info, Usage usage) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001960 GrTextureDesc desc;
1961 desc.fConfig = fRenderTarget->config();
1962 desc.fFlags = kRenderTarget_GrTextureFlagBit;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001963 desc.fWidth = info.width();
1964 desc.fHeight = info.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001965 desc.fSampleCnt = fRenderTarget->numSamples();
1966
1967 SkAutoTUnref<GrTexture> texture;
1968 // Skia's convention is to only clear a device if it is non-opaque.
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001969 bool needClear = !info.isOpaque();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001970
1971#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1972 // layers are never draw in repeat modes, so we can request an approx
1973 // match and ignore any padding.
1974 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == usage) ?
1975 GrContext::kApprox_ScratchTexMatch :
1976 GrContext::kExact_ScratchTexMatch;
1977 texture.reset(fContext->lockAndRefScratchTexture(desc, match));
1978#else
1979 texture.reset(fContext->createUncachedTexture(desc, NULL, 0));
1980#endif
1981 if (NULL != texture.get()) {
1982 return SkNEW_ARGS(SkGpuDevice,(fContext, texture, needClear));
1983 } else {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001984 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1985 info.width(), info.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001986 return NULL;
1987 }
1988}
1989
reed@google.com76f10a32014-02-05 15:32:21 +00001990SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info) {
1991 return SkSurface::NewRenderTarget(fContext, info, fRenderTarget->numSamples());
1992}
1993
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001994SkGpuDevice::SkGpuDevice(GrContext* context,
1995 GrTexture* texture,
1996 bool needClear)
1997 : SkBitmapDevice(make_bitmap(context, texture->asRenderTarget())) {
1998
1999 SkASSERT(texture && texture->asRenderTarget());
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00002000 // This constructor is called from onCreateDevice. It has locked the RT in the texture
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00002001 // cache. We pass true for the third argument so that it will get unlocked.
2002 this->initFromRenderTarget(context, texture->asRenderTarget(), true);
2003 fNeedClear = needClear;
2004}
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00002005
2006class GPUAccelData : public SkPicture::AccelData {
2007public:
2008 GPUAccelData(Key key) : INHERITED(key) { }
2009
2010protected:
2011
2012private:
2013 typedef SkPicture::AccelData INHERITED;
2014};
2015
2016// In the future this may not be a static method if we need to incorporate the
2017// clip and matrix state into the key
2018SkPicture::AccelData::Key SkGpuDevice::ComputeAccelDataKey() {
2019 static const SkPicture::AccelData::Key gGPUID = SkPicture::AccelData::GenerateDomain();
2020
2021 return gGPUID;
2022}
2023
2024void SkGpuDevice::EXPERIMENTAL_optimize(SkPicture* picture) {
2025 SkPicture::AccelData::Key key = ComputeAccelDataKey();
2026
2027 GPUAccelData* data = SkNEW_ARGS(GPUAccelData, (key));
2028
2029 picture->EXPERIMENTAL_addAccelData(data);
2030}
2031
2032bool SkGpuDevice::EXPERIMENTAL_drawPicture(const SkPicture& picture) {
2033 SkPicture::AccelData::Key key = ComputeAccelDataKey();
2034
2035 const SkPicture::AccelData* data = picture.EXPERIMENTAL_getAccelData(key);
2036 if (NULL == data) {
2037 return false;
2038 }
2039
2040#if 0
2041 const GPUAccelData *gpuData = static_cast<const GPUAccelData*>(data);
2042#endif
2043
2044 return false;
2045}