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