blob: 149359f83e022fc94f84ddb58d9bb8a1b6e760f2 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
bsalomon@google.com27847de2011-02-22 20:59:41 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
bsalomon@google.com27847de2011-02-22 20:59:41 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
bsalomon@google.com1fadb202011-12-12 16:10:08 +000010#include "GrContext.h"
11
bsalomon@google.comb505a122012-05-31 18:40:36 +000012#include "effects/GrMorphologyEffect.h"
13#include "effects/GrConvolutionEffect.h"
14
tomhudson@google.com278cbb42011-06-30 19:37:01 +000015#include "GrBufferAllocPool.h"
16#include "GrClipIterator.h"
bsalomon@google.com05ef5102011-05-02 21:14:59 +000017#include "GrGpu.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000018#include "GrIndexBuffer.h"
19#include "GrInOrderDrawBuffer.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000020#include "GrPathRenderer.h"
tomhudson@google.comd22b6e42011-06-24 15:53:40 +000021#include "GrPathUtils.h"
bsalomon@google.com50398bf2011-07-26 20:45:30 +000022#include "GrResourceCache.h"
robertphillips@google.com72176b22012-05-23 13:19:12 +000023#include "GrSoftwarePathRenderer.h"
bsalomon@google.com558a75b2011-08-08 17:01:14 +000024#include "GrStencilBuffer.h"
tomhudson@google.com278cbb42011-06-30 19:37:01 +000025#include "GrTextStrike.h"
bsalomon@google.com8c2fe992011-09-13 15:27:18 +000026#include "SkTLazy.h"
bsalomon@google.comc0af3172012-06-15 14:10:09 +000027#include "SkTLS.h"
tomhudson@google.com0c8d93a2011-07-01 17:08:26 +000028#include "SkTrace.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000029
reed@google.comfa35e3d2012-06-26 20:16:17 +000030SK_DEFINE_INST_COUNT(GrContext)
31SK_DEFINE_INST_COUNT(GrDrawState)
32
bsalomon@google.com3c4d0322012-04-03 18:04:51 +000033#define DEFER_TEXT_RENDERING 1
bsalomon@google.com27847de2011-02-22 20:59:41 +000034
bsalomon@google.com3c4d0322012-04-03 18:04:51 +000035#define DEFER_PATHS 1
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +000036
bsalomon@google.com3c4d0322012-04-03 18:04:51 +000037#define BATCH_RECT_TO_RECT (1 && !GR_STATIC_RECT_VB)
bsalomon@google.com27847de2011-02-22 20:59:41 +000038
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +000039#define MAX_BLUR_SIGMA 4.0f
40
bsalomon@google.comd46e2422011-09-23 17:40:07 +000041// When we're using coverage AA but the blend is incompatible (given gpu
42// limitations) should we disable AA or draw wrong?
bsalomon@google.com950d7a82011-09-28 15:05:33 +000043#define DISABLE_COVERAGE_AA_FOR_BLEND 1
bsalomon@google.comd46e2422011-09-23 17:40:07 +000044
reed@google.com4b2d3f32012-05-15 18:05:50 +000045#if GR_DEBUG
46 // change this to a 1 to see notifications when partial coverage fails
47 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
48#else
49 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
50#endif
51
bsalomon@google.com07fc0d12012-06-22 15:15:59 +000052static const size_t MAX_TEXTURE_CACHE_COUNT = 256;
53static const size_t MAX_TEXTURE_CACHE_BYTES = 16 * 1024 * 1024;
bsalomon@google.com27847de2011-02-22 20:59:41 +000054
bsalomon@google.com60361492012-03-15 17:47:06 +000055static const size_t DRAW_BUFFER_VBPOOL_BUFFER_SIZE = 1 << 15;
bsalomon@google.com27847de2011-02-22 20:59:41 +000056static const int DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS = 4;
57
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +000058// path rendering is the only thing we defer today that uses non-static indices
59static const size_t DRAW_BUFFER_IBPOOL_BUFFER_SIZE = DEFER_PATHS ? 1 << 11 : 0;
60static const int DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS = DEFER_PATHS ? 4 : 0;
bsalomon@google.com27847de2011-02-22 20:59:41 +000061
bsalomon@google.combc4b6542011-11-19 13:56:11 +000062#define ASSERT_OWNED_RESOURCE(R) GrAssert(!(R) || (R)->getContext() == this)
63
bsalomon@google.com05ef5102011-05-02 21:14:59 +000064GrContext* GrContext::Create(GrEngine engine,
65 GrPlatform3DContext context3D) {
bsalomon@google.com27847de2011-02-22 20:59:41 +000066 GrContext* ctx = NULL;
67 GrGpu* fGpu = GrGpu::Create(engine, context3D);
68 if (NULL != fGpu) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +000069 ctx = SkNEW_ARGS(GrContext, (fGpu));
bsalomon@google.com27847de2011-02-22 20:59:41 +000070 fGpu->unref();
71 }
72 return ctx;
73}
74
bsalomon@google.comc0af3172012-06-15 14:10:09 +000075namespace {
76void* CreateThreadInstanceCount() {
tomhudson@google.comc377baf2012-07-09 20:17:56 +000077 return SkNEW_ARGS(int, (0));
bsalomon@google.comc0af3172012-06-15 14:10:09 +000078}
79void DeleteThreadInstanceCount(void* v) {
80 delete reinterpret_cast<int*>(v);
81}
82#define THREAD_INSTANCE_COUNT \
83 (*reinterpret_cast<int*>(SkTLS::Get(CreateThreadInstanceCount, \
84 DeleteThreadInstanceCount)))
85
86}
87
88int GrContext::GetThreadInstanceCount() {
89 return THREAD_INSTANCE_COUNT;
90}
91
bsalomon@google.com27847de2011-02-22 20:59:41 +000092GrContext::~GrContext() {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000093 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +000094
95 // Since the gpu can hold scratch textures, give it a chance to let go
96 // of them before freeing the texture cache
97 fGpu->purgeResources();
98
bsalomon@google.com27847de2011-02-22 20:59:41 +000099 delete fTextureCache;
100 delete fFontCache;
101 delete fDrawBuffer;
102 delete fDrawBufferVBAllocPool;
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000103 delete fDrawBufferIBAllocPool;
bsalomon@google.com30085192011-08-19 15:42:31 +0000104
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000105 fAARectRenderer->unref();
106
bsalomon@google.com205d4602011-04-25 12:43:45 +0000107 fGpu->unref();
bsalomon@google.com30085192011-08-19 15:42:31 +0000108 GrSafeUnref(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000109 GrSafeUnref(fSoftwarePathRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000110 fDrawState->unref();
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000111
112 --THREAD_INSTANCE_COUNT;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000113}
114
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000115void GrContext::contextLost() {
junov@google.com53a55842011-06-08 22:55:10 +0000116 contextDestroyed();
117 this->setupDrawBuffer();
118}
119
120void GrContext::contextDestroyed() {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000121 // abandon first to so destructors
122 // don't try to free the resources in the API.
123 fGpu->abandonResources();
124
bsalomon@google.com30085192011-08-19 15:42:31 +0000125 // a path renderer may be holding onto resources that
126 // are now unusable
127 GrSafeSetNull(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000128 GrSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com30085192011-08-19 15:42:31 +0000129
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000130 delete fDrawBuffer;
131 fDrawBuffer = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000132
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000133 delete fDrawBufferVBAllocPool;
134 fDrawBufferVBAllocPool = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000135
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000136 delete fDrawBufferIBAllocPool;
137 fDrawBufferIBAllocPool = NULL;
138
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000139 fAARectRenderer->reset();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000140
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000141 fTextureCache->removeAll();
142 fFontCache->freeAll();
143 fGpu->markContextDirty();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000144}
145
146void GrContext::resetContext() {
147 fGpu->markContextDirty();
148}
149
150void GrContext::freeGpuResources() {
151 this->flush();
robertphillips@google.comff175842012-05-14 19:31:39 +0000152
153 fGpu->purgeResources();
154
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000155 fAARectRenderer->reset();
156
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000157 fTextureCache->removeAll();
158 fFontCache->freeAll();
bsalomon@google.com30085192011-08-19 15:42:31 +0000159 // a path renderer may be holding onto resources
160 GrSafeSetNull(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000161 GrSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000162}
163
twiz@google.com05e70242012-01-27 19:12:00 +0000164size_t GrContext::getGpuTextureCacheBytes() const {
165 return fTextureCache->getCachedResourceBytes();
166}
167
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000168////////////////////////////////////////////////////////////////////////////////
169
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000170int GrContext::PaintStageVertexLayoutBits(
171 const GrPaint& paint,
172 const bool hasTexCoords[GrPaint::kTotalStages]) {
173 int stageMask = paint.getActiveStageMask();
174 int layout = 0;
175 for (int i = 0; i < GrPaint::kTotalStages; ++i) {
176 if ((1 << i) & stageMask) {
177 if (NULL != hasTexCoords && hasTexCoords[i]) {
178 layout |= GrDrawTarget::StageTexCoordVertexLayoutBit(i, i);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000179 }
180 }
181 }
182 return layout;
183}
184
185
186////////////////////////////////////////////////////////////////////////////////
187
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000188GrTexture* GrContext::TextureCacheEntry::texture() const {
189 if (NULL == fEntry) {
190 return NULL;
191 } else {
192 return (GrTexture*) fEntry->resource();
193 }
194}
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000195
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000196namespace {
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000197
198// we should never have more than one stencil buffer with same combo of
199// (width,height,samplecount)
200void gen_stencil_key_values(int width, int height,
201 int sampleCnt, uint32_t v[4]) {
202 v[0] = width;
203 v[1] = height;
204 v[2] = sampleCnt;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000205 v[3] = GrResourceKey::kStencilBuffer_TypeBit;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000206}
207
208void gen_stencil_key_values(const GrStencilBuffer* sb,
209 uint32_t v[4]) {
210 gen_stencil_key_values(sb->width(), sb->height(),
211 sb->numSamples(), v);
212}
bsalomon@google.com82c7bd82011-11-09 15:32:29 +0000213
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000214void scale_rect(SkRect* rect, float xScale, float yScale) {
robertphillips@google.com5af56062012-04-27 15:39:52 +0000215 rect->fLeft = SkScalarMul(rect->fLeft, SkFloatToScalar(xScale));
216 rect->fTop = SkScalarMul(rect->fTop, SkFloatToScalar(yScale));
217 rect->fRight = SkScalarMul(rect->fRight, SkFloatToScalar(xScale));
218 rect->fBottom = SkScalarMul(rect->fBottom, SkFloatToScalar(yScale));
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000219}
220
bsalomon@google.comb505a122012-05-31 18:40:36 +0000221float adjust_sigma(float sigma, int *scaleFactor, int *radius) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000222 *scaleFactor = 1;
223 while (sigma > MAX_BLUR_SIGMA) {
224 *scaleFactor *= 2;
225 sigma *= 0.5f;
226 }
bsalomon@google.comb505a122012-05-31 18:40:36 +0000227 *radius = static_cast<int>(ceilf(sigma * 3.0f));
228 GrAssert(*radius <= GrConvolutionEffect::kMaxKernelRadius);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000229 return sigma;
230}
231
232void apply_morphology(GrGpu* gpu,
233 GrTexture* texture,
234 const SkRect& rect,
235 int radius,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000236 GrContext::MorphologyType morphType,
237 Gr1DKernelEffect::Direction direction) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000238
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000239 GrRenderTarget* target = gpu->drawState()->getRenderTarget();
240 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000241 GrDrawState* drawState = gpu->drawState();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000242 drawState->setRenderTarget(target);
243 GrMatrix sampleM;
244 sampleM.setIDiv(texture->width(), texture->height());
bsalomon@google.comb505a122012-05-31 18:40:36 +0000245 drawState->sampler(0)->reset(sampleM);
246 SkAutoTUnref<GrCustomStage> morph(
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000247 SkNEW_ARGS(GrMorphologyEffect, (direction, radius, morphType)));
bsalomon@google.comb505a122012-05-31 18:40:36 +0000248 drawState->sampler(0)->setCustomStage(morph);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000249 drawState->setTexture(0, texture);
250 gpu->drawSimpleRect(rect, NULL, 1 << 0);
251}
252
bsalomon@google.comb505a122012-05-31 18:40:36 +0000253void convolve_gaussian(GrGpu* gpu,
254 GrTexture* texture,
255 const SkRect& rect,
256 float sigma,
257 int radius,
258 Gr1DKernelEffect::Direction direction) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000259 GrRenderTarget* target = gpu->drawState()->getRenderTarget();
260 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000261 GrDrawState* drawState = gpu->drawState();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000262 drawState->setRenderTarget(target);
263 GrMatrix sampleM;
264 sampleM.setIDiv(texture->width(), texture->height());
bsalomon@google.comb505a122012-05-31 18:40:36 +0000265 drawState->sampler(0)->reset(sampleM);
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000266 SkAutoTUnref<GrConvolutionEffect> conv(SkNEW_ARGS(GrConvolutionEffect,
267 (direction, radius)));
bsalomon@google.comb505a122012-05-31 18:40:36 +0000268 conv->setGaussianKernel(sigma);
269 drawState->sampler(0)->setCustomStage(conv);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000270 drawState->setTexture(0, texture);
271 gpu->drawSimpleRect(rect, NULL, 1 << 0);
272}
273
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000274}
275
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000276GrContext::TextureCacheEntry GrContext::findAndLockTexture(
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000277 const GrTextureDesc& desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000278 const GrSamplerState* sampler) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000279 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, sampler, desc, false);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000280 return TextureCacheEntry(fTextureCache->findAndLock(resourceKey,
281 GrResourceCache::kNested_LockType));
282}
283
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000284bool GrContext::isTextureInCache(const GrTextureDesc& desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000285 const GrSamplerState* sampler) const {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000286 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, sampler, desc, false);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000287 return fTextureCache->hasKey(resourceKey);
288}
289
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000290GrResourceEntry* GrContext::addAndLockStencilBuffer(GrStencilBuffer* sb) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000291 ASSERT_OWNED_RESOURCE(sb);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000292 uint32_t v[4];
293 gen_stencil_key_values(sb, v);
294 GrResourceKey resourceKey(v);
295 return fTextureCache->createAndLock(resourceKey, sb);
296}
297
298GrStencilBuffer* GrContext::findStencilBuffer(int width, int height,
299 int sampleCnt) {
300 uint32_t v[4];
301 gen_stencil_key_values(width, height, sampleCnt, v);
302 GrResourceKey resourceKey(v);
303 GrResourceEntry* entry = fTextureCache->findAndLock(resourceKey,
304 GrResourceCache::kSingle_LockType);
305 if (NULL != entry) {
306 GrStencilBuffer* sb = (GrStencilBuffer*) entry->resource();
307 return sb;
308 } else {
309 return NULL;
310 }
311}
312
313void GrContext::unlockStencilBuffer(GrResourceEntry* sbEntry) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000314 ASSERT_OWNED_RESOURCE(sbEntry->resource());
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000315 fTextureCache->unlock(sbEntry);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000316}
317
318static void stretchImage(void* dst,
319 int dstW,
320 int dstH,
321 void* src,
322 int srcW,
323 int srcH,
324 int bpp) {
325 GrFixed dx = (srcW << 16) / dstW;
326 GrFixed dy = (srcH << 16) / dstH;
327
328 GrFixed y = dy >> 1;
329
330 int dstXLimit = dstW*bpp;
331 for (int j = 0; j < dstH; ++j) {
332 GrFixed x = dx >> 1;
333 void* srcRow = (uint8_t*)src + (y>>16)*srcW*bpp;
334 void* dstRow = (uint8_t*)dst + j*dstW*bpp;
335 for (int i = 0; i < dstXLimit; i += bpp) {
336 memcpy((uint8_t*) dstRow + i,
337 (uint8_t*) srcRow + (x>>16)*bpp,
338 bpp);
339 x += dx;
340 }
341 y += dy;
342 }
343}
344
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000345GrContext::TextureCacheEntry GrContext::createAndLockTexture(
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000346 const GrSamplerState* sampler,
347 const GrTextureDesc& desc,
348 void* srcData,
349 size_t rowBytes) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000350 SK_TRACE_EVENT0("GrContext::createAndLockTexture");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000351
352#if GR_DUMP_TEXTURE_UPLOAD
353 GrPrintf("GrContext::createAndLockTexture [%d %d]\n", desc.fWidth, desc.fHeight);
354#endif
355
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000356 TextureCacheEntry entry;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000357
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000358 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, sampler,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000359 desc, false);
360
361 if (GrTexture::NeedsResizing(resourceKey)) {
362 // The desired texture is NPOT and tiled but that isn't supported by
363 // the current hardware. Resize the texture to be a POT
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000364 GrAssert(NULL != sampler);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000365 TextureCacheEntry clampEntry = this->findAndLockTexture(desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000366 NULL);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000367
368 if (NULL == clampEntry.texture()) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000369 clampEntry = this->createAndLockTexture(NULL, desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000370 srcData, rowBytes);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000371 GrAssert(NULL != clampEntry.texture());
372 if (NULL == clampEntry.texture()) {
373 return entry;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000374 }
375 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000376 GrTextureDesc rtDesc = desc;
377 rtDesc.fFlags = rtDesc.fFlags |
378 kRenderTarget_GrTextureFlagBit |
379 kNoStencil_GrTextureFlagBit;
bsalomon@google.com99621082011-11-15 16:47:16 +0000380 rtDesc.fWidth = GrNextPow2(GrMax(desc.fWidth, 64));
381 rtDesc.fHeight = GrNextPow2(GrMax(desc.fHeight, 64));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000382
383 GrTexture* texture = fGpu->createTexture(rtDesc, NULL, 0);
384
385 if (NULL != texture) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000386 GrDrawTarget::AutoStateRestore asr(fGpu,
387 GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000388 GrDrawState* drawState = fGpu->drawState();
389 drawState->setRenderTarget(texture->asRenderTarget());
390 drawState->setTexture(0, clampEntry.texture());
bsalomon@google.com82c7bd82011-11-09 15:32:29 +0000391
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000392 GrSamplerState::Filter filter;
393 // if filtering is not desired then we want to ensure all
394 // texels in the resampled image are copies of texels from
395 // the original.
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000396 if (GrTexture::NeedsFiltering(resourceKey)) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000397 filter = GrSamplerState::kBilinear_Filter;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000398 } else {
399 filter = GrSamplerState::kNearest_Filter;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000400 }
bsalomon@google.com1e266f82011-12-12 16:11:33 +0000401 drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode,
402 filter);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000403
404 static const GrVertexLayout layout =
405 GrDrawTarget::StageTexCoordVertexLayoutBit(0,0);
406 GrDrawTarget::AutoReleaseGeometry arg(fGpu, layout, 4, 0);
407
408 if (arg.succeeded()) {
409 GrPoint* verts = (GrPoint*) arg.vertices();
410 verts[0].setIRectFan(0, 0,
411 texture->width(),
412 texture->height(),
413 2*sizeof(GrPoint));
414 verts[1].setIRectFan(0, 0, 1, 1, 2*sizeof(GrPoint));
bsalomon@google.com47059542012-06-06 20:51:20 +0000415 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000416 0, 4);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000417 entry.set(fTextureCache->createAndLock(resourceKey, texture));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000418 }
bsalomon@google.com1da07462011-03-10 14:51:57 +0000419 texture->releaseRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000420 } else {
421 // TODO: Our CPU stretch doesn't filter. But we create separate
422 // stretched textures when the sampler state is either filtered or
423 // not. Either implement filtered stretch blit on CPU or just create
424 // one when FBO case fails.
425
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000426 rtDesc.fFlags = kNone_GrTextureFlags;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000427 // no longer need to clamp at min RT size.
428 rtDesc.fWidth = GrNextPow2(desc.fWidth);
429 rtDesc.fHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000430 int bpp = GrBytesPerPixel(desc.fConfig);
bsalomon@google.com3582bf92011-06-30 21:32:31 +0000431 SkAutoSMalloc<128*128*4> stretchedPixels(bpp *
bsalomon@google.com27847de2011-02-22 20:59:41 +0000432 rtDesc.fWidth *
433 rtDesc.fHeight);
434 stretchImage(stretchedPixels.get(), rtDesc.fWidth, rtDesc.fHeight,
435 srcData, desc.fWidth, desc.fHeight, bpp);
436
437 size_t stretchedRowBytes = rtDesc.fWidth * bpp;
438
439 GrTexture* texture = fGpu->createTexture(rtDesc,
440 stretchedPixels.get(),
441 stretchedRowBytes);
442 GrAssert(NULL != texture);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000443 entry.set(fTextureCache->createAndLock(resourceKey, texture));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000444 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000445 fTextureCache->unlock(clampEntry.cacheEntry());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000446
447 } else {
448 GrTexture* texture = fGpu->createTexture(desc, srcData, rowBytes);
449 if (NULL != texture) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000450 entry.set(fTextureCache->createAndLock(resourceKey, texture));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000451 }
452 }
453 return entry;
454}
455
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000456GrContext::TextureCacheEntry GrContext::lockScratchTexture(
457 const GrTextureDesc& inDesc,
458 ScratchTexMatch match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000459 GrTextureDesc desc = inDesc;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000460 desc.fClientCacheID = kScratch_CacheID;
461
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000462 if (kExact_ScratchTexMatch != match) {
463 // bin by pow2 with a reasonable min
464 static const int MIN_SIZE = 256;
465 desc.fWidth = GrMax(MIN_SIZE, GrNextPow2(desc.fWidth));
466 desc.fHeight = GrMax(MIN_SIZE, GrNextPow2(desc.fHeight));
467 }
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000468
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000469 GrResourceEntry* entry;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000470 int origWidth = desc.fWidth;
471 int origHeight = desc.fHeight;
472 bool doubledW = false;
473 bool doubledH = false;
474
475 do {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000476 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL, desc, true);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000477 entry = fTextureCache->findAndLock(key,
478 GrResourceCache::kNested_LockType);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000479 // if we miss, relax the fit of the flags...
480 // then try doubling width... then height.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000481 if (NULL != entry || kExact_ScratchTexMatch == match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000482 break;
483 }
484 if (!(desc.fFlags & kRenderTarget_GrTextureFlagBit)) {
485 desc.fFlags = desc.fFlags | kRenderTarget_GrTextureFlagBit;
486 } else if (desc.fFlags & kNoStencil_GrTextureFlagBit) {
487 desc.fFlags = desc.fFlags & ~kNoStencil_GrTextureFlagBit;
488 } else if (!doubledW) {
489 desc.fFlags = inDesc.fFlags;
490 desc.fWidth *= 2;
491 doubledW = true;
492 } else if (!doubledH) {
493 desc.fFlags = inDesc.fFlags;
494 desc.fWidth = origWidth;
495 desc.fHeight *= 2;
496 doubledH = true;
497 } else {
498 break;
499 }
500
501 } while (true);
502
503 if (NULL == entry) {
504 desc.fFlags = inDesc.fFlags;
505 desc.fWidth = origWidth;
506 desc.fHeight = origHeight;
507 GrTexture* texture = fGpu->createTexture(desc, NULL, 0);
508 if (NULL != texture) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000509 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000510 texture->desc(),
511 true);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000512 entry = fTextureCache->createAndLock(key, texture);
513 }
514 }
515
516 // If the caller gives us the same desc/sampler twice we don't want
517 // to return the same texture the second time (unless it was previously
518 // released). So we detach the entry from the cache and reattach at release.
519 if (NULL != entry) {
520 fTextureCache->detach(entry);
521 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000522 return TextureCacheEntry(entry);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000523}
524
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000525void GrContext::addExistingTextureToCache(GrTexture* texture) {
526
527 if (NULL == texture) {
528 return;
529 }
530
531 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL,
532 texture->desc(),
533 true);
534 fTextureCache->attach(key, texture);
535}
536
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000537void GrContext::unlockTexture(TextureCacheEntry entry) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000538 ASSERT_OWNED_RESOURCE(entry.texture());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000539 // If this is a scratch texture we detached it from the cache
540 // while it was locked (to avoid two callers simultaneously getting
541 // the same texture).
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000542 if (GrTexture::IsScratchTexture(entry.cacheEntry()->key())) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000543 fTextureCache->reattachAndUnlock(entry.cacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000544 } else {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000545 fTextureCache->unlock(entry.cacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000546 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000547}
548
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000549void GrContext::freeEntry(TextureCacheEntry entry) {
550 ASSERT_OWNED_RESOURCE(entry.texture());
551
552 fTextureCache->freeEntry(entry.cacheEntry());
553}
554
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000555GrTexture* GrContext::createUncachedTexture(const GrTextureDesc& descIn,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000556 void* srcData,
557 size_t rowBytes) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000558 GrTextureDesc descCopy = descIn;
559 descCopy.fClientCacheID = kUncached_CacheID;
560 return fGpu->createTexture(descCopy, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000561}
562
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000563void GrContext::getTextureCacheLimits(int* maxTextures,
564 size_t* maxTextureBytes) const {
565 fTextureCache->getLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000566}
567
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000568void GrContext::setTextureCacheLimits(int maxTextures, size_t maxTextureBytes) {
569 fTextureCache->setLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000570}
571
bsalomon@google.com91958362011-06-13 17:58:13 +0000572int GrContext::getMaxTextureSize() const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000573 return fGpu->getCaps().fMaxTextureSize;
bsalomon@google.com91958362011-06-13 17:58:13 +0000574}
575
576int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000577 return fGpu->getCaps().fMaxRenderTargetSize;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000578}
579
580///////////////////////////////////////////////////////////////////////////////
581
bsalomon@google.come269f212011-11-07 13:29:52 +0000582GrTexture* GrContext::createPlatformTexture(const GrPlatformTextureDesc& desc) {
583 return fGpu->createPlatformTexture(desc);
584}
585
586GrRenderTarget* GrContext::createPlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
587 return fGpu->createPlatformRenderTarget(desc);
588}
589
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000590///////////////////////////////////////////////////////////////////////////////
591
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000592bool GrContext::supportsIndex8PixelConfig(const GrSamplerState* sampler,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000593 int width, int height) const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000594 const GrDrawTarget::Caps& caps = fGpu->getCaps();
595 if (!caps.f8BitPaletteSupport) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000596 return false;
597 }
598
bsalomon@google.com27847de2011-02-22 20:59:41 +0000599 bool isPow2 = GrIsPow2(width) && GrIsPow2(height);
600
601 if (!isPow2) {
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000602 bool tiled = NULL != sampler &&
603 (sampler->getWrapX() != GrSamplerState::kClamp_WrapMode ||
604 sampler->getWrapY() != GrSamplerState::kClamp_WrapMode);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000605 if (tiled && !caps.fNPOTTextureTileSupport) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000606 return false;
607 }
608 }
609 return true;
610}
611
612////////////////////////////////////////////////////////////////////////////////
613
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000614const GrClip& GrContext::getClip() const { return fGpu->getClip(); }
615
bsalomon@google.com27847de2011-02-22 20:59:41 +0000616void GrContext::setClip(const GrClip& clip) {
617 fGpu->setClip(clip);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000618 fDrawState->enableState(GrDrawState::kClip_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000619}
620
bsalomon@google.com27847de2011-02-22 20:59:41 +0000621////////////////////////////////////////////////////////////////////////////////
622
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000623void GrContext::clear(const GrIRect* rect,
624 const GrColor color,
625 GrRenderTarget* target) {
bsalomon@google.com398109c2011-04-14 18:40:27 +0000626 this->flush();
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000627 fGpu->clear(rect, color, target);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000628}
629
630void GrContext::drawPaint(const GrPaint& paint) {
631 // set rect to be big enough to fill the space, but not super-huge, so we
632 // don't overflow fixed-point implementations
bsalomon@google.comd302f142011-03-03 13:54:13 +0000633 GrRect r;
634 r.setLTRB(0, 0,
635 GrIntToScalar(getRenderTarget()->width()),
636 GrIntToScalar(getRenderTarget()->height()));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000637 GrMatrix inverse;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000638 SkTLazy<GrPaint> tmpPaint;
639 const GrPaint* p = &paint;
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000640 AutoMatrix am;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000641
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000642 // We attempt to map r by the inverse matrix and draw that. mapRect will
643 // map the four corners and bound them with a new rect. This will not
644 // produce a correct result for some perspective matrices.
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000645 if (!this->getMatrix().hasPerspective()) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000646 if (!fDrawState->getViewInverse(&inverse)) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000647 GrPrintf("Could not invert matrix");
648 return;
649 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000650 inverse.mapRect(&r);
651 } else {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000652 if (paint.getActiveMaskStageMask() || paint.getActiveStageMask()) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000653 if (!fDrawState->getViewInverse(&inverse)) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000654 GrPrintf("Could not invert matrix");
655 return;
656 }
657 tmpPaint.set(paint);
658 tmpPaint.get()->preConcatActiveSamplerMatrices(inverse);
659 p = tmpPaint.get();
660 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000661 am.set(this, GrMatrix::I());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000662 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000663 // by definition this fills the entire clip, no need for AA
664 if (paint.fAntiAlias) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000665 if (!tmpPaint.isValid()) {
666 tmpPaint.set(paint);
667 p = tmpPaint.get();
668 }
669 GrAssert(p == tmpPaint.get());
670 tmpPaint.get()->fAntiAlias = false;
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000671 }
672 this->drawRect(*p, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000673}
674
bsalomon@google.com205d4602011-04-25 12:43:45 +0000675////////////////////////////////////////////////////////////////////////////////
676
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000677namespace {
678inline bool disable_coverage_aa_for_blend(GrDrawTarget* target) {
679 return DISABLE_COVERAGE_AA_FOR_BLEND && !target->canApplyCoverage();
680}
681}
682
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000683////////////////////////////////////////////////////////////////////////////////
684
bsalomon@google.com27847de2011-02-22 20:59:41 +0000685/* create a triangle strip that strokes the specified triangle. There are 8
686 unique vertices, but we repreat the last 2 to close up. Alternatively we
687 could use an indices array, and then only send 8 verts, but not sure that
688 would be faster.
689 */
bsalomon@google.com205d4602011-04-25 12:43:45 +0000690static void setStrokeRectStrip(GrPoint verts[10], GrRect rect,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000691 GrScalar width) {
692 const GrScalar rad = GrScalarHalf(width);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000693 rect.sort();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000694
695 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
696 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
697 verts[2].set(rect.fRight - rad, rect.fTop + rad);
698 verts[3].set(rect.fRight + rad, rect.fTop - rad);
699 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
700 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
701 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
702 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
703 verts[8] = verts[0];
704 verts[9] = verts[1];
705}
706
reed@google.com20efde72011-05-09 17:00:02 +0000707/**
708 * Returns true if the rects edges are integer-aligned.
709 */
710static bool isIRect(const GrRect& r) {
711 return GrScalarIsInt(r.fLeft) && GrScalarIsInt(r.fTop) &&
712 GrScalarIsInt(r.fRight) && GrScalarIsInt(r.fBottom);
713}
714
bsalomon@google.com205d4602011-04-25 12:43:45 +0000715static bool apply_aa_to_rect(GrDrawTarget* target,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000716 const GrRect& rect,
717 GrScalar width,
718 const GrMatrix* matrix,
719 GrMatrix* combinedMatrix,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000720 GrRect* devRect,
721 bool* useVertexCoverage) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000722 // we use a simple coverage ramp to do aa on axis-aligned rects
723 // we check if the rect will be axis-aligned, and the rect won't land on
724 // integer coords.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000725
bsalomon@google.coma3108262011-10-10 14:08:47 +0000726 // we are keeping around the "tweak the alpha" trick because
727 // it is our only hope for the fixed-pipe implementation.
728 // In a shader implementation we can give a separate coverage input
bsalomon@google.com289533a2011-10-27 12:34:25 +0000729 // TODO: remove this ugliness when we drop the fixed-pipe impl
bsalomon@google.coma3108262011-10-10 14:08:47 +0000730 *useVertexCoverage = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000731 if (!target->canTweakAlphaForCoverage()) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000732 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +0000733#if GR_DEBUG
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000734 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +0000735#endif
bsalomon@google.coma3108262011-10-10 14:08:47 +0000736 return false;
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000737 } else {
738 *useVertexCoverage = true;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000739 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000740 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000741 const GrDrawState& drawState = target->getDrawState();
742 if (drawState.getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000743 return false;
744 }
745
bsalomon@google.com471d4712011-08-23 15:45:25 +0000746 if (0 == width && target->willUseHWAALines()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000747 return false;
748 }
749
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000750 if (!drawState.getViewMatrix().preservesAxisAlignment()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000751 return false;
752 }
753
754 if (NULL != matrix &&
755 !matrix->preservesAxisAlignment()) {
756 return false;
757 }
758
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000759 *combinedMatrix = drawState.getViewMatrix();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000760 if (NULL != matrix) {
761 combinedMatrix->preConcat(*matrix);
762 GrAssert(combinedMatrix->preservesAxisAlignment());
763 }
764
765 combinedMatrix->mapRect(devRect, rect);
766 devRect->sort();
767
768 if (width < 0) {
reed@google.com20efde72011-05-09 17:00:02 +0000769 return !isIRect(*devRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000770 } else {
771 return true;
772 }
773}
774
bsalomon@google.com27847de2011-02-22 20:59:41 +0000775void GrContext::drawRect(const GrPaint& paint,
776 const GrRect& rect,
777 GrScalar width,
778 const GrMatrix* matrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000779 SK_TRACE_EVENT0("GrContext::drawRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000780
781 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000782 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000783 int stageMask = paint.getActiveStageMask();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000784
bsalomon@google.com205d4602011-04-25 12:43:45 +0000785 GrRect devRect = rect;
786 GrMatrix combinedMatrix;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000787 bool useVertexCoverage;
bsalomon@google.com289533a2011-10-27 12:34:25 +0000788 bool needAA = paint.fAntiAlias &&
789 !this->getRenderTarget()->isMultisampled();
790 bool doAA = needAA && apply_aa_to_rect(target, rect, width, matrix,
791 &combinedMatrix, &devRect,
792 &useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000793
794 if (doAA) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000795 GrDrawTarget::AutoDeviceCoordDraw adcd(target, stageMask);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000796 if (width >= 0) {
797 GrVec strokeSize;;
798 if (width > 0) {
799 strokeSize.set(width, width);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000800 combinedMatrix.mapVectors(&strokeSize, 1);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000801 strokeSize.setAbs(strokeSize);
802 } else {
803 strokeSize.set(GR_Scalar1, GR_Scalar1);
804 }
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000805 fAARectRenderer->strokeAARect(this->getGpu(), target, devRect,
806 strokeSize, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000807 } else {
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000808 fAARectRenderer->fillAARect(this->getGpu(), target,
809 devRect, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000810 }
811 return;
812 }
813
bsalomon@google.com27847de2011-02-22 20:59:41 +0000814 if (width >= 0) {
815 // TODO: consider making static vertex buffers for these cases.
816 // Hairline could be done by just adding closing vertex to
817 // unitSquareVertexBuffer()
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000818 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL);
819
bsalomon@google.com27847de2011-02-22 20:59:41 +0000820 static const int worstCaseVertCount = 10;
821 GrDrawTarget::AutoReleaseGeometry geo(target, layout, worstCaseVertCount, 0);
822
823 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000824 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000825 return;
826 }
827
828 GrPrimitiveType primType;
829 int vertCount;
830 GrPoint* vertex = geo.positions();
831
832 if (width > 0) {
833 vertCount = 10;
bsalomon@google.com47059542012-06-06 20:51:20 +0000834 primType = kTriangleStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000835 setStrokeRectStrip(vertex, rect, width);
836 } else {
837 // hairline
838 vertCount = 5;
bsalomon@google.com47059542012-06-06 20:51:20 +0000839 primType = kLineStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000840 vertex[0].set(rect.fLeft, rect.fTop);
841 vertex[1].set(rect.fRight, rect.fTop);
842 vertex[2].set(rect.fRight, rect.fBottom);
843 vertex[3].set(rect.fLeft, rect.fBottom);
844 vertex[4].set(rect.fLeft, rect.fTop);
845 }
846
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000847 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000848 if (NULL != matrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000849 GrDrawState* drawState = target->drawState();
850 avmr.set(drawState);
851 drawState->preConcatViewMatrix(*matrix);
852 drawState->preConcatSamplerMatrices(stageMask, *matrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000853 }
854
855 target->drawNonIndexed(primType, 0, vertCount);
856 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000857#if GR_STATIC_RECT_VB
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000858 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL);
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000859 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
860 if (NULL == sqVB) {
861 GrPrintf("Failed to create static rect vb.\n");
862 return;
863 }
864 target->setVertexSourceToBuffer(layout, sqVB);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000865 GrDrawState* drawState = target->drawState();
866 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000867 GrMatrix m;
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000868 m.setAll(rect.width(), 0, rect.fLeft,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000869 0, rect.height(), rect.fTop,
870 0, 0, GrMatrix::I()[8]);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000871
872 if (NULL != matrix) {
873 m.postConcat(*matrix);
874 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000875 drawState->preConcatViewMatrix(m);
876 drawState->preConcatSamplerMatrices(stageMask, m);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000877
bsalomon@google.com47059542012-06-06 20:51:20 +0000878 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000879#else
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000880 target->drawSimpleRect(rect, matrix, stageMask);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000881#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +0000882 }
883}
884
885void GrContext::drawRectToRect(const GrPaint& paint,
886 const GrRect& dstRect,
887 const GrRect& srcRect,
888 const GrMatrix* dstMatrix,
889 const GrMatrix* srcMatrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000890 SK_TRACE_EVENT0("GrContext::drawRectToRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000891
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000892 // srcRect refers to paint's first texture
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000893 if (!paint.isTextureStageEnabled(0)) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000894 drawRect(paint, dstRect, -1, dstMatrix);
895 return;
896 }
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000897
bsalomon@google.com27847de2011-02-22 20:59:41 +0000898 GR_STATIC_ASSERT(!BATCH_RECT_TO_RECT || !GR_STATIC_RECT_VB);
899
900#if GR_STATIC_RECT_VB
901 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000902 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000903 GrDrawState* drawState = target->drawState();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000904 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000905 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000906
907 GrMatrix m;
908
909 m.setAll(dstRect.width(), 0, dstRect.fLeft,
910 0, dstRect.height(), dstRect.fTop,
911 0, 0, GrMatrix::I()[8]);
912 if (NULL != dstMatrix) {
913 m.postConcat(*dstMatrix);
914 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000915 drawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000916
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000917 // srcRect refers to first stage
918 int otherStageMask = paint.getActiveStageMask() &
919 (~(1 << GrPaint::kFirstTextureStage));
920 if (otherStageMask) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000921 drawState->preConcatSamplerMatrices(otherStageMask, m);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000922 }
923
bsalomon@google.com27847de2011-02-22 20:59:41 +0000924 m.setAll(srcRect.width(), 0, srcRect.fLeft,
925 0, srcRect.height(), srcRect.fTop,
926 0, 0, GrMatrix::I()[8]);
927 if (NULL != srcMatrix) {
928 m.postConcat(*srcMatrix);
929 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000930 drawState->sampler(GrPaint::kFirstTextureStage)->preConcatMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000931
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000932 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
933 if (NULL == sqVB) {
934 GrPrintf("Failed to create static rect vb.\n");
935 return;
936 }
937 target->setVertexSourceToBuffer(layout, sqVB);
bsalomon@google.com47059542012-06-06 20:51:20 +0000938 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000939#else
940
941 GrDrawTarget* target;
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000942#if BATCH_RECT_TO_RECT
bsalomon@google.com27847de2011-02-22 20:59:41 +0000943 target = this->prepareToDraw(paint, kBuffered_DrawCategory);
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000944#else
bsalomon@google.com27847de2011-02-22 20:59:41 +0000945 target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
946#endif
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000947 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000948
tomhudson@google.com93813632011-10-27 20:21:16 +0000949 const GrRect* srcRects[GrDrawState::kNumStages] = {NULL};
950 const GrMatrix* srcMatrices[GrDrawState::kNumStages] = {NULL};
bsalomon@google.com27847de2011-02-22 20:59:41 +0000951 srcRects[0] = &srcRect;
952 srcMatrices[0] = srcMatrix;
953
954 target->drawRect(dstRect, dstMatrix, 1, srcRects, srcMatrices);
955#endif
956}
957
958void GrContext::drawVertices(const GrPaint& paint,
959 GrPrimitiveType primitiveType,
960 int vertexCount,
961 const GrPoint positions[],
962 const GrPoint texCoords[],
963 const GrColor colors[],
964 const uint16_t indices[],
965 int indexCount) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000966 SK_TRACE_EVENT0("GrContext::drawVertices");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000967
968 GrDrawTarget::AutoReleaseGeometry geo;
969
970 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000971 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000972
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000973 bool hasTexCoords[GrPaint::kTotalStages] = {
974 NULL != texCoords, // texCoordSrc provides explicit stage 0 coords
975 0 // remaining stages use positions
976 };
977
978 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, hasTexCoords);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000979
980 if (NULL != colors) {
981 layout |= GrDrawTarget::kColor_VertexLayoutBit;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000982 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000983 int vertexSize = GrDrawTarget::VertexSize(layout);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000984
985 if (sizeof(GrPoint) != vertexSize) {
986 if (!geo.set(target, layout, vertexCount, 0)) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000987 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000988 return;
989 }
tomhudson@google.com93813632011-10-27 20:21:16 +0000990 int texOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000991 int colorOffset;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000992 GrDrawTarget::VertexSizeAndOffsetsByIdx(layout,
993 texOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000994 &colorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000995 NULL,
996 NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000997 void* curVertex = geo.vertices();
998
999 for (int i = 0; i < vertexCount; ++i) {
1000 *((GrPoint*)curVertex) = positions[i];
1001
1002 if (texOffsets[0] > 0) {
1003 *(GrPoint*)((intptr_t)curVertex + texOffsets[0]) = texCoords[i];
1004 }
1005 if (colorOffset > 0) {
1006 *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i];
1007 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001008 curVertex = (void*)((intptr_t)curVertex + vertexSize);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001009 }
1010 } else {
1011 target->setVertexSourceToArray(layout, positions, vertexCount);
1012 }
1013
bsalomon@google.com91958362011-06-13 17:58:13 +00001014 // we don't currently apply offscreen AA to this path. Need improved
1015 // management of GrDrawTarget's geometry to avoid copying points per-tile.
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001016
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001017 if (NULL != indices) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001018 target->setIndexSourceToArray(indices, indexCount);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001019 target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001020 } else {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001021 target->drawNonIndexed(primitiveType, 0, vertexCount);
1022 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001023}
1024
bsalomon@google.com06afe7b2011-04-26 15:31:40 +00001025///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com150d2842012-01-12 20:19:56 +00001026namespace {
1027
bsalomon@google.com93c96602012-04-27 13:05:21 +00001028struct CircleVertex {
1029 GrPoint fPos;
1030 GrPoint fCenter;
1031 GrScalar fOuterRadius;
1032 GrScalar fInnerRadius;
1033};
1034
1035/* Returns true if will map a circle to another circle. This can be true
1036 * if the matrix only includes square-scale, rotation, translation.
1037 */
1038inline bool isSimilarityTransformation(const SkMatrix& matrix,
1039 SkScalar tol = SK_ScalarNearlyZero) {
1040 if (matrix.isIdentity() || matrix.getType() == SkMatrix::kTranslate_Mask) {
1041 return true;
1042 }
1043 if (matrix.hasPerspective()) {
1044 return false;
1045 }
1046
1047 SkScalar mx = matrix.get(SkMatrix::kMScaleX);
1048 SkScalar sx = matrix.get(SkMatrix::kMSkewX);
1049 SkScalar my = matrix.get(SkMatrix::kMScaleY);
1050 SkScalar sy = matrix.get(SkMatrix::kMSkewY);
1051
1052 if (mx == 0 && sx == 0 && my == 0 && sy == 0) {
1053 return false;
1054 }
1055
1056 // it has scales or skews, but it could also be rotation, check it out.
1057 SkVector vec[2];
1058 vec[0].set(mx, sx);
1059 vec[1].set(sy, my);
1060
1061 return SkScalarNearlyZero(vec[0].dot(vec[1]), SkScalarSquare(tol)) &&
1062 SkScalarNearlyEqual(vec[0].lengthSqd(), vec[1].lengthSqd(),
1063 SkScalarSquare(tol));
1064}
1065
1066}
1067
1068// TODO: strokeWidth can't be larger than zero right now.
1069// It will be fixed when drawPath() can handle strokes.
1070void GrContext::drawOval(const GrPaint& paint,
1071 const GrRect& rect,
1072 SkScalar strokeWidth) {
1073 DrawCategory category = (DEFER_PATHS) ? kBuffered_DrawCategory :
1074 kUnbuffered_DrawCategory;
1075 GrDrawTarget* target = this->prepareToDraw(paint, category);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +00001076 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001077 GrDrawState* drawState = target->drawState();
1078 GrMatrix vm = drawState->getViewMatrix();
1079
1080 if (!isSimilarityTransformation(vm) ||
1081 !paint.fAntiAlias ||
1082 rect.height() != rect.width()) {
1083 SkPath path;
1084 path.addOval(rect);
1085 GrPathFill fill = (strokeWidth == 0) ?
bsalomon@google.com47059542012-06-06 20:51:20 +00001086 kHairLine_GrPathFill : kWinding_GrPathFill;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001087 this->internalDrawPath(paint, path, fill, NULL);
1088 return;
1089 }
1090
1091 const GrRenderTarget* rt = drawState->getRenderTarget();
1092 if (NULL == rt) {
1093 return;
1094 }
1095
1096 GrDrawTarget::AutoDeviceCoordDraw adcd(target, paint.getActiveStageMask());
1097
1098 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL);
1099 layout |= GrDrawTarget::kEdge_VertexLayoutBit;
1100 GrAssert(sizeof(CircleVertex) == GrDrawTarget::VertexSize(layout));
1101
1102 GrPoint center = GrPoint::Make(rect.centerX(), rect.centerY());
1103 GrScalar radius = SkScalarHalf(rect.width());
1104
1105 vm.mapPoints(&center, 1);
1106 radius = vm.mapRadius(radius);
1107
1108 GrScalar outerRadius = radius;
1109 GrScalar innerRadius = 0;
1110 SkScalar halfWidth = 0;
1111 if (strokeWidth == 0) {
1112 halfWidth = SkScalarHalf(SK_Scalar1);
1113
1114 outerRadius += halfWidth;
1115 innerRadius = SkMaxScalar(0, radius - halfWidth);
1116 }
1117
1118 GrDrawTarget::AutoReleaseGeometry geo(target, layout, 4, 0);
1119 if (!geo.succeeded()) {
1120 GrPrintf("Failed to get space for vertices!\n");
1121 return;
1122 }
1123
1124 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
1125
robertphillips@google.coma0a66c12012-06-22 13:14:29 +00001126 // The fragment shader will extend the radius out half a pixel
1127 // to antialias. Expand the drawn rect here so all the pixels
1128 // will be captured.
1129 SkScalar L = center.fX - outerRadius - SkFloatToScalar(0.5f);
1130 SkScalar R = center.fX + outerRadius + SkFloatToScalar(0.5f);
1131 SkScalar T = center.fY - outerRadius - SkFloatToScalar(0.5f);
1132 SkScalar B = center.fY + outerRadius + SkFloatToScalar(0.5f);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001133
1134 verts[0].fPos = SkPoint::Make(L, T);
1135 verts[1].fPos = SkPoint::Make(R, T);
1136 verts[2].fPos = SkPoint::Make(L, B);
1137 verts[3].fPos = SkPoint::Make(R, B);
1138
1139 for (int i = 0; i < 4; ++i) {
1140 // this goes to fragment shader, it should be in y-points-up space.
1141 verts[i].fCenter = SkPoint::Make(center.fX, rt->height() - center.fY);
1142
1143 verts[i].fOuterRadius = outerRadius;
1144 verts[i].fInnerRadius = innerRadius;
1145 }
1146
1147 drawState->setVertexEdgeType(GrDrawState::kCircle_EdgeType);
bsalomon@google.com47059542012-06-06 20:51:20 +00001148 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4);
bsalomon@google.com150d2842012-01-12 20:19:56 +00001149}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001150
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001151void GrContext::drawPath(const GrPaint& paint, const SkPath& path,
reed@google.com07f3ee12011-05-16 17:21:57 +00001152 GrPathFill fill, const GrPoint* translate) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001153
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001154 if (path.isEmpty()) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001155 if (GrIsFillInverted(fill)) {
1156 this->drawPaint(paint);
1157 }
1158 return;
1159 }
1160
bsalomon@google.com93c96602012-04-27 13:05:21 +00001161 SkRect ovalRect;
1162 if (!GrIsFillInverted(fill) && path.isOval(&ovalRect)) {
1163 if (translate) {
1164 ovalRect.offset(*translate);
1165 }
bsalomon@google.com47059542012-06-06 20:51:20 +00001166 SkScalar width = (fill == kHairLine_GrPathFill) ? 0 : -SK_Scalar1;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001167 this->drawOval(paint, ovalRect, width);
1168 return;
1169 }
1170
1171 internalDrawPath(paint, path, fill, translate);
1172}
1173
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001174void GrContext::internalDrawPath(const GrPaint& paint, const SkPath& path,
bsalomon@google.com93c96602012-04-27 13:05:21 +00001175 GrPathFill fill, const GrPoint* translate) {
1176
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001177 // Note that below we may sw-rasterize the path into a scratch texture.
1178 // Scratch textures can be recycled after they are returned to the texture
1179 // cache. This presents a potential hazard for buffered drawing. However,
1180 // the writePixels that uploads to the scratch will perform a flush so we're
1181 // OK.
1182 DrawCategory category = (DEFER_PATHS) ? kBuffered_DrawCategory :
1183 kUnbuffered_DrawCategory;
1184 GrDrawTarget* target = this->prepareToDraw(paint, category);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +00001185 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com150d2842012-01-12 20:19:56 +00001186 GrDrawState::StageMask stageMask = paint.getActiveStageMask();
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001187
bsalomon@google.com289533a2011-10-27 12:34:25 +00001188 bool prAA = paint.fAntiAlias && !this->getRenderTarget()->isMultisampled();
1189
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001190 // An Assumption here is that path renderer would use some form of tweaking
1191 // the src color (either the input alpha or in the frag shader) to implement
1192 // aa. If we have some future driver-mojo path AA that can do the right
1193 // thing WRT to the blend then we'll need some query on the PR.
1194 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001195#if GR_DEBUG
bsalomon@google.com979432b2011-11-05 21:38:22 +00001196 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001197#endif
bsalomon@google.com289533a2011-10-27 12:34:25 +00001198 prAA = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001199 }
bsalomon@google.com289533a2011-10-27 12:34:25 +00001200
robertphillips@google.com72176b22012-05-23 13:19:12 +00001201 GrPathRenderer* pr = this->getPathRenderer(path, fill, target, prAA, true);
bsalomon@google.com30085192011-08-19 15:42:31 +00001202 if (NULL == pr) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001203#if GR_DEBUG
bsalomon@google.com30085192011-08-19 15:42:31 +00001204 GrPrintf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001205#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001206 return;
1207 }
1208
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001209 pr->drawPath(path, fill, translate, target, stageMask, prAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001210}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001211
bsalomon@google.com27847de2011-02-22 20:59:41 +00001212////////////////////////////////////////////////////////////////////////////////
1213
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001214void GrContext::flush(int flagsBitfield) {
1215 if (kDiscard_FlushBit & flagsBitfield) {
1216 fDrawBuffer->reset();
1217 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001218 this->flushDrawBuffer();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001219 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001220 if (kForceCurrentRenderTarget_FlushBit & flagsBitfield) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001221 fGpu->forceRenderTargetFlush();
1222 }
1223}
1224
bsalomon@google.com27847de2011-02-22 20:59:41 +00001225void GrContext::flushDrawBuffer() {
junov@google.com53a55842011-06-08 22:55:10 +00001226 if (fDrawBuffer) {
robertphillips@google.com58b38182012-05-03 16:29:41 +00001227 // With addition of the AA clip path, flushing the draw buffer can
1228 // result in the generation of an AA clip mask. During this
1229 // process the SW path renderer may be invoked which recusively
1230 // calls this method (via internalWriteTexturePixels) creating
1231 // infinite recursion
1232 GrInOrderDrawBuffer* temp = fDrawBuffer;
1233 fDrawBuffer = NULL;
1234
1235 temp->flushTo(fGpu);
1236
1237 fDrawBuffer = temp;
junov@google.com53a55842011-06-08 22:55:10 +00001238 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001239}
1240
bsalomon@google.com6f379512011-11-16 20:36:03 +00001241void GrContext::internalWriteTexturePixels(GrTexture* texture,
1242 int left, int top,
1243 int width, int height,
1244 GrPixelConfig config,
1245 const void* buffer,
1246 size_t rowBytes,
1247 uint32_t flags) {
1248 SK_TRACE_EVENT0("GrContext::writeTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001249 ASSERT_OWNED_RESOURCE(texture);
1250
bsalomon@google.com6f379512011-11-16 20:36:03 +00001251 if (!(kDontFlush_PixelOpsFlag & flags)) {
1252 this->flush();
1253 }
1254 // TODO: use scratch texture to perform conversion
1255 if (GrPixelConfigIsUnpremultiplied(texture->config()) !=
1256 GrPixelConfigIsUnpremultiplied(config)) {
1257 return;
1258 }
1259
1260 fGpu->writeTexturePixels(texture, left, top, width, height,
1261 config, buffer, rowBytes);
1262}
1263
1264bool GrContext::internalReadTexturePixels(GrTexture* texture,
1265 int left, int top,
1266 int width, int height,
1267 GrPixelConfig config,
1268 void* buffer,
1269 size_t rowBytes,
1270 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001271 SK_TRACE_EVENT0("GrContext::readTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001272 ASSERT_OWNED_RESOURCE(texture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001273
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001274 // TODO: code read pixels for textures that aren't also rendertargets
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001275 GrRenderTarget* target = texture->asRenderTarget();
1276 if (NULL != target) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001277 return this->internalReadRenderTargetPixels(target,
1278 left, top, width, height,
1279 config, buffer, rowBytes,
1280 flags);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001281 } else {
1282 return false;
1283 }
1284}
1285
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001286#include "SkConfig8888.h"
1287
1288namespace {
1289/**
1290 * Converts a GrPixelConfig to a SkCanvas::Config8888. Only byte-per-channel
1291 * formats are representable as Config8888 and so the function returns false
1292 * if the GrPixelConfig has no equivalent Config8888.
1293 */
1294bool grconfig_to_config8888(GrPixelConfig config,
1295 SkCanvas::Config8888* config8888) {
1296 switch (config) {
1297 case kRGBA_8888_PM_GrPixelConfig:
1298 *config8888 = SkCanvas::kRGBA_Premul_Config8888;
1299 return true;
1300 case kRGBA_8888_UPM_GrPixelConfig:
1301 *config8888 = SkCanvas::kRGBA_Unpremul_Config8888;
1302 return true;
1303 case kBGRA_8888_PM_GrPixelConfig:
1304 *config8888 = SkCanvas::kBGRA_Premul_Config8888;
1305 return true;
1306 case kBGRA_8888_UPM_GrPixelConfig:
1307 *config8888 = SkCanvas::kBGRA_Unpremul_Config8888;
1308 return true;
1309 default:
1310 return false;
1311 }
1312}
1313}
1314
bsalomon@google.com6f379512011-11-16 20:36:03 +00001315bool GrContext::internalReadRenderTargetPixels(GrRenderTarget* target,
1316 int left, int top,
1317 int width, int height,
1318 GrPixelConfig config,
1319 void* buffer,
1320 size_t rowBytes,
1321 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001322 SK_TRACE_EVENT0("GrContext::readRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001323 ASSERT_OWNED_RESOURCE(target);
1324
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001325 if (NULL == target) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001326 target = fDrawState->getRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001327 if (NULL == target) {
1328 return false;
1329 }
1330 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001331
bsalomon@google.com6f379512011-11-16 20:36:03 +00001332 if (!(kDontFlush_PixelOpsFlag & flags)) {
1333 this->flush();
1334 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001335
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001336 if (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1337 GrPixelConfigIsUnpremultiplied(config) &&
1338 !fGpu->canPreserveReadWriteUnpremulPixels()) {
1339 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1340 if (!grconfig_to_config8888(target->config(), &srcConfig8888) ||
1341 !grconfig_to_config8888(config, &dstConfig8888)) {
1342 return false;
1343 }
1344 // do read back using target's own config
1345 this->internalReadRenderTargetPixels(target,
1346 left, top,
1347 width, height,
1348 target->config(),
1349 buffer, rowBytes,
1350 kDontFlush_PixelOpsFlag);
1351 // sw convert the pixels to unpremul config
1352 uint32_t* pixels = reinterpret_cast<uint32_t*>(buffer);
1353 SkConvertConfig8888Pixels(pixels, rowBytes, dstConfig8888,
1354 pixels, rowBytes, srcConfig8888,
1355 width, height);
1356 return true;
1357 }
1358
bsalomon@google.comc4364992011-11-07 15:54:49 +00001359 GrTexture* src = target->asTexture();
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001360 bool swapRAndB = NULL != src &&
1361 fGpu->preferredReadPixelsConfig(config) ==
1362 GrPixelConfigSwapRAndB(config);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001363
1364 bool flipY = NULL != src &&
1365 fGpu->readPixelsWillPayForYFlip(target, left, top,
1366 width, height, config,
1367 rowBytes);
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001368 bool alphaConversion = (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1369 GrPixelConfigIsUnpremultiplied(config));
bsalomon@google.comc4364992011-11-07 15:54:49 +00001370
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001371 if (NULL == src && alphaConversion) {
1372 // we should fallback to cpu conversion here. This could happen when
1373 // we were given an external render target by the client that is not
1374 // also a texture (e.g. FBO 0 in GL)
1375 return false;
1376 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001377 // we draw to a scratch texture if any of these conversion are applied
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001378 GrAutoScratchTexture ast;
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001379 if (flipY || swapRAndB || alphaConversion) {
1380 GrAssert(NULL != src);
1381 if (swapRAndB) {
1382 config = GrPixelConfigSwapRAndB(config);
1383 GrAssert(kUnknown_GrPixelConfig != config);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001384 }
1385 // Make the scratch a render target because we don't have a robust
1386 // readTexturePixels as of yet (it calls this function).
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001387 GrTextureDesc desc;
1388 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1389 desc.fWidth = width;
1390 desc.fHeight = height;
1391 desc.fConfig = config;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001392
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001393 // When a full readback is faster than a partial we could always make
1394 // the scratch exactly match the passed rect. However, if we see many
1395 // different size rectangles we will trash our texture cache and pay the
1396 // cost of creating and destroying many textures. So, we only request
1397 // an exact match when the caller is reading an entire RT.
1398 ScratchTexMatch match = kApprox_ScratchTexMatch;
1399 if (0 == left &&
1400 0 == top &&
1401 target->width() == width &&
1402 target->height() == height &&
1403 fGpu->fullReadPixelsIsFasterThanPartial()) {
1404 match = kExact_ScratchTexMatch;
1405 }
1406 ast.set(this, desc, match);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001407 GrTexture* texture = ast.texture();
1408 if (!texture) {
1409 return false;
1410 }
1411 target = texture->asRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001412 GrAssert(NULL != target);
1413
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001414 GrDrawTarget::AutoStateRestore asr(fGpu,
1415 GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001416 GrDrawState* drawState = fGpu->drawState();
1417 drawState->setRenderTarget(target);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001418
bsalomon@google.comc4364992011-11-07 15:54:49 +00001419 GrMatrix matrix;
1420 if (flipY) {
1421 matrix.setTranslate(SK_Scalar1 * left,
1422 SK_Scalar1 * (top + height));
1423 matrix.set(GrMatrix::kMScaleY, -GR_Scalar1);
1424 } else {
1425 matrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
1426 }
1427 matrix.postIDiv(src->width(), src->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001428 drawState->sampler(0)->reset(matrix);
1429 drawState->sampler(0)->setRAndBSwap(swapRAndB);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001430 drawState->setTexture(0, src);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001431 GrRect rect;
1432 rect.setXYWH(0, 0, SK_Scalar1 * width, SK_Scalar1 * height);
1433 fGpu->drawSimpleRect(rect, NULL, 0x1);
1434 left = 0;
1435 top = 0;
1436 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001437 return fGpu->readPixels(target,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001438 left, top, width, height,
1439 config, buffer, rowBytes, flipY);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001440}
1441
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001442void GrContext::resolveRenderTarget(GrRenderTarget* target) {
1443 GrAssert(target);
1444 ASSERT_OWNED_RESOURCE(target);
1445 // In the future we may track whether there are any pending draws to this
1446 // target. We don't today so we always perform a flush. We don't promise
1447 // this to our clients, though.
1448 this->flush();
1449 fGpu->resolveRenderTarget(target);
1450}
1451
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001452void GrContext::copyTexture(GrTexture* src, GrRenderTarget* dst) {
1453 if (NULL == src || NULL == dst) {
1454 return;
1455 }
1456 ASSERT_OWNED_RESOURCE(src);
1457
twiz@google.com1ac87ff2012-04-27 19:39:33 +00001458 // Writes pending to the source texture are not tracked, so a flush
1459 // is required to ensure that the copy captures the most recent contents
1460 // of the source texture. See similar behaviour in
1461 // GrContext::resolveRenderTarget.
1462 this->flush();
1463
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001464 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001465 GrDrawState* drawState = fGpu->drawState();
1466 drawState->setRenderTarget(dst);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001467 GrMatrix sampleM;
1468 sampleM.setIDiv(src->width(), src->height());
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001469 drawState->setTexture(0, src);
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001470 drawState->sampler(0)->reset(sampleM);
bsalomon@google.com5db3b6c2012-01-12 20:38:57 +00001471 SkRect rect = SkRect::MakeXYWH(0, 0,
1472 SK_Scalar1 * src->width(),
1473 SK_Scalar1 * src->height());
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001474 fGpu->drawSimpleRect(rect, NULL, 1 << 0);
1475}
1476
bsalomon@google.com6f379512011-11-16 20:36:03 +00001477void GrContext::internalWriteRenderTargetPixels(GrRenderTarget* target,
1478 int left, int top,
1479 int width, int height,
1480 GrPixelConfig config,
1481 const void* buffer,
1482 size_t rowBytes,
1483 uint32_t flags) {
1484 SK_TRACE_EVENT0("GrContext::writeRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001485 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com6f379512011-11-16 20:36:03 +00001486
1487 if (NULL == target) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001488 target = fDrawState->getRenderTarget();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001489 if (NULL == target) {
1490 return;
1491 }
1492 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001493
1494 // TODO: when underlying api has a direct way to do this we should use it
1495 // (e.g. glDrawPixels on desktop GL).
1496
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001497 // If the RT is also a texture and we don't have to do PM/UPM conversion
1498 // then take the texture path, which we expect to be at least as fast or
1499 // faster since it doesn't use an intermediate texture as we do below.
1500
1501#if !GR_MAC_BUILD
1502 // At least some drivers on the Mac get confused when glTexImage2D is called
1503 // on a texture attached to an FBO. The FBO still sees the old image. TODO:
1504 // determine what OS versions and/or HW is affected.
1505 if (NULL != target->asTexture() &&
1506 GrPixelConfigIsUnpremultiplied(target->config()) ==
1507 GrPixelConfigIsUnpremultiplied(config)) {
1508
1509 this->internalWriteTexturePixels(target->asTexture(),
1510 left, top, width, height,
1511 config, buffer, rowBytes, flags);
1512 return;
1513 }
1514#endif
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001515 if (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1516 GrPixelConfigIsUnpremultiplied(config) &&
1517 !fGpu->canPreserveReadWriteUnpremulPixels()) {
1518 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1519 if (!grconfig_to_config8888(config, &srcConfig8888) ||
1520 !grconfig_to_config8888(target->config(), &dstConfig8888)) {
1521 return;
1522 }
1523 // allocate a tmp buffer and sw convert the pixels to premul
1524 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(width * height);
1525 const uint32_t* src = reinterpret_cast<const uint32_t*>(buffer);
1526 SkConvertConfig8888Pixels(tmpPixels.get(), 4 * width, dstConfig8888,
1527 src, rowBytes, srcConfig8888,
1528 width, height);
1529 // upload the already premul pixels
1530 this->internalWriteRenderTargetPixels(target,
1531 left, top,
1532 width, height,
1533 target->config(),
1534 tmpPixels, 4 * width, flags);
1535 return;
1536 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001537
1538 bool swapRAndB = fGpu->preferredReadPixelsConfig(config) ==
1539 GrPixelConfigSwapRAndB(config);
1540 if (swapRAndB) {
1541 config = GrPixelConfigSwapRAndB(config);
1542 }
1543
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001544 GrTextureDesc desc;
1545 desc.fWidth = width;
1546 desc.fHeight = height;
1547 desc.fConfig = config;
1548
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001549 GrAutoScratchTexture ast(this, desc);
1550 GrTexture* texture = ast.texture();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001551 if (NULL == texture) {
1552 return;
1553 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001554 this->internalWriteTexturePixels(texture, 0, 0, width, height,
1555 config, buffer, rowBytes, flags);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001556
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001557 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001558 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001559
1560 GrMatrix matrix;
1561 matrix.setTranslate(GrIntToScalar(left), GrIntToScalar(top));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001562 drawState->setViewMatrix(matrix);
1563 drawState->setRenderTarget(target);
1564 drawState->setTexture(0, texture);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001565
bsalomon@google.com5c638652011-07-18 19:31:59 +00001566 matrix.setIDiv(texture->width(), texture->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001567 drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode,
1568 GrSamplerState::kNearest_Filter,
1569 matrix);
1570 drawState->sampler(0)->setRAndBSwap(swapRAndB);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001571
tomhudson@google.comb213ed82012-06-25 15:22:12 +00001572 static const GrVertexLayout layout = 0;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001573 static const int VCOUNT = 4;
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001574 // TODO: Use GrGpu::drawRect here
bsalomon@google.com27847de2011-02-22 20:59:41 +00001575 GrDrawTarget::AutoReleaseGeometry geo(fGpu, layout, VCOUNT, 0);
1576 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001577 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +00001578 return;
1579 }
1580 ((GrPoint*)geo.vertices())->setIRectFan(0, 0, width, height);
bsalomon@google.com47059542012-06-06 20:51:20 +00001581 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, VCOUNT);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001582}
1583////////////////////////////////////////////////////////////////////////////////
1584
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001585void GrContext::setPaint(const GrPaint& paint) {
tomhudson@google.comcb325ce2012-07-11 14:41:19 +00001586 GrAssert(fDrawState->stagesDisabled());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001587
1588 for (int i = 0; i < GrPaint::kMaxTextures; ++i) {
1589 int s = i + GrPaint::kFirstTextureStage;
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001590 ASSERT_OWNED_RESOURCE(paint.getTexture(i));
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001591 if (paint.isTextureStageEnabled(i)) {
1592 fDrawState->setTexture(s, paint.getTexture(i));
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001593 *fDrawState->sampler(s) = paint.getTextureSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001594 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001595 }
1596
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001597 fDrawState->setFirstCoverageStage(GrPaint::kFirstMaskStage);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001598
1599 for (int i = 0; i < GrPaint::kMaxMasks; ++i) {
1600 int s = i + GrPaint::kFirstMaskStage;
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001601 ASSERT_OWNED_RESOURCE(paint.getMask(i));
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001602 if (paint.isMaskStageEnabled(i)) {
1603 fDrawState->setTexture(s, paint.getMask(i));
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001604 *fDrawState->sampler(s) = paint.getMaskSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001605 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001606 }
bsalomon@google.com26936d02012-03-19 13:06:19 +00001607
1608 // disable all stages not accessible via the paint
1609 for (int s = GrPaint::kTotalStages; s < GrDrawState::kNumStages; ++s) {
tomhudson@google.com676e6602012-07-10 17:21:48 +00001610 fDrawState->disableStage(s);
bsalomon@google.com26936d02012-03-19 13:06:19 +00001611 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001612
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001613 fDrawState->setColor(paint.fColor);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001614
1615 if (paint.fDither) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001616 fDrawState->enableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001617 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001618 fDrawState->disableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001619 }
1620 if (paint.fAntiAlias) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001621 fDrawState->enableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001622 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001623 fDrawState->disableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001624 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001625 if (paint.fColorMatrixEnabled) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001626 fDrawState->enableState(GrDrawState::kColorMatrix_StateBit);
1627 fDrawState->setColorMatrix(paint.fColorMatrix);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001628 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001629 fDrawState->disableState(GrDrawState::kColorMatrix_StateBit);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001630 }
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001631 fDrawState->setBlendFunc(paint.fSrcBlendCoeff, paint.fDstBlendCoeff);
1632 fDrawState->setColorFilter(paint.fColorFilterColor, paint.fColorFilterXfermode);
1633 fDrawState->setCoverage(paint.fCoverage);
reed@google.com4b2d3f32012-05-15 18:05:50 +00001634#if GR_DEBUG_PARTIAL_COVERAGE_CHECK
bsalomon@google.come79c8152012-03-29 19:07:12 +00001635 if ((paint.getActiveMaskStageMask() || 0xff != paint.fCoverage) &&
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001636 !fGpu->canApplyCoverage()) {
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001637 GrPrintf("Partial pixel coverage will be incorrectly blended.\n");
1638 }
bsalomon@google.com95cd7bd2012-03-28 15:35:05 +00001639#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +00001640}
1641
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001642GrDrawTarget* GrContext::prepareToDraw(const GrPaint& paint,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001643 DrawCategory category) {
1644 if (category != fLastDrawCategory) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001645 this->flushDrawBuffer();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001646 fLastDrawCategory = category;
1647 }
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001648 this->setPaint(paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001649 GrDrawTarget* target = fGpu;
1650 switch (category) {
bsalomon@google.com193395c2012-03-30 17:35:12 +00001651 case kUnbuffered_DrawCategory:
1652 target = fGpu;
1653 break;
1654 case kBuffered_DrawCategory:
1655 target = fDrawBuffer;
1656 fDrawBuffer->setClip(fGpu->getClip());
1657 break;
1658 default:
1659 GrCrash("Unexpected DrawCategory.");
1660 break;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001661 }
1662 return target;
1663}
1664
robertphillips@google.com72176b22012-05-23 13:19:12 +00001665/*
1666 * This method finds a path renderer that can draw the specified path on
1667 * the provided target.
1668 * Due to its expense, the software path renderer has split out so it can
1669 * can be individually allowed/disallowed via the "allowSW" boolean.
1670 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001671GrPathRenderer* GrContext::getPathRenderer(const SkPath& path,
bsalomon@google.com289533a2011-10-27 12:34:25 +00001672 GrPathFill fill,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001673 const GrDrawTarget* target,
robertphillips@google.com72176b22012-05-23 13:19:12 +00001674 bool antiAlias,
1675 bool allowSW) {
bsalomon@google.com30085192011-08-19 15:42:31 +00001676 if (NULL == fPathRendererChain) {
1677 fPathRendererChain =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001678 SkNEW_ARGS(GrPathRendererChain,
1679 (this, GrPathRendererChain::kNone_UsageFlag));
bsalomon@google.com30085192011-08-19 15:42:31 +00001680 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001681
1682 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(path, fill,
1683 target,
1684 antiAlias);
1685
1686 if (NULL == pr && allowSW) {
1687 if (NULL == fSoftwarePathRenderer) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001688 fSoftwarePathRenderer = SkNEW_ARGS(GrSoftwarePathRenderer, (this));
robertphillips@google.com72176b22012-05-23 13:19:12 +00001689 }
1690
1691 pr = fSoftwarePathRenderer;
1692 }
1693
1694 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001695}
1696
bsalomon@google.com27847de2011-02-22 20:59:41 +00001697////////////////////////////////////////////////////////////////////////////////
1698
bsalomon@google.com27847de2011-02-22 20:59:41 +00001699void GrContext::setRenderTarget(GrRenderTarget* target) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001700 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001701 if (fDrawState->getRenderTarget() != target) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001702 this->flush(false);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001703 fDrawState->setRenderTarget(target);
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001704 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001705}
1706
1707GrRenderTarget* GrContext::getRenderTarget() {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001708 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001709}
1710
1711const GrRenderTarget* GrContext::getRenderTarget() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001712 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001713}
1714
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001715bool GrContext::isConfigRenderable(GrPixelConfig config) const {
1716 return fGpu->isConfigRenderable(config);
1717}
1718
bsalomon@google.com27847de2011-02-22 20:59:41 +00001719const GrMatrix& GrContext::getMatrix() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001720 return fDrawState->getViewMatrix();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001721}
1722
1723void GrContext::setMatrix(const GrMatrix& m) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001724 fDrawState->setViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001725}
1726
1727void GrContext::concatMatrix(const GrMatrix& m) const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001728 fDrawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001729}
1730
1731static inline intptr_t setOrClear(intptr_t bits, int shift, intptr_t pred) {
1732 intptr_t mask = 1 << shift;
1733 if (pred) {
1734 bits |= mask;
1735 } else {
1736 bits &= ~mask;
1737 }
1738 return bits;
1739}
1740
bsalomon@google.com583a1e32011-08-17 13:42:46 +00001741GrContext::GrContext(GrGpu* gpu) {
bsalomon@google.comc0af3172012-06-15 14:10:09 +00001742 ++THREAD_INSTANCE_COUNT;
1743
bsalomon@google.com27847de2011-02-22 20:59:41 +00001744 fGpu = gpu;
1745 fGpu->ref();
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001746 fGpu->setContext(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001747
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001748 fDrawState = SkNEW(GrDrawState);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001749 fGpu->setDrawState(fDrawState);
1750
bsalomon@google.com30085192011-08-19 15:42:31 +00001751 fPathRendererChain = NULL;
robertphillips@google.com72176b22012-05-23 13:19:12 +00001752 fSoftwarePathRenderer = NULL;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001753
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001754 fTextureCache = SkNEW_ARGS(GrResourceCache,
1755 (MAX_TEXTURE_CACHE_COUNT,
1756 MAX_TEXTURE_CACHE_BYTES));
1757 fFontCache = SkNEW_ARGS(GrFontCache, (fGpu));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001758
1759 fLastDrawCategory = kUnbuffered_DrawCategory;
1760
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001761 fDrawBuffer = NULL;
1762 fDrawBufferVBAllocPool = NULL;
1763 fDrawBufferIBAllocPool = NULL;
1764
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001765 fAARectRenderer = SkNEW(GrAARectRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001766
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001767 this->setupDrawBuffer();
1768}
1769
1770void GrContext::setupDrawBuffer() {
1771
1772 GrAssert(NULL == fDrawBuffer);
1773 GrAssert(NULL == fDrawBufferVBAllocPool);
1774 GrAssert(NULL == fDrawBufferIBAllocPool);
1775
bsalomon@google.com92edd312012-04-04 21:40:21 +00001776#if DEFER_TEXT_RENDERING || BATCH_RECT_TO_RECT || DEFER_PATHS
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001777 fDrawBufferVBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001778 SkNEW_ARGS(GrVertexBufferAllocPool, (fGpu, false,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001779 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001780 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS));
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001781 fDrawBufferIBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001782 SkNEW_ARGS(GrIndexBufferAllocPool, (fGpu, false,
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001783 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001784 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001785
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001786 fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (fGpu,
bsalomon@google.com471d4712011-08-23 15:45:25 +00001787 fDrawBufferVBAllocPool,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001788 fDrawBufferIBAllocPool));
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001789#endif
1790
1791#if BATCH_RECT_TO_RECT
bsalomon@google.com27847de2011-02-22 20:59:41 +00001792 fDrawBuffer->setQuadIndexBuffer(this->getQuadIndexBuffer());
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001793#endif
bsalomon@google.com1015e032012-06-25 18:41:04 +00001794 if (fDrawBuffer) {
1795 fDrawBuffer->setAutoFlushTarget(fGpu);
1796 fDrawBuffer->setDrawState(fDrawState);
1797 }
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();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001819 GrTexture* origTexture = srcTexture;
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001820 AutoMatrix avm(this, GrMatrix::I());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001821 SkIRect clearRect;
bsalomon@google.comb505a122012-05-31 18:40:36 +00001822 int scaleFactorX, radiusX;
1823 int scaleFactorY, radiusY;
1824 sigmaX = adjust_sigma(sigmaX, &scaleFactorX, &radiusX);
1825 sigmaY = adjust_sigma(sigmaY, &scaleFactorY, &radiusY);
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001826
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001827 SkRect srcRect(rect);
1828 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
1829 srcRect.roundOut();
robertphillips@google.com8637a362012-04-10 18:32:35 +00001830 scale_rect(&srcRect, static_cast<float>(scaleFactorX),
1831 static_cast<float>(scaleFactorY));
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +00001832
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001833 AutoClip acs(this, srcRect);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001834
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);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001933 return srcTexture;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001934}
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001935
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001936GrTexture* GrContext::applyMorphology(GrTexture* srcTexture,
1937 const GrRect& rect,
1938 GrTexture* temp1, GrTexture* temp2,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001939 MorphologyType morphType,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001940 SkISize radius) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001941 ASSERT_OWNED_RESOURCE(srcTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001942 GrRenderTarget* oldRenderTarget = this->getRenderTarget();
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +00001943
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001944 AutoMatrix avm(this, GrMatrix::I());
1945
1946 AutoClip acs(this, GrRect::MakeWH(SkIntToScalar(srcTexture->width()),
1947 SkIntToScalar(srcTexture->height())));
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +00001948
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001949 if (radius.fWidth > 0) {
1950 this->setRenderTarget(temp1->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001951 apply_morphology(fGpu, srcTexture, rect, radius.fWidth, morphType,
1952 Gr1DKernelEffect::kX_Direction);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001953 SkIRect clearRect = SkIRect::MakeXYWH(
1954 SkScalarFloorToInt(rect.fLeft),
1955 SkScalarFloorToInt(rect.fBottom),
1956 SkScalarFloorToInt(rect.width()),
1957 radius.fHeight);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001958 this->clear(&clearRect, 0x0);
1959 srcTexture = temp1;
1960 }
1961 if (radius.fHeight > 0) {
1962 this->setRenderTarget(temp2->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001963 apply_morphology(fGpu, srcTexture, rect, radius.fHeight, morphType,
1964 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001965 srcTexture = temp2;
1966 }
1967 this->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001968 return srcTexture;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001969}
bsalomon@google.comc4364992011-11-07 15:54:49 +00001970
1971///////////////////////////////////////////////////////////////////////////////