blob: add0fa744f09a4c3c66f193cb8f3b7ad2223e084 [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
commit-bot@chromium.org82139702014-03-10 22:53:20 +000023#include "SkBounder.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000024#include "SkDeviceImageFilterProxy.h"
25#include "SkDrawProcs.h"
26#include "SkGlyphCache.h"
27#include "SkImageFilter.h"
commit-bot@chromium.org82139702014-03-10 22:53:20 +000028#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000029#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000030#include "SkPicture.h"
robertphillips@google.combeb1af22014-05-07 21:31:09 +000031#include "SkPicturePlayback.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000032#include "SkRRect.h"
33#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000034#include "SkSurface.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000035#include "SkTLazy.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000036#include "SkUtils.h"
commit-bot@chromium.org559a8832014-05-30 10:08:22 +000037#include "SkVertState.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000038#include "SkErrorInternals.h"
39
40#define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
41
42#if 0
43 extern bool (*gShouldDrawProc)();
44 #define CHECK_SHOULD_DRAW(draw, forceI) \
45 do { \
46 if (gShouldDrawProc && !gShouldDrawProc()) return; \
47 this->prepareDraw(draw, forceI); \
48 } while (0)
49#else
50 #define CHECK_SHOULD_DRAW(draw, forceI) this->prepareDraw(draw, forceI)
51#endif
52
53// This constant represents the screen alignment criterion in texels for
54// requiring texture domain clamping to prevent color bleeding when drawing
55// a sub region of a larger source image.
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000056#define COLOR_BLEED_TOLERANCE 0.001f
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000057
58#define DO_DEFERRED_CLEAR() \
59 do { \
60 if (fNeedClear) { \
61 this->clear(SK_ColorTRANSPARENT); \
62 } \
63 } while (false) \
64
65///////////////////////////////////////////////////////////////////////////////
66
67#define CHECK_FOR_ANNOTATION(paint) \
68 do { if (paint.getAnnotation()) { return; } } while (0)
69
70///////////////////////////////////////////////////////////////////////////////
71
72
73class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
74public:
75 SkAutoCachedTexture()
76 : fDevice(NULL)
77 , fTexture(NULL) {
78 }
79
80 SkAutoCachedTexture(SkGpuDevice* device,
81 const SkBitmap& bitmap,
82 const GrTextureParams* params,
83 GrTexture** texture)
84 : fDevice(NULL)
85 , fTexture(NULL) {
86 SkASSERT(NULL != texture);
87 *texture = this->set(device, bitmap, params);
88 }
89
90 ~SkAutoCachedTexture() {
91 if (NULL != fTexture) {
92 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
93 }
94 }
95
96 GrTexture* set(SkGpuDevice* device,
97 const SkBitmap& bitmap,
98 const GrTextureParams* params) {
99 if (NULL != fTexture) {
100 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
101 fTexture = NULL;
102 }
103 fDevice = device;
104 GrTexture* result = (GrTexture*)bitmap.getTexture();
105 if (NULL == result) {
106 // Cannot return the native texture so look it up in our cache
107 fTexture = GrLockAndRefCachedBitmapTexture(device->context(), bitmap, params);
108 result = fTexture;
109 }
110 return result;
111 }
112
113private:
114 SkGpuDevice* fDevice;
115 GrTexture* fTexture;
116};
117
118///////////////////////////////////////////////////////////////////////////////
119
120struct GrSkDrawProcs : public SkDrawProcs {
121public:
122 GrContext* fContext;
123 GrTextContext* fTextContext;
124 GrFontScaler* fFontScaler; // cached in the skia glyphcache
125};
126
127///////////////////////////////////////////////////////////////////////////////
128
129static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
130 switch (config) {
131 case kAlpha_8_GrPixelConfig:
132 *isOpaque = false;
133 return SkBitmap::kA8_Config;
134 case kRGB_565_GrPixelConfig:
135 *isOpaque = true;
136 return SkBitmap::kRGB_565_Config;
137 case kRGBA_4444_GrPixelConfig:
138 *isOpaque = false;
139 return SkBitmap::kARGB_4444_Config;
140 case kSkia8888_GrPixelConfig:
141 // we don't currently have a way of knowing whether
142 // a 8888 is opaque based on the config.
143 *isOpaque = false;
144 return SkBitmap::kARGB_8888_Config;
145 default:
146 *isOpaque = false;
147 return SkBitmap::kNo_Config;
148 }
149}
150
151/*
152 * GrRenderTarget does not know its opaqueness, only its config, so we have
153 * to make conservative guesses when we return an "equivalent" bitmap.
154 */
155static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
156 bool isOpaque;
157 SkBitmap::Config config = grConfig2skConfig(renderTarget->config(), &isOpaque);
158
159 SkBitmap bitmap;
160 bitmap.setConfig(config, renderTarget->width(), renderTarget->height(), 0,
161 isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
162 return bitmap;
163}
164
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000165SkGpuDevice* SkGpuDevice::Create(GrSurface* surface, unsigned flags) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000166 SkASSERT(NULL != surface);
167 if (NULL == surface->asRenderTarget() || NULL == surface->getContext()) {
168 return NULL;
169 }
170 if (surface->asTexture()) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000171 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asTexture(), flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000172 } else {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000173 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asRenderTarget(), flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000174 }
175}
176
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000177SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture, unsigned flags)
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000178 : SkBitmapDevice(make_bitmap(context, texture->asRenderTarget())) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000179 this->initFromRenderTarget(context, texture->asRenderTarget(), flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000180}
181
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000182SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget, unsigned flags)
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000183 : SkBitmapDevice(make_bitmap(context, renderTarget)) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000184 this->initFromRenderTarget(context, renderTarget, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000185}
186
187void SkGpuDevice::initFromRenderTarget(GrContext* context,
188 GrRenderTarget* renderTarget,
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000189 unsigned flags) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000190 fDrawProcs = NULL;
191
192 fContext = context;
193 fContext->ref();
194
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +0000195 bool useDFFonts = !!(flags & kDFFonts_Flag);
196 fMainTextContext = SkNEW_ARGS(GrDistanceFieldTextContext, (fContext, fLeakyProperties,
197 useDFFonts));
commit-bot@chromium.org47841822014-03-27 14:19:17 +0000198 fFallbackTextContext = SkNEW_ARGS(GrBitmapTextContext, (fContext, fLeakyProperties));
199
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000200 fRenderTarget = NULL;
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000201 fNeedClear = flags & kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000202
203 SkASSERT(NULL != renderTarget);
204 fRenderTarget = renderTarget;
205 fRenderTarget->ref();
206
207 // Hold onto to the texture in the pixel ref (if there is one) because the texture holds a ref
208 // on the RT but not vice-versa.
209 // TODO: Remove this trickery once we figure out how to make SkGrPixelRef do this without
210 // busting chrome (for a currently unknown reason).
211 GrSurface* surface = fRenderTarget->asTexture();
212 if (NULL == surface) {
213 surface = fRenderTarget;
214 }
reed@google.combf790232013-12-13 19:45:58 +0000215
216 SkImageInfo info;
217 surface->asImageInfo(&info);
bungeman@google.coma95a0662014-03-19 23:26:50 +0000218 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (info, surface, SkToBool(flags & kCached_Flag)));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000219
reed@google.com672588b2014-01-08 15:42:01 +0000220 this->setPixelRef(pr)->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000221}
222
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000223SkGpuDevice* SkGpuDevice::Create(GrContext* context, const SkImageInfo& origInfo,
224 int sampleCount) {
225 if (kUnknown_SkColorType == origInfo.colorType() ||
226 origInfo.width() < 0 || origInfo.height() < 0) {
227 return NULL;
228 }
229
230 SkImageInfo info = origInfo;
231 // TODO: perhas we can loosen this check now that colortype is more detailed
232 // e.g. can we support both RGBA and BGRA here?
233 if (kRGB_565_SkColorType == info.colorType()) {
234 info.fAlphaType = kOpaque_SkAlphaType; // force this setting
235 } else {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000236 info.fColorType = kN32_SkColorType;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000237 if (kOpaque_SkAlphaType != info.alphaType()) {
238 info.fAlphaType = kPremul_SkAlphaType; // force this setting
239 }
240 }
241
242 GrTextureDesc desc;
243 desc.fFlags = kRenderTarget_GrTextureFlagBit;
244 desc.fWidth = info.width();
245 desc.fHeight = info.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000246 desc.fConfig = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000247 desc.fSampleCnt = sampleCount;
248
249 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0));
250 if (!texture.get()) {
251 return NULL;
252 }
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000253
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000254 return SkNEW_ARGS(SkGpuDevice, (context, texture.get()));
255}
256
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000257SkGpuDevice::~SkGpuDevice() {
258 if (fDrawProcs) {
259 delete fDrawProcs;
260 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000261
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000262 delete fMainTextContext;
263 delete fFallbackTextContext;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000264
265 // The GrContext takes a ref on the target. We don't want to cause the render
266 // target to be unnecessarily kept alive.
267 if (fContext->getRenderTarget() == fRenderTarget) {
268 fContext->setRenderTarget(NULL);
269 }
270
271 if (fContext->getClip() == &fClipData) {
272 fContext->setClip(NULL);
273 }
274
275 SkSafeUnref(fRenderTarget);
276 fContext->unref();
277}
278
279///////////////////////////////////////////////////////////////////////////////
280
281void SkGpuDevice::makeRenderTargetCurrent() {
282 DO_DEFERRED_CLEAR();
283 fContext->setRenderTarget(fRenderTarget);
284}
285
286///////////////////////////////////////////////////////////////////////////////
287
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000288bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
289 int x, int y) {
290 DO_DEFERRED_CLEAR();
291
292 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000293 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000294 if (kUnknown_GrPixelConfig == config) {
295 return false;
296 }
297
298 uint32_t flags = 0;
299 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
300 flags = GrContext::kUnpremul_PixelOpsFlag;
301 }
302 return fContext->readRenderTargetPixels(fRenderTarget, x, y, dstInfo.width(), dstInfo.height(),
303 config, dstPixels, dstRowBytes, flags);
304}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000305
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000306bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
307 int x, int y) {
308 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000309 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000310 if (kUnknown_GrPixelConfig == config) {
311 return false;
312 }
313 uint32_t flags = 0;
314 if (kUnpremul_SkAlphaType == info.alphaType()) {
315 flags = GrContext::kUnpremul_PixelOpsFlag;
316 }
317 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
318
319 // need to bump our genID for compatibility with clients that "know" we have a bitmap
320 this->onAccessBitmap().notifyPixelsChanged();
321
322 return true;
323}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000324
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000325const SkBitmap& SkGpuDevice::onAccessBitmap() {
326 DO_DEFERRED_CLEAR();
327 return INHERITED::onAccessBitmap();
328}
329
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000330void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
331 INHERITED::onAttachToCanvas(canvas);
332
333 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
334 fClipData.fClipStack = canvas->getClipStack();
335}
336
337void SkGpuDevice::onDetachFromCanvas() {
338 INHERITED::onDetachFromCanvas();
339 fClipData.fClipStack = NULL;
340}
341
342// call this every draw call, to ensure that the context reflects our state,
343// and not the state from some other canvas/device
344void SkGpuDevice::prepareDraw(const SkDraw& draw, bool forceIdentity) {
345 SkASSERT(NULL != fClipData.fClipStack);
346
347 fContext->setRenderTarget(fRenderTarget);
348
349 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
350
351 if (forceIdentity) {
352 fContext->setIdentityMatrix();
353 } else {
354 fContext->setMatrix(*draw.fMatrix);
355 }
356 fClipData.fOrigin = this->getOrigin();
357
358 fContext->setClip(&fClipData);
359
360 DO_DEFERRED_CLEAR();
361}
362
363GrRenderTarget* SkGpuDevice::accessRenderTarget() {
364 DO_DEFERRED_CLEAR();
365 return fRenderTarget;
366}
367
368///////////////////////////////////////////////////////////////////////////////
369
370SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
371SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
372SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
373SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
374SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
375 shader_type_mismatch);
376SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
377 shader_type_mismatch);
378SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
379SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
380
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000381///////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000382
reeda6a8f002014-06-02 05:45:31 -0700383#ifdef SK_SUPPORT_LEGACY_DEVICE_CONFIG
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000384SkBitmap::Config SkGpuDevice::config() const {
385 if (NULL == fRenderTarget) {
386 return SkBitmap::kNo_Config;
387 }
388
389 bool isOpaque;
390 return grConfig2skConfig(fRenderTarget->config(), &isOpaque);
391}
reeda6a8f002014-06-02 05:45:31 -0700392#endif
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000393
394void SkGpuDevice::clear(SkColor color) {
395 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
396 fContext->clear(&rect, SkColor2GrColor(color), true, fRenderTarget);
397 fNeedClear = false;
398}
399
400void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
401 CHECK_SHOULD_DRAW(draw, false);
402
403 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000404 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000405
406 fContext->drawPaint(grPaint);
407}
408
409// must be in SkCanvas::PointMode order
410static const GrPrimitiveType gPointMode2PrimtiveType[] = {
411 kPoints_GrPrimitiveType,
412 kLines_GrPrimitiveType,
413 kLineStrip_GrPrimitiveType
414};
415
416void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
417 size_t count, const SkPoint pts[], const SkPaint& paint) {
418 CHECK_FOR_ANNOTATION(paint);
419 CHECK_SHOULD_DRAW(draw, false);
420
421 SkScalar width = paint.getStrokeWidth();
422 if (width < 0) {
423 return;
424 }
425
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000426 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000427 if (GrDashingEffect::DrawDashLine(pts, paint, this->context())) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000428 return;
429 }
430 }
431
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000432 // we only handle hairlines and paints without path effects or mask filters,
433 // else we let the SkDraw call our drawPath()
434 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
435 draw.drawPoints(mode, count, pts, paint, true);
436 return;
437 }
438
439 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000440 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000441
442 fContext->drawVertices(grPaint,
443 gPointMode2PrimtiveType[mode],
robertphillips@google.coma4662862013-11-21 14:24:16 +0000444 SkToS32(count),
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000445 (SkPoint*)pts,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000446 NULL,
447 NULL,
448 NULL,
449 0);
450}
451
452///////////////////////////////////////////////////////////////////////////////
453
454void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
455 const SkPaint& paint) {
456 CHECK_FOR_ANNOTATION(paint);
457 CHECK_SHOULD_DRAW(draw, false);
458
459 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
460 SkScalar width = paint.getStrokeWidth();
461
462 /*
463 We have special code for hairline strokes, miter-strokes, bevel-stroke
464 and fills. Anything else we just call our path code.
465 */
466 bool usePath = doStroke && width > 0 &&
467 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
468 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
469 // another two reasons we might need to call drawPath...
470 if (paint.getMaskFilter() || paint.getPathEffect()) {
471 usePath = true;
472 }
473 if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) {
474#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
475 if (doStroke) {
476#endif
477 usePath = true;
478#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
479 } else {
480 usePath = !fContext->getMatrix().preservesRightAngles();
481 }
482#endif
483 }
484 // until we can both stroke and fill rectangles
485 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
486 usePath = true;
487 }
488
489 if (usePath) {
490 SkPath path;
491 path.addRect(rect);
492 this->drawPath(draw, path, paint, NULL, true);
493 return;
494 }
495
496 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000497 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000498
499 if (!doStroke) {
500 fContext->drawRect(grPaint, rect);
501 } else {
502 SkStrokeRec stroke(paint);
503 fContext->drawRect(grPaint, rect, &stroke);
504 }
505}
506
507///////////////////////////////////////////////////////////////////////////////
508
509void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
510 const SkPaint& paint) {
511 CHECK_FOR_ANNOTATION(paint);
512 CHECK_SHOULD_DRAW(draw, false);
513
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000514 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000515 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000516
517 SkStrokeRec stroke(paint);
518 if (paint.getMaskFilter()) {
519 // try to hit the fast path for drawing filtered round rects
520
521 SkRRect devRRect;
522 if (rect.transform(fContext->getMatrix(), &devRRect)) {
523 if (devRRect.allCornersCircular()) {
524 SkRect maskRect;
525 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(),
526 draw.fClip->getBounds(),
527 fContext->getMatrix(),
528 &maskRect)) {
529 SkIRect finalIRect;
530 maskRect.roundOut(&finalIRect);
531 if (draw.fClip->quickReject(finalIRect)) {
532 // clipped out
533 return;
534 }
535 if (NULL != draw.fBounder && !draw.fBounder->doIRect(finalIRect)) {
536 // nothing to draw
537 return;
538 }
539 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext, &grPaint,
540 stroke, devRRect)) {
541 return;
542 }
543 }
544
545 }
546 }
547
548 }
549
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000550 if (paint.getMaskFilter() || paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000551 SkPath path;
552 path.addRRect(rect);
553 this->drawPath(draw, path, paint, NULL, true);
554 return;
555 }
556
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000557 fContext->drawRRect(grPaint, rect, stroke);
558}
559
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000560void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
561 const SkRRect& inner, const SkPaint& paint) {
562 SkStrokeRec stroke(paint);
563 if (stroke.isFillStyle()) {
564
565 CHECK_FOR_ANNOTATION(paint);
566 CHECK_SHOULD_DRAW(draw, false);
567
568 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000569 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000570
571 if (NULL == paint.getMaskFilter() && NULL == paint.getPathEffect()) {
572 fContext->drawDRRect(grPaint, outer, inner);
573 return;
574 }
575 }
576
577 SkPath path;
578 path.addRRect(outer);
579 path.addRRect(inner);
580 path.setFillType(SkPath::kEvenOdd_FillType);
581
582 this->drawPath(draw, path, paint, NULL, true);
583}
584
585
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000586/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000587
588void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
589 const SkPaint& paint) {
590 CHECK_FOR_ANNOTATION(paint);
591 CHECK_SHOULD_DRAW(draw, false);
592
593 bool usePath = false;
594 // some basic reasons we might need to call drawPath...
595 if (paint.getMaskFilter() || paint.getPathEffect()) {
596 usePath = true;
597 }
598
599 if (usePath) {
600 SkPath path;
601 path.addOval(oval);
602 this->drawPath(draw, path, paint, NULL, true);
603 return;
604 }
605
606 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000607 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000608 SkStrokeRec stroke(paint);
609
610 fContext->drawOval(grPaint, oval, stroke);
611}
612
613#include "SkMaskFilter.h"
614#include "SkBounder.h"
615
616///////////////////////////////////////////////////////////////////////////////
617
618// helpers for applying mask filters
619namespace {
620
621// Draw a mask using the supplied paint. Since the coverage/geometry
622// is already burnt into the mask this boils down to a rect draw.
623// Return true if the mask was successfully drawn.
624bool draw_mask(GrContext* context, const SkRect& maskRect,
625 GrPaint* grp, GrTexture* mask) {
626 GrContext::AutoMatrix am;
627 if (!am.setIdentity(context, grp)) {
628 return false;
629 }
630
631 SkMatrix matrix;
632 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
633 matrix.postIDiv(mask->width(), mask->height());
634
635 grp->addCoverageEffect(GrSimpleTextureEffect::Create(mask, matrix))->unref();
636 context->drawRect(*grp, maskRect);
637 return true;
638}
639
640bool draw_with_mask_filter(GrContext* context, const SkPath& devPath,
641 SkMaskFilter* filter, const SkRegion& clip, SkBounder* bounder,
642 GrPaint* grp, SkPaint::Style style) {
643 SkMask srcM, dstM;
644
645 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
646 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
647 return false;
648 }
649 SkAutoMaskFreeImage autoSrc(srcM.fImage);
650
651 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
652 return false;
653 }
654 // this will free-up dstM when we're done (allocated in filterMask())
655 SkAutoMaskFreeImage autoDst(dstM.fImage);
656
657 if (clip.quickReject(dstM.fBounds)) {
658 return false;
659 }
660 if (bounder && !bounder->doIRect(dstM.fBounds)) {
661 return false;
662 }
663
664 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
665 // the current clip (and identity matrix) and GrPaint settings
666 GrTextureDesc desc;
667 desc.fWidth = dstM.fBounds.width();
668 desc.fHeight = dstM.fBounds.height();
669 desc.fConfig = kAlpha_8_GrPixelConfig;
670
671 GrAutoScratchTexture ast(context, desc);
672 GrTexture* texture = ast.texture();
673
674 if (NULL == texture) {
675 return false;
676 }
677 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
678 dstM.fImage, dstM.fRowBytes);
679
680 SkRect maskRect = SkRect::Make(dstM.fBounds);
681
682 return draw_mask(context, maskRect, grp, texture);
683}
684
685// Create a mask of 'devPath' and place the result in 'mask'. Return true on
686// success; false otherwise.
687bool create_mask_GPU(GrContext* context,
688 const SkRect& maskRect,
689 const SkPath& devPath,
690 const SkStrokeRec& stroke,
691 bool doAA,
692 GrAutoScratchTexture* mask) {
693 GrTextureDesc desc;
694 desc.fFlags = kRenderTarget_GrTextureFlagBit;
695 desc.fWidth = SkScalarCeilToInt(maskRect.width());
696 desc.fHeight = SkScalarCeilToInt(maskRect.height());
697 // We actually only need A8, but it often isn't supported as a
698 // render target so default to RGBA_8888
699 desc.fConfig = kRGBA_8888_GrPixelConfig;
700 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
701 desc.fConfig = kAlpha_8_GrPixelConfig;
702 }
703
704 mask->set(context, desc);
705 if (NULL == mask->texture()) {
706 return false;
707 }
708
709 GrTexture* maskTexture = mask->texture();
710 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
711
712 GrContext::AutoRenderTarget art(context, maskTexture->asRenderTarget());
713 GrContext::AutoClip ac(context, clipRect);
714
715 context->clear(NULL, 0x0, true);
716
717 GrPaint tempPaint;
718 if (doAA) {
719 tempPaint.setAntiAlias(true);
720 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
721 // blend coeff of zero requires dual source blending support in order
722 // to properly blend partially covered pixels. This means the AA
723 // code path may not be taken. So we use a dst blend coeff of ISA. We
724 // could special case AA draws to a dst surface with known alpha=0 to
725 // use a zero dst coeff when dual source blending isn't available.
726 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
727 }
728
729 GrContext::AutoMatrix am;
730
731 // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint.
732 SkMatrix translate;
733 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
734 am.set(context, translate);
735 context->drawPath(tempPaint, devPath, stroke);
736 return true;
737}
738
739SkBitmap wrap_texture(GrTexture* texture) {
reed@google.combf790232013-12-13 19:45:58 +0000740 SkImageInfo info;
741 texture->asImageInfo(&info);
742
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000743 SkBitmap result;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000744 result.setInfo(info);
reed@google.combf790232013-12-13 19:45:58 +0000745 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000746 return result;
747}
748
749};
750
751void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
752 const SkPaint& paint, const SkMatrix* prePathMatrix,
753 bool pathIsMutable) {
754 CHECK_FOR_ANNOTATION(paint);
755 CHECK_SHOULD_DRAW(draw, false);
756
757 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000758 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000759
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000760 // If we have a prematrix, apply it to the path, optimizing for the case
761 // where the original path can in fact be modified in place (even though
762 // its parameter type is const).
763 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000764 SkTLazy<SkPath> tmpPath;
765 SkTLazy<SkPath> effectPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000766
767 if (prePathMatrix) {
768 SkPath* result = pathPtr;
769
770 if (!pathIsMutable) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000771 result = tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000772 pathIsMutable = true;
773 }
774 // should I push prePathMatrix on our MV stack temporarily, instead
775 // of applying it here? See SkDraw.cpp
776 pathPtr->transform(*prePathMatrix, result);
777 pathPtr = result;
778 }
779 // at this point we're done with prePathMatrix
780 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
781
782 SkStrokeRec stroke(paint);
783 SkPathEffect* pathEffect = paint.getPathEffect();
784 const SkRect* cullRect = NULL; // TODO: what is our bounds?
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000785 if (pathEffect && pathEffect->filterPath(effectPath.init(), *pathPtr, &stroke,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000786 cullRect)) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000787 pathPtr = effectPath.get();
788 pathIsMutable = true;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000789 }
790
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000791 if (paint.getMaskFilter()) {
792 if (!stroke.isHairlineStyle()) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000793 SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init();
794 if (stroke.applyToPath(strokedPath, *pathPtr)) {
795 pathPtr = strokedPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000796 pathIsMutable = true;
797 stroke.setFillStyle();
798 }
799 }
800
801 // avoid possibly allocating a new path in transform if we can
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000802 SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000803
804 // transform the path into device space
805 pathPtr->transform(fContext->getMatrix(), devPathPtr);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000806
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000807 SkRect maskRect;
808 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
809 draw.fClip->getBounds(),
810 fContext->getMatrix(),
811 &maskRect)) {
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000812 // The context's matrix may change while creating the mask, so save the CTM here to
813 // pass to filterMaskGPU.
814 const SkMatrix ctm = fContext->getMatrix();
815
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000816 SkIRect finalIRect;
817 maskRect.roundOut(&finalIRect);
818 if (draw.fClip->quickReject(finalIRect)) {
819 // clipped out
820 return;
821 }
822 if (NULL != draw.fBounder && !draw.fBounder->doIRect(finalIRect)) {
823 // nothing to draw
824 return;
825 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000826
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000827 if (paint.getMaskFilter()->directFilterMaskGPU(fContext, &grPaint,
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000828 stroke, *devPathPtr)) {
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000829 // the mask filter was able to draw itself directly, so there's nothing
830 // left to do.
831 return;
832 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000833
834 GrAutoScratchTexture mask;
835
836 if (create_mask_GPU(fContext, maskRect, *devPathPtr, stroke,
837 grPaint.isAntiAlias(), &mask)) {
838 GrTexture* filtered;
839
commit-bot@chromium.org41bf9302014-01-08 22:25:53 +0000840 if (paint.getMaskFilter()->filterMaskGPU(mask.texture(),
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000841 ctm, maskRect, &filtered, true)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000842 // filterMaskGPU gives us ownership of a ref to the result
843 SkAutoTUnref<GrTexture> atu(filtered);
844
845 // If the scratch texture that we used as the filter src also holds the filter
846 // result then we must detach so that this texture isn't recycled for a later
847 // draw.
848 if (filtered == mask.texture()) {
849 mask.detach();
850 filtered->unref(); // detach transfers GrAutoScratchTexture's ref to us.
851 }
852
853 if (draw_mask(fContext, maskRect, &grPaint, filtered)) {
854 // This path is completely drawn
855 return;
856 }
857 }
858 }
859 }
860
861 // draw the mask on the CPU - this is a fallthrough path in case the
862 // GPU path fails
863 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
864 SkPaint::kFill_Style;
865 draw_with_mask_filter(fContext, *devPathPtr, paint.getMaskFilter(),
866 *draw.fClip, draw.fBounder, &grPaint, style);
867 return;
868 }
869
870 fContext->drawPath(grPaint, *pathPtr, stroke);
871}
872
873static const int kBmpSmallTileSize = 1 << 10;
874
875static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
876 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
877 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
878 return tilesX * tilesY;
879}
880
881static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
882 if (maxTileSize <= kBmpSmallTileSize) {
883 return maxTileSize;
884 }
885
886 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
887 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
888
889 maxTileTotalTileSize *= maxTileSize * maxTileSize;
890 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
891
892 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
893 return kBmpSmallTileSize;
894 } else {
895 return maxTileSize;
896 }
897}
898
899// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
900// pixels from the bitmap are necessary.
901static void determine_clipped_src_rect(const GrContext* context,
902 const SkBitmap& bitmap,
903 const SkRect* srcRectPtr,
904 SkIRect* clippedSrcIRect) {
905 const GrClipData* clip = context->getClip();
906 clip->getConservativeBounds(context->getRenderTarget(), clippedSrcIRect, NULL);
907 SkMatrix inv;
908 if (!context->getMatrix().invert(&inv)) {
909 clippedSrcIRect->setEmpty();
910 return;
911 }
912 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
913 inv.mapRect(&clippedSrcRect);
914 if (NULL != srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000915 // we've setup src space 0,0 to map to the top left of the src rect.
916 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000917 if (!clippedSrcRect.intersect(*srcRectPtr)) {
918 clippedSrcIRect->setEmpty();
919 return;
920 }
921 }
922 clippedSrcRect.roundOut(clippedSrcIRect);
923 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
924 if (!clippedSrcIRect->intersect(bmpBounds)) {
925 clippedSrcIRect->setEmpty();
926 }
927}
928
929bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
930 const GrTextureParams& params,
931 const SkRect* srcRectPtr,
932 int maxTileSize,
933 int* tileSize,
934 SkIRect* clippedSrcRect) const {
935 // if bitmap is explictly texture backed then just use the texture
936 if (NULL != bitmap.getTexture()) {
937 return false;
938 }
939
940 // if it's larger than the max tile size, then we have no choice but tiling.
941 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
942 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
943 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
944 return true;
945 }
946
947 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
948 return false;
949 }
950
951 // if the entire texture is already in our cache then no reason to tile it
952 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
953 return false;
954 }
955
956 // At this point we know we could do the draw by uploading the entire bitmap
957 // as a texture. However, if the texture would be large compared to the
958 // cache size and we don't require most of it for this draw then tile to
959 // reduce the amount of upload and cache spill.
960
961 // assumption here is that sw bitmap size is a good proxy for its size as
962 // a texture
963 size_t bmpSize = bitmap.getSize();
964 size_t cacheSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000965 fContext->getResourceCacheLimits(NULL, &cacheSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000966 if (bmpSize < cacheSize / 2) {
967 return false;
968 }
969
970 // Figure out how much of the src we will need based on the src rect and clipping.
971 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
972 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
973 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
974 kBmpSmallTileSize * kBmpSmallTileSize;
975
976 return usedTileBytes < 2 * bmpSize;
977}
978
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000979void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000980 const SkBitmap& bitmap,
981 const SkMatrix& m,
982 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000983 SkMatrix concat;
984 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
985 if (!m.isIdentity()) {
986 concat.setConcat(*draw->fMatrix, m);
987 draw.writable()->fMatrix = &concat;
988 }
989 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000990}
991
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000992// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000993// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
994// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000995static inline void clamped_outset_with_offset(SkIRect* iRect,
996 int outset,
997 SkPoint* offset,
998 const SkIRect& clamp) {
999 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001000
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001001 int leftClampDelta = clamp.fLeft - iRect->fLeft;
1002 if (leftClampDelta > 0) {
1003 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001004 iRect->fLeft = clamp.fLeft;
1005 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001006 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001007 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001008
1009 int topClampDelta = clamp.fTop - iRect->fTop;
1010 if (topClampDelta > 0) {
1011 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001012 iRect->fTop = clamp.fTop;
1013 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001014 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001015 }
1016
1017 if (iRect->fRight > clamp.fRight) {
1018 iRect->fRight = clamp.fRight;
1019 }
1020 if (iRect->fBottom > clamp.fBottom) {
1021 iRect->fBottom = clamp.fBottom;
1022 }
1023}
1024
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001025static bool has_aligned_samples(const SkRect& srcRect,
1026 const SkRect& transformedRect) {
1027 // detect pixel disalignment
1028 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1029 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1030 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1031 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1032 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1033 COLOR_BLEED_TOLERANCE &&
1034 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1035 COLOR_BLEED_TOLERANCE) {
1036 return true;
1037 }
1038 return false;
1039}
1040
1041static bool may_color_bleed(const SkRect& srcRect,
1042 const SkRect& transformedRect,
1043 const SkMatrix& m) {
1044 // Only gets called if has_aligned_samples returned false.
1045 // So we can assume that sampling is axis aligned but not texel aligned.
1046 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
1047 SkRect innerSrcRect(srcRect), innerTransformedRect,
1048 outerTransformedRect(transformedRect);
1049 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1050 m.mapRect(&innerTransformedRect, innerSrcRect);
1051
1052 // The gap between outerTransformedRect and innerTransformedRect
1053 // represents the projection of the source border area, which is
1054 // problematic for color bleeding. We must check whether any
1055 // destination pixels sample the border area.
1056 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1057 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1058 SkIRect outer, inner;
1059 outerTransformedRect.round(&outer);
1060 innerTransformedRect.round(&inner);
1061 // If the inner and outer rects round to the same result, it means the
1062 // border does not overlap any pixel centers. Yay!
1063 return inner != outer;
1064}
1065
1066static bool needs_texture_domain(const SkBitmap& bitmap,
1067 const SkRect& srcRect,
1068 GrTextureParams &params,
1069 const SkMatrix& contextMatrix,
1070 bool bicubic) {
1071 bool needsTextureDomain = false;
1072
1073 if (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode) {
1074 // Need texture domain if drawing a sub rect
1075 needsTextureDomain = srcRect.width() < bitmap.width() ||
1076 srcRect.height() < bitmap.height();
1077 if (!bicubic && needsTextureDomain && contextMatrix.rectStaysRect()) {
1078 // sampling is axis-aligned
1079 SkRect transformedRect;
1080 contextMatrix.mapRect(&transformedRect, srcRect);
1081
1082 if (has_aligned_samples(srcRect, transformedRect)) {
1083 params.setFilterMode(GrTextureParams::kNone_FilterMode);
1084 needsTextureDomain = false;
1085 } else {
1086 needsTextureDomain = may_color_bleed(srcRect, transformedRect, contextMatrix);
1087 }
1088 }
1089 }
1090 return needsTextureDomain;
1091}
1092
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001093void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1094 const SkBitmap& bitmap,
1095 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001096 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001097 const SkPaint& paint,
1098 SkCanvas::DrawBitmapRectFlags flags) {
1099 CHECK_SHOULD_DRAW(draw, false);
1100
1101 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001102 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001103 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
1104 // in the (easier) bleed case, so update flags.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001105 if (NULL == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001106 SkScalar w = SkIntToScalar(bitmap.width());
1107 SkScalar h = SkIntToScalar(bitmap.height());
1108 dstSize.fWidth = w;
1109 dstSize.fHeight = h;
1110 srcRect.set(0, 0, w, h);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001111 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001112 } else {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001113 SkASSERT(NULL != dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001114 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001115 dstSize = *dstSizePtr;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001116 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
1117 srcRect.fRight >= bitmap.width() && srcRect.fBottom >= bitmap.height()) {
1118 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
1119 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001120 }
1121
1122 if (paint.getMaskFilter()){
1123 // Convert the bitmap to a shader so that the rect can be drawn
1124 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001125 SkBitmap tmp; // subset of bitmap, if necessary
1126 const SkBitmap* bitmapPtr = &bitmap;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001127 SkMatrix localM;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001128 if (NULL != srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001129 localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
1130 localM.postScale(dstSize.fWidth / srcRectPtr->width(),
1131 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001132 // In bleed mode we position and trim the bitmap based on the src rect which is
1133 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
1134 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
1135 // compensate.
1136 if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) {
1137 SkIRect iSrc;
1138 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001139
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001140 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
1141 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001142
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001143 if (!bitmap.extractSubset(&tmp, iSrc)) {
1144 return; // extraction failed
1145 }
1146 bitmapPtr = &tmp;
1147 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001148
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001149 // The source rect has changed so update the matrix
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001150 localM.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001151 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001152 } else {
1153 localM.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001154 }
1155
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001156 SkPaint paintWithShader(paint);
1157 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +00001158 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &localM))->unref();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001159 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1160 this->drawRect(draw, dstRect, paintWithShader);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001161
1162 return;
1163 }
1164
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001165 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
1166 // the view matrix rather than a local matrix.
1167 SkMatrix m;
1168 m.setScale(dstSize.fWidth / srcRect.width(),
1169 dstSize.fHeight / srcRect.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001170 fContext->concatMatrix(m);
1171
1172 GrTextureParams params;
1173 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel();
1174 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001175
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001176 bool doBicubic = false;
1177
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001178 switch(paintFilterLevel) {
1179 case SkPaint::kNone_FilterLevel:
1180 textureFilterMode = GrTextureParams::kNone_FilterMode;
1181 break;
1182 case SkPaint::kLow_FilterLevel:
1183 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1184 break;
1185 case SkPaint::kMedium_FilterLevel:
commit-bot@chromium.org18786512014-05-20 14:53:45 +00001186 if (fContext->getMatrix().getMinScale() < SK_Scalar1) {
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001187 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1188 } else {
1189 // Don't trigger MIP level generation unnecessarily.
1190 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1191 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001192 break;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001193 case SkPaint::kHigh_FilterLevel:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001194 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001195 doBicubic =
1196 GrBicubicEffect::ShouldUseBicubic(fContext->getMatrix(), &textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001197 break;
1198 default:
1199 SkErrorInternals::SetError( kInvalidPaint_SkError,
1200 "Sorry, I don't understand the filtering "
1201 "mode you asked for. Falling back to "
1202 "MIPMaps.");
1203 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1204 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001205 }
1206
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001207 int tileFilterPad;
1208 if (doBicubic) {
1209 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1210 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1211 tileFilterPad = 0;
1212 } else {
1213 tileFilterPad = 1;
1214 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001215 params.setFilterMode(textureFilterMode);
1216
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001217 int maxTileSize = fContext->getMaxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001218 int tileSize;
1219
1220 SkIRect clippedSrcRect;
1221 if (this->shouldTileBitmap(bitmap, params, srcRectPtr, maxTileSize, &tileSize,
1222 &clippedSrcRect)) {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001223 this->drawTiledBitmap(bitmap, srcRect, clippedSrcRect, params, paint, flags, tileSize,
1224 doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001225 } else {
1226 // take the simple case
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001227 bool needsTextureDomain = needs_texture_domain(bitmap,
1228 srcRect,
1229 params,
1230 fContext->getMatrix(),
1231 doBicubic);
1232 this->internalDrawBitmap(bitmap,
1233 srcRect,
1234 params,
1235 paint,
1236 flags,
1237 doBicubic,
1238 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001239 }
1240}
1241
1242// Break 'bitmap' into several tiles to draw it since it has already
1243// been determined to be too large to fit in VRAM
1244void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
1245 const SkRect& srcRect,
1246 const SkIRect& clippedSrcIRect,
1247 const GrTextureParams& params,
1248 const SkPaint& paint,
1249 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001250 int tileSize,
1251 bool bicubic) {
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +00001252 // The following pixel lock is technically redundant, but it is desirable
1253 // to lock outside of the tile loop to prevent redecoding the whole image
1254 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1255 // is larger than the limit of the discardable memory pool.
1256 SkAutoLockPixels alp(bitmap);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001257 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1258
1259 int nx = bitmap.width() / tileSize;
1260 int ny = bitmap.height() / tileSize;
1261 for (int x = 0; x <= nx; x++) {
1262 for (int y = 0; y <= ny; y++) {
1263 SkRect tileR;
1264 tileR.set(SkIntToScalar(x * tileSize),
1265 SkIntToScalar(y * tileSize),
1266 SkIntToScalar((x + 1) * tileSize),
1267 SkIntToScalar((y + 1) * tileSize));
1268
1269 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1270 continue;
1271 }
1272
1273 if (!tileR.intersect(srcRect)) {
1274 continue;
1275 }
1276
1277 SkBitmap tmpB;
1278 SkIRect iTileR;
1279 tileR.roundOut(&iTileR);
1280 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1281 SkIntToScalar(iTileR.fTop));
1282
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001283 // Adjust the context matrix to draw at the right x,y in device space
1284 SkMatrix tmpM;
1285 GrContext::AutoMatrix am;
1286 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
1287 am.setPreConcat(fContext, tmpM);
1288
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001289 if (SkPaint::kNone_FilterLevel != paint.getFilterLevel() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001290 SkIRect iClampRect;
1291
1292 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1293 // In bleed mode we want to always expand the tile on all edges
1294 // but stay within the bitmap bounds
1295 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1296 } else {
1297 // In texture-domain/clamp mode we only want to expand the
1298 // tile on edges interior to "srcRect" (i.e., we want to
1299 // not bleed across the original clamped edges)
1300 srcRect.roundOut(&iClampRect);
1301 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001302 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1303 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001304 }
1305
1306 if (bitmap.extractSubset(&tmpB, iTileR)) {
1307 // now offset it to make it "local" to our tmp bitmap
1308 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001309 GrTextureParams paramsTemp = params;
1310 bool needsTextureDomain = needs_texture_domain(bitmap,
1311 srcRect,
1312 paramsTemp,
1313 fContext->getMatrix(),
1314 bicubic);
1315 this->internalDrawBitmap(tmpB,
1316 tileR,
1317 paramsTemp,
1318 paint,
1319 flags,
1320 bicubic,
1321 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001322 }
1323 }
1324 }
1325}
1326
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001327
1328/*
1329 * This is called by drawBitmap(), which has to handle images that may be too
1330 * large to be represented by a single texture.
1331 *
1332 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1333 * and that non-texture portion of the GrPaint has already been setup.
1334 */
1335void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
1336 const SkRect& srcRect,
1337 const GrTextureParams& params,
1338 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001339 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001340 bool bicubic,
1341 bool needsTextureDomain) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001342 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1343 bitmap.height() <= fContext->getMaxTextureSize());
1344
1345 GrTexture* texture;
1346 SkAutoCachedTexture act(this, bitmap, &params, &texture);
1347 if (NULL == texture) {
1348 return;
1349 }
1350
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001351 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001352 SkRect paintRect;
1353 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1354 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1355 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1356 SkScalarMul(srcRect.fTop, hInv),
1357 SkScalarMul(srcRect.fRight, wInv),
1358 SkScalarMul(srcRect.fBottom, hInv));
1359
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001360 SkRect textureDomain = SkRect::MakeEmpty();
1361 SkAutoTUnref<GrEffectRef> effect;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001362 if (needsTextureDomain && !(flags & SkCanvas::kBleed_DrawBitmapRectFlag)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001363 // Use a constrained texture domain to avoid color bleeding
1364 SkScalar left, top, right, bottom;
1365 if (srcRect.width() > SK_Scalar1) {
1366 SkScalar border = SK_ScalarHalf / texture->width();
1367 left = paintRect.left() + border;
1368 right = paintRect.right() - border;
1369 } else {
1370 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1371 }
1372 if (srcRect.height() > SK_Scalar1) {
1373 SkScalar border = SK_ScalarHalf / texture->height();
1374 top = paintRect.top() + border;
1375 bottom = paintRect.bottom() - border;
1376 } else {
1377 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1378 }
1379 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001380 if (bicubic) {
1381 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
1382 } else {
1383 effect.reset(GrTextureDomainEffect::Create(texture,
1384 SkMatrix::I(),
1385 textureDomain,
1386 GrTextureDomain::kClamp_Mode,
1387 params.filterMode()));
1388 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001389 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001390 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1391 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
1392 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001393 } else {
1394 effect.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
1395 }
1396
1397 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1398 // the rest from the SkPaint.
1399 GrPaint grPaint;
1400 grPaint.addColorEffect(effect);
1401 bool alphaOnly = !(SkBitmap::kA8_Config == bitmap.config());
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001402 SkPaint2GrPaintNoShader(this->context(), paint, alphaOnly, false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001403
1404 fContext->drawRectToRect(grPaint, dstRect, paintRect, NULL);
1405}
1406
1407static bool filter_texture(SkBaseDevice* device, GrContext* context,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001408 GrTexture* texture, const SkImageFilter* filter,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001409 int w, int h, const SkImageFilter::Context& ctx,
1410 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001411 SkASSERT(filter);
1412 SkDeviceImageFilterProxy proxy(device);
1413
1414 if (filter->canFilterImageGPU()) {
1415 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1416 // filter. Also set the clip wide open and the matrix to identity.
1417 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001418 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001419 } else {
1420 return false;
1421 }
1422}
1423
1424void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1425 int left, int top, const SkPaint& paint) {
1426 // drawSprite is defined to be in device coords.
1427 CHECK_SHOULD_DRAW(draw, true);
1428
1429 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1430 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1431 return;
1432 }
1433
1434 int w = bitmap.width();
1435 int h = bitmap.height();
1436
1437 GrTexture* texture;
1438 // draw sprite uses the default texture params
1439 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
1440
1441 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001442 // This bitmap will own the filtered result as a texture.
1443 SkBitmap filteredBitmap;
1444
1445 if (NULL != filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001446 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001447 SkMatrix matrix(*draw.fMatrix);
1448 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001449 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001450 SkImageFilter::Cache* cache = SkImageFilter::Cache::Create();
1451 SkAutoUnref aur(cache);
1452 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001453 if (filter_texture(this, fContext, texture, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001454 &offset)) {
1455 texture = (GrTexture*) filteredBitmap.getTexture();
1456 w = filteredBitmap.width();
1457 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001458 left += offset.x();
1459 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001460 } else {
1461 return;
1462 }
1463 }
1464
1465 GrPaint grPaint;
1466 grPaint.addColorTextureEffect(texture, SkMatrix::I());
1467
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001468 SkPaint2GrPaintNoShader(this->context(), paint, true, false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001469
1470 fContext->drawRectToRect(grPaint,
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001471 SkRect::MakeXYWH(SkIntToScalar(left),
1472 SkIntToScalar(top),
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001473 SkIntToScalar(w),
1474 SkIntToScalar(h)),
1475 SkRect::MakeXYWH(0,
1476 0,
1477 SK_Scalar1 * w / texture->width(),
1478 SK_Scalar1 * h / texture->height()));
1479}
1480
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001481void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001482 const SkRect* src, const SkRect& dst,
1483 const SkPaint& paint,
1484 SkCanvas::DrawBitmapRectFlags flags) {
1485 SkMatrix matrix;
1486 SkRect bitmapBounds, tmpSrc;
1487
1488 bitmapBounds.set(0, 0,
1489 SkIntToScalar(bitmap.width()),
1490 SkIntToScalar(bitmap.height()));
1491
1492 // Compute matrix from the two rectangles
1493 if (NULL != src) {
1494 tmpSrc = *src;
1495 } else {
1496 tmpSrc = bitmapBounds;
1497 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001498
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001499 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1500
1501 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
1502 if (NULL != src) {
1503 if (!bitmapBounds.contains(tmpSrc)) {
1504 if (!tmpSrc.intersect(bitmapBounds)) {
1505 return; // nothing to draw
1506 }
1507 }
1508 }
1509
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001510 SkRect tmpDst;
1511 matrix.mapRect(&tmpDst, tmpSrc);
1512
1513 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1514 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1515 // Translate so that tempDst's top left is at the origin.
1516 matrix = *origDraw.fMatrix;
1517 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1518 draw.writable()->fMatrix = &matrix;
1519 }
1520 SkSize dstSize;
1521 dstSize.fWidth = tmpDst.width();
1522 dstSize.fHeight = tmpDst.height();
1523
1524 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001525}
1526
1527void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1528 int x, int y, const SkPaint& paint) {
1529 // clear of the source device must occur before CHECK_SHOULD_DRAW
1530 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1531 if (dev->fNeedClear) {
1532 // TODO: could check here whether we really need to draw at all
1533 dev->clear(0x0);
1534 }
1535
1536 // drawDevice is defined to be in device coords.
1537 CHECK_SHOULD_DRAW(draw, true);
1538
1539 GrRenderTarget* devRT = dev->accessRenderTarget();
1540 GrTexture* devTex;
1541 if (NULL == (devTex = devRT->asTexture())) {
1542 return;
1543 }
1544
1545 const SkBitmap& bm = dev->accessBitmap(false);
1546 int w = bm.width();
1547 int h = bm.height();
1548
1549 SkImageFilter* filter = paint.getImageFilter();
1550 // This bitmap will own the filtered result as a texture.
1551 SkBitmap filteredBitmap;
1552
1553 if (NULL != filter) {
1554 SkIPoint offset = SkIPoint::Make(0, 0);
1555 SkMatrix matrix(*draw.fMatrix);
1556 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001557 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001558 SkImageFilter::Cache* cache = SkImageFilter::Cache::Create();
1559 SkAutoUnref aur(cache);
1560 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001561 if (filter_texture(this, fContext, devTex, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001562 &offset)) {
1563 devTex = filteredBitmap.getTexture();
1564 w = filteredBitmap.width();
1565 h = filteredBitmap.height();
1566 x += offset.fX;
1567 y += offset.fY;
1568 } else {
1569 return;
1570 }
1571 }
1572
1573 GrPaint grPaint;
1574 grPaint.addColorTextureEffect(devTex, SkMatrix::I());
1575
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001576 SkPaint2GrPaintNoShader(this->context(), paint, true, false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001577
1578 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1579 SkIntToScalar(y),
1580 SkIntToScalar(w),
1581 SkIntToScalar(h));
1582
1583 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1584 // scratch texture).
1585 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1586 SK_Scalar1 * h / devTex->height());
1587
1588 fContext->drawRectToRect(grPaint, dstRect, srcRect);
1589}
1590
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001591bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001592 return filter->canFilterImageGPU();
1593}
1594
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001595bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001596 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001597 SkBitmap* result, SkIPoint* offset) {
1598 // want explicitly our impl, so guard against a subclass of us overriding it
1599 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1600 return false;
1601 }
1602
1603 SkAutoLockPixels alp(src, !src.getTexture());
1604 if (!src.getTexture() && !src.readyToDraw()) {
1605 return false;
1606 }
1607
1608 GrTexture* texture;
1609 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1610 // must be pushed upstack.
1611 SkAutoCachedTexture act(this, src, NULL, &texture);
1612
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001613 return filter_texture(this, fContext, texture, filter, src.width(), src.height(), ctx,
1614 result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001615}
1616
1617///////////////////////////////////////////////////////////////////////////////
1618
1619// must be in SkCanvas::VertexMode order
1620static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1621 kTriangles_GrPrimitiveType,
1622 kTriangleStrip_GrPrimitiveType,
1623 kTriangleFan_GrPrimitiveType,
1624};
1625
1626void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1627 int vertexCount, const SkPoint vertices[],
1628 const SkPoint texs[], const SkColor colors[],
1629 SkXfermode* xmode,
1630 const uint16_t indices[], int indexCount,
1631 const SkPaint& paint) {
1632 CHECK_SHOULD_DRAW(draw, false);
1633
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001634 // If both textures and vertex-colors are NULL, strokes hairlines with the paint's color.
1635 if ((NULL == texs || NULL == paint.getShader()) && NULL == colors) {
1636 texs = NULL;
1637 SkPaint copy(paint);
1638 copy.setStyle(SkPaint::kStroke_Style);
1639 copy.setStrokeWidth(0);
1640
1641 VertState state(vertexCount, indices, indexCount);
1642 VertState::Proc vertProc = state.chooseProc(vmode);
1643
1644 SkPoint* pts = new SkPoint[vertexCount * 6];
1645 int i = 0;
1646 while (vertProc(&state)) {
1647 pts[i] = vertices[state.f0];
1648 pts[i + 1] = vertices[state.f1];
1649 pts[i + 2] = vertices[state.f1];
1650 pts[i + 3] = vertices[state.f2];
1651 pts[i + 4] = vertices[state.f2];
1652 pts[i + 5] = vertices[state.f0];
1653 i += 6;
1654 }
1655 draw.drawPoints(SkCanvas::kLines_PointMode, i, pts, copy, true);
1656 return;
1657 }
1658
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001659 GrPaint grPaint;
1660 // we ignore the shader if texs is null.
1661 if (NULL == texs) {
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001662 SkPaint2GrPaintNoShader(this->context(), paint, false, NULL == colors, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001663 } else {
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001664 SkPaint2GrPaintShader(this->context(), paint, NULL == colors, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001665 }
1666
1667 if (NULL != xmode && NULL != texs && NULL != colors) {
1668 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1669 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1670#if 0
1671 return
1672#endif
1673 }
1674 }
1675
1676 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1677 if (NULL != colors) {
1678 // need to convert byte order and from non-PM to PM
1679 convertedColors.reset(vertexCount);
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001680 SkColor color;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001681 for (int i = 0; i < vertexCount; ++i) {
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001682 color = colors[i];
1683 if (paint.getAlpha() != 255) {
1684 color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paint.getAlpha()));
1685 }
1686 convertedColors[i] = SkColor2GrColor(color);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001687 }
1688 colors = convertedColors.get();
1689 }
1690 fContext->drawVertices(grPaint,
1691 gVertexMode2PrimitiveType[vmode],
1692 vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001693 vertices,
1694 texs,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001695 colors,
1696 indices,
1697 indexCount);
1698}
1699
1700///////////////////////////////////////////////////////////////////////////////
1701
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001702void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1703 size_t byteLength, SkScalar x, SkScalar y,
1704 const SkPaint& paint) {
1705 CHECK_SHOULD_DRAW(draw, false);
1706
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001707 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001708 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001709 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001710
1711 SkDEBUGCODE(this->validate();)
1712
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001713 fMainTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
1714 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001715 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001716 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001717
1718 SkDEBUGCODE(this->validate();)
1719
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001720 fFallbackTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001721 } else {
1722 // this guy will just call our drawPath()
1723 draw.drawText_asPaths((const char*)text, byteLength, x, y, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001724 }
1725}
1726
1727void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1728 size_t byteLength, const SkScalar pos[],
1729 SkScalar constY, int scalarsPerPos,
1730 const SkPaint& paint) {
1731 CHECK_SHOULD_DRAW(draw, false);
1732
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001733 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001734 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001735 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001736
1737 SkDEBUGCODE(this->validate();)
1738
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001739 fMainTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001740 constY, scalarsPerPos);
1741 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001742 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001743 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001744
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001745 SkDEBUGCODE(this->validate();)
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001746
1747 fFallbackTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001748 constY, scalarsPerPos);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001749 } else {
1750 draw.drawPosText_asPaths((const char*)text, byteLength, pos, constY,
1751 scalarsPerPos, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001752 }
1753}
1754
1755void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1756 size_t len, const SkPath& path,
1757 const SkMatrix* m, const SkPaint& paint) {
1758 CHECK_SHOULD_DRAW(draw, false);
1759
1760 SkASSERT(draw.fDevice == this);
1761 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1762}
1763
1764///////////////////////////////////////////////////////////////////////////////
1765
1766bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1767 if (!paint.isLCDRenderText()) {
1768 // we're cool with the paint as is
1769 return false;
1770 }
1771
1772 if (paint.getShader() ||
1773 paint.getXfermode() || // unless its srcover
1774 paint.getMaskFilter() ||
1775 paint.getRasterizer() ||
1776 paint.getColorFilter() ||
1777 paint.getPathEffect() ||
1778 paint.isFakeBoldText() ||
1779 paint.getStyle() != SkPaint::kFill_Style) {
1780 // turn off lcd
1781 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1782 flags->fHinting = paint.getHinting();
1783 return true;
1784 }
1785 // we're cool with the paint as is
1786 return false;
1787}
1788
1789void SkGpuDevice::flush() {
1790 DO_DEFERRED_CLEAR();
1791 fContext->resolveRenderTarget(fRenderTarget);
1792}
1793
1794///////////////////////////////////////////////////////////////////////////////
1795
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001796SkBaseDevice* SkGpuDevice::onCreateDevice(const SkImageInfo& info, Usage usage) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001797 GrTextureDesc desc;
1798 desc.fConfig = fRenderTarget->config();
1799 desc.fFlags = kRenderTarget_GrTextureFlagBit;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001800 desc.fWidth = info.width();
1801 desc.fHeight = info.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001802 desc.fSampleCnt = fRenderTarget->numSamples();
1803
1804 SkAutoTUnref<GrTexture> texture;
1805 // Skia's convention is to only clear a device if it is non-opaque.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001806 unsigned flags = info.isOpaque() ? 0 : kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001807
1808#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1809 // layers are never draw in repeat modes, so we can request an approx
1810 // match and ignore any padding.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001811 flags |= kCached_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001812 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == usage) ?
1813 GrContext::kApprox_ScratchTexMatch :
1814 GrContext::kExact_ScratchTexMatch;
1815 texture.reset(fContext->lockAndRefScratchTexture(desc, match));
1816#else
1817 texture.reset(fContext->createUncachedTexture(desc, NULL, 0));
1818#endif
1819 if (NULL != texture.get()) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001820 return SkGpuDevice::Create(texture, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001821 } else {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001822 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1823 info.width(), info.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001824 return NULL;
1825 }
1826}
1827
reed@google.com76f10a32014-02-05 15:32:21 +00001828SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info) {
1829 return SkSurface::NewRenderTarget(fContext, info, fRenderTarget->numSamples());
1830}
1831
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001832void SkGpuDevice::EXPERIMENTAL_optimize(SkPicture* picture) {
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001833 SkPicture::AccelData::Key key = GPUAccelData::ComputeAccelDataKey();
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001834
commit-bot@chromium.org8ec8bab2014-05-14 13:11:48 +00001835 const SkPicture::AccelData* existing = picture->EXPERIMENTAL_getAccelData(key);
1836 if (NULL != existing) {
1837 return;
1838 }
1839
commit-bot@chromium.org8fd93822014-05-06 13:43:22 +00001840 SkAutoTUnref<GPUAccelData> data(SkNEW_ARGS(GPUAccelData, (key)));
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001841
1842 picture->EXPERIMENTAL_addAccelData(data);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001843
1844 GatherGPUInfo(picture, data);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001845}
1846
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001847static void wrap_texture(GrTexture* texture, int width, int height, SkBitmap* result) {
1848 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001849 result->setInfo(info);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001850 result->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref();
1851}
1852
commit-bot@chromium.orgc8733292014-04-11 15:54:14 +00001853void SkGpuDevice::EXPERIMENTAL_purge(SkPicture* picture) {
1854
1855}
1856
1857bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* canvas, SkPicture* picture) {
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001858
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001859 SkPicture::AccelData::Key key = GPUAccelData::ComputeAccelDataKey();
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001860
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001861 const SkPicture::AccelData* data = picture->EXPERIMENTAL_getAccelData(key);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001862 if (NULL == data) {
1863 return false;
1864 }
1865
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001866 const GPUAccelData *gpuData = static_cast<const GPUAccelData*>(data);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001867
commit-bot@chromium.org8ec8bab2014-05-14 13:11:48 +00001868 if (0 == gpuData->numSaveLayers()) {
1869 return false;
1870 }
1871
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001872 SkAutoTArray<bool> pullForward(gpuData->numSaveLayers());
1873 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1874 pullForward[i] = false;
1875 }
1876
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001877 SkRect clipBounds;
1878 if (!canvas->getClipBounds(&clipBounds)) {
1879 return true;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001880 }
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001881 SkIRect query;
1882 clipBounds.roundOut(&query);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001883
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001884 const SkPicture::OperationList& ops = picture->EXPERIMENTAL_getActiveOps(query);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001885
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001886 // This code pre-renders the entire layer since it will be cached and potentially
1887 // reused with different clips (e.g., in different tiles). Because of this the
1888 // clip will not be limiting the size of the pre-rendered layer. kSaveLayerMaxSize
1889 // is used to limit which clips are pre-rendered.
1890 static const int kSaveLayerMaxSize = 256;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001891
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001892 if (ops.valid()) {
1893 // In this case the picture has been generated with a BBH so we use
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001894 // the BBH to limit the pre-rendering to just the layers needed to cover
1895 // the region being drawn
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001896 for (int i = 0; i < ops.numOps(); ++i) {
1897 uint32_t offset = ops.offset(i);
1898
1899 // For now we're saving all the layers in the GPUAccelData so they
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001900 // can be nested. Additionally, the nested layers appear before
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001901 // their parent in the list.
1902 for (int j = 0 ; j < gpuData->numSaveLayers(); ++j) {
1903 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(j);
1904
1905 if (pullForward[j]) {
1906 continue; // already pulling forward
1907 }
1908
1909 if (offset < info.fSaveLayerOpID || offset > info.fRestoreOpID) {
1910 continue; // the op isn't in this range
1911 }
1912
1913 // TODO: once this code is more stable unsuitable layers can
1914 // just be omitted during the optimization stage
1915 if (!info.fValid ||
1916 kSaveLayerMaxSize < info.fSize.fWidth ||
1917 kSaveLayerMaxSize < info.fSize.fHeight ||
1918 info.fIsNested) {
1919 continue; // this layer is unsuitable
1920 }
1921
1922 pullForward[j] = true;
1923 }
1924 }
1925 } else {
1926 // In this case there is no BBH associated with the picture. Pre-render
commit-bot@chromium.orgf97d65d2014-05-08 23:24:05 +00001927 // all the layers that intersect the drawn region
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001928 for (int j = 0; j < gpuData->numSaveLayers(); ++j) {
1929 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(j);
1930
commit-bot@chromium.orgf97d65d2014-05-08 23:24:05 +00001931 SkIRect layerRect = SkIRect::MakeXYWH(info.fOffset.fX,
1932 info.fOffset.fY,
1933 info.fSize.fWidth,
1934 info.fSize.fHeight);
1935
1936 if (!SkIRect::Intersects(query, layerRect)) {
1937 continue;
1938 }
1939
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001940 // TODO: once this code is more stable unsuitable layers can
1941 // just be omitted during the optimization stage
1942 if (!info.fValid ||
1943 kSaveLayerMaxSize < info.fSize.fWidth ||
1944 kSaveLayerMaxSize < info.fSize.fHeight ||
1945 info.fIsNested) {
1946 continue;
1947 }
1948
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001949 pullForward[j] = true;
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001950 }
1951 }
1952
1953 SkPicturePlayback::PlaybackReplacements replacements;
1954
1955 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1956 if (pullForward[i]) {
1957 GrCachedLayer* layer = fContext->getLayerCache()->findLayerOrCreate(picture, i);
1958
1959 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(i);
1960
1961 if (NULL != picture->fPlayback) {
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001962 SkPicturePlayback::PlaybackReplacements::ReplacementInfo* layerInfo =
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001963 replacements.push();
1964 layerInfo->fStart = info.fSaveLayerOpID;
1965 layerInfo->fStop = info.fRestoreOpID;
1966 layerInfo->fPos = info.fOffset;
1967
1968 GrTextureDesc desc;
1969 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1970 desc.fWidth = info.fSize.fWidth;
1971 desc.fHeight = info.fSize.fHeight;
1972 desc.fConfig = kSkia8888_GrPixelConfig;
1973 // TODO: need to deal with sample count
1974
1975 bool bNeedsRendering = true;
1976
1977 // This just uses scratch textures and doesn't cache the texture.
1978 // This can yield a lot of re-rendering
1979 if (NULL == layer->getTexture()) {
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001980 layer->setTexture(fContext->lockAndRefScratchTexture(desc,
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001981 GrContext::kApprox_ScratchTexMatch));
1982 if (NULL == layer->getTexture()) {
1983 continue;
1984 }
1985 } else {
1986 bNeedsRendering = false;
1987 }
1988
1989 layerInfo->fBM = SkNEW(SkBitmap);
1990 wrap_texture(layer->getTexture(), desc.fWidth, desc.fHeight, layerInfo->fBM);
1991
1992 SkASSERT(info.fPaint);
1993 layerInfo->fPaint = info.fPaint;
1994
1995 if (bNeedsRendering) {
1996 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTargetDirect(
1997 layer->getTexture()->asRenderTarget()));
1998
1999 SkCanvas* canvas = surface->getCanvas();
2000
2001 canvas->setMatrix(info.fCTM);
2002 canvas->clear(SK_ColorTRANSPARENT);
2003
2004 picture->fPlayback->setDrawLimits(info.fSaveLayerOpID, info.fRestoreOpID);
2005 picture->fPlayback->draw(*canvas, NULL);
2006 picture->fPlayback->setDrawLimits(0, 0);
2007 canvas->flush();
2008 }
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00002009 }
2010 }
2011 }
2012
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002013 // Playback using new layers
2014 picture->fPlayback->setReplacements(&replacements);
2015 picture->fPlayback->draw(*canvas, NULL);
2016 picture->fPlayback->setReplacements(NULL);
2017
2018 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
2019 GrCachedLayer* layer = fContext->getLayerCache()->findLayerOrCreate(picture, i);
2020
2021 if (NULL != layer->getTexture()) {
2022 fContext->unlockScratchTexture(layer->getTexture());
2023 layer->setTexture(NULL);
2024 }
2025 }
2026
2027 return true;
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00002028}