blob: 4ef0f5badf55f42f67a1dac122bef7cb331195a5 [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
tomhudson@google.com278cbb42011-06-30 19:37:01 +000012#include "GrBufferAllocPool.h"
13#include "GrClipIterator.h"
bsalomon@google.com05ef5102011-05-02 21:14:59 +000014#include "GrGpu.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000015#include "GrIndexBuffer.h"
16#include "GrInOrderDrawBuffer.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000017#include "GrPathRenderer.h"
tomhudson@google.comd22b6e42011-06-24 15:53:40 +000018#include "GrPathUtils.h"
bsalomon@google.com50398bf2011-07-26 20:45:30 +000019#include "GrResourceCache.h"
bsalomon@google.com558a75b2011-08-08 17:01:14 +000020#include "GrStencilBuffer.h"
tomhudson@google.com278cbb42011-06-30 19:37:01 +000021#include "GrTextStrike.h"
bsalomon@google.com8c2fe992011-09-13 15:27:18 +000022#include "SkTLazy.h"
tomhudson@google.com0c8d93a2011-07-01 17:08:26 +000023#include "SkTrace.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000024
25#define DEFER_TEXT_RENDERING 1
26
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +000027#define DEFER_PATHS 1
28
bsalomon@google.com27847de2011-02-22 20:59:41 +000029#define BATCH_RECT_TO_RECT (1 && !GR_STATIC_RECT_VB)
30
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +000031#define MAX_BLUR_SIGMA 4.0f
32
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +000033
bsalomon@google.comd46e2422011-09-23 17:40:07 +000034// When we're using coverage AA but the blend is incompatible (given gpu
35// limitations) should we disable AA or draw wrong?
bsalomon@google.com950d7a82011-09-28 15:05:33 +000036#define DISABLE_COVERAGE_AA_FOR_BLEND 1
bsalomon@google.comd46e2422011-09-23 17:40:07 +000037
bsalomon@google.com8ccaddd2011-08-09 16:49:03 +000038static const size_t MAX_TEXTURE_CACHE_COUNT = 256;
39static const size_t MAX_TEXTURE_CACHE_BYTES = 16 * 1024 * 1024;
bsalomon@google.com27847de2011-02-22 20:59:41 +000040
bsalomon@google.com60361492012-03-15 17:47:06 +000041static const size_t DRAW_BUFFER_VBPOOL_BUFFER_SIZE = 1 << 15;
bsalomon@google.com27847de2011-02-22 20:59:41 +000042static const int DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS = 4;
43
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +000044// path rendering is the only thing we defer today that uses non-static indices
45static const size_t DRAW_BUFFER_IBPOOL_BUFFER_SIZE = DEFER_PATHS ? 1 << 11 : 0;
46static const int DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS = DEFER_PATHS ? 4 : 0;
bsalomon@google.com27847de2011-02-22 20:59:41 +000047
bsalomon@google.combc4b6542011-11-19 13:56:11 +000048#define ASSERT_OWNED_RESOURCE(R) GrAssert(!(R) || (R)->getContext() == this)
49
bsalomon@google.com05ef5102011-05-02 21:14:59 +000050GrContext* GrContext::Create(GrEngine engine,
51 GrPlatform3DContext context3D) {
bsalomon@google.com27847de2011-02-22 20:59:41 +000052 GrContext* ctx = NULL;
53 GrGpu* fGpu = GrGpu::Create(engine, context3D);
54 if (NULL != fGpu) {
55 ctx = new GrContext(fGpu);
56 fGpu->unref();
57 }
58 return ctx;
59}
60
bsalomon@google.com27847de2011-02-22 20:59:41 +000061GrContext::~GrContext() {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000062 this->flush();
bsalomon@google.com27847de2011-02-22 20:59:41 +000063 delete fTextureCache;
64 delete fFontCache;
65 delete fDrawBuffer;
66 delete fDrawBufferVBAllocPool;
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +000067 delete fDrawBufferIBAllocPool;
bsalomon@google.com30085192011-08-19 15:42:31 +000068
bsalomon@google.com205d4602011-04-25 12:43:45 +000069 GrSafeUnref(fAAFillRectIndexBuffer);
70 GrSafeUnref(fAAStrokeRectIndexBuffer);
71 fGpu->unref();
bsalomon@google.com30085192011-08-19 15:42:31 +000072 GrSafeUnref(fPathRendererChain);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +000073 fDrawState->unref();
bsalomon@google.com27847de2011-02-22 20:59:41 +000074}
75
bsalomon@google.com8fe72472011-03-30 21:26:44 +000076void GrContext::contextLost() {
junov@google.com53a55842011-06-08 22:55:10 +000077 contextDestroyed();
78 this->setupDrawBuffer();
79}
80
81void GrContext::contextDestroyed() {
bsalomon@google.com205d4602011-04-25 12:43:45 +000082 // abandon first to so destructors
83 // don't try to free the resources in the API.
84 fGpu->abandonResources();
85
bsalomon@google.com30085192011-08-19 15:42:31 +000086 // a path renderer may be holding onto resources that
87 // are now unusable
88 GrSafeSetNull(fPathRendererChain);
89
bsalomon@google.com8fe72472011-03-30 21:26:44 +000090 delete fDrawBuffer;
91 fDrawBuffer = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +000092
bsalomon@google.com8fe72472011-03-30 21:26:44 +000093 delete fDrawBufferVBAllocPool;
94 fDrawBufferVBAllocPool = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +000095
bsalomon@google.com8fe72472011-03-30 21:26:44 +000096 delete fDrawBufferIBAllocPool;
97 fDrawBufferIBAllocPool = NULL;
98
bsalomon@google.com205d4602011-04-25 12:43:45 +000099 GrSafeSetNull(fAAFillRectIndexBuffer);
100 GrSafeSetNull(fAAStrokeRectIndexBuffer);
101
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000102 fTextureCache->removeAll();
103 fFontCache->freeAll();
104 fGpu->markContextDirty();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000105}
106
107void GrContext::resetContext() {
108 fGpu->markContextDirty();
109}
110
111void GrContext::freeGpuResources() {
112 this->flush();
113 fTextureCache->removeAll();
114 fFontCache->freeAll();
bsalomon@google.com30085192011-08-19 15:42:31 +0000115 // a path renderer may be holding onto resources
116 GrSafeSetNull(fPathRendererChain);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000117}
118
twiz@google.com05e70242012-01-27 19:12:00 +0000119size_t GrContext::getGpuTextureCacheBytes() const {
120 return fTextureCache->getCachedResourceBytes();
121}
122
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000123////////////////////////////////////////////////////////////////////////////////
124
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000125int GrContext::PaintStageVertexLayoutBits(
126 const GrPaint& paint,
127 const bool hasTexCoords[GrPaint::kTotalStages]) {
128 int stageMask = paint.getActiveStageMask();
129 int layout = 0;
130 for (int i = 0; i < GrPaint::kTotalStages; ++i) {
131 if ((1 << i) & stageMask) {
132 if (NULL != hasTexCoords && hasTexCoords[i]) {
133 layout |= GrDrawTarget::StageTexCoordVertexLayoutBit(i, i);
134 } else {
135 layout |= GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(i);
136 }
137 }
138 }
139 return layout;
140}
141
142
143////////////////////////////////////////////////////////////////////////////////
144
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000145enum {
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000146 // flags for textures
147 kNPOTBit = 0x1,
148 kFilterBit = 0x2,
149 kScratchBit = 0x4,
150
151 // resource type
152 kTextureBit = 0x8,
153 kStencilBufferBit = 0x10
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000154};
155
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000156GrTexture* GrContext::TextureCacheEntry::texture() const {
157 if (NULL == fEntry) {
158 return NULL;
159 } else {
160 return (GrTexture*) fEntry->resource();
161 }
162}
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000163
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000164namespace {
165// returns true if this is a "special" texture because of gpu NPOT limitations
166bool gen_texture_key_values(const GrGpu* gpu,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000167 const GrSamplerState* sampler,
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000168 GrContext::TextureKey clientKey,
169 int width,
170 int height,
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000171 int sampleCnt,
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000172 bool scratch,
173 uint32_t v[4]) {
174 GR_STATIC_ASSERT(sizeof(GrContext::TextureKey) == sizeof(uint64_t));
175 // we assume we only need 16 bits of width and height
176 // assert that texture creation will fail anyway if this assumption
177 // would cause key collisions.
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000178 GrAssert(gpu->getCaps().fMaxTextureSize <= SK_MaxU16);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000179 v[0] = clientKey & 0xffffffffUL;
180 v[1] = (clientKey >> 32) & 0xffffffffUL;
181 v[2] = width | (height << 16);
182
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000183 v[3] = (sampleCnt << 24);
184 GrAssert(sampleCnt >= 0 && sampleCnt < 256);
185
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000186 if (!gpu->getCaps().fNPOTTextureTileSupport) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000187 bool isPow2 = GrIsPow2(width) && GrIsPow2(height);
188
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000189 bool tiled = NULL != sampler &&
190 ((sampler->getWrapX() != GrSamplerState::kClamp_WrapMode) ||
191 (sampler->getWrapY() != GrSamplerState::kClamp_WrapMode));
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000192
193 if (tiled && !isPow2) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000194 v[3] |= kNPOTBit;
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000195 if (GrSamplerState::kNearest_Filter != sampler->getFilter()) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000196 v[3] |= kFilterBit;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000197 }
198 }
199 }
200
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000201 if (scratch) {
202 v[3] |= kScratchBit;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000203 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000204
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000205 v[3] |= kTextureBit;
206
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000207 return v[3] & kNPOTBit;
208}
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000209
210// we should never have more than one stencil buffer with same combo of
211// (width,height,samplecount)
212void gen_stencil_key_values(int width, int height,
213 int sampleCnt, uint32_t v[4]) {
214 v[0] = width;
215 v[1] = height;
216 v[2] = sampleCnt;
217 v[3] = kStencilBufferBit;
218}
219
220void gen_stencil_key_values(const GrStencilBuffer* sb,
221 uint32_t v[4]) {
222 gen_stencil_key_values(sb->width(), sb->height(),
223 sb->numSamples(), v);
224}
bsalomon@google.com82c7bd82011-11-09 15:32:29 +0000225
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000226void build_kernel(float sigma, float* kernel, int kernelWidth) {
227 int halfWidth = (kernelWidth - 1) / 2;
228 float sum = 0.0f;
229 float denom = 1.0f / (2.0f * sigma * sigma);
230 for (int i = 0; i < kernelWidth; ++i) {
231 float x = static_cast<float>(i - halfWidth);
232 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
233 // is dropped here, since we renormalize the kernel below.
234 kernel[i] = sk_float_exp(- x * x * denom);
235 sum += kernel[i];
236 }
237 // Normalize the kernel
238 float scale = 1.0f / sum;
239 for (int i = 0; i < kernelWidth; ++i)
240 kernel[i] *= scale;
241}
242
243void scale_rect(SkRect* rect, float xScale, float yScale) {
244 rect->fLeft *= xScale;
245 rect->fTop *= yScale;
246 rect->fRight *= xScale;
247 rect->fBottom *= yScale;
248}
249
250float adjust_sigma(float sigma, int *scaleFactor, int *halfWidth,
251 int *kernelWidth) {
252 *scaleFactor = 1;
253 while (sigma > MAX_BLUR_SIGMA) {
254 *scaleFactor *= 2;
255 sigma *= 0.5f;
256 }
257 *halfWidth = static_cast<int>(ceilf(sigma * 3.0f));
258 *kernelWidth = *halfWidth * 2 + 1;
259 return sigma;
260}
261
262void apply_morphology(GrGpu* gpu,
263 GrTexture* texture,
264 const SkRect& rect,
265 int radius,
266 GrSamplerState::Filter filter,
267 GrSamplerState::FilterDirection direction) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000268 GrAssert(filter == GrSamplerState::kErode_Filter ||
269 filter == GrSamplerState::kDilate_Filter);
270
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000271 GrRenderTarget* target = gpu->drawState()->getRenderTarget();
272 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000273 GrDrawState* drawState = gpu->drawState();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000274 drawState->setRenderTarget(target);
275 GrMatrix sampleM;
276 sampleM.setIDiv(texture->width(), texture->height());
277 drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode, filter,
278 sampleM);
279 drawState->sampler(0)->setMorphologyRadius(radius);
280 drawState->sampler(0)->setFilterDirection(direction);
281 drawState->setTexture(0, texture);
282 gpu->drawSimpleRect(rect, NULL, 1 << 0);
283}
284
285void convolve(GrGpu* gpu,
286 GrTexture* texture,
287 const SkRect& rect,
288 const float* kernel,
289 int kernelWidth,
290 GrSamplerState::FilterDirection direction) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000291 GrRenderTarget* target = gpu->drawState()->getRenderTarget();
292 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000293 GrDrawState* drawState = gpu->drawState();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000294 drawState->setRenderTarget(target);
295 GrMatrix sampleM;
296 sampleM.setIDiv(texture->width(), texture->height());
297 drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode,
298 GrSamplerState::kConvolution_Filter,
299 sampleM);
300 drawState->sampler(0)->setConvolutionParams(kernelWidth, kernel);
301 drawState->sampler(0)->setFilterDirection(direction);
302 drawState->setTexture(0, texture);
303 gpu->drawSimpleRect(rect, NULL, 1 << 0);
304}
305
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000306}
307
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000308GrContext::TextureCacheEntry GrContext::findAndLockTexture(
309 TextureKey key,
310 int width,
311 int height,
312 const GrSamplerState* sampler) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000313 uint32_t v[4];
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000314 gen_texture_key_values(fGpu, sampler, key, width, height, 0, false, v);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000315 GrResourceKey resourceKey(v);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000316 return TextureCacheEntry(fTextureCache->findAndLock(resourceKey,
317 GrResourceCache::kNested_LockType));
318}
319
bsalomon@google.comfb309512011-11-30 14:13:48 +0000320bool GrContext::isTextureInCache(TextureKey key,
321 int width,
322 int height,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000323 const GrSamplerState* sampler) const {
bsalomon@google.comfb309512011-11-30 14:13:48 +0000324 uint32_t v[4];
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000325 gen_texture_key_values(fGpu, sampler, key, width, height, 0, false, v);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000326 GrResourceKey resourceKey(v);
327 return fTextureCache->hasKey(resourceKey);
328}
329
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000330GrResourceEntry* GrContext::addAndLockStencilBuffer(GrStencilBuffer* sb) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000331 ASSERT_OWNED_RESOURCE(sb);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000332 uint32_t v[4];
333 gen_stencil_key_values(sb, v);
334 GrResourceKey resourceKey(v);
335 return fTextureCache->createAndLock(resourceKey, sb);
336}
337
338GrStencilBuffer* GrContext::findStencilBuffer(int width, int height,
339 int sampleCnt) {
340 uint32_t v[4];
341 gen_stencil_key_values(width, height, sampleCnt, v);
342 GrResourceKey resourceKey(v);
343 GrResourceEntry* entry = fTextureCache->findAndLock(resourceKey,
344 GrResourceCache::kSingle_LockType);
345 if (NULL != entry) {
346 GrStencilBuffer* sb = (GrStencilBuffer*) entry->resource();
347 return sb;
348 } else {
349 return NULL;
350 }
351}
352
353void GrContext::unlockStencilBuffer(GrResourceEntry* sbEntry) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000354 ASSERT_OWNED_RESOURCE(sbEntry->resource());
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000355 fTextureCache->unlock(sbEntry);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000356}
357
358static void stretchImage(void* dst,
359 int dstW,
360 int dstH,
361 void* src,
362 int srcW,
363 int srcH,
364 int bpp) {
365 GrFixed dx = (srcW << 16) / dstW;
366 GrFixed dy = (srcH << 16) / dstH;
367
368 GrFixed y = dy >> 1;
369
370 int dstXLimit = dstW*bpp;
371 for (int j = 0; j < dstH; ++j) {
372 GrFixed x = dx >> 1;
373 void* srcRow = (uint8_t*)src + (y>>16)*srcW*bpp;
374 void* dstRow = (uint8_t*)dst + j*dstW*bpp;
375 for (int i = 0; i < dstXLimit; i += bpp) {
376 memcpy((uint8_t*) dstRow + i,
377 (uint8_t*) srcRow + (x>>16)*bpp,
378 bpp);
379 x += dx;
380 }
381 y += dy;
382 }
383}
384
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000385GrContext::TextureCacheEntry GrContext::createAndLockTexture(
386 TextureKey key,
387 const GrSamplerState* sampler,
388 const GrTextureDesc& desc,
389 void* srcData,
390 size_t rowBytes) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000391 SK_TRACE_EVENT0("GrContext::createAndLockTexture");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000392
393#if GR_DUMP_TEXTURE_UPLOAD
394 GrPrintf("GrContext::createAndLockTexture [%d %d]\n", desc.fWidth, desc.fHeight);
395#endif
396
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000397 TextureCacheEntry entry;
398 uint32_t v[4];
399 bool special = gen_texture_key_values(fGpu, sampler, key,
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000400 desc.fWidth, desc.fHeight,
401 desc.fSampleCnt, false, v);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000402 GrResourceKey resourceKey(v);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000403
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000404 if (special) {
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000405 GrAssert(NULL != sampler);
406 TextureCacheEntry clampEntry = this->findAndLockTexture(key,
407 desc.fWidth,
408 desc.fHeight,
409 NULL);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000410
411 if (NULL == clampEntry.texture()) {
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000412 clampEntry = this->createAndLockTexture(key, NULL, desc,
413 srcData, rowBytes);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000414 GrAssert(NULL != clampEntry.texture());
415 if (NULL == clampEntry.texture()) {
416 return entry;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000417 }
418 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000419 GrTextureDesc rtDesc = desc;
420 rtDesc.fFlags = rtDesc.fFlags |
421 kRenderTarget_GrTextureFlagBit |
422 kNoStencil_GrTextureFlagBit;
bsalomon@google.com99621082011-11-15 16:47:16 +0000423 rtDesc.fWidth = GrNextPow2(GrMax(desc.fWidth, 64));
424 rtDesc.fHeight = GrNextPow2(GrMax(desc.fHeight, 64));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000425
426 GrTexture* texture = fGpu->createTexture(rtDesc, NULL, 0);
427
428 if (NULL != texture) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000429 GrDrawTarget::AutoStateRestore asr(fGpu,
430 GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000431 GrDrawState* drawState = fGpu->drawState();
432 drawState->setRenderTarget(texture->asRenderTarget());
433 drawState->setTexture(0, clampEntry.texture());
bsalomon@google.com82c7bd82011-11-09 15:32:29 +0000434
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000435 GrSamplerState::Filter filter;
436 // if filtering is not desired then we want to ensure all
437 // texels in the resampled image are copies of texels from
438 // the original.
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000439 if (GrSamplerState::kNearest_Filter == sampler->getFilter()) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000440 filter = GrSamplerState::kNearest_Filter;
441 } else {
442 filter = GrSamplerState::kBilinear_Filter;
443 }
bsalomon@google.com1e266f82011-12-12 16:11:33 +0000444 drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode,
445 filter);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000446
447 static const GrVertexLayout layout =
448 GrDrawTarget::StageTexCoordVertexLayoutBit(0,0);
449 GrDrawTarget::AutoReleaseGeometry arg(fGpu, layout, 4, 0);
450
451 if (arg.succeeded()) {
452 GrPoint* verts = (GrPoint*) arg.vertices();
453 verts[0].setIRectFan(0, 0,
454 texture->width(),
455 texture->height(),
456 2*sizeof(GrPoint));
457 verts[1].setIRectFan(0, 0, 1, 1, 2*sizeof(GrPoint));
458 fGpu->drawNonIndexed(kTriangleFan_PrimitiveType,
459 0, 4);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000460 entry.set(fTextureCache->createAndLock(resourceKey, texture));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000461 }
bsalomon@google.com1da07462011-03-10 14:51:57 +0000462 texture->releaseRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000463 } else {
464 // TODO: Our CPU stretch doesn't filter. But we create separate
465 // stretched textures when the sampler state is either filtered or
466 // not. Either implement filtered stretch blit on CPU or just create
467 // one when FBO case fails.
468
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000469 rtDesc.fFlags = kNone_GrTextureFlags;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000470 // no longer need to clamp at min RT size.
471 rtDesc.fWidth = GrNextPow2(desc.fWidth);
472 rtDesc.fHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000473 int bpp = GrBytesPerPixel(desc.fConfig);
bsalomon@google.com3582bf92011-06-30 21:32:31 +0000474 SkAutoSMalloc<128*128*4> stretchedPixels(bpp *
bsalomon@google.com27847de2011-02-22 20:59:41 +0000475 rtDesc.fWidth *
476 rtDesc.fHeight);
477 stretchImage(stretchedPixels.get(), rtDesc.fWidth, rtDesc.fHeight,
478 srcData, desc.fWidth, desc.fHeight, bpp);
479
480 size_t stretchedRowBytes = rtDesc.fWidth * bpp;
481
482 GrTexture* texture = fGpu->createTexture(rtDesc,
483 stretchedPixels.get(),
484 stretchedRowBytes);
485 GrAssert(NULL != texture);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000486 entry.set(fTextureCache->createAndLock(resourceKey, texture));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000487 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000488 fTextureCache->unlock(clampEntry.cacheEntry());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000489
490 } else {
491 GrTexture* texture = fGpu->createTexture(desc, srcData, rowBytes);
492 if (NULL != texture) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000493 entry.set(fTextureCache->createAndLock(resourceKey, texture));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000494 }
495 }
496 return entry;
497}
498
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000499namespace {
500inline void gen_scratch_tex_key_values(const GrGpu* gpu,
501 const GrTextureDesc& desc,
502 uint32_t v[4]) {
503 // Instead of a client-provided key of the texture contents
504 // we create a key of from the descriptor.
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000505 GrContext::TextureKey descKey = (desc.fFlags << 8) |
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000506 ((uint64_t) desc.fConfig << 32);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000507 // this code path isn't friendly to tiling with NPOT restricitons
508 // We just pass ClampNoFilter()
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000509 gen_texture_key_values(gpu, NULL, descKey, desc.fWidth,
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000510 desc.fHeight, desc.fSampleCnt, true, v);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000511}
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000512}
513
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000514GrContext::TextureCacheEntry GrContext::lockScratchTexture(
515 const GrTextureDesc& inDesc,
516 ScratchTexMatch match) {
517
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000518 GrTextureDesc desc = inDesc;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000519 if (kExact_ScratchTexMatch != match) {
520 // bin by pow2 with a reasonable min
521 static const int MIN_SIZE = 256;
522 desc.fWidth = GrMax(MIN_SIZE, GrNextPow2(desc.fWidth));
523 desc.fHeight = GrMax(MIN_SIZE, GrNextPow2(desc.fHeight));
524 }
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000525
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000526 GrResourceEntry* entry;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000527 int origWidth = desc.fWidth;
528 int origHeight = desc.fHeight;
529 bool doubledW = false;
530 bool doubledH = false;
531
532 do {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000533 uint32_t v[4];
534 gen_scratch_tex_key_values(fGpu, desc, v);
535 GrResourceKey key(v);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000536 entry = fTextureCache->findAndLock(key,
537 GrResourceCache::kNested_LockType);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000538 // if we miss, relax the fit of the flags...
539 // then try doubling width... then height.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000540 if (NULL != entry || kExact_ScratchTexMatch == match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000541 break;
542 }
543 if (!(desc.fFlags & kRenderTarget_GrTextureFlagBit)) {
544 desc.fFlags = desc.fFlags | kRenderTarget_GrTextureFlagBit;
545 } else if (desc.fFlags & kNoStencil_GrTextureFlagBit) {
546 desc.fFlags = desc.fFlags & ~kNoStencil_GrTextureFlagBit;
547 } else if (!doubledW) {
548 desc.fFlags = inDesc.fFlags;
549 desc.fWidth *= 2;
550 doubledW = true;
551 } else if (!doubledH) {
552 desc.fFlags = inDesc.fFlags;
553 desc.fWidth = origWidth;
554 desc.fHeight *= 2;
555 doubledH = true;
556 } else {
557 break;
558 }
559
560 } while (true);
561
562 if (NULL == entry) {
563 desc.fFlags = inDesc.fFlags;
564 desc.fWidth = origWidth;
565 desc.fHeight = origHeight;
566 GrTexture* texture = fGpu->createTexture(desc, NULL, 0);
567 if (NULL != texture) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000568 uint32_t v[4];
569 gen_scratch_tex_key_values(fGpu, desc, v);
570 GrResourceKey key(v);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000571 entry = fTextureCache->createAndLock(key, texture);
572 }
573 }
574
575 // If the caller gives us the same desc/sampler twice we don't want
576 // to return the same texture the second time (unless it was previously
577 // released). So we detach the entry from the cache and reattach at release.
578 if (NULL != entry) {
579 fTextureCache->detach(entry);
580 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000581 return TextureCacheEntry(entry);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000582}
583
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000584void GrContext::unlockTexture(TextureCacheEntry entry) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000585 ASSERT_OWNED_RESOURCE(entry.texture());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000586 // If this is a scratch texture we detached it from the cache
587 // while it was locked (to avoid two callers simultaneously getting
588 // the same texture).
589 if (kScratchBit & entry.cacheEntry()->key().getValue32(3)) {
590 fTextureCache->reattachAndUnlock(entry.cacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000591 } else {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000592 fTextureCache->unlock(entry.cacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000593 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000594}
595
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000596GrTexture* GrContext::createUncachedTexture(const GrTextureDesc& desc,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000597 void* srcData,
598 size_t rowBytes) {
599 return fGpu->createTexture(desc, srcData, rowBytes);
600}
601
602void GrContext::getTextureCacheLimits(int* maxTextures,
603 size_t* maxTextureBytes) const {
604 fTextureCache->getLimits(maxTextures, maxTextureBytes);
605}
606
607void GrContext::setTextureCacheLimits(int maxTextures, size_t maxTextureBytes) {
608 fTextureCache->setLimits(maxTextures, maxTextureBytes);
609}
610
bsalomon@google.com91958362011-06-13 17:58:13 +0000611int GrContext::getMaxTextureSize() const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000612 return fGpu->getCaps().fMaxTextureSize;
bsalomon@google.com91958362011-06-13 17:58:13 +0000613}
614
615int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000616 return fGpu->getCaps().fMaxRenderTargetSize;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000617}
618
619///////////////////////////////////////////////////////////////////////////////
620
bsalomon@google.come269f212011-11-07 13:29:52 +0000621GrTexture* GrContext::createPlatformTexture(const GrPlatformTextureDesc& desc) {
622 return fGpu->createPlatformTexture(desc);
623}
624
625GrRenderTarget* GrContext::createPlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
626 return fGpu->createPlatformRenderTarget(desc);
627}
628
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000629///////////////////////////////////////////////////////////////////////////////
630
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000631bool GrContext::supportsIndex8PixelConfig(const GrSamplerState* sampler,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000632 int width, int height) const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000633 const GrDrawTarget::Caps& caps = fGpu->getCaps();
634 if (!caps.f8BitPaletteSupport) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000635 return false;
636 }
637
bsalomon@google.com27847de2011-02-22 20:59:41 +0000638 bool isPow2 = GrIsPow2(width) && GrIsPow2(height);
639
640 if (!isPow2) {
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000641 bool tiled = NULL != sampler &&
642 (sampler->getWrapX() != GrSamplerState::kClamp_WrapMode ||
643 sampler->getWrapY() != GrSamplerState::kClamp_WrapMode);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000644 if (tiled && !caps.fNPOTTextureTileSupport) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000645 return false;
646 }
647 }
648 return true;
649}
650
651////////////////////////////////////////////////////////////////////////////////
652
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000653const GrClip& GrContext::getClip() const { return fGpu->getClip(); }
654
bsalomon@google.com27847de2011-02-22 20:59:41 +0000655void GrContext::setClip(const GrClip& clip) {
656 fGpu->setClip(clip);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000657 fDrawState->enableState(GrDrawState::kClip_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000658}
659
660void GrContext::setClip(const GrIRect& rect) {
661 GrClip clip;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000662 clip.setFromIRect(rect);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000663 fGpu->setClip(clip);
664}
665
666////////////////////////////////////////////////////////////////////////////////
667
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000668void GrContext::clear(const GrIRect* rect, const GrColor color) {
bsalomon@google.com398109c2011-04-14 18:40:27 +0000669 this->flush();
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000670 fGpu->clear(rect, color);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000671}
672
673void GrContext::drawPaint(const GrPaint& paint) {
674 // set rect to be big enough to fill the space, but not super-huge, so we
675 // don't overflow fixed-point implementations
bsalomon@google.comd302f142011-03-03 13:54:13 +0000676 GrRect r;
677 r.setLTRB(0, 0,
678 GrIntToScalar(getRenderTarget()->width()),
679 GrIntToScalar(getRenderTarget()->height()));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000680 GrMatrix inverse;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000681 SkTLazy<GrPaint> tmpPaint;
682 const GrPaint* p = &paint;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000683 GrAutoMatrix am;
684
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000685 // We attempt to map r by the inverse matrix and draw that. mapRect will
686 // map the four corners and bound them with a new rect. This will not
687 // produce a correct result for some perspective matrices.
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000688 if (!this->getMatrix().hasPerspective()) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000689 if (!fDrawState->getViewInverse(&inverse)) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000690 GrPrintf("Could not invert matrix");
691 return;
692 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000693 inverse.mapRect(&r);
694 } else {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000695 if (paint.getActiveMaskStageMask() || paint.getActiveStageMask()) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000696 if (!fDrawState->getViewInverse(&inverse)) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000697 GrPrintf("Could not invert matrix");
698 return;
699 }
700 tmpPaint.set(paint);
701 tmpPaint.get()->preConcatActiveSamplerMatrices(inverse);
702 p = tmpPaint.get();
703 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000704 am.set(this, GrMatrix::I());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000705 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000706 // by definition this fills the entire clip, no need for AA
707 if (paint.fAntiAlias) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000708 if (!tmpPaint.isValid()) {
709 tmpPaint.set(paint);
710 p = tmpPaint.get();
711 }
712 GrAssert(p == tmpPaint.get());
713 tmpPaint.get()->fAntiAlias = false;
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000714 }
715 this->drawRect(*p, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000716}
717
bsalomon@google.com205d4602011-04-25 12:43:45 +0000718////////////////////////////////////////////////////////////////////////////////
719
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000720namespace {
721inline bool disable_coverage_aa_for_blend(GrDrawTarget* target) {
722 return DISABLE_COVERAGE_AA_FOR_BLEND && !target->canApplyCoverage();
723}
724}
725
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000726////////////////////////////////////////////////////////////////////////////////
727
bsalomon@google.com27847de2011-02-22 20:59:41 +0000728/* create a triangle strip that strokes the specified triangle. There are 8
729 unique vertices, but we repreat the last 2 to close up. Alternatively we
730 could use an indices array, and then only send 8 verts, but not sure that
731 would be faster.
732 */
bsalomon@google.com205d4602011-04-25 12:43:45 +0000733static void setStrokeRectStrip(GrPoint verts[10], GrRect rect,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000734 GrScalar width) {
735 const GrScalar rad = GrScalarHalf(width);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000736 rect.sort();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000737
738 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
739 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
740 verts[2].set(rect.fRight - rad, rect.fTop + rad);
741 verts[3].set(rect.fRight + rad, rect.fTop - rad);
742 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
743 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
744 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
745 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
746 verts[8] = verts[0];
747 verts[9] = verts[1];
748}
749
bsalomon@google.com205d4602011-04-25 12:43:45 +0000750static void setInsetFan(GrPoint* pts, size_t stride,
751 const GrRect& r, GrScalar dx, GrScalar dy) {
752 pts->setRectFan(r.fLeft + dx, r.fTop + dy, r.fRight - dx, r.fBottom - dy, stride);
753}
754
755static const uint16_t gFillAARectIdx[] = {
756 0, 1, 5, 5, 4, 0,
757 1, 2, 6, 6, 5, 1,
758 2, 3, 7, 7, 6, 2,
759 3, 0, 4, 4, 7, 3,
760 4, 5, 6, 6, 7, 4,
761};
762
763int GrContext::aaFillRectIndexCount() const {
764 return GR_ARRAY_COUNT(gFillAARectIdx);
765}
766
767GrIndexBuffer* GrContext::aaFillRectIndexBuffer() {
768 if (NULL == fAAFillRectIndexBuffer) {
769 fAAFillRectIndexBuffer = fGpu->createIndexBuffer(sizeof(gFillAARectIdx),
770 false);
bsalomon@google.com9b09c9e2011-08-31 13:33:40 +0000771 if (NULL != fAAFillRectIndexBuffer) {
772 #if GR_DEBUG
773 bool updated =
774 #endif
775 fAAFillRectIndexBuffer->updateData(gFillAARectIdx,
776 sizeof(gFillAARectIdx));
777 GR_DEBUGASSERT(updated);
778 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000779 }
780 return fAAFillRectIndexBuffer;
781}
782
783static const uint16_t gStrokeAARectIdx[] = {
784 0 + 0, 1 + 0, 5 + 0, 5 + 0, 4 + 0, 0 + 0,
785 1 + 0, 2 + 0, 6 + 0, 6 + 0, 5 + 0, 1 + 0,
786 2 + 0, 3 + 0, 7 + 0, 7 + 0, 6 + 0, 2 + 0,
787 3 + 0, 0 + 0, 4 + 0, 4 + 0, 7 + 0, 3 + 0,
788
789 0 + 4, 1 + 4, 5 + 4, 5 + 4, 4 + 4, 0 + 4,
790 1 + 4, 2 + 4, 6 + 4, 6 + 4, 5 + 4, 1 + 4,
791 2 + 4, 3 + 4, 7 + 4, 7 + 4, 6 + 4, 2 + 4,
792 3 + 4, 0 + 4, 4 + 4, 4 + 4, 7 + 4, 3 + 4,
793
794 0 + 8, 1 + 8, 5 + 8, 5 + 8, 4 + 8, 0 + 8,
795 1 + 8, 2 + 8, 6 + 8, 6 + 8, 5 + 8, 1 + 8,
796 2 + 8, 3 + 8, 7 + 8, 7 + 8, 6 + 8, 2 + 8,
797 3 + 8, 0 + 8, 4 + 8, 4 + 8, 7 + 8, 3 + 8,
798};
799
800int GrContext::aaStrokeRectIndexCount() const {
801 return GR_ARRAY_COUNT(gStrokeAARectIdx);
802}
803
804GrIndexBuffer* GrContext::aaStrokeRectIndexBuffer() {
805 if (NULL == fAAStrokeRectIndexBuffer) {
806 fAAStrokeRectIndexBuffer = fGpu->createIndexBuffer(sizeof(gStrokeAARectIdx),
807 false);
bsalomon@google.com9b09c9e2011-08-31 13:33:40 +0000808 if (NULL != fAAStrokeRectIndexBuffer) {
809 #if GR_DEBUG
810 bool updated =
811 #endif
812 fAAStrokeRectIndexBuffer->updateData(gStrokeAARectIdx,
813 sizeof(gStrokeAARectIdx));
814 GR_DEBUGASSERT(updated);
815 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000816 }
817 return fAAStrokeRectIndexBuffer;
818}
819
bsalomon@google.coma3108262011-10-10 14:08:47 +0000820static GrVertexLayout aa_rect_layout(const GrDrawTarget* target,
821 bool useCoverage) {
822 GrVertexLayout layout = 0;
tomhudson@google.com93813632011-10-27 20:21:16 +0000823 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000824 if (NULL != target->getDrawState().getTexture(s)) {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000825 layout |= GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(s);
826 }
827 }
828 if (useCoverage) {
829 layout |= GrDrawTarget::kCoverage_VertexLayoutBit;
830 } else {
831 layout |= GrDrawTarget::kColor_VertexLayoutBit;
832 }
833 return layout;
834}
835
bsalomon@google.com205d4602011-04-25 12:43:45 +0000836void GrContext::fillAARect(GrDrawTarget* target,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000837 const GrRect& devRect,
838 bool useVertexCoverage) {
839 GrVertexLayout layout = aa_rect_layout(target, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000840
841 size_t vsize = GrDrawTarget::VertexSize(layout);
842
843 GrDrawTarget::AutoReleaseGeometry geo(target, layout, 8, 0);
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000844 if (!geo.succeeded()) {
845 GrPrintf("Failed to get space for vertices!\n");
846 return;
847 }
bsalomon@google.com9b09c9e2011-08-31 13:33:40 +0000848 GrIndexBuffer* indexBuffer = this->aaFillRectIndexBuffer();
849 if (NULL == indexBuffer) {
850 GrPrintf("Failed to create index buffer!\n");
851 return;
852 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000853
854 intptr_t verts = reinterpret_cast<intptr_t>(geo.vertices());
855
856 GrPoint* fan0Pos = reinterpret_cast<GrPoint*>(verts);
857 GrPoint* fan1Pos = reinterpret_cast<GrPoint*>(verts + 4 * vsize);
858
859 setInsetFan(fan0Pos, vsize, devRect, -GR_ScalarHalf, -GR_ScalarHalf);
860 setInsetFan(fan1Pos, vsize, devRect, GR_ScalarHalf, GR_ScalarHalf);
861
862 verts += sizeof(GrPoint);
863 for (int i = 0; i < 4; ++i) {
864 *reinterpret_cast<GrColor*>(verts + i * vsize) = 0;
865 }
866
bsalomon@google.coma3108262011-10-10 14:08:47 +0000867 GrColor innerColor;
868 if (useVertexCoverage) {
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000869 innerColor = 0xffffffff;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000870 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000871 innerColor = target->getDrawState().getColor();
bsalomon@google.coma3108262011-10-10 14:08:47 +0000872 }
873
bsalomon@google.com205d4602011-04-25 12:43:45 +0000874 verts += 4 * vsize;
875 for (int i = 0; i < 4; ++i) {
876 *reinterpret_cast<GrColor*>(verts + i * vsize) = innerColor;
877 }
878
bsalomon@google.com9b09c9e2011-08-31 13:33:40 +0000879 target->setIndexSourceToBuffer(indexBuffer);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000880
881 target->drawIndexed(kTriangles_PrimitiveType, 0,
882 0, 8, this->aaFillRectIndexCount());
883}
884
bsalomon@google.coma3108262011-10-10 14:08:47 +0000885void GrContext::strokeAARect(GrDrawTarget* target,
886 const GrRect& devRect,
887 const GrVec& devStrokeSize,
888 bool useVertexCoverage) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000889 const GrScalar& dx = devStrokeSize.fX;
890 const GrScalar& dy = devStrokeSize.fY;
891 const GrScalar rx = GrMul(dx, GR_ScalarHalf);
892 const GrScalar ry = GrMul(dy, GR_ScalarHalf);
893
bsalomon@google.com205d4602011-04-25 12:43:45 +0000894 GrScalar spare;
895 {
896 GrScalar w = devRect.width() - dx;
897 GrScalar h = devRect.height() - dy;
898 spare = GrMin(w, h);
899 }
900
901 if (spare <= 0) {
902 GrRect r(devRect);
903 r.inset(-rx, -ry);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000904 fillAARect(target, r, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000905 return;
906 }
bsalomon@google.coma3108262011-10-10 14:08:47 +0000907 GrVertexLayout layout = aa_rect_layout(target, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000908 size_t vsize = GrDrawTarget::VertexSize(layout);
909
910 GrDrawTarget::AutoReleaseGeometry geo(target, layout, 16, 0);
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000911 if (!geo.succeeded()) {
912 GrPrintf("Failed to get space for vertices!\n");
913 return;
914 }
bsalomon@google.com9b09c9e2011-08-31 13:33:40 +0000915 GrIndexBuffer* indexBuffer = this->aaStrokeRectIndexBuffer();
916 if (NULL == indexBuffer) {
917 GrPrintf("Failed to create index buffer!\n");
918 return;
919 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000920
921 intptr_t verts = reinterpret_cast<intptr_t>(geo.vertices());
922
923 GrPoint* fan0Pos = reinterpret_cast<GrPoint*>(verts);
924 GrPoint* fan1Pos = reinterpret_cast<GrPoint*>(verts + 4 * vsize);
925 GrPoint* fan2Pos = reinterpret_cast<GrPoint*>(verts + 8 * vsize);
926 GrPoint* fan3Pos = reinterpret_cast<GrPoint*>(verts + 12 * vsize);
927
928 setInsetFan(fan0Pos, vsize, devRect, -rx - GR_ScalarHalf, -ry - GR_ScalarHalf);
929 setInsetFan(fan1Pos, vsize, devRect, -rx + GR_ScalarHalf, -ry + GR_ScalarHalf);
930 setInsetFan(fan2Pos, vsize, devRect, rx - GR_ScalarHalf, ry - GR_ScalarHalf);
931 setInsetFan(fan3Pos, vsize, devRect, rx + GR_ScalarHalf, ry + GR_ScalarHalf);
932
933 verts += sizeof(GrPoint);
934 for (int i = 0; i < 4; ++i) {
935 *reinterpret_cast<GrColor*>(verts + i * vsize) = 0;
936 }
937
bsalomon@google.coma3108262011-10-10 14:08:47 +0000938 GrColor innerColor;
939 if (useVertexCoverage) {
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000940 innerColor = 0xffffffff;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000941 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000942 innerColor = target->getDrawState().getColor();
bsalomon@google.coma3108262011-10-10 14:08:47 +0000943 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000944 verts += 4 * vsize;
945 for (int i = 0; i < 8; ++i) {
946 *reinterpret_cast<GrColor*>(verts + i * vsize) = innerColor;
947 }
948
949 verts += 8 * vsize;
950 for (int i = 0; i < 8; ++i) {
951 *reinterpret_cast<GrColor*>(verts + i * vsize) = 0;
952 }
953
bsalomon@google.com9b09c9e2011-08-31 13:33:40 +0000954 target->setIndexSourceToBuffer(indexBuffer);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000955 target->drawIndexed(kTriangles_PrimitiveType,
956 0, 0, 16, aaStrokeRectIndexCount());
957}
958
reed@google.com20efde72011-05-09 17:00:02 +0000959/**
960 * Returns true if the rects edges are integer-aligned.
961 */
962static bool isIRect(const GrRect& r) {
963 return GrScalarIsInt(r.fLeft) && GrScalarIsInt(r.fTop) &&
964 GrScalarIsInt(r.fRight) && GrScalarIsInt(r.fBottom);
965}
966
bsalomon@google.com205d4602011-04-25 12:43:45 +0000967static bool apply_aa_to_rect(GrDrawTarget* target,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000968 const GrRect& rect,
969 GrScalar width,
970 const GrMatrix* matrix,
971 GrMatrix* combinedMatrix,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000972 GrRect* devRect,
973 bool* useVertexCoverage) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000974 // we use a simple coverage ramp to do aa on axis-aligned rects
975 // we check if the rect will be axis-aligned, and the rect won't land on
976 // integer coords.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000977
bsalomon@google.coma3108262011-10-10 14:08:47 +0000978 // we are keeping around the "tweak the alpha" trick because
979 // it is our only hope for the fixed-pipe implementation.
980 // In a shader implementation we can give a separate coverage input
bsalomon@google.com289533a2011-10-27 12:34:25 +0000981 // TODO: remove this ugliness when we drop the fixed-pipe impl
bsalomon@google.coma3108262011-10-10 14:08:47 +0000982 *useVertexCoverage = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000983 if (!target->canTweakAlphaForCoverage()) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000984 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +0000985#if GR_DEBUG
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000986 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +0000987#endif
bsalomon@google.coma3108262011-10-10 14:08:47 +0000988 return false;
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000989 } else {
990 *useVertexCoverage = true;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000991 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000992 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000993 const GrDrawState& drawState = target->getDrawState();
994 if (drawState.getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000995 return false;
996 }
997
bsalomon@google.com471d4712011-08-23 15:45:25 +0000998 if (0 == width && target->willUseHWAALines()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000999 return false;
1000 }
1001
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001002 if (!drawState.getViewMatrix().preservesAxisAlignment()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +00001003 return false;
1004 }
1005
1006 if (NULL != matrix &&
1007 !matrix->preservesAxisAlignment()) {
1008 return false;
1009 }
1010
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001011 *combinedMatrix = drawState.getViewMatrix();
bsalomon@google.com205d4602011-04-25 12:43:45 +00001012 if (NULL != matrix) {
1013 combinedMatrix->preConcat(*matrix);
1014 GrAssert(combinedMatrix->preservesAxisAlignment());
1015 }
1016
1017 combinedMatrix->mapRect(devRect, rect);
1018 devRect->sort();
1019
1020 if (width < 0) {
reed@google.com20efde72011-05-09 17:00:02 +00001021 return !isIRect(*devRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +00001022 } else {
1023 return true;
1024 }
1025}
1026
bsalomon@google.com27847de2011-02-22 20:59:41 +00001027void GrContext::drawRect(const GrPaint& paint,
1028 const GrRect& rect,
1029 GrScalar width,
1030 const GrMatrix* matrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001031 SK_TRACE_EVENT0("GrContext::drawRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +00001032
1033 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001034 int stageMask = paint.getActiveStageMask();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001035
bsalomon@google.com205d4602011-04-25 12:43:45 +00001036 GrRect devRect = rect;
1037 GrMatrix combinedMatrix;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001038 bool useVertexCoverage;
bsalomon@google.com289533a2011-10-27 12:34:25 +00001039 bool needAA = paint.fAntiAlias &&
1040 !this->getRenderTarget()->isMultisampled();
1041 bool doAA = needAA && apply_aa_to_rect(target, rect, width, matrix,
1042 &combinedMatrix, &devRect,
1043 &useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +00001044
1045 if (doAA) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001046 GrDrawTarget::AutoDeviceCoordDraw adcd(target, stageMask);
bsalomon@google.com205d4602011-04-25 12:43:45 +00001047 if (width >= 0) {
1048 GrVec strokeSize;;
1049 if (width > 0) {
1050 strokeSize.set(width, width);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001051 combinedMatrix.mapVectors(&strokeSize, 1);
bsalomon@google.com205d4602011-04-25 12:43:45 +00001052 strokeSize.setAbs(strokeSize);
1053 } else {
1054 strokeSize.set(GR_Scalar1, GR_Scalar1);
1055 }
bsalomon@google.coma3108262011-10-10 14:08:47 +00001056 strokeAARect(target, devRect, strokeSize, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +00001057 } else {
bsalomon@google.coma3108262011-10-10 14:08:47 +00001058 fillAARect(target, devRect, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +00001059 }
1060 return;
1061 }
1062
bsalomon@google.com27847de2011-02-22 20:59:41 +00001063 if (width >= 0) {
1064 // TODO: consider making static vertex buffers for these cases.
1065 // Hairline could be done by just adding closing vertex to
1066 // unitSquareVertexBuffer()
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001067 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL);
1068
bsalomon@google.com27847de2011-02-22 20:59:41 +00001069 static const int worstCaseVertCount = 10;
1070 GrDrawTarget::AutoReleaseGeometry geo(target, layout, worstCaseVertCount, 0);
1071
1072 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001073 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +00001074 return;
1075 }
1076
1077 GrPrimitiveType primType;
1078 int vertCount;
1079 GrPoint* vertex = geo.positions();
1080
1081 if (width > 0) {
1082 vertCount = 10;
1083 primType = kTriangleStrip_PrimitiveType;
1084 setStrokeRectStrip(vertex, rect, width);
1085 } else {
1086 // hairline
1087 vertCount = 5;
1088 primType = kLineStrip_PrimitiveType;
1089 vertex[0].set(rect.fLeft, rect.fTop);
1090 vertex[1].set(rect.fRight, rect.fTop);
1091 vertex[2].set(rect.fRight, rect.fBottom);
1092 vertex[3].set(rect.fLeft, rect.fBottom);
1093 vertex[4].set(rect.fLeft, rect.fTop);
1094 }
1095
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001096 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001097 if (NULL != matrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001098 GrDrawState* drawState = target->drawState();
1099 avmr.set(drawState);
1100 drawState->preConcatViewMatrix(*matrix);
1101 drawState->preConcatSamplerMatrices(stageMask, *matrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001102 }
1103
1104 target->drawNonIndexed(primType, 0, vertCount);
1105 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001106#if GR_STATIC_RECT_VB
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001107 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL);
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001108 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
1109 if (NULL == sqVB) {
1110 GrPrintf("Failed to create static rect vb.\n");
1111 return;
1112 }
1113 target->setVertexSourceToBuffer(layout, sqVB);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001114 GrDrawState* drawState = target->drawState();
1115 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001116 GrMatrix m;
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001117 m.setAll(rect.width(), 0, rect.fLeft,
bsalomon@google.com205d4602011-04-25 12:43:45 +00001118 0, rect.height(), rect.fTop,
1119 0, 0, GrMatrix::I()[8]);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001120
1121 if (NULL != matrix) {
1122 m.postConcat(*matrix);
1123 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001124 drawState->preConcatViewMatrix(m);
1125 drawState->preConcatSamplerMatrices(stageMask, m);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001126
bsalomon@google.com27847de2011-02-22 20:59:41 +00001127 target->drawNonIndexed(kTriangleFan_PrimitiveType, 0, 4);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001128#else
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001129 target->drawSimpleRect(rect, matrix, stageMask);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001130#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +00001131 }
1132}
1133
1134void GrContext::drawRectToRect(const GrPaint& paint,
1135 const GrRect& dstRect,
1136 const GrRect& srcRect,
1137 const GrMatrix* dstMatrix,
1138 const GrMatrix* srcMatrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001139 SK_TRACE_EVENT0("GrContext::drawRectToRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +00001140
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001141 // srcRect refers to paint's first texture
1142 if (NULL == paint.getTexture(0)) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001143 drawRect(paint, dstRect, -1, dstMatrix);
1144 return;
1145 }
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001146
bsalomon@google.com27847de2011-02-22 20:59:41 +00001147 GR_STATIC_ASSERT(!BATCH_RECT_TO_RECT || !GR_STATIC_RECT_VB);
1148
1149#if GR_STATIC_RECT_VB
1150 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001151 GrDrawState* drawState = target->drawState();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001152 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001153 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001154
1155 GrMatrix m;
1156
1157 m.setAll(dstRect.width(), 0, dstRect.fLeft,
1158 0, dstRect.height(), dstRect.fTop,
1159 0, 0, GrMatrix::I()[8]);
1160 if (NULL != dstMatrix) {
1161 m.postConcat(*dstMatrix);
1162 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001163 drawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001164
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001165 // srcRect refers to first stage
1166 int otherStageMask = paint.getActiveStageMask() &
1167 (~(1 << GrPaint::kFirstTextureStage));
1168 if (otherStageMask) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001169 drawState->preConcatSamplerMatrices(otherStageMask, m);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001170 }
1171
bsalomon@google.com27847de2011-02-22 20:59:41 +00001172 m.setAll(srcRect.width(), 0, srcRect.fLeft,
1173 0, srcRect.height(), srcRect.fTop,
1174 0, 0, GrMatrix::I()[8]);
1175 if (NULL != srcMatrix) {
1176 m.postConcat(*srcMatrix);
1177 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001178 drawState->sampler(GrPaint::kFirstTextureStage)->preConcatMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001179
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001180 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
1181 if (NULL == sqVB) {
1182 GrPrintf("Failed to create static rect vb.\n");
1183 return;
1184 }
1185 target->setVertexSourceToBuffer(layout, sqVB);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001186 target->drawNonIndexed(kTriangleFan_PrimitiveType, 0, 4);
1187#else
1188
1189 GrDrawTarget* target;
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001190#if BATCH_RECT_TO_RECT
bsalomon@google.com27847de2011-02-22 20:59:41 +00001191 target = this->prepareToDraw(paint, kBuffered_DrawCategory);
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001192#else
bsalomon@google.com27847de2011-02-22 20:59:41 +00001193 target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
1194#endif
1195
tomhudson@google.com93813632011-10-27 20:21:16 +00001196 const GrRect* srcRects[GrDrawState::kNumStages] = {NULL};
1197 const GrMatrix* srcMatrices[GrDrawState::kNumStages] = {NULL};
bsalomon@google.com27847de2011-02-22 20:59:41 +00001198 srcRects[0] = &srcRect;
1199 srcMatrices[0] = srcMatrix;
1200
1201 target->drawRect(dstRect, dstMatrix, 1, srcRects, srcMatrices);
1202#endif
1203}
1204
1205void GrContext::drawVertices(const GrPaint& paint,
1206 GrPrimitiveType primitiveType,
1207 int vertexCount,
1208 const GrPoint positions[],
1209 const GrPoint texCoords[],
1210 const GrColor colors[],
1211 const uint16_t indices[],
1212 int indexCount) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001213 SK_TRACE_EVENT0("GrContext::drawVertices");
bsalomon@google.com27847de2011-02-22 20:59:41 +00001214
1215 GrDrawTarget::AutoReleaseGeometry geo;
1216
1217 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
1218
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001219 bool hasTexCoords[GrPaint::kTotalStages] = {
1220 NULL != texCoords, // texCoordSrc provides explicit stage 0 coords
1221 0 // remaining stages use positions
1222 };
1223
1224 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, hasTexCoords);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001225
1226 if (NULL != colors) {
1227 layout |= GrDrawTarget::kColor_VertexLayoutBit;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001228 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001229 int vertexSize = GrDrawTarget::VertexSize(layout);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001230
1231 if (sizeof(GrPoint) != vertexSize) {
1232 if (!geo.set(target, layout, vertexCount, 0)) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001233 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +00001234 return;
1235 }
tomhudson@google.com93813632011-10-27 20:21:16 +00001236 int texOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.com27847de2011-02-22 20:59:41 +00001237 int colorOffset;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001238 GrDrawTarget::VertexSizeAndOffsetsByIdx(layout,
1239 texOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001240 &colorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +00001241 NULL,
1242 NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001243 void* curVertex = geo.vertices();
1244
1245 for (int i = 0; i < vertexCount; ++i) {
1246 *((GrPoint*)curVertex) = positions[i];
1247
1248 if (texOffsets[0] > 0) {
1249 *(GrPoint*)((intptr_t)curVertex + texOffsets[0]) = texCoords[i];
1250 }
1251 if (colorOffset > 0) {
1252 *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i];
1253 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001254 curVertex = (void*)((intptr_t)curVertex + vertexSize);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001255 }
1256 } else {
1257 target->setVertexSourceToArray(layout, positions, vertexCount);
1258 }
1259
bsalomon@google.com91958362011-06-13 17:58:13 +00001260 // we don't currently apply offscreen AA to this path. Need improved
1261 // management of GrDrawTarget's geometry to avoid copying points per-tile.
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001262
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001263 if (NULL != indices) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001264 target->setIndexSourceToArray(indices, indexCount);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001265 target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001266 } else {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001267 target->drawNonIndexed(primitiveType, 0, vertexCount);
1268 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001269}
1270
bsalomon@google.com06afe7b2011-04-26 15:31:40 +00001271///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com150d2842012-01-12 20:19:56 +00001272#include "SkDraw.h"
1273#include "SkRasterClip.h"
1274
1275namespace {
1276
1277SkPath::FillType gr_fill_to_sk_fill(GrPathFill fill) {
1278 switch (fill) {
1279 case kWinding_PathFill:
1280 return SkPath::kWinding_FillType;
1281 case kEvenOdd_PathFill:
1282 return SkPath::kEvenOdd_FillType;
1283 case kInverseWinding_PathFill:
1284 return SkPath::kInverseWinding_FillType;
1285 case kInverseEvenOdd_PathFill:
1286 return SkPath::kInverseEvenOdd_FillType;
1287 default:
1288 GrCrash("Unexpected fill.");
1289 return SkPath::kWinding_FillType;
1290 }
1291}
1292
1293// gets device coord bounds of path (not considering the fill) and clip. The
1294// path bounds will be a subset of the clip bounds. returns false if path bounds
1295// would be empty.
1296bool get_path_and_clip_bounds(const GrDrawTarget* target,
1297 const GrPath& path,
1298 const GrVec* translate,
1299 GrIRect* pathBounds,
1300 GrIRect* clipBounds) {
1301 // compute bounds as intersection of rt size, clip, and path
1302 const GrRenderTarget* rt = target->getDrawState().getRenderTarget();
1303 if (NULL == rt) {
1304 return false;
1305 }
1306 *pathBounds = GrIRect::MakeWH(rt->width(), rt->height());
1307 const GrClip& clip = target->getClip();
1308 if (clip.hasConservativeBounds()) {
1309 clip.getConservativeBounds().roundOut(clipBounds);
1310 if (!pathBounds->intersect(*clipBounds)) {
1311 return false;
1312 }
1313 } else {
1314 // pathBounds is currently the rt extent, set clip bounds to that rect.
1315 *clipBounds = *pathBounds;
1316 }
1317 GrRect pathSBounds = path.getBounds();
1318 if (!pathSBounds.isEmpty()) {
1319 if (NULL != translate) {
1320 pathSBounds.offset(*translate);
1321 }
1322 target->getDrawState().getViewMatrix().mapRect(&pathSBounds,
1323 pathSBounds);
1324 GrIRect pathIBounds;
1325 pathSBounds.roundOut(&pathIBounds);
1326 if (!pathBounds->intersect(pathIBounds)) {
1327 return false;
1328 }
1329 } else {
1330 return false;
1331 }
1332 return true;
1333}
1334
1335/**
1336 * sw rasterizes path to A8 mask using the context's matrix and uploads to a
1337 * scratch texture.
1338 */
1339
1340bool sw_draw_path_to_mask_texture(const GrPath& clientPath,
1341 const GrIRect& pathDevBounds,
1342 GrPathFill fill,
1343 GrContext* context,
1344 const GrPoint* translate,
1345 GrAutoScratchTexture* tex) {
1346 SkPaint paint;
1347 SkPath tmpPath;
1348 const SkPath* pathToDraw = &clientPath;
1349 if (kHairLine_PathFill == fill) {
1350 paint.setStyle(SkPaint::kStroke_Style);
1351 paint.setStrokeWidth(SK_Scalar1);
1352 } else {
1353 paint.setStyle(SkPaint::kFill_Style);
1354 SkPath::FillType skfill = gr_fill_to_sk_fill(fill);
1355 if (skfill != pathToDraw->getFillType()) {
1356 tmpPath = *pathToDraw;
1357 tmpPath.setFillType(skfill);
1358 pathToDraw = &tmpPath;
1359 }
1360 }
1361 paint.setAntiAlias(true);
1362 paint.setColor(SK_ColorWHITE);
1363
1364 GrMatrix matrix = context->getMatrix();
1365 if (NULL != translate) {
1366 matrix.postTranslate(translate->fX, translate->fY);
1367 }
1368
1369 matrix.postTranslate(-pathDevBounds.fLeft * SK_Scalar1,
1370 -pathDevBounds.fTop * SK_Scalar1);
1371 GrIRect bounds = GrIRect::MakeWH(pathDevBounds.width(),
1372 pathDevBounds.height());
1373
1374 SkBitmap bm;
1375 bm.setConfig(SkBitmap::kA8_Config, bounds.fRight, bounds.fBottom);
1376 if (!bm.allocPixels()) {
1377 return false;
1378 }
1379 sk_bzero(bm.getPixels(), bm.getSafeSize());
1380
1381 SkDraw draw;
1382 sk_bzero(&draw, sizeof(draw));
1383 SkRasterClip rc(bounds);
1384 draw.fRC = &rc;
1385 draw.fClip = &rc.bwRgn();
1386 draw.fMatrix = &matrix;
1387 draw.fBitmap = &bm;
1388 draw.drawPath(*pathToDraw, paint);
1389
1390 const GrTextureDesc desc = {
1391 kNone_GrTextureFlags,
bsalomon@google.com150d2842012-01-12 20:19:56 +00001392 bounds.fRight,
1393 bounds.fBottom,
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001394 kAlpha_8_GrPixelConfig,
bsalomon@google.comb9014f42012-03-30 14:22:41 +00001395 0 // samples
bsalomon@google.com150d2842012-01-12 20:19:56 +00001396 };
1397
1398 tex->set(context, desc);
1399 GrTexture* texture = tex->texture();
1400
1401 if (NULL == texture) {
1402 return false;
1403 }
1404 SkAutoLockPixels alp(bm);
1405 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
1406 bm.getPixels(), bm.rowBytes());
1407 return true;
1408}
1409
1410void draw_around_inv_path(GrDrawTarget* target,
1411 GrDrawState::StageMask stageMask,
1412 const GrIRect& clipBounds,
1413 const GrIRect& pathBounds) {
1414 GrDrawTarget::AutoDeviceCoordDraw adcd(target, stageMask);
1415 GrRect rect;
1416 if (clipBounds.fTop < pathBounds.fTop) {
1417 rect.iset(clipBounds.fLeft, clipBounds.fTop,
1418 clipBounds.fRight, pathBounds.fTop);
1419 target->drawSimpleRect(rect, NULL, stageMask);
1420 }
1421 if (clipBounds.fLeft < pathBounds.fLeft) {
1422 rect.iset(clipBounds.fLeft, pathBounds.fTop,
1423 pathBounds.fLeft, pathBounds.fBottom);
1424 target->drawSimpleRect(rect, NULL, stageMask);
1425 }
1426 if (clipBounds.fRight > pathBounds.fRight) {
1427 rect.iset(pathBounds.fRight, pathBounds.fTop,
1428 clipBounds.fRight, pathBounds.fBottom);
1429 target->drawSimpleRect(rect, NULL, stageMask);
1430 }
1431 if (clipBounds.fBottom > pathBounds.fBottom) {
1432 rect.iset(clipBounds.fLeft, pathBounds.fBottom,
1433 clipBounds.fRight, clipBounds.fBottom);
1434 target->drawSimpleRect(rect, NULL, stageMask);
1435 }
1436}
1437
1438}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001439
reed@google.com07f3ee12011-05-16 17:21:57 +00001440void GrContext::drawPath(const GrPaint& paint, const GrPath& path,
1441 GrPathFill fill, const GrPoint* translate) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001442
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001443 if (path.isEmpty()) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001444 if (GrIsFillInverted(fill)) {
1445 this->drawPaint(paint);
1446 }
1447 return;
1448 }
1449
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001450 // Note that below we may sw-rasterize the path into a scratch texture.
1451 // Scratch textures can be recycled after they are returned to the texture
1452 // cache. This presents a potential hazard for buffered drawing. However,
1453 // the writePixels that uploads to the scratch will perform a flush so we're
1454 // OK.
1455 DrawCategory category = (DEFER_PATHS) ? kBuffered_DrawCategory :
1456 kUnbuffered_DrawCategory;
1457 GrDrawTarget* target = this->prepareToDraw(paint, category);
bsalomon@google.com150d2842012-01-12 20:19:56 +00001458 GrDrawState::StageMask stageMask = paint.getActiveStageMask();
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001459
bsalomon@google.com289533a2011-10-27 12:34:25 +00001460 bool prAA = paint.fAntiAlias && !this->getRenderTarget()->isMultisampled();
1461
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001462 // An Assumption here is that path renderer would use some form of tweaking
1463 // the src color (either the input alpha or in the frag shader) to implement
1464 // aa. If we have some future driver-mojo path AA that can do the right
1465 // thing WRT to the blend then we'll need some query on the PR.
1466 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001467#if GR_DEBUG
bsalomon@google.com979432b2011-11-05 21:38:22 +00001468 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001469#endif
bsalomon@google.com289533a2011-10-27 12:34:25 +00001470 prAA = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001471 }
bsalomon@google.com289533a2011-10-27 12:34:25 +00001472
bsalomon@google.com289533a2011-10-27 12:34:25 +00001473 GrPathRenderer* pr = NULL;
1474 if (prAA) {
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001475 pr = this->getPathRenderer(path, fill, target, true);
bsalomon@google.com289533a2011-10-27 12:34:25 +00001476 if (NULL == pr) {
bsalomon@google.com150d2842012-01-12 20:19:56 +00001477 GrAutoScratchTexture ast;
1478 GrIRect pathBounds, clipBounds;
1479 if (!get_path_and_clip_bounds(target, path, translate,
1480 &pathBounds, &clipBounds)) {
1481 return;
1482 }
bsalomon@google.com150d2842012-01-12 20:19:56 +00001483 if (NULL == pr && sw_draw_path_to_mask_texture(path, pathBounds,
1484 fill, this,
1485 translate, &ast)) {
1486 GrTexture* texture = ast.texture();
1487 GrAssert(NULL != texture);
1488 GrDrawTarget::AutoDeviceCoordDraw adcd(target, stageMask);
1489 enum {
1490 kPathMaskStage = GrPaint::kTotalStages,
1491 };
1492 target->drawState()->setTexture(kPathMaskStage, texture);
1493 target->drawState()->sampler(kPathMaskStage)->reset();
1494 GrScalar w = GrIntToScalar(pathBounds.width());
1495 GrScalar h = GrIntToScalar(pathBounds.height());
1496 GrRect maskRect = GrRect::MakeWH(w / texture->width(),
1497 h / texture->height());
1498 const GrRect* srcRects[GrDrawState::kNumStages] = {NULL};
1499 srcRects[kPathMaskStage] = &maskRect;
1500 stageMask |= 1 << kPathMaskStage;
bsalomon@google.com5db3b6c2012-01-12 20:38:57 +00001501 GrRect dstRect = GrRect::MakeLTRB(
1502 SK_Scalar1* pathBounds.fLeft,
1503 SK_Scalar1* pathBounds.fTop,
1504 SK_Scalar1* pathBounds.fRight,
1505 SK_Scalar1* pathBounds.fBottom);
bsalomon@google.com150d2842012-01-12 20:19:56 +00001506 target->drawRect(dstRect, NULL, stageMask, srcRects, NULL);
1507 target->drawState()->setTexture(kPathMaskStage, NULL);
1508 if (GrIsFillInverted(fill)) {
1509 draw_around_inv_path(target, stageMask,
1510 clipBounds, pathBounds);
1511 }
1512 return;
1513 }
bsalomon@google.com289533a2011-10-27 12:34:25 +00001514 }
1515 } else {
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001516 pr = this->getPathRenderer(path, fill, target, false);
bsalomon@google.com289533a2011-10-27 12:34:25 +00001517 }
1518
bsalomon@google.com30085192011-08-19 15:42:31 +00001519 if (NULL == pr) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001520#if GR_DEBUG
bsalomon@google.com30085192011-08-19 15:42:31 +00001521 GrPrintf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001522#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001523 return;
1524 }
1525
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001526 pr->drawPath(path, fill, translate, target, stageMask, prAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001527}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001528
bsalomon@google.com27847de2011-02-22 20:59:41 +00001529////////////////////////////////////////////////////////////////////////////////
1530
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001531void GrContext::flush(int flagsBitfield) {
1532 if (kDiscard_FlushBit & flagsBitfield) {
1533 fDrawBuffer->reset();
1534 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001535 this->flushDrawBuffer();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001536 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001537 if (kForceCurrentRenderTarget_FlushBit & flagsBitfield) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001538 fGpu->forceRenderTargetFlush();
1539 }
1540}
1541
1542void GrContext::flushText() {
1543 if (kText_DrawCategory == fLastDrawCategory) {
1544 flushDrawBuffer();
1545 }
1546}
1547
1548void GrContext::flushDrawBuffer() {
1549#if BATCH_RECT_TO_RECT || DEFER_TEXT_RENDERING
junov@google.com53a55842011-06-08 22:55:10 +00001550 if (fDrawBuffer) {
bsalomon@google.com97805382012-03-13 14:32:07 +00001551 fDrawBuffer->flushTo(fGpu);
junov@google.com53a55842011-06-08 22:55:10 +00001552 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001553#endif
1554}
1555
bsalomon@google.com6f379512011-11-16 20:36:03 +00001556void GrContext::internalWriteTexturePixels(GrTexture* texture,
1557 int left, int top,
1558 int width, int height,
1559 GrPixelConfig config,
1560 const void* buffer,
1561 size_t rowBytes,
1562 uint32_t flags) {
1563 SK_TRACE_EVENT0("GrContext::writeTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001564 ASSERT_OWNED_RESOURCE(texture);
1565
bsalomon@google.com6f379512011-11-16 20:36:03 +00001566 if (!(kDontFlush_PixelOpsFlag & flags)) {
1567 this->flush();
1568 }
1569 // TODO: use scratch texture to perform conversion
1570 if (GrPixelConfigIsUnpremultiplied(texture->config()) !=
1571 GrPixelConfigIsUnpremultiplied(config)) {
1572 return;
1573 }
1574
1575 fGpu->writeTexturePixels(texture, left, top, width, height,
1576 config, buffer, rowBytes);
1577}
1578
1579bool GrContext::internalReadTexturePixels(GrTexture* texture,
1580 int left, int top,
1581 int width, int height,
1582 GrPixelConfig config,
1583 void* buffer,
1584 size_t rowBytes,
1585 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001586 SK_TRACE_EVENT0("GrContext::readTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001587 ASSERT_OWNED_RESOURCE(texture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001588
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001589 // TODO: code read pixels for textures that aren't also rendertargets
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001590 GrRenderTarget* target = texture->asRenderTarget();
1591 if (NULL != target) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001592 return this->internalReadRenderTargetPixels(target,
1593 left, top, width, height,
1594 config, buffer, rowBytes,
1595 flags);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001596 } else {
1597 return false;
1598 }
1599}
1600
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001601#include "SkConfig8888.h"
1602
1603namespace {
1604/**
1605 * Converts a GrPixelConfig to a SkCanvas::Config8888. Only byte-per-channel
1606 * formats are representable as Config8888 and so the function returns false
1607 * if the GrPixelConfig has no equivalent Config8888.
1608 */
1609bool grconfig_to_config8888(GrPixelConfig config,
1610 SkCanvas::Config8888* config8888) {
1611 switch (config) {
1612 case kRGBA_8888_PM_GrPixelConfig:
1613 *config8888 = SkCanvas::kRGBA_Premul_Config8888;
1614 return true;
1615 case kRGBA_8888_UPM_GrPixelConfig:
1616 *config8888 = SkCanvas::kRGBA_Unpremul_Config8888;
1617 return true;
1618 case kBGRA_8888_PM_GrPixelConfig:
1619 *config8888 = SkCanvas::kBGRA_Premul_Config8888;
1620 return true;
1621 case kBGRA_8888_UPM_GrPixelConfig:
1622 *config8888 = SkCanvas::kBGRA_Unpremul_Config8888;
1623 return true;
1624 default:
1625 return false;
1626 }
1627}
1628}
1629
bsalomon@google.com6f379512011-11-16 20:36:03 +00001630bool GrContext::internalReadRenderTargetPixels(GrRenderTarget* target,
1631 int left, int top,
1632 int width, int height,
1633 GrPixelConfig config,
1634 void* buffer,
1635 size_t rowBytes,
1636 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001637 SK_TRACE_EVENT0("GrContext::readRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001638 ASSERT_OWNED_RESOURCE(target);
1639
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001640 if (NULL == target) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001641 target = fDrawState->getRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001642 if (NULL == target) {
1643 return false;
1644 }
1645 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001646
bsalomon@google.com6f379512011-11-16 20:36:03 +00001647 if (!(kDontFlush_PixelOpsFlag & flags)) {
1648 this->flush();
1649 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001650
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001651 if (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1652 GrPixelConfigIsUnpremultiplied(config) &&
1653 !fGpu->canPreserveReadWriteUnpremulPixels()) {
1654 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1655 if (!grconfig_to_config8888(target->config(), &srcConfig8888) ||
1656 !grconfig_to_config8888(config, &dstConfig8888)) {
1657 return false;
1658 }
1659 // do read back using target's own config
1660 this->internalReadRenderTargetPixels(target,
1661 left, top,
1662 width, height,
1663 target->config(),
1664 buffer, rowBytes,
1665 kDontFlush_PixelOpsFlag);
1666 // sw convert the pixels to unpremul config
1667 uint32_t* pixels = reinterpret_cast<uint32_t*>(buffer);
1668 SkConvertConfig8888Pixels(pixels, rowBytes, dstConfig8888,
1669 pixels, rowBytes, srcConfig8888,
1670 width, height);
1671 return true;
1672 }
1673
bsalomon@google.comc4364992011-11-07 15:54:49 +00001674 GrTexture* src = target->asTexture();
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001675 bool swapRAndB = NULL != src &&
1676 fGpu->preferredReadPixelsConfig(config) ==
1677 GrPixelConfigSwapRAndB(config);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001678
1679 bool flipY = NULL != src &&
1680 fGpu->readPixelsWillPayForYFlip(target, left, top,
1681 width, height, config,
1682 rowBytes);
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001683 bool alphaConversion = (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1684 GrPixelConfigIsUnpremultiplied(config));
bsalomon@google.comc4364992011-11-07 15:54:49 +00001685
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001686 if (NULL == src && alphaConversion) {
1687 // we should fallback to cpu conversion here. This could happen when
1688 // we were given an external render target by the client that is not
1689 // also a texture (e.g. FBO 0 in GL)
1690 return false;
1691 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001692 // we draw to a scratch texture if any of these conversion are applied
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001693 GrAutoScratchTexture ast;
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001694 if (flipY || swapRAndB || alphaConversion) {
1695 GrAssert(NULL != src);
1696 if (swapRAndB) {
1697 config = GrPixelConfigSwapRAndB(config);
1698 GrAssert(kUnknown_GrPixelConfig != config);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001699 }
1700 // Make the scratch a render target because we don't have a robust
1701 // readTexturePixels as of yet (it calls this function).
1702 const GrTextureDesc desc = {
1703 kRenderTarget_GrTextureFlagBit,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001704 width, height,
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001705 config,
bsalomon@google.comb9014f42012-03-30 14:22:41 +00001706 0 // samples
bsalomon@google.comc4364992011-11-07 15:54:49 +00001707 };
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001708
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001709 // When a full readback is faster than a partial we could always make
1710 // the scratch exactly match the passed rect. However, if we see many
1711 // different size rectangles we will trash our texture cache and pay the
1712 // cost of creating and destroying many textures. So, we only request
1713 // an exact match when the caller is reading an entire RT.
1714 ScratchTexMatch match = kApprox_ScratchTexMatch;
1715 if (0 == left &&
1716 0 == top &&
1717 target->width() == width &&
1718 target->height() == height &&
1719 fGpu->fullReadPixelsIsFasterThanPartial()) {
1720 match = kExact_ScratchTexMatch;
1721 }
1722 ast.set(this, desc, match);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001723 GrTexture* texture = ast.texture();
1724 if (!texture) {
1725 return false;
1726 }
1727 target = texture->asRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001728 GrAssert(NULL != target);
1729
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001730 GrDrawTarget::AutoStateRestore asr(fGpu,
1731 GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001732 GrDrawState* drawState = fGpu->drawState();
1733 drawState->setRenderTarget(target);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001734
bsalomon@google.comc4364992011-11-07 15:54:49 +00001735 GrMatrix matrix;
1736 if (flipY) {
1737 matrix.setTranslate(SK_Scalar1 * left,
1738 SK_Scalar1 * (top + height));
1739 matrix.set(GrMatrix::kMScaleY, -GR_Scalar1);
1740 } else {
1741 matrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
1742 }
1743 matrix.postIDiv(src->width(), src->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001744 drawState->sampler(0)->reset(matrix);
1745 drawState->sampler(0)->setRAndBSwap(swapRAndB);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001746 drawState->setTexture(0, src);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001747 GrRect rect;
1748 rect.setXYWH(0, 0, SK_Scalar1 * width, SK_Scalar1 * height);
1749 fGpu->drawSimpleRect(rect, NULL, 0x1);
1750 left = 0;
1751 top = 0;
1752 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001753 return fGpu->readPixels(target,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001754 left, top, width, height,
1755 config, buffer, rowBytes, flipY);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001756}
1757
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001758void GrContext::resolveRenderTarget(GrRenderTarget* target) {
1759 GrAssert(target);
1760 ASSERT_OWNED_RESOURCE(target);
1761 // In the future we may track whether there are any pending draws to this
1762 // target. We don't today so we always perform a flush. We don't promise
1763 // this to our clients, though.
1764 this->flush();
1765 fGpu->resolveRenderTarget(target);
1766}
1767
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001768void GrContext::copyTexture(GrTexture* src, GrRenderTarget* dst) {
1769 if (NULL == src || NULL == dst) {
1770 return;
1771 }
1772 ASSERT_OWNED_RESOURCE(src);
1773
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001774 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001775 GrDrawState* drawState = fGpu->drawState();
1776 drawState->setRenderTarget(dst);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001777 GrMatrix sampleM;
1778 sampleM.setIDiv(src->width(), src->height());
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001779 drawState->setTexture(0, src);
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001780 drawState->sampler(0)->reset(sampleM);
bsalomon@google.com5db3b6c2012-01-12 20:38:57 +00001781 SkRect rect = SkRect::MakeXYWH(0, 0,
1782 SK_Scalar1 * src->width(),
1783 SK_Scalar1 * src->height());
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001784 fGpu->drawSimpleRect(rect, NULL, 1 << 0);
1785}
1786
bsalomon@google.com6f379512011-11-16 20:36:03 +00001787void GrContext::internalWriteRenderTargetPixels(GrRenderTarget* target,
1788 int left, int top,
1789 int width, int height,
1790 GrPixelConfig config,
1791 const void* buffer,
1792 size_t rowBytes,
1793 uint32_t flags) {
1794 SK_TRACE_EVENT0("GrContext::writeRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001795 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com6f379512011-11-16 20:36:03 +00001796
1797 if (NULL == target) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001798 target = fDrawState->getRenderTarget();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001799 if (NULL == target) {
1800 return;
1801 }
1802 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001803
1804 // TODO: when underlying api has a direct way to do this we should use it
1805 // (e.g. glDrawPixels on desktop GL).
1806
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001807 // If the RT is also a texture and we don't have to do PM/UPM conversion
1808 // then take the texture path, which we expect to be at least as fast or
1809 // faster since it doesn't use an intermediate texture as we do below.
1810
1811#if !GR_MAC_BUILD
1812 // At least some drivers on the Mac get confused when glTexImage2D is called
1813 // on a texture attached to an FBO. The FBO still sees the old image. TODO:
1814 // determine what OS versions and/or HW is affected.
1815 if (NULL != target->asTexture() &&
1816 GrPixelConfigIsUnpremultiplied(target->config()) ==
1817 GrPixelConfigIsUnpremultiplied(config)) {
1818
1819 this->internalWriteTexturePixels(target->asTexture(),
1820 left, top, width, height,
1821 config, buffer, rowBytes, flags);
1822 return;
1823 }
1824#endif
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001825 if (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1826 GrPixelConfigIsUnpremultiplied(config) &&
1827 !fGpu->canPreserveReadWriteUnpremulPixels()) {
1828 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1829 if (!grconfig_to_config8888(config, &srcConfig8888) ||
1830 !grconfig_to_config8888(target->config(), &dstConfig8888)) {
1831 return;
1832 }
1833 // allocate a tmp buffer and sw convert the pixels to premul
1834 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(width * height);
1835 const uint32_t* src = reinterpret_cast<const uint32_t*>(buffer);
1836 SkConvertConfig8888Pixels(tmpPixels.get(), 4 * width, dstConfig8888,
1837 src, rowBytes, srcConfig8888,
1838 width, height);
1839 // upload the already premul pixels
1840 this->internalWriteRenderTargetPixels(target,
1841 left, top,
1842 width, height,
1843 target->config(),
1844 tmpPixels, 4 * width, flags);
1845 return;
1846 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001847
1848 bool swapRAndB = fGpu->preferredReadPixelsConfig(config) ==
1849 GrPixelConfigSwapRAndB(config);
1850 if (swapRAndB) {
1851 config = GrPixelConfigSwapRAndB(config);
1852 }
1853
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001854 const GrTextureDesc desc = {
bsalomon@google.comb9014f42012-03-30 14:22:41 +00001855 kNone_GrTextureFlags, width, height, config, 0
bsalomon@google.com27847de2011-02-22 20:59:41 +00001856 };
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001857 GrAutoScratchTexture ast(this, desc);
1858 GrTexture* texture = ast.texture();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001859 if (NULL == texture) {
1860 return;
1861 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001862 this->internalWriteTexturePixels(texture, 0, 0, width, height,
1863 config, buffer, rowBytes, flags);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001864
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001865 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001866 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001867
1868 GrMatrix matrix;
1869 matrix.setTranslate(GrIntToScalar(left), GrIntToScalar(top));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001870 drawState->setViewMatrix(matrix);
1871 drawState->setRenderTarget(target);
1872 drawState->setTexture(0, texture);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001873
bsalomon@google.com5c638652011-07-18 19:31:59 +00001874 matrix.setIDiv(texture->width(), texture->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001875 drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode,
1876 GrSamplerState::kNearest_Filter,
1877 matrix);
1878 drawState->sampler(0)->setRAndBSwap(swapRAndB);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001879
1880 GrVertexLayout layout = GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(0);
1881 static const int VCOUNT = 4;
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001882 // TODO: Use GrGpu::drawRect here
bsalomon@google.com27847de2011-02-22 20:59:41 +00001883 GrDrawTarget::AutoReleaseGeometry geo(fGpu, layout, VCOUNT, 0);
1884 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001885 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +00001886 return;
1887 }
1888 ((GrPoint*)geo.vertices())->setIRectFan(0, 0, width, height);
1889 fGpu->drawNonIndexed(kTriangleFan_PrimitiveType, 0, VCOUNT);
1890}
1891////////////////////////////////////////////////////////////////////////////////
1892
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001893void GrContext::setPaint(const GrPaint& paint) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001894
1895 for (int i = 0; i < GrPaint::kMaxTextures; ++i) {
1896 int s = i + GrPaint::kFirstTextureStage;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001897 fDrawState->setTexture(s, paint.getTexture(i));
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001898 ASSERT_OWNED_RESOURCE(paint.getTexture(i));
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001899 if (paint.getTexture(i)) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001900 *fDrawState->sampler(s) = paint.getTextureSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001901 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001902 }
1903
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001904 fDrawState->setFirstCoverageStage(GrPaint::kFirstMaskStage);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001905
1906 for (int i = 0; i < GrPaint::kMaxMasks; ++i) {
1907 int s = i + GrPaint::kFirstMaskStage;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001908 fDrawState->setTexture(s, paint.getMask(i));
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001909 ASSERT_OWNED_RESOURCE(paint.getMask(i));
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001910 if (paint.getMask(i)) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001911 *fDrawState->sampler(s) = paint.getMaskSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001912 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001913 }
bsalomon@google.com26936d02012-03-19 13:06:19 +00001914
1915 // disable all stages not accessible via the paint
1916 for (int s = GrPaint::kTotalStages; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001917 fDrawState->setTexture(s, NULL);
bsalomon@google.com26936d02012-03-19 13:06:19 +00001918 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001919
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001920 fDrawState->setColor(paint.fColor);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001921
1922 if (paint.fDither) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001923 fDrawState->enableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001924 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001925 fDrawState->disableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001926 }
1927 if (paint.fAntiAlias) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001928 fDrawState->enableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001929 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001930 fDrawState->disableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001931 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001932 if (paint.fColorMatrixEnabled) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001933 fDrawState->enableState(GrDrawState::kColorMatrix_StateBit);
1934 fDrawState->setColorMatrix(paint.fColorMatrix);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001935 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001936 fDrawState->disableState(GrDrawState::kColorMatrix_StateBit);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001937 }
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001938 fDrawState->setBlendFunc(paint.fSrcBlendCoeff, paint.fDstBlendCoeff);
1939 fDrawState->setColorFilter(paint.fColorFilterColor, paint.fColorFilterXfermode);
1940 fDrawState->setCoverage(paint.fCoverage);
bsalomon@google.come79c8152012-03-29 19:07:12 +00001941#if GR_DEBUG
1942 if ((paint.getActiveMaskStageMask() || 0xff != paint.fCoverage) &&
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001943 !fGpu->canApplyCoverage()) {
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001944 GrPrintf("Partial pixel coverage will be incorrectly blended.\n");
1945 }
bsalomon@google.com95cd7bd2012-03-28 15:35:05 +00001946#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +00001947}
1948
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001949GrDrawTarget* GrContext::prepareToDraw(const GrPaint& paint,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001950 DrawCategory category) {
1951 if (category != fLastDrawCategory) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001952 this->flushDrawBuffer();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001953 fLastDrawCategory = category;
1954 }
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001955 this->setPaint(paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001956 GrDrawTarget* target = fGpu;
1957 switch (category) {
1958 case kText_DrawCategory:
1959#if DEFER_TEXT_RENDERING
1960 target = fDrawBuffer;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001961 fDrawBuffer->setClip(fGpu->getClip());
bsalomon@google.com27847de2011-02-22 20:59:41 +00001962#else
1963 target = fGpu;
1964#endif
1965 break;
1966 case kUnbuffered_DrawCategory:
1967 target = fGpu;
1968 break;
1969 case kBuffered_DrawCategory:
1970 target = fDrawBuffer;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001971 fDrawBuffer->setClip(fGpu->getClip());
bsalomon@google.com27847de2011-02-22 20:59:41 +00001972 break;
1973 }
1974 return target;
1975}
1976
bsalomon@google.com289533a2011-10-27 12:34:25 +00001977GrPathRenderer* GrContext::getPathRenderer(const GrPath& path,
1978 GrPathFill fill,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001979 const GrDrawTarget* target,
bsalomon@google.com289533a2011-10-27 12:34:25 +00001980 bool antiAlias) {
bsalomon@google.com30085192011-08-19 15:42:31 +00001981 if (NULL == fPathRendererChain) {
1982 fPathRendererChain =
1983 new GrPathRendererChain(this, GrPathRendererChain::kNone_UsageFlag);
1984 }
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001985 return fPathRendererChain->getPathRenderer(path, fill, target, antiAlias);
bsalomon@google.com30085192011-08-19 15:42:31 +00001986}
1987
bsalomon@google.com27847de2011-02-22 20:59:41 +00001988////////////////////////////////////////////////////////////////////////////////
1989
bsalomon@google.com27847de2011-02-22 20:59:41 +00001990void GrContext::setRenderTarget(GrRenderTarget* target) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001991 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001992 if (fDrawState->getRenderTarget() != target) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001993 this->flush(false);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001994 fDrawState->setRenderTarget(target);
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001995 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001996}
1997
1998GrRenderTarget* GrContext::getRenderTarget() {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001999 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00002000}
2001
2002const GrRenderTarget* GrContext::getRenderTarget() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00002003 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00002004}
2005
2006const GrMatrix& GrContext::getMatrix() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00002007 return fDrawState->getViewMatrix();
bsalomon@google.com27847de2011-02-22 20:59:41 +00002008}
2009
2010void GrContext::setMatrix(const GrMatrix& m) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00002011 fDrawState->setViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00002012}
2013
2014void GrContext::concatMatrix(const GrMatrix& m) const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00002015 fDrawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00002016}
2017
2018static inline intptr_t setOrClear(intptr_t bits, int shift, intptr_t pred) {
2019 intptr_t mask = 1 << shift;
2020 if (pred) {
2021 bits |= mask;
2022 } else {
2023 bits &= ~mask;
2024 }
2025 return bits;
2026}
2027
2028void GrContext::resetStats() {
2029 fGpu->resetStats();
2030}
2031
bsalomon@google.com05ef5102011-05-02 21:14:59 +00002032const GrGpuStats& GrContext::getStats() const {
bsalomon@google.com27847de2011-02-22 20:59:41 +00002033 return fGpu->getStats();
2034}
2035
2036void GrContext::printStats() const {
2037 fGpu->printStats();
2038}
2039
bsalomon@google.com583a1e32011-08-17 13:42:46 +00002040GrContext::GrContext(GrGpu* gpu) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00002041 fGpu = gpu;
2042 fGpu->ref();
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002043 fGpu->setContext(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +00002044
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00002045 fDrawState = new GrDrawState();
2046 fGpu->setDrawState(fDrawState);
2047
bsalomon@google.com30085192011-08-19 15:42:31 +00002048 fPathRendererChain = NULL;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00002049
bsalomon@google.com50398bf2011-07-26 20:45:30 +00002050 fTextureCache = new GrResourceCache(MAX_TEXTURE_CACHE_COUNT,
2051 MAX_TEXTURE_CACHE_BYTES);
bsalomon@google.com27847de2011-02-22 20:59:41 +00002052 fFontCache = new GrFontCache(fGpu);
2053
2054 fLastDrawCategory = kUnbuffered_DrawCategory;
2055
bsalomon@google.com8fe72472011-03-30 21:26:44 +00002056 fDrawBuffer = NULL;
2057 fDrawBufferVBAllocPool = NULL;
2058 fDrawBufferIBAllocPool = NULL;
2059
bsalomon@google.com205d4602011-04-25 12:43:45 +00002060 fAAFillRectIndexBuffer = NULL;
2061 fAAStrokeRectIndexBuffer = NULL;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00002062
bsalomon@google.com8fe72472011-03-30 21:26:44 +00002063 this->setupDrawBuffer();
2064}
2065
2066void GrContext::setupDrawBuffer() {
2067
2068 GrAssert(NULL == fDrawBuffer);
2069 GrAssert(NULL == fDrawBufferVBAllocPool);
2070 GrAssert(NULL == fDrawBufferIBAllocPool);
2071
bsalomon@google.com27847de2011-02-22 20:59:41 +00002072#if DEFER_TEXT_RENDERING || BATCH_RECT_TO_RECT
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00002073 fDrawBufferVBAllocPool =
bsalomon@google.com8fe72472011-03-30 21:26:44 +00002074 new GrVertexBufferAllocPool(fGpu, false,
bsalomon@google.com27847de2011-02-22 20:59:41 +00002075 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
2076 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS);
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00002077 fDrawBufferIBAllocPool =
bsalomon@google.com8fe72472011-03-30 21:26:44 +00002078 new GrIndexBufferAllocPool(fGpu, false,
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00002079 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
bsalomon@google.com27847de2011-02-22 20:59:41 +00002080 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS);
2081
bsalomon@google.com471d4712011-08-23 15:45:25 +00002082 fDrawBuffer = new GrInOrderDrawBuffer(fGpu,
2083 fDrawBufferVBAllocPool,
bsalomon@google.com27847de2011-02-22 20:59:41 +00002084 fDrawBufferIBAllocPool);
bsalomon@google.com27847de2011-02-22 20:59:41 +00002085#endif
2086
2087#if BATCH_RECT_TO_RECT
2088 fDrawBuffer->setQuadIndexBuffer(this->getQuadIndexBuffer());
2089#endif
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00002090 fDrawBuffer->setAutoFlushTarget(fGpu);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00002091 fDrawBuffer->setDrawState(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +00002092}
2093
bsalomon@google.com27847de2011-02-22 20:59:41 +00002094GrDrawTarget* GrContext::getTextTarget(const GrPaint& paint) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00002095#if DEFER_TEXT_RENDERING
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00002096 return prepareToDraw(paint, kText_DrawCategory);
bsalomon@google.com27847de2011-02-22 20:59:41 +00002097#else
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00002098 return prepareToDraw(paint, kUnbuffered_DrawCategory);
bsalomon@google.com27847de2011-02-22 20:59:41 +00002099#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +00002100}
2101
2102const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {
2103 return fGpu->getQuadIndexBuffer();
2104}
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00002105
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002106GrTexture* GrContext::gaussianBlur(GrTexture* srcTexture,
2107 GrAutoScratchTexture* temp1,
2108 GrAutoScratchTexture* temp2,
2109 const SkRect& rect,
2110 float sigmaX, float sigmaY) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00002111 ASSERT_OWNED_RESOURCE(srcTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002112 GrRenderTarget* oldRenderTarget = this->getRenderTarget();
2113 GrClip oldClip = this->getClip();
2114 GrTexture* origTexture = srcTexture;
2115 GrAutoMatrix avm(this, GrMatrix::I());
2116 SkIRect clearRect;
2117 int scaleFactorX, halfWidthX, kernelWidthX;
2118 int scaleFactorY, halfWidthY, kernelWidthY;
2119 sigmaX = adjust_sigma(sigmaX, &scaleFactorX, &halfWidthX, &kernelWidthX);
2120 sigmaY = adjust_sigma(sigmaY, &scaleFactorY, &halfWidthY, &kernelWidthY);
bsalomon@google.combc4b6542011-11-19 13:56:11 +00002121
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002122 SkRect srcRect(rect);
2123 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
2124 srcRect.roundOut();
2125 scale_rect(&srcRect, scaleFactorX, scaleFactorY);
2126 this->setClip(srcRect);
2127
2128 const GrTextureDesc desc = {
2129 kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit,
bungeman@google.comf8aa18c2012-03-19 21:04:52 +00002130 SkScalarFloorToInt(srcRect.width()),
2131 SkScalarFloorToInt(srcRect.height()),
bsalomon@google.comb9014f42012-03-30 14:22:41 +00002132 kRGBA_8888_PM_GrPixelConfig,
2133 0 // samples
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002134 };
2135
2136 temp1->set(this, desc);
2137 if (temp2) temp2->set(this, desc);
2138
2139 GrTexture* dstTexture = temp1->texture();
2140 GrPaint paint;
2141 paint.reset();
2142 paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
2143
2144 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
2145 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
2146 srcTexture->height());
2147 this->setRenderTarget(dstTexture->asRenderTarget());
2148 SkRect dstRect(srcRect);
2149 scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
2150 i < scaleFactorY ? 0.5f : 1.0f);
2151 paint.setTexture(0, srcTexture);
2152 this->drawRectToRect(paint, dstRect, srcRect);
2153 srcRect = dstRect;
2154 SkTSwap(srcTexture, dstTexture);
2155 // If temp2 is non-NULL, don't render back to origTexture
2156 if (temp2 && dstTexture == origTexture) dstTexture = temp2->texture();
2157 }
2158
2159 if (sigmaX > 0.0f) {
2160 SkAutoTMalloc<float> kernelStorageX(kernelWidthX);
2161 float* kernelX = kernelStorageX.get();
2162 build_kernel(sigmaX, kernelX, kernelWidthX);
2163
2164 if (scaleFactorX > 1) {
2165 // Clear out a halfWidth to the right of the srcRect to prevent the
2166 // X convolution from reading garbage.
2167 clearRect = SkIRect::MakeXYWH(
2168 srcRect.fRight, srcRect.fTop, halfWidthX, srcRect.height());
2169 this->clear(&clearRect, 0x0);
2170 }
2171
2172 this->setRenderTarget(dstTexture->asRenderTarget());
2173 convolve(fGpu, srcTexture, srcRect, kernelX, kernelWidthX,
2174 GrSamplerState::kX_FilterDirection);
2175 SkTSwap(srcTexture, dstTexture);
2176 if (temp2 && dstTexture == origTexture) dstTexture = temp2->texture();
2177 }
2178
2179 if (sigmaY > 0.0f) {
2180 SkAutoTMalloc<float> kernelStorageY(kernelWidthY);
2181 float* kernelY = kernelStorageY.get();
2182 build_kernel(sigmaY, kernelY, kernelWidthY);
2183
2184 if (scaleFactorY > 1 || sigmaX > 0.0f) {
2185 // Clear out a halfWidth below the srcRect to prevent the Y
2186 // convolution from reading garbage.
2187 clearRect = SkIRect::MakeXYWH(
2188 srcRect.fLeft, srcRect.fBottom, srcRect.width(), halfWidthY);
2189 this->clear(&clearRect, 0x0);
2190 }
2191
2192 this->setRenderTarget(dstTexture->asRenderTarget());
2193 convolve(fGpu, srcTexture, srcRect, kernelY, kernelWidthY,
2194 GrSamplerState::kY_FilterDirection);
2195 SkTSwap(srcTexture, dstTexture);
2196 if (temp2 && dstTexture == origTexture) dstTexture = temp2->texture();
2197 }
2198
2199 if (scaleFactorX > 1 || scaleFactorY > 1) {
2200 // Clear one pixel to the right and below, to accommodate bilinear
2201 // upsampling.
2202 clearRect = SkIRect::MakeXYWH(
2203 srcRect.fLeft, srcRect.fBottom, srcRect.width() + 1, 1);
2204 this->clear(&clearRect, 0x0);
2205 clearRect = SkIRect::MakeXYWH(
2206 srcRect.fRight, srcRect.fTop, 1, srcRect.height());
2207 this->clear(&clearRect, 0x0);
2208 // FIXME: This should be mitchell, not bilinear.
2209 paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
2210 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
2211 srcTexture->height());
2212 this->setRenderTarget(dstTexture->asRenderTarget());
2213 paint.setTexture(0, srcTexture);
2214 SkRect dstRect(srcRect);
2215 scale_rect(&dstRect, scaleFactorX, scaleFactorY);
2216 this->drawRectToRect(paint, dstRect, srcRect);
2217 srcRect = dstRect;
2218 SkTSwap(srcTexture, dstTexture);
2219 }
2220 this->setRenderTarget(oldRenderTarget);
2221 this->setClip(oldClip);
2222 return srcTexture;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00002223}
bsalomon@google.com1e266f82011-12-12 16:11:33 +00002224
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002225GrTexture* GrContext::applyMorphology(GrTexture* srcTexture,
2226 const GrRect& rect,
2227 GrTexture* temp1, GrTexture* temp2,
2228 GrSamplerState::Filter filter,
2229 SkISize radius) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00002230 ASSERT_OWNED_RESOURCE(srcTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002231 GrRenderTarget* oldRenderTarget = this->getRenderTarget();
2232 GrAutoMatrix avm(this, GrMatrix::I());
2233 GrClip oldClip = this->getClip();
2234 this->setClip(GrRect::MakeWH(srcTexture->width(), srcTexture->height()));
2235 if (radius.fWidth > 0) {
2236 this->setRenderTarget(temp1->asRenderTarget());
2237 apply_morphology(fGpu, srcTexture, rect, radius.fWidth, filter,
2238 GrSamplerState::kX_FilterDirection);
2239 SkIRect clearRect = SkIRect::MakeXYWH(rect.fLeft, rect.fBottom,
2240 rect.width(), radius.fHeight);
2241 this->clear(&clearRect, 0x0);
2242 srcTexture = temp1;
2243 }
2244 if (radius.fHeight > 0) {
2245 this->setRenderTarget(temp2->asRenderTarget());
2246 apply_morphology(fGpu, srcTexture, rect, radius.fHeight, filter,
2247 GrSamplerState::kY_FilterDirection);
2248 srcTexture = temp2;
2249 }
2250 this->setRenderTarget(oldRenderTarget);
2251 this->setClip(oldClip);
2252 return srcTexture;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002253}
bsalomon@google.comc4364992011-11-07 15:54:49 +00002254
2255///////////////////////////////////////////////////////////////////////////////