blob: 6326f4d24e64cd806d862803b22545f838ccf00f [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/GrMorphologyEffect.h"
13#include "effects/GrConvolutionEffect.h"
14
tomhudson@google.com278cbb42011-06-30 19:37:01 +000015#include "GrBufferAllocPool.h"
16#include "GrClipIterator.h"
bsalomon@google.com05ef5102011-05-02 21:14:59 +000017#include "GrGpu.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000018#include "GrIndexBuffer.h"
19#include "GrInOrderDrawBuffer.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000020#include "GrPathRenderer.h"
tomhudson@google.comd22b6e42011-06-24 15:53:40 +000021#include "GrPathUtils.h"
bsalomon@google.com50398bf2011-07-26 20:45:30 +000022#include "GrResourceCache.h"
robertphillips@google.com72176b22012-05-23 13:19:12 +000023#include "GrSoftwarePathRenderer.h"
bsalomon@google.com558a75b2011-08-08 17:01:14 +000024#include "GrStencilBuffer.h"
tomhudson@google.com278cbb42011-06-30 19:37:01 +000025#include "GrTextStrike.h"
bsalomon@google.com8c2fe992011-09-13 15:27:18 +000026#include "SkTLazy.h"
bsalomon@google.comc0af3172012-06-15 14:10:09 +000027#include "SkTLS.h"
tomhudson@google.com0c8d93a2011-07-01 17:08:26 +000028#include "SkTrace.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000029
reed@google.comfa35e3d2012-06-26 20:16:17 +000030SK_DEFINE_INST_COUNT(GrContext)
31SK_DEFINE_INST_COUNT(GrDrawState)
32
bsalomon@google.com3c4d0322012-04-03 18:04:51 +000033#define DEFER_TEXT_RENDERING 1
bsalomon@google.com27847de2011-02-22 20:59:41 +000034
bsalomon@google.com3c4d0322012-04-03 18:04:51 +000035#define DEFER_PATHS 1
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +000036
bsalomon@google.com3c4d0322012-04-03 18:04:51 +000037#define BATCH_RECT_TO_RECT (1 && !GR_STATIC_RECT_VB)
bsalomon@google.com27847de2011-02-22 20:59:41 +000038
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +000039#define MAX_BLUR_SIGMA 4.0f
40
bsalomon@google.comd46e2422011-09-23 17:40:07 +000041// When we're using coverage AA but the blend is incompatible (given gpu
42// limitations) should we disable AA or draw wrong?
bsalomon@google.com950d7a82011-09-28 15:05:33 +000043#define DISABLE_COVERAGE_AA_FOR_BLEND 1
bsalomon@google.comd46e2422011-09-23 17:40:07 +000044
reed@google.com4b2d3f32012-05-15 18:05:50 +000045#if GR_DEBUG
46 // change this to a 1 to see notifications when partial coverage fails
47 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
48#else
49 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
50#endif
51
bsalomon@google.com07fc0d12012-06-22 15:15:59 +000052static const size_t MAX_TEXTURE_CACHE_COUNT = 256;
53static const size_t MAX_TEXTURE_CACHE_BYTES = 16 * 1024 * 1024;
bsalomon@google.com27847de2011-02-22 20:59:41 +000054
bsalomon@google.com60361492012-03-15 17:47:06 +000055static const size_t DRAW_BUFFER_VBPOOL_BUFFER_SIZE = 1 << 15;
bsalomon@google.com27847de2011-02-22 20:59:41 +000056static const int DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS = 4;
57
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +000058// path rendering is the only thing we defer today that uses non-static indices
59static const size_t DRAW_BUFFER_IBPOOL_BUFFER_SIZE = DEFER_PATHS ? 1 << 11 : 0;
60static const int DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS = DEFER_PATHS ? 4 : 0;
bsalomon@google.com27847de2011-02-22 20:59:41 +000061
bsalomon@google.combc4b6542011-11-19 13:56:11 +000062#define ASSERT_OWNED_RESOURCE(R) GrAssert(!(R) || (R)->getContext() == this)
63
bsalomon@google.com05ef5102011-05-02 21:14:59 +000064GrContext* GrContext::Create(GrEngine engine,
65 GrPlatform3DContext context3D) {
bsalomon@google.com27847de2011-02-22 20:59:41 +000066 GrContext* ctx = NULL;
67 GrGpu* fGpu = GrGpu::Create(engine, context3D);
68 if (NULL != fGpu) {
69 ctx = new GrContext(fGpu);
70 fGpu->unref();
71 }
72 return ctx;
73}
74
bsalomon@google.comc0af3172012-06-15 14:10:09 +000075namespace {
76void* CreateThreadInstanceCount() {
77 return new int(0);
78}
79void DeleteThreadInstanceCount(void* v) {
80 delete reinterpret_cast<int*>(v);
81}
82#define THREAD_INSTANCE_COUNT \
83 (*reinterpret_cast<int*>(SkTLS::Get(CreateThreadInstanceCount, \
84 DeleteThreadInstanceCount)))
85
86}
87
88int GrContext::GetThreadInstanceCount() {
89 return THREAD_INSTANCE_COUNT;
90}
91
bsalomon@google.com27847de2011-02-22 20:59:41 +000092GrContext::~GrContext() {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000093 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +000094
95 // Since the gpu can hold scratch textures, give it a chance to let go
96 // of them before freeing the texture cache
97 fGpu->purgeResources();
98
bsalomon@google.com27847de2011-02-22 20:59:41 +000099 delete fTextureCache;
100 delete fFontCache;
101 delete fDrawBuffer;
102 delete fDrawBufferVBAllocPool;
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000103 delete fDrawBufferIBAllocPool;
bsalomon@google.com30085192011-08-19 15:42:31 +0000104
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000105 fAARectRenderer->unref();
106
bsalomon@google.com205d4602011-04-25 12:43:45 +0000107 fGpu->unref();
bsalomon@google.com30085192011-08-19 15:42:31 +0000108 GrSafeUnref(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000109 GrSafeUnref(fSoftwarePathRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000110 fDrawState->unref();
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000111
112 --THREAD_INSTANCE_COUNT;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000113}
114
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000115void GrContext::contextLost() {
junov@google.com53a55842011-06-08 22:55:10 +0000116 contextDestroyed();
117 this->setupDrawBuffer();
118}
119
120void GrContext::contextDestroyed() {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000121 // abandon first to so destructors
122 // don't try to free the resources in the API.
123 fGpu->abandonResources();
124
bsalomon@google.com30085192011-08-19 15:42:31 +0000125 // a path renderer may be holding onto resources that
126 // are now unusable
127 GrSafeSetNull(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000128 GrSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com30085192011-08-19 15:42:31 +0000129
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000130 delete fDrawBuffer;
131 fDrawBuffer = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000132
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000133 delete fDrawBufferVBAllocPool;
134 fDrawBufferVBAllocPool = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000135
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000136 delete fDrawBufferIBAllocPool;
137 fDrawBufferIBAllocPool = NULL;
138
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000139 fAARectRenderer->reset();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000140
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000141 fTextureCache->removeAll();
142 fFontCache->freeAll();
143 fGpu->markContextDirty();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000144}
145
146void GrContext::resetContext() {
147 fGpu->markContextDirty();
148}
149
150void GrContext::freeGpuResources() {
151 this->flush();
robertphillips@google.comff175842012-05-14 19:31:39 +0000152
153 fGpu->purgeResources();
154
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000155 fAARectRenderer->reset();
156
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000157 fTextureCache->removeAll();
158 fFontCache->freeAll();
bsalomon@google.com30085192011-08-19 15:42:31 +0000159 // a path renderer may be holding onto resources
160 GrSafeSetNull(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000161 GrSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000162}
163
twiz@google.com05e70242012-01-27 19:12:00 +0000164size_t GrContext::getGpuTextureCacheBytes() const {
165 return fTextureCache->getCachedResourceBytes();
166}
167
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000168////////////////////////////////////////////////////////////////////////////////
169
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000170int GrContext::PaintStageVertexLayoutBits(
171 const GrPaint& paint,
172 const bool hasTexCoords[GrPaint::kTotalStages]) {
173 int stageMask = paint.getActiveStageMask();
174 int layout = 0;
175 for (int i = 0; i < GrPaint::kTotalStages; ++i) {
176 if ((1 << i) & stageMask) {
177 if (NULL != hasTexCoords && hasTexCoords[i]) {
178 layout |= GrDrawTarget::StageTexCoordVertexLayoutBit(i, i);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000179 }
180 }
181 }
182 return layout;
183}
184
185
186////////////////////////////////////////////////////////////////////////////////
187
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000188GrTexture* GrContext::TextureCacheEntry::texture() const {
189 if (NULL == fEntry) {
190 return NULL;
191 } else {
192 return (GrTexture*) fEntry->resource();
193 }
194}
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000195
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000196namespace {
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000197
198// we should never have more than one stencil buffer with same combo of
199// (width,height,samplecount)
200void gen_stencil_key_values(int width, int height,
201 int sampleCnt, uint32_t v[4]) {
202 v[0] = width;
203 v[1] = height;
204 v[2] = sampleCnt;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000205 v[3] = GrResourceKey::kStencilBuffer_TypeBit;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000206}
207
208void gen_stencil_key_values(const GrStencilBuffer* sb,
209 uint32_t v[4]) {
210 gen_stencil_key_values(sb->width(), sb->height(),
211 sb->numSamples(), v);
212}
bsalomon@google.com82c7bd82011-11-09 15:32:29 +0000213
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000214void scale_rect(SkRect* rect, float xScale, float yScale) {
robertphillips@google.com5af56062012-04-27 15:39:52 +0000215 rect->fLeft = SkScalarMul(rect->fLeft, SkFloatToScalar(xScale));
216 rect->fTop = SkScalarMul(rect->fTop, SkFloatToScalar(yScale));
217 rect->fRight = SkScalarMul(rect->fRight, SkFloatToScalar(xScale));
218 rect->fBottom = SkScalarMul(rect->fBottom, SkFloatToScalar(yScale));
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000219}
220
bsalomon@google.comb505a122012-05-31 18:40:36 +0000221float adjust_sigma(float sigma, int *scaleFactor, int *radius) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000222 *scaleFactor = 1;
223 while (sigma > MAX_BLUR_SIGMA) {
224 *scaleFactor *= 2;
225 sigma *= 0.5f;
226 }
bsalomon@google.comb505a122012-05-31 18:40:36 +0000227 *radius = static_cast<int>(ceilf(sigma * 3.0f));
228 GrAssert(*radius <= GrConvolutionEffect::kMaxKernelRadius);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000229 return sigma;
230}
231
232void apply_morphology(GrGpu* gpu,
233 GrTexture* texture,
234 const SkRect& rect,
235 int radius,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000236 GrContext::MorphologyType morphType,
237 Gr1DKernelEffect::Direction direction) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000238
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000239 GrRenderTarget* target = gpu->drawState()->getRenderTarget();
240 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000241 GrDrawState* drawState = gpu->drawState();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000242 drawState->setRenderTarget(target);
243 GrMatrix sampleM;
244 sampleM.setIDiv(texture->width(), texture->height());
bsalomon@google.comb505a122012-05-31 18:40:36 +0000245 drawState->sampler(0)->reset(sampleM);
246 SkAutoTUnref<GrCustomStage> morph(
247 new GrMorphologyEffect(direction, radius, morphType));
248 drawState->sampler(0)->setCustomStage(morph);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000249 drawState->setTexture(0, texture);
250 gpu->drawSimpleRect(rect, NULL, 1 << 0);
251}
252
bsalomon@google.comb505a122012-05-31 18:40:36 +0000253void convolve_gaussian(GrGpu* gpu,
254 GrTexture* texture,
255 const SkRect& rect,
256 float sigma,
257 int radius,
258 Gr1DKernelEffect::Direction direction) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000259 GrRenderTarget* target = gpu->drawState()->getRenderTarget();
260 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000261 GrDrawState* drawState = gpu->drawState();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000262 drawState->setRenderTarget(target);
263 GrMatrix sampleM;
264 sampleM.setIDiv(texture->width(), texture->height());
bsalomon@google.comb505a122012-05-31 18:40:36 +0000265 drawState->sampler(0)->reset(sampleM);
266 SkAutoTUnref<GrConvolutionEffect> conv(new
267 GrConvolutionEffect(direction, radius));
268 conv->setGaussianKernel(sigma);
269 drawState->sampler(0)->setCustomStage(conv);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000270 drawState->setTexture(0, texture);
271 gpu->drawSimpleRect(rect, NULL, 1 << 0);
272}
273
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000274}
275
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000276GrContext::TextureCacheEntry GrContext::findAndLockTexture(
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000277 const GrTextureDesc& desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000278 const GrSamplerState* sampler) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000279 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, sampler, desc, false);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000280 return TextureCacheEntry(fTextureCache->findAndLock(resourceKey,
281 GrResourceCache::kNested_LockType));
282}
283
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000284bool GrContext::isTextureInCache(const GrTextureDesc& desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000285 const GrSamplerState* sampler) const {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000286 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, sampler, desc, false);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000287 return fTextureCache->hasKey(resourceKey);
288}
289
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000290GrResourceEntry* GrContext::addAndLockStencilBuffer(GrStencilBuffer* sb) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000291 ASSERT_OWNED_RESOURCE(sb);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000292 uint32_t v[4];
293 gen_stencil_key_values(sb, v);
294 GrResourceKey resourceKey(v);
295 return fTextureCache->createAndLock(resourceKey, sb);
296}
297
298GrStencilBuffer* GrContext::findStencilBuffer(int width, int height,
299 int sampleCnt) {
300 uint32_t v[4];
301 gen_stencil_key_values(width, height, sampleCnt, v);
302 GrResourceKey resourceKey(v);
303 GrResourceEntry* entry = fTextureCache->findAndLock(resourceKey,
304 GrResourceCache::kSingle_LockType);
305 if (NULL != entry) {
306 GrStencilBuffer* sb = (GrStencilBuffer*) entry->resource();
307 return sb;
308 } else {
309 return NULL;
310 }
311}
312
313void GrContext::unlockStencilBuffer(GrResourceEntry* sbEntry) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000314 ASSERT_OWNED_RESOURCE(sbEntry->resource());
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000315 fTextureCache->unlock(sbEntry);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000316}
317
318static void stretchImage(void* dst,
319 int dstW,
320 int dstH,
321 void* src,
322 int srcW,
323 int srcH,
324 int bpp) {
325 GrFixed dx = (srcW << 16) / dstW;
326 GrFixed dy = (srcH << 16) / dstH;
327
328 GrFixed y = dy >> 1;
329
330 int dstXLimit = dstW*bpp;
331 for (int j = 0; j < dstH; ++j) {
332 GrFixed x = dx >> 1;
333 void* srcRow = (uint8_t*)src + (y>>16)*srcW*bpp;
334 void* dstRow = (uint8_t*)dst + j*dstW*bpp;
335 for (int i = 0; i < dstXLimit; i += bpp) {
336 memcpy((uint8_t*) dstRow + i,
337 (uint8_t*) srcRow + (x>>16)*bpp,
338 bpp);
339 x += dx;
340 }
341 y += dy;
342 }
343}
344
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000345GrContext::TextureCacheEntry GrContext::createAndLockTexture(
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000346 const GrSamplerState* sampler,
347 const GrTextureDesc& desc,
348 void* srcData,
349 size_t rowBytes) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000350 SK_TRACE_EVENT0("GrContext::createAndLockTexture");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000351
352#if GR_DUMP_TEXTURE_UPLOAD
353 GrPrintf("GrContext::createAndLockTexture [%d %d]\n", desc.fWidth, desc.fHeight);
354#endif
355
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000356 TextureCacheEntry entry;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000357
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000358 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, sampler,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000359 desc, false);
360
361 if (GrTexture::NeedsResizing(resourceKey)) {
362 // The desired texture is NPOT and tiled but that isn't supported by
363 // the current hardware. Resize the texture to be a POT
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000364 GrAssert(NULL != sampler);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000365 TextureCacheEntry clampEntry = this->findAndLockTexture(desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000366 NULL);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000367
368 if (NULL == clampEntry.texture()) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000369 clampEntry = this->createAndLockTexture(NULL, desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000370 srcData, rowBytes);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000371 GrAssert(NULL != clampEntry.texture());
372 if (NULL == clampEntry.texture()) {
373 return entry;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000374 }
375 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000376 GrTextureDesc rtDesc = desc;
377 rtDesc.fFlags = rtDesc.fFlags |
378 kRenderTarget_GrTextureFlagBit |
379 kNoStencil_GrTextureFlagBit;
bsalomon@google.com99621082011-11-15 16:47:16 +0000380 rtDesc.fWidth = GrNextPow2(GrMax(desc.fWidth, 64));
381 rtDesc.fHeight = GrNextPow2(GrMax(desc.fHeight, 64));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000382
383 GrTexture* texture = fGpu->createTexture(rtDesc, NULL, 0);
384
385 if (NULL != texture) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000386 GrDrawTarget::AutoStateRestore asr(fGpu,
387 GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000388 GrDrawState* drawState = fGpu->drawState();
389 drawState->setRenderTarget(texture->asRenderTarget());
390 drawState->setTexture(0, clampEntry.texture());
bsalomon@google.com82c7bd82011-11-09 15:32:29 +0000391
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000392 GrSamplerState::Filter filter;
393 // if filtering is not desired then we want to ensure all
394 // texels in the resampled image are copies of texels from
395 // the original.
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000396 if (GrTexture::NeedsFiltering(resourceKey)) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000397 filter = GrSamplerState::kBilinear_Filter;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000398 } else {
399 filter = GrSamplerState::kNearest_Filter;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000400 }
bsalomon@google.com1e266f82011-12-12 16:11:33 +0000401 drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode,
402 filter);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000403
404 static const GrVertexLayout layout =
405 GrDrawTarget::StageTexCoordVertexLayoutBit(0,0);
406 GrDrawTarget::AutoReleaseGeometry arg(fGpu, layout, 4, 0);
407
408 if (arg.succeeded()) {
409 GrPoint* verts = (GrPoint*) arg.vertices();
410 verts[0].setIRectFan(0, 0,
411 texture->width(),
412 texture->height(),
413 2*sizeof(GrPoint));
414 verts[1].setIRectFan(0, 0, 1, 1, 2*sizeof(GrPoint));
bsalomon@google.com47059542012-06-06 20:51:20 +0000415 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000416 0, 4);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000417 entry.set(fTextureCache->createAndLock(resourceKey, texture));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000418 }
bsalomon@google.com1da07462011-03-10 14:51:57 +0000419 texture->releaseRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000420 } else {
421 // TODO: Our CPU stretch doesn't filter. But we create separate
422 // stretched textures when the sampler state is either filtered or
423 // not. Either implement filtered stretch blit on CPU or just create
424 // one when FBO case fails.
425
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000426 rtDesc.fFlags = kNone_GrTextureFlags;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000427 // no longer need to clamp at min RT size.
428 rtDesc.fWidth = GrNextPow2(desc.fWidth);
429 rtDesc.fHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000430 int bpp = GrBytesPerPixel(desc.fConfig);
bsalomon@google.com3582bf92011-06-30 21:32:31 +0000431 SkAutoSMalloc<128*128*4> stretchedPixels(bpp *
bsalomon@google.com27847de2011-02-22 20:59:41 +0000432 rtDesc.fWidth *
433 rtDesc.fHeight);
434 stretchImage(stretchedPixels.get(), rtDesc.fWidth, rtDesc.fHeight,
435 srcData, desc.fWidth, desc.fHeight, bpp);
436
437 size_t stretchedRowBytes = rtDesc.fWidth * bpp;
438
439 GrTexture* texture = fGpu->createTexture(rtDesc,
440 stretchedPixels.get(),
441 stretchedRowBytes);
442 GrAssert(NULL != texture);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000443 entry.set(fTextureCache->createAndLock(resourceKey, texture));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000444 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000445 fTextureCache->unlock(clampEntry.cacheEntry());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000446
447 } else {
448 GrTexture* texture = fGpu->createTexture(desc, srcData, rowBytes);
449 if (NULL != texture) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000450 entry.set(fTextureCache->createAndLock(resourceKey, texture));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000451 }
452 }
453 return entry;
454}
455
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000456GrContext::TextureCacheEntry GrContext::lockScratchTexture(
457 const GrTextureDesc& inDesc,
458 ScratchTexMatch match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000459 GrTextureDesc desc = inDesc;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000460 desc.fClientCacheID = kScratch_CacheID;
461
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000462 if (kExact_ScratchTexMatch != match) {
463 // bin by pow2 with a reasonable min
464 static const int MIN_SIZE = 256;
465 desc.fWidth = GrMax(MIN_SIZE, GrNextPow2(desc.fWidth));
466 desc.fHeight = GrMax(MIN_SIZE, GrNextPow2(desc.fHeight));
467 }
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000468
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000469 GrResourceEntry* entry;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000470 int origWidth = desc.fWidth;
471 int origHeight = desc.fHeight;
472 bool doubledW = false;
473 bool doubledH = false;
474
475 do {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000476 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL, desc, true);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000477 entry = fTextureCache->findAndLock(key,
478 GrResourceCache::kNested_LockType);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000479 // if we miss, relax the fit of the flags...
480 // then try doubling width... then height.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000481 if (NULL != entry || kExact_ScratchTexMatch == match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000482 break;
483 }
484 if (!(desc.fFlags & kRenderTarget_GrTextureFlagBit)) {
485 desc.fFlags = desc.fFlags | kRenderTarget_GrTextureFlagBit;
486 } else if (desc.fFlags & kNoStencil_GrTextureFlagBit) {
487 desc.fFlags = desc.fFlags & ~kNoStencil_GrTextureFlagBit;
488 } else if (!doubledW) {
489 desc.fFlags = inDesc.fFlags;
490 desc.fWidth *= 2;
491 doubledW = true;
492 } else if (!doubledH) {
493 desc.fFlags = inDesc.fFlags;
494 desc.fWidth = origWidth;
495 desc.fHeight *= 2;
496 doubledH = true;
497 } else {
498 break;
499 }
500
501 } while (true);
502
503 if (NULL == entry) {
504 desc.fFlags = inDesc.fFlags;
505 desc.fWidth = origWidth;
506 desc.fHeight = origHeight;
507 GrTexture* texture = fGpu->createTexture(desc, NULL, 0);
508 if (NULL != texture) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000509 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000510 texture->desc(),
511 true);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000512 entry = fTextureCache->createAndLock(key, texture);
513 }
514 }
515
516 // If the caller gives us the same desc/sampler twice we don't want
517 // to return the same texture the second time (unless it was previously
518 // released). So we detach the entry from the cache and reattach at release.
519 if (NULL != entry) {
520 fTextureCache->detach(entry);
521 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000522 return TextureCacheEntry(entry);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000523}
524
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000525void GrContext::addExistingTextureToCache(GrTexture* texture) {
526
527 if (NULL == texture) {
528 return;
529 }
530
531 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL,
532 texture->desc(),
533 true);
534 fTextureCache->attach(key, texture);
535}
536
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000537void GrContext::unlockTexture(TextureCacheEntry entry) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000538 ASSERT_OWNED_RESOURCE(entry.texture());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000539 // If this is a scratch texture we detached it from the cache
540 // while it was locked (to avoid two callers simultaneously getting
541 // the same texture).
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000542 if (GrTexture::IsScratchTexture(entry.cacheEntry()->key())) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000543 fTextureCache->reattachAndUnlock(entry.cacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000544 } else {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000545 fTextureCache->unlock(entry.cacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000546 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000547}
548
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000549void GrContext::freeEntry(TextureCacheEntry entry) {
550 ASSERT_OWNED_RESOURCE(entry.texture());
551
552 fTextureCache->freeEntry(entry.cacheEntry());
553}
554
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000555GrTexture* GrContext::createUncachedTexture(const GrTextureDesc& descIn,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000556 void* srcData,
557 size_t rowBytes) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000558 GrTextureDesc descCopy = descIn;
559 descCopy.fClientCacheID = kUncached_CacheID;
560 return fGpu->createTexture(descCopy, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000561}
562
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000563void GrContext::getTextureCacheLimits(int* maxTextures,
564 size_t* maxTextureBytes) const {
565 fTextureCache->getLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000566}
567
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000568void GrContext::setTextureCacheLimits(int maxTextures, size_t maxTextureBytes) {
569 fTextureCache->setLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000570}
571
bsalomon@google.com91958362011-06-13 17:58:13 +0000572int GrContext::getMaxTextureSize() const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000573 return fGpu->getCaps().fMaxTextureSize;
bsalomon@google.com91958362011-06-13 17:58:13 +0000574}
575
576int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000577 return fGpu->getCaps().fMaxRenderTargetSize;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000578}
579
580///////////////////////////////////////////////////////////////////////////////
581
bsalomon@google.come269f212011-11-07 13:29:52 +0000582GrTexture* GrContext::createPlatformTexture(const GrPlatformTextureDesc& desc) {
583 return fGpu->createPlatformTexture(desc);
584}
585
586GrRenderTarget* GrContext::createPlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
587 return fGpu->createPlatformRenderTarget(desc);
588}
589
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000590///////////////////////////////////////////////////////////////////////////////
591
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000592bool GrContext::supportsIndex8PixelConfig(const GrSamplerState* sampler,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000593 int width, int height) const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000594 const GrDrawTarget::Caps& caps = fGpu->getCaps();
595 if (!caps.f8BitPaletteSupport) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000596 return false;
597 }
598
bsalomon@google.com27847de2011-02-22 20:59:41 +0000599 bool isPow2 = GrIsPow2(width) && GrIsPow2(height);
600
601 if (!isPow2) {
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000602 bool tiled = NULL != sampler &&
603 (sampler->getWrapX() != GrSamplerState::kClamp_WrapMode ||
604 sampler->getWrapY() != GrSamplerState::kClamp_WrapMode);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000605 if (tiled && !caps.fNPOTTextureTileSupport) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000606 return false;
607 }
608 }
609 return true;
610}
611
612////////////////////////////////////////////////////////////////////////////////
613
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000614const GrClip& GrContext::getClip() const { return fGpu->getClip(); }
615
bsalomon@google.com27847de2011-02-22 20:59:41 +0000616void GrContext::setClip(const GrClip& clip) {
617 fGpu->setClip(clip);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000618 fDrawState->enableState(GrDrawState::kClip_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000619}
620
621void GrContext::setClip(const GrIRect& rect) {
622 GrClip clip;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000623 clip.setFromIRect(rect);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000624 fGpu->setClip(clip);
625}
626
627////////////////////////////////////////////////////////////////////////////////
628
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000629void GrContext::clear(const GrIRect* rect,
630 const GrColor color,
631 GrRenderTarget* target) {
bsalomon@google.com398109c2011-04-14 18:40:27 +0000632 this->flush();
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000633 fGpu->clear(rect, color, target);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000634}
635
636void GrContext::drawPaint(const GrPaint& paint) {
637 // set rect to be big enough to fill the space, but not super-huge, so we
638 // don't overflow fixed-point implementations
bsalomon@google.comd302f142011-03-03 13:54:13 +0000639 GrRect r;
640 r.setLTRB(0, 0,
641 GrIntToScalar(getRenderTarget()->width()),
642 GrIntToScalar(getRenderTarget()->height()));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000643 GrMatrix inverse;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000644 SkTLazy<GrPaint> tmpPaint;
645 const GrPaint* p = &paint;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000646 GrAutoMatrix am;
647
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000648 // We attempt to map r by the inverse matrix and draw that. mapRect will
649 // map the four corners and bound them with a new rect. This will not
650 // produce a correct result for some perspective matrices.
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000651 if (!this->getMatrix().hasPerspective()) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000652 if (!fDrawState->getViewInverse(&inverse)) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000653 GrPrintf("Could not invert matrix");
654 return;
655 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000656 inverse.mapRect(&r);
657 } else {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000658 if (paint.getActiveMaskStageMask() || paint.getActiveStageMask()) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000659 if (!fDrawState->getViewInverse(&inverse)) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000660 GrPrintf("Could not invert matrix");
661 return;
662 }
663 tmpPaint.set(paint);
664 tmpPaint.get()->preConcatActiveSamplerMatrices(inverse);
665 p = tmpPaint.get();
666 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000667 am.set(this, GrMatrix::I());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000668 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000669 // by definition this fills the entire clip, no need for AA
670 if (paint.fAntiAlias) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000671 if (!tmpPaint.isValid()) {
672 tmpPaint.set(paint);
673 p = tmpPaint.get();
674 }
675 GrAssert(p == tmpPaint.get());
676 tmpPaint.get()->fAntiAlias = false;
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000677 }
678 this->drawRect(*p, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000679}
680
bsalomon@google.com205d4602011-04-25 12:43:45 +0000681////////////////////////////////////////////////////////////////////////////////
682
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000683namespace {
684inline bool disable_coverage_aa_for_blend(GrDrawTarget* target) {
685 return DISABLE_COVERAGE_AA_FOR_BLEND && !target->canApplyCoverage();
686}
687}
688
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000689////////////////////////////////////////////////////////////////////////////////
690
bsalomon@google.com27847de2011-02-22 20:59:41 +0000691/* create a triangle strip that strokes the specified triangle. There are 8
692 unique vertices, but we repreat the last 2 to close up. Alternatively we
693 could use an indices array, and then only send 8 verts, but not sure that
694 would be faster.
695 */
bsalomon@google.com205d4602011-04-25 12:43:45 +0000696static void setStrokeRectStrip(GrPoint verts[10], GrRect rect,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000697 GrScalar width) {
698 const GrScalar rad = GrScalarHalf(width);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000699 rect.sort();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000700
701 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
702 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
703 verts[2].set(rect.fRight - rad, rect.fTop + rad);
704 verts[3].set(rect.fRight + rad, rect.fTop - rad);
705 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
706 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
707 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
708 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
709 verts[8] = verts[0];
710 verts[9] = verts[1];
711}
712
reed@google.com20efde72011-05-09 17:00:02 +0000713/**
714 * Returns true if the rects edges are integer-aligned.
715 */
716static bool isIRect(const GrRect& r) {
717 return GrScalarIsInt(r.fLeft) && GrScalarIsInt(r.fTop) &&
718 GrScalarIsInt(r.fRight) && GrScalarIsInt(r.fBottom);
719}
720
bsalomon@google.com205d4602011-04-25 12:43:45 +0000721static bool apply_aa_to_rect(GrDrawTarget* target,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000722 const GrRect& rect,
723 GrScalar width,
724 const GrMatrix* matrix,
725 GrMatrix* combinedMatrix,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000726 GrRect* devRect,
727 bool* useVertexCoverage) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000728 // we use a simple coverage ramp to do aa on axis-aligned rects
729 // we check if the rect will be axis-aligned, and the rect won't land on
730 // integer coords.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000731
bsalomon@google.coma3108262011-10-10 14:08:47 +0000732 // we are keeping around the "tweak the alpha" trick because
733 // it is our only hope for the fixed-pipe implementation.
734 // In a shader implementation we can give a separate coverage input
bsalomon@google.com289533a2011-10-27 12:34:25 +0000735 // TODO: remove this ugliness when we drop the fixed-pipe impl
bsalomon@google.coma3108262011-10-10 14:08:47 +0000736 *useVertexCoverage = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000737 if (!target->canTweakAlphaForCoverage()) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000738 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +0000739#if GR_DEBUG
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000740 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +0000741#endif
bsalomon@google.coma3108262011-10-10 14:08:47 +0000742 return false;
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000743 } else {
744 *useVertexCoverage = true;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000745 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000746 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000747 const GrDrawState& drawState = target->getDrawState();
748 if (drawState.getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000749 return false;
750 }
751
bsalomon@google.com471d4712011-08-23 15:45:25 +0000752 if (0 == width && target->willUseHWAALines()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000753 return false;
754 }
755
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000756 if (!drawState.getViewMatrix().preservesAxisAlignment()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000757 return false;
758 }
759
760 if (NULL != matrix &&
761 !matrix->preservesAxisAlignment()) {
762 return false;
763 }
764
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000765 *combinedMatrix = drawState.getViewMatrix();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000766 if (NULL != matrix) {
767 combinedMatrix->preConcat(*matrix);
768 GrAssert(combinedMatrix->preservesAxisAlignment());
769 }
770
771 combinedMatrix->mapRect(devRect, rect);
772 devRect->sort();
773
774 if (width < 0) {
reed@google.com20efde72011-05-09 17:00:02 +0000775 return !isIRect(*devRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000776 } else {
777 return true;
778 }
779}
780
bsalomon@google.com27847de2011-02-22 20:59:41 +0000781void GrContext::drawRect(const GrPaint& paint,
782 const GrRect& rect,
783 GrScalar width,
784 const GrMatrix* matrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000785 SK_TRACE_EVENT0("GrContext::drawRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000786
787 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000788 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000789 int stageMask = paint.getActiveStageMask();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000790
bsalomon@google.com205d4602011-04-25 12:43:45 +0000791 GrRect devRect = rect;
792 GrMatrix combinedMatrix;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000793 bool useVertexCoverage;
bsalomon@google.com289533a2011-10-27 12:34:25 +0000794 bool needAA = paint.fAntiAlias &&
795 !this->getRenderTarget()->isMultisampled();
796 bool doAA = needAA && apply_aa_to_rect(target, rect, width, matrix,
797 &combinedMatrix, &devRect,
798 &useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000799
800 if (doAA) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000801 GrDrawTarget::AutoDeviceCoordDraw adcd(target, stageMask);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000802 if (width >= 0) {
803 GrVec strokeSize;;
804 if (width > 0) {
805 strokeSize.set(width, width);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000806 combinedMatrix.mapVectors(&strokeSize, 1);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000807 strokeSize.setAbs(strokeSize);
808 } else {
809 strokeSize.set(GR_Scalar1, GR_Scalar1);
810 }
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000811 fAARectRenderer->strokeAARect(this->getGpu(), target, devRect,
812 strokeSize, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000813 } else {
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000814 fAARectRenderer->fillAARect(this->getGpu(), target,
815 devRect, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000816 }
817 return;
818 }
819
bsalomon@google.com27847de2011-02-22 20:59:41 +0000820 if (width >= 0) {
821 // TODO: consider making static vertex buffers for these cases.
822 // Hairline could be done by just adding closing vertex to
823 // unitSquareVertexBuffer()
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000824 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL);
825
bsalomon@google.com27847de2011-02-22 20:59:41 +0000826 static const int worstCaseVertCount = 10;
827 GrDrawTarget::AutoReleaseGeometry geo(target, layout, worstCaseVertCount, 0);
828
829 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000830 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000831 return;
832 }
833
834 GrPrimitiveType primType;
835 int vertCount;
836 GrPoint* vertex = geo.positions();
837
838 if (width > 0) {
839 vertCount = 10;
bsalomon@google.com47059542012-06-06 20:51:20 +0000840 primType = kTriangleStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000841 setStrokeRectStrip(vertex, rect, width);
842 } else {
843 // hairline
844 vertCount = 5;
bsalomon@google.com47059542012-06-06 20:51:20 +0000845 primType = kLineStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000846 vertex[0].set(rect.fLeft, rect.fTop);
847 vertex[1].set(rect.fRight, rect.fTop);
848 vertex[2].set(rect.fRight, rect.fBottom);
849 vertex[3].set(rect.fLeft, rect.fBottom);
850 vertex[4].set(rect.fLeft, rect.fTop);
851 }
852
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000853 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000854 if (NULL != matrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000855 GrDrawState* drawState = target->drawState();
856 avmr.set(drawState);
857 drawState->preConcatViewMatrix(*matrix);
858 drawState->preConcatSamplerMatrices(stageMask, *matrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000859 }
860
861 target->drawNonIndexed(primType, 0, vertCount);
862 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000863#if GR_STATIC_RECT_VB
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000864 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL);
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000865 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
866 if (NULL == sqVB) {
867 GrPrintf("Failed to create static rect vb.\n");
868 return;
869 }
870 target->setVertexSourceToBuffer(layout, sqVB);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000871 GrDrawState* drawState = target->drawState();
872 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000873 GrMatrix m;
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000874 m.setAll(rect.width(), 0, rect.fLeft,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000875 0, rect.height(), rect.fTop,
876 0, 0, GrMatrix::I()[8]);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000877
878 if (NULL != matrix) {
879 m.postConcat(*matrix);
880 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000881 drawState->preConcatViewMatrix(m);
882 drawState->preConcatSamplerMatrices(stageMask, m);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000883
bsalomon@google.com47059542012-06-06 20:51:20 +0000884 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000885#else
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000886 target->drawSimpleRect(rect, matrix, stageMask);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000887#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +0000888 }
889}
890
891void GrContext::drawRectToRect(const GrPaint& paint,
892 const GrRect& dstRect,
893 const GrRect& srcRect,
894 const GrMatrix* dstMatrix,
895 const GrMatrix* srcMatrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000896 SK_TRACE_EVENT0("GrContext::drawRectToRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000897
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000898 // srcRect refers to paint's first texture
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000899 if (!paint.isTextureStageEnabled(0)) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000900 drawRect(paint, dstRect, -1, dstMatrix);
901 return;
902 }
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000903
bsalomon@google.com27847de2011-02-22 20:59:41 +0000904 GR_STATIC_ASSERT(!BATCH_RECT_TO_RECT || !GR_STATIC_RECT_VB);
905
906#if GR_STATIC_RECT_VB
907 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000908 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000909 GrDrawState* drawState = target->drawState();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000910 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000911 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000912
913 GrMatrix m;
914
915 m.setAll(dstRect.width(), 0, dstRect.fLeft,
916 0, dstRect.height(), dstRect.fTop,
917 0, 0, GrMatrix::I()[8]);
918 if (NULL != dstMatrix) {
919 m.postConcat(*dstMatrix);
920 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000921 drawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000922
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000923 // srcRect refers to first stage
924 int otherStageMask = paint.getActiveStageMask() &
925 (~(1 << GrPaint::kFirstTextureStage));
926 if (otherStageMask) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000927 drawState->preConcatSamplerMatrices(otherStageMask, m);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000928 }
929
bsalomon@google.com27847de2011-02-22 20:59:41 +0000930 m.setAll(srcRect.width(), 0, srcRect.fLeft,
931 0, srcRect.height(), srcRect.fTop,
932 0, 0, GrMatrix::I()[8]);
933 if (NULL != srcMatrix) {
934 m.postConcat(*srcMatrix);
935 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000936 drawState->sampler(GrPaint::kFirstTextureStage)->preConcatMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000937
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000938 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
939 if (NULL == sqVB) {
940 GrPrintf("Failed to create static rect vb.\n");
941 return;
942 }
943 target->setVertexSourceToBuffer(layout, sqVB);
bsalomon@google.com47059542012-06-06 20:51:20 +0000944 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000945#else
946
947 GrDrawTarget* target;
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000948#if BATCH_RECT_TO_RECT
bsalomon@google.com27847de2011-02-22 20:59:41 +0000949 target = this->prepareToDraw(paint, kBuffered_DrawCategory);
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000950#else
bsalomon@google.com27847de2011-02-22 20:59:41 +0000951 target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
952#endif
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000953 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000954
tomhudson@google.com93813632011-10-27 20:21:16 +0000955 const GrRect* srcRects[GrDrawState::kNumStages] = {NULL};
956 const GrMatrix* srcMatrices[GrDrawState::kNumStages] = {NULL};
bsalomon@google.com27847de2011-02-22 20:59:41 +0000957 srcRects[0] = &srcRect;
958 srcMatrices[0] = srcMatrix;
959
960 target->drawRect(dstRect, dstMatrix, 1, srcRects, srcMatrices);
961#endif
962}
963
964void GrContext::drawVertices(const GrPaint& paint,
965 GrPrimitiveType primitiveType,
966 int vertexCount,
967 const GrPoint positions[],
968 const GrPoint texCoords[],
969 const GrColor colors[],
970 const uint16_t indices[],
971 int indexCount) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000972 SK_TRACE_EVENT0("GrContext::drawVertices");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000973
974 GrDrawTarget::AutoReleaseGeometry geo;
975
976 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000977 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000978
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000979 bool hasTexCoords[GrPaint::kTotalStages] = {
980 NULL != texCoords, // texCoordSrc provides explicit stage 0 coords
981 0 // remaining stages use positions
982 };
983
984 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, hasTexCoords);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000985
986 if (NULL != colors) {
987 layout |= GrDrawTarget::kColor_VertexLayoutBit;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000988 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000989 int vertexSize = GrDrawTarget::VertexSize(layout);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000990
991 if (sizeof(GrPoint) != vertexSize) {
992 if (!geo.set(target, layout, vertexCount, 0)) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000993 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000994 return;
995 }
tomhudson@google.com93813632011-10-27 20:21:16 +0000996 int texOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000997 int colorOffset;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000998 GrDrawTarget::VertexSizeAndOffsetsByIdx(layout,
999 texOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001000 &colorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +00001001 NULL,
1002 NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001003 void* curVertex = geo.vertices();
1004
1005 for (int i = 0; i < vertexCount; ++i) {
1006 *((GrPoint*)curVertex) = positions[i];
1007
1008 if (texOffsets[0] > 0) {
1009 *(GrPoint*)((intptr_t)curVertex + texOffsets[0]) = texCoords[i];
1010 }
1011 if (colorOffset > 0) {
1012 *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i];
1013 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001014 curVertex = (void*)((intptr_t)curVertex + vertexSize);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001015 }
1016 } else {
1017 target->setVertexSourceToArray(layout, positions, vertexCount);
1018 }
1019
bsalomon@google.com91958362011-06-13 17:58:13 +00001020 // we don't currently apply offscreen AA to this path. Need improved
1021 // management of GrDrawTarget's geometry to avoid copying points per-tile.
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001022
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001023 if (NULL != indices) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001024 target->setIndexSourceToArray(indices, indexCount);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001025 target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001026 } else {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001027 target->drawNonIndexed(primitiveType, 0, vertexCount);
1028 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001029}
1030
bsalomon@google.com06afe7b2011-04-26 15:31:40 +00001031///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com150d2842012-01-12 20:19:56 +00001032namespace {
1033
bsalomon@google.com93c96602012-04-27 13:05:21 +00001034struct CircleVertex {
1035 GrPoint fPos;
1036 GrPoint fCenter;
1037 GrScalar fOuterRadius;
1038 GrScalar fInnerRadius;
1039};
1040
1041/* Returns true if will map a circle to another circle. This can be true
1042 * if the matrix only includes square-scale, rotation, translation.
1043 */
1044inline bool isSimilarityTransformation(const SkMatrix& matrix,
1045 SkScalar tol = SK_ScalarNearlyZero) {
1046 if (matrix.isIdentity() || matrix.getType() == SkMatrix::kTranslate_Mask) {
1047 return true;
1048 }
1049 if (matrix.hasPerspective()) {
1050 return false;
1051 }
1052
1053 SkScalar mx = matrix.get(SkMatrix::kMScaleX);
1054 SkScalar sx = matrix.get(SkMatrix::kMSkewX);
1055 SkScalar my = matrix.get(SkMatrix::kMScaleY);
1056 SkScalar sy = matrix.get(SkMatrix::kMSkewY);
1057
1058 if (mx == 0 && sx == 0 && my == 0 && sy == 0) {
1059 return false;
1060 }
1061
1062 // it has scales or skews, but it could also be rotation, check it out.
1063 SkVector vec[2];
1064 vec[0].set(mx, sx);
1065 vec[1].set(sy, my);
1066
1067 return SkScalarNearlyZero(vec[0].dot(vec[1]), SkScalarSquare(tol)) &&
1068 SkScalarNearlyEqual(vec[0].lengthSqd(), vec[1].lengthSqd(),
1069 SkScalarSquare(tol));
1070}
1071
1072}
1073
1074// TODO: strokeWidth can't be larger than zero right now.
1075// It will be fixed when drawPath() can handle strokes.
1076void GrContext::drawOval(const GrPaint& paint,
1077 const GrRect& rect,
1078 SkScalar strokeWidth) {
1079 DrawCategory category = (DEFER_PATHS) ? kBuffered_DrawCategory :
1080 kUnbuffered_DrawCategory;
1081 GrDrawTarget* target = this->prepareToDraw(paint, category);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +00001082 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001083 GrDrawState* drawState = target->drawState();
1084 GrMatrix vm = drawState->getViewMatrix();
1085
1086 if (!isSimilarityTransformation(vm) ||
1087 !paint.fAntiAlias ||
1088 rect.height() != rect.width()) {
1089 SkPath path;
1090 path.addOval(rect);
1091 GrPathFill fill = (strokeWidth == 0) ?
bsalomon@google.com47059542012-06-06 20:51:20 +00001092 kHairLine_GrPathFill : kWinding_GrPathFill;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001093 this->internalDrawPath(paint, path, fill, NULL);
1094 return;
1095 }
1096
1097 const GrRenderTarget* rt = drawState->getRenderTarget();
1098 if (NULL == rt) {
1099 return;
1100 }
1101
1102 GrDrawTarget::AutoDeviceCoordDraw adcd(target, paint.getActiveStageMask());
1103
1104 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL);
1105 layout |= GrDrawTarget::kEdge_VertexLayoutBit;
1106 GrAssert(sizeof(CircleVertex) == GrDrawTarget::VertexSize(layout));
1107
1108 GrPoint center = GrPoint::Make(rect.centerX(), rect.centerY());
1109 GrScalar radius = SkScalarHalf(rect.width());
1110
1111 vm.mapPoints(&center, 1);
1112 radius = vm.mapRadius(radius);
1113
1114 GrScalar outerRadius = radius;
1115 GrScalar innerRadius = 0;
1116 SkScalar halfWidth = 0;
1117 if (strokeWidth == 0) {
1118 halfWidth = SkScalarHalf(SK_Scalar1);
1119
1120 outerRadius += halfWidth;
1121 innerRadius = SkMaxScalar(0, radius - halfWidth);
1122 }
1123
1124 GrDrawTarget::AutoReleaseGeometry geo(target, layout, 4, 0);
1125 if (!geo.succeeded()) {
1126 GrPrintf("Failed to get space for vertices!\n");
1127 return;
1128 }
1129
1130 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
1131
robertphillips@google.coma0a66c12012-06-22 13:14:29 +00001132 // The fragment shader will extend the radius out half a pixel
1133 // to antialias. Expand the drawn rect here so all the pixels
1134 // will be captured.
1135 SkScalar L = center.fX - outerRadius - SkFloatToScalar(0.5f);
1136 SkScalar R = center.fX + outerRadius + SkFloatToScalar(0.5f);
1137 SkScalar T = center.fY - outerRadius - SkFloatToScalar(0.5f);
1138 SkScalar B = center.fY + outerRadius + SkFloatToScalar(0.5f);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001139
1140 verts[0].fPos = SkPoint::Make(L, T);
1141 verts[1].fPos = SkPoint::Make(R, T);
1142 verts[2].fPos = SkPoint::Make(L, B);
1143 verts[3].fPos = SkPoint::Make(R, B);
1144
1145 for (int i = 0; i < 4; ++i) {
1146 // this goes to fragment shader, it should be in y-points-up space.
1147 verts[i].fCenter = SkPoint::Make(center.fX, rt->height() - center.fY);
1148
1149 verts[i].fOuterRadius = outerRadius;
1150 verts[i].fInnerRadius = innerRadius;
1151 }
1152
1153 drawState->setVertexEdgeType(GrDrawState::kCircle_EdgeType);
bsalomon@google.com47059542012-06-06 20:51:20 +00001154 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4);
bsalomon@google.com150d2842012-01-12 20:19:56 +00001155}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001156
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001157void GrContext::drawPath(const GrPaint& paint, const SkPath& path,
reed@google.com07f3ee12011-05-16 17:21:57 +00001158 GrPathFill fill, const GrPoint* translate) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001159
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001160 if (path.isEmpty()) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001161 if (GrIsFillInverted(fill)) {
1162 this->drawPaint(paint);
1163 }
1164 return;
1165 }
1166
bsalomon@google.com93c96602012-04-27 13:05:21 +00001167 SkRect ovalRect;
1168 if (!GrIsFillInverted(fill) && path.isOval(&ovalRect)) {
1169 if (translate) {
1170 ovalRect.offset(*translate);
1171 }
bsalomon@google.com47059542012-06-06 20:51:20 +00001172 SkScalar width = (fill == kHairLine_GrPathFill) ? 0 : -SK_Scalar1;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001173 this->drawOval(paint, ovalRect, width);
1174 return;
1175 }
1176
1177 internalDrawPath(paint, path, fill, translate);
1178}
1179
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001180void GrContext::internalDrawPath(const GrPaint& paint, const SkPath& path,
bsalomon@google.com93c96602012-04-27 13:05:21 +00001181 GrPathFill fill, const GrPoint* translate) {
1182
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001183 // Note that below we may sw-rasterize the path into a scratch texture.
1184 // Scratch textures can be recycled after they are returned to the texture
1185 // cache. This presents a potential hazard for buffered drawing. However,
1186 // the writePixels that uploads to the scratch will perform a flush so we're
1187 // OK.
1188 DrawCategory category = (DEFER_PATHS) ? kBuffered_DrawCategory :
1189 kUnbuffered_DrawCategory;
1190 GrDrawTarget* target = this->prepareToDraw(paint, category);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +00001191 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com150d2842012-01-12 20:19:56 +00001192 GrDrawState::StageMask stageMask = paint.getActiveStageMask();
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001193
bsalomon@google.com289533a2011-10-27 12:34:25 +00001194 bool prAA = paint.fAntiAlias && !this->getRenderTarget()->isMultisampled();
1195
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001196 // An Assumption here is that path renderer would use some form of tweaking
1197 // the src color (either the input alpha or in the frag shader) to implement
1198 // aa. If we have some future driver-mojo path AA that can do the right
1199 // thing WRT to the blend then we'll need some query on the PR.
1200 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001201#if GR_DEBUG
bsalomon@google.com979432b2011-11-05 21:38:22 +00001202 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001203#endif
bsalomon@google.com289533a2011-10-27 12:34:25 +00001204 prAA = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001205 }
bsalomon@google.com289533a2011-10-27 12:34:25 +00001206
robertphillips@google.com72176b22012-05-23 13:19:12 +00001207 GrPathRenderer* pr = this->getPathRenderer(path, fill, target, prAA, true);
bsalomon@google.com30085192011-08-19 15:42:31 +00001208 if (NULL == pr) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001209#if GR_DEBUG
bsalomon@google.com30085192011-08-19 15:42:31 +00001210 GrPrintf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001211#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001212 return;
1213 }
1214
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001215 pr->drawPath(path, fill, translate, target, stageMask, prAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001216}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001217
bsalomon@google.com27847de2011-02-22 20:59:41 +00001218////////////////////////////////////////////////////////////////////////////////
1219
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001220void GrContext::flush(int flagsBitfield) {
1221 if (kDiscard_FlushBit & flagsBitfield) {
1222 fDrawBuffer->reset();
1223 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001224 this->flushDrawBuffer();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001225 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001226 if (kForceCurrentRenderTarget_FlushBit & flagsBitfield) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001227 fGpu->forceRenderTargetFlush();
1228 }
1229}
1230
bsalomon@google.com27847de2011-02-22 20:59:41 +00001231void GrContext::flushDrawBuffer() {
junov@google.com53a55842011-06-08 22:55:10 +00001232 if (fDrawBuffer) {
robertphillips@google.com58b38182012-05-03 16:29:41 +00001233 // With addition of the AA clip path, flushing the draw buffer can
1234 // result in the generation of an AA clip mask. During this
1235 // process the SW path renderer may be invoked which recusively
1236 // calls this method (via internalWriteTexturePixels) creating
1237 // infinite recursion
1238 GrInOrderDrawBuffer* temp = fDrawBuffer;
1239 fDrawBuffer = NULL;
1240
1241 temp->flushTo(fGpu);
1242
1243 fDrawBuffer = temp;
junov@google.com53a55842011-06-08 22:55:10 +00001244 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001245}
1246
bsalomon@google.com6f379512011-11-16 20:36:03 +00001247void GrContext::internalWriteTexturePixels(GrTexture* texture,
1248 int left, int top,
1249 int width, int height,
1250 GrPixelConfig config,
1251 const void* buffer,
1252 size_t rowBytes,
1253 uint32_t flags) {
1254 SK_TRACE_EVENT0("GrContext::writeTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001255 ASSERT_OWNED_RESOURCE(texture);
1256
bsalomon@google.com6f379512011-11-16 20:36:03 +00001257 if (!(kDontFlush_PixelOpsFlag & flags)) {
1258 this->flush();
1259 }
1260 // TODO: use scratch texture to perform conversion
1261 if (GrPixelConfigIsUnpremultiplied(texture->config()) !=
1262 GrPixelConfigIsUnpremultiplied(config)) {
1263 return;
1264 }
1265
1266 fGpu->writeTexturePixels(texture, left, top, width, height,
1267 config, buffer, rowBytes);
1268}
1269
1270bool GrContext::internalReadTexturePixels(GrTexture* texture,
1271 int left, int top,
1272 int width, int height,
1273 GrPixelConfig config,
1274 void* buffer,
1275 size_t rowBytes,
1276 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001277 SK_TRACE_EVENT0("GrContext::readTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001278 ASSERT_OWNED_RESOURCE(texture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001279
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001280 // TODO: code read pixels for textures that aren't also rendertargets
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001281 GrRenderTarget* target = texture->asRenderTarget();
1282 if (NULL != target) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001283 return this->internalReadRenderTargetPixels(target,
1284 left, top, width, height,
1285 config, buffer, rowBytes,
1286 flags);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001287 } else {
1288 return false;
1289 }
1290}
1291
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001292#include "SkConfig8888.h"
1293
1294namespace {
1295/**
1296 * Converts a GrPixelConfig to a SkCanvas::Config8888. Only byte-per-channel
1297 * formats are representable as Config8888 and so the function returns false
1298 * if the GrPixelConfig has no equivalent Config8888.
1299 */
1300bool grconfig_to_config8888(GrPixelConfig config,
1301 SkCanvas::Config8888* config8888) {
1302 switch (config) {
1303 case kRGBA_8888_PM_GrPixelConfig:
1304 *config8888 = SkCanvas::kRGBA_Premul_Config8888;
1305 return true;
1306 case kRGBA_8888_UPM_GrPixelConfig:
1307 *config8888 = SkCanvas::kRGBA_Unpremul_Config8888;
1308 return true;
1309 case kBGRA_8888_PM_GrPixelConfig:
1310 *config8888 = SkCanvas::kBGRA_Premul_Config8888;
1311 return true;
1312 case kBGRA_8888_UPM_GrPixelConfig:
1313 *config8888 = SkCanvas::kBGRA_Unpremul_Config8888;
1314 return true;
1315 default:
1316 return false;
1317 }
1318}
1319}
1320
bsalomon@google.com6f379512011-11-16 20:36:03 +00001321bool GrContext::internalReadRenderTargetPixels(GrRenderTarget* target,
1322 int left, int top,
1323 int width, int height,
1324 GrPixelConfig config,
1325 void* buffer,
1326 size_t rowBytes,
1327 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001328 SK_TRACE_EVENT0("GrContext::readRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001329 ASSERT_OWNED_RESOURCE(target);
1330
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001331 if (NULL == target) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001332 target = fDrawState->getRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001333 if (NULL == target) {
1334 return false;
1335 }
1336 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001337
bsalomon@google.com6f379512011-11-16 20:36:03 +00001338 if (!(kDontFlush_PixelOpsFlag & flags)) {
1339 this->flush();
1340 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001341
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001342 if (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1343 GrPixelConfigIsUnpremultiplied(config) &&
1344 !fGpu->canPreserveReadWriteUnpremulPixels()) {
1345 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1346 if (!grconfig_to_config8888(target->config(), &srcConfig8888) ||
1347 !grconfig_to_config8888(config, &dstConfig8888)) {
1348 return false;
1349 }
1350 // do read back using target's own config
1351 this->internalReadRenderTargetPixels(target,
1352 left, top,
1353 width, height,
1354 target->config(),
1355 buffer, rowBytes,
1356 kDontFlush_PixelOpsFlag);
1357 // sw convert the pixels to unpremul config
1358 uint32_t* pixels = reinterpret_cast<uint32_t*>(buffer);
1359 SkConvertConfig8888Pixels(pixels, rowBytes, dstConfig8888,
1360 pixels, rowBytes, srcConfig8888,
1361 width, height);
1362 return true;
1363 }
1364
bsalomon@google.comc4364992011-11-07 15:54:49 +00001365 GrTexture* src = target->asTexture();
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001366 bool swapRAndB = NULL != src &&
1367 fGpu->preferredReadPixelsConfig(config) ==
1368 GrPixelConfigSwapRAndB(config);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001369
1370 bool flipY = NULL != src &&
1371 fGpu->readPixelsWillPayForYFlip(target, left, top,
1372 width, height, config,
1373 rowBytes);
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001374 bool alphaConversion = (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1375 GrPixelConfigIsUnpremultiplied(config));
bsalomon@google.comc4364992011-11-07 15:54:49 +00001376
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001377 if (NULL == src && alphaConversion) {
1378 // we should fallback to cpu conversion here. This could happen when
1379 // we were given an external render target by the client that is not
1380 // also a texture (e.g. FBO 0 in GL)
1381 return false;
1382 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001383 // we draw to a scratch texture if any of these conversion are applied
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001384 GrAutoScratchTexture ast;
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001385 if (flipY || swapRAndB || alphaConversion) {
1386 GrAssert(NULL != src);
1387 if (swapRAndB) {
1388 config = GrPixelConfigSwapRAndB(config);
1389 GrAssert(kUnknown_GrPixelConfig != config);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001390 }
1391 // Make the scratch a render target because we don't have a robust
1392 // readTexturePixels as of yet (it calls this function).
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001393 GrTextureDesc desc;
1394 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1395 desc.fWidth = width;
1396 desc.fHeight = height;
1397 desc.fConfig = config;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001398
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001399 // When a full readback is faster than a partial we could always make
1400 // the scratch exactly match the passed rect. However, if we see many
1401 // different size rectangles we will trash our texture cache and pay the
1402 // cost of creating and destroying many textures. So, we only request
1403 // an exact match when the caller is reading an entire RT.
1404 ScratchTexMatch match = kApprox_ScratchTexMatch;
1405 if (0 == left &&
1406 0 == top &&
1407 target->width() == width &&
1408 target->height() == height &&
1409 fGpu->fullReadPixelsIsFasterThanPartial()) {
1410 match = kExact_ScratchTexMatch;
1411 }
1412 ast.set(this, desc, match);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001413 GrTexture* texture = ast.texture();
1414 if (!texture) {
1415 return false;
1416 }
1417 target = texture->asRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001418 GrAssert(NULL != target);
1419
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001420 GrDrawTarget::AutoStateRestore asr(fGpu,
1421 GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001422 GrDrawState* drawState = fGpu->drawState();
1423 drawState->setRenderTarget(target);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001424
bsalomon@google.comc4364992011-11-07 15:54:49 +00001425 GrMatrix matrix;
1426 if (flipY) {
1427 matrix.setTranslate(SK_Scalar1 * left,
1428 SK_Scalar1 * (top + height));
1429 matrix.set(GrMatrix::kMScaleY, -GR_Scalar1);
1430 } else {
1431 matrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
1432 }
1433 matrix.postIDiv(src->width(), src->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001434 drawState->sampler(0)->reset(matrix);
1435 drawState->sampler(0)->setRAndBSwap(swapRAndB);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001436 drawState->setTexture(0, src);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001437 GrRect rect;
1438 rect.setXYWH(0, 0, SK_Scalar1 * width, SK_Scalar1 * height);
1439 fGpu->drawSimpleRect(rect, NULL, 0x1);
1440 left = 0;
1441 top = 0;
1442 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001443 return fGpu->readPixels(target,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001444 left, top, width, height,
1445 config, buffer, rowBytes, flipY);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001446}
1447
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001448void GrContext::resolveRenderTarget(GrRenderTarget* target) {
1449 GrAssert(target);
1450 ASSERT_OWNED_RESOURCE(target);
1451 // In the future we may track whether there are any pending draws to this
1452 // target. We don't today so we always perform a flush. We don't promise
1453 // this to our clients, though.
1454 this->flush();
1455 fGpu->resolveRenderTarget(target);
1456}
1457
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001458void GrContext::copyTexture(GrTexture* src, GrRenderTarget* dst) {
1459 if (NULL == src || NULL == dst) {
1460 return;
1461 }
1462 ASSERT_OWNED_RESOURCE(src);
1463
twiz@google.com1ac87ff2012-04-27 19:39:33 +00001464 // Writes pending to the source texture are not tracked, so a flush
1465 // is required to ensure that the copy captures the most recent contents
1466 // of the source texture. See similar behaviour in
1467 // GrContext::resolveRenderTarget.
1468 this->flush();
1469
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001470 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001471 GrDrawState* drawState = fGpu->drawState();
1472 drawState->setRenderTarget(dst);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001473 GrMatrix sampleM;
1474 sampleM.setIDiv(src->width(), src->height());
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001475 drawState->setTexture(0, src);
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001476 drawState->sampler(0)->reset(sampleM);
bsalomon@google.com5db3b6c2012-01-12 20:38:57 +00001477 SkRect rect = SkRect::MakeXYWH(0, 0,
1478 SK_Scalar1 * src->width(),
1479 SK_Scalar1 * src->height());
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001480 fGpu->drawSimpleRect(rect, NULL, 1 << 0);
1481}
1482
bsalomon@google.com6f379512011-11-16 20:36:03 +00001483void GrContext::internalWriteRenderTargetPixels(GrRenderTarget* target,
1484 int left, int top,
1485 int width, int height,
1486 GrPixelConfig config,
1487 const void* buffer,
1488 size_t rowBytes,
1489 uint32_t flags) {
1490 SK_TRACE_EVENT0("GrContext::writeRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001491 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com6f379512011-11-16 20:36:03 +00001492
1493 if (NULL == target) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001494 target = fDrawState->getRenderTarget();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001495 if (NULL == target) {
1496 return;
1497 }
1498 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001499
1500 // TODO: when underlying api has a direct way to do this we should use it
1501 // (e.g. glDrawPixels on desktop GL).
1502
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001503 // If the RT is also a texture and we don't have to do PM/UPM conversion
1504 // then take the texture path, which we expect to be at least as fast or
1505 // faster since it doesn't use an intermediate texture as we do below.
1506
1507#if !GR_MAC_BUILD
1508 // At least some drivers on the Mac get confused when glTexImage2D is called
1509 // on a texture attached to an FBO. The FBO still sees the old image. TODO:
1510 // determine what OS versions and/or HW is affected.
1511 if (NULL != target->asTexture() &&
1512 GrPixelConfigIsUnpremultiplied(target->config()) ==
1513 GrPixelConfigIsUnpremultiplied(config)) {
1514
1515 this->internalWriteTexturePixels(target->asTexture(),
1516 left, top, width, height,
1517 config, buffer, rowBytes, flags);
1518 return;
1519 }
1520#endif
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001521 if (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1522 GrPixelConfigIsUnpremultiplied(config) &&
1523 !fGpu->canPreserveReadWriteUnpremulPixels()) {
1524 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1525 if (!grconfig_to_config8888(config, &srcConfig8888) ||
1526 !grconfig_to_config8888(target->config(), &dstConfig8888)) {
1527 return;
1528 }
1529 // allocate a tmp buffer and sw convert the pixels to premul
1530 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(width * height);
1531 const uint32_t* src = reinterpret_cast<const uint32_t*>(buffer);
1532 SkConvertConfig8888Pixels(tmpPixels.get(), 4 * width, dstConfig8888,
1533 src, rowBytes, srcConfig8888,
1534 width, height);
1535 // upload the already premul pixels
1536 this->internalWriteRenderTargetPixels(target,
1537 left, top,
1538 width, height,
1539 target->config(),
1540 tmpPixels, 4 * width, flags);
1541 return;
1542 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001543
1544 bool swapRAndB = fGpu->preferredReadPixelsConfig(config) ==
1545 GrPixelConfigSwapRAndB(config);
1546 if (swapRAndB) {
1547 config = GrPixelConfigSwapRAndB(config);
1548 }
1549
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001550 GrTextureDesc desc;
1551 desc.fWidth = width;
1552 desc.fHeight = height;
1553 desc.fConfig = config;
1554
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001555 GrAutoScratchTexture ast(this, desc);
1556 GrTexture* texture = ast.texture();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001557 if (NULL == texture) {
1558 return;
1559 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001560 this->internalWriteTexturePixels(texture, 0, 0, width, height,
1561 config, buffer, rowBytes, flags);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001562
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001563 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001564 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001565
1566 GrMatrix matrix;
1567 matrix.setTranslate(GrIntToScalar(left), GrIntToScalar(top));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001568 drawState->setViewMatrix(matrix);
1569 drawState->setRenderTarget(target);
1570 drawState->setTexture(0, texture);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001571
bsalomon@google.com5c638652011-07-18 19:31:59 +00001572 matrix.setIDiv(texture->width(), texture->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001573 drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode,
1574 GrSamplerState::kNearest_Filter,
1575 matrix);
1576 drawState->sampler(0)->setRAndBSwap(swapRAndB);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001577
tomhudson@google.comb213ed82012-06-25 15:22:12 +00001578 static const GrVertexLayout layout = 0;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001579 static const int VCOUNT = 4;
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001580 // TODO: Use GrGpu::drawRect here
bsalomon@google.com27847de2011-02-22 20:59:41 +00001581 GrDrawTarget::AutoReleaseGeometry geo(fGpu, layout, VCOUNT, 0);
1582 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001583 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +00001584 return;
1585 }
1586 ((GrPoint*)geo.vertices())->setIRectFan(0, 0, width, height);
bsalomon@google.com47059542012-06-06 20:51:20 +00001587 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, VCOUNT);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001588}
1589////////////////////////////////////////////////////////////////////////////////
1590
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001591void GrContext::setPaint(const GrPaint& paint) {
tomhudson@google.com09662062012-06-25 20:18:47 +00001592 // TODO: reenable this once we've cleaned up text state management
1593 //GrAssert(fDrawState->stagesDisabled());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001594
1595 for (int i = 0; i < GrPaint::kMaxTextures; ++i) {
1596 int s = i + GrPaint::kFirstTextureStage;
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001597 ASSERT_OWNED_RESOURCE(paint.getTexture(i));
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001598 if (paint.isTextureStageEnabled(i)) {
1599 fDrawState->setTexture(s, paint.getTexture(i));
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001600 *fDrawState->sampler(s) = paint.getTextureSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001601 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001602 }
1603
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001604 fDrawState->setFirstCoverageStage(GrPaint::kFirstMaskStage);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001605
1606 for (int i = 0; i < GrPaint::kMaxMasks; ++i) {
1607 int s = i + GrPaint::kFirstMaskStage;
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001608 ASSERT_OWNED_RESOURCE(paint.getMask(i));
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001609 if (paint.isMaskStageEnabled(i)) {
1610 fDrawState->setTexture(s, paint.getMask(i));
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001611 *fDrawState->sampler(s) = paint.getMaskSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001612 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001613 }
bsalomon@google.com26936d02012-03-19 13:06:19 +00001614
1615 // disable all stages not accessible via the paint
1616 for (int s = GrPaint::kTotalStages; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001617 fDrawState->setTexture(s, NULL);
bsalomon@google.com26936d02012-03-19 13:06:19 +00001618 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001619
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001620 fDrawState->setColor(paint.fColor);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001621
1622 if (paint.fDither) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001623 fDrawState->enableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001624 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001625 fDrawState->disableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001626 }
1627 if (paint.fAntiAlias) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001628 fDrawState->enableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001629 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001630 fDrawState->disableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001631 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001632 if (paint.fColorMatrixEnabled) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001633 fDrawState->enableState(GrDrawState::kColorMatrix_StateBit);
1634 fDrawState->setColorMatrix(paint.fColorMatrix);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001635 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001636 fDrawState->disableState(GrDrawState::kColorMatrix_StateBit);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001637 }
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001638 fDrawState->setBlendFunc(paint.fSrcBlendCoeff, paint.fDstBlendCoeff);
1639 fDrawState->setColorFilter(paint.fColorFilterColor, paint.fColorFilterXfermode);
1640 fDrawState->setCoverage(paint.fCoverage);
reed@google.com4b2d3f32012-05-15 18:05:50 +00001641#if GR_DEBUG_PARTIAL_COVERAGE_CHECK
bsalomon@google.come79c8152012-03-29 19:07:12 +00001642 if ((paint.getActiveMaskStageMask() || 0xff != paint.fCoverage) &&
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001643 !fGpu->canApplyCoverage()) {
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001644 GrPrintf("Partial pixel coverage will be incorrectly blended.\n");
1645 }
bsalomon@google.com95cd7bd2012-03-28 15:35:05 +00001646#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +00001647}
1648
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001649GrDrawTarget* GrContext::prepareToDraw(const GrPaint& paint,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001650 DrawCategory category) {
1651 if (category != fLastDrawCategory) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001652 this->flushDrawBuffer();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001653 fLastDrawCategory = category;
1654 }
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001655 this->setPaint(paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001656 GrDrawTarget* target = fGpu;
1657 switch (category) {
bsalomon@google.com193395c2012-03-30 17:35:12 +00001658 case kUnbuffered_DrawCategory:
1659 target = fGpu;
1660 break;
1661 case kBuffered_DrawCategory:
1662 target = fDrawBuffer;
1663 fDrawBuffer->setClip(fGpu->getClip());
1664 break;
1665 default:
1666 GrCrash("Unexpected DrawCategory.");
1667 break;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001668 }
1669 return target;
1670}
1671
robertphillips@google.com72176b22012-05-23 13:19:12 +00001672/*
1673 * This method finds a path renderer that can draw the specified path on
1674 * the provided target.
1675 * Due to its expense, the software path renderer has split out so it can
1676 * can be individually allowed/disallowed via the "allowSW" boolean.
1677 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001678GrPathRenderer* GrContext::getPathRenderer(const SkPath& path,
bsalomon@google.com289533a2011-10-27 12:34:25 +00001679 GrPathFill fill,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001680 const GrDrawTarget* target,
robertphillips@google.com72176b22012-05-23 13:19:12 +00001681 bool antiAlias,
1682 bool allowSW) {
bsalomon@google.com30085192011-08-19 15:42:31 +00001683 if (NULL == fPathRendererChain) {
1684 fPathRendererChain =
1685 new GrPathRendererChain(this, GrPathRendererChain::kNone_UsageFlag);
1686 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001687
1688 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(path, fill,
1689 target,
1690 antiAlias);
1691
1692 if (NULL == pr && allowSW) {
1693 if (NULL == fSoftwarePathRenderer) {
1694 fSoftwarePathRenderer = new GrSoftwarePathRenderer(this);
1695 }
1696
1697 pr = fSoftwarePathRenderer;
1698 }
1699
1700 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001701}
1702
bsalomon@google.com27847de2011-02-22 20:59:41 +00001703////////////////////////////////////////////////////////////////////////////////
1704
bsalomon@google.com27847de2011-02-22 20:59:41 +00001705void GrContext::setRenderTarget(GrRenderTarget* target) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001706 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001707 if (fDrawState->getRenderTarget() != target) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001708 this->flush(false);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001709 fDrawState->setRenderTarget(target);
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001710 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001711}
1712
1713GrRenderTarget* GrContext::getRenderTarget() {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001714 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001715}
1716
1717const GrRenderTarget* GrContext::getRenderTarget() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001718 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001719}
1720
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001721bool GrContext::isConfigRenderable(GrPixelConfig config) const {
1722 return fGpu->isConfigRenderable(config);
1723}
1724
bsalomon@google.com27847de2011-02-22 20:59:41 +00001725const GrMatrix& GrContext::getMatrix() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001726 return fDrawState->getViewMatrix();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001727}
1728
1729void GrContext::setMatrix(const GrMatrix& m) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001730 fDrawState->setViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001731}
1732
1733void GrContext::concatMatrix(const GrMatrix& m) const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001734 fDrawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001735}
1736
1737static inline intptr_t setOrClear(intptr_t bits, int shift, intptr_t pred) {
1738 intptr_t mask = 1 << shift;
1739 if (pred) {
1740 bits |= mask;
1741 } else {
1742 bits &= ~mask;
1743 }
1744 return bits;
1745}
1746
bsalomon@google.com583a1e32011-08-17 13:42:46 +00001747GrContext::GrContext(GrGpu* gpu) {
bsalomon@google.comc0af3172012-06-15 14:10:09 +00001748 ++THREAD_INSTANCE_COUNT;
1749
bsalomon@google.com27847de2011-02-22 20:59:41 +00001750 fGpu = gpu;
1751 fGpu->ref();
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001752 fGpu->setContext(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001753
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001754 fDrawState = new GrDrawState();
1755 fGpu->setDrawState(fDrawState);
1756
bsalomon@google.com30085192011-08-19 15:42:31 +00001757 fPathRendererChain = NULL;
robertphillips@google.com72176b22012-05-23 13:19:12 +00001758 fSoftwarePathRenderer = NULL;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001759
bsalomon@google.com07fc0d12012-06-22 15:15:59 +00001760 fTextureCache = new GrResourceCache(MAX_TEXTURE_CACHE_COUNT,
1761 MAX_TEXTURE_CACHE_BYTES);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001762 fFontCache = new GrFontCache(fGpu);
1763
1764 fLastDrawCategory = kUnbuffered_DrawCategory;
1765
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001766 fDrawBuffer = NULL;
1767 fDrawBufferVBAllocPool = NULL;
1768 fDrawBufferIBAllocPool = NULL;
1769
robertphillips@google.comf6747b02012-06-12 00:32:28 +00001770 fAARectRenderer = new GrAARectRenderer;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001771
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001772 this->setupDrawBuffer();
1773}
1774
1775void GrContext::setupDrawBuffer() {
1776
1777 GrAssert(NULL == fDrawBuffer);
1778 GrAssert(NULL == fDrawBufferVBAllocPool);
1779 GrAssert(NULL == fDrawBufferIBAllocPool);
1780
bsalomon@google.com92edd312012-04-04 21:40:21 +00001781#if DEFER_TEXT_RENDERING || BATCH_RECT_TO_RECT || DEFER_PATHS
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001782 fDrawBufferVBAllocPool =
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001783 new GrVertexBufferAllocPool(fGpu, false,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001784 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
1785 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS);
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001786 fDrawBufferIBAllocPool =
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001787 new GrIndexBufferAllocPool(fGpu, false,
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001788 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001789 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS);
1790
bsalomon@google.com471d4712011-08-23 15:45:25 +00001791 fDrawBuffer = new GrInOrderDrawBuffer(fGpu,
1792 fDrawBufferVBAllocPool,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001793 fDrawBufferIBAllocPool);
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001794#endif
1795
1796#if BATCH_RECT_TO_RECT
bsalomon@google.com27847de2011-02-22 20:59:41 +00001797 fDrawBuffer->setQuadIndexBuffer(this->getQuadIndexBuffer());
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001798#endif
bsalomon@google.com1015e032012-06-25 18:41:04 +00001799 if (fDrawBuffer) {
1800 fDrawBuffer->setAutoFlushTarget(fGpu);
1801 fDrawBuffer->setDrawState(fDrawState);
1802 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001803}
1804
bsalomon@google.com27847de2011-02-22 20:59:41 +00001805GrDrawTarget* GrContext::getTextTarget(const GrPaint& paint) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001806#if DEFER_TEXT_RENDERING
bsalomon@google.com193395c2012-03-30 17:35:12 +00001807 return prepareToDraw(paint, kBuffered_DrawCategory);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001808#else
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001809 return prepareToDraw(paint, kUnbuffered_DrawCategory);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001810#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +00001811}
1812
1813const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {
1814 return fGpu->getQuadIndexBuffer();
1815}
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001816
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001817GrTexture* GrContext::gaussianBlur(GrTexture* srcTexture,
1818 GrAutoScratchTexture* temp1,
1819 GrAutoScratchTexture* temp2,
1820 const SkRect& rect,
1821 float sigmaX, float sigmaY) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001822 ASSERT_OWNED_RESOURCE(srcTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001823 GrRenderTarget* oldRenderTarget = this->getRenderTarget();
1824 GrClip oldClip = this->getClip();
1825 GrTexture* origTexture = srcTexture;
1826 GrAutoMatrix avm(this, GrMatrix::I());
1827 SkIRect clearRect;
bsalomon@google.comb505a122012-05-31 18:40:36 +00001828 int scaleFactorX, radiusX;
1829 int scaleFactorY, radiusY;
1830 sigmaX = adjust_sigma(sigmaX, &scaleFactorX, &radiusX);
1831 sigmaY = adjust_sigma(sigmaY, &scaleFactorY, &radiusY);
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001832
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001833 SkRect srcRect(rect);
1834 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
1835 srcRect.roundOut();
robertphillips@google.com8637a362012-04-10 18:32:35 +00001836 scale_rect(&srcRect, static_cast<float>(scaleFactorX),
1837 static_cast<float>(scaleFactorY));
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001838 this->setClip(srcRect);
1839
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001840 GrAssert(kBGRA_8888_PM_GrPixelConfig == srcTexture->config() ||
1841 kRGBA_8888_PM_GrPixelConfig == srcTexture->config() ||
1842 kAlpha_8_GrPixelConfig == srcTexture->config());
1843
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001844 GrTextureDesc desc;
1845 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
1846 desc.fWidth = SkScalarFloorToInt(srcRect.width());
1847 desc.fHeight = SkScalarFloorToInt(srcRect.height());
1848 desc.fConfig = srcTexture->config();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001849
1850 temp1->set(this, desc);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001851 if (temp2) {
1852 temp2->set(this, desc);
1853 }
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001854
1855 GrTexture* dstTexture = temp1->texture();
1856 GrPaint paint;
1857 paint.reset();
1858 paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
1859
1860 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
1861 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1862 srcTexture->height());
1863 this->setRenderTarget(dstTexture->asRenderTarget());
1864 SkRect dstRect(srcRect);
1865 scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
1866 i < scaleFactorY ? 0.5f : 1.0f);
1867 paint.setTexture(0, srcTexture);
1868 this->drawRectToRect(paint, dstRect, srcRect);
1869 srcRect = dstRect;
1870 SkTSwap(srcTexture, dstTexture);
1871 // If temp2 is non-NULL, don't render back to origTexture
robertphillips@google.com972265d2012-06-13 18:49:30 +00001872 if (temp2 && dstTexture == origTexture) {
1873 dstTexture = temp2->texture();
1874 }
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001875 }
1876
robertphillips@google.com7a396332012-05-10 15:11:27 +00001877 SkIRect srcIRect;
1878 srcRect.roundOut(&srcIRect);
1879
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001880 if (sigmaX > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001881 if (scaleFactorX > 1) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001882 // Clear out a radius to the right of the srcRect to prevent the
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001883 // X convolution from reading garbage.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001884 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001885 radiusX, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001886 this->clear(&clearRect, 0x0);
1887 }
1888
1889 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001890 convolve_gaussian(fGpu, srcTexture, srcRect, sigmaX, radiusX,
1891 Gr1DKernelEffect::kX_Direction);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001892 SkTSwap(srcTexture, dstTexture);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001893 if (temp2 && dstTexture == origTexture) {
1894 dstTexture = temp2->texture();
1895 }
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001896 }
1897
1898 if (sigmaY > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001899 if (scaleFactorY > 1 || sigmaX > 0.0f) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001900 // Clear out a radius below the srcRect to prevent the Y
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001901 // convolution from reading garbage.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001902 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001903 srcIRect.width(), radiusY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001904 this->clear(&clearRect, 0x0);
1905 }
1906
1907 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001908 convolve_gaussian(fGpu, srcTexture, srcRect, sigmaY, radiusY,
1909 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001910 SkTSwap(srcTexture, dstTexture);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001911 if (temp2 && dstTexture == origTexture) {
1912 dstTexture = temp2->texture();
1913 }
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001914 }
1915
1916 if (scaleFactorX > 1 || scaleFactorY > 1) {
1917 // Clear one pixel to the right and below, to accommodate bilinear
1918 // upsampling.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001919 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
1920 srcIRect.width() + 1, 1);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001921 this->clear(&clearRect, 0x0);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001922 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
1923 1, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001924 this->clear(&clearRect, 0x0);
1925 // FIXME: This should be mitchell, not bilinear.
1926 paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
1927 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1928 srcTexture->height());
1929 this->setRenderTarget(dstTexture->asRenderTarget());
1930 paint.setTexture(0, srcTexture);
1931 SkRect dstRect(srcRect);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001932 scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001933 this->drawRectToRect(paint, dstRect, srcRect);
1934 srcRect = dstRect;
1935 SkTSwap(srcTexture, dstTexture);
1936 }
1937 this->setRenderTarget(oldRenderTarget);
1938 this->setClip(oldClip);
1939 return srcTexture;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001940}
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001941
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001942GrTexture* GrContext::applyMorphology(GrTexture* srcTexture,
1943 const GrRect& rect,
1944 GrTexture* temp1, GrTexture* temp2,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001945 MorphologyType morphType,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001946 SkISize radius) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001947 ASSERT_OWNED_RESOURCE(srcTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001948 GrRenderTarget* oldRenderTarget = this->getRenderTarget();
1949 GrAutoMatrix avm(this, GrMatrix::I());
1950 GrClip oldClip = this->getClip();
robertphillips@google.com7a396332012-05-10 15:11:27 +00001951 this->setClip(GrRect::MakeWH(SkIntToScalar(srcTexture->width()),
1952 SkIntToScalar(srcTexture->height())));
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001953 if (radius.fWidth > 0) {
1954 this->setRenderTarget(temp1->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001955 apply_morphology(fGpu, srcTexture, rect, radius.fWidth, morphType,
1956 Gr1DKernelEffect::kX_Direction);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001957 SkIRect clearRect = SkIRect::MakeXYWH(
1958 SkScalarFloorToInt(rect.fLeft),
1959 SkScalarFloorToInt(rect.fBottom),
1960 SkScalarFloorToInt(rect.width()),
1961 radius.fHeight);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001962 this->clear(&clearRect, 0x0);
1963 srcTexture = temp1;
1964 }
1965 if (radius.fHeight > 0) {
1966 this->setRenderTarget(temp2->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001967 apply_morphology(fGpu, srcTexture, rect, radius.fHeight, morphType,
1968 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001969 srcTexture = temp2;
1970 }
1971 this->setRenderTarget(oldRenderTarget);
1972 this->setClip(oldClip);
1973 return srcTexture;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001974}
bsalomon@google.comc4364992011-11-07 15:54:49 +00001975
1976///////////////////////////////////////////////////////////////////////////////