blob: 934f1784af42a61a798dc0678047403f1c631a6b [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
bsalomon@google.com27847de2011-02-22 20:59:41 +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.
bsalomon@google.com27847de2011-02-22 20:59:41 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
bsalomon@google.com1fadb202011-12-12 16:10:08 +000010#include "GrContext.h"
11
bsalomon@google.comb505a122012-05-31 18:40:36 +000012#include "effects/GrConvolutionEffect.h"
tomhudson@google.comaa72eab2012-07-19 18:01:07 +000013#include "effects/GrSingleTextureEffect.h"
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000014#include "effects/GrConfigConversionEffect.h"
bsalomon@google.comb505a122012-05-31 18:40:36 +000015
tomhudson@google.com278cbb42011-06-30 19:37:01 +000016#include "GrBufferAllocPool.h"
bsalomon@google.com05ef5102011-05-02 21:14:59 +000017#include "GrGpu.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000018#include "GrIndexBuffer.h"
19#include "GrInOrderDrawBuffer.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000020#include "GrPathRenderer.h"
tomhudson@google.comd22b6e42011-06-24 15:53:40 +000021#include "GrPathUtils.h"
bsalomon@google.com50398bf2011-07-26 20:45:30 +000022#include "GrResourceCache.h"
robertphillips@google.com72176b22012-05-23 13:19:12 +000023#include "GrSoftwarePathRenderer.h"
bsalomon@google.com558a75b2011-08-08 17:01:14 +000024#include "GrStencilBuffer.h"
tomhudson@google.com278cbb42011-06-30 19:37:01 +000025#include "GrTextStrike.h"
bsalomon@google.com82b0ec62013-02-07 21:02:36 +000026#include "SkRTConf.h"
sugoi@google.com5f74cf82012-12-17 21:16:45 +000027#include "SkStrokeRec.h"
bsalomon@google.com8c2fe992011-09-13 15:27:18 +000028#include "SkTLazy.h"
bsalomon@google.comc0af3172012-06-15 14:10:09 +000029#include "SkTLS.h"
tomhudson@google.com0c8d93a2011-07-01 17:08:26 +000030#include "SkTrace.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000031
reed@google.comfa35e3d2012-06-26 20:16:17 +000032SK_DEFINE_INST_COUNT(GrContext)
33SK_DEFINE_INST_COUNT(GrDrawState)
34
bsalomon@google.com82b0ec62013-02-07 21:02:36 +000035// It can be useful to set this to false to test whether a bug is caused by using the
bsalomon@google.com1d4edd32012-08-16 18:36:06 +000036// InOrderDrawBuffer, to compare performance of using/not using InOrderDrawBuffer, or to make
bsalomon@google.com82b0ec62013-02-07 21:02:36 +000037// debugging simpler.
38SK_CONF_DECLARE(bool, c_Defer, "gpu.deferContext", true, "Defers rendering in GrContext via GrInOrderDrawBuffer.");
39
40#define BUFFERED_DRAW (c_Defer ? kYes_BufferedDraw : kNo_BufferedDraw)
bsalomon@google.com27847de2011-02-22 20:59:41 +000041
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +000042#define MAX_BLUR_SIGMA 4.0f
43
bsalomon@google.comd46e2422011-09-23 17:40:07 +000044// When we're using coverage AA but the blend is incompatible (given gpu
45// limitations) should we disable AA or draw wrong?
bsalomon@google.com950d7a82011-09-28 15:05:33 +000046#define DISABLE_COVERAGE_AA_FOR_BLEND 1
bsalomon@google.comd46e2422011-09-23 17:40:07 +000047
reed@google.com4b2d3f32012-05-15 18:05:50 +000048#if GR_DEBUG
49 // change this to a 1 to see notifications when partial coverage fails
50 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
51#else
52 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
53#endif
54
robertphillips@google.com1947ba62012-10-17 13:35:24 +000055static const size_t MAX_TEXTURE_CACHE_COUNT = 2048;
djsollen@google.com42041e62012-10-29 19:24:45 +000056static const size_t MAX_TEXTURE_CACHE_BYTES = GR_DEFAULT_TEXTURE_CACHE_MB_LIMIT * 1024 * 1024;
bsalomon@google.com27847de2011-02-22 20:59:41 +000057
bsalomon@google.com60361492012-03-15 17:47:06 +000058static const size_t DRAW_BUFFER_VBPOOL_BUFFER_SIZE = 1 << 15;
bsalomon@google.com27847de2011-02-22 20:59:41 +000059static const int DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS = 4;
60
bsalomon@google.com1d4edd32012-08-16 18:36:06 +000061static const size_t DRAW_BUFFER_IBPOOL_BUFFER_SIZE = 1 << 11;
62static const int DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS = 4;
bsalomon@google.com27847de2011-02-22 20:59:41 +000063
bsalomon@google.combc4b6542011-11-19 13:56:11 +000064#define ASSERT_OWNED_RESOURCE(R) GrAssert(!(R) || (R)->getContext() == this)
65
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000066GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext) {
67 GrContext* context = SkNEW(GrContext);
68 if (context->init(backend, backendContext)) {
69 return context;
70 } else {
71 context->unref();
72 return NULL;
bsalomon@google.com27847de2011-02-22 20:59:41 +000073 }
bsalomon@google.com27847de2011-02-22 20:59:41 +000074}
75
bsalomon@google.comc0af3172012-06-15 14:10:09 +000076namespace {
77void* CreateThreadInstanceCount() {
tomhudson@google.comc377baf2012-07-09 20:17:56 +000078 return SkNEW_ARGS(int, (0));
bsalomon@google.comc0af3172012-06-15 14:10:09 +000079}
80void DeleteThreadInstanceCount(void* v) {
81 delete reinterpret_cast<int*>(v);
82}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000083#define THREAD_INSTANCE_COUNT \
84 (*reinterpret_cast<int*>(SkTLS::Get(CreateThreadInstanceCount, DeleteThreadInstanceCount)))
85}
bsalomon@google.comc0af3172012-06-15 14:10:09 +000086
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000087GrContext::GrContext() {
88 ++THREAD_INSTANCE_COUNT;
89 fDrawState = NULL;
90 fGpu = NULL;
91 fPathRendererChain = NULL;
92 fSoftwarePathRenderer = NULL;
93 fTextureCache = NULL;
94 fFontCache = NULL;
95 fDrawBuffer = NULL;
96 fDrawBufferVBAllocPool = NULL;
97 fDrawBufferIBAllocPool = NULL;
98 fAARectRenderer = NULL;
99}
100
101bool GrContext::init(GrBackend backend, GrBackendContext backendContext) {
102 GrAssert(NULL == fGpu);
103
104 fGpu = GrGpu::Create(backend, backendContext, this);
105 if (NULL == fGpu) {
106 return false;
107 }
108
109 fDrawState = SkNEW(GrDrawState);
110 fGpu->setDrawState(fDrawState);
111
112
113 fTextureCache = SkNEW_ARGS(GrResourceCache,
114 (MAX_TEXTURE_CACHE_COUNT,
115 MAX_TEXTURE_CACHE_BYTES));
116 fFontCache = SkNEW_ARGS(GrFontCache, (fGpu));
117
118 fLastDrawWasBuffered = kNo_BufferedDraw;
119
120 fAARectRenderer = SkNEW(GrAARectRenderer);
121
122 fDidTestPMConversions = false;
123
124 this->setupDrawBuffer();
125
126 return true;
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000127}
128
129int GrContext::GetThreadInstanceCount() {
130 return THREAD_INSTANCE_COUNT;
131}
132
bsalomon@google.com27847de2011-02-22 20:59:41 +0000133GrContext::~GrContext() {
robertphillips@google.comcdb426d2012-09-24 19:33:59 +0000134 for (int i = 0; i < fCleanUpData.count(); ++i) {
135 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo);
136 }
137
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000138 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000139
140 // Since the gpu can hold scratch textures, give it a chance to let go
141 // of them before freeing the texture cache
142 fGpu->purgeResources();
143
bsalomon@google.com27847de2011-02-22 20:59:41 +0000144 delete fTextureCache;
robertphillips@google.comeb9b3e12012-09-11 12:50:53 +0000145 fTextureCache = NULL;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000146 delete fFontCache;
147 delete fDrawBuffer;
148 delete fDrawBufferVBAllocPool;
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000149 delete fDrawBufferIBAllocPool;
bsalomon@google.com30085192011-08-19 15:42:31 +0000150
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000151 fAARectRenderer->unref();
152
bsalomon@google.com205d4602011-04-25 12:43:45 +0000153 fGpu->unref();
bsalomon@google.com30085192011-08-19 15:42:31 +0000154 GrSafeUnref(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000155 GrSafeUnref(fSoftwarePathRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000156 fDrawState->unref();
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000157
158 --THREAD_INSTANCE_COUNT;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000159}
160
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000161void GrContext::contextLost() {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000162 this->contextDestroyed();
junov@google.com53a55842011-06-08 22:55:10 +0000163 this->setupDrawBuffer();
164}
165
166void GrContext::contextDestroyed() {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000167 // abandon first to so destructors
168 // don't try to free the resources in the API.
169 fGpu->abandonResources();
170
bsalomon@google.com30085192011-08-19 15:42:31 +0000171 // a path renderer may be holding onto resources that
172 // are now unusable
173 GrSafeSetNull(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000174 GrSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com30085192011-08-19 15:42:31 +0000175
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000176 delete fDrawBuffer;
177 fDrawBuffer = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000178
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000179 delete fDrawBufferVBAllocPool;
180 fDrawBufferVBAllocPool = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000181
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000182 delete fDrawBufferIBAllocPool;
183 fDrawBufferIBAllocPool = NULL;
184
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000185 fAARectRenderer->reset();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000186
bsalomon@google.coma2921122012-08-28 12:34:17 +0000187 fTextureCache->purgeAllUnlocked();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000188 fFontCache->freeAll();
189 fGpu->markContextDirty();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000190}
191
192void GrContext::resetContext() {
193 fGpu->markContextDirty();
194}
195
196void GrContext::freeGpuResources() {
197 this->flush();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000198
robertphillips@google.comff175842012-05-14 19:31:39 +0000199 fGpu->purgeResources();
200
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000201 fAARectRenderer->reset();
202
bsalomon@google.coma2921122012-08-28 12:34:17 +0000203 fTextureCache->purgeAllUnlocked();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000204 fFontCache->freeAll();
bsalomon@google.com30085192011-08-19 15:42:31 +0000205 // a path renderer may be holding onto resources
206 GrSafeSetNull(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000207 GrSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000208}
209
twiz@google.com05e70242012-01-27 19:12:00 +0000210size_t GrContext::getGpuTextureCacheBytes() const {
211 return fTextureCache->getCachedResourceBytes();
212}
213
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000214////////////////////////////////////////////////////////////////////////////////
215
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000216namespace {
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000217
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000218void scale_rect(SkRect* rect, float xScale, float yScale) {
robertphillips@google.com5af56062012-04-27 15:39:52 +0000219 rect->fLeft = SkScalarMul(rect->fLeft, SkFloatToScalar(xScale));
220 rect->fTop = SkScalarMul(rect->fTop, SkFloatToScalar(yScale));
221 rect->fRight = SkScalarMul(rect->fRight, SkFloatToScalar(xScale));
222 rect->fBottom = SkScalarMul(rect->fBottom, SkFloatToScalar(yScale));
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000223}
224
bsalomon@google.comb505a122012-05-31 18:40:36 +0000225float adjust_sigma(float sigma, int *scaleFactor, int *radius) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000226 *scaleFactor = 1;
227 while (sigma > MAX_BLUR_SIGMA) {
228 *scaleFactor *= 2;
229 sigma *= 0.5f;
230 }
bsalomon@google.comb505a122012-05-31 18:40:36 +0000231 *radius = static_cast<int>(ceilf(sigma * 3.0f));
232 GrAssert(*radius <= GrConvolutionEffect::kMaxKernelRadius);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000233 return sigma;
234}
235
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000236void convolve_gaussian(GrDrawTarget* target,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000237 GrTexture* texture,
238 const SkRect& rect,
239 float sigma,
240 int radius,
241 Gr1DKernelEffect::Direction direction) {
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000242 GrRenderTarget* rt = target->drawState()->getRenderTarget();
243 GrDrawTarget::AutoStateRestore asr(target, GrDrawTarget::kReset_ASRInit);
244 GrDrawState* drawState = target->drawState();
245 drawState->setRenderTarget(rt);
bsalomon@google.comc5fae9e2013-01-24 19:00:02 +0000246 SkAutoTUnref<GrEffectRef> conv(GrConvolutionEffect::CreateGaussian(texture,
bsalomon@google.com6340a412013-01-22 19:55:59 +0000247 direction,
248 radius,
249 sigma));
bsalomon@google.comadc65362013-01-28 14:26:09 +0000250 drawState->setEffect(0, conv);
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000251 target->drawSimpleRect(rect, NULL);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000252}
253
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000254}
255
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000256////////////////////////////////////////////////////////////////////////////////
257
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000258GrTexture* GrContext::findAndRefTexture(const GrTextureDesc& desc,
259 const GrCacheID& cacheID,
260 const GrTextureParams* params) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000261 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheID);
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000262 GrResource* resource = fTextureCache->find(resourceKey);
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000263 SkSafeRef(resource);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000264 return static_cast<GrTexture*>(resource);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000265}
266
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000267bool GrContext::isTextureInCache(const GrTextureDesc& desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000268 const GrCacheID& cacheID,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000269 const GrTextureParams* params) const {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000270 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheID);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000271 return fTextureCache->hasKey(resourceKey);
272}
273
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000274void GrContext::addStencilBuffer(GrStencilBuffer* sb) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000275 ASSERT_OWNED_RESOURCE(sb);
robertphillips@google.com46a86002012-08-08 10:42:44 +0000276
277 GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(sb->width(),
278 sb->height(),
279 sb->numSamples());
robertphillips@google.com209a1142012-10-31 12:25:21 +0000280 fTextureCache->addResource(resourceKey, sb);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000281}
282
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000283GrStencilBuffer* GrContext::findStencilBuffer(int width, int height,
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000284 int sampleCnt) {
robertphillips@google.com46a86002012-08-08 10:42:44 +0000285 GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(width,
286 height,
287 sampleCnt);
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000288 GrResource* resource = fTextureCache->find(resourceKey);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000289 return static_cast<GrStencilBuffer*>(resource);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000290}
291
bsalomon@google.com27847de2011-02-22 20:59:41 +0000292static void stretchImage(void* dst,
293 int dstW,
294 int dstH,
295 void* src,
296 int srcW,
297 int srcH,
298 int bpp) {
299 GrFixed dx = (srcW << 16) / dstW;
300 GrFixed dy = (srcH << 16) / dstH;
301
302 GrFixed y = dy >> 1;
303
304 int dstXLimit = dstW*bpp;
305 for (int j = 0; j < dstH; ++j) {
306 GrFixed x = dx >> 1;
307 void* srcRow = (uint8_t*)src + (y>>16)*srcW*bpp;
308 void* dstRow = (uint8_t*)dst + j*dstW*bpp;
309 for (int i = 0; i < dstXLimit; i += bpp) {
310 memcpy((uint8_t*) dstRow + i,
311 (uint8_t*) srcRow + (x>>16)*bpp,
312 bpp);
313 x += dx;
314 }
315 y += dy;
316 }
317}
318
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000319// The desired texture is NPOT and tiled but that isn't supported by
robertphillips@google.com3319f332012-08-13 18:00:36 +0000320// the current hardware. Resize the texture to be a POT
321GrTexture* GrContext::createResizedTexture(const GrTextureDesc& desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000322 const GrCacheID& cacheID,
robertphillips@google.com3319f332012-08-13 18:00:36 +0000323 void* srcData,
324 size_t rowBytes,
325 bool needsFiltering) {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000326 SkAutoTUnref<GrTexture> clampedTexture(this->findAndRefTexture(desc, cacheID, NULL));
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000327 if (NULL == clampedTexture) {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000328 clampedTexture.reset(this->createTexture(NULL, desc, cacheID, srcData, rowBytes));
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000329
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000330 if (NULL == clampedTexture) {
robertphillips@google.com3319f332012-08-13 18:00:36 +0000331 return NULL;
332 }
333 }
robertphillips@google.com0d25eef2012-09-11 21:25:51 +0000334
robertphillips@google.com3319f332012-08-13 18:00:36 +0000335 GrTextureDesc rtDesc = desc;
336 rtDesc.fFlags = rtDesc.fFlags |
337 kRenderTarget_GrTextureFlagBit |
338 kNoStencil_GrTextureFlagBit;
339 rtDesc.fWidth = GrNextPow2(GrMax(desc.fWidth, 64));
340 rtDesc.fHeight = GrNextPow2(GrMax(desc.fHeight, 64));
341
342 GrTexture* texture = fGpu->createTexture(rtDesc, NULL, 0);
343
344 if (NULL != texture) {
345 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
346 GrDrawState* drawState = fGpu->drawState();
347 drawState->setRenderTarget(texture->asRenderTarget());
348
349 // if filtering is not desired then we want to ensure all
350 // texels in the resampled image are copies of texels from
351 // the original.
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000352 GrTextureParams params(SkShader::kClamp_TileMode, needsFiltering);
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000353 drawState->createTextureEffect(0, clampedTexture, SkMatrix::I(), params);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000354
jvanverth@google.com39768252013-02-14 15:25:44 +0000355 static const GrVertexLayout layout = GrDrawState::StageTexCoordVertexLayoutBit(0);
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000356 drawState->setVertexLayout(layout);
357 GrDrawTarget::AutoReleaseGeometry arg(fGpu, 4, 0);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000358
359 if (arg.succeeded()) {
360 GrPoint* verts = (GrPoint*) arg.vertices();
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000361 verts[0].setIRectFan(0, 0, texture->width(), texture->height(), 2 * sizeof(GrPoint));
362 verts[1].setIRectFan(0, 0, 1, 1, 2 * sizeof(GrPoint));
363 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000364 }
365 texture->releaseRenderTarget();
366 } else {
367 // TODO: Our CPU stretch doesn't filter. But we create separate
bsalomon@google.com08283af2012-10-26 13:01:20 +0000368 // stretched textures when the texture params is either filtered or
robertphillips@google.com3319f332012-08-13 18:00:36 +0000369 // not. Either implement filtered stretch blit on CPU or just create
370 // one when FBO case fails.
371
372 rtDesc.fFlags = kNone_GrTextureFlags;
373 // no longer need to clamp at min RT size.
374 rtDesc.fWidth = GrNextPow2(desc.fWidth);
375 rtDesc.fHeight = GrNextPow2(desc.fHeight);
376 int bpp = GrBytesPerPixel(desc.fConfig);
bsalomon@google.com08283af2012-10-26 13:01:20 +0000377 SkAutoSMalloc<128*128*4> stretchedPixels(bpp * rtDesc.fWidth * rtDesc.fHeight);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000378 stretchImage(stretchedPixels.get(), rtDesc.fWidth, rtDesc.fHeight,
bsalomon@google.com08283af2012-10-26 13:01:20 +0000379 srcData, desc.fWidth, desc.fHeight, bpp);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000380
381 size_t stretchedRowBytes = rtDesc.fWidth * bpp;
382
humper@google.com0e515772013-01-07 19:54:40 +0000383 SkDEBUGCODE(GrTexture* texture = )fGpu->createTexture(rtDesc, stretchedPixels.get(), stretchedRowBytes);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000384 GrAssert(NULL != texture);
385 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000386
387 return texture;
388}
389
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000390GrTexture* GrContext::createTexture(const GrTextureParams* params,
391 const GrTextureDesc& desc,
392 const GrCacheID& cacheID,
393 void* srcData,
394 size_t rowBytes) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000395 SK_TRACE_EVENT0("GrContext::createTexture");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000396
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000397 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheID);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000398
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000399 GrTexture* texture;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000400 if (GrTexture::NeedsResizing(resourceKey)) {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000401 texture = this->createResizedTexture(desc, cacheID,
402 srcData, rowBytes,
403 GrTexture::NeedsFiltering(resourceKey));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000404 } else {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000405 texture= fGpu->createTexture(desc, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000406 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000407
408 if (NULL != texture) {
robertphillips@google.com209a1142012-10-31 12:25:21 +0000409 fTextureCache->addResource(resourceKey, texture);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000410 }
411
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000412 return texture;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000413}
414
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000415GrTexture* GrContext::lockAndRefScratchTexture(const GrTextureDesc& inDesc, ScratchTexMatch match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000416 GrTextureDesc desc = inDesc;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000417
rileya@google.com2afb8ec2012-08-23 14:08:57 +0000418 GrAssert((desc.fFlags & kRenderTarget_GrTextureFlagBit) ||
419 !(desc.fFlags & kNoStencil_GrTextureFlagBit));
420
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000421 if (kApprox_ScratchTexMatch == match) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000422 // bin by pow2 with a reasonable min
423 static const int MIN_SIZE = 256;
424 desc.fWidth = GrMax(MIN_SIZE, GrNextPow2(desc.fWidth));
425 desc.fHeight = GrMax(MIN_SIZE, GrNextPow2(desc.fHeight));
426 }
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000427
robertphillips@google.com94ba6c62013-02-27 16:43:49 +0000428 // Renderable A8 targets are not universally supported (e.g., not on ANGLE)
429 GrAssert(this->isConfigRenderable(kAlpha_8_GrPixelConfig) ||
430 !(desc.fFlags & kRenderTarget_GrTextureFlagBit) ||
431 (desc.fConfig != kAlpha_8_GrPixelConfig));
432
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000433 GrResource* resource = NULL;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000434 int origWidth = desc.fWidth;
435 int origHeight = desc.fHeight;
436 bool doubledW = false;
437 bool doubledH = false;
438
439 do {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000440 GrResourceKey key = GrTexture::ComputeScratchKey(desc);
robertphillips@google.com209a1142012-10-31 12:25:21 +0000441 // Ensure we have exclusive access to the texture so future 'find' calls don't return it
442 resource = fTextureCache->find(key, GrResourceCache::kHide_OwnershipFlag);
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000443 if (NULL != resource) {
444 resource->ref();
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000445 break;
446 }
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000447 if (kExact_ScratchTexMatch == match) {
448 break;
449 }
450 // We had a cache miss and we are in approx mode, relax the fit of the flags... then try
451 // doubling width... then the height.
452
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000453 // We no longer try to reuse textures that were previously used as render targets in
rileya@google.com2afb8ec2012-08-23 14:08:57 +0000454 // situations where no RT is needed; doing otherwise can confuse the video driver and
455 // cause significant performance problems in some cases.
456 if (desc.fFlags & kNoStencil_GrTextureFlagBit) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000457 desc.fFlags = desc.fFlags & ~kNoStencil_GrTextureFlagBit;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000458 } else if (!doubledW) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000459 desc.fFlags = inDesc.fFlags;
460 desc.fWidth *= 2;
461 doubledW = true;
462 } else if (!doubledH) {
463 desc.fFlags = inDesc.fFlags;
464 desc.fWidth = origWidth;
465 desc.fHeight *= 2;
466 doubledH = true;
467 } else {
468 break;
469 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000470
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000471 } while (true);
472
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000473 if (NULL == resource) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000474 desc.fFlags = inDesc.fFlags;
475 desc.fWidth = origWidth;
476 desc.fHeight = origHeight;
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000477 GrTexture* texture = fGpu->createTexture(desc, NULL, 0);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000478 if (NULL != texture) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000479 GrResourceKey key = GrTexture::ComputeScratchKey(texture->desc());
robertphillips@google.com209a1142012-10-31 12:25:21 +0000480 // Make the resource exclusive so future 'find' calls don't return it
481 fTextureCache->addResource(key, texture, GrResourceCache::kHide_OwnershipFlag);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000482 resource = texture;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000483 }
484 }
485
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000486 return static_cast<GrTexture*>(resource);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000487}
488
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000489void GrContext::addExistingTextureToCache(GrTexture* texture) {
490
491 if (NULL == texture) {
492 return;
493 }
494
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000495 // This texture should already have a cache entry since it was once
496 // attached
497 GrAssert(NULL != texture->getCacheEntry());
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000498
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000499 // Conceptually, the cache entry is going to assume responsibility
500 // for the creation ref.
501 GrAssert(1 == texture->getRefCnt());
502
503 // Since this texture came from an AutoScratchTexture it should
504 // still be in the exclusive pile
505 fTextureCache->makeNonExclusive(texture->getCacheEntry());
506
robertphillips@google.com50a035d2012-09-07 19:44:33 +0000507 this->purgeCache();
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000508}
509
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000510
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000511void GrContext::unlockScratchTexture(GrTexture* texture) {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000512 ASSERT_OWNED_RESOURCE(texture);
513 GrAssert(NULL != texture->getCacheEntry());
514
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000515 // If this is a scratch texture we detached it from the cache
516 // while it was locked (to avoid two callers simultaneously getting
517 // the same texture).
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000518 if (texture->getCacheEntry()->key().isScratch()) {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000519 fTextureCache->makeNonExclusive(texture->getCacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000520 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000521
robertphillips@google.com50a035d2012-09-07 19:44:33 +0000522 this->purgeCache();
523}
524
525void GrContext::purgeCache() {
robertphillips@google.comeb9b3e12012-09-11 12:50:53 +0000526 if (NULL != fTextureCache) {
527 fTextureCache->purgeAsNeeded();
528 }
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000529}
530
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000531GrTexture* GrContext::createUncachedTexture(const GrTextureDesc& descIn,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000532 void* srcData,
533 size_t rowBytes) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000534 GrTextureDesc descCopy = descIn;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000535 return fGpu->createTexture(descCopy, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000536}
537
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000538void GrContext::getTextureCacheLimits(int* maxTextures,
539 size_t* maxTextureBytes) const {
540 fTextureCache->getLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000541}
542
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000543void GrContext::setTextureCacheLimits(int maxTextures, size_t maxTextureBytes) {
544 fTextureCache->setLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000545}
546
bsalomon@google.com91958362011-06-13 17:58:13 +0000547int GrContext::getMaxTextureSize() const {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000548 return fGpu->getCaps().maxTextureSize();
bsalomon@google.com91958362011-06-13 17:58:13 +0000549}
550
551int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000552 return fGpu->getCaps().maxRenderTargetSize();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000553}
554
555///////////////////////////////////////////////////////////////////////////////
556
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000557GrTexture* GrContext::wrapBackendTexture(const GrBackendTextureDesc& desc) {
558 return fGpu->wrapBackendTexture(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000559}
560
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000561GrRenderTarget* GrContext::wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
562 return fGpu->wrapBackendRenderTarget(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000563}
564
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000565///////////////////////////////////////////////////////////////////////////////
566
bsalomon@google.comb8670992012-07-25 21:27:09 +0000567bool GrContext::supportsIndex8PixelConfig(const GrTextureParams* params,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000568 int width, int height) const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000569 const GrDrawTarget::Caps& caps = fGpu->getCaps();
bsalomon@google.comf6601872012-08-28 21:11:35 +0000570 if (!caps.eightBitPaletteSupport()) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000571 return false;
572 }
573
bsalomon@google.com27847de2011-02-22 20:59:41 +0000574 bool isPow2 = GrIsPow2(width) && GrIsPow2(height);
575
576 if (!isPow2) {
bsalomon@google.comb8670992012-07-25 21:27:09 +0000577 bool tiled = NULL != params && params->isTiled();
bsalomon@google.comf6601872012-08-28 21:11:35 +0000578 if (tiled && !caps.npotTextureTileSupport()) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000579 return false;
580 }
581 }
582 return true;
583}
584
585////////////////////////////////////////////////////////////////////////////////
586
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000587const GrClipData* GrContext::getClip() const {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000588 return fGpu->getClip();
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000589}
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000590
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000591void GrContext::setClip(const GrClipData* clipData) {
592 fGpu->setClip(clipData);
robertphillips@google.com837ec432012-10-04 17:57:05 +0000593
skia.committer@gmail.comf467ce72012-10-09 02:01:37 +0000594 fDrawState->setState(GrDrawState::kClip_StateBit,
robertphillips@google.com183e34b2012-10-19 18:19:01 +0000595 clipData && clipData->fClipStack && !clipData->fClipStack->isWideOpen());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000596}
597
bsalomon@google.com27847de2011-02-22 20:59:41 +0000598////////////////////////////////////////////////////////////////////////////////
599
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000600void GrContext::clear(const GrIRect* rect,
601 const GrColor color,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000602 GrRenderTarget* target) {
bsalomon@google.com82b0ec62013-02-07 21:02:36 +0000603 this->prepareToDraw(NULL, BUFFERED_DRAW)->clear(rect, color, target);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000604}
605
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000606void GrContext::drawPaint(const GrPaint& origPaint) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000607 // set rect to be big enough to fill the space, but not super-huge, so we
608 // don't overflow fixed-point implementations
bsalomon@google.comd302f142011-03-03 13:54:13 +0000609 GrRect r;
610 r.setLTRB(0, 0,
bsalomon@google.com81712882012-11-01 17:12:34 +0000611 SkIntToScalar(getRenderTarget()->width()),
612 SkIntToScalar(getRenderTarget()->height()));
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000613 SkMatrix inverse;
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000614 SkTCopyOnFirstWrite<GrPaint> paint(origPaint);
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000615 AutoMatrix am;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000616
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000617 // We attempt to map r by the inverse matrix and draw that. mapRect will
618 // map the four corners and bound them with a new rect. This will not
619 // produce a correct result for some perspective matrices.
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000620 if (!this->getMatrix().hasPerspective()) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000621 if (!fDrawState->getViewInverse(&inverse)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000622 GrPrintf("Could not invert matrix\n");
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000623 return;
624 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000625 inverse.mapRect(&r);
626 } else {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000627 if (!am.setIdentity(this, paint.writable())) {
628 GrPrintf("Could not invert matrix\n");
629 return;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000630 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000631 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000632 // by definition this fills the entire clip, no need for AA
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000633 if (paint->isAntiAlias()) {
634 paint.writable()->setAntiAlias(false);
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000635 }
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000636 this->drawRect(*paint, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000637}
638
bsalomon@google.com205d4602011-04-25 12:43:45 +0000639////////////////////////////////////////////////////////////////////////////////
640
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000641namespace {
642inline bool disable_coverage_aa_for_blend(GrDrawTarget* target) {
643 return DISABLE_COVERAGE_AA_FOR_BLEND && !target->canApplyCoverage();
644}
645}
646
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000647////////////////////////////////////////////////////////////////////////////////
648
bsalomon@google.com27847de2011-02-22 20:59:41 +0000649/* create a triangle strip that strokes the specified triangle. There are 8
650 unique vertices, but we repreat the last 2 to close up. Alternatively we
651 could use an indices array, and then only send 8 verts, but not sure that
652 would be faster.
653 */
bsalomon@google.com205d4602011-04-25 12:43:45 +0000654static void setStrokeRectStrip(GrPoint verts[10], GrRect rect,
bsalomon@google.com81712882012-11-01 17:12:34 +0000655 SkScalar width) {
656 const SkScalar rad = SkScalarHalf(width);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000657 rect.sort();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000658
659 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
660 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
661 verts[2].set(rect.fRight - rad, rect.fTop + rad);
662 verts[3].set(rect.fRight + rad, rect.fTop - rad);
663 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
664 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
665 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
666 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
667 verts[8] = verts[0];
668 verts[9] = verts[1];
669}
670
reed@google.com20efde72011-05-09 17:00:02 +0000671/**
672 * Returns true if the rects edges are integer-aligned.
673 */
674static bool isIRect(const GrRect& r) {
bsalomon@google.com81712882012-11-01 17:12:34 +0000675 return SkScalarIsInt(r.fLeft) && SkScalarIsInt(r.fTop) &&
676 SkScalarIsInt(r.fRight) && SkScalarIsInt(r.fBottom);
reed@google.com20efde72011-05-09 17:00:02 +0000677}
678
bsalomon@google.com205d4602011-04-25 12:43:45 +0000679static bool apply_aa_to_rect(GrDrawTarget* target,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000680 const GrRect& rect,
bsalomon@google.com81712882012-11-01 17:12:34 +0000681 SkScalar width,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000682 const SkMatrix* matrix,
683 SkMatrix* combinedMatrix,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000684 GrRect* devRect,
685 bool* useVertexCoverage) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000686 // we use a simple coverage ramp to do aa on axis-aligned rects
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000687 // we check if the rect will be axis-aligned, and the rect won't land on
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000688 // integer coords.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000689
bsalomon@google.coma3108262011-10-10 14:08:47 +0000690 // we are keeping around the "tweak the alpha" trick because
691 // it is our only hope for the fixed-pipe implementation.
692 // In a shader implementation we can give a separate coverage input
bsalomon@google.com289533a2011-10-27 12:34:25 +0000693 // TODO: remove this ugliness when we drop the fixed-pipe impl
bsalomon@google.coma3108262011-10-10 14:08:47 +0000694 *useVertexCoverage = false;
bsalomon@google.com2b446732013-02-12 16:47:41 +0000695 if (!target->getDrawState().canTweakAlphaForCoverage()) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000696 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +0000697#if GR_DEBUG
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000698 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +0000699#endif
bsalomon@google.coma3108262011-10-10 14:08:47 +0000700 return false;
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000701 } else {
702 *useVertexCoverage = true;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000703 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000704 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000705 const GrDrawState& drawState = target->getDrawState();
706 if (drawState.getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000707 return false;
708 }
709
bsalomon@google.com471d4712011-08-23 15:45:25 +0000710 if (0 == width && target->willUseHWAALines()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000711 return false;
712 }
713
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000714 if (!drawState.getViewMatrix().preservesAxisAlignment()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000715 return false;
716 }
717
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000718 if (NULL != matrix &&
bsalomon@google.com205d4602011-04-25 12:43:45 +0000719 !matrix->preservesAxisAlignment()) {
720 return false;
721 }
722
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000723 *combinedMatrix = drawState.getViewMatrix();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000724 if (NULL != matrix) {
725 combinedMatrix->preConcat(*matrix);
726 GrAssert(combinedMatrix->preservesAxisAlignment());
727 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000728
bsalomon@google.com205d4602011-04-25 12:43:45 +0000729 combinedMatrix->mapRect(devRect, rect);
730 devRect->sort();
731
732 if (width < 0) {
reed@google.com20efde72011-05-09 17:00:02 +0000733 return !isIRect(*devRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000734 } else {
735 return true;
736 }
737}
738
bsalomon@google.com27847de2011-02-22 20:59:41 +0000739void GrContext::drawRect(const GrPaint& paint,
740 const GrRect& rect,
bsalomon@google.com81712882012-11-01 17:12:34 +0000741 SkScalar width,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000742 const SkMatrix* matrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000743 SK_TRACE_EVENT0("GrContext::drawRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000744
bsalomon@google.com82b0ec62013-02-07 21:02:36 +0000745 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000746 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000747
bsalomon@google.com205d4602011-04-25 12:43:45 +0000748 GrRect devRect = rect;
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000749 SkMatrix combinedMatrix;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000750 bool useVertexCoverage;
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000751 bool needAA = paint.isAntiAlias() &&
bsalomon@google.com289533a2011-10-27 12:34:25 +0000752 !this->getRenderTarget()->isMultisampled();
753 bool doAA = needAA && apply_aa_to_rect(target, rect, width, matrix,
754 &combinedMatrix, &devRect,
755 &useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000756
757 if (doAA) {
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000758 GrDrawState::AutoDeviceCoordDraw adcd(target->drawState());
bsalomon@google.come3d32162012-07-20 13:37:06 +0000759 if (!adcd.succeeded()) {
760 return;
761 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000762 if (width >= 0) {
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000763 GrVec strokeSize;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000764 if (width > 0) {
765 strokeSize.set(width, width);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000766 combinedMatrix.mapVectors(&strokeSize, 1);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000767 strokeSize.setAbs(strokeSize);
768 } else {
bsalomon@google.com81712882012-11-01 17:12:34 +0000769 strokeSize.set(SK_Scalar1, SK_Scalar1);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000770 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000771 fAARectRenderer->strokeAARect(this->getGpu(), target, devRect,
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000772 strokeSize, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000773 } else {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000774 fAARectRenderer->fillAARect(this->getGpu(), target,
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000775 devRect, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000776 }
777 return;
778 }
779
bsalomon@google.com27847de2011-02-22 20:59:41 +0000780 if (width >= 0) {
781 // TODO: consider making static vertex buffers for these cases.
bsalomon@google.com64386952013-02-08 21:22:44 +0000782 // Hairline could be done by just adding closing vertex to
783 // unitSquareVertexBuffer()
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000784
bsalomon@google.com27847de2011-02-22 20:59:41 +0000785 static const int worstCaseVertCount = 10;
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000786 target->drawState()->setVertexLayout(GrDrawState::kDefault_VertexLayout);
787 GrDrawTarget::AutoReleaseGeometry geo(target, worstCaseVertCount, 0);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000788
789 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000790 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000791 return;
792 }
793
794 GrPrimitiveType primType;
795 int vertCount;
796 GrPoint* vertex = geo.positions();
797
798 if (width > 0) {
799 vertCount = 10;
bsalomon@google.com47059542012-06-06 20:51:20 +0000800 primType = kTriangleStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000801 setStrokeRectStrip(vertex, rect, width);
802 } else {
803 // hairline
804 vertCount = 5;
bsalomon@google.com47059542012-06-06 20:51:20 +0000805 primType = kLineStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000806 vertex[0].set(rect.fLeft, rect.fTop);
807 vertex[1].set(rect.fRight, rect.fTop);
808 vertex[2].set(rect.fRight, rect.fBottom);
809 vertex[3].set(rect.fLeft, rect.fBottom);
810 vertex[4].set(rect.fLeft, rect.fTop);
811 }
812
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000813 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000814 if (NULL != matrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000815 GrDrawState* drawState = target->drawState();
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000816 avmr.set(drawState, *matrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000817 }
818
819 target->drawNonIndexed(primType, 0, vertCount);
820 } else {
bsalomon@google.com64386952013-02-08 21:22:44 +0000821#if GR_STATIC_RECT_VB
822 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
823 if (NULL == sqVB) {
824 GrPrintf("Failed to create static rect vb.\n");
825 return;
826 }
827
828 GrDrawState* drawState = target->drawState();
829 drawState->setVertexLayout(GrDrawState::kDefault_VertexLayout);
830 target->setVertexSourceToBuffer(sqVB);
831 SkMatrix m;
832 m.setAll(rect.width(), 0, rect.fLeft,
833 0, rect.height(), rect.fTop,
834 0, 0, SkMatrix::I()[8]);
835
836 if (NULL != matrix) {
837 m.postConcat(*matrix);
838 }
839 GrDrawState::AutoViewMatrixRestore avmr(drawState, m);
840
841 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
842#else
843 target->drawSimpleRect(rect, matrix);
844#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +0000845 }
846}
847
848void GrContext::drawRectToRect(const GrPaint& paint,
849 const GrRect& dstRect,
850 const GrRect& srcRect,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000851 const SkMatrix* dstMatrix,
852 const SkMatrix* srcMatrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000853 SK_TRACE_EVENT0("GrContext::drawRectToRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000854
bsalomon@google.com88becf42012-10-05 14:54:42 +0000855 // srcRect refers to paint's first color stage
856 if (!paint.isColorStageEnabled(0)) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000857 drawRect(paint, dstRect, -1, dstMatrix);
858 return;
859 }
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000860
bsalomon@google.com82b0ec62013-02-07 21:02:36 +0000861 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW);
bsalomon@google.com64386952013-02-08 21:22:44 +0000862
863#if GR_STATIC_RECT_VB
864 GrDrawState::AutoStageDisable atr(fDrawState);
865 GrDrawState* drawState = target->drawState();
866
867 SkMatrix m;
868
869 m.setAll(dstRect.width(), 0, dstRect.fLeft,
870 0, dstRect.height(), dstRect.fTop,
871 0, 0, SkMatrix::I()[8]);
872 if (NULL != dstMatrix) {
873 m.postConcat(*dstMatrix);
874 }
875
876 // The first color stage's coords come from srcRect rather than applying a matrix to dstRect.
877 // We explicitly compute a matrix for that stage below, no need to adjust here.
878 static const uint32_t kExplicitCoordMask = 1 << GrPaint::kFirstColorStage;
879 GrDrawState::AutoViewMatrixRestore avmr(drawState, m, kExplicitCoordMask);
880
881 m.setAll(srcRect.width(), 0, srcRect.fLeft,
882 0, srcRect.height(), srcRect.fTop,
883 0, 0, SkMatrix::I()[8]);
884 if (NULL != srcMatrix) {
885 m.postConcat(*srcMatrix);
886 }
887
888 drawState->preConcatStageMatrices(kExplicitCoordMask, m);
889
890 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
891 if (NULL == sqVB) {
892 GrPrintf("Failed to create static rect vb.\n");
893 return;
894 }
895 drawState->setVertexLayout(GrDrawState::kDefault_VertexLayout);
896 target->setVertexSourceToBuffer(sqVB);
897 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
898#else
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000899 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000900
jvanverth@google.com39768252013-02-14 15:25:44 +0000901 target->drawRect(dstRect, dstMatrix, &srcRect, srcMatrix, 0);
bsalomon@google.com64386952013-02-08 21:22:44 +0000902#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +0000903}
904
905void GrContext::drawVertices(const GrPaint& paint,
906 GrPrimitiveType primitiveType,
907 int vertexCount,
908 const GrPoint positions[],
909 const GrPoint texCoords[],
910 const GrColor colors[],
911 const uint16_t indices[],
912 int indexCount) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000913 SK_TRACE_EVENT0("GrContext::drawVertices");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000914
915 GrDrawTarget::AutoReleaseGeometry geo;
916
bsalomon@google.com82b0ec62013-02-07 21:02:36 +0000917 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000918 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000919
bsalomon@google.come3d32162012-07-20 13:37:06 +0000920 GrVertexLayout layout = 0;
921 if (NULL != texCoords) {
jvanverth@google.com39768252013-02-14 15:25:44 +0000922 layout |= GrDrawState::StageTexCoordVertexLayoutBit(0);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000923 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000924 if (NULL != colors) {
jvanverth@google.comcc782382013-01-28 20:39:48 +0000925 layout |= GrDrawState::kColor_VertexLayoutBit;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000926 }
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000927 target->drawState()->setVertexLayout(layout);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000928
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000929 int vertexSize = target->getDrawState().getVertexSize();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000930 if (sizeof(GrPoint) != vertexSize) {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000931 if (!geo.set(target, vertexCount, 0)) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000932 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000933 return;
934 }
jvanverth@google.com39768252013-02-14 15:25:44 +0000935 int texOffset;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000936 int colorOffset;
jvanverth@google.com39768252013-02-14 15:25:44 +0000937 GrDrawState::VertexSizeAndOffsets(layout,
938 &texOffset,
939 &colorOffset,
940 NULL,
941 NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000942 void* curVertex = geo.vertices();
943
944 for (int i = 0; i < vertexCount; ++i) {
945 *((GrPoint*)curVertex) = positions[i];
946
jvanverth@google.com39768252013-02-14 15:25:44 +0000947 if (texOffset > 0) {
948 *(GrPoint*)((intptr_t)curVertex + texOffset) = texCoords[i];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000949 }
950 if (colorOffset > 0) {
951 *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i];
952 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000953 curVertex = (void*)((intptr_t)curVertex + vertexSize);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000954 }
955 } else {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000956 target->setVertexSourceToArray(positions, vertexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000957 }
958
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000959 // we don't currently apply offscreen AA to this path. Need improved
bsalomon@google.com91958362011-06-13 17:58:13 +0000960 // management of GrDrawTarget's geometry to avoid copying points per-tile.
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000961
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000962 if (NULL != indices) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000963 target->setIndexSourceToArray(indices, indexCount);
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000964 target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000965 } else {
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000966 target->drawNonIndexed(primitiveType, 0, vertexCount);
967 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000968}
969
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000970///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com150d2842012-01-12 20:19:56 +0000971namespace {
972
bsalomon@google.com93c96602012-04-27 13:05:21 +0000973struct CircleVertex {
974 GrPoint fPos;
975 GrPoint fCenter;
bsalomon@google.com81712882012-11-01 17:12:34 +0000976 SkScalar fOuterRadius;
977 SkScalar fInnerRadius;
bsalomon@google.com93c96602012-04-27 13:05:21 +0000978};
979
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000980inline bool circleStaysCircle(const SkMatrix& m) {
981 return m.isSimilarity();
bsalomon@google.com93c96602012-04-27 13:05:21 +0000982}
983
984}
985
bsalomon@google.com93c96602012-04-27 13:05:21 +0000986void GrContext::drawOval(const GrPaint& paint,
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000987 const GrRect& oval,
988 const SkStrokeRec& stroke) {
989
990 if (!canDrawOval(paint, oval, stroke)) {
bsalomon@google.com93c96602012-04-27 13:05:21 +0000991 SkPath path;
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000992 path.addOval(oval);
993 this->drawPath(paint, path, stroke);
bsalomon@google.com93c96602012-04-27 13:05:21 +0000994 return;
skia.committer@gmail.com98ded842013-01-23 07:06:17 +0000995 }
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000996
997 internalDrawOval(paint, oval, stroke);
998}
999
1000bool GrContext::canDrawOval(const GrPaint& paint, const GrRect& oval, const SkStrokeRec& stroke) const {
1001
1002 if (!paint.isAntiAlias()) {
1003 return false;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001004 }
skia.committer@gmail.com98ded842013-01-23 07:06:17 +00001005
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001006 // we can draw circles in any style
skia.committer@gmail.com98ded842013-01-23 07:06:17 +00001007 bool isCircle = SkScalarNearlyEqual(oval.width(), oval.height())
1008 && circleStaysCircle(this->getMatrix());
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001009 // and for now, axis-aligned ellipses only with fill or stroke-and-fill
1010 SkStrokeRec::Style style = stroke.getStyle();
1011 bool isStroke = (style == SkStrokeRec::kStroke_Style || style == SkStrokeRec::kHairline_Style);
1012 bool isFilledAxisAlignedEllipse = this->getMatrix().rectStaysRect() && !isStroke;
1013
1014 return isCircle || isFilledAxisAlignedEllipse;
1015}
1016
1017void GrContext::internalDrawOval(const GrPaint& paint,
1018 const GrRect& oval,
1019 const SkStrokeRec& stroke) {
1020
1021 SkScalar xRadius = SkScalarHalf(oval.width());
1022 SkScalar yRadius = SkScalarHalf(oval.height());
skia.committer@gmail.com98ded842013-01-23 07:06:17 +00001023
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001024 SkScalar strokeWidth = stroke.getWidth();
1025 SkStrokeRec::Style style = stroke.getStyle();
1026
skia.committer@gmail.com98ded842013-01-23 07:06:17 +00001027 bool isCircle = SkScalarNearlyEqual(xRadius, yRadius) && circleStaysCircle(this->getMatrix());
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001028#ifdef SK_DEBUG
1029 {
1030 // we should have checked for this previously
1031 bool isStroke = (style == SkStrokeRec::kStroke_Style || style == SkStrokeRec::kHairline_Style);
1032 bool isFilledAxisAlignedEllipse = this->getMatrix().rectStaysRect() && !isStroke;
1033 SkASSERT(paint.isAntiAlias() && (isCircle || isFilledAxisAlignedEllipse));
1034 }
1035#endif
bsalomon@google.com93c96602012-04-27 13:05:21 +00001036
bsalomon@google.com82b0ec62013-02-07 21:02:36 +00001037 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW);
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001038
bsalomon@google.com0982d352012-07-31 15:33:25 +00001039 GrDrawState* drawState = target->drawState();
1040 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001041 const SkMatrix vm = drawState->getViewMatrix();
bsalomon@google.com0982d352012-07-31 15:33:25 +00001042
bsalomon@google.com93c96602012-04-27 13:05:21 +00001043 const GrRenderTarget* rt = drawState->getRenderTarget();
1044 if (NULL == rt) {
1045 return;
1046 }
1047
bsalomon@google.com5b3e8902012-10-05 20:13:28 +00001048 GrDrawState::AutoDeviceCoordDraw adcd(drawState);
bsalomon@google.come3d32162012-07-20 13:37:06 +00001049 if (!adcd.succeeded()) {
1050 return;
1051 }
bsalomon@google.com93c96602012-04-27 13:05:21 +00001052
jvanverth@google.comcc782382013-01-28 20:39:48 +00001053 GrVertexLayout layout = GrDrawState::kEdge_VertexLayoutBit;
jvanverth@google.comb75b0a02013-02-05 20:33:30 +00001054 drawState->setVertexLayout(layout);
1055 GrAssert(sizeof(CircleVertex) == drawState->getVertexSize());
bsalomon@google.com93c96602012-04-27 13:05:21 +00001056
jvanverth@google.comb75b0a02013-02-05 20:33:30 +00001057 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001058 if (!geo.succeeded()) {
1059 GrPrintf("Failed to get space for vertices!\n");
1060 return;
1061 }
1062
1063 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
1064
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001065 GrPoint center = GrPoint::Make(oval.centerX(), oval.centerY());
1066 vm.mapPoints(&center, 1);
1067
1068 SkScalar L;
1069 SkScalar R;
1070 SkScalar T;
1071 SkScalar B;
1072
1073 if (isCircle) {
1074 drawState->setVertexEdgeType(GrDrawState::kCircle_EdgeType);
1075
1076 xRadius = vm.mapRadius(xRadius);
1077
1078 SkScalar outerRadius = xRadius;
1079 SkScalar innerRadius = 0;
1080 SkScalar halfWidth = 0;
1081 if (style != SkStrokeRec::kFill_Style) {
1082 strokeWidth = vm.mapRadius(strokeWidth);
1083 if (SkScalarNearlyZero(strokeWidth)) {
1084 halfWidth = SK_ScalarHalf;
1085 } else {
1086 halfWidth = SkScalarHalf(strokeWidth);
1087 }
1088
1089 outerRadius += halfWidth;
1090 if (style == SkStrokeRec::kStroke_Style || style == SkStrokeRec::kHairline_Style) {
1091 innerRadius = SkMaxScalar(0, xRadius - halfWidth);
1092 }
1093 }
1094
1095 for (int i = 0; i < 4; ++i) {
1096 verts[i].fCenter = center;
1097 verts[i].fOuterRadius = outerRadius;
1098 verts[i].fInnerRadius = innerRadius;
1099 }
1100
1101 L = -outerRadius;
1102 R = +outerRadius;
1103 T = -outerRadius;
1104 B = +outerRadius;
1105 } else { // is axis-aligned ellipse
1106 drawState->setVertexEdgeType(GrDrawState::kEllipse_EdgeType);
1107
1108 SkRect xformedRect;
1109 vm.mapRect(&xformedRect, oval);
1110
1111 xRadius = SkScalarHalf(xformedRect.width());
1112 yRadius = SkScalarHalf(xformedRect.height());
1113
1114 if (style == SkStrokeRec::kStrokeAndFill_Style && strokeWidth > 0.0f) {
1115 SkScalar halfWidth = SkScalarHalf(strokeWidth);
1116 // do (potentially) anisotropic mapping
1117 SkVector scaledStroke;
1118 scaledStroke.set(halfWidth, halfWidth);
1119 vm.mapVectors(&scaledStroke, 1);
1120 // this is legit only if scale & translation (which should be the case at the moment)
1121 xRadius += scaledStroke.fX;
1122 yRadius += scaledStroke.fY;
1123 }
1124
1125 SkScalar ratio = SkScalarDiv(xRadius, yRadius);
skia.committer@gmail.com98ded842013-01-23 07:06:17 +00001126
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001127 for (int i = 0; i < 4; ++i) {
1128 verts[i].fCenter = center;
1129 verts[i].fOuterRadius = xRadius;
1130 verts[i].fInnerRadius = ratio;
1131 }
1132
1133 L = -xRadius;
1134 R = +xRadius;
1135 T = -yRadius;
1136 B = +yRadius;
1137 }
1138
robertphillips@google.coma0a66c12012-06-22 13:14:29 +00001139 // The fragment shader will extend the radius out half a pixel
1140 // to antialias. Expand the drawn rect here so all the pixels
1141 // will be captured.
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001142 L += center.fX - SK_ScalarHalf;
1143 R += center.fX + SK_ScalarHalf;
1144 T += center.fY - SK_ScalarHalf;
1145 B += center.fY + SK_ScalarHalf;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001146
1147 verts[0].fPos = SkPoint::Make(L, T);
1148 verts[1].fPos = SkPoint::Make(R, T);
1149 verts[2].fPos = SkPoint::Make(L, B);
1150 verts[3].fPos = SkPoint::Make(R, B);
1151
bsalomon@google.com47059542012-06-06 20:51:20 +00001152 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4);
bsalomon@google.com150d2842012-01-12 20:19:56 +00001153}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001154
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001155void GrContext::drawPath(const GrPaint& paint, const SkPath& path, const SkStrokeRec& stroke) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001156
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001157 if (path.isEmpty()) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001158 if (path.isInverseFillType()) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001159 this->drawPaint(paint);
1160 }
1161 return;
1162 }
1163
bsalomon@google.com93c96602012-04-27 13:05:21 +00001164 SkRect ovalRect;
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001165 bool isOval = path.isOval(&ovalRect);
1166
1167 if (isOval && !path.isInverseFillType() && this->canDrawOval(paint, ovalRect, stroke)) {
1168 this->drawOval(paint, ovalRect, stroke);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001169 return;
1170 }
1171
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001172 this->internalDrawPath(paint, path, stroke);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001173}
1174
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001175void GrContext::internalDrawPath(const GrPaint& paint, const SkPath& path, const SkStrokeRec& stroke) {
bsalomon@google.com93c96602012-04-27 13:05:21 +00001176
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001177 // Note that below we may sw-rasterize the path into a scratch texture.
1178 // Scratch textures can be recycled after they are returned to the texture
1179 // cache. This presents a potential hazard for buffered drawing. However,
1180 // the writePixels that uploads to the scratch will perform a flush so we're
1181 // OK.
bsalomon@google.com82b0ec62013-02-07 21:02:36 +00001182 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +00001183 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001184
bsalomon@google.comc7448ce2012-10-05 19:04:13 +00001185 bool prAA = paint.isAntiAlias() && !this->getRenderTarget()->isMultisampled();
bsalomon@google.com289533a2011-10-27 12:34:25 +00001186
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001187 // An Assumption here is that path renderer would use some form of tweaking
1188 // the src color (either the input alpha or in the frag shader) to implement
1189 // aa. If we have some future driver-mojo path AA that can do the right
1190 // thing WRT to the blend then we'll need some query on the PR.
1191 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001192#if GR_DEBUG
bsalomon@google.com979432b2011-11-05 21:38:22 +00001193 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001194#endif
bsalomon@google.com289533a2011-10-27 12:34:25 +00001195 prAA = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001196 }
bsalomon@google.com289533a2011-10-27 12:34:25 +00001197
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001198 GrPathRendererChain::DrawType type = prAA ? GrPathRendererChain::kColorAntiAlias_DrawType :
1199 GrPathRendererChain::kColor_DrawType;
1200
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001201 const SkPath* pathPtr = &path;
1202 SkPath tmpPath;
1203 SkStrokeRec strokeRec(stroke);
1204
1205 // Try a 1st time without stroking the path and without allowing the SW renderer
1206 GrPathRenderer* pr = this->getPathRenderer(*pathPtr, strokeRec, target, false, type);
1207
1208 if (NULL == pr) {
1209 if (!strokeRec.isHairlineStyle()) {
1210 // It didn't work the 1st time, so try again with the stroked path
1211 if (strokeRec.applyToPath(&tmpPath, *pathPtr)) {
1212 pathPtr = &tmpPath;
1213 strokeRec.setFillStyle();
1214 }
1215 }
1216 // This time, allow SW renderer
1217 pr = this->getPathRenderer(*pathPtr, strokeRec, target, true, type);
1218 }
1219
bsalomon@google.com30085192011-08-19 15:42:31 +00001220 if (NULL == pr) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001221#if GR_DEBUG
bsalomon@google.com30085192011-08-19 15:42:31 +00001222 GrPrintf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001223#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001224 return;
1225 }
1226
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001227 pr->drawPath(*pathPtr, strokeRec, target, prAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001228}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001229
bsalomon@google.com27847de2011-02-22 20:59:41 +00001230////////////////////////////////////////////////////////////////////////////////
1231
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001232void GrContext::flush(int flagsBitfield) {
1233 if (kDiscard_FlushBit & flagsBitfield) {
1234 fDrawBuffer->reset();
1235 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001236 this->flushDrawBuffer();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001237 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001238 if (kForceCurrentRenderTarget_FlushBit & flagsBitfield) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001239 fGpu->forceRenderTargetFlush();
1240 }
1241}
1242
bsalomon@google.com27847de2011-02-22 20:59:41 +00001243void GrContext::flushDrawBuffer() {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001244 if (NULL != fDrawBuffer && !fDrawBuffer->isFlushing()) {
1245 fDrawBuffer->flush();
junov@google.com53a55842011-06-08 22:55:10 +00001246 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001247}
1248
bsalomon@google.com9c680582013-02-06 18:17:50 +00001249bool GrContext::writeTexturePixels(GrTexture* texture,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001250 int left, int top, int width, int height,
1251 GrPixelConfig config, const void* buffer, size_t rowBytes,
1252 uint32_t flags) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001253 SK_TRACE_EVENT0("GrContext::writeTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001254 ASSERT_OWNED_RESOURCE(texture);
1255
bsalomon@google.com9c680582013-02-06 18:17:50 +00001256 if ((kUnpremul_PixelOpsFlag & flags) || !fGpu->canWriteTexturePixels(texture, config)) {
1257 if (NULL != texture->asRenderTarget()) {
1258 return this->writeRenderTargetPixels(texture->asRenderTarget(),
1259 left, top, width, height,
1260 config, buffer, rowBytes, flags);
1261 } else {
1262 return false;
1263 }
bsalomon@google.com0342a852012-08-20 19:22:38 +00001264 }
bsalomon@google.com9c680582013-02-06 18:17:50 +00001265
bsalomon@google.com6f379512011-11-16 20:36:03 +00001266 if (!(kDontFlush_PixelOpsFlag & flags)) {
1267 this->flush();
1268 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001269
bsalomon@google.com9c680582013-02-06 18:17:50 +00001270 return fGpu->writeTexturePixels(texture, left, top, width, height,
1271 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +00001272}
1273
bsalomon@google.com0342a852012-08-20 19:22:38 +00001274bool GrContext::readTexturePixels(GrTexture* texture,
1275 int left, int top, int width, int height,
1276 GrPixelConfig config, void* buffer, size_t rowBytes,
1277 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001278 SK_TRACE_EVENT0("GrContext::readTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001279 ASSERT_OWNED_RESOURCE(texture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001280
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001281 // TODO: code read pixels for textures that aren't also rendertargets
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001282 GrRenderTarget* target = texture->asRenderTarget();
1283 if (NULL != target) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00001284 return this->readRenderTargetPixels(target,
1285 left, top, width, height,
1286 config, buffer, rowBytes,
1287 flags);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001288 } else {
1289 return false;
1290 }
1291}
1292
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001293#include "SkConfig8888.h"
1294
1295namespace {
1296/**
1297 * Converts a GrPixelConfig to a SkCanvas::Config8888. Only byte-per-channel
1298 * formats are representable as Config8888 and so the function returns false
1299 * if the GrPixelConfig has no equivalent Config8888.
1300 */
1301bool grconfig_to_config8888(GrPixelConfig config,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001302 bool unpremul,
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001303 SkCanvas::Config8888* config8888) {
1304 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00001305 case kRGBA_8888_GrPixelConfig:
1306 if (unpremul) {
1307 *config8888 = SkCanvas::kRGBA_Unpremul_Config8888;
1308 } else {
1309 *config8888 = SkCanvas::kRGBA_Premul_Config8888;
1310 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001311 return true;
bsalomon@google.com0342a852012-08-20 19:22:38 +00001312 case kBGRA_8888_GrPixelConfig:
1313 if (unpremul) {
1314 *config8888 = SkCanvas::kBGRA_Unpremul_Config8888;
1315 } else {
1316 *config8888 = SkCanvas::kBGRA_Premul_Config8888;
1317 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001318 return true;
1319 default:
1320 return false;
1321 }
1322}
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001323
1324// It returns a configuration with where the byte position of the R & B components are swapped in
1325// relation to the input config. This should only be called with the result of
1326// grconfig_to_config8888 as it will fail for other configs.
1327SkCanvas::Config8888 swap_config8888_red_and_blue(SkCanvas::Config8888 config8888) {
1328 switch (config8888) {
1329 case SkCanvas::kBGRA_Premul_Config8888:
1330 return SkCanvas::kRGBA_Premul_Config8888;
1331 case SkCanvas::kBGRA_Unpremul_Config8888:
1332 return SkCanvas::kRGBA_Unpremul_Config8888;
1333 case SkCanvas::kRGBA_Premul_Config8888:
1334 return SkCanvas::kBGRA_Premul_Config8888;
1335 case SkCanvas::kRGBA_Unpremul_Config8888:
1336 return SkCanvas::kBGRA_Unpremul_Config8888;
1337 default:
1338 GrCrash("Unexpected input");
1339 return SkCanvas::kBGRA_Unpremul_Config8888;;
1340 }
1341}
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001342}
1343
bsalomon@google.com0342a852012-08-20 19:22:38 +00001344bool GrContext::readRenderTargetPixels(GrRenderTarget* target,
1345 int left, int top, int width, int height,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001346 GrPixelConfig dstConfig, void* buffer, size_t rowBytes,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001347 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001348 SK_TRACE_EVENT0("GrContext::readRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001349 ASSERT_OWNED_RESOURCE(target);
1350
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001351 if (NULL == target) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001352 target = fDrawState->getRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001353 if (NULL == target) {
1354 return false;
1355 }
1356 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001357
bsalomon@google.com6f379512011-11-16 20:36:03 +00001358 if (!(kDontFlush_PixelOpsFlag & flags)) {
1359 this->flush();
1360 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001361
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001362 // Determine which conversions have to be applied: flipY, swapRAnd, and/or unpremul.
bsalomon@google.com0342a852012-08-20 19:22:38 +00001363
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001364 // If fGpu->readPixels would incur a y-flip cost then we will read the pixels upside down. We'll
1365 // either do the flipY by drawing into a scratch with a matrix or on the cpu after the read.
1366 bool flipY = fGpu->readPixelsWillPayForYFlip(target, left, top,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001367 width, height, dstConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001368 rowBytes);
bsalomon@google.com9c680582013-02-06 18:17:50 +00001369 // We ignore the preferred config if it is different than our config unless it is an R/B swap.
1370 // In that case we'll perform an R and B swap while drawing to a scratch texture of the swapped
1371 // config. Then we will call readPixels on the scratch with the swapped config. The swaps during
1372 // the draw cancels out the fact that we call readPixels with a config that is R/B swapped from
1373 // dstConfig.
1374 GrPixelConfig readConfig = dstConfig;
1375 bool swapRAndB = false;
1376 if (GrPixelConfigSwapRAndB(dstConfig) == fGpu->preferredReadPixelsConfig(dstConfig)) {
1377 readConfig = GrPixelConfigSwapRAndB(readConfig);
1378 swapRAndB = true;
1379 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001380
bsalomon@google.com0342a852012-08-20 19:22:38 +00001381 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001382
bsalomon@google.com9c680582013-02-06 18:17:50 +00001383 if (unpremul && !GrPixelConfigIs8888(dstConfig)) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001384 // The unpremul flag is only allowed for these two configs.
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001385 return false;
1386 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001387
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001388 // If the src is a texture and we would have to do conversions after read pixels, we instead
1389 // do the conversions by drawing the src to a scratch texture. If we handle any of the
1390 // conversions in the draw we set the corresponding bool to false so that we don't reapply it
1391 // on the read back pixels.
1392 GrTexture* src = target->asTexture();
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001393 GrAutoScratchTexture ast;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001394 if (NULL != src && (swapRAndB || unpremul || flipY)) {
1395 // Make the scratch a render target because we don't have a robust readTexturePixels as of
1396 // yet. It calls this function.
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001397 GrTextureDesc desc;
1398 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1399 desc.fWidth = width;
1400 desc.fHeight = height;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001401 desc.fConfig = readConfig;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001402 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001403
bsalomon@google.com9c680582013-02-06 18:17:50 +00001404 // When a full read back is faster than a partial we could always make the scratch exactly
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001405 // match the passed rect. However, if we see many different size rectangles we will trash
1406 // our texture cache and pay the cost of creating and destroying many textures. So, we only
1407 // request an exact match when the caller is reading an entire RT.
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001408 ScratchTexMatch match = kApprox_ScratchTexMatch;
1409 if (0 == left &&
1410 0 == top &&
1411 target->width() == width &&
1412 target->height() == height &&
1413 fGpu->fullReadPixelsIsFasterThanPartial()) {
1414 match = kExact_ScratchTexMatch;
1415 }
1416 ast.set(this, desc, match);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001417 GrTexture* texture = ast.texture();
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001418 if (texture) {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001419 // compute a matrix to perform the draw
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001420 SkMatrix textureMatrix;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001421 textureMatrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001422 textureMatrix.postIDiv(src->width(), src->height());
1423
bsalomon@google.comadc65362013-01-28 14:26:09 +00001424 SkAutoTUnref<const GrEffectRef> effect;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001425 if (unpremul) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001426 effect.reset(this->createPMToUPMEffect(src, swapRAndB, textureMatrix));
1427 if (NULL != effect) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001428 unpremul = false; // we no longer need to do this on CPU after the read back.
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001429 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001430 }
1431 // If we failed to create a PM->UPM effect and have no other conversions to perform then
1432 // there is no longer any point to using the scratch.
bsalomon@google.comadc65362013-01-28 14:26:09 +00001433 if (NULL != effect || flipY || swapRAndB) {
1434 if (!effect) {
1435 effect.reset(GrConfigConversionEffect::Create(
1436 src,
1437 swapRAndB,
1438 GrConfigConversionEffect::kNone_PMConversion,
1439 textureMatrix));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001440 }
1441 swapRAndB = false; // we will handle the swap in the draw.
bsalomon@google.comc4364992011-11-07 15:54:49 +00001442
robertphillips@google.com94ba6c62013-02-27 16:43:49 +00001443 // We protect the existing geometry here since it may not be
1444 // clear to the caller that a draw operation (i.e., drawSimpleRect)
1445 // can be invoked in this method
1446 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001447 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.comadc65362013-01-28 14:26:09 +00001448 GrAssert(effect);
bsalomon@google.com9e040ae2013-01-28 14:31:19 +00001449 drawState->setEffect(0, effect);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001450
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001451 drawState->setRenderTarget(texture->asRenderTarget());
bsalomon@google.com81712882012-11-01 17:12:34 +00001452 GrRect rect = GrRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001453 fGpu->drawSimpleRect(rect, NULL);
1454 // we want to read back from the scratch's origin
1455 left = 0;
1456 top = 0;
1457 target = texture->asRenderTarget();
1458 }
bsalomon@google.com0342a852012-08-20 19:22:38 +00001459 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001460 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001461 if (!fGpu->readPixels(target,
1462 left, top, width, height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001463 readConfig, buffer, rowBytes)) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001464 return false;
1465 }
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001466 // Perform any conversions we weren't able to perform using a scratch texture.
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001467 if (unpremul || swapRAndB) {
bsalomon@google.comccaa0022012-09-25 19:55:07 +00001468 // These are initialized to suppress a warning
1469 SkCanvas::Config8888 srcC8888 = SkCanvas::kNative_Premul_Config8888;
1470 SkCanvas::Config8888 dstC8888 = SkCanvas::kNative_Premul_Config8888;
1471
bsalomon@google.com9c680582013-02-06 18:17:50 +00001472 SkDEBUGCODE(bool c8888IsValid =) grconfig_to_config8888(dstConfig, false, &srcC8888);
1473 grconfig_to_config8888(dstConfig, unpremul, &dstC8888);
bsalomon@google.comccaa0022012-09-25 19:55:07 +00001474
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001475 if (swapRAndB) {
1476 GrAssert(c8888IsValid); // we should only do r/b swap on 8888 configs
1477 srcC8888 = swap_config8888_red_and_blue(srcC8888);
1478 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001479 GrAssert(c8888IsValid);
1480 uint32_t* b32 = reinterpret_cast<uint32_t*>(buffer);
1481 SkConvertConfig8888Pixels(b32, rowBytes, dstC8888,
1482 b32, rowBytes, srcC8888,
1483 width, height);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001484 }
1485 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001486}
1487
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001488void GrContext::resolveRenderTarget(GrRenderTarget* target) {
1489 GrAssert(target);
1490 ASSERT_OWNED_RESOURCE(target);
1491 // In the future we may track whether there are any pending draws to this
1492 // target. We don't today so we always perform a flush. We don't promise
1493 // this to our clients, though.
1494 this->flush();
1495 fGpu->resolveRenderTarget(target);
1496}
1497
scroggo@google.coma2a31922012-12-07 19:14:45 +00001498void GrContext::copyTexture(GrTexture* src, GrRenderTarget* dst, const SkIPoint* topLeft) {
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001499 if (NULL == src || NULL == dst) {
1500 return;
1501 }
1502 ASSERT_OWNED_RESOURCE(src);
1503
twiz@google.com1ac87ff2012-04-27 19:39:33 +00001504 // Writes pending to the source texture are not tracked, so a flush
1505 // is required to ensure that the copy captures the most recent contents
bsalomon@google.comadc65362013-01-28 14:26:09 +00001506 // of the source texture. See similar behavior in
twiz@google.com1ac87ff2012-04-27 19:39:33 +00001507 // GrContext::resolveRenderTarget.
1508 this->flush();
1509
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001510 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001511 GrDrawState* drawState = fGpu->drawState();
1512 drawState->setRenderTarget(dst);
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001513 SkMatrix sampleM;
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001514 sampleM.setIDiv(src->width(), src->height());
scroggo@google.coma2a31922012-12-07 19:14:45 +00001515 SkIRect srcRect = SkIRect::MakeWH(dst->width(), dst->height());
1516 if (NULL != topLeft) {
1517 srcRect.offset(*topLeft);
1518 }
1519 SkIRect srcBounds = SkIRect::MakeWH(src->width(), src->height());
1520 if (!srcRect.intersect(srcBounds)) {
1521 return;
1522 }
1523 sampleM.preTranslate(SkIntToScalar(srcRect.fLeft), SkIntToScalar(srcRect.fTop));
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +00001524 drawState->createTextureEffect(0, src, sampleM);
scroggo@google.coma2a31922012-12-07 19:14:45 +00001525 SkRect dstR = SkRect::MakeWH(SkIntToScalar(srcRect.width()), SkIntToScalar(srcRect.height()));
1526 fGpu->drawSimpleRect(dstR, NULL);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001527}
1528
bsalomon@google.com9c680582013-02-06 18:17:50 +00001529bool GrContext::writeRenderTargetPixels(GrRenderTarget* target,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001530 int left, int top, int width, int height,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001531 GrPixelConfig srcConfig,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001532 const void* buffer,
1533 size_t rowBytes,
1534 uint32_t flags) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001535 SK_TRACE_EVENT0("GrContext::writeRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001536 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com6f379512011-11-16 20:36:03 +00001537
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001538 if (NULL == target) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001539 target = fDrawState->getRenderTarget();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001540 if (NULL == target) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001541 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +00001542 }
1543 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001544
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001545 // TODO: when underlying api has a direct way to do this we should use it (e.g. glDrawPixels on
1546 // desktop GL).
1547
1548 // We will always call some form of writeTexturePixels and we will pass our flags on to it.
1549 // Thus, we don't perform a flush here since that call will do it (if the kNoFlush flag isn't
1550 // set.)
bsalomon@google.com27847de2011-02-22 20:59:41 +00001551
bsalomon@google.com0342a852012-08-20 19:22:38 +00001552 // If the RT is also a texture and we don't have to premultiply then take the texture path.
1553 // We expect to be at least as fast or faster since it doesn't use an intermediate texture as
1554 // we do below.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001555
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001556#if !GR_MAC_BUILD
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001557 // At least some drivers on the Mac get confused when glTexImage2D is called on a texture
1558 // attached to an FBO. The FBO still sees the old image. TODO: determine what OS versions and/or
1559 // HW is affected.
bsalomon@google.com9c680582013-02-06 18:17:50 +00001560 if (NULL != target->asTexture() && !(kUnpremul_PixelOpsFlag & flags) &&
1561 fGpu->canWriteTexturePixels(target->asTexture(), srcConfig)) {
1562 return this->writeTexturePixels(target->asTexture(),
1563 left, top, width, height,
1564 srcConfig, buffer, rowBytes, flags);
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001565 }
1566#endif
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001567
bsalomon@google.com9c680582013-02-06 18:17:50 +00001568 // We ignore the preferred config unless it is a R/B swap of the src config. In that case
1569 // we will upload the original src data to a scratch texture but we will spoof it as the swapped
1570 // config. This scratch will then have R and B swapped. We correct for this by swapping again
1571 // when drawing the scratch to the dst using a conversion effect.
1572 bool swapRAndB = false;
1573 GrPixelConfig writeConfig = srcConfig;
1574 if (fGpu->preferredWritePixelsConfig(srcConfig) == GrPixelConfigSwapRAndB(srcConfig)) {
1575 writeConfig = GrPixelConfigSwapRAndB(srcConfig);
1576 swapRAndB = true;
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001577 }
1578
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001579 GrTextureDesc desc;
1580 desc.fWidth = width;
1581 desc.fHeight = height;
bsalomon@google.com9c680582013-02-06 18:17:50 +00001582 desc.fConfig = writeConfig;
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001583 GrAutoScratchTexture ast(this, desc);
1584 GrTexture* texture = ast.texture();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001585 if (NULL == texture) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001586 return false;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001587 }
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001588
bsalomon@google.comadc65362013-01-28 14:26:09 +00001589 SkAutoTUnref<const GrEffectRef> effect;
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001590 SkMatrix textureMatrix;
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001591 textureMatrix.setIDiv(texture->width(), texture->height());
1592
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001593 // allocate a tmp buffer and sw convert the pixels to premul
1594 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
1595
1596 if (kUnpremul_PixelOpsFlag & flags) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001597 if (!GrPixelConfigIs8888(srcConfig)) {
1598 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001599 }
bsalomon@google.comadc65362013-01-28 14:26:09 +00001600 effect.reset(this->createUPMToPMEffect(texture, swapRAndB, textureMatrix));
bsalomon@google.com9c680582013-02-06 18:17:50 +00001601 // handle the unpremul step on the CPU if we couldn't create an effect to do it.
bsalomon@google.comadc65362013-01-28 14:26:09 +00001602 if (NULL == effect) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001603 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1604 GR_DEBUGCODE(bool success = )
bsalomon@google.com9c680582013-02-06 18:17:50 +00001605 grconfig_to_config8888(srcConfig, true, &srcConfig8888);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001606 GrAssert(success);
1607 GR_DEBUGCODE(success = )
bsalomon@google.com9c680582013-02-06 18:17:50 +00001608 grconfig_to_config8888(srcConfig, false, &dstConfig8888);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001609 GrAssert(success);
1610 const uint32_t* src = reinterpret_cast<const uint32_t*>(buffer);
1611 tmpPixels.reset(width * height);
1612 SkConvertConfig8888Pixels(tmpPixels.get(), 4 * width, dstConfig8888,
1613 src, rowBytes, srcConfig8888,
1614 width, height);
1615 buffer = tmpPixels.get();
1616 rowBytes = 4 * width;
1617 }
1618 }
bsalomon@google.comadc65362013-01-28 14:26:09 +00001619 if (NULL == effect) {
1620 effect.reset(GrConfigConversionEffect::Create(texture,
1621 swapRAndB,
1622 GrConfigConversionEffect::kNone_PMConversion,
1623 textureMatrix));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001624 }
1625
bsalomon@google.com9c680582013-02-06 18:17:50 +00001626 if (!this->writeTexturePixels(texture,
1627 0, 0, width, height,
1628 writeConfig, buffer, rowBytes,
1629 flags & ~kUnpremul_PixelOpsFlag)) {
1630 return false;
1631 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001632
robertphillips@google.com94ba6c62013-02-27 16:43:49 +00001633 // writeRenderTargetPixels can be called in the midst of drawing another
1634 // object (e.g., when uploading a SW path rendering to the gpu while
1635 // drawing a rect) so preserve the current geometry.
1636 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001637 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.comadc65362013-01-28 14:26:09 +00001638 GrAssert(effect);
bsalomon@google.com9e040ae2013-01-28 14:31:19 +00001639 drawState->setEffect(0, effect);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001640
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001641 SkMatrix matrix;
bsalomon@google.com81712882012-11-01 17:12:34 +00001642 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001643 drawState->setViewMatrix(matrix);
1644 drawState->setRenderTarget(target);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001645
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001646 fGpu->drawSimpleRect(GrRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)), NULL);
bsalomon@google.com9c680582013-02-06 18:17:50 +00001647 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001648}
1649////////////////////////////////////////////////////////////////////////////////
1650
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001651GrDrawTarget* GrContext::prepareToDraw(const GrPaint* paint, BufferedDraw buffered) {
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001652 if (kNo_BufferedDraw == buffered && kYes_BufferedDraw == fLastDrawWasBuffered) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001653 this->flushDrawBuffer();
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001654 fLastDrawWasBuffered = kNo_BufferedDraw;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001655 }
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001656 if (NULL != paint) {
bsalomon@google.comaf84e742012-10-05 13:23:24 +00001657 GrAssert(fDrawState->stagesDisabled());
1658 fDrawState->setFromPaint(*paint);
1659#if GR_DEBUG_PARTIAL_COVERAGE_CHECK
1660 if ((paint->hasMask() || 0xff != paint->fCoverage) &&
1661 !fGpu->canApplyCoverage()) {
1662 GrPrintf("Partial pixel coverage will be incorrectly blended.\n");
1663 }
1664#endif
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001665 }
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001666 if (kYes_BufferedDraw == buffered) {
1667 fDrawBuffer->setClip(fGpu->getClip());
1668 fLastDrawWasBuffered = kYes_BufferedDraw;
1669 return fDrawBuffer;
1670 } else {
1671 GrAssert(kNo_BufferedDraw == buffered);
1672 return fGpu;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001673 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001674}
1675
robertphillips@google.com72176b22012-05-23 13:19:12 +00001676/*
1677 * This method finds a path renderer that can draw the specified path on
1678 * the provided target.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001679 * Due to its expense, the software path renderer has split out so it can
robertphillips@google.com72176b22012-05-23 13:19:12 +00001680 * can be individually allowed/disallowed via the "allowSW" boolean.
1681 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001682GrPathRenderer* GrContext::getPathRenderer(const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001683 const SkStrokeRec& stroke,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001684 const GrDrawTarget* target,
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001685 bool allowSW,
1686 GrPathRendererChain::DrawType drawType,
1687 GrPathRendererChain::StencilSupport* stencilSupport) {
1688
bsalomon@google.com30085192011-08-19 15:42:31 +00001689 if (NULL == fPathRendererChain) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001690 fPathRendererChain = SkNEW_ARGS(GrPathRendererChain, (this));
bsalomon@google.com30085192011-08-19 15:42:31 +00001691 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001692
sugoi@google.com12b4e272012-12-06 20:13:11 +00001693 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(path,
1694 stroke,
robertphillips@google.com72176b22012-05-23 13:19:12 +00001695 target,
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001696 drawType,
1697 stencilSupport);
robertphillips@google.com72176b22012-05-23 13:19:12 +00001698
1699 if (NULL == pr && allowSW) {
1700 if (NULL == fSoftwarePathRenderer) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001701 fSoftwarePathRenderer = SkNEW_ARGS(GrSoftwarePathRenderer, (this));
robertphillips@google.com72176b22012-05-23 13:19:12 +00001702 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001703 pr = fSoftwarePathRenderer;
1704 }
1705
1706 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001707}
1708
bsalomon@google.com27847de2011-02-22 20:59:41 +00001709////////////////////////////////////////////////////////////////////////////////
1710
bsalomon@google.com27847de2011-02-22 20:59:41 +00001711void GrContext::setRenderTarget(GrRenderTarget* target) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001712 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001713 fDrawState->setRenderTarget(target);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001714}
1715
1716GrRenderTarget* GrContext::getRenderTarget() {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001717 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001718}
1719
1720const GrRenderTarget* GrContext::getRenderTarget() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001721 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001722}
1723
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001724bool GrContext::isConfigRenderable(GrPixelConfig config) const {
1725 return fGpu->isConfigRenderable(config);
1726}
1727
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001728const SkMatrix& GrContext::getMatrix() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001729 return fDrawState->getViewMatrix();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001730}
1731
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001732void GrContext::setMatrix(const SkMatrix& m) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001733 fDrawState->setViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001734}
1735
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +00001736void GrContext::setIdentityMatrix() {
1737 fDrawState->viewMatrix()->reset();
1738}
1739
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001740void GrContext::concatMatrix(const SkMatrix& m) const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001741 fDrawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001742}
1743
1744static inline intptr_t setOrClear(intptr_t bits, int shift, intptr_t pred) {
1745 intptr_t mask = 1 << shift;
1746 if (pred) {
1747 bits |= mask;
1748 } else {
1749 bits &= ~mask;
1750 }
1751 return bits;
1752}
1753
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001754void GrContext::setupDrawBuffer() {
1755
1756 GrAssert(NULL == fDrawBuffer);
1757 GrAssert(NULL == fDrawBufferVBAllocPool);
1758 GrAssert(NULL == fDrawBufferIBAllocPool);
1759
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001760 fDrawBufferVBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001761 SkNEW_ARGS(GrVertexBufferAllocPool, (fGpu, false,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001762 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001763 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS));
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001764 fDrawBufferIBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001765 SkNEW_ARGS(GrIndexBufferAllocPool, (fGpu, false,
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001766 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001767 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001768
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001769 fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (fGpu,
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001770 fDrawBufferVBAllocPool,
1771 fDrawBufferIBAllocPool));
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001772
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001773 fDrawBuffer->setDrawState(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001774}
1775
bsalomon@google.com27847de2011-02-22 20:59:41 +00001776GrDrawTarget* GrContext::getTextTarget(const GrPaint& paint) {
bsalomon@google.com82b0ec62013-02-07 21:02:36 +00001777 return this->prepareToDraw(&paint, BUFFERED_DRAW);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001778}
1779
1780const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {
1781 return fGpu->getQuadIndexBuffer();
1782}
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001783
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001784namespace {
1785void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
1786 GrConfigConversionEffect::PMConversion pmToUPM;
1787 GrConfigConversionEffect::PMConversion upmToPM;
1788 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM);
1789 *pmToUPMValue = pmToUPM;
1790 *upmToPMValue = upmToPM;
1791}
1792}
1793
bsalomon@google.comadc65362013-01-28 14:26:09 +00001794const GrEffectRef* GrContext::createPMToUPMEffect(GrTexture* texture,
1795 bool swapRAndB,
1796 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001797 if (!fDidTestPMConversions) {
1798 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001799 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001800 }
1801 GrConfigConversionEffect::PMConversion pmToUPM =
1802 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
1803 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001804 return GrConfigConversionEffect::Create(texture, swapRAndB, pmToUPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001805 } else {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001806 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001807 }
1808}
1809
bsalomon@google.comadc65362013-01-28 14:26:09 +00001810const GrEffectRef* GrContext::createUPMToPMEffect(GrTexture* texture,
1811 bool swapRAndB,
1812 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001813 if (!fDidTestPMConversions) {
1814 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001815 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001816 }
1817 GrConfigConversionEffect::PMConversion upmToPM =
1818 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion);
1819 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001820 return GrConfigConversionEffect::Create(texture, swapRAndB, upmToPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001821 } else {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001822 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001823 }
1824}
1825
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001826GrTexture* GrContext::gaussianBlur(GrTexture* srcTexture,
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001827 bool canClobberSrc,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001828 const SkRect& rect,
1829 float sigmaX, float sigmaY) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001830 ASSERT_OWNED_RESOURCE(srcTexture);
robertphillips@google.comccb39502012-10-01 18:25:13 +00001831
1832 AutoRenderTarget art(this);
1833
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001834 AutoMatrix am;
1835 am.setIdentity(this);
1836
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001837 SkIRect clearRect;
bsalomon@google.comb505a122012-05-31 18:40:36 +00001838 int scaleFactorX, radiusX;
1839 int scaleFactorY, radiusY;
1840 sigmaX = adjust_sigma(sigmaX, &scaleFactorX, &radiusX);
1841 sigmaY = adjust_sigma(sigmaY, &scaleFactorY, &radiusY);
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001842
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001843 SkRect srcRect(rect);
1844 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
1845 srcRect.roundOut();
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001846 scale_rect(&srcRect, static_cast<float>(scaleFactorX),
robertphillips@google.com8637a362012-04-10 18:32:35 +00001847 static_cast<float>(scaleFactorY));
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +00001848
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001849 AutoClip acs(this, srcRect);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001850
bsalomon@google.com0342a852012-08-20 19:22:38 +00001851 GrAssert(kBGRA_8888_GrPixelConfig == srcTexture->config() ||
1852 kRGBA_8888_GrPixelConfig == srcTexture->config() ||
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001853 kAlpha_8_GrPixelConfig == srcTexture->config());
1854
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001855 GrTextureDesc desc;
1856 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
1857 desc.fWidth = SkScalarFloorToInt(srcRect.width());
1858 desc.fHeight = SkScalarFloorToInt(srcRect.height());
1859 desc.fConfig = srcTexture->config();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001860
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001861 GrAutoScratchTexture temp1, temp2;
1862 GrTexture* dstTexture = temp1.set(this, desc);
1863 GrTexture* tempTexture = canClobberSrc ? srcTexture : temp2.set(this, desc);
robertphillips@google.com7d126752012-10-19 12:56:26 +00001864 if (NULL == dstTexture || NULL == tempTexture) {
1865 return NULL;
1866 }
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001867
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001868 GrPaint paint;
1869 paint.reset();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001870
1871 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001872 SkMatrix matrix;
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +00001873 matrix.setIDiv(srcTexture->width(), srcTexture->height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001874 this->setRenderTarget(dstTexture->asRenderTarget());
1875 SkRect dstRect(srcRect);
1876 scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +00001877 i < scaleFactorY ? 0.5f : 1.0f);
1878
bsalomon@google.com68b58c92013-01-17 16:50:08 +00001879 paint.colorStage(0)->setEffect(GrSimpleTextureEffect::Create(srcTexture,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +00001880 matrix,
1881 true))->unref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001882 this->drawRectToRect(paint, dstRect, srcRect);
1883 srcRect = dstRect;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001884 srcTexture = dstTexture;
1885 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001886 }
1887
robertphillips@google.com7a396332012-05-10 15:11:27 +00001888 SkIRect srcIRect;
1889 srcRect.roundOut(&srcIRect);
1890
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001891 if (sigmaX > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001892 if (scaleFactorX > 1) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001893 // Clear out a radius to the right of the srcRect to prevent the
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001894 // X convolution from reading garbage.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001895 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001896 radiusX, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001897 this->clear(&clearRect, 0x0);
1898 }
1899
1900 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.com82b0ec62013-02-07 21:02:36 +00001901 GrDrawTarget* target = this->prepareToDraw(NULL, BUFFERED_DRAW);
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001902 convolve_gaussian(target, srcTexture, srcRect, sigmaX, radiusX,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001903 Gr1DKernelEffect::kX_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001904 srcTexture = dstTexture;
1905 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001906 }
1907
1908 if (sigmaY > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001909 if (scaleFactorY > 1 || sigmaX > 0.0f) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001910 // Clear out a radius below the srcRect to prevent the Y
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001911 // convolution from reading garbage.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001912 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001913 srcIRect.width(), radiusY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001914 this->clear(&clearRect, 0x0);
1915 }
1916
1917 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.com82b0ec62013-02-07 21:02:36 +00001918 GrDrawTarget* target = this->prepareToDraw(NULL, BUFFERED_DRAW);
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001919 convolve_gaussian(target, srcTexture, srcRect, sigmaY, radiusY,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001920 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001921 srcTexture = dstTexture;
1922 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001923 }
1924
1925 if (scaleFactorX > 1 || scaleFactorY > 1) {
1926 // Clear one pixel to the right and below, to accommodate bilinear
1927 // upsampling.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001928 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
robertphillips@google.com7a396332012-05-10 15:11:27 +00001929 srcIRect.width() + 1, 1);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001930 this->clear(&clearRect, 0x0);
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001931 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
robertphillips@google.com7a396332012-05-10 15:11:27 +00001932 1, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001933 this->clear(&clearRect, 0x0);
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001934 SkMatrix matrix;
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001935 // FIXME: This should be mitchell, not bilinear.
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +00001936 matrix.setIDiv(srcTexture->width(), srcTexture->height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001937 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.com68b58c92013-01-17 16:50:08 +00001938 paint.colorStage(0)->setEffect(GrSimpleTextureEffect::Create(srcTexture,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +00001939 matrix,
1940 true))->unref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001941 SkRect dstRect(srcRect);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001942 scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001943 this->drawRectToRect(paint, dstRect, srcRect);
1944 srcRect = dstRect;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001945 srcTexture = dstTexture;
1946 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001947 }
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001948 if (srcTexture == temp1.texture()) {
1949 return temp1.detach();
1950 } else if (srcTexture == temp2.texture()) {
1951 return temp2.detach();
1952 } else {
1953 srcTexture->ref();
1954 return srcTexture;
1955 }
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001956}
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001957
bsalomon@google.comc4364992011-11-07 15:54:49 +00001958///////////////////////////////////////////////////////////////////////////////
robertphillips@google.com59552022012-08-31 13:07:37 +00001959#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001960void GrContext::printCacheStats() const {
1961 fTextureCache->printStats();
1962}
1963#endif