gfx/skia/trunk/src/gpu/SkGpuDevice.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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

mercurial