skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 10 | #include "GrBlurUtils.h" |
kkinnunen | abcfab4 | 2015-02-22 22:53:44 -0800 | [diff] [blame] | 11 | #include "GrContext.h" |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 12 | #include "GrDrawContext.h" |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 13 | #include "GrFontScaler.h" |
kkinnunen | abcfab4 | 2015-02-22 22:53:44 -0800 | [diff] [blame] | 14 | #include "GrGpu.h" |
| 15 | #include "GrGpuResourcePriv.h" |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 16 | #include "GrLayerHoister.h" |
robertphillips | 274b4ba | 2014-09-04 07:24:18 -0700 | [diff] [blame] | 17 | #include "GrRecordReplaceDraw.h" |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 18 | #include "GrStrokeInfo.h" |
joshualitt | 8f94bb2 | 2015-04-28 07:04:11 -0700 | [diff] [blame] | 19 | #include "GrTextContext.h" |
egdaniel | bbcb38d | 2014-06-19 10:19:29 -0700 | [diff] [blame] | 20 | #include "GrTracing.h" |
robertphillips | 30d7841 | 2014-11-24 09:49:17 -0800 | [diff] [blame] | 21 | #include "SkCanvasPriv.h" |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 22 | #include "SkDrawProcs.h" |
kkinnunen | abcfab4 | 2015-02-22 22:53:44 -0800 | [diff] [blame] | 23 | #include "SkErrorInternals.h" |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 24 | #include "SkGlyphCache.h" |
kkinnunen | abcfab4 | 2015-02-22 22:53:44 -0800 | [diff] [blame] | 25 | #include "SkGrTexturePixelRef.h" |
reed | a85d4d0 | 2015-05-06 12:56:48 -0700 | [diff] [blame] | 26 | #include "SkImage_Base.h" |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 27 | #include "SkImageFilter.h" |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 28 | #include "SkLayerInfo.h" |
commit-bot@chromium.org | 8213970 | 2014-03-10 22:53:20 +0000 | [diff] [blame] | 29 | #include "SkMaskFilter.h" |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 30 | #include "SkPathEffect.h" |
commit-bot@chromium.org | 145d1c0 | 2014-03-16 19:46:36 +0000 | [diff] [blame] | 31 | #include "SkPicture.h" |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 32 | #include "SkPictureData.h" |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 33 | #include "SkRRect.h" |
kkinnunen | abcfab4 | 2015-02-22 22:53:44 -0800 | [diff] [blame] | 34 | #include "SkRecord.h" |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 35 | #include "SkStroke.h" |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 36 | #include "SkSurface.h" |
kkinnunen | abcfab4 | 2015-02-22 22:53:44 -0800 | [diff] [blame] | 37 | #include "SkSurface_Gpu.h" |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 38 | #include "SkTLazy.h" |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 39 | #include "SkUtils.h" |
commit-bot@chromium.org | 559a883 | 2014-05-30 10:08:22 +0000 | [diff] [blame] | 40 | #include "SkVertState.h" |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 41 | #include "SkXfermode.h" |
kkinnunen | abcfab4 | 2015-02-22 22:53:44 -0800 | [diff] [blame] | 42 | #include "effects/GrBicubicEffect.h" |
| 43 | #include "effects/GrDashingEffect.h" |
| 44 | #include "effects/GrSimpleTextureEffect.h" |
| 45 | #include "effects/GrTextureDomain.h" |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 46 | |
reed | f037e0b | 2014-10-30 11:34:15 -0700 | [diff] [blame] | 47 | #if SK_SUPPORT_GPU |
| 48 | |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 49 | enum { kDefaultImageFilterCacheSize = 32 * 1024 * 1024 }; |
| 50 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 51 | #if 0 |
| 52 | extern bool (*gShouldDrawProc)(); |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 53 | #define CHECK_SHOULD_DRAW(draw) \ |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 54 | do { \ |
| 55 | if (gShouldDrawProc && !gShouldDrawProc()) return; \ |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 56 | this->prepareDraw(draw); \ |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 57 | } while (0) |
| 58 | #else |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 59 | #define CHECK_SHOULD_DRAW(draw) this->prepareDraw(draw) |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 60 | #endif |
| 61 | |
| 62 | // This constant represents the screen alignment criterion in texels for |
| 63 | // requiring texture domain clamping to prevent color bleeding when drawing |
| 64 | // a sub region of a larger source image. |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 65 | #define COLOR_BLEED_TOLERANCE 0.001f |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 66 | |
| 67 | #define DO_DEFERRED_CLEAR() \ |
| 68 | do { \ |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 69 | if (fNeedClear) { \ |
| 70 | this->clearAll(); \ |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 71 | } \ |
| 72 | } while (false) \ |
| 73 | |
| 74 | /////////////////////////////////////////////////////////////////////////////// |
| 75 | |
| 76 | #define CHECK_FOR_ANNOTATION(paint) \ |
| 77 | do { if (paint.getAnnotation()) { return; } } while (0) |
| 78 | |
| 79 | /////////////////////////////////////////////////////////////////////////////// |
| 80 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 81 | // Helper for turning a bitmap into a texture. If the bitmap is GrTexture backed this |
| 82 | // just accesses the backing GrTexture. Otherwise, it creates a cached texture |
| 83 | // representation and releases it in the destructor. |
| 84 | class AutoBitmapTexture : public SkNoncopyable { |
Brian Salomon | 9323b8b | 2014-10-07 15:07:38 -0400 | [diff] [blame] | 85 | public: |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 86 | AutoBitmapTexture() {} |
robertphillips | dbe6074 | 2014-09-30 06:54:17 -0700 | [diff] [blame] | 87 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 88 | AutoBitmapTexture(GrContext* context, |
| 89 | const SkBitmap& bitmap, |
| 90 | const GrTextureParams* params, |
| 91 | GrTexture** texture) { |
Brian Salomon | 9323b8b | 2014-10-07 15:07:38 -0400 | [diff] [blame] | 92 | SkASSERT(texture); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 93 | *texture = this->set(context, bitmap, params); |
Brian Salomon | 9323b8b | 2014-10-07 15:07:38 -0400 | [diff] [blame] | 94 | } |
| 95 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 96 | GrTexture* set(GrContext* context, |
Brian Salomon | 9323b8b | 2014-10-07 15:07:38 -0400 | [diff] [blame] | 97 | const SkBitmap& bitmap, |
| 98 | const GrTextureParams* params) { |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 99 | // Either get the texture directly from the bitmap, or else use the cache and |
| 100 | // remember to unref it. |
| 101 | if (GrTexture* bmpTexture = bitmap.getTexture()) { |
| 102 | fTexture.reset(NULL); |
| 103 | return bmpTexture; |
| 104 | } else { |
| 105 | fTexture.reset(GrRefCachedBitmapTexture(context, bitmap, params)); |
| 106 | return fTexture.get(); |
Brian Salomon | 9323b8b | 2014-10-07 15:07:38 -0400 | [diff] [blame] | 107 | } |
Brian Salomon | 9323b8b | 2014-10-07 15:07:38 -0400 | [diff] [blame] | 108 | } |
| 109 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 110 | private: |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 111 | SkAutoTUnref<GrTexture> fTexture; |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | /////////////////////////////////////////////////////////////////////////////// |
| 115 | |
| 116 | struct GrSkDrawProcs : public SkDrawProcs { |
| 117 | public: |
| 118 | GrContext* fContext; |
| 119 | GrTextContext* fTextContext; |
| 120 | GrFontScaler* fFontScaler; // cached in the skia glyphcache |
| 121 | }; |
| 122 | |
| 123 | /////////////////////////////////////////////////////////////////////////////// |
| 124 | |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 125 | SkGpuDevice* SkGpuDevice::Create(GrRenderTarget* rt, const SkSurfaceProps* props, unsigned flags) { |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 126 | return SkGpuDevice::Create(rt, rt->width(), rt->height(), props, flags); |
| 127 | } |
| 128 | |
| 129 | SkGpuDevice* SkGpuDevice::Create(GrRenderTarget* rt, int width, int height, |
| 130 | const SkSurfaceProps* props, unsigned flags) { |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 131 | if (!rt || rt->wasDestroyed()) { |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 132 | return NULL; |
| 133 | } |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 134 | return SkNEW_ARGS(SkGpuDevice, (rt, width, height, props, flags)); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 135 | } |
| 136 | |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 137 | SkGpuDevice::SkGpuDevice(GrRenderTarget* rt, int width, int height, |
| 138 | const SkSurfaceProps* props, unsigned flags) |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 139 | : INHERITED(SkSurfacePropsCopyOrDefault(props)) |
reed | b2db898 | 2014-11-13 12:41:02 -0800 | [diff] [blame] | 140 | { |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 141 | fDrawProcs = NULL; |
| 142 | |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 143 | fContext = SkRef(rt->getContext()); |
| 144 | fNeedClear = flags & kNeedClear_Flag; |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 145 | |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 146 | fRenderTarget = SkRef(rt); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 147 | |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 148 | SkImageInfo info = rt->surfacePriv().info().makeWH(width, height); |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 149 | SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (info, rt)); |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 150 | fLegacyBitmap.setInfo(info); |
reed | 89443ab | 2014-06-27 11:34:19 -0700 | [diff] [blame] | 151 | fLegacyBitmap.setPixelRef(pr)->unref(); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 152 | |
robertphillips | 7b05ff1 | 2015-06-19 14:14:54 -0700 | [diff] [blame] | 153 | fDrawContext.reset(SkRef(fContext->drawContext(&this->surfaceProps()))); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 154 | } |
| 155 | |
kkinnunen | abcfab4 | 2015-02-22 22:53:44 -0800 | [diff] [blame] | 156 | GrRenderTarget* SkGpuDevice::CreateRenderTarget(GrContext* context, SkSurface::Budgeted budgeted, |
| 157 | const SkImageInfo& origInfo, int sampleCount) { |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 158 | if (kUnknown_SkColorType == origInfo.colorType() || |
| 159 | origInfo.width() < 0 || origInfo.height() < 0) { |
| 160 | return NULL; |
| 161 | } |
| 162 | |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 163 | if (!context) { |
| 164 | return NULL; |
| 165 | } |
| 166 | |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 167 | SkColorType ct = origInfo.colorType(); |
| 168 | SkAlphaType at = origInfo.alphaType(); |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 169 | if (kRGB_565_SkColorType == ct) { |
| 170 | at = kOpaque_SkAlphaType; // force this setting |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 171 | } else if (ct != kBGRA_8888_SkColorType && ct != kRGBA_8888_SkColorType) { |
| 172 | // Fall back from whatever ct was to default of kRGBA or kBGRA which is aliased as kN32 |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 173 | ct = kN32_SkColorType; |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 174 | } |
| 175 | if (kOpaque_SkAlphaType != at) { |
| 176 | at = kPremul_SkAlphaType; // force this setting |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 177 | } |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 178 | const SkImageInfo info = SkImageInfo::Make(origInfo.width(), origInfo.height(), ct, at); |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 179 | |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 180 | GrSurfaceDesc desc; |
| 181 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 182 | desc.fWidth = info.width(); |
| 183 | desc.fHeight = info.height(); |
commit-bot@chromium.org | 3adcc34 | 2014-04-23 19:18:09 +0000 | [diff] [blame] | 184 | desc.fConfig = SkImageInfo2GrPixelConfig(info); |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 185 | desc.fSampleCnt = sampleCount; |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 186 | GrTexture* texture = context->textureProvider()->createTexture( |
| 187 | desc, SkToBool(budgeted), NULL, 0); |
kkinnunen | abcfab4 | 2015-02-22 22:53:44 -0800 | [diff] [blame] | 188 | if (NULL == texture) { |
| 189 | return NULL; |
| 190 | } |
| 191 | SkASSERT(NULL != texture->asRenderTarget()); |
| 192 | return texture->asRenderTarget(); |
| 193 | } |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 194 | |
kkinnunen | abcfab4 | 2015-02-22 22:53:44 -0800 | [diff] [blame] | 195 | SkGpuDevice* SkGpuDevice::Create(GrContext* context, SkSurface::Budgeted budgeted, |
| 196 | const SkImageInfo& info, int sampleCount, |
| 197 | const SkSurfaceProps* props, unsigned flags) { |
| 198 | |
| 199 | SkAutoTUnref<GrRenderTarget> rt(CreateRenderTarget(context, budgeted, info, sampleCount)); |
| 200 | if (NULL == rt) { |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 201 | return NULL; |
| 202 | } |
skia.committer@gmail.com | 969588f | 2014-02-16 03:01:56 +0000 | [diff] [blame] | 203 | |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 204 | return SkNEW_ARGS(SkGpuDevice, (rt, info.width(), info.height(), props, flags)); |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 205 | } |
| 206 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 207 | SkGpuDevice::~SkGpuDevice() { |
| 208 | if (fDrawProcs) { |
| 209 | delete fDrawProcs; |
| 210 | } |
skia.committer@gmail.com | d2ac07b | 2014-01-25 07:01:49 +0000 | [diff] [blame] | 211 | |
bsalomon | 32d0b3b | 2014-08-29 07:50:23 -0700 | [diff] [blame] | 212 | fRenderTarget->unref(); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 213 | fContext->unref(); |
| 214 | } |
| 215 | |
| 216 | /////////////////////////////////////////////////////////////////////////////// |
| 217 | |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 218 | bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, |
| 219 | int x, int y) { |
| 220 | DO_DEFERRED_CLEAR(); |
| 221 | |
| 222 | // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels |
commit-bot@chromium.org | 3adcc34 | 2014-04-23 19:18:09 +0000 | [diff] [blame] | 223 | GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo); |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 224 | if (kUnknown_GrPixelConfig == config) { |
| 225 | return false; |
| 226 | } |
| 227 | |
| 228 | uint32_t flags = 0; |
| 229 | if (kUnpremul_SkAlphaType == dstInfo.alphaType()) { |
| 230 | flags = GrContext::kUnpremul_PixelOpsFlag; |
| 231 | } |
| 232 | return fContext->readRenderTargetPixels(fRenderTarget, x, y, dstInfo.width(), dstInfo.height(), |
| 233 | config, dstPixels, dstRowBytes, flags); |
| 234 | } |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 235 | |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 236 | bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes, |
| 237 | int x, int y) { |
| 238 | // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels |
commit-bot@chromium.org | 3adcc34 | 2014-04-23 19:18:09 +0000 | [diff] [blame] | 239 | GrPixelConfig config = SkImageInfo2GrPixelConfig(info); |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 240 | if (kUnknown_GrPixelConfig == config) { |
| 241 | return false; |
| 242 | } |
| 243 | uint32_t flags = 0; |
| 244 | if (kUnpremul_SkAlphaType == info.alphaType()) { |
| 245 | flags = GrContext::kUnpremul_PixelOpsFlag; |
| 246 | } |
| 247 | fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags); |
| 248 | |
| 249 | // need to bump our genID for compatibility with clients that "know" we have a bitmap |
reed | 89443ab | 2014-06-27 11:34:19 -0700 | [diff] [blame] | 250 | fLegacyBitmap.notifyPixelsChanged(); |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 251 | |
| 252 | return true; |
| 253 | } |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 254 | |
senorblanco@chromium.org | b7b7eb3 | 2014-03-19 18:24:04 +0000 | [diff] [blame] | 255 | const SkBitmap& SkGpuDevice::onAccessBitmap() { |
| 256 | DO_DEFERRED_CLEAR(); |
reed | 89443ab | 2014-06-27 11:34:19 -0700 | [diff] [blame] | 257 | return fLegacyBitmap; |
senorblanco@chromium.org | b7b7eb3 | 2014-03-19 18:24:04 +0000 | [diff] [blame] | 258 | } |
| 259 | |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 260 | bool SkGpuDevice::onAccessPixels(SkPixmap* pmap) { |
| 261 | DO_DEFERRED_CLEAR(); |
| 262 | // For compatibility with clients the know we're backed w/ a bitmap, and want to inspect its |
| 263 | // genID. When we can hide/remove that fact, we can eliminate this call to notify. |
| 264 | // ... ugh. |
| 265 | fLegacyBitmap.notifyPixelsChanged(); |
| 266 | return false; |
| 267 | } |
| 268 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 269 | void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) { |
| 270 | INHERITED::onAttachToCanvas(canvas); |
| 271 | |
| 272 | // Canvas promises that this ptr is valid until onDetachFromCanvas is called |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 273 | fClipStack.reset(SkRef(canvas->getClipStack())); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | void SkGpuDevice::onDetachFromCanvas() { |
| 277 | INHERITED::onDetachFromCanvas(); |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 278 | fClip.reset(); |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 279 | fClipStack.reset(NULL); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 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 |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 284 | void SkGpuDevice::prepareDraw(const SkDraw& draw) { |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 285 | SkASSERT(fClipStack.get()); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 286 | |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 287 | SkASSERT(draw.fClipStack && draw.fClipStack == fClipStack); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 288 | |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 289 | fClip.setClipStack(fClipStack, &this->getOrigin()); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 290 | |
| 291 | DO_DEFERRED_CLEAR(); |
| 292 | } |
| 293 | |
| 294 | GrRenderTarget* SkGpuDevice::accessRenderTarget() { |
robertphillips | 7156b09 | 2015-05-14 08:54:12 -0700 | [diff] [blame] | 295 | DO_DEFERRED_CLEAR(); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 296 | return fRenderTarget; |
| 297 | } |
| 298 | |
reed | 8eddfb5 | 2014-12-04 07:50:14 -0800 | [diff] [blame] | 299 | void SkGpuDevice::clearAll() { |
| 300 | GrColor color = 0; |
| 301 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::clearAll", fContext); |
| 302 | SkIRect rect = SkIRect::MakeWH(this->width(), this->height()); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 303 | fDrawContext->clear(fRenderTarget, &rect, color, true); |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 304 | fNeedClear = false; |
reed | 8eddfb5 | 2014-12-04 07:50:14 -0800 | [diff] [blame] | 305 | } |
| 306 | |
kkinnunen | abcfab4 | 2015-02-22 22:53:44 -0800 | [diff] [blame] | 307 | void SkGpuDevice::replaceRenderTarget(bool shouldRetainContent) { |
| 308 | // Caller must have accessed the render target, because it knows the rt must be replaced. |
| 309 | SkASSERT(!fNeedClear); |
| 310 | |
| 311 | SkSurface::Budgeted budgeted = |
| 312 | fRenderTarget->resourcePriv().isBudgeted() ? SkSurface::kYes_Budgeted |
| 313 | : SkSurface::kNo_Budgeted; |
| 314 | |
| 315 | SkAutoTUnref<GrRenderTarget> newRT(CreateRenderTarget( |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 316 | fRenderTarget->getContext(), budgeted, this->imageInfo(), fRenderTarget->desc().fSampleCnt)); |
kkinnunen | abcfab4 | 2015-02-22 22:53:44 -0800 | [diff] [blame] | 317 | |
| 318 | if (NULL == newRT) { |
| 319 | return; |
| 320 | } |
| 321 | |
| 322 | if (shouldRetainContent) { |
| 323 | if (fRenderTarget->wasDestroyed()) { |
| 324 | return; |
| 325 | } |
| 326 | this->context()->copySurface(newRT, fRenderTarget); |
| 327 | } |
| 328 | |
| 329 | SkASSERT(fRenderTarget != newRT); |
| 330 | |
| 331 | fRenderTarget->unref(); |
| 332 | fRenderTarget = newRT.detach(); |
| 333 | |
| 334 | SkASSERT(fRenderTarget->surfacePriv().info() == fLegacyBitmap.info()); |
| 335 | SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (fRenderTarget->surfacePriv().info(), fRenderTarget)); |
| 336 | fLegacyBitmap.setPixelRef(pr)->unref(); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 337 | |
robertphillips | 7b05ff1 | 2015-06-19 14:14:54 -0700 | [diff] [blame] | 338 | fDrawContext.reset(SkRef(fRenderTarget->getContext()->drawContext(&this->surfaceProps()))); |
kkinnunen | abcfab4 | 2015-02-22 22:53:44 -0800 | [diff] [blame] | 339 | } |
| 340 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 341 | /////////////////////////////////////////////////////////////////////////////// |
| 342 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 343 | void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) { |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 344 | CHECK_SHOULD_DRAW(draw); |
egdaniel | d78a168 | 2014-07-09 10:41:26 -0700 | [diff] [blame] | 345 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPaint", fContext); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 346 | |
| 347 | GrPaint grPaint; |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 348 | if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) { |
| 349 | return; |
| 350 | } |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 351 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 352 | fDrawContext->drawPaint(fRenderTarget, fClip, grPaint, *draw.fMatrix); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | // must be in SkCanvas::PointMode order |
| 356 | static const GrPrimitiveType gPointMode2PrimtiveType[] = { |
| 357 | kPoints_GrPrimitiveType, |
| 358 | kLines_GrPrimitiveType, |
| 359 | kLineStrip_GrPrimitiveType |
| 360 | }; |
| 361 | |
| 362 | void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode, |
| 363 | size_t count, const SkPoint pts[], const SkPaint& paint) { |
| 364 | CHECK_FOR_ANNOTATION(paint); |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 365 | CHECK_SHOULD_DRAW(draw); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 366 | |
| 367 | SkScalar width = paint.getStrokeWidth(); |
| 368 | if (width < 0) { |
| 369 | return; |
| 370 | } |
| 371 | |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 372 | if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) { |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 373 | GrStrokeInfo strokeInfo(paint, SkPaint::kStroke_Style); |
| 374 | GrPaint grPaint; |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 375 | if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, |
| 376 | &grPaint)) { |
| 377 | return; |
| 378 | } |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 379 | SkPath path; |
jvanverth | b3eb687 | 2014-10-24 07:12:51 -0700 | [diff] [blame] | 380 | path.setIsVolatile(true); |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 381 | path.moveTo(pts[0]); |
| 382 | path.lineTo(pts[1]); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 383 | fDrawContext->drawPath(fRenderTarget, fClip, grPaint, *draw.fMatrix, path, strokeInfo); |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 384 | return; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 385 | } |
| 386 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 387 | // we only handle hairlines and paints without path effects or mask filters, |
| 388 | // else we let the SkDraw call our drawPath() |
| 389 | if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) { |
| 390 | draw.drawPoints(mode, count, pts, paint, true); |
| 391 | return; |
| 392 | } |
| 393 | |
| 394 | GrPaint grPaint; |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 395 | if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) { |
| 396 | return; |
| 397 | } |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 398 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 399 | fDrawContext->drawVertices(fRenderTarget, |
| 400 | fClip, |
| 401 | grPaint, |
| 402 | *draw.fMatrix, |
| 403 | gPointMode2PrimtiveType[mode], |
| 404 | SkToS32(count), |
| 405 | (SkPoint*)pts, |
| 406 | NULL, |
| 407 | NULL, |
| 408 | NULL, |
| 409 | 0); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | /////////////////////////////////////////////////////////////////////////////// |
| 413 | |
| 414 | void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect, |
| 415 | const SkPaint& paint) { |
egdaniel | bbcb38d | 2014-06-19 10:19:29 -0700 | [diff] [blame] | 416 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRect", fContext); |
| 417 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 418 | CHECK_FOR_ANNOTATION(paint); |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 419 | CHECK_SHOULD_DRAW(draw); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 420 | |
| 421 | bool doStroke = paint.getStyle() != SkPaint::kFill_Style; |
| 422 | SkScalar width = paint.getStrokeWidth(); |
| 423 | |
| 424 | /* |
| 425 | We have special code for hairline strokes, miter-strokes, bevel-stroke |
| 426 | and fills. Anything else we just call our path code. |
| 427 | */ |
| 428 | bool usePath = doStroke && width > 0 && |
| 429 | (paint.getStrokeJoin() == SkPaint::kRound_Join || |
| 430 | (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty())); |
| 431 | // another two reasons we might need to call drawPath... |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 432 | |
| 433 | if (paint.getMaskFilter()) { |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 434 | usePath = true; |
| 435 | } |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 436 | |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 437 | if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) { |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 438 | #if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT) |
| 439 | if (doStroke) { |
| 440 | #endif |
| 441 | usePath = true; |
| 442 | #if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT) |
| 443 | } else { |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 444 | usePath = !draw.fMatrix->preservesRightAngles(); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 445 | } |
| 446 | #endif |
| 447 | } |
| 448 | // until we can both stroke and fill rectangles |
| 449 | if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) { |
| 450 | usePath = true; |
| 451 | } |
| 452 | |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 453 | GrStrokeInfo strokeInfo(paint); |
| 454 | |
| 455 | const SkPathEffect* pe = paint.getPathEffect(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 456 | if (!usePath && pe && !strokeInfo.isDashed()) { |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 457 | usePath = true; |
| 458 | } |
| 459 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 460 | if (usePath) { |
| 461 | SkPath path; |
jvanverth | b3eb687 | 2014-10-24 07:12:51 -0700 | [diff] [blame] | 462 | path.setIsVolatile(true); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 463 | path.addRect(rect); |
| 464 | this->drawPath(draw, path, paint, NULL, true); |
| 465 | return; |
| 466 | } |
| 467 | |
| 468 | GrPaint grPaint; |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 469 | if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) { |
| 470 | return; |
| 471 | } |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 472 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 473 | fDrawContext->drawRect(fRenderTarget, fClip, grPaint, *draw.fMatrix, rect, &strokeInfo); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | /////////////////////////////////////////////////////////////////////////////// |
| 477 | |
| 478 | void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect, |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 479 | const SkPaint& paint) { |
egdaniel | d78a168 | 2014-07-09 10:41:26 -0700 | [diff] [blame] | 480 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRRect", fContext); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 481 | CHECK_FOR_ANNOTATION(paint); |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 482 | CHECK_SHOULD_DRAW(draw); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 483 | |
commit-bot@chromium.org | 8213970 | 2014-03-10 22:53:20 +0000 | [diff] [blame] | 484 | GrPaint grPaint; |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 485 | if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) { |
| 486 | return; |
| 487 | } |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 488 | |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 489 | GrStrokeInfo strokeInfo(paint); |
commit-bot@chromium.org | 8213970 | 2014-03-10 22:53:20 +0000 | [diff] [blame] | 490 | if (paint.getMaskFilter()) { |
| 491 | // try to hit the fast path for drawing filtered round rects |
| 492 | |
| 493 | SkRRect devRRect; |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 494 | if (rect.transform(*draw.fMatrix, &devRRect)) { |
commit-bot@chromium.org | 8213970 | 2014-03-10 22:53:20 +0000 | [diff] [blame] | 495 | if (devRRect.allCornersCircular()) { |
| 496 | SkRect maskRect; |
| 497 | if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(), |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 498 | draw.fClip->getBounds(), |
| 499 | *draw.fMatrix, |
| 500 | &maskRect)) { |
commit-bot@chromium.org | 8213970 | 2014-03-10 22:53:20 +0000 | [diff] [blame] | 501 | SkIRect finalIRect; |
| 502 | maskRect.roundOut(&finalIRect); |
| 503 | if (draw.fClip->quickReject(finalIRect)) { |
| 504 | // clipped out |
| 505 | return; |
| 506 | } |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 507 | if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext, |
| 508 | fRenderTarget, |
| 509 | &grPaint, |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 510 | fClip, |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 511 | *draw.fMatrix, |
kkinnunen | d156d36 | 2015-05-18 22:23:54 -0700 | [diff] [blame] | 512 | strokeInfo, |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 513 | devRRect)) { |
commit-bot@chromium.org | 8213970 | 2014-03-10 22:53:20 +0000 | [diff] [blame] | 514 | return; |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | } |
| 522 | |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 523 | bool usePath = false; |
| 524 | |
| 525 | if (paint.getMaskFilter()) { |
| 526 | usePath = true; |
| 527 | } else { |
| 528 | const SkPathEffect* pe = paint.getPathEffect(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 529 | if (pe && !strokeInfo.isDashed()) { |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 530 | usePath = true; |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | |
| 535 | if (usePath) { |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 536 | SkPath path; |
jvanverth | b3eb687 | 2014-10-24 07:12:51 -0700 | [diff] [blame] | 537 | path.setIsVolatile(true); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 538 | path.addRRect(rect); |
| 539 | this->drawPath(draw, path, paint, NULL, true); |
| 540 | return; |
| 541 | } |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 542 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 543 | fDrawContext->drawRRect(fRenderTarget, fClip, grPaint, *draw.fMatrix, rect, strokeInfo); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 544 | } |
| 545 | |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 546 | void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer, |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 547 | const SkRRect& inner, const SkPaint& paint) { |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 548 | SkStrokeRec stroke(paint); |
| 549 | if (stroke.isFillStyle()) { |
| 550 | |
| 551 | CHECK_FOR_ANNOTATION(paint); |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 552 | CHECK_SHOULD_DRAW(draw); |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 553 | |
| 554 | GrPaint grPaint; |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 555 | if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, |
| 556 | &grPaint)) { |
| 557 | return; |
| 558 | } |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 559 | |
| 560 | if (NULL == paint.getMaskFilter() && NULL == paint.getPathEffect()) { |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 561 | fDrawContext->drawDRRect(fRenderTarget, fClip, grPaint, *draw.fMatrix, outer, inner); |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 562 | return; |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | SkPath path; |
jvanverth | b3eb687 | 2014-10-24 07:12:51 -0700 | [diff] [blame] | 567 | path.setIsVolatile(true); |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 568 | path.addRRect(outer); |
| 569 | path.addRRect(inner); |
| 570 | path.setFillType(SkPath::kEvenOdd_FillType); |
| 571 | |
| 572 | this->drawPath(draw, path, paint, NULL, true); |
| 573 | } |
| 574 | |
| 575 | |
commit-bot@chromium.org | 8213970 | 2014-03-10 22:53:20 +0000 | [diff] [blame] | 576 | ///////////////////////////////////////////////////////////////////////////// |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 577 | |
| 578 | void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval, |
| 579 | const SkPaint& paint) { |
egdaniel | d78a168 | 2014-07-09 10:41:26 -0700 | [diff] [blame] | 580 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawOval", fContext); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 581 | CHECK_FOR_ANNOTATION(paint); |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 582 | CHECK_SHOULD_DRAW(draw); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 583 | |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 584 | GrStrokeInfo strokeInfo(paint); |
| 585 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 586 | bool usePath = false; |
| 587 | // some basic reasons we might need to call drawPath... |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 588 | if (paint.getMaskFilter()) { |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 589 | usePath = true; |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 590 | } else { |
| 591 | const SkPathEffect* pe = paint.getPathEffect(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 592 | if (pe && !strokeInfo.isDashed()) { |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 593 | usePath = true; |
| 594 | } |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | if (usePath) { |
| 598 | SkPath path; |
jvanverth | b3eb687 | 2014-10-24 07:12:51 -0700 | [diff] [blame] | 599 | path.setIsVolatile(true); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 600 | path.addOval(oval); |
| 601 | this->drawPath(draw, path, paint, NULL, true); |
| 602 | return; |
| 603 | } |
| 604 | |
| 605 | GrPaint grPaint; |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 606 | if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) { |
| 607 | return; |
| 608 | } |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 609 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 610 | fDrawContext->drawOval(fRenderTarget, fClip, grPaint, *draw.fMatrix, oval, strokeInfo); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | #include "SkMaskFilter.h" |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 614 | |
| 615 | /////////////////////////////////////////////////////////////////////////////// |
| 616 | |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 617 | static SkBitmap wrap_texture(GrTexture* texture, int width, int height) { |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 618 | SkBitmap result; |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 619 | result.setInfo(SkImageInfo::MakeN32Premul(width, height)); |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 620 | result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (result.info(), texture)))->unref(); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 621 | return result; |
| 622 | } |
| 623 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 624 | void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath, |
| 625 | const SkPaint& paint, const SkMatrix* prePathMatrix, |
| 626 | bool pathIsMutable) { |
| 627 | CHECK_FOR_ANNOTATION(paint); |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 628 | CHECK_SHOULD_DRAW(draw); |
egdaniel | d78a168 | 2014-07-09 10:41:26 -0700 | [diff] [blame] | 629 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPath", fContext); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 630 | |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 631 | GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext, fRenderTarget, |
| 632 | fClip, origSrcPath, paint, |
| 633 | *draw.fMatrix, prePathMatrix, |
| 634 | draw.fClip->getBounds(), pathIsMutable); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | static const int kBmpSmallTileSize = 1 << 10; |
| 638 | |
| 639 | static inline int get_tile_count(const SkIRect& srcRect, int tileSize) { |
| 640 | int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1; |
| 641 | int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1; |
| 642 | return tilesX * tilesY; |
| 643 | } |
| 644 | |
| 645 | static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) { |
| 646 | if (maxTileSize <= kBmpSmallTileSize) { |
| 647 | return maxTileSize; |
| 648 | } |
| 649 | |
| 650 | size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize); |
| 651 | size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize); |
| 652 | |
| 653 | maxTileTotalTileSize *= maxTileSize * maxTileSize; |
| 654 | smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize; |
| 655 | |
| 656 | if (maxTileTotalTileSize > 2 * smallTotalTileSize) { |
| 657 | return kBmpSmallTileSize; |
| 658 | } else { |
| 659 | return maxTileSize; |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | // Given a bitmap, an optional src rect, and a context with a clip and matrix determine what |
| 664 | // pixels from the bitmap are necessary. |
| 665 | static void determine_clipped_src_rect(const GrContext* context, |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 666 | const GrRenderTarget* rt, |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 667 | const GrClip& clip, |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 668 | const SkMatrix& viewMatrix, |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 669 | const SkBitmap& bitmap, |
| 670 | const SkRect* srcRectPtr, |
| 671 | SkIRect* clippedSrcIRect) { |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 672 | clip.getConservativeBounds(rt, clippedSrcIRect, NULL); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 673 | SkMatrix inv; |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 674 | if (!viewMatrix.invert(&inv)) { |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 675 | clippedSrcIRect->setEmpty(); |
| 676 | return; |
| 677 | } |
| 678 | SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect); |
| 679 | inv.mapRect(&clippedSrcRect); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 680 | if (srcRectPtr) { |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 681 | // we've setup src space 0,0 to map to the top left of the src rect. |
| 682 | clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 683 | if (!clippedSrcRect.intersect(*srcRectPtr)) { |
| 684 | clippedSrcIRect->setEmpty(); |
| 685 | return; |
| 686 | } |
| 687 | } |
| 688 | clippedSrcRect.roundOut(clippedSrcIRect); |
| 689 | SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height()); |
| 690 | if (!clippedSrcIRect->intersect(bmpBounds)) { |
| 691 | clippedSrcIRect->setEmpty(); |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap, |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 696 | const SkMatrix& viewMatrix, |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 697 | const GrTextureParams& params, |
| 698 | const SkRect* srcRectPtr, |
| 699 | int maxTileSize, |
| 700 | int* tileSize, |
| 701 | SkIRect* clippedSrcRect) const { |
| 702 | // if bitmap is explictly texture backed then just use the texture |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 703 | if (bitmap.getTexture()) { |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 704 | return false; |
| 705 | } |
| 706 | |
| 707 | // if it's larger than the max tile size, then we have no choice but tiling. |
| 708 | if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) { |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 709 | determine_clipped_src_rect(fContext, fRenderTarget, fClip, viewMatrix, bitmap, |
| 710 | srcRectPtr, clippedSrcRect); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 711 | *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize); |
| 712 | return true; |
| 713 | } |
| 714 | |
| 715 | if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) { |
| 716 | return false; |
| 717 | } |
| 718 | |
| 719 | // if the entire texture is already in our cache then no reason to tile it |
| 720 | if (GrIsBitmapInCache(fContext, bitmap, ¶ms)) { |
| 721 | return false; |
| 722 | } |
| 723 | |
| 724 | // At this point we know we could do the draw by uploading the entire bitmap |
| 725 | // as a texture. However, if the texture would be large compared to the |
| 726 | // cache size and we don't require most of it for this draw then tile to |
| 727 | // reduce the amount of upload and cache spill. |
| 728 | |
| 729 | // assumption here is that sw bitmap size is a good proxy for its size as |
| 730 | // a texture |
| 731 | size_t bmpSize = bitmap.getSize(); |
| 732 | size_t cacheSize; |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 733 | fContext->getResourceCacheLimits(NULL, &cacheSize); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 734 | if (bmpSize < cacheSize / 2) { |
| 735 | return false; |
| 736 | } |
| 737 | |
| 738 | // Figure out how much of the src we will need based on the src rect and clipping. |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 739 | determine_clipped_src_rect(fContext, fRenderTarget, fClip, viewMatrix, bitmap, srcRectPtr, |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 740 | clippedSrcRect); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 741 | *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile. |
| 742 | size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) * |
| 743 | kBmpSmallTileSize * kBmpSmallTileSize; |
| 744 | |
| 745 | return usedTileBytes < 2 * bmpSize; |
| 746 | } |
| 747 | |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 748 | void SkGpuDevice::drawBitmap(const SkDraw& origDraw, |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 749 | const SkBitmap& bitmap, |
| 750 | const SkMatrix& m, |
| 751 | const SkPaint& paint) { |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 752 | SkMatrix concat; |
| 753 | SkTCopyOnFirstWrite<SkDraw> draw(origDraw); |
| 754 | if (!m.isIdentity()) { |
| 755 | concat.setConcat(*draw->fMatrix, m); |
| 756 | draw.writable()->fMatrix = &concat; |
| 757 | } |
| 758 | this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 759 | } |
| 760 | |
commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 761 | // This method outsets 'iRect' by 'outset' all around and then clamps its extents to |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 762 | // 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner |
| 763 | // of 'iRect' for all possible outsets/clamps. |
commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 764 | static inline void clamped_outset_with_offset(SkIRect* iRect, |
| 765 | int outset, |
| 766 | SkPoint* offset, |
| 767 | const SkIRect& clamp) { |
| 768 | iRect->outset(outset, outset); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 769 | |
commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 770 | int leftClampDelta = clamp.fLeft - iRect->fLeft; |
| 771 | if (leftClampDelta > 0) { |
| 772 | offset->fX -= outset - leftClampDelta; |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 773 | iRect->fLeft = clamp.fLeft; |
| 774 | } else { |
commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 775 | offset->fX -= outset; |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 776 | } |
commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 777 | |
| 778 | int topClampDelta = clamp.fTop - iRect->fTop; |
| 779 | if (topClampDelta > 0) { |
| 780 | offset->fY -= outset - topClampDelta; |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 781 | iRect->fTop = clamp.fTop; |
| 782 | } else { |
commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 783 | offset->fY -= outset; |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | if (iRect->fRight > clamp.fRight) { |
| 787 | iRect->fRight = clamp.fRight; |
| 788 | } |
| 789 | if (iRect->fBottom > clamp.fBottom) { |
| 790 | iRect->fBottom = clamp.fBottom; |
| 791 | } |
| 792 | } |
| 793 | |
commit-bot@chromium.org | a17773f | 2014-05-09 13:53:38 +0000 | [diff] [blame] | 794 | static bool has_aligned_samples(const SkRect& srcRect, |
| 795 | const SkRect& transformedRect) { |
| 796 | // detect pixel disalignment |
| 797 | if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) - |
| 798 | transformedRect.left()) < COLOR_BLEED_TOLERANCE && |
| 799 | SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) - |
| 800 | transformedRect.top()) < COLOR_BLEED_TOLERANCE && |
| 801 | SkScalarAbs(transformedRect.width() - srcRect.width()) < |
| 802 | COLOR_BLEED_TOLERANCE && |
| 803 | SkScalarAbs(transformedRect.height() - srcRect.height()) < |
| 804 | COLOR_BLEED_TOLERANCE) { |
| 805 | return true; |
| 806 | } |
| 807 | return false; |
| 808 | } |
| 809 | |
| 810 | static bool may_color_bleed(const SkRect& srcRect, |
| 811 | const SkRect& transformedRect, |
| 812 | const SkMatrix& m) { |
| 813 | // Only gets called if has_aligned_samples returned false. |
| 814 | // So we can assume that sampling is axis aligned but not texel aligned. |
| 815 | SkASSERT(!has_aligned_samples(srcRect, transformedRect)); |
| 816 | SkRect innerSrcRect(srcRect), innerTransformedRect, |
| 817 | outerTransformedRect(transformedRect); |
| 818 | innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf); |
| 819 | m.mapRect(&innerTransformedRect, innerSrcRect); |
| 820 | |
| 821 | // The gap between outerTransformedRect and innerTransformedRect |
| 822 | // represents the projection of the source border area, which is |
| 823 | // problematic for color bleeding. We must check whether any |
| 824 | // destination pixels sample the border area. |
| 825 | outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE); |
| 826 | innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE); |
| 827 | SkIRect outer, inner; |
| 828 | outerTransformedRect.round(&outer); |
| 829 | innerTransformedRect.round(&inner); |
| 830 | // If the inner and outer rects round to the same result, it means the |
| 831 | // border does not overlap any pixel centers. Yay! |
| 832 | return inner != outer; |
| 833 | } |
| 834 | |
| 835 | static bool needs_texture_domain(const SkBitmap& bitmap, |
| 836 | const SkRect& srcRect, |
| 837 | GrTextureParams ¶ms, |
| 838 | const SkMatrix& contextMatrix, |
| 839 | bool bicubic) { |
| 840 | bool needsTextureDomain = false; |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 841 | GrTexture* tex = bitmap.getTexture(); |
| 842 | int width = tex ? tex->width() : bitmap.width(); |
| 843 | int height = tex ? tex->height() : bitmap.height(); |
commit-bot@chromium.org | a17773f | 2014-05-09 13:53:38 +0000 | [diff] [blame] | 844 | |
| 845 | if (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode) { |
| 846 | // Need texture domain if drawing a sub rect |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 847 | needsTextureDomain = srcRect.width() < width || |
| 848 | srcRect.height() < height; |
commit-bot@chromium.org | a17773f | 2014-05-09 13:53:38 +0000 | [diff] [blame] | 849 | if (!bicubic && needsTextureDomain && contextMatrix.rectStaysRect()) { |
| 850 | // sampling is axis-aligned |
| 851 | SkRect transformedRect; |
| 852 | contextMatrix.mapRect(&transformedRect, srcRect); |
| 853 | |
| 854 | if (has_aligned_samples(srcRect, transformedRect)) { |
| 855 | params.setFilterMode(GrTextureParams::kNone_FilterMode); |
| 856 | needsTextureDomain = false; |
| 857 | } else { |
| 858 | needsTextureDomain = may_color_bleed(srcRect, transformedRect, contextMatrix); |
| 859 | } |
| 860 | } |
| 861 | } |
| 862 | return needsTextureDomain; |
| 863 | } |
| 864 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 865 | void SkGpuDevice::drawBitmapCommon(const SkDraw& draw, |
| 866 | const SkBitmap& bitmap, |
| 867 | const SkRect* srcRectPtr, |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 868 | const SkSize* dstSizePtr, |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 869 | const SkPaint& paint, |
| 870 | SkCanvas::DrawBitmapRectFlags flags) { |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 871 | CHECK_SHOULD_DRAW(draw); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 872 | |
| 873 | SkRect srcRect; |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 874 | SkSize dstSize; |
commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 875 | // If there is no src rect, or the src rect contains the entire bitmap then we're effectively |
| 876 | // in the (easier) bleed case, so update flags. |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 877 | if (NULL == srcRectPtr) { |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 878 | SkScalar w = SkIntToScalar(bitmap.width()); |
| 879 | SkScalar h = SkIntToScalar(bitmap.height()); |
| 880 | dstSize.fWidth = w; |
| 881 | dstSize.fHeight = h; |
| 882 | srcRect.set(0, 0, w, h); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 883 | } else { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 884 | SkASSERT(dstSizePtr); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 885 | srcRect = *srcRectPtr; |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 886 | dstSize = *dstSizePtr; |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 887 | } |
| 888 | GrTexture* tex = bitmap.getTexture(); |
| 889 | int width = tex ? tex->width() : bitmap.width(); |
| 890 | int height = tex ? tex->height() : bitmap.height(); |
| 891 | if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 && |
| 892 | srcRect.fRight >= width && srcRect.fBottom >= height) { |
| 893 | flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 894 | } |
| 895 | |
derekf | 367e186 | 2014-12-02 11:02:06 -0800 | [diff] [blame] | 896 | // If the render target is not msaa and draw is antialiased, we call |
| 897 | // drawRect instead of drawing on the render target directly. |
| 898 | // FIXME: the tiled bitmap code path doesn't currently support |
| 899 | // anti-aliased edges, we work around that for now by drawing directly |
| 900 | // if the image size exceeds maximum texture size. |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 901 | int maxTextureSize = fContext->caps()->maxTextureSize(); |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 902 | bool directDraw = fRenderTarget->isUnifiedMultisampled() || |
derekf | 367e186 | 2014-12-02 11:02:06 -0800 | [diff] [blame] | 903 | !paint.isAntiAlias() || |
| 904 | bitmap.width() > maxTextureSize || |
| 905 | bitmap.height() > maxTextureSize; |
| 906 | |
| 907 | // we check whether dst rect are pixel aligned |
| 908 | if (!directDraw) { |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 909 | bool staysRect = draw.fMatrix->rectStaysRect(); |
derekf | 367e186 | 2014-12-02 11:02:06 -0800 | [diff] [blame] | 910 | |
| 911 | if (staysRect) { |
| 912 | SkRect rect; |
| 913 | SkRect dstRect = SkRect::MakeXYWH(0, 0, dstSize.fWidth, dstSize.fHeight); |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 914 | draw.fMatrix->mapRect(&rect, dstRect); |
derekf | 367e186 | 2014-12-02 11:02:06 -0800 | [diff] [blame] | 915 | const SkScalar *scalars = rect.asScalars(); |
| 916 | bool isDstPixelAligned = true; |
| 917 | for (int i = 0; i < 4; i++) { |
| 918 | if (!SkScalarIsInt(scalars[i])) { |
| 919 | isDstPixelAligned = false; |
| 920 | break; |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | if (isDstPixelAligned) |
| 925 | directDraw = true; |
| 926 | } |
| 927 | } |
| 928 | |
| 929 | if (paint.getMaskFilter() || !directDraw) { |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 930 | // Convert the bitmap to a shader so that the rect can be drawn |
| 931 | // through drawRect, which supports mask filters. |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 932 | SkBitmap tmp; // subset of bitmap, if necessary |
| 933 | const SkBitmap* bitmapPtr = &bitmap; |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 934 | SkMatrix localM; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 935 | if (srcRectPtr) { |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 936 | localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop); |
| 937 | localM.postScale(dstSize.fWidth / srcRectPtr->width(), |
| 938 | dstSize.fHeight / srcRectPtr->height()); |
commit-bot@chromium.org | d6ca4ac | 2013-11-22 20:34:59 +0000 | [diff] [blame] | 939 | // In bleed mode we position and trim the bitmap based on the src rect which is |
| 940 | // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out |
| 941 | // the desired portion of the bitmap and then update 'm' and 'srcRect' to |
| 942 | // compensate. |
| 943 | if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) { |
| 944 | SkIRect iSrc; |
| 945 | srcRect.roundOut(&iSrc); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 946 | |
commit-bot@chromium.org | d6ca4ac | 2013-11-22 20:34:59 +0000 | [diff] [blame] | 947 | SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft), |
| 948 | SkIntToScalar(iSrc.fTop)); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 949 | |
commit-bot@chromium.org | d6ca4ac | 2013-11-22 20:34:59 +0000 | [diff] [blame] | 950 | if (!bitmap.extractSubset(&tmp, iSrc)) { |
| 951 | return; // extraction failed |
| 952 | } |
| 953 | bitmapPtr = &tmp; |
| 954 | srcRect.offset(-offset.fX, -offset.fY); |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 955 | |
commit-bot@chromium.org | d6ca4ac | 2013-11-22 20:34:59 +0000 | [diff] [blame] | 956 | // The source rect has changed so update the matrix |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 957 | localM.preTranslate(offset.fX, offset.fY); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 958 | } |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 959 | } else { |
| 960 | localM.reset(); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 961 | } |
| 962 | |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 963 | SkPaint paintWithShader(paint); |
| 964 | paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr, |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 965 | SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &localM))->unref(); |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 966 | SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight}; |
| 967 | this->drawRect(draw, dstRect, paintWithShader); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 968 | |
| 969 | return; |
| 970 | } |
| 971 | |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 972 | // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using |
| 973 | // the view matrix rather than a local matrix. |
| 974 | SkMatrix m; |
| 975 | m.setScale(dstSize.fWidth / srcRect.width(), |
| 976 | dstSize.fHeight / srcRect.height()); |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 977 | SkMatrix viewM = *draw.fMatrix; |
| 978 | viewM.preConcat(m); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 979 | |
| 980 | GrTextureParams params; |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 981 | SkFilterQuality paintFilterQuality = paint.getFilterQuality(); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 982 | GrTextureParams::FilterMode textureFilterMode; |
commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 983 | |
commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 984 | bool doBicubic = false; |
| 985 | |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 986 | switch(paintFilterQuality) { |
| 987 | case kNone_SkFilterQuality: |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 988 | textureFilterMode = GrTextureParams::kNone_FilterMode; |
| 989 | break; |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 990 | case kLow_SkFilterQuality: |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 991 | textureFilterMode = GrTextureParams::kBilerp_FilterMode; |
| 992 | break; |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 993 | case kMedium_SkFilterQuality: |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 994 | if (viewM.getMinScale() < SK_Scalar1) { |
commit-bot@chromium.org | 79b7eee | 2013-12-16 21:02:29 +0000 | [diff] [blame] | 995 | textureFilterMode = GrTextureParams::kMipMap_FilterMode; |
| 996 | } else { |
| 997 | // Don't trigger MIP level generation unnecessarily. |
| 998 | textureFilterMode = GrTextureParams::kBilerp_FilterMode; |
| 999 | } |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1000 | break; |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 1001 | case kHigh_SkFilterQuality: |
commit-bot@chromium.org | cea9abb | 2013-12-09 19:15:37 +0000 | [diff] [blame] | 1002 | // Minification can look bad with the bicubic effect. |
commit-bot@chromium.org | 9927bd3 | 2014-05-20 17:51:13 +0000 | [diff] [blame] | 1003 | doBicubic = |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 1004 | GrBicubicEffect::ShouldUseBicubic(viewM, &textureFilterMode); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1005 | break; |
| 1006 | default: |
| 1007 | SkErrorInternals::SetError( kInvalidPaint_SkError, |
| 1008 | "Sorry, I don't understand the filtering " |
| 1009 | "mode you asked for. Falling back to " |
| 1010 | "MIPMaps."); |
| 1011 | textureFilterMode = GrTextureParams::kMipMap_FilterMode; |
| 1012 | break; |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
commit-bot@chromium.org | 9927bd3 | 2014-05-20 17:51:13 +0000 | [diff] [blame] | 1015 | int tileFilterPad; |
| 1016 | if (doBicubic) { |
| 1017 | tileFilterPad = GrBicubicEffect::kFilterTexelPad; |
| 1018 | } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) { |
| 1019 | tileFilterPad = 0; |
| 1020 | } else { |
| 1021 | tileFilterPad = 1; |
| 1022 | } |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1023 | params.setFilterMode(textureFilterMode); |
| 1024 | |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 1025 | int maxTileSize = fContext->caps()->maxTextureSize() - 2 * tileFilterPad; |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1026 | int tileSize; |
| 1027 | |
| 1028 | SkIRect clippedSrcRect; |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 1029 | if (this->shouldTileBitmap(bitmap, viewM, params, srcRectPtr, maxTileSize, &tileSize, |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1030 | &clippedSrcRect)) { |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 1031 | this->drawTiledBitmap(bitmap, viewM, srcRect, clippedSrcRect, params, paint, flags, |
| 1032 | tileSize, doBicubic); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1033 | } else { |
| 1034 | // take the simple case |
commit-bot@chromium.org | a17773f | 2014-05-09 13:53:38 +0000 | [diff] [blame] | 1035 | bool needsTextureDomain = needs_texture_domain(bitmap, |
| 1036 | srcRect, |
| 1037 | params, |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 1038 | viewM, |
commit-bot@chromium.org | a17773f | 2014-05-09 13:53:38 +0000 | [diff] [blame] | 1039 | doBicubic); |
| 1040 | this->internalDrawBitmap(bitmap, |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 1041 | viewM, |
commit-bot@chromium.org | a17773f | 2014-05-09 13:53:38 +0000 | [diff] [blame] | 1042 | srcRect, |
| 1043 | params, |
| 1044 | paint, |
| 1045 | flags, |
| 1046 | doBicubic, |
| 1047 | needsTextureDomain); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1048 | } |
| 1049 | } |
| 1050 | |
| 1051 | // Break 'bitmap' into several tiles to draw it since it has already |
| 1052 | // been determined to be too large to fit in VRAM |
| 1053 | void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap, |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 1054 | const SkMatrix& viewMatrix, |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1055 | const SkRect& srcRect, |
| 1056 | const SkIRect& clippedSrcIRect, |
| 1057 | const GrTextureParams& params, |
| 1058 | const SkPaint& paint, |
| 1059 | SkCanvas::DrawBitmapRectFlags flags, |
commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 1060 | int tileSize, |
| 1061 | bool bicubic) { |
commit-bot@chromium.org | 9d5e3f1 | 2014-05-01 21:23:19 +0000 | [diff] [blame] | 1062 | // The following pixel lock is technically redundant, but it is desirable |
| 1063 | // to lock outside of the tile loop to prevent redecoding the whole image |
| 1064 | // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that |
| 1065 | // is larger than the limit of the discardable memory pool. |
| 1066 | SkAutoLockPixels alp(bitmap); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1067 | SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect); |
| 1068 | |
| 1069 | int nx = bitmap.width() / tileSize; |
| 1070 | int ny = bitmap.height() / tileSize; |
| 1071 | for (int x = 0; x <= nx; x++) { |
| 1072 | for (int y = 0; y <= ny; y++) { |
| 1073 | SkRect tileR; |
| 1074 | tileR.set(SkIntToScalar(x * tileSize), |
| 1075 | SkIntToScalar(y * tileSize), |
| 1076 | SkIntToScalar((x + 1) * tileSize), |
| 1077 | SkIntToScalar((y + 1) * tileSize)); |
| 1078 | |
| 1079 | if (!SkRect::Intersects(tileR, clippedSrcRect)) { |
| 1080 | continue; |
| 1081 | } |
| 1082 | |
| 1083 | if (!tileR.intersect(srcRect)) { |
| 1084 | continue; |
| 1085 | } |
| 1086 | |
| 1087 | SkBitmap tmpB; |
| 1088 | SkIRect iTileR; |
| 1089 | tileR.roundOut(&iTileR); |
| 1090 | SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft), |
| 1091 | SkIntToScalar(iTileR.fTop)); |
| 1092 | |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1093 | // Adjust the context matrix to draw at the right x,y in device space |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 1094 | SkMatrix viewM = viewMatrix; |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1095 | SkMatrix tmpM; |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1096 | tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop); |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 1097 | viewM.preConcat(tmpM); |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1098 | |
robertphillips | ec8bb94 | 2014-11-21 10:16:25 -0800 | [diff] [blame] | 1099 | if (GrTextureParams::kNone_FilterMode != params.filterMode() || bicubic) { |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1100 | SkIRect iClampRect; |
| 1101 | |
| 1102 | if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) { |
| 1103 | // In bleed mode we want to always expand the tile on all edges |
| 1104 | // but stay within the bitmap bounds |
| 1105 | iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height()); |
| 1106 | } else { |
| 1107 | // In texture-domain/clamp mode we only want to expand the |
| 1108 | // tile on edges interior to "srcRect" (i.e., we want to |
| 1109 | // not bleed across the original clamped edges) |
| 1110 | srcRect.roundOut(&iClampRect); |
| 1111 | } |
commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 1112 | int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1; |
| 1113 | clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
| 1116 | if (bitmap.extractSubset(&tmpB, iTileR)) { |
| 1117 | // now offset it to make it "local" to our tmp bitmap |
| 1118 | tileR.offset(-offset.fX, -offset.fY); |
commit-bot@chromium.org | a17773f | 2014-05-09 13:53:38 +0000 | [diff] [blame] | 1119 | GrTextureParams paramsTemp = params; |
| 1120 | bool needsTextureDomain = needs_texture_domain(bitmap, |
| 1121 | srcRect, |
| 1122 | paramsTemp, |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 1123 | viewM, |
commit-bot@chromium.org | a17773f | 2014-05-09 13:53:38 +0000 | [diff] [blame] | 1124 | bicubic); |
| 1125 | this->internalDrawBitmap(tmpB, |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 1126 | viewM, |
commit-bot@chromium.org | a17773f | 2014-05-09 13:53:38 +0000 | [diff] [blame] | 1127 | tileR, |
| 1128 | paramsTemp, |
| 1129 | paint, |
| 1130 | flags, |
| 1131 | bicubic, |
| 1132 | needsTextureDomain); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1133 | } |
| 1134 | } |
| 1135 | } |
| 1136 | } |
| 1137 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1138 | |
| 1139 | /* |
| 1140 | * This is called by drawBitmap(), which has to handle images that may be too |
| 1141 | * large to be represented by a single texture. |
| 1142 | * |
| 1143 | * internalDrawBitmap assumes that the specified bitmap will fit in a texture |
| 1144 | * and that non-texture portion of the GrPaint has already been setup. |
| 1145 | */ |
| 1146 | void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap, |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 1147 | const SkMatrix& viewMatrix, |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1148 | const SkRect& srcRect, |
| 1149 | const GrTextureParams& params, |
| 1150 | const SkPaint& paint, |
commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 1151 | SkCanvas::DrawBitmapRectFlags flags, |
commit-bot@chromium.org | a17773f | 2014-05-09 13:53:38 +0000 | [diff] [blame] | 1152 | bool bicubic, |
| 1153 | bool needsTextureDomain) { |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 1154 | SkASSERT(bitmap.width() <= fContext->caps()->maxTextureSize() && |
| 1155 | bitmap.height() <= fContext->caps()->maxTextureSize()); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1156 | |
| 1157 | GrTexture* texture; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 1158 | AutoBitmapTexture abt(fContext, bitmap, ¶ms, &texture); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1159 | if (NULL == texture) { |
| 1160 | return; |
| 1161 | } |
| 1162 | |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1163 | SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() }; |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1164 | SkRect paintRect; |
| 1165 | SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width())); |
| 1166 | SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height())); |
| 1167 | paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv), |
| 1168 | SkScalarMul(srcRect.fTop, hInv), |
| 1169 | SkScalarMul(srcRect.fRight, wInv), |
| 1170 | SkScalarMul(srcRect.fBottom, hInv)); |
| 1171 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1172 | SkRect textureDomain = SkRect::MakeEmpty(); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1173 | SkAutoTUnref<GrFragmentProcessor> fp; |
commit-bot@chromium.org | a17773f | 2014-05-09 13:53:38 +0000 | [diff] [blame] | 1174 | if (needsTextureDomain && !(flags & SkCanvas::kBleed_DrawBitmapRectFlag)) { |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1175 | // Use a constrained texture domain to avoid color bleeding |
| 1176 | SkScalar left, top, right, bottom; |
| 1177 | if (srcRect.width() > SK_Scalar1) { |
| 1178 | SkScalar border = SK_ScalarHalf / texture->width(); |
| 1179 | left = paintRect.left() + border; |
| 1180 | right = paintRect.right() - border; |
| 1181 | } else { |
| 1182 | left = right = SkScalarHalf(paintRect.left() + paintRect.right()); |
| 1183 | } |
| 1184 | if (srcRect.height() > SK_Scalar1) { |
| 1185 | SkScalar border = SK_ScalarHalf / texture->height(); |
| 1186 | top = paintRect.top() + border; |
| 1187 | bottom = paintRect.bottom() - border; |
| 1188 | } else { |
| 1189 | top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom()); |
| 1190 | } |
| 1191 | textureDomain.setLTRB(left, top, right, bottom); |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 1192 | if (bicubic) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1193 | fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain)); |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 1194 | } else { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1195 | fp.reset(GrTextureDomainEffect::Create(texture, |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 1196 | SkMatrix::I(), |
| 1197 | textureDomain, |
| 1198 | GrTextureDomain::kClamp_Mode, |
| 1199 | params.filterMode())); |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 1200 | } |
commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 1201 | } else if (bicubic) { |
commit-bot@chromium.org | bc91fd7 | 2013-12-10 12:53:39 +0000 | [diff] [blame] | 1202 | SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode()); |
| 1203 | SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() }; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1204 | fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes)); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1205 | } else { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1206 | fp.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params)); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
| 1209 | // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring |
| 1210 | // the rest from the SkPaint. |
| 1211 | GrPaint grPaint; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1212 | grPaint.addColorProcessor(fp); |
reed | 0689d7b | 2014-06-14 05:30:20 -0700 | [diff] [blame] | 1213 | bool alphaOnly = !(kAlpha_8_SkColorType == bitmap.colorType()); |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 1214 | GrColor paintColor = (alphaOnly) ? SkColor2GrColorJustAlpha(paint.getColor()) : |
| 1215 | SkColor2GrColor(paint.getColor()); |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 1216 | if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint, paintColor, false, |
| 1217 | &grPaint)) { |
| 1218 | return; |
| 1219 | } |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1220 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1221 | fDrawContext->drawNonAARectToRect(fRenderTarget, fClip, grPaint, viewMatrix, dstRect, |
| 1222 | paintRect); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
fmalita | 2d97bc1 | 2014-11-20 10:44:58 -0800 | [diff] [blame] | 1225 | bool SkGpuDevice::filterTexture(GrContext* context, GrTexture* texture, |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 1226 | int width, int height, |
fmalita | 2d97bc1 | 2014-11-20 10:44:58 -0800 | [diff] [blame] | 1227 | const SkImageFilter* filter, |
| 1228 | const SkImageFilter::Context& ctx, |
| 1229 | SkBitmap* result, SkIPoint* offset) { |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1230 | SkASSERT(filter); |
fmalita | 2d97bc1 | 2014-11-20 10:44:58 -0800 | [diff] [blame] | 1231 | |
robertphillips | efbffed | 2015-06-22 12:06:08 -0700 | [diff] [blame^] | 1232 | SkImageFilter::Proxy proxy(this); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1233 | |
| 1234 | if (filter->canFilterImageGPU()) { |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 1235 | return filter->filterImageGPU(&proxy, wrap_texture(texture, width, height), |
| 1236 | ctx, result, offset); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1237 | } else { |
| 1238 | return false; |
| 1239 | } |
| 1240 | } |
| 1241 | |
| 1242 | void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap, |
| 1243 | int left, int top, const SkPaint& paint) { |
| 1244 | // drawSprite is defined to be in device coords. |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 1245 | CHECK_SHOULD_DRAW(draw); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1246 | |
| 1247 | SkAutoLockPixels alp(bitmap, !bitmap.getTexture()); |
| 1248 | if (!bitmap.getTexture() && !bitmap.readyToDraw()) { |
| 1249 | return; |
| 1250 | } |
| 1251 | |
| 1252 | int w = bitmap.width(); |
| 1253 | int h = bitmap.height(); |
| 1254 | |
| 1255 | GrTexture* texture; |
| 1256 | // draw sprite uses the default texture params |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 1257 | AutoBitmapTexture abt(fContext, bitmap, NULL, &texture); |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 1258 | if (!texture) { |
| 1259 | return; |
| 1260 | } |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1261 | |
| 1262 | SkImageFilter* filter = paint.getImageFilter(); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1263 | // This bitmap will own the filtered result as a texture. |
| 1264 | SkBitmap filteredBitmap; |
| 1265 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1266 | if (filter) { |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 1267 | SkIPoint offset = SkIPoint::Make(0, 0); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1268 | SkMatrix matrix(*draw.fMatrix); |
| 1269 | matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top)); |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 1270 | SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height()); |
senorblanco | be129b2 | 2014-08-08 07:14:35 -0700 | [diff] [blame] | 1271 | SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache()); |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 1272 | // This cache is transient, and is freed (along with all its contained |
| 1273 | // textures) when it goes out of scope. |
commit-bot@chromium.org | f7efa50 | 2014-04-11 18:57:00 +0000 | [diff] [blame] | 1274 | SkImageFilter::Context ctx(matrix, clipBounds, cache); |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 1275 | if (this->filterTexture(fContext, texture, w, h, filter, ctx, &filteredBitmap, |
fmalita | 2d97bc1 | 2014-11-20 10:44:58 -0800 | [diff] [blame] | 1276 | &offset)) { |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1277 | texture = (GrTexture*) filteredBitmap.getTexture(); |
| 1278 | w = filteredBitmap.width(); |
| 1279 | h = filteredBitmap.height(); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 1280 | left += offset.x(); |
| 1281 | top += offset.y(); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1282 | } else { |
| 1283 | return; |
| 1284 | } |
| 1285 | } |
| 1286 | |
| 1287 | GrPaint grPaint; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1288 | grPaint.addColorTextureProcessor(texture, SkMatrix::I()); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1289 | |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 1290 | if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint, |
| 1291 | SkColor2GrColorJustAlpha(paint.getColor()), false, &grPaint)) { |
| 1292 | return; |
| 1293 | } |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1294 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1295 | fDrawContext->drawNonAARectToRect(fRenderTarget, |
| 1296 | fClip, |
| 1297 | grPaint, |
| 1298 | SkMatrix::I(), |
| 1299 | SkRect::MakeXYWH(SkIntToScalar(left), |
| 1300 | SkIntToScalar(top), |
| 1301 | SkIntToScalar(w), |
| 1302 | SkIntToScalar(h)), |
| 1303 | SkRect::MakeXYWH(0, |
| 1304 | 0, |
| 1305 | SK_Scalar1 * w / texture->width(), |
| 1306 | SK_Scalar1 * h / texture->height())); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1309 | void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap, |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1310 | const SkRect* src, const SkRect& dst, |
| 1311 | const SkPaint& paint, |
| 1312 | SkCanvas::DrawBitmapRectFlags flags) { |
| 1313 | SkMatrix matrix; |
| 1314 | SkRect bitmapBounds, tmpSrc; |
| 1315 | |
| 1316 | bitmapBounds.set(0, 0, |
| 1317 | SkIntToScalar(bitmap.width()), |
| 1318 | SkIntToScalar(bitmap.height())); |
| 1319 | |
| 1320 | // Compute matrix from the two rectangles |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1321 | if (src) { |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1322 | tmpSrc = *src; |
| 1323 | } else { |
| 1324 | tmpSrc = bitmapBounds; |
| 1325 | } |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1326 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1327 | matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit); |
| 1328 | |
| 1329 | // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null. |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1330 | if (src) { |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1331 | if (!bitmapBounds.contains(tmpSrc)) { |
| 1332 | if (!tmpSrc.intersect(bitmapBounds)) { |
| 1333 | return; // nothing to draw |
| 1334 | } |
| 1335 | } |
| 1336 | } |
| 1337 | |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1338 | SkRect tmpDst; |
| 1339 | matrix.mapRect(&tmpDst, tmpSrc); |
| 1340 | |
| 1341 | SkTCopyOnFirstWrite<SkDraw> draw(origDraw); |
| 1342 | if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) { |
| 1343 | // Translate so that tempDst's top left is at the origin. |
| 1344 | matrix = *origDraw.fMatrix; |
| 1345 | matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop); |
| 1346 | draw.writable()->fMatrix = &matrix; |
| 1347 | } |
| 1348 | SkSize dstSize; |
| 1349 | dstSize.fWidth = tmpDst.width(); |
| 1350 | dstSize.fHeight = tmpDst.height(); |
| 1351 | |
| 1352 | this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1353 | } |
| 1354 | |
| 1355 | void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device, |
| 1356 | int x, int y, const SkPaint& paint) { |
| 1357 | // clear of the source device must occur before CHECK_SHOULD_DRAW |
egdaniel | d78a168 | 2014-07-09 10:41:26 -0700 | [diff] [blame] | 1358 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawDevice", fContext); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1359 | SkGpuDevice* dev = static_cast<SkGpuDevice*>(device); |
kkinnunen | 2e4414e | 2015-02-19 07:20:40 -0800 | [diff] [blame] | 1360 | |
| 1361 | // TODO: If the source device covers the whole of this device, we could |
| 1362 | // omit fNeedsClear -related flushing. |
| 1363 | // TODO: if source needs clear, we could maybe omit the draw fully. |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1364 | |
| 1365 | // drawDevice is defined to be in device coords. |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 1366 | CHECK_SHOULD_DRAW(draw); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1367 | |
| 1368 | GrRenderTarget* devRT = dev->accessRenderTarget(); |
| 1369 | GrTexture* devTex; |
| 1370 | if (NULL == (devTex = devRT->asTexture())) { |
| 1371 | return; |
| 1372 | } |
| 1373 | |
robertphillips | 7b9e8a4 | 2014-12-11 08:20:31 -0800 | [diff] [blame] | 1374 | const SkImageInfo ii = dev->imageInfo(); |
| 1375 | int w = ii.width(); |
| 1376 | int h = ii.height(); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1377 | |
| 1378 | SkImageFilter* filter = paint.getImageFilter(); |
| 1379 | // This bitmap will own the filtered result as a texture. |
| 1380 | SkBitmap filteredBitmap; |
| 1381 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1382 | if (filter) { |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1383 | SkIPoint offset = SkIPoint::Make(0, 0); |
| 1384 | SkMatrix matrix(*draw.fMatrix); |
| 1385 | matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y)); |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 1386 | SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height()); |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 1387 | // This cache is transient, and is freed (along with all its contained |
| 1388 | // textures) when it goes out of scope. |
senorblanco | be129b2 | 2014-08-08 07:14:35 -0700 | [diff] [blame] | 1389 | SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache()); |
commit-bot@chromium.org | f7efa50 | 2014-04-11 18:57:00 +0000 | [diff] [blame] | 1390 | SkImageFilter::Context ctx(matrix, clipBounds, cache); |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 1391 | if (this->filterTexture(fContext, devTex, device->width(), device->height(), |
| 1392 | filter, ctx, &filteredBitmap, &offset)) { |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1393 | devTex = filteredBitmap.getTexture(); |
| 1394 | w = filteredBitmap.width(); |
| 1395 | h = filteredBitmap.height(); |
| 1396 | x += offset.fX; |
| 1397 | y += offset.fY; |
| 1398 | } else { |
| 1399 | return; |
| 1400 | } |
| 1401 | } |
| 1402 | |
| 1403 | GrPaint grPaint; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1404 | grPaint.addColorTextureProcessor(devTex, SkMatrix::I()); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1405 | |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 1406 | if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint, |
| 1407 | SkColor2GrColorJustAlpha(paint.getColor()), false, &grPaint)) { |
| 1408 | return; |
| 1409 | } |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1410 | |
| 1411 | SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x), |
| 1412 | SkIntToScalar(y), |
| 1413 | SkIntToScalar(w), |
| 1414 | SkIntToScalar(h)); |
| 1415 | |
| 1416 | // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate |
| 1417 | // scratch texture). |
| 1418 | SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(), |
| 1419 | SK_Scalar1 * h / devTex->height()); |
| 1420 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1421 | fDrawContext->drawNonAARectToRect(fRenderTarget, fClip, grPaint, SkMatrix::I(), dstRect, |
| 1422 | srcRect); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1423 | } |
| 1424 | |
commit-bot@chromium.org | ae761f7 | 2014-02-05 22:32:02 +0000 | [diff] [blame] | 1425 | bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) { |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1426 | return filter->canFilterImageGPU(); |
| 1427 | } |
| 1428 | |
commit-bot@chromium.org | ae761f7 | 2014-02-05 22:32:02 +0000 | [diff] [blame] | 1429 | bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src, |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 1430 | const SkImageFilter::Context& ctx, |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1431 | SkBitmap* result, SkIPoint* offset) { |
| 1432 | // want explicitly our impl, so guard against a subclass of us overriding it |
| 1433 | if (!this->SkGpuDevice::canHandleImageFilter(filter)) { |
| 1434 | return false; |
| 1435 | } |
| 1436 | |
| 1437 | SkAutoLockPixels alp(src, !src.getTexture()); |
| 1438 | if (!src.getTexture() && !src.readyToDraw()) { |
| 1439 | return false; |
| 1440 | } |
| 1441 | |
| 1442 | GrTexture* texture; |
| 1443 | // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup |
| 1444 | // must be pushed upstack. |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 1445 | AutoBitmapTexture abt(fContext, src, NULL, &texture); |
robertphillips | f83be82 | 2015-04-30 08:55:06 -0700 | [diff] [blame] | 1446 | if (!texture) { |
| 1447 | return false; |
| 1448 | } |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1449 | |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 1450 | return this->filterTexture(fContext, texture, src.width(), src.height(), |
| 1451 | filter, ctx, result, offset); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1452 | } |
| 1453 | |
reed | a85d4d0 | 2015-05-06 12:56:48 -0700 | [diff] [blame] | 1454 | static bool wrap_as_bm(const SkImage* image, SkBitmap* bm) { |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 1455 | GrTexture* tex = as_IB(image)->getTexture(); |
reed | a85d4d0 | 2015-05-06 12:56:48 -0700 | [diff] [blame] | 1456 | if (tex) { |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 1457 | GrWrapTextureInBitmap(tex, image->width(), image->height(), image->isOpaque(), bm); |
| 1458 | return true; |
reed | a85d4d0 | 2015-05-06 12:56:48 -0700 | [diff] [blame] | 1459 | } else { |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 1460 | return as_IB(image)->getROPixels(bm); |
reed | a85d4d0 | 2015-05-06 12:56:48 -0700 | [diff] [blame] | 1461 | } |
reed | a85d4d0 | 2015-05-06 12:56:48 -0700 | [diff] [blame] | 1462 | } |
| 1463 | |
| 1464 | void SkGpuDevice::drawImage(const SkDraw& draw, const SkImage* image, SkScalar x, SkScalar y, |
| 1465 | const SkPaint& paint) { |
| 1466 | SkBitmap bm; |
| 1467 | if (wrap_as_bm(image, &bm)) { |
| 1468 | this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint); |
| 1469 | } |
| 1470 | } |
| 1471 | |
| 1472 | void SkGpuDevice::drawImageRect(const SkDraw& draw, const SkImage* image, const SkRect* src, |
| 1473 | const SkRect& dst, const SkPaint& paint) { |
| 1474 | SkBitmap bm; |
| 1475 | if (wrap_as_bm(image, &bm)) { |
| 1476 | this->drawBitmapRect(draw, bm, src, dst, paint, SkCanvas::kNone_DrawBitmapRectFlag); |
| 1477 | } |
| 1478 | } |
| 1479 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1480 | /////////////////////////////////////////////////////////////////////////////// |
| 1481 | |
| 1482 | // must be in SkCanvas::VertexMode order |
| 1483 | static const GrPrimitiveType gVertexMode2PrimitiveType[] = { |
| 1484 | kTriangles_GrPrimitiveType, |
| 1485 | kTriangleStrip_GrPrimitiveType, |
| 1486 | kTriangleFan_GrPrimitiveType, |
| 1487 | }; |
| 1488 | |
| 1489 | void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode, |
| 1490 | int vertexCount, const SkPoint vertices[], |
| 1491 | const SkPoint texs[], const SkColor colors[], |
| 1492 | SkXfermode* xmode, |
| 1493 | const uint16_t indices[], int indexCount, |
| 1494 | const SkPaint& paint) { |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 1495 | CHECK_SHOULD_DRAW(draw); |
egdaniel | d78a168 | 2014-07-09 10:41:26 -0700 | [diff] [blame] | 1496 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawVertices", fContext); |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 1497 | |
dandov | 32a311b | 2014-07-15 19:46:26 -0700 | [diff] [blame] | 1498 | const uint16_t* outIndices; |
| 1499 | SkAutoTDeleteArray<uint16_t> outAlloc(NULL); |
| 1500 | GrPrimitiveType primType; |
| 1501 | GrPaint grPaint; |
bsalomon | a098dd4 | 2014-08-06 11:01:44 -0700 | [diff] [blame] | 1502 | |
commit-bot@chromium.org | 559a883 | 2014-05-30 10:08:22 +0000 | [diff] [blame] | 1503 | // If both textures and vertex-colors are NULL, strokes hairlines with the paint's color. |
| 1504 | if ((NULL == texs || NULL == paint.getShader()) && NULL == colors) { |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 1505 | |
commit-bot@chromium.org | 559a883 | 2014-05-30 10:08:22 +0000 | [diff] [blame] | 1506 | texs = NULL; |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 1507 | |
commit-bot@chromium.org | 559a883 | 2014-05-30 10:08:22 +0000 | [diff] [blame] | 1508 | SkPaint copy(paint); |
| 1509 | copy.setStyle(SkPaint::kStroke_Style); |
| 1510 | copy.setStrokeWidth(0); |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 1511 | |
dandov | 32a311b | 2014-07-15 19:46:26 -0700 | [diff] [blame] | 1512 | // we ignore the shader if texs is null. |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 1513 | if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, copy, |
| 1514 | SkColor2GrColor(copy.getColor()), NULL == colors, &grPaint)) { |
| 1515 | return; |
| 1516 | } |
commit-bot@chromium.org | 559a883 | 2014-05-30 10:08:22 +0000 | [diff] [blame] | 1517 | |
dandov | 32a311b | 2014-07-15 19:46:26 -0700 | [diff] [blame] | 1518 | primType = kLines_GrPrimitiveType; |
| 1519 | int triangleCount = 0; |
bsalomon | a098dd4 | 2014-08-06 11:01:44 -0700 | [diff] [blame] | 1520 | int n = (NULL == indices) ? vertexCount : indexCount; |
dandov | 32a311b | 2014-07-15 19:46:26 -0700 | [diff] [blame] | 1521 | switch (vmode) { |
| 1522 | case SkCanvas::kTriangles_VertexMode: |
bsalomon | a098dd4 | 2014-08-06 11:01:44 -0700 | [diff] [blame] | 1523 | triangleCount = n / 3; |
dandov | 32a311b | 2014-07-15 19:46:26 -0700 | [diff] [blame] | 1524 | break; |
| 1525 | case SkCanvas::kTriangleStrip_VertexMode: |
| 1526 | case SkCanvas::kTriangleFan_VertexMode: |
bsalomon | a098dd4 | 2014-08-06 11:01:44 -0700 | [diff] [blame] | 1527 | triangleCount = n - 2; |
dandov | 32a311b | 2014-07-15 19:46:26 -0700 | [diff] [blame] | 1528 | break; |
| 1529 | } |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 1530 | |
commit-bot@chromium.org | 559a883 | 2014-05-30 10:08:22 +0000 | [diff] [blame] | 1531 | VertState state(vertexCount, indices, indexCount); |
| 1532 | VertState::Proc vertProc = state.chooseProc(vmode); |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 1533 | |
dandov | 32a311b | 2014-07-15 19:46:26 -0700 | [diff] [blame] | 1534 | //number of indices for lines per triangle with kLines |
| 1535 | indexCount = triangleCount * 6; |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 1536 | |
dandov | 32a311b | 2014-07-15 19:46:26 -0700 | [diff] [blame] | 1537 | outAlloc.reset(SkNEW_ARRAY(uint16_t, indexCount)); |
| 1538 | outIndices = outAlloc.get(); |
| 1539 | uint16_t* auxIndices = outAlloc.get(); |
commit-bot@chromium.org | 559a883 | 2014-05-30 10:08:22 +0000 | [diff] [blame] | 1540 | int i = 0; |
| 1541 | while (vertProc(&state)) { |
dandov | 32a311b | 2014-07-15 19:46:26 -0700 | [diff] [blame] | 1542 | auxIndices[i] = state.f0; |
| 1543 | auxIndices[i + 1] = state.f1; |
| 1544 | auxIndices[i + 2] = state.f1; |
| 1545 | auxIndices[i + 3] = state.f2; |
| 1546 | auxIndices[i + 4] = state.f2; |
| 1547 | auxIndices[i + 5] = state.f0; |
commit-bot@chromium.org | 559a883 | 2014-05-30 10:08:22 +0000 | [diff] [blame] | 1548 | i += 6; |
| 1549 | } |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1550 | } else { |
dandov | 32a311b | 2014-07-15 19:46:26 -0700 | [diff] [blame] | 1551 | outIndices = indices; |
| 1552 | primType = gVertexMode2PrimitiveType[vmode]; |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 1553 | |
dandov | 32a311b | 2014-07-15 19:46:26 -0700 | [diff] [blame] | 1554 | if (NULL == texs || NULL == paint.getShader()) { |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 1555 | if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint, |
| 1556 | SkColor2GrColor(paint.getColor()), |
| 1557 | NULL == colors, &grPaint)) { |
| 1558 | return; |
| 1559 | } |
dandov | 32a311b | 2014-07-15 19:46:26 -0700 | [diff] [blame] | 1560 | } else { |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 1561 | if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, |
| 1562 | NULL == colors, &grPaint)) { |
| 1563 | return; |
| 1564 | } |
dandov | 32a311b | 2014-07-15 19:46:26 -0700 | [diff] [blame] | 1565 | } |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1566 | } |
| 1567 | |
mtklein | 2583b62 | 2014-06-04 08:20:41 -0700 | [diff] [blame] | 1568 | #if 0 |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1569 | if (xmode && texs && colors) { |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1570 | if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) { |
| 1571 | SkDebugf("Unsupported vertex-color/texture xfer mode.\n"); |
mtklein | 2583b62 | 2014-06-04 08:20:41 -0700 | [diff] [blame] | 1572 | return; |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1573 | } |
| 1574 | } |
mtklein | 2583b62 | 2014-06-04 08:20:41 -0700 | [diff] [blame] | 1575 | #endif |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1576 | |
| 1577 | SkAutoSTMalloc<128, GrColor> convertedColors(0); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1578 | if (colors) { |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1579 | // need to convert byte order and from non-PM to PM |
| 1580 | convertedColors.reset(vertexCount); |
commit-bot@chromium.org | c93e681 | 2014-05-23 08:09:26 +0000 | [diff] [blame] | 1581 | SkColor color; |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1582 | for (int i = 0; i < vertexCount; ++i) { |
commit-bot@chromium.org | c93e681 | 2014-05-23 08:09:26 +0000 | [diff] [blame] | 1583 | color = colors[i]; |
| 1584 | if (paint.getAlpha() != 255) { |
| 1585 | color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paint.getAlpha())); |
| 1586 | } |
| 1587 | convertedColors[i] = SkColor2GrColor(color); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1588 | } |
| 1589 | colors = convertedColors.get(); |
| 1590 | } |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1591 | fDrawContext->drawVertices(fRenderTarget, |
| 1592 | fClip, |
| 1593 | grPaint, |
| 1594 | *draw.fMatrix, |
| 1595 | primType, |
| 1596 | vertexCount, |
| 1597 | vertices, |
| 1598 | texs, |
| 1599 | colors, |
| 1600 | outIndices, |
| 1601 | indexCount); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1602 | } |
| 1603 | |
| 1604 | /////////////////////////////////////////////////////////////////////////////// |
| 1605 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1606 | void SkGpuDevice::drawText(const SkDraw& draw, const void* text, |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 1607 | size_t byteLength, SkScalar x, SkScalar y, |
| 1608 | const SkPaint& paint) { |
| 1609 | CHECK_SHOULD_DRAW(draw); |
egdaniel | d78a168 | 2014-07-09 10:41:26 -0700 | [diff] [blame] | 1610 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawText", fContext); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1611 | |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 1612 | GrPaint grPaint; |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 1613 | if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) { |
| 1614 | return; |
| 1615 | } |
commit-bot@chromium.org | 8128d8c | 2013-12-19 16:12:25 +0000 | [diff] [blame] | 1616 | |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 1617 | SkDEBUGCODE(this->validate();) |
commit-bot@chromium.org | 8128d8c | 2013-12-19 16:12:25 +0000 | [diff] [blame] | 1618 | |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 1619 | fDrawContext->drawText(fRenderTarget, fClip, grPaint, paint, *draw.fMatrix, |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame] | 1620 | (const char *)text, byteLength, x, y, draw.fClip->getBounds()); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1621 | } |
| 1622 | |
fmalita | 05c4a43 | 2014-09-29 06:29:53 -0700 | [diff] [blame] | 1623 | void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength, |
| 1624 | const SkScalar pos[], int scalarsPerPos, |
| 1625 | const SkPoint& offset, const SkPaint& paint) { |
egdaniel | bbcb38d | 2014-06-19 10:19:29 -0700 | [diff] [blame] | 1626 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPosText", fContext); |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 1627 | CHECK_SHOULD_DRAW(draw); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1628 | |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 1629 | GrPaint grPaint; |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 1630 | if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) { |
| 1631 | return; |
| 1632 | } |
commit-bot@chromium.org | 8128d8c | 2013-12-19 16:12:25 +0000 | [diff] [blame] | 1633 | |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 1634 | SkDEBUGCODE(this->validate();) |
commit-bot@chromium.org | 8128d8c | 2013-12-19 16:12:25 +0000 | [diff] [blame] | 1635 | |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 1636 | fDrawContext->drawPosText(fRenderTarget, fClip, grPaint, paint, *draw.fMatrix, |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame] | 1637 | (const char *)text, byteLength, pos, scalarsPerPos, offset, |
| 1638 | draw.fClip->getBounds()); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1639 | } |
| 1640 | |
joshualitt | 9c32818 | 2015-03-23 08:13:04 -0700 | [diff] [blame] | 1641 | void SkGpuDevice::drawTextBlob(const SkDraw& draw, const SkTextBlob* blob, SkScalar x, SkScalar y, |
| 1642 | const SkPaint& paint, SkDrawFilter* drawFilter) { |
| 1643 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawTextBlob", fContext); |
| 1644 | CHECK_SHOULD_DRAW(draw); |
| 1645 | |
| 1646 | SkDEBUGCODE(this->validate();) |
| 1647 | |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 1648 | fDrawContext->drawTextBlob(fRenderTarget, fClip, paint, *draw.fMatrix, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 1649 | blob, x, y, drawFilter, draw.fClip->getBounds()); |
joshualitt | 9c32818 | 2015-03-23 08:13:04 -0700 | [diff] [blame] | 1650 | } |
| 1651 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1652 | /////////////////////////////////////////////////////////////////////////////// |
| 1653 | |
reed | b2db898 | 2014-11-13 12:41:02 -0800 | [diff] [blame] | 1654 | bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const { |
robertphillips | 9c240a1 | 2015-05-28 07:45:59 -0700 | [diff] [blame] | 1655 | return GrTextContext::ShouldDisableLCD(paint); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1656 | } |
| 1657 | |
| 1658 | void SkGpuDevice::flush() { |
| 1659 | DO_DEFERRED_CLEAR(); |
bsalomon | 87a94eb | 2014-11-03 14:28:32 -0800 | [diff] [blame] | 1660 | fRenderTarget->prepareForExternalRead(); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1661 | } |
| 1662 | |
| 1663 | /////////////////////////////////////////////////////////////////////////////// |
| 1664 | |
reed | 76033be | 2015-03-14 10:54:31 -0700 | [diff] [blame] | 1665 | SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) { |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 1666 | GrSurfaceDesc desc; |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1667 | desc.fConfig = fRenderTarget->config(); |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 1668 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
fmalita | 6987dca | 2014-11-13 08:33:37 -0800 | [diff] [blame] | 1669 | desc.fWidth = cinfo.fInfo.width(); |
| 1670 | desc.fHeight = cinfo.fInfo.height(); |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 1671 | desc.fSampleCnt = fRenderTarget->desc().fSampleCnt; |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1672 | |
| 1673 | SkAutoTUnref<GrTexture> texture; |
| 1674 | // Skia's convention is to only clear a device if it is non-opaque. |
fmalita | 6987dca | 2014-11-13 08:33:37 -0800 | [diff] [blame] | 1675 | unsigned flags = cinfo.fInfo.isOpaque() ? 0 : kNeedClear_Flag; |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1676 | |
hcm | 4396fa5 | 2014-10-27 21:43:30 -0700 | [diff] [blame] | 1677 | // layers are never draw in repeat modes, so we can request an approx |
| 1678 | // match and ignore any padding. |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 1679 | const GrTextureProvider::ScratchTexMatch match = (kNever_TileUsage == cinfo.fTileUsage) ? |
| 1680 | GrTextureProvider::kApprox_ScratchTexMatch : |
| 1681 | GrTextureProvider::kExact_ScratchTexMatch; |
| 1682 | texture.reset(fContext->textureProvider()->refScratchTexture(desc, match)); |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 1683 | |
| 1684 | if (texture) { |
robertphillips | 7b05ff1 | 2015-06-19 14:14:54 -0700 | [diff] [blame] | 1685 | SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry); |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 1686 | return SkGpuDevice::Create( |
| 1687 | texture->asRenderTarget(), cinfo.fInfo.width(), cinfo.fInfo.height(), &props, flags); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1688 | } else { |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 1689 | SkErrorInternals::SetError( kInternalError_SkError, |
reed | 61f501f | 2015-04-29 08:34:00 -0700 | [diff] [blame] | 1690 | "---- failed to create gpu device texture [%d %d]\n", |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 1691 | cinfo.fInfo.width(), cinfo.fInfo.height()); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1692 | return NULL; |
| 1693 | } |
| 1694 | } |
| 1695 | |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 1696 | SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info, const SkSurfaceProps& props) { |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 1697 | // TODO: Change the signature of newSurface to take a budgeted parameter. |
| 1698 | static const SkSurface::Budgeted kBudgeted = SkSurface::kNo_Budgeted; |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 1699 | return SkSurface::NewRenderTarget(fContext, kBudgeted, info, fRenderTarget->desc().fSampleCnt, |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 1700 | &props); |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 1701 | } |
| 1702 | |
robertphillips | 30d2cc6 | 2014-09-24 08:52:18 -0700 | [diff] [blame] | 1703 | bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* mainCanvas, const SkPicture* mainPicture, |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 1704 | const SkMatrix* matrix, const SkPaint* paint) { |
robertphillips | 63242d7 | 2014-12-04 08:31:02 -0800 | [diff] [blame] | 1705 | #ifndef SK_IGNORE_GPU_LAYER_HOISTING |
robertphillips | 30d7841 | 2014-11-24 09:49:17 -0800 | [diff] [blame] | 1706 | // todo: should handle this natively |
| 1707 | if (paint) { |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 1708 | return false; |
| 1709 | } |
| 1710 | |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 1711 | const SkBigPicture::AccelData* data = NULL; |
| 1712 | if (const SkBigPicture* bp = mainPicture->asSkBigPicture()) { |
| 1713 | data = bp->accelData(); |
| 1714 | } |
robertphillips | 81f71b6 | 2014-11-11 04:54:49 -0800 | [diff] [blame] | 1715 | if (!data) { |
| 1716 | return false; |
| 1717 | } |
| 1718 | |
robertphillips | e5524cd | 2015-02-20 12:30:26 -0800 | [diff] [blame] | 1719 | const SkLayerInfo *gpuData = static_cast<const SkLayerInfo*>(data); |
| 1720 | if (0 == gpuData->numBlocks()) { |
| 1721 | return false; |
commit-bot@chromium.org | 8ddc26b | 2014-03-31 17:55:12 +0000 | [diff] [blame] | 1722 | } |
| 1723 | |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 1724 | SkTDArray<GrHoistedLayer> atlasedNeedRendering, atlasedRecycled; |
robertphillips | 1c4c528 | 2014-09-18 12:03:15 -0700 | [diff] [blame] | 1725 | |
robertphillips | e5524cd | 2015-02-20 12:30:26 -0800 | [diff] [blame] | 1726 | SkIRect iBounds; |
| 1727 | if (!mainCanvas->getClipDeviceBounds(&iBounds)) { |
| 1728 | return false; |
| 1729 | } |
| 1730 | |
| 1731 | SkRect clipBounds = SkRect::Make(iBounds); |
| 1732 | |
| 1733 | SkMatrix initialMatrix = mainCanvas->getTotalMatrix(); |
| 1734 | |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 1735 | GrLayerHoister::FindLayersToAtlas(fContext, mainPicture, |
robertphillips | 30d7841 | 2014-11-24 09:49:17 -0800 | [diff] [blame] | 1736 | initialMatrix, |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 1737 | clipBounds, |
robertphillips | a63f32e | 2014-11-10 08:10:42 -0800 | [diff] [blame] | 1738 | &atlasedNeedRendering, &atlasedRecycled, |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 1739 | fRenderTarget->numColorSamples()); |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 1740 | |
| 1741 | GrLayerHoister::DrawLayersToAtlas(fContext, atlasedNeedRendering); |
| 1742 | |
| 1743 | SkTDArray<GrHoistedLayer> needRendering, recycled; |
| 1744 | |
robertphillips | e5524cd | 2015-02-20 12:30:26 -0800 | [diff] [blame] | 1745 | SkAutoCanvasMatrixPaint acmp(mainCanvas, matrix, paint, mainPicture->cullRect()); |
| 1746 | |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 1747 | GrLayerHoister::FindLayersToHoist(fContext, mainPicture, |
robertphillips | 30d7841 | 2014-11-24 09:49:17 -0800 | [diff] [blame] | 1748 | initialMatrix, |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 1749 | clipBounds, |
robertphillips | a63f32e | 2014-11-10 08:10:42 -0800 | [diff] [blame] | 1750 | &needRendering, &recycled, |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 1751 | fRenderTarget->numColorSamples()); |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 1752 | |
| 1753 | GrLayerHoister::DrawLayers(fContext, needRendering); |
robertphillips@google.com | beb1af2 | 2014-05-07 21:31:09 +0000 | [diff] [blame] | 1754 | |
robertphillips | 64bf767 | 2014-08-21 13:07:35 -0700 | [diff] [blame] | 1755 | // Render the entire picture using new layers |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame] | 1756 | GrRecordReplaceDraw(mainPicture, mainCanvas, fContext->getLayerCache(), |
| 1757 | initialMatrix, NULL); |
robertphillips | 64bf767 | 2014-08-21 13:07:35 -0700 | [diff] [blame] | 1758 | |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 1759 | GrLayerHoister::UnlockLayers(fContext, needRendering); |
| 1760 | GrLayerHoister::UnlockLayers(fContext, recycled); |
| 1761 | GrLayerHoister::UnlockLayers(fContext, atlasedNeedRendering); |
| 1762 | GrLayerHoister::UnlockLayers(fContext, atlasedRecycled); |
robertphillips | 64bf767 | 2014-08-21 13:07:35 -0700 | [diff] [blame] | 1763 | |
| 1764 | return true; |
robertphillips | 63242d7 | 2014-12-04 08:31:02 -0800 | [diff] [blame] | 1765 | #else |
| 1766 | return false; |
| 1767 | #endif |
robertphillips | 64bf767 | 2014-08-21 13:07:35 -0700 | [diff] [blame] | 1768 | } |
| 1769 | |
senorblanco | be129b2 | 2014-08-08 07:14:35 -0700 | [diff] [blame] | 1770 | SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() { |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 1771 | // We always return a transient cache, so it is freed after each |
| 1772 | // filter traversal. |
senorblanco | be129b2 | 2014-08-08 07:14:35 -0700 | [diff] [blame] | 1773 | return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize); |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 1774 | } |
reed | f037e0b | 2014-10-30 11:34:15 -0700 | [diff] [blame] | 1775 | |
| 1776 | #endif |