blob: 7321ffb61f5540e9e6598a8969baf650570a3be2 [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"
tomhudson@google.comaa72eab2012-07-19 18:01:07 +000014#include "effects/GrSingleTextureEffect.h"
bsalomon@google.comb505a122012-05-31 18:40:36 +000015
tomhudson@google.com278cbb42011-06-30 19:37:01 +000016#include "GrBufferAllocPool.h"
17#include "GrClipIterator.h"
bsalomon@google.com05ef5102011-05-02 21:14:59 +000018#include "GrGpu.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000019#include "GrIndexBuffer.h"
20#include "GrInOrderDrawBuffer.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000021#include "GrPathRenderer.h"
tomhudson@google.comd22b6e42011-06-24 15:53:40 +000022#include "GrPathUtils.h"
bsalomon@google.com50398bf2011-07-26 20:45:30 +000023#include "GrResourceCache.h"
robertphillips@google.com72176b22012-05-23 13:19:12 +000024#include "GrSoftwarePathRenderer.h"
bsalomon@google.com558a75b2011-08-08 17:01:14 +000025#include "GrStencilBuffer.h"
tomhudson@google.com278cbb42011-06-30 19:37:01 +000026#include "GrTextStrike.h"
bsalomon@google.com8c2fe992011-09-13 15:27:18 +000027#include "SkTLazy.h"
bsalomon@google.comc0af3172012-06-15 14:10:09 +000028#include "SkTLS.h"
tomhudson@google.com0c8d93a2011-07-01 17:08:26 +000029#include "SkTrace.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000030
reed@google.comfa35e3d2012-06-26 20:16:17 +000031SK_DEFINE_INST_COUNT(GrContext)
32SK_DEFINE_INST_COUNT(GrDrawState)
33
bsalomon@google.com3c4d0322012-04-03 18:04:51 +000034#define DEFER_TEXT_RENDERING 1
bsalomon@google.com27847de2011-02-22 20:59:41 +000035
bsalomon@google.com3c4d0322012-04-03 18:04:51 +000036#define DEFER_PATHS 1
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +000037
bsalomon@google.com3c4d0322012-04-03 18:04:51 +000038#define BATCH_RECT_TO_RECT (1 && !GR_STATIC_RECT_VB)
bsalomon@google.com27847de2011-02-22 20:59:41 +000039
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +000040#define MAX_BLUR_SIGMA 4.0f
41
bsalomon@google.comd46e2422011-09-23 17:40:07 +000042// When we're using coverage AA but the blend is incompatible (given gpu
43// limitations) should we disable AA or draw wrong?
bsalomon@google.com950d7a82011-09-28 15:05:33 +000044#define DISABLE_COVERAGE_AA_FOR_BLEND 1
bsalomon@google.comd46e2422011-09-23 17:40:07 +000045
reed@google.com4b2d3f32012-05-15 18:05:50 +000046#if GR_DEBUG
47 // change this to a 1 to see notifications when partial coverage fails
48 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
49#else
50 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
51#endif
52
bsalomon@google.com07fc0d12012-06-22 15:15:59 +000053static const size_t MAX_TEXTURE_CACHE_COUNT = 256;
54static const size_t MAX_TEXTURE_CACHE_BYTES = 16 * 1024 * 1024;
bsalomon@google.com27847de2011-02-22 20:59:41 +000055
bsalomon@google.com60361492012-03-15 17:47:06 +000056static const size_t DRAW_BUFFER_VBPOOL_BUFFER_SIZE = 1 << 15;
bsalomon@google.com27847de2011-02-22 20:59:41 +000057static const int DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS = 4;
58
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +000059// path rendering is the only thing we defer today that uses non-static indices
60static const size_t DRAW_BUFFER_IBPOOL_BUFFER_SIZE = DEFER_PATHS ? 1 << 11 : 0;
61static const int DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS = DEFER_PATHS ? 4 : 0;
bsalomon@google.com27847de2011-02-22 20:59:41 +000062
bsalomon@google.combc4b6542011-11-19 13:56:11 +000063#define ASSERT_OWNED_RESOURCE(R) GrAssert(!(R) || (R)->getContext() == this)
64
bsalomon@google.com05ef5102011-05-02 21:14:59 +000065GrContext* GrContext::Create(GrEngine engine,
66 GrPlatform3DContext context3D) {
bsalomon@google.com27847de2011-02-22 20:59:41 +000067 GrContext* ctx = NULL;
68 GrGpu* fGpu = GrGpu::Create(engine, context3D);
69 if (NULL != fGpu) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +000070 ctx = SkNEW_ARGS(GrContext, (fGpu));
bsalomon@google.com27847de2011-02-22 20:59:41 +000071 fGpu->unref();
72 }
73 return ctx;
74}
75
bsalomon@google.comc0af3172012-06-15 14:10:09 +000076namespace {
77void* CreateThreadInstanceCount() {
tomhudson@google.comc377baf2012-07-09 20:17:56 +000078 return SkNEW_ARGS(int, (0));
bsalomon@google.comc0af3172012-06-15 14:10:09 +000079}
80void DeleteThreadInstanceCount(void* v) {
81 delete reinterpret_cast<int*>(v);
82}
83#define THREAD_INSTANCE_COUNT \
84 (*reinterpret_cast<int*>(SkTLS::Get(CreateThreadInstanceCount, \
85 DeleteThreadInstanceCount)))
86
87}
88
89int GrContext::GetThreadInstanceCount() {
90 return THREAD_INSTANCE_COUNT;
91}
92
bsalomon@google.com27847de2011-02-22 20:59:41 +000093GrContext::~GrContext() {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000094 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +000095
96 // Since the gpu can hold scratch textures, give it a chance to let go
97 // of them before freeing the texture cache
98 fGpu->purgeResources();
99
bsalomon@google.com27847de2011-02-22 20:59:41 +0000100 delete fTextureCache;
101 delete fFontCache;
102 delete fDrawBuffer;
103 delete fDrawBufferVBAllocPool;
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000104 delete fDrawBufferIBAllocPool;
bsalomon@google.com30085192011-08-19 15:42:31 +0000105
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000106 fAARectRenderer->unref();
107
bsalomon@google.com205d4602011-04-25 12:43:45 +0000108 fGpu->unref();
bsalomon@google.com30085192011-08-19 15:42:31 +0000109 GrSafeUnref(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000110 GrSafeUnref(fSoftwarePathRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000111 fDrawState->unref();
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000112
113 --THREAD_INSTANCE_COUNT;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000114}
115
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000116void GrContext::contextLost() {
junov@google.com53a55842011-06-08 22:55:10 +0000117 contextDestroyed();
118 this->setupDrawBuffer();
119}
120
121void GrContext::contextDestroyed() {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000122 // abandon first to so destructors
123 // don't try to free the resources in the API.
124 fGpu->abandonResources();
125
bsalomon@google.com30085192011-08-19 15:42:31 +0000126 // a path renderer may be holding onto resources that
127 // are now unusable
128 GrSafeSetNull(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000129 GrSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com30085192011-08-19 15:42:31 +0000130
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000131 delete fDrawBuffer;
132 fDrawBuffer = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000133
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000134 delete fDrawBufferVBAllocPool;
135 fDrawBufferVBAllocPool = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000136
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000137 delete fDrawBufferIBAllocPool;
138 fDrawBufferIBAllocPool = NULL;
139
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000140 fAARectRenderer->reset();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000141
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000142 fTextureCache->removeAll();
143 fFontCache->freeAll();
144 fGpu->markContextDirty();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000145}
146
147void GrContext::resetContext() {
148 fGpu->markContextDirty();
149}
150
151void GrContext::freeGpuResources() {
152 this->flush();
robertphillips@google.comff175842012-05-14 19:31:39 +0000153
154 fGpu->purgeResources();
155
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000156 fAARectRenderer->reset();
157
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000158 fTextureCache->removeAll();
159 fFontCache->freeAll();
bsalomon@google.com30085192011-08-19 15:42:31 +0000160 // a path renderer may be holding onto resources
161 GrSafeSetNull(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000162 GrSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000163}
164
twiz@google.com05e70242012-01-27 19:12:00 +0000165size_t GrContext::getGpuTextureCacheBytes() const {
166 return fTextureCache->getCachedResourceBytes();
167}
168
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000169////////////////////////////////////////////////////////////////////////////////
170
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000171GrTexture* GrContext::TextureCacheEntry::texture() const {
172 if (NULL == fEntry) {
173 return NULL;
174 } else {
175 return (GrTexture*) fEntry->resource();
176 }
177}
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000178
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000179namespace {
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000180
181// we should never have more than one stencil buffer with same combo of
182// (width,height,samplecount)
183void gen_stencil_key_values(int width, int height,
184 int sampleCnt, uint32_t v[4]) {
185 v[0] = width;
186 v[1] = height;
187 v[2] = sampleCnt;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000188 v[3] = GrResourceKey::kStencilBuffer_TypeBit;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000189}
190
191void gen_stencil_key_values(const GrStencilBuffer* sb,
192 uint32_t v[4]) {
193 gen_stencil_key_values(sb->width(), sb->height(),
194 sb->numSamples(), v);
195}
bsalomon@google.com82c7bd82011-11-09 15:32:29 +0000196
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000197void scale_rect(SkRect* rect, float xScale, float yScale) {
robertphillips@google.com5af56062012-04-27 15:39:52 +0000198 rect->fLeft = SkScalarMul(rect->fLeft, SkFloatToScalar(xScale));
199 rect->fTop = SkScalarMul(rect->fTop, SkFloatToScalar(yScale));
200 rect->fRight = SkScalarMul(rect->fRight, SkFloatToScalar(xScale));
201 rect->fBottom = SkScalarMul(rect->fBottom, SkFloatToScalar(yScale));
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000202}
203
bsalomon@google.comb505a122012-05-31 18:40:36 +0000204float adjust_sigma(float sigma, int *scaleFactor, int *radius) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000205 *scaleFactor = 1;
206 while (sigma > MAX_BLUR_SIGMA) {
207 *scaleFactor *= 2;
208 sigma *= 0.5f;
209 }
bsalomon@google.comb505a122012-05-31 18:40:36 +0000210 *radius = static_cast<int>(ceilf(sigma * 3.0f));
211 GrAssert(*radius <= GrConvolutionEffect::kMaxKernelRadius);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000212 return sigma;
213}
214
215void apply_morphology(GrGpu* gpu,
216 GrTexture* texture,
217 const SkRect& rect,
218 int radius,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000219 GrContext::MorphologyType morphType,
220 Gr1DKernelEffect::Direction direction) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000221
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000222 GrRenderTarget* target = gpu->drawState()->getRenderTarget();
223 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000224 GrDrawState* drawState = gpu->drawState();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000225 drawState->setRenderTarget(target);
226 GrMatrix sampleM;
227 sampleM.setIDiv(texture->width(), texture->height());
bsalomon@google.comb505a122012-05-31 18:40:36 +0000228 drawState->sampler(0)->reset(sampleM);
229 SkAutoTUnref<GrCustomStage> morph(
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000230 SkNEW_ARGS(GrMorphologyEffect, (texture, direction, radius, morphType)));
bsalomon@google.comb505a122012-05-31 18:40:36 +0000231 drawState->sampler(0)->setCustomStage(morph);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000232 gpu->drawSimpleRect(rect, NULL);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000233}
234
bsalomon@google.comb505a122012-05-31 18:40:36 +0000235void convolve_gaussian(GrGpu* gpu,
236 GrTexture* texture,
237 const SkRect& rect,
238 float sigma,
239 int radius,
240 Gr1DKernelEffect::Direction direction) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000241 GrRenderTarget* target = gpu->drawState()->getRenderTarget();
242 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000243 GrDrawState* drawState = gpu->drawState();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000244 drawState->setRenderTarget(target);
245 GrMatrix sampleM;
246 sampleM.setIDiv(texture->width(), texture->height());
bsalomon@google.comb505a122012-05-31 18:40:36 +0000247 drawState->sampler(0)->reset(sampleM);
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000248 SkAutoTUnref<GrConvolutionEffect> conv(SkNEW_ARGS(GrConvolutionEffect,
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000249 (texture, direction, radius,
250 sigma)));
bsalomon@google.comb505a122012-05-31 18:40:36 +0000251 drawState->sampler(0)->setCustomStage(conv);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000252 gpu->drawSimpleRect(rect, NULL);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000253}
254
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000255}
256
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000257GrContext::TextureCacheEntry GrContext::findAndLockTexture(
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000258 const GrTextureDesc& desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000259 const GrSamplerState* sampler) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000260 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, sampler, desc, false);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000261 return TextureCacheEntry(fTextureCache->findAndLock(resourceKey,
262 GrResourceCache::kNested_LockType));
263}
264
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000265bool GrContext::isTextureInCache(const GrTextureDesc& desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000266 const GrSamplerState* sampler) const {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000267 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, sampler, desc, false);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000268 return fTextureCache->hasKey(resourceKey);
269}
270
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000271GrResourceEntry* GrContext::addAndLockStencilBuffer(GrStencilBuffer* sb) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000272 ASSERT_OWNED_RESOURCE(sb);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000273 uint32_t v[4];
274 gen_stencil_key_values(sb, v);
275 GrResourceKey resourceKey(v);
276 return fTextureCache->createAndLock(resourceKey, sb);
277}
278
279GrStencilBuffer* GrContext::findStencilBuffer(int width, int height,
280 int sampleCnt) {
281 uint32_t v[4];
282 gen_stencil_key_values(width, height, sampleCnt, v);
283 GrResourceKey resourceKey(v);
284 GrResourceEntry* entry = fTextureCache->findAndLock(resourceKey,
285 GrResourceCache::kSingle_LockType);
286 if (NULL != entry) {
287 GrStencilBuffer* sb = (GrStencilBuffer*) entry->resource();
288 return sb;
289 } else {
290 return NULL;
291 }
292}
293
294void GrContext::unlockStencilBuffer(GrResourceEntry* sbEntry) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000295 ASSERT_OWNED_RESOURCE(sbEntry->resource());
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000296 fTextureCache->unlock(sbEntry);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000297}
298
299static void stretchImage(void* dst,
300 int dstW,
301 int dstH,
302 void* src,
303 int srcW,
304 int srcH,
305 int bpp) {
306 GrFixed dx = (srcW << 16) / dstW;
307 GrFixed dy = (srcH << 16) / dstH;
308
309 GrFixed y = dy >> 1;
310
311 int dstXLimit = dstW*bpp;
312 for (int j = 0; j < dstH; ++j) {
313 GrFixed x = dx >> 1;
314 void* srcRow = (uint8_t*)src + (y>>16)*srcW*bpp;
315 void* dstRow = (uint8_t*)dst + j*dstW*bpp;
316 for (int i = 0; i < dstXLimit; i += bpp) {
317 memcpy((uint8_t*) dstRow + i,
318 (uint8_t*) srcRow + (x>>16)*bpp,
319 bpp);
320 x += dx;
321 }
322 y += dy;
323 }
324}
325
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000326GrContext::TextureCacheEntry GrContext::createAndLockTexture(
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000327 const GrSamplerState* sampler,
328 const GrTextureDesc& desc,
329 void* srcData,
330 size_t rowBytes) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000331 SK_TRACE_EVENT0("GrContext::createAndLockTexture");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000332
333#if GR_DUMP_TEXTURE_UPLOAD
334 GrPrintf("GrContext::createAndLockTexture [%d %d]\n", desc.fWidth, desc.fHeight);
335#endif
336
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000337 TextureCacheEntry entry;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000338
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000339 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, sampler,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000340 desc, false);
341
342 if (GrTexture::NeedsResizing(resourceKey)) {
343 // The desired texture is NPOT and tiled but that isn't supported by
344 // the current hardware. Resize the texture to be a POT
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000345 GrAssert(NULL != sampler);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000346 TextureCacheEntry clampEntry = this->findAndLockTexture(desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000347 NULL);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000348
349 if (NULL == clampEntry.texture()) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000350 clampEntry = this->createAndLockTexture(NULL, desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000351 srcData, rowBytes);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000352 GrAssert(NULL != clampEntry.texture());
353 if (NULL == clampEntry.texture()) {
354 return entry;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000355 }
356 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000357 GrTextureDesc rtDesc = desc;
358 rtDesc.fFlags = rtDesc.fFlags |
359 kRenderTarget_GrTextureFlagBit |
360 kNoStencil_GrTextureFlagBit;
bsalomon@google.com99621082011-11-15 16:47:16 +0000361 rtDesc.fWidth = GrNextPow2(GrMax(desc.fWidth, 64));
362 rtDesc.fHeight = GrNextPow2(GrMax(desc.fHeight, 64));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000363
364 GrTexture* texture = fGpu->createTexture(rtDesc, NULL, 0);
365
366 if (NULL != texture) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000367 GrDrawTarget::AutoStateRestore asr(fGpu,
368 GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000369 GrDrawState* drawState = fGpu->drawState();
370 drawState->setRenderTarget(texture->asRenderTarget());
bsalomon@google.com82c7bd82011-11-09 15:32:29 +0000371
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000372 GrSamplerState::Filter filter;
373 // if filtering is not desired then we want to ensure all
374 // texels in the resampled image are copies of texels from
375 // the original.
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000376 if (GrTexture::NeedsFiltering(resourceKey)) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000377 filter = GrSamplerState::kBilinear_Filter;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000378 } else {
379 filter = GrSamplerState::kNearest_Filter;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000380 }
bsalomon@google.com1e266f82011-12-12 16:11:33 +0000381 drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode,
382 filter);
bsalomon@google.com8f4fdc92012-07-24 17:59:01 +0000383 drawState->createTextureEffect(0, clampEntry.texture());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000384
385 static const GrVertexLayout layout =
386 GrDrawTarget::StageTexCoordVertexLayoutBit(0,0);
387 GrDrawTarget::AutoReleaseGeometry arg(fGpu, layout, 4, 0);
388
389 if (arg.succeeded()) {
390 GrPoint* verts = (GrPoint*) arg.vertices();
391 verts[0].setIRectFan(0, 0,
392 texture->width(),
393 texture->height(),
394 2*sizeof(GrPoint));
395 verts[1].setIRectFan(0, 0, 1, 1, 2*sizeof(GrPoint));
bsalomon@google.com47059542012-06-06 20:51:20 +0000396 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000397 0, 4);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000398 entry.set(fTextureCache->createAndLock(resourceKey, texture));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000399 }
bsalomon@google.com1da07462011-03-10 14:51:57 +0000400 texture->releaseRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000401 } else {
402 // TODO: Our CPU stretch doesn't filter. But we create separate
403 // stretched textures when the sampler state is either filtered or
404 // not. Either implement filtered stretch blit on CPU or just create
405 // one when FBO case fails.
406
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000407 rtDesc.fFlags = kNone_GrTextureFlags;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000408 // no longer need to clamp at min RT size.
409 rtDesc.fWidth = GrNextPow2(desc.fWidth);
410 rtDesc.fHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000411 int bpp = GrBytesPerPixel(desc.fConfig);
bsalomon@google.com3582bf92011-06-30 21:32:31 +0000412 SkAutoSMalloc<128*128*4> stretchedPixels(bpp *
bsalomon@google.com27847de2011-02-22 20:59:41 +0000413 rtDesc.fWidth *
414 rtDesc.fHeight);
415 stretchImage(stretchedPixels.get(), rtDesc.fWidth, rtDesc.fHeight,
416 srcData, desc.fWidth, desc.fHeight, bpp);
417
418 size_t stretchedRowBytes = rtDesc.fWidth * bpp;
419
420 GrTexture* texture = fGpu->createTexture(rtDesc,
421 stretchedPixels.get(),
422 stretchedRowBytes);
423 GrAssert(NULL != texture);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000424 entry.set(fTextureCache->createAndLock(resourceKey, texture));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000425 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000426 fTextureCache->unlock(clampEntry.cacheEntry());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000427
428 } else {
429 GrTexture* texture = fGpu->createTexture(desc, srcData, rowBytes);
430 if (NULL != texture) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000431 entry.set(fTextureCache->createAndLock(resourceKey, texture));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000432 }
433 }
434 return entry;
435}
436
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000437GrContext::TextureCacheEntry GrContext::lockScratchTexture(
438 const GrTextureDesc& inDesc,
439 ScratchTexMatch match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000440 GrTextureDesc desc = inDesc;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000441 desc.fClientCacheID = kScratch_CacheID;
442
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000443 if (kExact_ScratchTexMatch != match) {
444 // bin by pow2 with a reasonable min
445 static const int MIN_SIZE = 256;
446 desc.fWidth = GrMax(MIN_SIZE, GrNextPow2(desc.fWidth));
447 desc.fHeight = GrMax(MIN_SIZE, GrNextPow2(desc.fHeight));
448 }
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000449
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000450 GrResourceEntry* entry;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000451 int origWidth = desc.fWidth;
452 int origHeight = desc.fHeight;
453 bool doubledW = false;
454 bool doubledH = false;
455
456 do {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000457 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL, desc, true);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000458 entry = fTextureCache->findAndLock(key,
459 GrResourceCache::kNested_LockType);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000460 // if we miss, relax the fit of the flags...
461 // then try doubling width... then height.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000462 if (NULL != entry || kExact_ScratchTexMatch == match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000463 break;
464 }
465 if (!(desc.fFlags & kRenderTarget_GrTextureFlagBit)) {
466 desc.fFlags = desc.fFlags | kRenderTarget_GrTextureFlagBit;
467 } else if (desc.fFlags & kNoStencil_GrTextureFlagBit) {
468 desc.fFlags = desc.fFlags & ~kNoStencil_GrTextureFlagBit;
469 } else if (!doubledW) {
470 desc.fFlags = inDesc.fFlags;
471 desc.fWidth *= 2;
472 doubledW = true;
473 } else if (!doubledH) {
474 desc.fFlags = inDesc.fFlags;
475 desc.fWidth = origWidth;
476 desc.fHeight *= 2;
477 doubledH = true;
478 } else {
479 break;
480 }
481
482 } while (true);
483
484 if (NULL == entry) {
485 desc.fFlags = inDesc.fFlags;
486 desc.fWidth = origWidth;
487 desc.fHeight = origHeight;
488 GrTexture* texture = fGpu->createTexture(desc, NULL, 0);
489 if (NULL != texture) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000490 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000491 texture->desc(),
492 true);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000493 entry = fTextureCache->createAndLock(key, texture);
494 }
495 }
496
497 // If the caller gives us the same desc/sampler twice we don't want
498 // to return the same texture the second time (unless it was previously
499 // released). So we detach the entry from the cache and reattach at release.
500 if (NULL != entry) {
501 fTextureCache->detach(entry);
502 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000503 return TextureCacheEntry(entry);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000504}
505
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000506void GrContext::addExistingTextureToCache(GrTexture* texture) {
507
508 if (NULL == texture) {
509 return;
510 }
511
512 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL,
513 texture->desc(),
514 true);
515 fTextureCache->attach(key, texture);
516}
517
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000518void GrContext::unlockTexture(TextureCacheEntry entry) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000519 ASSERT_OWNED_RESOURCE(entry.texture());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000520 // If this is a scratch texture we detached it from the cache
521 // while it was locked (to avoid two callers simultaneously getting
522 // the same texture).
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000523 if (GrTexture::IsScratchTexture(entry.cacheEntry()->key())) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000524 fTextureCache->reattachAndUnlock(entry.cacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000525 } else {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000526 fTextureCache->unlock(entry.cacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000527 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000528}
529
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000530void GrContext::freeEntry(TextureCacheEntry entry) {
531 ASSERT_OWNED_RESOURCE(entry.texture());
532
533 fTextureCache->freeEntry(entry.cacheEntry());
534}
535
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000536GrTexture* GrContext::createUncachedTexture(const GrTextureDesc& descIn,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000537 void* srcData,
538 size_t rowBytes) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000539 GrTextureDesc descCopy = descIn;
540 descCopy.fClientCacheID = kUncached_CacheID;
541 return fGpu->createTexture(descCopy, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000542}
543
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000544void GrContext::getTextureCacheLimits(int* maxTextures,
545 size_t* maxTextureBytes) const {
546 fTextureCache->getLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000547}
548
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000549void GrContext::setTextureCacheLimits(int maxTextures, size_t maxTextureBytes) {
550 fTextureCache->setLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000551}
552
bsalomon@google.com91958362011-06-13 17:58:13 +0000553int GrContext::getMaxTextureSize() const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000554 return fGpu->getCaps().fMaxTextureSize;
bsalomon@google.com91958362011-06-13 17:58:13 +0000555}
556
557int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000558 return fGpu->getCaps().fMaxRenderTargetSize;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000559}
560
561///////////////////////////////////////////////////////////////////////////////
562
bsalomon@google.come269f212011-11-07 13:29:52 +0000563GrTexture* GrContext::createPlatformTexture(const GrPlatformTextureDesc& desc) {
564 return fGpu->createPlatformTexture(desc);
565}
566
567GrRenderTarget* GrContext::createPlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
568 return fGpu->createPlatformRenderTarget(desc);
569}
570
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000571///////////////////////////////////////////////////////////////////////////////
572
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000573bool GrContext::supportsIndex8PixelConfig(const GrSamplerState* sampler,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000574 int width, int height) const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000575 const GrDrawTarget::Caps& caps = fGpu->getCaps();
576 if (!caps.f8BitPaletteSupport) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000577 return false;
578 }
579
bsalomon@google.com27847de2011-02-22 20:59:41 +0000580 bool isPow2 = GrIsPow2(width) && GrIsPow2(height);
581
582 if (!isPow2) {
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000583 bool tiled = NULL != sampler &&
584 (sampler->getWrapX() != GrSamplerState::kClamp_WrapMode ||
585 sampler->getWrapY() != GrSamplerState::kClamp_WrapMode);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000586 if (tiled && !caps.fNPOTTextureTileSupport) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000587 return false;
588 }
589 }
590 return true;
591}
592
593////////////////////////////////////////////////////////////////////////////////
594
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000595const GrClip& GrContext::getClip() const { return fGpu->getClip(); }
596
bsalomon@google.com27847de2011-02-22 20:59:41 +0000597void GrContext::setClip(const GrClip& clip) {
598 fGpu->setClip(clip);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000599 fDrawState->enableState(GrDrawState::kClip_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000600}
601
bsalomon@google.com27847de2011-02-22 20:59:41 +0000602////////////////////////////////////////////////////////////////////////////////
603
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000604void GrContext::clear(const GrIRect* rect,
605 const GrColor color,
606 GrRenderTarget* target) {
bsalomon@google.com398109c2011-04-14 18:40:27 +0000607 this->flush();
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000608 fGpu->clear(rect, color, target);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000609}
610
611void GrContext::drawPaint(const GrPaint& paint) {
612 // set rect to be big enough to fill the space, but not super-huge, so we
613 // don't overflow fixed-point implementations
bsalomon@google.comd302f142011-03-03 13:54:13 +0000614 GrRect r;
615 r.setLTRB(0, 0,
616 GrIntToScalar(getRenderTarget()->width()),
617 GrIntToScalar(getRenderTarget()->height()));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000618 GrMatrix inverse;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000619 SkTLazy<GrPaint> tmpPaint;
620 const GrPaint* p = &paint;
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000621 AutoMatrix am;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000622
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000623 // We attempt to map r by the inverse matrix and draw that. mapRect will
624 // map the four corners and bound them with a new rect. This will not
625 // produce a correct result for some perspective matrices.
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000626 if (!this->getMatrix().hasPerspective()) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000627 if (!fDrawState->getViewInverse(&inverse)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000628 GrPrintf("Could not invert matrix\n");
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000629 return;
630 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000631 inverse.mapRect(&r);
632 } else {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000633 if (paint.hasTextureOrMask()) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000634 tmpPaint.set(paint);
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000635 p = tmpPaint.get();
bsalomon@google.come3d32162012-07-20 13:37:06 +0000636 if (!tmpPaint.get()->preConcatSamplerMatricesWithInverse(fDrawState->getViewMatrix())) {
637 GrPrintf("Could not invert matrix\n");
638 }
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000639 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000640 am.set(this, GrMatrix::I());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000641 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000642 // by definition this fills the entire clip, no need for AA
643 if (paint.fAntiAlias) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000644 if (!tmpPaint.isValid()) {
645 tmpPaint.set(paint);
646 p = tmpPaint.get();
647 }
648 GrAssert(p == tmpPaint.get());
649 tmpPaint.get()->fAntiAlias = false;
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000650 }
651 this->drawRect(*p, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000652}
653
bsalomon@google.com205d4602011-04-25 12:43:45 +0000654////////////////////////////////////////////////////////////////////////////////
655
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000656namespace {
657inline bool disable_coverage_aa_for_blend(GrDrawTarget* target) {
658 return DISABLE_COVERAGE_AA_FOR_BLEND && !target->canApplyCoverage();
659}
660}
661
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000662////////////////////////////////////////////////////////////////////////////////
663
bsalomon@google.com27847de2011-02-22 20:59:41 +0000664/* create a triangle strip that strokes the specified triangle. There are 8
665 unique vertices, but we repreat the last 2 to close up. Alternatively we
666 could use an indices array, and then only send 8 verts, but not sure that
667 would be faster.
668 */
bsalomon@google.com205d4602011-04-25 12:43:45 +0000669static void setStrokeRectStrip(GrPoint verts[10], GrRect rect,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000670 GrScalar width) {
671 const GrScalar rad = GrScalarHalf(width);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000672 rect.sort();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000673
674 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
675 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
676 verts[2].set(rect.fRight - rad, rect.fTop + rad);
677 verts[3].set(rect.fRight + rad, rect.fTop - rad);
678 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
679 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
680 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
681 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
682 verts[8] = verts[0];
683 verts[9] = verts[1];
684}
685
reed@google.com20efde72011-05-09 17:00:02 +0000686/**
687 * Returns true if the rects edges are integer-aligned.
688 */
689static bool isIRect(const GrRect& r) {
690 return GrScalarIsInt(r.fLeft) && GrScalarIsInt(r.fTop) &&
691 GrScalarIsInt(r.fRight) && GrScalarIsInt(r.fBottom);
692}
693
bsalomon@google.com205d4602011-04-25 12:43:45 +0000694static bool apply_aa_to_rect(GrDrawTarget* target,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000695 const GrRect& rect,
696 GrScalar width,
697 const GrMatrix* matrix,
698 GrMatrix* combinedMatrix,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000699 GrRect* devRect,
700 bool* useVertexCoverage) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000701 // we use a simple coverage ramp to do aa on axis-aligned rects
702 // we check if the rect will be axis-aligned, and the rect won't land on
703 // integer coords.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000704
bsalomon@google.coma3108262011-10-10 14:08:47 +0000705 // we are keeping around the "tweak the alpha" trick because
706 // it is our only hope for the fixed-pipe implementation.
707 // In a shader implementation we can give a separate coverage input
bsalomon@google.com289533a2011-10-27 12:34:25 +0000708 // TODO: remove this ugliness when we drop the fixed-pipe impl
bsalomon@google.coma3108262011-10-10 14:08:47 +0000709 *useVertexCoverage = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000710 if (!target->canTweakAlphaForCoverage()) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000711 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +0000712#if GR_DEBUG
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000713 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +0000714#endif
bsalomon@google.coma3108262011-10-10 14:08:47 +0000715 return false;
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000716 } else {
717 *useVertexCoverage = true;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000718 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000719 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000720 const GrDrawState& drawState = target->getDrawState();
721 if (drawState.getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000722 return false;
723 }
724
bsalomon@google.com471d4712011-08-23 15:45:25 +0000725 if (0 == width && target->willUseHWAALines()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000726 return false;
727 }
728
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000729 if (!drawState.getViewMatrix().preservesAxisAlignment()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000730 return false;
731 }
732
733 if (NULL != matrix &&
734 !matrix->preservesAxisAlignment()) {
735 return false;
736 }
737
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000738 *combinedMatrix = drawState.getViewMatrix();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000739 if (NULL != matrix) {
740 combinedMatrix->preConcat(*matrix);
741 GrAssert(combinedMatrix->preservesAxisAlignment());
742 }
743
744 combinedMatrix->mapRect(devRect, rect);
745 devRect->sort();
746
747 if (width < 0) {
reed@google.com20efde72011-05-09 17:00:02 +0000748 return !isIRect(*devRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000749 } else {
750 return true;
751 }
752}
753
bsalomon@google.com27847de2011-02-22 20:59:41 +0000754void GrContext::drawRect(const GrPaint& paint,
755 const GrRect& rect,
756 GrScalar width,
757 const GrMatrix* matrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000758 SK_TRACE_EVENT0("GrContext::drawRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000759
760 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000761 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000762
bsalomon@google.com205d4602011-04-25 12:43:45 +0000763 GrRect devRect = rect;
764 GrMatrix combinedMatrix;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000765 bool useVertexCoverage;
bsalomon@google.com289533a2011-10-27 12:34:25 +0000766 bool needAA = paint.fAntiAlias &&
767 !this->getRenderTarget()->isMultisampled();
768 bool doAA = needAA && apply_aa_to_rect(target, rect, width, matrix,
769 &combinedMatrix, &devRect,
770 &useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000771
772 if (doAA) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000773 GrDrawTarget::AutoDeviceCoordDraw adcd(target);
774 if (!adcd.succeeded()) {
775 return;
776 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000777 if (width >= 0) {
778 GrVec strokeSize;;
779 if (width > 0) {
780 strokeSize.set(width, width);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000781 combinedMatrix.mapVectors(&strokeSize, 1);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000782 strokeSize.setAbs(strokeSize);
783 } else {
784 strokeSize.set(GR_Scalar1, GR_Scalar1);
785 }
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000786 fAARectRenderer->strokeAARect(this->getGpu(), target, devRect,
787 strokeSize, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000788 } else {
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000789 fAARectRenderer->fillAARect(this->getGpu(), target,
790 devRect, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000791 }
792 return;
793 }
794
bsalomon@google.com27847de2011-02-22 20:59:41 +0000795 if (width >= 0) {
796 // TODO: consider making static vertex buffers for these cases.
797 // Hairline could be done by just adding closing vertex to
798 // unitSquareVertexBuffer()
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000799
bsalomon@google.com27847de2011-02-22 20:59:41 +0000800 static const int worstCaseVertCount = 10;
bsalomon@google.come3d32162012-07-20 13:37:06 +0000801 GrDrawTarget::AutoReleaseGeometry geo(target, 0, worstCaseVertCount, 0);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000802
803 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000804 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000805 return;
806 }
807
808 GrPrimitiveType primType;
809 int vertCount;
810 GrPoint* vertex = geo.positions();
811
812 if (width > 0) {
813 vertCount = 10;
bsalomon@google.com47059542012-06-06 20:51:20 +0000814 primType = kTriangleStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000815 setStrokeRectStrip(vertex, rect, width);
816 } else {
817 // hairline
818 vertCount = 5;
bsalomon@google.com47059542012-06-06 20:51:20 +0000819 primType = kLineStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000820 vertex[0].set(rect.fLeft, rect.fTop);
821 vertex[1].set(rect.fRight, rect.fTop);
822 vertex[2].set(rect.fRight, rect.fBottom);
823 vertex[3].set(rect.fLeft, rect.fBottom);
824 vertex[4].set(rect.fLeft, rect.fTop);
825 }
826
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000827 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000828 if (NULL != matrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000829 GrDrawState* drawState = target->drawState();
830 avmr.set(drawState);
831 drawState->preConcatViewMatrix(*matrix);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000832 drawState->preConcatSamplerMatrices(*matrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000833 }
834
835 target->drawNonIndexed(primType, 0, vertCount);
836 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000837#if GR_STATIC_RECT_VB
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000838 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
839 if (NULL == sqVB) {
840 GrPrintf("Failed to create static rect vb.\n");
841 return;
842 }
bsalomon@google.come3d32162012-07-20 13:37:06 +0000843 target->setVertexSourceToBuffer(0, sqVB);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000844 GrDrawState* drawState = target->drawState();
845 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000846 GrMatrix m;
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000847 m.setAll(rect.width(), 0, rect.fLeft,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000848 0, rect.height(), rect.fTop,
849 0, 0, GrMatrix::I()[8]);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000850
851 if (NULL != matrix) {
852 m.postConcat(*matrix);
853 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000854 drawState->preConcatViewMatrix(m);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000855 drawState->preConcatSamplerMatrices(m);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000856
bsalomon@google.com47059542012-06-06 20:51:20 +0000857 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000858#else
bsalomon@google.come3d32162012-07-20 13:37:06 +0000859 target->drawSimpleRect(rect, matrix);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000860#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +0000861 }
862}
863
864void GrContext::drawRectToRect(const GrPaint& paint,
865 const GrRect& dstRect,
866 const GrRect& srcRect,
867 const GrMatrix* dstMatrix,
868 const GrMatrix* srcMatrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000869 SK_TRACE_EVENT0("GrContext::drawRectToRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000870
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000871 // srcRect refers to paint's first texture
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000872 if (!paint.isTextureStageEnabled(0)) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000873 drawRect(paint, dstRect, -1, dstMatrix);
874 return;
875 }
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000876
bsalomon@google.com27847de2011-02-22 20:59:41 +0000877 GR_STATIC_ASSERT(!BATCH_RECT_TO_RECT || !GR_STATIC_RECT_VB);
878
879#if GR_STATIC_RECT_VB
880 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000881 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000882 GrDrawState* drawState = target->drawState();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000883 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000884
885 GrMatrix m;
886
887 m.setAll(dstRect.width(), 0, dstRect.fLeft,
888 0, dstRect.height(), dstRect.fTop,
889 0, 0, GrMatrix::I()[8]);
890 if (NULL != dstMatrix) {
891 m.postConcat(*dstMatrix);
892 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000893 drawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000894
bsalomon@google.come3d32162012-07-20 13:37:06 +0000895 // we explicitly setup the correct coords for the first stage. The others
896 // must know about the view matrix change.
897 for (int s = 1; s < GrPaint::kTotalStages; ++s) {
898 if (drawState->isStageEnabled(s)) {
899 drawState->sampler(s)->preConcatMatrix(m);
900 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000901 }
902
bsalomon@google.com27847de2011-02-22 20:59:41 +0000903 m.setAll(srcRect.width(), 0, srcRect.fLeft,
904 0, srcRect.height(), srcRect.fTop,
905 0, 0, GrMatrix::I()[8]);
906 if (NULL != srcMatrix) {
907 m.postConcat(*srcMatrix);
908 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000909 drawState->sampler(GrPaint::kFirstTextureStage)->preConcatMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000910
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000911 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
912 if (NULL == sqVB) {
913 GrPrintf("Failed to create static rect vb.\n");
914 return;
915 }
bsalomon@google.come3d32162012-07-20 13:37:06 +0000916 target->setVertexSourceToBuffer(0, sqVB);
bsalomon@google.com47059542012-06-06 20:51:20 +0000917 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000918#else
919
920 GrDrawTarget* target;
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000921#if BATCH_RECT_TO_RECT
bsalomon@google.com27847de2011-02-22 20:59:41 +0000922 target = this->prepareToDraw(paint, kBuffered_DrawCategory);
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000923#else
bsalomon@google.com27847de2011-02-22 20:59:41 +0000924 target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
925#endif
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000926 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000927
tomhudson@google.com93813632011-10-27 20:21:16 +0000928 const GrRect* srcRects[GrDrawState::kNumStages] = {NULL};
929 const GrMatrix* srcMatrices[GrDrawState::kNumStages] = {NULL};
bsalomon@google.com27847de2011-02-22 20:59:41 +0000930 srcRects[0] = &srcRect;
931 srcMatrices[0] = srcMatrix;
932
bsalomon@google.come3d32162012-07-20 13:37:06 +0000933 target->drawRect(dstRect, dstMatrix, srcRects, srcMatrices);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000934#endif
935}
936
937void GrContext::drawVertices(const GrPaint& paint,
938 GrPrimitiveType primitiveType,
939 int vertexCount,
940 const GrPoint positions[],
941 const GrPoint texCoords[],
942 const GrColor colors[],
943 const uint16_t indices[],
944 int indexCount) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000945 SK_TRACE_EVENT0("GrContext::drawVertices");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000946
947 GrDrawTarget::AutoReleaseGeometry geo;
948
949 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000950 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000951
bsalomon@google.come3d32162012-07-20 13:37:06 +0000952 GrVertexLayout layout = 0;
953 if (NULL != texCoords) {
954 layout |= GrDrawTarget::StageTexCoordVertexLayoutBit(0, 0);
955 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000956 if (NULL != colors) {
957 layout |= GrDrawTarget::kColor_VertexLayoutBit;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000958 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000959 int vertexSize = GrDrawTarget::VertexSize(layout);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000960
961 if (sizeof(GrPoint) != vertexSize) {
962 if (!geo.set(target, layout, vertexCount, 0)) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000963 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000964 return;
965 }
tomhudson@google.com93813632011-10-27 20:21:16 +0000966 int texOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000967 int colorOffset;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000968 GrDrawTarget::VertexSizeAndOffsetsByIdx(layout,
969 texOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000970 &colorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000971 NULL,
972 NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000973 void* curVertex = geo.vertices();
974
975 for (int i = 0; i < vertexCount; ++i) {
976 *((GrPoint*)curVertex) = positions[i];
977
978 if (texOffsets[0] > 0) {
979 *(GrPoint*)((intptr_t)curVertex + texOffsets[0]) = texCoords[i];
980 }
981 if (colorOffset > 0) {
982 *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i];
983 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000984 curVertex = (void*)((intptr_t)curVertex + vertexSize);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000985 }
986 } else {
987 target->setVertexSourceToArray(layout, positions, vertexCount);
988 }
989
bsalomon@google.com91958362011-06-13 17:58:13 +0000990 // we don't currently apply offscreen AA to this path. Need improved
991 // management of GrDrawTarget's geometry to avoid copying points per-tile.
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000992
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000993 if (NULL != indices) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000994 target->setIndexSourceToArray(indices, indexCount);
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000995 target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000996 } else {
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000997 target->drawNonIndexed(primitiveType, 0, vertexCount);
998 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000999}
1000
bsalomon@google.com06afe7b2011-04-26 15:31:40 +00001001///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com150d2842012-01-12 20:19:56 +00001002namespace {
1003
bsalomon@google.com93c96602012-04-27 13:05:21 +00001004struct CircleVertex {
1005 GrPoint fPos;
1006 GrPoint fCenter;
1007 GrScalar fOuterRadius;
1008 GrScalar fInnerRadius;
1009};
1010
1011/* Returns true if will map a circle to another circle. This can be true
1012 * if the matrix only includes square-scale, rotation, translation.
1013 */
1014inline bool isSimilarityTransformation(const SkMatrix& matrix,
1015 SkScalar tol = SK_ScalarNearlyZero) {
1016 if (matrix.isIdentity() || matrix.getType() == SkMatrix::kTranslate_Mask) {
1017 return true;
1018 }
1019 if (matrix.hasPerspective()) {
1020 return false;
1021 }
1022
1023 SkScalar mx = matrix.get(SkMatrix::kMScaleX);
1024 SkScalar sx = matrix.get(SkMatrix::kMSkewX);
1025 SkScalar my = matrix.get(SkMatrix::kMScaleY);
1026 SkScalar sy = matrix.get(SkMatrix::kMSkewY);
1027
1028 if (mx == 0 && sx == 0 && my == 0 && sy == 0) {
1029 return false;
1030 }
1031
1032 // it has scales or skews, but it could also be rotation, check it out.
1033 SkVector vec[2];
1034 vec[0].set(mx, sx);
1035 vec[1].set(sy, my);
1036
1037 return SkScalarNearlyZero(vec[0].dot(vec[1]), SkScalarSquare(tol)) &&
1038 SkScalarNearlyEqual(vec[0].lengthSqd(), vec[1].lengthSqd(),
1039 SkScalarSquare(tol));
1040}
1041
1042}
1043
1044// TODO: strokeWidth can't be larger than zero right now.
1045// It will be fixed when drawPath() can handle strokes.
1046void GrContext::drawOval(const GrPaint& paint,
1047 const GrRect& rect,
1048 SkScalar strokeWidth) {
1049 DrawCategory category = (DEFER_PATHS) ? kBuffered_DrawCategory :
1050 kUnbuffered_DrawCategory;
1051 GrDrawTarget* target = this->prepareToDraw(paint, category);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +00001052 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001053 GrDrawState* drawState = target->drawState();
1054 GrMatrix vm = drawState->getViewMatrix();
1055
1056 if (!isSimilarityTransformation(vm) ||
1057 !paint.fAntiAlias ||
1058 rect.height() != rect.width()) {
1059 SkPath path;
1060 path.addOval(rect);
1061 GrPathFill fill = (strokeWidth == 0) ?
bsalomon@google.com47059542012-06-06 20:51:20 +00001062 kHairLine_GrPathFill : kWinding_GrPathFill;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001063 this->internalDrawPath(paint, path, fill, NULL);
1064 return;
1065 }
1066
1067 const GrRenderTarget* rt = drawState->getRenderTarget();
1068 if (NULL == rt) {
1069 return;
1070 }
1071
bsalomon@google.come3d32162012-07-20 13:37:06 +00001072 GrDrawTarget::AutoDeviceCoordDraw adcd(target);
1073 if (!adcd.succeeded()) {
1074 return;
1075 }
bsalomon@google.com93c96602012-04-27 13:05:21 +00001076
bsalomon@google.come3d32162012-07-20 13:37:06 +00001077 GrVertexLayout layout = GrDrawTarget::kEdge_VertexLayoutBit;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001078 GrAssert(sizeof(CircleVertex) == GrDrawTarget::VertexSize(layout));
1079
1080 GrPoint center = GrPoint::Make(rect.centerX(), rect.centerY());
1081 GrScalar radius = SkScalarHalf(rect.width());
1082
1083 vm.mapPoints(&center, 1);
1084 radius = vm.mapRadius(radius);
1085
1086 GrScalar outerRadius = radius;
1087 GrScalar innerRadius = 0;
1088 SkScalar halfWidth = 0;
1089 if (strokeWidth == 0) {
1090 halfWidth = SkScalarHalf(SK_Scalar1);
1091
1092 outerRadius += halfWidth;
1093 innerRadius = SkMaxScalar(0, radius - halfWidth);
1094 }
1095
1096 GrDrawTarget::AutoReleaseGeometry geo(target, layout, 4, 0);
1097 if (!geo.succeeded()) {
1098 GrPrintf("Failed to get space for vertices!\n");
1099 return;
1100 }
1101
1102 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
1103
robertphillips@google.coma0a66c12012-06-22 13:14:29 +00001104 // The fragment shader will extend the radius out half a pixel
1105 // to antialias. Expand the drawn rect here so all the pixels
1106 // will be captured.
1107 SkScalar L = center.fX - outerRadius - SkFloatToScalar(0.5f);
1108 SkScalar R = center.fX + outerRadius + SkFloatToScalar(0.5f);
1109 SkScalar T = center.fY - outerRadius - SkFloatToScalar(0.5f);
1110 SkScalar B = center.fY + outerRadius + SkFloatToScalar(0.5f);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001111
1112 verts[0].fPos = SkPoint::Make(L, T);
1113 verts[1].fPos = SkPoint::Make(R, T);
1114 verts[2].fPos = SkPoint::Make(L, B);
1115 verts[3].fPos = SkPoint::Make(R, B);
1116
1117 for (int i = 0; i < 4; ++i) {
1118 // this goes to fragment shader, it should be in y-points-up space.
1119 verts[i].fCenter = SkPoint::Make(center.fX, rt->height() - center.fY);
1120
1121 verts[i].fOuterRadius = outerRadius;
1122 verts[i].fInnerRadius = innerRadius;
1123 }
1124
1125 drawState->setVertexEdgeType(GrDrawState::kCircle_EdgeType);
bsalomon@google.com47059542012-06-06 20:51:20 +00001126 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4);
bsalomon@google.com150d2842012-01-12 20:19:56 +00001127}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001128
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001129void GrContext::drawPath(const GrPaint& paint, const SkPath& path,
reed@google.com07f3ee12011-05-16 17:21:57 +00001130 GrPathFill fill, const GrPoint* translate) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001131
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001132 if (path.isEmpty()) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001133 if (GrIsFillInverted(fill)) {
1134 this->drawPaint(paint);
1135 }
1136 return;
1137 }
1138
bsalomon@google.com93c96602012-04-27 13:05:21 +00001139 SkRect ovalRect;
1140 if (!GrIsFillInverted(fill) && path.isOval(&ovalRect)) {
1141 if (translate) {
1142 ovalRect.offset(*translate);
1143 }
bsalomon@google.com47059542012-06-06 20:51:20 +00001144 SkScalar width = (fill == kHairLine_GrPathFill) ? 0 : -SK_Scalar1;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001145 this->drawOval(paint, ovalRect, width);
1146 return;
1147 }
1148
1149 internalDrawPath(paint, path, fill, translate);
1150}
1151
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001152void GrContext::internalDrawPath(const GrPaint& paint, const SkPath& path,
bsalomon@google.com93c96602012-04-27 13:05:21 +00001153 GrPathFill fill, const GrPoint* translate) {
1154
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001155 // Note that below we may sw-rasterize the path into a scratch texture.
1156 // Scratch textures can be recycled after they are returned to the texture
1157 // cache. This presents a potential hazard for buffered drawing. However,
1158 // the writePixels that uploads to the scratch will perform a flush so we're
1159 // OK.
1160 DrawCategory category = (DEFER_PATHS) ? kBuffered_DrawCategory :
1161 kUnbuffered_DrawCategory;
1162 GrDrawTarget* target = this->prepareToDraw(paint, category);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +00001163 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001164
bsalomon@google.com289533a2011-10-27 12:34:25 +00001165 bool prAA = paint.fAntiAlias && !this->getRenderTarget()->isMultisampled();
1166
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001167 // An Assumption here is that path renderer would use some form of tweaking
1168 // the src color (either the input alpha or in the frag shader) to implement
1169 // aa. If we have some future driver-mojo path AA that can do the right
1170 // thing WRT to the blend then we'll need some query on the PR.
1171 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001172#if GR_DEBUG
bsalomon@google.com979432b2011-11-05 21:38:22 +00001173 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001174#endif
bsalomon@google.com289533a2011-10-27 12:34:25 +00001175 prAA = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001176 }
bsalomon@google.com289533a2011-10-27 12:34:25 +00001177
robertphillips@google.com72176b22012-05-23 13:19:12 +00001178 GrPathRenderer* pr = this->getPathRenderer(path, fill, target, prAA, true);
bsalomon@google.com30085192011-08-19 15:42:31 +00001179 if (NULL == pr) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001180#if GR_DEBUG
bsalomon@google.com30085192011-08-19 15:42:31 +00001181 GrPrintf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001182#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001183 return;
1184 }
1185
bsalomon@google.come3d32162012-07-20 13:37:06 +00001186 pr->drawPath(path, fill, translate, target, prAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001187}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001188
bsalomon@google.com27847de2011-02-22 20:59:41 +00001189////////////////////////////////////////////////////////////////////////////////
1190
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001191void GrContext::flush(int flagsBitfield) {
1192 if (kDiscard_FlushBit & flagsBitfield) {
1193 fDrawBuffer->reset();
1194 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001195 this->flushDrawBuffer();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001196 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001197 if (kForceCurrentRenderTarget_FlushBit & flagsBitfield) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001198 fGpu->forceRenderTargetFlush();
1199 }
1200}
1201
bsalomon@google.com27847de2011-02-22 20:59:41 +00001202void GrContext::flushDrawBuffer() {
junov@google.com53a55842011-06-08 22:55:10 +00001203 if (fDrawBuffer) {
robertphillips@google.com58b38182012-05-03 16:29:41 +00001204 // With addition of the AA clip path, flushing the draw buffer can
1205 // result in the generation of an AA clip mask. During this
1206 // process the SW path renderer may be invoked which recusively
1207 // calls this method (via internalWriteTexturePixels) creating
1208 // infinite recursion
1209 GrInOrderDrawBuffer* temp = fDrawBuffer;
1210 fDrawBuffer = NULL;
1211
1212 temp->flushTo(fGpu);
1213
1214 fDrawBuffer = temp;
junov@google.com53a55842011-06-08 22:55:10 +00001215 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001216}
1217
bsalomon@google.com6f379512011-11-16 20:36:03 +00001218void GrContext::internalWriteTexturePixels(GrTexture* texture,
1219 int left, int top,
1220 int width, int height,
1221 GrPixelConfig config,
1222 const void* buffer,
1223 size_t rowBytes,
1224 uint32_t flags) {
1225 SK_TRACE_EVENT0("GrContext::writeTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001226 ASSERT_OWNED_RESOURCE(texture);
1227
bsalomon@google.com6f379512011-11-16 20:36:03 +00001228 if (!(kDontFlush_PixelOpsFlag & flags)) {
1229 this->flush();
1230 }
1231 // TODO: use scratch texture to perform conversion
1232 if (GrPixelConfigIsUnpremultiplied(texture->config()) !=
1233 GrPixelConfigIsUnpremultiplied(config)) {
1234 return;
1235 }
1236
1237 fGpu->writeTexturePixels(texture, left, top, width, height,
1238 config, buffer, rowBytes);
1239}
1240
1241bool GrContext::internalReadTexturePixels(GrTexture* texture,
1242 int left, int top,
1243 int width, int height,
1244 GrPixelConfig config,
1245 void* buffer,
1246 size_t rowBytes,
1247 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001248 SK_TRACE_EVENT0("GrContext::readTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001249 ASSERT_OWNED_RESOURCE(texture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001250
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001251 // TODO: code read pixels for textures that aren't also rendertargets
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001252 GrRenderTarget* target = texture->asRenderTarget();
1253 if (NULL != target) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001254 return this->internalReadRenderTargetPixels(target,
1255 left, top, width, height,
1256 config, buffer, rowBytes,
1257 flags);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001258 } else {
1259 return false;
1260 }
1261}
1262
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001263#include "SkConfig8888.h"
1264
1265namespace {
1266/**
1267 * Converts a GrPixelConfig to a SkCanvas::Config8888. Only byte-per-channel
1268 * formats are representable as Config8888 and so the function returns false
1269 * if the GrPixelConfig has no equivalent Config8888.
1270 */
1271bool grconfig_to_config8888(GrPixelConfig config,
1272 SkCanvas::Config8888* config8888) {
1273 switch (config) {
1274 case kRGBA_8888_PM_GrPixelConfig:
1275 *config8888 = SkCanvas::kRGBA_Premul_Config8888;
1276 return true;
1277 case kRGBA_8888_UPM_GrPixelConfig:
1278 *config8888 = SkCanvas::kRGBA_Unpremul_Config8888;
1279 return true;
1280 case kBGRA_8888_PM_GrPixelConfig:
1281 *config8888 = SkCanvas::kBGRA_Premul_Config8888;
1282 return true;
1283 case kBGRA_8888_UPM_GrPixelConfig:
1284 *config8888 = SkCanvas::kBGRA_Unpremul_Config8888;
1285 return true;
1286 default:
1287 return false;
1288 }
1289}
1290}
1291
bsalomon@google.com6f379512011-11-16 20:36:03 +00001292bool GrContext::internalReadRenderTargetPixels(GrRenderTarget* target,
1293 int left, int top,
1294 int width, int height,
1295 GrPixelConfig config,
1296 void* buffer,
1297 size_t rowBytes,
1298 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001299 SK_TRACE_EVENT0("GrContext::readRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001300 ASSERT_OWNED_RESOURCE(target);
1301
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001302 if (NULL == target) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001303 target = fDrawState->getRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001304 if (NULL == target) {
1305 return false;
1306 }
1307 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001308
bsalomon@google.com6f379512011-11-16 20:36:03 +00001309 if (!(kDontFlush_PixelOpsFlag & flags)) {
1310 this->flush();
1311 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001312
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001313 if (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1314 GrPixelConfigIsUnpremultiplied(config) &&
1315 !fGpu->canPreserveReadWriteUnpremulPixels()) {
1316 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1317 if (!grconfig_to_config8888(target->config(), &srcConfig8888) ||
1318 !grconfig_to_config8888(config, &dstConfig8888)) {
1319 return false;
1320 }
1321 // do read back using target's own config
1322 this->internalReadRenderTargetPixels(target,
1323 left, top,
1324 width, height,
1325 target->config(),
1326 buffer, rowBytes,
1327 kDontFlush_PixelOpsFlag);
1328 // sw convert the pixels to unpremul config
1329 uint32_t* pixels = reinterpret_cast<uint32_t*>(buffer);
1330 SkConvertConfig8888Pixels(pixels, rowBytes, dstConfig8888,
1331 pixels, rowBytes, srcConfig8888,
1332 width, height);
1333 return true;
1334 }
1335
bsalomon@google.comc4364992011-11-07 15:54:49 +00001336 GrTexture* src = target->asTexture();
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001337 bool swapRAndB = NULL != src &&
1338 fGpu->preferredReadPixelsConfig(config) ==
1339 GrPixelConfigSwapRAndB(config);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001340
1341 bool flipY = NULL != src &&
1342 fGpu->readPixelsWillPayForYFlip(target, left, top,
1343 width, height, config,
1344 rowBytes);
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001345 bool alphaConversion = (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1346 GrPixelConfigIsUnpremultiplied(config));
bsalomon@google.comc4364992011-11-07 15:54:49 +00001347
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001348 if (NULL == src && alphaConversion) {
1349 // we should fallback to cpu conversion here. This could happen when
1350 // we were given an external render target by the client that is not
1351 // also a texture (e.g. FBO 0 in GL)
1352 return false;
1353 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001354 // we draw to a scratch texture if any of these conversion are applied
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001355 GrAutoScratchTexture ast;
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001356 if (flipY || swapRAndB || alphaConversion) {
1357 GrAssert(NULL != src);
1358 if (swapRAndB) {
1359 config = GrPixelConfigSwapRAndB(config);
1360 GrAssert(kUnknown_GrPixelConfig != config);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001361 }
1362 // Make the scratch a render target because we don't have a robust
1363 // readTexturePixels as of yet (it calls this function).
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001364 GrTextureDesc desc;
1365 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1366 desc.fWidth = width;
1367 desc.fHeight = height;
1368 desc.fConfig = config;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001369
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001370 // When a full readback is faster than a partial we could always make
1371 // the scratch exactly match the passed rect. However, if we see many
1372 // different size rectangles we will trash our texture cache and pay the
1373 // cost of creating and destroying many textures. So, we only request
1374 // an exact match when the caller is reading an entire RT.
1375 ScratchTexMatch match = kApprox_ScratchTexMatch;
1376 if (0 == left &&
1377 0 == top &&
1378 target->width() == width &&
1379 target->height() == height &&
1380 fGpu->fullReadPixelsIsFasterThanPartial()) {
1381 match = kExact_ScratchTexMatch;
1382 }
1383 ast.set(this, desc, match);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001384 GrTexture* texture = ast.texture();
1385 if (!texture) {
1386 return false;
1387 }
1388 target = texture->asRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001389 GrAssert(NULL != target);
1390
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001391 GrDrawTarget::AutoStateRestore asr(fGpu,
1392 GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001393 GrDrawState* drawState = fGpu->drawState();
1394 drawState->setRenderTarget(target);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001395
bsalomon@google.comc4364992011-11-07 15:54:49 +00001396 GrMatrix matrix;
1397 if (flipY) {
1398 matrix.setTranslate(SK_Scalar1 * left,
1399 SK_Scalar1 * (top + height));
1400 matrix.set(GrMatrix::kMScaleY, -GR_Scalar1);
1401 } else {
1402 matrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
1403 }
1404 matrix.postIDiv(src->width(), src->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001405 drawState->sampler(0)->reset(matrix);
1406 drawState->sampler(0)->setRAndBSwap(swapRAndB);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001407 drawState->createTextureEffect(0, src);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001408 GrRect rect;
1409 rect.setXYWH(0, 0, SK_Scalar1 * width, SK_Scalar1 * height);
bsalomon@google.come3d32162012-07-20 13:37:06 +00001410 fGpu->drawSimpleRect(rect, NULL);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001411 left = 0;
1412 top = 0;
1413 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001414 return fGpu->readPixels(target,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001415 left, top, width, height,
1416 config, buffer, rowBytes, flipY);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001417}
1418
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001419void GrContext::resolveRenderTarget(GrRenderTarget* target) {
1420 GrAssert(target);
1421 ASSERT_OWNED_RESOURCE(target);
1422 // In the future we may track whether there are any pending draws to this
1423 // target. We don't today so we always perform a flush. We don't promise
1424 // this to our clients, though.
1425 this->flush();
1426 fGpu->resolveRenderTarget(target);
1427}
1428
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001429void GrContext::copyTexture(GrTexture* src, GrRenderTarget* dst) {
1430 if (NULL == src || NULL == dst) {
1431 return;
1432 }
1433 ASSERT_OWNED_RESOURCE(src);
1434
twiz@google.com1ac87ff2012-04-27 19:39:33 +00001435 // Writes pending to the source texture are not tracked, so a flush
1436 // is required to ensure that the copy captures the most recent contents
1437 // of the source texture. See similar behaviour in
1438 // GrContext::resolveRenderTarget.
1439 this->flush();
1440
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001441 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001442 GrDrawState* drawState = fGpu->drawState();
1443 drawState->setRenderTarget(dst);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001444 GrMatrix sampleM;
1445 sampleM.setIDiv(src->width(), src->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001446 drawState->sampler(0)->reset(sampleM);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001447 drawState->createTextureEffect(0, src);
bsalomon@google.com5db3b6c2012-01-12 20:38:57 +00001448 SkRect rect = SkRect::MakeXYWH(0, 0,
1449 SK_Scalar1 * src->width(),
1450 SK_Scalar1 * src->height());
bsalomon@google.come3d32162012-07-20 13:37:06 +00001451 fGpu->drawSimpleRect(rect, NULL);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001452}
1453
bsalomon@google.com6f379512011-11-16 20:36:03 +00001454void GrContext::internalWriteRenderTargetPixels(GrRenderTarget* target,
1455 int left, int top,
1456 int width, int height,
1457 GrPixelConfig config,
1458 const void* buffer,
1459 size_t rowBytes,
1460 uint32_t flags) {
1461 SK_TRACE_EVENT0("GrContext::writeRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001462 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com6f379512011-11-16 20:36:03 +00001463
1464 if (NULL == target) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001465 target = fDrawState->getRenderTarget();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001466 if (NULL == target) {
1467 return;
1468 }
1469 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001470
1471 // TODO: when underlying api has a direct way to do this we should use it
1472 // (e.g. glDrawPixels on desktop GL).
1473
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001474 // If the RT is also a texture and we don't have to do PM/UPM conversion
1475 // then take the texture path, which we expect to be at least as fast or
1476 // faster since it doesn't use an intermediate texture as we do below.
1477
1478#if !GR_MAC_BUILD
1479 // At least some drivers on the Mac get confused when glTexImage2D is called
1480 // on a texture attached to an FBO. The FBO still sees the old image. TODO:
1481 // determine what OS versions and/or HW is affected.
1482 if (NULL != target->asTexture() &&
1483 GrPixelConfigIsUnpremultiplied(target->config()) ==
1484 GrPixelConfigIsUnpremultiplied(config)) {
1485
1486 this->internalWriteTexturePixels(target->asTexture(),
1487 left, top, width, height,
1488 config, buffer, rowBytes, flags);
1489 return;
1490 }
1491#endif
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001492 if (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1493 GrPixelConfigIsUnpremultiplied(config) &&
1494 !fGpu->canPreserveReadWriteUnpremulPixels()) {
1495 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1496 if (!grconfig_to_config8888(config, &srcConfig8888) ||
1497 !grconfig_to_config8888(target->config(), &dstConfig8888)) {
1498 return;
1499 }
1500 // allocate a tmp buffer and sw convert the pixels to premul
1501 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(width * height);
1502 const uint32_t* src = reinterpret_cast<const uint32_t*>(buffer);
1503 SkConvertConfig8888Pixels(tmpPixels.get(), 4 * width, dstConfig8888,
1504 src, rowBytes, srcConfig8888,
1505 width, height);
1506 // upload the already premul pixels
1507 this->internalWriteRenderTargetPixels(target,
1508 left, top,
1509 width, height,
1510 target->config(),
1511 tmpPixels, 4 * width, flags);
1512 return;
1513 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001514
1515 bool swapRAndB = fGpu->preferredReadPixelsConfig(config) ==
1516 GrPixelConfigSwapRAndB(config);
1517 if (swapRAndB) {
1518 config = GrPixelConfigSwapRAndB(config);
1519 }
1520
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001521 GrTextureDesc desc;
1522 desc.fWidth = width;
1523 desc.fHeight = height;
1524 desc.fConfig = config;
1525
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001526 GrAutoScratchTexture ast(this, desc);
1527 GrTexture* texture = ast.texture();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001528 if (NULL == texture) {
1529 return;
1530 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001531 this->internalWriteTexturePixels(texture, 0, 0, width, height,
1532 config, buffer, rowBytes, flags);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001533
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001534 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001535 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001536
1537 GrMatrix matrix;
1538 matrix.setTranslate(GrIntToScalar(left), GrIntToScalar(top));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001539 drawState->setViewMatrix(matrix);
1540 drawState->setRenderTarget(target);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001541
bsalomon@google.com5c638652011-07-18 19:31:59 +00001542 matrix.setIDiv(texture->width(), texture->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001543 drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode,
1544 GrSamplerState::kNearest_Filter,
1545 matrix);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001546 drawState->createTextureEffect(0, texture);
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001547 drawState->sampler(0)->setRAndBSwap(swapRAndB);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001548
tomhudson@google.comb213ed82012-06-25 15:22:12 +00001549 static const GrVertexLayout layout = 0;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001550 static const int VCOUNT = 4;
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001551 // TODO: Use GrGpu::drawRect here
bsalomon@google.com27847de2011-02-22 20:59:41 +00001552 GrDrawTarget::AutoReleaseGeometry geo(fGpu, layout, VCOUNT, 0);
1553 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001554 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +00001555 return;
1556 }
1557 ((GrPoint*)geo.vertices())->setIRectFan(0, 0, width, height);
bsalomon@google.com47059542012-06-06 20:51:20 +00001558 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, VCOUNT);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001559}
1560////////////////////////////////////////////////////////////////////////////////
1561
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001562void GrContext::setPaint(const GrPaint& paint) {
tomhudson@google.comcb325ce2012-07-11 14:41:19 +00001563 GrAssert(fDrawState->stagesDisabled());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001564
1565 for (int i = 0; i < GrPaint::kMaxTextures; ++i) {
1566 int s = i + GrPaint::kFirstTextureStage;
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001567 ASSERT_OWNED_RESOURCE(paint.getTexture(i));
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001568 if (paint.isTextureStageEnabled(i)) {
1569 fDrawState->setTexture(s, paint.getTexture(i));
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001570 *fDrawState->sampler(s) = paint.getTextureSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001571 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001572 }
1573
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001574 fDrawState->setFirstCoverageStage(GrPaint::kFirstMaskStage);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001575
1576 for (int i = 0; i < GrPaint::kMaxMasks; ++i) {
1577 int s = i + GrPaint::kFirstMaskStage;
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001578 ASSERT_OWNED_RESOURCE(paint.getMask(i));
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001579 if (paint.isMaskStageEnabled(i)) {
1580 fDrawState->setTexture(s, paint.getMask(i));
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001581 *fDrawState->sampler(s) = paint.getMaskSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001582 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001583 }
bsalomon@google.com26936d02012-03-19 13:06:19 +00001584
1585 // disable all stages not accessible via the paint
1586 for (int s = GrPaint::kTotalStages; s < GrDrawState::kNumStages; ++s) {
tomhudson@google.com676e6602012-07-10 17:21:48 +00001587 fDrawState->disableStage(s);
bsalomon@google.com26936d02012-03-19 13:06:19 +00001588 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001589
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001590 fDrawState->setColor(paint.fColor);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001591
1592 if (paint.fDither) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001593 fDrawState->enableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001594 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001595 fDrawState->disableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001596 }
1597 if (paint.fAntiAlias) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001598 fDrawState->enableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001599 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001600 fDrawState->disableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001601 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001602 if (paint.fColorMatrixEnabled) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001603 fDrawState->enableState(GrDrawState::kColorMatrix_StateBit);
1604 fDrawState->setColorMatrix(paint.fColorMatrix);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001605 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001606 fDrawState->disableState(GrDrawState::kColorMatrix_StateBit);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001607 }
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001608 fDrawState->setBlendFunc(paint.fSrcBlendCoeff, paint.fDstBlendCoeff);
1609 fDrawState->setColorFilter(paint.fColorFilterColor, paint.fColorFilterXfermode);
1610 fDrawState->setCoverage(paint.fCoverage);
reed@google.com4b2d3f32012-05-15 18:05:50 +00001611#if GR_DEBUG_PARTIAL_COVERAGE_CHECK
bsalomon@google.come3d32162012-07-20 13:37:06 +00001612 if ((paint.hasMask() || 0xff != paint.fCoverage) &&
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001613 !fGpu->canApplyCoverage()) {
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001614 GrPrintf("Partial pixel coverage will be incorrectly blended.\n");
1615 }
bsalomon@google.com95cd7bd2012-03-28 15:35:05 +00001616#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +00001617}
1618
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001619GrDrawTarget* GrContext::prepareToDraw(const GrPaint& paint,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001620 DrawCategory category) {
1621 if (category != fLastDrawCategory) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001622 this->flushDrawBuffer();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001623 fLastDrawCategory = category;
1624 }
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001625 this->setPaint(paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001626 GrDrawTarget* target = fGpu;
1627 switch (category) {
bsalomon@google.com193395c2012-03-30 17:35:12 +00001628 case kUnbuffered_DrawCategory:
1629 target = fGpu;
1630 break;
1631 case kBuffered_DrawCategory:
1632 target = fDrawBuffer;
1633 fDrawBuffer->setClip(fGpu->getClip());
1634 break;
1635 default:
1636 GrCrash("Unexpected DrawCategory.");
1637 break;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001638 }
1639 return target;
1640}
1641
robertphillips@google.com72176b22012-05-23 13:19:12 +00001642/*
1643 * This method finds a path renderer that can draw the specified path on
1644 * the provided target.
1645 * Due to its expense, the software path renderer has split out so it can
1646 * can be individually allowed/disallowed via the "allowSW" boolean.
1647 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001648GrPathRenderer* GrContext::getPathRenderer(const SkPath& path,
bsalomon@google.com289533a2011-10-27 12:34:25 +00001649 GrPathFill fill,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001650 const GrDrawTarget* target,
robertphillips@google.com72176b22012-05-23 13:19:12 +00001651 bool antiAlias,
1652 bool allowSW) {
bsalomon@google.com30085192011-08-19 15:42:31 +00001653 if (NULL == fPathRendererChain) {
1654 fPathRendererChain =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001655 SkNEW_ARGS(GrPathRendererChain,
1656 (this, GrPathRendererChain::kNone_UsageFlag));
bsalomon@google.com30085192011-08-19 15:42:31 +00001657 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001658
1659 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(path, fill,
1660 target,
1661 antiAlias);
1662
1663 if (NULL == pr && allowSW) {
1664 if (NULL == fSoftwarePathRenderer) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001665 fSoftwarePathRenderer = SkNEW_ARGS(GrSoftwarePathRenderer, (this));
robertphillips@google.com72176b22012-05-23 13:19:12 +00001666 }
1667
1668 pr = fSoftwarePathRenderer;
1669 }
1670
1671 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001672}
1673
bsalomon@google.com27847de2011-02-22 20:59:41 +00001674////////////////////////////////////////////////////////////////////////////////
1675
bsalomon@google.com27847de2011-02-22 20:59:41 +00001676void GrContext::setRenderTarget(GrRenderTarget* target) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001677 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001678 if (fDrawState->getRenderTarget() != target) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001679 this->flush(false);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001680 fDrawState->setRenderTarget(target);
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001681 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001682}
1683
1684GrRenderTarget* GrContext::getRenderTarget() {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001685 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001686}
1687
1688const GrRenderTarget* GrContext::getRenderTarget() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001689 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001690}
1691
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001692bool GrContext::isConfigRenderable(GrPixelConfig config) const {
1693 return fGpu->isConfigRenderable(config);
1694}
1695
bsalomon@google.com27847de2011-02-22 20:59:41 +00001696const GrMatrix& GrContext::getMatrix() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001697 return fDrawState->getViewMatrix();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001698}
1699
1700void GrContext::setMatrix(const GrMatrix& m) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001701 fDrawState->setViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001702}
1703
1704void GrContext::concatMatrix(const GrMatrix& m) const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001705 fDrawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001706}
1707
1708static inline intptr_t setOrClear(intptr_t bits, int shift, intptr_t pred) {
1709 intptr_t mask = 1 << shift;
1710 if (pred) {
1711 bits |= mask;
1712 } else {
1713 bits &= ~mask;
1714 }
1715 return bits;
1716}
1717
bsalomon@google.com583a1e32011-08-17 13:42:46 +00001718GrContext::GrContext(GrGpu* gpu) {
bsalomon@google.comc0af3172012-06-15 14:10:09 +00001719 ++THREAD_INSTANCE_COUNT;
1720
bsalomon@google.com27847de2011-02-22 20:59:41 +00001721 fGpu = gpu;
1722 fGpu->ref();
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001723 fGpu->setContext(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001724
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001725 fDrawState = SkNEW(GrDrawState);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001726 fGpu->setDrawState(fDrawState);
1727
bsalomon@google.com30085192011-08-19 15:42:31 +00001728 fPathRendererChain = NULL;
robertphillips@google.com72176b22012-05-23 13:19:12 +00001729 fSoftwarePathRenderer = NULL;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001730
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001731 fTextureCache = SkNEW_ARGS(GrResourceCache,
1732 (MAX_TEXTURE_CACHE_COUNT,
1733 MAX_TEXTURE_CACHE_BYTES));
1734 fFontCache = SkNEW_ARGS(GrFontCache, (fGpu));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001735
1736 fLastDrawCategory = kUnbuffered_DrawCategory;
1737
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001738 fDrawBuffer = NULL;
1739 fDrawBufferVBAllocPool = NULL;
1740 fDrawBufferIBAllocPool = NULL;
1741
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001742 fAARectRenderer = SkNEW(GrAARectRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001743
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001744 this->setupDrawBuffer();
1745}
1746
1747void GrContext::setupDrawBuffer() {
1748
1749 GrAssert(NULL == fDrawBuffer);
1750 GrAssert(NULL == fDrawBufferVBAllocPool);
1751 GrAssert(NULL == fDrawBufferIBAllocPool);
1752
bsalomon@google.com92edd312012-04-04 21:40:21 +00001753#if DEFER_TEXT_RENDERING || BATCH_RECT_TO_RECT || DEFER_PATHS
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001754 fDrawBufferVBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001755 SkNEW_ARGS(GrVertexBufferAllocPool, (fGpu, false,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001756 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001757 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS));
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001758 fDrawBufferIBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001759 SkNEW_ARGS(GrIndexBufferAllocPool, (fGpu, false,
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001760 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001761 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001762
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001763 fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (fGpu,
bsalomon@google.com471d4712011-08-23 15:45:25 +00001764 fDrawBufferVBAllocPool,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001765 fDrawBufferIBAllocPool));
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001766#endif
1767
1768#if BATCH_RECT_TO_RECT
bsalomon@google.com27847de2011-02-22 20:59:41 +00001769 fDrawBuffer->setQuadIndexBuffer(this->getQuadIndexBuffer());
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001770#endif
bsalomon@google.com1015e032012-06-25 18:41:04 +00001771 if (fDrawBuffer) {
1772 fDrawBuffer->setAutoFlushTarget(fGpu);
1773 fDrawBuffer->setDrawState(fDrawState);
1774 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001775}
1776
bsalomon@google.com27847de2011-02-22 20:59:41 +00001777GrDrawTarget* GrContext::getTextTarget(const GrPaint& paint) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001778#if DEFER_TEXT_RENDERING
bsalomon@google.com193395c2012-03-30 17:35:12 +00001779 return prepareToDraw(paint, kBuffered_DrawCategory);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001780#else
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001781 return prepareToDraw(paint, kUnbuffered_DrawCategory);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001782#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +00001783}
1784
1785const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {
1786 return fGpu->getQuadIndexBuffer();
1787}
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001788
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001789GrTexture* GrContext::gaussianBlur(GrTexture* srcTexture,
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001790 bool canClobberSrc,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001791 const SkRect& rect,
1792 float sigmaX, float sigmaY) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001793 ASSERT_OWNED_RESOURCE(srcTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001794 GrRenderTarget* oldRenderTarget = this->getRenderTarget();
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001795 AutoMatrix avm(this, GrMatrix::I());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001796 SkIRect clearRect;
bsalomon@google.comb505a122012-05-31 18:40:36 +00001797 int scaleFactorX, radiusX;
1798 int scaleFactorY, radiusY;
1799 sigmaX = adjust_sigma(sigmaX, &scaleFactorX, &radiusX);
1800 sigmaY = adjust_sigma(sigmaY, &scaleFactorY, &radiusY);
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001801
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001802 SkRect srcRect(rect);
1803 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
1804 srcRect.roundOut();
robertphillips@google.com8637a362012-04-10 18:32:35 +00001805 scale_rect(&srcRect, static_cast<float>(scaleFactorX),
1806 static_cast<float>(scaleFactorY));
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +00001807
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001808 AutoClip acs(this, srcRect);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001809
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001810 GrAssert(kBGRA_8888_PM_GrPixelConfig == srcTexture->config() ||
1811 kRGBA_8888_PM_GrPixelConfig == srcTexture->config() ||
1812 kAlpha_8_GrPixelConfig == srcTexture->config());
1813
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001814 GrTextureDesc desc;
1815 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
1816 desc.fWidth = SkScalarFloorToInt(srcRect.width());
1817 desc.fHeight = SkScalarFloorToInt(srcRect.height());
1818 desc.fConfig = srcTexture->config();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001819
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001820 GrAutoScratchTexture temp1, temp2;
1821 GrTexture* dstTexture = temp1.set(this, desc);
1822 GrTexture* tempTexture = canClobberSrc ? srcTexture : temp2.set(this, desc);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001823
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001824 GrPaint paint;
1825 paint.reset();
1826 paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
1827
1828 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
1829 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1830 srcTexture->height());
1831 this->setRenderTarget(dstTexture->asRenderTarget());
1832 SkRect dstRect(srcRect);
1833 scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
1834 i < scaleFactorY ? 0.5f : 1.0f);
tomhudson@google.comaa72eab2012-07-19 18:01:07 +00001835 paint.textureSampler(0)->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect,
1836 (srcTexture)))->unref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001837 this->drawRectToRect(paint, dstRect, srcRect);
1838 srcRect = dstRect;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001839 srcTexture = dstTexture;
1840 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001841 }
1842
robertphillips@google.com7a396332012-05-10 15:11:27 +00001843 SkIRect srcIRect;
1844 srcRect.roundOut(&srcIRect);
1845
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001846 if (sigmaX > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001847 if (scaleFactorX > 1) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001848 // Clear out a radius to the right of the srcRect to prevent the
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001849 // X convolution from reading garbage.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001850 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001851 radiusX, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001852 this->clear(&clearRect, 0x0);
1853 }
1854
1855 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001856 convolve_gaussian(fGpu, srcTexture, srcRect, sigmaX, radiusX,
1857 Gr1DKernelEffect::kX_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001858 srcTexture = dstTexture;
1859 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001860 }
1861
1862 if (sigmaY > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001863 if (scaleFactorY > 1 || sigmaX > 0.0f) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001864 // Clear out a radius below the srcRect to prevent the Y
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001865 // convolution from reading garbage.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001866 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001867 srcIRect.width(), radiusY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001868 this->clear(&clearRect, 0x0);
1869 }
1870
1871 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001872 convolve_gaussian(fGpu, srcTexture, srcRect, sigmaY, radiusY,
1873 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001874 srcTexture = dstTexture;
1875 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001876 }
1877
1878 if (scaleFactorX > 1 || scaleFactorY > 1) {
1879 // Clear one pixel to the right and below, to accommodate bilinear
1880 // upsampling.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001881 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
1882 srcIRect.width() + 1, 1);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001883 this->clear(&clearRect, 0x0);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001884 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
1885 1, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001886 this->clear(&clearRect, 0x0);
1887 // FIXME: This should be mitchell, not bilinear.
1888 paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
1889 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1890 srcTexture->height());
1891 this->setRenderTarget(dstTexture->asRenderTarget());
tomhudson@google.comaa72eab2012-07-19 18:01:07 +00001892 paint.textureSampler(0)->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect,
1893 (srcTexture)))->unref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001894 SkRect dstRect(srcRect);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001895 scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001896 this->drawRectToRect(paint, dstRect, srcRect);
1897 srcRect = dstRect;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001898 srcTexture = dstTexture;
1899 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001900 }
1901 this->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001902 if (srcTexture == temp1.texture()) {
1903 return temp1.detach();
1904 } else if (srcTexture == temp2.texture()) {
1905 return temp2.detach();
1906 } else {
1907 srcTexture->ref();
1908 return srcTexture;
1909 }
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001910}
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001911
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001912GrTexture* GrContext::applyMorphology(GrTexture* srcTexture,
1913 const GrRect& rect,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001914 MorphologyType morphType,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001915 SkISize radius) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001916 ASSERT_OWNED_RESOURCE(srcTexture);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001917 srcTexture->ref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001918 GrRenderTarget* oldRenderTarget = this->getRenderTarget();
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +00001919
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001920 AutoMatrix avm(this, GrMatrix::I());
1921
1922 AutoClip acs(this, GrRect::MakeWH(SkIntToScalar(srcTexture->width()),
1923 SkIntToScalar(srcTexture->height())));
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001924 GrTextureDesc desc;
1925 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
1926 desc.fWidth = SkScalarCeilToInt(rect.width());
1927 desc.fHeight = SkScalarCeilToInt(rect.height());
1928 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001929 if (radius.fWidth > 0) {
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001930 GrAutoScratchTexture ast(this, desc);
1931 this->setRenderTarget(ast.texture()->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001932 apply_morphology(fGpu, srcTexture, rect, radius.fWidth, morphType,
1933 Gr1DKernelEffect::kX_Direction);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001934 SkIRect clearRect = SkIRect::MakeXYWH(
1935 SkScalarFloorToInt(rect.fLeft),
1936 SkScalarFloorToInt(rect.fBottom),
1937 SkScalarFloorToInt(rect.width()),
1938 radius.fHeight);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001939 this->clear(&clearRect, 0x0);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001940 srcTexture->unref();
1941 srcTexture = ast.detach();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001942 }
1943 if (radius.fHeight > 0) {
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001944 GrAutoScratchTexture ast(this, desc);
1945 this->setRenderTarget(ast.texture()->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001946 apply_morphology(fGpu, srcTexture, rect, radius.fHeight, morphType,
1947 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001948 srcTexture->unref();
1949 srcTexture = ast.detach();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001950 }
1951 this->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001952 return srcTexture;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001953}
bsalomon@google.comc4364992011-11-07 15:54:49 +00001954
1955///////////////////////////////////////////////////////////////////////////////