blob: fc2b143c606787cf78de6af5a39c43c233670809 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
bsalomon@google.com27847de2011-02-22 20:59:41 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
bsalomon@google.com27847de2011-02-22 20:59:41 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
bsalomon@google.com1fadb202011-12-12 16:10:08 +000010#include "GrContext.h"
11
bsalomon@google.comb505a122012-05-31 18:40:36 +000012#include "effects/GrMorphologyEffect.h"
13#include "effects/GrConvolutionEffect.h"
14
tomhudson@google.com278cbb42011-06-30 19:37:01 +000015#include "GrBufferAllocPool.h"
16#include "GrClipIterator.h"
bsalomon@google.com05ef5102011-05-02 21:14:59 +000017#include "GrGpu.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000018#include "GrIndexBuffer.h"
19#include "GrInOrderDrawBuffer.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000020#include "GrPathRenderer.h"
tomhudson@google.comd22b6e42011-06-24 15:53:40 +000021#include "GrPathUtils.h"
bsalomon@google.com50398bf2011-07-26 20:45:30 +000022#include "GrResourceCache.h"
robertphillips@google.com72176b22012-05-23 13:19:12 +000023#include "GrSoftwarePathRenderer.h"
bsalomon@google.com558a75b2011-08-08 17:01:14 +000024#include "GrStencilBuffer.h"
tomhudson@google.com278cbb42011-06-30 19:37:01 +000025#include "GrTextStrike.h"
bsalomon@google.com8c2fe992011-09-13 15:27:18 +000026#include "SkTLazy.h"
bsalomon@google.comc0af3172012-06-15 14:10:09 +000027#include "SkTLS.h"
tomhudson@google.com0c8d93a2011-07-01 17:08:26 +000028#include "SkTrace.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000029
bsalomon@google.com3c4d0322012-04-03 18:04:51 +000030#define DEFER_TEXT_RENDERING 1
bsalomon@google.com27847de2011-02-22 20:59:41 +000031
bsalomon@google.com3c4d0322012-04-03 18:04:51 +000032#define DEFER_PATHS 1
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +000033
bsalomon@google.com3c4d0322012-04-03 18:04:51 +000034#define BATCH_RECT_TO_RECT (1 && !GR_STATIC_RECT_VB)
bsalomon@google.com27847de2011-02-22 20:59:41 +000035
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +000036#define MAX_BLUR_SIGMA 4.0f
37
bsalomon@google.comd46e2422011-09-23 17:40:07 +000038// When we're using coverage AA but the blend is incompatible (given gpu
39// limitations) should we disable AA or draw wrong?
bsalomon@google.com950d7a82011-09-28 15:05:33 +000040#define DISABLE_COVERAGE_AA_FOR_BLEND 1
bsalomon@google.comd46e2422011-09-23 17:40:07 +000041
reed@google.com4b2d3f32012-05-15 18:05:50 +000042#if GR_DEBUG
43 // change this to a 1 to see notifications when partial coverage fails
44 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
45#else
46 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
47#endif
48
bsalomon@google.com8f7e1da2012-06-21 19:48:32 +000049static const size_t kDefaultTextureCacheBudget = 16 * 1024 * 1024;
bsalomon@google.com27847de2011-02-22 20:59:41 +000050
bsalomon@google.com60361492012-03-15 17:47:06 +000051static const size_t DRAW_BUFFER_VBPOOL_BUFFER_SIZE = 1 << 15;
bsalomon@google.com27847de2011-02-22 20:59:41 +000052static const int DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS = 4;
53
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +000054// path rendering is the only thing we defer today that uses non-static indices
55static const size_t DRAW_BUFFER_IBPOOL_BUFFER_SIZE = DEFER_PATHS ? 1 << 11 : 0;
56static const int DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS = DEFER_PATHS ? 4 : 0;
bsalomon@google.com27847de2011-02-22 20:59:41 +000057
bsalomon@google.combc4b6542011-11-19 13:56:11 +000058#define ASSERT_OWNED_RESOURCE(R) GrAssert(!(R) || (R)->getContext() == this)
59
bsalomon@google.com05ef5102011-05-02 21:14:59 +000060GrContext* GrContext::Create(GrEngine engine,
61 GrPlatform3DContext context3D) {
bsalomon@google.com27847de2011-02-22 20:59:41 +000062 GrContext* ctx = NULL;
63 GrGpu* fGpu = GrGpu::Create(engine, context3D);
64 if (NULL != fGpu) {
65 ctx = new GrContext(fGpu);
66 fGpu->unref();
67 }
68 return ctx;
69}
70
bsalomon@google.comc0af3172012-06-15 14:10:09 +000071namespace {
72void* CreateThreadInstanceCount() {
73 return new int(0);
74}
75void DeleteThreadInstanceCount(void* v) {
76 delete reinterpret_cast<int*>(v);
77}
78#define THREAD_INSTANCE_COUNT \
79 (*reinterpret_cast<int*>(SkTLS::Get(CreateThreadInstanceCount, \
80 DeleteThreadInstanceCount)))
81
82}
83
84int GrContext::GetThreadInstanceCount() {
85 return THREAD_INSTANCE_COUNT;
86}
87
bsalomon@google.com27847de2011-02-22 20:59:41 +000088GrContext::~GrContext() {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000089 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +000090
91 // Since the gpu can hold scratch textures, give it a chance to let go
92 // of them before freeing the texture cache
93 fGpu->purgeResources();
94
bsalomon@google.com27847de2011-02-22 20:59:41 +000095 delete fTextureCache;
96 delete fFontCache;
97 delete fDrawBuffer;
98 delete fDrawBufferVBAllocPool;
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +000099 delete fDrawBufferIBAllocPool;
bsalomon@google.com30085192011-08-19 15:42:31 +0000100
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000101 fAARectRenderer->unref();
102
bsalomon@google.com205d4602011-04-25 12:43:45 +0000103 fGpu->unref();
bsalomon@google.com30085192011-08-19 15:42:31 +0000104 GrSafeUnref(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000105 GrSafeUnref(fSoftwarePathRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000106 fDrawState->unref();
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000107
108 --THREAD_INSTANCE_COUNT;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000109}
110
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000111void GrContext::contextLost() {
junov@google.com53a55842011-06-08 22:55:10 +0000112 contextDestroyed();
113 this->setupDrawBuffer();
114}
115
116void GrContext::contextDestroyed() {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000117 // abandon first to so destructors
118 // don't try to free the resources in the API.
119 fGpu->abandonResources();
120
bsalomon@google.com30085192011-08-19 15:42:31 +0000121 // a path renderer may be holding onto resources that
122 // are now unusable
123 GrSafeSetNull(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000124 GrSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com30085192011-08-19 15:42:31 +0000125
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000126 delete fDrawBuffer;
127 fDrawBuffer = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000128
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000129 delete fDrawBufferVBAllocPool;
130 fDrawBufferVBAllocPool = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000131
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000132 delete fDrawBufferIBAllocPool;
133 fDrawBufferIBAllocPool = NULL;
134
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000135 fAARectRenderer->reset();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000136
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000137 fTextureCache->removeAll();
138 fFontCache->freeAll();
139 fGpu->markContextDirty();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000140}
141
142void GrContext::resetContext() {
143 fGpu->markContextDirty();
144}
145
146void GrContext::freeGpuResources() {
147 this->flush();
robertphillips@google.comff175842012-05-14 19:31:39 +0000148
149 fGpu->purgeResources();
150
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000151 fAARectRenderer->reset();
152
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000153 fTextureCache->removeAll();
154 fFontCache->freeAll();
bsalomon@google.com30085192011-08-19 15:42:31 +0000155 // a path renderer may be holding onto resources
156 GrSafeSetNull(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000157 GrSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000158}
159
twiz@google.com05e70242012-01-27 19:12:00 +0000160size_t GrContext::getGpuTextureCacheBytes() const {
161 return fTextureCache->getCachedResourceBytes();
162}
163
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000164////////////////////////////////////////////////////////////////////////////////
165
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000166int GrContext::PaintStageVertexLayoutBits(
167 const GrPaint& paint,
168 const bool hasTexCoords[GrPaint::kTotalStages]) {
169 int stageMask = paint.getActiveStageMask();
170 int layout = 0;
171 for (int i = 0; i < GrPaint::kTotalStages; ++i) {
172 if ((1 << i) & stageMask) {
173 if (NULL != hasTexCoords && hasTexCoords[i]) {
174 layout |= GrDrawTarget::StageTexCoordVertexLayoutBit(i, i);
175 } else {
176 layout |= GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(i);
177 }
178 }
179 }
180 return layout;
181}
182
183
184////////////////////////////////////////////////////////////////////////////////
185
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000186GrTexture* GrContext::TextureCacheEntry::texture() const {
187 if (NULL == fEntry) {
188 return NULL;
189 } else {
190 return (GrTexture*) fEntry->resource();
191 }
192}
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000193
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000194namespace {
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000195
196// we should never have more than one stencil buffer with same combo of
197// (width,height,samplecount)
198void gen_stencil_key_values(int width, int height,
199 int sampleCnt, uint32_t v[4]) {
200 v[0] = width;
201 v[1] = height;
202 v[2] = sampleCnt;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000203 v[3] = GrResourceKey::kStencilBuffer_TypeBit;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000204}
205
206void gen_stencil_key_values(const GrStencilBuffer* sb,
207 uint32_t v[4]) {
208 gen_stencil_key_values(sb->width(), sb->height(),
209 sb->numSamples(), v);
210}
bsalomon@google.com82c7bd82011-11-09 15:32:29 +0000211
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000212void scale_rect(SkRect* rect, float xScale, float yScale) {
robertphillips@google.com5af56062012-04-27 15:39:52 +0000213 rect->fLeft = SkScalarMul(rect->fLeft, SkFloatToScalar(xScale));
214 rect->fTop = SkScalarMul(rect->fTop, SkFloatToScalar(yScale));
215 rect->fRight = SkScalarMul(rect->fRight, SkFloatToScalar(xScale));
216 rect->fBottom = SkScalarMul(rect->fBottom, SkFloatToScalar(yScale));
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000217}
218
bsalomon@google.comb505a122012-05-31 18:40:36 +0000219float adjust_sigma(float sigma, int *scaleFactor, int *radius) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000220 *scaleFactor = 1;
221 while (sigma > MAX_BLUR_SIGMA) {
222 *scaleFactor *= 2;
223 sigma *= 0.5f;
224 }
bsalomon@google.comb505a122012-05-31 18:40:36 +0000225 *radius = static_cast<int>(ceilf(sigma * 3.0f));
226 GrAssert(*radius <= GrConvolutionEffect::kMaxKernelRadius);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000227 return sigma;
228}
229
230void apply_morphology(GrGpu* gpu,
231 GrTexture* texture,
232 const SkRect& rect,
233 int radius,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000234 GrContext::MorphologyType morphType,
235 Gr1DKernelEffect::Direction direction) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000236
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000237 GrRenderTarget* target = gpu->drawState()->getRenderTarget();
238 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000239 GrDrawState* drawState = gpu->drawState();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000240 drawState->setRenderTarget(target);
241 GrMatrix sampleM;
242 sampleM.setIDiv(texture->width(), texture->height());
bsalomon@google.comb505a122012-05-31 18:40:36 +0000243 drawState->sampler(0)->reset(sampleM);
244 SkAutoTUnref<GrCustomStage> morph(
245 new GrMorphologyEffect(direction, radius, morphType));
246 drawState->sampler(0)->setCustomStage(morph);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000247 drawState->setTexture(0, texture);
248 gpu->drawSimpleRect(rect, NULL, 1 << 0);
249}
250
bsalomon@google.comb505a122012-05-31 18:40:36 +0000251void convolve_gaussian(GrGpu* gpu,
252 GrTexture* texture,
253 const SkRect& rect,
254 float sigma,
255 int radius,
256 Gr1DKernelEffect::Direction direction) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000257 GrRenderTarget* target = gpu->drawState()->getRenderTarget();
258 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000259 GrDrawState* drawState = gpu->drawState();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000260 drawState->setRenderTarget(target);
261 GrMatrix sampleM;
262 sampleM.setIDiv(texture->width(), texture->height());
bsalomon@google.comb505a122012-05-31 18:40:36 +0000263 drawState->sampler(0)->reset(sampleM);
264 SkAutoTUnref<GrConvolutionEffect> conv(new
265 GrConvolutionEffect(direction, radius));
266 conv->setGaussianKernel(sigma);
267 drawState->sampler(0)->setCustomStage(conv);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000268 drawState->setTexture(0, texture);
269 gpu->drawSimpleRect(rect, NULL, 1 << 0);
270}
271
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000272}
273
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000274GrContext::TextureCacheEntry GrContext::findAndLockTexture(
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000275 const GrTextureDesc& desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000276 const GrSamplerState* sampler) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000277 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, sampler, desc, false);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000278 return TextureCacheEntry(fTextureCache->findAndLock(resourceKey,
279 GrResourceCache::kNested_LockType));
280}
281
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000282bool GrContext::isTextureInCache(const GrTextureDesc& desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000283 const GrSamplerState* sampler) const {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000284 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, sampler, desc, false);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000285 return fTextureCache->hasKey(resourceKey);
286}
287
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000288GrResourceEntry* GrContext::addAndLockStencilBuffer(GrStencilBuffer* sb) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000289 ASSERT_OWNED_RESOURCE(sb);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000290 uint32_t v[4];
291 gen_stencil_key_values(sb, v);
292 GrResourceKey resourceKey(v);
293 return fTextureCache->createAndLock(resourceKey, sb);
294}
295
296GrStencilBuffer* GrContext::findStencilBuffer(int width, int height,
297 int sampleCnt) {
298 uint32_t v[4];
299 gen_stencil_key_values(width, height, sampleCnt, v);
300 GrResourceKey resourceKey(v);
301 GrResourceEntry* entry = fTextureCache->findAndLock(resourceKey,
302 GrResourceCache::kSingle_LockType);
303 if (NULL != entry) {
304 GrStencilBuffer* sb = (GrStencilBuffer*) entry->resource();
305 return sb;
306 } else {
307 return NULL;
308 }
309}
310
311void GrContext::unlockStencilBuffer(GrResourceEntry* sbEntry) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000312 ASSERT_OWNED_RESOURCE(sbEntry->resource());
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000313 fTextureCache->unlock(sbEntry);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000314}
315
316static void stretchImage(void* dst,
317 int dstW,
318 int dstH,
319 void* src,
320 int srcW,
321 int srcH,
322 int bpp) {
323 GrFixed dx = (srcW << 16) / dstW;
324 GrFixed dy = (srcH << 16) / dstH;
325
326 GrFixed y = dy >> 1;
327
328 int dstXLimit = dstW*bpp;
329 for (int j = 0; j < dstH; ++j) {
330 GrFixed x = dx >> 1;
331 void* srcRow = (uint8_t*)src + (y>>16)*srcW*bpp;
332 void* dstRow = (uint8_t*)dst + j*dstW*bpp;
333 for (int i = 0; i < dstXLimit; i += bpp) {
334 memcpy((uint8_t*) dstRow + i,
335 (uint8_t*) srcRow + (x>>16)*bpp,
336 bpp);
337 x += dx;
338 }
339 y += dy;
340 }
341}
342
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000343GrContext::TextureCacheEntry GrContext::createAndLockTexture(
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000344 const GrSamplerState* sampler,
345 const GrTextureDesc& desc,
346 void* srcData,
347 size_t rowBytes) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000348 SK_TRACE_EVENT0("GrContext::createAndLockTexture");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000349
350#if GR_DUMP_TEXTURE_UPLOAD
351 GrPrintf("GrContext::createAndLockTexture [%d %d]\n", desc.fWidth, desc.fHeight);
352#endif
353
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000354 TextureCacheEntry entry;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000355
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000356 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, sampler,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000357 desc, false);
358
359 if (GrTexture::NeedsResizing(resourceKey)) {
360 // The desired texture is NPOT and tiled but that isn't supported by
361 // the current hardware. Resize the texture to be a POT
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000362 GrAssert(NULL != sampler);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000363 TextureCacheEntry clampEntry = this->findAndLockTexture(desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000364 NULL);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000365
366 if (NULL == clampEntry.texture()) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000367 clampEntry = this->createAndLockTexture(NULL, desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000368 srcData, rowBytes);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000369 GrAssert(NULL != clampEntry.texture());
370 if (NULL == clampEntry.texture()) {
371 return entry;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000372 }
373 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000374 GrTextureDesc rtDesc = desc;
375 rtDesc.fFlags = rtDesc.fFlags |
376 kRenderTarget_GrTextureFlagBit |
377 kNoStencil_GrTextureFlagBit;
bsalomon@google.com99621082011-11-15 16:47:16 +0000378 rtDesc.fWidth = GrNextPow2(GrMax(desc.fWidth, 64));
379 rtDesc.fHeight = GrNextPow2(GrMax(desc.fHeight, 64));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000380
381 GrTexture* texture = fGpu->createTexture(rtDesc, NULL, 0);
382
383 if (NULL != texture) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000384 GrDrawTarget::AutoStateRestore asr(fGpu,
385 GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000386 GrDrawState* drawState = fGpu->drawState();
387 drawState->setRenderTarget(texture->asRenderTarget());
388 drawState->setTexture(0, clampEntry.texture());
bsalomon@google.com82c7bd82011-11-09 15:32:29 +0000389
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000390 GrSamplerState::Filter filter;
391 // if filtering is not desired then we want to ensure all
392 // texels in the resampled image are copies of texels from
393 // the original.
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000394 if (GrTexture::NeedsFiltering(resourceKey)) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000395 filter = GrSamplerState::kBilinear_Filter;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000396 } else {
397 filter = GrSamplerState::kNearest_Filter;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000398 }
bsalomon@google.com1e266f82011-12-12 16:11:33 +0000399 drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode,
400 filter);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000401
402 static const GrVertexLayout layout =
403 GrDrawTarget::StageTexCoordVertexLayoutBit(0,0);
404 GrDrawTarget::AutoReleaseGeometry arg(fGpu, layout, 4, 0);
405
406 if (arg.succeeded()) {
407 GrPoint* verts = (GrPoint*) arg.vertices();
408 verts[0].setIRectFan(0, 0,
409 texture->width(),
410 texture->height(),
411 2*sizeof(GrPoint));
412 verts[1].setIRectFan(0, 0, 1, 1, 2*sizeof(GrPoint));
bsalomon@google.com47059542012-06-06 20:51:20 +0000413 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000414 0, 4);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000415 entry.set(fTextureCache->createAndLock(resourceKey, texture));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000416 }
bsalomon@google.com1da07462011-03-10 14:51:57 +0000417 texture->releaseRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000418 } else {
419 // TODO: Our CPU stretch doesn't filter. But we create separate
420 // stretched textures when the sampler state is either filtered or
421 // not. Either implement filtered stretch blit on CPU or just create
422 // one when FBO case fails.
423
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000424 rtDesc.fFlags = kNone_GrTextureFlags;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000425 // no longer need to clamp at min RT size.
426 rtDesc.fWidth = GrNextPow2(desc.fWidth);
427 rtDesc.fHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000428 int bpp = GrBytesPerPixel(desc.fConfig);
bsalomon@google.com3582bf92011-06-30 21:32:31 +0000429 SkAutoSMalloc<128*128*4> stretchedPixels(bpp *
bsalomon@google.com27847de2011-02-22 20:59:41 +0000430 rtDesc.fWidth *
431 rtDesc.fHeight);
432 stretchImage(stretchedPixels.get(), rtDesc.fWidth, rtDesc.fHeight,
433 srcData, desc.fWidth, desc.fHeight, bpp);
434
435 size_t stretchedRowBytes = rtDesc.fWidth * bpp;
436
437 GrTexture* texture = fGpu->createTexture(rtDesc,
438 stretchedPixels.get(),
439 stretchedRowBytes);
440 GrAssert(NULL != texture);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000441 entry.set(fTextureCache->createAndLock(resourceKey, texture));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000442 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000443 fTextureCache->unlock(clampEntry.cacheEntry());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000444
445 } else {
446 GrTexture* texture = fGpu->createTexture(desc, srcData, rowBytes);
447 if (NULL != texture) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000448 entry.set(fTextureCache->createAndLock(resourceKey, texture));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000449 }
450 }
451 return entry;
452}
453
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000454GrContext::TextureCacheEntry GrContext::lockScratchTexture(
455 const GrTextureDesc& inDesc,
456 ScratchTexMatch match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000457 GrTextureDesc desc = inDesc;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000458 desc.fClientCacheID = kScratch_CacheID;
459
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000460 if (kExact_ScratchTexMatch != match) {
461 // bin by pow2 with a reasonable min
462 static const int MIN_SIZE = 256;
463 desc.fWidth = GrMax(MIN_SIZE, GrNextPow2(desc.fWidth));
464 desc.fHeight = GrMax(MIN_SIZE, GrNextPow2(desc.fHeight));
465 }
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000466
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000467 GrResourceEntry* entry;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000468 int origWidth = desc.fWidth;
469 int origHeight = desc.fHeight;
470 bool doubledW = false;
471 bool doubledH = false;
472
473 do {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000474 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL, desc, true);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000475 entry = fTextureCache->findAndLock(key,
476 GrResourceCache::kNested_LockType);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000477 // if we miss, relax the fit of the flags...
478 // then try doubling width... then height.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000479 if (NULL != entry || kExact_ScratchTexMatch == match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000480 break;
481 }
482 if (!(desc.fFlags & kRenderTarget_GrTextureFlagBit)) {
483 desc.fFlags = desc.fFlags | kRenderTarget_GrTextureFlagBit;
484 } else if (desc.fFlags & kNoStencil_GrTextureFlagBit) {
485 desc.fFlags = desc.fFlags & ~kNoStencil_GrTextureFlagBit;
486 } else if (!doubledW) {
487 desc.fFlags = inDesc.fFlags;
488 desc.fWidth *= 2;
489 doubledW = true;
490 } else if (!doubledH) {
491 desc.fFlags = inDesc.fFlags;
492 desc.fWidth = origWidth;
493 desc.fHeight *= 2;
494 doubledH = true;
495 } else {
496 break;
497 }
498
499 } while (true);
500
501 if (NULL == entry) {
502 desc.fFlags = inDesc.fFlags;
503 desc.fWidth = origWidth;
504 desc.fHeight = origHeight;
505 GrTexture* texture = fGpu->createTexture(desc, NULL, 0);
506 if (NULL != texture) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000507 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000508 texture->desc(),
509 true);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000510 entry = fTextureCache->createAndLock(key, texture);
511 }
512 }
513
514 // If the caller gives us the same desc/sampler twice we don't want
515 // to return the same texture the second time (unless it was previously
516 // released). So we detach the entry from the cache and reattach at release.
517 if (NULL != entry) {
518 fTextureCache->detach(entry);
519 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000520 return TextureCacheEntry(entry);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000521}
522
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000523void GrContext::addExistingTextureToCache(GrTexture* texture) {
524
525 if (NULL == texture) {
526 return;
527 }
528
529 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL,
530 texture->desc(),
531 true);
532 fTextureCache->attach(key, texture);
533}
534
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000535void GrContext::unlockTexture(TextureCacheEntry entry) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000536 ASSERT_OWNED_RESOURCE(entry.texture());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000537 // If this is a scratch texture we detached it from the cache
538 // while it was locked (to avoid two callers simultaneously getting
539 // the same texture).
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000540 if (GrTexture::IsScratchTexture(entry.cacheEntry()->key())) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000541 fTextureCache->reattachAndUnlock(entry.cacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000542 } else {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000543 fTextureCache->unlock(entry.cacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000544 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000545}
546
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000547void GrContext::freeEntry(TextureCacheEntry entry) {
548 ASSERT_OWNED_RESOURCE(entry.texture());
549
550 fTextureCache->freeEntry(entry.cacheEntry());
551}
552
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000553GrTexture* GrContext::createUncachedTexture(const GrTextureDesc& descIn,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000554 void* srcData,
555 size_t rowBytes) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000556 GrTextureDesc descCopy = descIn;
557 descCopy.fClientCacheID = kUncached_CacheID;
558 return fGpu->createTexture(descCopy, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000559}
560
bsalomon@google.com8f7e1da2012-06-21 19:48:32 +0000561size_t GrContext::getTextureCacheBudget() const {
562 return fTextureCache->getBudget();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000563}
564
bsalomon@google.com8f7e1da2012-06-21 19:48:32 +0000565void GrContext::setTextureCacheBudget(size_t maxTextureBytes) {
566 fTextureCache->setBudget(maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000567}
568
bsalomon@google.com91958362011-06-13 17:58:13 +0000569int GrContext::getMaxTextureSize() const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000570 return fGpu->getCaps().fMaxTextureSize;
bsalomon@google.com91958362011-06-13 17:58:13 +0000571}
572
573int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000574 return fGpu->getCaps().fMaxRenderTargetSize;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000575}
576
577///////////////////////////////////////////////////////////////////////////////
578
bsalomon@google.come269f212011-11-07 13:29:52 +0000579GrTexture* GrContext::createPlatformTexture(const GrPlatformTextureDesc& desc) {
580 return fGpu->createPlatformTexture(desc);
581}
582
583GrRenderTarget* GrContext::createPlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
584 return fGpu->createPlatformRenderTarget(desc);
585}
586
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000587///////////////////////////////////////////////////////////////////////////////
588
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000589bool GrContext::supportsIndex8PixelConfig(const GrSamplerState* sampler,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000590 int width, int height) const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000591 const GrDrawTarget::Caps& caps = fGpu->getCaps();
592 if (!caps.f8BitPaletteSupport) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000593 return false;
594 }
595
bsalomon@google.com27847de2011-02-22 20:59:41 +0000596 bool isPow2 = GrIsPow2(width) && GrIsPow2(height);
597
598 if (!isPow2) {
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000599 bool tiled = NULL != sampler &&
600 (sampler->getWrapX() != GrSamplerState::kClamp_WrapMode ||
601 sampler->getWrapY() != GrSamplerState::kClamp_WrapMode);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000602 if (tiled && !caps.fNPOTTextureTileSupport) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000603 return false;
604 }
605 }
606 return true;
607}
608
609////////////////////////////////////////////////////////////////////////////////
610
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000611const GrClip& GrContext::getClip() const { return fGpu->getClip(); }
612
bsalomon@google.com27847de2011-02-22 20:59:41 +0000613void GrContext::setClip(const GrClip& clip) {
614 fGpu->setClip(clip);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000615 fDrawState->enableState(GrDrawState::kClip_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000616}
617
618void GrContext::setClip(const GrIRect& rect) {
619 GrClip clip;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000620 clip.setFromIRect(rect);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000621 fGpu->setClip(clip);
622}
623
624////////////////////////////////////////////////////////////////////////////////
625
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000626void GrContext::clear(const GrIRect* rect,
627 const GrColor color,
628 GrRenderTarget* target) {
bsalomon@google.com398109c2011-04-14 18:40:27 +0000629 this->flush();
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000630 fGpu->clear(rect, color, target);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000631}
632
633void GrContext::drawPaint(const GrPaint& paint) {
634 // set rect to be big enough to fill the space, but not super-huge, so we
635 // don't overflow fixed-point implementations
bsalomon@google.comd302f142011-03-03 13:54:13 +0000636 GrRect r;
637 r.setLTRB(0, 0,
638 GrIntToScalar(getRenderTarget()->width()),
639 GrIntToScalar(getRenderTarget()->height()));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000640 GrMatrix inverse;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000641 SkTLazy<GrPaint> tmpPaint;
642 const GrPaint* p = &paint;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000643 GrAutoMatrix am;
644
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000645 // We attempt to map r by the inverse matrix and draw that. mapRect will
646 // map the four corners and bound them with a new rect. This will not
647 // produce a correct result for some perspective matrices.
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000648 if (!this->getMatrix().hasPerspective()) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000649 if (!fDrawState->getViewInverse(&inverse)) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000650 GrPrintf("Could not invert matrix");
651 return;
652 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000653 inverse.mapRect(&r);
654 } else {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000655 if (paint.getActiveMaskStageMask() || paint.getActiveStageMask()) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000656 if (!fDrawState->getViewInverse(&inverse)) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000657 GrPrintf("Could not invert matrix");
658 return;
659 }
660 tmpPaint.set(paint);
661 tmpPaint.get()->preConcatActiveSamplerMatrices(inverse);
662 p = tmpPaint.get();
663 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000664 am.set(this, GrMatrix::I());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000665 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000666 // by definition this fills the entire clip, no need for AA
667 if (paint.fAntiAlias) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000668 if (!tmpPaint.isValid()) {
669 tmpPaint.set(paint);
670 p = tmpPaint.get();
671 }
672 GrAssert(p == tmpPaint.get());
673 tmpPaint.get()->fAntiAlias = false;
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000674 }
675 this->drawRect(*p, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000676}
677
bsalomon@google.com205d4602011-04-25 12:43:45 +0000678////////////////////////////////////////////////////////////////////////////////
679
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000680namespace {
681inline bool disable_coverage_aa_for_blend(GrDrawTarget* target) {
682 return DISABLE_COVERAGE_AA_FOR_BLEND && !target->canApplyCoverage();
683}
684}
685
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000686////////////////////////////////////////////////////////////////////////////////
687
bsalomon@google.com27847de2011-02-22 20:59:41 +0000688/* create a triangle strip that strokes the specified triangle. There are 8
689 unique vertices, but we repreat the last 2 to close up. Alternatively we
690 could use an indices array, and then only send 8 verts, but not sure that
691 would be faster.
692 */
bsalomon@google.com205d4602011-04-25 12:43:45 +0000693static void setStrokeRectStrip(GrPoint verts[10], GrRect rect,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000694 GrScalar width) {
695 const GrScalar rad = GrScalarHalf(width);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000696 rect.sort();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000697
698 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
699 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
700 verts[2].set(rect.fRight - rad, rect.fTop + rad);
701 verts[3].set(rect.fRight + rad, rect.fTop - rad);
702 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
703 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
704 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
705 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
706 verts[8] = verts[0];
707 verts[9] = verts[1];
708}
709
reed@google.com20efde72011-05-09 17:00:02 +0000710/**
711 * Returns true if the rects edges are integer-aligned.
712 */
713static bool isIRect(const GrRect& r) {
714 return GrScalarIsInt(r.fLeft) && GrScalarIsInt(r.fTop) &&
715 GrScalarIsInt(r.fRight) && GrScalarIsInt(r.fBottom);
716}
717
bsalomon@google.com205d4602011-04-25 12:43:45 +0000718static bool apply_aa_to_rect(GrDrawTarget* target,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000719 const GrRect& rect,
720 GrScalar width,
721 const GrMatrix* matrix,
722 GrMatrix* combinedMatrix,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000723 GrRect* devRect,
724 bool* useVertexCoverage) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000725 // we use a simple coverage ramp to do aa on axis-aligned rects
726 // we check if the rect will be axis-aligned, and the rect won't land on
727 // integer coords.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000728
bsalomon@google.coma3108262011-10-10 14:08:47 +0000729 // we are keeping around the "tweak the alpha" trick because
730 // it is our only hope for the fixed-pipe implementation.
731 // In a shader implementation we can give a separate coverage input
bsalomon@google.com289533a2011-10-27 12:34:25 +0000732 // TODO: remove this ugliness when we drop the fixed-pipe impl
bsalomon@google.coma3108262011-10-10 14:08:47 +0000733 *useVertexCoverage = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000734 if (!target->canTweakAlphaForCoverage()) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000735 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +0000736#if GR_DEBUG
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000737 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +0000738#endif
bsalomon@google.coma3108262011-10-10 14:08:47 +0000739 return false;
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000740 } else {
741 *useVertexCoverage = true;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000742 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000743 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000744 const GrDrawState& drawState = target->getDrawState();
745 if (drawState.getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000746 return false;
747 }
748
bsalomon@google.com471d4712011-08-23 15:45:25 +0000749 if (0 == width && target->willUseHWAALines()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000750 return false;
751 }
752
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000753 if (!drawState.getViewMatrix().preservesAxisAlignment()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000754 return false;
755 }
756
757 if (NULL != matrix &&
758 !matrix->preservesAxisAlignment()) {
759 return false;
760 }
761
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000762 *combinedMatrix = drawState.getViewMatrix();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000763 if (NULL != matrix) {
764 combinedMatrix->preConcat(*matrix);
765 GrAssert(combinedMatrix->preservesAxisAlignment());
766 }
767
768 combinedMatrix->mapRect(devRect, rect);
769 devRect->sort();
770
771 if (width < 0) {
reed@google.com20efde72011-05-09 17:00:02 +0000772 return !isIRect(*devRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000773 } else {
774 return true;
775 }
776}
777
bsalomon@google.com27847de2011-02-22 20:59:41 +0000778void GrContext::drawRect(const GrPaint& paint,
779 const GrRect& rect,
780 GrScalar width,
781 const GrMatrix* matrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000782 SK_TRACE_EVENT0("GrContext::drawRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000783
784 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
robertphillips@google.com972265d2012-06-13 18:49:30 +0000785 GrDrawState::AutoTextureRelease atr(fDrawState);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000786 int stageMask = paint.getActiveStageMask();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000787
bsalomon@google.com205d4602011-04-25 12:43:45 +0000788 GrRect devRect = rect;
789 GrMatrix combinedMatrix;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000790 bool useVertexCoverage;
bsalomon@google.com289533a2011-10-27 12:34:25 +0000791 bool needAA = paint.fAntiAlias &&
792 !this->getRenderTarget()->isMultisampled();
793 bool doAA = needAA && apply_aa_to_rect(target, rect, width, matrix,
794 &combinedMatrix, &devRect,
795 &useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000796
797 if (doAA) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000798 GrDrawTarget::AutoDeviceCoordDraw adcd(target, stageMask);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000799 if (width >= 0) {
800 GrVec strokeSize;;
801 if (width > 0) {
802 strokeSize.set(width, width);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000803 combinedMatrix.mapVectors(&strokeSize, 1);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000804 strokeSize.setAbs(strokeSize);
805 } else {
806 strokeSize.set(GR_Scalar1, GR_Scalar1);
807 }
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000808 fAARectRenderer->strokeAARect(this->getGpu(), target, devRect,
809 strokeSize, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000810 } else {
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000811 fAARectRenderer->fillAARect(this->getGpu(), target,
812 devRect, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000813 }
814 return;
815 }
816
bsalomon@google.com27847de2011-02-22 20:59:41 +0000817 if (width >= 0) {
818 // TODO: consider making static vertex buffers for these cases.
819 // Hairline could be done by just adding closing vertex to
820 // unitSquareVertexBuffer()
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000821 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL);
822
bsalomon@google.com27847de2011-02-22 20:59:41 +0000823 static const int worstCaseVertCount = 10;
824 GrDrawTarget::AutoReleaseGeometry geo(target, layout, worstCaseVertCount, 0);
825
826 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000827 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000828 return;
829 }
830
831 GrPrimitiveType primType;
832 int vertCount;
833 GrPoint* vertex = geo.positions();
834
835 if (width > 0) {
836 vertCount = 10;
bsalomon@google.com47059542012-06-06 20:51:20 +0000837 primType = kTriangleStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000838 setStrokeRectStrip(vertex, rect, width);
839 } else {
840 // hairline
841 vertCount = 5;
bsalomon@google.com47059542012-06-06 20:51:20 +0000842 primType = kLineStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000843 vertex[0].set(rect.fLeft, rect.fTop);
844 vertex[1].set(rect.fRight, rect.fTop);
845 vertex[2].set(rect.fRight, rect.fBottom);
846 vertex[3].set(rect.fLeft, rect.fBottom);
847 vertex[4].set(rect.fLeft, rect.fTop);
848 }
849
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000850 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000851 if (NULL != matrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000852 GrDrawState* drawState = target->drawState();
853 avmr.set(drawState);
854 drawState->preConcatViewMatrix(*matrix);
855 drawState->preConcatSamplerMatrices(stageMask, *matrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000856 }
857
858 target->drawNonIndexed(primType, 0, vertCount);
859 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000860#if GR_STATIC_RECT_VB
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000861 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL);
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000862 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
863 if (NULL == sqVB) {
864 GrPrintf("Failed to create static rect vb.\n");
865 return;
866 }
867 target->setVertexSourceToBuffer(layout, sqVB);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000868 GrDrawState* drawState = target->drawState();
869 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000870 GrMatrix m;
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000871 m.setAll(rect.width(), 0, rect.fLeft,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000872 0, rect.height(), rect.fTop,
873 0, 0, GrMatrix::I()[8]);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000874
875 if (NULL != matrix) {
876 m.postConcat(*matrix);
877 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000878 drawState->preConcatViewMatrix(m);
879 drawState->preConcatSamplerMatrices(stageMask, m);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000880
bsalomon@google.com47059542012-06-06 20:51:20 +0000881 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000882#else
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000883 target->drawSimpleRect(rect, matrix, stageMask);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000884#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +0000885 }
886}
887
888void GrContext::drawRectToRect(const GrPaint& paint,
889 const GrRect& dstRect,
890 const GrRect& srcRect,
891 const GrMatrix* dstMatrix,
892 const GrMatrix* srcMatrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000893 SK_TRACE_EVENT0("GrContext::drawRectToRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000894
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000895 // srcRect refers to paint's first texture
896 if (NULL == paint.getTexture(0)) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000897 drawRect(paint, dstRect, -1, dstMatrix);
898 return;
899 }
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000900
bsalomon@google.com27847de2011-02-22 20:59:41 +0000901 GR_STATIC_ASSERT(!BATCH_RECT_TO_RECT || !GR_STATIC_RECT_VB);
902
903#if GR_STATIC_RECT_VB
904 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
robertphillips@google.com972265d2012-06-13 18:49:30 +0000905 GrDrawState::AutoTextureRelease atr(fDrawState);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000906 GrDrawState* drawState = target->drawState();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000907 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000908 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000909
910 GrMatrix m;
911
912 m.setAll(dstRect.width(), 0, dstRect.fLeft,
913 0, dstRect.height(), dstRect.fTop,
914 0, 0, GrMatrix::I()[8]);
915 if (NULL != dstMatrix) {
916 m.postConcat(*dstMatrix);
917 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000918 drawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000919
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000920 // srcRect refers to first stage
921 int otherStageMask = paint.getActiveStageMask() &
922 (~(1 << GrPaint::kFirstTextureStage));
923 if (otherStageMask) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000924 drawState->preConcatSamplerMatrices(otherStageMask, m);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000925 }
926
bsalomon@google.com27847de2011-02-22 20:59:41 +0000927 m.setAll(srcRect.width(), 0, srcRect.fLeft,
928 0, srcRect.height(), srcRect.fTop,
929 0, 0, GrMatrix::I()[8]);
930 if (NULL != srcMatrix) {
931 m.postConcat(*srcMatrix);
932 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000933 drawState->sampler(GrPaint::kFirstTextureStage)->preConcatMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000934
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000935 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
936 if (NULL == sqVB) {
937 GrPrintf("Failed to create static rect vb.\n");
938 return;
939 }
940 target->setVertexSourceToBuffer(layout, sqVB);
bsalomon@google.com47059542012-06-06 20:51:20 +0000941 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000942#else
943
944 GrDrawTarget* target;
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000945#if BATCH_RECT_TO_RECT
bsalomon@google.com27847de2011-02-22 20:59:41 +0000946 target = this->prepareToDraw(paint, kBuffered_DrawCategory);
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000947#else
bsalomon@google.com27847de2011-02-22 20:59:41 +0000948 target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
949#endif
robertphillips@google.com972265d2012-06-13 18:49:30 +0000950 GrDrawState::AutoTextureRelease atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000951
tomhudson@google.com93813632011-10-27 20:21:16 +0000952 const GrRect* srcRects[GrDrawState::kNumStages] = {NULL};
953 const GrMatrix* srcMatrices[GrDrawState::kNumStages] = {NULL};
bsalomon@google.com27847de2011-02-22 20:59:41 +0000954 srcRects[0] = &srcRect;
955 srcMatrices[0] = srcMatrix;
956
957 target->drawRect(dstRect, dstMatrix, 1, srcRects, srcMatrices);
958#endif
959}
960
961void GrContext::drawVertices(const GrPaint& paint,
962 GrPrimitiveType primitiveType,
963 int vertexCount,
964 const GrPoint positions[],
965 const GrPoint texCoords[],
966 const GrColor colors[],
967 const uint16_t indices[],
968 int indexCount) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000969 SK_TRACE_EVENT0("GrContext::drawVertices");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000970
971 GrDrawTarget::AutoReleaseGeometry geo;
972
973 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
robertphillips@google.com972265d2012-06-13 18:49:30 +0000974 GrDrawState::AutoTextureRelease atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000975
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000976 bool hasTexCoords[GrPaint::kTotalStages] = {
977 NULL != texCoords, // texCoordSrc provides explicit stage 0 coords
978 0 // remaining stages use positions
979 };
980
981 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, hasTexCoords);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000982
983 if (NULL != colors) {
984 layout |= GrDrawTarget::kColor_VertexLayoutBit;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000985 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000986 int vertexSize = GrDrawTarget::VertexSize(layout);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000987
988 if (sizeof(GrPoint) != vertexSize) {
989 if (!geo.set(target, layout, vertexCount, 0)) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000990 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000991 return;
992 }
tomhudson@google.com93813632011-10-27 20:21:16 +0000993 int texOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000994 int colorOffset;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000995 GrDrawTarget::VertexSizeAndOffsetsByIdx(layout,
996 texOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000997 &colorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000998 NULL,
999 NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001000 void* curVertex = geo.vertices();
1001
1002 for (int i = 0; i < vertexCount; ++i) {
1003 *((GrPoint*)curVertex) = positions[i];
1004
1005 if (texOffsets[0] > 0) {
1006 *(GrPoint*)((intptr_t)curVertex + texOffsets[0]) = texCoords[i];
1007 }
1008 if (colorOffset > 0) {
1009 *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i];
1010 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001011 curVertex = (void*)((intptr_t)curVertex + vertexSize);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001012 }
1013 } else {
1014 target->setVertexSourceToArray(layout, positions, vertexCount);
1015 }
1016
bsalomon@google.com91958362011-06-13 17:58:13 +00001017 // we don't currently apply offscreen AA to this path. Need improved
1018 // management of GrDrawTarget's geometry to avoid copying points per-tile.
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001019
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001020 if (NULL != indices) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001021 target->setIndexSourceToArray(indices, indexCount);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001022 target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001023 } else {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001024 target->drawNonIndexed(primitiveType, 0, vertexCount);
1025 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001026}
1027
bsalomon@google.com06afe7b2011-04-26 15:31:40 +00001028///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com150d2842012-01-12 20:19:56 +00001029namespace {
1030
bsalomon@google.com93c96602012-04-27 13:05:21 +00001031struct CircleVertex {
1032 GrPoint fPos;
1033 GrPoint fCenter;
1034 GrScalar fOuterRadius;
1035 GrScalar fInnerRadius;
1036};
1037
1038/* Returns true if will map a circle to another circle. This can be true
1039 * if the matrix only includes square-scale, rotation, translation.
1040 */
1041inline bool isSimilarityTransformation(const SkMatrix& matrix,
1042 SkScalar tol = SK_ScalarNearlyZero) {
1043 if (matrix.isIdentity() || matrix.getType() == SkMatrix::kTranslate_Mask) {
1044 return true;
1045 }
1046 if (matrix.hasPerspective()) {
1047 return false;
1048 }
1049
1050 SkScalar mx = matrix.get(SkMatrix::kMScaleX);
1051 SkScalar sx = matrix.get(SkMatrix::kMSkewX);
1052 SkScalar my = matrix.get(SkMatrix::kMScaleY);
1053 SkScalar sy = matrix.get(SkMatrix::kMSkewY);
1054
1055 if (mx == 0 && sx == 0 && my == 0 && sy == 0) {
1056 return false;
1057 }
1058
1059 // it has scales or skews, but it could also be rotation, check it out.
1060 SkVector vec[2];
1061 vec[0].set(mx, sx);
1062 vec[1].set(sy, my);
1063
1064 return SkScalarNearlyZero(vec[0].dot(vec[1]), SkScalarSquare(tol)) &&
1065 SkScalarNearlyEqual(vec[0].lengthSqd(), vec[1].lengthSqd(),
1066 SkScalarSquare(tol));
1067}
1068
1069}
1070
1071// TODO: strokeWidth can't be larger than zero right now.
1072// It will be fixed when drawPath() can handle strokes.
1073void GrContext::drawOval(const GrPaint& paint,
1074 const GrRect& rect,
1075 SkScalar strokeWidth) {
1076 DrawCategory category = (DEFER_PATHS) ? kBuffered_DrawCategory :
1077 kUnbuffered_DrawCategory;
1078 GrDrawTarget* target = this->prepareToDraw(paint, category);
robertphillips@google.com972265d2012-06-13 18:49:30 +00001079 GrDrawState::AutoTextureRelease atr(fDrawState);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001080 GrDrawState* drawState = target->drawState();
1081 GrMatrix vm = drawState->getViewMatrix();
1082
1083 if (!isSimilarityTransformation(vm) ||
1084 !paint.fAntiAlias ||
1085 rect.height() != rect.width()) {
1086 SkPath path;
1087 path.addOval(rect);
1088 GrPathFill fill = (strokeWidth == 0) ?
bsalomon@google.com47059542012-06-06 20:51:20 +00001089 kHairLine_GrPathFill : kWinding_GrPathFill;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001090 this->internalDrawPath(paint, path, fill, NULL);
1091 return;
1092 }
1093
1094 const GrRenderTarget* rt = drawState->getRenderTarget();
1095 if (NULL == rt) {
1096 return;
1097 }
1098
1099 GrDrawTarget::AutoDeviceCoordDraw adcd(target, paint.getActiveStageMask());
1100
1101 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL);
1102 layout |= GrDrawTarget::kEdge_VertexLayoutBit;
1103 GrAssert(sizeof(CircleVertex) == GrDrawTarget::VertexSize(layout));
1104
1105 GrPoint center = GrPoint::Make(rect.centerX(), rect.centerY());
1106 GrScalar radius = SkScalarHalf(rect.width());
1107
1108 vm.mapPoints(&center, 1);
1109 radius = vm.mapRadius(radius);
1110
1111 GrScalar outerRadius = radius;
1112 GrScalar innerRadius = 0;
1113 SkScalar halfWidth = 0;
1114 if (strokeWidth == 0) {
1115 halfWidth = SkScalarHalf(SK_Scalar1);
1116
1117 outerRadius += halfWidth;
1118 innerRadius = SkMaxScalar(0, radius - halfWidth);
1119 }
1120
1121 GrDrawTarget::AutoReleaseGeometry geo(target, layout, 4, 0);
1122 if (!geo.succeeded()) {
1123 GrPrintf("Failed to get space for vertices!\n");
1124 return;
1125 }
1126
1127 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
1128
1129 SkScalar L = center.fX - outerRadius;
1130 SkScalar R = center.fX + outerRadius;
1131 SkScalar T = center.fY - outerRadius;
1132 SkScalar B = center.fY + outerRadius;
1133
1134 verts[0].fPos = SkPoint::Make(L, T);
1135 verts[1].fPos = SkPoint::Make(R, T);
1136 verts[2].fPos = SkPoint::Make(L, B);
1137 verts[3].fPos = SkPoint::Make(R, B);
1138
1139 for (int i = 0; i < 4; ++i) {
1140 // this goes to fragment shader, it should be in y-points-up space.
1141 verts[i].fCenter = SkPoint::Make(center.fX, rt->height() - center.fY);
1142
1143 verts[i].fOuterRadius = outerRadius;
1144 verts[i].fInnerRadius = innerRadius;
1145 }
1146
1147 drawState->setVertexEdgeType(GrDrawState::kCircle_EdgeType);
bsalomon@google.com47059542012-06-06 20:51:20 +00001148 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4);
bsalomon@google.com150d2842012-01-12 20:19:56 +00001149}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001150
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001151void GrContext::drawPath(const GrPaint& paint, const SkPath& path,
reed@google.com07f3ee12011-05-16 17:21:57 +00001152 GrPathFill fill, const GrPoint* translate) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001153
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001154 if (path.isEmpty()) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001155 if (GrIsFillInverted(fill)) {
1156 this->drawPaint(paint);
1157 }
1158 return;
1159 }
1160
bsalomon@google.com93c96602012-04-27 13:05:21 +00001161 SkRect ovalRect;
1162 if (!GrIsFillInverted(fill) && path.isOval(&ovalRect)) {
1163 if (translate) {
1164 ovalRect.offset(*translate);
1165 }
bsalomon@google.com47059542012-06-06 20:51:20 +00001166 SkScalar width = (fill == kHairLine_GrPathFill) ? 0 : -SK_Scalar1;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001167 this->drawOval(paint, ovalRect, width);
1168 return;
1169 }
1170
1171 internalDrawPath(paint, path, fill, translate);
1172}
1173
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001174void GrContext::internalDrawPath(const GrPaint& paint, const SkPath& path,
bsalomon@google.com93c96602012-04-27 13:05:21 +00001175 GrPathFill fill, const GrPoint* translate) {
1176
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001177 // Note that below we may sw-rasterize the path into a scratch texture.
1178 // Scratch textures can be recycled after they are returned to the texture
1179 // cache. This presents a potential hazard for buffered drawing. However,
1180 // the writePixels that uploads to the scratch will perform a flush so we're
1181 // OK.
1182 DrawCategory category = (DEFER_PATHS) ? kBuffered_DrawCategory :
1183 kUnbuffered_DrawCategory;
1184 GrDrawTarget* target = this->prepareToDraw(paint, category);
robertphillips@google.com972265d2012-06-13 18:49:30 +00001185 GrDrawState::AutoTextureRelease atr(fDrawState);
bsalomon@google.com150d2842012-01-12 20:19:56 +00001186 GrDrawState::StageMask stageMask = paint.getActiveStageMask();
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001187
bsalomon@google.com289533a2011-10-27 12:34:25 +00001188 bool prAA = paint.fAntiAlias && !this->getRenderTarget()->isMultisampled();
1189
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001190 // An Assumption here is that path renderer would use some form of tweaking
1191 // the src color (either the input alpha or in the frag shader) to implement
1192 // aa. If we have some future driver-mojo path AA that can do the right
1193 // thing WRT to the blend then we'll need some query on the PR.
1194 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001195#if GR_DEBUG
bsalomon@google.com979432b2011-11-05 21:38:22 +00001196 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001197#endif
bsalomon@google.com289533a2011-10-27 12:34:25 +00001198 prAA = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001199 }
bsalomon@google.com289533a2011-10-27 12:34:25 +00001200
robertphillips@google.com72176b22012-05-23 13:19:12 +00001201 GrPathRenderer* pr = this->getPathRenderer(path, fill, target, prAA, true);
bsalomon@google.com30085192011-08-19 15:42:31 +00001202 if (NULL == pr) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001203#if GR_DEBUG
bsalomon@google.com30085192011-08-19 15:42:31 +00001204 GrPrintf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001205#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001206 return;
1207 }
1208
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001209 pr->drawPath(path, fill, translate, target, stageMask, prAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001210}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001211
bsalomon@google.com27847de2011-02-22 20:59:41 +00001212////////////////////////////////////////////////////////////////////////////////
1213
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001214void GrContext::flush(int flagsBitfield) {
1215 if (kDiscard_FlushBit & flagsBitfield) {
1216 fDrawBuffer->reset();
1217 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001218 this->flushDrawBuffer();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001219 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001220 if (kForceCurrentRenderTarget_FlushBit & flagsBitfield) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001221 fGpu->forceRenderTargetFlush();
1222 }
1223}
1224
bsalomon@google.com27847de2011-02-22 20:59:41 +00001225void GrContext::flushDrawBuffer() {
junov@google.com53a55842011-06-08 22:55:10 +00001226 if (fDrawBuffer) {
robertphillips@google.com58b38182012-05-03 16:29:41 +00001227 // With addition of the AA clip path, flushing the draw buffer can
1228 // result in the generation of an AA clip mask. During this
1229 // process the SW path renderer may be invoked which recusively
1230 // calls this method (via internalWriteTexturePixels) creating
1231 // infinite recursion
1232 GrInOrderDrawBuffer* temp = fDrawBuffer;
1233 fDrawBuffer = NULL;
1234
1235 temp->flushTo(fGpu);
1236
1237 fDrawBuffer = temp;
junov@google.com53a55842011-06-08 22:55:10 +00001238 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001239}
1240
bsalomon@google.com6f379512011-11-16 20:36:03 +00001241void GrContext::internalWriteTexturePixels(GrTexture* texture,
1242 int left, int top,
1243 int width, int height,
1244 GrPixelConfig config,
1245 const void* buffer,
1246 size_t rowBytes,
1247 uint32_t flags) {
1248 SK_TRACE_EVENT0("GrContext::writeTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001249 ASSERT_OWNED_RESOURCE(texture);
1250
bsalomon@google.com6f379512011-11-16 20:36:03 +00001251 if (!(kDontFlush_PixelOpsFlag & flags)) {
1252 this->flush();
1253 }
1254 // TODO: use scratch texture to perform conversion
1255 if (GrPixelConfigIsUnpremultiplied(texture->config()) !=
1256 GrPixelConfigIsUnpremultiplied(config)) {
1257 return;
1258 }
1259
1260 fGpu->writeTexturePixels(texture, left, top, width, height,
1261 config, buffer, rowBytes);
1262}
1263
1264bool GrContext::internalReadTexturePixels(GrTexture* texture,
1265 int left, int top,
1266 int width, int height,
1267 GrPixelConfig config,
1268 void* buffer,
1269 size_t rowBytes,
1270 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001271 SK_TRACE_EVENT0("GrContext::readTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001272 ASSERT_OWNED_RESOURCE(texture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001273
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001274 // TODO: code read pixels for textures that aren't also rendertargets
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001275 GrRenderTarget* target = texture->asRenderTarget();
1276 if (NULL != target) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001277 return this->internalReadRenderTargetPixels(target,
1278 left, top, width, height,
1279 config, buffer, rowBytes,
1280 flags);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001281 } else {
1282 return false;
1283 }
1284}
1285
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001286#include "SkConfig8888.h"
1287
1288namespace {
1289/**
1290 * Converts a GrPixelConfig to a SkCanvas::Config8888. Only byte-per-channel
1291 * formats are representable as Config8888 and so the function returns false
1292 * if the GrPixelConfig has no equivalent Config8888.
1293 */
1294bool grconfig_to_config8888(GrPixelConfig config,
1295 SkCanvas::Config8888* config8888) {
1296 switch (config) {
1297 case kRGBA_8888_PM_GrPixelConfig:
1298 *config8888 = SkCanvas::kRGBA_Premul_Config8888;
1299 return true;
1300 case kRGBA_8888_UPM_GrPixelConfig:
1301 *config8888 = SkCanvas::kRGBA_Unpremul_Config8888;
1302 return true;
1303 case kBGRA_8888_PM_GrPixelConfig:
1304 *config8888 = SkCanvas::kBGRA_Premul_Config8888;
1305 return true;
1306 case kBGRA_8888_UPM_GrPixelConfig:
1307 *config8888 = SkCanvas::kBGRA_Unpremul_Config8888;
1308 return true;
1309 default:
1310 return false;
1311 }
1312}
1313}
1314
bsalomon@google.com6f379512011-11-16 20:36:03 +00001315bool GrContext::internalReadRenderTargetPixels(GrRenderTarget* target,
1316 int left, int top,
1317 int width, int height,
1318 GrPixelConfig config,
1319 void* buffer,
1320 size_t rowBytes,
1321 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001322 SK_TRACE_EVENT0("GrContext::readRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001323 ASSERT_OWNED_RESOURCE(target);
1324
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001325 if (NULL == target) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001326 target = fDrawState->getRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001327 if (NULL == target) {
1328 return false;
1329 }
1330 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001331
bsalomon@google.com6f379512011-11-16 20:36:03 +00001332 if (!(kDontFlush_PixelOpsFlag & flags)) {
1333 this->flush();
1334 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001335
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001336 if (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1337 GrPixelConfigIsUnpremultiplied(config) &&
1338 !fGpu->canPreserveReadWriteUnpremulPixels()) {
1339 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1340 if (!grconfig_to_config8888(target->config(), &srcConfig8888) ||
1341 !grconfig_to_config8888(config, &dstConfig8888)) {
1342 return false;
1343 }
1344 // do read back using target's own config
1345 this->internalReadRenderTargetPixels(target,
1346 left, top,
1347 width, height,
1348 target->config(),
1349 buffer, rowBytes,
1350 kDontFlush_PixelOpsFlag);
1351 // sw convert the pixels to unpremul config
1352 uint32_t* pixels = reinterpret_cast<uint32_t*>(buffer);
1353 SkConvertConfig8888Pixels(pixels, rowBytes, dstConfig8888,
1354 pixels, rowBytes, srcConfig8888,
1355 width, height);
1356 return true;
1357 }
1358
bsalomon@google.comc4364992011-11-07 15:54:49 +00001359 GrTexture* src = target->asTexture();
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001360 bool swapRAndB = NULL != src &&
1361 fGpu->preferredReadPixelsConfig(config) ==
1362 GrPixelConfigSwapRAndB(config);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001363
1364 bool flipY = NULL != src &&
1365 fGpu->readPixelsWillPayForYFlip(target, left, top,
1366 width, height, config,
1367 rowBytes);
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001368 bool alphaConversion = (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1369 GrPixelConfigIsUnpremultiplied(config));
bsalomon@google.comc4364992011-11-07 15:54:49 +00001370
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001371 if (NULL == src && alphaConversion) {
1372 // we should fallback to cpu conversion here. This could happen when
1373 // we were given an external render target by the client that is not
1374 // also a texture (e.g. FBO 0 in GL)
1375 return false;
1376 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001377 // we draw to a scratch texture if any of these conversion are applied
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001378 GrAutoScratchTexture ast;
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001379 if (flipY || swapRAndB || alphaConversion) {
1380 GrAssert(NULL != src);
1381 if (swapRAndB) {
1382 config = GrPixelConfigSwapRAndB(config);
1383 GrAssert(kUnknown_GrPixelConfig != config);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001384 }
1385 // Make the scratch a render target because we don't have a robust
1386 // readTexturePixels as of yet (it calls this function).
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001387 GrTextureDesc desc;
1388 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1389 desc.fWidth = width;
1390 desc.fHeight = height;
1391 desc.fConfig = config;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001392
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001393 // When a full readback is faster than a partial we could always make
1394 // the scratch exactly match the passed rect. However, if we see many
1395 // different size rectangles we will trash our texture cache and pay the
1396 // cost of creating and destroying many textures. So, we only request
1397 // an exact match when the caller is reading an entire RT.
1398 ScratchTexMatch match = kApprox_ScratchTexMatch;
1399 if (0 == left &&
1400 0 == top &&
1401 target->width() == width &&
1402 target->height() == height &&
1403 fGpu->fullReadPixelsIsFasterThanPartial()) {
1404 match = kExact_ScratchTexMatch;
1405 }
1406 ast.set(this, desc, match);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001407 GrTexture* texture = ast.texture();
1408 if (!texture) {
1409 return false;
1410 }
1411 target = texture->asRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001412 GrAssert(NULL != target);
1413
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001414 GrDrawTarget::AutoStateRestore asr(fGpu,
1415 GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001416 GrDrawState* drawState = fGpu->drawState();
1417 drawState->setRenderTarget(target);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001418
bsalomon@google.comc4364992011-11-07 15:54:49 +00001419 GrMatrix matrix;
1420 if (flipY) {
1421 matrix.setTranslate(SK_Scalar1 * left,
1422 SK_Scalar1 * (top + height));
1423 matrix.set(GrMatrix::kMScaleY, -GR_Scalar1);
1424 } else {
1425 matrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
1426 }
1427 matrix.postIDiv(src->width(), src->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001428 drawState->sampler(0)->reset(matrix);
1429 drawState->sampler(0)->setRAndBSwap(swapRAndB);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001430 drawState->setTexture(0, src);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001431 GrRect rect;
1432 rect.setXYWH(0, 0, SK_Scalar1 * width, SK_Scalar1 * height);
1433 fGpu->drawSimpleRect(rect, NULL, 0x1);
1434 left = 0;
1435 top = 0;
1436 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001437 return fGpu->readPixels(target,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001438 left, top, width, height,
1439 config, buffer, rowBytes, flipY);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001440}
1441
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001442void GrContext::resolveRenderTarget(GrRenderTarget* target) {
1443 GrAssert(target);
1444 ASSERT_OWNED_RESOURCE(target);
1445 // In the future we may track whether there are any pending draws to this
1446 // target. We don't today so we always perform a flush. We don't promise
1447 // this to our clients, though.
1448 this->flush();
1449 fGpu->resolveRenderTarget(target);
1450}
1451
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001452void GrContext::copyTexture(GrTexture* src, GrRenderTarget* dst) {
1453 if (NULL == src || NULL == dst) {
1454 return;
1455 }
1456 ASSERT_OWNED_RESOURCE(src);
1457
twiz@google.com1ac87ff2012-04-27 19:39:33 +00001458 // Writes pending to the source texture are not tracked, so a flush
1459 // is required to ensure that the copy captures the most recent contents
1460 // of the source texture. See similar behaviour in
1461 // GrContext::resolveRenderTarget.
1462 this->flush();
1463
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001464 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001465 GrDrawState* drawState = fGpu->drawState();
1466 drawState->setRenderTarget(dst);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001467 GrMatrix sampleM;
1468 sampleM.setIDiv(src->width(), src->height());
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001469 drawState->setTexture(0, src);
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001470 drawState->sampler(0)->reset(sampleM);
bsalomon@google.com5db3b6c2012-01-12 20:38:57 +00001471 SkRect rect = SkRect::MakeXYWH(0, 0,
1472 SK_Scalar1 * src->width(),
1473 SK_Scalar1 * src->height());
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001474 fGpu->drawSimpleRect(rect, NULL, 1 << 0);
1475}
1476
bsalomon@google.com6f379512011-11-16 20:36:03 +00001477void GrContext::internalWriteRenderTargetPixels(GrRenderTarget* target,
1478 int left, int top,
1479 int width, int height,
1480 GrPixelConfig config,
1481 const void* buffer,
1482 size_t rowBytes,
1483 uint32_t flags) {
1484 SK_TRACE_EVENT0("GrContext::writeRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001485 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com6f379512011-11-16 20:36:03 +00001486
1487 if (NULL == target) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001488 target = fDrawState->getRenderTarget();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001489 if (NULL == target) {
1490 return;
1491 }
1492 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001493
1494 // TODO: when underlying api has a direct way to do this we should use it
1495 // (e.g. glDrawPixels on desktop GL).
1496
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001497 // If the RT is also a texture and we don't have to do PM/UPM conversion
1498 // then take the texture path, which we expect to be at least as fast or
1499 // faster since it doesn't use an intermediate texture as we do below.
1500
1501#if !GR_MAC_BUILD
1502 // At least some drivers on the Mac get confused when glTexImage2D is called
1503 // on a texture attached to an FBO. The FBO still sees the old image. TODO:
1504 // determine what OS versions and/or HW is affected.
1505 if (NULL != target->asTexture() &&
1506 GrPixelConfigIsUnpremultiplied(target->config()) ==
1507 GrPixelConfigIsUnpremultiplied(config)) {
1508
1509 this->internalWriteTexturePixels(target->asTexture(),
1510 left, top, width, height,
1511 config, buffer, rowBytes, flags);
1512 return;
1513 }
1514#endif
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001515 if (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1516 GrPixelConfigIsUnpremultiplied(config) &&
1517 !fGpu->canPreserveReadWriteUnpremulPixels()) {
1518 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1519 if (!grconfig_to_config8888(config, &srcConfig8888) ||
1520 !grconfig_to_config8888(target->config(), &dstConfig8888)) {
1521 return;
1522 }
1523 // allocate a tmp buffer and sw convert the pixels to premul
1524 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(width * height);
1525 const uint32_t* src = reinterpret_cast<const uint32_t*>(buffer);
1526 SkConvertConfig8888Pixels(tmpPixels.get(), 4 * width, dstConfig8888,
1527 src, rowBytes, srcConfig8888,
1528 width, height);
1529 // upload the already premul pixels
1530 this->internalWriteRenderTargetPixels(target,
1531 left, top,
1532 width, height,
1533 target->config(),
1534 tmpPixels, 4 * width, flags);
1535 return;
1536 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001537
1538 bool swapRAndB = fGpu->preferredReadPixelsConfig(config) ==
1539 GrPixelConfigSwapRAndB(config);
1540 if (swapRAndB) {
1541 config = GrPixelConfigSwapRAndB(config);
1542 }
1543
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001544 GrTextureDesc desc;
1545 desc.fWidth = width;
1546 desc.fHeight = height;
1547 desc.fConfig = config;
1548
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001549 GrAutoScratchTexture ast(this, desc);
1550 GrTexture* texture = ast.texture();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001551 if (NULL == texture) {
1552 return;
1553 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001554 this->internalWriteTexturePixels(texture, 0, 0, width, height,
1555 config, buffer, rowBytes, flags);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001556
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001557 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001558 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001559
1560 GrMatrix matrix;
1561 matrix.setTranslate(GrIntToScalar(left), GrIntToScalar(top));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001562 drawState->setViewMatrix(matrix);
1563 drawState->setRenderTarget(target);
1564 drawState->setTexture(0, texture);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001565
bsalomon@google.com5c638652011-07-18 19:31:59 +00001566 matrix.setIDiv(texture->width(), texture->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001567 drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode,
1568 GrSamplerState::kNearest_Filter,
1569 matrix);
1570 drawState->sampler(0)->setRAndBSwap(swapRAndB);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001571
1572 GrVertexLayout layout = GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(0);
1573 static const int VCOUNT = 4;
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001574 // TODO: Use GrGpu::drawRect here
bsalomon@google.com27847de2011-02-22 20:59:41 +00001575 GrDrawTarget::AutoReleaseGeometry geo(fGpu, layout, VCOUNT, 0);
1576 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001577 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +00001578 return;
1579 }
1580 ((GrPoint*)geo.vertices())->setIRectFan(0, 0, width, height);
bsalomon@google.com47059542012-06-06 20:51:20 +00001581 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, VCOUNT);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001582}
1583////////////////////////////////////////////////////////////////////////////////
1584
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001585void GrContext::setPaint(const GrPaint& paint) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001586
1587 for (int i = 0; i < GrPaint::kMaxTextures; ++i) {
1588 int s = i + GrPaint::kFirstTextureStage;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001589 fDrawState->setTexture(s, paint.getTexture(i));
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001590 ASSERT_OWNED_RESOURCE(paint.getTexture(i));
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001591 if (paint.getTexture(i)) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001592 *fDrawState->sampler(s) = paint.getTextureSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001593 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001594 }
1595
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001596 fDrawState->setFirstCoverageStage(GrPaint::kFirstMaskStage);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001597
1598 for (int i = 0; i < GrPaint::kMaxMasks; ++i) {
1599 int s = i + GrPaint::kFirstMaskStage;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001600 fDrawState->setTexture(s, paint.getMask(i));
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001601 ASSERT_OWNED_RESOURCE(paint.getMask(i));
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001602 if (paint.getMask(i)) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001603 *fDrawState->sampler(s) = paint.getMaskSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001604 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001605 }
bsalomon@google.com26936d02012-03-19 13:06:19 +00001606
1607 // disable all stages not accessible via the paint
1608 for (int s = GrPaint::kTotalStages; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001609 fDrawState->setTexture(s, NULL);
bsalomon@google.com26936d02012-03-19 13:06:19 +00001610 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001611
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001612 fDrawState->setColor(paint.fColor);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001613
1614 if (paint.fDither) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001615 fDrawState->enableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001616 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001617 fDrawState->disableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001618 }
1619 if (paint.fAntiAlias) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001620 fDrawState->enableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001621 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001622 fDrawState->disableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001623 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001624 if (paint.fColorMatrixEnabled) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001625 fDrawState->enableState(GrDrawState::kColorMatrix_StateBit);
1626 fDrawState->setColorMatrix(paint.fColorMatrix);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001627 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001628 fDrawState->disableState(GrDrawState::kColorMatrix_StateBit);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001629 }
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001630 fDrawState->setBlendFunc(paint.fSrcBlendCoeff, paint.fDstBlendCoeff);
1631 fDrawState->setColorFilter(paint.fColorFilterColor, paint.fColorFilterXfermode);
1632 fDrawState->setCoverage(paint.fCoverage);
reed@google.com4b2d3f32012-05-15 18:05:50 +00001633#if GR_DEBUG_PARTIAL_COVERAGE_CHECK
bsalomon@google.come79c8152012-03-29 19:07:12 +00001634 if ((paint.getActiveMaskStageMask() || 0xff != paint.fCoverage) &&
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001635 !fGpu->canApplyCoverage()) {
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001636 GrPrintf("Partial pixel coverage will be incorrectly blended.\n");
1637 }
bsalomon@google.com95cd7bd2012-03-28 15:35:05 +00001638#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +00001639}
1640
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001641GrDrawTarget* GrContext::prepareToDraw(const GrPaint& paint,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001642 DrawCategory category) {
1643 if (category != fLastDrawCategory) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001644 this->flushDrawBuffer();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001645 fLastDrawCategory = category;
1646 }
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001647 this->setPaint(paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001648 GrDrawTarget* target = fGpu;
1649 switch (category) {
bsalomon@google.com193395c2012-03-30 17:35:12 +00001650 case kUnbuffered_DrawCategory:
1651 target = fGpu;
1652 break;
1653 case kBuffered_DrawCategory:
1654 target = fDrawBuffer;
1655 fDrawBuffer->setClip(fGpu->getClip());
1656 break;
1657 default:
1658 GrCrash("Unexpected DrawCategory.");
1659 break;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001660 }
1661 return target;
1662}
1663
robertphillips@google.com72176b22012-05-23 13:19:12 +00001664/*
1665 * This method finds a path renderer that can draw the specified path on
1666 * the provided target.
1667 * Due to its expense, the software path renderer has split out so it can
1668 * can be individually allowed/disallowed via the "allowSW" boolean.
1669 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001670GrPathRenderer* GrContext::getPathRenderer(const SkPath& path,
bsalomon@google.com289533a2011-10-27 12:34:25 +00001671 GrPathFill fill,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001672 const GrDrawTarget* target,
robertphillips@google.com72176b22012-05-23 13:19:12 +00001673 bool antiAlias,
1674 bool allowSW) {
bsalomon@google.com30085192011-08-19 15:42:31 +00001675 if (NULL == fPathRendererChain) {
1676 fPathRendererChain =
1677 new GrPathRendererChain(this, GrPathRendererChain::kNone_UsageFlag);
1678 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001679
1680 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(path, fill,
1681 target,
1682 antiAlias);
1683
1684 if (NULL == pr && allowSW) {
1685 if (NULL == fSoftwarePathRenderer) {
1686 fSoftwarePathRenderer = new GrSoftwarePathRenderer(this);
1687 }
1688
1689 pr = fSoftwarePathRenderer;
1690 }
1691
1692 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001693}
1694
bsalomon@google.com27847de2011-02-22 20:59:41 +00001695////////////////////////////////////////////////////////////////////////////////
1696
bsalomon@google.com27847de2011-02-22 20:59:41 +00001697void GrContext::setRenderTarget(GrRenderTarget* target) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001698 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001699 if (fDrawState->getRenderTarget() != target) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001700 this->flush(false);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001701 fDrawState->setRenderTarget(target);
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001702 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001703}
1704
1705GrRenderTarget* GrContext::getRenderTarget() {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001706 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001707}
1708
1709const GrRenderTarget* GrContext::getRenderTarget() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001710 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001711}
1712
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001713bool GrContext::isConfigRenderable(GrPixelConfig config) const {
1714 return fGpu->isConfigRenderable(config);
1715}
1716
bsalomon@google.com27847de2011-02-22 20:59:41 +00001717const GrMatrix& GrContext::getMatrix() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001718 return fDrawState->getViewMatrix();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001719}
1720
1721void GrContext::setMatrix(const GrMatrix& m) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001722 fDrawState->setViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001723}
1724
1725void GrContext::concatMatrix(const GrMatrix& m) const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001726 fDrawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001727}
1728
1729static inline intptr_t setOrClear(intptr_t bits, int shift, intptr_t pred) {
1730 intptr_t mask = 1 << shift;
1731 if (pred) {
1732 bits |= mask;
1733 } else {
1734 bits &= ~mask;
1735 }
1736 return bits;
1737}
1738
bsalomon@google.com583a1e32011-08-17 13:42:46 +00001739GrContext::GrContext(GrGpu* gpu) {
bsalomon@google.comc0af3172012-06-15 14:10:09 +00001740 ++THREAD_INSTANCE_COUNT;
1741
bsalomon@google.com27847de2011-02-22 20:59:41 +00001742 fGpu = gpu;
1743 fGpu->ref();
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001744 fGpu->setContext(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001745
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001746 fDrawState = new GrDrawState();
1747 fGpu->setDrawState(fDrawState);
1748
bsalomon@google.com30085192011-08-19 15:42:31 +00001749 fPathRendererChain = NULL;
robertphillips@google.com72176b22012-05-23 13:19:12 +00001750 fSoftwarePathRenderer = NULL;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001751
bsalomon@google.com8f7e1da2012-06-21 19:48:32 +00001752 fTextureCache = new GrResourceCache(kDefaultTextureCacheBudget);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001753 fFontCache = new GrFontCache(fGpu);
1754
1755 fLastDrawCategory = kUnbuffered_DrawCategory;
1756
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001757 fDrawBuffer = NULL;
1758 fDrawBufferVBAllocPool = NULL;
1759 fDrawBufferIBAllocPool = NULL;
1760
robertphillips@google.comf6747b02012-06-12 00:32:28 +00001761 fAARectRenderer = new GrAARectRenderer;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001762
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001763 this->setupDrawBuffer();
1764}
1765
1766void GrContext::setupDrawBuffer() {
1767
1768 GrAssert(NULL == fDrawBuffer);
1769 GrAssert(NULL == fDrawBufferVBAllocPool);
1770 GrAssert(NULL == fDrawBufferIBAllocPool);
1771
bsalomon@google.com92edd312012-04-04 21:40:21 +00001772#if DEFER_TEXT_RENDERING || BATCH_RECT_TO_RECT || DEFER_PATHS
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001773 fDrawBufferVBAllocPool =
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001774 new GrVertexBufferAllocPool(fGpu, false,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001775 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
1776 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS);
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001777 fDrawBufferIBAllocPool =
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001778 new GrIndexBufferAllocPool(fGpu, false,
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001779 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001780 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS);
1781
bsalomon@google.com471d4712011-08-23 15:45:25 +00001782 fDrawBuffer = new GrInOrderDrawBuffer(fGpu,
1783 fDrawBufferVBAllocPool,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001784 fDrawBufferIBAllocPool);
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001785#endif
1786
1787#if BATCH_RECT_TO_RECT
bsalomon@google.com27847de2011-02-22 20:59:41 +00001788 fDrawBuffer->setQuadIndexBuffer(this->getQuadIndexBuffer());
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001789#endif
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001790 fDrawBuffer->setAutoFlushTarget(fGpu);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001791 fDrawBuffer->setDrawState(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001792}
1793
bsalomon@google.com27847de2011-02-22 20:59:41 +00001794GrDrawTarget* GrContext::getTextTarget(const GrPaint& paint) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001795#if DEFER_TEXT_RENDERING
bsalomon@google.com193395c2012-03-30 17:35:12 +00001796 return prepareToDraw(paint, kBuffered_DrawCategory);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001797#else
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001798 return prepareToDraw(paint, kUnbuffered_DrawCategory);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001799#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +00001800}
1801
1802const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {
1803 return fGpu->getQuadIndexBuffer();
1804}
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001805
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001806GrTexture* GrContext::gaussianBlur(GrTexture* srcTexture,
1807 GrAutoScratchTexture* temp1,
1808 GrAutoScratchTexture* temp2,
1809 const SkRect& rect,
1810 float sigmaX, float sigmaY) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001811 ASSERT_OWNED_RESOURCE(srcTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001812 GrRenderTarget* oldRenderTarget = this->getRenderTarget();
1813 GrClip oldClip = this->getClip();
1814 GrTexture* origTexture = srcTexture;
1815 GrAutoMatrix avm(this, GrMatrix::I());
1816 SkIRect clearRect;
bsalomon@google.comb505a122012-05-31 18:40:36 +00001817 int scaleFactorX, radiusX;
1818 int scaleFactorY, radiusY;
1819 sigmaX = adjust_sigma(sigmaX, &scaleFactorX, &radiusX);
1820 sigmaY = adjust_sigma(sigmaY, &scaleFactorY, &radiusY);
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001821
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001822 SkRect srcRect(rect);
1823 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
1824 srcRect.roundOut();
robertphillips@google.com8637a362012-04-10 18:32:35 +00001825 scale_rect(&srcRect, static_cast<float>(scaleFactorX),
1826 static_cast<float>(scaleFactorY));
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001827 this->setClip(srcRect);
1828
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001829 GrAssert(kBGRA_8888_PM_GrPixelConfig == srcTexture->config() ||
1830 kRGBA_8888_PM_GrPixelConfig == srcTexture->config() ||
1831 kAlpha_8_GrPixelConfig == srcTexture->config());
1832
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001833 GrTextureDesc desc;
1834 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
1835 desc.fWidth = SkScalarFloorToInt(srcRect.width());
1836 desc.fHeight = SkScalarFloorToInt(srcRect.height());
1837 desc.fConfig = srcTexture->config();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001838
1839 temp1->set(this, desc);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001840 if (temp2) {
1841 temp2->set(this, desc);
1842 }
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001843
1844 GrTexture* dstTexture = temp1->texture();
1845 GrPaint paint;
1846 paint.reset();
1847 paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
1848
1849 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
1850 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1851 srcTexture->height());
1852 this->setRenderTarget(dstTexture->asRenderTarget());
1853 SkRect dstRect(srcRect);
1854 scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
1855 i < scaleFactorY ? 0.5f : 1.0f);
1856 paint.setTexture(0, srcTexture);
1857 this->drawRectToRect(paint, dstRect, srcRect);
1858 srcRect = dstRect;
1859 SkTSwap(srcTexture, dstTexture);
1860 // If temp2 is non-NULL, don't render back to origTexture
robertphillips@google.com972265d2012-06-13 18:49:30 +00001861 if (temp2 && dstTexture == origTexture) {
1862 dstTexture = temp2->texture();
1863 }
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001864 }
1865
robertphillips@google.com7a396332012-05-10 15:11:27 +00001866 SkIRect srcIRect;
1867 srcRect.roundOut(&srcIRect);
1868
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001869 if (sigmaX > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001870 if (scaleFactorX > 1) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001871 // Clear out a radius to the right of the srcRect to prevent the
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001872 // X convolution from reading garbage.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001873 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001874 radiusX, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001875 this->clear(&clearRect, 0x0);
1876 }
1877
1878 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001879 convolve_gaussian(fGpu, srcTexture, srcRect, sigmaX, radiusX,
1880 Gr1DKernelEffect::kX_Direction);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001881 SkTSwap(srcTexture, dstTexture);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001882 if (temp2 && dstTexture == origTexture) {
1883 dstTexture = temp2->texture();
1884 }
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001885 }
1886
1887 if (sigmaY > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001888 if (scaleFactorY > 1 || sigmaX > 0.0f) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001889 // Clear out a radius below the srcRect to prevent the Y
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001890 // convolution from reading garbage.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001891 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001892 srcIRect.width(), radiusY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001893 this->clear(&clearRect, 0x0);
1894 }
1895
1896 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001897 convolve_gaussian(fGpu, srcTexture, srcRect, sigmaY, radiusY,
1898 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001899 SkTSwap(srcTexture, dstTexture);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001900 if (temp2 && dstTexture == origTexture) {
1901 dstTexture = temp2->texture();
1902 }
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001903 }
1904
1905 if (scaleFactorX > 1 || scaleFactorY > 1) {
1906 // Clear one pixel to the right and below, to accommodate bilinear
1907 // upsampling.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001908 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
1909 srcIRect.width() + 1, 1);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001910 this->clear(&clearRect, 0x0);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001911 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
1912 1, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001913 this->clear(&clearRect, 0x0);
1914 // FIXME: This should be mitchell, not bilinear.
1915 paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
1916 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1917 srcTexture->height());
1918 this->setRenderTarget(dstTexture->asRenderTarget());
1919 paint.setTexture(0, srcTexture);
1920 SkRect dstRect(srcRect);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001921 scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001922 this->drawRectToRect(paint, dstRect, srcRect);
1923 srcRect = dstRect;
1924 SkTSwap(srcTexture, dstTexture);
1925 }
1926 this->setRenderTarget(oldRenderTarget);
1927 this->setClip(oldClip);
1928 return srcTexture;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001929}
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001930
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001931GrTexture* GrContext::applyMorphology(GrTexture* srcTexture,
1932 const GrRect& rect,
1933 GrTexture* temp1, GrTexture* temp2,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001934 MorphologyType morphType,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001935 SkISize radius) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001936 ASSERT_OWNED_RESOURCE(srcTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001937 GrRenderTarget* oldRenderTarget = this->getRenderTarget();
1938 GrAutoMatrix avm(this, GrMatrix::I());
1939 GrClip oldClip = this->getClip();
robertphillips@google.com7a396332012-05-10 15:11:27 +00001940 this->setClip(GrRect::MakeWH(SkIntToScalar(srcTexture->width()),
1941 SkIntToScalar(srcTexture->height())));
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001942 if (radius.fWidth > 0) {
1943 this->setRenderTarget(temp1->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001944 apply_morphology(fGpu, srcTexture, rect, radius.fWidth, morphType,
1945 Gr1DKernelEffect::kX_Direction);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001946 SkIRect clearRect = SkIRect::MakeXYWH(
1947 SkScalarFloorToInt(rect.fLeft),
1948 SkScalarFloorToInt(rect.fBottom),
1949 SkScalarFloorToInt(rect.width()),
1950 radius.fHeight);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001951 this->clear(&clearRect, 0x0);
1952 srcTexture = temp1;
1953 }
1954 if (radius.fHeight > 0) {
1955 this->setRenderTarget(temp2->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001956 apply_morphology(fGpu, srcTexture, rect, radius.fHeight, morphType,
1957 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001958 srcTexture = temp2;
1959 }
1960 this->setRenderTarget(oldRenderTarget);
1961 this->setClip(oldClip);
1962 return srcTexture;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001963}
bsalomon@google.comc4364992011-11-07 15:54:49 +00001964
robertphillips@google.com49d9fd52012-05-23 11:44:08 +00001965void GrContext::postClipPush() {
1966 fGpu->postClipPush();
1967}
1968
1969void GrContext::preClipPop() {
1970 fGpu->preClipPop();
1971};
1972
bsalomon@google.comc4364992011-11-07 15:54:49 +00001973///////////////////////////////////////////////////////////////////////////////