blob: 271923aedf46178d69016babc7a778a51518d5ee [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
552///////////////////////////////////////////////////////////////////////////////
553
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000554GrTexture* GrContext::wrapBackendTexture(const GrBackendTextureDesc& desc) {
555 return fGpu->wrapBackendTexture(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000556}
557
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000558GrRenderTarget* GrContext::wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
559 return fGpu->wrapBackendRenderTarget(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000560}
561
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000562///////////////////////////////////////////////////////////////////////////////
563
bsalomon@google.comb8670992012-07-25 21:27:09 +0000564bool GrContext::supportsIndex8PixelConfig(const GrTextureParams* params,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000565 int width, int height) const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000566 const GrDrawTarget::Caps& caps = fGpu->getCaps();
bsalomon@google.comf6601872012-08-28 21:11:35 +0000567 if (!caps.eightBitPaletteSupport()) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000568 return false;
569 }
570
bsalomon@google.com27847de2011-02-22 20:59:41 +0000571 bool isPow2 = GrIsPow2(width) && GrIsPow2(height);
572
573 if (!isPow2) {
bsalomon@google.comb8670992012-07-25 21:27:09 +0000574 bool tiled = NULL != params && params->isTiled();
bsalomon@google.comf6601872012-08-28 21:11:35 +0000575 if (tiled && !caps.npotTextureTileSupport()) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000576 return false;
577 }
578 }
579 return true;
580}
581
582////////////////////////////////////////////////////////////////////////////////
583
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000584const GrClipData* GrContext::getClip() const {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000585 return fGpu->getClip();
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000586}
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000587
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000588void GrContext::setClip(const GrClipData* clipData) {
589 fGpu->setClip(clipData);
robertphillips@google.com837ec432012-10-04 17:57:05 +0000590
skia.committer@gmail.comf467ce72012-10-09 02:01:37 +0000591 fDrawState->setState(GrDrawState::kClip_StateBit,
robertphillips@google.com183e34b2012-10-19 18:19:01 +0000592 clipData && clipData->fClipStack && !clipData->fClipStack->isWideOpen());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000593}
594
bsalomon@google.com27847de2011-02-22 20:59:41 +0000595////////////////////////////////////////////////////////////////////////////////
596
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000597void GrContext::clear(const GrIRect* rect,
598 const GrColor color,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000599 GrRenderTarget* target) {
bsalomon@google.com82b0ec62013-02-07 21:02:36 +0000600 this->prepareToDraw(NULL, BUFFERED_DRAW)->clear(rect, color, target);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000601}
602
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000603void GrContext::drawPaint(const GrPaint& origPaint) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000604 // set rect to be big enough to fill the space, but not super-huge, so we
605 // don't overflow fixed-point implementations
bsalomon@google.comd302f142011-03-03 13:54:13 +0000606 GrRect r;
607 r.setLTRB(0, 0,
bsalomon@google.com81712882012-11-01 17:12:34 +0000608 SkIntToScalar(getRenderTarget()->width()),
609 SkIntToScalar(getRenderTarget()->height()));
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000610 SkMatrix inverse;
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000611 SkTCopyOnFirstWrite<GrPaint> paint(origPaint);
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000612 AutoMatrix am;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000613
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000614 // We attempt to map r by the inverse matrix and draw that. mapRect will
615 // map the four corners and bound them with a new rect. This will not
616 // produce a correct result for some perspective matrices.
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000617 if (!this->getMatrix().hasPerspective()) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000618 if (!fDrawState->getViewInverse(&inverse)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000619 GrPrintf("Could not invert matrix\n");
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000620 return;
621 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000622 inverse.mapRect(&r);
623 } else {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000624 if (!am.setIdentity(this, paint.writable())) {
625 GrPrintf("Could not invert matrix\n");
626 return;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000627 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000628 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000629 // by definition this fills the entire clip, no need for AA
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000630 if (paint->isAntiAlias()) {
631 paint.writable()->setAntiAlias(false);
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000632 }
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000633 this->drawRect(*paint, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000634}
635
bsalomon@google.com205d4602011-04-25 12:43:45 +0000636////////////////////////////////////////////////////////////////////////////////
637
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000638namespace {
639inline bool disable_coverage_aa_for_blend(GrDrawTarget* target) {
640 return DISABLE_COVERAGE_AA_FOR_BLEND && !target->canApplyCoverage();
641}
642}
643
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000644////////////////////////////////////////////////////////////////////////////////
645
bsalomon@google.com27847de2011-02-22 20:59:41 +0000646/* create a triangle strip that strokes the specified triangle. There are 8
647 unique vertices, but we repreat the last 2 to close up. Alternatively we
648 could use an indices array, and then only send 8 verts, but not sure that
649 would be faster.
650 */
bsalomon@google.com205d4602011-04-25 12:43:45 +0000651static void setStrokeRectStrip(GrPoint verts[10], GrRect rect,
bsalomon@google.com81712882012-11-01 17:12:34 +0000652 SkScalar width) {
653 const SkScalar rad = SkScalarHalf(width);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000654 rect.sort();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000655
656 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
657 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
658 verts[2].set(rect.fRight - rad, rect.fTop + rad);
659 verts[3].set(rect.fRight + rad, rect.fTop - rad);
660 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
661 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
662 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
663 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
664 verts[8] = verts[0];
665 verts[9] = verts[1];
666}
667
reed@google.com20efde72011-05-09 17:00:02 +0000668/**
669 * Returns true if the rects edges are integer-aligned.
670 */
671static bool isIRect(const GrRect& r) {
bsalomon@google.com81712882012-11-01 17:12:34 +0000672 return SkScalarIsInt(r.fLeft) && SkScalarIsInt(r.fTop) &&
673 SkScalarIsInt(r.fRight) && SkScalarIsInt(r.fBottom);
reed@google.com20efde72011-05-09 17:00:02 +0000674}
675
bsalomon@google.com205d4602011-04-25 12:43:45 +0000676static bool apply_aa_to_rect(GrDrawTarget* target,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000677 const GrRect& rect,
bsalomon@google.com81712882012-11-01 17:12:34 +0000678 SkScalar width,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000679 const SkMatrix* matrix,
680 SkMatrix* combinedMatrix,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000681 GrRect* devRect,
682 bool* useVertexCoverage) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000683 // we use a simple coverage ramp to do aa on axis-aligned rects
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000684 // we check if the rect will be axis-aligned, and the rect won't land on
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000685 // integer coords.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000686
bsalomon@google.coma3108262011-10-10 14:08:47 +0000687 // we are keeping around the "tweak the alpha" trick because
688 // it is our only hope for the fixed-pipe implementation.
689 // In a shader implementation we can give a separate coverage input
bsalomon@google.com289533a2011-10-27 12:34:25 +0000690 // TODO: remove this ugliness when we drop the fixed-pipe impl
bsalomon@google.coma3108262011-10-10 14:08:47 +0000691 *useVertexCoverage = false;
bsalomon@google.com2b446732013-02-12 16:47:41 +0000692 if (!target->getDrawState().canTweakAlphaForCoverage()) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000693 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +0000694#if GR_DEBUG
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000695 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +0000696#endif
bsalomon@google.coma3108262011-10-10 14:08:47 +0000697 return false;
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000698 } else {
699 *useVertexCoverage = true;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000700 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000701 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000702 const GrDrawState& drawState = target->getDrawState();
703 if (drawState.getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000704 return false;
705 }
706
bsalomon@google.com471d4712011-08-23 15:45:25 +0000707 if (0 == width && target->willUseHWAALines()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000708 return false;
709 }
710
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000711 if (!drawState.getViewMatrix().preservesAxisAlignment()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000712 return false;
713 }
714
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000715 if (NULL != matrix &&
bsalomon@google.com205d4602011-04-25 12:43:45 +0000716 !matrix->preservesAxisAlignment()) {
717 return false;
718 }
719
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000720 *combinedMatrix = drawState.getViewMatrix();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000721 if (NULL != matrix) {
722 combinedMatrix->preConcat(*matrix);
723 GrAssert(combinedMatrix->preservesAxisAlignment());
724 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000725
bsalomon@google.com205d4602011-04-25 12:43:45 +0000726 combinedMatrix->mapRect(devRect, rect);
727 devRect->sort();
728
729 if (width < 0) {
reed@google.com20efde72011-05-09 17:00:02 +0000730 return !isIRect(*devRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000731 } else {
732 return true;
733 }
734}
735
bsalomon@google.com27847de2011-02-22 20:59:41 +0000736void GrContext::drawRect(const GrPaint& paint,
737 const GrRect& rect,
bsalomon@google.com81712882012-11-01 17:12:34 +0000738 SkScalar width,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000739 const SkMatrix* matrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000740 SK_TRACE_EVENT0("GrContext::drawRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000741
bsalomon@google.com82b0ec62013-02-07 21:02:36 +0000742 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000743 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000744
bsalomon@google.com205d4602011-04-25 12:43:45 +0000745 GrRect devRect = rect;
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000746 SkMatrix combinedMatrix;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000747 bool useVertexCoverage;
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000748 bool needAA = paint.isAntiAlias() &&
bsalomon@google.com289533a2011-10-27 12:34:25 +0000749 !this->getRenderTarget()->isMultisampled();
750 bool doAA = needAA && apply_aa_to_rect(target, rect, width, matrix,
751 &combinedMatrix, &devRect,
752 &useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000753
754 if (doAA) {
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000755 GrDrawState::AutoDeviceCoordDraw adcd(target->drawState());
bsalomon@google.come3d32162012-07-20 13:37:06 +0000756 if (!adcd.succeeded()) {
757 return;
758 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000759 if (width >= 0) {
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000760 GrVec strokeSize;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000761 if (width > 0) {
762 strokeSize.set(width, width);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000763 combinedMatrix.mapVectors(&strokeSize, 1);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000764 strokeSize.setAbs(strokeSize);
765 } else {
bsalomon@google.com81712882012-11-01 17:12:34 +0000766 strokeSize.set(SK_Scalar1, SK_Scalar1);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000767 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000768 fAARectRenderer->strokeAARect(this->getGpu(), target, devRect,
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000769 strokeSize, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000770 } else {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000771 fAARectRenderer->fillAARect(this->getGpu(), target,
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000772 devRect, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000773 }
774 return;
775 }
776
bsalomon@google.com27847de2011-02-22 20:59:41 +0000777 if (width >= 0) {
778 // TODO: consider making static vertex buffers for these cases.
bsalomon@google.com64386952013-02-08 21:22:44 +0000779 // Hairline could be done by just adding closing vertex to
780 // unitSquareVertexBuffer()
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000781
bsalomon@google.com27847de2011-02-22 20:59:41 +0000782 static const int worstCaseVertCount = 10;
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000783 target->drawState()->setDefaultVertexAttribs();
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000784 GrDrawTarget::AutoReleaseGeometry geo(target, worstCaseVertCount, 0);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000785
786 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000787 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000788 return;
789 }
790
791 GrPrimitiveType primType;
792 int vertCount;
793 GrPoint* vertex = geo.positions();
794
795 if (width > 0) {
796 vertCount = 10;
bsalomon@google.com47059542012-06-06 20:51:20 +0000797 primType = kTriangleStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000798 setStrokeRectStrip(vertex, rect, width);
799 } else {
800 // hairline
801 vertCount = 5;
bsalomon@google.com47059542012-06-06 20:51:20 +0000802 primType = kLineStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000803 vertex[0].set(rect.fLeft, rect.fTop);
804 vertex[1].set(rect.fRight, rect.fTop);
805 vertex[2].set(rect.fRight, rect.fBottom);
806 vertex[3].set(rect.fLeft, rect.fBottom);
807 vertex[4].set(rect.fLeft, rect.fTop);
808 }
809
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000810 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000811 if (NULL != matrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000812 GrDrawState* drawState = target->drawState();
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000813 avmr.set(drawState, *matrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000814 }
815
816 target->drawNonIndexed(primType, 0, vertCount);
817 } else {
bsalomon@google.com64386952013-02-08 21:22:44 +0000818#if GR_STATIC_RECT_VB
819 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
820 if (NULL == sqVB) {
821 GrPrintf("Failed to create static rect vb.\n");
822 return;
823 }
824
825 GrDrawState* drawState = target->drawState();
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000826 target->drawState()->setDefaultVertexAttribs();
bsalomon@google.com64386952013-02-08 21:22:44 +0000827 target->setVertexSourceToBuffer(sqVB);
828 SkMatrix m;
829 m.setAll(rect.width(), 0, rect.fLeft,
830 0, rect.height(), rect.fTop,
831 0, 0, SkMatrix::I()[8]);
832
833 if (NULL != matrix) {
834 m.postConcat(*matrix);
835 }
836 GrDrawState::AutoViewMatrixRestore avmr(drawState, m);
837
838 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
839#else
840 target->drawSimpleRect(rect, matrix);
841#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +0000842 }
843}
844
845void GrContext::drawRectToRect(const GrPaint& paint,
846 const GrRect& dstRect,
847 const GrRect& srcRect,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000848 const SkMatrix* dstMatrix,
849 const SkMatrix* srcMatrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000850 SK_TRACE_EVENT0("GrContext::drawRectToRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000851
bsalomon@google.com88becf42012-10-05 14:54:42 +0000852 // srcRect refers to paint's first color stage
853 if (!paint.isColorStageEnabled(0)) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000854 drawRect(paint, dstRect, -1, dstMatrix);
855 return;
856 }
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000857
bsalomon@google.com82b0ec62013-02-07 21:02:36 +0000858 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW);
bsalomon@google.com64386952013-02-08 21:22:44 +0000859
860#if GR_STATIC_RECT_VB
861 GrDrawState::AutoStageDisable atr(fDrawState);
862 GrDrawState* drawState = target->drawState();
863
864 SkMatrix m;
865
866 m.setAll(dstRect.width(), 0, dstRect.fLeft,
867 0, dstRect.height(), dstRect.fTop,
868 0, 0, SkMatrix::I()[8]);
869 if (NULL != dstMatrix) {
870 m.postConcat(*dstMatrix);
871 }
872
873 // The first color stage's coords come from srcRect rather than applying a matrix to dstRect.
874 // We explicitly compute a matrix for that stage below, no need to adjust here.
875 static const uint32_t kExplicitCoordMask = 1 << GrPaint::kFirstColorStage;
876 GrDrawState::AutoViewMatrixRestore avmr(drawState, m, kExplicitCoordMask);
877
878 m.setAll(srcRect.width(), 0, srcRect.fLeft,
879 0, srcRect.height(), srcRect.fTop,
880 0, 0, SkMatrix::I()[8]);
881 if (NULL != srcMatrix) {
882 m.postConcat(*srcMatrix);
883 }
884
885 drawState->preConcatStageMatrices(kExplicitCoordMask, m);
886
887 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
888 if (NULL == sqVB) {
889 GrPrintf("Failed to create static rect vb.\n");
890 return;
891 }
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000892 drawState->setDefaultVertexAttribs();
bsalomon@google.com64386952013-02-08 21:22:44 +0000893 target->setVertexSourceToBuffer(sqVB);
894 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
895#else
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000896 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000897
jvanverth@google.com39768252013-02-14 15:25:44 +0000898 target->drawRect(dstRect, dstMatrix, &srcRect, srcMatrix, 0);
bsalomon@google.com64386952013-02-08 21:22:44 +0000899#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +0000900}
901
902void GrContext::drawVertices(const GrPaint& paint,
903 GrPrimitiveType primitiveType,
904 int vertexCount,
905 const GrPoint positions[],
906 const GrPoint texCoords[],
907 const GrColor colors[],
908 const uint16_t indices[],
909 int indexCount) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000910 SK_TRACE_EVENT0("GrContext::drawVertices");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000911
912 GrDrawTarget::AutoReleaseGeometry geo;
913
bsalomon@google.com82b0ec62013-02-07 21:02:36 +0000914 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000915 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000916
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000917 GrDrawState* drawState = target->drawState();
jvanverth@google.comb8b705b2013-02-28 16:28:34 +0000918
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000919 GrVertexAttribArray<3> attribs;
920 size_t currentOffset = 0;
921 int colorOffset = -1, texOffset = -1;
922 GrAttribBindings bindings = GrDrawState::kDefault_AttribBindings;
923
924 // set position attribute
925 drawState->setAttribIndex(GrDrawState::kPosition_AttribIndex, attribs.count());
jvanverth@google.com3b0d6312013-03-01 20:30:01 +0000926 GrVertexAttrib currAttrib = {kVec2f_GrVertexAttribType, currentOffset};
927 attribs.push_back(currAttrib);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000928 currentOffset += sizeof(GrPoint);
929
930 // set up optional texture coordinate attributes
931 if (NULL != texCoords) {
932 bindings |= GrDrawState::ExplicitTexCoordAttribBindingsBit(0);
933 drawState->setAttribIndex(GrDrawState::kTexCoord_AttribIndex, attribs.count());
jvanverth@google.com3b0d6312013-03-01 20:30:01 +0000934 currAttrib.set(kVec2f_GrVertexAttribType, currentOffset);
935 attribs.push_back(currAttrib);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000936 texOffset = currentOffset;
937 currentOffset += sizeof(GrPoint);
938 }
939
940 // set up optional color attributes
941 if (NULL != colors) {
942 bindings |= GrDrawState::kColor_AttribBindingsBit;
943 drawState->setAttribIndex(GrDrawState::kColor_AttribIndex, attribs.count());
jvanverth@google.com3b0d6312013-03-01 20:30:01 +0000944 currAttrib.set(kVec4ub_GrVertexAttribType, currentOffset);
945 attribs.push_back(currAttrib);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000946 colorOffset = currentOffset;
947 currentOffset += sizeof(GrColor);
948 }
949
950 drawState->setVertexAttribs(attribs.begin(), attribs.count());
951 drawState->setAttribBindings(bindings);
952
953 size_t vertexSize = drawState->getVertexSize();
954 GrAssert(vertexSize == currentOffset);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000955 if (sizeof(GrPoint) != vertexSize) {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000956 if (!geo.set(target, vertexCount, 0)) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000957 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000958 return;
959 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000960 void* curVertex = geo.vertices();
961
962 for (int i = 0; i < vertexCount; ++i) {
963 *((GrPoint*)curVertex) = positions[i];
964
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000965 if (texOffset >= 0) {
jvanverth@google.com39768252013-02-14 15:25:44 +0000966 *(GrPoint*)((intptr_t)curVertex + texOffset) = texCoords[i];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000967 }
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000968 if (colorOffset >= 0) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000969 *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i];
970 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000971 curVertex = (void*)((intptr_t)curVertex + vertexSize);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000972 }
973 } else {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000974 target->setVertexSourceToArray(positions, vertexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000975 }
976
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000977 // we don't currently apply offscreen AA to this path. Need improved
bsalomon@google.com91958362011-06-13 17:58:13 +0000978 // management of GrDrawTarget's geometry to avoid copying points per-tile.
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000979
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000980 if (NULL != indices) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000981 target->setIndexSourceToArray(indices, indexCount);
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000982 target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000983 } else {
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000984 target->drawNonIndexed(primitiveType, 0, vertexCount);
985 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000986}
987
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000988///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com150d2842012-01-12 20:19:56 +0000989namespace {
990
bsalomon@google.com93c96602012-04-27 13:05:21 +0000991struct CircleVertex {
992 GrPoint fPos;
993 GrPoint fCenter;
bsalomon@google.com81712882012-11-01 17:12:34 +0000994 SkScalar fOuterRadius;
995 SkScalar fInnerRadius;
bsalomon@google.com93c96602012-04-27 13:05:21 +0000996};
997
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000998inline bool circleStaysCircle(const SkMatrix& m) {
999 return m.isSimilarity();
bsalomon@google.com93c96602012-04-27 13:05:21 +00001000}
1001
1002}
1003
bsalomon@google.com93c96602012-04-27 13:05:21 +00001004void GrContext::drawOval(const GrPaint& paint,
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001005 const GrRect& oval,
1006 const SkStrokeRec& stroke) {
1007
1008 if (!canDrawOval(paint, oval, stroke)) {
bsalomon@google.com93c96602012-04-27 13:05:21 +00001009 SkPath path;
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001010 path.addOval(oval);
1011 this->drawPath(paint, path, stroke);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001012 return;
skia.committer@gmail.com98ded842013-01-23 07:06:17 +00001013 }
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001014
1015 internalDrawOval(paint, oval, stroke);
1016}
1017
1018bool GrContext::canDrawOval(const GrPaint& paint, const GrRect& oval, const SkStrokeRec& stroke) const {
1019
1020 if (!paint.isAntiAlias()) {
1021 return false;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001022 }
skia.committer@gmail.com98ded842013-01-23 07:06:17 +00001023
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001024 // we can draw circles in any style
skia.committer@gmail.com98ded842013-01-23 07:06:17 +00001025 bool isCircle = SkScalarNearlyEqual(oval.width(), oval.height())
1026 && circleStaysCircle(this->getMatrix());
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001027 // and for now, axis-aligned ellipses only with fill or stroke-and-fill
1028 SkStrokeRec::Style style = stroke.getStyle();
1029 bool isStroke = (style == SkStrokeRec::kStroke_Style || style == SkStrokeRec::kHairline_Style);
1030 bool isFilledAxisAlignedEllipse = this->getMatrix().rectStaysRect() && !isStroke;
1031
1032 return isCircle || isFilledAxisAlignedEllipse;
1033}
1034
1035void GrContext::internalDrawOval(const GrPaint& paint,
1036 const GrRect& oval,
1037 const SkStrokeRec& stroke) {
1038
1039 SkScalar xRadius = SkScalarHalf(oval.width());
1040 SkScalar yRadius = SkScalarHalf(oval.height());
skia.committer@gmail.com98ded842013-01-23 07:06:17 +00001041
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001042 SkScalar strokeWidth = stroke.getWidth();
1043 SkStrokeRec::Style style = stroke.getStyle();
1044
skia.committer@gmail.com98ded842013-01-23 07:06:17 +00001045 bool isCircle = SkScalarNearlyEqual(xRadius, yRadius) && circleStaysCircle(this->getMatrix());
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001046#ifdef SK_DEBUG
1047 {
1048 // we should have checked for this previously
1049 bool isStroke = (style == SkStrokeRec::kStroke_Style || style == SkStrokeRec::kHairline_Style);
1050 bool isFilledAxisAlignedEllipse = this->getMatrix().rectStaysRect() && !isStroke;
1051 SkASSERT(paint.isAntiAlias() && (isCircle || isFilledAxisAlignedEllipse));
1052 }
1053#endif
bsalomon@google.com93c96602012-04-27 13:05:21 +00001054
bsalomon@google.com82b0ec62013-02-07 21:02:36 +00001055 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW);
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001056
bsalomon@google.com0982d352012-07-31 15:33:25 +00001057 GrDrawState* drawState = target->drawState();
1058 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001059 const SkMatrix vm = drawState->getViewMatrix();
bsalomon@google.com0982d352012-07-31 15:33:25 +00001060
bsalomon@google.com93c96602012-04-27 13:05:21 +00001061 const GrRenderTarget* rt = drawState->getRenderTarget();
1062 if (NULL == rt) {
1063 return;
1064 }
1065
bsalomon@google.com5b3e8902012-10-05 20:13:28 +00001066 GrDrawState::AutoDeviceCoordDraw adcd(drawState);
bsalomon@google.come3d32162012-07-20 13:37:06 +00001067 if (!adcd.succeeded()) {
1068 return;
1069 }
bsalomon@google.com93c96602012-04-27 13:05:21 +00001070
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001071 // position + edge
1072 static const GrVertexAttrib kVertexAttribs[] = {
jvanverth@google.com3b0d6312013-03-01 20:30:01 +00001073 {kVec2f_GrVertexAttribType, 0},
1074 {kVec4f_GrVertexAttribType, sizeof(GrPoint)}
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001075 };
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001076 drawState->setVertexAttribs(kVertexAttribs, SK_ARRAY_COUNT(kVertexAttribs));
1077 drawState->setAttribIndex(GrDrawState::kPosition_AttribIndex, 0);
jvanverth@google.comb75b0a02013-02-05 20:33:30 +00001078 GrAssert(sizeof(CircleVertex) == drawState->getVertexSize());
bsalomon@google.com93c96602012-04-27 13:05:21 +00001079
jvanverth@google.comb75b0a02013-02-05 20:33:30 +00001080 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001081 if (!geo.succeeded()) {
1082 GrPrintf("Failed to get space for vertices!\n");
1083 return;
1084 }
1085
1086 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
1087
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001088 GrPoint center = GrPoint::Make(oval.centerX(), oval.centerY());
1089 vm.mapPoints(&center, 1);
1090
1091 SkScalar L;
1092 SkScalar R;
1093 SkScalar T;
1094 SkScalar B;
1095
1096 if (isCircle) {
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +00001097 drawState->setAttribBindings(GrDrawState::kEdge_AttribBindingsBit);
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001098 drawState->setVertexEdgeType(GrDrawState::kCircle_EdgeType);
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +00001099 drawState->setAttribIndex(GrDrawState::kEdge_AttribIndex, 1);
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001100
1101 xRadius = vm.mapRadius(xRadius);
1102
1103 SkScalar outerRadius = xRadius;
1104 SkScalar innerRadius = 0;
1105 SkScalar halfWidth = 0;
1106 if (style != SkStrokeRec::kFill_Style) {
1107 strokeWidth = vm.mapRadius(strokeWidth);
1108 if (SkScalarNearlyZero(strokeWidth)) {
1109 halfWidth = SK_ScalarHalf;
1110 } else {
1111 halfWidth = SkScalarHalf(strokeWidth);
1112 }
1113
1114 outerRadius += halfWidth;
1115 if (style == SkStrokeRec::kStroke_Style || style == SkStrokeRec::kHairline_Style) {
1116 innerRadius = SkMaxScalar(0, xRadius - halfWidth);
1117 }
1118 }
1119
1120 for (int i = 0; i < 4; ++i) {
1121 verts[i].fCenter = center;
1122 verts[i].fOuterRadius = outerRadius;
1123 verts[i].fInnerRadius = innerRadius;
1124 }
1125
1126 L = -outerRadius;
1127 R = +outerRadius;
1128 T = -outerRadius;
1129 B = +outerRadius;
1130 } else { // is axis-aligned ellipse
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +00001131 drawState->setAttribBindings(GrDrawState::kDefault_AttribBindings);
1132
1133 enum {
skia.committer@gmail.com91274b92013-03-13 07:01:04 +00001134 // the edge effects share this stage with glyph rendering
1135 // (kGlyphMaskStage in GrTextContext) && SW path rendering
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +00001136 // (kPathMaskStage in GrSWMaskHelper)
1137 kEdgeEffectStage = GrPaint::kTotalStages,
1138 };
1139 GrEffectRef* effect = GrEllipseEdgeEffect::Create();
1140 static const int kEdgeAttrIndex = 1;
1141 drawState->setEffect(kEdgeEffectStage, effect, &kEdgeAttrIndex)->unref();
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001142
1143 SkRect xformedRect;
1144 vm.mapRect(&xformedRect, oval);
1145
1146 xRadius = SkScalarHalf(xformedRect.width());
1147 yRadius = SkScalarHalf(xformedRect.height());
1148
1149 if (style == SkStrokeRec::kStrokeAndFill_Style && strokeWidth > 0.0f) {
1150 SkScalar halfWidth = SkScalarHalf(strokeWidth);
1151 // do (potentially) anisotropic mapping
1152 SkVector scaledStroke;
1153 scaledStroke.set(halfWidth, halfWidth);
1154 vm.mapVectors(&scaledStroke, 1);
1155 // this is legit only if scale & translation (which should be the case at the moment)
1156 xRadius += scaledStroke.fX;
1157 yRadius += scaledStroke.fY;
1158 }
1159
1160 SkScalar ratio = SkScalarDiv(xRadius, yRadius);
skia.committer@gmail.com98ded842013-01-23 07:06:17 +00001161
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001162 for (int i = 0; i < 4; ++i) {
1163 verts[i].fCenter = center;
1164 verts[i].fOuterRadius = xRadius;
1165 verts[i].fInnerRadius = ratio;
1166 }
1167
1168 L = -xRadius;
1169 R = +xRadius;
1170 T = -yRadius;
1171 B = +yRadius;
1172 }
1173
robertphillips@google.coma0a66c12012-06-22 13:14:29 +00001174 // The fragment shader will extend the radius out half a pixel
1175 // to antialias. Expand the drawn rect here so all the pixels
1176 // will be captured.
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001177 L += center.fX - SK_ScalarHalf;
1178 R += center.fX + SK_ScalarHalf;
1179 T += center.fY - SK_ScalarHalf;
1180 B += center.fY + SK_ScalarHalf;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001181
1182 verts[0].fPos = SkPoint::Make(L, T);
1183 verts[1].fPos = SkPoint::Make(R, T);
1184 verts[2].fPos = SkPoint::Make(L, B);
1185 verts[3].fPos = SkPoint::Make(R, B);
1186
bsalomon@google.com47059542012-06-06 20:51:20 +00001187 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4);
bsalomon@google.com150d2842012-01-12 20:19:56 +00001188}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001189
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001190void GrContext::drawPath(const GrPaint& paint, const SkPath& path, const SkStrokeRec& stroke) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001191
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001192 if (path.isEmpty()) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001193 if (path.isInverseFillType()) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001194 this->drawPaint(paint);
1195 }
1196 return;
1197 }
1198
bsalomon@google.com93c96602012-04-27 13:05:21 +00001199 SkRect ovalRect;
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001200 bool isOval = path.isOval(&ovalRect);
1201
1202 if (isOval && !path.isInverseFillType() && this->canDrawOval(paint, ovalRect, stroke)) {
1203 this->drawOval(paint, ovalRect, stroke);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001204 return;
1205 }
1206
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001207 this->internalDrawPath(paint, path, stroke);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001208}
1209
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001210void GrContext::internalDrawPath(const GrPaint& paint, const SkPath& path, const SkStrokeRec& stroke) {
bsalomon@google.com93c96602012-04-27 13:05:21 +00001211
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001212 // Note that below we may sw-rasterize the path into a scratch texture.
1213 // Scratch textures can be recycled after they are returned to the texture
1214 // cache. This presents a potential hazard for buffered drawing. However,
1215 // the writePixels that uploads to the scratch will perform a flush so we're
1216 // OK.
bsalomon@google.com82b0ec62013-02-07 21:02:36 +00001217 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +00001218 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001219
bsalomon@google.comc7448ce2012-10-05 19:04:13 +00001220 bool prAA = paint.isAntiAlias() && !this->getRenderTarget()->isMultisampled();
bsalomon@google.com289533a2011-10-27 12:34:25 +00001221
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001222 // An Assumption here is that path renderer would use some form of tweaking
1223 // the src color (either the input alpha or in the frag shader) to implement
1224 // aa. If we have some future driver-mojo path AA that can do the right
1225 // thing WRT to the blend then we'll need some query on the PR.
1226 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001227#if GR_DEBUG
bsalomon@google.com979432b2011-11-05 21:38:22 +00001228 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001229#endif
bsalomon@google.com289533a2011-10-27 12:34:25 +00001230 prAA = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001231 }
bsalomon@google.com289533a2011-10-27 12:34:25 +00001232
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001233 GrPathRendererChain::DrawType type = prAA ? GrPathRendererChain::kColorAntiAlias_DrawType :
1234 GrPathRendererChain::kColor_DrawType;
1235
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001236 const SkPath* pathPtr = &path;
1237 SkPath tmpPath;
1238 SkStrokeRec strokeRec(stroke);
1239
1240 // Try a 1st time without stroking the path and without allowing the SW renderer
1241 GrPathRenderer* pr = this->getPathRenderer(*pathPtr, strokeRec, target, false, type);
1242
1243 if (NULL == pr) {
1244 if (!strokeRec.isHairlineStyle()) {
1245 // It didn't work the 1st time, so try again with the stroked path
1246 if (strokeRec.applyToPath(&tmpPath, *pathPtr)) {
1247 pathPtr = &tmpPath;
1248 strokeRec.setFillStyle();
1249 }
1250 }
1251 // This time, allow SW renderer
1252 pr = this->getPathRenderer(*pathPtr, strokeRec, target, true, type);
1253 }
1254
bsalomon@google.com30085192011-08-19 15:42:31 +00001255 if (NULL == pr) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001256#if GR_DEBUG
bsalomon@google.com30085192011-08-19 15:42:31 +00001257 GrPrintf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001258#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001259 return;
1260 }
1261
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001262 pr->drawPath(*pathPtr, strokeRec, target, prAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001263}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001264
bsalomon@google.com27847de2011-02-22 20:59:41 +00001265////////////////////////////////////////////////////////////////////////////////
1266
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001267void GrContext::flush(int flagsBitfield) {
1268 if (kDiscard_FlushBit & flagsBitfield) {
1269 fDrawBuffer->reset();
1270 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001271 this->flushDrawBuffer();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001272 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001273 if (kForceCurrentRenderTarget_FlushBit & flagsBitfield) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001274 fGpu->forceRenderTargetFlush();
1275 }
1276}
1277
bsalomon@google.com27847de2011-02-22 20:59:41 +00001278void GrContext::flushDrawBuffer() {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001279 if (NULL != fDrawBuffer && !fDrawBuffer->isFlushing()) {
1280 fDrawBuffer->flush();
junov@google.com53a55842011-06-08 22:55:10 +00001281 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001282}
1283
bsalomon@google.com9c680582013-02-06 18:17:50 +00001284bool GrContext::writeTexturePixels(GrTexture* texture,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001285 int left, int top, int width, int height,
1286 GrPixelConfig config, const void* buffer, size_t rowBytes,
1287 uint32_t flags) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001288 SK_TRACE_EVENT0("GrContext::writeTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001289 ASSERT_OWNED_RESOURCE(texture);
1290
bsalomon@google.com9c680582013-02-06 18:17:50 +00001291 if ((kUnpremul_PixelOpsFlag & flags) || !fGpu->canWriteTexturePixels(texture, config)) {
1292 if (NULL != texture->asRenderTarget()) {
1293 return this->writeRenderTargetPixels(texture->asRenderTarget(),
1294 left, top, width, height,
1295 config, buffer, rowBytes, flags);
1296 } else {
1297 return false;
1298 }
bsalomon@google.com0342a852012-08-20 19:22:38 +00001299 }
bsalomon@google.com9c680582013-02-06 18:17:50 +00001300
bsalomon@google.com6f379512011-11-16 20:36:03 +00001301 if (!(kDontFlush_PixelOpsFlag & flags)) {
1302 this->flush();
1303 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001304
bsalomon@google.com9c680582013-02-06 18:17:50 +00001305 return fGpu->writeTexturePixels(texture, left, top, width, height,
1306 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +00001307}
1308
bsalomon@google.com0342a852012-08-20 19:22:38 +00001309bool GrContext::readTexturePixels(GrTexture* texture,
1310 int left, int top, int width, int height,
1311 GrPixelConfig config, void* buffer, size_t rowBytes,
1312 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001313 SK_TRACE_EVENT0("GrContext::readTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001314 ASSERT_OWNED_RESOURCE(texture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001315
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001316 // TODO: code read pixels for textures that aren't also rendertargets
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001317 GrRenderTarget* target = texture->asRenderTarget();
1318 if (NULL != target) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00001319 return this->readRenderTargetPixels(target,
1320 left, top, width, height,
1321 config, buffer, rowBytes,
1322 flags);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001323 } else {
1324 return false;
1325 }
1326}
1327
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001328#include "SkConfig8888.h"
1329
1330namespace {
1331/**
1332 * Converts a GrPixelConfig to a SkCanvas::Config8888. Only byte-per-channel
1333 * formats are representable as Config8888 and so the function returns false
1334 * if the GrPixelConfig has no equivalent Config8888.
1335 */
1336bool grconfig_to_config8888(GrPixelConfig config,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001337 bool unpremul,
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001338 SkCanvas::Config8888* config8888) {
1339 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00001340 case kRGBA_8888_GrPixelConfig:
1341 if (unpremul) {
1342 *config8888 = SkCanvas::kRGBA_Unpremul_Config8888;
1343 } else {
1344 *config8888 = SkCanvas::kRGBA_Premul_Config8888;
1345 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001346 return true;
bsalomon@google.com0342a852012-08-20 19:22:38 +00001347 case kBGRA_8888_GrPixelConfig:
1348 if (unpremul) {
1349 *config8888 = SkCanvas::kBGRA_Unpremul_Config8888;
1350 } else {
1351 *config8888 = SkCanvas::kBGRA_Premul_Config8888;
1352 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001353 return true;
1354 default:
1355 return false;
1356 }
1357}
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001358
1359// It returns a configuration with where the byte position of the R & B components are swapped in
1360// relation to the input config. This should only be called with the result of
1361// grconfig_to_config8888 as it will fail for other configs.
1362SkCanvas::Config8888 swap_config8888_red_and_blue(SkCanvas::Config8888 config8888) {
1363 switch (config8888) {
1364 case SkCanvas::kBGRA_Premul_Config8888:
1365 return SkCanvas::kRGBA_Premul_Config8888;
1366 case SkCanvas::kBGRA_Unpremul_Config8888:
1367 return SkCanvas::kRGBA_Unpremul_Config8888;
1368 case SkCanvas::kRGBA_Premul_Config8888:
1369 return SkCanvas::kBGRA_Premul_Config8888;
1370 case SkCanvas::kRGBA_Unpremul_Config8888:
1371 return SkCanvas::kBGRA_Unpremul_Config8888;
1372 default:
1373 GrCrash("Unexpected input");
1374 return SkCanvas::kBGRA_Unpremul_Config8888;;
1375 }
1376}
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001377}
1378
bsalomon@google.com0342a852012-08-20 19:22:38 +00001379bool GrContext::readRenderTargetPixels(GrRenderTarget* target,
1380 int left, int top, int width, int height,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001381 GrPixelConfig dstConfig, void* buffer, size_t rowBytes,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001382 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001383 SK_TRACE_EVENT0("GrContext::readRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001384 ASSERT_OWNED_RESOURCE(target);
1385
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001386 if (NULL == target) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001387 target = fDrawState->getRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001388 if (NULL == target) {
1389 return false;
1390 }
1391 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001392
bsalomon@google.com6f379512011-11-16 20:36:03 +00001393 if (!(kDontFlush_PixelOpsFlag & flags)) {
1394 this->flush();
1395 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001396
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001397 // Determine which conversions have to be applied: flipY, swapRAnd, and/or unpremul.
bsalomon@google.com0342a852012-08-20 19:22:38 +00001398
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001399 // If fGpu->readPixels would incur a y-flip cost then we will read the pixels upside down. We'll
1400 // either do the flipY by drawing into a scratch with a matrix or on the cpu after the read.
1401 bool flipY = fGpu->readPixelsWillPayForYFlip(target, left, top,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001402 width, height, dstConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001403 rowBytes);
bsalomon@google.com9c680582013-02-06 18:17:50 +00001404 // We ignore the preferred config if it is different than our config unless it is an R/B swap.
1405 // In that case we'll perform an R and B swap while drawing to a scratch texture of the swapped
1406 // config. Then we will call readPixels on the scratch with the swapped config. The swaps during
1407 // the draw cancels out the fact that we call readPixels with a config that is R/B swapped from
1408 // dstConfig.
1409 GrPixelConfig readConfig = dstConfig;
1410 bool swapRAndB = false;
1411 if (GrPixelConfigSwapRAndB(dstConfig) == fGpu->preferredReadPixelsConfig(dstConfig)) {
1412 readConfig = GrPixelConfigSwapRAndB(readConfig);
1413 swapRAndB = true;
1414 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001415
bsalomon@google.com0342a852012-08-20 19:22:38 +00001416 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001417
bsalomon@google.com9c680582013-02-06 18:17:50 +00001418 if (unpremul && !GrPixelConfigIs8888(dstConfig)) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001419 // The unpremul flag is only allowed for these two configs.
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001420 return false;
1421 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001422
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001423 // If the src is a texture and we would have to do conversions after read pixels, we instead
1424 // do the conversions by drawing the src to a scratch texture. If we handle any of the
1425 // conversions in the draw we set the corresponding bool to false so that we don't reapply it
1426 // on the read back pixels.
1427 GrTexture* src = target->asTexture();
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001428 GrAutoScratchTexture ast;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001429 if (NULL != src && (swapRAndB || unpremul || flipY)) {
1430 // Make the scratch a render target because we don't have a robust readTexturePixels as of
1431 // yet. It calls this function.
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001432 GrTextureDesc desc;
1433 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1434 desc.fWidth = width;
1435 desc.fHeight = height;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001436 desc.fConfig = readConfig;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001437 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001438
bsalomon@google.com9c680582013-02-06 18:17:50 +00001439 // 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 +00001440 // match the passed rect. However, if we see many different size rectangles we will trash
1441 // our texture cache and pay the cost of creating and destroying many textures. So, we only
1442 // request an exact match when the caller is reading an entire RT.
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001443 ScratchTexMatch match = kApprox_ScratchTexMatch;
1444 if (0 == left &&
1445 0 == top &&
1446 target->width() == width &&
1447 target->height() == height &&
1448 fGpu->fullReadPixelsIsFasterThanPartial()) {
1449 match = kExact_ScratchTexMatch;
1450 }
1451 ast.set(this, desc, match);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001452 GrTexture* texture = ast.texture();
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001453 if (texture) {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001454 // compute a matrix to perform the draw
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001455 SkMatrix textureMatrix;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001456 textureMatrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001457 textureMatrix.postIDiv(src->width(), src->height());
1458
bsalomon@google.comadc65362013-01-28 14:26:09 +00001459 SkAutoTUnref<const GrEffectRef> effect;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001460 if (unpremul) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001461 effect.reset(this->createPMToUPMEffect(src, swapRAndB, textureMatrix));
1462 if (NULL != effect) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001463 unpremul = false; // we no longer need to do this on CPU after the read back.
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001464 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001465 }
1466 // If we failed to create a PM->UPM effect and have no other conversions to perform then
1467 // there is no longer any point to using the scratch.
bsalomon@google.comadc65362013-01-28 14:26:09 +00001468 if (NULL != effect || flipY || swapRAndB) {
1469 if (!effect) {
1470 effect.reset(GrConfigConversionEffect::Create(
1471 src,
1472 swapRAndB,
1473 GrConfigConversionEffect::kNone_PMConversion,
1474 textureMatrix));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001475 }
1476 swapRAndB = false; // we will handle the swap in the draw.
bsalomon@google.comc4364992011-11-07 15:54:49 +00001477
robertphillips@google.com13f181f2013-03-02 12:02:08 +00001478 // We protect the existing geometry here since it may not be
1479 // clear to the caller that a draw operation (i.e., drawSimpleRect)
1480 // can be invoked in this method
1481 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001482 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.comadc65362013-01-28 14:26:09 +00001483 GrAssert(effect);
bsalomon@google.com9e040ae2013-01-28 14:31:19 +00001484 drawState->setEffect(0, effect);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001485
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001486 drawState->setRenderTarget(texture->asRenderTarget());
bsalomon@google.com81712882012-11-01 17:12:34 +00001487 GrRect rect = GrRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001488 fGpu->drawSimpleRect(rect, NULL);
1489 // we want to read back from the scratch's origin
1490 left = 0;
1491 top = 0;
1492 target = texture->asRenderTarget();
1493 }
bsalomon@google.com0342a852012-08-20 19:22:38 +00001494 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001495 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001496 if (!fGpu->readPixels(target,
1497 left, top, width, height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001498 readConfig, buffer, rowBytes)) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001499 return false;
1500 }
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001501 // Perform any conversions we weren't able to perform using a scratch texture.
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001502 if (unpremul || swapRAndB) {
bsalomon@google.comccaa0022012-09-25 19:55:07 +00001503 // These are initialized to suppress a warning
1504 SkCanvas::Config8888 srcC8888 = SkCanvas::kNative_Premul_Config8888;
1505 SkCanvas::Config8888 dstC8888 = SkCanvas::kNative_Premul_Config8888;
1506
bsalomon@google.com9c680582013-02-06 18:17:50 +00001507 SkDEBUGCODE(bool c8888IsValid =) grconfig_to_config8888(dstConfig, false, &srcC8888);
1508 grconfig_to_config8888(dstConfig, unpremul, &dstC8888);
bsalomon@google.comccaa0022012-09-25 19:55:07 +00001509
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001510 if (swapRAndB) {
1511 GrAssert(c8888IsValid); // we should only do r/b swap on 8888 configs
1512 srcC8888 = swap_config8888_red_and_blue(srcC8888);
1513 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001514 GrAssert(c8888IsValid);
1515 uint32_t* b32 = reinterpret_cast<uint32_t*>(buffer);
1516 SkConvertConfig8888Pixels(b32, rowBytes, dstC8888,
1517 b32, rowBytes, srcC8888,
1518 width, height);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001519 }
1520 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001521}
1522
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001523void GrContext::resolveRenderTarget(GrRenderTarget* target) {
1524 GrAssert(target);
1525 ASSERT_OWNED_RESOURCE(target);
1526 // In the future we may track whether there are any pending draws to this
1527 // target. We don't today so we always perform a flush. We don't promise
1528 // this to our clients, though.
1529 this->flush();
1530 fGpu->resolveRenderTarget(target);
1531}
1532
scroggo@google.coma2a31922012-12-07 19:14:45 +00001533void GrContext::copyTexture(GrTexture* src, GrRenderTarget* dst, const SkIPoint* topLeft) {
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001534 if (NULL == src || NULL == dst) {
1535 return;
1536 }
1537 ASSERT_OWNED_RESOURCE(src);
1538
twiz@google.com1ac87ff2012-04-27 19:39:33 +00001539 // Writes pending to the source texture are not tracked, so a flush
1540 // is required to ensure that the copy captures the most recent contents
bsalomon@google.comadc65362013-01-28 14:26:09 +00001541 // of the source texture. See similar behavior in
twiz@google.com1ac87ff2012-04-27 19:39:33 +00001542 // GrContext::resolveRenderTarget.
1543 this->flush();
1544
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001545 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001546 GrDrawState* drawState = fGpu->drawState();
1547 drawState->setRenderTarget(dst);
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001548 SkMatrix sampleM;
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001549 sampleM.setIDiv(src->width(), src->height());
scroggo@google.coma2a31922012-12-07 19:14:45 +00001550 SkIRect srcRect = SkIRect::MakeWH(dst->width(), dst->height());
1551 if (NULL != topLeft) {
1552 srcRect.offset(*topLeft);
1553 }
1554 SkIRect srcBounds = SkIRect::MakeWH(src->width(), src->height());
1555 if (!srcRect.intersect(srcBounds)) {
1556 return;
1557 }
1558 sampleM.preTranslate(SkIntToScalar(srcRect.fLeft), SkIntToScalar(srcRect.fTop));
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +00001559 drawState->createTextureEffect(0, src, sampleM);
scroggo@google.coma2a31922012-12-07 19:14:45 +00001560 SkRect dstR = SkRect::MakeWH(SkIntToScalar(srcRect.width()), SkIntToScalar(srcRect.height()));
1561 fGpu->drawSimpleRect(dstR, NULL);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001562}
1563
bsalomon@google.com9c680582013-02-06 18:17:50 +00001564bool GrContext::writeRenderTargetPixels(GrRenderTarget* target,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001565 int left, int top, int width, int height,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001566 GrPixelConfig srcConfig,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001567 const void* buffer,
1568 size_t rowBytes,
1569 uint32_t flags) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001570 SK_TRACE_EVENT0("GrContext::writeRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001571 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com6f379512011-11-16 20:36:03 +00001572
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001573 if (NULL == target) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001574 target = fDrawState->getRenderTarget();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001575 if (NULL == target) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001576 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +00001577 }
1578 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001579
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001580 // TODO: when underlying api has a direct way to do this we should use it (e.g. glDrawPixels on
1581 // desktop GL).
1582
1583 // We will always call some form of writeTexturePixels and we will pass our flags on to it.
1584 // Thus, we don't perform a flush here since that call will do it (if the kNoFlush flag isn't
1585 // set.)
bsalomon@google.com27847de2011-02-22 20:59:41 +00001586
bsalomon@google.com0342a852012-08-20 19:22:38 +00001587 // If the RT is also a texture and we don't have to premultiply then take the texture path.
1588 // We expect to be at least as fast or faster since it doesn't use an intermediate texture as
1589 // we do below.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001590
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001591#if !GR_MAC_BUILD
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001592 // At least some drivers on the Mac get confused when glTexImage2D is called on a texture
1593 // attached to an FBO. The FBO still sees the old image. TODO: determine what OS versions and/or
1594 // HW is affected.
bsalomon@google.com9c680582013-02-06 18:17:50 +00001595 if (NULL != target->asTexture() && !(kUnpremul_PixelOpsFlag & flags) &&
1596 fGpu->canWriteTexturePixels(target->asTexture(), srcConfig)) {
1597 return this->writeTexturePixels(target->asTexture(),
1598 left, top, width, height,
1599 srcConfig, buffer, rowBytes, flags);
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001600 }
1601#endif
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001602
bsalomon@google.com9c680582013-02-06 18:17:50 +00001603 // We ignore the preferred config unless it is a R/B swap of the src config. In that case
1604 // we will upload the original src data to a scratch texture but we will spoof it as the swapped
1605 // config. This scratch will then have R and B swapped. We correct for this by swapping again
1606 // when drawing the scratch to the dst using a conversion effect.
1607 bool swapRAndB = false;
1608 GrPixelConfig writeConfig = srcConfig;
1609 if (fGpu->preferredWritePixelsConfig(srcConfig) == GrPixelConfigSwapRAndB(srcConfig)) {
1610 writeConfig = GrPixelConfigSwapRAndB(srcConfig);
1611 swapRAndB = true;
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001612 }
1613
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001614 GrTextureDesc desc;
1615 desc.fWidth = width;
1616 desc.fHeight = height;
bsalomon@google.com9c680582013-02-06 18:17:50 +00001617 desc.fConfig = writeConfig;
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001618 GrAutoScratchTexture ast(this, desc);
1619 GrTexture* texture = ast.texture();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001620 if (NULL == texture) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001621 return false;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001622 }
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001623
bsalomon@google.comadc65362013-01-28 14:26:09 +00001624 SkAutoTUnref<const GrEffectRef> effect;
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001625 SkMatrix textureMatrix;
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001626 textureMatrix.setIDiv(texture->width(), texture->height());
1627
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001628 // allocate a tmp buffer and sw convert the pixels to premul
1629 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
1630
1631 if (kUnpremul_PixelOpsFlag & flags) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001632 if (!GrPixelConfigIs8888(srcConfig)) {
1633 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001634 }
bsalomon@google.comadc65362013-01-28 14:26:09 +00001635 effect.reset(this->createUPMToPMEffect(texture, swapRAndB, textureMatrix));
bsalomon@google.com9c680582013-02-06 18:17:50 +00001636 // 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 +00001637 if (NULL == effect) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001638 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1639 GR_DEBUGCODE(bool success = )
bsalomon@google.com9c680582013-02-06 18:17:50 +00001640 grconfig_to_config8888(srcConfig, true, &srcConfig8888);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001641 GrAssert(success);
1642 GR_DEBUGCODE(success = )
bsalomon@google.com9c680582013-02-06 18:17:50 +00001643 grconfig_to_config8888(srcConfig, false, &dstConfig8888);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001644 GrAssert(success);
1645 const uint32_t* src = reinterpret_cast<const uint32_t*>(buffer);
1646 tmpPixels.reset(width * height);
1647 SkConvertConfig8888Pixels(tmpPixels.get(), 4 * width, dstConfig8888,
1648 src, rowBytes, srcConfig8888,
1649 width, height);
1650 buffer = tmpPixels.get();
1651 rowBytes = 4 * width;
1652 }
1653 }
bsalomon@google.comadc65362013-01-28 14:26:09 +00001654 if (NULL == effect) {
1655 effect.reset(GrConfigConversionEffect::Create(texture,
1656 swapRAndB,
1657 GrConfigConversionEffect::kNone_PMConversion,
1658 textureMatrix));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001659 }
1660
bsalomon@google.com9c680582013-02-06 18:17:50 +00001661 if (!this->writeTexturePixels(texture,
1662 0, 0, width, height,
1663 writeConfig, buffer, rowBytes,
1664 flags & ~kUnpremul_PixelOpsFlag)) {
1665 return false;
1666 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001667
robertphillips@google.com13f181f2013-03-02 12:02:08 +00001668 // writeRenderTargetPixels can be called in the midst of drawing another
skia.committer@gmail.com0c23faf2013-03-03 07:16:29 +00001669 // object (e.g., when uploading a SW path rendering to the gpu while
robertphillips@google.com13f181f2013-03-02 12:02:08 +00001670 // drawing a rect) so preserve the current geometry.
1671 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001672 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.comadc65362013-01-28 14:26:09 +00001673 GrAssert(effect);
bsalomon@google.com9e040ae2013-01-28 14:31:19 +00001674 drawState->setEffect(0, effect);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001675
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001676 SkMatrix matrix;
bsalomon@google.com81712882012-11-01 17:12:34 +00001677 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001678 drawState->setViewMatrix(matrix);
1679 drawState->setRenderTarget(target);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001680
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001681 fGpu->drawSimpleRect(GrRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)), NULL);
bsalomon@google.com9c680582013-02-06 18:17:50 +00001682 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001683}
1684////////////////////////////////////////////////////////////////////////////////
1685
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001686GrDrawTarget* GrContext::prepareToDraw(const GrPaint* paint, BufferedDraw buffered) {
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001687 if (kNo_BufferedDraw == buffered && kYes_BufferedDraw == fLastDrawWasBuffered) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001688 this->flushDrawBuffer();
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001689 fLastDrawWasBuffered = kNo_BufferedDraw;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001690 }
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001691 if (NULL != paint) {
bsalomon@google.comaf84e742012-10-05 13:23:24 +00001692 GrAssert(fDrawState->stagesDisabled());
1693 fDrawState->setFromPaint(*paint);
1694#if GR_DEBUG_PARTIAL_COVERAGE_CHECK
1695 if ((paint->hasMask() || 0xff != paint->fCoverage) &&
1696 !fGpu->canApplyCoverage()) {
1697 GrPrintf("Partial pixel coverage will be incorrectly blended.\n");
1698 }
1699#endif
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001700 }
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001701 if (kYes_BufferedDraw == buffered) {
1702 fDrawBuffer->setClip(fGpu->getClip());
1703 fLastDrawWasBuffered = kYes_BufferedDraw;
1704 return fDrawBuffer;
1705 } else {
1706 GrAssert(kNo_BufferedDraw == buffered);
1707 return fGpu;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001708 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001709}
1710
robertphillips@google.com72176b22012-05-23 13:19:12 +00001711/*
1712 * This method finds a path renderer that can draw the specified path on
1713 * the provided target.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001714 * Due to its expense, the software path renderer has split out so it can
robertphillips@google.com72176b22012-05-23 13:19:12 +00001715 * can be individually allowed/disallowed via the "allowSW" boolean.
1716 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001717GrPathRenderer* GrContext::getPathRenderer(const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001718 const SkStrokeRec& stroke,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001719 const GrDrawTarget* target,
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001720 bool allowSW,
1721 GrPathRendererChain::DrawType drawType,
1722 GrPathRendererChain::StencilSupport* stencilSupport) {
1723
bsalomon@google.com30085192011-08-19 15:42:31 +00001724 if (NULL == fPathRendererChain) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001725 fPathRendererChain = SkNEW_ARGS(GrPathRendererChain, (this));
bsalomon@google.com30085192011-08-19 15:42:31 +00001726 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001727
sugoi@google.com12b4e272012-12-06 20:13:11 +00001728 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(path,
1729 stroke,
robertphillips@google.com72176b22012-05-23 13:19:12 +00001730 target,
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001731 drawType,
1732 stencilSupport);
robertphillips@google.com72176b22012-05-23 13:19:12 +00001733
1734 if (NULL == pr && allowSW) {
1735 if (NULL == fSoftwarePathRenderer) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001736 fSoftwarePathRenderer = SkNEW_ARGS(GrSoftwarePathRenderer, (this));
robertphillips@google.com72176b22012-05-23 13:19:12 +00001737 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001738 pr = fSoftwarePathRenderer;
1739 }
1740
1741 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001742}
1743
bsalomon@google.com27847de2011-02-22 20:59:41 +00001744////////////////////////////////////////////////////////////////////////////////
1745
bsalomon@google.com27847de2011-02-22 20:59:41 +00001746void GrContext::setRenderTarget(GrRenderTarget* target) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001747 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001748 fDrawState->setRenderTarget(target);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001749}
1750
1751GrRenderTarget* GrContext::getRenderTarget() {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001752 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001753}
1754
1755const GrRenderTarget* GrContext::getRenderTarget() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001756 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001757}
1758
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001759bool GrContext::isConfigRenderable(GrPixelConfig config) const {
1760 return fGpu->isConfigRenderable(config);
1761}
1762
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001763const SkMatrix& GrContext::getMatrix() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001764 return fDrawState->getViewMatrix();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001765}
1766
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001767void GrContext::setMatrix(const SkMatrix& m) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001768 fDrawState->setViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001769}
1770
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +00001771void GrContext::setIdentityMatrix() {
1772 fDrawState->viewMatrix()->reset();
1773}
1774
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001775void GrContext::concatMatrix(const SkMatrix& m) const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001776 fDrawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001777}
1778
1779static inline intptr_t setOrClear(intptr_t bits, int shift, intptr_t pred) {
1780 intptr_t mask = 1 << shift;
1781 if (pred) {
1782 bits |= mask;
1783 } else {
1784 bits &= ~mask;
1785 }
1786 return bits;
1787}
1788
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001789void GrContext::setupDrawBuffer() {
1790
1791 GrAssert(NULL == fDrawBuffer);
1792 GrAssert(NULL == fDrawBufferVBAllocPool);
1793 GrAssert(NULL == fDrawBufferIBAllocPool);
1794
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001795 fDrawBufferVBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001796 SkNEW_ARGS(GrVertexBufferAllocPool, (fGpu, false,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001797 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001798 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS));
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001799 fDrawBufferIBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001800 SkNEW_ARGS(GrIndexBufferAllocPool, (fGpu, false,
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001801 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001802 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001803
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001804 fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (fGpu,
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001805 fDrawBufferVBAllocPool,
1806 fDrawBufferIBAllocPool));
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001807
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001808 fDrawBuffer->setDrawState(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001809}
1810
bsalomon@google.com27847de2011-02-22 20:59:41 +00001811GrDrawTarget* GrContext::getTextTarget(const GrPaint& paint) {
bsalomon@google.com82b0ec62013-02-07 21:02:36 +00001812 return this->prepareToDraw(&paint, BUFFERED_DRAW);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001813}
1814
1815const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {
1816 return fGpu->getQuadIndexBuffer();
1817}
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001818
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001819namespace {
1820void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
1821 GrConfigConversionEffect::PMConversion pmToUPM;
1822 GrConfigConversionEffect::PMConversion upmToPM;
1823 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM);
1824 *pmToUPMValue = pmToUPM;
1825 *upmToPMValue = upmToPM;
1826}
1827}
1828
bsalomon@google.comadc65362013-01-28 14:26:09 +00001829const GrEffectRef* GrContext::createPMToUPMEffect(GrTexture* texture,
1830 bool swapRAndB,
1831 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001832 if (!fDidTestPMConversions) {
1833 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001834 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001835 }
1836 GrConfigConversionEffect::PMConversion pmToUPM =
1837 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
1838 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001839 return GrConfigConversionEffect::Create(texture, swapRAndB, pmToUPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001840 } else {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001841 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001842 }
1843}
1844
bsalomon@google.comadc65362013-01-28 14:26:09 +00001845const GrEffectRef* GrContext::createUPMToPMEffect(GrTexture* texture,
1846 bool swapRAndB,
1847 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001848 if (!fDidTestPMConversions) {
1849 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001850 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001851 }
1852 GrConfigConversionEffect::PMConversion upmToPM =
1853 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion);
1854 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001855 return GrConfigConversionEffect::Create(texture, swapRAndB, upmToPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001856 } else {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001857 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001858 }
1859}
1860
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001861GrTexture* GrContext::gaussianBlur(GrTexture* srcTexture,
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001862 bool canClobberSrc,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001863 const SkRect& rect,
1864 float sigmaX, float sigmaY) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001865 ASSERT_OWNED_RESOURCE(srcTexture);
robertphillips@google.comccb39502012-10-01 18:25:13 +00001866
1867 AutoRenderTarget art(this);
1868
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001869 AutoMatrix am;
1870 am.setIdentity(this);
1871
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001872 SkIRect clearRect;
bsalomon@google.comb505a122012-05-31 18:40:36 +00001873 int scaleFactorX, radiusX;
1874 int scaleFactorY, radiusY;
1875 sigmaX = adjust_sigma(sigmaX, &scaleFactorX, &radiusX);
1876 sigmaY = adjust_sigma(sigmaY, &scaleFactorY, &radiusY);
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001877
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001878 SkRect srcRect(rect);
1879 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
1880 srcRect.roundOut();
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001881 scale_rect(&srcRect, static_cast<float>(scaleFactorX),
robertphillips@google.com8637a362012-04-10 18:32:35 +00001882 static_cast<float>(scaleFactorY));
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +00001883
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001884 AutoClip acs(this, srcRect);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001885
bsalomon@google.com0342a852012-08-20 19:22:38 +00001886 GrAssert(kBGRA_8888_GrPixelConfig == srcTexture->config() ||
1887 kRGBA_8888_GrPixelConfig == srcTexture->config() ||
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001888 kAlpha_8_GrPixelConfig == srcTexture->config());
1889
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001890 GrTextureDesc desc;
1891 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
1892 desc.fWidth = SkScalarFloorToInt(srcRect.width());
1893 desc.fHeight = SkScalarFloorToInt(srcRect.height());
1894 desc.fConfig = srcTexture->config();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001895
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001896 GrAutoScratchTexture temp1, temp2;
1897 GrTexture* dstTexture = temp1.set(this, desc);
1898 GrTexture* tempTexture = canClobberSrc ? srcTexture : temp2.set(this, desc);
robertphillips@google.com7d126752012-10-19 12:56:26 +00001899 if (NULL == dstTexture || NULL == tempTexture) {
1900 return NULL;
1901 }
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001902
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001903 GrPaint paint;
1904 paint.reset();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001905
1906 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001907 SkMatrix matrix;
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +00001908 matrix.setIDiv(srcTexture->width(), srcTexture->height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001909 this->setRenderTarget(dstTexture->asRenderTarget());
1910 SkRect dstRect(srcRect);
1911 scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +00001912 i < scaleFactorY ? 0.5f : 1.0f);
1913
bsalomon@google.com68b58c92013-01-17 16:50:08 +00001914 paint.colorStage(0)->setEffect(GrSimpleTextureEffect::Create(srcTexture,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +00001915 matrix,
1916 true))->unref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001917 this->drawRectToRect(paint, dstRect, srcRect);
1918 srcRect = dstRect;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001919 srcTexture = dstTexture;
1920 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001921 }
1922
robertphillips@google.com7a396332012-05-10 15:11:27 +00001923 SkIRect srcIRect;
1924 srcRect.roundOut(&srcIRect);
1925
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001926 if (sigmaX > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001927 if (scaleFactorX > 1) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001928 // Clear out a radius to the right of the srcRect to prevent the
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001929 // X convolution from reading garbage.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001930 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001931 radiusX, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001932 this->clear(&clearRect, 0x0);
1933 }
1934
1935 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.com82b0ec62013-02-07 21:02:36 +00001936 GrDrawTarget* target = this->prepareToDraw(NULL, BUFFERED_DRAW);
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001937 convolve_gaussian(target, srcTexture, srcRect, sigmaX, radiusX,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001938 Gr1DKernelEffect::kX_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001939 srcTexture = dstTexture;
1940 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001941 }
1942
1943 if (sigmaY > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001944 if (scaleFactorY > 1 || sigmaX > 0.0f) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001945 // Clear out a radius below the srcRect to prevent the Y
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001946 // convolution from reading garbage.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001947 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001948 srcIRect.width(), radiusY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001949 this->clear(&clearRect, 0x0);
1950 }
1951
1952 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.com82b0ec62013-02-07 21:02:36 +00001953 GrDrawTarget* target = this->prepareToDraw(NULL, BUFFERED_DRAW);
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001954 convolve_gaussian(target, srcTexture, srcRect, sigmaY, radiusY,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001955 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001956 srcTexture = dstTexture;
1957 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001958 }
1959
1960 if (scaleFactorX > 1 || scaleFactorY > 1) {
1961 // Clear one pixel to the right and below, to accommodate bilinear
1962 // upsampling.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001963 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
robertphillips@google.com7a396332012-05-10 15:11:27 +00001964 srcIRect.width() + 1, 1);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001965 this->clear(&clearRect, 0x0);
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001966 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
robertphillips@google.com7a396332012-05-10 15:11:27 +00001967 1, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001968 this->clear(&clearRect, 0x0);
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001969 SkMatrix matrix;
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001970 // FIXME: This should be mitchell, not bilinear.
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +00001971 matrix.setIDiv(srcTexture->width(), srcTexture->height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001972 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.com68b58c92013-01-17 16:50:08 +00001973 paint.colorStage(0)->setEffect(GrSimpleTextureEffect::Create(srcTexture,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +00001974 matrix,
1975 true))->unref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001976 SkRect dstRect(srcRect);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001977 scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001978 this->drawRectToRect(paint, dstRect, srcRect);
1979 srcRect = dstRect;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001980 srcTexture = dstTexture;
1981 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001982 }
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001983 if (srcTexture == temp1.texture()) {
1984 return temp1.detach();
1985 } else if (srcTexture == temp2.texture()) {
1986 return temp2.detach();
1987 } else {
1988 srcTexture->ref();
1989 return srcTexture;
1990 }
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001991}
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001992
bsalomon@google.comc4364992011-11-07 15:54:49 +00001993///////////////////////////////////////////////////////////////////////////////
robertphillips@google.com59552022012-08-31 13:07:37 +00001994#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001995void GrContext::printCacheStats() const {
1996 fTextureCache->printStats();
1997}
1998#endif