blob: c426405039df0db230601ca9c7232357f5ea6773 [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
bsalomon@google.com3c4d0322012-04-03 18:04:51 +000030#define DEFER_TEXT_RENDERING 1
bsalomon@google.com27847de2011-02-22 20:59:41 +000031
bsalomon@google.com3c4d0322012-04-03 18:04:51 +000032#define DEFER_PATHS 1
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +000033
bsalomon@google.com3c4d0322012-04-03 18:04:51 +000034#define BATCH_RECT_TO_RECT (1 && !GR_STATIC_RECT_VB)
bsalomon@google.com27847de2011-02-22 20:59:41 +000035
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +000036#define MAX_BLUR_SIGMA 4.0f
37
bsalomon@google.comd46e2422011-09-23 17:40:07 +000038// When we're using coverage AA but the blend is incompatible (given gpu
39// limitations) should we disable AA or draw wrong?
bsalomon@google.com950d7a82011-09-28 15:05:33 +000040#define DISABLE_COVERAGE_AA_FOR_BLEND 1
bsalomon@google.comd46e2422011-09-23 17:40:07 +000041
reed@google.com4b2d3f32012-05-15 18:05:50 +000042#if GR_DEBUG
43 // change this to a 1 to see notifications when partial coverage fails
44 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
45#else
46 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
47#endif
48
bsalomon@google.com07fc0d12012-06-22 15:15:59 +000049static const size_t MAX_TEXTURE_CACHE_COUNT = 256;
50static const size_t MAX_TEXTURE_CACHE_BYTES = 16 * 1024 * 1024;
bsalomon@google.com27847de2011-02-22 20:59:41 +000051
bsalomon@google.com60361492012-03-15 17:47:06 +000052static const size_t DRAW_BUFFER_VBPOOL_BUFFER_SIZE = 1 << 15;
bsalomon@google.com27847de2011-02-22 20:59:41 +000053static const int DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS = 4;
54
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +000055// path rendering is the only thing we defer today that uses non-static indices
56static const size_t DRAW_BUFFER_IBPOOL_BUFFER_SIZE = DEFER_PATHS ? 1 << 11 : 0;
57static const int DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS = DEFER_PATHS ? 4 : 0;
bsalomon@google.com27847de2011-02-22 20:59:41 +000058
bsalomon@google.combc4b6542011-11-19 13:56:11 +000059#define ASSERT_OWNED_RESOURCE(R) GrAssert(!(R) || (R)->getContext() == this)
60
bsalomon@google.com05ef5102011-05-02 21:14:59 +000061GrContext* GrContext::Create(GrEngine engine,
62 GrPlatform3DContext context3D) {
bsalomon@google.com27847de2011-02-22 20:59:41 +000063 GrContext* ctx = NULL;
64 GrGpu* fGpu = GrGpu::Create(engine, context3D);
65 if (NULL != fGpu) {
66 ctx = new GrContext(fGpu);
67 fGpu->unref();
68 }
69 return ctx;
70}
71
bsalomon@google.comc0af3172012-06-15 14:10:09 +000072namespace {
73void* CreateThreadInstanceCount() {
74 return new int(0);
75}
76void DeleteThreadInstanceCount(void* v) {
77 delete reinterpret_cast<int*>(v);
78}
79#define THREAD_INSTANCE_COUNT \
80 (*reinterpret_cast<int*>(SkTLS::Get(CreateThreadInstanceCount, \
81 DeleteThreadInstanceCount)))
82
83}
84
85int GrContext::GetThreadInstanceCount() {
86 return THREAD_INSTANCE_COUNT;
87}
88
bsalomon@google.com27847de2011-02-22 20:59:41 +000089GrContext::~GrContext() {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000090 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +000091
92 // Since the gpu can hold scratch textures, give it a chance to let go
93 // of them before freeing the texture cache
94 fGpu->purgeResources();
95
bsalomon@google.com27847de2011-02-22 20:59:41 +000096 delete fTextureCache;
97 delete fFontCache;
98 delete fDrawBuffer;
99 delete fDrawBufferVBAllocPool;
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000100 delete fDrawBufferIBAllocPool;
bsalomon@google.com30085192011-08-19 15:42:31 +0000101
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000102 fAARectRenderer->unref();
103
bsalomon@google.com205d4602011-04-25 12:43:45 +0000104 fGpu->unref();
bsalomon@google.com30085192011-08-19 15:42:31 +0000105 GrSafeUnref(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000106 GrSafeUnref(fSoftwarePathRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000107 fDrawState->unref();
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000108
109 --THREAD_INSTANCE_COUNT;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000110}
111
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000112void GrContext::contextLost() {
junov@google.com53a55842011-06-08 22:55:10 +0000113 contextDestroyed();
114 this->setupDrawBuffer();
115}
116
117void GrContext::contextDestroyed() {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000118 // abandon first to so destructors
119 // don't try to free the resources in the API.
120 fGpu->abandonResources();
121
bsalomon@google.com30085192011-08-19 15:42:31 +0000122 // a path renderer may be holding onto resources that
123 // are now unusable
124 GrSafeSetNull(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000125 GrSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com30085192011-08-19 15:42:31 +0000126
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000127 delete fDrawBuffer;
128 fDrawBuffer = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000129
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000130 delete fDrawBufferVBAllocPool;
131 fDrawBufferVBAllocPool = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000132
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000133 delete fDrawBufferIBAllocPool;
134 fDrawBufferIBAllocPool = NULL;
135
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000136 fAARectRenderer->reset();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000137
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000138 fTextureCache->removeAll();
139 fFontCache->freeAll();
140 fGpu->markContextDirty();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000141}
142
143void GrContext::resetContext() {
144 fGpu->markContextDirty();
145}
146
147void GrContext::freeGpuResources() {
148 this->flush();
robertphillips@google.comff175842012-05-14 19:31:39 +0000149
150 fGpu->purgeResources();
151
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000152 fAARectRenderer->reset();
153
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000154 fTextureCache->removeAll();
155 fFontCache->freeAll();
bsalomon@google.com30085192011-08-19 15:42:31 +0000156 // a path renderer may be holding onto resources
157 GrSafeSetNull(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000158 GrSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000159}
160
twiz@google.com05e70242012-01-27 19:12:00 +0000161size_t GrContext::getGpuTextureCacheBytes() const {
162 return fTextureCache->getCachedResourceBytes();
163}
164
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000165////////////////////////////////////////////////////////////////////////////////
166
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000167int GrContext::PaintStageVertexLayoutBits(
168 const GrPaint& paint,
169 const bool hasTexCoords[GrPaint::kTotalStages]) {
170 int stageMask = paint.getActiveStageMask();
171 int layout = 0;
172 for (int i = 0; i < GrPaint::kTotalStages; ++i) {
173 if ((1 << i) & stageMask) {
174 if (NULL != hasTexCoords && hasTexCoords[i]) {
175 layout |= GrDrawTarget::StageTexCoordVertexLayoutBit(i, i);
176 } else {
177 layout |= GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(i);
178 }
179 }
180 }
181 return layout;
182}
183
184
185////////////////////////////////////////////////////////////////////////////////
186
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000187GrTexture* GrContext::TextureCacheEntry::texture() const {
188 if (NULL == fEntry) {
189 return NULL;
190 } else {
191 return (GrTexture*) fEntry->resource();
192 }
193}
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000194
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000195namespace {
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000196
197// we should never have more than one stencil buffer with same combo of
198// (width,height,samplecount)
199void gen_stencil_key_values(int width, int height,
200 int sampleCnt, uint32_t v[4]) {
201 v[0] = width;
202 v[1] = height;
203 v[2] = sampleCnt;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000204 v[3] = GrResourceKey::kStencilBuffer_TypeBit;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000205}
206
207void gen_stencil_key_values(const GrStencilBuffer* sb,
208 uint32_t v[4]) {
209 gen_stencil_key_values(sb->width(), sb->height(),
210 sb->numSamples(), v);
211}
bsalomon@google.com82c7bd82011-11-09 15:32:29 +0000212
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000213void scale_rect(SkRect* rect, float xScale, float yScale) {
robertphillips@google.com5af56062012-04-27 15:39:52 +0000214 rect->fLeft = SkScalarMul(rect->fLeft, SkFloatToScalar(xScale));
215 rect->fTop = SkScalarMul(rect->fTop, SkFloatToScalar(yScale));
216 rect->fRight = SkScalarMul(rect->fRight, SkFloatToScalar(xScale));
217 rect->fBottom = SkScalarMul(rect->fBottom, SkFloatToScalar(yScale));
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000218}
219
bsalomon@google.comb505a122012-05-31 18:40:36 +0000220float adjust_sigma(float sigma, int *scaleFactor, int *radius) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000221 *scaleFactor = 1;
222 while (sigma > MAX_BLUR_SIGMA) {
223 *scaleFactor *= 2;
224 sigma *= 0.5f;
225 }
bsalomon@google.comb505a122012-05-31 18:40:36 +0000226 *radius = static_cast<int>(ceilf(sigma * 3.0f));
227 GrAssert(*radius <= GrConvolutionEffect::kMaxKernelRadius);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000228 return sigma;
229}
230
231void apply_morphology(GrGpu* gpu,
232 GrTexture* texture,
233 const SkRect& rect,
234 int radius,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000235 GrContext::MorphologyType morphType,
236 Gr1DKernelEffect::Direction direction) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000237
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000238 GrRenderTarget* target = gpu->drawState()->getRenderTarget();
239 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000240 GrDrawState* drawState = gpu->drawState();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000241 drawState->setRenderTarget(target);
242 GrMatrix sampleM;
243 sampleM.setIDiv(texture->width(), texture->height());
bsalomon@google.comb505a122012-05-31 18:40:36 +0000244 drawState->sampler(0)->reset(sampleM);
245 SkAutoTUnref<GrCustomStage> morph(
246 new GrMorphologyEffect(direction, radius, morphType));
247 drawState->sampler(0)->setCustomStage(morph);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000248 drawState->setTexture(0, texture);
249 gpu->drawSimpleRect(rect, NULL, 1 << 0);
250}
251
bsalomon@google.comb505a122012-05-31 18:40:36 +0000252void convolve_gaussian(GrGpu* gpu,
253 GrTexture* texture,
254 const SkRect& rect,
255 float sigma,
256 int radius,
257 Gr1DKernelEffect::Direction direction) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000258 GrRenderTarget* target = gpu->drawState()->getRenderTarget();
259 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000260 GrDrawState* drawState = gpu->drawState();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000261 drawState->setRenderTarget(target);
262 GrMatrix sampleM;
263 sampleM.setIDiv(texture->width(), texture->height());
bsalomon@google.comb505a122012-05-31 18:40:36 +0000264 drawState->sampler(0)->reset(sampleM);
265 SkAutoTUnref<GrConvolutionEffect> conv(new
266 GrConvolutionEffect(direction, radius));
267 conv->setGaussianKernel(sigma);
268 drawState->sampler(0)->setCustomStage(conv);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000269 drawState->setTexture(0, texture);
270 gpu->drawSimpleRect(rect, NULL, 1 << 0);
271}
272
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000273}
274
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000275GrContext::TextureCacheEntry GrContext::findAndLockTexture(
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000276 const GrTextureDesc& desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000277 const GrSamplerState* sampler) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000278 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, sampler, desc, false);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000279 return TextureCacheEntry(fTextureCache->findAndLock(resourceKey,
280 GrResourceCache::kNested_LockType));
281}
282
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000283bool GrContext::isTextureInCache(const GrTextureDesc& desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000284 const GrSamplerState* sampler) const {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000285 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, sampler, desc, false);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000286 return fTextureCache->hasKey(resourceKey);
287}
288
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000289GrResourceEntry* GrContext::addAndLockStencilBuffer(GrStencilBuffer* sb) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000290 ASSERT_OWNED_RESOURCE(sb);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000291 uint32_t v[4];
292 gen_stencil_key_values(sb, v);
293 GrResourceKey resourceKey(v);
294 return fTextureCache->createAndLock(resourceKey, sb);
295}
296
297GrStencilBuffer* GrContext::findStencilBuffer(int width, int height,
298 int sampleCnt) {
299 uint32_t v[4];
300 gen_stencil_key_values(width, height, sampleCnt, v);
301 GrResourceKey resourceKey(v);
302 GrResourceEntry* entry = fTextureCache->findAndLock(resourceKey,
303 GrResourceCache::kSingle_LockType);
304 if (NULL != entry) {
305 GrStencilBuffer* sb = (GrStencilBuffer*) entry->resource();
306 return sb;
307 } else {
308 return NULL;
309 }
310}
311
312void GrContext::unlockStencilBuffer(GrResourceEntry* sbEntry) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000313 ASSERT_OWNED_RESOURCE(sbEntry->resource());
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000314 fTextureCache->unlock(sbEntry);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000315}
316
317static void stretchImage(void* dst,
318 int dstW,
319 int dstH,
320 void* src,
321 int srcW,
322 int srcH,
323 int bpp) {
324 GrFixed dx = (srcW << 16) / dstW;
325 GrFixed dy = (srcH << 16) / dstH;
326
327 GrFixed y = dy >> 1;
328
329 int dstXLimit = dstW*bpp;
330 for (int j = 0; j < dstH; ++j) {
331 GrFixed x = dx >> 1;
332 void* srcRow = (uint8_t*)src + (y>>16)*srcW*bpp;
333 void* dstRow = (uint8_t*)dst + j*dstW*bpp;
334 for (int i = 0; i < dstXLimit; i += bpp) {
335 memcpy((uint8_t*) dstRow + i,
336 (uint8_t*) srcRow + (x>>16)*bpp,
337 bpp);
338 x += dx;
339 }
340 y += dy;
341 }
342}
343
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000344GrContext::TextureCacheEntry GrContext::createAndLockTexture(
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000345 const GrSamplerState* sampler,
346 const GrTextureDesc& desc,
347 void* srcData,
348 size_t rowBytes) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000349 SK_TRACE_EVENT0("GrContext::createAndLockTexture");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000350
351#if GR_DUMP_TEXTURE_UPLOAD
352 GrPrintf("GrContext::createAndLockTexture [%d %d]\n", desc.fWidth, desc.fHeight);
353#endif
354
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000355 TextureCacheEntry entry;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000356
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000357 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, sampler,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000358 desc, false);
359
360 if (GrTexture::NeedsResizing(resourceKey)) {
361 // The desired texture is NPOT and tiled but that isn't supported by
362 // the current hardware. Resize the texture to be a POT
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000363 GrAssert(NULL != sampler);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000364 TextureCacheEntry clampEntry = this->findAndLockTexture(desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000365 NULL);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000366
367 if (NULL == clampEntry.texture()) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000368 clampEntry = this->createAndLockTexture(NULL, desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000369 srcData, rowBytes);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000370 GrAssert(NULL != clampEntry.texture());
371 if (NULL == clampEntry.texture()) {
372 return entry;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000373 }
374 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000375 GrTextureDesc rtDesc = desc;
376 rtDesc.fFlags = rtDesc.fFlags |
377 kRenderTarget_GrTextureFlagBit |
378 kNoStencil_GrTextureFlagBit;
bsalomon@google.com99621082011-11-15 16:47:16 +0000379 rtDesc.fWidth = GrNextPow2(GrMax(desc.fWidth, 64));
380 rtDesc.fHeight = GrNextPow2(GrMax(desc.fHeight, 64));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000381
382 GrTexture* texture = fGpu->createTexture(rtDesc, NULL, 0);
383
384 if (NULL != texture) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000385 GrDrawTarget::AutoStateRestore asr(fGpu,
386 GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000387 GrDrawState* drawState = fGpu->drawState();
388 drawState->setRenderTarget(texture->asRenderTarget());
389 drawState->setTexture(0, clampEntry.texture());
bsalomon@google.com82c7bd82011-11-09 15:32:29 +0000390
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000391 GrSamplerState::Filter filter;
392 // if filtering is not desired then we want to ensure all
393 // texels in the resampled image are copies of texels from
394 // the original.
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000395 if (GrTexture::NeedsFiltering(resourceKey)) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000396 filter = GrSamplerState::kBilinear_Filter;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000397 } else {
398 filter = GrSamplerState::kNearest_Filter;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000399 }
bsalomon@google.com1e266f82011-12-12 16:11:33 +0000400 drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode,
401 filter);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000402
403 static const GrVertexLayout layout =
404 GrDrawTarget::StageTexCoordVertexLayoutBit(0,0);
405 GrDrawTarget::AutoReleaseGeometry arg(fGpu, layout, 4, 0);
406
407 if (arg.succeeded()) {
408 GrPoint* verts = (GrPoint*) arg.vertices();
409 verts[0].setIRectFan(0, 0,
410 texture->width(),
411 texture->height(),
412 2*sizeof(GrPoint));
413 verts[1].setIRectFan(0, 0, 1, 1, 2*sizeof(GrPoint));
bsalomon@google.com47059542012-06-06 20:51:20 +0000414 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000415 0, 4);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000416 entry.set(fTextureCache->createAndLock(resourceKey, texture));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000417 }
bsalomon@google.com1da07462011-03-10 14:51:57 +0000418 texture->releaseRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000419 } else {
420 // TODO: Our CPU stretch doesn't filter. But we create separate
421 // stretched textures when the sampler state is either filtered or
422 // not. Either implement filtered stretch blit on CPU or just create
423 // one when FBO case fails.
424
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000425 rtDesc.fFlags = kNone_GrTextureFlags;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000426 // no longer need to clamp at min RT size.
427 rtDesc.fWidth = GrNextPow2(desc.fWidth);
428 rtDesc.fHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000429 int bpp = GrBytesPerPixel(desc.fConfig);
bsalomon@google.com3582bf92011-06-30 21:32:31 +0000430 SkAutoSMalloc<128*128*4> stretchedPixels(bpp *
bsalomon@google.com27847de2011-02-22 20:59:41 +0000431 rtDesc.fWidth *
432 rtDesc.fHeight);
433 stretchImage(stretchedPixels.get(), rtDesc.fWidth, rtDesc.fHeight,
434 srcData, desc.fWidth, desc.fHeight, bpp);
435
436 size_t stretchedRowBytes = rtDesc.fWidth * bpp;
437
438 GrTexture* texture = fGpu->createTexture(rtDesc,
439 stretchedPixels.get(),
440 stretchedRowBytes);
441 GrAssert(NULL != texture);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000442 entry.set(fTextureCache->createAndLock(resourceKey, texture));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000443 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000444 fTextureCache->unlock(clampEntry.cacheEntry());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000445
446 } else {
447 GrTexture* texture = fGpu->createTexture(desc, srcData, rowBytes);
448 if (NULL != texture) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000449 entry.set(fTextureCache->createAndLock(resourceKey, texture));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000450 }
451 }
452 return entry;
453}
454
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000455GrContext::TextureCacheEntry GrContext::lockScratchTexture(
456 const GrTextureDesc& inDesc,
457 ScratchTexMatch match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000458 GrTextureDesc desc = inDesc;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000459 desc.fClientCacheID = kScratch_CacheID;
460
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000461 if (kExact_ScratchTexMatch != match) {
462 // bin by pow2 with a reasonable min
463 static const int MIN_SIZE = 256;
464 desc.fWidth = GrMax(MIN_SIZE, GrNextPow2(desc.fWidth));
465 desc.fHeight = GrMax(MIN_SIZE, GrNextPow2(desc.fHeight));
466 }
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000467
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000468 GrResourceEntry* entry;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000469 int origWidth = desc.fWidth;
470 int origHeight = desc.fHeight;
471 bool doubledW = false;
472 bool doubledH = false;
473
474 do {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000475 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL, desc, true);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000476 entry = fTextureCache->findAndLock(key,
477 GrResourceCache::kNested_LockType);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000478 // if we miss, relax the fit of the flags...
479 // then try doubling width... then height.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000480 if (NULL != entry || kExact_ScratchTexMatch == match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000481 break;
482 }
483 if (!(desc.fFlags & kRenderTarget_GrTextureFlagBit)) {
484 desc.fFlags = desc.fFlags | kRenderTarget_GrTextureFlagBit;
485 } else if (desc.fFlags & kNoStencil_GrTextureFlagBit) {
486 desc.fFlags = desc.fFlags & ~kNoStencil_GrTextureFlagBit;
487 } else if (!doubledW) {
488 desc.fFlags = inDesc.fFlags;
489 desc.fWidth *= 2;
490 doubledW = true;
491 } else if (!doubledH) {
492 desc.fFlags = inDesc.fFlags;
493 desc.fWidth = origWidth;
494 desc.fHeight *= 2;
495 doubledH = true;
496 } else {
497 break;
498 }
499
500 } while (true);
501
502 if (NULL == entry) {
503 desc.fFlags = inDesc.fFlags;
504 desc.fWidth = origWidth;
505 desc.fHeight = origHeight;
506 GrTexture* texture = fGpu->createTexture(desc, NULL, 0);
507 if (NULL != texture) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000508 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000509 texture->desc(),
510 true);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000511 entry = fTextureCache->createAndLock(key, texture);
512 }
513 }
514
515 // If the caller gives us the same desc/sampler twice we don't want
516 // to return the same texture the second time (unless it was previously
517 // released). So we detach the entry from the cache and reattach at release.
518 if (NULL != entry) {
519 fTextureCache->detach(entry);
520 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000521 return TextureCacheEntry(entry);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000522}
523
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000524void GrContext::addExistingTextureToCache(GrTexture* texture) {
525
526 if (NULL == texture) {
527 return;
528 }
529
530 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL,
531 texture->desc(),
532 true);
533 fTextureCache->attach(key, texture);
534}
535
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000536void GrContext::unlockTexture(TextureCacheEntry entry) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000537 ASSERT_OWNED_RESOURCE(entry.texture());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000538 // If this is a scratch texture we detached it from the cache
539 // while it was locked (to avoid two callers simultaneously getting
540 // the same texture).
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000541 if (GrTexture::IsScratchTexture(entry.cacheEntry()->key())) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000542 fTextureCache->reattachAndUnlock(entry.cacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000543 } else {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000544 fTextureCache->unlock(entry.cacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000545 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000546}
547
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000548void GrContext::freeEntry(TextureCacheEntry entry) {
549 ASSERT_OWNED_RESOURCE(entry.texture());
550
551 fTextureCache->freeEntry(entry.cacheEntry());
552}
553
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000554GrTexture* GrContext::createUncachedTexture(const GrTextureDesc& descIn,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000555 void* srcData,
556 size_t rowBytes) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000557 GrTextureDesc descCopy = descIn;
558 descCopy.fClientCacheID = kUncached_CacheID;
559 return fGpu->createTexture(descCopy, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000560}
561
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000562void GrContext::getTextureCacheLimits(int* maxTextures,
563 size_t* maxTextureBytes) const {
564 fTextureCache->getLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000565}
566
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000567void GrContext::setTextureCacheLimits(int maxTextures, size_t maxTextureBytes) {
568 fTextureCache->setLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000569}
570
bsalomon@google.com91958362011-06-13 17:58:13 +0000571int GrContext::getMaxTextureSize() const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000572 return fGpu->getCaps().fMaxTextureSize;
bsalomon@google.com91958362011-06-13 17:58:13 +0000573}
574
575int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000576 return fGpu->getCaps().fMaxRenderTargetSize;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000577}
578
579///////////////////////////////////////////////////////////////////////////////
580
bsalomon@google.come269f212011-11-07 13:29:52 +0000581GrTexture* GrContext::createPlatformTexture(const GrPlatformTextureDesc& desc) {
582 return fGpu->createPlatformTexture(desc);
583}
584
585GrRenderTarget* GrContext::createPlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
586 return fGpu->createPlatformRenderTarget(desc);
587}
588
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000589///////////////////////////////////////////////////////////////////////////////
590
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000591bool GrContext::supportsIndex8PixelConfig(const GrSamplerState* sampler,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000592 int width, int height) const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000593 const GrDrawTarget::Caps& caps = fGpu->getCaps();
594 if (!caps.f8BitPaletteSupport) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000595 return false;
596 }
597
bsalomon@google.com27847de2011-02-22 20:59:41 +0000598 bool isPow2 = GrIsPow2(width) && GrIsPow2(height);
599
600 if (!isPow2) {
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000601 bool tiled = NULL != sampler &&
602 (sampler->getWrapX() != GrSamplerState::kClamp_WrapMode ||
603 sampler->getWrapY() != GrSamplerState::kClamp_WrapMode);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000604 if (tiled && !caps.fNPOTTextureTileSupport) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000605 return false;
606 }
607 }
608 return true;
609}
610
611////////////////////////////////////////////////////////////////////////////////
612
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000613const GrClip& GrContext::getClip() const { return fGpu->getClip(); }
614
bsalomon@google.com27847de2011-02-22 20:59:41 +0000615void GrContext::setClip(const GrClip& clip) {
616 fGpu->setClip(clip);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000617 fDrawState->enableState(GrDrawState::kClip_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000618}
619
620void GrContext::setClip(const GrIRect& rect) {
621 GrClip clip;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000622 clip.setFromIRect(rect);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000623 fGpu->setClip(clip);
624}
625
626////////////////////////////////////////////////////////////////////////////////
627
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000628void GrContext::clear(const GrIRect* rect,
629 const GrColor color,
630 GrRenderTarget* target) {
bsalomon@google.com398109c2011-04-14 18:40:27 +0000631 this->flush();
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000632 fGpu->clear(rect, color, target);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000633}
634
635void GrContext::drawPaint(const GrPaint& paint) {
636 // set rect to be big enough to fill the space, but not super-huge, so we
637 // don't overflow fixed-point implementations
bsalomon@google.comd302f142011-03-03 13:54:13 +0000638 GrRect r;
639 r.setLTRB(0, 0,
640 GrIntToScalar(getRenderTarget()->width()),
641 GrIntToScalar(getRenderTarget()->height()));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000642 GrMatrix inverse;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000643 SkTLazy<GrPaint> tmpPaint;
644 const GrPaint* p = &paint;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000645 GrAutoMatrix am;
646
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000647 // We attempt to map r by the inverse matrix and draw that. mapRect will
648 // map the four corners and bound them with a new rect. This will not
649 // produce a correct result for some perspective matrices.
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000650 if (!this->getMatrix().hasPerspective()) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000651 if (!fDrawState->getViewInverse(&inverse)) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000652 GrPrintf("Could not invert matrix");
653 return;
654 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000655 inverse.mapRect(&r);
656 } else {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000657 if (paint.getActiveMaskStageMask() || paint.getActiveStageMask()) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000658 if (!fDrawState->getViewInverse(&inverse)) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000659 GrPrintf("Could not invert matrix");
660 return;
661 }
662 tmpPaint.set(paint);
663 tmpPaint.get()->preConcatActiveSamplerMatrices(inverse);
664 p = tmpPaint.get();
665 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000666 am.set(this, GrMatrix::I());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000667 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000668 // by definition this fills the entire clip, no need for AA
669 if (paint.fAntiAlias) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000670 if (!tmpPaint.isValid()) {
671 tmpPaint.set(paint);
672 p = tmpPaint.get();
673 }
674 GrAssert(p == tmpPaint.get());
675 tmpPaint.get()->fAntiAlias = false;
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000676 }
677 this->drawRect(*p, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000678}
679
bsalomon@google.com205d4602011-04-25 12:43:45 +0000680////////////////////////////////////////////////////////////////////////////////
681
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000682namespace {
683inline bool disable_coverage_aa_for_blend(GrDrawTarget* target) {
684 return DISABLE_COVERAGE_AA_FOR_BLEND && !target->canApplyCoverage();
685}
686}
687
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000688////////////////////////////////////////////////////////////////////////////////
689
bsalomon@google.com27847de2011-02-22 20:59:41 +0000690/* create a triangle strip that strokes the specified triangle. There are 8
691 unique vertices, but we repreat the last 2 to close up. Alternatively we
692 could use an indices array, and then only send 8 verts, but not sure that
693 would be faster.
694 */
bsalomon@google.com205d4602011-04-25 12:43:45 +0000695static void setStrokeRectStrip(GrPoint verts[10], GrRect rect,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000696 GrScalar width) {
697 const GrScalar rad = GrScalarHalf(width);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000698 rect.sort();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000699
700 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
701 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
702 verts[2].set(rect.fRight - rad, rect.fTop + rad);
703 verts[3].set(rect.fRight + rad, rect.fTop - rad);
704 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
705 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
706 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
707 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
708 verts[8] = verts[0];
709 verts[9] = verts[1];
710}
711
reed@google.com20efde72011-05-09 17:00:02 +0000712/**
713 * Returns true if the rects edges are integer-aligned.
714 */
715static bool isIRect(const GrRect& r) {
716 return GrScalarIsInt(r.fLeft) && GrScalarIsInt(r.fTop) &&
717 GrScalarIsInt(r.fRight) && GrScalarIsInt(r.fBottom);
718}
719
bsalomon@google.com205d4602011-04-25 12:43:45 +0000720static bool apply_aa_to_rect(GrDrawTarget* target,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000721 const GrRect& rect,
722 GrScalar width,
723 const GrMatrix* matrix,
724 GrMatrix* combinedMatrix,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000725 GrRect* devRect,
726 bool* useVertexCoverage) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000727 // we use a simple coverage ramp to do aa on axis-aligned rects
728 // we check if the rect will be axis-aligned, and the rect won't land on
729 // integer coords.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000730
bsalomon@google.coma3108262011-10-10 14:08:47 +0000731 // we are keeping around the "tweak the alpha" trick because
732 // it is our only hope for the fixed-pipe implementation.
733 // In a shader implementation we can give a separate coverage input
bsalomon@google.com289533a2011-10-27 12:34:25 +0000734 // TODO: remove this ugliness when we drop the fixed-pipe impl
bsalomon@google.coma3108262011-10-10 14:08:47 +0000735 *useVertexCoverage = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000736 if (!target->canTweakAlphaForCoverage()) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000737 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +0000738#if GR_DEBUG
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000739 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +0000740#endif
bsalomon@google.coma3108262011-10-10 14:08:47 +0000741 return false;
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000742 } else {
743 *useVertexCoverage = true;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000744 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000745 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000746 const GrDrawState& drawState = target->getDrawState();
747 if (drawState.getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000748 return false;
749 }
750
bsalomon@google.com471d4712011-08-23 15:45:25 +0000751 if (0 == width && target->willUseHWAALines()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000752 return false;
753 }
754
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000755 if (!drawState.getViewMatrix().preservesAxisAlignment()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000756 return false;
757 }
758
759 if (NULL != matrix &&
760 !matrix->preservesAxisAlignment()) {
761 return false;
762 }
763
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000764 *combinedMatrix = drawState.getViewMatrix();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000765 if (NULL != matrix) {
766 combinedMatrix->preConcat(*matrix);
767 GrAssert(combinedMatrix->preservesAxisAlignment());
768 }
769
770 combinedMatrix->mapRect(devRect, rect);
771 devRect->sort();
772
773 if (width < 0) {
reed@google.com20efde72011-05-09 17:00:02 +0000774 return !isIRect(*devRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000775 } else {
776 return true;
777 }
778}
779
bsalomon@google.com27847de2011-02-22 20:59:41 +0000780void GrContext::drawRect(const GrPaint& paint,
781 const GrRect& rect,
782 GrScalar width,
783 const GrMatrix* matrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000784 SK_TRACE_EVENT0("GrContext::drawRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000785
786 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
robertphillips@google.com972265d2012-06-13 18:49:30 +0000787 GrDrawState::AutoTextureRelease atr(fDrawState);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000788 int stageMask = paint.getActiveStageMask();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000789
bsalomon@google.com205d4602011-04-25 12:43:45 +0000790 GrRect devRect = rect;
791 GrMatrix combinedMatrix;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000792 bool useVertexCoverage;
bsalomon@google.com289533a2011-10-27 12:34:25 +0000793 bool needAA = paint.fAntiAlias &&
794 !this->getRenderTarget()->isMultisampled();
795 bool doAA = needAA && apply_aa_to_rect(target, rect, width, matrix,
796 &combinedMatrix, &devRect,
797 &useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000798
799 if (doAA) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000800 GrDrawTarget::AutoDeviceCoordDraw adcd(target, stageMask);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000801 if (width >= 0) {
802 GrVec strokeSize;;
803 if (width > 0) {
804 strokeSize.set(width, width);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000805 combinedMatrix.mapVectors(&strokeSize, 1);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000806 strokeSize.setAbs(strokeSize);
807 } else {
808 strokeSize.set(GR_Scalar1, GR_Scalar1);
809 }
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000810 fAARectRenderer->strokeAARect(this->getGpu(), target, devRect,
811 strokeSize, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000812 } else {
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000813 fAARectRenderer->fillAARect(this->getGpu(), target,
814 devRect, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000815 }
816 return;
817 }
818
bsalomon@google.com27847de2011-02-22 20:59:41 +0000819 if (width >= 0) {
820 // TODO: consider making static vertex buffers for these cases.
821 // Hairline could be done by just adding closing vertex to
822 // unitSquareVertexBuffer()
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000823 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL);
824
bsalomon@google.com27847de2011-02-22 20:59:41 +0000825 static const int worstCaseVertCount = 10;
826 GrDrawTarget::AutoReleaseGeometry geo(target, layout, worstCaseVertCount, 0);
827
828 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000829 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000830 return;
831 }
832
833 GrPrimitiveType primType;
834 int vertCount;
835 GrPoint* vertex = geo.positions();
836
837 if (width > 0) {
838 vertCount = 10;
bsalomon@google.com47059542012-06-06 20:51:20 +0000839 primType = kTriangleStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000840 setStrokeRectStrip(vertex, rect, width);
841 } else {
842 // hairline
843 vertCount = 5;
bsalomon@google.com47059542012-06-06 20:51:20 +0000844 primType = kLineStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000845 vertex[0].set(rect.fLeft, rect.fTop);
846 vertex[1].set(rect.fRight, rect.fTop);
847 vertex[2].set(rect.fRight, rect.fBottom);
848 vertex[3].set(rect.fLeft, rect.fBottom);
849 vertex[4].set(rect.fLeft, rect.fTop);
850 }
851
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000852 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000853 if (NULL != matrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000854 GrDrawState* drawState = target->drawState();
855 avmr.set(drawState);
856 drawState->preConcatViewMatrix(*matrix);
857 drawState->preConcatSamplerMatrices(stageMask, *matrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000858 }
859
860 target->drawNonIndexed(primType, 0, vertCount);
861 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000862#if GR_STATIC_RECT_VB
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000863 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL);
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000864 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
865 if (NULL == sqVB) {
866 GrPrintf("Failed to create static rect vb.\n");
867 return;
868 }
869 target->setVertexSourceToBuffer(layout, sqVB);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000870 GrDrawState* drawState = target->drawState();
871 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000872 GrMatrix m;
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000873 m.setAll(rect.width(), 0, rect.fLeft,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000874 0, rect.height(), rect.fTop,
875 0, 0, GrMatrix::I()[8]);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000876
877 if (NULL != matrix) {
878 m.postConcat(*matrix);
879 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000880 drawState->preConcatViewMatrix(m);
881 drawState->preConcatSamplerMatrices(stageMask, m);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000882
bsalomon@google.com47059542012-06-06 20:51:20 +0000883 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000884#else
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000885 target->drawSimpleRect(rect, matrix, stageMask);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000886#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +0000887 }
888}
889
890void GrContext::drawRectToRect(const GrPaint& paint,
891 const GrRect& dstRect,
892 const GrRect& srcRect,
893 const GrMatrix* dstMatrix,
894 const GrMatrix* srcMatrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000895 SK_TRACE_EVENT0("GrContext::drawRectToRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000896
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000897 // srcRect refers to paint's first texture
898 if (NULL == paint.getTexture(0)) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000899 drawRect(paint, dstRect, -1, dstMatrix);
900 return;
901 }
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000902
bsalomon@google.com27847de2011-02-22 20:59:41 +0000903 GR_STATIC_ASSERT(!BATCH_RECT_TO_RECT || !GR_STATIC_RECT_VB);
904
905#if GR_STATIC_RECT_VB
906 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
robertphillips@google.com972265d2012-06-13 18:49:30 +0000907 GrDrawState::AutoTextureRelease atr(fDrawState);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000908 GrDrawState* drawState = target->drawState();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000909 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000910 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000911
912 GrMatrix m;
913
914 m.setAll(dstRect.width(), 0, dstRect.fLeft,
915 0, dstRect.height(), dstRect.fTop,
916 0, 0, GrMatrix::I()[8]);
917 if (NULL != dstMatrix) {
918 m.postConcat(*dstMatrix);
919 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000920 drawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000921
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000922 // srcRect refers to first stage
923 int otherStageMask = paint.getActiveStageMask() &
924 (~(1 << GrPaint::kFirstTextureStage));
925 if (otherStageMask) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000926 drawState->preConcatSamplerMatrices(otherStageMask, m);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000927 }
928
bsalomon@google.com27847de2011-02-22 20:59:41 +0000929 m.setAll(srcRect.width(), 0, srcRect.fLeft,
930 0, srcRect.height(), srcRect.fTop,
931 0, 0, GrMatrix::I()[8]);
932 if (NULL != srcMatrix) {
933 m.postConcat(*srcMatrix);
934 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000935 drawState->sampler(GrPaint::kFirstTextureStage)->preConcatMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000936
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000937 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
938 if (NULL == sqVB) {
939 GrPrintf("Failed to create static rect vb.\n");
940 return;
941 }
942 target->setVertexSourceToBuffer(layout, sqVB);
bsalomon@google.com47059542012-06-06 20:51:20 +0000943 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000944#else
945
946 GrDrawTarget* target;
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000947#if BATCH_RECT_TO_RECT
bsalomon@google.com27847de2011-02-22 20:59:41 +0000948 target = this->prepareToDraw(paint, kBuffered_DrawCategory);
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000949#else
bsalomon@google.com27847de2011-02-22 20:59:41 +0000950 target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
951#endif
robertphillips@google.com972265d2012-06-13 18:49:30 +0000952 GrDrawState::AutoTextureRelease atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000953
tomhudson@google.com93813632011-10-27 20:21:16 +0000954 const GrRect* srcRects[GrDrawState::kNumStages] = {NULL};
955 const GrMatrix* srcMatrices[GrDrawState::kNumStages] = {NULL};
bsalomon@google.com27847de2011-02-22 20:59:41 +0000956 srcRects[0] = &srcRect;
957 srcMatrices[0] = srcMatrix;
958
959 target->drawRect(dstRect, dstMatrix, 1, srcRects, srcMatrices);
960#endif
961}
962
963void GrContext::drawVertices(const GrPaint& paint,
964 GrPrimitiveType primitiveType,
965 int vertexCount,
966 const GrPoint positions[],
967 const GrPoint texCoords[],
968 const GrColor colors[],
969 const uint16_t indices[],
970 int indexCount) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000971 SK_TRACE_EVENT0("GrContext::drawVertices");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000972
973 GrDrawTarget::AutoReleaseGeometry geo;
974
975 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
robertphillips@google.com972265d2012-06-13 18:49:30 +0000976 GrDrawState::AutoTextureRelease atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000977
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000978 bool hasTexCoords[GrPaint::kTotalStages] = {
979 NULL != texCoords, // texCoordSrc provides explicit stage 0 coords
980 0 // remaining stages use positions
981 };
982
983 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, hasTexCoords);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000984
985 if (NULL != colors) {
986 layout |= GrDrawTarget::kColor_VertexLayoutBit;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000987 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000988 int vertexSize = GrDrawTarget::VertexSize(layout);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000989
990 if (sizeof(GrPoint) != vertexSize) {
991 if (!geo.set(target, layout, vertexCount, 0)) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000992 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000993 return;
994 }
tomhudson@google.com93813632011-10-27 20:21:16 +0000995 int texOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000996 int colorOffset;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000997 GrDrawTarget::VertexSizeAndOffsetsByIdx(layout,
998 texOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000999 &colorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +00001000 NULL,
1001 NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001002 void* curVertex = geo.vertices();
1003
1004 for (int i = 0; i < vertexCount; ++i) {
1005 *((GrPoint*)curVertex) = positions[i];
1006
1007 if (texOffsets[0] > 0) {
1008 *(GrPoint*)((intptr_t)curVertex + texOffsets[0]) = texCoords[i];
1009 }
1010 if (colorOffset > 0) {
1011 *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i];
1012 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001013 curVertex = (void*)((intptr_t)curVertex + vertexSize);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001014 }
1015 } else {
1016 target->setVertexSourceToArray(layout, positions, vertexCount);
1017 }
1018
bsalomon@google.com91958362011-06-13 17:58:13 +00001019 // we don't currently apply offscreen AA to this path. Need improved
1020 // management of GrDrawTarget's geometry to avoid copying points per-tile.
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001021
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001022 if (NULL != indices) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001023 target->setIndexSourceToArray(indices, indexCount);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001024 target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001025 } else {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001026 target->drawNonIndexed(primitiveType, 0, vertexCount);
1027 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001028}
1029
bsalomon@google.com06afe7b2011-04-26 15:31:40 +00001030///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com150d2842012-01-12 20:19:56 +00001031namespace {
1032
bsalomon@google.com93c96602012-04-27 13:05:21 +00001033struct CircleVertex {
1034 GrPoint fPos;
1035 GrPoint fCenter;
1036 GrScalar fOuterRadius;
1037 GrScalar fInnerRadius;
1038};
1039
1040/* Returns true if will map a circle to another circle. This can be true
1041 * if the matrix only includes square-scale, rotation, translation.
1042 */
1043inline bool isSimilarityTransformation(const SkMatrix& matrix,
1044 SkScalar tol = SK_ScalarNearlyZero) {
1045 if (matrix.isIdentity() || matrix.getType() == SkMatrix::kTranslate_Mask) {
1046 return true;
1047 }
1048 if (matrix.hasPerspective()) {
1049 return false;
1050 }
1051
1052 SkScalar mx = matrix.get(SkMatrix::kMScaleX);
1053 SkScalar sx = matrix.get(SkMatrix::kMSkewX);
1054 SkScalar my = matrix.get(SkMatrix::kMScaleY);
1055 SkScalar sy = matrix.get(SkMatrix::kMSkewY);
1056
1057 if (mx == 0 && sx == 0 && my == 0 && sy == 0) {
1058 return false;
1059 }
1060
1061 // it has scales or skews, but it could also be rotation, check it out.
1062 SkVector vec[2];
1063 vec[0].set(mx, sx);
1064 vec[1].set(sy, my);
1065
1066 return SkScalarNearlyZero(vec[0].dot(vec[1]), SkScalarSquare(tol)) &&
1067 SkScalarNearlyEqual(vec[0].lengthSqd(), vec[1].lengthSqd(),
1068 SkScalarSquare(tol));
1069}
1070
1071}
1072
1073// TODO: strokeWidth can't be larger than zero right now.
1074// It will be fixed when drawPath() can handle strokes.
1075void GrContext::drawOval(const GrPaint& paint,
1076 const GrRect& rect,
1077 SkScalar strokeWidth) {
1078 DrawCategory category = (DEFER_PATHS) ? kBuffered_DrawCategory :
1079 kUnbuffered_DrawCategory;
1080 GrDrawTarget* target = this->prepareToDraw(paint, category);
robertphillips@google.com972265d2012-06-13 18:49:30 +00001081 GrDrawState::AutoTextureRelease atr(fDrawState);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001082 GrDrawState* drawState = target->drawState();
1083 GrMatrix vm = drawState->getViewMatrix();
1084
1085 if (!isSimilarityTransformation(vm) ||
1086 !paint.fAntiAlias ||
1087 rect.height() != rect.width()) {
1088 SkPath path;
1089 path.addOval(rect);
1090 GrPathFill fill = (strokeWidth == 0) ?
bsalomon@google.com47059542012-06-06 20:51:20 +00001091 kHairLine_GrPathFill : kWinding_GrPathFill;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001092 this->internalDrawPath(paint, path, fill, NULL);
1093 return;
1094 }
1095
1096 const GrRenderTarget* rt = drawState->getRenderTarget();
1097 if (NULL == rt) {
1098 return;
1099 }
1100
1101 GrDrawTarget::AutoDeviceCoordDraw adcd(target, paint.getActiveStageMask());
1102
1103 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL);
1104 layout |= GrDrawTarget::kEdge_VertexLayoutBit;
1105 GrAssert(sizeof(CircleVertex) == GrDrawTarget::VertexSize(layout));
1106
1107 GrPoint center = GrPoint::Make(rect.centerX(), rect.centerY());
1108 GrScalar radius = SkScalarHalf(rect.width());
1109
1110 vm.mapPoints(&center, 1);
1111 radius = vm.mapRadius(radius);
1112
1113 GrScalar outerRadius = radius;
1114 GrScalar innerRadius = 0;
1115 SkScalar halfWidth = 0;
1116 if (strokeWidth == 0) {
1117 halfWidth = SkScalarHalf(SK_Scalar1);
1118
1119 outerRadius += halfWidth;
1120 innerRadius = SkMaxScalar(0, radius - halfWidth);
1121 }
1122
1123 GrDrawTarget::AutoReleaseGeometry geo(target, layout, 4, 0);
1124 if (!geo.succeeded()) {
1125 GrPrintf("Failed to get space for vertices!\n");
1126 return;
1127 }
1128
1129 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
1130
robertphillips@google.coma0a66c12012-06-22 13:14:29 +00001131 // The fragment shader will extend the radius out half a pixel
1132 // to antialias. Expand the drawn rect here so all the pixels
1133 // will be captured.
1134 SkScalar L = center.fX - outerRadius - SkFloatToScalar(0.5f);
1135 SkScalar R = center.fX + outerRadius + SkFloatToScalar(0.5f);
1136 SkScalar T = center.fY - outerRadius - SkFloatToScalar(0.5f);
1137 SkScalar B = center.fY + outerRadius + SkFloatToScalar(0.5f);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001138
1139 verts[0].fPos = SkPoint::Make(L, T);
1140 verts[1].fPos = SkPoint::Make(R, T);
1141 verts[2].fPos = SkPoint::Make(L, B);
1142 verts[3].fPos = SkPoint::Make(R, B);
1143
1144 for (int i = 0; i < 4; ++i) {
1145 // this goes to fragment shader, it should be in y-points-up space.
1146 verts[i].fCenter = SkPoint::Make(center.fX, rt->height() - center.fY);
1147
1148 verts[i].fOuterRadius = outerRadius;
1149 verts[i].fInnerRadius = innerRadius;
1150 }
1151
1152 drawState->setVertexEdgeType(GrDrawState::kCircle_EdgeType);
bsalomon@google.com47059542012-06-06 20:51:20 +00001153 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4);
bsalomon@google.com150d2842012-01-12 20:19:56 +00001154}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001155
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001156void GrContext::drawPath(const GrPaint& paint, const SkPath& path,
reed@google.com07f3ee12011-05-16 17:21:57 +00001157 GrPathFill fill, const GrPoint* translate) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001158
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001159 if (path.isEmpty()) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001160 if (GrIsFillInverted(fill)) {
1161 this->drawPaint(paint);
1162 }
1163 return;
1164 }
1165
bsalomon@google.com93c96602012-04-27 13:05:21 +00001166 SkRect ovalRect;
1167 if (!GrIsFillInverted(fill) && path.isOval(&ovalRect)) {
1168 if (translate) {
1169 ovalRect.offset(*translate);
1170 }
bsalomon@google.com47059542012-06-06 20:51:20 +00001171 SkScalar width = (fill == kHairLine_GrPathFill) ? 0 : -SK_Scalar1;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001172 this->drawOval(paint, ovalRect, width);
1173 return;
1174 }
1175
1176 internalDrawPath(paint, path, fill, translate);
1177}
1178
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001179void GrContext::internalDrawPath(const GrPaint& paint, const SkPath& path,
bsalomon@google.com93c96602012-04-27 13:05:21 +00001180 GrPathFill fill, const GrPoint* translate) {
1181
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001182 // Note that below we may sw-rasterize the path into a scratch texture.
1183 // Scratch textures can be recycled after they are returned to the texture
1184 // cache. This presents a potential hazard for buffered drawing. However,
1185 // the writePixels that uploads to the scratch will perform a flush so we're
1186 // OK.
1187 DrawCategory category = (DEFER_PATHS) ? kBuffered_DrawCategory :
1188 kUnbuffered_DrawCategory;
1189 GrDrawTarget* target = this->prepareToDraw(paint, category);
robertphillips@google.com972265d2012-06-13 18:49:30 +00001190 GrDrawState::AutoTextureRelease atr(fDrawState);
bsalomon@google.com150d2842012-01-12 20:19:56 +00001191 GrDrawState::StageMask stageMask = paint.getActiveStageMask();
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001192
bsalomon@google.com289533a2011-10-27 12:34:25 +00001193 bool prAA = paint.fAntiAlias && !this->getRenderTarget()->isMultisampled();
1194
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001195 // An Assumption here is that path renderer would use some form of tweaking
1196 // the src color (either the input alpha or in the frag shader) to implement
1197 // aa. If we have some future driver-mojo path AA that can do the right
1198 // thing WRT to the blend then we'll need some query on the PR.
1199 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001200#if GR_DEBUG
bsalomon@google.com979432b2011-11-05 21:38:22 +00001201 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001202#endif
bsalomon@google.com289533a2011-10-27 12:34:25 +00001203 prAA = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001204 }
bsalomon@google.com289533a2011-10-27 12:34:25 +00001205
robertphillips@google.com72176b22012-05-23 13:19:12 +00001206 GrPathRenderer* pr = this->getPathRenderer(path, fill, target, prAA, true);
bsalomon@google.com30085192011-08-19 15:42:31 +00001207 if (NULL == pr) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001208#if GR_DEBUG
bsalomon@google.com30085192011-08-19 15:42:31 +00001209 GrPrintf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001210#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001211 return;
1212 }
1213
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001214 pr->drawPath(path, fill, translate, target, stageMask, prAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001215}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001216
bsalomon@google.com27847de2011-02-22 20:59:41 +00001217////////////////////////////////////////////////////////////////////////////////
1218
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001219void GrContext::flush(int flagsBitfield) {
1220 if (kDiscard_FlushBit & flagsBitfield) {
1221 fDrawBuffer->reset();
1222 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001223 this->flushDrawBuffer();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001224 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001225 if (kForceCurrentRenderTarget_FlushBit & flagsBitfield) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001226 fGpu->forceRenderTargetFlush();
1227 }
1228}
1229
bsalomon@google.com27847de2011-02-22 20:59:41 +00001230void GrContext::flushDrawBuffer() {
junov@google.com53a55842011-06-08 22:55:10 +00001231 if (fDrawBuffer) {
robertphillips@google.com58b38182012-05-03 16:29:41 +00001232 // With addition of the AA clip path, flushing the draw buffer can
1233 // result in the generation of an AA clip mask. During this
1234 // process the SW path renderer may be invoked which recusively
1235 // calls this method (via internalWriteTexturePixels) creating
1236 // infinite recursion
1237 GrInOrderDrawBuffer* temp = fDrawBuffer;
1238 fDrawBuffer = NULL;
1239
1240 temp->flushTo(fGpu);
1241
1242 fDrawBuffer = temp;
junov@google.com53a55842011-06-08 22:55:10 +00001243 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001244}
1245
bsalomon@google.com6f379512011-11-16 20:36:03 +00001246void GrContext::internalWriteTexturePixels(GrTexture* texture,
1247 int left, int top,
1248 int width, int height,
1249 GrPixelConfig config,
1250 const void* buffer,
1251 size_t rowBytes,
1252 uint32_t flags) {
1253 SK_TRACE_EVENT0("GrContext::writeTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001254 ASSERT_OWNED_RESOURCE(texture);
1255
bsalomon@google.com6f379512011-11-16 20:36:03 +00001256 if (!(kDontFlush_PixelOpsFlag & flags)) {
1257 this->flush();
1258 }
1259 // TODO: use scratch texture to perform conversion
1260 if (GrPixelConfigIsUnpremultiplied(texture->config()) !=
1261 GrPixelConfigIsUnpremultiplied(config)) {
1262 return;
1263 }
1264
1265 fGpu->writeTexturePixels(texture, left, top, width, height,
1266 config, buffer, rowBytes);
1267}
1268
1269bool GrContext::internalReadTexturePixels(GrTexture* texture,
1270 int left, int top,
1271 int width, int height,
1272 GrPixelConfig config,
1273 void* buffer,
1274 size_t rowBytes,
1275 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001276 SK_TRACE_EVENT0("GrContext::readTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001277 ASSERT_OWNED_RESOURCE(texture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001278
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001279 // TODO: code read pixels for textures that aren't also rendertargets
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001280 GrRenderTarget* target = texture->asRenderTarget();
1281 if (NULL != target) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001282 return this->internalReadRenderTargetPixels(target,
1283 left, top, width, height,
1284 config, buffer, rowBytes,
1285 flags);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001286 } else {
1287 return false;
1288 }
1289}
1290
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001291#include "SkConfig8888.h"
1292
1293namespace {
1294/**
1295 * Converts a GrPixelConfig to a SkCanvas::Config8888. Only byte-per-channel
1296 * formats are representable as Config8888 and so the function returns false
1297 * if the GrPixelConfig has no equivalent Config8888.
1298 */
1299bool grconfig_to_config8888(GrPixelConfig config,
1300 SkCanvas::Config8888* config8888) {
1301 switch (config) {
1302 case kRGBA_8888_PM_GrPixelConfig:
1303 *config8888 = SkCanvas::kRGBA_Premul_Config8888;
1304 return true;
1305 case kRGBA_8888_UPM_GrPixelConfig:
1306 *config8888 = SkCanvas::kRGBA_Unpremul_Config8888;
1307 return true;
1308 case kBGRA_8888_PM_GrPixelConfig:
1309 *config8888 = SkCanvas::kBGRA_Premul_Config8888;
1310 return true;
1311 case kBGRA_8888_UPM_GrPixelConfig:
1312 *config8888 = SkCanvas::kBGRA_Unpremul_Config8888;
1313 return true;
1314 default:
1315 return false;
1316 }
1317}
1318}
1319
bsalomon@google.com6f379512011-11-16 20:36:03 +00001320bool GrContext::internalReadRenderTargetPixels(GrRenderTarget* target,
1321 int left, int top,
1322 int width, int height,
1323 GrPixelConfig config,
1324 void* buffer,
1325 size_t rowBytes,
1326 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001327 SK_TRACE_EVENT0("GrContext::readRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001328 ASSERT_OWNED_RESOURCE(target);
1329
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001330 if (NULL == target) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001331 target = fDrawState->getRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001332 if (NULL == target) {
1333 return false;
1334 }
1335 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001336
bsalomon@google.com6f379512011-11-16 20:36:03 +00001337 if (!(kDontFlush_PixelOpsFlag & flags)) {
1338 this->flush();
1339 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001340
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001341 if (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1342 GrPixelConfigIsUnpremultiplied(config) &&
1343 !fGpu->canPreserveReadWriteUnpremulPixels()) {
1344 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1345 if (!grconfig_to_config8888(target->config(), &srcConfig8888) ||
1346 !grconfig_to_config8888(config, &dstConfig8888)) {
1347 return false;
1348 }
1349 // do read back using target's own config
1350 this->internalReadRenderTargetPixels(target,
1351 left, top,
1352 width, height,
1353 target->config(),
1354 buffer, rowBytes,
1355 kDontFlush_PixelOpsFlag);
1356 // sw convert the pixels to unpremul config
1357 uint32_t* pixels = reinterpret_cast<uint32_t*>(buffer);
1358 SkConvertConfig8888Pixels(pixels, rowBytes, dstConfig8888,
1359 pixels, rowBytes, srcConfig8888,
1360 width, height);
1361 return true;
1362 }
1363
bsalomon@google.comc4364992011-11-07 15:54:49 +00001364 GrTexture* src = target->asTexture();
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001365 bool swapRAndB = NULL != src &&
1366 fGpu->preferredReadPixelsConfig(config) ==
1367 GrPixelConfigSwapRAndB(config);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001368
1369 bool flipY = NULL != src &&
1370 fGpu->readPixelsWillPayForYFlip(target, left, top,
1371 width, height, config,
1372 rowBytes);
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001373 bool alphaConversion = (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1374 GrPixelConfigIsUnpremultiplied(config));
bsalomon@google.comc4364992011-11-07 15:54:49 +00001375
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001376 if (NULL == src && alphaConversion) {
1377 // we should fallback to cpu conversion here. This could happen when
1378 // we were given an external render target by the client that is not
1379 // also a texture (e.g. FBO 0 in GL)
1380 return false;
1381 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001382 // we draw to a scratch texture if any of these conversion are applied
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001383 GrAutoScratchTexture ast;
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001384 if (flipY || swapRAndB || alphaConversion) {
1385 GrAssert(NULL != src);
1386 if (swapRAndB) {
1387 config = GrPixelConfigSwapRAndB(config);
1388 GrAssert(kUnknown_GrPixelConfig != config);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001389 }
1390 // Make the scratch a render target because we don't have a robust
1391 // readTexturePixels as of yet (it calls this function).
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001392 GrTextureDesc desc;
1393 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1394 desc.fWidth = width;
1395 desc.fHeight = height;
1396 desc.fConfig = config;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001397
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001398 // When a full readback is faster than a partial we could always make
1399 // the scratch exactly match the passed rect. However, if we see many
1400 // different size rectangles we will trash our texture cache and pay the
1401 // cost of creating and destroying many textures. So, we only request
1402 // an exact match when the caller is reading an entire RT.
1403 ScratchTexMatch match = kApprox_ScratchTexMatch;
1404 if (0 == left &&
1405 0 == top &&
1406 target->width() == width &&
1407 target->height() == height &&
1408 fGpu->fullReadPixelsIsFasterThanPartial()) {
1409 match = kExact_ScratchTexMatch;
1410 }
1411 ast.set(this, desc, match);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001412 GrTexture* texture = ast.texture();
1413 if (!texture) {
1414 return false;
1415 }
1416 target = texture->asRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001417 GrAssert(NULL != target);
1418
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001419 GrDrawTarget::AutoStateRestore asr(fGpu,
1420 GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001421 GrDrawState* drawState = fGpu->drawState();
1422 drawState->setRenderTarget(target);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001423
bsalomon@google.comc4364992011-11-07 15:54:49 +00001424 GrMatrix matrix;
1425 if (flipY) {
1426 matrix.setTranslate(SK_Scalar1 * left,
1427 SK_Scalar1 * (top + height));
1428 matrix.set(GrMatrix::kMScaleY, -GR_Scalar1);
1429 } else {
1430 matrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
1431 }
1432 matrix.postIDiv(src->width(), src->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001433 drawState->sampler(0)->reset(matrix);
1434 drawState->sampler(0)->setRAndBSwap(swapRAndB);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001435 drawState->setTexture(0, src);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001436 GrRect rect;
1437 rect.setXYWH(0, 0, SK_Scalar1 * width, SK_Scalar1 * height);
1438 fGpu->drawSimpleRect(rect, NULL, 0x1);
1439 left = 0;
1440 top = 0;
1441 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001442 return fGpu->readPixels(target,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001443 left, top, width, height,
1444 config, buffer, rowBytes, flipY);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001445}
1446
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001447void GrContext::resolveRenderTarget(GrRenderTarget* target) {
1448 GrAssert(target);
1449 ASSERT_OWNED_RESOURCE(target);
1450 // In the future we may track whether there are any pending draws to this
1451 // target. We don't today so we always perform a flush. We don't promise
1452 // this to our clients, though.
1453 this->flush();
1454 fGpu->resolveRenderTarget(target);
1455}
1456
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001457void GrContext::copyTexture(GrTexture* src, GrRenderTarget* dst) {
1458 if (NULL == src || NULL == dst) {
1459 return;
1460 }
1461 ASSERT_OWNED_RESOURCE(src);
1462
twiz@google.com1ac87ff2012-04-27 19:39:33 +00001463 // Writes pending to the source texture are not tracked, so a flush
1464 // is required to ensure that the copy captures the most recent contents
1465 // of the source texture. See similar behaviour in
1466 // GrContext::resolveRenderTarget.
1467 this->flush();
1468
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001469 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001470 GrDrawState* drawState = fGpu->drawState();
1471 drawState->setRenderTarget(dst);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001472 GrMatrix sampleM;
1473 sampleM.setIDiv(src->width(), src->height());
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001474 drawState->setTexture(0, src);
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001475 drawState->sampler(0)->reset(sampleM);
bsalomon@google.com5db3b6c2012-01-12 20:38:57 +00001476 SkRect rect = SkRect::MakeXYWH(0, 0,
1477 SK_Scalar1 * src->width(),
1478 SK_Scalar1 * src->height());
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001479 fGpu->drawSimpleRect(rect, NULL, 1 << 0);
1480}
1481
bsalomon@google.com6f379512011-11-16 20:36:03 +00001482void GrContext::internalWriteRenderTargetPixels(GrRenderTarget* target,
1483 int left, int top,
1484 int width, int height,
1485 GrPixelConfig config,
1486 const void* buffer,
1487 size_t rowBytes,
1488 uint32_t flags) {
1489 SK_TRACE_EVENT0("GrContext::writeRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001490 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com6f379512011-11-16 20:36:03 +00001491
1492 if (NULL == target) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001493 target = fDrawState->getRenderTarget();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001494 if (NULL == target) {
1495 return;
1496 }
1497 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001498
1499 // TODO: when underlying api has a direct way to do this we should use it
1500 // (e.g. glDrawPixels on desktop GL).
1501
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001502 // If the RT is also a texture and we don't have to do PM/UPM conversion
1503 // then take the texture path, which we expect to be at least as fast or
1504 // faster since it doesn't use an intermediate texture as we do below.
1505
1506#if !GR_MAC_BUILD
1507 // At least some drivers on the Mac get confused when glTexImage2D is called
1508 // on a texture attached to an FBO. The FBO still sees the old image. TODO:
1509 // determine what OS versions and/or HW is affected.
1510 if (NULL != target->asTexture() &&
1511 GrPixelConfigIsUnpremultiplied(target->config()) ==
1512 GrPixelConfigIsUnpremultiplied(config)) {
1513
1514 this->internalWriteTexturePixels(target->asTexture(),
1515 left, top, width, height,
1516 config, buffer, rowBytes, flags);
1517 return;
1518 }
1519#endif
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001520 if (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1521 GrPixelConfigIsUnpremultiplied(config) &&
1522 !fGpu->canPreserveReadWriteUnpremulPixels()) {
1523 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1524 if (!grconfig_to_config8888(config, &srcConfig8888) ||
1525 !grconfig_to_config8888(target->config(), &dstConfig8888)) {
1526 return;
1527 }
1528 // allocate a tmp buffer and sw convert the pixels to premul
1529 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(width * height);
1530 const uint32_t* src = reinterpret_cast<const uint32_t*>(buffer);
1531 SkConvertConfig8888Pixels(tmpPixels.get(), 4 * width, dstConfig8888,
1532 src, rowBytes, srcConfig8888,
1533 width, height);
1534 // upload the already premul pixels
1535 this->internalWriteRenderTargetPixels(target,
1536 left, top,
1537 width, height,
1538 target->config(),
1539 tmpPixels, 4 * width, flags);
1540 return;
1541 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001542
1543 bool swapRAndB = fGpu->preferredReadPixelsConfig(config) ==
1544 GrPixelConfigSwapRAndB(config);
1545 if (swapRAndB) {
1546 config = GrPixelConfigSwapRAndB(config);
1547 }
1548
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001549 GrTextureDesc desc;
1550 desc.fWidth = width;
1551 desc.fHeight = height;
1552 desc.fConfig = config;
1553
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001554 GrAutoScratchTexture ast(this, desc);
1555 GrTexture* texture = ast.texture();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001556 if (NULL == texture) {
1557 return;
1558 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001559 this->internalWriteTexturePixels(texture, 0, 0, width, height,
1560 config, buffer, rowBytes, flags);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001561
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001562 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001563 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001564
1565 GrMatrix matrix;
1566 matrix.setTranslate(GrIntToScalar(left), GrIntToScalar(top));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001567 drawState->setViewMatrix(matrix);
1568 drawState->setRenderTarget(target);
1569 drawState->setTexture(0, texture);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001570
bsalomon@google.com5c638652011-07-18 19:31:59 +00001571 matrix.setIDiv(texture->width(), texture->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001572 drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode,
1573 GrSamplerState::kNearest_Filter,
1574 matrix);
1575 drawState->sampler(0)->setRAndBSwap(swapRAndB);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001576
1577 GrVertexLayout layout = GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(0);
1578 static const int VCOUNT = 4;
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001579 // TODO: Use GrGpu::drawRect here
bsalomon@google.com27847de2011-02-22 20:59:41 +00001580 GrDrawTarget::AutoReleaseGeometry geo(fGpu, layout, VCOUNT, 0);
1581 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001582 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +00001583 return;
1584 }
1585 ((GrPoint*)geo.vertices())->setIRectFan(0, 0, width, height);
bsalomon@google.com47059542012-06-06 20:51:20 +00001586 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, VCOUNT);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001587}
1588////////////////////////////////////////////////////////////////////////////////
1589
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001590void GrContext::setPaint(const GrPaint& paint) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001591
1592 for (int i = 0; i < GrPaint::kMaxTextures; ++i) {
1593 int s = i + GrPaint::kFirstTextureStage;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001594 fDrawState->setTexture(s, paint.getTexture(i));
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001595 ASSERT_OWNED_RESOURCE(paint.getTexture(i));
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001596 if (paint.getTexture(i)) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001597 *fDrawState->sampler(s) = paint.getTextureSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001598 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001599 }
1600
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001601 fDrawState->setFirstCoverageStage(GrPaint::kFirstMaskStage);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001602
1603 for (int i = 0; i < GrPaint::kMaxMasks; ++i) {
1604 int s = i + GrPaint::kFirstMaskStage;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001605 fDrawState->setTexture(s, paint.getMask(i));
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001606 ASSERT_OWNED_RESOURCE(paint.getMask(i));
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001607 if (paint.getMask(i)) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001608 *fDrawState->sampler(s) = paint.getMaskSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001609 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001610 }
bsalomon@google.com26936d02012-03-19 13:06:19 +00001611
1612 // disable all stages not accessible via the paint
1613 for (int s = GrPaint::kTotalStages; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001614 fDrawState->setTexture(s, NULL);
bsalomon@google.com26936d02012-03-19 13:06:19 +00001615 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001616
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001617 fDrawState->setColor(paint.fColor);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001618
1619 if (paint.fDither) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001620 fDrawState->enableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001621 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001622 fDrawState->disableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001623 }
1624 if (paint.fAntiAlias) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001625 fDrawState->enableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001626 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001627 fDrawState->disableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001628 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001629 if (paint.fColorMatrixEnabled) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001630 fDrawState->enableState(GrDrawState::kColorMatrix_StateBit);
1631 fDrawState->setColorMatrix(paint.fColorMatrix);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001632 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001633 fDrawState->disableState(GrDrawState::kColorMatrix_StateBit);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001634 }
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001635 fDrawState->setBlendFunc(paint.fSrcBlendCoeff, paint.fDstBlendCoeff);
1636 fDrawState->setColorFilter(paint.fColorFilterColor, paint.fColorFilterXfermode);
1637 fDrawState->setCoverage(paint.fCoverage);
reed@google.com4b2d3f32012-05-15 18:05:50 +00001638#if GR_DEBUG_PARTIAL_COVERAGE_CHECK
bsalomon@google.come79c8152012-03-29 19:07:12 +00001639 if ((paint.getActiveMaskStageMask() || 0xff != paint.fCoverage) &&
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001640 !fGpu->canApplyCoverage()) {
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001641 GrPrintf("Partial pixel coverage will be incorrectly blended.\n");
1642 }
bsalomon@google.com95cd7bd2012-03-28 15:35:05 +00001643#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +00001644}
1645
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001646GrDrawTarget* GrContext::prepareToDraw(const GrPaint& paint,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001647 DrawCategory category) {
1648 if (category != fLastDrawCategory) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001649 this->flushDrawBuffer();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001650 fLastDrawCategory = category;
1651 }
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001652 this->setPaint(paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001653 GrDrawTarget* target = fGpu;
1654 switch (category) {
bsalomon@google.com193395c2012-03-30 17:35:12 +00001655 case kUnbuffered_DrawCategory:
1656 target = fGpu;
1657 break;
1658 case kBuffered_DrawCategory:
1659 target = fDrawBuffer;
1660 fDrawBuffer->setClip(fGpu->getClip());
1661 break;
1662 default:
1663 GrCrash("Unexpected DrawCategory.");
1664 break;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001665 }
1666 return target;
1667}
1668
robertphillips@google.com72176b22012-05-23 13:19:12 +00001669/*
1670 * This method finds a path renderer that can draw the specified path on
1671 * the provided target.
1672 * Due to its expense, the software path renderer has split out so it can
1673 * can be individually allowed/disallowed via the "allowSW" boolean.
1674 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001675GrPathRenderer* GrContext::getPathRenderer(const SkPath& path,
bsalomon@google.com289533a2011-10-27 12:34:25 +00001676 GrPathFill fill,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001677 const GrDrawTarget* target,
robertphillips@google.com72176b22012-05-23 13:19:12 +00001678 bool antiAlias,
1679 bool allowSW) {
bsalomon@google.com30085192011-08-19 15:42:31 +00001680 if (NULL == fPathRendererChain) {
1681 fPathRendererChain =
1682 new GrPathRendererChain(this, GrPathRendererChain::kNone_UsageFlag);
1683 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001684
1685 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(path, fill,
1686 target,
1687 antiAlias);
1688
1689 if (NULL == pr && allowSW) {
1690 if (NULL == fSoftwarePathRenderer) {
1691 fSoftwarePathRenderer = new GrSoftwarePathRenderer(this);
1692 }
1693
1694 pr = fSoftwarePathRenderer;
1695 }
1696
1697 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001698}
1699
bsalomon@google.com27847de2011-02-22 20:59:41 +00001700////////////////////////////////////////////////////////////////////////////////
1701
bsalomon@google.com27847de2011-02-22 20:59:41 +00001702void GrContext::setRenderTarget(GrRenderTarget* target) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001703 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001704 if (fDrawState->getRenderTarget() != target) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001705 this->flush(false);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001706 fDrawState->setRenderTarget(target);
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001707 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001708}
1709
1710GrRenderTarget* GrContext::getRenderTarget() {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001711 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001712}
1713
1714const GrRenderTarget* GrContext::getRenderTarget() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001715 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001716}
1717
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001718bool GrContext::isConfigRenderable(GrPixelConfig config) const {
1719 return fGpu->isConfigRenderable(config);
1720}
1721
bsalomon@google.com27847de2011-02-22 20:59:41 +00001722const GrMatrix& GrContext::getMatrix() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001723 return fDrawState->getViewMatrix();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001724}
1725
1726void GrContext::setMatrix(const GrMatrix& m) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001727 fDrawState->setViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001728}
1729
1730void GrContext::concatMatrix(const GrMatrix& m) const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001731 fDrawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001732}
1733
1734static inline intptr_t setOrClear(intptr_t bits, int shift, intptr_t pred) {
1735 intptr_t mask = 1 << shift;
1736 if (pred) {
1737 bits |= mask;
1738 } else {
1739 bits &= ~mask;
1740 }
1741 return bits;
1742}
1743
bsalomon@google.com583a1e32011-08-17 13:42:46 +00001744GrContext::GrContext(GrGpu* gpu) {
bsalomon@google.comc0af3172012-06-15 14:10:09 +00001745 ++THREAD_INSTANCE_COUNT;
1746
bsalomon@google.com27847de2011-02-22 20:59:41 +00001747 fGpu = gpu;
1748 fGpu->ref();
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001749 fGpu->setContext(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001750
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001751 fDrawState = new GrDrawState();
1752 fGpu->setDrawState(fDrawState);
1753
bsalomon@google.com30085192011-08-19 15:42:31 +00001754 fPathRendererChain = NULL;
robertphillips@google.com72176b22012-05-23 13:19:12 +00001755 fSoftwarePathRenderer = NULL;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001756
bsalomon@google.com07fc0d12012-06-22 15:15:59 +00001757 fTextureCache = new GrResourceCache(MAX_TEXTURE_CACHE_COUNT,
1758 MAX_TEXTURE_CACHE_BYTES);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001759 fFontCache = new GrFontCache(fGpu);
1760
1761 fLastDrawCategory = kUnbuffered_DrawCategory;
1762
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001763 fDrawBuffer = NULL;
1764 fDrawBufferVBAllocPool = NULL;
1765 fDrawBufferIBAllocPool = NULL;
1766
robertphillips@google.comf6747b02012-06-12 00:32:28 +00001767 fAARectRenderer = new GrAARectRenderer;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001768
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001769 this->setupDrawBuffer();
1770}
1771
1772void GrContext::setupDrawBuffer() {
1773
1774 GrAssert(NULL == fDrawBuffer);
1775 GrAssert(NULL == fDrawBufferVBAllocPool);
1776 GrAssert(NULL == fDrawBufferIBAllocPool);
1777
bsalomon@google.com92edd312012-04-04 21:40:21 +00001778#if DEFER_TEXT_RENDERING || BATCH_RECT_TO_RECT || DEFER_PATHS
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001779 fDrawBufferVBAllocPool =
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001780 new GrVertexBufferAllocPool(fGpu, false,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001781 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
1782 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS);
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001783 fDrawBufferIBAllocPool =
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001784 new GrIndexBufferAllocPool(fGpu, false,
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001785 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001786 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS);
1787
bsalomon@google.com471d4712011-08-23 15:45:25 +00001788 fDrawBuffer = new GrInOrderDrawBuffer(fGpu,
1789 fDrawBufferVBAllocPool,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001790 fDrawBufferIBAllocPool);
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001791#endif
1792
1793#if BATCH_RECT_TO_RECT
bsalomon@google.com27847de2011-02-22 20:59:41 +00001794 fDrawBuffer->setQuadIndexBuffer(this->getQuadIndexBuffer());
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001795#endif
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001796 fDrawBuffer->setAutoFlushTarget(fGpu);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001797 fDrawBuffer->setDrawState(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001798}
1799
bsalomon@google.com27847de2011-02-22 20:59:41 +00001800GrDrawTarget* GrContext::getTextTarget(const GrPaint& paint) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001801#if DEFER_TEXT_RENDERING
bsalomon@google.com193395c2012-03-30 17:35:12 +00001802 return prepareToDraw(paint, kBuffered_DrawCategory);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001803#else
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001804 return prepareToDraw(paint, kUnbuffered_DrawCategory);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001805#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +00001806}
1807
1808const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {
1809 return fGpu->getQuadIndexBuffer();
1810}
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001811
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001812GrTexture* GrContext::gaussianBlur(GrTexture* srcTexture,
1813 GrAutoScratchTexture* temp1,
1814 GrAutoScratchTexture* temp2,
1815 const SkRect& rect,
1816 float sigmaX, float sigmaY) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001817 ASSERT_OWNED_RESOURCE(srcTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001818 GrRenderTarget* oldRenderTarget = this->getRenderTarget();
1819 GrClip oldClip = this->getClip();
1820 GrTexture* origTexture = srcTexture;
1821 GrAutoMatrix avm(this, GrMatrix::I());
1822 SkIRect clearRect;
bsalomon@google.comb505a122012-05-31 18:40:36 +00001823 int scaleFactorX, radiusX;
1824 int scaleFactorY, radiusY;
1825 sigmaX = adjust_sigma(sigmaX, &scaleFactorX, &radiusX);
1826 sigmaY = adjust_sigma(sigmaY, &scaleFactorY, &radiusY);
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001827
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001828 SkRect srcRect(rect);
1829 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
1830 srcRect.roundOut();
robertphillips@google.com8637a362012-04-10 18:32:35 +00001831 scale_rect(&srcRect, static_cast<float>(scaleFactorX),
1832 static_cast<float>(scaleFactorY));
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001833 this->setClip(srcRect);
1834
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001835 GrAssert(kBGRA_8888_PM_GrPixelConfig == srcTexture->config() ||
1836 kRGBA_8888_PM_GrPixelConfig == srcTexture->config() ||
1837 kAlpha_8_GrPixelConfig == srcTexture->config());
1838
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001839 GrTextureDesc desc;
1840 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
1841 desc.fWidth = SkScalarFloorToInt(srcRect.width());
1842 desc.fHeight = SkScalarFloorToInt(srcRect.height());
1843 desc.fConfig = srcTexture->config();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001844
1845 temp1->set(this, desc);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001846 if (temp2) {
1847 temp2->set(this, desc);
1848 }
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001849
1850 GrTexture* dstTexture = temp1->texture();
1851 GrPaint paint;
1852 paint.reset();
1853 paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
1854
1855 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
1856 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1857 srcTexture->height());
1858 this->setRenderTarget(dstTexture->asRenderTarget());
1859 SkRect dstRect(srcRect);
1860 scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
1861 i < scaleFactorY ? 0.5f : 1.0f);
1862 paint.setTexture(0, srcTexture);
1863 this->drawRectToRect(paint, dstRect, srcRect);
1864 srcRect = dstRect;
1865 SkTSwap(srcTexture, dstTexture);
1866 // If temp2 is non-NULL, don't render back to origTexture
robertphillips@google.com972265d2012-06-13 18:49:30 +00001867 if (temp2 && dstTexture == origTexture) {
1868 dstTexture = temp2->texture();
1869 }
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001870 }
1871
robertphillips@google.com7a396332012-05-10 15:11:27 +00001872 SkIRect srcIRect;
1873 srcRect.roundOut(&srcIRect);
1874
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001875 if (sigmaX > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001876 if (scaleFactorX > 1) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001877 // Clear out a radius to the right of the srcRect to prevent the
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001878 // X convolution from reading garbage.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001879 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001880 radiusX, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001881 this->clear(&clearRect, 0x0);
1882 }
1883
1884 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001885 convolve_gaussian(fGpu, srcTexture, srcRect, sigmaX, radiusX,
1886 Gr1DKernelEffect::kX_Direction);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001887 SkTSwap(srcTexture, dstTexture);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001888 if (temp2 && dstTexture == origTexture) {
1889 dstTexture = temp2->texture();
1890 }
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001891 }
1892
1893 if (sigmaY > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001894 if (scaleFactorY > 1 || sigmaX > 0.0f) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001895 // Clear out a radius below the srcRect to prevent the Y
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001896 // convolution from reading garbage.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001897 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001898 srcIRect.width(), radiusY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001899 this->clear(&clearRect, 0x0);
1900 }
1901
1902 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001903 convolve_gaussian(fGpu, srcTexture, srcRect, sigmaY, radiusY,
1904 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001905 SkTSwap(srcTexture, dstTexture);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001906 if (temp2 && dstTexture == origTexture) {
1907 dstTexture = temp2->texture();
1908 }
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001909 }
1910
1911 if (scaleFactorX > 1 || scaleFactorY > 1) {
1912 // Clear one pixel to the right and below, to accommodate bilinear
1913 // upsampling.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001914 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
1915 srcIRect.width() + 1, 1);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001916 this->clear(&clearRect, 0x0);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001917 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
1918 1, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001919 this->clear(&clearRect, 0x0);
1920 // FIXME: This should be mitchell, not bilinear.
1921 paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
1922 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1923 srcTexture->height());
1924 this->setRenderTarget(dstTexture->asRenderTarget());
1925 paint.setTexture(0, srcTexture);
1926 SkRect dstRect(srcRect);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001927 scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001928 this->drawRectToRect(paint, dstRect, srcRect);
1929 srcRect = dstRect;
1930 SkTSwap(srcTexture, dstTexture);
1931 }
1932 this->setRenderTarget(oldRenderTarget);
1933 this->setClip(oldClip);
1934 return srcTexture;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001935}
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001936
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001937GrTexture* GrContext::applyMorphology(GrTexture* srcTexture,
1938 const GrRect& rect,
1939 GrTexture* temp1, GrTexture* temp2,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001940 MorphologyType morphType,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001941 SkISize radius) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001942 ASSERT_OWNED_RESOURCE(srcTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001943 GrRenderTarget* oldRenderTarget = this->getRenderTarget();
1944 GrAutoMatrix avm(this, GrMatrix::I());
1945 GrClip oldClip = this->getClip();
robertphillips@google.com7a396332012-05-10 15:11:27 +00001946 this->setClip(GrRect::MakeWH(SkIntToScalar(srcTexture->width()),
1947 SkIntToScalar(srcTexture->height())));
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001948 if (radius.fWidth > 0) {
1949 this->setRenderTarget(temp1->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001950 apply_morphology(fGpu, srcTexture, rect, radius.fWidth, morphType,
1951 Gr1DKernelEffect::kX_Direction);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001952 SkIRect clearRect = SkIRect::MakeXYWH(
1953 SkScalarFloorToInt(rect.fLeft),
1954 SkScalarFloorToInt(rect.fBottom),
1955 SkScalarFloorToInt(rect.width()),
1956 radius.fHeight);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001957 this->clear(&clearRect, 0x0);
1958 srcTexture = temp1;
1959 }
1960 if (radius.fHeight > 0) {
1961 this->setRenderTarget(temp2->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001962 apply_morphology(fGpu, srcTexture, rect, radius.fHeight, morphType,
1963 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001964 srcTexture = temp2;
1965 }
1966 this->setRenderTarget(oldRenderTarget);
1967 this->setClip(oldClip);
1968 return srcTexture;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001969}
bsalomon@google.comc4364992011-11-07 15:54:49 +00001970
robertphillips@google.com49d9fd52012-05-23 11:44:08 +00001971void GrContext::postClipPush() {
1972 fGpu->postClipPush();
1973}
1974
1975void GrContext::preClipPop() {
1976 fGpu->preClipPop();
1977};
1978
bsalomon@google.comc4364992011-11-07 15:54:49 +00001979///////////////////////////////////////////////////////////////////////////////