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