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 | |
commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 10 | #include "effects/GrBicubicEffect.h" |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 11 | #include "effects/GrDashingEffect.h" |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 12 | #include "effects/GrTextureDomain.h" |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 13 | #include "effects/GrSimpleTextureEffect.h" |
| 14 | |
| 15 | #include "GrContext.h" |
| 16 | #include "GrBitmapTextContext.h" |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 17 | #include "GrDistanceFieldTextContext.h" |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 18 | #include "GrLayerCache.h" |
commit-bot@chromium.org | 8ddc26b | 2014-03-31 17:55:12 +0000 | [diff] [blame] | 19 | #include "GrPictureUtils.h" |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 20 | #include "GrStrokeInfo.h" |
egdaniel | bbcb38d | 2014-06-19 10:19:29 -0700 | [diff] [blame] | 21 | #include "GrTracing.h" |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 22 | |
| 23 | #include "SkGrTexturePixelRef.h" |
| 24 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 25 | #include "SkDeviceImageFilterProxy.h" |
| 26 | #include "SkDrawProcs.h" |
| 27 | #include "SkGlyphCache.h" |
| 28 | #include "SkImageFilter.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" |
robertphillips | 1ad00e4 | 2014-07-08 08:28:08 -0700 | [diff] [blame] | 33 | #include "SkPictureRangePlayback.h" |
robertphillips | c26d991 | 2014-07-10 07:21:27 -0700 | [diff] [blame] | 34 | #include "SkPictureReplacementPlayback.h" |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 35 | #include "SkRRect.h" |
| 36 | #include "SkStroke.h" |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 37 | #include "SkSurface.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" |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 41 | #include "SkErrorInternals.h" |
| 42 | |
| 43 | #define CACHE_COMPATIBLE_DEVICE_TEXTURES 1 |
| 44 | |
| 45 | #if 0 |
| 46 | extern bool (*gShouldDrawProc)(); |
| 47 | #define CHECK_SHOULD_DRAW(draw, forceI) \ |
| 48 | do { \ |
| 49 | if (gShouldDrawProc && !gShouldDrawProc()) return; \ |
| 50 | this->prepareDraw(draw, forceI); \ |
| 51 | } while (0) |
| 52 | #else |
| 53 | #define CHECK_SHOULD_DRAW(draw, forceI) this->prepareDraw(draw, forceI) |
| 54 | #endif |
| 55 | |
| 56 | // This constant represents the screen alignment criterion in texels for |
| 57 | // requiring texture domain clamping to prevent color bleeding when drawing |
| 58 | // a sub region of a larger source image. |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 59 | #define COLOR_BLEED_TOLERANCE 0.001f |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 60 | |
| 61 | #define DO_DEFERRED_CLEAR() \ |
| 62 | do { \ |
| 63 | if (fNeedClear) { \ |
| 64 | this->clear(SK_ColorTRANSPARENT); \ |
| 65 | } \ |
| 66 | } while (false) \ |
| 67 | |
| 68 | /////////////////////////////////////////////////////////////////////////////// |
| 69 | |
| 70 | #define CHECK_FOR_ANNOTATION(paint) \ |
| 71 | do { if (paint.getAnnotation()) { return; } } while (0) |
| 72 | |
| 73 | /////////////////////////////////////////////////////////////////////////////// |
| 74 | |
| 75 | |
| 76 | class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable { |
| 77 | public: |
| 78 | SkAutoCachedTexture() |
| 79 | : fDevice(NULL) |
| 80 | , fTexture(NULL) { |
| 81 | } |
| 82 | |
| 83 | SkAutoCachedTexture(SkGpuDevice* device, |
| 84 | const SkBitmap& bitmap, |
| 85 | const GrTextureParams* params, |
| 86 | GrTexture** texture) |
| 87 | : fDevice(NULL) |
| 88 | , fTexture(NULL) { |
| 89 | SkASSERT(NULL != texture); |
| 90 | *texture = this->set(device, bitmap, params); |
| 91 | } |
| 92 | |
| 93 | ~SkAutoCachedTexture() { |
| 94 | if (NULL != fTexture) { |
| 95 | GrUnlockAndUnrefCachedBitmapTexture(fTexture); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | GrTexture* set(SkGpuDevice* device, |
| 100 | const SkBitmap& bitmap, |
| 101 | const GrTextureParams* params) { |
| 102 | if (NULL != fTexture) { |
| 103 | GrUnlockAndUnrefCachedBitmapTexture(fTexture); |
| 104 | fTexture = NULL; |
| 105 | } |
| 106 | fDevice = device; |
| 107 | GrTexture* result = (GrTexture*)bitmap.getTexture(); |
| 108 | if (NULL == result) { |
| 109 | // Cannot return the native texture so look it up in our cache |
| 110 | fTexture = GrLockAndRefCachedBitmapTexture(device->context(), bitmap, params); |
| 111 | result = fTexture; |
| 112 | } |
| 113 | return result; |
| 114 | } |
| 115 | |
| 116 | private: |
| 117 | SkGpuDevice* fDevice; |
| 118 | GrTexture* fTexture; |
| 119 | }; |
| 120 | |
| 121 | /////////////////////////////////////////////////////////////////////////////// |
| 122 | |
| 123 | struct GrSkDrawProcs : public SkDrawProcs { |
| 124 | public: |
| 125 | GrContext* fContext; |
| 126 | GrTextContext* fTextContext; |
| 127 | GrFontScaler* fFontScaler; // cached in the skia glyphcache |
| 128 | }; |
| 129 | |
| 130 | /////////////////////////////////////////////////////////////////////////////// |
| 131 | |
commit-bot@chromium.org | d8a57af | 2014-03-19 21:19:16 +0000 | [diff] [blame] | 132 | SkGpuDevice* SkGpuDevice::Create(GrSurface* surface, unsigned flags) { |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 133 | SkASSERT(NULL != surface); |
| 134 | if (NULL == surface->asRenderTarget() || NULL == surface->getContext()) { |
| 135 | return NULL; |
| 136 | } |
| 137 | if (surface->asTexture()) { |
commit-bot@chromium.org | d8a57af | 2014-03-19 21:19:16 +0000 | [diff] [blame] | 138 | return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asTexture(), flags)); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 139 | } else { |
commit-bot@chromium.org | d8a57af | 2014-03-19 21:19:16 +0000 | [diff] [blame] | 140 | return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asRenderTarget(), flags)); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
reed | 89443ab | 2014-06-27 11:34:19 -0700 | [diff] [blame] | 144 | SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture, unsigned flags) { |
commit-bot@chromium.org | d8a57af | 2014-03-19 21:19:16 +0000 | [diff] [blame] | 145 | this->initFromRenderTarget(context, texture->asRenderTarget(), flags); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 146 | } |
| 147 | |
reed | 89443ab | 2014-06-27 11:34:19 -0700 | [diff] [blame] | 148 | SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget, unsigned flags) { |
commit-bot@chromium.org | d8a57af | 2014-03-19 21:19:16 +0000 | [diff] [blame] | 149 | this->initFromRenderTarget(context, renderTarget, flags); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | void SkGpuDevice::initFromRenderTarget(GrContext* context, |
| 153 | GrRenderTarget* renderTarget, |
commit-bot@chromium.org | d8a57af | 2014-03-19 21:19:16 +0000 | [diff] [blame] | 154 | unsigned flags) { |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 155 | fDrawProcs = NULL; |
| 156 | |
| 157 | fContext = context; |
| 158 | fContext->ref(); |
| 159 | |
| 160 | fRenderTarget = NULL; |
commit-bot@chromium.org | d8a57af | 2014-03-19 21:19:16 +0000 | [diff] [blame] | 161 | fNeedClear = flags & kNeedClear_Flag; |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 162 | |
| 163 | SkASSERT(NULL != renderTarget); |
| 164 | fRenderTarget = renderTarget; |
| 165 | fRenderTarget->ref(); |
| 166 | |
| 167 | // Hold onto to the texture in the pixel ref (if there is one) because the texture holds a ref |
| 168 | // on the RT but not vice-versa. |
| 169 | // TODO: Remove this trickery once we figure out how to make SkGrPixelRef do this without |
| 170 | // busting chrome (for a currently unknown reason). |
| 171 | GrSurface* surface = fRenderTarget->asTexture(); |
| 172 | if (NULL == surface) { |
| 173 | surface = fRenderTarget; |
| 174 | } |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 175 | |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 176 | SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, |
| 177 | (surface->info(), surface, SkToBool(flags & kCached_Flag))); |
reed | 89443ab | 2014-06-27 11:34:19 -0700 | [diff] [blame] | 178 | fLegacyBitmap.setInfo(surface->info()); |
| 179 | fLegacyBitmap.setPixelRef(pr)->unref(); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 180 | |
| 181 | bool useDFFonts = !!(flags & kDFFonts_Flag); |
| 182 | fMainTextContext = fContext->createTextContext(fRenderTarget, fLeakyProperties, useDFFonts); |
| 183 | fFallbackTextContext = SkNEW_ARGS(GrBitmapTextContext, (fContext, fLeakyProperties)); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 184 | } |
| 185 | |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 186 | SkGpuDevice* SkGpuDevice::Create(GrContext* context, const SkImageInfo& origInfo, |
| 187 | int sampleCount) { |
| 188 | if (kUnknown_SkColorType == origInfo.colorType() || |
| 189 | origInfo.width() < 0 || origInfo.height() < 0) { |
| 190 | return NULL; |
| 191 | } |
| 192 | |
| 193 | SkImageInfo info = origInfo; |
| 194 | // TODO: perhas we can loosen this check now that colortype is more detailed |
| 195 | // e.g. can we support both RGBA and BGRA here? |
| 196 | if (kRGB_565_SkColorType == info.colorType()) { |
| 197 | info.fAlphaType = kOpaque_SkAlphaType; // force this setting |
| 198 | } else { |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 199 | info.fColorType = kN32_SkColorType; |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 200 | if (kOpaque_SkAlphaType != info.alphaType()) { |
| 201 | info.fAlphaType = kPremul_SkAlphaType; // force this setting |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | GrTextureDesc desc; |
| 206 | desc.fFlags = kRenderTarget_GrTextureFlagBit; |
| 207 | desc.fWidth = info.width(); |
| 208 | desc.fHeight = info.height(); |
commit-bot@chromium.org | 3adcc34 | 2014-04-23 19:18:09 +0000 | [diff] [blame] | 209 | desc.fConfig = SkImageInfo2GrPixelConfig(info); |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 210 | desc.fSampleCnt = sampleCount; |
| 211 | |
| 212 | SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0)); |
| 213 | if (!texture.get()) { |
| 214 | return NULL; |
| 215 | } |
skia.committer@gmail.com | 969588f | 2014-02-16 03:01:56 +0000 | [diff] [blame] | 216 | |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 217 | return SkNEW_ARGS(SkGpuDevice, (context, texture.get())); |
| 218 | } |
| 219 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 220 | SkGpuDevice::~SkGpuDevice() { |
| 221 | if (fDrawProcs) { |
| 222 | delete fDrawProcs; |
| 223 | } |
skia.committer@gmail.com | d2ac07b | 2014-01-25 07:01:49 +0000 | [diff] [blame] | 224 | |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 225 | delete fMainTextContext; |
| 226 | delete fFallbackTextContext; |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 227 | |
| 228 | // The GrContext takes a ref on the target. We don't want to cause the render |
| 229 | // target to be unnecessarily kept alive. |
| 230 | if (fContext->getRenderTarget() == fRenderTarget) { |
| 231 | fContext->setRenderTarget(NULL); |
| 232 | } |
| 233 | |
| 234 | if (fContext->getClip() == &fClipData) { |
| 235 | fContext->setClip(NULL); |
| 236 | } |
| 237 | |
| 238 | SkSafeUnref(fRenderTarget); |
| 239 | fContext->unref(); |
| 240 | } |
| 241 | |
| 242 | /////////////////////////////////////////////////////////////////////////////// |
| 243 | |
| 244 | void SkGpuDevice::makeRenderTargetCurrent() { |
| 245 | DO_DEFERRED_CLEAR(); |
| 246 | fContext->setRenderTarget(fRenderTarget); |
| 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 | } |
| 265 | return fContext->readRenderTargetPixels(fRenderTarget, x, y, dstInfo.width(), dstInfo.height(), |
| 266 | config, dstPixels, dstRowBytes, flags); |
| 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 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 293 | void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) { |
| 294 | INHERITED::onAttachToCanvas(canvas); |
| 295 | |
| 296 | // Canvas promises that this ptr is valid until onDetachFromCanvas is called |
| 297 | fClipData.fClipStack = canvas->getClipStack(); |
| 298 | } |
| 299 | |
| 300 | void SkGpuDevice::onDetachFromCanvas() { |
| 301 | INHERITED::onDetachFromCanvas(); |
| 302 | fClipData.fClipStack = NULL; |
| 303 | } |
| 304 | |
| 305 | // call this every draw call, to ensure that the context reflects our state, |
| 306 | // and not the state from some other canvas/device |
| 307 | void SkGpuDevice::prepareDraw(const SkDraw& draw, bool forceIdentity) { |
| 308 | SkASSERT(NULL != fClipData.fClipStack); |
| 309 | |
| 310 | fContext->setRenderTarget(fRenderTarget); |
| 311 | |
| 312 | SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack); |
| 313 | |
| 314 | if (forceIdentity) { |
| 315 | fContext->setIdentityMatrix(); |
| 316 | } else { |
| 317 | fContext->setMatrix(*draw.fMatrix); |
| 318 | } |
| 319 | fClipData.fOrigin = this->getOrigin(); |
| 320 | |
| 321 | fContext->setClip(&fClipData); |
| 322 | |
| 323 | DO_DEFERRED_CLEAR(); |
| 324 | } |
| 325 | |
| 326 | GrRenderTarget* SkGpuDevice::accessRenderTarget() { |
| 327 | DO_DEFERRED_CLEAR(); |
| 328 | return fRenderTarget; |
| 329 | } |
| 330 | |
| 331 | /////////////////////////////////////////////////////////////////////////////// |
| 332 | |
| 333 | SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch); |
| 334 | SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch); |
| 335 | SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch); |
| 336 | SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch); |
| 337 | SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4, |
| 338 | shader_type_mismatch); |
| 339 | SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5, |
| 340 | shader_type_mismatch); |
| 341 | SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch); |
| 342 | SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch); |
| 343 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 344 | /////////////////////////////////////////////////////////////////////////////// |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 345 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 346 | void SkGpuDevice::clear(SkColor color) { |
egdaniel | d78a168 | 2014-07-09 10:41:26 -0700 | [diff] [blame] | 347 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::clear", fContext); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 348 | SkIRect rect = SkIRect::MakeWH(this->width(), this->height()); |
| 349 | fContext->clear(&rect, SkColor2GrColor(color), true, fRenderTarget); |
| 350 | fNeedClear = false; |
| 351 | } |
| 352 | |
| 353 | void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) { |
| 354 | CHECK_SHOULD_DRAW(draw, false); |
egdaniel | d78a168 | 2014-07-09 10:41:26 -0700 | [diff] [blame] | 355 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPaint", fContext); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 356 | |
| 357 | GrPaint grPaint; |
commit-bot@chromium.org | 3595f88 | 2014-05-19 19:35:57 +0000 | [diff] [blame] | 358 | SkPaint2GrPaintShader(this->context(), paint, true, &grPaint); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 359 | |
| 360 | fContext->drawPaint(grPaint); |
| 361 | } |
| 362 | |
| 363 | // must be in SkCanvas::PointMode order |
| 364 | static const GrPrimitiveType gPointMode2PrimtiveType[] = { |
| 365 | kPoints_GrPrimitiveType, |
| 366 | kLines_GrPrimitiveType, |
| 367 | kLineStrip_GrPrimitiveType |
| 368 | }; |
| 369 | |
| 370 | void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode, |
| 371 | size_t count, const SkPoint pts[], const SkPaint& paint) { |
| 372 | CHECK_FOR_ANNOTATION(paint); |
| 373 | CHECK_SHOULD_DRAW(draw, false); |
| 374 | |
| 375 | SkScalar width = paint.getStrokeWidth(); |
| 376 | if (width < 0) { |
| 377 | return; |
| 378 | } |
| 379 | |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 380 | if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) { |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 381 | GrStrokeInfo strokeInfo(paint, SkPaint::kStroke_Style); |
| 382 | GrPaint grPaint; |
| 383 | SkPaint2GrPaintShader(this->context(), paint, true, &grPaint); |
| 384 | SkPath path; |
| 385 | path.moveTo(pts[0]); |
| 386 | path.lineTo(pts[1]); |
| 387 | fContext->drawPath(grPaint, path, strokeInfo); |
| 388 | return; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 389 | } |
| 390 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 391 | // we only handle hairlines and paints without path effects or mask filters, |
| 392 | // else we let the SkDraw call our drawPath() |
| 393 | if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) { |
| 394 | draw.drawPoints(mode, count, pts, paint, true); |
| 395 | return; |
| 396 | } |
| 397 | |
| 398 | GrPaint grPaint; |
commit-bot@chromium.org | 3595f88 | 2014-05-19 19:35:57 +0000 | [diff] [blame] | 399 | SkPaint2GrPaintShader(this->context(), paint, true, &grPaint); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 400 | |
| 401 | fContext->drawVertices(grPaint, |
| 402 | gPointMode2PrimtiveType[mode], |
robertphillips@google.com | a466286 | 2013-11-21 14:24:16 +0000 | [diff] [blame] | 403 | SkToS32(count), |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 404 | (SkPoint*)pts, |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 405 | NULL, |
| 406 | NULL, |
| 407 | NULL, |
| 408 | 0); |
| 409 | } |
| 410 | |
| 411 | /////////////////////////////////////////////////////////////////////////////// |
| 412 | |
| 413 | void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect, |
| 414 | const SkPaint& paint) { |
egdaniel | bbcb38d | 2014-06-19 10:19:29 -0700 | [diff] [blame] | 415 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRect", fContext); |
| 416 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 417 | CHECK_FOR_ANNOTATION(paint); |
| 418 | CHECK_SHOULD_DRAW(draw, false); |
| 419 | |
| 420 | bool doStroke = paint.getStyle() != SkPaint::kFill_Style; |
| 421 | SkScalar width = paint.getStrokeWidth(); |
| 422 | |
| 423 | /* |
| 424 | We have special code for hairline strokes, miter-strokes, bevel-stroke |
| 425 | and fills. Anything else we just call our path code. |
| 426 | */ |
| 427 | bool usePath = doStroke && width > 0 && |
| 428 | (paint.getStrokeJoin() == SkPaint::kRound_Join || |
| 429 | (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty())); |
| 430 | // another two reasons we might need to call drawPath... |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 431 | |
| 432 | if (paint.getMaskFilter()) { |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 433 | usePath = true; |
| 434 | } |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 435 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 436 | if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) { |
| 437 | #if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT) |
| 438 | if (doStroke) { |
| 439 | #endif |
| 440 | usePath = true; |
| 441 | #if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT) |
| 442 | } else { |
| 443 | usePath = !fContext->getMatrix().preservesRightAngles(); |
| 444 | } |
| 445 | #endif |
| 446 | } |
| 447 | // until we can both stroke and fill rectangles |
| 448 | if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) { |
| 449 | usePath = true; |
| 450 | } |
| 451 | |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 452 | GrStrokeInfo strokeInfo(paint); |
| 453 | |
| 454 | const SkPathEffect* pe = paint.getPathEffect(); |
| 455 | if (!usePath && NULL != pe && !strokeInfo.isDashed()) { |
| 456 | usePath = true; |
| 457 | } |
| 458 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 459 | if (usePath) { |
| 460 | SkPath path; |
| 461 | path.addRect(rect); |
| 462 | this->drawPath(draw, path, paint, NULL, true); |
| 463 | return; |
| 464 | } |
| 465 | |
| 466 | GrPaint grPaint; |
commit-bot@chromium.org | 3595f88 | 2014-05-19 19:35:57 +0000 | [diff] [blame] | 467 | SkPaint2GrPaintShader(this->context(), paint, true, &grPaint); |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 468 | |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 469 | fContext->drawRect(grPaint, rect, &strokeInfo); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | /////////////////////////////////////////////////////////////////////////////// |
| 473 | |
| 474 | void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect, |
| 475 | const SkPaint& paint) { |
egdaniel | d78a168 | 2014-07-09 10:41:26 -0700 | [diff] [blame] | 476 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRRect", fContext); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 477 | CHECK_FOR_ANNOTATION(paint); |
| 478 | CHECK_SHOULD_DRAW(draw, false); |
| 479 | |
commit-bot@chromium.org | 8213970 | 2014-03-10 22:53:20 +0000 | [diff] [blame] | 480 | GrPaint grPaint; |
commit-bot@chromium.org | 3595f88 | 2014-05-19 19:35:57 +0000 | [diff] [blame] | 481 | SkPaint2GrPaintShader(this->context(), paint, true, &grPaint); |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 482 | |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 483 | GrStrokeInfo strokeInfo(paint); |
commit-bot@chromium.org | 8213970 | 2014-03-10 22:53:20 +0000 | [diff] [blame] | 484 | if (paint.getMaskFilter()) { |
| 485 | // try to hit the fast path for drawing filtered round rects |
| 486 | |
| 487 | SkRRect devRRect; |
| 488 | if (rect.transform(fContext->getMatrix(), &devRRect)) { |
| 489 | if (devRRect.allCornersCircular()) { |
| 490 | SkRect maskRect; |
| 491 | if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(), |
| 492 | draw.fClip->getBounds(), |
| 493 | fContext->getMatrix(), |
| 494 | &maskRect)) { |
| 495 | SkIRect finalIRect; |
| 496 | maskRect.roundOut(&finalIRect); |
| 497 | if (draw.fClip->quickReject(finalIRect)) { |
| 498 | // clipped out |
| 499 | return; |
| 500 | } |
commit-bot@chromium.org | 8213970 | 2014-03-10 22:53:20 +0000 | [diff] [blame] | 501 | if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext, &grPaint, |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 502 | strokeInfo.getStrokeRec(), |
| 503 | devRRect)) { |
commit-bot@chromium.org | 8213970 | 2014-03-10 22:53:20 +0000 | [diff] [blame] | 504 | return; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | } |
| 512 | |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 513 | bool usePath = false; |
| 514 | |
| 515 | if (paint.getMaskFilter()) { |
| 516 | usePath = true; |
| 517 | } else { |
| 518 | const SkPathEffect* pe = paint.getPathEffect(); |
| 519 | if (NULL != pe && !strokeInfo.isDashed()) { |
| 520 | usePath = true; |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | |
| 525 | if (usePath) { |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 526 | SkPath path; |
| 527 | path.addRRect(rect); |
| 528 | this->drawPath(draw, path, paint, NULL, true); |
| 529 | return; |
| 530 | } |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 531 | |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 532 | fContext->drawRRect(grPaint, rect, strokeInfo); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 533 | } |
| 534 | |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 535 | void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer, |
| 536 | const SkRRect& inner, const SkPaint& paint) { |
| 537 | SkStrokeRec stroke(paint); |
| 538 | if (stroke.isFillStyle()) { |
| 539 | |
| 540 | CHECK_FOR_ANNOTATION(paint); |
| 541 | CHECK_SHOULD_DRAW(draw, false); |
| 542 | |
| 543 | GrPaint grPaint; |
commit-bot@chromium.org | 3595f88 | 2014-05-19 19:35:57 +0000 | [diff] [blame] | 544 | SkPaint2GrPaintShader(this->context(), paint, true, &grPaint); |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 545 | |
| 546 | if (NULL == paint.getMaskFilter() && NULL == paint.getPathEffect()) { |
| 547 | fContext->drawDRRect(grPaint, outer, inner); |
| 548 | return; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | SkPath path; |
| 553 | path.addRRect(outer); |
| 554 | path.addRRect(inner); |
| 555 | path.setFillType(SkPath::kEvenOdd_FillType); |
| 556 | |
| 557 | this->drawPath(draw, path, paint, NULL, true); |
| 558 | } |
| 559 | |
| 560 | |
commit-bot@chromium.org | 8213970 | 2014-03-10 22:53:20 +0000 | [diff] [blame] | 561 | ///////////////////////////////////////////////////////////////////////////// |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 562 | |
| 563 | void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval, |
| 564 | const SkPaint& paint) { |
egdaniel | d78a168 | 2014-07-09 10:41:26 -0700 | [diff] [blame] | 565 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawOval", fContext); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 566 | CHECK_FOR_ANNOTATION(paint); |
| 567 | CHECK_SHOULD_DRAW(draw, false); |
| 568 | |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 569 | GrStrokeInfo strokeInfo(paint); |
| 570 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 571 | bool usePath = false; |
| 572 | // some basic reasons we might need to call drawPath... |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 573 | if (paint.getMaskFilter()) { |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 574 | usePath = true; |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 575 | } else { |
| 576 | const SkPathEffect* pe = paint.getPathEffect(); |
| 577 | if (NULL != pe && !strokeInfo.isDashed()) { |
| 578 | usePath = true; |
| 579 | } |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | if (usePath) { |
| 583 | SkPath path; |
| 584 | path.addOval(oval); |
| 585 | this->drawPath(draw, path, paint, NULL, true); |
| 586 | return; |
| 587 | } |
| 588 | |
| 589 | GrPaint grPaint; |
commit-bot@chromium.org | 3595f88 | 2014-05-19 19:35:57 +0000 | [diff] [blame] | 590 | SkPaint2GrPaintShader(this->context(), paint, true, &grPaint); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 591 | |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 592 | fContext->drawOval(grPaint, oval, strokeInfo); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | #include "SkMaskFilter.h" |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 596 | |
| 597 | /////////////////////////////////////////////////////////////////////////////// |
| 598 | |
| 599 | // helpers for applying mask filters |
| 600 | namespace { |
| 601 | |
| 602 | // Draw a mask using the supplied paint. Since the coverage/geometry |
| 603 | // is already burnt into the mask this boils down to a rect draw. |
| 604 | // Return true if the mask was successfully drawn. |
| 605 | bool draw_mask(GrContext* context, const SkRect& maskRect, |
| 606 | GrPaint* grp, GrTexture* mask) { |
| 607 | GrContext::AutoMatrix am; |
| 608 | if (!am.setIdentity(context, grp)) { |
| 609 | return false; |
| 610 | } |
| 611 | |
| 612 | SkMatrix matrix; |
| 613 | matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop); |
| 614 | matrix.postIDiv(mask->width(), mask->height()); |
| 615 | |
| 616 | grp->addCoverageEffect(GrSimpleTextureEffect::Create(mask, matrix))->unref(); |
| 617 | context->drawRect(*grp, maskRect); |
| 618 | return true; |
| 619 | } |
| 620 | |
| 621 | bool draw_with_mask_filter(GrContext* context, const SkPath& devPath, |
reed | 868074b | 2014-06-03 10:53:59 -0700 | [diff] [blame] | 622 | SkMaskFilter* filter, const SkRegion& clip, |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 623 | GrPaint* grp, SkPaint::Style style) { |
| 624 | SkMask srcM, dstM; |
| 625 | |
| 626 | if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM, |
| 627 | SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) { |
| 628 | return false; |
| 629 | } |
| 630 | SkAutoMaskFreeImage autoSrc(srcM.fImage); |
| 631 | |
| 632 | if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) { |
| 633 | return false; |
| 634 | } |
| 635 | // this will free-up dstM when we're done (allocated in filterMask()) |
| 636 | SkAutoMaskFreeImage autoDst(dstM.fImage); |
| 637 | |
| 638 | if (clip.quickReject(dstM.fBounds)) { |
| 639 | return false; |
| 640 | } |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 641 | |
| 642 | // we now have a device-aligned 8bit mask in dstM, ready to be drawn using |
| 643 | // the current clip (and identity matrix) and GrPaint settings |
| 644 | GrTextureDesc desc; |
| 645 | desc.fWidth = dstM.fBounds.width(); |
| 646 | desc.fHeight = dstM.fBounds.height(); |
| 647 | desc.fConfig = kAlpha_8_GrPixelConfig; |
| 648 | |
| 649 | GrAutoScratchTexture ast(context, desc); |
| 650 | GrTexture* texture = ast.texture(); |
| 651 | |
| 652 | if (NULL == texture) { |
| 653 | return false; |
| 654 | } |
| 655 | texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, |
| 656 | dstM.fImage, dstM.fRowBytes); |
| 657 | |
| 658 | SkRect maskRect = SkRect::Make(dstM.fBounds); |
| 659 | |
| 660 | return draw_mask(context, maskRect, grp, texture); |
| 661 | } |
| 662 | |
| 663 | // Create a mask of 'devPath' and place the result in 'mask'. Return true on |
| 664 | // success; false otherwise. |
| 665 | bool create_mask_GPU(GrContext* context, |
| 666 | const SkRect& maskRect, |
| 667 | const SkPath& devPath, |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 668 | const GrStrokeInfo& strokeInfo, |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 669 | bool doAA, |
| 670 | GrAutoScratchTexture* mask) { |
| 671 | GrTextureDesc desc; |
| 672 | desc.fFlags = kRenderTarget_GrTextureFlagBit; |
| 673 | desc.fWidth = SkScalarCeilToInt(maskRect.width()); |
| 674 | desc.fHeight = SkScalarCeilToInt(maskRect.height()); |
| 675 | // We actually only need A8, but it often isn't supported as a |
| 676 | // render target so default to RGBA_8888 |
| 677 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 678 | if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) { |
| 679 | desc.fConfig = kAlpha_8_GrPixelConfig; |
| 680 | } |
| 681 | |
| 682 | mask->set(context, desc); |
| 683 | if (NULL == mask->texture()) { |
| 684 | return false; |
| 685 | } |
| 686 | |
| 687 | GrTexture* maskTexture = mask->texture(); |
| 688 | SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height()); |
| 689 | |
| 690 | GrContext::AutoRenderTarget art(context, maskTexture->asRenderTarget()); |
| 691 | GrContext::AutoClip ac(context, clipRect); |
| 692 | |
| 693 | context->clear(NULL, 0x0, true); |
| 694 | |
| 695 | GrPaint tempPaint; |
| 696 | if (doAA) { |
| 697 | tempPaint.setAntiAlias(true); |
| 698 | // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst |
| 699 | // blend coeff of zero requires dual source blending support in order |
| 700 | // to properly blend partially covered pixels. This means the AA |
| 701 | // code path may not be taken. So we use a dst blend coeff of ISA. We |
| 702 | // could special case AA draws to a dst surface with known alpha=0 to |
| 703 | // use a zero dst coeff when dual source blending isn't available. |
| 704 | tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff); |
| 705 | } |
| 706 | |
| 707 | GrContext::AutoMatrix am; |
| 708 | |
| 709 | // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint. |
| 710 | SkMatrix translate; |
| 711 | translate.setTranslate(-maskRect.fLeft, -maskRect.fTop); |
| 712 | am.set(context, translate); |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 713 | context->drawPath(tempPaint, devPath, strokeInfo); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 714 | return true; |
| 715 | } |
| 716 | |
| 717 | SkBitmap wrap_texture(GrTexture* texture) { |
| 718 | SkBitmap result; |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 719 | result.setInfo(texture->info()); |
| 720 | result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (result.info(), texture)))->unref(); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 721 | return result; |
| 722 | } |
| 723 | |
| 724 | }; |
| 725 | |
| 726 | void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath, |
| 727 | const SkPaint& paint, const SkMatrix* prePathMatrix, |
| 728 | bool pathIsMutable) { |
| 729 | CHECK_FOR_ANNOTATION(paint); |
| 730 | CHECK_SHOULD_DRAW(draw, false); |
egdaniel | d78a168 | 2014-07-09 10:41:26 -0700 | [diff] [blame] | 731 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPath", fContext); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 732 | |
| 733 | GrPaint grPaint; |
commit-bot@chromium.org | 3595f88 | 2014-05-19 19:35:57 +0000 | [diff] [blame] | 734 | SkPaint2GrPaintShader(this->context(), paint, true, &grPaint); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 735 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 736 | // If we have a prematrix, apply it to the path, optimizing for the case |
| 737 | // where the original path can in fact be modified in place (even though |
| 738 | // its parameter type is const). |
| 739 | SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath); |
commit-bot@chromium.org | f0c41e2 | 2014-01-14 18:42:34 +0000 | [diff] [blame] | 740 | SkTLazy<SkPath> tmpPath; |
| 741 | SkTLazy<SkPath> effectPath; |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 742 | |
| 743 | if (prePathMatrix) { |
| 744 | SkPath* result = pathPtr; |
| 745 | |
| 746 | if (!pathIsMutable) { |
commit-bot@chromium.org | f0c41e2 | 2014-01-14 18:42:34 +0000 | [diff] [blame] | 747 | result = tmpPath.init(); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 748 | pathIsMutable = true; |
| 749 | } |
| 750 | // should I push prePathMatrix on our MV stack temporarily, instead |
| 751 | // of applying it here? See SkDraw.cpp |
| 752 | pathPtr->transform(*prePathMatrix, result); |
| 753 | pathPtr = result; |
| 754 | } |
| 755 | // at this point we're done with prePathMatrix |
| 756 | SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;) |
| 757 | |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 758 | GrStrokeInfo strokeInfo(paint); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 759 | SkPathEffect* pathEffect = paint.getPathEffect(); |
| 760 | const SkRect* cullRect = NULL; // TODO: what is our bounds? |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 761 | SkStrokeRec* strokePtr = strokeInfo.getStrokeRecPtr(); |
| 762 | if (pathEffect && pathEffect->filterPath(effectPath.init(), *pathPtr, strokePtr, |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 763 | cullRect)) { |
commit-bot@chromium.org | f0c41e2 | 2014-01-14 18:42:34 +0000 | [diff] [blame] | 764 | pathPtr = effectPath.get(); |
| 765 | pathIsMutable = true; |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 766 | strokeInfo.removeDash(); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 767 | } |
| 768 | |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 769 | const SkStrokeRec& stroke = strokeInfo.getStrokeRec(); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 770 | if (paint.getMaskFilter()) { |
| 771 | if (!stroke.isHairlineStyle()) { |
commit-bot@chromium.org | f0c41e2 | 2014-01-14 18:42:34 +0000 | [diff] [blame] | 772 | SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init(); |
| 773 | if (stroke.applyToPath(strokedPath, *pathPtr)) { |
| 774 | pathPtr = strokedPath; |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 775 | pathIsMutable = true; |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 776 | strokeInfo.setFillStyle(); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 777 | } |
| 778 | } |
| 779 | |
| 780 | // avoid possibly allocating a new path in transform if we can |
commit-bot@chromium.org | f0c41e2 | 2014-01-14 18:42:34 +0000 | [diff] [blame] | 781 | SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init(); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 782 | |
| 783 | // transform the path into device space |
| 784 | pathPtr->transform(fContext->getMatrix(), devPathPtr); |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 785 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 786 | SkRect maskRect; |
| 787 | if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(), |
| 788 | draw.fClip->getBounds(), |
| 789 | fContext->getMatrix(), |
| 790 | &maskRect)) { |
commit-bot@chromium.org | 439ff1b | 2014-01-13 16:39:39 +0000 | [diff] [blame] | 791 | // The context's matrix may change while creating the mask, so save the CTM here to |
| 792 | // pass to filterMaskGPU. |
| 793 | const SkMatrix ctm = fContext->getMatrix(); |
| 794 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 795 | SkIRect finalIRect; |
| 796 | maskRect.roundOut(&finalIRect); |
| 797 | if (draw.fClip->quickReject(finalIRect)) { |
| 798 | // clipped out |
| 799 | return; |
| 800 | } |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 801 | |
commit-bot@chromium.org | cf34bc0 | 2014-01-30 15:34:43 +0000 | [diff] [blame] | 802 | if (paint.getMaskFilter()->directFilterMaskGPU(fContext, &grPaint, |
commit-bot@chromium.org | 8213970 | 2014-03-10 22:53:20 +0000 | [diff] [blame] | 803 | stroke, *devPathPtr)) { |
commit-bot@chromium.org | cf34bc0 | 2014-01-30 15:34:43 +0000 | [diff] [blame] | 804 | // the mask filter was able to draw itself directly, so there's nothing |
| 805 | // left to do. |
| 806 | return; |
| 807 | } |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 808 | |
| 809 | GrAutoScratchTexture mask; |
| 810 | |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 811 | if (create_mask_GPU(fContext, maskRect, *devPathPtr, strokeInfo, |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 812 | grPaint.isAntiAlias(), &mask)) { |
| 813 | GrTexture* filtered; |
| 814 | |
commit-bot@chromium.org | 41bf930 | 2014-01-08 22:25:53 +0000 | [diff] [blame] | 815 | if (paint.getMaskFilter()->filterMaskGPU(mask.texture(), |
commit-bot@chromium.org | 439ff1b | 2014-01-13 16:39:39 +0000 | [diff] [blame] | 816 | ctm, maskRect, &filtered, true)) { |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 817 | // filterMaskGPU gives us ownership of a ref to the result |
| 818 | SkAutoTUnref<GrTexture> atu(filtered); |
| 819 | |
| 820 | // If the scratch texture that we used as the filter src also holds the filter |
| 821 | // result then we must detach so that this texture isn't recycled for a later |
| 822 | // draw. |
| 823 | if (filtered == mask.texture()) { |
| 824 | mask.detach(); |
| 825 | filtered->unref(); // detach transfers GrAutoScratchTexture's ref to us. |
| 826 | } |
| 827 | |
| 828 | if (draw_mask(fContext, maskRect, &grPaint, filtered)) { |
| 829 | // This path is completely drawn |
| 830 | return; |
| 831 | } |
| 832 | } |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | // draw the mask on the CPU - this is a fallthrough path in case the |
| 837 | // GPU path fails |
| 838 | SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style : |
| 839 | SkPaint::kFill_Style; |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 840 | draw_with_mask_filter(fContext, *devPathPtr, paint.getMaskFilter(), |
| 841 | *draw.fClip, &grPaint, style); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 842 | return; |
| 843 | } |
| 844 | |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 845 | fContext->drawPath(grPaint, *pathPtr, strokeInfo); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | static const int kBmpSmallTileSize = 1 << 10; |
| 849 | |
| 850 | static inline int get_tile_count(const SkIRect& srcRect, int tileSize) { |
| 851 | int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1; |
| 852 | int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1; |
| 853 | return tilesX * tilesY; |
| 854 | } |
| 855 | |
| 856 | static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) { |
| 857 | if (maxTileSize <= kBmpSmallTileSize) { |
| 858 | return maxTileSize; |
| 859 | } |
| 860 | |
| 861 | size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize); |
| 862 | size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize); |
| 863 | |
| 864 | maxTileTotalTileSize *= maxTileSize * maxTileSize; |
| 865 | smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize; |
| 866 | |
| 867 | if (maxTileTotalTileSize > 2 * smallTotalTileSize) { |
| 868 | return kBmpSmallTileSize; |
| 869 | } else { |
| 870 | return maxTileSize; |
| 871 | } |
| 872 | } |
| 873 | |
| 874 | // Given a bitmap, an optional src rect, and a context with a clip and matrix determine what |
| 875 | // pixels from the bitmap are necessary. |
| 876 | static void determine_clipped_src_rect(const GrContext* context, |
| 877 | const SkBitmap& bitmap, |
| 878 | const SkRect* srcRectPtr, |
| 879 | SkIRect* clippedSrcIRect) { |
| 880 | const GrClipData* clip = context->getClip(); |
| 881 | clip->getConservativeBounds(context->getRenderTarget(), clippedSrcIRect, NULL); |
| 882 | SkMatrix inv; |
| 883 | if (!context->getMatrix().invert(&inv)) { |
| 884 | clippedSrcIRect->setEmpty(); |
| 885 | return; |
| 886 | } |
| 887 | SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect); |
| 888 | inv.mapRect(&clippedSrcRect); |
| 889 | if (NULL != srcRectPtr) { |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 890 | // we've setup src space 0,0 to map to the top left of the src rect. |
| 891 | clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 892 | if (!clippedSrcRect.intersect(*srcRectPtr)) { |
| 893 | clippedSrcIRect->setEmpty(); |
| 894 | return; |
| 895 | } |
| 896 | } |
| 897 | clippedSrcRect.roundOut(clippedSrcIRect); |
| 898 | SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height()); |
| 899 | if (!clippedSrcIRect->intersect(bmpBounds)) { |
| 900 | clippedSrcIRect->setEmpty(); |
| 901 | } |
| 902 | } |
| 903 | |
| 904 | bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap, |
| 905 | const GrTextureParams& params, |
| 906 | const SkRect* srcRectPtr, |
| 907 | int maxTileSize, |
| 908 | int* tileSize, |
| 909 | SkIRect* clippedSrcRect) const { |
| 910 | // if bitmap is explictly texture backed then just use the texture |
| 911 | if (NULL != bitmap.getTexture()) { |
| 912 | return false; |
| 913 | } |
| 914 | |
| 915 | // if it's larger than the max tile size, then we have no choice but tiling. |
| 916 | if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) { |
| 917 | determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect); |
| 918 | *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize); |
| 919 | return true; |
| 920 | } |
| 921 | |
| 922 | if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) { |
| 923 | return false; |
| 924 | } |
| 925 | |
| 926 | // if the entire texture is already in our cache then no reason to tile it |
| 927 | if (GrIsBitmapInCache(fContext, bitmap, ¶ms)) { |
| 928 | return false; |
| 929 | } |
| 930 | |
| 931 | // At this point we know we could do the draw by uploading the entire bitmap |
| 932 | // as a texture. However, if the texture would be large compared to the |
| 933 | // cache size and we don't require most of it for this draw then tile to |
| 934 | // reduce the amount of upload and cache spill. |
| 935 | |
| 936 | // assumption here is that sw bitmap size is a good proxy for its size as |
| 937 | // a texture |
| 938 | size_t bmpSize = bitmap.getSize(); |
| 939 | size_t cacheSize; |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 940 | fContext->getResourceCacheLimits(NULL, &cacheSize); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 941 | if (bmpSize < cacheSize / 2) { |
| 942 | return false; |
| 943 | } |
| 944 | |
| 945 | // Figure out how much of the src we will need based on the src rect and clipping. |
| 946 | determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect); |
| 947 | *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile. |
| 948 | size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) * |
| 949 | kBmpSmallTileSize * kBmpSmallTileSize; |
| 950 | |
| 951 | return usedTileBytes < 2 * bmpSize; |
| 952 | } |
| 953 | |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 954 | void SkGpuDevice::drawBitmap(const SkDraw& origDraw, |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 955 | const SkBitmap& bitmap, |
| 956 | const SkMatrix& m, |
| 957 | const SkPaint& paint) { |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 958 | SkMatrix concat; |
| 959 | SkTCopyOnFirstWrite<SkDraw> draw(origDraw); |
| 960 | if (!m.isIdentity()) { |
| 961 | concat.setConcat(*draw->fMatrix, m); |
| 962 | draw.writable()->fMatrix = &concat; |
| 963 | } |
| 964 | this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 965 | } |
| 966 | |
commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 967 | // 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] | 968 | // 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner |
| 969 | // of 'iRect' for all possible outsets/clamps. |
commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 970 | static inline void clamped_outset_with_offset(SkIRect* iRect, |
| 971 | int outset, |
| 972 | SkPoint* offset, |
| 973 | const SkIRect& clamp) { |
| 974 | iRect->outset(outset, outset); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 975 | |
commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 976 | int leftClampDelta = clamp.fLeft - iRect->fLeft; |
| 977 | if (leftClampDelta > 0) { |
| 978 | offset->fX -= outset - leftClampDelta; |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 979 | iRect->fLeft = clamp.fLeft; |
| 980 | } else { |
commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 981 | offset->fX -= outset; |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 982 | } |
commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 983 | |
| 984 | int topClampDelta = clamp.fTop - iRect->fTop; |
| 985 | if (topClampDelta > 0) { |
| 986 | offset->fY -= outset - topClampDelta; |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 987 | iRect->fTop = clamp.fTop; |
| 988 | } else { |
commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 989 | offset->fY -= outset; |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | if (iRect->fRight > clamp.fRight) { |
| 993 | iRect->fRight = clamp.fRight; |
| 994 | } |
| 995 | if (iRect->fBottom > clamp.fBottom) { |
| 996 | iRect->fBottom = clamp.fBottom; |
| 997 | } |
| 998 | } |
| 999 | |
commit-bot@chromium.org | a17773f | 2014-05-09 13:53:38 +0000 | [diff] [blame] | 1000 | static bool has_aligned_samples(const SkRect& srcRect, |
| 1001 | const SkRect& transformedRect) { |
| 1002 | // detect pixel disalignment |
| 1003 | if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) - |
| 1004 | transformedRect.left()) < COLOR_BLEED_TOLERANCE && |
| 1005 | SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) - |
| 1006 | transformedRect.top()) < COLOR_BLEED_TOLERANCE && |
| 1007 | SkScalarAbs(transformedRect.width() - srcRect.width()) < |
| 1008 | COLOR_BLEED_TOLERANCE && |
| 1009 | SkScalarAbs(transformedRect.height() - srcRect.height()) < |
| 1010 | COLOR_BLEED_TOLERANCE) { |
| 1011 | return true; |
| 1012 | } |
| 1013 | return false; |
| 1014 | } |
| 1015 | |
| 1016 | static bool may_color_bleed(const SkRect& srcRect, |
| 1017 | const SkRect& transformedRect, |
| 1018 | const SkMatrix& m) { |
| 1019 | // Only gets called if has_aligned_samples returned false. |
| 1020 | // So we can assume that sampling is axis aligned but not texel aligned. |
| 1021 | SkASSERT(!has_aligned_samples(srcRect, transformedRect)); |
| 1022 | SkRect innerSrcRect(srcRect), innerTransformedRect, |
| 1023 | outerTransformedRect(transformedRect); |
| 1024 | innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf); |
| 1025 | m.mapRect(&innerTransformedRect, innerSrcRect); |
| 1026 | |
| 1027 | // The gap between outerTransformedRect and innerTransformedRect |
| 1028 | // represents the projection of the source border area, which is |
| 1029 | // problematic for color bleeding. We must check whether any |
| 1030 | // destination pixels sample the border area. |
| 1031 | outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE); |
| 1032 | innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE); |
| 1033 | SkIRect outer, inner; |
| 1034 | outerTransformedRect.round(&outer); |
| 1035 | innerTransformedRect.round(&inner); |
| 1036 | // If the inner and outer rects round to the same result, it means the |
| 1037 | // border does not overlap any pixel centers. Yay! |
| 1038 | return inner != outer; |
| 1039 | } |
| 1040 | |
| 1041 | static bool needs_texture_domain(const SkBitmap& bitmap, |
| 1042 | const SkRect& srcRect, |
| 1043 | GrTextureParams ¶ms, |
| 1044 | const SkMatrix& contextMatrix, |
| 1045 | bool bicubic) { |
| 1046 | bool needsTextureDomain = false; |
| 1047 | |
| 1048 | if (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode) { |
| 1049 | // Need texture domain if drawing a sub rect |
| 1050 | needsTextureDomain = srcRect.width() < bitmap.width() || |
| 1051 | srcRect.height() < bitmap.height(); |
| 1052 | if (!bicubic && needsTextureDomain && contextMatrix.rectStaysRect()) { |
| 1053 | // sampling is axis-aligned |
| 1054 | SkRect transformedRect; |
| 1055 | contextMatrix.mapRect(&transformedRect, srcRect); |
| 1056 | |
| 1057 | if (has_aligned_samples(srcRect, transformedRect)) { |
| 1058 | params.setFilterMode(GrTextureParams::kNone_FilterMode); |
| 1059 | needsTextureDomain = false; |
| 1060 | } else { |
| 1061 | needsTextureDomain = may_color_bleed(srcRect, transformedRect, contextMatrix); |
| 1062 | } |
| 1063 | } |
| 1064 | } |
| 1065 | return needsTextureDomain; |
| 1066 | } |
| 1067 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1068 | void SkGpuDevice::drawBitmapCommon(const SkDraw& draw, |
| 1069 | const SkBitmap& bitmap, |
| 1070 | const SkRect* srcRectPtr, |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1071 | const SkSize* dstSizePtr, |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1072 | const SkPaint& paint, |
| 1073 | SkCanvas::DrawBitmapRectFlags flags) { |
| 1074 | CHECK_SHOULD_DRAW(draw, false); |
| 1075 | |
| 1076 | SkRect srcRect; |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1077 | SkSize dstSize; |
commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 1078 | // If there is no src rect, or the src rect contains the entire bitmap then we're effectively |
| 1079 | // in the (easier) bleed case, so update flags. |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1080 | if (NULL == srcRectPtr) { |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1081 | SkScalar w = SkIntToScalar(bitmap.width()); |
| 1082 | SkScalar h = SkIntToScalar(bitmap.height()); |
| 1083 | dstSize.fWidth = w; |
| 1084 | dstSize.fHeight = h; |
| 1085 | srcRect.set(0, 0, w, h); |
commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 1086 | flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1087 | } else { |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1088 | SkASSERT(NULL != dstSizePtr); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1089 | srcRect = *srcRectPtr; |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1090 | dstSize = *dstSizePtr; |
commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 1091 | if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 && |
| 1092 | srcRect.fRight >= bitmap.width() && srcRect.fBottom >= bitmap.height()) { |
| 1093 | flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag); |
| 1094 | } |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
| 1097 | if (paint.getMaskFilter()){ |
| 1098 | // Convert the bitmap to a shader so that the rect can be drawn |
| 1099 | // through drawRect, which supports mask filters. |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1100 | SkBitmap tmp; // subset of bitmap, if necessary |
| 1101 | const SkBitmap* bitmapPtr = &bitmap; |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1102 | SkMatrix localM; |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1103 | if (NULL != srcRectPtr) { |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1104 | localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop); |
| 1105 | localM.postScale(dstSize.fWidth / srcRectPtr->width(), |
| 1106 | dstSize.fHeight / srcRectPtr->height()); |
commit-bot@chromium.org | d6ca4ac | 2013-11-22 20:34:59 +0000 | [diff] [blame] | 1107 | // In bleed mode we position and trim the bitmap based on the src rect which is |
| 1108 | // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out |
| 1109 | // the desired portion of the bitmap and then update 'm' and 'srcRect' to |
| 1110 | // compensate. |
| 1111 | if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) { |
| 1112 | SkIRect iSrc; |
| 1113 | srcRect.roundOut(&iSrc); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1114 | |
commit-bot@chromium.org | d6ca4ac | 2013-11-22 20:34:59 +0000 | [diff] [blame] | 1115 | SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft), |
| 1116 | SkIntToScalar(iSrc.fTop)); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1117 | |
commit-bot@chromium.org | d6ca4ac | 2013-11-22 20:34:59 +0000 | [diff] [blame] | 1118 | if (!bitmap.extractSubset(&tmp, iSrc)) { |
| 1119 | return; // extraction failed |
| 1120 | } |
| 1121 | bitmapPtr = &tmp; |
| 1122 | srcRect.offset(-offset.fX, -offset.fY); |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1123 | |
commit-bot@chromium.org | d6ca4ac | 2013-11-22 20:34:59 +0000 | [diff] [blame] | 1124 | // The source rect has changed so update the matrix |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1125 | localM.preTranslate(offset.fX, offset.fY); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1126 | } |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1127 | } else { |
| 1128 | localM.reset(); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1131 | SkPaint paintWithShader(paint); |
| 1132 | paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr, |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 1133 | SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &localM))->unref(); |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1134 | SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight}; |
| 1135 | this->drawRect(draw, dstRect, paintWithShader); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1136 | |
| 1137 | return; |
| 1138 | } |
| 1139 | |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1140 | // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using |
| 1141 | // the view matrix rather than a local matrix. |
| 1142 | SkMatrix m; |
| 1143 | m.setScale(dstSize.fWidth / srcRect.width(), |
| 1144 | dstSize.fHeight / srcRect.height()); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1145 | fContext->concatMatrix(m); |
| 1146 | |
| 1147 | GrTextureParams params; |
| 1148 | SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel(); |
| 1149 | GrTextureParams::FilterMode textureFilterMode; |
commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 1150 | |
commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 1151 | bool doBicubic = false; |
| 1152 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1153 | switch(paintFilterLevel) { |
| 1154 | case SkPaint::kNone_FilterLevel: |
| 1155 | textureFilterMode = GrTextureParams::kNone_FilterMode; |
| 1156 | break; |
| 1157 | case SkPaint::kLow_FilterLevel: |
| 1158 | textureFilterMode = GrTextureParams::kBilerp_FilterMode; |
| 1159 | break; |
| 1160 | case SkPaint::kMedium_FilterLevel: |
commit-bot@chromium.org | 1878651 | 2014-05-20 14:53:45 +0000 | [diff] [blame] | 1161 | if (fContext->getMatrix().getMinScale() < SK_Scalar1) { |
commit-bot@chromium.org | 79b7eee | 2013-12-16 21:02:29 +0000 | [diff] [blame] | 1162 | textureFilterMode = GrTextureParams::kMipMap_FilterMode; |
| 1163 | } else { |
| 1164 | // Don't trigger MIP level generation unnecessarily. |
| 1165 | textureFilterMode = GrTextureParams::kBilerp_FilterMode; |
| 1166 | } |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1167 | break; |
commit-bot@chromium.org | 79b7eee | 2013-12-16 21:02:29 +0000 | [diff] [blame] | 1168 | case SkPaint::kHigh_FilterLevel: |
commit-bot@chromium.org | cea9abb | 2013-12-09 19:15:37 +0000 | [diff] [blame] | 1169 | // Minification can look bad with the bicubic effect. |
commit-bot@chromium.org | 9927bd3 | 2014-05-20 17:51:13 +0000 | [diff] [blame] | 1170 | doBicubic = |
| 1171 | GrBicubicEffect::ShouldUseBicubic(fContext->getMatrix(), &textureFilterMode); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1172 | break; |
| 1173 | default: |
| 1174 | SkErrorInternals::SetError( kInvalidPaint_SkError, |
| 1175 | "Sorry, I don't understand the filtering " |
| 1176 | "mode you asked for. Falling back to " |
| 1177 | "MIPMaps."); |
| 1178 | textureFilterMode = GrTextureParams::kMipMap_FilterMode; |
| 1179 | break; |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
commit-bot@chromium.org | 9927bd3 | 2014-05-20 17:51:13 +0000 | [diff] [blame] | 1182 | int tileFilterPad; |
| 1183 | if (doBicubic) { |
| 1184 | tileFilterPad = GrBicubicEffect::kFilterTexelPad; |
| 1185 | } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) { |
| 1186 | tileFilterPad = 0; |
| 1187 | } else { |
| 1188 | tileFilterPad = 1; |
| 1189 | } |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1190 | params.setFilterMode(textureFilterMode); |
| 1191 | |
commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 1192 | int maxTileSize = fContext->getMaxTextureSize() - 2 * tileFilterPad; |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1193 | int tileSize; |
| 1194 | |
| 1195 | SkIRect clippedSrcRect; |
| 1196 | if (this->shouldTileBitmap(bitmap, params, srcRectPtr, maxTileSize, &tileSize, |
| 1197 | &clippedSrcRect)) { |
commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 1198 | this->drawTiledBitmap(bitmap, srcRect, clippedSrcRect, params, paint, flags, tileSize, |
| 1199 | doBicubic); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1200 | } else { |
| 1201 | // take the simple case |
commit-bot@chromium.org | a17773f | 2014-05-09 13:53:38 +0000 | [diff] [blame] | 1202 | bool needsTextureDomain = needs_texture_domain(bitmap, |
| 1203 | srcRect, |
| 1204 | params, |
| 1205 | fContext->getMatrix(), |
| 1206 | doBicubic); |
| 1207 | this->internalDrawBitmap(bitmap, |
| 1208 | srcRect, |
| 1209 | params, |
| 1210 | paint, |
| 1211 | flags, |
| 1212 | doBicubic, |
| 1213 | needsTextureDomain); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1214 | } |
| 1215 | } |
| 1216 | |
| 1217 | // Break 'bitmap' into several tiles to draw it since it has already |
| 1218 | // been determined to be too large to fit in VRAM |
| 1219 | void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap, |
| 1220 | const SkRect& srcRect, |
| 1221 | const SkIRect& clippedSrcIRect, |
| 1222 | const GrTextureParams& params, |
| 1223 | const SkPaint& paint, |
| 1224 | SkCanvas::DrawBitmapRectFlags flags, |
commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 1225 | int tileSize, |
| 1226 | bool bicubic) { |
commit-bot@chromium.org | 9d5e3f1 | 2014-05-01 21:23:19 +0000 | [diff] [blame] | 1227 | // The following pixel lock is technically redundant, but it is desirable |
| 1228 | // to lock outside of the tile loop to prevent redecoding the whole image |
| 1229 | // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that |
| 1230 | // is larger than the limit of the discardable memory pool. |
| 1231 | SkAutoLockPixels alp(bitmap); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1232 | SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect); |
| 1233 | |
| 1234 | int nx = bitmap.width() / tileSize; |
| 1235 | int ny = bitmap.height() / tileSize; |
| 1236 | for (int x = 0; x <= nx; x++) { |
| 1237 | for (int y = 0; y <= ny; y++) { |
| 1238 | SkRect tileR; |
| 1239 | tileR.set(SkIntToScalar(x * tileSize), |
| 1240 | SkIntToScalar(y * tileSize), |
| 1241 | SkIntToScalar((x + 1) * tileSize), |
| 1242 | SkIntToScalar((y + 1) * tileSize)); |
| 1243 | |
| 1244 | if (!SkRect::Intersects(tileR, clippedSrcRect)) { |
| 1245 | continue; |
| 1246 | } |
| 1247 | |
| 1248 | if (!tileR.intersect(srcRect)) { |
| 1249 | continue; |
| 1250 | } |
| 1251 | |
| 1252 | SkBitmap tmpB; |
| 1253 | SkIRect iTileR; |
| 1254 | tileR.roundOut(&iTileR); |
| 1255 | SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft), |
| 1256 | SkIntToScalar(iTileR.fTop)); |
| 1257 | |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1258 | // Adjust the context matrix to draw at the right x,y in device space |
| 1259 | SkMatrix tmpM; |
| 1260 | GrContext::AutoMatrix am; |
| 1261 | tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop); |
| 1262 | am.setPreConcat(fContext, tmpM); |
| 1263 | |
commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 1264 | if (SkPaint::kNone_FilterLevel != paint.getFilterLevel() || bicubic) { |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1265 | SkIRect iClampRect; |
| 1266 | |
| 1267 | if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) { |
| 1268 | // In bleed mode we want to always expand the tile on all edges |
| 1269 | // but stay within the bitmap bounds |
| 1270 | iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height()); |
| 1271 | } else { |
| 1272 | // In texture-domain/clamp mode we only want to expand the |
| 1273 | // tile on edges interior to "srcRect" (i.e., we want to |
| 1274 | // not bleed across the original clamped edges) |
| 1275 | srcRect.roundOut(&iClampRect); |
| 1276 | } |
commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 1277 | int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1; |
| 1278 | clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1279 | } |
| 1280 | |
| 1281 | if (bitmap.extractSubset(&tmpB, iTileR)) { |
| 1282 | // now offset it to make it "local" to our tmp bitmap |
| 1283 | tileR.offset(-offset.fX, -offset.fY); |
commit-bot@chromium.org | a17773f | 2014-05-09 13:53:38 +0000 | [diff] [blame] | 1284 | GrTextureParams paramsTemp = params; |
| 1285 | bool needsTextureDomain = needs_texture_domain(bitmap, |
| 1286 | srcRect, |
| 1287 | paramsTemp, |
| 1288 | fContext->getMatrix(), |
| 1289 | bicubic); |
| 1290 | this->internalDrawBitmap(tmpB, |
| 1291 | tileR, |
| 1292 | paramsTemp, |
| 1293 | paint, |
| 1294 | flags, |
| 1295 | bicubic, |
| 1296 | needsTextureDomain); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1297 | } |
| 1298 | } |
| 1299 | } |
| 1300 | } |
| 1301 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1302 | |
| 1303 | /* |
| 1304 | * This is called by drawBitmap(), which has to handle images that may be too |
| 1305 | * large to be represented by a single texture. |
| 1306 | * |
| 1307 | * internalDrawBitmap assumes that the specified bitmap will fit in a texture |
| 1308 | * and that non-texture portion of the GrPaint has already been setup. |
| 1309 | */ |
| 1310 | void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap, |
| 1311 | const SkRect& srcRect, |
| 1312 | const GrTextureParams& params, |
| 1313 | const SkPaint& paint, |
commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 1314 | SkCanvas::DrawBitmapRectFlags flags, |
commit-bot@chromium.org | a17773f | 2014-05-09 13:53:38 +0000 | [diff] [blame] | 1315 | bool bicubic, |
| 1316 | bool needsTextureDomain) { |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1317 | SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() && |
| 1318 | bitmap.height() <= fContext->getMaxTextureSize()); |
| 1319 | |
| 1320 | GrTexture* texture; |
| 1321 | SkAutoCachedTexture act(this, bitmap, ¶ms, &texture); |
| 1322 | if (NULL == texture) { |
| 1323 | return; |
| 1324 | } |
| 1325 | |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1326 | SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() }; |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1327 | SkRect paintRect; |
| 1328 | SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width())); |
| 1329 | SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height())); |
| 1330 | paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv), |
| 1331 | SkScalarMul(srcRect.fTop, hInv), |
| 1332 | SkScalarMul(srcRect.fRight, wInv), |
| 1333 | SkScalarMul(srcRect.fBottom, hInv)); |
| 1334 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1335 | SkRect textureDomain = SkRect::MakeEmpty(); |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 1336 | SkAutoTUnref<GrEffect> effect; |
commit-bot@chromium.org | a17773f | 2014-05-09 13:53:38 +0000 | [diff] [blame] | 1337 | if (needsTextureDomain && !(flags & SkCanvas::kBleed_DrawBitmapRectFlag)) { |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1338 | // Use a constrained texture domain to avoid color bleeding |
| 1339 | SkScalar left, top, right, bottom; |
| 1340 | if (srcRect.width() > SK_Scalar1) { |
| 1341 | SkScalar border = SK_ScalarHalf / texture->width(); |
| 1342 | left = paintRect.left() + border; |
| 1343 | right = paintRect.right() - border; |
| 1344 | } else { |
| 1345 | left = right = SkScalarHalf(paintRect.left() + paintRect.right()); |
| 1346 | } |
| 1347 | if (srcRect.height() > SK_Scalar1) { |
| 1348 | SkScalar border = SK_ScalarHalf / texture->height(); |
| 1349 | top = paintRect.top() + border; |
| 1350 | bottom = paintRect.bottom() - border; |
| 1351 | } else { |
| 1352 | top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom()); |
| 1353 | } |
| 1354 | textureDomain.setLTRB(left, top, right, bottom); |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 1355 | if (bicubic) { |
| 1356 | effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain)); |
| 1357 | } else { |
| 1358 | effect.reset(GrTextureDomainEffect::Create(texture, |
| 1359 | SkMatrix::I(), |
| 1360 | textureDomain, |
| 1361 | GrTextureDomain::kClamp_Mode, |
| 1362 | params.filterMode())); |
| 1363 | } |
commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 1364 | } else if (bicubic) { |
commit-bot@chromium.org | bc91fd7 | 2013-12-10 12:53:39 +0000 | [diff] [blame] | 1365 | SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode()); |
| 1366 | SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() }; |
| 1367 | effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes)); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1368 | } else { |
| 1369 | effect.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params)); |
| 1370 | } |
| 1371 | |
| 1372 | // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring |
| 1373 | // the rest from the SkPaint. |
| 1374 | GrPaint grPaint; |
| 1375 | grPaint.addColorEffect(effect); |
reed | 0689d7b | 2014-06-14 05:30:20 -0700 | [diff] [blame] | 1376 | bool alphaOnly = !(kAlpha_8_SkColorType == bitmap.colorType()); |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 1377 | GrColor paintColor = (alphaOnly) ? SkColor2GrColorJustAlpha(paint.getColor()) : |
| 1378 | SkColor2GrColor(paint.getColor()); |
| 1379 | SkPaint2GrPaintNoShader(this->context(), paint, paintColor, false, &grPaint); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1380 | |
| 1381 | fContext->drawRectToRect(grPaint, dstRect, paintRect, NULL); |
| 1382 | } |
| 1383 | |
| 1384 | static bool filter_texture(SkBaseDevice* device, GrContext* context, |
commit-bot@chromium.org | ae761f7 | 2014-02-05 22:32:02 +0000 | [diff] [blame] | 1385 | GrTexture* texture, const SkImageFilter* filter, |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 1386 | int w, int h, const SkImageFilter::Context& ctx, |
| 1387 | SkBitmap* result, SkIPoint* offset) { |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1388 | SkASSERT(filter); |
| 1389 | SkDeviceImageFilterProxy proxy(device); |
| 1390 | |
| 1391 | if (filter->canFilterImageGPU()) { |
| 1392 | // Save the render target and set it to NULL, so we don't accidentally draw to it in the |
| 1393 | // filter. Also set the clip wide open and the matrix to identity. |
| 1394 | GrContext::AutoWideOpenIdentityDraw awo(context, NULL); |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 1395 | return filter->filterImageGPU(&proxy, wrap_texture(texture), ctx, result, offset); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1396 | } else { |
| 1397 | return false; |
| 1398 | } |
| 1399 | } |
| 1400 | |
| 1401 | void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap, |
| 1402 | int left, int top, const SkPaint& paint) { |
| 1403 | // drawSprite is defined to be in device coords. |
| 1404 | CHECK_SHOULD_DRAW(draw, true); |
| 1405 | |
| 1406 | SkAutoLockPixels alp(bitmap, !bitmap.getTexture()); |
| 1407 | if (!bitmap.getTexture() && !bitmap.readyToDraw()) { |
| 1408 | return; |
| 1409 | } |
| 1410 | |
| 1411 | int w = bitmap.width(); |
| 1412 | int h = bitmap.height(); |
| 1413 | |
| 1414 | GrTexture* texture; |
| 1415 | // draw sprite uses the default texture params |
| 1416 | SkAutoCachedTexture act(this, bitmap, NULL, &texture); |
| 1417 | |
| 1418 | SkImageFilter* filter = paint.getImageFilter(); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1419 | // This bitmap will own the filtered result as a texture. |
| 1420 | SkBitmap filteredBitmap; |
| 1421 | |
| 1422 | if (NULL != filter) { |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 1423 | SkIPoint offset = SkIPoint::Make(0, 0); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1424 | SkMatrix matrix(*draw.fMatrix); |
| 1425 | matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top)); |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 1426 | SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height()); |
commit-bot@chromium.org | f7efa50 | 2014-04-11 18:57:00 +0000 | [diff] [blame] | 1427 | SkImageFilter::Cache* cache = SkImageFilter::Cache::Create(); |
| 1428 | SkAutoUnref aur(cache); |
| 1429 | SkImageFilter::Context ctx(matrix, clipBounds, cache); |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 1430 | if (filter_texture(this, fContext, texture, filter, w, h, ctx, &filteredBitmap, |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1431 | &offset)) { |
| 1432 | texture = (GrTexture*) filteredBitmap.getTexture(); |
| 1433 | w = filteredBitmap.width(); |
| 1434 | h = filteredBitmap.height(); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 1435 | left += offset.x(); |
| 1436 | top += offset.y(); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1437 | } else { |
| 1438 | return; |
| 1439 | } |
| 1440 | } |
| 1441 | |
| 1442 | GrPaint grPaint; |
| 1443 | grPaint.addColorTextureEffect(texture, SkMatrix::I()); |
| 1444 | |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 1445 | SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColorJustAlpha(paint.getColor()), |
| 1446 | false, &grPaint); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1447 | |
| 1448 | fContext->drawRectToRect(grPaint, |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 1449 | SkRect::MakeXYWH(SkIntToScalar(left), |
| 1450 | SkIntToScalar(top), |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1451 | SkIntToScalar(w), |
| 1452 | SkIntToScalar(h)), |
| 1453 | SkRect::MakeXYWH(0, |
| 1454 | 0, |
| 1455 | SK_Scalar1 * w / texture->width(), |
| 1456 | SK_Scalar1 * h / texture->height())); |
| 1457 | } |
| 1458 | |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1459 | void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap, |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1460 | const SkRect* src, const SkRect& dst, |
| 1461 | const SkPaint& paint, |
| 1462 | SkCanvas::DrawBitmapRectFlags flags) { |
| 1463 | SkMatrix matrix; |
| 1464 | SkRect bitmapBounds, tmpSrc; |
| 1465 | |
| 1466 | bitmapBounds.set(0, 0, |
| 1467 | SkIntToScalar(bitmap.width()), |
| 1468 | SkIntToScalar(bitmap.height())); |
| 1469 | |
| 1470 | // Compute matrix from the two rectangles |
| 1471 | if (NULL != src) { |
| 1472 | tmpSrc = *src; |
| 1473 | } else { |
| 1474 | tmpSrc = bitmapBounds; |
| 1475 | } |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1476 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1477 | matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit); |
| 1478 | |
| 1479 | // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null. |
| 1480 | if (NULL != src) { |
| 1481 | if (!bitmapBounds.contains(tmpSrc)) { |
| 1482 | if (!tmpSrc.intersect(bitmapBounds)) { |
| 1483 | return; // nothing to draw |
| 1484 | } |
| 1485 | } |
| 1486 | } |
| 1487 | |
commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1488 | SkRect tmpDst; |
| 1489 | matrix.mapRect(&tmpDst, tmpSrc); |
| 1490 | |
| 1491 | SkTCopyOnFirstWrite<SkDraw> draw(origDraw); |
| 1492 | if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) { |
| 1493 | // Translate so that tempDst's top left is at the origin. |
| 1494 | matrix = *origDraw.fMatrix; |
| 1495 | matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop); |
| 1496 | draw.writable()->fMatrix = &matrix; |
| 1497 | } |
| 1498 | SkSize dstSize; |
| 1499 | dstSize.fWidth = tmpDst.width(); |
| 1500 | dstSize.fHeight = tmpDst.height(); |
| 1501 | |
| 1502 | this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1503 | } |
| 1504 | |
| 1505 | void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device, |
| 1506 | int x, int y, const SkPaint& paint) { |
| 1507 | // clear of the source device must occur before CHECK_SHOULD_DRAW |
egdaniel | d78a168 | 2014-07-09 10:41:26 -0700 | [diff] [blame] | 1508 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawDevice", fContext); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1509 | SkGpuDevice* dev = static_cast<SkGpuDevice*>(device); |
| 1510 | if (dev->fNeedClear) { |
| 1511 | // TODO: could check here whether we really need to draw at all |
| 1512 | dev->clear(0x0); |
| 1513 | } |
| 1514 | |
| 1515 | // drawDevice is defined to be in device coords. |
| 1516 | CHECK_SHOULD_DRAW(draw, true); |
| 1517 | |
| 1518 | GrRenderTarget* devRT = dev->accessRenderTarget(); |
| 1519 | GrTexture* devTex; |
| 1520 | if (NULL == (devTex = devRT->asTexture())) { |
| 1521 | return; |
| 1522 | } |
| 1523 | |
| 1524 | const SkBitmap& bm = dev->accessBitmap(false); |
| 1525 | int w = bm.width(); |
| 1526 | int h = bm.height(); |
| 1527 | |
| 1528 | SkImageFilter* filter = paint.getImageFilter(); |
| 1529 | // This bitmap will own the filtered result as a texture. |
| 1530 | SkBitmap filteredBitmap; |
| 1531 | |
| 1532 | if (NULL != filter) { |
| 1533 | SkIPoint offset = SkIPoint::Make(0, 0); |
| 1534 | SkMatrix matrix(*draw.fMatrix); |
| 1535 | matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y)); |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 1536 | SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height()); |
commit-bot@chromium.org | f7efa50 | 2014-04-11 18:57:00 +0000 | [diff] [blame] | 1537 | SkImageFilter::Cache* cache = SkImageFilter::Cache::Create(); |
| 1538 | SkAutoUnref aur(cache); |
| 1539 | SkImageFilter::Context ctx(matrix, clipBounds, cache); |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 1540 | if (filter_texture(this, fContext, devTex, filter, w, h, ctx, &filteredBitmap, |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1541 | &offset)) { |
| 1542 | devTex = filteredBitmap.getTexture(); |
| 1543 | w = filteredBitmap.width(); |
| 1544 | h = filteredBitmap.height(); |
| 1545 | x += offset.fX; |
| 1546 | y += offset.fY; |
| 1547 | } else { |
| 1548 | return; |
| 1549 | } |
| 1550 | } |
| 1551 | |
| 1552 | GrPaint grPaint; |
| 1553 | grPaint.addColorTextureEffect(devTex, SkMatrix::I()); |
| 1554 | |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 1555 | SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColorJustAlpha(paint.getColor()), |
| 1556 | false, &grPaint); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1557 | |
| 1558 | SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x), |
| 1559 | SkIntToScalar(y), |
| 1560 | SkIntToScalar(w), |
| 1561 | SkIntToScalar(h)); |
| 1562 | |
| 1563 | // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate |
| 1564 | // scratch texture). |
| 1565 | SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(), |
| 1566 | SK_Scalar1 * h / devTex->height()); |
| 1567 | |
| 1568 | fContext->drawRectToRect(grPaint, dstRect, srcRect); |
| 1569 | } |
| 1570 | |
commit-bot@chromium.org | ae761f7 | 2014-02-05 22:32:02 +0000 | [diff] [blame] | 1571 | bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) { |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1572 | return filter->canFilterImageGPU(); |
| 1573 | } |
| 1574 | |
commit-bot@chromium.org | ae761f7 | 2014-02-05 22:32:02 +0000 | [diff] [blame] | 1575 | bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src, |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 1576 | const SkImageFilter::Context& ctx, |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1577 | SkBitmap* result, SkIPoint* offset) { |
| 1578 | // want explicitly our impl, so guard against a subclass of us overriding it |
| 1579 | if (!this->SkGpuDevice::canHandleImageFilter(filter)) { |
| 1580 | return false; |
| 1581 | } |
| 1582 | |
| 1583 | SkAutoLockPixels alp(src, !src.getTexture()); |
| 1584 | if (!src.getTexture() && !src.readyToDraw()) { |
| 1585 | return false; |
| 1586 | } |
| 1587 | |
| 1588 | GrTexture* texture; |
| 1589 | // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup |
| 1590 | // must be pushed upstack. |
| 1591 | SkAutoCachedTexture act(this, src, NULL, &texture); |
| 1592 | |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 1593 | return filter_texture(this, fContext, texture, filter, src.width(), src.height(), ctx, |
| 1594 | result, offset); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1595 | } |
| 1596 | |
| 1597 | /////////////////////////////////////////////////////////////////////////////// |
| 1598 | |
| 1599 | // must be in SkCanvas::VertexMode order |
| 1600 | static const GrPrimitiveType gVertexMode2PrimitiveType[] = { |
| 1601 | kTriangles_GrPrimitiveType, |
| 1602 | kTriangleStrip_GrPrimitiveType, |
| 1603 | kTriangleFan_GrPrimitiveType, |
| 1604 | }; |
| 1605 | |
| 1606 | void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode, |
| 1607 | int vertexCount, const SkPoint vertices[], |
| 1608 | const SkPoint texs[], const SkColor colors[], |
| 1609 | SkXfermode* xmode, |
| 1610 | const uint16_t indices[], int indexCount, |
| 1611 | const SkPaint& paint) { |
| 1612 | CHECK_SHOULD_DRAW(draw, false); |
| 1613 | |
egdaniel | d78a168 | 2014-07-09 10:41:26 -0700 | [diff] [blame] | 1614 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawVertices", fContext); |
dandov | 32a311b | 2014-07-15 19:46:26 -0700 | [diff] [blame] | 1615 | |
| 1616 | const uint16_t* outIndices; |
| 1617 | SkAutoTDeleteArray<uint16_t> outAlloc(NULL); |
| 1618 | GrPrimitiveType primType; |
| 1619 | GrPaint grPaint; |
| 1620 | |
commit-bot@chromium.org | 559a883 | 2014-05-30 10:08:22 +0000 | [diff] [blame] | 1621 | // If both textures and vertex-colors are NULL, strokes hairlines with the paint's color. |
| 1622 | if ((NULL == texs || NULL == paint.getShader()) && NULL == colors) { |
dandov | 32a311b | 2014-07-15 19:46:26 -0700 | [diff] [blame] | 1623 | |
commit-bot@chromium.org | 559a883 | 2014-05-30 10:08:22 +0000 | [diff] [blame] | 1624 | texs = NULL; |
dandov | 32a311b | 2014-07-15 19:46:26 -0700 | [diff] [blame] | 1625 | |
commit-bot@chromium.org | 559a883 | 2014-05-30 10:08:22 +0000 | [diff] [blame] | 1626 | SkPaint copy(paint); |
| 1627 | copy.setStyle(SkPaint::kStroke_Style); |
| 1628 | copy.setStrokeWidth(0); |
dandov | 32a311b | 2014-07-15 19:46:26 -0700 | [diff] [blame] | 1629 | |
| 1630 | // we ignore the shader if texs is null. |
| 1631 | SkPaint2GrPaintNoShader(this->context(), copy, SkColor2GrColor(copy.getColor()), |
| 1632 | NULL == colors, &grPaint); |
commit-bot@chromium.org | 559a883 | 2014-05-30 10:08:22 +0000 | [diff] [blame] | 1633 | |
dandov | 32a311b | 2014-07-15 19:46:26 -0700 | [diff] [blame] | 1634 | primType = kLines_GrPrimitiveType; |
| 1635 | int triangleCount = 0; |
| 1636 | switch (vmode) { |
| 1637 | case SkCanvas::kTriangles_VertexMode: |
| 1638 | triangleCount = indexCount / 3; |
| 1639 | break; |
| 1640 | case SkCanvas::kTriangleStrip_VertexMode: |
| 1641 | case SkCanvas::kTriangleFan_VertexMode: |
| 1642 | triangleCount = indexCount - 2; |
| 1643 | break; |
| 1644 | } |
| 1645 | |
commit-bot@chromium.org | 559a883 | 2014-05-30 10:08:22 +0000 | [diff] [blame] | 1646 | VertState state(vertexCount, indices, indexCount); |
| 1647 | VertState::Proc vertProc = state.chooseProc(vmode); |
dandov | 32a311b | 2014-07-15 19:46:26 -0700 | [diff] [blame] | 1648 | |
| 1649 | //number of indices for lines per triangle with kLines |
| 1650 | indexCount = triangleCount * 6; |
| 1651 | |
| 1652 | outAlloc.reset(SkNEW_ARRAY(uint16_t, indexCount)); |
| 1653 | outIndices = outAlloc.get(); |
| 1654 | uint16_t* auxIndices = outAlloc.get(); |
commit-bot@chromium.org | 559a883 | 2014-05-30 10:08:22 +0000 | [diff] [blame] | 1655 | int i = 0; |
| 1656 | while (vertProc(&state)) { |
dandov | 32a311b | 2014-07-15 19:46:26 -0700 | [diff] [blame] | 1657 | auxIndices[i] = state.f0; |
| 1658 | auxIndices[i + 1] = state.f1; |
| 1659 | auxIndices[i + 2] = state.f1; |
| 1660 | auxIndices[i + 3] = state.f2; |
| 1661 | auxIndices[i + 4] = state.f2; |
| 1662 | auxIndices[i + 5] = state.f0; |
commit-bot@chromium.org | 559a883 | 2014-05-30 10:08:22 +0000 | [diff] [blame] | 1663 | i += 6; |
| 1664 | } |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1665 | } else { |
dandov | 32a311b | 2014-07-15 19:46:26 -0700 | [diff] [blame] | 1666 | outIndices = indices; |
| 1667 | primType = gVertexMode2PrimitiveType[vmode]; |
| 1668 | |
| 1669 | if (NULL == texs || NULL == paint.getShader()) { |
| 1670 | SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColor(paint.getColor()), |
| 1671 | NULL == colors, &grPaint); |
| 1672 | } else { |
| 1673 | SkPaint2GrPaintShader(this->context(), paint, NULL == colors, &grPaint); |
| 1674 | } |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1675 | } |
| 1676 | |
mtklein | 2583b62 | 2014-06-04 08:20:41 -0700 | [diff] [blame] | 1677 | #if 0 |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1678 | if (NULL != xmode && NULL != texs && NULL != colors) { |
| 1679 | if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) { |
| 1680 | SkDebugf("Unsupported vertex-color/texture xfer mode.\n"); |
mtklein | 2583b62 | 2014-06-04 08:20:41 -0700 | [diff] [blame] | 1681 | return; |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1682 | } |
| 1683 | } |
mtklein | 2583b62 | 2014-06-04 08:20:41 -0700 | [diff] [blame] | 1684 | #endif |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1685 | |
| 1686 | SkAutoSTMalloc<128, GrColor> convertedColors(0); |
| 1687 | if (NULL != colors) { |
| 1688 | // need to convert byte order and from non-PM to PM |
| 1689 | convertedColors.reset(vertexCount); |
commit-bot@chromium.org | c93e681 | 2014-05-23 08:09:26 +0000 | [diff] [blame] | 1690 | SkColor color; |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1691 | for (int i = 0; i < vertexCount; ++i) { |
commit-bot@chromium.org | c93e681 | 2014-05-23 08:09:26 +0000 | [diff] [blame] | 1692 | color = colors[i]; |
| 1693 | if (paint.getAlpha() != 255) { |
| 1694 | color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paint.getAlpha())); |
| 1695 | } |
| 1696 | convertedColors[i] = SkColor2GrColor(color); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1697 | } |
| 1698 | colors = convertedColors.get(); |
| 1699 | } |
| 1700 | fContext->drawVertices(grPaint, |
dandov | 32a311b | 2014-07-15 19:46:26 -0700 | [diff] [blame] | 1701 | primType, |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1702 | vertexCount, |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 1703 | vertices, |
| 1704 | texs, |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1705 | colors, |
dandov | 32a311b | 2014-07-15 19:46:26 -0700 | [diff] [blame] | 1706 | outIndices, |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1707 | indexCount); |
| 1708 | } |
| 1709 | |
| 1710 | /////////////////////////////////////////////////////////////////////////////// |
| 1711 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1712 | void SkGpuDevice::drawText(const SkDraw& draw, const void* text, |
| 1713 | size_t byteLength, SkScalar x, SkScalar y, |
| 1714 | const SkPaint& paint) { |
| 1715 | CHECK_SHOULD_DRAW(draw, false); |
egdaniel | d78a168 | 2014-07-09 10:41:26 -0700 | [diff] [blame] | 1716 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawText", fContext); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1717 | |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 1718 | if (fMainTextContext->canDraw(paint)) { |
commit-bot@chromium.org | 8128d8c | 2013-12-19 16:12:25 +0000 | [diff] [blame] | 1719 | GrPaint grPaint; |
commit-bot@chromium.org | 3595f88 | 2014-05-19 19:35:57 +0000 | [diff] [blame] | 1720 | SkPaint2GrPaintShader(this->context(), paint, true, &grPaint); |
commit-bot@chromium.org | 8128d8c | 2013-12-19 16:12:25 +0000 | [diff] [blame] | 1721 | |
| 1722 | SkDEBUGCODE(this->validate();) |
| 1723 | |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 1724 | fMainTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y); |
| 1725 | } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) { |
commit-bot@chromium.org | 9f94b91 | 2014-01-30 15:22:54 +0000 | [diff] [blame] | 1726 | GrPaint grPaint; |
commit-bot@chromium.org | 3595f88 | 2014-05-19 19:35:57 +0000 | [diff] [blame] | 1727 | SkPaint2GrPaintShader(this->context(), paint, true, &grPaint); |
commit-bot@chromium.org | 9f94b91 | 2014-01-30 15:22:54 +0000 | [diff] [blame] | 1728 | |
| 1729 | SkDEBUGCODE(this->validate();) |
| 1730 | |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 1731 | fFallbackTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y); |
commit-bot@chromium.org | 9f94b91 | 2014-01-30 15:22:54 +0000 | [diff] [blame] | 1732 | } else { |
| 1733 | // this guy will just call our drawPath() |
| 1734 | draw.drawText_asPaths((const char*)text, byteLength, x, y, paint); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1735 | } |
| 1736 | } |
| 1737 | |
| 1738 | void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, |
| 1739 | size_t byteLength, const SkScalar pos[], |
| 1740 | SkScalar constY, int scalarsPerPos, |
| 1741 | const SkPaint& paint) { |
egdaniel | bbcb38d | 2014-06-19 10:19:29 -0700 | [diff] [blame] | 1742 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPosText", fContext); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1743 | CHECK_SHOULD_DRAW(draw, false); |
| 1744 | |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 1745 | if (fMainTextContext->canDraw(paint)) { |
commit-bot@chromium.org | 8128d8c | 2013-12-19 16:12:25 +0000 | [diff] [blame] | 1746 | GrPaint grPaint; |
commit-bot@chromium.org | 3595f88 | 2014-05-19 19:35:57 +0000 | [diff] [blame] | 1747 | SkPaint2GrPaintShader(this->context(), paint, true, &grPaint); |
commit-bot@chromium.org | 8128d8c | 2013-12-19 16:12:25 +0000 | [diff] [blame] | 1748 | |
| 1749 | SkDEBUGCODE(this->validate();) |
| 1750 | |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 1751 | fMainTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos, |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 1752 | constY, scalarsPerPos); |
| 1753 | } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) { |
commit-bot@chromium.org | 9f94b91 | 2014-01-30 15:22:54 +0000 | [diff] [blame] | 1754 | GrPaint grPaint; |
commit-bot@chromium.org | 3595f88 | 2014-05-19 19:35:57 +0000 | [diff] [blame] | 1755 | SkPaint2GrPaintShader(this->context(), paint, true, &grPaint); |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 1756 | |
commit-bot@chromium.org | 9f94b91 | 2014-01-30 15:22:54 +0000 | [diff] [blame] | 1757 | SkDEBUGCODE(this->validate();) |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 1758 | |
| 1759 | fFallbackTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos, |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 1760 | constY, scalarsPerPos); |
commit-bot@chromium.org | 9f94b91 | 2014-01-30 15:22:54 +0000 | [diff] [blame] | 1761 | } else { |
| 1762 | draw.drawPosText_asPaths((const char*)text, byteLength, pos, constY, |
| 1763 | scalarsPerPos, paint); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1764 | } |
| 1765 | } |
| 1766 | |
| 1767 | void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text, |
| 1768 | size_t len, const SkPath& path, |
| 1769 | const SkMatrix* m, const SkPaint& paint) { |
| 1770 | CHECK_SHOULD_DRAW(draw, false); |
| 1771 | |
| 1772 | SkASSERT(draw.fDevice == this); |
| 1773 | draw.drawTextOnPath((const char*)text, len, path, m, paint); |
| 1774 | } |
| 1775 | |
| 1776 | /////////////////////////////////////////////////////////////////////////////// |
| 1777 | |
| 1778 | bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) { |
| 1779 | if (!paint.isLCDRenderText()) { |
| 1780 | // we're cool with the paint as is |
| 1781 | return false; |
| 1782 | } |
| 1783 | |
| 1784 | if (paint.getShader() || |
| 1785 | paint.getXfermode() || // unless its srcover |
| 1786 | paint.getMaskFilter() || |
| 1787 | paint.getRasterizer() || |
| 1788 | paint.getColorFilter() || |
| 1789 | paint.getPathEffect() || |
| 1790 | paint.isFakeBoldText() || |
| 1791 | paint.getStyle() != SkPaint::kFill_Style) { |
| 1792 | // turn off lcd |
| 1793 | flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag; |
| 1794 | flags->fHinting = paint.getHinting(); |
| 1795 | return true; |
| 1796 | } |
| 1797 | // we're cool with the paint as is |
| 1798 | return false; |
| 1799 | } |
| 1800 | |
| 1801 | void SkGpuDevice::flush() { |
| 1802 | DO_DEFERRED_CLEAR(); |
| 1803 | fContext->resolveRenderTarget(fRenderTarget); |
| 1804 | } |
| 1805 | |
| 1806 | /////////////////////////////////////////////////////////////////////////////// |
| 1807 | |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 1808 | SkBaseDevice* SkGpuDevice::onCreateDevice(const SkImageInfo& info, Usage usage) { |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1809 | GrTextureDesc desc; |
| 1810 | desc.fConfig = fRenderTarget->config(); |
| 1811 | desc.fFlags = kRenderTarget_GrTextureFlagBit; |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 1812 | desc.fWidth = info.width(); |
| 1813 | desc.fHeight = info.height(); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1814 | desc.fSampleCnt = fRenderTarget->numSamples(); |
| 1815 | |
| 1816 | SkAutoTUnref<GrTexture> texture; |
| 1817 | // Skia's convention is to only clear a device if it is non-opaque. |
commit-bot@chromium.org | d8a57af | 2014-03-19 21:19:16 +0000 | [diff] [blame] | 1818 | unsigned flags = info.isOpaque() ? 0 : kNeedClear_Flag; |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1819 | |
| 1820 | #if CACHE_COMPATIBLE_DEVICE_TEXTURES |
| 1821 | // layers are never draw in repeat modes, so we can request an approx |
| 1822 | // match and ignore any padding. |
commit-bot@chromium.org | d8a57af | 2014-03-19 21:19:16 +0000 | [diff] [blame] | 1823 | flags |= kCached_Flag; |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1824 | const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == usage) ? |
| 1825 | GrContext::kApprox_ScratchTexMatch : |
| 1826 | GrContext::kExact_ScratchTexMatch; |
| 1827 | texture.reset(fContext->lockAndRefScratchTexture(desc, match)); |
| 1828 | #else |
| 1829 | texture.reset(fContext->createUncachedTexture(desc, NULL, 0)); |
| 1830 | #endif |
| 1831 | if (NULL != texture.get()) { |
commit-bot@chromium.org | d8a57af | 2014-03-19 21:19:16 +0000 | [diff] [blame] | 1832 | return SkGpuDevice::Create(texture, flags); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1833 | } else { |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 1834 | GrPrintf("---- failed to create compatible device texture [%d %d]\n", |
| 1835 | info.width(), info.height()); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1836 | return NULL; |
| 1837 | } |
| 1838 | } |
| 1839 | |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 1840 | SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info) { |
| 1841 | return SkSurface::NewRenderTarget(fContext, info, fRenderTarget->numSamples()); |
| 1842 | } |
| 1843 | |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 1844 | void SkGpuDevice::EXPERIMENTAL_optimize(const SkPicture* picture) { |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 1845 | SkPicture::AccelData::Key key = GPUAccelData::ComputeAccelDataKey(); |
commit-bot@chromium.org | 145d1c0 | 2014-03-16 19:46:36 +0000 | [diff] [blame] | 1846 | |
commit-bot@chromium.org | 8ec8bab | 2014-05-14 13:11:48 +0000 | [diff] [blame] | 1847 | const SkPicture::AccelData* existing = picture->EXPERIMENTAL_getAccelData(key); |
| 1848 | if (NULL != existing) { |
| 1849 | return; |
| 1850 | } |
| 1851 | |
commit-bot@chromium.org | 8fd9382 | 2014-05-06 13:43:22 +0000 | [diff] [blame] | 1852 | SkAutoTUnref<GPUAccelData> data(SkNEW_ARGS(GPUAccelData, (key))); |
commit-bot@chromium.org | 145d1c0 | 2014-03-16 19:46:36 +0000 | [diff] [blame] | 1853 | |
| 1854 | picture->EXPERIMENTAL_addAccelData(data); |
commit-bot@chromium.org | 8ddc26b | 2014-03-31 17:55:12 +0000 | [diff] [blame] | 1855 | |
| 1856 | GatherGPUInfo(picture, data); |
commit-bot@chromium.org | 145d1c0 | 2014-03-16 19:46:36 +0000 | [diff] [blame] | 1857 | } |
| 1858 | |
robertphillips@google.com | beb1af2 | 2014-05-07 21:31:09 +0000 | [diff] [blame] | 1859 | static void wrap_texture(GrTexture* texture, int width, int height, SkBitmap* result) { |
| 1860 | SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 1861 | result->setInfo(info); |
robertphillips@google.com | beb1af2 | 2014-05-07 21:31:09 +0000 | [diff] [blame] | 1862 | result->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref(); |
| 1863 | } |
| 1864 | |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 1865 | void SkGpuDevice::EXPERIMENTAL_purge(const SkPicture* picture) { |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 1866 | fContext->getLayerCache()->purge(picture); |
commit-bot@chromium.org | c873329 | 2014-04-11 15:54:14 +0000 | [diff] [blame] | 1867 | } |
| 1868 | |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 1869 | bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* canvas, const SkPicture* picture) { |
commit-bot@chromium.org | 8ddc26b | 2014-03-31 17:55:12 +0000 | [diff] [blame] | 1870 | |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 1871 | SkPicture::AccelData::Key key = GPUAccelData::ComputeAccelDataKey(); |
commit-bot@chromium.org | 145d1c0 | 2014-03-16 19:46:36 +0000 | [diff] [blame] | 1872 | |
commit-bot@chromium.org | 8ddc26b | 2014-03-31 17:55:12 +0000 | [diff] [blame] | 1873 | const SkPicture::AccelData* data = picture->EXPERIMENTAL_getAccelData(key); |
commit-bot@chromium.org | 145d1c0 | 2014-03-16 19:46:36 +0000 | [diff] [blame] | 1874 | if (NULL == data) { |
| 1875 | return false; |
| 1876 | } |
| 1877 | |
commit-bot@chromium.org | 145d1c0 | 2014-03-16 19:46:36 +0000 | [diff] [blame] | 1878 | const GPUAccelData *gpuData = static_cast<const GPUAccelData*>(data); |
commit-bot@chromium.org | 8ddc26b | 2014-03-31 17:55:12 +0000 | [diff] [blame] | 1879 | |
commit-bot@chromium.org | 8ec8bab | 2014-05-14 13:11:48 +0000 | [diff] [blame] | 1880 | if (0 == gpuData->numSaveLayers()) { |
| 1881 | return false; |
| 1882 | } |
| 1883 | |
commit-bot@chromium.org | 8ddc26b | 2014-03-31 17:55:12 +0000 | [diff] [blame] | 1884 | SkAutoTArray<bool> pullForward(gpuData->numSaveLayers()); |
| 1885 | for (int i = 0; i < gpuData->numSaveLayers(); ++i) { |
| 1886 | pullForward[i] = false; |
| 1887 | } |
| 1888 | |
robertphillips@google.com | beb1af2 | 2014-05-07 21:31:09 +0000 | [diff] [blame] | 1889 | SkRect clipBounds; |
| 1890 | if (!canvas->getClipBounds(&clipBounds)) { |
| 1891 | return true; |
commit-bot@chromium.org | 8ddc26b | 2014-03-31 17:55:12 +0000 | [diff] [blame] | 1892 | } |
robertphillips@google.com | beb1af2 | 2014-05-07 21:31:09 +0000 | [diff] [blame] | 1893 | SkIRect query; |
| 1894 | clipBounds.roundOut(&query); |
commit-bot@chromium.org | 8ddc26b | 2014-03-31 17:55:12 +0000 | [diff] [blame] | 1895 | |
robertphillips | ce4dd3d | 2014-07-07 13:46:35 -0700 | [diff] [blame] | 1896 | SkAutoTDelete<const SkPicture::OperationList> ops(picture->EXPERIMENTAL_getActiveOps(query)); |
commit-bot@chromium.org | 8ddc26b | 2014-03-31 17:55:12 +0000 | [diff] [blame] | 1897 | |
robertphillips@google.com | beb1af2 | 2014-05-07 21:31:09 +0000 | [diff] [blame] | 1898 | // This code pre-renders the entire layer since it will be cached and potentially |
| 1899 | // reused with different clips (e.g., in different tiles). Because of this the |
| 1900 | // clip will not be limiting the size of the pre-rendered layer. kSaveLayerMaxSize |
| 1901 | // is used to limit which clips are pre-rendered. |
| 1902 | static const int kSaveLayerMaxSize = 256; |
commit-bot@chromium.org | 8ddc26b | 2014-03-31 17:55:12 +0000 | [diff] [blame] | 1903 | |
robertphillips | ce4dd3d | 2014-07-07 13:46:35 -0700 | [diff] [blame] | 1904 | if (NULL != ops.get()) { |
robertphillips@google.com | beb1af2 | 2014-05-07 21:31:09 +0000 | [diff] [blame] | 1905 | // In this case the picture has been generated with a BBH so we use |
skia.committer@gmail.com | b2c82c9 | 2014-05-08 03:05:29 +0000 | [diff] [blame] | 1906 | // the BBH to limit the pre-rendering to just the layers needed to cover |
| 1907 | // the region being drawn |
robertphillips | ce4dd3d | 2014-07-07 13:46:35 -0700 | [diff] [blame] | 1908 | for (int i = 0; i < ops->numOps(); ++i) { |
| 1909 | uint32_t offset = ops->offset(i); |
robertphillips@google.com | beb1af2 | 2014-05-07 21:31:09 +0000 | [diff] [blame] | 1910 | |
| 1911 | // For now we're saving all the layers in the GPUAccelData so they |
skia.committer@gmail.com | b2c82c9 | 2014-05-08 03:05:29 +0000 | [diff] [blame] | 1912 | // can be nested. Additionally, the nested layers appear before |
robertphillips@google.com | beb1af2 | 2014-05-07 21:31:09 +0000 | [diff] [blame] | 1913 | // their parent in the list. |
| 1914 | for (int j = 0 ; j < gpuData->numSaveLayers(); ++j) { |
| 1915 | const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(j); |
| 1916 | |
| 1917 | if (pullForward[j]) { |
| 1918 | continue; // already pulling forward |
| 1919 | } |
| 1920 | |
| 1921 | if (offset < info.fSaveLayerOpID || offset > info.fRestoreOpID) { |
| 1922 | continue; // the op isn't in this range |
| 1923 | } |
| 1924 | |
| 1925 | // TODO: once this code is more stable unsuitable layers can |
| 1926 | // just be omitted during the optimization stage |
| 1927 | if (!info.fValid || |
| 1928 | kSaveLayerMaxSize < info.fSize.fWidth || |
| 1929 | kSaveLayerMaxSize < info.fSize.fHeight || |
| 1930 | info.fIsNested) { |
| 1931 | continue; // this layer is unsuitable |
| 1932 | } |
| 1933 | |
| 1934 | pullForward[j] = true; |
| 1935 | } |
| 1936 | } |
| 1937 | } else { |
| 1938 | // In this case there is no BBH associated with the picture. Pre-render |
commit-bot@chromium.org | f97d65d | 2014-05-08 23:24:05 +0000 | [diff] [blame] | 1939 | // all the layers that intersect the drawn region |
commit-bot@chromium.org | 8ddc26b | 2014-03-31 17:55:12 +0000 | [diff] [blame] | 1940 | for (int j = 0; j < gpuData->numSaveLayers(); ++j) { |
| 1941 | const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(j); |
| 1942 | |
commit-bot@chromium.org | f97d65d | 2014-05-08 23:24:05 +0000 | [diff] [blame] | 1943 | SkIRect layerRect = SkIRect::MakeXYWH(info.fOffset.fX, |
| 1944 | info.fOffset.fY, |
| 1945 | info.fSize.fWidth, |
| 1946 | info.fSize.fHeight); |
| 1947 | |
| 1948 | if (!SkIRect::Intersects(query, layerRect)) { |
| 1949 | continue; |
| 1950 | } |
| 1951 | |
robertphillips@google.com | beb1af2 | 2014-05-07 21:31:09 +0000 | [diff] [blame] | 1952 | // TODO: once this code is more stable unsuitable layers can |
| 1953 | // just be omitted during the optimization stage |
| 1954 | if (!info.fValid || |
| 1955 | kSaveLayerMaxSize < info.fSize.fWidth || |
| 1956 | kSaveLayerMaxSize < info.fSize.fHeight || |
| 1957 | info.fIsNested) { |
| 1958 | continue; |
| 1959 | } |
| 1960 | |
skia.committer@gmail.com | b2c82c9 | 2014-05-08 03:05:29 +0000 | [diff] [blame] | 1961 | pullForward[j] = true; |
robertphillips@google.com | beb1af2 | 2014-05-07 21:31:09 +0000 | [diff] [blame] | 1962 | } |
| 1963 | } |
| 1964 | |
robertphillips | c26d991 | 2014-07-10 07:21:27 -0700 | [diff] [blame] | 1965 | SkPictureReplacementPlayback::PlaybackReplacements replacements; |
robertphillips@google.com | beb1af2 | 2014-05-07 21:31:09 +0000 | [diff] [blame] | 1966 | |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 1967 | // Generate the layer and/or ensure it is locked |
robertphillips@google.com | beb1af2 | 2014-05-07 21:31:09 +0000 | [diff] [blame] | 1968 | for (int i = 0; i < gpuData->numSaveLayers(); ++i) { |
| 1969 | if (pullForward[i]) { |
| 1970 | GrCachedLayer* layer = fContext->getLayerCache()->findLayerOrCreate(picture, i); |
| 1971 | |
| 1972 | const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(i); |
| 1973 | |
robertphillips | c26d991 | 2014-07-10 07:21:27 -0700 | [diff] [blame] | 1974 | SkPictureReplacementPlayback::PlaybackReplacements::ReplacementInfo* layerInfo = |
robertphillips@google.com | beb1af2 | 2014-05-07 21:31:09 +0000 | [diff] [blame] | 1975 | replacements.push(); |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 1976 | layerInfo->fStart = info.fSaveLayerOpID; |
| 1977 | layerInfo->fStop = info.fRestoreOpID; |
| 1978 | layerInfo->fPos = info.fOffset; |
robertphillips@google.com | beb1af2 | 2014-05-07 21:31:09 +0000 | [diff] [blame] | 1979 | |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 1980 | GrTextureDesc desc; |
| 1981 | desc.fFlags = kRenderTarget_GrTextureFlagBit; |
| 1982 | desc.fWidth = info.fSize.fWidth; |
| 1983 | desc.fHeight = info.fSize.fHeight; |
| 1984 | desc.fConfig = kSkia8888_GrPixelConfig; |
| 1985 | // TODO: need to deal with sample count |
robertphillips@google.com | beb1af2 | 2014-05-07 21:31:09 +0000 | [diff] [blame] | 1986 | |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 1987 | bool needsRendering = !fContext->getLayerCache()->lock(layer, desc); |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 1988 | if (NULL == layer->texture()) { |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 1989 | continue; |
| 1990 | } |
robertphillips@google.com | beb1af2 | 2014-05-07 21:31:09 +0000 | [diff] [blame] | 1991 | |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 1992 | layerInfo->fBM = SkNEW(SkBitmap); // fBM is allocated so ReplacementInfo can be POD |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 1993 | wrap_texture(layer->texture(), |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 1994 | !layer->isAtlased() ? desc.fWidth : layer->texture()->width(), |
| 1995 | !layer->isAtlased() ? desc.fHeight : layer->texture()->height(), |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 1996 | layerInfo->fBM); |
robertphillips@google.com | beb1af2 | 2014-05-07 21:31:09 +0000 | [diff] [blame] | 1997 | |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 1998 | SkASSERT(info.fPaint); |
| 1999 | layerInfo->fPaint = info.fPaint; |
robertphillips@google.com | beb1af2 | 2014-05-07 21:31:09 +0000 | [diff] [blame] | 2000 | |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 2001 | layerInfo->fSrcRect = SkIRect::MakeXYWH(layer->rect().fLeft, |
| 2002 | layer->rect().fTop, |
| 2003 | layer->rect().width(), |
| 2004 | layer->rect().height()); |
| 2005 | |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 2006 | |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 2007 | if (needsRendering) { |
| 2008 | SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTargetDirect( |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 2009 | layer->texture()->asRenderTarget(), |
| 2010 | SkSurface::kStandard_TextRenderMode, |
| 2011 | SkSurface::kDontClear_RenderTargetFlag)); |
robertphillips@google.com | beb1af2 | 2014-05-07 21:31:09 +0000 | [diff] [blame] | 2012 | |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 2013 | SkCanvas* canvas = surface->getCanvas(); |
robertphillips@google.com | beb1af2 | 2014-05-07 21:31:09 +0000 | [diff] [blame] | 2014 | |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 2015 | // Add a rect clip to make sure the rendering doesn't |
| 2016 | // extend beyond the boundaries of the atlased sub-rect |
| 2017 | SkRect bound = SkRect::Make(layerInfo->fSrcRect); |
| 2018 | canvas->clipRect(bound); |
| 2019 | |
| 2020 | if (layer->isAtlased()) { |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 2021 | // Since 'clear' doesn't respect the clip we need to draw a rect |
| 2022 | // TODO: ensure none of the atlased layers contain a clear call! |
| 2023 | SkPaint paint; |
| 2024 | paint.setColor(SK_ColorTRANSPARENT); |
| 2025 | canvas->drawRect(bound, paint); |
| 2026 | } else { |
| 2027 | canvas->clear(SK_ColorTRANSPARENT); |
| 2028 | } |
| 2029 | |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 2030 | // info.fCTM maps the layer's top/left to the origin. |
| 2031 | // If this layer is atlased the top/left corner needs |
| 2032 | // to be offset to some arbitrary location in the backing |
| 2033 | // texture. |
| 2034 | canvas->translate(bound.fLeft, bound.fTop); |
| 2035 | canvas->concat(info.fCTM); |
robertphillips@google.com | beb1af2 | 2014-05-07 21:31:09 +0000 | [diff] [blame] | 2036 | |
robertphillips | 1ad00e4 | 2014-07-08 08:28:08 -0700 | [diff] [blame] | 2037 | SkPictureRangePlayback rangePlayback(picture, |
| 2038 | info.fSaveLayerOpID, |
| 2039 | info.fRestoreOpID); |
| 2040 | rangePlayback.draw(canvas, NULL); |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 2041 | |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 2042 | canvas->flush(); |
commit-bot@chromium.org | 8ddc26b | 2014-03-31 17:55:12 +0000 | [diff] [blame] | 2043 | } |
| 2044 | } |
| 2045 | } |
| 2046 | |
robertphillips@google.com | beb1af2 | 2014-05-07 21:31:09 +0000 | [diff] [blame] | 2047 | // Playback using new layers |
robertphillips | c26d991 | 2014-07-10 07:21:27 -0700 | [diff] [blame] | 2048 | SkPictureReplacementPlayback playback(picture, &replacements, ops.get()); |
robertphillips | ce4dd3d | 2014-07-07 13:46:35 -0700 | [diff] [blame] | 2049 | |
robertphillips | ce4dd3d | 2014-07-07 13:46:35 -0700 | [diff] [blame] | 2050 | playback.draw(canvas, NULL); |
robertphillips@google.com | beb1af2 | 2014-05-07 21:31:09 +0000 | [diff] [blame] | 2051 | |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 2052 | // unlock the layers |
robertphillips@google.com | beb1af2 | 2014-05-07 21:31:09 +0000 | [diff] [blame] | 2053 | for (int i = 0; i < gpuData->numSaveLayers(); ++i) { |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 2054 | GrCachedLayer* layer = fContext->getLayerCache()->findLayer(picture, i); |
| 2055 | fContext->getLayerCache()->unlock(layer); |
robertphillips@google.com | beb1af2 | 2014-05-07 21:31:09 +0000 | [diff] [blame] | 2056 | } |
| 2057 | |
| 2058 | return true; |
commit-bot@chromium.org | 145d1c0 | 2014-03-16 19:46:36 +0000 | [diff] [blame] | 2059 | } |