blob: 086dbdcdbd079ae0e9eafd65f41222dc9677fe3f [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"
robertphillips98d709b2014-09-02 10:20:50 -070019#include "GrLayerHoister.h"
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +000020#include "GrPictureUtils.h"
robertphillips274b4ba2014-09-04 07:24:18 -070021#include "GrRecordReplaceDraw.h"
egdanield58a0ba2014-06-11 10:30:05 -070022#include "GrStrokeInfo.h"
egdanielbbcb38d2014-06-19 10:19:29 -070023#include "GrTracing.h"
derekff4555aa2014-10-06 12:19:12 -070024#include "GrGpu.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000025
26#include "SkGrTexturePixelRef.h"
27
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000028#include "SkDeviceImageFilterProxy.h"
29#include "SkDrawProcs.h"
30#include "SkGlyphCache.h"
31#include "SkImageFilter.h"
commit-bot@chromium.org82139702014-03-10 22:53:20 +000032#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000033#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000034#include "SkPicture.h"
robertphillipsdb539902014-07-01 08:47:04 -070035#include "SkPictureData.h"
robertphillips274b4ba2014-09-04 07:24:18 -070036#include "SkRecord.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000037#include "SkRRect.h"
38#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000039#include "SkSurface.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000040#include "SkTLazy.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000041#include "SkUtils.h"
commit-bot@chromium.org559a8832014-05-30 10:08:22 +000042#include "SkVertState.h"
robertphillips320c9232014-07-29 06:07:19 -070043#include "SkXfermode.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000044#include "SkErrorInternals.h"
45
senorblanco55b6d8b2014-07-30 11:26:46 -070046enum { kDefaultImageFilterCacheSize = 32 * 1024 * 1024 };
47
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000048#define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
49
50#if 0
51 extern bool (*gShouldDrawProc)();
52 #define CHECK_SHOULD_DRAW(draw, forceI) \
53 do { \
54 if (gShouldDrawProc && !gShouldDrawProc()) return; \
55 this->prepareDraw(draw, forceI); \
56 } while (0)
57#else
58 #define CHECK_SHOULD_DRAW(draw, forceI) this->prepareDraw(draw, forceI)
59#endif
60
61// This constant represents the screen alignment criterion in texels for
62// requiring texture domain clamping to prevent color bleeding when drawing
63// a sub region of a larger source image.
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000064#define COLOR_BLEED_TOLERANCE 0.001f
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000065
66#define DO_DEFERRED_CLEAR() \
67 do { \
68 if (fNeedClear) { \
69 this->clear(SK_ColorTRANSPARENT); \
70 } \
71 } while (false) \
72
73///////////////////////////////////////////////////////////////////////////////
74
75#define CHECK_FOR_ANNOTATION(paint) \
76 do { if (paint.getAnnotation()) { return; } } while (0)
77
78///////////////////////////////////////////////////////////////////////////////
79
bsalomonbcf0a522014-10-08 08:40:09 -070080// Helper for turning a bitmap into a texture. If the bitmap is GrTexture backed this
81// just accesses the backing GrTexture. Otherwise, it creates a cached texture
82// representation and releases it in the destructor.
83class AutoBitmapTexture : public SkNoncopyable {
Brian Salomon9323b8b2014-10-07 15:07:38 -040084public:
bsalomonbcf0a522014-10-08 08:40:09 -070085 AutoBitmapTexture() {}
robertphillipsdbe60742014-09-30 06:54:17 -070086
bsalomonbcf0a522014-10-08 08:40:09 -070087 AutoBitmapTexture(GrContext* context,
88 const SkBitmap& bitmap,
89 const GrTextureParams* params,
90 GrTexture** texture) {
Brian Salomon9323b8b2014-10-07 15:07:38 -040091 SkASSERT(texture);
bsalomonbcf0a522014-10-08 08:40:09 -070092 *texture = this->set(context, bitmap, params);
Brian Salomon9323b8b2014-10-07 15:07:38 -040093 }
94
bsalomonbcf0a522014-10-08 08:40:09 -070095 GrTexture* set(GrContext* context,
Brian Salomon9323b8b2014-10-07 15:07:38 -040096 const SkBitmap& bitmap,
97 const GrTextureParams* params) {
bsalomonbcf0a522014-10-08 08:40:09 -070098 // Either get the texture directly from the bitmap, or else use the cache and
99 // remember to unref it.
100 if (GrTexture* bmpTexture = bitmap.getTexture()) {
101 fTexture.reset(NULL);
102 return bmpTexture;
103 } else {
104 fTexture.reset(GrRefCachedBitmapTexture(context, bitmap, params));
105 return fTexture.get();
Brian Salomon9323b8b2014-10-07 15:07:38 -0400106 }
Brian Salomon9323b8b2014-10-07 15:07:38 -0400107 }
108
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000109private:
bsalomonbcf0a522014-10-08 08:40:09 -0700110 SkAutoTUnref<GrTexture> fTexture;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000111};
112
113///////////////////////////////////////////////////////////////////////////////
114
115struct GrSkDrawProcs : public SkDrawProcs {
116public:
117 GrContext* fContext;
118 GrTextContext* fTextContext;
119 GrFontScaler* fFontScaler; // cached in the skia glyphcache
120};
121
122///////////////////////////////////////////////////////////////////////////////
123
reed4a8126e2014-09-22 07:29:03 -0700124SkGpuDevice* SkGpuDevice::Create(GrSurface* surface, const SkSurfaceProps& props, unsigned flags) {
bsalomon49f085d2014-09-05 13:34:00 -0700125 SkASSERT(surface);
bsalomon32d0b3b2014-08-29 07:50:23 -0700126 if (NULL == surface->asRenderTarget() || surface->wasDestroyed()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000127 return NULL;
128 }
reed4a8126e2014-09-22 07:29:03 -0700129 return SkNEW_ARGS(SkGpuDevice, (surface, props, flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000130}
131
reed4a8126e2014-09-22 07:29:03 -0700132SkGpuDevice::SkGpuDevice(GrSurface* surface, const SkSurfaceProps& props, unsigned flags) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000133
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000134 fDrawProcs = NULL;
135
bsalomon32d0b3b2014-08-29 07:50:23 -0700136 fContext = SkRef(surface->getContext());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000137
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000138 fNeedClear = flags & kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000139
bsalomon32d0b3b2014-08-29 07:50:23 -0700140 fRenderTarget = SkRef(surface->asRenderTarget());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000141
bsalomonafbf2d62014-09-30 12:18:44 -0700142 SkImageInfo info = surface->surfacePriv().info();
bsalomonbcf0a522014-10-08 08:40:09 -0700143 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (info, surface));
bsalomonafbf2d62014-09-30 12:18:44 -0700144 fLegacyBitmap.setInfo(info);
reed89443ab2014-06-27 11:34:19 -0700145 fLegacyBitmap.setPixelRef(pr)->unref();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700146
reed4a8126e2014-09-22 07:29:03 -0700147 this->setPixelGeometry(props.pixelGeometry());
148
kkinnunenc6cb56f2014-06-24 00:12:27 -0700149 bool useDFFonts = !!(flags & kDFFonts_Flag);
jvanverth8c27a182014-10-14 08:45:50 -0700150 fTextContext = fContext->createTextContext(fRenderTarget, this->getLeakyProperties(), useDFFonts);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000151}
152
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000153SkGpuDevice* SkGpuDevice::Create(GrContext* context, const SkImageInfo& origInfo,
reed4a8126e2014-09-22 07:29:03 -0700154 const SkSurfaceProps& props, int sampleCount) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000155 if (kUnknown_SkColorType == origInfo.colorType() ||
156 origInfo.width() < 0 || origInfo.height() < 0) {
157 return NULL;
158 }
159
reede5ea5002014-09-03 11:54:58 -0700160 SkColorType ct = origInfo.colorType();
161 SkAlphaType at = origInfo.alphaType();
robertphillipsa0537de2014-09-18 08:01:23 -0700162 // TODO: perhaps we can loosen this check now that colortype is more detailed
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000163 // e.g. can we support both RGBA and BGRA here?
reede5ea5002014-09-03 11:54:58 -0700164 if (kRGB_565_SkColorType == ct) {
165 at = kOpaque_SkAlphaType; // force this setting
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000166 } else {
reede5ea5002014-09-03 11:54:58 -0700167 ct = kN32_SkColorType;
168 if (kOpaque_SkAlphaType != at) {
169 at = kPremul_SkAlphaType; // force this setting
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000170 }
171 }
reede5ea5002014-09-03 11:54:58 -0700172 const SkImageInfo info = SkImageInfo::Make(origInfo.width(), origInfo.height(), ct, at);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000173
174 GrTextureDesc desc;
175 desc.fFlags = kRenderTarget_GrTextureFlagBit;
176 desc.fWidth = info.width();
177 desc.fHeight = info.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000178 desc.fConfig = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000179 desc.fSampleCnt = sampleCount;
180
181 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0));
182 if (!texture.get()) {
183 return NULL;
184 }
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000185
reed4a8126e2014-09-22 07:29:03 -0700186 return SkNEW_ARGS(SkGpuDevice, (texture.get(), props));
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000187}
188
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000189SkGpuDevice::~SkGpuDevice() {
190 if (fDrawProcs) {
191 delete fDrawProcs;
192 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000193
jvanverth8c27a182014-10-14 08:45:50 -0700194 delete fTextContext;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000195
196 // The GrContext takes a ref on the target. We don't want to cause the render
197 // target to be unnecessarily kept alive.
198 if (fContext->getRenderTarget() == fRenderTarget) {
199 fContext->setRenderTarget(NULL);
200 }
201
202 if (fContext->getClip() == &fClipData) {
203 fContext->setClip(NULL);
204 }
205
bsalomon32d0b3b2014-08-29 07:50:23 -0700206 fRenderTarget->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000207 fContext->unref();
208}
209
210///////////////////////////////////////////////////////////////////////////////
211
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000212bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
213 int x, int y) {
214 DO_DEFERRED_CLEAR();
215
216 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000217 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000218 if (kUnknown_GrPixelConfig == config) {
219 return false;
220 }
221
222 uint32_t flags = 0;
223 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
224 flags = GrContext::kUnpremul_PixelOpsFlag;
225 }
226 return fContext->readRenderTargetPixels(fRenderTarget, x, y, dstInfo.width(), dstInfo.height(),
227 config, dstPixels, dstRowBytes, flags);
228}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000229
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000230bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
231 int x, int y) {
232 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000233 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000234 if (kUnknown_GrPixelConfig == config) {
235 return false;
236 }
237 uint32_t flags = 0;
238 if (kUnpremul_SkAlphaType == info.alphaType()) {
239 flags = GrContext::kUnpremul_PixelOpsFlag;
240 }
241 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
242
243 // need to bump our genID for compatibility with clients that "know" we have a bitmap
reed89443ab2014-06-27 11:34:19 -0700244 fLegacyBitmap.notifyPixelsChanged();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000245
246 return true;
247}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000248
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000249const SkBitmap& SkGpuDevice::onAccessBitmap() {
250 DO_DEFERRED_CLEAR();
reed89443ab2014-06-27 11:34:19 -0700251 return fLegacyBitmap;
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000252}
253
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000254void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
255 INHERITED::onAttachToCanvas(canvas);
256
257 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
258 fClipData.fClipStack = canvas->getClipStack();
259}
260
261void SkGpuDevice::onDetachFromCanvas() {
262 INHERITED::onDetachFromCanvas();
263 fClipData.fClipStack = NULL;
264}
265
266// call this every draw call, to ensure that the context reflects our state,
267// and not the state from some other canvas/device
268void SkGpuDevice::prepareDraw(const SkDraw& draw, bool forceIdentity) {
bsalomon49f085d2014-09-05 13:34:00 -0700269 SkASSERT(fClipData.fClipStack);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000270
271 fContext->setRenderTarget(fRenderTarget);
272
273 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
274
275 if (forceIdentity) {
276 fContext->setIdentityMatrix();
277 } else {
278 fContext->setMatrix(*draw.fMatrix);
279 }
280 fClipData.fOrigin = this->getOrigin();
281
282 fContext->setClip(&fClipData);
283
284 DO_DEFERRED_CLEAR();
285}
286
287GrRenderTarget* SkGpuDevice::accessRenderTarget() {
288 DO_DEFERRED_CLEAR();
289 return fRenderTarget;
290}
291
292///////////////////////////////////////////////////////////////////////////////
293
294SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
295SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
296SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
297SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
298SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
299 shader_type_mismatch);
300SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
301 shader_type_mismatch);
302SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
303SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
304
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000305///////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000306
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000307void SkGpuDevice::clear(SkColor color) {
egdanield78a1682014-07-09 10:41:26 -0700308 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::clear", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000309 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
310 fContext->clear(&rect, SkColor2GrColor(color), true, fRenderTarget);
311 fNeedClear = false;
312}
313
314void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
315 CHECK_SHOULD_DRAW(draw, false);
egdanield78a1682014-07-09 10:41:26 -0700316 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPaint", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000317
318 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000319 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000320
321 fContext->drawPaint(grPaint);
322}
323
324// must be in SkCanvas::PointMode order
325static const GrPrimitiveType gPointMode2PrimtiveType[] = {
326 kPoints_GrPrimitiveType,
327 kLines_GrPrimitiveType,
328 kLineStrip_GrPrimitiveType
329};
330
331void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
332 size_t count, const SkPoint pts[], const SkPaint& paint) {
333 CHECK_FOR_ANNOTATION(paint);
334 CHECK_SHOULD_DRAW(draw, false);
335
336 SkScalar width = paint.getStrokeWidth();
337 if (width < 0) {
338 return;
339 }
340
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000341 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
egdaniele61c4112014-06-12 10:24:21 -0700342 GrStrokeInfo strokeInfo(paint, SkPaint::kStroke_Style);
343 GrPaint grPaint;
344 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
345 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700346 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700347 path.moveTo(pts[0]);
348 path.lineTo(pts[1]);
349 fContext->drawPath(grPaint, path, strokeInfo);
350 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000351 }
352
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000353 // we only handle hairlines and paints without path effects or mask filters,
354 // else we let the SkDraw call our drawPath()
355 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
356 draw.drawPoints(mode, count, pts, paint, true);
357 return;
358 }
359
360 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000361 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000362
363 fContext->drawVertices(grPaint,
364 gPointMode2PrimtiveType[mode],
robertphillips@google.coma4662862013-11-21 14:24:16 +0000365 SkToS32(count),
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000366 (SkPoint*)pts,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000367 NULL,
368 NULL,
369 NULL,
370 0);
371}
372
373///////////////////////////////////////////////////////////////////////////////
374
375void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
376 const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -0700377 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRect", fContext);
378
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000379 CHECK_FOR_ANNOTATION(paint);
380 CHECK_SHOULD_DRAW(draw, false);
381
382 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
383 SkScalar width = paint.getStrokeWidth();
384
385 /*
386 We have special code for hairline strokes, miter-strokes, bevel-stroke
387 and fills. Anything else we just call our path code.
388 */
389 bool usePath = doStroke && width > 0 &&
390 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
391 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
392 // another two reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700393
394 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000395 usePath = true;
396 }
egdanield58a0ba2014-06-11 10:30:05 -0700397
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000398 if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) {
399#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
400 if (doStroke) {
401#endif
402 usePath = true;
403#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
404 } else {
405 usePath = !fContext->getMatrix().preservesRightAngles();
406 }
407#endif
408 }
409 // until we can both stroke and fill rectangles
410 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
411 usePath = true;
412 }
413
egdanield58a0ba2014-06-11 10:30:05 -0700414 GrStrokeInfo strokeInfo(paint);
415
416 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700417 if (!usePath && pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700418 usePath = true;
419 }
420
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000421 if (usePath) {
422 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700423 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000424 path.addRect(rect);
425 this->drawPath(draw, path, paint, NULL, true);
426 return;
427 }
428
429 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000430 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
Mike Klein744fb732014-06-23 15:13:26 -0400431
egdanield58a0ba2014-06-11 10:30:05 -0700432 fContext->drawRect(grPaint, rect, &strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000433}
434
435///////////////////////////////////////////////////////////////////////////////
436
437void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
438 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700439 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRRect", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000440 CHECK_FOR_ANNOTATION(paint);
441 CHECK_SHOULD_DRAW(draw, false);
442
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000443 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000444 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
Mike Klein744fb732014-06-23 15:13:26 -0400445
egdanield58a0ba2014-06-11 10:30:05 -0700446 GrStrokeInfo strokeInfo(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000447 if (paint.getMaskFilter()) {
448 // try to hit the fast path for drawing filtered round rects
449
450 SkRRect devRRect;
451 if (rect.transform(fContext->getMatrix(), &devRRect)) {
452 if (devRRect.allCornersCircular()) {
453 SkRect maskRect;
454 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(),
455 draw.fClip->getBounds(),
456 fContext->getMatrix(),
457 &maskRect)) {
458 SkIRect finalIRect;
459 maskRect.roundOut(&finalIRect);
460 if (draw.fClip->quickReject(finalIRect)) {
461 // clipped out
462 return;
463 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000464 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext, &grPaint,
egdanield58a0ba2014-06-11 10:30:05 -0700465 strokeInfo.getStrokeRec(),
466 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000467 return;
468 }
469 }
470
471 }
472 }
473
474 }
475
egdanield58a0ba2014-06-11 10:30:05 -0700476 bool usePath = false;
477
478 if (paint.getMaskFilter()) {
479 usePath = true;
480 } else {
481 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700482 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700483 usePath = true;
484 }
485 }
486
487
488 if (usePath) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000489 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700490 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000491 path.addRRect(rect);
492 this->drawPath(draw, path, paint, NULL, true);
493 return;
494 }
Mike Klein744fb732014-06-23 15:13:26 -0400495
egdanield58a0ba2014-06-11 10:30:05 -0700496 fContext->drawRRect(grPaint, rect, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000497}
498
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000499void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
500 const SkRRect& inner, const SkPaint& paint) {
501 SkStrokeRec stroke(paint);
502 if (stroke.isFillStyle()) {
503
504 CHECK_FOR_ANNOTATION(paint);
505 CHECK_SHOULD_DRAW(draw, false);
506
507 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000508 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000509
510 if (NULL == paint.getMaskFilter() && NULL == paint.getPathEffect()) {
511 fContext->drawDRRect(grPaint, outer, inner);
512 return;
513 }
514 }
515
516 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700517 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000518 path.addRRect(outer);
519 path.addRRect(inner);
520 path.setFillType(SkPath::kEvenOdd_FillType);
521
522 this->drawPath(draw, path, paint, NULL, true);
523}
524
525
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000526/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000527
528void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
529 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700530 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawOval", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000531 CHECK_FOR_ANNOTATION(paint);
532 CHECK_SHOULD_DRAW(draw, false);
533
egdanield58a0ba2014-06-11 10:30:05 -0700534 GrStrokeInfo strokeInfo(paint);
535
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000536 bool usePath = false;
537 // some basic reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700538 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000539 usePath = true;
egdanield58a0ba2014-06-11 10:30:05 -0700540 } else {
541 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700542 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700543 usePath = true;
544 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000545 }
546
547 if (usePath) {
548 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700549 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000550 path.addOval(oval);
551 this->drawPath(draw, path, paint, NULL, true);
552 return;
553 }
554
555 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000556 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000557
egdanield58a0ba2014-06-11 10:30:05 -0700558 fContext->drawOval(grPaint, oval, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000559}
560
561#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000562
563///////////////////////////////////////////////////////////////////////////////
564
565// helpers for applying mask filters
566namespace {
567
568// Draw a mask using the supplied paint. Since the coverage/geometry
569// is already burnt into the mask this boils down to a rect draw.
570// Return true if the mask was successfully drawn.
571bool draw_mask(GrContext* context, const SkRect& maskRect,
572 GrPaint* grp, GrTexture* mask) {
573 GrContext::AutoMatrix am;
574 if (!am.setIdentity(context, grp)) {
575 return false;
576 }
577
578 SkMatrix matrix;
579 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
580 matrix.postIDiv(mask->width(), mask->height());
581
joshualittb0a8a372014-09-23 09:50:21 -0700582 grp->addCoverageProcessor(GrSimpleTextureEffect::Create(mask, matrix))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000583 context->drawRect(*grp, maskRect);
584 return true;
585}
586
587bool draw_with_mask_filter(GrContext* context, const SkPath& devPath,
reed868074b2014-06-03 10:53:59 -0700588 SkMaskFilter* filter, const SkRegion& clip,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000589 GrPaint* grp, SkPaint::Style style) {
590 SkMask srcM, dstM;
591
592 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
593 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
594 return false;
595 }
596 SkAutoMaskFreeImage autoSrc(srcM.fImage);
597
598 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
599 return false;
600 }
601 // this will free-up dstM when we're done (allocated in filterMask())
602 SkAutoMaskFreeImage autoDst(dstM.fImage);
603
604 if (clip.quickReject(dstM.fBounds)) {
605 return false;
606 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000607
608 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
609 // the current clip (and identity matrix) and GrPaint settings
610 GrTextureDesc desc;
611 desc.fWidth = dstM.fBounds.width();
612 desc.fHeight = dstM.fBounds.height();
613 desc.fConfig = kAlpha_8_GrPixelConfig;
614
bsalomone3059732014-10-14 11:47:22 -0700615 SkAutoTUnref<GrTexture> texture(
616 context->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch));
617 if (!texture) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000618 return false;
619 }
620 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
621 dstM.fImage, dstM.fRowBytes);
622
623 SkRect maskRect = SkRect::Make(dstM.fBounds);
624
625 return draw_mask(context, maskRect, grp, texture);
626}
627
bsalomone3059732014-10-14 11:47:22 -0700628// Create a mask of 'devPath' and place the result in 'mask'.
629GrTexture* create_mask_GPU(GrContext* context,
630 const SkRect& maskRect,
631 const SkPath& devPath,
632 const GrStrokeInfo& strokeInfo,
633 bool doAA,
634 int sampleCnt) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000635 GrTextureDesc desc;
636 desc.fFlags = kRenderTarget_GrTextureFlagBit;
637 desc.fWidth = SkScalarCeilToInt(maskRect.width());
638 desc.fHeight = SkScalarCeilToInt(maskRect.height());
bsalomone3059732014-10-14 11:47:22 -0700639 desc.fSampleCnt = doAA ? sampleCnt : 0;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000640 // We actually only need A8, but it often isn't supported as a
641 // render target so default to RGBA_8888
642 desc.fConfig = kRGBA_8888_GrPixelConfig;
derekff4555aa2014-10-06 12:19:12 -0700643
644 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig,
645 desc.fSampleCnt > 0)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000646 desc.fConfig = kAlpha_8_GrPixelConfig;
647 }
648
bsalomone3059732014-10-14 11:47:22 -0700649 GrTexture* mask = context->refScratchTexture(desc,GrContext::kApprox_ScratchTexMatch);
650 if (NULL == mask) {
651 return NULL;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000652 }
653
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000654 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
655
bsalomone3059732014-10-14 11:47:22 -0700656 GrContext::AutoRenderTarget art(context, mask->asRenderTarget());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000657 GrContext::AutoClip ac(context, clipRect);
658
659 context->clear(NULL, 0x0, true);
660
661 GrPaint tempPaint;
662 if (doAA) {
663 tempPaint.setAntiAlias(true);
664 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
665 // blend coeff of zero requires dual source blending support in order
666 // to properly blend partially covered pixels. This means the AA
667 // code path may not be taken. So we use a dst blend coeff of ISA. We
668 // could special case AA draws to a dst surface with known alpha=0 to
669 // use a zero dst coeff when dual source blending isn't available.
670 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
671 }
672
673 GrContext::AutoMatrix am;
674
675 // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint.
676 SkMatrix translate;
677 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
678 am.set(context, translate);
egdanield58a0ba2014-06-11 10:30:05 -0700679 context->drawPath(tempPaint, devPath, strokeInfo);
bsalomone3059732014-10-14 11:47:22 -0700680 return mask;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000681}
682
683SkBitmap wrap_texture(GrTexture* texture) {
684 SkBitmap result;
bsalomonafbf2d62014-09-30 12:18:44 -0700685 result.setInfo(texture->surfacePriv().info());
reed6c225732014-06-09 19:52:07 -0700686 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (result.info(), texture)))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000687 return result;
688}
689
690};
691
692void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
693 const SkPaint& paint, const SkMatrix* prePathMatrix,
694 bool pathIsMutable) {
695 CHECK_FOR_ANNOTATION(paint);
696 CHECK_SHOULD_DRAW(draw, false);
egdanield78a1682014-07-09 10:41:26 -0700697 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPath", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000698
jvanverthb3eb6872014-10-24 07:12:51 -0700699 SkASSERT(!pathIsMutable || origSrcPath.isVolatile());
700
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000701 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000702 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000703
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000704 // If we have a prematrix, apply it to the path, optimizing for the case
705 // where the original path can in fact be modified in place (even though
706 // its parameter type is const).
707 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000708 SkTLazy<SkPath> tmpPath;
709 SkTLazy<SkPath> effectPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000710
711 if (prePathMatrix) {
712 SkPath* result = pathPtr;
713
714 if (!pathIsMutable) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000715 result = tmpPath.init();
jvanverthb3eb6872014-10-24 07:12:51 -0700716 result->setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000717 pathIsMutable = true;
718 }
719 // should I push prePathMatrix on our MV stack temporarily, instead
720 // of applying it here? See SkDraw.cpp
721 pathPtr->transform(*prePathMatrix, result);
722 pathPtr = result;
723 }
724 // at this point we're done with prePathMatrix
725 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
726
egdanield58a0ba2014-06-11 10:30:05 -0700727 GrStrokeInfo strokeInfo(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000728 SkPathEffect* pathEffect = paint.getPathEffect();
729 const SkRect* cullRect = NULL; // TODO: what is our bounds?
egdanield58a0ba2014-06-11 10:30:05 -0700730 SkStrokeRec* strokePtr = strokeInfo.getStrokeRecPtr();
731 if (pathEffect && pathEffect->filterPath(effectPath.init(), *pathPtr, strokePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000732 cullRect)) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000733 pathPtr = effectPath.get();
734 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700735 strokeInfo.removeDash();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000736 }
737
egdanield58a0ba2014-06-11 10:30:05 -0700738 const SkStrokeRec& stroke = strokeInfo.getStrokeRec();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000739 if (paint.getMaskFilter()) {
740 if (!stroke.isHairlineStyle()) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000741 SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init();
742 if (stroke.applyToPath(strokedPath, *pathPtr)) {
743 pathPtr = strokedPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000744 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700745 strokeInfo.setFillStyle();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000746 }
747 }
748
749 // avoid possibly allocating a new path in transform if we can
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000750 SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init();
jvanverthb3eb6872014-10-24 07:12:51 -0700751 if (!pathIsMutable) {
752 devPathPtr->setIsVolatile(true);
753 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000754
755 // transform the path into device space
756 pathPtr->transform(fContext->getMatrix(), devPathPtr);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000757
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000758 SkRect maskRect;
759 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
760 draw.fClip->getBounds(),
761 fContext->getMatrix(),
762 &maskRect)) {
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000763 // The context's matrix may change while creating the mask, so save the CTM here to
764 // pass to filterMaskGPU.
765 const SkMatrix ctm = fContext->getMatrix();
766
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000767 SkIRect finalIRect;
768 maskRect.roundOut(&finalIRect);
769 if (draw.fClip->quickReject(finalIRect)) {
770 // clipped out
771 return;
772 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000773
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000774 if (paint.getMaskFilter()->directFilterMaskGPU(fContext, &grPaint,
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000775 stroke, *devPathPtr)) {
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000776 // the mask filter was able to draw itself directly, so there's nothing
777 // left to do.
778 return;
779 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000780
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000781
bsalomone3059732014-10-14 11:47:22 -0700782 SkAutoTUnref<GrTexture> mask(create_mask_GPU(fContext, maskRect, *devPathPtr,
783 strokeInfo, grPaint.isAntiAlias(),
784 fRenderTarget->numSamples()));
785 if (mask) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000786 GrTexture* filtered;
787
bsalomone3059732014-10-14 11:47:22 -0700788 if (paint.getMaskFilter()->filterMaskGPU(mask, ctm, maskRect, &filtered, true)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000789 // filterMaskGPU gives us ownership of a ref to the result
790 SkAutoTUnref<GrTexture> atu(filtered);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000791 if (draw_mask(fContext, maskRect, &grPaint, filtered)) {
792 // This path is completely drawn
793 return;
794 }
795 }
796 }
797 }
798
799 // draw the mask on the CPU - this is a fallthrough path in case the
800 // GPU path fails
801 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
802 SkPaint::kFill_Style;
egdanield58a0ba2014-06-11 10:30:05 -0700803 draw_with_mask_filter(fContext, *devPathPtr, paint.getMaskFilter(),
804 *draw.fClip, &grPaint, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000805 return;
806 }
807
egdanield58a0ba2014-06-11 10:30:05 -0700808 fContext->drawPath(grPaint, *pathPtr, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000809}
810
811static const int kBmpSmallTileSize = 1 << 10;
812
813static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
814 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
815 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
816 return tilesX * tilesY;
817}
818
819static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
820 if (maxTileSize <= kBmpSmallTileSize) {
821 return maxTileSize;
822 }
823
824 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
825 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
826
827 maxTileTotalTileSize *= maxTileSize * maxTileSize;
828 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
829
830 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
831 return kBmpSmallTileSize;
832 } else {
833 return maxTileSize;
834 }
835}
836
837// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
838// pixels from the bitmap are necessary.
839static void determine_clipped_src_rect(const GrContext* context,
840 const SkBitmap& bitmap,
841 const SkRect* srcRectPtr,
842 SkIRect* clippedSrcIRect) {
843 const GrClipData* clip = context->getClip();
844 clip->getConservativeBounds(context->getRenderTarget(), clippedSrcIRect, NULL);
845 SkMatrix inv;
846 if (!context->getMatrix().invert(&inv)) {
847 clippedSrcIRect->setEmpty();
848 return;
849 }
850 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
851 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700852 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000853 // we've setup src space 0,0 to map to the top left of the src rect.
854 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000855 if (!clippedSrcRect.intersect(*srcRectPtr)) {
856 clippedSrcIRect->setEmpty();
857 return;
858 }
859 }
860 clippedSrcRect.roundOut(clippedSrcIRect);
861 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
862 if (!clippedSrcIRect->intersect(bmpBounds)) {
863 clippedSrcIRect->setEmpty();
864 }
865}
866
867bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
868 const GrTextureParams& params,
869 const SkRect* srcRectPtr,
870 int maxTileSize,
871 int* tileSize,
872 SkIRect* clippedSrcRect) const {
873 // if bitmap is explictly texture backed then just use the texture
bsalomon49f085d2014-09-05 13:34:00 -0700874 if (bitmap.getTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000875 return false;
876 }
877
878 // if it's larger than the max tile size, then we have no choice but tiling.
879 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
880 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
881 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
882 return true;
883 }
884
885 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
886 return false;
887 }
888
889 // if the entire texture is already in our cache then no reason to tile it
890 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
891 return false;
892 }
893
894 // At this point we know we could do the draw by uploading the entire bitmap
895 // as a texture. However, if the texture would be large compared to the
896 // cache size and we don't require most of it for this draw then tile to
897 // reduce the amount of upload and cache spill.
898
899 // assumption here is that sw bitmap size is a good proxy for its size as
900 // a texture
901 size_t bmpSize = bitmap.getSize();
902 size_t cacheSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000903 fContext->getResourceCacheLimits(NULL, &cacheSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000904 if (bmpSize < cacheSize / 2) {
905 return false;
906 }
907
908 // Figure out how much of the src we will need based on the src rect and clipping.
909 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
910 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
911 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
912 kBmpSmallTileSize * kBmpSmallTileSize;
913
914 return usedTileBytes < 2 * bmpSize;
915}
916
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000917void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000918 const SkBitmap& bitmap,
919 const SkMatrix& m,
920 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000921 SkMatrix concat;
922 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
923 if (!m.isIdentity()) {
924 concat.setConcat(*draw->fMatrix, m);
925 draw.writable()->fMatrix = &concat;
926 }
927 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000928}
929
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000930// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000931// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
932// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000933static inline void clamped_outset_with_offset(SkIRect* iRect,
934 int outset,
935 SkPoint* offset,
936 const SkIRect& clamp) {
937 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000938
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000939 int leftClampDelta = clamp.fLeft - iRect->fLeft;
940 if (leftClampDelta > 0) {
941 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000942 iRect->fLeft = clamp.fLeft;
943 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000944 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000945 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000946
947 int topClampDelta = clamp.fTop - iRect->fTop;
948 if (topClampDelta > 0) {
949 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000950 iRect->fTop = clamp.fTop;
951 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000952 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000953 }
954
955 if (iRect->fRight > clamp.fRight) {
956 iRect->fRight = clamp.fRight;
957 }
958 if (iRect->fBottom > clamp.fBottom) {
959 iRect->fBottom = clamp.fBottom;
960 }
961}
962
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000963static bool has_aligned_samples(const SkRect& srcRect,
964 const SkRect& transformedRect) {
965 // detect pixel disalignment
966 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
967 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
968 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
969 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
970 SkScalarAbs(transformedRect.width() - srcRect.width()) <
971 COLOR_BLEED_TOLERANCE &&
972 SkScalarAbs(transformedRect.height() - srcRect.height()) <
973 COLOR_BLEED_TOLERANCE) {
974 return true;
975 }
976 return false;
977}
978
979static bool may_color_bleed(const SkRect& srcRect,
980 const SkRect& transformedRect,
981 const SkMatrix& m) {
982 // Only gets called if has_aligned_samples returned false.
983 // So we can assume that sampling is axis aligned but not texel aligned.
984 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
985 SkRect innerSrcRect(srcRect), innerTransformedRect,
986 outerTransformedRect(transformedRect);
987 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
988 m.mapRect(&innerTransformedRect, innerSrcRect);
989
990 // The gap between outerTransformedRect and innerTransformedRect
991 // represents the projection of the source border area, which is
992 // problematic for color bleeding. We must check whether any
993 // destination pixels sample the border area.
994 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
995 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
996 SkIRect outer, inner;
997 outerTransformedRect.round(&outer);
998 innerTransformedRect.round(&inner);
999 // If the inner and outer rects round to the same result, it means the
1000 // border does not overlap any pixel centers. Yay!
1001 return inner != outer;
1002}
1003
1004static bool needs_texture_domain(const SkBitmap& bitmap,
1005 const SkRect& srcRect,
1006 GrTextureParams &params,
1007 const SkMatrix& contextMatrix,
1008 bool bicubic) {
1009 bool needsTextureDomain = false;
1010
1011 if (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode) {
1012 // Need texture domain if drawing a sub rect
1013 needsTextureDomain = srcRect.width() < bitmap.width() ||
1014 srcRect.height() < bitmap.height();
1015 if (!bicubic && needsTextureDomain && contextMatrix.rectStaysRect()) {
1016 // sampling is axis-aligned
1017 SkRect transformedRect;
1018 contextMatrix.mapRect(&transformedRect, srcRect);
1019
1020 if (has_aligned_samples(srcRect, transformedRect)) {
1021 params.setFilterMode(GrTextureParams::kNone_FilterMode);
1022 needsTextureDomain = false;
1023 } else {
1024 needsTextureDomain = may_color_bleed(srcRect, transformedRect, contextMatrix);
1025 }
1026 }
1027 }
1028 return needsTextureDomain;
1029}
1030
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001031void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1032 const SkBitmap& bitmap,
1033 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001034 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001035 const SkPaint& paint,
1036 SkCanvas::DrawBitmapRectFlags flags) {
1037 CHECK_SHOULD_DRAW(draw, false);
1038
1039 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001040 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001041 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
1042 // in the (easier) bleed case, so update flags.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001043 if (NULL == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001044 SkScalar w = SkIntToScalar(bitmap.width());
1045 SkScalar h = SkIntToScalar(bitmap.height());
1046 dstSize.fWidth = w;
1047 dstSize.fHeight = h;
1048 srcRect.set(0, 0, w, h);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001049 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001050 } else {
bsalomon49f085d2014-09-05 13:34:00 -07001051 SkASSERT(dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001052 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001053 dstSize = *dstSizePtr;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001054 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
1055 srcRect.fRight >= bitmap.width() && srcRect.fBottom >= bitmap.height()) {
1056 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
1057 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001058 }
1059
1060 if (paint.getMaskFilter()){
1061 // Convert the bitmap to a shader so that the rect can be drawn
1062 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001063 SkBitmap tmp; // subset of bitmap, if necessary
1064 const SkBitmap* bitmapPtr = &bitmap;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001065 SkMatrix localM;
bsalomon49f085d2014-09-05 13:34:00 -07001066 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001067 localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
1068 localM.postScale(dstSize.fWidth / srcRectPtr->width(),
1069 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001070 // In bleed mode we position and trim the bitmap based on the src rect which is
1071 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
1072 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
1073 // compensate.
1074 if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) {
1075 SkIRect iSrc;
1076 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001077
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001078 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
1079 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001080
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001081 if (!bitmap.extractSubset(&tmp, iSrc)) {
1082 return; // extraction failed
1083 }
1084 bitmapPtr = &tmp;
1085 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001086
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001087 // The source rect has changed so update the matrix
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001088 localM.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001089 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001090 } else {
1091 localM.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001092 }
1093
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001094 SkPaint paintWithShader(paint);
1095 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +00001096 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &localM))->unref();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001097 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1098 this->drawRect(draw, dstRect, paintWithShader);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001099
1100 return;
1101 }
1102
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001103 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
1104 // the view matrix rather than a local matrix.
1105 SkMatrix m;
1106 m.setScale(dstSize.fWidth / srcRect.width(),
1107 dstSize.fHeight / srcRect.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001108 fContext->concatMatrix(m);
1109
1110 GrTextureParams params;
1111 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel();
1112 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001113
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001114 bool doBicubic = false;
1115
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001116 switch(paintFilterLevel) {
1117 case SkPaint::kNone_FilterLevel:
1118 textureFilterMode = GrTextureParams::kNone_FilterMode;
1119 break;
1120 case SkPaint::kLow_FilterLevel:
1121 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1122 break;
1123 case SkPaint::kMedium_FilterLevel:
commit-bot@chromium.org18786512014-05-20 14:53:45 +00001124 if (fContext->getMatrix().getMinScale() < SK_Scalar1) {
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001125 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1126 } else {
1127 // Don't trigger MIP level generation unnecessarily.
1128 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1129 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001130 break;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001131 case SkPaint::kHigh_FilterLevel:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001132 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001133 doBicubic =
1134 GrBicubicEffect::ShouldUseBicubic(fContext->getMatrix(), &textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001135 break;
1136 default:
1137 SkErrorInternals::SetError( kInvalidPaint_SkError,
1138 "Sorry, I don't understand the filtering "
1139 "mode you asked for. Falling back to "
1140 "MIPMaps.");
1141 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1142 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001143 }
1144
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001145 int tileFilterPad;
1146 if (doBicubic) {
1147 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1148 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1149 tileFilterPad = 0;
1150 } else {
1151 tileFilterPad = 1;
1152 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001153 params.setFilterMode(textureFilterMode);
1154
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001155 int maxTileSize = fContext->getMaxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001156 int tileSize;
1157
1158 SkIRect clippedSrcRect;
1159 if (this->shouldTileBitmap(bitmap, params, srcRectPtr, maxTileSize, &tileSize,
1160 &clippedSrcRect)) {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001161 this->drawTiledBitmap(bitmap, srcRect, clippedSrcRect, params, paint, flags, tileSize,
1162 doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001163 } else {
1164 // take the simple case
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001165 bool needsTextureDomain = needs_texture_domain(bitmap,
1166 srcRect,
1167 params,
1168 fContext->getMatrix(),
1169 doBicubic);
1170 this->internalDrawBitmap(bitmap,
1171 srcRect,
1172 params,
1173 paint,
1174 flags,
1175 doBicubic,
1176 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001177 }
1178}
1179
1180// Break 'bitmap' into several tiles to draw it since it has already
1181// been determined to be too large to fit in VRAM
1182void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
1183 const SkRect& srcRect,
1184 const SkIRect& clippedSrcIRect,
1185 const GrTextureParams& params,
1186 const SkPaint& paint,
1187 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001188 int tileSize,
1189 bool bicubic) {
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +00001190 // The following pixel lock is technically redundant, but it is desirable
1191 // to lock outside of the tile loop to prevent redecoding the whole image
1192 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1193 // is larger than the limit of the discardable memory pool.
1194 SkAutoLockPixels alp(bitmap);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001195 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1196
1197 int nx = bitmap.width() / tileSize;
1198 int ny = bitmap.height() / tileSize;
1199 for (int x = 0; x <= nx; x++) {
1200 for (int y = 0; y <= ny; y++) {
1201 SkRect tileR;
1202 tileR.set(SkIntToScalar(x * tileSize),
1203 SkIntToScalar(y * tileSize),
1204 SkIntToScalar((x + 1) * tileSize),
1205 SkIntToScalar((y + 1) * tileSize));
1206
1207 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1208 continue;
1209 }
1210
1211 if (!tileR.intersect(srcRect)) {
1212 continue;
1213 }
1214
1215 SkBitmap tmpB;
1216 SkIRect iTileR;
1217 tileR.roundOut(&iTileR);
1218 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1219 SkIntToScalar(iTileR.fTop));
1220
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001221 // Adjust the context matrix to draw at the right x,y in device space
1222 SkMatrix tmpM;
1223 GrContext::AutoMatrix am;
1224 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
1225 am.setPreConcat(fContext, tmpM);
1226
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001227 if (SkPaint::kNone_FilterLevel != paint.getFilterLevel() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001228 SkIRect iClampRect;
1229
1230 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1231 // In bleed mode we want to always expand the tile on all edges
1232 // but stay within the bitmap bounds
1233 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1234 } else {
1235 // In texture-domain/clamp mode we only want to expand the
1236 // tile on edges interior to "srcRect" (i.e., we want to
1237 // not bleed across the original clamped edges)
1238 srcRect.roundOut(&iClampRect);
1239 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001240 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1241 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001242 }
1243
1244 if (bitmap.extractSubset(&tmpB, iTileR)) {
1245 // now offset it to make it "local" to our tmp bitmap
1246 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001247 GrTextureParams paramsTemp = params;
1248 bool needsTextureDomain = needs_texture_domain(bitmap,
1249 srcRect,
1250 paramsTemp,
1251 fContext->getMatrix(),
1252 bicubic);
1253 this->internalDrawBitmap(tmpB,
1254 tileR,
1255 paramsTemp,
1256 paint,
1257 flags,
1258 bicubic,
1259 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001260 }
1261 }
1262 }
1263}
1264
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001265
1266/*
1267 * This is called by drawBitmap(), which has to handle images that may be too
1268 * large to be represented by a single texture.
1269 *
1270 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1271 * and that non-texture portion of the GrPaint has already been setup.
1272 */
1273void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
1274 const SkRect& srcRect,
1275 const GrTextureParams& params,
1276 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001277 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001278 bool bicubic,
1279 bool needsTextureDomain) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001280 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1281 bitmap.height() <= fContext->getMaxTextureSize());
1282
1283 GrTexture* texture;
bsalomonbcf0a522014-10-08 08:40:09 -07001284 AutoBitmapTexture abt(fContext, bitmap, &params, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001285 if (NULL == texture) {
1286 return;
1287 }
1288
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001289 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001290 SkRect paintRect;
1291 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1292 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1293 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1294 SkScalarMul(srcRect.fTop, hInv),
1295 SkScalarMul(srcRect.fRight, wInv),
1296 SkScalarMul(srcRect.fBottom, hInv));
1297
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001298 SkRect textureDomain = SkRect::MakeEmpty();
joshualittb0a8a372014-09-23 09:50:21 -07001299 SkAutoTUnref<GrFragmentProcessor> fp;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001300 if (needsTextureDomain && !(flags & SkCanvas::kBleed_DrawBitmapRectFlag)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001301 // Use a constrained texture domain to avoid color bleeding
1302 SkScalar left, top, right, bottom;
1303 if (srcRect.width() > SK_Scalar1) {
1304 SkScalar border = SK_ScalarHalf / texture->width();
1305 left = paintRect.left() + border;
1306 right = paintRect.right() - border;
1307 } else {
1308 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1309 }
1310 if (srcRect.height() > SK_Scalar1) {
1311 SkScalar border = SK_ScalarHalf / texture->height();
1312 top = paintRect.top() + border;
1313 bottom = paintRect.bottom() - border;
1314 } else {
1315 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1316 }
1317 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001318 if (bicubic) {
joshualittb0a8a372014-09-23 09:50:21 -07001319 fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001320 } else {
joshualittb0a8a372014-09-23 09:50:21 -07001321 fp.reset(GrTextureDomainEffect::Create(texture,
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001322 SkMatrix::I(),
1323 textureDomain,
1324 GrTextureDomain::kClamp_Mode,
1325 params.filterMode()));
1326 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001327 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001328 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1329 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
joshualittb0a8a372014-09-23 09:50:21 -07001330 fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001331 } else {
joshualittb0a8a372014-09-23 09:50:21 -07001332 fp.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001333 }
1334
1335 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1336 // the rest from the SkPaint.
1337 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001338 grPaint.addColorProcessor(fp);
reed0689d7b2014-06-14 05:30:20 -07001339 bool alphaOnly = !(kAlpha_8_SkColorType == bitmap.colorType());
bsalomon83d081a2014-07-08 09:56:10 -07001340 GrColor paintColor = (alphaOnly) ? SkColor2GrColorJustAlpha(paint.getColor()) :
1341 SkColor2GrColor(paint.getColor());
1342 SkPaint2GrPaintNoShader(this->context(), paint, paintColor, false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001343
bsalomon01c8da12014-08-04 09:21:30 -07001344 fContext->drawRectToRect(grPaint, dstRect, paintRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001345}
1346
1347static bool filter_texture(SkBaseDevice* device, GrContext* context,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001348 GrTexture* texture, const SkImageFilter* filter,
robertphillips6219e1f2014-10-20 08:12:04 -07001349 const SkImageFilter::Context& ctx,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001350 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001351 SkASSERT(filter);
1352 SkDeviceImageFilterProxy proxy(device);
1353
1354 if (filter->canFilterImageGPU()) {
1355 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1356 // filter. Also set the clip wide open and the matrix to identity.
1357 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001358 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001359 } else {
1360 return false;
1361 }
1362}
1363
1364void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1365 int left, int top, const SkPaint& paint) {
1366 // drawSprite is defined to be in device coords.
1367 CHECK_SHOULD_DRAW(draw, true);
1368
1369 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1370 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1371 return;
1372 }
1373
1374 int w = bitmap.width();
1375 int h = bitmap.height();
1376
1377 GrTexture* texture;
1378 // draw sprite uses the default texture params
bsalomonbcf0a522014-10-08 08:40:09 -07001379 AutoBitmapTexture abt(fContext, bitmap, NULL, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001380
1381 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001382 // This bitmap will own the filtered result as a texture.
1383 SkBitmap filteredBitmap;
1384
bsalomon49f085d2014-09-05 13:34:00 -07001385 if (filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001386 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001387 SkMatrix matrix(*draw.fMatrix);
1388 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001389 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
senorblancobe129b22014-08-08 07:14:35 -07001390 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
senorblanco55b6d8b2014-07-30 11:26:46 -07001391 // This cache is transient, and is freed (along with all its contained
1392 // textures) when it goes out of scope.
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001393 SkImageFilter::Context ctx(matrix, clipBounds, cache);
robertphillips6219e1f2014-10-20 08:12:04 -07001394 if (filter_texture(this, fContext, texture, filter, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001395 &offset)) {
1396 texture = (GrTexture*) filteredBitmap.getTexture();
1397 w = filteredBitmap.width();
1398 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001399 left += offset.x();
1400 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001401 } else {
1402 return;
1403 }
1404 }
1405
1406 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001407 grPaint.addColorTextureProcessor(texture, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001408
dandov9de5b512014-06-10 14:38:28 -07001409 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColorJustAlpha(paint.getColor()),
1410 false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001411
1412 fContext->drawRectToRect(grPaint,
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001413 SkRect::MakeXYWH(SkIntToScalar(left),
1414 SkIntToScalar(top),
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001415 SkIntToScalar(w),
1416 SkIntToScalar(h)),
1417 SkRect::MakeXYWH(0,
1418 0,
1419 SK_Scalar1 * w / texture->width(),
1420 SK_Scalar1 * h / texture->height()));
1421}
1422
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001423void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001424 const SkRect* src, const SkRect& dst,
1425 const SkPaint& paint,
1426 SkCanvas::DrawBitmapRectFlags flags) {
1427 SkMatrix matrix;
1428 SkRect bitmapBounds, tmpSrc;
1429
1430 bitmapBounds.set(0, 0,
1431 SkIntToScalar(bitmap.width()),
1432 SkIntToScalar(bitmap.height()));
1433
1434 // Compute matrix from the two rectangles
bsalomon49f085d2014-09-05 13:34:00 -07001435 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001436 tmpSrc = *src;
1437 } else {
1438 tmpSrc = bitmapBounds;
1439 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001440
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001441 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1442
1443 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
bsalomon49f085d2014-09-05 13:34:00 -07001444 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001445 if (!bitmapBounds.contains(tmpSrc)) {
1446 if (!tmpSrc.intersect(bitmapBounds)) {
1447 return; // nothing to draw
1448 }
1449 }
1450 }
1451
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001452 SkRect tmpDst;
1453 matrix.mapRect(&tmpDst, tmpSrc);
1454
1455 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1456 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1457 // Translate so that tempDst's top left is at the origin.
1458 matrix = *origDraw.fMatrix;
1459 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1460 draw.writable()->fMatrix = &matrix;
1461 }
1462 SkSize dstSize;
1463 dstSize.fWidth = tmpDst.width();
1464 dstSize.fHeight = tmpDst.height();
1465
1466 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001467}
1468
1469void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1470 int x, int y, const SkPaint& paint) {
1471 // clear of the source device must occur before CHECK_SHOULD_DRAW
egdanield78a1682014-07-09 10:41:26 -07001472 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawDevice", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001473 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1474 if (dev->fNeedClear) {
1475 // TODO: could check here whether we really need to draw at all
1476 dev->clear(0x0);
1477 }
1478
1479 // drawDevice is defined to be in device coords.
1480 CHECK_SHOULD_DRAW(draw, true);
1481
1482 GrRenderTarget* devRT = dev->accessRenderTarget();
1483 GrTexture* devTex;
1484 if (NULL == (devTex = devRT->asTexture())) {
1485 return;
1486 }
1487
1488 const SkBitmap& bm = dev->accessBitmap(false);
1489 int w = bm.width();
1490 int h = bm.height();
1491
1492 SkImageFilter* filter = paint.getImageFilter();
1493 // This bitmap will own the filtered result as a texture.
1494 SkBitmap filteredBitmap;
1495
bsalomon49f085d2014-09-05 13:34:00 -07001496 if (filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001497 SkIPoint offset = SkIPoint::Make(0, 0);
1498 SkMatrix matrix(*draw.fMatrix);
1499 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001500 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
senorblanco55b6d8b2014-07-30 11:26:46 -07001501 // This cache is transient, and is freed (along with all its contained
1502 // textures) when it goes out of scope.
senorblancobe129b22014-08-08 07:14:35 -07001503 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001504 SkImageFilter::Context ctx(matrix, clipBounds, cache);
robertphillips6219e1f2014-10-20 08:12:04 -07001505 if (filter_texture(this, fContext, devTex, filter, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001506 &offset)) {
1507 devTex = filteredBitmap.getTexture();
1508 w = filteredBitmap.width();
1509 h = filteredBitmap.height();
1510 x += offset.fX;
1511 y += offset.fY;
1512 } else {
1513 return;
1514 }
1515 }
1516
1517 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001518 grPaint.addColorTextureProcessor(devTex, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001519
dandov9de5b512014-06-10 14:38:28 -07001520 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColorJustAlpha(paint.getColor()),
1521 false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001522
1523 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1524 SkIntToScalar(y),
1525 SkIntToScalar(w),
1526 SkIntToScalar(h));
1527
1528 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1529 // scratch texture).
1530 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1531 SK_Scalar1 * h / devTex->height());
1532
1533 fContext->drawRectToRect(grPaint, dstRect, srcRect);
1534}
1535
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001536bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001537 return filter->canFilterImageGPU();
1538}
1539
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001540bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001541 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001542 SkBitmap* result, SkIPoint* offset) {
1543 // want explicitly our impl, so guard against a subclass of us overriding it
1544 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1545 return false;
1546 }
1547
1548 SkAutoLockPixels alp(src, !src.getTexture());
1549 if (!src.getTexture() && !src.readyToDraw()) {
1550 return false;
1551 }
1552
1553 GrTexture* texture;
1554 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1555 // must be pushed upstack.
bsalomonbcf0a522014-10-08 08:40:09 -07001556 AutoBitmapTexture abt(fContext, src, NULL, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001557
robertphillips6219e1f2014-10-20 08:12:04 -07001558 return filter_texture(this, fContext, texture, filter, ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001559}
1560
1561///////////////////////////////////////////////////////////////////////////////
1562
1563// must be in SkCanvas::VertexMode order
1564static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1565 kTriangles_GrPrimitiveType,
1566 kTriangleStrip_GrPrimitiveType,
1567 kTriangleFan_GrPrimitiveType,
1568};
1569
1570void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1571 int vertexCount, const SkPoint vertices[],
1572 const SkPoint texs[], const SkColor colors[],
1573 SkXfermode* xmode,
1574 const uint16_t indices[], int indexCount,
1575 const SkPaint& paint) {
1576 CHECK_SHOULD_DRAW(draw, false);
1577
egdanield78a1682014-07-09 10:41:26 -07001578 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawVertices", fContext);
mtklein533eb782014-08-27 10:39:42 -07001579
dandov32a311b2014-07-15 19:46:26 -07001580 const uint16_t* outIndices;
1581 SkAutoTDeleteArray<uint16_t> outAlloc(NULL);
1582 GrPrimitiveType primType;
1583 GrPaint grPaint;
bsalomona098dd42014-08-06 11:01:44 -07001584
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001585 // If both textures and vertex-colors are NULL, strokes hairlines with the paint's color.
1586 if ((NULL == texs || NULL == paint.getShader()) && NULL == colors) {
mtklein533eb782014-08-27 10:39:42 -07001587
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001588 texs = NULL;
mtklein533eb782014-08-27 10:39:42 -07001589
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001590 SkPaint copy(paint);
1591 copy.setStyle(SkPaint::kStroke_Style);
1592 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001593
dandov32a311b2014-07-15 19:46:26 -07001594 // we ignore the shader if texs is null.
1595 SkPaint2GrPaintNoShader(this->context(), copy, SkColor2GrColor(copy.getColor()),
1596 NULL == colors, &grPaint);
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001597
dandov32a311b2014-07-15 19:46:26 -07001598 primType = kLines_GrPrimitiveType;
1599 int triangleCount = 0;
bsalomona098dd42014-08-06 11:01:44 -07001600 int n = (NULL == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001601 switch (vmode) {
1602 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001603 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001604 break;
1605 case SkCanvas::kTriangleStrip_VertexMode:
1606 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001607 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001608 break;
1609 }
mtklein533eb782014-08-27 10:39:42 -07001610
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001611 VertState state(vertexCount, indices, indexCount);
1612 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001613
dandov32a311b2014-07-15 19:46:26 -07001614 //number of indices for lines per triangle with kLines
1615 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001616
dandov32a311b2014-07-15 19:46:26 -07001617 outAlloc.reset(SkNEW_ARRAY(uint16_t, indexCount));
1618 outIndices = outAlloc.get();
1619 uint16_t* auxIndices = outAlloc.get();
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001620 int i = 0;
1621 while (vertProc(&state)) {
dandov32a311b2014-07-15 19:46:26 -07001622 auxIndices[i] = state.f0;
1623 auxIndices[i + 1] = state.f1;
1624 auxIndices[i + 2] = state.f1;
1625 auxIndices[i + 3] = state.f2;
1626 auxIndices[i + 4] = state.f2;
1627 auxIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001628 i += 6;
1629 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001630 } else {
dandov32a311b2014-07-15 19:46:26 -07001631 outIndices = indices;
1632 primType = gVertexMode2PrimitiveType[vmode];
mtklein533eb782014-08-27 10:39:42 -07001633
dandov32a311b2014-07-15 19:46:26 -07001634 if (NULL == texs || NULL == paint.getShader()) {
1635 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColor(paint.getColor()),
1636 NULL == colors, &grPaint);
1637 } else {
1638 SkPaint2GrPaintShader(this->context(), paint, NULL == colors, &grPaint);
1639 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001640 }
1641
mtklein2583b622014-06-04 08:20:41 -07001642#if 0
bsalomon49f085d2014-09-05 13:34:00 -07001643 if (xmode && texs && colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001644 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1645 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
mtklein2583b622014-06-04 08:20:41 -07001646 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001647 }
1648 }
mtklein2583b622014-06-04 08:20:41 -07001649#endif
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001650
1651 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001652 if (colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001653 // need to convert byte order and from non-PM to PM
1654 convertedColors.reset(vertexCount);
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001655 SkColor color;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001656 for (int i = 0; i < vertexCount; ++i) {
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001657 color = colors[i];
1658 if (paint.getAlpha() != 255) {
1659 color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paint.getAlpha()));
1660 }
1661 convertedColors[i] = SkColor2GrColor(color);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001662 }
1663 colors = convertedColors.get();
1664 }
1665 fContext->drawVertices(grPaint,
dandov32a311b2014-07-15 19:46:26 -07001666 primType,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001667 vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001668 vertices,
1669 texs,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001670 colors,
dandov32a311b2014-07-15 19:46:26 -07001671 outIndices,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001672 indexCount);
1673}
1674
1675///////////////////////////////////////////////////////////////////////////////
1676
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001677void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1678 size_t byteLength, SkScalar x, SkScalar y,
1679 const SkPaint& paint) {
1680 CHECK_SHOULD_DRAW(draw, false);
egdanield78a1682014-07-09 10:41:26 -07001681 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001682
jvanverth8c27a182014-10-14 08:45:50 -07001683 GrPaint grPaint;
1684 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001685
jvanverth8c27a182014-10-14 08:45:50 -07001686 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001687
jvanverth8c27a182014-10-14 08:45:50 -07001688 if (!fTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y)) {
1689 // this will just call our drawPath()
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001690 draw.drawText_asPaths((const char*)text, byteLength, x, y, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001691 }
1692}
1693
fmalita05c4a432014-09-29 06:29:53 -07001694void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1695 const SkScalar pos[], int scalarsPerPos,
1696 const SkPoint& offset, const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -07001697 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPosText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001698 CHECK_SHOULD_DRAW(draw, false);
1699
jvanverth8c27a182014-10-14 08:45:50 -07001700 GrPaint grPaint;
1701 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001702
jvanverth8c27a182014-10-14 08:45:50 -07001703 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001704
jvanverth8c27a182014-10-14 08:45:50 -07001705 if (!fTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
1706 scalarsPerPos, offset)) {
1707 // this will just call our drawPath()
fmalita05c4a432014-09-29 06:29:53 -07001708 draw.drawPosText_asPaths((const char*)text, byteLength, pos, scalarsPerPos, offset, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001709 }
1710}
1711
1712void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1713 size_t len, const SkPath& path,
1714 const SkMatrix* m, const SkPaint& paint) {
1715 CHECK_SHOULD_DRAW(draw, false);
1716
1717 SkASSERT(draw.fDevice == this);
1718 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1719}
1720
1721///////////////////////////////////////////////////////////////////////////////
1722
1723bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1724 if (!paint.isLCDRenderText()) {
1725 // we're cool with the paint as is
1726 return false;
1727 }
1728
1729 if (paint.getShader() ||
1730 paint.getXfermode() || // unless its srcover
1731 paint.getMaskFilter() ||
1732 paint.getRasterizer() ||
1733 paint.getColorFilter() ||
1734 paint.getPathEffect() ||
1735 paint.isFakeBoldText() ||
1736 paint.getStyle() != SkPaint::kFill_Style) {
reed3375c802014-09-16 12:27:55 -07001737 // turn off lcd, but turn on kGenA8
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001738 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
reed3375c802014-09-16 12:27:55 -07001739 flags->fFlags |= SkPaint::kGenA8FromLCD_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001740 return true;
1741 }
1742 // we're cool with the paint as is
1743 return false;
1744}
1745
1746void SkGpuDevice::flush() {
1747 DO_DEFERRED_CLEAR();
1748 fContext->resolveRenderTarget(fRenderTarget);
1749}
1750
1751///////////////////////////////////////////////////////////////////////////////
1752
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001753SkBaseDevice* SkGpuDevice::onCreateDevice(const SkImageInfo& info, Usage usage) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001754 GrTextureDesc desc;
1755 desc.fConfig = fRenderTarget->config();
1756 desc.fFlags = kRenderTarget_GrTextureFlagBit;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001757 desc.fWidth = info.width();
1758 desc.fHeight = info.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001759 desc.fSampleCnt = fRenderTarget->numSamples();
1760
1761 SkAutoTUnref<GrTexture> texture;
1762 // Skia's convention is to only clear a device if it is non-opaque.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001763 unsigned flags = info.isOpaque() ? 0 : kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001764
1765#if CACHE_COMPATIBLE_DEVICE_TEXTURES
hcm4396fa52014-10-27 21:43:30 -07001766 // layers are never draw in repeat modes, so we can request an approx
1767 // match and ignore any padding.
1768 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == usage) ?
1769 GrContext::kApprox_ScratchTexMatch :
1770 GrContext::kExact_ScratchTexMatch;
bsalomone3059732014-10-14 11:47:22 -07001771 texture.reset(fContext->refScratchTexture(desc, match));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001772#else
1773 texture.reset(fContext->createUncachedTexture(desc, NULL, 0));
1774#endif
bsalomon49f085d2014-09-05 13:34:00 -07001775 if (texture.get()) {
reed4a8126e2014-09-22 07:29:03 -07001776 return SkGpuDevice::Create(texture, SkSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType), flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001777 } else {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001778 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1779 info.width(), info.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001780 return NULL;
1781 }
1782}
1783
reed4a8126e2014-09-22 07:29:03 -07001784SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
1785 return SkSurface::NewRenderTarget(fContext, info, fRenderTarget->numSamples(), &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001786}
1787
robertphillips9b14f262014-06-04 05:40:44 -07001788void SkGpuDevice::EXPERIMENTAL_optimize(const SkPicture* picture) {
robertphillipsd771f6b2014-07-22 10:18:06 -07001789 fContext->getLayerCache()->processDeletedPictures();
1790
bsalomon49f085d2014-09-05 13:34:00 -07001791 if (picture->fData.get() && !picture->fData->suitableForLayerOptimization()) {
robertphillipsc019ec42014-08-12 05:35:58 -07001792 return;
1793 }
1794
robertphillips6617d502014-08-18 09:39:34 -07001795 SkPicture::AccelData::Key key = GrAccelData::ComputeAccelDataKey();
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001796
commit-bot@chromium.org8ec8bab2014-05-14 13:11:48 +00001797 const SkPicture::AccelData* existing = picture->EXPERIMENTAL_getAccelData(key);
bsalomon49f085d2014-09-05 13:34:00 -07001798 if (existing) {
commit-bot@chromium.org8ec8bab2014-05-14 13:11:48 +00001799 return;
1800 }
1801
robertphillipsd6283302014-08-27 11:53:28 -07001802 GPUOptimize(picture);
robertphillipsd771f6b2014-07-22 10:18:06 -07001803
1804 fContext->getLayerCache()->trackPicture(picture);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001805}
1806
robertphillips30d2cc62014-09-24 08:52:18 -07001807bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* mainCanvas, const SkPicture* mainPicture,
reedd5fa1a42014-08-09 11:08:05 -07001808 const SkMatrix* matrix, const SkPaint* paint) {
1809 // todo: should handle these natively
1810 if (matrix || paint) {
1811 return false;
1812 }
1813
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001814 SkRect clipBounds;
robertphillips0c423322014-07-31 11:02:38 -07001815 if (!mainCanvas->getClipBounds(&clipBounds)) {
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001816 return true;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001817 }
1818
robertphillipsfd61ed02014-10-28 07:21:44 -07001819 SkTDArray<GrHoistedLayer> atlasedNeedRendering, atlasedRecycled;
robertphillips1c4c5282014-09-18 12:03:15 -07001820
robertphillipsfd61ed02014-10-28 07:21:44 -07001821 GrLayerHoister::FindLayersToAtlas(fContext, mainPicture,
1822 clipBounds,
1823 &atlasedNeedRendering, &atlasedRecycled);
1824
1825 GrLayerHoister::DrawLayersToAtlas(fContext, atlasedNeedRendering);
1826
1827 SkTDArray<GrHoistedLayer> needRendering, recycled;
1828
1829 GrLayerHoister::FindLayersToHoist(fContext, mainPicture,
1830 clipBounds,
1831 &needRendering, &recycled);
1832
1833 GrLayerHoister::DrawLayers(fContext, needRendering);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001834
robertphillips4aa6dfc2014-09-17 07:50:47 -07001835 GrReplacements replacements;
1836
robertphillipsfd61ed02014-10-28 07:21:44 -07001837 GrLayerHoister::ConvertLayersToReplacements(needRendering, &replacements);
1838 GrLayerHoister::ConvertLayersToReplacements(recycled, &replacements);
robertphillips4aa6dfc2014-09-17 07:50:47 -07001839
robertphillips64bf7672014-08-21 13:07:35 -07001840 // Render the entire picture using new layers
robertphillipsee6631e2014-09-29 05:32:49 -07001841 const SkMatrix initialMatrix = mainCanvas->getTotalMatrix();
1842
1843 GrRecordReplaceDraw(mainPicture, mainCanvas, &replacements, initialMatrix, NULL);
robertphillips64bf7672014-08-21 13:07:35 -07001844
robertphillipsfd61ed02014-10-28 07:21:44 -07001845 GrLayerHoister::UnlockLayers(fContext, needRendering);
1846 GrLayerHoister::UnlockLayers(fContext, recycled);
1847 GrLayerHoister::UnlockLayers(fContext, atlasedNeedRendering);
1848 GrLayerHoister::UnlockLayers(fContext, atlasedRecycled);
robertphillips64bf7672014-08-21 13:07:35 -07001849
1850 return true;
1851}
1852
senorblancobe129b22014-08-08 07:14:35 -07001853SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() {
senorblanco55b6d8b2014-07-30 11:26:46 -07001854 // We always return a transient cache, so it is freed after each
1855 // filter traversal.
senorblancobe129b22014-08-08 07:14:35 -07001856 return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001857}