blob: 8d3f75ade08cd9eb3ee8131422174087b1b26d27 [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.org628ed0b2014-05-19 14:32:49 +000011#include "effects/GrDashingEffect.h"
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000012#include "effects/GrTextureDomain.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000013#include "effects/GrSimpleTextureEffect.h"
14
15#include "GrContext.h"
16#include "GrBitmapTextContext.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000017#include "GrDistanceFieldTextContext.h"
robertphillips@google.come930a072014-04-03 00:34:27 +000018#include "GrLayerCache.h"
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +000019#include "GrPictureUtils.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000020
21#include "SkGrTexturePixelRef.h"
22
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000023#include "SkDeviceImageFilterProxy.h"
24#include "SkDrawProcs.h"
25#include "SkGlyphCache.h"
26#include "SkImageFilter.h"
commit-bot@chromium.org82139702014-03-10 22:53:20 +000027#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000028#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000029#include "SkPicture.h"
robertphillips@google.combeb1af22014-05-07 21:31:09 +000030#include "SkPicturePlayback.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000031#include "SkRRect.h"
32#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000033#include "SkSurface.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000034#include "SkTLazy.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000035#include "SkUtils.h"
commit-bot@chromium.org559a8832014-05-30 10:08:22 +000036#include "SkVertState.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000037#include "SkErrorInternals.h"
38
39#define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
40
41#if 0
42 extern bool (*gShouldDrawProc)();
43 #define CHECK_SHOULD_DRAW(draw, forceI) \
44 do { \
45 if (gShouldDrawProc && !gShouldDrawProc()) return; \
46 this->prepareDraw(draw, forceI); \
47 } while (0)
48#else
49 #define CHECK_SHOULD_DRAW(draw, forceI) this->prepareDraw(draw, forceI)
50#endif
51
52// This constant represents the screen alignment criterion in texels for
53// requiring texture domain clamping to prevent color bleeding when drawing
54// a sub region of a larger source image.
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000055#define COLOR_BLEED_TOLERANCE 0.001f
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000056
57#define DO_DEFERRED_CLEAR() \
58 do { \
59 if (fNeedClear) { \
60 this->clear(SK_ColorTRANSPARENT); \
61 } \
62 } while (false) \
63
64///////////////////////////////////////////////////////////////////////////////
65
66#define CHECK_FOR_ANNOTATION(paint) \
67 do { if (paint.getAnnotation()) { return; } } while (0)
68
69///////////////////////////////////////////////////////////////////////////////
70
71
72class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
73public:
74 SkAutoCachedTexture()
75 : fDevice(NULL)
76 , fTexture(NULL) {
77 }
78
79 SkAutoCachedTexture(SkGpuDevice* device,
80 const SkBitmap& bitmap,
81 const GrTextureParams* params,
82 GrTexture** texture)
83 : fDevice(NULL)
84 , fTexture(NULL) {
85 SkASSERT(NULL != texture);
86 *texture = this->set(device, bitmap, params);
87 }
88
89 ~SkAutoCachedTexture() {
90 if (NULL != fTexture) {
91 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
92 }
93 }
94
95 GrTexture* set(SkGpuDevice* device,
96 const SkBitmap& bitmap,
97 const GrTextureParams* params) {
98 if (NULL != fTexture) {
99 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
100 fTexture = NULL;
101 }
102 fDevice = device;
103 GrTexture* result = (GrTexture*)bitmap.getTexture();
104 if (NULL == result) {
105 // Cannot return the native texture so look it up in our cache
106 fTexture = GrLockAndRefCachedBitmapTexture(device->context(), bitmap, params);
107 result = fTexture;
108 }
109 return result;
110 }
111
112private:
113 SkGpuDevice* fDevice;
114 GrTexture* fTexture;
115};
116
117///////////////////////////////////////////////////////////////////////////////
118
119struct GrSkDrawProcs : public SkDrawProcs {
120public:
121 GrContext* fContext;
122 GrTextContext* fTextContext;
123 GrFontScaler* fFontScaler; // cached in the skia glyphcache
124};
125
126///////////////////////////////////////////////////////////////////////////////
127
128static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
129 switch (config) {
130 case kAlpha_8_GrPixelConfig:
131 *isOpaque = false;
132 return SkBitmap::kA8_Config;
133 case kRGB_565_GrPixelConfig:
134 *isOpaque = true;
135 return SkBitmap::kRGB_565_Config;
136 case kRGBA_4444_GrPixelConfig:
137 *isOpaque = false;
138 return SkBitmap::kARGB_4444_Config;
139 case kSkia8888_GrPixelConfig:
140 // we don't currently have a way of knowing whether
141 // a 8888 is opaque based on the config.
142 *isOpaque = false;
143 return SkBitmap::kARGB_8888_Config;
144 default:
145 *isOpaque = false;
146 return SkBitmap::kNo_Config;
147 }
148}
149
150/*
151 * GrRenderTarget does not know its opaqueness, only its config, so we have
152 * to make conservative guesses when we return an "equivalent" bitmap.
153 */
154static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
155 bool isOpaque;
156 SkBitmap::Config config = grConfig2skConfig(renderTarget->config(), &isOpaque);
157
158 SkBitmap bitmap;
159 bitmap.setConfig(config, renderTarget->width(), renderTarget->height(), 0,
160 isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
161 return bitmap;
162}
163
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000164SkGpuDevice* SkGpuDevice::Create(GrSurface* surface, unsigned flags) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000165 SkASSERT(NULL != surface);
166 if (NULL == surface->asRenderTarget() || NULL == surface->getContext()) {
167 return NULL;
168 }
169 if (surface->asTexture()) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000170 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asTexture(), flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000171 } else {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000172 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asRenderTarget(), flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000173 }
174}
175
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000176SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture, unsigned flags)
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000177 : SkBitmapDevice(make_bitmap(context, texture->asRenderTarget())) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000178 this->initFromRenderTarget(context, texture->asRenderTarget(), flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000179}
180
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000181SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget, unsigned flags)
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000182 : SkBitmapDevice(make_bitmap(context, renderTarget)) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000183 this->initFromRenderTarget(context, renderTarget, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000184}
185
186void SkGpuDevice::initFromRenderTarget(GrContext* context,
187 GrRenderTarget* renderTarget,
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000188 unsigned flags) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000189 fDrawProcs = NULL;
190
191 fContext = context;
192 fContext->ref();
193
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +0000194 bool useDFFonts = !!(flags & kDFFonts_Flag);
195 fMainTextContext = SkNEW_ARGS(GrDistanceFieldTextContext, (fContext, fLeakyProperties,
196 useDFFonts));
commit-bot@chromium.org47841822014-03-27 14:19:17 +0000197 fFallbackTextContext = SkNEW_ARGS(GrBitmapTextContext, (fContext, fLeakyProperties));
198
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000199 fRenderTarget = NULL;
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000200 fNeedClear = flags & kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000201
202 SkASSERT(NULL != renderTarget);
203 fRenderTarget = renderTarget;
204 fRenderTarget->ref();
205
206 // Hold onto to the texture in the pixel ref (if there is one) because the texture holds a ref
207 // on the RT but not vice-versa.
208 // TODO: Remove this trickery once we figure out how to make SkGrPixelRef do this without
209 // busting chrome (for a currently unknown reason).
210 GrSurface* surface = fRenderTarget->asTexture();
211 if (NULL == surface) {
212 surface = fRenderTarget;
213 }
reed@google.combf790232013-12-13 19:45:58 +0000214
215 SkImageInfo info;
216 surface->asImageInfo(&info);
bungeman@google.coma95a0662014-03-19 23:26:50 +0000217 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (info, surface, SkToBool(flags & kCached_Flag)));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000218
reed@google.com672588b2014-01-08 15:42:01 +0000219 this->setPixelRef(pr)->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000220}
221
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000222SkGpuDevice* SkGpuDevice::Create(GrContext* context, const SkImageInfo& origInfo,
223 int sampleCount) {
224 if (kUnknown_SkColorType == origInfo.colorType() ||
225 origInfo.width() < 0 || origInfo.height() < 0) {
226 return NULL;
227 }
228
229 SkImageInfo info = origInfo;
230 // TODO: perhas we can loosen this check now that colortype is more detailed
231 // e.g. can we support both RGBA and BGRA here?
232 if (kRGB_565_SkColorType == info.colorType()) {
233 info.fAlphaType = kOpaque_SkAlphaType; // force this setting
234 } else {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000235 info.fColorType = kN32_SkColorType;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000236 if (kOpaque_SkAlphaType != info.alphaType()) {
237 info.fAlphaType = kPremul_SkAlphaType; // force this setting
238 }
239 }
240
241 GrTextureDesc desc;
242 desc.fFlags = kRenderTarget_GrTextureFlagBit;
243 desc.fWidth = info.width();
244 desc.fHeight = info.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000245 desc.fConfig = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000246 desc.fSampleCnt = sampleCount;
247
248 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0));
249 if (!texture.get()) {
250 return NULL;
251 }
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000252
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000253 return SkNEW_ARGS(SkGpuDevice, (context, texture.get()));
254}
255
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000256SkGpuDevice::~SkGpuDevice() {
257 if (fDrawProcs) {
258 delete fDrawProcs;
259 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000260
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000261 delete fMainTextContext;
262 delete fFallbackTextContext;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000263
264 // The GrContext takes a ref on the target. We don't want to cause the render
265 // target to be unnecessarily kept alive.
266 if (fContext->getRenderTarget() == fRenderTarget) {
267 fContext->setRenderTarget(NULL);
268 }
269
270 if (fContext->getClip() == &fClipData) {
271 fContext->setClip(NULL);
272 }
273
274 SkSafeUnref(fRenderTarget);
275 fContext->unref();
276}
277
278///////////////////////////////////////////////////////////////////////////////
279
280void SkGpuDevice::makeRenderTargetCurrent() {
281 DO_DEFERRED_CLEAR();
282 fContext->setRenderTarget(fRenderTarget);
283}
284
285///////////////////////////////////////////////////////////////////////////////
286
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000287bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
288 int x, int y) {
289 DO_DEFERRED_CLEAR();
290
291 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000292 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000293 if (kUnknown_GrPixelConfig == config) {
294 return false;
295 }
296
297 uint32_t flags = 0;
298 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
299 flags = GrContext::kUnpremul_PixelOpsFlag;
300 }
301 return fContext->readRenderTargetPixels(fRenderTarget, x, y, dstInfo.width(), dstInfo.height(),
302 config, dstPixels, dstRowBytes, flags);
303}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000304
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000305bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
306 int x, int y) {
307 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000308 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000309 if (kUnknown_GrPixelConfig == config) {
310 return false;
311 }
312 uint32_t flags = 0;
313 if (kUnpremul_SkAlphaType == info.alphaType()) {
314 flags = GrContext::kUnpremul_PixelOpsFlag;
315 }
316 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
317
318 // need to bump our genID for compatibility with clients that "know" we have a bitmap
319 this->onAccessBitmap().notifyPixelsChanged();
320
321 return true;
322}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000323
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000324const SkBitmap& SkGpuDevice::onAccessBitmap() {
325 DO_DEFERRED_CLEAR();
326 return INHERITED::onAccessBitmap();
327}
328
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000329void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
330 INHERITED::onAttachToCanvas(canvas);
331
332 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
333 fClipData.fClipStack = canvas->getClipStack();
334}
335
336void SkGpuDevice::onDetachFromCanvas() {
337 INHERITED::onDetachFromCanvas();
338 fClipData.fClipStack = NULL;
339}
340
341// call this every draw call, to ensure that the context reflects our state,
342// and not the state from some other canvas/device
343void SkGpuDevice::prepareDraw(const SkDraw& draw, bool forceIdentity) {
344 SkASSERT(NULL != fClipData.fClipStack);
345
346 fContext->setRenderTarget(fRenderTarget);
347
348 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
349
350 if (forceIdentity) {
351 fContext->setIdentityMatrix();
352 } else {
353 fContext->setMatrix(*draw.fMatrix);
354 }
355 fClipData.fOrigin = this->getOrigin();
356
357 fContext->setClip(&fClipData);
358
359 DO_DEFERRED_CLEAR();
360}
361
362GrRenderTarget* SkGpuDevice::accessRenderTarget() {
363 DO_DEFERRED_CLEAR();
364 return fRenderTarget;
365}
366
367///////////////////////////////////////////////////////////////////////////////
368
369SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
370SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
371SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
372SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
373SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
374 shader_type_mismatch);
375SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
376 shader_type_mismatch);
377SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
378SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
379
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000380///////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000381
reeda6a8f002014-06-02 05:45:31 -0700382#ifdef SK_SUPPORT_LEGACY_DEVICE_CONFIG
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000383SkBitmap::Config SkGpuDevice::config() const {
384 if (NULL == fRenderTarget) {
385 return SkBitmap::kNo_Config;
386 }
387
388 bool isOpaque;
389 return grConfig2skConfig(fRenderTarget->config(), &isOpaque);
390}
reeda6a8f002014-06-02 05:45:31 -0700391#endif
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000392
393void SkGpuDevice::clear(SkColor color) {
394 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
395 fContext->clear(&rect, SkColor2GrColor(color), true, fRenderTarget);
396 fNeedClear = false;
397}
398
399void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
400 CHECK_SHOULD_DRAW(draw, false);
401
402 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000403 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000404
405 fContext->drawPaint(grPaint);
406}
407
408// must be in SkCanvas::PointMode order
409static const GrPrimitiveType gPointMode2PrimtiveType[] = {
410 kPoints_GrPrimitiveType,
411 kLines_GrPrimitiveType,
412 kLineStrip_GrPrimitiveType
413};
414
415void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
416 size_t count, const SkPoint pts[], const SkPaint& paint) {
417 CHECK_FOR_ANNOTATION(paint);
418 CHECK_SHOULD_DRAW(draw, false);
419
420 SkScalar width = paint.getStrokeWidth();
421 if (width < 0) {
422 return;
423 }
424
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000425 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000426 if (GrDashingEffect::DrawDashLine(pts, paint, this->context())) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000427 return;
428 }
429 }
430
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000431 // we only handle hairlines and paints without path effects or mask filters,
432 // else we let the SkDraw call our drawPath()
433 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
434 draw.drawPoints(mode, count, pts, paint, true);
435 return;
436 }
437
438 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000439 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000440
441 fContext->drawVertices(grPaint,
442 gPointMode2PrimtiveType[mode],
robertphillips@google.coma4662862013-11-21 14:24:16 +0000443 SkToS32(count),
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000444 (SkPoint*)pts,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000445 NULL,
446 NULL,
447 NULL,
448 0);
449}
450
451///////////////////////////////////////////////////////////////////////////////
452
453void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
454 const SkPaint& paint) {
455 CHECK_FOR_ANNOTATION(paint);
456 CHECK_SHOULD_DRAW(draw, false);
457
458 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
459 SkScalar width = paint.getStrokeWidth();
460
461 /*
462 We have special code for hairline strokes, miter-strokes, bevel-stroke
463 and fills. Anything else we just call our path code.
464 */
465 bool usePath = doStroke && width > 0 &&
466 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
467 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
468 // another two reasons we might need to call drawPath...
469 if (paint.getMaskFilter() || paint.getPathEffect()) {
470 usePath = true;
471 }
472 if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) {
473#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
474 if (doStroke) {
475#endif
476 usePath = true;
477#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
478 } else {
479 usePath = !fContext->getMatrix().preservesRightAngles();
480 }
481#endif
482 }
483 // until we can both stroke and fill rectangles
484 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
485 usePath = true;
486 }
487
488 if (usePath) {
489 SkPath path;
490 path.addRect(rect);
491 this->drawPath(draw, path, paint, NULL, true);
492 return;
493 }
494
495 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000496 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000497
498 if (!doStroke) {
499 fContext->drawRect(grPaint, rect);
500 } else {
501 SkStrokeRec stroke(paint);
502 fContext->drawRect(grPaint, rect, &stroke);
503 }
504}
505
506///////////////////////////////////////////////////////////////////////////////
507
508void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
509 const SkPaint& paint) {
510 CHECK_FOR_ANNOTATION(paint);
511 CHECK_SHOULD_DRAW(draw, false);
512
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000513 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000514 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000515
516 SkStrokeRec stroke(paint);
517 if (paint.getMaskFilter()) {
518 // try to hit the fast path for drawing filtered round rects
519
520 SkRRect devRRect;
521 if (rect.transform(fContext->getMatrix(), &devRRect)) {
522 if (devRRect.allCornersCircular()) {
523 SkRect maskRect;
524 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(),
525 draw.fClip->getBounds(),
526 fContext->getMatrix(),
527 &maskRect)) {
528 SkIRect finalIRect;
529 maskRect.roundOut(&finalIRect);
530 if (draw.fClip->quickReject(finalIRect)) {
531 // clipped out
532 return;
533 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000534 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext, &grPaint,
535 stroke, devRRect)) {
536 return;
537 }
538 }
539
540 }
541 }
542
543 }
544
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000545 if (paint.getMaskFilter() || paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000546 SkPath path;
547 path.addRRect(rect);
548 this->drawPath(draw, path, paint, NULL, true);
549 return;
550 }
551
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000552 fContext->drawRRect(grPaint, rect, stroke);
553}
554
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000555void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
556 const SkRRect& inner, const SkPaint& paint) {
557 SkStrokeRec stroke(paint);
558 if (stroke.isFillStyle()) {
559
560 CHECK_FOR_ANNOTATION(paint);
561 CHECK_SHOULD_DRAW(draw, false);
562
563 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000564 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000565
566 if (NULL == paint.getMaskFilter() && NULL == paint.getPathEffect()) {
567 fContext->drawDRRect(grPaint, outer, inner);
568 return;
569 }
570 }
571
572 SkPath path;
573 path.addRRect(outer);
574 path.addRRect(inner);
575 path.setFillType(SkPath::kEvenOdd_FillType);
576
577 this->drawPath(draw, path, paint, NULL, true);
578}
579
580
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000581/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000582
583void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
584 const SkPaint& paint) {
585 CHECK_FOR_ANNOTATION(paint);
586 CHECK_SHOULD_DRAW(draw, false);
587
588 bool usePath = false;
589 // some basic reasons we might need to call drawPath...
590 if (paint.getMaskFilter() || paint.getPathEffect()) {
591 usePath = true;
592 }
593
594 if (usePath) {
595 SkPath path;
596 path.addOval(oval);
597 this->drawPath(draw, path, paint, NULL, true);
598 return;
599 }
600
601 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000602 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000603 SkStrokeRec stroke(paint);
604
605 fContext->drawOval(grPaint, oval, stroke);
606}
607
608#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000609
610///////////////////////////////////////////////////////////////////////////////
611
612// helpers for applying mask filters
613namespace {
614
615// Draw a mask using the supplied paint. Since the coverage/geometry
616// is already burnt into the mask this boils down to a rect draw.
617// Return true if the mask was successfully drawn.
618bool draw_mask(GrContext* context, const SkRect& maskRect,
619 GrPaint* grp, GrTexture* mask) {
620 GrContext::AutoMatrix am;
621 if (!am.setIdentity(context, grp)) {
622 return false;
623 }
624
625 SkMatrix matrix;
626 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
627 matrix.postIDiv(mask->width(), mask->height());
628
629 grp->addCoverageEffect(GrSimpleTextureEffect::Create(mask, matrix))->unref();
630 context->drawRect(*grp, maskRect);
631 return true;
632}
633
634bool draw_with_mask_filter(GrContext* context, const SkPath& devPath,
reed868074b2014-06-03 10:53:59 -0700635 SkMaskFilter* filter, const SkRegion& clip,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000636 GrPaint* grp, SkPaint::Style style) {
637 SkMask srcM, dstM;
638
639 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
640 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
641 return false;
642 }
643 SkAutoMaskFreeImage autoSrc(srcM.fImage);
644
645 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
646 return false;
647 }
648 // this will free-up dstM when we're done (allocated in filterMask())
649 SkAutoMaskFreeImage autoDst(dstM.fImage);
650
651 if (clip.quickReject(dstM.fBounds)) {
652 return false;
653 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000654
655 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
656 // the current clip (and identity matrix) and GrPaint settings
657 GrTextureDesc desc;
658 desc.fWidth = dstM.fBounds.width();
659 desc.fHeight = dstM.fBounds.height();
660 desc.fConfig = kAlpha_8_GrPixelConfig;
661
662 GrAutoScratchTexture ast(context, desc);
663 GrTexture* texture = ast.texture();
664
665 if (NULL == texture) {
666 return false;
667 }
668 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
669 dstM.fImage, dstM.fRowBytes);
670
671 SkRect maskRect = SkRect::Make(dstM.fBounds);
672
673 return draw_mask(context, maskRect, grp, texture);
674}
675
676// Create a mask of 'devPath' and place the result in 'mask'. Return true on
677// success; false otherwise.
678bool create_mask_GPU(GrContext* context,
679 const SkRect& maskRect,
680 const SkPath& devPath,
681 const SkStrokeRec& stroke,
682 bool doAA,
683 GrAutoScratchTexture* mask) {
684 GrTextureDesc desc;
685 desc.fFlags = kRenderTarget_GrTextureFlagBit;
686 desc.fWidth = SkScalarCeilToInt(maskRect.width());
687 desc.fHeight = SkScalarCeilToInt(maskRect.height());
688 // We actually only need A8, but it often isn't supported as a
689 // render target so default to RGBA_8888
690 desc.fConfig = kRGBA_8888_GrPixelConfig;
691 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
692 desc.fConfig = kAlpha_8_GrPixelConfig;
693 }
694
695 mask->set(context, desc);
696 if (NULL == mask->texture()) {
697 return false;
698 }
699
700 GrTexture* maskTexture = mask->texture();
701 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
702
703 GrContext::AutoRenderTarget art(context, maskTexture->asRenderTarget());
704 GrContext::AutoClip ac(context, clipRect);
705
706 context->clear(NULL, 0x0, true);
707
708 GrPaint tempPaint;
709 if (doAA) {
710 tempPaint.setAntiAlias(true);
711 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
712 // blend coeff of zero requires dual source blending support in order
713 // to properly blend partially covered pixels. This means the AA
714 // code path may not be taken. So we use a dst blend coeff of ISA. We
715 // could special case AA draws to a dst surface with known alpha=0 to
716 // use a zero dst coeff when dual source blending isn't available.
717 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
718 }
719
720 GrContext::AutoMatrix am;
721
722 // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint.
723 SkMatrix translate;
724 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
725 am.set(context, translate);
726 context->drawPath(tempPaint, devPath, stroke);
727 return true;
728}
729
730SkBitmap wrap_texture(GrTexture* texture) {
reed@google.combf790232013-12-13 19:45:58 +0000731 SkImageInfo info;
732 texture->asImageInfo(&info);
733
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000734 SkBitmap result;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000735 result.setInfo(info);
reed@google.combf790232013-12-13 19:45:58 +0000736 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000737 return result;
738}
739
740};
741
742void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
743 const SkPaint& paint, const SkMatrix* prePathMatrix,
744 bool pathIsMutable) {
745 CHECK_FOR_ANNOTATION(paint);
746 CHECK_SHOULD_DRAW(draw, false);
747
748 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000749 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000750
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000751 // If we have a prematrix, apply it to the path, optimizing for the case
752 // where the original path can in fact be modified in place (even though
753 // its parameter type is const).
754 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000755 SkTLazy<SkPath> tmpPath;
756 SkTLazy<SkPath> effectPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000757
758 if (prePathMatrix) {
759 SkPath* result = pathPtr;
760
761 if (!pathIsMutable) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000762 result = tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000763 pathIsMutable = true;
764 }
765 // should I push prePathMatrix on our MV stack temporarily, instead
766 // of applying it here? See SkDraw.cpp
767 pathPtr->transform(*prePathMatrix, result);
768 pathPtr = result;
769 }
770 // at this point we're done with prePathMatrix
771 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
772
773 SkStrokeRec stroke(paint);
774 SkPathEffect* pathEffect = paint.getPathEffect();
775 const SkRect* cullRect = NULL; // TODO: what is our bounds?
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000776 if (pathEffect && pathEffect->filterPath(effectPath.init(), *pathPtr, &stroke,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000777 cullRect)) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000778 pathPtr = effectPath.get();
779 pathIsMutable = true;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000780 }
781
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000782 if (paint.getMaskFilter()) {
783 if (!stroke.isHairlineStyle()) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000784 SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init();
785 if (stroke.applyToPath(strokedPath, *pathPtr)) {
786 pathPtr = strokedPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000787 pathIsMutable = true;
788 stroke.setFillStyle();
789 }
790 }
791
792 // avoid possibly allocating a new path in transform if we can
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000793 SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000794
795 // transform the path into device space
796 pathPtr->transform(fContext->getMatrix(), devPathPtr);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000797
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000798 SkRect maskRect;
799 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
800 draw.fClip->getBounds(),
801 fContext->getMatrix(),
802 &maskRect)) {
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000803 // The context's matrix may change while creating the mask, so save the CTM here to
804 // pass to filterMaskGPU.
805 const SkMatrix ctm = fContext->getMatrix();
806
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000807 SkIRect finalIRect;
808 maskRect.roundOut(&finalIRect);
809 if (draw.fClip->quickReject(finalIRect)) {
810 // clipped out
811 return;
812 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000813
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000814 if (paint.getMaskFilter()->directFilterMaskGPU(fContext, &grPaint,
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000815 stroke, *devPathPtr)) {
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000816 // the mask filter was able to draw itself directly, so there's nothing
817 // left to do.
818 return;
819 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000820
821 GrAutoScratchTexture mask;
822
823 if (create_mask_GPU(fContext, maskRect, *devPathPtr, stroke,
824 grPaint.isAntiAlias(), &mask)) {
825 GrTexture* filtered;
826
commit-bot@chromium.org41bf9302014-01-08 22:25:53 +0000827 if (paint.getMaskFilter()->filterMaskGPU(mask.texture(),
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000828 ctm, maskRect, &filtered, true)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000829 // filterMaskGPU gives us ownership of a ref to the result
830 SkAutoTUnref<GrTexture> atu(filtered);
831
832 // If the scratch texture that we used as the filter src also holds the filter
833 // result then we must detach so that this texture isn't recycled for a later
834 // draw.
835 if (filtered == mask.texture()) {
836 mask.detach();
837 filtered->unref(); // detach transfers GrAutoScratchTexture's ref to us.
838 }
839
840 if (draw_mask(fContext, maskRect, &grPaint, filtered)) {
841 // This path is completely drawn
842 return;
843 }
844 }
845 }
846 }
847
848 // draw the mask on the CPU - this is a fallthrough path in case the
849 // GPU path fails
850 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
851 SkPaint::kFill_Style;
reed868074b2014-06-03 10:53:59 -0700852 draw_with_mask_filter(fContext, *devPathPtr, paint.getMaskFilter(), *draw.fClip, &grPaint,
853 style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000854 return;
855 }
856
857 fContext->drawPath(grPaint, *pathPtr, stroke);
858}
859
860static const int kBmpSmallTileSize = 1 << 10;
861
862static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
863 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
864 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
865 return tilesX * tilesY;
866}
867
868static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
869 if (maxTileSize <= kBmpSmallTileSize) {
870 return maxTileSize;
871 }
872
873 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
874 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
875
876 maxTileTotalTileSize *= maxTileSize * maxTileSize;
877 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
878
879 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
880 return kBmpSmallTileSize;
881 } else {
882 return maxTileSize;
883 }
884}
885
886// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
887// pixels from the bitmap are necessary.
888static void determine_clipped_src_rect(const GrContext* context,
889 const SkBitmap& bitmap,
890 const SkRect* srcRectPtr,
891 SkIRect* clippedSrcIRect) {
892 const GrClipData* clip = context->getClip();
893 clip->getConservativeBounds(context->getRenderTarget(), clippedSrcIRect, NULL);
894 SkMatrix inv;
895 if (!context->getMatrix().invert(&inv)) {
896 clippedSrcIRect->setEmpty();
897 return;
898 }
899 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
900 inv.mapRect(&clippedSrcRect);
901 if (NULL != srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000902 // we've setup src space 0,0 to map to the top left of the src rect.
903 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000904 if (!clippedSrcRect.intersect(*srcRectPtr)) {
905 clippedSrcIRect->setEmpty();
906 return;
907 }
908 }
909 clippedSrcRect.roundOut(clippedSrcIRect);
910 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
911 if (!clippedSrcIRect->intersect(bmpBounds)) {
912 clippedSrcIRect->setEmpty();
913 }
914}
915
916bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
917 const GrTextureParams& params,
918 const SkRect* srcRectPtr,
919 int maxTileSize,
920 int* tileSize,
921 SkIRect* clippedSrcRect) const {
922 // if bitmap is explictly texture backed then just use the texture
923 if (NULL != bitmap.getTexture()) {
924 return false;
925 }
926
927 // if it's larger than the max tile size, then we have no choice but tiling.
928 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
929 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
930 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
931 return true;
932 }
933
934 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
935 return false;
936 }
937
938 // if the entire texture is already in our cache then no reason to tile it
939 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
940 return false;
941 }
942
943 // At this point we know we could do the draw by uploading the entire bitmap
944 // as a texture. However, if the texture would be large compared to the
945 // cache size and we don't require most of it for this draw then tile to
946 // reduce the amount of upload and cache spill.
947
948 // assumption here is that sw bitmap size is a good proxy for its size as
949 // a texture
950 size_t bmpSize = bitmap.getSize();
951 size_t cacheSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000952 fContext->getResourceCacheLimits(NULL, &cacheSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000953 if (bmpSize < cacheSize / 2) {
954 return false;
955 }
956
957 // Figure out how much of the src we will need based on the src rect and clipping.
958 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
959 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
960 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
961 kBmpSmallTileSize * kBmpSmallTileSize;
962
963 return usedTileBytes < 2 * bmpSize;
964}
965
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000966void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000967 const SkBitmap& bitmap,
968 const SkMatrix& m,
969 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000970 SkMatrix concat;
971 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
972 if (!m.isIdentity()) {
973 concat.setConcat(*draw->fMatrix, m);
974 draw.writable()->fMatrix = &concat;
975 }
976 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000977}
978
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000979// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000980// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
981// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000982static inline void clamped_outset_with_offset(SkIRect* iRect,
983 int outset,
984 SkPoint* offset,
985 const SkIRect& clamp) {
986 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000987
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000988 int leftClampDelta = clamp.fLeft - iRect->fLeft;
989 if (leftClampDelta > 0) {
990 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000991 iRect->fLeft = clamp.fLeft;
992 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000993 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000994 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000995
996 int topClampDelta = clamp.fTop - iRect->fTop;
997 if (topClampDelta > 0) {
998 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000999 iRect->fTop = clamp.fTop;
1000 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001001 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001002 }
1003
1004 if (iRect->fRight > clamp.fRight) {
1005 iRect->fRight = clamp.fRight;
1006 }
1007 if (iRect->fBottom > clamp.fBottom) {
1008 iRect->fBottom = clamp.fBottom;
1009 }
1010}
1011
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001012static bool has_aligned_samples(const SkRect& srcRect,
1013 const SkRect& transformedRect) {
1014 // detect pixel disalignment
1015 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1016 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1017 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1018 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1019 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1020 COLOR_BLEED_TOLERANCE &&
1021 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1022 COLOR_BLEED_TOLERANCE) {
1023 return true;
1024 }
1025 return false;
1026}
1027
1028static bool may_color_bleed(const SkRect& srcRect,
1029 const SkRect& transformedRect,
1030 const SkMatrix& m) {
1031 // Only gets called if has_aligned_samples returned false.
1032 // So we can assume that sampling is axis aligned but not texel aligned.
1033 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
1034 SkRect innerSrcRect(srcRect), innerTransformedRect,
1035 outerTransformedRect(transformedRect);
1036 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1037 m.mapRect(&innerTransformedRect, innerSrcRect);
1038
1039 // The gap between outerTransformedRect and innerTransformedRect
1040 // represents the projection of the source border area, which is
1041 // problematic for color bleeding. We must check whether any
1042 // destination pixels sample the border area.
1043 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1044 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1045 SkIRect outer, inner;
1046 outerTransformedRect.round(&outer);
1047 innerTransformedRect.round(&inner);
1048 // If the inner and outer rects round to the same result, it means the
1049 // border does not overlap any pixel centers. Yay!
1050 return inner != outer;
1051}
1052
1053static bool needs_texture_domain(const SkBitmap& bitmap,
1054 const SkRect& srcRect,
1055 GrTextureParams &params,
1056 const SkMatrix& contextMatrix,
1057 bool bicubic) {
1058 bool needsTextureDomain = false;
1059
1060 if (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode) {
1061 // Need texture domain if drawing a sub rect
1062 needsTextureDomain = srcRect.width() < bitmap.width() ||
1063 srcRect.height() < bitmap.height();
1064 if (!bicubic && needsTextureDomain && contextMatrix.rectStaysRect()) {
1065 // sampling is axis-aligned
1066 SkRect transformedRect;
1067 contextMatrix.mapRect(&transformedRect, srcRect);
1068
1069 if (has_aligned_samples(srcRect, transformedRect)) {
1070 params.setFilterMode(GrTextureParams::kNone_FilterMode);
1071 needsTextureDomain = false;
1072 } else {
1073 needsTextureDomain = may_color_bleed(srcRect, transformedRect, contextMatrix);
1074 }
1075 }
1076 }
1077 return needsTextureDomain;
1078}
1079
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001080void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1081 const SkBitmap& bitmap,
1082 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001083 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001084 const SkPaint& paint,
1085 SkCanvas::DrawBitmapRectFlags flags) {
1086 CHECK_SHOULD_DRAW(draw, false);
1087
1088 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001089 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001090 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
1091 // in the (easier) bleed case, so update flags.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001092 if (NULL == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001093 SkScalar w = SkIntToScalar(bitmap.width());
1094 SkScalar h = SkIntToScalar(bitmap.height());
1095 dstSize.fWidth = w;
1096 dstSize.fHeight = h;
1097 srcRect.set(0, 0, w, h);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001098 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001099 } else {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001100 SkASSERT(NULL != dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001101 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001102 dstSize = *dstSizePtr;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001103 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
1104 srcRect.fRight >= bitmap.width() && srcRect.fBottom >= bitmap.height()) {
1105 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
1106 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001107 }
1108
1109 if (paint.getMaskFilter()){
1110 // Convert the bitmap to a shader so that the rect can be drawn
1111 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001112 SkBitmap tmp; // subset of bitmap, if necessary
1113 const SkBitmap* bitmapPtr = &bitmap;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001114 SkMatrix localM;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001115 if (NULL != srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001116 localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
1117 localM.postScale(dstSize.fWidth / srcRectPtr->width(),
1118 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001119 // In bleed mode we position and trim the bitmap based on the src rect which is
1120 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
1121 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
1122 // compensate.
1123 if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) {
1124 SkIRect iSrc;
1125 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001126
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001127 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
1128 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001129
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001130 if (!bitmap.extractSubset(&tmp, iSrc)) {
1131 return; // extraction failed
1132 }
1133 bitmapPtr = &tmp;
1134 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001135
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001136 // The source rect has changed so update the matrix
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001137 localM.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001138 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001139 } else {
1140 localM.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001141 }
1142
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001143 SkPaint paintWithShader(paint);
1144 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +00001145 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &localM))->unref();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001146 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1147 this->drawRect(draw, dstRect, paintWithShader);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001148
1149 return;
1150 }
1151
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001152 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
1153 // the view matrix rather than a local matrix.
1154 SkMatrix m;
1155 m.setScale(dstSize.fWidth / srcRect.width(),
1156 dstSize.fHeight / srcRect.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001157 fContext->concatMatrix(m);
1158
1159 GrTextureParams params;
1160 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel();
1161 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001162
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001163 bool doBicubic = false;
1164
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001165 switch(paintFilterLevel) {
1166 case SkPaint::kNone_FilterLevel:
1167 textureFilterMode = GrTextureParams::kNone_FilterMode;
1168 break;
1169 case SkPaint::kLow_FilterLevel:
1170 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1171 break;
1172 case SkPaint::kMedium_FilterLevel:
commit-bot@chromium.org18786512014-05-20 14:53:45 +00001173 if (fContext->getMatrix().getMinScale() < SK_Scalar1) {
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001174 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1175 } else {
1176 // Don't trigger MIP level generation unnecessarily.
1177 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1178 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001179 break;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001180 case SkPaint::kHigh_FilterLevel:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001181 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001182 doBicubic =
1183 GrBicubicEffect::ShouldUseBicubic(fContext->getMatrix(), &textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001184 break;
1185 default:
1186 SkErrorInternals::SetError( kInvalidPaint_SkError,
1187 "Sorry, I don't understand the filtering "
1188 "mode you asked for. Falling back to "
1189 "MIPMaps.");
1190 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1191 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001192 }
1193
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001194 int tileFilterPad;
1195 if (doBicubic) {
1196 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1197 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1198 tileFilterPad = 0;
1199 } else {
1200 tileFilterPad = 1;
1201 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001202 params.setFilterMode(textureFilterMode);
1203
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001204 int maxTileSize = fContext->getMaxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001205 int tileSize;
1206
1207 SkIRect clippedSrcRect;
1208 if (this->shouldTileBitmap(bitmap, params, srcRectPtr, maxTileSize, &tileSize,
1209 &clippedSrcRect)) {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001210 this->drawTiledBitmap(bitmap, srcRect, clippedSrcRect, params, paint, flags, tileSize,
1211 doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001212 } else {
1213 // take the simple case
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001214 bool needsTextureDomain = needs_texture_domain(bitmap,
1215 srcRect,
1216 params,
1217 fContext->getMatrix(),
1218 doBicubic);
1219 this->internalDrawBitmap(bitmap,
1220 srcRect,
1221 params,
1222 paint,
1223 flags,
1224 doBicubic,
1225 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001226 }
1227}
1228
1229// Break 'bitmap' into several tiles to draw it since it has already
1230// been determined to be too large to fit in VRAM
1231void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
1232 const SkRect& srcRect,
1233 const SkIRect& clippedSrcIRect,
1234 const GrTextureParams& params,
1235 const SkPaint& paint,
1236 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001237 int tileSize,
1238 bool bicubic) {
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +00001239 // The following pixel lock is technically redundant, but it is desirable
1240 // to lock outside of the tile loop to prevent redecoding the whole image
1241 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1242 // is larger than the limit of the discardable memory pool.
1243 SkAutoLockPixels alp(bitmap);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001244 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1245
1246 int nx = bitmap.width() / tileSize;
1247 int ny = bitmap.height() / tileSize;
1248 for (int x = 0; x <= nx; x++) {
1249 for (int y = 0; y <= ny; y++) {
1250 SkRect tileR;
1251 tileR.set(SkIntToScalar(x * tileSize),
1252 SkIntToScalar(y * tileSize),
1253 SkIntToScalar((x + 1) * tileSize),
1254 SkIntToScalar((y + 1) * tileSize));
1255
1256 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1257 continue;
1258 }
1259
1260 if (!tileR.intersect(srcRect)) {
1261 continue;
1262 }
1263
1264 SkBitmap tmpB;
1265 SkIRect iTileR;
1266 tileR.roundOut(&iTileR);
1267 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1268 SkIntToScalar(iTileR.fTop));
1269
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001270 // Adjust the context matrix to draw at the right x,y in device space
1271 SkMatrix tmpM;
1272 GrContext::AutoMatrix am;
1273 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
1274 am.setPreConcat(fContext, tmpM);
1275
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001276 if (SkPaint::kNone_FilterLevel != paint.getFilterLevel() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001277 SkIRect iClampRect;
1278
1279 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1280 // In bleed mode we want to always expand the tile on all edges
1281 // but stay within the bitmap bounds
1282 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1283 } else {
1284 // In texture-domain/clamp mode we only want to expand the
1285 // tile on edges interior to "srcRect" (i.e., we want to
1286 // not bleed across the original clamped edges)
1287 srcRect.roundOut(&iClampRect);
1288 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001289 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1290 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001291 }
1292
1293 if (bitmap.extractSubset(&tmpB, iTileR)) {
1294 // now offset it to make it "local" to our tmp bitmap
1295 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001296 GrTextureParams paramsTemp = params;
1297 bool needsTextureDomain = needs_texture_domain(bitmap,
1298 srcRect,
1299 paramsTemp,
1300 fContext->getMatrix(),
1301 bicubic);
1302 this->internalDrawBitmap(tmpB,
1303 tileR,
1304 paramsTemp,
1305 paint,
1306 flags,
1307 bicubic,
1308 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001309 }
1310 }
1311 }
1312}
1313
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001314
1315/*
1316 * This is called by drawBitmap(), which has to handle images that may be too
1317 * large to be represented by a single texture.
1318 *
1319 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1320 * and that non-texture portion of the GrPaint has already been setup.
1321 */
1322void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
1323 const SkRect& srcRect,
1324 const GrTextureParams& params,
1325 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001326 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001327 bool bicubic,
1328 bool needsTextureDomain) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001329 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1330 bitmap.height() <= fContext->getMaxTextureSize());
1331
1332 GrTexture* texture;
1333 SkAutoCachedTexture act(this, bitmap, &params, &texture);
1334 if (NULL == texture) {
1335 return;
1336 }
1337
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001338 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001339 SkRect paintRect;
1340 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1341 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1342 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1343 SkScalarMul(srcRect.fTop, hInv),
1344 SkScalarMul(srcRect.fRight, wInv),
1345 SkScalarMul(srcRect.fBottom, hInv));
1346
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001347 SkRect textureDomain = SkRect::MakeEmpty();
1348 SkAutoTUnref<GrEffectRef> effect;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001349 if (needsTextureDomain && !(flags & SkCanvas::kBleed_DrawBitmapRectFlag)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001350 // Use a constrained texture domain to avoid color bleeding
1351 SkScalar left, top, right, bottom;
1352 if (srcRect.width() > SK_Scalar1) {
1353 SkScalar border = SK_ScalarHalf / texture->width();
1354 left = paintRect.left() + border;
1355 right = paintRect.right() - border;
1356 } else {
1357 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1358 }
1359 if (srcRect.height() > SK_Scalar1) {
1360 SkScalar border = SK_ScalarHalf / texture->height();
1361 top = paintRect.top() + border;
1362 bottom = paintRect.bottom() - border;
1363 } else {
1364 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1365 }
1366 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001367 if (bicubic) {
1368 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
1369 } else {
1370 effect.reset(GrTextureDomainEffect::Create(texture,
1371 SkMatrix::I(),
1372 textureDomain,
1373 GrTextureDomain::kClamp_Mode,
1374 params.filterMode()));
1375 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001376 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001377 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1378 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
1379 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001380 } else {
1381 effect.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
1382 }
1383
1384 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1385 // the rest from the SkPaint.
1386 GrPaint grPaint;
1387 grPaint.addColorEffect(effect);
1388 bool alphaOnly = !(SkBitmap::kA8_Config == bitmap.config());
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001389 SkPaint2GrPaintNoShader(this->context(), paint, alphaOnly, false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001390
1391 fContext->drawRectToRect(grPaint, dstRect, paintRect, NULL);
1392}
1393
1394static bool filter_texture(SkBaseDevice* device, GrContext* context,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001395 GrTexture* texture, const SkImageFilter* filter,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001396 int w, int h, const SkImageFilter::Context& ctx,
1397 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001398 SkASSERT(filter);
1399 SkDeviceImageFilterProxy proxy(device);
1400
1401 if (filter->canFilterImageGPU()) {
1402 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1403 // filter. Also set the clip wide open and the matrix to identity.
1404 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001405 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001406 } else {
1407 return false;
1408 }
1409}
1410
1411void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1412 int left, int top, const SkPaint& paint) {
1413 // drawSprite is defined to be in device coords.
1414 CHECK_SHOULD_DRAW(draw, true);
1415
1416 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1417 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1418 return;
1419 }
1420
1421 int w = bitmap.width();
1422 int h = bitmap.height();
1423
1424 GrTexture* texture;
1425 // draw sprite uses the default texture params
1426 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
1427
1428 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001429 // This bitmap will own the filtered result as a texture.
1430 SkBitmap filteredBitmap;
1431
1432 if (NULL != filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001433 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001434 SkMatrix matrix(*draw.fMatrix);
1435 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001436 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001437 SkImageFilter::Cache* cache = SkImageFilter::Cache::Create();
1438 SkAutoUnref aur(cache);
1439 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001440 if (filter_texture(this, fContext, texture, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001441 &offset)) {
1442 texture = (GrTexture*) filteredBitmap.getTexture();
1443 w = filteredBitmap.width();
1444 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001445 left += offset.x();
1446 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001447 } else {
1448 return;
1449 }
1450 }
1451
1452 GrPaint grPaint;
1453 grPaint.addColorTextureEffect(texture, SkMatrix::I());
1454
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001455 SkPaint2GrPaintNoShader(this->context(), paint, true, false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001456
1457 fContext->drawRectToRect(grPaint,
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001458 SkRect::MakeXYWH(SkIntToScalar(left),
1459 SkIntToScalar(top),
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001460 SkIntToScalar(w),
1461 SkIntToScalar(h)),
1462 SkRect::MakeXYWH(0,
1463 0,
1464 SK_Scalar1 * w / texture->width(),
1465 SK_Scalar1 * h / texture->height()));
1466}
1467
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001468void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001469 const SkRect* src, const SkRect& dst,
1470 const SkPaint& paint,
1471 SkCanvas::DrawBitmapRectFlags flags) {
1472 SkMatrix matrix;
1473 SkRect bitmapBounds, tmpSrc;
1474
1475 bitmapBounds.set(0, 0,
1476 SkIntToScalar(bitmap.width()),
1477 SkIntToScalar(bitmap.height()));
1478
1479 // Compute matrix from the two rectangles
1480 if (NULL != src) {
1481 tmpSrc = *src;
1482 } else {
1483 tmpSrc = bitmapBounds;
1484 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001485
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001486 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1487
1488 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
1489 if (NULL != src) {
1490 if (!bitmapBounds.contains(tmpSrc)) {
1491 if (!tmpSrc.intersect(bitmapBounds)) {
1492 return; // nothing to draw
1493 }
1494 }
1495 }
1496
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001497 SkRect tmpDst;
1498 matrix.mapRect(&tmpDst, tmpSrc);
1499
1500 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1501 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1502 // Translate so that tempDst's top left is at the origin.
1503 matrix = *origDraw.fMatrix;
1504 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1505 draw.writable()->fMatrix = &matrix;
1506 }
1507 SkSize dstSize;
1508 dstSize.fWidth = tmpDst.width();
1509 dstSize.fHeight = tmpDst.height();
1510
1511 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001512}
1513
1514void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1515 int x, int y, const SkPaint& paint) {
1516 // clear of the source device must occur before CHECK_SHOULD_DRAW
1517 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1518 if (dev->fNeedClear) {
1519 // TODO: could check here whether we really need to draw at all
1520 dev->clear(0x0);
1521 }
1522
1523 // drawDevice is defined to be in device coords.
1524 CHECK_SHOULD_DRAW(draw, true);
1525
1526 GrRenderTarget* devRT = dev->accessRenderTarget();
1527 GrTexture* devTex;
1528 if (NULL == (devTex = devRT->asTexture())) {
1529 return;
1530 }
1531
1532 const SkBitmap& bm = dev->accessBitmap(false);
1533 int w = bm.width();
1534 int h = bm.height();
1535
1536 SkImageFilter* filter = paint.getImageFilter();
1537 // This bitmap will own the filtered result as a texture.
1538 SkBitmap filteredBitmap;
1539
1540 if (NULL != filter) {
1541 SkIPoint offset = SkIPoint::Make(0, 0);
1542 SkMatrix matrix(*draw.fMatrix);
1543 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001544 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001545 SkImageFilter::Cache* cache = SkImageFilter::Cache::Create();
1546 SkAutoUnref aur(cache);
1547 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001548 if (filter_texture(this, fContext, devTex, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001549 &offset)) {
1550 devTex = filteredBitmap.getTexture();
1551 w = filteredBitmap.width();
1552 h = filteredBitmap.height();
1553 x += offset.fX;
1554 y += offset.fY;
1555 } else {
1556 return;
1557 }
1558 }
1559
1560 GrPaint grPaint;
1561 grPaint.addColorTextureEffect(devTex, SkMatrix::I());
1562
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001563 SkPaint2GrPaintNoShader(this->context(), paint, true, false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001564
1565 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1566 SkIntToScalar(y),
1567 SkIntToScalar(w),
1568 SkIntToScalar(h));
1569
1570 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1571 // scratch texture).
1572 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1573 SK_Scalar1 * h / devTex->height());
1574
1575 fContext->drawRectToRect(grPaint, dstRect, srcRect);
1576}
1577
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001578bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001579 return filter->canFilterImageGPU();
1580}
1581
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001582bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001583 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001584 SkBitmap* result, SkIPoint* offset) {
1585 // want explicitly our impl, so guard against a subclass of us overriding it
1586 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1587 return false;
1588 }
1589
1590 SkAutoLockPixels alp(src, !src.getTexture());
1591 if (!src.getTexture() && !src.readyToDraw()) {
1592 return false;
1593 }
1594
1595 GrTexture* texture;
1596 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1597 // must be pushed upstack.
1598 SkAutoCachedTexture act(this, src, NULL, &texture);
1599
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001600 return filter_texture(this, fContext, texture, filter, src.width(), src.height(), ctx,
1601 result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001602}
1603
1604///////////////////////////////////////////////////////////////////////////////
1605
1606// must be in SkCanvas::VertexMode order
1607static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1608 kTriangles_GrPrimitiveType,
1609 kTriangleStrip_GrPrimitiveType,
1610 kTriangleFan_GrPrimitiveType,
1611};
1612
1613void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1614 int vertexCount, const SkPoint vertices[],
1615 const SkPoint texs[], const SkColor colors[],
1616 SkXfermode* xmode,
1617 const uint16_t indices[], int indexCount,
1618 const SkPaint& paint) {
1619 CHECK_SHOULD_DRAW(draw, false);
1620
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001621 // If both textures and vertex-colors are NULL, strokes hairlines with the paint's color.
1622 if ((NULL == texs || NULL == paint.getShader()) && NULL == colors) {
1623 texs = NULL;
1624 SkPaint copy(paint);
1625 copy.setStyle(SkPaint::kStroke_Style);
1626 copy.setStrokeWidth(0);
1627
1628 VertState state(vertexCount, indices, indexCount);
1629 VertState::Proc vertProc = state.chooseProc(vmode);
1630
1631 SkPoint* pts = new SkPoint[vertexCount * 6];
1632 int i = 0;
1633 while (vertProc(&state)) {
1634 pts[i] = vertices[state.f0];
1635 pts[i + 1] = vertices[state.f1];
1636 pts[i + 2] = vertices[state.f1];
1637 pts[i + 3] = vertices[state.f2];
1638 pts[i + 4] = vertices[state.f2];
1639 pts[i + 5] = vertices[state.f0];
1640 i += 6;
1641 }
1642 draw.drawPoints(SkCanvas::kLines_PointMode, i, pts, copy, true);
1643 return;
1644 }
1645
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001646 GrPaint grPaint;
1647 // we ignore the shader if texs is null.
1648 if (NULL == texs) {
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001649 SkPaint2GrPaintNoShader(this->context(), paint, false, NULL == colors, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001650 } else {
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001651 SkPaint2GrPaintShader(this->context(), paint, NULL == colors, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001652 }
1653
mtklein2583b622014-06-04 08:20:41 -07001654#if 0
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001655 if (NULL != xmode && NULL != texs && NULL != colors) {
1656 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1657 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
mtklein2583b622014-06-04 08:20:41 -07001658 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001659 }
1660 }
mtklein2583b622014-06-04 08:20:41 -07001661#endif
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001662
1663 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1664 if (NULL != colors) {
1665 // need to convert byte order and from non-PM to PM
1666 convertedColors.reset(vertexCount);
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001667 SkColor color;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001668 for (int i = 0; i < vertexCount; ++i) {
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001669 color = colors[i];
1670 if (paint.getAlpha() != 255) {
1671 color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paint.getAlpha()));
1672 }
1673 convertedColors[i] = SkColor2GrColor(color);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001674 }
1675 colors = convertedColors.get();
1676 }
1677 fContext->drawVertices(grPaint,
1678 gVertexMode2PrimitiveType[vmode],
1679 vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001680 vertices,
1681 texs,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001682 colors,
1683 indices,
1684 indexCount);
1685}
1686
1687///////////////////////////////////////////////////////////////////////////////
1688
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001689void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1690 size_t byteLength, SkScalar x, SkScalar y,
1691 const SkPaint& paint) {
1692 CHECK_SHOULD_DRAW(draw, false);
1693
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001694 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001695 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001696 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001697
1698 SkDEBUGCODE(this->validate();)
1699
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001700 fMainTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
1701 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001702 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001703 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001704
1705 SkDEBUGCODE(this->validate();)
1706
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001707 fFallbackTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001708 } else {
1709 // this guy will just call our drawPath()
1710 draw.drawText_asPaths((const char*)text, byteLength, x, y, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001711 }
1712}
1713
1714void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1715 size_t byteLength, const SkScalar pos[],
1716 SkScalar constY, int scalarsPerPos,
1717 const SkPaint& paint) {
1718 CHECK_SHOULD_DRAW(draw, false);
1719
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001720 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001721 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001722 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001723
1724 SkDEBUGCODE(this->validate();)
1725
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001726 fMainTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001727 constY, scalarsPerPos);
1728 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001729 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001730 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001731
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001732 SkDEBUGCODE(this->validate();)
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001733
1734 fFallbackTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001735 constY, scalarsPerPos);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001736 } else {
1737 draw.drawPosText_asPaths((const char*)text, byteLength, pos, constY,
1738 scalarsPerPos, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001739 }
1740}
1741
1742void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1743 size_t len, const SkPath& path,
1744 const SkMatrix* m, const SkPaint& paint) {
1745 CHECK_SHOULD_DRAW(draw, false);
1746
1747 SkASSERT(draw.fDevice == this);
1748 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1749}
1750
1751///////////////////////////////////////////////////////////////////////////////
1752
1753bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1754 if (!paint.isLCDRenderText()) {
1755 // we're cool with the paint as is
1756 return false;
1757 }
1758
1759 if (paint.getShader() ||
1760 paint.getXfermode() || // unless its srcover
1761 paint.getMaskFilter() ||
1762 paint.getRasterizer() ||
1763 paint.getColorFilter() ||
1764 paint.getPathEffect() ||
1765 paint.isFakeBoldText() ||
1766 paint.getStyle() != SkPaint::kFill_Style) {
1767 // turn off lcd
1768 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1769 flags->fHinting = paint.getHinting();
1770 return true;
1771 }
1772 // we're cool with the paint as is
1773 return false;
1774}
1775
1776void SkGpuDevice::flush() {
1777 DO_DEFERRED_CLEAR();
1778 fContext->resolveRenderTarget(fRenderTarget);
1779}
1780
1781///////////////////////////////////////////////////////////////////////////////
1782
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001783SkBaseDevice* SkGpuDevice::onCreateDevice(const SkImageInfo& info, Usage usage) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001784 GrTextureDesc desc;
1785 desc.fConfig = fRenderTarget->config();
1786 desc.fFlags = kRenderTarget_GrTextureFlagBit;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001787 desc.fWidth = info.width();
1788 desc.fHeight = info.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001789 desc.fSampleCnt = fRenderTarget->numSamples();
1790
1791 SkAutoTUnref<GrTexture> texture;
1792 // Skia's convention is to only clear a device if it is non-opaque.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001793 unsigned flags = info.isOpaque() ? 0 : kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001794
1795#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1796 // layers are never draw in repeat modes, so we can request an approx
1797 // match and ignore any padding.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001798 flags |= kCached_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001799 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == usage) ?
1800 GrContext::kApprox_ScratchTexMatch :
1801 GrContext::kExact_ScratchTexMatch;
1802 texture.reset(fContext->lockAndRefScratchTexture(desc, match));
1803#else
1804 texture.reset(fContext->createUncachedTexture(desc, NULL, 0));
1805#endif
1806 if (NULL != texture.get()) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001807 return SkGpuDevice::Create(texture, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001808 } else {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001809 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1810 info.width(), info.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001811 return NULL;
1812 }
1813}
1814
reed@google.com76f10a32014-02-05 15:32:21 +00001815SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info) {
1816 return SkSurface::NewRenderTarget(fContext, info, fRenderTarget->numSamples());
1817}
1818
robertphillips9b14f262014-06-04 05:40:44 -07001819void SkGpuDevice::EXPERIMENTAL_optimize(const SkPicture* picture) {
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001820 SkPicture::AccelData::Key key = GPUAccelData::ComputeAccelDataKey();
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001821
commit-bot@chromium.org8ec8bab2014-05-14 13:11:48 +00001822 const SkPicture::AccelData* existing = picture->EXPERIMENTAL_getAccelData(key);
1823 if (NULL != existing) {
1824 return;
1825 }
1826
commit-bot@chromium.org8fd93822014-05-06 13:43:22 +00001827 SkAutoTUnref<GPUAccelData> data(SkNEW_ARGS(GPUAccelData, (key)));
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001828
1829 picture->EXPERIMENTAL_addAccelData(data);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001830
1831 GatherGPUInfo(picture, data);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001832}
1833
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001834static void wrap_texture(GrTexture* texture, int width, int height, SkBitmap* result) {
1835 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001836 result->setInfo(info);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001837 result->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref();
1838}
1839
robertphillips9b14f262014-06-04 05:40:44 -07001840void SkGpuDevice::EXPERIMENTAL_purge(const SkPicture* picture) {
commit-bot@chromium.orgc8733292014-04-11 15:54:14 +00001841
1842}
1843
robertphillips9b14f262014-06-04 05:40:44 -07001844bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* canvas, const SkPicture* picture) {
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001845
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001846 SkPicture::AccelData::Key key = GPUAccelData::ComputeAccelDataKey();
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001847
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001848 const SkPicture::AccelData* data = picture->EXPERIMENTAL_getAccelData(key);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001849 if (NULL == data) {
1850 return false;
1851 }
1852
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001853 const GPUAccelData *gpuData = static_cast<const GPUAccelData*>(data);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001854
commit-bot@chromium.org8ec8bab2014-05-14 13:11:48 +00001855 if (0 == gpuData->numSaveLayers()) {
1856 return false;
1857 }
1858
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001859 SkAutoTArray<bool> pullForward(gpuData->numSaveLayers());
1860 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1861 pullForward[i] = false;
1862 }
1863
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001864 SkRect clipBounds;
1865 if (!canvas->getClipBounds(&clipBounds)) {
1866 return true;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001867 }
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001868 SkIRect query;
1869 clipBounds.roundOut(&query);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001870
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001871 const SkPicture::OperationList& ops = picture->EXPERIMENTAL_getActiveOps(query);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001872
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001873 // This code pre-renders the entire layer since it will be cached and potentially
1874 // reused with different clips (e.g., in different tiles). Because of this the
1875 // clip will not be limiting the size of the pre-rendered layer. kSaveLayerMaxSize
1876 // is used to limit which clips are pre-rendered.
1877 static const int kSaveLayerMaxSize = 256;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001878
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001879 if (ops.valid()) {
1880 // In this case the picture has been generated with a BBH so we use
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001881 // the BBH to limit the pre-rendering to just the layers needed to cover
1882 // the region being drawn
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001883 for (int i = 0; i < ops.numOps(); ++i) {
1884 uint32_t offset = ops.offset(i);
1885
1886 // For now we're saving all the layers in the GPUAccelData so they
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001887 // can be nested. Additionally, the nested layers appear before
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001888 // their parent in the list.
1889 for (int j = 0 ; j < gpuData->numSaveLayers(); ++j) {
1890 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(j);
1891
1892 if (pullForward[j]) {
1893 continue; // already pulling forward
1894 }
1895
1896 if (offset < info.fSaveLayerOpID || offset > info.fRestoreOpID) {
1897 continue; // the op isn't in this range
1898 }
1899
1900 // TODO: once this code is more stable unsuitable layers can
1901 // just be omitted during the optimization stage
1902 if (!info.fValid ||
1903 kSaveLayerMaxSize < info.fSize.fWidth ||
1904 kSaveLayerMaxSize < info.fSize.fHeight ||
1905 info.fIsNested) {
1906 continue; // this layer is unsuitable
1907 }
1908
1909 pullForward[j] = true;
1910 }
1911 }
1912 } else {
1913 // In this case there is no BBH associated with the picture. Pre-render
commit-bot@chromium.orgf97d65d2014-05-08 23:24:05 +00001914 // all the layers that intersect the drawn region
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001915 for (int j = 0; j < gpuData->numSaveLayers(); ++j) {
1916 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(j);
1917
commit-bot@chromium.orgf97d65d2014-05-08 23:24:05 +00001918 SkIRect layerRect = SkIRect::MakeXYWH(info.fOffset.fX,
1919 info.fOffset.fY,
1920 info.fSize.fWidth,
1921 info.fSize.fHeight);
1922
1923 if (!SkIRect::Intersects(query, layerRect)) {
1924 continue;
1925 }
1926
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001927 // TODO: once this code is more stable unsuitable layers can
1928 // just be omitted during the optimization stage
1929 if (!info.fValid ||
1930 kSaveLayerMaxSize < info.fSize.fWidth ||
1931 kSaveLayerMaxSize < info.fSize.fHeight ||
1932 info.fIsNested) {
1933 continue;
1934 }
1935
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001936 pullForward[j] = true;
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001937 }
1938 }
1939
1940 SkPicturePlayback::PlaybackReplacements replacements;
1941
1942 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1943 if (pullForward[i]) {
1944 GrCachedLayer* layer = fContext->getLayerCache()->findLayerOrCreate(picture, i);
1945
1946 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(i);
1947
1948 if (NULL != picture->fPlayback) {
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001949 SkPicturePlayback::PlaybackReplacements::ReplacementInfo* layerInfo =
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001950 replacements.push();
1951 layerInfo->fStart = info.fSaveLayerOpID;
1952 layerInfo->fStop = info.fRestoreOpID;
1953 layerInfo->fPos = info.fOffset;
1954
1955 GrTextureDesc desc;
1956 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1957 desc.fWidth = info.fSize.fWidth;
1958 desc.fHeight = info.fSize.fHeight;
1959 desc.fConfig = kSkia8888_GrPixelConfig;
1960 // TODO: need to deal with sample count
1961
1962 bool bNeedsRendering = true;
1963
1964 // This just uses scratch textures and doesn't cache the texture.
1965 // This can yield a lot of re-rendering
1966 if (NULL == layer->getTexture()) {
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001967 layer->setTexture(fContext->lockAndRefScratchTexture(desc,
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001968 GrContext::kApprox_ScratchTexMatch));
1969 if (NULL == layer->getTexture()) {
1970 continue;
1971 }
1972 } else {
1973 bNeedsRendering = false;
1974 }
1975
1976 layerInfo->fBM = SkNEW(SkBitmap);
1977 wrap_texture(layer->getTexture(), desc.fWidth, desc.fHeight, layerInfo->fBM);
1978
1979 SkASSERT(info.fPaint);
1980 layerInfo->fPaint = info.fPaint;
1981
1982 if (bNeedsRendering) {
1983 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTargetDirect(
1984 layer->getTexture()->asRenderTarget()));
1985
1986 SkCanvas* canvas = surface->getCanvas();
1987
1988 canvas->setMatrix(info.fCTM);
1989 canvas->clear(SK_ColorTRANSPARENT);
1990
1991 picture->fPlayback->setDrawLimits(info.fSaveLayerOpID, info.fRestoreOpID);
1992 picture->fPlayback->draw(*canvas, NULL);
1993 picture->fPlayback->setDrawLimits(0, 0);
1994 canvas->flush();
1995 }
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001996 }
1997 }
1998 }
1999
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002000 // Playback using new layers
2001 picture->fPlayback->setReplacements(&replacements);
2002 picture->fPlayback->draw(*canvas, NULL);
2003 picture->fPlayback->setReplacements(NULL);
2004
2005 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
2006 GrCachedLayer* layer = fContext->getLayerCache()->findLayerOrCreate(picture, i);
2007
2008 if (NULL != layer->getTexture()) {
2009 fContext->unlockScratchTexture(layer->getTexture());
2010 layer->setTexture(NULL);
2011 }
2012 }
2013
2014 return true;
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00002015}