blob: 6e5fb08535cf05dc0be01511be87de7832cb8397 [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.comd0c1a062012-07-12 17:23:52 +0000247 SkNEW_ARGS(GrMorphologyEffect, (texture, 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 gpu->drawSimpleRect(rect, NULL, 1 << 0);
250}
251
bsalomon@google.comb505a122012-05-31 18:40:36 +0000252void convolve_gaussian(GrGpu* gpu,
253 GrTexture* texture,
254 const SkRect& rect,
255 float sigma,
256 int radius,
257 Gr1DKernelEffect::Direction direction) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000258 GrRenderTarget* target = gpu->drawState()->getRenderTarget();
259 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000260 GrDrawState* drawState = gpu->drawState();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000261 drawState->setRenderTarget(target);
262 GrMatrix sampleM;
263 sampleM.setIDiv(texture->width(), texture->height());
bsalomon@google.comb505a122012-05-31 18:40:36 +0000264 drawState->sampler(0)->reset(sampleM);
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000265 SkAutoTUnref<GrConvolutionEffect> conv(SkNEW_ARGS(GrConvolutionEffect,
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000266 (texture, direction, radius,
267 sigma)));
bsalomon@google.comb505a122012-05-31 18:40:36 +0000268 drawState->sampler(0)->setCustomStage(conv);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000269 gpu->drawSimpleRect(rect, NULL, 1 << 0);
270}
271
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000272}
273
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000274GrContext::TextureCacheEntry GrContext::findAndLockTexture(
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000275 const GrTextureDesc& desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000276 const GrSamplerState* sampler) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000277 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, sampler, desc, false);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000278 return TextureCacheEntry(fTextureCache->findAndLock(resourceKey,
279 GrResourceCache::kNested_LockType));
280}
281
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000282bool GrContext::isTextureInCache(const GrTextureDesc& desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000283 const GrSamplerState* sampler) const {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000284 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, sampler, desc, false);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000285 return fTextureCache->hasKey(resourceKey);
286}
287
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000288GrResourceEntry* GrContext::addAndLockStencilBuffer(GrStencilBuffer* sb) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000289 ASSERT_OWNED_RESOURCE(sb);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000290 uint32_t v[4];
291 gen_stencil_key_values(sb, v);
292 GrResourceKey resourceKey(v);
293 return fTextureCache->createAndLock(resourceKey, sb);
294}
295
296GrStencilBuffer* GrContext::findStencilBuffer(int width, int height,
297 int sampleCnt) {
298 uint32_t v[4];
299 gen_stencil_key_values(width, height, sampleCnt, v);
300 GrResourceKey resourceKey(v);
301 GrResourceEntry* entry = fTextureCache->findAndLock(resourceKey,
302 GrResourceCache::kSingle_LockType);
303 if (NULL != entry) {
304 GrStencilBuffer* sb = (GrStencilBuffer*) entry->resource();
305 return sb;
306 } else {
307 return NULL;
308 }
309}
310
311void GrContext::unlockStencilBuffer(GrResourceEntry* sbEntry) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000312 ASSERT_OWNED_RESOURCE(sbEntry->resource());
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000313 fTextureCache->unlock(sbEntry);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000314}
315
316static void stretchImage(void* dst,
317 int dstW,
318 int dstH,
319 void* src,
320 int srcW,
321 int srcH,
322 int bpp) {
323 GrFixed dx = (srcW << 16) / dstW;
324 GrFixed dy = (srcH << 16) / dstH;
325
326 GrFixed y = dy >> 1;
327
328 int dstXLimit = dstW*bpp;
329 for (int j = 0; j < dstH; ++j) {
330 GrFixed x = dx >> 1;
331 void* srcRow = (uint8_t*)src + (y>>16)*srcW*bpp;
332 void* dstRow = (uint8_t*)dst + j*dstW*bpp;
333 for (int i = 0; i < dstXLimit; i += bpp) {
334 memcpy((uint8_t*) dstRow + i,
335 (uint8_t*) srcRow + (x>>16)*bpp,
336 bpp);
337 x += dx;
338 }
339 y += dy;
340 }
341}
342
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000343GrContext::TextureCacheEntry GrContext::createAndLockTexture(
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000344 const GrSamplerState* sampler,
345 const GrTextureDesc& desc,
346 void* srcData,
347 size_t rowBytes) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000348 SK_TRACE_EVENT0("GrContext::createAndLockTexture");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000349
350#if GR_DUMP_TEXTURE_UPLOAD
351 GrPrintf("GrContext::createAndLockTexture [%d %d]\n", desc.fWidth, desc.fHeight);
352#endif
353
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000354 TextureCacheEntry entry;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000355
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000356 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, sampler,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000357 desc, false);
358
359 if (GrTexture::NeedsResizing(resourceKey)) {
360 // The desired texture is NPOT and tiled but that isn't supported by
361 // the current hardware. Resize the texture to be a POT
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000362 GrAssert(NULL != sampler);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000363 TextureCacheEntry clampEntry = this->findAndLockTexture(desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000364 NULL);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000365
366 if (NULL == clampEntry.texture()) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000367 clampEntry = this->createAndLockTexture(NULL, desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000368 srcData, rowBytes);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000369 GrAssert(NULL != clampEntry.texture());
370 if (NULL == clampEntry.texture()) {
371 return entry;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000372 }
373 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000374 GrTextureDesc rtDesc = desc;
375 rtDesc.fFlags = rtDesc.fFlags |
376 kRenderTarget_GrTextureFlagBit |
377 kNoStencil_GrTextureFlagBit;
bsalomon@google.com99621082011-11-15 16:47:16 +0000378 rtDesc.fWidth = GrNextPow2(GrMax(desc.fWidth, 64));
379 rtDesc.fHeight = GrNextPow2(GrMax(desc.fHeight, 64));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000380
381 GrTexture* texture = fGpu->createTexture(rtDesc, NULL, 0);
382
383 if (NULL != texture) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000384 GrDrawTarget::AutoStateRestore asr(fGpu,
385 GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000386 GrDrawState* drawState = fGpu->drawState();
387 drawState->setRenderTarget(texture->asRenderTarget());
388 drawState->setTexture(0, clampEntry.texture());
bsalomon@google.com82c7bd82011-11-09 15:32:29 +0000389
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000390 GrSamplerState::Filter filter;
391 // if filtering is not desired then we want to ensure all
392 // texels in the resampled image are copies of texels from
393 // the original.
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000394 if (GrTexture::NeedsFiltering(resourceKey)) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000395 filter = GrSamplerState::kBilinear_Filter;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000396 } else {
397 filter = GrSamplerState::kNearest_Filter;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000398 }
bsalomon@google.com1e266f82011-12-12 16:11:33 +0000399 drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode,
400 filter);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000401
402 static const GrVertexLayout layout =
403 GrDrawTarget::StageTexCoordVertexLayoutBit(0,0);
404 GrDrawTarget::AutoReleaseGeometry arg(fGpu, layout, 4, 0);
405
406 if (arg.succeeded()) {
407 GrPoint* verts = (GrPoint*) arg.vertices();
408 verts[0].setIRectFan(0, 0,
409 texture->width(),
410 texture->height(),
411 2*sizeof(GrPoint));
412 verts[1].setIRectFan(0, 0, 1, 1, 2*sizeof(GrPoint));
bsalomon@google.com47059542012-06-06 20:51:20 +0000413 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000414 0, 4);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000415 entry.set(fTextureCache->createAndLock(resourceKey, texture));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000416 }
bsalomon@google.com1da07462011-03-10 14:51:57 +0000417 texture->releaseRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000418 } else {
419 // TODO: Our CPU stretch doesn't filter. But we create separate
420 // stretched textures when the sampler state is either filtered or
421 // not. Either implement filtered stretch blit on CPU or just create
422 // one when FBO case fails.
423
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000424 rtDesc.fFlags = kNone_GrTextureFlags;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000425 // no longer need to clamp at min RT size.
426 rtDesc.fWidth = GrNextPow2(desc.fWidth);
427 rtDesc.fHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000428 int bpp = GrBytesPerPixel(desc.fConfig);
bsalomon@google.com3582bf92011-06-30 21:32:31 +0000429 SkAutoSMalloc<128*128*4> stretchedPixels(bpp *
bsalomon@google.com27847de2011-02-22 20:59:41 +0000430 rtDesc.fWidth *
431 rtDesc.fHeight);
432 stretchImage(stretchedPixels.get(), rtDesc.fWidth, rtDesc.fHeight,
433 srcData, desc.fWidth, desc.fHeight, bpp);
434
435 size_t stretchedRowBytes = rtDesc.fWidth * bpp;
436
437 GrTexture* texture = fGpu->createTexture(rtDesc,
438 stretchedPixels.get(),
439 stretchedRowBytes);
440 GrAssert(NULL != texture);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000441 entry.set(fTextureCache->createAndLock(resourceKey, texture));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000442 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000443 fTextureCache->unlock(clampEntry.cacheEntry());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000444
445 } else {
446 GrTexture* texture = fGpu->createTexture(desc, srcData, rowBytes);
447 if (NULL != texture) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000448 entry.set(fTextureCache->createAndLock(resourceKey, texture));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000449 }
450 }
451 return entry;
452}
453
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000454GrContext::TextureCacheEntry GrContext::lockScratchTexture(
455 const GrTextureDesc& inDesc,
456 ScratchTexMatch match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000457 GrTextureDesc desc = inDesc;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000458 desc.fClientCacheID = kScratch_CacheID;
459
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000460 if (kExact_ScratchTexMatch != match) {
461 // bin by pow2 with a reasonable min
462 static const int MIN_SIZE = 256;
463 desc.fWidth = GrMax(MIN_SIZE, GrNextPow2(desc.fWidth));
464 desc.fHeight = GrMax(MIN_SIZE, GrNextPow2(desc.fHeight));
465 }
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000466
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000467 GrResourceEntry* entry;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000468 int origWidth = desc.fWidth;
469 int origHeight = desc.fHeight;
470 bool doubledW = false;
471 bool doubledH = false;
472
473 do {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000474 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL, desc, true);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000475 entry = fTextureCache->findAndLock(key,
476 GrResourceCache::kNested_LockType);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000477 // if we miss, relax the fit of the flags...
478 // then try doubling width... then height.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000479 if (NULL != entry || kExact_ScratchTexMatch == match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000480 break;
481 }
482 if (!(desc.fFlags & kRenderTarget_GrTextureFlagBit)) {
483 desc.fFlags = desc.fFlags | kRenderTarget_GrTextureFlagBit;
484 } else if (desc.fFlags & kNoStencil_GrTextureFlagBit) {
485 desc.fFlags = desc.fFlags & ~kNoStencil_GrTextureFlagBit;
486 } else if (!doubledW) {
487 desc.fFlags = inDesc.fFlags;
488 desc.fWidth *= 2;
489 doubledW = true;
490 } else if (!doubledH) {
491 desc.fFlags = inDesc.fFlags;
492 desc.fWidth = origWidth;
493 desc.fHeight *= 2;
494 doubledH = true;
495 } else {
496 break;
497 }
498
499 } while (true);
500
501 if (NULL == entry) {
502 desc.fFlags = inDesc.fFlags;
503 desc.fWidth = origWidth;
504 desc.fHeight = origHeight;
505 GrTexture* texture = fGpu->createTexture(desc, NULL, 0);
506 if (NULL != texture) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000507 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000508 texture->desc(),
509 true);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000510 entry = fTextureCache->createAndLock(key, texture);
511 }
512 }
513
514 // If the caller gives us the same desc/sampler twice we don't want
515 // to return the same texture the second time (unless it was previously
516 // released). So we detach the entry from the cache and reattach at release.
517 if (NULL != entry) {
518 fTextureCache->detach(entry);
519 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000520 return TextureCacheEntry(entry);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000521}
522
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000523void GrContext::addExistingTextureToCache(GrTexture* texture) {
524
525 if (NULL == texture) {
526 return;
527 }
528
529 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL,
530 texture->desc(),
531 true);
532 fTextureCache->attach(key, texture);
533}
534
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000535void GrContext::unlockTexture(TextureCacheEntry entry) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000536 ASSERT_OWNED_RESOURCE(entry.texture());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000537 // If this is a scratch texture we detached it from the cache
538 // while it was locked (to avoid two callers simultaneously getting
539 // the same texture).
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000540 if (GrTexture::IsScratchTexture(entry.cacheEntry()->key())) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000541 fTextureCache->reattachAndUnlock(entry.cacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000542 } else {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000543 fTextureCache->unlock(entry.cacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000544 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000545}
546
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000547void GrContext::freeEntry(TextureCacheEntry entry) {
548 ASSERT_OWNED_RESOURCE(entry.texture());
549
550 fTextureCache->freeEntry(entry.cacheEntry());
551}
552
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000553GrTexture* GrContext::createUncachedTexture(const GrTextureDesc& descIn,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000554 void* srcData,
555 size_t rowBytes) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000556 GrTextureDesc descCopy = descIn;
557 descCopy.fClientCacheID = kUncached_CacheID;
558 return fGpu->createTexture(descCopy, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000559}
560
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000561void GrContext::getTextureCacheLimits(int* maxTextures,
562 size_t* maxTextureBytes) const {
563 fTextureCache->getLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000564}
565
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000566void GrContext::setTextureCacheLimits(int maxTextures, size_t maxTextureBytes) {
567 fTextureCache->setLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000568}
569
bsalomon@google.com91958362011-06-13 17:58:13 +0000570int GrContext::getMaxTextureSize() const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000571 return fGpu->getCaps().fMaxTextureSize;
bsalomon@google.com91958362011-06-13 17:58:13 +0000572}
573
574int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000575 return fGpu->getCaps().fMaxRenderTargetSize;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000576}
577
578///////////////////////////////////////////////////////////////////////////////
579
bsalomon@google.come269f212011-11-07 13:29:52 +0000580GrTexture* GrContext::createPlatformTexture(const GrPlatformTextureDesc& desc) {
581 return fGpu->createPlatformTexture(desc);
582}
583
584GrRenderTarget* GrContext::createPlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
585 return fGpu->createPlatformRenderTarget(desc);
586}
587
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000588///////////////////////////////////////////////////////////////////////////////
589
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000590bool GrContext::supportsIndex8PixelConfig(const GrSamplerState* sampler,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000591 int width, int height) const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000592 const GrDrawTarget::Caps& caps = fGpu->getCaps();
593 if (!caps.f8BitPaletteSupport) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000594 return false;
595 }
596
bsalomon@google.com27847de2011-02-22 20:59:41 +0000597 bool isPow2 = GrIsPow2(width) && GrIsPow2(height);
598
599 if (!isPow2) {
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000600 bool tiled = NULL != sampler &&
601 (sampler->getWrapX() != GrSamplerState::kClamp_WrapMode ||
602 sampler->getWrapY() != GrSamplerState::kClamp_WrapMode);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000603 if (tiled && !caps.fNPOTTextureTileSupport) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000604 return false;
605 }
606 }
607 return true;
608}
609
610////////////////////////////////////////////////////////////////////////////////
611
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000612const GrClip& GrContext::getClip() const { return fGpu->getClip(); }
613
bsalomon@google.com27847de2011-02-22 20:59:41 +0000614void GrContext::setClip(const GrClip& clip) {
615 fGpu->setClip(clip);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000616 fDrawState->enableState(GrDrawState::kClip_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000617}
618
bsalomon@google.com27847de2011-02-22 20:59:41 +0000619////////////////////////////////////////////////////////////////////////////////
620
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000621void GrContext::clear(const GrIRect* rect,
622 const GrColor color,
623 GrRenderTarget* target) {
bsalomon@google.com398109c2011-04-14 18:40:27 +0000624 this->flush();
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000625 fGpu->clear(rect, color, target);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000626}
627
628void GrContext::drawPaint(const GrPaint& paint) {
629 // set rect to be big enough to fill the space, but not super-huge, so we
630 // don't overflow fixed-point implementations
bsalomon@google.comd302f142011-03-03 13:54:13 +0000631 GrRect r;
632 r.setLTRB(0, 0,
633 GrIntToScalar(getRenderTarget()->width()),
634 GrIntToScalar(getRenderTarget()->height()));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000635 GrMatrix inverse;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000636 SkTLazy<GrPaint> tmpPaint;
637 const GrPaint* p = &paint;
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000638 AutoMatrix am;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000639
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000640 // We attempt to map r by the inverse matrix and draw that. mapRect will
641 // map the four corners and bound them with a new rect. This will not
642 // produce a correct result for some perspective matrices.
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000643 if (!this->getMatrix().hasPerspective()) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000644 if (!fDrawState->getViewInverse(&inverse)) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000645 GrPrintf("Could not invert matrix");
646 return;
647 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000648 inverse.mapRect(&r);
649 } else {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000650 if (paint.getActiveMaskStageMask() || paint.getActiveStageMask()) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000651 if (!fDrawState->getViewInverse(&inverse)) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000652 GrPrintf("Could not invert matrix");
653 return;
654 }
655 tmpPaint.set(paint);
656 tmpPaint.get()->preConcatActiveSamplerMatrices(inverse);
657 p = tmpPaint.get();
658 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000659 am.set(this, GrMatrix::I());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000660 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000661 // by definition this fills the entire clip, no need for AA
662 if (paint.fAntiAlias) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000663 if (!tmpPaint.isValid()) {
664 tmpPaint.set(paint);
665 p = tmpPaint.get();
666 }
667 GrAssert(p == tmpPaint.get());
668 tmpPaint.get()->fAntiAlias = false;
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000669 }
670 this->drawRect(*p, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000671}
672
bsalomon@google.com205d4602011-04-25 12:43:45 +0000673////////////////////////////////////////////////////////////////////////////////
674
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000675namespace {
676inline bool disable_coverage_aa_for_blend(GrDrawTarget* target) {
677 return DISABLE_COVERAGE_AA_FOR_BLEND && !target->canApplyCoverage();
678}
679}
680
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000681////////////////////////////////////////////////////////////////////////////////
682
bsalomon@google.com27847de2011-02-22 20:59:41 +0000683/* create a triangle strip that strokes the specified triangle. There are 8
684 unique vertices, but we repreat the last 2 to close up. Alternatively we
685 could use an indices array, and then only send 8 verts, but not sure that
686 would be faster.
687 */
bsalomon@google.com205d4602011-04-25 12:43:45 +0000688static void setStrokeRectStrip(GrPoint verts[10], GrRect rect,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000689 GrScalar width) {
690 const GrScalar rad = GrScalarHalf(width);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000691 rect.sort();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000692
693 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
694 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
695 verts[2].set(rect.fRight - rad, rect.fTop + rad);
696 verts[3].set(rect.fRight + rad, rect.fTop - rad);
697 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
698 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
699 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
700 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
701 verts[8] = verts[0];
702 verts[9] = verts[1];
703}
704
reed@google.com20efde72011-05-09 17:00:02 +0000705/**
706 * Returns true if the rects edges are integer-aligned.
707 */
708static bool isIRect(const GrRect& r) {
709 return GrScalarIsInt(r.fLeft) && GrScalarIsInt(r.fTop) &&
710 GrScalarIsInt(r.fRight) && GrScalarIsInt(r.fBottom);
711}
712
bsalomon@google.com205d4602011-04-25 12:43:45 +0000713static bool apply_aa_to_rect(GrDrawTarget* target,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000714 const GrRect& rect,
715 GrScalar width,
716 const GrMatrix* matrix,
717 GrMatrix* combinedMatrix,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000718 GrRect* devRect,
719 bool* useVertexCoverage) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000720 // we use a simple coverage ramp to do aa on axis-aligned rects
721 // we check if the rect will be axis-aligned, and the rect won't land on
722 // integer coords.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000723
bsalomon@google.coma3108262011-10-10 14:08:47 +0000724 // we are keeping around the "tweak the alpha" trick because
725 // it is our only hope for the fixed-pipe implementation.
726 // In a shader implementation we can give a separate coverage input
bsalomon@google.com289533a2011-10-27 12:34:25 +0000727 // TODO: remove this ugliness when we drop the fixed-pipe impl
bsalomon@google.coma3108262011-10-10 14:08:47 +0000728 *useVertexCoverage = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000729 if (!target->canTweakAlphaForCoverage()) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000730 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +0000731#if GR_DEBUG
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000732 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +0000733#endif
bsalomon@google.coma3108262011-10-10 14:08:47 +0000734 return false;
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000735 } else {
736 *useVertexCoverage = true;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000737 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000738 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000739 const GrDrawState& drawState = target->getDrawState();
740 if (drawState.getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000741 return false;
742 }
743
bsalomon@google.com471d4712011-08-23 15:45:25 +0000744 if (0 == width && target->willUseHWAALines()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000745 return false;
746 }
747
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000748 if (!drawState.getViewMatrix().preservesAxisAlignment()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000749 return false;
750 }
751
752 if (NULL != matrix &&
753 !matrix->preservesAxisAlignment()) {
754 return false;
755 }
756
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000757 *combinedMatrix = drawState.getViewMatrix();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000758 if (NULL != matrix) {
759 combinedMatrix->preConcat(*matrix);
760 GrAssert(combinedMatrix->preservesAxisAlignment());
761 }
762
763 combinedMatrix->mapRect(devRect, rect);
764 devRect->sort();
765
766 if (width < 0) {
reed@google.com20efde72011-05-09 17:00:02 +0000767 return !isIRect(*devRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000768 } else {
769 return true;
770 }
771}
772
bsalomon@google.com27847de2011-02-22 20:59:41 +0000773void GrContext::drawRect(const GrPaint& paint,
774 const GrRect& rect,
775 GrScalar width,
776 const GrMatrix* matrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000777 SK_TRACE_EVENT0("GrContext::drawRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000778
779 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000780 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000781 int stageMask = paint.getActiveStageMask();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000782
bsalomon@google.com205d4602011-04-25 12:43:45 +0000783 GrRect devRect = rect;
784 GrMatrix combinedMatrix;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000785 bool useVertexCoverage;
bsalomon@google.com289533a2011-10-27 12:34:25 +0000786 bool needAA = paint.fAntiAlias &&
787 !this->getRenderTarget()->isMultisampled();
788 bool doAA = needAA && apply_aa_to_rect(target, rect, width, matrix,
789 &combinedMatrix, &devRect,
790 &useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000791
792 if (doAA) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000793 GrDrawTarget::AutoDeviceCoordDraw adcd(target, stageMask);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000794 if (width >= 0) {
795 GrVec strokeSize;;
796 if (width > 0) {
797 strokeSize.set(width, width);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000798 combinedMatrix.mapVectors(&strokeSize, 1);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000799 strokeSize.setAbs(strokeSize);
800 } else {
801 strokeSize.set(GR_Scalar1, GR_Scalar1);
802 }
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000803 fAARectRenderer->strokeAARect(this->getGpu(), target, devRect,
804 strokeSize, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000805 } else {
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000806 fAARectRenderer->fillAARect(this->getGpu(), target,
807 devRect, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000808 }
809 return;
810 }
811
bsalomon@google.com27847de2011-02-22 20:59:41 +0000812 if (width >= 0) {
813 // TODO: consider making static vertex buffers for these cases.
814 // Hairline could be done by just adding closing vertex to
815 // unitSquareVertexBuffer()
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000816 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL);
817
bsalomon@google.com27847de2011-02-22 20:59:41 +0000818 static const int worstCaseVertCount = 10;
819 GrDrawTarget::AutoReleaseGeometry geo(target, layout, worstCaseVertCount, 0);
820
821 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000822 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000823 return;
824 }
825
826 GrPrimitiveType primType;
827 int vertCount;
828 GrPoint* vertex = geo.positions();
829
830 if (width > 0) {
831 vertCount = 10;
bsalomon@google.com47059542012-06-06 20:51:20 +0000832 primType = kTriangleStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000833 setStrokeRectStrip(vertex, rect, width);
834 } else {
835 // hairline
836 vertCount = 5;
bsalomon@google.com47059542012-06-06 20:51:20 +0000837 primType = kLineStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000838 vertex[0].set(rect.fLeft, rect.fTop);
839 vertex[1].set(rect.fRight, rect.fTop);
840 vertex[2].set(rect.fRight, rect.fBottom);
841 vertex[3].set(rect.fLeft, rect.fBottom);
842 vertex[4].set(rect.fLeft, rect.fTop);
843 }
844
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000845 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000846 if (NULL != matrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000847 GrDrawState* drawState = target->drawState();
848 avmr.set(drawState);
849 drawState->preConcatViewMatrix(*matrix);
850 drawState->preConcatSamplerMatrices(stageMask, *matrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000851 }
852
853 target->drawNonIndexed(primType, 0, vertCount);
854 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000855#if GR_STATIC_RECT_VB
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000856 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL);
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000857 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
858 if (NULL == sqVB) {
859 GrPrintf("Failed to create static rect vb.\n");
860 return;
861 }
862 target->setVertexSourceToBuffer(layout, sqVB);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000863 GrDrawState* drawState = target->drawState();
864 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000865 GrMatrix m;
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000866 m.setAll(rect.width(), 0, rect.fLeft,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000867 0, rect.height(), rect.fTop,
868 0, 0, GrMatrix::I()[8]);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000869
870 if (NULL != matrix) {
871 m.postConcat(*matrix);
872 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000873 drawState->preConcatViewMatrix(m);
874 drawState->preConcatSamplerMatrices(stageMask, m);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000875
bsalomon@google.com47059542012-06-06 20:51:20 +0000876 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000877#else
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000878 target->drawSimpleRect(rect, matrix, stageMask);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000879#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +0000880 }
881}
882
883void GrContext::drawRectToRect(const GrPaint& paint,
884 const GrRect& dstRect,
885 const GrRect& srcRect,
886 const GrMatrix* dstMatrix,
887 const GrMatrix* srcMatrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000888 SK_TRACE_EVENT0("GrContext::drawRectToRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000889
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000890 // srcRect refers to paint's first texture
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000891 if (!paint.isTextureStageEnabled(0)) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000892 drawRect(paint, dstRect, -1, dstMatrix);
893 return;
894 }
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000895
bsalomon@google.com27847de2011-02-22 20:59:41 +0000896 GR_STATIC_ASSERT(!BATCH_RECT_TO_RECT || !GR_STATIC_RECT_VB);
897
898#if GR_STATIC_RECT_VB
899 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000900 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000901 GrDrawState* drawState = target->drawState();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000902 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000903 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000904
905 GrMatrix m;
906
907 m.setAll(dstRect.width(), 0, dstRect.fLeft,
908 0, dstRect.height(), dstRect.fTop,
909 0, 0, GrMatrix::I()[8]);
910 if (NULL != dstMatrix) {
911 m.postConcat(*dstMatrix);
912 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000913 drawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000914
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000915 // srcRect refers to first stage
916 int otherStageMask = paint.getActiveStageMask() &
917 (~(1 << GrPaint::kFirstTextureStage));
918 if (otherStageMask) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000919 drawState->preConcatSamplerMatrices(otherStageMask, m);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000920 }
921
bsalomon@google.com27847de2011-02-22 20:59:41 +0000922 m.setAll(srcRect.width(), 0, srcRect.fLeft,
923 0, srcRect.height(), srcRect.fTop,
924 0, 0, GrMatrix::I()[8]);
925 if (NULL != srcMatrix) {
926 m.postConcat(*srcMatrix);
927 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000928 drawState->sampler(GrPaint::kFirstTextureStage)->preConcatMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000929
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000930 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
931 if (NULL == sqVB) {
932 GrPrintf("Failed to create static rect vb.\n");
933 return;
934 }
935 target->setVertexSourceToBuffer(layout, sqVB);
bsalomon@google.com47059542012-06-06 20:51:20 +0000936 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000937#else
938
939 GrDrawTarget* target;
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000940#if BATCH_RECT_TO_RECT
bsalomon@google.com27847de2011-02-22 20:59:41 +0000941 target = this->prepareToDraw(paint, kBuffered_DrawCategory);
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000942#else
bsalomon@google.com27847de2011-02-22 20:59:41 +0000943 target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
944#endif
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000945 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000946
tomhudson@google.com93813632011-10-27 20:21:16 +0000947 const GrRect* srcRects[GrDrawState::kNumStages] = {NULL};
948 const GrMatrix* srcMatrices[GrDrawState::kNumStages] = {NULL};
bsalomon@google.com27847de2011-02-22 20:59:41 +0000949 srcRects[0] = &srcRect;
950 srcMatrices[0] = srcMatrix;
951
952 target->drawRect(dstRect, dstMatrix, 1, srcRects, srcMatrices);
953#endif
954}
955
956void GrContext::drawVertices(const GrPaint& paint,
957 GrPrimitiveType primitiveType,
958 int vertexCount,
959 const GrPoint positions[],
960 const GrPoint texCoords[],
961 const GrColor colors[],
962 const uint16_t indices[],
963 int indexCount) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000964 SK_TRACE_EVENT0("GrContext::drawVertices");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000965
966 GrDrawTarget::AutoReleaseGeometry geo;
967
968 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000969 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000970
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000971 bool hasTexCoords[GrPaint::kTotalStages] = {
972 NULL != texCoords, // texCoordSrc provides explicit stage 0 coords
973 0 // remaining stages use positions
974 };
975
976 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, hasTexCoords);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000977
978 if (NULL != colors) {
979 layout |= GrDrawTarget::kColor_VertexLayoutBit;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000980 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000981 int vertexSize = GrDrawTarget::VertexSize(layout);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000982
983 if (sizeof(GrPoint) != vertexSize) {
984 if (!geo.set(target, layout, vertexCount, 0)) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000985 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000986 return;
987 }
tomhudson@google.com93813632011-10-27 20:21:16 +0000988 int texOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000989 int colorOffset;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000990 GrDrawTarget::VertexSizeAndOffsetsByIdx(layout,
991 texOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000992 &colorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000993 NULL,
994 NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000995 void* curVertex = geo.vertices();
996
997 for (int i = 0; i < vertexCount; ++i) {
998 *((GrPoint*)curVertex) = positions[i];
999
1000 if (texOffsets[0] > 0) {
1001 *(GrPoint*)((intptr_t)curVertex + texOffsets[0]) = texCoords[i];
1002 }
1003 if (colorOffset > 0) {
1004 *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i];
1005 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001006 curVertex = (void*)((intptr_t)curVertex + vertexSize);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001007 }
1008 } else {
1009 target->setVertexSourceToArray(layout, positions, vertexCount);
1010 }
1011
bsalomon@google.com91958362011-06-13 17:58:13 +00001012 // we don't currently apply offscreen AA to this path. Need improved
1013 // management of GrDrawTarget's geometry to avoid copying points per-tile.
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001014
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001015 if (NULL != indices) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001016 target->setIndexSourceToArray(indices, indexCount);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001017 target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001018 } else {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001019 target->drawNonIndexed(primitiveType, 0, vertexCount);
1020 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001021}
1022
bsalomon@google.com06afe7b2011-04-26 15:31:40 +00001023///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com150d2842012-01-12 20:19:56 +00001024namespace {
1025
bsalomon@google.com93c96602012-04-27 13:05:21 +00001026struct CircleVertex {
1027 GrPoint fPos;
1028 GrPoint fCenter;
1029 GrScalar fOuterRadius;
1030 GrScalar fInnerRadius;
1031};
1032
1033/* Returns true if will map a circle to another circle. This can be true
1034 * if the matrix only includes square-scale, rotation, translation.
1035 */
1036inline bool isSimilarityTransformation(const SkMatrix& matrix,
1037 SkScalar tol = SK_ScalarNearlyZero) {
1038 if (matrix.isIdentity() || matrix.getType() == SkMatrix::kTranslate_Mask) {
1039 return true;
1040 }
1041 if (matrix.hasPerspective()) {
1042 return false;
1043 }
1044
1045 SkScalar mx = matrix.get(SkMatrix::kMScaleX);
1046 SkScalar sx = matrix.get(SkMatrix::kMSkewX);
1047 SkScalar my = matrix.get(SkMatrix::kMScaleY);
1048 SkScalar sy = matrix.get(SkMatrix::kMSkewY);
1049
1050 if (mx == 0 && sx == 0 && my == 0 && sy == 0) {
1051 return false;
1052 }
1053
1054 // it has scales or skews, but it could also be rotation, check it out.
1055 SkVector vec[2];
1056 vec[0].set(mx, sx);
1057 vec[1].set(sy, my);
1058
1059 return SkScalarNearlyZero(vec[0].dot(vec[1]), SkScalarSquare(tol)) &&
1060 SkScalarNearlyEqual(vec[0].lengthSqd(), vec[1].lengthSqd(),
1061 SkScalarSquare(tol));
1062}
1063
1064}
1065
1066// TODO: strokeWidth can't be larger than zero right now.
1067// It will be fixed when drawPath() can handle strokes.
1068void GrContext::drawOval(const GrPaint& paint,
1069 const GrRect& rect,
1070 SkScalar strokeWidth) {
1071 DrawCategory category = (DEFER_PATHS) ? kBuffered_DrawCategory :
1072 kUnbuffered_DrawCategory;
1073 GrDrawTarget* target = this->prepareToDraw(paint, category);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +00001074 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001075 GrDrawState* drawState = target->drawState();
1076 GrMatrix vm = drawState->getViewMatrix();
1077
1078 if (!isSimilarityTransformation(vm) ||
1079 !paint.fAntiAlias ||
1080 rect.height() != rect.width()) {
1081 SkPath path;
1082 path.addOval(rect);
1083 GrPathFill fill = (strokeWidth == 0) ?
bsalomon@google.com47059542012-06-06 20:51:20 +00001084 kHairLine_GrPathFill : kWinding_GrPathFill;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001085 this->internalDrawPath(paint, path, fill, NULL);
1086 return;
1087 }
1088
1089 const GrRenderTarget* rt = drawState->getRenderTarget();
1090 if (NULL == rt) {
1091 return;
1092 }
1093
1094 GrDrawTarget::AutoDeviceCoordDraw adcd(target, paint.getActiveStageMask());
1095
1096 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL);
1097 layout |= GrDrawTarget::kEdge_VertexLayoutBit;
1098 GrAssert(sizeof(CircleVertex) == GrDrawTarget::VertexSize(layout));
1099
1100 GrPoint center = GrPoint::Make(rect.centerX(), rect.centerY());
1101 GrScalar radius = SkScalarHalf(rect.width());
1102
1103 vm.mapPoints(&center, 1);
1104 radius = vm.mapRadius(radius);
1105
1106 GrScalar outerRadius = radius;
1107 GrScalar innerRadius = 0;
1108 SkScalar halfWidth = 0;
1109 if (strokeWidth == 0) {
1110 halfWidth = SkScalarHalf(SK_Scalar1);
1111
1112 outerRadius += halfWidth;
1113 innerRadius = SkMaxScalar(0, radius - halfWidth);
1114 }
1115
1116 GrDrawTarget::AutoReleaseGeometry geo(target, layout, 4, 0);
1117 if (!geo.succeeded()) {
1118 GrPrintf("Failed to get space for vertices!\n");
1119 return;
1120 }
1121
1122 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
1123
robertphillips@google.coma0a66c12012-06-22 13:14:29 +00001124 // The fragment shader will extend the radius out half a pixel
1125 // to antialias. Expand the drawn rect here so all the pixels
1126 // will be captured.
1127 SkScalar L = center.fX - outerRadius - SkFloatToScalar(0.5f);
1128 SkScalar R = center.fX + outerRadius + SkFloatToScalar(0.5f);
1129 SkScalar T = center.fY - outerRadius - SkFloatToScalar(0.5f);
1130 SkScalar B = center.fY + outerRadius + SkFloatToScalar(0.5f);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001131
1132 verts[0].fPos = SkPoint::Make(L, T);
1133 verts[1].fPos = SkPoint::Make(R, T);
1134 verts[2].fPos = SkPoint::Make(L, B);
1135 verts[3].fPos = SkPoint::Make(R, B);
1136
1137 for (int i = 0; i < 4; ++i) {
1138 // this goes to fragment shader, it should be in y-points-up space.
1139 verts[i].fCenter = SkPoint::Make(center.fX, rt->height() - center.fY);
1140
1141 verts[i].fOuterRadius = outerRadius;
1142 verts[i].fInnerRadius = innerRadius;
1143 }
1144
1145 drawState->setVertexEdgeType(GrDrawState::kCircle_EdgeType);
bsalomon@google.com47059542012-06-06 20:51:20 +00001146 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4);
bsalomon@google.com150d2842012-01-12 20:19:56 +00001147}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001148
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001149void GrContext::drawPath(const GrPaint& paint, const SkPath& path,
reed@google.com07f3ee12011-05-16 17:21:57 +00001150 GrPathFill fill, const GrPoint* translate) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001151
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001152 if (path.isEmpty()) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001153 if (GrIsFillInverted(fill)) {
1154 this->drawPaint(paint);
1155 }
1156 return;
1157 }
1158
bsalomon@google.com93c96602012-04-27 13:05:21 +00001159 SkRect ovalRect;
1160 if (!GrIsFillInverted(fill) && path.isOval(&ovalRect)) {
1161 if (translate) {
1162 ovalRect.offset(*translate);
1163 }
bsalomon@google.com47059542012-06-06 20:51:20 +00001164 SkScalar width = (fill == kHairLine_GrPathFill) ? 0 : -SK_Scalar1;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001165 this->drawOval(paint, ovalRect, width);
1166 return;
1167 }
1168
1169 internalDrawPath(paint, path, fill, translate);
1170}
1171
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001172void GrContext::internalDrawPath(const GrPaint& paint, const SkPath& path,
bsalomon@google.com93c96602012-04-27 13:05:21 +00001173 GrPathFill fill, const GrPoint* translate) {
1174
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001175 // Note that below we may sw-rasterize the path into a scratch texture.
1176 // Scratch textures can be recycled after they are returned to the texture
1177 // cache. This presents a potential hazard for buffered drawing. However,
1178 // the writePixels that uploads to the scratch will perform a flush so we're
1179 // OK.
1180 DrawCategory category = (DEFER_PATHS) ? kBuffered_DrawCategory :
1181 kUnbuffered_DrawCategory;
1182 GrDrawTarget* target = this->prepareToDraw(paint, category);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +00001183 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com150d2842012-01-12 20:19:56 +00001184 GrDrawState::StageMask stageMask = paint.getActiveStageMask();
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001185
bsalomon@google.com289533a2011-10-27 12:34:25 +00001186 bool prAA = paint.fAntiAlias && !this->getRenderTarget()->isMultisampled();
1187
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001188 // An Assumption here is that path renderer would use some form of tweaking
1189 // the src color (either the input alpha or in the frag shader) to implement
1190 // aa. If we have some future driver-mojo path AA that can do the right
1191 // thing WRT to the blend then we'll need some query on the PR.
1192 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001193#if GR_DEBUG
bsalomon@google.com979432b2011-11-05 21:38:22 +00001194 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001195#endif
bsalomon@google.com289533a2011-10-27 12:34:25 +00001196 prAA = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001197 }
bsalomon@google.com289533a2011-10-27 12:34:25 +00001198
robertphillips@google.com72176b22012-05-23 13:19:12 +00001199 GrPathRenderer* pr = this->getPathRenderer(path, fill, target, prAA, true);
bsalomon@google.com30085192011-08-19 15:42:31 +00001200 if (NULL == pr) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001201#if GR_DEBUG
bsalomon@google.com30085192011-08-19 15:42:31 +00001202 GrPrintf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001203#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001204 return;
1205 }
1206
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001207 pr->drawPath(path, fill, translate, target, stageMask, prAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001208}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001209
bsalomon@google.com27847de2011-02-22 20:59:41 +00001210////////////////////////////////////////////////////////////////////////////////
1211
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001212void GrContext::flush(int flagsBitfield) {
1213 if (kDiscard_FlushBit & flagsBitfield) {
1214 fDrawBuffer->reset();
1215 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001216 this->flushDrawBuffer();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001217 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001218 if (kForceCurrentRenderTarget_FlushBit & flagsBitfield) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001219 fGpu->forceRenderTargetFlush();
1220 }
1221}
1222
bsalomon@google.com27847de2011-02-22 20:59:41 +00001223void GrContext::flushDrawBuffer() {
junov@google.com53a55842011-06-08 22:55:10 +00001224 if (fDrawBuffer) {
robertphillips@google.com58b38182012-05-03 16:29:41 +00001225 // With addition of the AA clip path, flushing the draw buffer can
1226 // result in the generation of an AA clip mask. During this
1227 // process the SW path renderer may be invoked which recusively
1228 // calls this method (via internalWriteTexturePixels) creating
1229 // infinite recursion
1230 GrInOrderDrawBuffer* temp = fDrawBuffer;
1231 fDrawBuffer = NULL;
1232
1233 temp->flushTo(fGpu);
1234
1235 fDrawBuffer = temp;
junov@google.com53a55842011-06-08 22:55:10 +00001236 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001237}
1238
bsalomon@google.com6f379512011-11-16 20:36:03 +00001239void GrContext::internalWriteTexturePixels(GrTexture* texture,
1240 int left, int top,
1241 int width, int height,
1242 GrPixelConfig config,
1243 const void* buffer,
1244 size_t rowBytes,
1245 uint32_t flags) {
1246 SK_TRACE_EVENT0("GrContext::writeTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001247 ASSERT_OWNED_RESOURCE(texture);
1248
bsalomon@google.com6f379512011-11-16 20:36:03 +00001249 if (!(kDontFlush_PixelOpsFlag & flags)) {
1250 this->flush();
1251 }
1252 // TODO: use scratch texture to perform conversion
1253 if (GrPixelConfigIsUnpremultiplied(texture->config()) !=
1254 GrPixelConfigIsUnpremultiplied(config)) {
1255 return;
1256 }
1257
1258 fGpu->writeTexturePixels(texture, left, top, width, height,
1259 config, buffer, rowBytes);
1260}
1261
1262bool GrContext::internalReadTexturePixels(GrTexture* texture,
1263 int left, int top,
1264 int width, int height,
1265 GrPixelConfig config,
1266 void* buffer,
1267 size_t rowBytes,
1268 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001269 SK_TRACE_EVENT0("GrContext::readTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001270 ASSERT_OWNED_RESOURCE(texture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001271
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001272 // TODO: code read pixels for textures that aren't also rendertargets
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001273 GrRenderTarget* target = texture->asRenderTarget();
1274 if (NULL != target) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001275 return this->internalReadRenderTargetPixels(target,
1276 left, top, width, height,
1277 config, buffer, rowBytes,
1278 flags);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001279 } else {
1280 return false;
1281 }
1282}
1283
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001284#include "SkConfig8888.h"
1285
1286namespace {
1287/**
1288 * Converts a GrPixelConfig to a SkCanvas::Config8888. Only byte-per-channel
1289 * formats are representable as Config8888 and so the function returns false
1290 * if the GrPixelConfig has no equivalent Config8888.
1291 */
1292bool grconfig_to_config8888(GrPixelConfig config,
1293 SkCanvas::Config8888* config8888) {
1294 switch (config) {
1295 case kRGBA_8888_PM_GrPixelConfig:
1296 *config8888 = SkCanvas::kRGBA_Premul_Config8888;
1297 return true;
1298 case kRGBA_8888_UPM_GrPixelConfig:
1299 *config8888 = SkCanvas::kRGBA_Unpremul_Config8888;
1300 return true;
1301 case kBGRA_8888_PM_GrPixelConfig:
1302 *config8888 = SkCanvas::kBGRA_Premul_Config8888;
1303 return true;
1304 case kBGRA_8888_UPM_GrPixelConfig:
1305 *config8888 = SkCanvas::kBGRA_Unpremul_Config8888;
1306 return true;
1307 default:
1308 return false;
1309 }
1310}
1311}
1312
bsalomon@google.com6f379512011-11-16 20:36:03 +00001313bool GrContext::internalReadRenderTargetPixels(GrRenderTarget* target,
1314 int left, int top,
1315 int width, int height,
1316 GrPixelConfig config,
1317 void* buffer,
1318 size_t rowBytes,
1319 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001320 SK_TRACE_EVENT0("GrContext::readRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001321 ASSERT_OWNED_RESOURCE(target);
1322
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001323 if (NULL == target) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001324 target = fDrawState->getRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001325 if (NULL == target) {
1326 return false;
1327 }
1328 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001329
bsalomon@google.com6f379512011-11-16 20:36:03 +00001330 if (!(kDontFlush_PixelOpsFlag & flags)) {
1331 this->flush();
1332 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001333
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001334 if (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1335 GrPixelConfigIsUnpremultiplied(config) &&
1336 !fGpu->canPreserveReadWriteUnpremulPixels()) {
1337 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1338 if (!grconfig_to_config8888(target->config(), &srcConfig8888) ||
1339 !grconfig_to_config8888(config, &dstConfig8888)) {
1340 return false;
1341 }
1342 // do read back using target's own config
1343 this->internalReadRenderTargetPixels(target,
1344 left, top,
1345 width, height,
1346 target->config(),
1347 buffer, rowBytes,
1348 kDontFlush_PixelOpsFlag);
1349 // sw convert the pixels to unpremul config
1350 uint32_t* pixels = reinterpret_cast<uint32_t*>(buffer);
1351 SkConvertConfig8888Pixels(pixels, rowBytes, dstConfig8888,
1352 pixels, rowBytes, srcConfig8888,
1353 width, height);
1354 return true;
1355 }
1356
bsalomon@google.comc4364992011-11-07 15:54:49 +00001357 GrTexture* src = target->asTexture();
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001358 bool swapRAndB = NULL != src &&
1359 fGpu->preferredReadPixelsConfig(config) ==
1360 GrPixelConfigSwapRAndB(config);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001361
1362 bool flipY = NULL != src &&
1363 fGpu->readPixelsWillPayForYFlip(target, left, top,
1364 width, height, config,
1365 rowBytes);
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001366 bool alphaConversion = (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1367 GrPixelConfigIsUnpremultiplied(config));
bsalomon@google.comc4364992011-11-07 15:54:49 +00001368
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001369 if (NULL == src && alphaConversion) {
1370 // we should fallback to cpu conversion here. This could happen when
1371 // we were given an external render target by the client that is not
1372 // also a texture (e.g. FBO 0 in GL)
1373 return false;
1374 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001375 // we draw to a scratch texture if any of these conversion are applied
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001376 GrAutoScratchTexture ast;
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001377 if (flipY || swapRAndB || alphaConversion) {
1378 GrAssert(NULL != src);
1379 if (swapRAndB) {
1380 config = GrPixelConfigSwapRAndB(config);
1381 GrAssert(kUnknown_GrPixelConfig != config);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001382 }
1383 // Make the scratch a render target because we don't have a robust
1384 // readTexturePixels as of yet (it calls this function).
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001385 GrTextureDesc desc;
1386 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1387 desc.fWidth = width;
1388 desc.fHeight = height;
1389 desc.fConfig = config;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001390
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001391 // When a full readback is faster than a partial we could always make
1392 // the scratch exactly match the passed rect. However, if we see many
1393 // different size rectangles we will trash our texture cache and pay the
1394 // cost of creating and destroying many textures. So, we only request
1395 // an exact match when the caller is reading an entire RT.
1396 ScratchTexMatch match = kApprox_ScratchTexMatch;
1397 if (0 == left &&
1398 0 == top &&
1399 target->width() == width &&
1400 target->height() == height &&
1401 fGpu->fullReadPixelsIsFasterThanPartial()) {
1402 match = kExact_ScratchTexMatch;
1403 }
1404 ast.set(this, desc, match);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001405 GrTexture* texture = ast.texture();
1406 if (!texture) {
1407 return false;
1408 }
1409 target = texture->asRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001410 GrAssert(NULL != target);
1411
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001412 GrDrawTarget::AutoStateRestore asr(fGpu,
1413 GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001414 GrDrawState* drawState = fGpu->drawState();
1415 drawState->setRenderTarget(target);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001416
bsalomon@google.comc4364992011-11-07 15:54:49 +00001417 GrMatrix matrix;
1418 if (flipY) {
1419 matrix.setTranslate(SK_Scalar1 * left,
1420 SK_Scalar1 * (top + height));
1421 matrix.set(GrMatrix::kMScaleY, -GR_Scalar1);
1422 } else {
1423 matrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
1424 }
1425 matrix.postIDiv(src->width(), src->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001426 drawState->sampler(0)->reset(matrix);
1427 drawState->sampler(0)->setRAndBSwap(swapRAndB);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001428 drawState->setTexture(0, src);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001429 GrRect rect;
1430 rect.setXYWH(0, 0, SK_Scalar1 * width, SK_Scalar1 * height);
1431 fGpu->drawSimpleRect(rect, NULL, 0x1);
1432 left = 0;
1433 top = 0;
1434 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001435 return fGpu->readPixels(target,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001436 left, top, width, height,
1437 config, buffer, rowBytes, flipY);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001438}
1439
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001440void GrContext::resolveRenderTarget(GrRenderTarget* target) {
1441 GrAssert(target);
1442 ASSERT_OWNED_RESOURCE(target);
1443 // In the future we may track whether there are any pending draws to this
1444 // target. We don't today so we always perform a flush. We don't promise
1445 // this to our clients, though.
1446 this->flush();
1447 fGpu->resolveRenderTarget(target);
1448}
1449
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001450void GrContext::copyTexture(GrTexture* src, GrRenderTarget* dst) {
1451 if (NULL == src || NULL == dst) {
1452 return;
1453 }
1454 ASSERT_OWNED_RESOURCE(src);
1455
twiz@google.com1ac87ff2012-04-27 19:39:33 +00001456 // Writes pending to the source texture are not tracked, so a flush
1457 // is required to ensure that the copy captures the most recent contents
1458 // of the source texture. See similar behaviour in
1459 // GrContext::resolveRenderTarget.
1460 this->flush();
1461
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001462 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001463 GrDrawState* drawState = fGpu->drawState();
1464 drawState->setRenderTarget(dst);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001465 GrMatrix sampleM;
1466 sampleM.setIDiv(src->width(), src->height());
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001467 drawState->setTexture(0, src);
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001468 drawState->sampler(0)->reset(sampleM);
bsalomon@google.com5db3b6c2012-01-12 20:38:57 +00001469 SkRect rect = SkRect::MakeXYWH(0, 0,
1470 SK_Scalar1 * src->width(),
1471 SK_Scalar1 * src->height());
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001472 fGpu->drawSimpleRect(rect, NULL, 1 << 0);
1473}
1474
bsalomon@google.com6f379512011-11-16 20:36:03 +00001475void GrContext::internalWriteRenderTargetPixels(GrRenderTarget* target,
1476 int left, int top,
1477 int width, int height,
1478 GrPixelConfig config,
1479 const void* buffer,
1480 size_t rowBytes,
1481 uint32_t flags) {
1482 SK_TRACE_EVENT0("GrContext::writeRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001483 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com6f379512011-11-16 20:36:03 +00001484
1485 if (NULL == target) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001486 target = fDrawState->getRenderTarget();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001487 if (NULL == target) {
1488 return;
1489 }
1490 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001491
1492 // TODO: when underlying api has a direct way to do this we should use it
1493 // (e.g. glDrawPixels on desktop GL).
1494
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001495 // If the RT is also a texture and we don't have to do PM/UPM conversion
1496 // then take the texture path, which we expect to be at least as fast or
1497 // faster since it doesn't use an intermediate texture as we do below.
1498
1499#if !GR_MAC_BUILD
1500 // At least some drivers on the Mac get confused when glTexImage2D is called
1501 // on a texture attached to an FBO. The FBO still sees the old image. TODO:
1502 // determine what OS versions and/or HW is affected.
1503 if (NULL != target->asTexture() &&
1504 GrPixelConfigIsUnpremultiplied(target->config()) ==
1505 GrPixelConfigIsUnpremultiplied(config)) {
1506
1507 this->internalWriteTexturePixels(target->asTexture(),
1508 left, top, width, height,
1509 config, buffer, rowBytes, flags);
1510 return;
1511 }
1512#endif
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001513 if (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1514 GrPixelConfigIsUnpremultiplied(config) &&
1515 !fGpu->canPreserveReadWriteUnpremulPixels()) {
1516 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1517 if (!grconfig_to_config8888(config, &srcConfig8888) ||
1518 !grconfig_to_config8888(target->config(), &dstConfig8888)) {
1519 return;
1520 }
1521 // allocate a tmp buffer and sw convert the pixels to premul
1522 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(width * height);
1523 const uint32_t* src = reinterpret_cast<const uint32_t*>(buffer);
1524 SkConvertConfig8888Pixels(tmpPixels.get(), 4 * width, dstConfig8888,
1525 src, rowBytes, srcConfig8888,
1526 width, height);
1527 // upload the already premul pixels
1528 this->internalWriteRenderTargetPixels(target,
1529 left, top,
1530 width, height,
1531 target->config(),
1532 tmpPixels, 4 * width, flags);
1533 return;
1534 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001535
1536 bool swapRAndB = fGpu->preferredReadPixelsConfig(config) ==
1537 GrPixelConfigSwapRAndB(config);
1538 if (swapRAndB) {
1539 config = GrPixelConfigSwapRAndB(config);
1540 }
1541
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001542 GrTextureDesc desc;
1543 desc.fWidth = width;
1544 desc.fHeight = height;
1545 desc.fConfig = config;
1546
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001547 GrAutoScratchTexture ast(this, desc);
1548 GrTexture* texture = ast.texture();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001549 if (NULL == texture) {
1550 return;
1551 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001552 this->internalWriteTexturePixels(texture, 0, 0, width, height,
1553 config, buffer, rowBytes, flags);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001554
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001555 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001556 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001557
1558 GrMatrix matrix;
1559 matrix.setTranslate(GrIntToScalar(left), GrIntToScalar(top));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001560 drawState->setViewMatrix(matrix);
1561 drawState->setRenderTarget(target);
1562 drawState->setTexture(0, texture);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001563
bsalomon@google.com5c638652011-07-18 19:31:59 +00001564 matrix.setIDiv(texture->width(), texture->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001565 drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode,
1566 GrSamplerState::kNearest_Filter,
1567 matrix);
1568 drawState->sampler(0)->setRAndBSwap(swapRAndB);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001569
tomhudson@google.comb213ed82012-06-25 15:22:12 +00001570 static const GrVertexLayout layout = 0;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001571 static const int VCOUNT = 4;
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001572 // TODO: Use GrGpu::drawRect here
bsalomon@google.com27847de2011-02-22 20:59:41 +00001573 GrDrawTarget::AutoReleaseGeometry geo(fGpu, layout, VCOUNT, 0);
1574 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001575 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +00001576 return;
1577 }
1578 ((GrPoint*)geo.vertices())->setIRectFan(0, 0, width, height);
bsalomon@google.com47059542012-06-06 20:51:20 +00001579 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, VCOUNT);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001580}
1581////////////////////////////////////////////////////////////////////////////////
1582
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001583void GrContext::setPaint(const GrPaint& paint) {
tomhudson@google.comcb325ce2012-07-11 14:41:19 +00001584 GrAssert(fDrawState->stagesDisabled());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001585
1586 for (int i = 0; i < GrPaint::kMaxTextures; ++i) {
1587 int s = i + GrPaint::kFirstTextureStage;
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001588 ASSERT_OWNED_RESOURCE(paint.getTexture(i));
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001589 if (paint.isTextureStageEnabled(i)) {
1590 fDrawState->setTexture(s, paint.getTexture(i));
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001591 *fDrawState->sampler(s) = paint.getTextureSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001592 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001593 }
1594
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001595 fDrawState->setFirstCoverageStage(GrPaint::kFirstMaskStage);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001596
1597 for (int i = 0; i < GrPaint::kMaxMasks; ++i) {
1598 int s = i + GrPaint::kFirstMaskStage;
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001599 ASSERT_OWNED_RESOURCE(paint.getMask(i));
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001600 if (paint.isMaskStageEnabled(i)) {
1601 fDrawState->setTexture(s, paint.getMask(i));
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001602 *fDrawState->sampler(s) = paint.getMaskSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001603 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001604 }
bsalomon@google.com26936d02012-03-19 13:06:19 +00001605
1606 // disable all stages not accessible via the paint
1607 for (int s = GrPaint::kTotalStages; s < GrDrawState::kNumStages; ++s) {
tomhudson@google.com676e6602012-07-10 17:21:48 +00001608 fDrawState->disableStage(s);
bsalomon@google.com26936d02012-03-19 13:06:19 +00001609 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001610
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001611 fDrawState->setColor(paint.fColor);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001612
1613 if (paint.fDither) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001614 fDrawState->enableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001615 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001616 fDrawState->disableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001617 }
1618 if (paint.fAntiAlias) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001619 fDrawState->enableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001620 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001621 fDrawState->disableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001622 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001623 if (paint.fColorMatrixEnabled) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001624 fDrawState->enableState(GrDrawState::kColorMatrix_StateBit);
1625 fDrawState->setColorMatrix(paint.fColorMatrix);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001626 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001627 fDrawState->disableState(GrDrawState::kColorMatrix_StateBit);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001628 }
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001629 fDrawState->setBlendFunc(paint.fSrcBlendCoeff, paint.fDstBlendCoeff);
1630 fDrawState->setColorFilter(paint.fColorFilterColor, paint.fColorFilterXfermode);
1631 fDrawState->setCoverage(paint.fCoverage);
reed@google.com4b2d3f32012-05-15 18:05:50 +00001632#if GR_DEBUG_PARTIAL_COVERAGE_CHECK
bsalomon@google.come79c8152012-03-29 19:07:12 +00001633 if ((paint.getActiveMaskStageMask() || 0xff != paint.fCoverage) &&
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001634 !fGpu->canApplyCoverage()) {
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001635 GrPrintf("Partial pixel coverage will be incorrectly blended.\n");
1636 }
bsalomon@google.com95cd7bd2012-03-28 15:35:05 +00001637#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +00001638}
1639
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001640GrDrawTarget* GrContext::prepareToDraw(const GrPaint& paint,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001641 DrawCategory category) {
1642 if (category != fLastDrawCategory) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001643 this->flushDrawBuffer();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001644 fLastDrawCategory = category;
1645 }
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001646 this->setPaint(paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001647 GrDrawTarget* target = fGpu;
1648 switch (category) {
bsalomon@google.com193395c2012-03-30 17:35:12 +00001649 case kUnbuffered_DrawCategory:
1650 target = fGpu;
1651 break;
1652 case kBuffered_DrawCategory:
1653 target = fDrawBuffer;
1654 fDrawBuffer->setClip(fGpu->getClip());
1655 break;
1656 default:
1657 GrCrash("Unexpected DrawCategory.");
1658 break;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001659 }
1660 return target;
1661}
1662
robertphillips@google.com72176b22012-05-23 13:19:12 +00001663/*
1664 * This method finds a path renderer that can draw the specified path on
1665 * the provided target.
1666 * Due to its expense, the software path renderer has split out so it can
1667 * can be individually allowed/disallowed via the "allowSW" boolean.
1668 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001669GrPathRenderer* GrContext::getPathRenderer(const SkPath& path,
bsalomon@google.com289533a2011-10-27 12:34:25 +00001670 GrPathFill fill,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001671 const GrDrawTarget* target,
robertphillips@google.com72176b22012-05-23 13:19:12 +00001672 bool antiAlias,
1673 bool allowSW) {
bsalomon@google.com30085192011-08-19 15:42:31 +00001674 if (NULL == fPathRendererChain) {
1675 fPathRendererChain =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001676 SkNEW_ARGS(GrPathRendererChain,
1677 (this, GrPathRendererChain::kNone_UsageFlag));
bsalomon@google.com30085192011-08-19 15:42:31 +00001678 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001679
1680 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(path, fill,
1681 target,
1682 antiAlias);
1683
1684 if (NULL == pr && allowSW) {
1685 if (NULL == fSoftwarePathRenderer) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001686 fSoftwarePathRenderer = SkNEW_ARGS(GrSoftwarePathRenderer, (this));
robertphillips@google.com72176b22012-05-23 13:19:12 +00001687 }
1688
1689 pr = fSoftwarePathRenderer;
1690 }
1691
1692 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001693}
1694
bsalomon@google.com27847de2011-02-22 20:59:41 +00001695////////////////////////////////////////////////////////////////////////////////
1696
bsalomon@google.com27847de2011-02-22 20:59:41 +00001697void GrContext::setRenderTarget(GrRenderTarget* target) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001698 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001699 if (fDrawState->getRenderTarget() != target) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001700 this->flush(false);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001701 fDrawState->setRenderTarget(target);
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001702 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001703}
1704
1705GrRenderTarget* GrContext::getRenderTarget() {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001706 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001707}
1708
1709const GrRenderTarget* GrContext::getRenderTarget() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001710 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001711}
1712
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001713bool GrContext::isConfigRenderable(GrPixelConfig config) const {
1714 return fGpu->isConfigRenderable(config);
1715}
1716
bsalomon@google.com27847de2011-02-22 20:59:41 +00001717const GrMatrix& GrContext::getMatrix() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001718 return fDrawState->getViewMatrix();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001719}
1720
1721void GrContext::setMatrix(const GrMatrix& m) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001722 fDrawState->setViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001723}
1724
1725void GrContext::concatMatrix(const GrMatrix& m) const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001726 fDrawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001727}
1728
1729static inline intptr_t setOrClear(intptr_t bits, int shift, intptr_t pred) {
1730 intptr_t mask = 1 << shift;
1731 if (pred) {
1732 bits |= mask;
1733 } else {
1734 bits &= ~mask;
1735 }
1736 return bits;
1737}
1738
bsalomon@google.com583a1e32011-08-17 13:42:46 +00001739GrContext::GrContext(GrGpu* gpu) {
bsalomon@google.comc0af3172012-06-15 14:10:09 +00001740 ++THREAD_INSTANCE_COUNT;
1741
bsalomon@google.com27847de2011-02-22 20:59:41 +00001742 fGpu = gpu;
1743 fGpu->ref();
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001744 fGpu->setContext(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001745
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001746 fDrawState = SkNEW(GrDrawState);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001747 fGpu->setDrawState(fDrawState);
1748
bsalomon@google.com30085192011-08-19 15:42:31 +00001749 fPathRendererChain = NULL;
robertphillips@google.com72176b22012-05-23 13:19:12 +00001750 fSoftwarePathRenderer = NULL;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001751
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001752 fTextureCache = SkNEW_ARGS(GrResourceCache,
1753 (MAX_TEXTURE_CACHE_COUNT,
1754 MAX_TEXTURE_CACHE_BYTES));
1755 fFontCache = SkNEW_ARGS(GrFontCache, (fGpu));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001756
1757 fLastDrawCategory = kUnbuffered_DrawCategory;
1758
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001759 fDrawBuffer = NULL;
1760 fDrawBufferVBAllocPool = NULL;
1761 fDrawBufferIBAllocPool = NULL;
1762
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001763 fAARectRenderer = SkNEW(GrAARectRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001764
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001765 this->setupDrawBuffer();
1766}
1767
1768void GrContext::setupDrawBuffer() {
1769
1770 GrAssert(NULL == fDrawBuffer);
1771 GrAssert(NULL == fDrawBufferVBAllocPool);
1772 GrAssert(NULL == fDrawBufferIBAllocPool);
1773
bsalomon@google.com92edd312012-04-04 21:40:21 +00001774#if DEFER_TEXT_RENDERING || BATCH_RECT_TO_RECT || DEFER_PATHS
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001775 fDrawBufferVBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001776 SkNEW_ARGS(GrVertexBufferAllocPool, (fGpu, false,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001777 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001778 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS));
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001779 fDrawBufferIBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001780 SkNEW_ARGS(GrIndexBufferAllocPool, (fGpu, false,
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001781 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001782 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001783
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001784 fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (fGpu,
bsalomon@google.com471d4712011-08-23 15:45:25 +00001785 fDrawBufferVBAllocPool,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001786 fDrawBufferIBAllocPool));
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001787#endif
1788
1789#if BATCH_RECT_TO_RECT
bsalomon@google.com27847de2011-02-22 20:59:41 +00001790 fDrawBuffer->setQuadIndexBuffer(this->getQuadIndexBuffer());
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001791#endif
bsalomon@google.com1015e032012-06-25 18:41:04 +00001792 if (fDrawBuffer) {
1793 fDrawBuffer->setAutoFlushTarget(fGpu);
1794 fDrawBuffer->setDrawState(fDrawState);
1795 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001796}
1797
bsalomon@google.com27847de2011-02-22 20:59:41 +00001798GrDrawTarget* GrContext::getTextTarget(const GrPaint& paint) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001799#if DEFER_TEXT_RENDERING
bsalomon@google.com193395c2012-03-30 17:35:12 +00001800 return prepareToDraw(paint, kBuffered_DrawCategory);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001801#else
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001802 return prepareToDraw(paint, kUnbuffered_DrawCategory);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001803#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +00001804}
1805
1806const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {
1807 return fGpu->getQuadIndexBuffer();
1808}
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001809
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001810GrTexture* GrContext::gaussianBlur(GrTexture* srcTexture,
1811 GrAutoScratchTexture* temp1,
1812 GrAutoScratchTexture* temp2,
1813 const SkRect& rect,
1814 float sigmaX, float sigmaY) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001815 ASSERT_OWNED_RESOURCE(srcTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001816 GrRenderTarget* oldRenderTarget = this->getRenderTarget();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001817 GrTexture* origTexture = srcTexture;
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001818 AutoMatrix avm(this, GrMatrix::I());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001819 SkIRect clearRect;
bsalomon@google.comb505a122012-05-31 18:40:36 +00001820 int scaleFactorX, radiusX;
1821 int scaleFactorY, radiusY;
1822 sigmaX = adjust_sigma(sigmaX, &scaleFactorX, &radiusX);
1823 sigmaY = adjust_sigma(sigmaY, &scaleFactorY, &radiusY);
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001824
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001825 SkRect srcRect(rect);
1826 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
1827 srcRect.roundOut();
robertphillips@google.com8637a362012-04-10 18:32:35 +00001828 scale_rect(&srcRect, static_cast<float>(scaleFactorX),
1829 static_cast<float>(scaleFactorY));
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +00001830
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001831 AutoClip acs(this, srcRect);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001832
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001833 GrAssert(kBGRA_8888_PM_GrPixelConfig == srcTexture->config() ||
1834 kRGBA_8888_PM_GrPixelConfig == srcTexture->config() ||
1835 kAlpha_8_GrPixelConfig == srcTexture->config());
1836
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001837 GrTextureDesc desc;
1838 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
1839 desc.fWidth = SkScalarFloorToInt(srcRect.width());
1840 desc.fHeight = SkScalarFloorToInt(srcRect.height());
1841 desc.fConfig = srcTexture->config();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001842
1843 temp1->set(this, desc);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001844 if (temp2) {
1845 temp2->set(this, desc);
1846 }
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001847
1848 GrTexture* dstTexture = temp1->texture();
1849 GrPaint paint;
1850 paint.reset();
1851 paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
1852
1853 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
1854 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1855 srcTexture->height());
1856 this->setRenderTarget(dstTexture->asRenderTarget());
1857 SkRect dstRect(srcRect);
1858 scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
1859 i < scaleFactorY ? 0.5f : 1.0f);
1860 paint.setTexture(0, srcTexture);
1861 this->drawRectToRect(paint, dstRect, srcRect);
1862 srcRect = dstRect;
1863 SkTSwap(srcTexture, dstTexture);
1864 // If temp2 is non-NULL, don't render back to origTexture
robertphillips@google.com972265d2012-06-13 18:49:30 +00001865 if (temp2 && dstTexture == origTexture) {
1866 dstTexture = temp2->texture();
1867 }
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001868 }
1869
robertphillips@google.com7a396332012-05-10 15:11:27 +00001870 SkIRect srcIRect;
1871 srcRect.roundOut(&srcIRect);
1872
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001873 if (sigmaX > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001874 if (scaleFactorX > 1) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001875 // Clear out a radius to the right of the srcRect to prevent the
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001876 // X convolution from reading garbage.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001877 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001878 radiusX, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001879 this->clear(&clearRect, 0x0);
1880 }
1881
1882 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001883 convolve_gaussian(fGpu, srcTexture, srcRect, sigmaX, radiusX,
1884 Gr1DKernelEffect::kX_Direction);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001885 SkTSwap(srcTexture, dstTexture);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001886 if (temp2 && dstTexture == origTexture) {
1887 dstTexture = temp2->texture();
1888 }
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001889 }
1890
1891 if (sigmaY > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001892 if (scaleFactorY > 1 || sigmaX > 0.0f) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001893 // Clear out a radius below the srcRect to prevent the Y
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001894 // convolution from reading garbage.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001895 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001896 srcIRect.width(), radiusY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001897 this->clear(&clearRect, 0x0);
1898 }
1899
1900 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001901 convolve_gaussian(fGpu, srcTexture, srcRect, sigmaY, radiusY,
1902 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001903 SkTSwap(srcTexture, dstTexture);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001904 if (temp2 && dstTexture == origTexture) {
1905 dstTexture = temp2->texture();
1906 }
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001907 }
1908
1909 if (scaleFactorX > 1 || scaleFactorY > 1) {
1910 // Clear one pixel to the right and below, to accommodate bilinear
1911 // upsampling.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001912 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
1913 srcIRect.width() + 1, 1);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001914 this->clear(&clearRect, 0x0);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001915 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
1916 1, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001917 this->clear(&clearRect, 0x0);
1918 // FIXME: This should be mitchell, not bilinear.
1919 paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
1920 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1921 srcTexture->height());
1922 this->setRenderTarget(dstTexture->asRenderTarget());
1923 paint.setTexture(0, srcTexture);
1924 SkRect dstRect(srcRect);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001925 scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001926 this->drawRectToRect(paint, dstRect, srcRect);
1927 srcRect = dstRect;
1928 SkTSwap(srcTexture, dstTexture);
1929 }
1930 this->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001931 return srcTexture;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001932}
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001933
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001934GrTexture* GrContext::applyMorphology(GrTexture* srcTexture,
1935 const GrRect& rect,
1936 GrTexture* temp1, GrTexture* temp2,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001937 MorphologyType morphType,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001938 SkISize radius) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001939 ASSERT_OWNED_RESOURCE(srcTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001940 GrRenderTarget* oldRenderTarget = this->getRenderTarget();
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +00001941
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001942 AutoMatrix avm(this, GrMatrix::I());
1943
1944 AutoClip acs(this, GrRect::MakeWH(SkIntToScalar(srcTexture->width()),
1945 SkIntToScalar(srcTexture->height())));
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +00001946
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001947 if (radius.fWidth > 0) {
1948 this->setRenderTarget(temp1->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001949 apply_morphology(fGpu, srcTexture, rect, radius.fWidth, morphType,
1950 Gr1DKernelEffect::kX_Direction);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001951 SkIRect clearRect = SkIRect::MakeXYWH(
1952 SkScalarFloorToInt(rect.fLeft),
1953 SkScalarFloorToInt(rect.fBottom),
1954 SkScalarFloorToInt(rect.width()),
1955 radius.fHeight);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001956 this->clear(&clearRect, 0x0);
1957 srcTexture = temp1;
1958 }
1959 if (radius.fHeight > 0) {
1960 this->setRenderTarget(temp2->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001961 apply_morphology(fGpu, srcTexture, rect, radius.fHeight, morphType,
1962 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001963 srcTexture = temp2;
1964 }
1965 this->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001966 return srcTexture;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001967}
bsalomon@google.comc4364992011-11-07 15:54:49 +00001968
1969///////////////////////////////////////////////////////////////////////////////