blob: 6a9c31de3f77e54cabec91c1e06029cbbac280db [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
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000080
Brian Salomon9323b8b2014-10-07 15:07:38 -040081class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
82public:
83 SkAutoCachedTexture()
84 : fDevice(NULL)
85 , fTexture(NULL) {
robertphillipsdbe60742014-09-30 06:54:17 -070086 }
87
Brian Salomon9323b8b2014-10-07 15:07:38 -040088 SkAutoCachedTexture(SkGpuDevice* device,
89 const SkBitmap& bitmap,
90 const GrTextureParams* params,
91 GrTexture** texture)
92 : fDevice(NULL)
93 , fTexture(NULL) {
94 SkASSERT(texture);
95 *texture = this->set(device, bitmap, params);
96 }
97
98 ~SkAutoCachedTexture() {
99 if (fTexture) {
100 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
robertphillipsdbe60742014-09-30 06:54:17 -0700101 }
robertphillipsdbe60742014-09-30 06:54:17 -0700102 }
103
Brian Salomon9323b8b2014-10-07 15:07:38 -0400104 GrTexture* set(SkGpuDevice* device,
105 const SkBitmap& bitmap,
106 const GrTextureParams* params) {
107 if (fTexture) {
108 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
109 fTexture = NULL;
110 }
111 fDevice = device;
112 GrTexture* result = (GrTexture*)bitmap.getTexture();
113 if (NULL == result) {
114 // Cannot return the native texture so look it up in our cache
115 fTexture = GrLockAndRefCachedBitmapTexture(device->context(), bitmap, params);
116 result = fTexture;
117 }
118 return result;
119 }
120
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000121private:
Brian Salomon9323b8b2014-10-07 15:07:38 -0400122 SkGpuDevice* fDevice;
123 GrTexture* fTexture;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000124};
125
126///////////////////////////////////////////////////////////////////////////////
127
128struct GrSkDrawProcs : public SkDrawProcs {
129public:
130 GrContext* fContext;
131 GrTextContext* fTextContext;
132 GrFontScaler* fFontScaler; // cached in the skia glyphcache
133};
134
135///////////////////////////////////////////////////////////////////////////////
136
reed4a8126e2014-09-22 07:29:03 -0700137SkGpuDevice* SkGpuDevice::Create(GrSurface* surface, const SkSurfaceProps& props, unsigned flags) {
bsalomon49f085d2014-09-05 13:34:00 -0700138 SkASSERT(surface);
bsalomon32d0b3b2014-08-29 07:50:23 -0700139 if (NULL == surface->asRenderTarget() || surface->wasDestroyed()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000140 return NULL;
141 }
reed4a8126e2014-09-22 07:29:03 -0700142 return SkNEW_ARGS(SkGpuDevice, (surface, props, flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000143}
144
reed4a8126e2014-09-22 07:29:03 -0700145SkGpuDevice::SkGpuDevice(GrSurface* surface, const SkSurfaceProps& props, unsigned flags) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000146
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000147 fDrawProcs = NULL;
148
bsalomon32d0b3b2014-08-29 07:50:23 -0700149 fContext = SkRef(surface->getContext());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000150
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000151 fNeedClear = flags & kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000152
bsalomon32d0b3b2014-08-29 07:50:23 -0700153 fRenderTarget = SkRef(surface->asRenderTarget());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000154
bsalomonafbf2d62014-09-30 12:18:44 -0700155 SkImageInfo info = surface->surfacePriv().info();
Brian Salomon9323b8b2014-10-07 15:07:38 -0400156 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef,
157 (info, surface, SkToBool(flags & kCached_Flag)));
bsalomonafbf2d62014-09-30 12:18:44 -0700158 fLegacyBitmap.setInfo(info);
reed89443ab2014-06-27 11:34:19 -0700159 fLegacyBitmap.setPixelRef(pr)->unref();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700160
reed4a8126e2014-09-22 07:29:03 -0700161 this->setPixelGeometry(props.pixelGeometry());
162
kkinnunenc6cb56f2014-06-24 00:12:27 -0700163 bool useDFFonts = !!(flags & kDFFonts_Flag);
reede010f1c2014-09-17 10:49:38 -0700164 fMainTextContext = fContext->createTextContext(fRenderTarget, this->getLeakyProperties(), useDFFonts);
165 fFallbackTextContext = SkNEW_ARGS(GrBitmapTextContext, (fContext, this->getLeakyProperties()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000166}
167
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000168SkGpuDevice* SkGpuDevice::Create(GrContext* context, const SkImageInfo& origInfo,
reed4a8126e2014-09-22 07:29:03 -0700169 const SkSurfaceProps& props, int sampleCount) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000170 if (kUnknown_SkColorType == origInfo.colorType() ||
171 origInfo.width() < 0 || origInfo.height() < 0) {
172 return NULL;
173 }
174
reede5ea5002014-09-03 11:54:58 -0700175 SkColorType ct = origInfo.colorType();
176 SkAlphaType at = origInfo.alphaType();
robertphillipsa0537de2014-09-18 08:01:23 -0700177 // TODO: perhaps we can loosen this check now that colortype is more detailed
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000178 // e.g. can we support both RGBA and BGRA here?
reede5ea5002014-09-03 11:54:58 -0700179 if (kRGB_565_SkColorType == ct) {
180 at = kOpaque_SkAlphaType; // force this setting
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000181 } else {
reede5ea5002014-09-03 11:54:58 -0700182 ct = kN32_SkColorType;
183 if (kOpaque_SkAlphaType != at) {
184 at = kPremul_SkAlphaType; // force this setting
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000185 }
186 }
reede5ea5002014-09-03 11:54:58 -0700187 const SkImageInfo info = SkImageInfo::Make(origInfo.width(), origInfo.height(), ct, at);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000188
189 GrTextureDesc desc;
190 desc.fFlags = kRenderTarget_GrTextureFlagBit;
191 desc.fWidth = info.width();
192 desc.fHeight = info.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000193 desc.fConfig = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000194 desc.fSampleCnt = sampleCount;
195
196 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0));
197 if (!texture.get()) {
198 return NULL;
199 }
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000200
reed4a8126e2014-09-22 07:29:03 -0700201 return SkNEW_ARGS(SkGpuDevice, (texture.get(), props));
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000202}
203
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000204SkGpuDevice::~SkGpuDevice() {
205 if (fDrawProcs) {
206 delete fDrawProcs;
207 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000208
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000209 delete fMainTextContext;
210 delete fFallbackTextContext;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000211
212 // The GrContext takes a ref on the target. We don't want to cause the render
213 // target to be unnecessarily kept alive.
214 if (fContext->getRenderTarget() == fRenderTarget) {
215 fContext->setRenderTarget(NULL);
216 }
217
218 if (fContext->getClip() == &fClipData) {
219 fContext->setClip(NULL);
220 }
221
bsalomon32d0b3b2014-08-29 07:50:23 -0700222 fRenderTarget->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000223 fContext->unref();
224}
225
226///////////////////////////////////////////////////////////////////////////////
227
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000228bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
229 int x, int y) {
230 DO_DEFERRED_CLEAR();
231
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(dstInfo);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000234 if (kUnknown_GrPixelConfig == config) {
235 return false;
236 }
237
238 uint32_t flags = 0;
239 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
240 flags = GrContext::kUnpremul_PixelOpsFlag;
241 }
242 return fContext->readRenderTargetPixels(fRenderTarget, x, y, dstInfo.width(), dstInfo.height(),
243 config, dstPixels, dstRowBytes, flags);
244}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000245
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000246bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
247 int x, int y) {
248 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000249 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000250 if (kUnknown_GrPixelConfig == config) {
251 return false;
252 }
253 uint32_t flags = 0;
254 if (kUnpremul_SkAlphaType == info.alphaType()) {
255 flags = GrContext::kUnpremul_PixelOpsFlag;
256 }
257 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
258
259 // need to bump our genID for compatibility with clients that "know" we have a bitmap
reed89443ab2014-06-27 11:34:19 -0700260 fLegacyBitmap.notifyPixelsChanged();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000261
262 return true;
263}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000264
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000265const SkBitmap& SkGpuDevice::onAccessBitmap() {
266 DO_DEFERRED_CLEAR();
reed89443ab2014-06-27 11:34:19 -0700267 return fLegacyBitmap;
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000268}
269
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000270void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
271 INHERITED::onAttachToCanvas(canvas);
272
273 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
274 fClipData.fClipStack = canvas->getClipStack();
275}
276
277void SkGpuDevice::onDetachFromCanvas() {
278 INHERITED::onDetachFromCanvas();
279 fClipData.fClipStack = NULL;
280}
281
282// call this every draw call, to ensure that the context reflects our state,
283// and not the state from some other canvas/device
284void SkGpuDevice::prepareDraw(const SkDraw& draw, bool forceIdentity) {
bsalomon49f085d2014-09-05 13:34:00 -0700285 SkASSERT(fClipData.fClipStack);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000286
287 fContext->setRenderTarget(fRenderTarget);
288
289 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
290
291 if (forceIdentity) {
292 fContext->setIdentityMatrix();
293 } else {
294 fContext->setMatrix(*draw.fMatrix);
295 }
296 fClipData.fOrigin = this->getOrigin();
297
298 fContext->setClip(&fClipData);
299
300 DO_DEFERRED_CLEAR();
301}
302
303GrRenderTarget* SkGpuDevice::accessRenderTarget() {
304 DO_DEFERRED_CLEAR();
305 return fRenderTarget;
306}
307
308///////////////////////////////////////////////////////////////////////////////
309
310SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
311SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
312SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
313SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
314SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
315 shader_type_mismatch);
316SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
317 shader_type_mismatch);
318SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
319SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
320
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000321///////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000322
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000323void SkGpuDevice::clear(SkColor color) {
egdanield78a1682014-07-09 10:41:26 -0700324 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::clear", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000325 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
326 fContext->clear(&rect, SkColor2GrColor(color), true, fRenderTarget);
327 fNeedClear = false;
328}
329
330void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
331 CHECK_SHOULD_DRAW(draw, false);
egdanield78a1682014-07-09 10:41:26 -0700332 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPaint", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000333
334 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000335 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000336
337 fContext->drawPaint(grPaint);
338}
339
340// must be in SkCanvas::PointMode order
341static const GrPrimitiveType gPointMode2PrimtiveType[] = {
342 kPoints_GrPrimitiveType,
343 kLines_GrPrimitiveType,
344 kLineStrip_GrPrimitiveType
345};
346
347void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
348 size_t count, const SkPoint pts[], const SkPaint& paint) {
349 CHECK_FOR_ANNOTATION(paint);
350 CHECK_SHOULD_DRAW(draw, false);
351
352 SkScalar width = paint.getStrokeWidth();
353 if (width < 0) {
354 return;
355 }
356
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000357 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
egdaniele61c4112014-06-12 10:24:21 -0700358 GrStrokeInfo strokeInfo(paint, SkPaint::kStroke_Style);
359 GrPaint grPaint;
360 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
361 SkPath path;
362 path.moveTo(pts[0]);
363 path.lineTo(pts[1]);
364 fContext->drawPath(grPaint, path, strokeInfo);
365 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000366 }
367
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000368 // we only handle hairlines and paints without path effects or mask filters,
369 // else we let the SkDraw call our drawPath()
370 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
371 draw.drawPoints(mode, count, pts, paint, true);
372 return;
373 }
374
375 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000376 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000377
378 fContext->drawVertices(grPaint,
379 gPointMode2PrimtiveType[mode],
robertphillips@google.coma4662862013-11-21 14:24:16 +0000380 SkToS32(count),
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000381 (SkPoint*)pts,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000382 NULL,
383 NULL,
384 NULL,
385 0);
386}
387
388///////////////////////////////////////////////////////////////////////////////
389
390void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
391 const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -0700392 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRect", fContext);
393
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000394 CHECK_FOR_ANNOTATION(paint);
395 CHECK_SHOULD_DRAW(draw, false);
396
397 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
398 SkScalar width = paint.getStrokeWidth();
399
400 /*
401 We have special code for hairline strokes, miter-strokes, bevel-stroke
402 and fills. Anything else we just call our path code.
403 */
404 bool usePath = doStroke && width > 0 &&
405 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
406 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
407 // another two reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700408
409 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000410 usePath = true;
411 }
egdanield58a0ba2014-06-11 10:30:05 -0700412
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000413 if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) {
414#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
415 if (doStroke) {
416#endif
417 usePath = true;
418#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
419 } else {
420 usePath = !fContext->getMatrix().preservesRightAngles();
421 }
422#endif
423 }
424 // until we can both stroke and fill rectangles
425 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
426 usePath = true;
427 }
428
egdanield58a0ba2014-06-11 10:30:05 -0700429 GrStrokeInfo strokeInfo(paint);
430
431 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700432 if (!usePath && pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700433 usePath = true;
434 }
435
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000436 if (usePath) {
437 SkPath path;
438 path.addRect(rect);
439 this->drawPath(draw, path, paint, NULL, true);
440 return;
441 }
442
443 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 fContext->drawRect(grPaint, rect, &strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000447}
448
449///////////////////////////////////////////////////////////////////////////////
450
451void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
452 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700453 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRRect", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000454 CHECK_FOR_ANNOTATION(paint);
455 CHECK_SHOULD_DRAW(draw, false);
456
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000457 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000458 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
Mike Klein744fb732014-06-23 15:13:26 -0400459
egdanield58a0ba2014-06-11 10:30:05 -0700460 GrStrokeInfo strokeInfo(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000461 if (paint.getMaskFilter()) {
462 // try to hit the fast path for drawing filtered round rects
463
464 SkRRect devRRect;
465 if (rect.transform(fContext->getMatrix(), &devRRect)) {
466 if (devRRect.allCornersCircular()) {
467 SkRect maskRect;
468 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(),
469 draw.fClip->getBounds(),
470 fContext->getMatrix(),
471 &maskRect)) {
472 SkIRect finalIRect;
473 maskRect.roundOut(&finalIRect);
474 if (draw.fClip->quickReject(finalIRect)) {
475 // clipped out
476 return;
477 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000478 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext, &grPaint,
egdanield58a0ba2014-06-11 10:30:05 -0700479 strokeInfo.getStrokeRec(),
480 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000481 return;
482 }
483 }
484
485 }
486 }
487
488 }
489
egdanield58a0ba2014-06-11 10:30:05 -0700490 bool usePath = false;
491
492 if (paint.getMaskFilter()) {
493 usePath = true;
494 } else {
495 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700496 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700497 usePath = true;
498 }
499 }
500
501
502 if (usePath) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000503 SkPath path;
504 path.addRRect(rect);
505 this->drawPath(draw, path, paint, NULL, true);
506 return;
507 }
Mike Klein744fb732014-06-23 15:13:26 -0400508
egdanield58a0ba2014-06-11 10:30:05 -0700509 fContext->drawRRect(grPaint, rect, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000510}
511
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000512void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
513 const SkRRect& inner, const SkPaint& paint) {
514 SkStrokeRec stroke(paint);
515 if (stroke.isFillStyle()) {
516
517 CHECK_FOR_ANNOTATION(paint);
518 CHECK_SHOULD_DRAW(draw, false);
519
520 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000521 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000522
523 if (NULL == paint.getMaskFilter() && NULL == paint.getPathEffect()) {
524 fContext->drawDRRect(grPaint, outer, inner);
525 return;
526 }
527 }
528
529 SkPath path;
530 path.addRRect(outer);
531 path.addRRect(inner);
532 path.setFillType(SkPath::kEvenOdd_FillType);
533
534 this->drawPath(draw, path, paint, NULL, true);
535}
536
537
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000538/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000539
540void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
541 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700542 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawOval", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000543 CHECK_FOR_ANNOTATION(paint);
544 CHECK_SHOULD_DRAW(draw, false);
545
egdanield58a0ba2014-06-11 10:30:05 -0700546 GrStrokeInfo strokeInfo(paint);
547
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000548 bool usePath = false;
549 // some basic reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700550 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000551 usePath = true;
egdanield58a0ba2014-06-11 10:30:05 -0700552 } else {
553 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700554 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700555 usePath = true;
556 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000557 }
558
559 if (usePath) {
560 SkPath path;
561 path.addOval(oval);
562 this->drawPath(draw, path, paint, NULL, true);
563 return;
564 }
565
566 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000567 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000568
egdanield58a0ba2014-06-11 10:30:05 -0700569 fContext->drawOval(grPaint, oval, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000570}
571
572#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000573
574///////////////////////////////////////////////////////////////////////////////
575
576// helpers for applying mask filters
577namespace {
578
579// Draw a mask using the supplied paint. Since the coverage/geometry
580// is already burnt into the mask this boils down to a rect draw.
581// Return true if the mask was successfully drawn.
582bool draw_mask(GrContext* context, const SkRect& maskRect,
583 GrPaint* grp, GrTexture* mask) {
584 GrContext::AutoMatrix am;
585 if (!am.setIdentity(context, grp)) {
586 return false;
587 }
588
589 SkMatrix matrix;
590 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
591 matrix.postIDiv(mask->width(), mask->height());
592
joshualittb0a8a372014-09-23 09:50:21 -0700593 grp->addCoverageProcessor(GrSimpleTextureEffect::Create(mask, matrix))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000594 context->drawRect(*grp, maskRect);
595 return true;
596}
597
598bool draw_with_mask_filter(GrContext* context, const SkPath& devPath,
reed868074b2014-06-03 10:53:59 -0700599 SkMaskFilter* filter, const SkRegion& clip,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000600 GrPaint* grp, SkPaint::Style style) {
601 SkMask srcM, dstM;
602
603 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
604 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
605 return false;
606 }
607 SkAutoMaskFreeImage autoSrc(srcM.fImage);
608
609 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
610 return false;
611 }
612 // this will free-up dstM when we're done (allocated in filterMask())
613 SkAutoMaskFreeImage autoDst(dstM.fImage);
614
615 if (clip.quickReject(dstM.fBounds)) {
616 return false;
617 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000618
619 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
620 // the current clip (and identity matrix) and GrPaint settings
621 GrTextureDesc desc;
622 desc.fWidth = dstM.fBounds.width();
623 desc.fHeight = dstM.fBounds.height();
624 desc.fConfig = kAlpha_8_GrPixelConfig;
625
626 GrAutoScratchTexture ast(context, desc);
627 GrTexture* texture = ast.texture();
628
629 if (NULL == texture) {
630 return false;
631 }
632 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
633 dstM.fImage, dstM.fRowBytes);
634
635 SkRect maskRect = SkRect::Make(dstM.fBounds);
636
637 return draw_mask(context, maskRect, grp, texture);
638}
639
640// Create a mask of 'devPath' and place the result in 'mask'. Return true on
641// success; false otherwise.
642bool create_mask_GPU(GrContext* context,
643 const SkRect& maskRect,
644 const SkPath& devPath,
egdanield58a0ba2014-06-11 10:30:05 -0700645 const GrStrokeInfo& strokeInfo,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000646 bool doAA,
derekff4555aa2014-10-06 12:19:12 -0700647 GrAutoScratchTexture* mask,
648 int SampleCnt) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000649 GrTextureDesc desc;
650 desc.fFlags = kRenderTarget_GrTextureFlagBit;
651 desc.fWidth = SkScalarCeilToInt(maskRect.width());
652 desc.fHeight = SkScalarCeilToInt(maskRect.height());
derekff4555aa2014-10-06 12:19:12 -0700653 desc.fSampleCnt = SampleCnt;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000654 // We actually only need A8, but it often isn't supported as a
655 // render target so default to RGBA_8888
656 desc.fConfig = kRGBA_8888_GrPixelConfig;
derekff4555aa2014-10-06 12:19:12 -0700657
658 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig,
659 desc.fSampleCnt > 0)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000660 desc.fConfig = kAlpha_8_GrPixelConfig;
661 }
662
663 mask->set(context, desc);
664 if (NULL == mask->texture()) {
665 return false;
666 }
667
668 GrTexture* maskTexture = mask->texture();
669 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
670
671 GrContext::AutoRenderTarget art(context, maskTexture->asRenderTarget());
672 GrContext::AutoClip ac(context, clipRect);
673
674 context->clear(NULL, 0x0, true);
675
676 GrPaint tempPaint;
677 if (doAA) {
678 tempPaint.setAntiAlias(true);
679 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
680 // blend coeff of zero requires dual source blending support in order
681 // to properly blend partially covered pixels. This means the AA
682 // code path may not be taken. So we use a dst blend coeff of ISA. We
683 // could special case AA draws to a dst surface with known alpha=0 to
684 // use a zero dst coeff when dual source blending isn't available.
685 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
686 }
687
688 GrContext::AutoMatrix am;
689
690 // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint.
691 SkMatrix translate;
692 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
693 am.set(context, translate);
egdanield58a0ba2014-06-11 10:30:05 -0700694 context->drawPath(tempPaint, devPath, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000695 return true;
696}
697
698SkBitmap wrap_texture(GrTexture* texture) {
699 SkBitmap result;
bsalomonafbf2d62014-09-30 12:18:44 -0700700 result.setInfo(texture->surfacePriv().info());
reed6c225732014-06-09 19:52:07 -0700701 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (result.info(), texture)))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000702 return result;
703}
704
705};
706
707void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
708 const SkPaint& paint, const SkMatrix* prePathMatrix,
709 bool pathIsMutable) {
710 CHECK_FOR_ANNOTATION(paint);
711 CHECK_SHOULD_DRAW(draw, false);
egdanield78a1682014-07-09 10:41:26 -0700712 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPath", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000713
714 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000715 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000716
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000717 // If we have a prematrix, apply it to the path, optimizing for the case
718 // where the original path can in fact be modified in place (even though
719 // its parameter type is const).
720 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000721 SkTLazy<SkPath> tmpPath;
722 SkTLazy<SkPath> effectPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000723
724 if (prePathMatrix) {
725 SkPath* result = pathPtr;
726
727 if (!pathIsMutable) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000728 result = tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000729 pathIsMutable = true;
730 }
731 // should I push prePathMatrix on our MV stack temporarily, instead
732 // of applying it here? See SkDraw.cpp
733 pathPtr->transform(*prePathMatrix, result);
734 pathPtr = result;
735 }
736 // at this point we're done with prePathMatrix
737 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
738
egdanield58a0ba2014-06-11 10:30:05 -0700739 GrStrokeInfo strokeInfo(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000740 SkPathEffect* pathEffect = paint.getPathEffect();
741 const SkRect* cullRect = NULL; // TODO: what is our bounds?
egdanield58a0ba2014-06-11 10:30:05 -0700742 SkStrokeRec* strokePtr = strokeInfo.getStrokeRecPtr();
743 if (pathEffect && pathEffect->filterPath(effectPath.init(), *pathPtr, strokePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000744 cullRect)) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000745 pathPtr = effectPath.get();
746 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700747 strokeInfo.removeDash();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000748 }
749
egdanield58a0ba2014-06-11 10:30:05 -0700750 const SkStrokeRec& stroke = strokeInfo.getStrokeRec();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000751 if (paint.getMaskFilter()) {
752 if (!stroke.isHairlineStyle()) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000753 SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init();
754 if (stroke.applyToPath(strokedPath, *pathPtr)) {
755 pathPtr = strokedPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000756 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700757 strokeInfo.setFillStyle();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000758 }
759 }
760
761 // avoid possibly allocating a new path in transform if we can
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000762 SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000763
764 // transform the path into device space
765 pathPtr->transform(fContext->getMatrix(), devPathPtr);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000766
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000767 SkRect maskRect;
768 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
769 draw.fClip->getBounds(),
770 fContext->getMatrix(),
771 &maskRect)) {
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000772 // The context's matrix may change while creating the mask, so save the CTM here to
773 // pass to filterMaskGPU.
774 const SkMatrix ctm = fContext->getMatrix();
775
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000776 SkIRect finalIRect;
777 maskRect.roundOut(&finalIRect);
778 if (draw.fClip->quickReject(finalIRect)) {
779 // clipped out
780 return;
781 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000782
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000783 if (paint.getMaskFilter()->directFilterMaskGPU(fContext, &grPaint,
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000784 stroke, *devPathPtr)) {
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000785 // the mask filter was able to draw itself directly, so there's nothing
786 // left to do.
787 return;
788 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000789
790 GrAutoScratchTexture mask;
791
egdanield58a0ba2014-06-11 10:30:05 -0700792 if (create_mask_GPU(fContext, maskRect, *devPathPtr, strokeInfo,
derekff4555aa2014-10-06 12:19:12 -0700793 grPaint.isAntiAlias(), &mask, fRenderTarget->numSamples())) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000794 GrTexture* filtered;
795
commit-bot@chromium.org41bf9302014-01-08 22:25:53 +0000796 if (paint.getMaskFilter()->filterMaskGPU(mask.texture(),
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000797 ctm, maskRect, &filtered, true)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000798 // filterMaskGPU gives us ownership of a ref to the result
799 SkAutoTUnref<GrTexture> atu(filtered);
800
801 // If the scratch texture that we used as the filter src also holds the filter
802 // result then we must detach so that this texture isn't recycled for a later
803 // draw.
804 if (filtered == mask.texture()) {
805 mask.detach();
806 filtered->unref(); // detach transfers GrAutoScratchTexture's ref to us.
807 }
808
809 if (draw_mask(fContext, maskRect, &grPaint, filtered)) {
810 // This path is completely drawn
811 return;
812 }
813 }
814 }
815 }
816
817 // draw the mask on the CPU - this is a fallthrough path in case the
818 // GPU path fails
819 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
820 SkPaint::kFill_Style;
egdanield58a0ba2014-06-11 10:30:05 -0700821 draw_with_mask_filter(fContext, *devPathPtr, paint.getMaskFilter(),
822 *draw.fClip, &grPaint, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000823 return;
824 }
825
egdanield58a0ba2014-06-11 10:30:05 -0700826 fContext->drawPath(grPaint, *pathPtr, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000827}
828
829static const int kBmpSmallTileSize = 1 << 10;
830
831static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
832 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
833 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
834 return tilesX * tilesY;
835}
836
837static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
838 if (maxTileSize <= kBmpSmallTileSize) {
839 return maxTileSize;
840 }
841
842 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
843 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
844
845 maxTileTotalTileSize *= maxTileSize * maxTileSize;
846 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
847
848 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
849 return kBmpSmallTileSize;
850 } else {
851 return maxTileSize;
852 }
853}
854
855// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
856// pixels from the bitmap are necessary.
857static void determine_clipped_src_rect(const GrContext* context,
858 const SkBitmap& bitmap,
859 const SkRect* srcRectPtr,
860 SkIRect* clippedSrcIRect) {
861 const GrClipData* clip = context->getClip();
862 clip->getConservativeBounds(context->getRenderTarget(), clippedSrcIRect, NULL);
863 SkMatrix inv;
864 if (!context->getMatrix().invert(&inv)) {
865 clippedSrcIRect->setEmpty();
866 return;
867 }
868 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
869 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700870 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000871 // we've setup src space 0,0 to map to the top left of the src rect.
872 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000873 if (!clippedSrcRect.intersect(*srcRectPtr)) {
874 clippedSrcIRect->setEmpty();
875 return;
876 }
877 }
878 clippedSrcRect.roundOut(clippedSrcIRect);
879 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
880 if (!clippedSrcIRect->intersect(bmpBounds)) {
881 clippedSrcIRect->setEmpty();
882 }
883}
884
885bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
886 const GrTextureParams& params,
887 const SkRect* srcRectPtr,
888 int maxTileSize,
889 int* tileSize,
890 SkIRect* clippedSrcRect) const {
891 // if bitmap is explictly texture backed then just use the texture
bsalomon49f085d2014-09-05 13:34:00 -0700892 if (bitmap.getTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000893 return false;
894 }
895
896 // if it's larger than the max tile size, then we have no choice but tiling.
897 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
898 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
899 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
900 return true;
901 }
902
903 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
904 return false;
905 }
906
907 // if the entire texture is already in our cache then no reason to tile it
908 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
909 return false;
910 }
911
912 // At this point we know we could do the draw by uploading the entire bitmap
913 // as a texture. However, if the texture would be large compared to the
914 // cache size and we don't require most of it for this draw then tile to
915 // reduce the amount of upload and cache spill.
916
917 // assumption here is that sw bitmap size is a good proxy for its size as
918 // a texture
919 size_t bmpSize = bitmap.getSize();
920 size_t cacheSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000921 fContext->getResourceCacheLimits(NULL, &cacheSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000922 if (bmpSize < cacheSize / 2) {
923 return false;
924 }
925
926 // Figure out how much of the src we will need based on the src rect and clipping.
927 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
928 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
929 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
930 kBmpSmallTileSize * kBmpSmallTileSize;
931
932 return usedTileBytes < 2 * bmpSize;
933}
934
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000935void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000936 const SkBitmap& bitmap,
937 const SkMatrix& m,
938 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000939 SkMatrix concat;
940 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
941 if (!m.isIdentity()) {
942 concat.setConcat(*draw->fMatrix, m);
943 draw.writable()->fMatrix = &concat;
944 }
945 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000946}
947
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000948// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000949// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
950// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000951static inline void clamped_outset_with_offset(SkIRect* iRect,
952 int outset,
953 SkPoint* offset,
954 const SkIRect& clamp) {
955 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000956
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000957 int leftClampDelta = clamp.fLeft - iRect->fLeft;
958 if (leftClampDelta > 0) {
959 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000960 iRect->fLeft = clamp.fLeft;
961 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000962 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000963 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000964
965 int topClampDelta = clamp.fTop - iRect->fTop;
966 if (topClampDelta > 0) {
967 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000968 iRect->fTop = clamp.fTop;
969 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000970 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000971 }
972
973 if (iRect->fRight > clamp.fRight) {
974 iRect->fRight = clamp.fRight;
975 }
976 if (iRect->fBottom > clamp.fBottom) {
977 iRect->fBottom = clamp.fBottom;
978 }
979}
980
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000981static bool has_aligned_samples(const SkRect& srcRect,
982 const SkRect& transformedRect) {
983 // detect pixel disalignment
984 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
985 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
986 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
987 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
988 SkScalarAbs(transformedRect.width() - srcRect.width()) <
989 COLOR_BLEED_TOLERANCE &&
990 SkScalarAbs(transformedRect.height() - srcRect.height()) <
991 COLOR_BLEED_TOLERANCE) {
992 return true;
993 }
994 return false;
995}
996
997static bool may_color_bleed(const SkRect& srcRect,
998 const SkRect& transformedRect,
999 const SkMatrix& m) {
1000 // Only gets called if has_aligned_samples returned false.
1001 // So we can assume that sampling is axis aligned but not texel aligned.
1002 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
1003 SkRect innerSrcRect(srcRect), innerTransformedRect,
1004 outerTransformedRect(transformedRect);
1005 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1006 m.mapRect(&innerTransformedRect, innerSrcRect);
1007
1008 // The gap between outerTransformedRect and innerTransformedRect
1009 // represents the projection of the source border area, which is
1010 // problematic for color bleeding. We must check whether any
1011 // destination pixels sample the border area.
1012 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1013 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1014 SkIRect outer, inner;
1015 outerTransformedRect.round(&outer);
1016 innerTransformedRect.round(&inner);
1017 // If the inner and outer rects round to the same result, it means the
1018 // border does not overlap any pixel centers. Yay!
1019 return inner != outer;
1020}
1021
1022static bool needs_texture_domain(const SkBitmap& bitmap,
1023 const SkRect& srcRect,
1024 GrTextureParams &params,
1025 const SkMatrix& contextMatrix,
1026 bool bicubic) {
1027 bool needsTextureDomain = false;
1028
1029 if (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode) {
1030 // Need texture domain if drawing a sub rect
1031 needsTextureDomain = srcRect.width() < bitmap.width() ||
1032 srcRect.height() < bitmap.height();
1033 if (!bicubic && needsTextureDomain && contextMatrix.rectStaysRect()) {
1034 // sampling is axis-aligned
1035 SkRect transformedRect;
1036 contextMatrix.mapRect(&transformedRect, srcRect);
1037
1038 if (has_aligned_samples(srcRect, transformedRect)) {
1039 params.setFilterMode(GrTextureParams::kNone_FilterMode);
1040 needsTextureDomain = false;
1041 } else {
1042 needsTextureDomain = may_color_bleed(srcRect, transformedRect, contextMatrix);
1043 }
1044 }
1045 }
1046 return needsTextureDomain;
1047}
1048
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001049void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1050 const SkBitmap& bitmap,
1051 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001052 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001053 const SkPaint& paint,
1054 SkCanvas::DrawBitmapRectFlags flags) {
1055 CHECK_SHOULD_DRAW(draw, false);
1056
1057 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001058 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001059 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
1060 // in the (easier) bleed case, so update flags.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001061 if (NULL == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001062 SkScalar w = SkIntToScalar(bitmap.width());
1063 SkScalar h = SkIntToScalar(bitmap.height());
1064 dstSize.fWidth = w;
1065 dstSize.fHeight = h;
1066 srcRect.set(0, 0, w, h);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001067 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001068 } else {
bsalomon49f085d2014-09-05 13:34:00 -07001069 SkASSERT(dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001070 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001071 dstSize = *dstSizePtr;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001072 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
1073 srcRect.fRight >= bitmap.width() && srcRect.fBottom >= bitmap.height()) {
1074 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
1075 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001076 }
1077
1078 if (paint.getMaskFilter()){
1079 // Convert the bitmap to a shader so that the rect can be drawn
1080 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001081 SkBitmap tmp; // subset of bitmap, if necessary
1082 const SkBitmap* bitmapPtr = &bitmap;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001083 SkMatrix localM;
bsalomon49f085d2014-09-05 13:34:00 -07001084 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001085 localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
1086 localM.postScale(dstSize.fWidth / srcRectPtr->width(),
1087 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001088 // In bleed mode we position and trim the bitmap based on the src rect which is
1089 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
1090 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
1091 // compensate.
1092 if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) {
1093 SkIRect iSrc;
1094 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001095
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001096 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
1097 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001098
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001099 if (!bitmap.extractSubset(&tmp, iSrc)) {
1100 return; // extraction failed
1101 }
1102 bitmapPtr = &tmp;
1103 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001104
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001105 // The source rect has changed so update the matrix
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001106 localM.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001107 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001108 } else {
1109 localM.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001110 }
1111
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001112 SkPaint paintWithShader(paint);
1113 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +00001114 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &localM))->unref();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001115 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1116 this->drawRect(draw, dstRect, paintWithShader);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001117
1118 return;
1119 }
1120
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001121 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
1122 // the view matrix rather than a local matrix.
1123 SkMatrix m;
1124 m.setScale(dstSize.fWidth / srcRect.width(),
1125 dstSize.fHeight / srcRect.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001126 fContext->concatMatrix(m);
1127
1128 GrTextureParams params;
1129 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel();
1130 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001131
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001132 bool doBicubic = false;
1133
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001134 switch(paintFilterLevel) {
1135 case SkPaint::kNone_FilterLevel:
1136 textureFilterMode = GrTextureParams::kNone_FilterMode;
1137 break;
1138 case SkPaint::kLow_FilterLevel:
1139 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1140 break;
1141 case SkPaint::kMedium_FilterLevel:
commit-bot@chromium.org18786512014-05-20 14:53:45 +00001142 if (fContext->getMatrix().getMinScale() < SK_Scalar1) {
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001143 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1144 } else {
1145 // Don't trigger MIP level generation unnecessarily.
1146 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1147 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001148 break;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001149 case SkPaint::kHigh_FilterLevel:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001150 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001151 doBicubic =
1152 GrBicubicEffect::ShouldUseBicubic(fContext->getMatrix(), &textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001153 break;
1154 default:
1155 SkErrorInternals::SetError( kInvalidPaint_SkError,
1156 "Sorry, I don't understand the filtering "
1157 "mode you asked for. Falling back to "
1158 "MIPMaps.");
1159 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1160 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001161 }
1162
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001163 int tileFilterPad;
1164 if (doBicubic) {
1165 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1166 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1167 tileFilterPad = 0;
1168 } else {
1169 tileFilterPad = 1;
1170 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001171 params.setFilterMode(textureFilterMode);
1172
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001173 int maxTileSize = fContext->getMaxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001174 int tileSize;
1175
1176 SkIRect clippedSrcRect;
1177 if (this->shouldTileBitmap(bitmap, params, srcRectPtr, maxTileSize, &tileSize,
1178 &clippedSrcRect)) {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001179 this->drawTiledBitmap(bitmap, srcRect, clippedSrcRect, params, paint, flags, tileSize,
1180 doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001181 } else {
1182 // take the simple case
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001183 bool needsTextureDomain = needs_texture_domain(bitmap,
1184 srcRect,
1185 params,
1186 fContext->getMatrix(),
1187 doBicubic);
1188 this->internalDrawBitmap(bitmap,
1189 srcRect,
1190 params,
1191 paint,
1192 flags,
1193 doBicubic,
1194 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001195 }
1196}
1197
1198// Break 'bitmap' into several tiles to draw it since it has already
1199// been determined to be too large to fit in VRAM
1200void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
1201 const SkRect& srcRect,
1202 const SkIRect& clippedSrcIRect,
1203 const GrTextureParams& params,
1204 const SkPaint& paint,
1205 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001206 int tileSize,
1207 bool bicubic) {
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +00001208 // The following pixel lock is technically redundant, but it is desirable
1209 // to lock outside of the tile loop to prevent redecoding the whole image
1210 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1211 // is larger than the limit of the discardable memory pool.
1212 SkAutoLockPixels alp(bitmap);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001213 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1214
1215 int nx = bitmap.width() / tileSize;
1216 int ny = bitmap.height() / tileSize;
1217 for (int x = 0; x <= nx; x++) {
1218 for (int y = 0; y <= ny; y++) {
1219 SkRect tileR;
1220 tileR.set(SkIntToScalar(x * tileSize),
1221 SkIntToScalar(y * tileSize),
1222 SkIntToScalar((x + 1) * tileSize),
1223 SkIntToScalar((y + 1) * tileSize));
1224
1225 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1226 continue;
1227 }
1228
1229 if (!tileR.intersect(srcRect)) {
1230 continue;
1231 }
1232
1233 SkBitmap tmpB;
1234 SkIRect iTileR;
1235 tileR.roundOut(&iTileR);
1236 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1237 SkIntToScalar(iTileR.fTop));
1238
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001239 // Adjust the context matrix to draw at the right x,y in device space
1240 SkMatrix tmpM;
1241 GrContext::AutoMatrix am;
1242 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
1243 am.setPreConcat(fContext, tmpM);
1244
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001245 if (SkPaint::kNone_FilterLevel != paint.getFilterLevel() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001246 SkIRect iClampRect;
1247
1248 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1249 // In bleed mode we want to always expand the tile on all edges
1250 // but stay within the bitmap bounds
1251 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1252 } else {
1253 // In texture-domain/clamp mode we only want to expand the
1254 // tile on edges interior to "srcRect" (i.e., we want to
1255 // not bleed across the original clamped edges)
1256 srcRect.roundOut(&iClampRect);
1257 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001258 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1259 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001260 }
1261
1262 if (bitmap.extractSubset(&tmpB, iTileR)) {
1263 // now offset it to make it "local" to our tmp bitmap
1264 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001265 GrTextureParams paramsTemp = params;
1266 bool needsTextureDomain = needs_texture_domain(bitmap,
1267 srcRect,
1268 paramsTemp,
1269 fContext->getMatrix(),
1270 bicubic);
1271 this->internalDrawBitmap(tmpB,
1272 tileR,
1273 paramsTemp,
1274 paint,
1275 flags,
1276 bicubic,
1277 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001278 }
1279 }
1280 }
1281}
1282
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001283
1284/*
1285 * This is called by drawBitmap(), which has to handle images that may be too
1286 * large to be represented by a single texture.
1287 *
1288 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1289 * and that non-texture portion of the GrPaint has already been setup.
1290 */
1291void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
1292 const SkRect& srcRect,
1293 const GrTextureParams& params,
1294 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001295 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001296 bool bicubic,
1297 bool needsTextureDomain) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001298 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1299 bitmap.height() <= fContext->getMaxTextureSize());
1300
1301 GrTexture* texture;
Brian Salomon9323b8b2014-10-07 15:07:38 -04001302 SkAutoCachedTexture act(this, bitmap, &params, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001303 if (NULL == texture) {
1304 return;
1305 }
1306
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001307 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001308 SkRect paintRect;
1309 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1310 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1311 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1312 SkScalarMul(srcRect.fTop, hInv),
1313 SkScalarMul(srcRect.fRight, wInv),
1314 SkScalarMul(srcRect.fBottom, hInv));
1315
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001316 SkRect textureDomain = SkRect::MakeEmpty();
joshualittb0a8a372014-09-23 09:50:21 -07001317 SkAutoTUnref<GrFragmentProcessor> fp;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001318 if (needsTextureDomain && !(flags & SkCanvas::kBleed_DrawBitmapRectFlag)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001319 // Use a constrained texture domain to avoid color bleeding
1320 SkScalar left, top, right, bottom;
1321 if (srcRect.width() > SK_Scalar1) {
1322 SkScalar border = SK_ScalarHalf / texture->width();
1323 left = paintRect.left() + border;
1324 right = paintRect.right() - border;
1325 } else {
1326 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1327 }
1328 if (srcRect.height() > SK_Scalar1) {
1329 SkScalar border = SK_ScalarHalf / texture->height();
1330 top = paintRect.top() + border;
1331 bottom = paintRect.bottom() - border;
1332 } else {
1333 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1334 }
1335 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001336 if (bicubic) {
joshualittb0a8a372014-09-23 09:50:21 -07001337 fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001338 } else {
joshualittb0a8a372014-09-23 09:50:21 -07001339 fp.reset(GrTextureDomainEffect::Create(texture,
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001340 SkMatrix::I(),
1341 textureDomain,
1342 GrTextureDomain::kClamp_Mode,
1343 params.filterMode()));
1344 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001345 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001346 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1347 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
joshualittb0a8a372014-09-23 09:50:21 -07001348 fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001349 } else {
joshualittb0a8a372014-09-23 09:50:21 -07001350 fp.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001351 }
1352
1353 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1354 // the rest from the SkPaint.
1355 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001356 grPaint.addColorProcessor(fp);
reed0689d7b2014-06-14 05:30:20 -07001357 bool alphaOnly = !(kAlpha_8_SkColorType == bitmap.colorType());
bsalomon83d081a2014-07-08 09:56:10 -07001358 GrColor paintColor = (alphaOnly) ? SkColor2GrColorJustAlpha(paint.getColor()) :
1359 SkColor2GrColor(paint.getColor());
1360 SkPaint2GrPaintNoShader(this->context(), paint, paintColor, false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001361
bsalomon01c8da12014-08-04 09:21:30 -07001362 fContext->drawRectToRect(grPaint, dstRect, paintRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001363}
1364
1365static bool filter_texture(SkBaseDevice* device, GrContext* context,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001366 GrTexture* texture, const SkImageFilter* filter,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001367 int w, int h, const SkImageFilter::Context& ctx,
1368 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001369 SkASSERT(filter);
1370 SkDeviceImageFilterProxy proxy(device);
1371
1372 if (filter->canFilterImageGPU()) {
1373 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1374 // filter. Also set the clip wide open and the matrix to identity.
1375 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001376 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001377 } else {
1378 return false;
1379 }
1380}
1381
1382void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1383 int left, int top, const SkPaint& paint) {
1384 // drawSprite is defined to be in device coords.
1385 CHECK_SHOULD_DRAW(draw, true);
1386
1387 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1388 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1389 return;
1390 }
1391
1392 int w = bitmap.width();
1393 int h = bitmap.height();
1394
1395 GrTexture* texture;
1396 // draw sprite uses the default texture params
Brian Salomon9323b8b2014-10-07 15:07:38 -04001397 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001398
1399 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001400 // This bitmap will own the filtered result as a texture.
1401 SkBitmap filteredBitmap;
1402
bsalomon49f085d2014-09-05 13:34:00 -07001403 if (filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001404 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001405 SkMatrix matrix(*draw.fMatrix);
1406 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001407 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
senorblancobe129b22014-08-08 07:14:35 -07001408 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
senorblanco55b6d8b2014-07-30 11:26:46 -07001409 // This cache is transient, and is freed (along with all its contained
1410 // textures) when it goes out of scope.
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001411 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001412 if (filter_texture(this, fContext, texture, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001413 &offset)) {
1414 texture = (GrTexture*) filteredBitmap.getTexture();
1415 w = filteredBitmap.width();
1416 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001417 left += offset.x();
1418 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001419 } else {
1420 return;
1421 }
1422 }
1423
1424 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001425 grPaint.addColorTextureProcessor(texture, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001426
dandov9de5b512014-06-10 14:38:28 -07001427 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColorJustAlpha(paint.getColor()),
1428 false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001429
1430 fContext->drawRectToRect(grPaint,
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001431 SkRect::MakeXYWH(SkIntToScalar(left),
1432 SkIntToScalar(top),
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001433 SkIntToScalar(w),
1434 SkIntToScalar(h)),
1435 SkRect::MakeXYWH(0,
1436 0,
1437 SK_Scalar1 * w / texture->width(),
1438 SK_Scalar1 * h / texture->height()));
1439}
1440
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001441void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001442 const SkRect* src, const SkRect& dst,
1443 const SkPaint& paint,
1444 SkCanvas::DrawBitmapRectFlags flags) {
1445 SkMatrix matrix;
1446 SkRect bitmapBounds, tmpSrc;
1447
1448 bitmapBounds.set(0, 0,
1449 SkIntToScalar(bitmap.width()),
1450 SkIntToScalar(bitmap.height()));
1451
1452 // Compute matrix from the two rectangles
bsalomon49f085d2014-09-05 13:34:00 -07001453 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001454 tmpSrc = *src;
1455 } else {
1456 tmpSrc = bitmapBounds;
1457 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001458
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001459 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1460
1461 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
bsalomon49f085d2014-09-05 13:34:00 -07001462 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001463 if (!bitmapBounds.contains(tmpSrc)) {
1464 if (!tmpSrc.intersect(bitmapBounds)) {
1465 return; // nothing to draw
1466 }
1467 }
1468 }
1469
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001470 SkRect tmpDst;
1471 matrix.mapRect(&tmpDst, tmpSrc);
1472
1473 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1474 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1475 // Translate so that tempDst's top left is at the origin.
1476 matrix = *origDraw.fMatrix;
1477 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1478 draw.writable()->fMatrix = &matrix;
1479 }
1480 SkSize dstSize;
1481 dstSize.fWidth = tmpDst.width();
1482 dstSize.fHeight = tmpDst.height();
1483
1484 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001485}
1486
1487void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1488 int x, int y, const SkPaint& paint) {
1489 // clear of the source device must occur before CHECK_SHOULD_DRAW
egdanield78a1682014-07-09 10:41:26 -07001490 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawDevice", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001491 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1492 if (dev->fNeedClear) {
1493 // TODO: could check here whether we really need to draw at all
1494 dev->clear(0x0);
1495 }
1496
1497 // drawDevice is defined to be in device coords.
1498 CHECK_SHOULD_DRAW(draw, true);
1499
1500 GrRenderTarget* devRT = dev->accessRenderTarget();
1501 GrTexture* devTex;
1502 if (NULL == (devTex = devRT->asTexture())) {
1503 return;
1504 }
1505
1506 const SkBitmap& bm = dev->accessBitmap(false);
1507 int w = bm.width();
1508 int h = bm.height();
1509
1510 SkImageFilter* filter = paint.getImageFilter();
1511 // This bitmap will own the filtered result as a texture.
1512 SkBitmap filteredBitmap;
1513
bsalomon49f085d2014-09-05 13:34:00 -07001514 if (filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001515 SkIPoint offset = SkIPoint::Make(0, 0);
1516 SkMatrix matrix(*draw.fMatrix);
1517 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001518 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
senorblanco55b6d8b2014-07-30 11:26:46 -07001519 // This cache is transient, and is freed (along with all its contained
1520 // textures) when it goes out of scope.
senorblancobe129b22014-08-08 07:14:35 -07001521 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001522 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001523 if (filter_texture(this, fContext, devTex, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001524 &offset)) {
1525 devTex = filteredBitmap.getTexture();
1526 w = filteredBitmap.width();
1527 h = filteredBitmap.height();
1528 x += offset.fX;
1529 y += offset.fY;
1530 } else {
1531 return;
1532 }
1533 }
1534
1535 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001536 grPaint.addColorTextureProcessor(devTex, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001537
dandov9de5b512014-06-10 14:38:28 -07001538 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColorJustAlpha(paint.getColor()),
1539 false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001540
1541 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1542 SkIntToScalar(y),
1543 SkIntToScalar(w),
1544 SkIntToScalar(h));
1545
1546 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1547 // scratch texture).
1548 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1549 SK_Scalar1 * h / devTex->height());
1550
1551 fContext->drawRectToRect(grPaint, dstRect, srcRect);
1552}
1553
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001554bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001555 return filter->canFilterImageGPU();
1556}
1557
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001558bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001559 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001560 SkBitmap* result, SkIPoint* offset) {
1561 // want explicitly our impl, so guard against a subclass of us overriding it
1562 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1563 return false;
1564 }
1565
1566 SkAutoLockPixels alp(src, !src.getTexture());
1567 if (!src.getTexture() && !src.readyToDraw()) {
1568 return false;
1569 }
1570
1571 GrTexture* texture;
1572 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1573 // must be pushed upstack.
Brian Salomon9323b8b2014-10-07 15:07:38 -04001574 SkAutoCachedTexture act(this, src, NULL, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001575
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001576 return filter_texture(this, fContext, texture, filter, src.width(), src.height(), ctx,
1577 result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001578}
1579
1580///////////////////////////////////////////////////////////////////////////////
1581
1582// must be in SkCanvas::VertexMode order
1583static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1584 kTriangles_GrPrimitiveType,
1585 kTriangleStrip_GrPrimitiveType,
1586 kTriangleFan_GrPrimitiveType,
1587};
1588
1589void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1590 int vertexCount, const SkPoint vertices[],
1591 const SkPoint texs[], const SkColor colors[],
1592 SkXfermode* xmode,
1593 const uint16_t indices[], int indexCount,
1594 const SkPaint& paint) {
1595 CHECK_SHOULD_DRAW(draw, false);
1596
egdanield78a1682014-07-09 10:41:26 -07001597 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawVertices", fContext);
mtklein533eb782014-08-27 10:39:42 -07001598
dandov32a311b2014-07-15 19:46:26 -07001599 const uint16_t* outIndices;
1600 SkAutoTDeleteArray<uint16_t> outAlloc(NULL);
1601 GrPrimitiveType primType;
1602 GrPaint grPaint;
bsalomona098dd42014-08-06 11:01:44 -07001603
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001604 // If both textures and vertex-colors are NULL, strokes hairlines with the paint's color.
1605 if ((NULL == texs || NULL == paint.getShader()) && NULL == colors) {
mtklein533eb782014-08-27 10:39:42 -07001606
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001607 texs = NULL;
mtklein533eb782014-08-27 10:39:42 -07001608
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001609 SkPaint copy(paint);
1610 copy.setStyle(SkPaint::kStroke_Style);
1611 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001612
dandov32a311b2014-07-15 19:46:26 -07001613 // we ignore the shader if texs is null.
1614 SkPaint2GrPaintNoShader(this->context(), copy, SkColor2GrColor(copy.getColor()),
1615 NULL == colors, &grPaint);
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001616
dandov32a311b2014-07-15 19:46:26 -07001617 primType = kLines_GrPrimitiveType;
1618 int triangleCount = 0;
bsalomona098dd42014-08-06 11:01:44 -07001619 int n = (NULL == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001620 switch (vmode) {
1621 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001622 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001623 break;
1624 case SkCanvas::kTriangleStrip_VertexMode:
1625 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001626 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001627 break;
1628 }
mtklein533eb782014-08-27 10:39:42 -07001629
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001630 VertState state(vertexCount, indices, indexCount);
1631 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001632
dandov32a311b2014-07-15 19:46:26 -07001633 //number of indices for lines per triangle with kLines
1634 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001635
dandov32a311b2014-07-15 19:46:26 -07001636 outAlloc.reset(SkNEW_ARRAY(uint16_t, indexCount));
1637 outIndices = outAlloc.get();
1638 uint16_t* auxIndices = outAlloc.get();
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001639 int i = 0;
1640 while (vertProc(&state)) {
dandov32a311b2014-07-15 19:46:26 -07001641 auxIndices[i] = state.f0;
1642 auxIndices[i + 1] = state.f1;
1643 auxIndices[i + 2] = state.f1;
1644 auxIndices[i + 3] = state.f2;
1645 auxIndices[i + 4] = state.f2;
1646 auxIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001647 i += 6;
1648 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001649 } else {
dandov32a311b2014-07-15 19:46:26 -07001650 outIndices = indices;
1651 primType = gVertexMode2PrimitiveType[vmode];
mtklein533eb782014-08-27 10:39:42 -07001652
dandov32a311b2014-07-15 19:46:26 -07001653 if (NULL == texs || NULL == paint.getShader()) {
1654 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColor(paint.getColor()),
1655 NULL == colors, &grPaint);
1656 } else {
1657 SkPaint2GrPaintShader(this->context(), paint, NULL == colors, &grPaint);
1658 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001659 }
1660
mtklein2583b622014-06-04 08:20:41 -07001661#if 0
bsalomon49f085d2014-09-05 13:34:00 -07001662 if (xmode && texs && colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001663 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1664 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
mtklein2583b622014-06-04 08:20:41 -07001665 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001666 }
1667 }
mtklein2583b622014-06-04 08:20:41 -07001668#endif
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001669
1670 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001671 if (colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001672 // need to convert byte order and from non-PM to PM
1673 convertedColors.reset(vertexCount);
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001674 SkColor color;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001675 for (int i = 0; i < vertexCount; ++i) {
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001676 color = colors[i];
1677 if (paint.getAlpha() != 255) {
1678 color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paint.getAlpha()));
1679 }
1680 convertedColors[i] = SkColor2GrColor(color);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001681 }
1682 colors = convertedColors.get();
1683 }
1684 fContext->drawVertices(grPaint,
dandov32a311b2014-07-15 19:46:26 -07001685 primType,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001686 vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001687 vertices,
1688 texs,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001689 colors,
dandov32a311b2014-07-15 19:46:26 -07001690 outIndices,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001691 indexCount);
1692}
1693
1694///////////////////////////////////////////////////////////////////////////////
1695
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001696void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1697 size_t byteLength, SkScalar x, SkScalar y,
1698 const SkPaint& paint) {
1699 CHECK_SHOULD_DRAW(draw, false);
egdanield78a1682014-07-09 10:41:26 -07001700 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001701
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001702 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001703 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001704 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001705
1706 SkDEBUGCODE(this->validate();)
1707
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001708 fMainTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
1709 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001710 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001711 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001712
1713 SkDEBUGCODE(this->validate();)
1714
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001715 fFallbackTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001716 } else {
1717 // this guy will just call our drawPath()
1718 draw.drawText_asPaths((const char*)text, byteLength, x, y, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001719 }
1720}
1721
fmalita05c4a432014-09-29 06:29:53 -07001722void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1723 const SkScalar pos[], int scalarsPerPos,
1724 const SkPoint& offset, const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -07001725 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPosText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001726 CHECK_SHOULD_DRAW(draw, false);
1727
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001728 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001729 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001730 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001731
1732 SkDEBUGCODE(this->validate();)
1733
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001734 fMainTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
fmalita05c4a432014-09-29 06:29:53 -07001735 scalarsPerPos, offset);
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001736 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001737 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001738 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001739
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001740 SkDEBUGCODE(this->validate();)
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001741
1742 fFallbackTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
fmalita05c4a432014-09-29 06:29:53 -07001743 scalarsPerPos, offset);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001744 } else {
fmalita05c4a432014-09-29 06:29:53 -07001745 draw.drawPosText_asPaths((const char*)text, byteLength, pos, scalarsPerPos, offset, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001746 }
1747}
1748
1749void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1750 size_t len, const SkPath& path,
1751 const SkMatrix* m, const SkPaint& paint) {
1752 CHECK_SHOULD_DRAW(draw, false);
1753
1754 SkASSERT(draw.fDevice == this);
1755 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1756}
1757
1758///////////////////////////////////////////////////////////////////////////////
1759
1760bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1761 if (!paint.isLCDRenderText()) {
1762 // we're cool with the paint as is
1763 return false;
1764 }
1765
1766 if (paint.getShader() ||
1767 paint.getXfermode() || // unless its srcover
1768 paint.getMaskFilter() ||
1769 paint.getRasterizer() ||
1770 paint.getColorFilter() ||
1771 paint.getPathEffect() ||
1772 paint.isFakeBoldText() ||
1773 paint.getStyle() != SkPaint::kFill_Style) {
reed3375c802014-09-16 12:27:55 -07001774 // turn off lcd, but turn on kGenA8
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001775 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
reed3375c802014-09-16 12:27:55 -07001776 flags->fFlags |= SkPaint::kGenA8FromLCD_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001777 return true;
1778 }
1779 // we're cool with the paint as is
1780 return false;
1781}
1782
1783void SkGpuDevice::flush() {
1784 DO_DEFERRED_CLEAR();
1785 fContext->resolveRenderTarget(fRenderTarget);
1786}
1787
1788///////////////////////////////////////////////////////////////////////////////
1789
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001790SkBaseDevice* SkGpuDevice::onCreateDevice(const SkImageInfo& info, Usage usage) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001791 GrTextureDesc desc;
1792 desc.fConfig = fRenderTarget->config();
1793 desc.fFlags = kRenderTarget_GrTextureFlagBit;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001794 desc.fWidth = info.width();
1795 desc.fHeight = info.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001796 desc.fSampleCnt = fRenderTarget->numSamples();
1797
1798 SkAutoTUnref<GrTexture> texture;
1799 // Skia's convention is to only clear a device if it is non-opaque.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001800 unsigned flags = info.isOpaque() ? 0 : kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001801
1802#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1803 // layers are never draw in repeat modes, so we can request an approx
1804 // match and ignore any padding.
Brian Salomon9323b8b2014-10-07 15:07:38 -04001805 flags |= kCached_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001806 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == usage) ?
1807 GrContext::kApprox_ScratchTexMatch :
1808 GrContext::kExact_ScratchTexMatch;
1809 texture.reset(fContext->lockAndRefScratchTexture(desc, match));
1810#else
1811 texture.reset(fContext->createUncachedTexture(desc, NULL, 0));
1812#endif
bsalomon49f085d2014-09-05 13:34:00 -07001813 if (texture.get()) {
reed4a8126e2014-09-22 07:29:03 -07001814 return SkGpuDevice::Create(texture, SkSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType), flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001815 } else {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001816 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1817 info.width(), info.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001818 return NULL;
1819 }
1820}
1821
reed4a8126e2014-09-22 07:29:03 -07001822SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
1823 return SkSurface::NewRenderTarget(fContext, info, fRenderTarget->numSamples(), &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001824}
1825
robertphillips9b14f262014-06-04 05:40:44 -07001826void SkGpuDevice::EXPERIMENTAL_optimize(const SkPicture* picture) {
robertphillipsd771f6b2014-07-22 10:18:06 -07001827 fContext->getLayerCache()->processDeletedPictures();
1828
bsalomon49f085d2014-09-05 13:34:00 -07001829 if (picture->fData.get() && !picture->fData->suitableForLayerOptimization()) {
robertphillipsc019ec42014-08-12 05:35:58 -07001830 return;
1831 }
1832
robertphillips6617d502014-08-18 09:39:34 -07001833 SkPicture::AccelData::Key key = GrAccelData::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);
bsalomon49f085d2014-09-05 13:34:00 -07001836 if (existing) {
commit-bot@chromium.org8ec8bab2014-05-14 13:11:48 +00001837 return;
1838 }
1839
robertphillipsd6283302014-08-27 11:53:28 -07001840 GPUOptimize(picture);
robertphillipsd771f6b2014-07-22 10:18:06 -07001841
1842 fContext->getLayerCache()->trackPicture(picture);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001843}
1844
robertphillips30d2cc62014-09-24 08:52:18 -07001845bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* mainCanvas, const SkPicture* mainPicture,
reedd5fa1a42014-08-09 11:08:05 -07001846 const SkMatrix* matrix, const SkPaint* paint) {
1847 // todo: should handle these natively
1848 if (matrix || paint) {
1849 return false;
1850 }
1851
robertphillipsd771f6b2014-07-22 10:18:06 -07001852 fContext->getLayerCache()->processDeletedPictures();
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001853
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001854 SkRect clipBounds;
robertphillips0c423322014-07-31 11:02:38 -07001855 if (!mainCanvas->getClipBounds(&clipBounds)) {
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001856 return true;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001857 }
1858
robertphillipsb5a97152014-09-30 11:33:02 -07001859 SkTDArray<GrLayerHoister::HoistedLayer> atlased, nonAtlased, recycled;
robertphillips1c4c5282014-09-18 12:03:15 -07001860
robertphillips30d2cc62014-09-24 08:52:18 -07001861 if (!GrLayerHoister::FindLayersToHoist(mainPicture, clipBounds, &atlased, &nonAtlased,
robertphillipsb5a97152014-09-30 11:33:02 -07001862 &recycled, fContext->getLayerCache())) {
robertphillips64bf7672014-08-21 13:07:35 -07001863 return false;
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001864 }
1865
robertphillips4aa6dfc2014-09-17 07:50:47 -07001866 GrReplacements replacements;
1867
robertphillipsb5a97152014-09-30 11:33:02 -07001868 GrLayerHoister::DrawLayers(atlased, nonAtlased, recycled, &replacements);
robertphillips4aa6dfc2014-09-17 07:50:47 -07001869
robertphillips64bf7672014-08-21 13:07:35 -07001870 // Render the entire picture using new layers
robertphillipsee6631e2014-09-29 05:32:49 -07001871 const SkMatrix initialMatrix = mainCanvas->getTotalMatrix();
1872
1873 GrRecordReplaceDraw(mainPicture, mainCanvas, &replacements, initialMatrix, NULL);
robertphillips64bf7672014-08-21 13:07:35 -07001874
robertphillipsb5a97152014-09-30 11:33:02 -07001875 GrLayerHoister::UnlockLayers(fContext->getLayerCache(), atlased, nonAtlased, recycled);
robertphillips64bf7672014-08-21 13:07:35 -07001876
1877 return true;
1878}
1879
senorblancobe129b22014-08-08 07:14:35 -07001880SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() {
senorblanco55b6d8b2014-07-30 11:26:46 -07001881 // We always return a transient cache, so it is freed after each
1882 // filter traversal.
senorblancobe129b22014-08-08 07:14:35 -07001883 return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001884}