blob: b719ec5a53719d9d5ce98c63dc99f711cfd42bcd [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"
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +000015#include "effects/GrEllipseEdgeEffect.h"
bsalomon@google.comb505a122012-05-31 18:40:36 +000016
tomhudson@google.com278cbb42011-06-30 19:37:01 +000017#include "GrBufferAllocPool.h"
bsalomon@google.com05ef5102011-05-02 21:14:59 +000018#include "GrGpu.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000019#include "GrIndexBuffer.h"
20#include "GrInOrderDrawBuffer.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000021#include "GrPathRenderer.h"
tomhudson@google.comd22b6e42011-06-24 15:53:40 +000022#include "GrPathUtils.h"
bsalomon@google.com50398bf2011-07-26 20:45:30 +000023#include "GrResourceCache.h"
robertphillips@google.com72176b22012-05-23 13:19:12 +000024#include "GrSoftwarePathRenderer.h"
bsalomon@google.com558a75b2011-08-08 17:01:14 +000025#include "GrStencilBuffer.h"
tomhudson@google.com278cbb42011-06-30 19:37:01 +000026#include "GrTextStrike.h"
bsalomon@google.com82b0ec62013-02-07 21:02:36 +000027#include "SkRTConf.h"
sugoi@google.com5f74cf82012-12-17 21:16:45 +000028#include "SkStrokeRec.h"
bsalomon@google.com8c2fe992011-09-13 15:27:18 +000029#include "SkTLazy.h"
bsalomon@google.comc0af3172012-06-15 14:10:09 +000030#include "SkTLS.h"
tomhudson@google.com0c8d93a2011-07-01 17:08:26 +000031#include "SkTrace.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000032
reed@google.comfa35e3d2012-06-26 20:16:17 +000033SK_DEFINE_INST_COUNT(GrContext)
34SK_DEFINE_INST_COUNT(GrDrawState)
35
bsalomon@google.com82b0ec62013-02-07 21:02:36 +000036// 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 +000037// InOrderDrawBuffer, to compare performance of using/not using InOrderDrawBuffer, or to make
bsalomon@google.com82b0ec62013-02-07 21:02:36 +000038// debugging simpler.
39SK_CONF_DECLARE(bool, c_Defer, "gpu.deferContext", true, "Defers rendering in GrContext via GrInOrderDrawBuffer.");
40
41#define BUFFERED_DRAW (c_Defer ? kYes_BufferedDraw : kNo_BufferedDraw)
bsalomon@google.com27847de2011-02-22 20:59:41 +000042
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +000043#define MAX_BLUR_SIGMA 4.0f
44
bsalomon@google.comd46e2422011-09-23 17:40:07 +000045// When we're using coverage AA but the blend is incompatible (given gpu
46// limitations) should we disable AA or draw wrong?
bsalomon@google.com950d7a82011-09-28 15:05:33 +000047#define DISABLE_COVERAGE_AA_FOR_BLEND 1
bsalomon@google.comd46e2422011-09-23 17:40:07 +000048
reed@google.com4b2d3f32012-05-15 18:05:50 +000049#if GR_DEBUG
50 // change this to a 1 to see notifications when partial coverage fails
51 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
52#else
53 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
54#endif
55
robertphillips@google.com1947ba62012-10-17 13:35:24 +000056static const size_t MAX_TEXTURE_CACHE_COUNT = 2048;
djsollen@google.com42041e62012-10-29 19:24:45 +000057static const size_t MAX_TEXTURE_CACHE_BYTES = GR_DEFAULT_TEXTURE_CACHE_MB_LIMIT * 1024 * 1024;
bsalomon@google.com27847de2011-02-22 20:59:41 +000058
bsalomon@google.com60361492012-03-15 17:47:06 +000059static const size_t DRAW_BUFFER_VBPOOL_BUFFER_SIZE = 1 << 15;
bsalomon@google.com27847de2011-02-22 20:59:41 +000060static const int DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS = 4;
61
bsalomon@google.com1d4edd32012-08-16 18:36:06 +000062static const size_t DRAW_BUFFER_IBPOOL_BUFFER_SIZE = 1 << 11;
63static const int DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS = 4;
bsalomon@google.com27847de2011-02-22 20:59:41 +000064
bsalomon@google.combc4b6542011-11-19 13:56:11 +000065#define ASSERT_OWNED_RESOURCE(R) GrAssert(!(R) || (R)->getContext() == this)
66
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000067GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext) {
68 GrContext* context = SkNEW(GrContext);
69 if (context->init(backend, backendContext)) {
70 return context;
71 } else {
72 context->unref();
73 return NULL;
bsalomon@google.com27847de2011-02-22 20:59:41 +000074 }
bsalomon@google.com27847de2011-02-22 20:59:41 +000075}
76
bsalomon@google.comc0af3172012-06-15 14:10:09 +000077namespace {
78void* CreateThreadInstanceCount() {
tomhudson@google.comc377baf2012-07-09 20:17:56 +000079 return SkNEW_ARGS(int, (0));
bsalomon@google.comc0af3172012-06-15 14:10:09 +000080}
81void DeleteThreadInstanceCount(void* v) {
82 delete reinterpret_cast<int*>(v);
83}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000084#define THREAD_INSTANCE_COUNT \
85 (*reinterpret_cast<int*>(SkTLS::Get(CreateThreadInstanceCount, DeleteThreadInstanceCount)))
86}
bsalomon@google.comc0af3172012-06-15 14:10:09 +000087
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000088GrContext::GrContext() {
89 ++THREAD_INSTANCE_COUNT;
90 fDrawState = NULL;
91 fGpu = NULL;
92 fPathRendererChain = NULL;
93 fSoftwarePathRenderer = NULL;
94 fTextureCache = NULL;
95 fFontCache = NULL;
96 fDrawBuffer = NULL;
97 fDrawBufferVBAllocPool = NULL;
98 fDrawBufferIBAllocPool = NULL;
99 fAARectRenderer = NULL;
100}
101
102bool GrContext::init(GrBackend backend, GrBackendContext backendContext) {
103 GrAssert(NULL == fGpu);
104
105 fGpu = GrGpu::Create(backend, backendContext, this);
106 if (NULL == fGpu) {
107 return false;
108 }
109
110 fDrawState = SkNEW(GrDrawState);
111 fGpu->setDrawState(fDrawState);
112
113
114 fTextureCache = SkNEW_ARGS(GrResourceCache,
115 (MAX_TEXTURE_CACHE_COUNT,
116 MAX_TEXTURE_CACHE_BYTES));
117 fFontCache = SkNEW_ARGS(GrFontCache, (fGpu));
118
119 fLastDrawWasBuffered = kNo_BufferedDraw;
120
121 fAARectRenderer = SkNEW(GrAARectRenderer);
122
123 fDidTestPMConversions = false;
124
125 this->setupDrawBuffer();
126
127 return true;
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000128}
129
130int GrContext::GetThreadInstanceCount() {
131 return THREAD_INSTANCE_COUNT;
132}
133
bsalomon@google.com27847de2011-02-22 20:59:41 +0000134GrContext::~GrContext() {
robertphillips@google.comcdb426d2012-09-24 19:33:59 +0000135 for (int i = 0; i < fCleanUpData.count(); ++i) {
136 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo);
137 }
138
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000139 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000140
141 // Since the gpu can hold scratch textures, give it a chance to let go
142 // of them before freeing the texture cache
143 fGpu->purgeResources();
144
bsalomon@google.com27847de2011-02-22 20:59:41 +0000145 delete fTextureCache;
robertphillips@google.comeb9b3e12012-09-11 12:50:53 +0000146 fTextureCache = NULL;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000147 delete fFontCache;
148 delete fDrawBuffer;
149 delete fDrawBufferVBAllocPool;
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000150 delete fDrawBufferIBAllocPool;
bsalomon@google.com30085192011-08-19 15:42:31 +0000151
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000152 fAARectRenderer->unref();
153
bsalomon@google.com205d4602011-04-25 12:43:45 +0000154 fGpu->unref();
bsalomon@google.com30085192011-08-19 15:42:31 +0000155 GrSafeUnref(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000156 GrSafeUnref(fSoftwarePathRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000157 fDrawState->unref();
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000158
159 --THREAD_INSTANCE_COUNT;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000160}
161
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000162void GrContext::contextLost() {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000163 this->contextDestroyed();
junov@google.com53a55842011-06-08 22:55:10 +0000164 this->setupDrawBuffer();
165}
166
167void GrContext::contextDestroyed() {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000168 // abandon first to so destructors
169 // don't try to free the resources in the API.
170 fGpu->abandonResources();
171
bsalomon@google.com30085192011-08-19 15:42:31 +0000172 // a path renderer may be holding onto resources that
173 // are now unusable
174 GrSafeSetNull(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000175 GrSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com30085192011-08-19 15:42:31 +0000176
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000177 delete fDrawBuffer;
178 fDrawBuffer = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000179
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000180 delete fDrawBufferVBAllocPool;
181 fDrawBufferVBAllocPool = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000182
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000183 delete fDrawBufferIBAllocPool;
184 fDrawBufferIBAllocPool = NULL;
185
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000186 fAARectRenderer->reset();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000187
bsalomon@google.coma2921122012-08-28 12:34:17 +0000188 fTextureCache->purgeAllUnlocked();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000189 fFontCache->freeAll();
190 fGpu->markContextDirty();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000191}
192
193void GrContext::resetContext() {
194 fGpu->markContextDirty();
195}
196
197void GrContext::freeGpuResources() {
198 this->flush();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000199
robertphillips@google.comff175842012-05-14 19:31:39 +0000200 fGpu->purgeResources();
201
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000202 fAARectRenderer->reset();
203
bsalomon@google.coma2921122012-08-28 12:34:17 +0000204 fTextureCache->purgeAllUnlocked();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000205 fFontCache->freeAll();
bsalomon@google.com30085192011-08-19 15:42:31 +0000206 // a path renderer may be holding onto resources
207 GrSafeSetNull(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000208 GrSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000209}
210
twiz@google.com05e70242012-01-27 19:12:00 +0000211size_t GrContext::getGpuTextureCacheBytes() const {
212 return fTextureCache->getCachedResourceBytes();
213}
214
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000215////////////////////////////////////////////////////////////////////////////////
216
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000217namespace {
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000218
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000219void scale_rect(SkRect* rect, float xScale, float yScale) {
robertphillips@google.com5af56062012-04-27 15:39:52 +0000220 rect->fLeft = SkScalarMul(rect->fLeft, SkFloatToScalar(xScale));
221 rect->fTop = SkScalarMul(rect->fTop, SkFloatToScalar(yScale));
222 rect->fRight = SkScalarMul(rect->fRight, SkFloatToScalar(xScale));
223 rect->fBottom = SkScalarMul(rect->fBottom, SkFloatToScalar(yScale));
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000224}
225
bsalomon@google.comb505a122012-05-31 18:40:36 +0000226float adjust_sigma(float sigma, int *scaleFactor, int *radius) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000227 *scaleFactor = 1;
228 while (sigma > MAX_BLUR_SIGMA) {
229 *scaleFactor *= 2;
230 sigma *= 0.5f;
231 }
bsalomon@google.comb505a122012-05-31 18:40:36 +0000232 *radius = static_cast<int>(ceilf(sigma * 3.0f));
233 GrAssert(*radius <= GrConvolutionEffect::kMaxKernelRadius);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000234 return sigma;
235}
236
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000237void convolve_gaussian(GrDrawTarget* target,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000238 GrTexture* texture,
239 const SkRect& rect,
240 float sigma,
241 int radius,
242 Gr1DKernelEffect::Direction direction) {
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000243 GrRenderTarget* rt = target->drawState()->getRenderTarget();
244 GrDrawTarget::AutoStateRestore asr(target, GrDrawTarget::kReset_ASRInit);
245 GrDrawState* drawState = target->drawState();
246 drawState->setRenderTarget(rt);
bsalomon@google.comc5fae9e2013-01-24 19:00:02 +0000247 SkAutoTUnref<GrEffectRef> conv(GrConvolutionEffect::CreateGaussian(texture,
bsalomon@google.com6340a412013-01-22 19:55:59 +0000248 direction,
249 radius,
250 sigma));
bsalomon@google.comadc65362013-01-28 14:26:09 +0000251 drawState->setEffect(0, conv);
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000252 target->drawSimpleRect(rect, NULL);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000253}
254
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000255}
256
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000257////////////////////////////////////////////////////////////////////////////////
258
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000259GrTexture* GrContext::findAndRefTexture(const GrTextureDesc& desc,
260 const GrCacheID& cacheID,
261 const GrTextureParams* params) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000262 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheID);
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000263 GrResource* resource = fTextureCache->find(resourceKey);
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000264 SkSafeRef(resource);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000265 return static_cast<GrTexture*>(resource);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000266}
267
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000268bool GrContext::isTextureInCache(const GrTextureDesc& desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000269 const GrCacheID& cacheID,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000270 const GrTextureParams* params) const {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000271 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheID);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000272 return fTextureCache->hasKey(resourceKey);
273}
274
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000275void GrContext::addStencilBuffer(GrStencilBuffer* sb) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000276 ASSERT_OWNED_RESOURCE(sb);
robertphillips@google.com46a86002012-08-08 10:42:44 +0000277
278 GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(sb->width(),
279 sb->height(),
280 sb->numSamples());
robertphillips@google.com209a1142012-10-31 12:25:21 +0000281 fTextureCache->addResource(resourceKey, sb);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000282}
283
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000284GrStencilBuffer* GrContext::findStencilBuffer(int width, int height,
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000285 int sampleCnt) {
robertphillips@google.com46a86002012-08-08 10:42:44 +0000286 GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(width,
287 height,
288 sampleCnt);
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000289 GrResource* resource = fTextureCache->find(resourceKey);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000290 return static_cast<GrStencilBuffer*>(resource);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000291}
292
bsalomon@google.com27847de2011-02-22 20:59:41 +0000293static void stretchImage(void* dst,
294 int dstW,
295 int dstH,
296 void* src,
297 int srcW,
298 int srcH,
299 int bpp) {
300 GrFixed dx = (srcW << 16) / dstW;
301 GrFixed dy = (srcH << 16) / dstH;
302
303 GrFixed y = dy >> 1;
304
305 int dstXLimit = dstW*bpp;
306 for (int j = 0; j < dstH; ++j) {
307 GrFixed x = dx >> 1;
308 void* srcRow = (uint8_t*)src + (y>>16)*srcW*bpp;
309 void* dstRow = (uint8_t*)dst + j*dstW*bpp;
310 for (int i = 0; i < dstXLimit; i += bpp) {
311 memcpy((uint8_t*) dstRow + i,
312 (uint8_t*) srcRow + (x>>16)*bpp,
313 bpp);
314 x += dx;
315 }
316 y += dy;
317 }
318}
319
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000320// The desired texture is NPOT and tiled but that isn't supported by
robertphillips@google.com3319f332012-08-13 18:00:36 +0000321// the current hardware. Resize the texture to be a POT
322GrTexture* GrContext::createResizedTexture(const GrTextureDesc& desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000323 const GrCacheID& cacheID,
robertphillips@google.com3319f332012-08-13 18:00:36 +0000324 void* srcData,
325 size_t rowBytes,
326 bool needsFiltering) {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000327 SkAutoTUnref<GrTexture> clampedTexture(this->findAndRefTexture(desc, cacheID, NULL));
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000328 if (NULL == clampedTexture) {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000329 clampedTexture.reset(this->createTexture(NULL, desc, cacheID, srcData, rowBytes));
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000330
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000331 if (NULL == clampedTexture) {
robertphillips@google.com3319f332012-08-13 18:00:36 +0000332 return NULL;
333 }
334 }
robertphillips@google.com0d25eef2012-09-11 21:25:51 +0000335
robertphillips@google.com3319f332012-08-13 18:00:36 +0000336 GrTextureDesc rtDesc = desc;
337 rtDesc.fFlags = rtDesc.fFlags |
338 kRenderTarget_GrTextureFlagBit |
339 kNoStencil_GrTextureFlagBit;
340 rtDesc.fWidth = GrNextPow2(GrMax(desc.fWidth, 64));
341 rtDesc.fHeight = GrNextPow2(GrMax(desc.fHeight, 64));
342
343 GrTexture* texture = fGpu->createTexture(rtDesc, NULL, 0);
344
345 if (NULL != texture) {
346 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
347 GrDrawState* drawState = fGpu->drawState();
348 drawState->setRenderTarget(texture->asRenderTarget());
349
350 // if filtering is not desired then we want to ensure all
351 // texels in the resampled image are copies of texels from
352 // the original.
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000353 GrTextureParams params(SkShader::kClamp_TileMode, needsFiltering);
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000354 drawState->createTextureEffect(0, clampedTexture, SkMatrix::I(), params);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000355
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000356 // position + texture coordinate
357 static const GrVertexAttrib kVertexAttribs[] = {
jvanverth@google.com3b0d6312013-03-01 20:30:01 +0000358 {kVec2f_GrVertexAttribType, 0},
359 {kVec2f_GrVertexAttribType, sizeof(GrPoint)}
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000360 };
361 static const GrAttribBindings kAttribBindings = GrDrawState::ExplicitTexCoordAttribBindingsBit(0);
362 drawState->setAttribBindings(kAttribBindings);
363 drawState->setVertexAttribs(kVertexAttribs, SK_ARRAY_COUNT(kVertexAttribs));
364 drawState->setAttribIndex(GrDrawState::kPosition_AttribIndex, 0);
365 drawState->setAttribIndex(GrDrawState::kTexCoord_AttribIndex, 1);
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000366 GrDrawTarget::AutoReleaseGeometry arg(fGpu, 4, 0);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000367
368 if (arg.succeeded()) {
369 GrPoint* verts = (GrPoint*) arg.vertices();
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000370 verts[0].setIRectFan(0, 0, texture->width(), texture->height(), 2 * sizeof(GrPoint));
371 verts[1].setIRectFan(0, 0, 1, 1, 2 * sizeof(GrPoint));
372 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000373 }
374 texture->releaseRenderTarget();
375 } else {
376 // TODO: Our CPU stretch doesn't filter. But we create separate
bsalomon@google.com08283af2012-10-26 13:01:20 +0000377 // stretched textures when the texture params is either filtered or
robertphillips@google.com3319f332012-08-13 18:00:36 +0000378 // not. Either implement filtered stretch blit on CPU or just create
379 // one when FBO case fails.
380
381 rtDesc.fFlags = kNone_GrTextureFlags;
382 // no longer need to clamp at min RT size.
383 rtDesc.fWidth = GrNextPow2(desc.fWidth);
384 rtDesc.fHeight = GrNextPow2(desc.fHeight);
385 int bpp = GrBytesPerPixel(desc.fConfig);
bsalomon@google.com08283af2012-10-26 13:01:20 +0000386 SkAutoSMalloc<128*128*4> stretchedPixels(bpp * rtDesc.fWidth * rtDesc.fHeight);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000387 stretchImage(stretchedPixels.get(), rtDesc.fWidth, rtDesc.fHeight,
bsalomon@google.com08283af2012-10-26 13:01:20 +0000388 srcData, desc.fWidth, desc.fHeight, bpp);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000389
390 size_t stretchedRowBytes = rtDesc.fWidth * bpp;
391
humper@google.com0e515772013-01-07 19:54:40 +0000392 SkDEBUGCODE(GrTexture* texture = )fGpu->createTexture(rtDesc, stretchedPixels.get(), stretchedRowBytes);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000393 GrAssert(NULL != texture);
394 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000395
396 return texture;
397}
398
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000399GrTexture* GrContext::createTexture(const GrTextureParams* params,
400 const GrTextureDesc& desc,
401 const GrCacheID& cacheID,
402 void* srcData,
403 size_t rowBytes) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000404 SK_TRACE_EVENT0("GrContext::createTexture");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000405
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000406 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheID);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000407
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000408 GrTexture* texture;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000409 if (GrTexture::NeedsResizing(resourceKey)) {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000410 texture = this->createResizedTexture(desc, cacheID,
411 srcData, rowBytes,
412 GrTexture::NeedsFiltering(resourceKey));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000413 } else {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000414 texture= fGpu->createTexture(desc, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000415 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000416
417 if (NULL != texture) {
robertphillips@google.com209a1142012-10-31 12:25:21 +0000418 fTextureCache->addResource(resourceKey, texture);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000419 }
420
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000421 return texture;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000422}
423
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000424GrTexture* GrContext::lockAndRefScratchTexture(const GrTextureDesc& inDesc, ScratchTexMatch match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000425 GrTextureDesc desc = inDesc;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000426
rileya@google.com2afb8ec2012-08-23 14:08:57 +0000427 GrAssert((desc.fFlags & kRenderTarget_GrTextureFlagBit) ||
428 !(desc.fFlags & kNoStencil_GrTextureFlagBit));
429
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000430 if (kApprox_ScratchTexMatch == match) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000431 // bin by pow2 with a reasonable min
bsalomon@google.com40db6dc2013-03-06 16:35:49 +0000432 static const int MIN_SIZE = 16;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000433 desc.fWidth = GrMax(MIN_SIZE, GrNextPow2(desc.fWidth));
434 desc.fHeight = GrMax(MIN_SIZE, GrNextPow2(desc.fHeight));
435 }
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000436
robertphillips@google.com13f181f2013-03-02 12:02:08 +0000437 // Renderable A8 targets are not universally supported (e.g., not on ANGLE)
438 GrAssert(this->isConfigRenderable(kAlpha_8_GrPixelConfig) ||
439 !(desc.fFlags & kRenderTarget_GrTextureFlagBit) ||
440 (desc.fConfig != kAlpha_8_GrPixelConfig));
441
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000442 GrResource* resource = NULL;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000443 int origWidth = desc.fWidth;
444 int origHeight = desc.fHeight;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000445
446 do {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000447 GrResourceKey key = GrTexture::ComputeScratchKey(desc);
robertphillips@google.com209a1142012-10-31 12:25:21 +0000448 // Ensure we have exclusive access to the texture so future 'find' calls don't return it
449 resource = fTextureCache->find(key, GrResourceCache::kHide_OwnershipFlag);
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000450 if (NULL != resource) {
451 resource->ref();
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000452 break;
453 }
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000454 if (kExact_ScratchTexMatch == match) {
455 break;
456 }
bsalomon@google.com40db6dc2013-03-06 16:35:49 +0000457 // We had a cache miss and we are in approx mode, relax the fit of the flags.
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000458
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000459 // We no longer try to reuse textures that were previously used as render targets in
rileya@google.com2afb8ec2012-08-23 14:08:57 +0000460 // situations where no RT is needed; doing otherwise can confuse the video driver and
461 // cause significant performance problems in some cases.
462 if (desc.fFlags & kNoStencil_GrTextureFlagBit) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000463 desc.fFlags = desc.fFlags & ~kNoStencil_GrTextureFlagBit;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000464 } else {
465 break;
466 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000467
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000468 } while (true);
469
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000470 if (NULL == resource) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000471 desc.fFlags = inDesc.fFlags;
472 desc.fWidth = origWidth;
473 desc.fHeight = origHeight;
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000474 GrTexture* texture = fGpu->createTexture(desc, NULL, 0);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000475 if (NULL != texture) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000476 GrResourceKey key = GrTexture::ComputeScratchKey(texture->desc());
robertphillips@google.com209a1142012-10-31 12:25:21 +0000477 // Make the resource exclusive so future 'find' calls don't return it
478 fTextureCache->addResource(key, texture, GrResourceCache::kHide_OwnershipFlag);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000479 resource = texture;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000480 }
481 }
482
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000483 return static_cast<GrTexture*>(resource);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000484}
485
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000486void GrContext::addExistingTextureToCache(GrTexture* texture) {
487
488 if (NULL == texture) {
489 return;
490 }
491
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000492 // This texture should already have a cache entry since it was once
493 // attached
494 GrAssert(NULL != texture->getCacheEntry());
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000495
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000496 // Conceptually, the cache entry is going to assume responsibility
497 // for the creation ref.
498 GrAssert(1 == texture->getRefCnt());
499
500 // Since this texture came from an AutoScratchTexture it should
501 // still be in the exclusive pile
502 fTextureCache->makeNonExclusive(texture->getCacheEntry());
503
robertphillips@google.com50a035d2012-09-07 19:44:33 +0000504 this->purgeCache();
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000505}
506
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000507
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000508void GrContext::unlockScratchTexture(GrTexture* texture) {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000509 ASSERT_OWNED_RESOURCE(texture);
510 GrAssert(NULL != texture->getCacheEntry());
511
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000512 // If this is a scratch texture we detached it from the cache
513 // while it was locked (to avoid two callers simultaneously getting
514 // the same texture).
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000515 if (texture->getCacheEntry()->key().isScratch()) {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000516 fTextureCache->makeNonExclusive(texture->getCacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000517 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000518
robertphillips@google.com50a035d2012-09-07 19:44:33 +0000519 this->purgeCache();
520}
521
522void GrContext::purgeCache() {
robertphillips@google.comeb9b3e12012-09-11 12:50:53 +0000523 if (NULL != fTextureCache) {
524 fTextureCache->purgeAsNeeded();
525 }
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000526}
527
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000528GrTexture* GrContext::createUncachedTexture(const GrTextureDesc& descIn,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000529 void* srcData,
530 size_t rowBytes) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000531 GrTextureDesc descCopy = descIn;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000532 return fGpu->createTexture(descCopy, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000533}
534
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000535void GrContext::getTextureCacheLimits(int* maxTextures,
536 size_t* maxTextureBytes) const {
537 fTextureCache->getLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000538}
539
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000540void GrContext::setTextureCacheLimits(int maxTextures, size_t maxTextureBytes) {
541 fTextureCache->setLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000542}
543
bsalomon@google.com91958362011-06-13 17:58:13 +0000544int GrContext::getMaxTextureSize() const {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000545 return fGpu->getCaps().maxTextureSize();
bsalomon@google.com91958362011-06-13 17:58:13 +0000546}
547
548int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000549 return fGpu->getCaps().maxRenderTargetSize();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000550}
551
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000552int GrContext::getMaxSampleCount() const {
553 return fGpu->getCaps().maxSampleCount();
554}
555
bsalomon@google.com27847de2011-02-22 20:59:41 +0000556///////////////////////////////////////////////////////////////////////////////
557
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000558GrTexture* GrContext::wrapBackendTexture(const GrBackendTextureDesc& desc) {
559 return fGpu->wrapBackendTexture(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000560}
561
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000562GrRenderTarget* GrContext::wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
563 return fGpu->wrapBackendRenderTarget(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000564}
565
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000566///////////////////////////////////////////////////////////////////////////////
567
bsalomon@google.comb8670992012-07-25 21:27:09 +0000568bool GrContext::supportsIndex8PixelConfig(const GrTextureParams* params,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000569 int width, int height) const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000570 const GrDrawTarget::Caps& caps = fGpu->getCaps();
bsalomon@google.comf6601872012-08-28 21:11:35 +0000571 if (!caps.eightBitPaletteSupport()) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000572 return false;
573 }
574
bsalomon@google.com27847de2011-02-22 20:59:41 +0000575 bool isPow2 = GrIsPow2(width) && GrIsPow2(height);
576
577 if (!isPow2) {
bsalomon@google.comb8670992012-07-25 21:27:09 +0000578 bool tiled = NULL != params && params->isTiled();
bsalomon@google.comf6601872012-08-28 21:11:35 +0000579 if (tiled && !caps.npotTextureTileSupport()) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000580 return false;
581 }
582 }
583 return true;
584}
585
586////////////////////////////////////////////////////////////////////////////////
587
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000588const GrClipData* GrContext::getClip() const {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000589 return fGpu->getClip();
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000590}
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000591
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000592void GrContext::setClip(const GrClipData* clipData) {
593 fGpu->setClip(clipData);
robertphillips@google.com837ec432012-10-04 17:57:05 +0000594
skia.committer@gmail.comf467ce72012-10-09 02:01:37 +0000595 fDrawState->setState(GrDrawState::kClip_StateBit,
robertphillips@google.com183e34b2012-10-19 18:19:01 +0000596 clipData && clipData->fClipStack && !clipData->fClipStack->isWideOpen());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000597}
598
bsalomon@google.com27847de2011-02-22 20:59:41 +0000599////////////////////////////////////////////////////////////////////////////////
600
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000601void GrContext::clear(const GrIRect* rect,
602 const GrColor color,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000603 GrRenderTarget* target) {
bsalomon@google.com82b0ec62013-02-07 21:02:36 +0000604 this->prepareToDraw(NULL, BUFFERED_DRAW)->clear(rect, color, target);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000605}
606
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000607void GrContext::drawPaint(const GrPaint& origPaint) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000608 // set rect to be big enough to fill the space, but not super-huge, so we
609 // don't overflow fixed-point implementations
bsalomon@google.comd302f142011-03-03 13:54:13 +0000610 GrRect r;
611 r.setLTRB(0, 0,
bsalomon@google.com81712882012-11-01 17:12:34 +0000612 SkIntToScalar(getRenderTarget()->width()),
613 SkIntToScalar(getRenderTarget()->height()));
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000614 SkMatrix inverse;
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000615 SkTCopyOnFirstWrite<GrPaint> paint(origPaint);
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000616 AutoMatrix am;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000617
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000618 // We attempt to map r by the inverse matrix and draw that. mapRect will
619 // map the four corners and bound them with a new rect. This will not
620 // produce a correct result for some perspective matrices.
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000621 if (!this->getMatrix().hasPerspective()) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000622 if (!fDrawState->getViewInverse(&inverse)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000623 GrPrintf("Could not invert matrix\n");
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000624 return;
625 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000626 inverse.mapRect(&r);
627 } else {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000628 if (!am.setIdentity(this, paint.writable())) {
629 GrPrintf("Could not invert matrix\n");
630 return;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000631 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000632 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000633 // by definition this fills the entire clip, no need for AA
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000634 if (paint->isAntiAlias()) {
635 paint.writable()->setAntiAlias(false);
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000636 }
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000637 this->drawRect(*paint, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000638}
639
bsalomon@google.com205d4602011-04-25 12:43:45 +0000640////////////////////////////////////////////////////////////////////////////////
641
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000642namespace {
643inline bool disable_coverage_aa_for_blend(GrDrawTarget* target) {
644 return DISABLE_COVERAGE_AA_FOR_BLEND && !target->canApplyCoverage();
645}
646}
647
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000648////////////////////////////////////////////////////////////////////////////////
649
bsalomon@google.com27847de2011-02-22 20:59:41 +0000650/* create a triangle strip that strokes the specified triangle. There are 8
651 unique vertices, but we repreat the last 2 to close up. Alternatively we
652 could use an indices array, and then only send 8 verts, but not sure that
653 would be faster.
654 */
bsalomon@google.com205d4602011-04-25 12:43:45 +0000655static void setStrokeRectStrip(GrPoint verts[10], GrRect rect,
bsalomon@google.com81712882012-11-01 17:12:34 +0000656 SkScalar width) {
657 const SkScalar rad = SkScalarHalf(width);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000658 rect.sort();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000659
660 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
661 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
662 verts[2].set(rect.fRight - rad, rect.fTop + rad);
663 verts[3].set(rect.fRight + rad, rect.fTop - rad);
664 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
665 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
666 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
667 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
668 verts[8] = verts[0];
669 verts[9] = verts[1];
670}
671
reed@google.com20efde72011-05-09 17:00:02 +0000672/**
673 * Returns true if the rects edges are integer-aligned.
674 */
675static bool isIRect(const GrRect& r) {
bsalomon@google.com81712882012-11-01 17:12:34 +0000676 return SkScalarIsInt(r.fLeft) && SkScalarIsInt(r.fTop) &&
677 SkScalarIsInt(r.fRight) && SkScalarIsInt(r.fBottom);
reed@google.com20efde72011-05-09 17:00:02 +0000678}
679
bsalomon@google.com205d4602011-04-25 12:43:45 +0000680static bool apply_aa_to_rect(GrDrawTarget* target,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000681 const GrRect& rect,
bsalomon@google.com81712882012-11-01 17:12:34 +0000682 SkScalar width,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000683 const SkMatrix* matrix,
684 SkMatrix* combinedMatrix,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000685 GrRect* devRect,
686 bool* useVertexCoverage) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000687 // we use a simple coverage ramp to do aa on axis-aligned rects
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000688 // we check if the rect will be axis-aligned, and the rect won't land on
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000689 // integer coords.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000690
bsalomon@google.coma3108262011-10-10 14:08:47 +0000691 // we are keeping around the "tweak the alpha" trick because
692 // it is our only hope for the fixed-pipe implementation.
693 // In a shader implementation we can give a separate coverage input
bsalomon@google.com289533a2011-10-27 12:34:25 +0000694 // TODO: remove this ugliness when we drop the fixed-pipe impl
bsalomon@google.coma3108262011-10-10 14:08:47 +0000695 *useVertexCoverage = false;
bsalomon@google.com2b446732013-02-12 16:47:41 +0000696 if (!target->getDrawState().canTweakAlphaForCoverage()) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000697 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +0000698#if GR_DEBUG
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000699 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +0000700#endif
bsalomon@google.coma3108262011-10-10 14:08:47 +0000701 return false;
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000702 } else {
703 *useVertexCoverage = true;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000704 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000705 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000706 const GrDrawState& drawState = target->getDrawState();
707 if (drawState.getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000708 return false;
709 }
710
bsalomon@google.com471d4712011-08-23 15:45:25 +0000711 if (0 == width && target->willUseHWAALines()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000712 return false;
713 }
714
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000715 if (!drawState.getViewMatrix().preservesAxisAlignment()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000716 return false;
717 }
718
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000719 if (NULL != matrix &&
bsalomon@google.com205d4602011-04-25 12:43:45 +0000720 !matrix->preservesAxisAlignment()) {
721 return false;
722 }
723
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000724 *combinedMatrix = drawState.getViewMatrix();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000725 if (NULL != matrix) {
726 combinedMatrix->preConcat(*matrix);
727 GrAssert(combinedMatrix->preservesAxisAlignment());
728 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000729
bsalomon@google.com205d4602011-04-25 12:43:45 +0000730 combinedMatrix->mapRect(devRect, rect);
731 devRect->sort();
732
733 if (width < 0) {
reed@google.com20efde72011-05-09 17:00:02 +0000734 return !isIRect(*devRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000735 } else {
736 return true;
737 }
738}
739
bsalomon@google.com27847de2011-02-22 20:59:41 +0000740void GrContext::drawRect(const GrPaint& paint,
741 const GrRect& rect,
bsalomon@google.com81712882012-11-01 17:12:34 +0000742 SkScalar width,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000743 const SkMatrix* matrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000744 SK_TRACE_EVENT0("GrContext::drawRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000745
bsalomon@google.com82b0ec62013-02-07 21:02:36 +0000746 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000747 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000748
bsalomon@google.com205d4602011-04-25 12:43:45 +0000749 GrRect devRect = rect;
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000750 SkMatrix combinedMatrix;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000751 bool useVertexCoverage;
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000752 bool needAA = paint.isAntiAlias() &&
bsalomon@google.com289533a2011-10-27 12:34:25 +0000753 !this->getRenderTarget()->isMultisampled();
754 bool doAA = needAA && apply_aa_to_rect(target, rect, width, matrix,
755 &combinedMatrix, &devRect,
756 &useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000757
758 if (doAA) {
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000759 GrDrawState::AutoDeviceCoordDraw adcd(target->drawState());
bsalomon@google.come3d32162012-07-20 13:37:06 +0000760 if (!adcd.succeeded()) {
761 return;
762 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000763 if (width >= 0) {
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000764 GrVec strokeSize;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000765 if (width > 0) {
766 strokeSize.set(width, width);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000767 combinedMatrix.mapVectors(&strokeSize, 1);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000768 strokeSize.setAbs(strokeSize);
769 } else {
bsalomon@google.com81712882012-11-01 17:12:34 +0000770 strokeSize.set(SK_Scalar1, SK_Scalar1);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000771 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000772 fAARectRenderer->strokeAARect(this->getGpu(), target, devRect,
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000773 strokeSize, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000774 } else {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000775 fAARectRenderer->fillAARect(this->getGpu(), target,
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000776 devRect, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000777 }
778 return;
779 }
780
bsalomon@google.com27847de2011-02-22 20:59:41 +0000781 if (width >= 0) {
782 // TODO: consider making static vertex buffers for these cases.
bsalomon@google.com64386952013-02-08 21:22:44 +0000783 // Hairline could be done by just adding closing vertex to
784 // unitSquareVertexBuffer()
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000785
bsalomon@google.com27847de2011-02-22 20:59:41 +0000786 static const int worstCaseVertCount = 10;
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000787 target->drawState()->setDefaultVertexAttribs();
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000788 GrDrawTarget::AutoReleaseGeometry geo(target, worstCaseVertCount, 0);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000789
790 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000791 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000792 return;
793 }
794
795 GrPrimitiveType primType;
796 int vertCount;
797 GrPoint* vertex = geo.positions();
798
799 if (width > 0) {
800 vertCount = 10;
bsalomon@google.com47059542012-06-06 20:51:20 +0000801 primType = kTriangleStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000802 setStrokeRectStrip(vertex, rect, width);
803 } else {
804 // hairline
805 vertCount = 5;
bsalomon@google.com47059542012-06-06 20:51:20 +0000806 primType = kLineStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000807 vertex[0].set(rect.fLeft, rect.fTop);
808 vertex[1].set(rect.fRight, rect.fTop);
809 vertex[2].set(rect.fRight, rect.fBottom);
810 vertex[3].set(rect.fLeft, rect.fBottom);
811 vertex[4].set(rect.fLeft, rect.fTop);
812 }
813
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000814 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000815 if (NULL != matrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000816 GrDrawState* drawState = target->drawState();
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000817 avmr.set(drawState, *matrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000818 }
819
820 target->drawNonIndexed(primType, 0, vertCount);
821 } else {
bsalomon@google.com64386952013-02-08 21:22:44 +0000822#if GR_STATIC_RECT_VB
823 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
824 if (NULL == sqVB) {
825 GrPrintf("Failed to create static rect vb.\n");
826 return;
827 }
828
829 GrDrawState* drawState = target->drawState();
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000830 target->drawState()->setDefaultVertexAttribs();
bsalomon@google.com64386952013-02-08 21:22:44 +0000831 target->setVertexSourceToBuffer(sqVB);
832 SkMatrix m;
833 m.setAll(rect.width(), 0, rect.fLeft,
834 0, rect.height(), rect.fTop,
835 0, 0, SkMatrix::I()[8]);
836
837 if (NULL != matrix) {
838 m.postConcat(*matrix);
839 }
840 GrDrawState::AutoViewMatrixRestore avmr(drawState, m);
841
842 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
843#else
844 target->drawSimpleRect(rect, matrix);
845#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +0000846 }
847}
848
849void GrContext::drawRectToRect(const GrPaint& paint,
850 const GrRect& dstRect,
851 const GrRect& srcRect,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000852 const SkMatrix* dstMatrix,
853 const SkMatrix* srcMatrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000854 SK_TRACE_EVENT0("GrContext::drawRectToRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000855
bsalomon@google.com88becf42012-10-05 14:54:42 +0000856 // srcRect refers to paint's first color stage
857 if (!paint.isColorStageEnabled(0)) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000858 drawRect(paint, dstRect, -1, dstMatrix);
859 return;
860 }
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000861
bsalomon@google.com82b0ec62013-02-07 21:02:36 +0000862 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW);
bsalomon@google.com64386952013-02-08 21:22:44 +0000863
864#if GR_STATIC_RECT_VB
865 GrDrawState::AutoStageDisable atr(fDrawState);
866 GrDrawState* drawState = target->drawState();
867
868 SkMatrix m;
869
870 m.setAll(dstRect.width(), 0, dstRect.fLeft,
871 0, dstRect.height(), dstRect.fTop,
872 0, 0, SkMatrix::I()[8]);
873 if (NULL != dstMatrix) {
874 m.postConcat(*dstMatrix);
875 }
876
877 // The first color stage's coords come from srcRect rather than applying a matrix to dstRect.
878 // We explicitly compute a matrix for that stage below, no need to adjust here.
879 static const uint32_t kExplicitCoordMask = 1 << GrPaint::kFirstColorStage;
880 GrDrawState::AutoViewMatrixRestore avmr(drawState, m, kExplicitCoordMask);
881
882 m.setAll(srcRect.width(), 0, srcRect.fLeft,
883 0, srcRect.height(), srcRect.fTop,
884 0, 0, SkMatrix::I()[8]);
885 if (NULL != srcMatrix) {
886 m.postConcat(*srcMatrix);
887 }
888
889 drawState->preConcatStageMatrices(kExplicitCoordMask, m);
890
891 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
892 if (NULL == sqVB) {
893 GrPrintf("Failed to create static rect vb.\n");
894 return;
895 }
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000896 drawState->setDefaultVertexAttribs();
bsalomon@google.com64386952013-02-08 21:22:44 +0000897 target->setVertexSourceToBuffer(sqVB);
898 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
899#else
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000900 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000901
jvanverth@google.com39768252013-02-14 15:25:44 +0000902 target->drawRect(dstRect, dstMatrix, &srcRect, srcMatrix, 0);
bsalomon@google.com64386952013-02-08 21:22:44 +0000903#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +0000904}
905
906void GrContext::drawVertices(const GrPaint& paint,
907 GrPrimitiveType primitiveType,
908 int vertexCount,
909 const GrPoint positions[],
910 const GrPoint texCoords[],
911 const GrColor colors[],
912 const uint16_t indices[],
913 int indexCount) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000914 SK_TRACE_EVENT0("GrContext::drawVertices");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000915
916 GrDrawTarget::AutoReleaseGeometry geo;
917
bsalomon@google.com82b0ec62013-02-07 21:02:36 +0000918 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000919 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000920
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000921 GrDrawState* drawState = target->drawState();
jvanverth@google.comb8b705b2013-02-28 16:28:34 +0000922
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000923 GrVertexAttribArray<3> attribs;
924 size_t currentOffset = 0;
925 int colorOffset = -1, texOffset = -1;
926 GrAttribBindings bindings = GrDrawState::kDefault_AttribBindings;
927
928 // set position attribute
929 drawState->setAttribIndex(GrDrawState::kPosition_AttribIndex, attribs.count());
jvanverth@google.com3b0d6312013-03-01 20:30:01 +0000930 GrVertexAttrib currAttrib = {kVec2f_GrVertexAttribType, currentOffset};
931 attribs.push_back(currAttrib);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000932 currentOffset += sizeof(GrPoint);
933
934 // set up optional texture coordinate attributes
935 if (NULL != texCoords) {
936 bindings |= GrDrawState::ExplicitTexCoordAttribBindingsBit(0);
937 drawState->setAttribIndex(GrDrawState::kTexCoord_AttribIndex, attribs.count());
jvanverth@google.com3b0d6312013-03-01 20:30:01 +0000938 currAttrib.set(kVec2f_GrVertexAttribType, currentOffset);
939 attribs.push_back(currAttrib);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000940 texOffset = currentOffset;
941 currentOffset += sizeof(GrPoint);
942 }
943
944 // set up optional color attributes
945 if (NULL != colors) {
946 bindings |= GrDrawState::kColor_AttribBindingsBit;
947 drawState->setAttribIndex(GrDrawState::kColor_AttribIndex, attribs.count());
jvanverth@google.com3b0d6312013-03-01 20:30:01 +0000948 currAttrib.set(kVec4ub_GrVertexAttribType, currentOffset);
949 attribs.push_back(currAttrib);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000950 colorOffset = currentOffset;
951 currentOffset += sizeof(GrColor);
952 }
953
954 drawState->setVertexAttribs(attribs.begin(), attribs.count());
955 drawState->setAttribBindings(bindings);
956
957 size_t vertexSize = drawState->getVertexSize();
958 GrAssert(vertexSize == currentOffset);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000959 if (sizeof(GrPoint) != vertexSize) {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000960 if (!geo.set(target, vertexCount, 0)) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000961 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000962 return;
963 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000964 void* curVertex = geo.vertices();
965
966 for (int i = 0; i < vertexCount; ++i) {
967 *((GrPoint*)curVertex) = positions[i];
968
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000969 if (texOffset >= 0) {
jvanverth@google.com39768252013-02-14 15:25:44 +0000970 *(GrPoint*)((intptr_t)curVertex + texOffset) = texCoords[i];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000971 }
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000972 if (colorOffset >= 0) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000973 *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i];
974 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000975 curVertex = (void*)((intptr_t)curVertex + vertexSize);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000976 }
977 } else {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000978 target->setVertexSourceToArray(positions, vertexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000979 }
980
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000981 // we don't currently apply offscreen AA to this path. Need improved
bsalomon@google.com91958362011-06-13 17:58:13 +0000982 // management of GrDrawTarget's geometry to avoid copying points per-tile.
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000983
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000984 if (NULL != indices) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000985 target->setIndexSourceToArray(indices, indexCount);
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000986 target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000987 } else {
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000988 target->drawNonIndexed(primitiveType, 0, vertexCount);
989 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000990}
991
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000992///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com150d2842012-01-12 20:19:56 +0000993namespace {
994
bsalomon@google.com93c96602012-04-27 13:05:21 +0000995struct CircleVertex {
996 GrPoint fPos;
997 GrPoint fCenter;
bsalomon@google.com81712882012-11-01 17:12:34 +0000998 SkScalar fOuterRadius;
999 SkScalar fInnerRadius;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001000};
1001
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001002inline bool circleStaysCircle(const SkMatrix& m) {
1003 return m.isSimilarity();
bsalomon@google.com93c96602012-04-27 13:05:21 +00001004}
1005
1006}
1007
bsalomon@google.com93c96602012-04-27 13:05:21 +00001008void GrContext::drawOval(const GrPaint& paint,
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001009 const GrRect& oval,
1010 const SkStrokeRec& stroke) {
1011
1012 if (!canDrawOval(paint, oval, stroke)) {
bsalomon@google.com93c96602012-04-27 13:05:21 +00001013 SkPath path;
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001014 path.addOval(oval);
1015 this->drawPath(paint, path, stroke);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001016 return;
skia.committer@gmail.com98ded842013-01-23 07:06:17 +00001017 }
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001018
1019 internalDrawOval(paint, oval, stroke);
1020}
1021
1022bool GrContext::canDrawOval(const GrPaint& paint, const GrRect& oval, const SkStrokeRec& stroke) const {
1023
1024 if (!paint.isAntiAlias()) {
1025 return false;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001026 }
skia.committer@gmail.com98ded842013-01-23 07:06:17 +00001027
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001028 // we can draw circles in any style
skia.committer@gmail.com98ded842013-01-23 07:06:17 +00001029 bool isCircle = SkScalarNearlyEqual(oval.width(), oval.height())
1030 && circleStaysCircle(this->getMatrix());
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001031 // and for now, axis-aligned ellipses only with fill or stroke-and-fill
1032 SkStrokeRec::Style style = stroke.getStyle();
1033 bool isStroke = (style == SkStrokeRec::kStroke_Style || style == SkStrokeRec::kHairline_Style);
1034 bool isFilledAxisAlignedEllipse = this->getMatrix().rectStaysRect() && !isStroke;
1035
1036 return isCircle || isFilledAxisAlignedEllipse;
1037}
1038
1039void GrContext::internalDrawOval(const GrPaint& paint,
1040 const GrRect& oval,
1041 const SkStrokeRec& stroke) {
1042
1043 SkScalar xRadius = SkScalarHalf(oval.width());
1044 SkScalar yRadius = SkScalarHalf(oval.height());
skia.committer@gmail.com98ded842013-01-23 07:06:17 +00001045
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001046 SkScalar strokeWidth = stroke.getWidth();
1047 SkStrokeRec::Style style = stroke.getStyle();
1048
skia.committer@gmail.com98ded842013-01-23 07:06:17 +00001049 bool isCircle = SkScalarNearlyEqual(xRadius, yRadius) && circleStaysCircle(this->getMatrix());
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001050#ifdef SK_DEBUG
1051 {
1052 // we should have checked for this previously
1053 bool isStroke = (style == SkStrokeRec::kStroke_Style || style == SkStrokeRec::kHairline_Style);
1054 bool isFilledAxisAlignedEllipse = this->getMatrix().rectStaysRect() && !isStroke;
1055 SkASSERT(paint.isAntiAlias() && (isCircle || isFilledAxisAlignedEllipse));
1056 }
1057#endif
bsalomon@google.com93c96602012-04-27 13:05:21 +00001058
bsalomon@google.com82b0ec62013-02-07 21:02:36 +00001059 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW);
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001060
bsalomon@google.com0982d352012-07-31 15:33:25 +00001061 GrDrawState* drawState = target->drawState();
1062 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001063 const SkMatrix vm = drawState->getViewMatrix();
bsalomon@google.com0982d352012-07-31 15:33:25 +00001064
bsalomon@google.com93c96602012-04-27 13:05:21 +00001065 const GrRenderTarget* rt = drawState->getRenderTarget();
1066 if (NULL == rt) {
1067 return;
1068 }
1069
bsalomon@google.com5b3e8902012-10-05 20:13:28 +00001070 GrDrawState::AutoDeviceCoordDraw adcd(drawState);
bsalomon@google.come3d32162012-07-20 13:37:06 +00001071 if (!adcd.succeeded()) {
1072 return;
1073 }
bsalomon@google.com93c96602012-04-27 13:05:21 +00001074
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001075 // position + edge
1076 static const GrVertexAttrib kVertexAttribs[] = {
jvanverth@google.com3b0d6312013-03-01 20:30:01 +00001077 {kVec2f_GrVertexAttribType, 0},
1078 {kVec4f_GrVertexAttribType, sizeof(GrPoint)}
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001079 };
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001080 drawState->setVertexAttribs(kVertexAttribs, SK_ARRAY_COUNT(kVertexAttribs));
1081 drawState->setAttribIndex(GrDrawState::kPosition_AttribIndex, 0);
jvanverth@google.comb75b0a02013-02-05 20:33:30 +00001082 GrAssert(sizeof(CircleVertex) == drawState->getVertexSize());
bsalomon@google.com93c96602012-04-27 13:05:21 +00001083
jvanverth@google.comb75b0a02013-02-05 20:33:30 +00001084 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001085 if (!geo.succeeded()) {
1086 GrPrintf("Failed to get space for vertices!\n");
1087 return;
1088 }
1089
1090 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
1091
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001092 GrPoint center = GrPoint::Make(oval.centerX(), oval.centerY());
1093 vm.mapPoints(&center, 1);
1094
1095 SkScalar L;
1096 SkScalar R;
1097 SkScalar T;
1098 SkScalar B;
1099
1100 if (isCircle) {
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +00001101 drawState->setAttribBindings(GrDrawState::kEdge_AttribBindingsBit);
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001102 drawState->setVertexEdgeType(GrDrawState::kCircle_EdgeType);
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +00001103 drawState->setAttribIndex(GrDrawState::kEdge_AttribIndex, 1);
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001104
1105 xRadius = vm.mapRadius(xRadius);
1106
1107 SkScalar outerRadius = xRadius;
1108 SkScalar innerRadius = 0;
1109 SkScalar halfWidth = 0;
1110 if (style != SkStrokeRec::kFill_Style) {
1111 strokeWidth = vm.mapRadius(strokeWidth);
1112 if (SkScalarNearlyZero(strokeWidth)) {
1113 halfWidth = SK_ScalarHalf;
1114 } else {
1115 halfWidth = SkScalarHalf(strokeWidth);
1116 }
1117
1118 outerRadius += halfWidth;
1119 if (style == SkStrokeRec::kStroke_Style || style == SkStrokeRec::kHairline_Style) {
1120 innerRadius = SkMaxScalar(0, xRadius - halfWidth);
1121 }
1122 }
1123
1124 for (int i = 0; i < 4; ++i) {
1125 verts[i].fCenter = center;
1126 verts[i].fOuterRadius = outerRadius;
1127 verts[i].fInnerRadius = innerRadius;
1128 }
1129
1130 L = -outerRadius;
1131 R = +outerRadius;
1132 T = -outerRadius;
1133 B = +outerRadius;
1134 } else { // is axis-aligned ellipse
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +00001135 drawState->setAttribBindings(GrDrawState::kDefault_AttribBindings);
1136
1137 enum {
skia.committer@gmail.com91274b92013-03-13 07:01:04 +00001138 // the edge effects share this stage with glyph rendering
1139 // (kGlyphMaskStage in GrTextContext) && SW path rendering
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +00001140 // (kPathMaskStage in GrSWMaskHelper)
1141 kEdgeEffectStage = GrPaint::kTotalStages,
1142 };
1143 GrEffectRef* effect = GrEllipseEdgeEffect::Create();
1144 static const int kEdgeAttrIndex = 1;
1145 drawState->setEffect(kEdgeEffectStage, effect, &kEdgeAttrIndex)->unref();
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001146
1147 SkRect xformedRect;
1148 vm.mapRect(&xformedRect, oval);
1149
1150 xRadius = SkScalarHalf(xformedRect.width());
1151 yRadius = SkScalarHalf(xformedRect.height());
1152
1153 if (style == SkStrokeRec::kStrokeAndFill_Style && strokeWidth > 0.0f) {
1154 SkScalar halfWidth = SkScalarHalf(strokeWidth);
1155 // do (potentially) anisotropic mapping
1156 SkVector scaledStroke;
1157 scaledStroke.set(halfWidth, halfWidth);
1158 vm.mapVectors(&scaledStroke, 1);
1159 // this is legit only if scale & translation (which should be the case at the moment)
1160 xRadius += scaledStroke.fX;
1161 yRadius += scaledStroke.fY;
1162 }
1163
1164 SkScalar ratio = SkScalarDiv(xRadius, yRadius);
skia.committer@gmail.com98ded842013-01-23 07:06:17 +00001165
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001166 for (int i = 0; i < 4; ++i) {
1167 verts[i].fCenter = center;
1168 verts[i].fOuterRadius = xRadius;
1169 verts[i].fInnerRadius = ratio;
1170 }
1171
1172 L = -xRadius;
1173 R = +xRadius;
1174 T = -yRadius;
1175 B = +yRadius;
1176 }
1177
robertphillips@google.coma0a66c12012-06-22 13:14:29 +00001178 // The fragment shader will extend the radius out half a pixel
1179 // to antialias. Expand the drawn rect here so all the pixels
1180 // will be captured.
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001181 L += center.fX - SK_ScalarHalf;
1182 R += center.fX + SK_ScalarHalf;
1183 T += center.fY - SK_ScalarHalf;
1184 B += center.fY + SK_ScalarHalf;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001185
1186 verts[0].fPos = SkPoint::Make(L, T);
1187 verts[1].fPos = SkPoint::Make(R, T);
1188 verts[2].fPos = SkPoint::Make(L, B);
1189 verts[3].fPos = SkPoint::Make(R, B);
1190
bsalomon@google.com47059542012-06-06 20:51:20 +00001191 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4);
bsalomon@google.com150d2842012-01-12 20:19:56 +00001192}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001193
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001194void GrContext::drawPath(const GrPaint& paint, const SkPath& path, const SkStrokeRec& stroke) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001195
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001196 if (path.isEmpty()) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001197 if (path.isInverseFillType()) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001198 this->drawPaint(paint);
1199 }
1200 return;
1201 }
1202
bsalomon@google.com93c96602012-04-27 13:05:21 +00001203 SkRect ovalRect;
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001204 bool isOval = path.isOval(&ovalRect);
1205
1206 if (isOval && !path.isInverseFillType() && this->canDrawOval(paint, ovalRect, stroke)) {
1207 this->drawOval(paint, ovalRect, stroke);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001208 return;
1209 }
1210
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001211 this->internalDrawPath(paint, path, stroke);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001212}
1213
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001214void GrContext::internalDrawPath(const GrPaint& paint, const SkPath& path, const SkStrokeRec& stroke) {
bsalomon@google.com93c96602012-04-27 13:05:21 +00001215
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001216 // Note that below we may sw-rasterize the path into a scratch texture.
1217 // Scratch textures can be recycled after they are returned to the texture
1218 // cache. This presents a potential hazard for buffered drawing. However,
1219 // the writePixels that uploads to the scratch will perform a flush so we're
1220 // OK.
bsalomon@google.com82b0ec62013-02-07 21:02:36 +00001221 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +00001222 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001223
bsalomon@google.comc7448ce2012-10-05 19:04:13 +00001224 bool prAA = paint.isAntiAlias() && !this->getRenderTarget()->isMultisampled();
bsalomon@google.com289533a2011-10-27 12:34:25 +00001225
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001226 // An Assumption here is that path renderer would use some form of tweaking
1227 // the src color (either the input alpha or in the frag shader) to implement
1228 // aa. If we have some future driver-mojo path AA that can do the right
1229 // thing WRT to the blend then we'll need some query on the PR.
1230 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001231#if GR_DEBUG
bsalomon@google.com979432b2011-11-05 21:38:22 +00001232 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001233#endif
bsalomon@google.com289533a2011-10-27 12:34:25 +00001234 prAA = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001235 }
bsalomon@google.com289533a2011-10-27 12:34:25 +00001236
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001237 GrPathRendererChain::DrawType type = prAA ? GrPathRendererChain::kColorAntiAlias_DrawType :
1238 GrPathRendererChain::kColor_DrawType;
1239
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001240 const SkPath* pathPtr = &path;
1241 SkPath tmpPath;
1242 SkStrokeRec strokeRec(stroke);
1243
1244 // Try a 1st time without stroking the path and without allowing the SW renderer
1245 GrPathRenderer* pr = this->getPathRenderer(*pathPtr, strokeRec, target, false, type);
1246
1247 if (NULL == pr) {
1248 if (!strokeRec.isHairlineStyle()) {
1249 // It didn't work the 1st time, so try again with the stroked path
1250 if (strokeRec.applyToPath(&tmpPath, *pathPtr)) {
1251 pathPtr = &tmpPath;
1252 strokeRec.setFillStyle();
1253 }
1254 }
1255 // This time, allow SW renderer
1256 pr = this->getPathRenderer(*pathPtr, strokeRec, target, true, type);
1257 }
1258
bsalomon@google.com30085192011-08-19 15:42:31 +00001259 if (NULL == pr) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001260#if GR_DEBUG
bsalomon@google.com30085192011-08-19 15:42:31 +00001261 GrPrintf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001262#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001263 return;
1264 }
1265
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001266 pr->drawPath(*pathPtr, strokeRec, target, prAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001267}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001268
bsalomon@google.com27847de2011-02-22 20:59:41 +00001269////////////////////////////////////////////////////////////////////////////////
1270
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001271void GrContext::flush(int flagsBitfield) {
1272 if (kDiscard_FlushBit & flagsBitfield) {
1273 fDrawBuffer->reset();
1274 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001275 this->flushDrawBuffer();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001276 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001277 if (kForceCurrentRenderTarget_FlushBit & flagsBitfield) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001278 fGpu->forceRenderTargetFlush();
1279 }
1280}
1281
bsalomon@google.com27847de2011-02-22 20:59:41 +00001282void GrContext::flushDrawBuffer() {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001283 if (NULL != fDrawBuffer && !fDrawBuffer->isFlushing()) {
1284 fDrawBuffer->flush();
junov@google.com53a55842011-06-08 22:55:10 +00001285 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001286}
1287
bsalomon@google.com9c680582013-02-06 18:17:50 +00001288bool GrContext::writeTexturePixels(GrTexture* texture,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001289 int left, int top, int width, int height,
1290 GrPixelConfig config, const void* buffer, size_t rowBytes,
1291 uint32_t flags) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001292 SK_TRACE_EVENT0("GrContext::writeTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001293 ASSERT_OWNED_RESOURCE(texture);
1294
bsalomon@google.com9c680582013-02-06 18:17:50 +00001295 if ((kUnpremul_PixelOpsFlag & flags) || !fGpu->canWriteTexturePixels(texture, config)) {
1296 if (NULL != texture->asRenderTarget()) {
1297 return this->writeRenderTargetPixels(texture->asRenderTarget(),
1298 left, top, width, height,
1299 config, buffer, rowBytes, flags);
1300 } else {
1301 return false;
1302 }
bsalomon@google.com0342a852012-08-20 19:22:38 +00001303 }
bsalomon@google.com9c680582013-02-06 18:17:50 +00001304
bsalomon@google.com6f379512011-11-16 20:36:03 +00001305 if (!(kDontFlush_PixelOpsFlag & flags)) {
1306 this->flush();
1307 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001308
bsalomon@google.com9c680582013-02-06 18:17:50 +00001309 return fGpu->writeTexturePixels(texture, left, top, width, height,
1310 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +00001311}
1312
bsalomon@google.com0342a852012-08-20 19:22:38 +00001313bool GrContext::readTexturePixels(GrTexture* texture,
1314 int left, int top, int width, int height,
1315 GrPixelConfig config, void* buffer, size_t rowBytes,
1316 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001317 SK_TRACE_EVENT0("GrContext::readTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001318 ASSERT_OWNED_RESOURCE(texture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001319
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001320 // TODO: code read pixels for textures that aren't also rendertargets
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001321 GrRenderTarget* target = texture->asRenderTarget();
1322 if (NULL != target) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00001323 return this->readRenderTargetPixels(target,
1324 left, top, width, height,
1325 config, buffer, rowBytes,
1326 flags);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001327 } else {
1328 return false;
1329 }
1330}
1331
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001332#include "SkConfig8888.h"
1333
1334namespace {
1335/**
1336 * Converts a GrPixelConfig to a SkCanvas::Config8888. Only byte-per-channel
1337 * formats are representable as Config8888 and so the function returns false
1338 * if the GrPixelConfig has no equivalent Config8888.
1339 */
1340bool grconfig_to_config8888(GrPixelConfig config,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001341 bool unpremul,
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001342 SkCanvas::Config8888* config8888) {
1343 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00001344 case kRGBA_8888_GrPixelConfig:
1345 if (unpremul) {
1346 *config8888 = SkCanvas::kRGBA_Unpremul_Config8888;
1347 } else {
1348 *config8888 = SkCanvas::kRGBA_Premul_Config8888;
1349 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001350 return true;
bsalomon@google.com0342a852012-08-20 19:22:38 +00001351 case kBGRA_8888_GrPixelConfig:
1352 if (unpremul) {
1353 *config8888 = SkCanvas::kBGRA_Unpremul_Config8888;
1354 } else {
1355 *config8888 = SkCanvas::kBGRA_Premul_Config8888;
1356 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001357 return true;
1358 default:
1359 return false;
1360 }
1361}
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001362
1363// It returns a configuration with where the byte position of the R & B components are swapped in
1364// relation to the input config. This should only be called with the result of
1365// grconfig_to_config8888 as it will fail for other configs.
1366SkCanvas::Config8888 swap_config8888_red_and_blue(SkCanvas::Config8888 config8888) {
1367 switch (config8888) {
1368 case SkCanvas::kBGRA_Premul_Config8888:
1369 return SkCanvas::kRGBA_Premul_Config8888;
1370 case SkCanvas::kBGRA_Unpremul_Config8888:
1371 return SkCanvas::kRGBA_Unpremul_Config8888;
1372 case SkCanvas::kRGBA_Premul_Config8888:
1373 return SkCanvas::kBGRA_Premul_Config8888;
1374 case SkCanvas::kRGBA_Unpremul_Config8888:
1375 return SkCanvas::kBGRA_Unpremul_Config8888;
1376 default:
1377 GrCrash("Unexpected input");
1378 return SkCanvas::kBGRA_Unpremul_Config8888;;
1379 }
1380}
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001381}
1382
bsalomon@google.com0342a852012-08-20 19:22:38 +00001383bool GrContext::readRenderTargetPixels(GrRenderTarget* target,
1384 int left, int top, int width, int height,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001385 GrPixelConfig dstConfig, void* buffer, size_t rowBytes,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001386 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001387 SK_TRACE_EVENT0("GrContext::readRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001388 ASSERT_OWNED_RESOURCE(target);
1389
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001390 if (NULL == target) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001391 target = fDrawState->getRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001392 if (NULL == target) {
1393 return false;
1394 }
1395 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001396
bsalomon@google.com6f379512011-11-16 20:36:03 +00001397 if (!(kDontFlush_PixelOpsFlag & flags)) {
1398 this->flush();
1399 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001400
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001401 // Determine which conversions have to be applied: flipY, swapRAnd, and/or unpremul.
bsalomon@google.com0342a852012-08-20 19:22:38 +00001402
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001403 // If fGpu->readPixels would incur a y-flip cost then we will read the pixels upside down. We'll
1404 // either do the flipY by drawing into a scratch with a matrix or on the cpu after the read.
1405 bool flipY = fGpu->readPixelsWillPayForYFlip(target, left, top,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001406 width, height, dstConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001407 rowBytes);
bsalomon@google.com9c680582013-02-06 18:17:50 +00001408 // We ignore the preferred config if it is different than our config unless it is an R/B swap.
1409 // In that case we'll perform an R and B swap while drawing to a scratch texture of the swapped
1410 // config. Then we will call readPixels on the scratch with the swapped config. The swaps during
1411 // the draw cancels out the fact that we call readPixels with a config that is R/B swapped from
1412 // dstConfig.
1413 GrPixelConfig readConfig = dstConfig;
1414 bool swapRAndB = false;
1415 if (GrPixelConfigSwapRAndB(dstConfig) == fGpu->preferredReadPixelsConfig(dstConfig)) {
1416 readConfig = GrPixelConfigSwapRAndB(readConfig);
1417 swapRAndB = true;
1418 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001419
bsalomon@google.com0342a852012-08-20 19:22:38 +00001420 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001421
bsalomon@google.com9c680582013-02-06 18:17:50 +00001422 if (unpremul && !GrPixelConfigIs8888(dstConfig)) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001423 // The unpremul flag is only allowed for these two configs.
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001424 return false;
1425 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001426
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001427 // If the src is a texture and we would have to do conversions after read pixels, we instead
1428 // do the conversions by drawing the src to a scratch texture. If we handle any of the
1429 // conversions in the draw we set the corresponding bool to false so that we don't reapply it
1430 // on the read back pixels.
1431 GrTexture* src = target->asTexture();
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001432 GrAutoScratchTexture ast;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001433 if (NULL != src && (swapRAndB || unpremul || flipY)) {
1434 // Make the scratch a render target because we don't have a robust readTexturePixels as of
1435 // yet. It calls this function.
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001436 GrTextureDesc desc;
1437 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1438 desc.fWidth = width;
1439 desc.fHeight = height;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001440 desc.fConfig = readConfig;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001441 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001442
bsalomon@google.com9c680582013-02-06 18:17:50 +00001443 // 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 +00001444 // match the passed rect. However, if we see many different size rectangles we will trash
1445 // our texture cache and pay the cost of creating and destroying many textures. So, we only
1446 // request an exact match when the caller is reading an entire RT.
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001447 ScratchTexMatch match = kApprox_ScratchTexMatch;
1448 if (0 == left &&
1449 0 == top &&
1450 target->width() == width &&
1451 target->height() == height &&
1452 fGpu->fullReadPixelsIsFasterThanPartial()) {
1453 match = kExact_ScratchTexMatch;
1454 }
1455 ast.set(this, desc, match);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001456 GrTexture* texture = ast.texture();
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001457 if (texture) {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001458 // compute a matrix to perform the draw
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001459 SkMatrix textureMatrix;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001460 textureMatrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001461 textureMatrix.postIDiv(src->width(), src->height());
1462
bsalomon@google.comadc65362013-01-28 14:26:09 +00001463 SkAutoTUnref<const GrEffectRef> effect;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001464 if (unpremul) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001465 effect.reset(this->createPMToUPMEffect(src, swapRAndB, textureMatrix));
1466 if (NULL != effect) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001467 unpremul = false; // we no longer need to do this on CPU after the read back.
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001468 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001469 }
1470 // If we failed to create a PM->UPM effect and have no other conversions to perform then
1471 // there is no longer any point to using the scratch.
bsalomon@google.comadc65362013-01-28 14:26:09 +00001472 if (NULL != effect || flipY || swapRAndB) {
1473 if (!effect) {
1474 effect.reset(GrConfigConversionEffect::Create(
1475 src,
1476 swapRAndB,
1477 GrConfigConversionEffect::kNone_PMConversion,
1478 textureMatrix));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001479 }
1480 swapRAndB = false; // we will handle the swap in the draw.
bsalomon@google.comc4364992011-11-07 15:54:49 +00001481
robertphillips@google.com13f181f2013-03-02 12:02:08 +00001482 // We protect the existing geometry here since it may not be
1483 // clear to the caller that a draw operation (i.e., drawSimpleRect)
1484 // can be invoked in this method
1485 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001486 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.comadc65362013-01-28 14:26:09 +00001487 GrAssert(effect);
bsalomon@google.com9e040ae2013-01-28 14:31:19 +00001488 drawState->setEffect(0, effect);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001489
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001490 drawState->setRenderTarget(texture->asRenderTarget());
bsalomon@google.com81712882012-11-01 17:12:34 +00001491 GrRect rect = GrRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001492 fGpu->drawSimpleRect(rect, NULL);
1493 // we want to read back from the scratch's origin
1494 left = 0;
1495 top = 0;
1496 target = texture->asRenderTarget();
1497 }
bsalomon@google.com0342a852012-08-20 19:22:38 +00001498 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001499 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001500 if (!fGpu->readPixels(target,
1501 left, top, width, height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001502 readConfig, buffer, rowBytes)) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001503 return false;
1504 }
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001505 // Perform any conversions we weren't able to perform using a scratch texture.
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001506 if (unpremul || swapRAndB) {
bsalomon@google.comccaa0022012-09-25 19:55:07 +00001507 // These are initialized to suppress a warning
1508 SkCanvas::Config8888 srcC8888 = SkCanvas::kNative_Premul_Config8888;
1509 SkCanvas::Config8888 dstC8888 = SkCanvas::kNative_Premul_Config8888;
1510
bsalomon@google.com9c680582013-02-06 18:17:50 +00001511 SkDEBUGCODE(bool c8888IsValid =) grconfig_to_config8888(dstConfig, false, &srcC8888);
1512 grconfig_to_config8888(dstConfig, unpremul, &dstC8888);
bsalomon@google.comccaa0022012-09-25 19:55:07 +00001513
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001514 if (swapRAndB) {
1515 GrAssert(c8888IsValid); // we should only do r/b swap on 8888 configs
1516 srcC8888 = swap_config8888_red_and_blue(srcC8888);
1517 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001518 GrAssert(c8888IsValid);
1519 uint32_t* b32 = reinterpret_cast<uint32_t*>(buffer);
1520 SkConvertConfig8888Pixels(b32, rowBytes, dstC8888,
1521 b32, rowBytes, srcC8888,
1522 width, height);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001523 }
1524 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001525}
1526
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001527void GrContext::resolveRenderTarget(GrRenderTarget* target) {
1528 GrAssert(target);
1529 ASSERT_OWNED_RESOURCE(target);
1530 // In the future we may track whether there are any pending draws to this
1531 // target. We don't today so we always perform a flush. We don't promise
1532 // this to our clients, though.
1533 this->flush();
1534 fGpu->resolveRenderTarget(target);
1535}
1536
scroggo@google.coma2a31922012-12-07 19:14:45 +00001537void GrContext::copyTexture(GrTexture* src, GrRenderTarget* dst, const SkIPoint* topLeft) {
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001538 if (NULL == src || NULL == dst) {
1539 return;
1540 }
1541 ASSERT_OWNED_RESOURCE(src);
1542
twiz@google.com1ac87ff2012-04-27 19:39:33 +00001543 // Writes pending to the source texture are not tracked, so a flush
1544 // is required to ensure that the copy captures the most recent contents
bsalomon@google.comadc65362013-01-28 14:26:09 +00001545 // of the source texture. See similar behavior in
twiz@google.com1ac87ff2012-04-27 19:39:33 +00001546 // GrContext::resolveRenderTarget.
1547 this->flush();
1548
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001549 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001550 GrDrawState* drawState = fGpu->drawState();
1551 drawState->setRenderTarget(dst);
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001552 SkMatrix sampleM;
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001553 sampleM.setIDiv(src->width(), src->height());
scroggo@google.coma2a31922012-12-07 19:14:45 +00001554 SkIRect srcRect = SkIRect::MakeWH(dst->width(), dst->height());
1555 if (NULL != topLeft) {
1556 srcRect.offset(*topLeft);
1557 }
1558 SkIRect srcBounds = SkIRect::MakeWH(src->width(), src->height());
1559 if (!srcRect.intersect(srcBounds)) {
1560 return;
1561 }
1562 sampleM.preTranslate(SkIntToScalar(srcRect.fLeft), SkIntToScalar(srcRect.fTop));
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +00001563 drawState->createTextureEffect(0, src, sampleM);
scroggo@google.coma2a31922012-12-07 19:14:45 +00001564 SkRect dstR = SkRect::MakeWH(SkIntToScalar(srcRect.width()), SkIntToScalar(srcRect.height()));
1565 fGpu->drawSimpleRect(dstR, NULL);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001566}
1567
bsalomon@google.com9c680582013-02-06 18:17:50 +00001568bool GrContext::writeRenderTargetPixels(GrRenderTarget* target,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001569 int left, int top, int width, int height,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001570 GrPixelConfig srcConfig,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001571 const void* buffer,
1572 size_t rowBytes,
1573 uint32_t flags) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001574 SK_TRACE_EVENT0("GrContext::writeRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001575 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com6f379512011-11-16 20:36:03 +00001576
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001577 if (NULL == target) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001578 target = fDrawState->getRenderTarget();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001579 if (NULL == target) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001580 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +00001581 }
1582 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001583
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001584 // TODO: when underlying api has a direct way to do this we should use it (e.g. glDrawPixels on
1585 // desktop GL).
1586
1587 // We will always call some form of writeTexturePixels and we will pass our flags on to it.
1588 // Thus, we don't perform a flush here since that call will do it (if the kNoFlush flag isn't
1589 // set.)
bsalomon@google.com27847de2011-02-22 20:59:41 +00001590
bsalomon@google.com0342a852012-08-20 19:22:38 +00001591 // If the RT is also a texture and we don't have to premultiply then take the texture path.
1592 // We expect to be at least as fast or faster since it doesn't use an intermediate texture as
1593 // we do below.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001594
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001595#if !GR_MAC_BUILD
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001596 // At least some drivers on the Mac get confused when glTexImage2D is called on a texture
1597 // attached to an FBO. The FBO still sees the old image. TODO: determine what OS versions and/or
1598 // HW is affected.
bsalomon@google.com9c680582013-02-06 18:17:50 +00001599 if (NULL != target->asTexture() && !(kUnpremul_PixelOpsFlag & flags) &&
1600 fGpu->canWriteTexturePixels(target->asTexture(), srcConfig)) {
1601 return this->writeTexturePixels(target->asTexture(),
1602 left, top, width, height,
1603 srcConfig, buffer, rowBytes, flags);
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001604 }
1605#endif
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001606
bsalomon@google.com9c680582013-02-06 18:17:50 +00001607 // We ignore the preferred config unless it is a R/B swap of the src config. In that case
1608 // we will upload the original src data to a scratch texture but we will spoof it as the swapped
1609 // config. This scratch will then have R and B swapped. We correct for this by swapping again
1610 // when drawing the scratch to the dst using a conversion effect.
1611 bool swapRAndB = false;
1612 GrPixelConfig writeConfig = srcConfig;
1613 if (fGpu->preferredWritePixelsConfig(srcConfig) == GrPixelConfigSwapRAndB(srcConfig)) {
1614 writeConfig = GrPixelConfigSwapRAndB(srcConfig);
1615 swapRAndB = true;
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001616 }
1617
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001618 GrTextureDesc desc;
1619 desc.fWidth = width;
1620 desc.fHeight = height;
bsalomon@google.com9c680582013-02-06 18:17:50 +00001621 desc.fConfig = writeConfig;
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001622 GrAutoScratchTexture ast(this, desc);
1623 GrTexture* texture = ast.texture();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001624 if (NULL == texture) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001625 return false;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001626 }
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001627
bsalomon@google.comadc65362013-01-28 14:26:09 +00001628 SkAutoTUnref<const GrEffectRef> effect;
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001629 SkMatrix textureMatrix;
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001630 textureMatrix.setIDiv(texture->width(), texture->height());
1631
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001632 // allocate a tmp buffer and sw convert the pixels to premul
1633 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
1634
1635 if (kUnpremul_PixelOpsFlag & flags) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001636 if (!GrPixelConfigIs8888(srcConfig)) {
1637 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001638 }
bsalomon@google.comadc65362013-01-28 14:26:09 +00001639 effect.reset(this->createUPMToPMEffect(texture, swapRAndB, textureMatrix));
bsalomon@google.com9c680582013-02-06 18:17:50 +00001640 // 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 +00001641 if (NULL == effect) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001642 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1643 GR_DEBUGCODE(bool success = )
bsalomon@google.com9c680582013-02-06 18:17:50 +00001644 grconfig_to_config8888(srcConfig, true, &srcConfig8888);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001645 GrAssert(success);
1646 GR_DEBUGCODE(success = )
bsalomon@google.com9c680582013-02-06 18:17:50 +00001647 grconfig_to_config8888(srcConfig, false, &dstConfig8888);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001648 GrAssert(success);
1649 const uint32_t* src = reinterpret_cast<const uint32_t*>(buffer);
1650 tmpPixels.reset(width * height);
1651 SkConvertConfig8888Pixels(tmpPixels.get(), 4 * width, dstConfig8888,
1652 src, rowBytes, srcConfig8888,
1653 width, height);
1654 buffer = tmpPixels.get();
1655 rowBytes = 4 * width;
1656 }
1657 }
bsalomon@google.comadc65362013-01-28 14:26:09 +00001658 if (NULL == effect) {
1659 effect.reset(GrConfigConversionEffect::Create(texture,
1660 swapRAndB,
1661 GrConfigConversionEffect::kNone_PMConversion,
1662 textureMatrix));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001663 }
1664
bsalomon@google.com9c680582013-02-06 18:17:50 +00001665 if (!this->writeTexturePixels(texture,
1666 0, 0, width, height,
1667 writeConfig, buffer, rowBytes,
1668 flags & ~kUnpremul_PixelOpsFlag)) {
1669 return false;
1670 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001671
robertphillips@google.com13f181f2013-03-02 12:02:08 +00001672 // writeRenderTargetPixels can be called in the midst of drawing another
skia.committer@gmail.com0c23faf2013-03-03 07:16:29 +00001673 // object (e.g., when uploading a SW path rendering to the gpu while
robertphillips@google.com13f181f2013-03-02 12:02:08 +00001674 // drawing a rect) so preserve the current geometry.
1675 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001676 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.comadc65362013-01-28 14:26:09 +00001677 GrAssert(effect);
bsalomon@google.com9e040ae2013-01-28 14:31:19 +00001678 drawState->setEffect(0, effect);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001679
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001680 SkMatrix matrix;
bsalomon@google.com81712882012-11-01 17:12:34 +00001681 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001682 drawState->setViewMatrix(matrix);
1683 drawState->setRenderTarget(target);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001684
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001685 fGpu->drawSimpleRect(GrRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)), NULL);
bsalomon@google.com9c680582013-02-06 18:17:50 +00001686 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001687}
1688////////////////////////////////////////////////////////////////////////////////
1689
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001690GrDrawTarget* GrContext::prepareToDraw(const GrPaint* paint, BufferedDraw buffered) {
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001691 if (kNo_BufferedDraw == buffered && kYes_BufferedDraw == fLastDrawWasBuffered) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001692 this->flushDrawBuffer();
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001693 fLastDrawWasBuffered = kNo_BufferedDraw;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001694 }
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001695 if (NULL != paint) {
bsalomon@google.comaf84e742012-10-05 13:23:24 +00001696 GrAssert(fDrawState->stagesDisabled());
1697 fDrawState->setFromPaint(*paint);
1698#if GR_DEBUG_PARTIAL_COVERAGE_CHECK
1699 if ((paint->hasMask() || 0xff != paint->fCoverage) &&
1700 !fGpu->canApplyCoverage()) {
1701 GrPrintf("Partial pixel coverage will be incorrectly blended.\n");
1702 }
1703#endif
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001704 }
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001705 if (kYes_BufferedDraw == buffered) {
1706 fDrawBuffer->setClip(fGpu->getClip());
1707 fLastDrawWasBuffered = kYes_BufferedDraw;
1708 return fDrawBuffer;
1709 } else {
1710 GrAssert(kNo_BufferedDraw == buffered);
1711 return fGpu;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001712 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001713}
1714
robertphillips@google.com72176b22012-05-23 13:19:12 +00001715/*
1716 * This method finds a path renderer that can draw the specified path on
1717 * the provided target.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001718 * Due to its expense, the software path renderer has split out so it can
robertphillips@google.com72176b22012-05-23 13:19:12 +00001719 * can be individually allowed/disallowed via the "allowSW" boolean.
1720 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001721GrPathRenderer* GrContext::getPathRenderer(const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001722 const SkStrokeRec& stroke,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001723 const GrDrawTarget* target,
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001724 bool allowSW,
1725 GrPathRendererChain::DrawType drawType,
1726 GrPathRendererChain::StencilSupport* stencilSupport) {
1727
bsalomon@google.com30085192011-08-19 15:42:31 +00001728 if (NULL == fPathRendererChain) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001729 fPathRendererChain = SkNEW_ARGS(GrPathRendererChain, (this));
bsalomon@google.com30085192011-08-19 15:42:31 +00001730 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001731
sugoi@google.com12b4e272012-12-06 20:13:11 +00001732 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(path,
1733 stroke,
robertphillips@google.com72176b22012-05-23 13:19:12 +00001734 target,
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001735 drawType,
1736 stencilSupport);
robertphillips@google.com72176b22012-05-23 13:19:12 +00001737
1738 if (NULL == pr && allowSW) {
1739 if (NULL == fSoftwarePathRenderer) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001740 fSoftwarePathRenderer = SkNEW_ARGS(GrSoftwarePathRenderer, (this));
robertphillips@google.com72176b22012-05-23 13:19:12 +00001741 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001742 pr = fSoftwarePathRenderer;
1743 }
1744
1745 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001746}
1747
bsalomon@google.com27847de2011-02-22 20:59:41 +00001748////////////////////////////////////////////////////////////////////////////////
1749
bsalomon@google.com27847de2011-02-22 20:59:41 +00001750void GrContext::setRenderTarget(GrRenderTarget* target) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001751 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001752 fDrawState->setRenderTarget(target);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001753}
1754
1755GrRenderTarget* GrContext::getRenderTarget() {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001756 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001757}
1758
1759const GrRenderTarget* GrContext::getRenderTarget() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001760 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001761}
1762
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001763bool GrContext::isConfigRenderable(GrPixelConfig config) const {
1764 return fGpu->isConfigRenderable(config);
1765}
1766
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001767const SkMatrix& GrContext::getMatrix() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001768 return fDrawState->getViewMatrix();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001769}
1770
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001771void GrContext::setMatrix(const SkMatrix& m) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001772 fDrawState->setViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001773}
1774
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +00001775void GrContext::setIdentityMatrix() {
1776 fDrawState->viewMatrix()->reset();
1777}
1778
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001779void GrContext::concatMatrix(const SkMatrix& m) const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001780 fDrawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001781}
1782
1783static inline intptr_t setOrClear(intptr_t bits, int shift, intptr_t pred) {
1784 intptr_t mask = 1 << shift;
1785 if (pred) {
1786 bits |= mask;
1787 } else {
1788 bits &= ~mask;
1789 }
1790 return bits;
1791}
1792
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001793void GrContext::setupDrawBuffer() {
1794
1795 GrAssert(NULL == fDrawBuffer);
1796 GrAssert(NULL == fDrawBufferVBAllocPool);
1797 GrAssert(NULL == fDrawBufferIBAllocPool);
1798
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001799 fDrawBufferVBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001800 SkNEW_ARGS(GrVertexBufferAllocPool, (fGpu, false,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001801 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001802 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS));
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001803 fDrawBufferIBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001804 SkNEW_ARGS(GrIndexBufferAllocPool, (fGpu, false,
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001805 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001806 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001807
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001808 fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (fGpu,
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001809 fDrawBufferVBAllocPool,
1810 fDrawBufferIBAllocPool));
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001811
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001812 fDrawBuffer->setDrawState(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001813}
1814
bsalomon@google.com27847de2011-02-22 20:59:41 +00001815GrDrawTarget* GrContext::getTextTarget(const GrPaint& paint) {
bsalomon@google.com82b0ec62013-02-07 21:02:36 +00001816 return this->prepareToDraw(&paint, BUFFERED_DRAW);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001817}
1818
1819const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {
1820 return fGpu->getQuadIndexBuffer();
1821}
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001822
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001823namespace {
1824void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
1825 GrConfigConversionEffect::PMConversion pmToUPM;
1826 GrConfigConversionEffect::PMConversion upmToPM;
1827 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM);
1828 *pmToUPMValue = pmToUPM;
1829 *upmToPMValue = upmToPM;
1830}
1831}
1832
bsalomon@google.comadc65362013-01-28 14:26:09 +00001833const GrEffectRef* GrContext::createPMToUPMEffect(GrTexture* texture,
1834 bool swapRAndB,
1835 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001836 if (!fDidTestPMConversions) {
1837 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001838 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001839 }
1840 GrConfigConversionEffect::PMConversion pmToUPM =
1841 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
1842 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001843 return GrConfigConversionEffect::Create(texture, swapRAndB, pmToUPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001844 } else {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001845 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001846 }
1847}
1848
bsalomon@google.comadc65362013-01-28 14:26:09 +00001849const GrEffectRef* GrContext::createUPMToPMEffect(GrTexture* texture,
1850 bool swapRAndB,
1851 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001852 if (!fDidTestPMConversions) {
1853 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001854 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001855 }
1856 GrConfigConversionEffect::PMConversion upmToPM =
1857 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion);
1858 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001859 return GrConfigConversionEffect::Create(texture, swapRAndB, upmToPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001860 } else {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001861 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001862 }
1863}
1864
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001865GrTexture* GrContext::gaussianBlur(GrTexture* srcTexture,
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001866 bool canClobberSrc,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001867 const SkRect& rect,
1868 float sigmaX, float sigmaY) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001869 ASSERT_OWNED_RESOURCE(srcTexture);
robertphillips@google.comccb39502012-10-01 18:25:13 +00001870
1871 AutoRenderTarget art(this);
1872
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001873 AutoMatrix am;
1874 am.setIdentity(this);
1875
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001876 SkIRect clearRect;
bsalomon@google.comb505a122012-05-31 18:40:36 +00001877 int scaleFactorX, radiusX;
1878 int scaleFactorY, radiusY;
1879 sigmaX = adjust_sigma(sigmaX, &scaleFactorX, &radiusX);
1880 sigmaY = adjust_sigma(sigmaY, &scaleFactorY, &radiusY);
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001881
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001882 SkRect srcRect(rect);
1883 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
1884 srcRect.roundOut();
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001885 scale_rect(&srcRect, static_cast<float>(scaleFactorX),
robertphillips@google.com8637a362012-04-10 18:32:35 +00001886 static_cast<float>(scaleFactorY));
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +00001887
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001888 AutoClip acs(this, srcRect);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001889
bsalomon@google.com0342a852012-08-20 19:22:38 +00001890 GrAssert(kBGRA_8888_GrPixelConfig == srcTexture->config() ||
1891 kRGBA_8888_GrPixelConfig == srcTexture->config() ||
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001892 kAlpha_8_GrPixelConfig == srcTexture->config());
1893
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001894 GrTextureDesc desc;
1895 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
1896 desc.fWidth = SkScalarFloorToInt(srcRect.width());
1897 desc.fHeight = SkScalarFloorToInt(srcRect.height());
1898 desc.fConfig = srcTexture->config();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001899
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001900 GrAutoScratchTexture temp1, temp2;
1901 GrTexture* dstTexture = temp1.set(this, desc);
1902 GrTexture* tempTexture = canClobberSrc ? srcTexture : temp2.set(this, desc);
robertphillips@google.com7d126752012-10-19 12:56:26 +00001903 if (NULL == dstTexture || NULL == tempTexture) {
1904 return NULL;
1905 }
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001906
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001907 GrPaint paint;
1908 paint.reset();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001909
1910 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001911 SkMatrix matrix;
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +00001912 matrix.setIDiv(srcTexture->width(), srcTexture->height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001913 this->setRenderTarget(dstTexture->asRenderTarget());
1914 SkRect dstRect(srcRect);
1915 scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +00001916 i < scaleFactorY ? 0.5f : 1.0f);
1917
bsalomon@google.com68b58c92013-01-17 16:50:08 +00001918 paint.colorStage(0)->setEffect(GrSimpleTextureEffect::Create(srcTexture,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +00001919 matrix,
1920 true))->unref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001921 this->drawRectToRect(paint, dstRect, srcRect);
1922 srcRect = dstRect;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001923 srcTexture = dstTexture;
1924 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001925 }
1926
robertphillips@google.com7a396332012-05-10 15:11:27 +00001927 SkIRect srcIRect;
1928 srcRect.roundOut(&srcIRect);
1929
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001930 if (sigmaX > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001931 if (scaleFactorX > 1) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001932 // Clear out a radius to the right of the srcRect to prevent the
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001933 // X convolution from reading garbage.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001934 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001935 radiusX, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001936 this->clear(&clearRect, 0x0);
1937 }
1938
1939 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.com82b0ec62013-02-07 21:02:36 +00001940 GrDrawTarget* target = this->prepareToDraw(NULL, BUFFERED_DRAW);
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001941 convolve_gaussian(target, srcTexture, srcRect, sigmaX, radiusX,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001942 Gr1DKernelEffect::kX_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001943 srcTexture = dstTexture;
1944 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001945 }
1946
1947 if (sigmaY > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001948 if (scaleFactorY > 1 || sigmaX > 0.0f) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001949 // Clear out a radius below the srcRect to prevent the Y
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001950 // convolution from reading garbage.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001951 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001952 srcIRect.width(), radiusY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001953 this->clear(&clearRect, 0x0);
1954 }
1955
1956 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.com82b0ec62013-02-07 21:02:36 +00001957 GrDrawTarget* target = this->prepareToDraw(NULL, BUFFERED_DRAW);
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001958 convolve_gaussian(target, srcTexture, srcRect, sigmaY, radiusY,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001959 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001960 srcTexture = dstTexture;
1961 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001962 }
1963
1964 if (scaleFactorX > 1 || scaleFactorY > 1) {
1965 // Clear one pixel to the right and below, to accommodate bilinear
1966 // upsampling.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001967 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
robertphillips@google.com7a396332012-05-10 15:11:27 +00001968 srcIRect.width() + 1, 1);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001969 this->clear(&clearRect, 0x0);
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001970 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
robertphillips@google.com7a396332012-05-10 15:11:27 +00001971 1, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001972 this->clear(&clearRect, 0x0);
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001973 SkMatrix matrix;
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001974 // FIXME: This should be mitchell, not bilinear.
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +00001975 matrix.setIDiv(srcTexture->width(), srcTexture->height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001976 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.com68b58c92013-01-17 16:50:08 +00001977 paint.colorStage(0)->setEffect(GrSimpleTextureEffect::Create(srcTexture,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +00001978 matrix,
1979 true))->unref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001980 SkRect dstRect(srcRect);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001981 scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001982 this->drawRectToRect(paint, dstRect, srcRect);
1983 srcRect = dstRect;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001984 srcTexture = dstTexture;
1985 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001986 }
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001987 if (srcTexture == temp1.texture()) {
1988 return temp1.detach();
1989 } else if (srcTexture == temp2.texture()) {
1990 return temp2.detach();
1991 } else {
1992 srcTexture->ref();
1993 return srcTexture;
1994 }
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001995}
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001996
bsalomon@google.comc4364992011-11-07 15:54:49 +00001997///////////////////////////////////////////////////////////////////////////////
robertphillips@google.com59552022012-08-31 13:07:37 +00001998#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001999void GrContext::printCacheStats() const {
2000 fTextureCache->printStats();
2001}
2002#endif