blob: 4294e490724304b7584d1a2311fab28f4ecf0f1e [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.comac10a2d2010-12-22 21:39:39 +000011#include "GrContext.h"
12#include "GrTextContext.h"
13
reed@google.comac10a2d2010-12-22 21:39:39 +000014#include "SkGpuDevice.h"
15#include "SkGrTexturePixelRef.h"
16
Scroggo97c88c22011-05-11 14:05:25 +000017#include "SkColorFilter.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000018#include "SkDrawProcs.h"
19#include "SkGlyphCache.h"
reed@google.comc9aa5872011-04-05 21:05:37 +000020#include "SkUtils.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000021
22#define CACHE_LAYER_TEXTURES 1
23
24#if 0
25 extern bool (*gShouldDrawProc)();
26 #define CHECK_SHOULD_DRAW(draw) \
27 do { \
28 if (gShouldDrawProc && !gShouldDrawProc()) return; \
29 this->prepareRenderTarget(draw); \
30 } while (0)
31#else
32 #define CHECK_SHOULD_DRAW(draw) this->prepareRenderTarget(draw)
33#endif
34
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000035// we use the same texture slot on GrPaint for bitmaps and shaders
36// (since drawBitmap, drawSprite, and drawDevice ignore skia's shader)
37enum {
38 kBitmapTextureIdx = 0,
39 kShaderTextureIdx = 0
40};
41
reed@google.comcde92112011-07-06 20:00:52 +000042
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +000043#define MAX_BLUR_SIGMA 4.0f
44// FIXME: This value comes from from SkBlurMaskFilter.cpp.
45// Should probably be put in a common header someplace.
46#define MAX_BLUR_RADIUS SkIntToScalar(128)
47// This constant approximates the scaling done in the software path's
48// "high quality" mode, in SkBlurMask::Blur() (1 / sqrt(3)).
49// IMHO, it actually should be 1: we blur "less" than we should do
50// according to the CSS and canvas specs, simply because Safari does the same.
51// Firefox used to do the same too, until 4.0 where they fixed it. So at some
52// point we should probably get rid of these scaling constants and rebaseline
53// all the blur tests.
54#define BLUR_SIGMA_SCALE 0.6f
reed@google.comac10a2d2010-12-22 21:39:39 +000055///////////////////////////////////////////////////////////////////////////////
56
57SkGpuDevice::SkAutoCachedTexture::
58 SkAutoCachedTexture(SkGpuDevice* device,
59 const SkBitmap& bitmap,
60 const GrSamplerState& sampler,
61 GrTexture** texture) {
62 GrAssert(texture);
reed@google.comac10a2d2010-12-22 21:39:39 +000063 *texture = this->set(device, bitmap, sampler);
64}
65
66SkGpuDevice::SkAutoCachedTexture::SkAutoCachedTexture() {
reed@google.comac10a2d2010-12-22 21:39:39 +000067}
68
69GrTexture* SkGpuDevice::SkAutoCachedTexture::set(SkGpuDevice* device,
70 const SkBitmap& bitmap,
71 const GrSamplerState& sampler) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +000072 if (fTex.texture()) {
reed@google.comac10a2d2010-12-22 21:39:39 +000073 fDevice->unlockCachedTexture(fTex);
74 }
75 fDevice = device;
76 GrTexture* texture = (GrTexture*)bitmap.getTexture();
77 if (texture) {
78 // return the native texture
bsalomon@google.com50398bf2011-07-26 20:45:30 +000079 fTex.reset();
reed@google.comac10a2d2010-12-22 21:39:39 +000080 } else {
81 // look it up in our cache
bsalomon@google.com50398bf2011-07-26 20:45:30 +000082 fTex = device->lockCachedTexture(bitmap, sampler);
83 texture = fTex.texture();
reed@google.comac10a2d2010-12-22 21:39:39 +000084 }
85 return texture;
86}
87
88SkGpuDevice::SkAutoCachedTexture::~SkAutoCachedTexture() {
bsalomon@google.com50398bf2011-07-26 20:45:30 +000089 if (fTex.texture()) {
reed@google.comac10a2d2010-12-22 21:39:39 +000090 fDevice->unlockCachedTexture(fTex);
91 }
92}
93
94///////////////////////////////////////////////////////////////////////////////
95
96bool gDoTraceDraw;
97
98struct GrSkDrawProcs : public SkDrawProcs {
99public:
100 GrContext* fContext;
101 GrTextContext* fTextContext;
102 GrFontScaler* fFontScaler; // cached in the skia glyphcache
103};
104
105///////////////////////////////////////////////////////////////////////////////
106
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000107GrRenderTarget* SkGpuDevice::Current3DApiRenderTarget() {
108 return (GrRenderTarget*) -1;
109}
110
reed@google.comaf951c92011-06-16 19:10:39 +0000111static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
112 switch (config) {
113 case kAlpha_8_GrPixelConfig:
114 *isOpaque = false;
115 return SkBitmap::kA8_Config;
116 case kRGB_565_GrPixelConfig:
117 *isOpaque = true;
118 return SkBitmap::kRGB_565_Config;
119 case kRGBA_4444_GrPixelConfig:
120 *isOpaque = false;
121 return SkBitmap::kARGB_4444_Config;
122 case kRGBA_8888_GrPixelConfig:
123 case kRGBX_8888_GrPixelConfig:
124 *isOpaque = (kRGBX_8888_GrPixelConfig == config);
125 return SkBitmap::kARGB_8888_Config;
126 default:
127 *isOpaque = false;
128 return SkBitmap::kNo_Config;
129 }
130}
reed@google.comac10a2d2010-12-22 21:39:39 +0000131
reed@google.comaf951c92011-06-16 19:10:39 +0000132static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
133 if (SkGpuDevice::Current3DApiRenderTarget() == renderTarget) {
134 renderTarget = context->createRenderTargetFrom3DApiState();
135 }
136 GrTexture* texture = renderTarget->asTexture();
137 GrPixelConfig config = texture ? texture->config() : kRGBA_8888_GrPixelConfig;
138
139 bool isOpaque;
140 SkBitmap bitmap;
141 bitmap.setConfig(grConfig2skConfig(config, &isOpaque),
142 renderTarget->width(), renderTarget->height());
143 bitmap.setIsOpaque(isOpaque);
144 return bitmap;
145}
146
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000147SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
148: SkDevice(make_bitmap(context, texture->asRenderTarget())) {
149 this->initFromRenderTarget(context, texture->asRenderTarget());
150}
151
reed@google.comaf951c92011-06-16 19:10:39 +0000152SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
153: SkDevice(make_bitmap(context, renderTarget)) {
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000154 this->initFromRenderTarget(context, renderTarget);
155}
156
157void SkGpuDevice::initFromRenderTarget(GrContext* context,
158 GrRenderTarget* renderTarget) {
reed@google.comaf951c92011-06-16 19:10:39 +0000159 fNeedPrepareRenderTarget = false;
160 fDrawProcs = NULL;
161
162 fContext = context;
163 fContext->ref();
164
reed@google.comaf951c92011-06-16 19:10:39 +0000165 fTexture = NULL;
166 fRenderTarget = NULL;
167 fNeedClear = false;
168
169 if (Current3DApiRenderTarget() == renderTarget) {
170 fRenderTarget = fContext->createRenderTargetFrom3DApiState();
171 } else {
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000172 GrAssert(NULL != renderTarget);
reed@google.comaf951c92011-06-16 19:10:39 +0000173 fRenderTarget = renderTarget;
174 fRenderTarget->ref();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000175 // if this RT is also a texture, hold a ref on it
176 fTexture = fRenderTarget->asTexture();
177 SkSafeRef(fTexture);
reed@google.comaf951c92011-06-16 19:10:39 +0000178 }
179
180 SkGrRenderTargetPixelRef* pr = new SkGrRenderTargetPixelRef(fRenderTarget);
181 this->setPixelRef(pr, 0)->unref();
182}
183
184SkGpuDevice::SkGpuDevice(GrContext* context, SkBitmap::Config config, int width,
bsalomon@google.come97f0852011-06-17 13:10:25 +0000185 int height, Usage usage)
reed@google.comaf951c92011-06-16 19:10:39 +0000186: SkDevice(config, width, height, false /*isOpaque*/) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000187 fNeedPrepareRenderTarget = false;
188 fDrawProcs = NULL;
189
reed@google.com7b201d22011-01-11 18:59:23 +0000190 fContext = context;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000191 fContext->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000192
reed@google.comac10a2d2010-12-22 21:39:39 +0000193 fTexture = NULL;
194 fRenderTarget = NULL;
195 fNeedClear = false;
196
reed@google.comaf951c92011-06-16 19:10:39 +0000197 if (config != SkBitmap::kRGB_565_Config) {
198 config = SkBitmap::kARGB_8888_Config;
199 }
200 SkBitmap bm;
201 bm.setConfig(config, width, height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000202
203#if CACHE_LAYER_TEXTURES
bsalomon@google.come97f0852011-06-17 13:10:25 +0000204 TexType type = (kSaveLayer_Usage == usage) ?
205 kSaveLayerDeviceRenderTarget_TexType :
206 kDeviceRenderTarget_TexType;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000207 fCache = this->lockCachedTexture(bm, GrSamplerState::ClampNoFilter(), type);
208 fTexture = fCache.texture();
209 if (fTexture) {
reed@google.comaf951c92011-06-16 19:10:39 +0000210 SkASSERT(NULL != fTexture->asRenderTarget());
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000211 // hold a ref directly on fTexture (even though fCache has one) to match
212 // other constructor paths. Simplifies cleanup.
213 fTexture->ref();
reed@google.comaf951c92011-06-16 19:10:39 +0000214 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000215#else
reed@google.comaf951c92011-06-16 19:10:39 +0000216 const GrTextureDesc desc = {
217 kRenderTarget_GrTextureFlagBit,
218 kNone_GrAALevel,
219 width,
220 height,
221 SkGr::Bitmap2PixelConfig(bm)
222 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000223
reed@google.comaf951c92011-06-16 19:10:39 +0000224 fTexture = fContext->createUncachedTexture(desc, NULL, 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000225#endif
reed@google.comaf951c92011-06-16 19:10:39 +0000226 if (NULL != fTexture) {
227 fRenderTarget = fTexture->asRenderTarget();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000228 fRenderTarget->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000229
reed@google.comaf951c92011-06-16 19:10:39 +0000230 GrAssert(NULL != fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000231
reed@google.comaf951c92011-06-16 19:10:39 +0000232 // we defer the actual clear until our gainFocus()
233 fNeedClear = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000234
reed@google.comaf951c92011-06-16 19:10:39 +0000235 // wrap the bitmap with a pixelref to expose our texture
236 SkGrTexturePixelRef* pr = new SkGrTexturePixelRef(fTexture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000237 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000238 } else {
239 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
240 width, height);
241 GrAssert(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000242 }
243}
244
245SkGpuDevice::~SkGpuDevice() {
246 if (fDrawProcs) {
247 delete fDrawProcs;
248 }
249
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000250 SkSafeUnref(fTexture);
251 SkSafeUnref(fRenderTarget);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000252 if (fCache.texture()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000253 GrAssert(NULL != fTexture);
254 GrAssert(fRenderTarget == fTexture->asRenderTarget());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000255 fContext->unlockTexture(fCache);
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000256 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000257 fContext->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000258}
259
reed@google.comac10a2d2010-12-22 21:39:39 +0000260///////////////////////////////////////////////////////////////////////////////
261
262void SkGpuDevice::makeRenderTargetCurrent() {
263 fContext->setRenderTarget(fRenderTarget);
264 fContext->flush(true);
265 fNeedPrepareRenderTarget = true;
266}
267
268///////////////////////////////////////////////////////////////////////////////
269
270bool SkGpuDevice::readPixels(const SkIRect& srcRect, SkBitmap* bitmap) {
271 SkIRect bounds;
272 bounds.set(0, 0, this->width(), this->height());
273 if (!bounds.intersect(srcRect)) {
274 return false;
275 }
276
277 const int w = bounds.width();
278 const int h = bounds.height();
279 SkBitmap tmp;
280 // note we explicitly specify our rowBytes to be snug (no gap between rows)
281 tmp.setConfig(SkBitmap::kARGB_8888_Config, w, h, w * 4);
282 if (!tmp.allocPixels()) {
283 return false;
284 }
285
Scroggo813c33c2011-04-07 20:56:21 +0000286 tmp.lockPixels();
reed@google.comac10a2d2010-12-22 21:39:39 +0000287
Scroggoeb176032011-04-07 21:11:49 +0000288 bool read = fContext->readRenderTargetPixels(fRenderTarget,
289 bounds.fLeft, bounds.fTop,
290 bounds.width(), bounds.height(),
291 kRGBA_8888_GrPixelConfig,
292 tmp.getPixels());
Scroggo813c33c2011-04-07 20:56:21 +0000293 tmp.unlockPixels();
294 if (!read) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000295 return false;
296 }
297
298 tmp.swap(*bitmap);
299 return true;
300}
301
302void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y) {
303 SkAutoLockPixels alp(bitmap);
304 if (!bitmap.readyToDraw()) {
305 return;
306 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000307 GrPixelConfig config = SkGr::BitmapConfig2PixelConfig(bitmap.config(),
308 bitmap.isOpaque());
reed@google.comac10a2d2010-12-22 21:39:39 +0000309 fContext->setRenderTarget(fRenderTarget);
310 // we aren't setting the clip or matrix, so mark as dirty
311 // we don't need to set them for this call and don't have them anyway
312 fNeedPrepareRenderTarget = true;
313
314 fContext->writePixels(x, y, bitmap.width(), bitmap.height(),
315 config, bitmap.getPixels(), bitmap.rowBytes());
316}
317
318///////////////////////////////////////////////////////////////////////////////
319
320static void convert_matrixclip(GrContext* context, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000321 const SkClipStack& clipStack,
reed@google.com6f8f2922011-03-04 22:27:10 +0000322 const SkRegion& clipRegion,
323 const SkIPoint& origin) {
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000324 context->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000325
326 SkGrClipIterator iter;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000327 iter.reset(clipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000328 const SkIRect& skBounds = clipRegion.getBounds();
329 GrRect bounds;
330 bounds.setLTRB(GrIntToScalar(skBounds.fLeft),
331 GrIntToScalar(skBounds.fTop),
332 GrIntToScalar(skBounds.fRight),
333 GrIntToScalar(skBounds.fBottom));
reed@google.com6f8f2922011-03-04 22:27:10 +0000334 GrClip grc(&iter, GrIntToScalar(-origin.x()), GrIntToScalar(-origin.y()),
335 &bounds);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000336 context->setClip(grc);
reed@google.comac10a2d2010-12-22 21:39:39 +0000337}
338
339// call this ever each draw call, to ensure that the context reflects our state,
340// and not the state from some other canvas/device
341void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) {
342 if (fNeedPrepareRenderTarget ||
bsalomon@google.com5782d712011-01-21 21:03:59 +0000343 fContext->getRenderTarget() != fRenderTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000344
345 fContext->setRenderTarget(fRenderTarget);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000346 SkASSERT(draw.fClipStack);
347 convert_matrixclip(fContext, *draw.fMatrix,
reed@google.com6f8f2922011-03-04 22:27:10 +0000348 *draw.fClipStack, *draw.fClip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000349 fNeedPrepareRenderTarget = false;
350 }
351}
352
reed@google.com46799cd2011-02-22 20:56:26 +0000353void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
354 const SkClipStack& clipStack) {
355 this->INHERITED::setMatrixClip(matrix, clip, clipStack);
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000356 // We don't need to set them now because the context may not reflect this device.
357 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000358}
359
360void SkGpuDevice::gainFocus(SkCanvas* canvas, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000361 const SkRegion& clip, const SkClipStack& clipStack) {
362
reed@google.comac10a2d2010-12-22 21:39:39 +0000363 fContext->setRenderTarget(fRenderTarget);
364
bsalomon@google.comd302f142011-03-03 13:54:13 +0000365 this->INHERITED::gainFocus(canvas, matrix, clip, clipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000366
reed@google.com6f8f2922011-03-04 22:27:10 +0000367 convert_matrixclip(fContext, matrix, clipStack, clip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000368
369 if (fNeedClear) {
bsalomon@google.com31a58402011-04-27 21:00:02 +0000370 fContext->clear(NULL, 0x0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000371 fNeedClear = false;
372 }
373}
374
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000375bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000376 if (NULL != fTexture) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000377 paint->setTexture(kBitmapTextureIdx, fTexture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000378 return true;
379 }
380 return false;
381}
382
383///////////////////////////////////////////////////////////////////////////////
384
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000385SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
386SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
387SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
388SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
389SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
390 shader_type_mismatch);
391SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 4, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000392
bsalomon@google.com5782d712011-01-21 21:03:59 +0000393static const GrSamplerState::SampleMode sk_bmp_type_to_sample_mode[] = {
394 (GrSamplerState::SampleMode) -1, // kNone_BitmapType
395 GrSamplerState::kNormal_SampleMode, // kDefault_BitmapType
396 GrSamplerState::kRadial_SampleMode, // kRadial_BitmapType
397 GrSamplerState::kSweep_SampleMode, // kSweep_BitmapType
398 GrSamplerState::kRadial2_SampleMode, // kTwoPointRadial_BitmapType
399};
400
401bool SkGpuDevice::skPaint2GrPaintNoShader(const SkPaint& skPaint,
402 bool justAlpha,
Scroggod757df22011-05-16 13:11:16 +0000403 GrPaint* grPaint,
404 bool constantColor) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000405
406 grPaint->fDither = skPaint.isDither();
407 grPaint->fAntiAlias = skPaint.isAntiAlias();
408
409 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
410 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
411
412 SkXfermode* mode = skPaint.getXfermode();
413 if (mode) {
414 if (!mode->asCoeff(&sm, &dm)) {
reed@google.com1a2e8d22011-01-21 22:08:29 +0000415 SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000416#if 0
417 return false;
418#endif
419 }
420 }
421 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
422 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
423
424 if (justAlpha) {
425 uint8_t alpha = skPaint.getAlpha();
426 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000427 // justAlpha is currently set to true only if there is a texture,
428 // so constantColor should not also be true.
429 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000430 } else {
431 grPaint->fColor = SkGr::SkColor2GrColor(skPaint.getColor());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000432 grPaint->setTexture(kShaderTextureIdx, NULL);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000433 }
Scroggo97c88c22011-05-11 14:05:25 +0000434 SkColorFilter* colorFilter = skPaint.getColorFilter();
435 SkColor color;
436 SkXfermode::Mode filterMode;
437 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
Scroggod757df22011-05-16 13:11:16 +0000438 if (!constantColor) {
439 grPaint->fColorFilterColor = SkGr::SkColor2GrColor(color);
440 grPaint->fColorFilterXfermode = filterMode;
441 return true;
442 }
443 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
444 grPaint->fColor = SkGr::SkColor2GrColor(filtered);
Scroggo97c88c22011-05-11 14:05:25 +0000445 }
Scroggod757df22011-05-16 13:11:16 +0000446 grPaint->resetColorFilter();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000447 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000448}
449
bsalomon@google.com5782d712011-01-21 21:03:59 +0000450bool SkGpuDevice::skPaint2GrPaintShader(const SkPaint& skPaint,
451 SkAutoCachedTexture* act,
452 const SkMatrix& ctm,
Scroggod757df22011-05-16 13:11:16 +0000453 GrPaint* grPaint,
454 bool constantColor) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000455
bsalomon@google.com5782d712011-01-21 21:03:59 +0000456 SkASSERT(NULL != act);
reed@google.comac10a2d2010-12-22 21:39:39 +0000457
bsalomon@google.com5782d712011-01-21 21:03:59 +0000458 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000459 if (NULL == shader) {
Scroggod757df22011-05-16 13:11:16 +0000460 return this->skPaint2GrPaintNoShader(skPaint,
461 false,
462 grPaint,
463 constantColor);
464 } else if (!this->skPaint2GrPaintNoShader(skPaint, true, grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000465 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000466 }
467
reed@google.comac10a2d2010-12-22 21:39:39 +0000468 SkBitmap bitmap;
469 SkMatrix matrix;
470 SkShader::TileMode tileModes[2];
471 SkScalar twoPointParams[3];
472 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, &matrix,
473 tileModes, twoPointParams);
474
bsalomon@google.com5782d712011-01-21 21:03:59 +0000475 GrSamplerState::SampleMode sampleMode = sk_bmp_type_to_sample_mode[bmptype];
476 if (-1 == sampleMode) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000477 SkShader::GradientInfo info;
478 SkColor color;
479
480 info.fColors = &color;
481 info.fColorOffsets = NULL;
482 info.fColorCount = 1;
483 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
484 SkPaint copy(skPaint);
485 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000486 // modulate the paint alpha by the shader's solid color alpha
487 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
488 copy.setColor(SkColorSetA(color, newA));
reed@google.com2be9e8b2011-07-06 21:18:09 +0000489 return this->skPaint2GrPaintNoShader(copy,
490 false,
491 grPaint,
492 constantColor);
493 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000494 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000495 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000496 GrSamplerState* sampler = grPaint->getTextureSampler(kShaderTextureIdx);
497 sampler->setSampleMode(sampleMode);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000498 if (skPaint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000499 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000500 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000501 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000502 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000503 sampler->setWrapX(sk_tile_mode_to_grwrap(tileModes[0]));
504 sampler->setWrapY(sk_tile_mode_to_grwrap(tileModes[1]));
reed@google.comac10a2d2010-12-22 21:39:39 +0000505 if (GrSamplerState::kRadial2_SampleMode == sampleMode) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000506 sampler->setRadial2Params(twoPointParams[0],
507 twoPointParams[1],
508 twoPointParams[2] < 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000509 }
510
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000511 GrTexture* texture = act->set(this, bitmap, *sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000512 if (NULL == texture) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000513 SkDebugf("Couldn't convert bitmap to texture.\n");
514 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000515 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000516 grPaint->setTexture(kShaderTextureIdx, texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000517
518 // since our texture coords will be in local space, we wack the texture
519 // matrix to map them back into 0...1 before we load it
520 SkMatrix localM;
521 if (shader->getLocalMatrix(&localM)) {
522 SkMatrix inverse;
523 if (localM.invert(&inverse)) {
524 matrix.preConcat(inverse);
525 }
526 }
527 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000528 GrScalar sx = GrFixedToScalar(GR_Fixed1 / bitmap.width());
529 GrScalar sy = GrFixedToScalar(GR_Fixed1 / bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +0000530 matrix.postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000531 } else if (SkShader::kRadial_BitmapType == bmptype) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000532 GrScalar s = GrFixedToScalar(GR_Fixed1 / bitmap.width());
reed@google.comac10a2d2010-12-22 21:39:39 +0000533 matrix.postScale(s, s);
534 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000535 sampler->setMatrix(matrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000536
537 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000538}
539
540///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com5782d712011-01-21 21:03:59 +0000541
542class SkPositionSource {
543public:
544 SkPositionSource(const SkPoint* points, int count)
545 : fPoints(points), fCount(count) {}
546
547 int count() const { return fCount; }
548
549 void writeValue(int i, GrPoint* dstPosition) const {
550 SkASSERT(i < fCount);
551 dstPosition->fX = SkScalarToGrScalar(fPoints[i].fX);
552 dstPosition->fY = SkScalarToGrScalar(fPoints[i].fY);
553 }
554private:
bsalomon@google.com5782d712011-01-21 21:03:59 +0000555 const SkPoint* fPoints;
bsalomon@google.com19628322011-02-03 21:30:17 +0000556 int fCount;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000557};
558
559class SkTexCoordSource {
560public:
561 SkTexCoordSource(const SkPoint* coords)
562 : fCoords(coords) {}
563
564 void writeValue(int i, GrPoint* dstCoord) const {
565 dstCoord->fX = SkScalarToGrScalar(fCoords[i].fX);
566 dstCoord->fY = SkScalarToGrScalar(fCoords[i].fY);
567 }
568private:
569 const SkPoint* fCoords;
570};
571
572class SkColorSource {
573public:
574 SkColorSource(const SkColor* colors) : fColors(colors) {}
575
576 void writeValue(int i, GrColor* dstColor) const {
577 *dstColor = SkGr::SkColor2GrColor(fColors[i]);
578 }
579private:
580 const SkColor* fColors;
581};
582
583class SkIndexSource {
584public:
585 SkIndexSource(const uint16_t* indices, int count)
586 : fIndices(indices), fCount(count) {
587 }
588
589 int count() const { return fCount; }
590
591 void writeValue(int i, uint16_t* dstIndex) const {
592 *dstIndex = fIndices[i];
593 }
594
595private:
bsalomon@google.com5782d712011-01-21 21:03:59 +0000596 const uint16_t* fIndices;
bsalomon@google.com19628322011-02-03 21:30:17 +0000597 int fCount;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000598};
599
600///////////////////////////////////////////////////////////////////////////////
601
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000602#if 0 // not currently being used so don't compile,
603
bsalomon@google.com5782d712011-01-21 21:03:59 +0000604// can be used for positions or texture coordinates
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000605
bsalomon@google.com5782d712011-01-21 21:03:59 +0000606class SkRectFanSource {
607public:
608 SkRectFanSource(const SkRect& rect) : fRect(rect) {}
609
610 int count() const { return 4; }
611
612 void writeValue(int i, GrPoint* dstPoint) const {
613 SkASSERT(i < 4);
614 dstPoint->fX = SkScalarToGrScalar((i % 3) ? fRect.fRight :
615 fRect.fLeft);
616 dstPoint->fY = SkScalarToGrScalar((i < 2) ? fRect.fTop :
617 fRect.fBottom);
618 }
619private:
620 const SkRect& fRect;
621};
622
623class SkIRectFanSource {
624public:
625 SkIRectFanSource(const SkIRect& rect) : fRect(rect) {}
626
627 int count() const { return 4; }
628
629 void writeValue(int i, GrPoint* dstPoint) const {
630 SkASSERT(i < 4);
631 dstPoint->fX = (i % 3) ? GrIntToScalar(fRect.fRight) :
632 GrIntToScalar(fRect.fLeft);
633 dstPoint->fY = (i < 2) ? GrIntToScalar(fRect.fTop) :
634 GrIntToScalar(fRect.fBottom);
635 }
636private:
637 const SkIRect& fRect;
638};
639
640class SkMatRectFanSource {
641public:
642 SkMatRectFanSource(const SkRect& rect, const SkMatrix& matrix)
643 : fRect(rect), fMatrix(matrix) {}
644
645 int count() const { return 4; }
646
647 void writeValue(int i, GrPoint* dstPoint) const {
648 SkASSERT(i < 4);
649
650#if SK_SCALAR_IS_GR_SCALAR
651 fMatrix.mapXY((i % 3) ? fRect.fRight : fRect.fLeft,
652 (i < 2) ? fRect.fTop : fRect.fBottom,
653 (SkPoint*)dstPoint);
654#else
655 SkPoint dst;
656 fMatrix.mapXY((i % 3) ? fRect.fRight : fRect.fLeft,
657 (i < 2) ? fRect.fTop : fRect.fBottom,
658 &dst);
659 dstPoint->fX = SkScalarToGrScalar(dst.fX);
660 dstPoint->fY = SkScalarToGrScalar(dst.fY);
661#endif
662 }
663private:
664 const SkRect& fRect;
665 const SkMatrix& fMatrix;
666};
667
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000668#endif
669
reed@google.comac10a2d2010-12-22 21:39:39 +0000670///////////////////////////////////////////////////////////////////////////////
671
bsalomon@google.com398109c2011-04-14 18:40:27 +0000672void SkGpuDevice::clear(SkColor color) {
bsalomon@google.com31a58402011-04-27 21:00:02 +0000673 fContext->clear(NULL, color);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000674}
675
reed@google.comac10a2d2010-12-22 21:39:39 +0000676void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
677 CHECK_SHOULD_DRAW(draw);
678
bsalomon@google.com5782d712011-01-21 21:03:59 +0000679 GrPaint grPaint;
680 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000681 if (!this->skPaint2GrPaintShader(paint,
682 &act,
683 *draw.fMatrix,
684 &grPaint,
685 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000686 return;
687 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000688
689 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000690}
691
692// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000693static const GrPrimitiveType gPointMode2PrimtiveType[] = {
694 kPoints_PrimitiveType,
695 kLines_PrimitiveType,
696 kLineStrip_PrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000697};
698
699void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000700 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000701 CHECK_SHOULD_DRAW(draw);
702
703 SkScalar width = paint.getStrokeWidth();
704 if (width < 0) {
705 return;
706 }
707
708 // we only handle hairlines here, else we let the SkDraw call our drawPath()
709 if (width > 0) {
710 draw.drawPoints(mode, count, pts, paint, true);
711 return;
712 }
713
bsalomon@google.com5782d712011-01-21 21:03:59 +0000714 GrPaint grPaint;
715 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000716 if (!this->skPaint2GrPaintShader(paint,
717 &act,
718 *draw.fMatrix,
719 &grPaint,
720 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000721 return;
722 }
723
reed@google.comac10a2d2010-12-22 21:39:39 +0000724#if SK_SCALAR_IS_GR_SCALAR
bsalomon@google.com5782d712011-01-21 21:03:59 +0000725 fContext->drawVertices(grPaint,
726 gPointMode2PrimtiveType[mode],
727 count,
728 (GrPoint*)pts,
729 NULL,
730 NULL,
731 NULL,
732 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000733#else
bsalomon@google.com5782d712011-01-21 21:03:59 +0000734 fContext->drawCustomVertices(grPaint,
735 gPointMode2PrimtiveType[mode],
736 SkPositionSource(pts, count));
reed@google.comac10a2d2010-12-22 21:39:39 +0000737#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000738}
739
reed@google.comc9aa5872011-04-05 21:05:37 +0000740///////////////////////////////////////////////////////////////////////////////
741
reed@google.comac10a2d2010-12-22 21:39:39 +0000742void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
743 const SkPaint& paint) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000744 CHECK_SHOULD_DRAW(draw);
745
bungeman@google.com79bd8772011-07-18 15:34:08 +0000746 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000747 SkScalar width = paint.getStrokeWidth();
748
749 /*
750 We have special code for hairline strokes, miter-strokes, and fills.
751 Anything else we just call our path code.
752 */
753 bool usePath = doStroke && width > 0 &&
754 paint.getStrokeJoin() != SkPaint::kMiter_Join;
755 // another reason we might need to call drawPath...
756 if (paint.getMaskFilter()) {
757 usePath = true;
758 }
reed@google.com67db6642011-05-26 11:46:35 +0000759 // until we aa rotated rects...
760 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
761 usePath = true;
762 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000763 // until we can both stroke and fill rectangles
764 // with large enough miter limit...
765 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
766 usePath = true;
767 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000768
769 if (usePath) {
770 SkPath path;
771 path.addRect(rect);
772 this->drawPath(draw, path, paint, NULL, true);
773 return;
774 }
775
776 GrPaint grPaint;
777 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000778 if (!this->skPaint2GrPaintShader(paint,
779 &act,
780 *draw.fMatrix,
781 &grPaint,
782 true)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000783 return;
784 }
reed@google.com20efde72011-05-09 17:00:02 +0000785 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000786}
787
reed@google.com69302852011-02-16 18:08:07 +0000788#include "SkMaskFilter.h"
789#include "SkBounder.h"
790
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000791static GrPathFill skToGrFillType(SkPath::FillType fillType) {
792 switch (fillType) {
793 case SkPath::kWinding_FillType:
794 return kWinding_PathFill;
795 case SkPath::kEvenOdd_FillType:
796 return kEvenOdd_PathFill;
797 case SkPath::kInverseWinding_FillType:
798 return kInverseWinding_PathFill;
799 case SkPath::kInverseEvenOdd_FillType:
800 return kInverseEvenOdd_PathFill;
801 default:
802 SkDebugf("Unsupported path fill type\n");
803 return kHairLine_PathFill;
804 }
805}
806
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000807static void buildKernel(float sigma, float* kernel, int kernelWidth) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000808 int halfWidth = (kernelWidth - 1) / 2;
809 float sum = 0.0f;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000810 float denom = 1.0f / (2.0f * sigma * sigma);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000811 for (int i = 0; i < kernelWidth; ++i) {
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000812 float x = static_cast<float>(i - halfWidth);
813 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
814 // is dropped here, since we renormalize the kernel below.
815 kernel[i] = sk_float_exp(- x * x * denom);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000816 sum += kernel[i];
817 }
818 // Normalize the kernel
819 float scale = 1.0f / sum;
820 for (int i = 0; i < kernelWidth; ++i)
821 kernel[i] *= scale;
822}
823
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000824static void scaleRect(SkRect* rect, float scale) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000825 rect->fLeft *= scale;
826 rect->fTop *= scale;
827 rect->fRight *= scale;
828 rect->fBottom *= scale;
829}
830
831static bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
832 SkMaskFilter* filter, const SkMatrix& matrix,
833 const SkRegion& clip, SkBounder* bounder,
834 GrPaint* grp) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000835#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000836 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000837#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000838 SkMaskFilter::BlurInfo info;
839 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000840 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000841 return false;
842 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000843 SkScalar radius = info.fIgnoreTransform ? info.fRadius
844 : matrix.mapRadius(info.fRadius);
845 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
846 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000847 SkRect srcRect = path.getBounds();
848
849 int scaleFactor = 1;
850
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000851 while (sigma > MAX_BLUR_SIGMA) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000852 scaleFactor *= 2;
853 sigma *= 0.5f;
854 }
855 scaleRect(&srcRect, 1.0f / scaleFactor);
senorblanco@chromium.org4a947d22011-07-18 21:48:35 +0000856 int halfWidth = static_cast<int>(ceilf(sigma * 3.0f));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000857 int kernelWidth = halfWidth * 2 + 1;
858
859 SkIRect srcIRect;
860 srcRect.roundOut(&srcIRect);
861 srcRect.set(srcIRect);
862 srcRect.inset(-halfWidth, -halfWidth);
863
864 scaleRect(&srcRect, scaleFactor);
865 SkRect finalRect = srcRect;
866
867 SkIRect finalIRect;
868 finalRect.roundOut(&finalIRect);
869 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000870 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000871 }
872 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000873 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000874 }
875 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
876 srcRect.offset(-srcRect.fLeft, -srcRect.fTop);
877 const GrTextureDesc desc = {
878 kRenderTarget_GrTextureFlagBit,
879 kNone_GrAALevel,
880 srcRect.width(),
881 srcRect.height(),
882 // We actually only need A8, but it often isn't supported as a
883 // render target
884 kRGBA_8888_GrPixelConfig
885 };
886
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000887 GrAutoScratchTexture srcEntry(context, desc);
888 GrAutoScratchTexture dstEntry(context, desc);
889 if (NULL == srcEntry.texture() || NULL == dstEntry.texture()) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000890 return false;
891 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000892 GrTexture* srcTexture = srcEntry.texture();
893 GrTexture* dstTexture = dstEntry.texture();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000894 if (NULL == srcTexture || NULL == dstTexture) {
895 return false;
896 }
897 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000898 // Once this code moves into GrContext, this should be changed to use
899 // an AutoClipRestore.
900 GrClip oldClip = context->getClip();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000901 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000902 context->setClip(srcRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000903 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000904 GrPaint tempPaint;
905 tempPaint.reset();
906
907 GrAutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000908 tempPaint.fAntiAlias = grp->fAntiAlias;
909 if (tempPaint.fAntiAlias) {
910 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
911 // blend coeff of zero requires dual source blending support in order
912 // to properly blend partially covered pixels. This means the AA
913 // code path may not be taken. So we use a dst blend coeff of ISA. We
914 // could special case AA draws to a dst surface with known alpha=0 to
915 // use a zero dst coeff when dual source blending isn't available.
916 tempPaint.fSrcBlendCoeff = kOne_BlendCoeff;
917 tempPaint.fDstBlendCoeff = kISC_BlendCoeff;
918 }
919 // Draw hard shadow to dstTexture with path topleft at origin 0,0.
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000920 context->drawPath(tempPaint, path, skToGrFillType(path.getFillType()), &offset);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000921 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000922
923 GrMatrix sampleM;
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +0000924 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000925 GrPaint paint;
926 paint.reset();
927 paint.getTextureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
928 paint.getTextureSampler(0)->setMatrix(sampleM);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000929 GrAutoScratchTexture origEntry;
930
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000931 if (blurType != SkMaskFilter::kNormal_BlurType) {
932 // Stash away a copy of the unblurred image.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000933 origEntry.set(context, desc);
934 if (NULL == origEntry.texture()) {
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000935 return false;
936 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000937 context->setRenderTarget(origEntry.texture()->asRenderTarget());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000938 paint.setTexture(0, srcTexture);
939 context->drawRect(paint, srcRect);
940 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000941 for (int i = 1; i < scaleFactor; i *= 2) {
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +0000942 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
943 paint.getTextureSampler(0)->setMatrix(sampleM);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000944 context->setRenderTarget(dstTexture->asRenderTarget());
945 SkRect dstRect(srcRect);
946 scaleRect(&dstRect, 0.5f);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000947 paint.setTexture(0, srcTexture);
948 context->drawRectToRect(paint, dstRect, srcRect);
949 srcRect = dstRect;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000950 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000951 }
952
953 SkAutoTMalloc<float> kernelStorage(kernelWidth);
954 float* kernel = kernelStorage.get();
955 buildKernel(sigma, kernel, kernelWidth);
956
senorblanco@chromium.org2ce9a042011-07-22 15:31:14 +0000957 // Clear out a halfWidth to the right of the srcRect to prevent the
958 // X convolution from reading garbage.
959 SkIRect clearRect = SkIRect::MakeXYWH(
960 srcRect.fRight, srcRect.fTop, halfWidth, srcRect.height());
961 context->clear(&clearRect, 0x0);
962
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000963 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000964 context->convolveInX(srcTexture, srcRect, kernel, kernelWidth);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000965 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000966
senorblanco@chromium.org2ce9a042011-07-22 15:31:14 +0000967 // Clear out a halfWidth below the srcRect to prevent the Y
968 // convolution from reading garbage.
969 clearRect = SkIRect::MakeXYWH(
970 srcRect.fLeft, srcRect.fBottom, srcRect.width(), halfWidth);
971 context->clear(&clearRect, 0x0);
972
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000973 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000974 context->convolveInY(srcTexture, srcRect, kernel, kernelWidth);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000975 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000976
senorblanco@chromium.org2ce9a042011-07-22 15:31:14 +0000977 // Clear one pixel to the right and below, to accommodate bilinear
978 // upsampling.
979 clearRect = SkIRect::MakeXYWH(
980 srcRect.fLeft, srcRect.fBottom, srcRect.width() + 1, 1);
981 context->clear(&clearRect, 0x0);
982 clearRect = SkIRect::MakeXYWH(
983 srcRect.fRight, srcRect.fTop, 1, srcRect.height());
984 context->clear(&clearRect, 0x0);
985
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000986 if (scaleFactor > 1) {
987 // FIXME: This should be mitchell, not bilinear.
988 paint.getTextureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +0000989 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000990 paint.getTextureSampler(0)->setMatrix(sampleM);
991 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000992 paint.setTexture(0, srcTexture);
993 SkRect dstRect(srcRect);
994 scaleRect(&dstRect, scaleFactor);
995 context->drawRectToRect(paint, dstRect, srcRect);
996 srcRect = dstRect;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000997 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000998 }
999
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001000 if (blurType != SkMaskFilter::kNormal_BlurType) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001001 GrTexture* origTexture = origEntry.texture();
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001002 paint.getTextureSampler(0)->setFilter(GrSamplerState::kNearest_Filter);
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +00001003 sampleM.setIDiv(origTexture->width(), origTexture->height());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001004 paint.getTextureSampler(0)->setMatrix(sampleM);
1005 // Blend origTexture over srcTexture.
1006 context->setRenderTarget(srcTexture->asRenderTarget());
1007 paint.setTexture(0, origTexture);
1008 if (SkMaskFilter::kInner_BlurType == blurType) {
1009 // inner: dst = dst * src
1010 paint.fSrcBlendCoeff = kDC_BlendCoeff;
1011 paint.fDstBlendCoeff = kZero_BlendCoeff;
1012 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
1013 // solid: dst = src + dst - src * dst
1014 // = (1 - dst) * src + 1 * dst
1015 paint.fSrcBlendCoeff = kIDC_BlendCoeff;
1016 paint.fDstBlendCoeff = kOne_BlendCoeff;
1017 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
1018 // outer: dst = dst * (1 - src)
1019 // = 0 * src + (1 - src) * dst
1020 paint.fSrcBlendCoeff = kZero_BlendCoeff;
1021 paint.fDstBlendCoeff = kISC_BlendCoeff;
1022 }
1023 context->drawRect(paint, srcRect);
1024 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001025 context->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +00001026 context->setClip(oldClip);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001027
1028 if (grp->hasTextureOrMask()) {
1029 GrMatrix inverse;
1030 if (!matrix.invert(&inverse)) {
1031 return false;
1032 }
1033 grp->preConcatActiveSamplerMatrices(inverse);
1034 }
1035
1036 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
1037 // we assume the last mask index is available for use
1038 GrAssert(NULL == grp->getMask(MASK_IDX));
1039 grp->setMask(MASK_IDX, srcTexture);
1040 grp->getMaskSampler(MASK_IDX)->setClampNoFilter();
1041
1042 GrMatrix m;
1043 m.setTranslate(-finalRect.fLeft, -finalRect.fTop);
1044 m.postIDiv(srcTexture->width(), srcTexture->height());
1045 grp->getMaskSampler(MASK_IDX)->setMatrix(m);
1046 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001047 return true;
1048}
1049
reed@google.com69302852011-02-16 18:08:07 +00001050static bool drawWithMaskFilter(GrContext* context, const SkPath& path,
1051 SkMaskFilter* filter, const SkMatrix& matrix,
1052 const SkRegion& clip, SkBounder* bounder,
1053 GrPaint* grp) {
1054 SkMask srcM, dstM;
1055
1056 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
1057 SkMask::kComputeBoundsAndRenderImage_CreateMode)) {
1058 return false;
1059 }
1060
1061 SkAutoMaskImage autoSrc(&srcM, false);
1062
1063 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
1064 return false;
1065 }
1066 // this will free-up dstM when we're done (allocated in filterMask())
1067 SkAutoMaskImage autoDst(&dstM, false);
1068
1069 if (clip.quickReject(dstM.fBounds)) {
1070 return false;
1071 }
1072 if (bounder && !bounder->doIRect(dstM.fBounds)) {
1073 return false;
1074 }
1075
1076 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
1077 // the current clip (and identity matrix) and grpaint settings
1078
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001079 // used to compute inverse view, if necessary
1080 GrMatrix ivm = context->getMatrix();
1081
reed@google.com0c219b62011-02-16 21:31:18 +00001082 GrAutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +00001083
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001084 const GrTextureDesc desc = {
1085 kNone_GrTextureFlags,
1086 kNone_GrAALevel,
reed@google.com69302852011-02-16 18:08:07 +00001087 dstM.fBounds.width(),
1088 dstM.fBounds.height(),
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001089 kAlpha_8_GrPixelConfig
reed@google.com69302852011-02-16 18:08:07 +00001090 };
1091
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001092 GrAutoScratchTexture ast(context, desc);
1093 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001094
reed@google.com69302852011-02-16 18:08:07 +00001095 if (NULL == texture) {
1096 return false;
1097 }
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001098 texture->uploadTextureData(0, 0, desc.fWidth, desc.fHeight,
1099 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +00001100
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001101 if (grp->hasTextureOrMask() && ivm.invert(&ivm)) {
1102 grp->preConcatActiveSamplerMatrices(ivm);
1103 }
1104
1105 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
1106 // we assume the last mask index is available for use
1107 GrAssert(NULL == grp->getMask(MASK_IDX));
1108 grp->setMask(MASK_IDX, texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001109 grp->getMaskSampler(MASK_IDX)->setClampNoFilter();
reed@google.com69302852011-02-16 18:08:07 +00001110
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001111 GrRect d;
1112 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +00001113 GrIntToScalar(dstM.fBounds.fTop),
1114 GrIntToScalar(dstM.fBounds.fRight),
1115 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001116
1117 GrMatrix m;
1118 m.setTranslate(-dstM.fBounds.fLeft, -dstM.fBounds.fTop);
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001119 m.postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001120 grp->getMaskSampler(MASK_IDX)->setMatrix(m);
1121
1122 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +00001123 return true;
1124}
reed@google.com69302852011-02-16 18:08:07 +00001125
reed@google.com0c219b62011-02-16 21:31:18 +00001126void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
reed@google.comac10a2d2010-12-22 21:39:39 +00001127 const SkPaint& paint, const SkMatrix* prePathMatrix,
1128 bool pathIsMutable) {
1129 CHECK_SHOULD_DRAW(draw);
1130
bsalomon@google.com5782d712011-01-21 21:03:59 +00001131 GrPaint grPaint;
1132 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +00001133 if (!this->skPaint2GrPaintShader(paint,
1134 &act,
1135 *draw.fMatrix,
1136 &grPaint,
1137 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001138 return;
1139 }
1140
reed@google.com0c219b62011-02-16 21:31:18 +00001141 // BEGIN lift from SkDraw::drawPath()
1142
1143 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1144 bool doFill = true;
1145 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001146
1147 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001148 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001149
reed@google.come3445642011-02-16 23:20:39 +00001150 if (!pathIsMutable) {
1151 result = &tmpPath;
1152 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001153 }
reed@google.come3445642011-02-16 23:20:39 +00001154 // should I push prePathMatrix on our MV stack temporarily, instead
1155 // of applying it here? See SkDraw.cpp
1156 pathPtr->transform(*prePathMatrix, result);
1157 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001158 }
reed@google.com0c219b62011-02-16 21:31:18 +00001159 // at this point we're done with prePathMatrix
1160 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001161
bsalomon@google.com04de7822011-03-25 18:04:43 +00001162 // This "if" is not part of the SkDraw::drawPath() lift.
1163 // When we get a 1.0 wide stroke we hairline stroke it instead of creating
1164 // a new stroked-path. This is motivated by canvas2D sites that draw
1165 // lines as 1.0 wide stroked paths. We can consider doing an alpha-modulated-
1166 // hairline for width < 1.0 when AA is enabled.
1167 static const int gMatrixMask = ~(SkMatrix::kIdentity_Mask |
1168 SkMatrix::kTranslate_Mask);
1169 if (!paint.getPathEffect() &&
1170 SkPaint::kStroke_Style == paint.getStyle() &&
1171 !(draw.fMatrix->getType() & gMatrixMask) &&
1172 SK_Scalar1 == paint.getStrokeWidth()) {
1173 doFill = false;
1174 }
1175
1176 if (doFill && (paint.getPathEffect() ||
1177 paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.com0c219b62011-02-16 21:31:18 +00001178 doFill = paint.getFillPath(*pathPtr, &tmpPath);
1179 pathPtr = &tmpPath;
1180 }
1181
1182 // END lift from SkDraw::drawPath()
1183
reed@google.com69302852011-02-16 18:08:07 +00001184 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001185 // avoid possibly allocating a new path in transform if we can
1186 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1187
1188 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001189 pathPtr->transform(*draw.fMatrix, devPathPtr);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001190 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
1191 *draw.fMatrix, *draw.fClip, draw.fBounder,
1192 &grPaint)) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001193 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
1194 *draw.fMatrix, *draw.fClip, draw.fBounder,
1195 &grPaint);
1196 }
reed@google.com69302852011-02-16 18:08:07 +00001197 return;
1198 }
reed@google.com69302852011-02-16 18:08:07 +00001199
bsalomon@google.comffca4002011-02-22 20:34:01 +00001200 GrPathFill fill = kHairLine_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001201
reed@google.com0c219b62011-02-16 21:31:18 +00001202 if (doFill) {
1203 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001204 case SkPath::kWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001205 fill = kWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001206 break;
1207 case SkPath::kEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001208 fill = kEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001209 break;
1210 case SkPath::kInverseWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001211 fill = kInverseWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001212 break;
1213 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001214 fill = kInverseEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001215 break;
1216 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001217 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001218 return;
1219 }
1220 }
1221
reed@google.com07f3ee12011-05-16 17:21:57 +00001222 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001223}
1224
reed@google.comac10a2d2010-12-22 21:39:39 +00001225void SkGpuDevice::drawBitmap(const SkDraw& draw,
1226 const SkBitmap& bitmap,
1227 const SkIRect* srcRectPtr,
1228 const SkMatrix& m,
1229 const SkPaint& paint) {
1230 CHECK_SHOULD_DRAW(draw);
1231
1232 SkIRect srcRect;
1233 if (NULL == srcRectPtr) {
1234 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1235 } else {
1236 srcRect = *srcRectPtr;
1237 }
1238
junov@google.comd935cfb2011-06-27 20:48:23 +00001239 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001240 // Convert the bitmap to a shader so that the rect can be drawn
1241 // through drawRect, which supports mask filters.
1242 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001243 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001244 if (srcRectPtr) {
1245 if (!bitmap.extractSubset(&tmp, srcRect)) {
1246 return; // extraction failed
1247 }
1248 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001249 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001250 }
1251 SkPaint paintWithTexture(paint);
1252 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1253 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001254 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001255 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001256
junov@google.com1d329782011-07-28 20:10:09 +00001257 // Transform 'm' needs to be concatenated to the draw matrix,
1258 // rather than transforming the primitive directly, so that 'm' will
1259 // also affect the behavior of the mask filter.
1260 SkMatrix drawMatrix;
1261 drawMatrix.setConcat(*draw.fMatrix, m);
1262 SkDraw transformedDraw(draw);
1263 transformedDraw.fMatrix = &drawMatrix;
1264
1265 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1266
junov@google.comd935cfb2011-06-27 20:48:23 +00001267 return;
1268 }
1269
bsalomon@google.com5782d712011-01-21 21:03:59 +00001270 GrPaint grPaint;
Scroggod757df22011-05-16 13:11:16 +00001271 if (!this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001272 return;
1273 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001274 GrSamplerState* sampler = grPaint.getTextureSampler(kBitmapTextureIdx);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001275 if (paint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001276 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001277 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001278 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001279 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001280
bsalomon@google.com91958362011-06-13 17:58:13 +00001281 const int maxTextureSize = fContext->getMaxTextureSize();
1282 if (bitmap.getTexture() || (bitmap.width() <= maxTextureSize &&
1283 bitmap.height() <= maxTextureSize)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001284 // take the fast case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001285 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001286 return;
1287 }
1288
1289 // undo the translate done by SkCanvas
1290 int DX = SkMax32(0, srcRect.fLeft);
1291 int DY = SkMax32(0, srcRect.fTop);
1292 // compute clip bounds in local coordinates
1293 SkIRect clipRect;
1294 {
1295 SkRect r;
1296 r.set(draw.fClip->getBounds());
1297 SkMatrix matrix, inverse;
1298 matrix.setConcat(*draw.fMatrix, m);
1299 if (!matrix.invert(&inverse)) {
1300 return;
1301 }
1302 inverse.mapRect(&r);
1303 r.roundOut(&clipRect);
1304 // apply the canvas' translate to our local clip
1305 clipRect.offset(DX, DY);
1306 }
1307
bsalomon@google.com91958362011-06-13 17:58:13 +00001308 int nx = bitmap.width() / maxTextureSize;
1309 int ny = bitmap.height() / maxTextureSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001310 for (int x = 0; x <= nx; x++) {
1311 for (int y = 0; y <= ny; y++) {
1312 SkIRect tileR;
bsalomon@google.com91958362011-06-13 17:58:13 +00001313 tileR.set(x * maxTextureSize, y * maxTextureSize,
1314 (x + 1) * maxTextureSize, (y + 1) * maxTextureSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001315 if (!SkIRect::Intersects(tileR, clipRect)) {
1316 continue;
1317 }
1318
1319 SkIRect srcR = tileR;
1320 if (!srcR.intersect(srcRect)) {
1321 continue;
1322 }
1323
1324 SkBitmap tmpB;
1325 if (bitmap.extractSubset(&tmpB, tileR)) {
1326 // now offset it to make it "local" to our tmp bitmap
1327 srcR.offset(-tileR.fLeft, -tileR.fTop);
1328
1329 SkMatrix tmpM(m);
1330 {
1331 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1332 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1333 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1334 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001335 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001336 }
1337 }
1338 }
1339}
1340
1341/*
1342 * This is called by drawBitmap(), which has to handle images that may be too
1343 * large to be represented by a single texture.
1344 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001345 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1346 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001347 */
1348void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1349 const SkBitmap& bitmap,
1350 const SkIRect& srcRect,
1351 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001352 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001353 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1354 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001355
reed@google.com9c49bc32011-07-07 13:42:37 +00001356 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001357 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001358 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001359 return;
1360 }
1361
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001362 GrSamplerState* sampler = grPaint->getTextureSampler(kBitmapTextureIdx);
1363
1364 sampler->setWrapX(GrSamplerState::kClamp_WrapMode);
1365 sampler->setWrapY(GrSamplerState::kClamp_WrapMode);
1366 sampler->setSampleMode(GrSamplerState::kNormal_SampleMode);
1367 sampler->setMatrix(GrMatrix::I());
reed@google.comac10a2d2010-12-22 21:39:39 +00001368
1369 GrTexture* texture;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001370 SkAutoCachedTexture act(this, bitmap, *sampler, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001371 if (NULL == texture) {
1372 return;
1373 }
1374
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001375 grPaint->setTexture(kShaderTextureIdx, texture);
reed@google.com46799cd2011-02-22 20:56:26 +00001376
reed@google.com20efde72011-05-09 17:00:02 +00001377 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1378 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001379 GrRect paintRect;
junov@google.com6acc9b32011-05-16 18:32:07 +00001380 paintRect.setLTRB(GrFixedToScalar((srcRect.fLeft << 16) / bitmap.width()),
1381 GrFixedToScalar((srcRect.fTop << 16) / bitmap.height()),
1382 GrFixedToScalar((srcRect.fRight << 16) / bitmap.width()),
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001383 GrFixedToScalar((srcRect.fBottom << 16) / bitmap.height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001384
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001385 if (GrSamplerState::kNearest_Filter != sampler->getFilter() &&
junov@google.com6acc9b32011-05-16 18:32:07 +00001386 (srcRect.width() < bitmap.width() ||
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001387 srcRect.height() < bitmap.height())) {
junov@google.com6acc9b32011-05-16 18:32:07 +00001388 // If drawing a subrect of the bitmap and filtering is enabled,
1389 // use a constrained texture domain to avoid color bleeding
1390 GrScalar left, top, right, bottom;
1391 if (srcRect.width() > 1) {
1392 GrScalar border = GR_ScalarHalf / bitmap.width();
1393 left = paintRect.left() + border;
1394 right = paintRect.right() - border;
1395 } else {
1396 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1397 }
1398 if (srcRect.height() > 1) {
1399 GrScalar border = GR_ScalarHalf / bitmap.height();
1400 top = paintRect.top() + border;
1401 bottom = paintRect.bottom() - border;
1402 } else {
1403 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1404 }
1405 GrRect textureDomain;
1406 textureDomain.setLTRB(left, top, right, bottom);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001407 sampler->setTextureDomain(textureDomain);
junov@google.com6acc9b32011-05-16 18:32:07 +00001408 }
1409
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001410 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001411}
1412
1413void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1414 int left, int top, const SkPaint& paint) {
1415 CHECK_SHOULD_DRAW(draw);
1416
1417 SkAutoLockPixels alp(bitmap);
1418 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1419 return;
1420 }
1421
bsalomon@google.com5782d712011-01-21 21:03:59 +00001422 GrPaint grPaint;
Scroggod757df22011-05-16 13:11:16 +00001423 if(!this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001424 return;
1425 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001426
bsalomon@google.com5782d712011-01-21 21:03:59 +00001427 GrAutoMatrix avm(fContext, GrMatrix::I());
1428
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001429 GrSamplerState* sampler = grPaint.getTextureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001430
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001431 GrTexture* texture;
1432 sampler->setClampNoFilter();
1433 SkAutoCachedTexture act(this, bitmap, *sampler, &texture);
1434
1435 grPaint.setTexture(kBitmapTextureIdx, texture);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001436
bsalomon@google.com5782d712011-01-21 21:03:59 +00001437 fContext->drawRectToRect(grPaint,
reed@google.com20efde72011-05-09 17:00:02 +00001438 GrRect::MakeXYWH(GrIntToScalar(left),
1439 GrIntToScalar(top),
1440 GrIntToScalar(bitmap.width()),
1441 GrIntToScalar(bitmap.height())),
1442 GrRect::MakeWH(GR_Scalar1, GR_Scalar1));
reed@google.comac10a2d2010-12-22 21:39:39 +00001443}
1444
1445void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* dev,
1446 int x, int y, const SkPaint& paint) {
1447 CHECK_SHOULD_DRAW(draw);
1448
bsalomon@google.com5782d712011-01-21 21:03:59 +00001449 GrPaint grPaint;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001450 if (!((SkGpuDevice*)dev)->bindDeviceAsTexture(&grPaint) ||
Scroggod757df22011-05-16 13:11:16 +00001451 !this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001452 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001453 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001454
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001455 GrTexture* devTex = grPaint.getTexture(0);
1456 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001457
1458 const SkBitmap& bm = dev->accessBitmap(false);
1459 int w = bm.width();
1460 int h = bm.height();
1461
1462 GrAutoMatrix avm(fContext, GrMatrix::I());
1463
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001464 grPaint.getTextureSampler(kBitmapTextureIdx)->setClampNoFilter();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001465
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001466 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1467 GrIntToScalar(y),
1468 GrIntToScalar(w),
1469 GrIntToScalar(h));
1470 // The device being drawn may not fill up its texture (saveLayer uses
1471 // the approximate ).
1472 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1473 GR_Scalar1 * h / devTex->height());
1474
1475 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001476}
1477
1478///////////////////////////////////////////////////////////////////////////////
1479
1480// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001481static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1482 kTriangles_PrimitiveType,
1483 kTriangleStrip_PrimitiveType,
1484 kTriangleFan_PrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001485};
1486
1487void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1488 int vertexCount, const SkPoint vertices[],
1489 const SkPoint texs[], const SkColor colors[],
1490 SkXfermode* xmode,
1491 const uint16_t indices[], int indexCount,
1492 const SkPaint& paint) {
1493 CHECK_SHOULD_DRAW(draw);
1494
bsalomon@google.com5782d712011-01-21 21:03:59 +00001495 GrPaint grPaint;
1496 SkAutoCachedTexture act;
1497 // we ignore the shader if texs is null.
1498 if (NULL == texs) {
Scroggod757df22011-05-16 13:11:16 +00001499 if (!this->skPaint2GrPaintNoShader(paint,
1500 false,
1501 &grPaint,
1502 NULL == colors)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001503 return;
1504 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001505 } else {
reed@google.com1a2e8d22011-01-21 22:08:29 +00001506 if (!this->skPaint2GrPaintShader(paint, &act,
1507 *draw.fMatrix,
Scroggod757df22011-05-16 13:11:16 +00001508 &grPaint,
1509 NULL == colors)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001510 return;
1511 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001512 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001513
1514 if (NULL != xmode && NULL != texs && NULL != colors) {
1515 SkXfermode::Mode mode;
1516 if (!SkXfermode::IsMode(xmode, &mode) ||
1517 SkXfermode::kMultiply_Mode != mode) {
1518 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1519#if 0
1520 return
1521#endif
1522 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001523 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001524
1525#if SK_SCALAR_IS_GR_SCALAR
1526 // even if GrColor and SkColor byte offsets match we need
1527 // to perform pre-multiply.
1528 if (NULL == colors) {
1529 fContext->drawVertices(grPaint,
1530 gVertexMode2PrimitiveType[vmode],
1531 vertexCount,
1532 (GrPoint*) vertices,
1533 (GrPoint*) texs,
1534 NULL,
1535 indices,
1536 indexCount);
1537 } else
1538#endif
1539 {
1540 SkTexCoordSource texSrc(texs);
1541 SkColorSource colSrc(colors);
1542 SkIndexSource idxSrc(indices, indexCount);
1543
1544 fContext->drawCustomVertices(grPaint,
1545 gVertexMode2PrimitiveType[vmode],
1546 SkPositionSource(vertices, vertexCount),
1547 (NULL == texs) ? NULL : &texSrc,
1548 (NULL == colors) ? NULL : &colSrc,
1549 (NULL == indices) ? NULL : &idxSrc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001550 }
1551}
1552
1553///////////////////////////////////////////////////////////////////////////////
1554
1555static void GlyphCacheAuxProc(void* data) {
1556 delete (GrFontScaler*)data;
1557}
1558
1559static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1560 void* auxData;
1561 GrFontScaler* scaler = NULL;
1562 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1563 scaler = (GrFontScaler*)auxData;
1564 }
1565 if (NULL == scaler) {
1566 scaler = new SkGrFontScaler(cache);
1567 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1568 }
1569 return scaler;
1570}
1571
1572static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1573 SkFixed fx, SkFixed fy,
1574 const SkGlyph& glyph) {
1575 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1576
1577 GrSkDrawProcs* procs = (GrSkDrawProcs*)state.fDraw->fProcs;
1578
1579 if (NULL == procs->fFontScaler) {
1580 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1581 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001582
1583 /*
reed@google.com3b139f52011-06-07 17:56:25 +00001584 * What should we do with fy? (assuming horizontal/latin text)
reed@google.com39ce0ac2011-04-08 15:42:19 +00001585 *
reed@google.com3b139f52011-06-07 17:56:25 +00001586 * The raster code calls SkFixedFloorToFixed on it, as it does with fx.
1587 * It calls that rather than round, because our caller has already added
1588 * SK_FixedHalf, so that calling floor gives us the rounded integer.
1589 *
1590 * Test code between raster and gpu (they should draw the same)
1591 *
1592 * canvas->drawText("Hamburgefons", 12, 0, 16.5f, paint);
1593 *
1594 * Perhaps we should only perform this integralization if there is no
1595 * fExtMatrix...
reed@google.com39ce0ac2011-04-08 15:42:19 +00001596 */
reed@google.com3b139f52011-06-07 17:56:25 +00001597 fy = SkFixedFloorToFixed(fy);
1598
reed@google.comac10a2d2010-12-22 21:39:39 +00001599 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(), fx, 0),
reed@google.com3b139f52011-06-07 17:56:25 +00001600 SkFixedFloorToFixed(fx), fy,
reed@google.comac10a2d2010-12-22 21:39:39 +00001601 procs->fFontScaler);
1602}
1603
bsalomon@google.com5782d712011-01-21 21:03:59 +00001604SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001605
1606 // deferred allocation
1607 if (NULL == fDrawProcs) {
1608 fDrawProcs = new GrSkDrawProcs;
1609 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1610 fDrawProcs->fContext = fContext;
1611 }
1612
1613 // init our (and GL's) state
1614 fDrawProcs->fTextContext = context;
1615 fDrawProcs->fFontScaler = NULL;
1616 return fDrawProcs;
1617}
1618
1619void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1620 size_t byteLength, SkScalar x, SkScalar y,
1621 const SkPaint& paint) {
1622 CHECK_SHOULD_DRAW(draw);
1623
1624 if (draw.fMatrix->getType() & SkMatrix::kPerspective_Mask) {
1625 // this guy will just call our drawPath()
1626 draw.drawText((const char*)text, byteLength, x, y, paint);
1627 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001628 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001629
1630 GrPaint grPaint;
1631 SkAutoCachedTexture act;
1632
Scroggod757df22011-05-16 13:11:16 +00001633 if (!this->skPaint2GrPaintShader(paint,
1634 &act,
1635 *draw.fMatrix,
1636 &grPaint,
1637 true)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001638 return;
1639 }
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001640 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001641 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001642 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1643 }
1644}
1645
1646void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1647 size_t byteLength, const SkScalar pos[],
1648 SkScalar constY, int scalarsPerPos,
1649 const SkPaint& paint) {
1650 CHECK_SHOULD_DRAW(draw);
1651
1652 if (draw.fMatrix->getType() & SkMatrix::kPerspective_Mask) {
1653 // this guy will just call our drawPath()
1654 draw.drawPosText((const char*)text, byteLength, pos, constY,
1655 scalarsPerPos, paint);
1656 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001657 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001658
1659 GrPaint grPaint;
1660 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +00001661 if (!this->skPaint2GrPaintShader(paint,
1662 &act,
1663 *draw.fMatrix,
1664 &grPaint,
1665 true)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001666 return;
1667 }
1668
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001669 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001670 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001671 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1672 scalarsPerPos, paint);
1673 }
1674}
1675
1676void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1677 size_t len, const SkPath& path,
1678 const SkMatrix* m, const SkPaint& paint) {
1679 CHECK_SHOULD_DRAW(draw);
1680
1681 SkASSERT(draw.fDevice == this);
1682 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1683}
1684
1685///////////////////////////////////////////////////////////////////////////////
1686
reed@google.comf67e4cf2011-03-15 20:56:58 +00001687bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1688 if (!paint.isLCDRenderText()) {
1689 // we're cool with the paint as is
1690 return false;
1691 }
1692
1693 if (paint.getShader() ||
1694 paint.getXfermode() || // unless its srcover
1695 paint.getMaskFilter() ||
1696 paint.getRasterizer() ||
1697 paint.getColorFilter() ||
1698 paint.getPathEffect() ||
1699 paint.isFakeBoldText() ||
1700 paint.getStyle() != SkPaint::kFill_Style) {
1701 // turn off lcd
1702 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1703 flags->fHinting = paint.getHinting();
1704 return true;
1705 }
1706 // we're cool with the paint as is
1707 return false;
1708}
1709
1710///////////////////////////////////////////////////////////////////////////////
1711
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001712SkGpuDevice::TexCache SkGpuDevice::lockCachedTexture(const SkBitmap& bitmap,
1713 const GrSamplerState& sampler,
1714 TexType type) {
1715 GrContext::TextureCacheEntry entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001716 GrContext* ctx = this->context();
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001717
bsalomon@google.come97f0852011-06-17 13:10:25 +00001718 if (kBitmap_TexType != type) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001719 const GrTextureDesc desc = {
1720 kRenderTarget_GrTextureFlagBit,
1721 kNone_GrAALevel,
1722 bitmap.width(),
1723 bitmap.height(),
1724 SkGr::Bitmap2PixelConfig(bitmap)
1725 };
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001726 GrContext::ScratchTexMatch match;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001727 if (kSaveLayerDeviceRenderTarget_TexType == type) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001728 // we know layers will only be drawn through drawDevice.
1729 // drawDevice has been made to work with content embedded in a
1730 // larger texture so its okay to use the approximate version.
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001731 match = GrContext::kApprox_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001732 } else {
bsalomon@google.come97f0852011-06-17 13:10:25 +00001733 SkASSERT(kDeviceRenderTarget_TexType == type);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001734 match = GrContext::kExact_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001735 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001736 entry = ctx->lockScratchTexture(desc, match);
reed@google.comac10a2d2010-12-22 21:39:39 +00001737 } else {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001738 if (!bitmap.isVolatile()) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001739 GrContext::TextureKey key = bitmap.getGenerationID();
1740 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
junov@google.com4ee7ae52011-06-30 17:30:49 +00001741
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001742 entry = ctx->findAndLockTexture(key, bitmap.width(),
1743 bitmap.height(), sampler);
1744 if (NULL == entry.texture()) {
1745 entry = sk_gr_create_bitmap_texture(ctx, key, sampler,
junov@google.com4ee7ae52011-06-30 17:30:49 +00001746 bitmap);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001747 }
junov@google.com4ee7ae52011-06-30 17:30:49 +00001748 } else {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001749 entry = sk_gr_create_bitmap_texture(ctx, gUNCACHED_KEY, sampler, bitmap);
junov@google.com4ee7ae52011-06-30 17:30:49 +00001750 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001751 if (NULL == entry.texture()) {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001752 GrPrintf("---- failed to create texture for cache [%d %d]\n",
1753 bitmap.width(), bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001754 }
1755 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001756 return entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001757}
1758
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001759void SkGpuDevice::unlockCachedTexture(TexCache cache) {
1760 this->context()->unlockTexture(cache);
reed@google.comac10a2d2010-12-22 21:39:39 +00001761}
1762
bsalomon@google.come97f0852011-06-17 13:10:25 +00001763SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1764 int width, int height,
1765 bool isOpaque,
1766 Usage usage) {
1767 return SkNEW_ARGS(SkGpuDevice,(this->context(), config,
1768 width, height, usage));
1769}
1770