blob: 1e52d23fba19f5238636ba5f9c8246ab00843f33 [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
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000161SkGpuDevice* SkGpuDevice::Create(GrSurface* surface, unsigned flags) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000162 SkASSERT(NULL != surface);
163 if (NULL == surface->asRenderTarget() || NULL == surface->getContext()) {
164 return NULL;
165 }
166 if (surface->asTexture()) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000167 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asTexture(), flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000168 } else {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000169 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asRenderTarget(), flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000170 }
171}
172
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000173SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture, unsigned flags)
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000174 : SkBitmapDevice(make_bitmap(context, texture->asRenderTarget())) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000175 this->initFromRenderTarget(context, texture->asRenderTarget(), flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000176}
177
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000178SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget, unsigned flags)
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000179 : SkBitmapDevice(make_bitmap(context, renderTarget)) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000180 this->initFromRenderTarget(context, renderTarget, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000181}
182
183void SkGpuDevice::initFromRenderTarget(GrContext* context,
184 GrRenderTarget* renderTarget,
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000185 unsigned flags) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000186 fDrawProcs = NULL;
187
188 fContext = context;
189 fContext->ref();
190
commit-bot@chromium.org47841822014-03-27 14:19:17 +0000191 fMainTextContext = SkNEW_ARGS(GrDistanceFieldTextContext, (fContext, fLeakyProperties));
192 fFallbackTextContext = SkNEW_ARGS(GrBitmapTextContext, (fContext, fLeakyProperties));
193
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000194 fRenderTarget = NULL;
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000195 fNeedClear = flags & kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000196
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);
bungeman@google.coma95a0662014-03-19 23:26:50 +0000212 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (info, surface, SkToBool(flags & kCached_Flag)));
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.org47841822014-03-27 14:19:17 +0000271 fMainTextContext = SkNEW_ARGS(GrDistanceFieldTextContext, (fContext, fLeakyProperties));
272 fFallbackTextContext = SkNEW_ARGS(GrBitmapTextContext, (fContext, fLeakyProperties));
273
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 +0000346bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
347 int x, int y) {
348 DO_DEFERRED_CLEAR();
349
350 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
351 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo.colorType(), dstInfo.alphaType());
352 if (kUnknown_GrPixelConfig == config) {
353 return false;
354 }
355
356 uint32_t flags = 0;
357 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
358 flags = GrContext::kUnpremul_PixelOpsFlag;
359 }
360 return fContext->readRenderTargetPixels(fRenderTarget, x, y, dstInfo.width(), dstInfo.height(),
361 config, dstPixels, dstRowBytes, flags);
362}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000363
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000364bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
365 int x, int y) {
366 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
367 GrPixelConfig config = SkImageInfo2GrPixelConfig(info.colorType(), info.alphaType());
368 if (kUnknown_GrPixelConfig == config) {
369 return false;
370 }
371 uint32_t flags = 0;
372 if (kUnpremul_SkAlphaType == info.alphaType()) {
373 flags = GrContext::kUnpremul_PixelOpsFlag;
374 }
375 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
376
377 // need to bump our genID for compatibility with clients that "know" we have a bitmap
378 this->onAccessBitmap().notifyPixelsChanged();
379
380 return true;
381}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000382
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000383const SkBitmap& SkGpuDevice::onAccessBitmap() {
384 DO_DEFERRED_CLEAR();
385 return INHERITED::onAccessBitmap();
386}
387
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000388void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
389 INHERITED::onAttachToCanvas(canvas);
390
391 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
392 fClipData.fClipStack = canvas->getClipStack();
393}
394
395void SkGpuDevice::onDetachFromCanvas() {
396 INHERITED::onDetachFromCanvas();
397 fClipData.fClipStack = NULL;
398}
399
400// call this every draw call, to ensure that the context reflects our state,
401// and not the state from some other canvas/device
402void SkGpuDevice::prepareDraw(const SkDraw& draw, bool forceIdentity) {
403 SkASSERT(NULL != fClipData.fClipStack);
404
405 fContext->setRenderTarget(fRenderTarget);
406
407 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
408
409 if (forceIdentity) {
410 fContext->setIdentityMatrix();
411 } else {
412 fContext->setMatrix(*draw.fMatrix);
413 }
414 fClipData.fOrigin = this->getOrigin();
415
416 fContext->setClip(&fClipData);
417
418 DO_DEFERRED_CLEAR();
419}
420
421GrRenderTarget* SkGpuDevice::accessRenderTarget() {
422 DO_DEFERRED_CLEAR();
423 return fRenderTarget;
424}
425
426///////////////////////////////////////////////////////////////////////////////
427
428SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
429SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
430SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
431SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
432SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
433 shader_type_mismatch);
434SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
435 shader_type_mismatch);
436SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
437SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
438
439namespace {
440
441// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
442// justAlpha indicates that skPaint's alpha should be used rather than the color
443// Callers may subsequently modify the GrPaint. Setting constantColor indicates
444// that the final paint will draw the same color at every pixel. This allows
445// an optimization where the the color filter can be applied to the skPaint's
446// color once while converting to GrPaint and then ignored.
447inline bool skPaint2GrPaintNoShader(SkGpuDevice* dev,
448 const SkPaint& skPaint,
449 bool justAlpha,
450 bool constantColor,
451 GrPaint* grPaint) {
452
453 grPaint->setDither(skPaint.isDither());
454 grPaint->setAntiAlias(skPaint.isAntiAlias());
455
456 SkXfermode::Coeff sm;
457 SkXfermode::Coeff dm;
458
459 SkXfermode* mode = skPaint.getXfermode();
460 GrEffectRef* xferEffect = NULL;
461 if (SkXfermode::AsNewEffectOrCoeff(mode, &xferEffect, &sm, &dm)) {
462 if (NULL != xferEffect) {
463 grPaint->addColorEffect(xferEffect)->unref();
464 sm = SkXfermode::kOne_Coeff;
465 dm = SkXfermode::kZero_Coeff;
466 }
467 } else {
468 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
469#if 0
470 return false;
471#else
472 // Fall back to src-over
473 sm = SkXfermode::kOne_Coeff;
474 dm = SkXfermode::kISA_Coeff;
475#endif
476 }
477 grPaint->setBlendFunc(sk_blend_to_grblend(sm), sk_blend_to_grblend(dm));
478
479 if (justAlpha) {
480 uint8_t alpha = skPaint.getAlpha();
481 grPaint->setColor(GrColorPackRGBA(alpha, alpha, alpha, alpha));
482 // justAlpha is currently set to true only if there is a texture,
483 // so constantColor should not also be true.
484 SkASSERT(!constantColor);
485 } else {
486 grPaint->setColor(SkColor2GrColor(skPaint.getColor()));
487 }
488
489 SkColorFilter* colorFilter = skPaint.getColorFilter();
490 if (NULL != colorFilter) {
491 // if the source color is a constant then apply the filter here once rather than per pixel
492 // in a shader.
493 if (constantColor) {
494 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
495 grPaint->setColor(SkColor2GrColor(filtered));
496 } else {
497 SkAutoTUnref<GrEffectRef> effect(colorFilter->asNewEffect(dev->context()));
498 if (NULL != effect.get()) {
499 grPaint->addColorEffect(effect);
500 }
501 }
502 }
503
504 return true;
505}
506
507// This function is similar to skPaint2GrPaintNoShader but also converts
508// skPaint's shader to a GrTexture/GrEffectStage if possible. The texture to
509// be used is set on grPaint and returned in param act. constantColor has the
510// same meaning as in skPaint2GrPaintNoShader.
511inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
512 const SkPaint& skPaint,
513 bool constantColor,
514 GrPaint* grPaint) {
515 SkShader* shader = skPaint.getShader();
516 if (NULL == shader) {
517 return skPaint2GrPaintNoShader(dev, skPaint, false, constantColor, grPaint);
518 }
519
commit-bot@chromium.org60770572014-01-13 15:57:05 +0000520 // SkShader::asNewEffect() may do offscreen rendering. Setup default drawing state and require
521 // the shader to set a render target .
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000522 GrContext::AutoWideOpenIdentityDraw awo(dev->context(), NULL);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000523
524 // setup the shader as the first color effect on the paint
525 SkAutoTUnref<GrEffectRef> effect(shader->asNewEffect(dev->context(), skPaint));
526 if (NULL != effect.get()) {
527 grPaint->addColorEffect(effect);
528 // Now setup the rest of the paint.
529 return skPaint2GrPaintNoShader(dev, skPaint, true, false, grPaint);
530 } else {
531 // We still don't have SkColorShader::asNewEffect() implemented.
532 SkShader::GradientInfo info;
533 SkColor color;
534
535 info.fColors = &color;
536 info.fColorOffsets = NULL;
537 info.fColorCount = 1;
538 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
539 SkPaint copy(skPaint);
540 copy.setShader(NULL);
541 // modulate the paint alpha by the shader's solid color alpha
542 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
543 copy.setColor(SkColorSetA(color, newA));
544 return skPaint2GrPaintNoShader(dev, copy, false, constantColor, grPaint);
545 } else {
546 return false;
547 }
548 }
549}
550}
551
552///////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000553
554SkBitmap::Config SkGpuDevice::config() const {
555 if (NULL == fRenderTarget) {
556 return SkBitmap::kNo_Config;
557 }
558
559 bool isOpaque;
560 return grConfig2skConfig(fRenderTarget->config(), &isOpaque);
561}
562
563void SkGpuDevice::clear(SkColor color) {
564 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
565 fContext->clear(&rect, SkColor2GrColor(color), true, fRenderTarget);
566 fNeedClear = false;
567}
568
569void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
570 CHECK_SHOULD_DRAW(draw, false);
571
572 GrPaint grPaint;
573 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
574 return;
575 }
576
577 fContext->drawPaint(grPaint);
578}
579
580// must be in SkCanvas::PointMode order
581static const GrPrimitiveType gPointMode2PrimtiveType[] = {
582 kPoints_GrPrimitiveType,
583 kLines_GrPrimitiveType,
584 kLineStrip_GrPrimitiveType
585};
586
587void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
588 size_t count, const SkPoint pts[], const SkPaint& paint) {
589 CHECK_FOR_ANNOTATION(paint);
590 CHECK_SHOULD_DRAW(draw, false);
591
592 SkScalar width = paint.getStrokeWidth();
593 if (width < 0) {
594 return;
595 }
596
597 // we only handle hairlines and paints without path effects or mask filters,
598 // else we let the SkDraw call our drawPath()
599 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
600 draw.drawPoints(mode, count, pts, paint, true);
601 return;
602 }
603
604 GrPaint grPaint;
605 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
606 return;
607 }
608
609 fContext->drawVertices(grPaint,
610 gPointMode2PrimtiveType[mode],
robertphillips@google.coma4662862013-11-21 14:24:16 +0000611 SkToS32(count),
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000612 (SkPoint*)pts,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000613 NULL,
614 NULL,
615 NULL,
616 0);
617}
618
619///////////////////////////////////////////////////////////////////////////////
620
621void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
622 const SkPaint& paint) {
623 CHECK_FOR_ANNOTATION(paint);
624 CHECK_SHOULD_DRAW(draw, false);
625
626 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
627 SkScalar width = paint.getStrokeWidth();
628
629 /*
630 We have special code for hairline strokes, miter-strokes, bevel-stroke
631 and fills. Anything else we just call our path code.
632 */
633 bool usePath = doStroke && width > 0 &&
634 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
635 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
636 // another two reasons we might need to call drawPath...
637 if (paint.getMaskFilter() || paint.getPathEffect()) {
638 usePath = true;
639 }
640 if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) {
641#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
642 if (doStroke) {
643#endif
644 usePath = true;
645#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
646 } else {
647 usePath = !fContext->getMatrix().preservesRightAngles();
648 }
649#endif
650 }
651 // until we can both stroke and fill rectangles
652 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
653 usePath = true;
654 }
655
656 if (usePath) {
657 SkPath path;
658 path.addRect(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 if (!doStroke) {
669 fContext->drawRect(grPaint, rect);
670 } else {
671 SkStrokeRec stroke(paint);
672 fContext->drawRect(grPaint, rect, &stroke);
673 }
674}
675
676///////////////////////////////////////////////////////////////////////////////
677
678void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
679 const SkPaint& paint) {
680 CHECK_FOR_ANNOTATION(paint);
681 CHECK_SHOULD_DRAW(draw, false);
682
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000683 GrPaint grPaint;
684 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
685 return;
686 }
687
688 SkStrokeRec stroke(paint);
689 if (paint.getMaskFilter()) {
690 // try to hit the fast path for drawing filtered round rects
691
692 SkRRect devRRect;
693 if (rect.transform(fContext->getMatrix(), &devRRect)) {
694 if (devRRect.allCornersCircular()) {
695 SkRect maskRect;
696 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(),
697 draw.fClip->getBounds(),
698 fContext->getMatrix(),
699 &maskRect)) {
700 SkIRect finalIRect;
701 maskRect.roundOut(&finalIRect);
702 if (draw.fClip->quickReject(finalIRect)) {
703 // clipped out
704 return;
705 }
706 if (NULL != draw.fBounder && !draw.fBounder->doIRect(finalIRect)) {
707 // nothing to draw
708 return;
709 }
710 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext, &grPaint,
711 stroke, devRRect)) {
712 return;
713 }
714 }
715
716 }
717 }
718
719 }
720
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000721 bool usePath = !rect.isSimple();
722 // another two reasons we might need to call drawPath...
723 if (paint.getMaskFilter() || paint.getPathEffect()) {
724 usePath = true;
725 }
726 // until we can rotate rrects...
727 if (!usePath && !fContext->getMatrix().rectStaysRect()) {
728 usePath = true;
729 }
730
731 if (usePath) {
732 SkPath path;
733 path.addRRect(rect);
734 this->drawPath(draw, path, paint, NULL, true);
735 return;
736 }
737
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000738 fContext->drawRRect(grPaint, rect, stroke);
739}
740
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000741/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000742
743void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
744 const SkPaint& paint) {
745 CHECK_FOR_ANNOTATION(paint);
746 CHECK_SHOULD_DRAW(draw, false);
747
748 bool usePath = false;
749 // some basic reasons we might need to call drawPath...
750 if (paint.getMaskFilter() || paint.getPathEffect()) {
751 usePath = true;
752 }
753
754 if (usePath) {
755 SkPath path;
756 path.addOval(oval);
757 this->drawPath(draw, path, paint, NULL, true);
758 return;
759 }
760
761 GrPaint grPaint;
762 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
763 return;
764 }
765 SkStrokeRec stroke(paint);
766
767 fContext->drawOval(grPaint, oval, stroke);
768}
769
770#include "SkMaskFilter.h"
771#include "SkBounder.h"
772
773///////////////////////////////////////////////////////////////////////////////
774
775// helpers for applying mask filters
776namespace {
777
778// Draw a mask using the supplied paint. Since the coverage/geometry
779// is already burnt into the mask this boils down to a rect draw.
780// Return true if the mask was successfully drawn.
781bool draw_mask(GrContext* context, const SkRect& maskRect,
782 GrPaint* grp, GrTexture* mask) {
783 GrContext::AutoMatrix am;
784 if (!am.setIdentity(context, grp)) {
785 return false;
786 }
787
788 SkMatrix matrix;
789 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
790 matrix.postIDiv(mask->width(), mask->height());
791
792 grp->addCoverageEffect(GrSimpleTextureEffect::Create(mask, matrix))->unref();
793 context->drawRect(*grp, maskRect);
794 return true;
795}
796
797bool draw_with_mask_filter(GrContext* context, const SkPath& devPath,
798 SkMaskFilter* filter, const SkRegion& clip, SkBounder* bounder,
799 GrPaint* grp, SkPaint::Style style) {
800 SkMask srcM, dstM;
801
802 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
803 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
804 return false;
805 }
806 SkAutoMaskFreeImage autoSrc(srcM.fImage);
807
808 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
809 return false;
810 }
811 // this will free-up dstM when we're done (allocated in filterMask())
812 SkAutoMaskFreeImage autoDst(dstM.fImage);
813
814 if (clip.quickReject(dstM.fBounds)) {
815 return false;
816 }
817 if (bounder && !bounder->doIRect(dstM.fBounds)) {
818 return false;
819 }
820
821 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
822 // the current clip (and identity matrix) and GrPaint settings
823 GrTextureDesc desc;
824 desc.fWidth = dstM.fBounds.width();
825 desc.fHeight = dstM.fBounds.height();
826 desc.fConfig = kAlpha_8_GrPixelConfig;
827
828 GrAutoScratchTexture ast(context, desc);
829 GrTexture* texture = ast.texture();
830
831 if (NULL == texture) {
832 return false;
833 }
834 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
835 dstM.fImage, dstM.fRowBytes);
836
837 SkRect maskRect = SkRect::Make(dstM.fBounds);
838
839 return draw_mask(context, maskRect, grp, texture);
840}
841
842// Create a mask of 'devPath' and place the result in 'mask'. Return true on
843// success; false otherwise.
844bool create_mask_GPU(GrContext* context,
845 const SkRect& maskRect,
846 const SkPath& devPath,
847 const SkStrokeRec& stroke,
848 bool doAA,
849 GrAutoScratchTexture* mask) {
850 GrTextureDesc desc;
851 desc.fFlags = kRenderTarget_GrTextureFlagBit;
852 desc.fWidth = SkScalarCeilToInt(maskRect.width());
853 desc.fHeight = SkScalarCeilToInt(maskRect.height());
854 // We actually only need A8, but it often isn't supported as a
855 // render target so default to RGBA_8888
856 desc.fConfig = kRGBA_8888_GrPixelConfig;
857 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
858 desc.fConfig = kAlpha_8_GrPixelConfig;
859 }
860
861 mask->set(context, desc);
862 if (NULL == mask->texture()) {
863 return false;
864 }
865
866 GrTexture* maskTexture = mask->texture();
867 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
868
869 GrContext::AutoRenderTarget art(context, maskTexture->asRenderTarget());
870 GrContext::AutoClip ac(context, clipRect);
871
872 context->clear(NULL, 0x0, true);
873
874 GrPaint tempPaint;
875 if (doAA) {
876 tempPaint.setAntiAlias(true);
877 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
878 // blend coeff of zero requires dual source blending support in order
879 // to properly blend partially covered pixels. This means the AA
880 // code path may not be taken. So we use a dst blend coeff of ISA. We
881 // could special case AA draws to a dst surface with known alpha=0 to
882 // use a zero dst coeff when dual source blending isn't available.
883 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
884 }
885
886 GrContext::AutoMatrix am;
887
888 // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint.
889 SkMatrix translate;
890 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
891 am.set(context, translate);
892 context->drawPath(tempPaint, devPath, stroke);
893 return true;
894}
895
896SkBitmap wrap_texture(GrTexture* texture) {
reed@google.combf790232013-12-13 19:45:58 +0000897 SkImageInfo info;
898 texture->asImageInfo(&info);
899
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000900 SkBitmap result;
reed@google.combf790232013-12-13 19:45:58 +0000901 result.setConfig(info);
902 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000903 return result;
904}
905
906};
907
908void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
909 const SkPaint& paint, const SkMatrix* prePathMatrix,
910 bool pathIsMutable) {
911 CHECK_FOR_ANNOTATION(paint);
912 CHECK_SHOULD_DRAW(draw, false);
913
914 GrPaint grPaint;
915 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
916 return;
917 }
918
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000919 // If we have a prematrix, apply it to the path, optimizing for the case
920 // where the original path can in fact be modified in place (even though
921 // its parameter type is const).
922 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000923 SkTLazy<SkPath> tmpPath;
924 SkTLazy<SkPath> effectPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000925
926 if (prePathMatrix) {
927 SkPath* result = pathPtr;
928
929 if (!pathIsMutable) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000930 result = tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000931 pathIsMutable = true;
932 }
933 // should I push prePathMatrix on our MV stack temporarily, instead
934 // of applying it here? See SkDraw.cpp
935 pathPtr->transform(*prePathMatrix, result);
936 pathPtr = result;
937 }
938 // at this point we're done with prePathMatrix
939 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
940
941 SkStrokeRec stroke(paint);
942 SkPathEffect* pathEffect = paint.getPathEffect();
943 const SkRect* cullRect = NULL; // TODO: what is our bounds?
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000944 if (pathEffect && pathEffect->filterPath(effectPath.init(), *pathPtr, &stroke,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000945 cullRect)) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000946 pathPtr = effectPath.get();
947 pathIsMutable = true;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000948 }
949
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000950 if (paint.getMaskFilter()) {
951 if (!stroke.isHairlineStyle()) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000952 SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init();
953 if (stroke.applyToPath(strokedPath, *pathPtr)) {
954 pathPtr = strokedPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000955 pathIsMutable = true;
956 stroke.setFillStyle();
957 }
958 }
959
960 // avoid possibly allocating a new path in transform if we can
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000961 SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000962
963 // transform the path into device space
964 pathPtr->transform(fContext->getMatrix(), devPathPtr);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000965
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000966 SkRect maskRect;
967 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
968 draw.fClip->getBounds(),
969 fContext->getMatrix(),
970 &maskRect)) {
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000971 // The context's matrix may change while creating the mask, so save the CTM here to
972 // pass to filterMaskGPU.
973 const SkMatrix ctm = fContext->getMatrix();
974
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000975 SkIRect finalIRect;
976 maskRect.roundOut(&finalIRect);
977 if (draw.fClip->quickReject(finalIRect)) {
978 // clipped out
979 return;
980 }
981 if (NULL != draw.fBounder && !draw.fBounder->doIRect(finalIRect)) {
982 // nothing to draw
983 return;
984 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000985
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000986 if (paint.getMaskFilter()->directFilterMaskGPU(fContext, &grPaint,
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000987 stroke, *devPathPtr)) {
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000988 // the mask filter was able to draw itself directly, so there's nothing
989 // left to do.
990 return;
991 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000992
993 GrAutoScratchTexture mask;
994
995 if (create_mask_GPU(fContext, maskRect, *devPathPtr, stroke,
996 grPaint.isAntiAlias(), &mask)) {
997 GrTexture* filtered;
998
commit-bot@chromium.org41bf9302014-01-08 22:25:53 +0000999 if (paint.getMaskFilter()->filterMaskGPU(mask.texture(),
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +00001000 ctm, maskRect, &filtered, true)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001001 // filterMaskGPU gives us ownership of a ref to the result
1002 SkAutoTUnref<GrTexture> atu(filtered);
1003
1004 // If the scratch texture that we used as the filter src also holds the filter
1005 // result then we must detach so that this texture isn't recycled for a later
1006 // draw.
1007 if (filtered == mask.texture()) {
1008 mask.detach();
1009 filtered->unref(); // detach transfers GrAutoScratchTexture's ref to us.
1010 }
1011
1012 if (draw_mask(fContext, maskRect, &grPaint, filtered)) {
1013 // This path is completely drawn
1014 return;
1015 }
1016 }
1017 }
1018 }
1019
1020 // draw the mask on the CPU - this is a fallthrough path in case the
1021 // GPU path fails
1022 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
1023 SkPaint::kFill_Style;
1024 draw_with_mask_filter(fContext, *devPathPtr, paint.getMaskFilter(),
1025 *draw.fClip, draw.fBounder, &grPaint, style);
1026 return;
1027 }
1028
1029 fContext->drawPath(grPaint, *pathPtr, stroke);
1030}
1031
1032static const int kBmpSmallTileSize = 1 << 10;
1033
1034static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
1035 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
1036 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
1037 return tilesX * tilesY;
1038}
1039
1040static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
1041 if (maxTileSize <= kBmpSmallTileSize) {
1042 return maxTileSize;
1043 }
1044
1045 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
1046 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
1047
1048 maxTileTotalTileSize *= maxTileSize * maxTileSize;
1049 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
1050
1051 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
1052 return kBmpSmallTileSize;
1053 } else {
1054 return maxTileSize;
1055 }
1056}
1057
1058// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
1059// pixels from the bitmap are necessary.
1060static void determine_clipped_src_rect(const GrContext* context,
1061 const SkBitmap& bitmap,
1062 const SkRect* srcRectPtr,
1063 SkIRect* clippedSrcIRect) {
1064 const GrClipData* clip = context->getClip();
1065 clip->getConservativeBounds(context->getRenderTarget(), clippedSrcIRect, NULL);
1066 SkMatrix inv;
1067 if (!context->getMatrix().invert(&inv)) {
1068 clippedSrcIRect->setEmpty();
1069 return;
1070 }
1071 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
1072 inv.mapRect(&clippedSrcRect);
1073 if (NULL != srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001074 // we've setup src space 0,0 to map to the top left of the src rect.
1075 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001076 if (!clippedSrcRect.intersect(*srcRectPtr)) {
1077 clippedSrcIRect->setEmpty();
1078 return;
1079 }
1080 }
1081 clippedSrcRect.roundOut(clippedSrcIRect);
1082 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1083 if (!clippedSrcIRect->intersect(bmpBounds)) {
1084 clippedSrcIRect->setEmpty();
1085 }
1086}
1087
1088bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
1089 const GrTextureParams& params,
1090 const SkRect* srcRectPtr,
1091 int maxTileSize,
1092 int* tileSize,
1093 SkIRect* clippedSrcRect) const {
1094 // if bitmap is explictly texture backed then just use the texture
1095 if (NULL != bitmap.getTexture()) {
1096 return false;
1097 }
1098
1099 // if it's larger than the max tile size, then we have no choice but tiling.
1100 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
1101 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
1102 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
1103 return true;
1104 }
1105
1106 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
1107 return false;
1108 }
1109
1110 // if the entire texture is already in our cache then no reason to tile it
1111 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
1112 return false;
1113 }
1114
1115 // At this point we know we could do the draw by uploading the entire bitmap
1116 // as a texture. However, if the texture would be large compared to the
1117 // cache size and we don't require most of it for this draw then tile to
1118 // reduce the amount of upload and cache spill.
1119
1120 // assumption here is that sw bitmap size is a good proxy for its size as
1121 // a texture
1122 size_t bmpSize = bitmap.getSize();
1123 size_t cacheSize;
1124 fContext->getTextureCacheLimits(NULL, &cacheSize);
1125 if (bmpSize < cacheSize / 2) {
1126 return false;
1127 }
1128
1129 // Figure out how much of the src we will need based on the src rect and clipping.
1130 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
1131 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
1132 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
1133 kBmpSmallTileSize * kBmpSmallTileSize;
1134
1135 return usedTileBytes < 2 * bmpSize;
1136}
1137
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001138void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001139 const SkBitmap& bitmap,
1140 const SkMatrix& m,
1141 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001142 SkMatrix concat;
1143 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1144 if (!m.isIdentity()) {
1145 concat.setConcat(*draw->fMatrix, m);
1146 draw.writable()->fMatrix = &concat;
1147 }
1148 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001149}
1150
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001151// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001152// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
1153// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001154static inline void clamped_outset_with_offset(SkIRect* iRect,
1155 int outset,
1156 SkPoint* offset,
1157 const SkIRect& clamp) {
1158 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001159
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001160 int leftClampDelta = clamp.fLeft - iRect->fLeft;
1161 if (leftClampDelta > 0) {
1162 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001163 iRect->fLeft = clamp.fLeft;
1164 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001165 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001166 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001167
1168 int topClampDelta = clamp.fTop - iRect->fTop;
1169 if (topClampDelta > 0) {
1170 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001171 iRect->fTop = clamp.fTop;
1172 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001173 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001174 }
1175
1176 if (iRect->fRight > clamp.fRight) {
1177 iRect->fRight = clamp.fRight;
1178 }
1179 if (iRect->fBottom > clamp.fBottom) {
1180 iRect->fBottom = clamp.fBottom;
1181 }
1182}
1183
1184void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1185 const SkBitmap& bitmap,
1186 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001187 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001188 const SkPaint& paint,
1189 SkCanvas::DrawBitmapRectFlags flags) {
1190 CHECK_SHOULD_DRAW(draw, false);
1191
1192 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001193 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001194 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
1195 // in the (easier) bleed case, so update flags.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001196 if (NULL == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001197 SkScalar w = SkIntToScalar(bitmap.width());
1198 SkScalar h = SkIntToScalar(bitmap.height());
1199 dstSize.fWidth = w;
1200 dstSize.fHeight = h;
1201 srcRect.set(0, 0, w, h);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001202 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001203 } else {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001204 SkASSERT(NULL != dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001205 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001206 dstSize = *dstSizePtr;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001207 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
1208 srcRect.fRight >= bitmap.width() && srcRect.fBottom >= bitmap.height()) {
1209 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
1210 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001211 }
1212
1213 if (paint.getMaskFilter()){
1214 // Convert the bitmap to a shader so that the rect can be drawn
1215 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001216 SkBitmap tmp; // subset of bitmap, if necessary
1217 const SkBitmap* bitmapPtr = &bitmap;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001218 SkMatrix localM;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001219 if (NULL != srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001220 localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
1221 localM.postScale(dstSize.fWidth / srcRectPtr->width(),
1222 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001223 // In bleed mode we position and trim the bitmap based on the src rect which is
1224 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
1225 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
1226 // compensate.
1227 if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) {
1228 SkIRect iSrc;
1229 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001230
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001231 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
1232 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001233
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001234 if (!bitmap.extractSubset(&tmp, iSrc)) {
1235 return; // extraction failed
1236 }
1237 bitmapPtr = &tmp;
1238 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001239
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001240 // The source rect has changed so update the matrix
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001241 localM.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001242 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001243 } else {
1244 localM.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001245 }
1246
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001247 SkPaint paintWithShader(paint);
1248 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001249 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001250 paintWithShader.getShader()->setLocalMatrix(localM);
1251 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1252 this->drawRect(draw, dstRect, paintWithShader);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001253
1254 return;
1255 }
1256
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001257 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
1258 // the view matrix rather than a local matrix.
1259 SkMatrix m;
1260 m.setScale(dstSize.fWidth / srcRect.width(),
1261 dstSize.fHeight / srcRect.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001262 fContext->concatMatrix(m);
1263
1264 GrTextureParams params;
1265 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel();
1266 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001267
1268 int tileFilterPad;
1269 bool doBicubic = false;
1270
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001271 switch(paintFilterLevel) {
1272 case SkPaint::kNone_FilterLevel:
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001273 tileFilterPad = 0;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001274 textureFilterMode = GrTextureParams::kNone_FilterMode;
1275 break;
1276 case SkPaint::kLow_FilterLevel:
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001277 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001278 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1279 break;
1280 case SkPaint::kMedium_FilterLevel:
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001281 tileFilterPad = 1;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001282 if (fContext->getMatrix().getMinStretch() < SK_Scalar1) {
1283 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1284 } else {
1285 // Don't trigger MIP level generation unnecessarily.
1286 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1287 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001288 break;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001289 case SkPaint::kHigh_FilterLevel:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001290 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001291 if (fContext->getMatrix().getMinStretch() >= SK_Scalar1) {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001292 // We will install an effect that does the filtering in the shader.
1293 textureFilterMode = GrTextureParams::kNone_FilterMode;
1294 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1295 doBicubic = true;
1296 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001297 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1298 tileFilterPad = 1;
1299 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001300 break;
1301 default:
1302 SkErrorInternals::SetError( kInvalidPaint_SkError,
1303 "Sorry, I don't understand the filtering "
1304 "mode you asked for. Falling back to "
1305 "MIPMaps.");
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001306 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001307 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1308 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001309 }
1310
1311 params.setFilterMode(textureFilterMode);
1312
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001313 int maxTileSize = fContext->getMaxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001314 int tileSize;
1315
1316 SkIRect clippedSrcRect;
1317 if (this->shouldTileBitmap(bitmap, params, srcRectPtr, maxTileSize, &tileSize,
1318 &clippedSrcRect)) {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001319 this->drawTiledBitmap(bitmap, srcRect, clippedSrcRect, params, paint, flags, tileSize,
1320 doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001321 } else {
1322 // take the simple case
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001323 this->internalDrawBitmap(bitmap, srcRect, params, paint, flags, doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001324 }
1325}
1326
1327// Break 'bitmap' into several tiles to draw it since it has already
1328// been determined to be too large to fit in VRAM
1329void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
1330 const SkRect& srcRect,
1331 const SkIRect& clippedSrcIRect,
1332 const GrTextureParams& params,
1333 const SkPaint& paint,
1334 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001335 int tileSize,
1336 bool bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001337 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1338
1339 int nx = bitmap.width() / tileSize;
1340 int ny = bitmap.height() / tileSize;
1341 for (int x = 0; x <= nx; x++) {
1342 for (int y = 0; y <= ny; y++) {
1343 SkRect tileR;
1344 tileR.set(SkIntToScalar(x * tileSize),
1345 SkIntToScalar(y * tileSize),
1346 SkIntToScalar((x + 1) * tileSize),
1347 SkIntToScalar((y + 1) * tileSize));
1348
1349 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1350 continue;
1351 }
1352
1353 if (!tileR.intersect(srcRect)) {
1354 continue;
1355 }
1356
1357 SkBitmap tmpB;
1358 SkIRect iTileR;
1359 tileR.roundOut(&iTileR);
1360 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1361 SkIntToScalar(iTileR.fTop));
1362
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001363 // Adjust the context matrix to draw at the right x,y in device space
1364 SkMatrix tmpM;
1365 GrContext::AutoMatrix am;
1366 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
1367 am.setPreConcat(fContext, tmpM);
1368
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001369 if (SkPaint::kNone_FilterLevel != paint.getFilterLevel() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001370 SkIRect iClampRect;
1371
1372 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1373 // In bleed mode we want to always expand the tile on all edges
1374 // but stay within the bitmap bounds
1375 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1376 } else {
1377 // In texture-domain/clamp mode we only want to expand the
1378 // tile on edges interior to "srcRect" (i.e., we want to
1379 // not bleed across the original clamped edges)
1380 srcRect.roundOut(&iClampRect);
1381 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001382 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1383 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001384 }
1385
1386 if (bitmap.extractSubset(&tmpB, iTileR)) {
1387 // now offset it to make it "local" to our tmp bitmap
1388 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001389
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001390 this->internalDrawBitmap(tmpB, tileR, params, paint, flags, bicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001391 }
1392 }
1393 }
1394}
1395
1396static bool has_aligned_samples(const SkRect& srcRect,
1397 const SkRect& transformedRect) {
1398 // detect pixel disalignment
1399 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1400 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1401 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1402 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1403 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1404 COLOR_BLEED_TOLERANCE &&
1405 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1406 COLOR_BLEED_TOLERANCE) {
1407 return true;
1408 }
1409 return false;
1410}
1411
1412static bool may_color_bleed(const SkRect& srcRect,
1413 const SkRect& transformedRect,
1414 const SkMatrix& m) {
1415 // Only gets called if has_aligned_samples returned false.
1416 // So we can assume that sampling is axis aligned but not texel aligned.
1417 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
1418 SkRect innerSrcRect(srcRect), innerTransformedRect,
1419 outerTransformedRect(transformedRect);
1420 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1421 m.mapRect(&innerTransformedRect, innerSrcRect);
1422
1423 // The gap between outerTransformedRect and innerTransformedRect
1424 // represents the projection of the source border area, which is
1425 // problematic for color bleeding. We must check whether any
1426 // destination pixels sample the border area.
1427 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1428 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1429 SkIRect outer, inner;
1430 outerTransformedRect.round(&outer);
1431 innerTransformedRect.round(&inner);
1432 // If the inner and outer rects round to the same result, it means the
1433 // border does not overlap any pixel centers. Yay!
1434 return inner != outer;
1435}
1436
1437
1438/*
1439 * This is called by drawBitmap(), which has to handle images that may be too
1440 * large to be represented by a single texture.
1441 *
1442 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1443 * and that non-texture portion of the GrPaint has already been setup.
1444 */
1445void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
1446 const SkRect& srcRect,
1447 const GrTextureParams& params,
1448 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001449 SkCanvas::DrawBitmapRectFlags flags,
1450 bool bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001451 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1452 bitmap.height() <= fContext->getMaxTextureSize());
1453
1454 GrTexture* texture;
1455 SkAutoCachedTexture act(this, bitmap, &params, &texture);
1456 if (NULL == texture) {
1457 return;
1458 }
1459
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001460 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001461 SkRect paintRect;
1462 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1463 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1464 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1465 SkScalarMul(srcRect.fTop, hInv),
1466 SkScalarMul(srcRect.fRight, wInv),
1467 SkScalarMul(srcRect.fBottom, hInv));
1468
1469 bool needsTextureDomain = false;
1470 if (!(flags & SkCanvas::kBleed_DrawBitmapRectFlag) &&
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001471 (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode)) {
1472 // Need texture domain if drawing a sub rect
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001473 needsTextureDomain = srcRect.width() < bitmap.width() ||
1474 srcRect.height() < bitmap.height();
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001475 if (!bicubic && needsTextureDomain && fContext->getMatrix().rectStaysRect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001476 const SkMatrix& matrix = fContext->getMatrix();
1477 // sampling is axis-aligned
1478 SkRect transformedRect;
1479 matrix.mapRect(&transformedRect, srcRect);
1480
1481 if (has_aligned_samples(srcRect, transformedRect)) {
1482 // We could also turn off filtering here (but we already did a cache lookup with
1483 // params).
1484 needsTextureDomain = false;
1485 } else {
1486 needsTextureDomain = may_color_bleed(srcRect, transformedRect, matrix);
1487 }
1488 }
1489 }
1490
1491 SkRect textureDomain = SkRect::MakeEmpty();
1492 SkAutoTUnref<GrEffectRef> effect;
1493 if (needsTextureDomain) {
1494 // Use a constrained texture domain to avoid color bleeding
1495 SkScalar left, top, right, bottom;
1496 if (srcRect.width() > SK_Scalar1) {
1497 SkScalar border = SK_ScalarHalf / texture->width();
1498 left = paintRect.left() + border;
1499 right = paintRect.right() - border;
1500 } else {
1501 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1502 }
1503 if (srcRect.height() > SK_Scalar1) {
1504 SkScalar border = SK_ScalarHalf / texture->height();
1505 top = paintRect.top() + border;
1506 bottom = paintRect.bottom() - border;
1507 } else {
1508 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1509 }
1510 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001511 if (bicubic) {
1512 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
1513 } else {
1514 effect.reset(GrTextureDomainEffect::Create(texture,
1515 SkMatrix::I(),
1516 textureDomain,
1517 GrTextureDomain::kClamp_Mode,
1518 params.filterMode()));
1519 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001520 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001521 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1522 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
1523 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001524 } else {
1525 effect.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
1526 }
1527
1528 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1529 // the rest from the SkPaint.
1530 GrPaint grPaint;
1531 grPaint.addColorEffect(effect);
1532 bool alphaOnly = !(SkBitmap::kA8_Config == bitmap.config());
1533 if (!skPaint2GrPaintNoShader(this, paint, alphaOnly, false, &grPaint)) {
1534 return;
1535 }
1536
1537 fContext->drawRectToRect(grPaint, dstRect, paintRect, NULL);
1538}
1539
1540static bool filter_texture(SkBaseDevice* device, GrContext* context,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001541 GrTexture* texture, const SkImageFilter* filter,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001542 int w, int h, const SkImageFilter::Context& ctx,
1543 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001544 SkASSERT(filter);
1545 SkDeviceImageFilterProxy proxy(device);
1546
1547 if (filter->canFilterImageGPU()) {
1548 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1549 // filter. Also set the clip wide open and the matrix to identity.
1550 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001551 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001552 } else {
1553 return false;
1554 }
1555}
1556
1557void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1558 int left, int top, const SkPaint& paint) {
1559 // drawSprite is defined to be in device coords.
1560 CHECK_SHOULD_DRAW(draw, true);
1561
1562 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1563 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1564 return;
1565 }
1566
1567 int w = bitmap.width();
1568 int h = bitmap.height();
1569
1570 GrTexture* texture;
1571 // draw sprite uses the default texture params
1572 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
1573
1574 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001575 // This bitmap will own the filtered result as a texture.
1576 SkBitmap filteredBitmap;
1577
1578 if (NULL != filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001579 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001580 SkMatrix matrix(*draw.fMatrix);
1581 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001582 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1583 SkImageFilter::Context ctx(matrix, clipBounds);
1584 if (filter_texture(this, fContext, texture, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001585 &offset)) {
1586 texture = (GrTexture*) filteredBitmap.getTexture();
1587 w = filteredBitmap.width();
1588 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001589 left += offset.x();
1590 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001591 } else {
1592 return;
1593 }
1594 }
1595
1596 GrPaint grPaint;
1597 grPaint.addColorTextureEffect(texture, SkMatrix::I());
1598
1599 if(!skPaint2GrPaintNoShader(this, paint, true, false, &grPaint)) {
1600 return;
1601 }
1602
1603 fContext->drawRectToRect(grPaint,
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001604 SkRect::MakeXYWH(SkIntToScalar(left),
1605 SkIntToScalar(top),
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001606 SkIntToScalar(w),
1607 SkIntToScalar(h)),
1608 SkRect::MakeXYWH(0,
1609 0,
1610 SK_Scalar1 * w / texture->width(),
1611 SK_Scalar1 * h / texture->height()));
1612}
1613
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001614void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001615 const SkRect* src, const SkRect& dst,
1616 const SkPaint& paint,
1617 SkCanvas::DrawBitmapRectFlags flags) {
1618 SkMatrix matrix;
1619 SkRect bitmapBounds, tmpSrc;
1620
1621 bitmapBounds.set(0, 0,
1622 SkIntToScalar(bitmap.width()),
1623 SkIntToScalar(bitmap.height()));
1624
1625 // Compute matrix from the two rectangles
1626 if (NULL != src) {
1627 tmpSrc = *src;
1628 } else {
1629 tmpSrc = bitmapBounds;
1630 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001631
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001632 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1633
1634 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
1635 if (NULL != src) {
1636 if (!bitmapBounds.contains(tmpSrc)) {
1637 if (!tmpSrc.intersect(bitmapBounds)) {
1638 return; // nothing to draw
1639 }
1640 }
1641 }
1642
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001643 SkRect tmpDst;
1644 matrix.mapRect(&tmpDst, tmpSrc);
1645
1646 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1647 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1648 // Translate so that tempDst's top left is at the origin.
1649 matrix = *origDraw.fMatrix;
1650 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1651 draw.writable()->fMatrix = &matrix;
1652 }
1653 SkSize dstSize;
1654 dstSize.fWidth = tmpDst.width();
1655 dstSize.fHeight = tmpDst.height();
1656
1657 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001658}
1659
1660void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1661 int x, int y, const SkPaint& paint) {
1662 // clear of the source device must occur before CHECK_SHOULD_DRAW
1663 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1664 if (dev->fNeedClear) {
1665 // TODO: could check here whether we really need to draw at all
1666 dev->clear(0x0);
1667 }
1668
1669 // drawDevice is defined to be in device coords.
1670 CHECK_SHOULD_DRAW(draw, true);
1671
1672 GrRenderTarget* devRT = dev->accessRenderTarget();
1673 GrTexture* devTex;
1674 if (NULL == (devTex = devRT->asTexture())) {
1675 return;
1676 }
1677
1678 const SkBitmap& bm = dev->accessBitmap(false);
1679 int w = bm.width();
1680 int h = bm.height();
1681
1682 SkImageFilter* filter = paint.getImageFilter();
1683 // This bitmap will own the filtered result as a texture.
1684 SkBitmap filteredBitmap;
1685
1686 if (NULL != filter) {
1687 SkIPoint offset = SkIPoint::Make(0, 0);
1688 SkMatrix matrix(*draw.fMatrix);
1689 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001690 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
1691 SkImageFilter::Context ctx(matrix, clipBounds);
1692 if (filter_texture(this, fContext, devTex, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001693 &offset)) {
1694 devTex = filteredBitmap.getTexture();
1695 w = filteredBitmap.width();
1696 h = filteredBitmap.height();
1697 x += offset.fX;
1698 y += offset.fY;
1699 } else {
1700 return;
1701 }
1702 }
1703
1704 GrPaint grPaint;
1705 grPaint.addColorTextureEffect(devTex, SkMatrix::I());
1706
1707 if (!skPaint2GrPaintNoShader(this, paint, true, false, &grPaint)) {
1708 return;
1709 }
1710
1711 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1712 SkIntToScalar(y),
1713 SkIntToScalar(w),
1714 SkIntToScalar(h));
1715
1716 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1717 // scratch texture).
1718 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1719 SK_Scalar1 * h / devTex->height());
1720
1721 fContext->drawRectToRect(grPaint, dstRect, srcRect);
1722}
1723
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001724bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001725 return filter->canFilterImageGPU();
1726}
1727
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001728bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001729 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001730 SkBitmap* result, SkIPoint* offset) {
1731 // want explicitly our impl, so guard against a subclass of us overriding it
1732 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1733 return false;
1734 }
1735
1736 SkAutoLockPixels alp(src, !src.getTexture());
1737 if (!src.getTexture() && !src.readyToDraw()) {
1738 return false;
1739 }
1740
1741 GrTexture* texture;
1742 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1743 // must be pushed upstack.
1744 SkAutoCachedTexture act(this, src, NULL, &texture);
1745
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001746 return filter_texture(this, fContext, texture, filter, src.width(), src.height(), ctx,
1747 result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001748}
1749
1750///////////////////////////////////////////////////////////////////////////////
1751
1752// must be in SkCanvas::VertexMode order
1753static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1754 kTriangles_GrPrimitiveType,
1755 kTriangleStrip_GrPrimitiveType,
1756 kTriangleFan_GrPrimitiveType,
1757};
1758
1759void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1760 int vertexCount, const SkPoint vertices[],
1761 const SkPoint texs[], const SkColor colors[],
1762 SkXfermode* xmode,
1763 const uint16_t indices[], int indexCount,
1764 const SkPaint& paint) {
1765 CHECK_SHOULD_DRAW(draw, false);
1766
1767 GrPaint grPaint;
1768 // we ignore the shader if texs is null.
1769 if (NULL == texs) {
1770 if (!skPaint2GrPaintNoShader(this, paint, false, NULL == colors, &grPaint)) {
1771 return;
1772 }
1773 } else {
1774 if (!skPaint2GrPaintShader(this, paint, NULL == colors, &grPaint)) {
1775 return;
1776 }
1777 }
1778
1779 if (NULL != xmode && NULL != texs && NULL != colors) {
1780 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1781 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1782#if 0
1783 return
1784#endif
1785 }
1786 }
1787
1788 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1789 if (NULL != colors) {
1790 // need to convert byte order and from non-PM to PM
1791 convertedColors.reset(vertexCount);
1792 for (int i = 0; i < vertexCount; ++i) {
1793 convertedColors[i] = SkColor2GrColor(colors[i]);
1794 }
1795 colors = convertedColors.get();
1796 }
1797 fContext->drawVertices(grPaint,
1798 gVertexMode2PrimitiveType[vmode],
1799 vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001800 vertices,
1801 texs,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001802 colors,
1803 indices,
1804 indexCount);
1805}
1806
1807///////////////////////////////////////////////////////////////////////////////
1808
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001809void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1810 size_t byteLength, SkScalar x, SkScalar y,
1811 const SkPaint& paint) {
1812 CHECK_SHOULD_DRAW(draw, false);
1813
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001814 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001815 GrPaint grPaint;
1816 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
1817 return;
1818 }
1819
1820 SkDEBUGCODE(this->validate();)
1821
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001822 fMainTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
1823 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001824 GrPaint grPaint;
1825 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
1826 return;
1827 }
1828
1829 SkDEBUGCODE(this->validate();)
1830
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001831 fFallbackTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001832 } else {
1833 // this guy will just call our drawPath()
1834 draw.drawText_asPaths((const char*)text, byteLength, x, y, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001835 }
1836}
1837
1838void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1839 size_t byteLength, const SkScalar pos[],
1840 SkScalar constY, int scalarsPerPos,
1841 const SkPaint& paint) {
1842 CHECK_SHOULD_DRAW(draw, false);
1843
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001844 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001845 GrPaint grPaint;
1846 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
1847 return;
1848 }
1849
1850 SkDEBUGCODE(this->validate();)
1851
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001852 fMainTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001853 constY, scalarsPerPos);
1854 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001855 GrPaint grPaint;
1856 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
1857 return;
1858 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001859
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001860 SkDEBUGCODE(this->validate();)
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001861
1862 fFallbackTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001863 constY, scalarsPerPos);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001864 } else {
1865 draw.drawPosText_asPaths((const char*)text, byteLength, pos, constY,
1866 scalarsPerPos, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001867 }
1868}
1869
1870void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1871 size_t len, const SkPath& path,
1872 const SkMatrix* m, const SkPaint& paint) {
1873 CHECK_SHOULD_DRAW(draw, false);
1874
1875 SkASSERT(draw.fDevice == this);
1876 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1877}
1878
1879///////////////////////////////////////////////////////////////////////////////
1880
1881bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1882 if (!paint.isLCDRenderText()) {
1883 // we're cool with the paint as is
1884 return false;
1885 }
1886
1887 if (paint.getShader() ||
1888 paint.getXfermode() || // unless its srcover
1889 paint.getMaskFilter() ||
1890 paint.getRasterizer() ||
1891 paint.getColorFilter() ||
1892 paint.getPathEffect() ||
1893 paint.isFakeBoldText() ||
1894 paint.getStyle() != SkPaint::kFill_Style) {
1895 // turn off lcd
1896 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1897 flags->fHinting = paint.getHinting();
1898 return true;
1899 }
1900 // we're cool with the paint as is
1901 return false;
1902}
1903
1904void SkGpuDevice::flush() {
1905 DO_DEFERRED_CLEAR();
1906 fContext->resolveRenderTarget(fRenderTarget);
1907}
1908
1909///////////////////////////////////////////////////////////////////////////////
1910
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001911SkBaseDevice* SkGpuDevice::onCreateDevice(const SkImageInfo& info, Usage usage) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001912 GrTextureDesc desc;
1913 desc.fConfig = fRenderTarget->config();
1914 desc.fFlags = kRenderTarget_GrTextureFlagBit;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001915 desc.fWidth = info.width();
1916 desc.fHeight = info.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001917 desc.fSampleCnt = fRenderTarget->numSamples();
1918
1919 SkAutoTUnref<GrTexture> texture;
1920 // Skia's convention is to only clear a device if it is non-opaque.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001921 unsigned flags = info.isOpaque() ? 0 : kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001922
1923#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1924 // layers are never draw in repeat modes, so we can request an approx
1925 // match and ignore any padding.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001926 flags |= kCached_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001927 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == usage) ?
1928 GrContext::kApprox_ScratchTexMatch :
1929 GrContext::kExact_ScratchTexMatch;
1930 texture.reset(fContext->lockAndRefScratchTexture(desc, match));
1931#else
1932 texture.reset(fContext->createUncachedTexture(desc, NULL, 0));
1933#endif
1934 if (NULL != texture.get()) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001935 return SkGpuDevice::Create(texture, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001936 } else {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001937 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1938 info.width(), info.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001939 return NULL;
1940 }
1941}
1942
reed@google.com76f10a32014-02-05 15:32:21 +00001943SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info) {
1944 return SkSurface::NewRenderTarget(fContext, info, fRenderTarget->numSamples());
1945}
1946
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001947class GPUAccelData : public SkPicture::AccelData {
1948public:
1949 GPUAccelData(Key key) : INHERITED(key) { }
1950
1951protected:
1952
1953private:
1954 typedef SkPicture::AccelData INHERITED;
1955};
1956
1957// In the future this may not be a static method if we need to incorporate the
1958// clip and matrix state into the key
1959SkPicture::AccelData::Key SkGpuDevice::ComputeAccelDataKey() {
1960 static const SkPicture::AccelData::Key gGPUID = SkPicture::AccelData::GenerateDomain();
1961
1962 return gGPUID;
1963}
1964
1965void SkGpuDevice::EXPERIMENTAL_optimize(SkPicture* picture) {
1966 SkPicture::AccelData::Key key = ComputeAccelDataKey();
1967
1968 GPUAccelData* data = SkNEW_ARGS(GPUAccelData, (key));
1969
1970 picture->EXPERIMENTAL_addAccelData(data);
1971}
1972
1973bool SkGpuDevice::EXPERIMENTAL_drawPicture(const SkPicture& picture) {
1974 SkPicture::AccelData::Key key = ComputeAccelDataKey();
1975
1976 const SkPicture::AccelData* data = picture.EXPERIMENTAL_getAccelData(key);
1977 if (NULL == data) {
1978 return false;
1979 }
1980
1981#if 0
1982 const GPUAccelData *gpuData = static_cast<const GPUAccelData*>(data);
1983#endif
1984
1985 return false;
1986}