blob: 4b3d942d615c5a21a07f4212750c0c1409fe5454 [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
bsalomon@google.com27847de2011-02-22 20:59:41 +00001542void GrContext::flushDrawBuffer() {
junov@google.com53a55842011-06-08 22:55:10 +00001543 if (fDrawBuffer) {
bsalomon@google.com97805382012-03-13 14:32:07 +00001544 fDrawBuffer->flushTo(fGpu);
junov@google.com53a55842011-06-08 22:55:10 +00001545 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001546}
1547
bsalomon@google.com6f379512011-11-16 20:36:03 +00001548void GrContext::internalWriteTexturePixels(GrTexture* texture,
1549 int left, int top,
1550 int width, int height,
1551 GrPixelConfig config,
1552 const void* buffer,
1553 size_t rowBytes,
1554 uint32_t flags) {
1555 SK_TRACE_EVENT0("GrContext::writeTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001556 ASSERT_OWNED_RESOURCE(texture);
1557
bsalomon@google.com6f379512011-11-16 20:36:03 +00001558 if (!(kDontFlush_PixelOpsFlag & flags)) {
1559 this->flush();
1560 }
1561 // TODO: use scratch texture to perform conversion
1562 if (GrPixelConfigIsUnpremultiplied(texture->config()) !=
1563 GrPixelConfigIsUnpremultiplied(config)) {
1564 return;
1565 }
1566
1567 fGpu->writeTexturePixels(texture, left, top, width, height,
1568 config, buffer, rowBytes);
1569}
1570
1571bool GrContext::internalReadTexturePixels(GrTexture* texture,
1572 int left, int top,
1573 int width, int height,
1574 GrPixelConfig config,
1575 void* buffer,
1576 size_t rowBytes,
1577 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001578 SK_TRACE_EVENT0("GrContext::readTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001579 ASSERT_OWNED_RESOURCE(texture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001580
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001581 // TODO: code read pixels for textures that aren't also rendertargets
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001582 GrRenderTarget* target = texture->asRenderTarget();
1583 if (NULL != target) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001584 return this->internalReadRenderTargetPixels(target,
1585 left, top, width, height,
1586 config, buffer, rowBytes,
1587 flags);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001588 } else {
1589 return false;
1590 }
1591}
1592
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001593#include "SkConfig8888.h"
1594
1595namespace {
1596/**
1597 * Converts a GrPixelConfig to a SkCanvas::Config8888. Only byte-per-channel
1598 * formats are representable as Config8888 and so the function returns false
1599 * if the GrPixelConfig has no equivalent Config8888.
1600 */
1601bool grconfig_to_config8888(GrPixelConfig config,
1602 SkCanvas::Config8888* config8888) {
1603 switch (config) {
1604 case kRGBA_8888_PM_GrPixelConfig:
1605 *config8888 = SkCanvas::kRGBA_Premul_Config8888;
1606 return true;
1607 case kRGBA_8888_UPM_GrPixelConfig:
1608 *config8888 = SkCanvas::kRGBA_Unpremul_Config8888;
1609 return true;
1610 case kBGRA_8888_PM_GrPixelConfig:
1611 *config8888 = SkCanvas::kBGRA_Premul_Config8888;
1612 return true;
1613 case kBGRA_8888_UPM_GrPixelConfig:
1614 *config8888 = SkCanvas::kBGRA_Unpremul_Config8888;
1615 return true;
1616 default:
1617 return false;
1618 }
1619}
1620}
1621
bsalomon@google.com6f379512011-11-16 20:36:03 +00001622bool GrContext::internalReadRenderTargetPixels(GrRenderTarget* target,
1623 int left, int top,
1624 int width, int height,
1625 GrPixelConfig config,
1626 void* buffer,
1627 size_t rowBytes,
1628 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001629 SK_TRACE_EVENT0("GrContext::readRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001630 ASSERT_OWNED_RESOURCE(target);
1631
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001632 if (NULL == target) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001633 target = fDrawState->getRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001634 if (NULL == target) {
1635 return false;
1636 }
1637 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001638
bsalomon@google.com6f379512011-11-16 20:36:03 +00001639 if (!(kDontFlush_PixelOpsFlag & flags)) {
1640 this->flush();
1641 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001642
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001643 if (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1644 GrPixelConfigIsUnpremultiplied(config) &&
1645 !fGpu->canPreserveReadWriteUnpremulPixels()) {
1646 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1647 if (!grconfig_to_config8888(target->config(), &srcConfig8888) ||
1648 !grconfig_to_config8888(config, &dstConfig8888)) {
1649 return false;
1650 }
1651 // do read back using target's own config
1652 this->internalReadRenderTargetPixels(target,
1653 left, top,
1654 width, height,
1655 target->config(),
1656 buffer, rowBytes,
1657 kDontFlush_PixelOpsFlag);
1658 // sw convert the pixels to unpremul config
1659 uint32_t* pixels = reinterpret_cast<uint32_t*>(buffer);
1660 SkConvertConfig8888Pixels(pixels, rowBytes, dstConfig8888,
1661 pixels, rowBytes, srcConfig8888,
1662 width, height);
1663 return true;
1664 }
1665
bsalomon@google.comc4364992011-11-07 15:54:49 +00001666 GrTexture* src = target->asTexture();
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001667 bool swapRAndB = NULL != src &&
1668 fGpu->preferredReadPixelsConfig(config) ==
1669 GrPixelConfigSwapRAndB(config);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001670
1671 bool flipY = NULL != src &&
1672 fGpu->readPixelsWillPayForYFlip(target, left, top,
1673 width, height, config,
1674 rowBytes);
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001675 bool alphaConversion = (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1676 GrPixelConfigIsUnpremultiplied(config));
bsalomon@google.comc4364992011-11-07 15:54:49 +00001677
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001678 if (NULL == src && alphaConversion) {
1679 // we should fallback to cpu conversion here. This could happen when
1680 // we were given an external render target by the client that is not
1681 // also a texture (e.g. FBO 0 in GL)
1682 return false;
1683 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001684 // we draw to a scratch texture if any of these conversion are applied
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001685 GrAutoScratchTexture ast;
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001686 if (flipY || swapRAndB || alphaConversion) {
1687 GrAssert(NULL != src);
1688 if (swapRAndB) {
1689 config = GrPixelConfigSwapRAndB(config);
1690 GrAssert(kUnknown_GrPixelConfig != config);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001691 }
1692 // Make the scratch a render target because we don't have a robust
1693 // readTexturePixels as of yet (it calls this function).
1694 const GrTextureDesc desc = {
1695 kRenderTarget_GrTextureFlagBit,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001696 width, height,
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001697 config,
bsalomon@google.comb9014f42012-03-30 14:22:41 +00001698 0 // samples
bsalomon@google.comc4364992011-11-07 15:54:49 +00001699 };
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001700
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001701 // When a full readback is faster than a partial we could always make
1702 // the scratch exactly match the passed rect. However, if we see many
1703 // different size rectangles we will trash our texture cache and pay the
1704 // cost of creating and destroying many textures. So, we only request
1705 // an exact match when the caller is reading an entire RT.
1706 ScratchTexMatch match = kApprox_ScratchTexMatch;
1707 if (0 == left &&
1708 0 == top &&
1709 target->width() == width &&
1710 target->height() == height &&
1711 fGpu->fullReadPixelsIsFasterThanPartial()) {
1712 match = kExact_ScratchTexMatch;
1713 }
1714 ast.set(this, desc, match);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001715 GrTexture* texture = ast.texture();
1716 if (!texture) {
1717 return false;
1718 }
1719 target = texture->asRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001720 GrAssert(NULL != target);
1721
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001722 GrDrawTarget::AutoStateRestore asr(fGpu,
1723 GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001724 GrDrawState* drawState = fGpu->drawState();
1725 drawState->setRenderTarget(target);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001726
bsalomon@google.comc4364992011-11-07 15:54:49 +00001727 GrMatrix matrix;
1728 if (flipY) {
1729 matrix.setTranslate(SK_Scalar1 * left,
1730 SK_Scalar1 * (top + height));
1731 matrix.set(GrMatrix::kMScaleY, -GR_Scalar1);
1732 } else {
1733 matrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
1734 }
1735 matrix.postIDiv(src->width(), src->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001736 drawState->sampler(0)->reset(matrix);
1737 drawState->sampler(0)->setRAndBSwap(swapRAndB);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001738 drawState->setTexture(0, src);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001739 GrRect rect;
1740 rect.setXYWH(0, 0, SK_Scalar1 * width, SK_Scalar1 * height);
1741 fGpu->drawSimpleRect(rect, NULL, 0x1);
1742 left = 0;
1743 top = 0;
1744 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001745 return fGpu->readPixels(target,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001746 left, top, width, height,
1747 config, buffer, rowBytes, flipY);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001748}
1749
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001750void GrContext::resolveRenderTarget(GrRenderTarget* target) {
1751 GrAssert(target);
1752 ASSERT_OWNED_RESOURCE(target);
1753 // In the future we may track whether there are any pending draws to this
1754 // target. We don't today so we always perform a flush. We don't promise
1755 // this to our clients, though.
1756 this->flush();
1757 fGpu->resolveRenderTarget(target);
1758}
1759
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001760void GrContext::copyTexture(GrTexture* src, GrRenderTarget* dst) {
1761 if (NULL == src || NULL == dst) {
1762 return;
1763 }
1764 ASSERT_OWNED_RESOURCE(src);
1765
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001766 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001767 GrDrawState* drawState = fGpu->drawState();
1768 drawState->setRenderTarget(dst);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001769 GrMatrix sampleM;
1770 sampleM.setIDiv(src->width(), src->height());
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001771 drawState->setTexture(0, src);
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001772 drawState->sampler(0)->reset(sampleM);
bsalomon@google.com5db3b6c2012-01-12 20:38:57 +00001773 SkRect rect = SkRect::MakeXYWH(0, 0,
1774 SK_Scalar1 * src->width(),
1775 SK_Scalar1 * src->height());
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001776 fGpu->drawSimpleRect(rect, NULL, 1 << 0);
1777}
1778
bsalomon@google.com6f379512011-11-16 20:36:03 +00001779void GrContext::internalWriteRenderTargetPixels(GrRenderTarget* target,
1780 int left, int top,
1781 int width, int height,
1782 GrPixelConfig config,
1783 const void* buffer,
1784 size_t rowBytes,
1785 uint32_t flags) {
1786 SK_TRACE_EVENT0("GrContext::writeRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001787 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com6f379512011-11-16 20:36:03 +00001788
1789 if (NULL == target) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001790 target = fDrawState->getRenderTarget();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001791 if (NULL == target) {
1792 return;
1793 }
1794 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001795
1796 // TODO: when underlying api has a direct way to do this we should use it
1797 // (e.g. glDrawPixels on desktop GL).
1798
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001799 // If the RT is also a texture and we don't have to do PM/UPM conversion
1800 // then take the texture path, which we expect to be at least as fast or
1801 // faster since it doesn't use an intermediate texture as we do below.
1802
1803#if !GR_MAC_BUILD
1804 // At least some drivers on the Mac get confused when glTexImage2D is called
1805 // on a texture attached to an FBO. The FBO still sees the old image. TODO:
1806 // determine what OS versions and/or HW is affected.
1807 if (NULL != target->asTexture() &&
1808 GrPixelConfigIsUnpremultiplied(target->config()) ==
1809 GrPixelConfigIsUnpremultiplied(config)) {
1810
1811 this->internalWriteTexturePixels(target->asTexture(),
1812 left, top, width, height,
1813 config, buffer, rowBytes, flags);
1814 return;
1815 }
1816#endif
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001817 if (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1818 GrPixelConfigIsUnpremultiplied(config) &&
1819 !fGpu->canPreserveReadWriteUnpremulPixels()) {
1820 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1821 if (!grconfig_to_config8888(config, &srcConfig8888) ||
1822 !grconfig_to_config8888(target->config(), &dstConfig8888)) {
1823 return;
1824 }
1825 // allocate a tmp buffer and sw convert the pixels to premul
1826 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(width * height);
1827 const uint32_t* src = reinterpret_cast<const uint32_t*>(buffer);
1828 SkConvertConfig8888Pixels(tmpPixels.get(), 4 * width, dstConfig8888,
1829 src, rowBytes, srcConfig8888,
1830 width, height);
1831 // upload the already premul pixels
1832 this->internalWriteRenderTargetPixels(target,
1833 left, top,
1834 width, height,
1835 target->config(),
1836 tmpPixels, 4 * width, flags);
1837 return;
1838 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001839
1840 bool swapRAndB = fGpu->preferredReadPixelsConfig(config) ==
1841 GrPixelConfigSwapRAndB(config);
1842 if (swapRAndB) {
1843 config = GrPixelConfigSwapRAndB(config);
1844 }
1845
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001846 const GrTextureDesc desc = {
bsalomon@google.comb9014f42012-03-30 14:22:41 +00001847 kNone_GrTextureFlags, width, height, config, 0
bsalomon@google.com27847de2011-02-22 20:59:41 +00001848 };
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001849 GrAutoScratchTexture ast(this, desc);
1850 GrTexture* texture = ast.texture();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001851 if (NULL == texture) {
1852 return;
1853 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001854 this->internalWriteTexturePixels(texture, 0, 0, width, height,
1855 config, buffer, rowBytes, flags);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001856
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001857 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001858 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001859
1860 GrMatrix matrix;
1861 matrix.setTranslate(GrIntToScalar(left), GrIntToScalar(top));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001862 drawState->setViewMatrix(matrix);
1863 drawState->setRenderTarget(target);
1864 drawState->setTexture(0, texture);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001865
bsalomon@google.com5c638652011-07-18 19:31:59 +00001866 matrix.setIDiv(texture->width(), texture->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001867 drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode,
1868 GrSamplerState::kNearest_Filter,
1869 matrix);
1870 drawState->sampler(0)->setRAndBSwap(swapRAndB);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001871
1872 GrVertexLayout layout = GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(0);
1873 static const int VCOUNT = 4;
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001874 // TODO: Use GrGpu::drawRect here
bsalomon@google.com27847de2011-02-22 20:59:41 +00001875 GrDrawTarget::AutoReleaseGeometry geo(fGpu, layout, VCOUNT, 0);
1876 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001877 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +00001878 return;
1879 }
1880 ((GrPoint*)geo.vertices())->setIRectFan(0, 0, width, height);
1881 fGpu->drawNonIndexed(kTriangleFan_PrimitiveType, 0, VCOUNT);
1882}
1883////////////////////////////////////////////////////////////////////////////////
1884
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001885void GrContext::setPaint(const GrPaint& paint) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001886
1887 for (int i = 0; i < GrPaint::kMaxTextures; ++i) {
1888 int s = i + GrPaint::kFirstTextureStage;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001889 fDrawState->setTexture(s, paint.getTexture(i));
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001890 ASSERT_OWNED_RESOURCE(paint.getTexture(i));
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001891 if (paint.getTexture(i)) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001892 *fDrawState->sampler(s) = paint.getTextureSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001893 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001894 }
1895
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001896 fDrawState->setFirstCoverageStage(GrPaint::kFirstMaskStage);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001897
1898 for (int i = 0; i < GrPaint::kMaxMasks; ++i) {
1899 int s = i + GrPaint::kFirstMaskStage;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001900 fDrawState->setTexture(s, paint.getMask(i));
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001901 ASSERT_OWNED_RESOURCE(paint.getMask(i));
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001902 if (paint.getMask(i)) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001903 *fDrawState->sampler(s) = paint.getMaskSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001904 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001905 }
bsalomon@google.com26936d02012-03-19 13:06:19 +00001906
1907 // disable all stages not accessible via the paint
1908 for (int s = GrPaint::kTotalStages; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001909 fDrawState->setTexture(s, NULL);
bsalomon@google.com26936d02012-03-19 13:06:19 +00001910 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001911
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001912 fDrawState->setColor(paint.fColor);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001913
1914 if (paint.fDither) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001915 fDrawState->enableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001916 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001917 fDrawState->disableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001918 }
1919 if (paint.fAntiAlias) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001920 fDrawState->enableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001921 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001922 fDrawState->disableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001923 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001924 if (paint.fColorMatrixEnabled) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001925 fDrawState->enableState(GrDrawState::kColorMatrix_StateBit);
1926 fDrawState->setColorMatrix(paint.fColorMatrix);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001927 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001928 fDrawState->disableState(GrDrawState::kColorMatrix_StateBit);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001929 }
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001930 fDrawState->setBlendFunc(paint.fSrcBlendCoeff, paint.fDstBlendCoeff);
1931 fDrawState->setColorFilter(paint.fColorFilterColor, paint.fColorFilterXfermode);
1932 fDrawState->setCoverage(paint.fCoverage);
bsalomon@google.come79c8152012-03-29 19:07:12 +00001933#if GR_DEBUG
1934 if ((paint.getActiveMaskStageMask() || 0xff != paint.fCoverage) &&
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001935 !fGpu->canApplyCoverage()) {
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001936 GrPrintf("Partial pixel coverage will be incorrectly blended.\n");
1937 }
bsalomon@google.com95cd7bd2012-03-28 15:35:05 +00001938#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +00001939}
1940
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001941GrDrawTarget* GrContext::prepareToDraw(const GrPaint& paint,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001942 DrawCategory category) {
1943 if (category != fLastDrawCategory) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001944 this->flushDrawBuffer();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001945 fLastDrawCategory = category;
1946 }
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001947 this->setPaint(paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001948 GrDrawTarget* target = fGpu;
1949 switch (category) {
bsalomon@google.com193395c2012-03-30 17:35:12 +00001950 case kUnbuffered_DrawCategory:
1951 target = fGpu;
1952 break;
1953 case kBuffered_DrawCategory:
1954 target = fDrawBuffer;
1955 fDrawBuffer->setClip(fGpu->getClip());
1956 break;
1957 default:
1958 GrCrash("Unexpected DrawCategory.");
1959 break;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001960 }
1961 return target;
1962}
1963
bsalomon@google.com289533a2011-10-27 12:34:25 +00001964GrPathRenderer* GrContext::getPathRenderer(const GrPath& path,
1965 GrPathFill fill,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001966 const GrDrawTarget* target,
bsalomon@google.com289533a2011-10-27 12:34:25 +00001967 bool antiAlias) {
bsalomon@google.com30085192011-08-19 15:42:31 +00001968 if (NULL == fPathRendererChain) {
1969 fPathRendererChain =
1970 new GrPathRendererChain(this, GrPathRendererChain::kNone_UsageFlag);
1971 }
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001972 return fPathRendererChain->getPathRenderer(path, fill, target, antiAlias);
bsalomon@google.com30085192011-08-19 15:42:31 +00001973}
1974
bsalomon@google.com27847de2011-02-22 20:59:41 +00001975////////////////////////////////////////////////////////////////////////////////
1976
bsalomon@google.com27847de2011-02-22 20:59:41 +00001977void GrContext::setRenderTarget(GrRenderTarget* target) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001978 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001979 if (fDrawState->getRenderTarget() != target) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001980 this->flush(false);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001981 fDrawState->setRenderTarget(target);
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001982 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001983}
1984
1985GrRenderTarget* GrContext::getRenderTarget() {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001986 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001987}
1988
1989const GrRenderTarget* GrContext::getRenderTarget() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001990 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001991}
1992
1993const GrMatrix& GrContext::getMatrix() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001994 return fDrawState->getViewMatrix();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001995}
1996
1997void GrContext::setMatrix(const GrMatrix& m) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001998 fDrawState->setViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001999}
2000
2001void GrContext::concatMatrix(const GrMatrix& m) const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00002002 fDrawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00002003}
2004
2005static inline intptr_t setOrClear(intptr_t bits, int shift, intptr_t pred) {
2006 intptr_t mask = 1 << shift;
2007 if (pred) {
2008 bits |= mask;
2009 } else {
2010 bits &= ~mask;
2011 }
2012 return bits;
2013}
2014
2015void GrContext::resetStats() {
2016 fGpu->resetStats();
2017}
2018
bsalomon@google.com05ef5102011-05-02 21:14:59 +00002019const GrGpuStats& GrContext::getStats() const {
bsalomon@google.com27847de2011-02-22 20:59:41 +00002020 return fGpu->getStats();
2021}
2022
2023void GrContext::printStats() const {
2024 fGpu->printStats();
2025}
2026
bsalomon@google.com583a1e32011-08-17 13:42:46 +00002027GrContext::GrContext(GrGpu* gpu) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00002028 fGpu = gpu;
2029 fGpu->ref();
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002030 fGpu->setContext(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +00002031
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00002032 fDrawState = new GrDrawState();
2033 fGpu->setDrawState(fDrawState);
2034
bsalomon@google.com30085192011-08-19 15:42:31 +00002035 fPathRendererChain = NULL;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00002036
bsalomon@google.com50398bf2011-07-26 20:45:30 +00002037 fTextureCache = new GrResourceCache(MAX_TEXTURE_CACHE_COUNT,
2038 MAX_TEXTURE_CACHE_BYTES);
bsalomon@google.com27847de2011-02-22 20:59:41 +00002039 fFontCache = new GrFontCache(fGpu);
2040
2041 fLastDrawCategory = kUnbuffered_DrawCategory;
2042
bsalomon@google.com8fe72472011-03-30 21:26:44 +00002043 fDrawBuffer = NULL;
2044 fDrawBufferVBAllocPool = NULL;
2045 fDrawBufferIBAllocPool = NULL;
2046
bsalomon@google.com205d4602011-04-25 12:43:45 +00002047 fAAFillRectIndexBuffer = NULL;
2048 fAAStrokeRectIndexBuffer = NULL;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00002049
bsalomon@google.com8fe72472011-03-30 21:26:44 +00002050 this->setupDrawBuffer();
2051}
2052
2053void GrContext::setupDrawBuffer() {
2054
2055 GrAssert(NULL == fDrawBuffer);
2056 GrAssert(NULL == fDrawBufferVBAllocPool);
2057 GrAssert(NULL == fDrawBufferIBAllocPool);
2058
bsalomon@google.com27847de2011-02-22 20:59:41 +00002059#if DEFER_TEXT_RENDERING || BATCH_RECT_TO_RECT
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00002060 fDrawBufferVBAllocPool =
bsalomon@google.com8fe72472011-03-30 21:26:44 +00002061 new GrVertexBufferAllocPool(fGpu, false,
bsalomon@google.com27847de2011-02-22 20:59:41 +00002062 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
2063 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS);
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00002064 fDrawBufferIBAllocPool =
bsalomon@google.com8fe72472011-03-30 21:26:44 +00002065 new GrIndexBufferAllocPool(fGpu, false,
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00002066 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
bsalomon@google.com27847de2011-02-22 20:59:41 +00002067 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS);
2068
bsalomon@google.com471d4712011-08-23 15:45:25 +00002069 fDrawBuffer = new GrInOrderDrawBuffer(fGpu,
2070 fDrawBufferVBAllocPool,
bsalomon@google.com27847de2011-02-22 20:59:41 +00002071 fDrawBufferIBAllocPool);
bsalomon@google.com27847de2011-02-22 20:59:41 +00002072#endif
2073
2074#if BATCH_RECT_TO_RECT
2075 fDrawBuffer->setQuadIndexBuffer(this->getQuadIndexBuffer());
2076#endif
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00002077 fDrawBuffer->setAutoFlushTarget(fGpu);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00002078 fDrawBuffer->setDrawState(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +00002079}
2080
bsalomon@google.com27847de2011-02-22 20:59:41 +00002081GrDrawTarget* GrContext::getTextTarget(const GrPaint& paint) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00002082#if DEFER_TEXT_RENDERING
bsalomon@google.com193395c2012-03-30 17:35:12 +00002083 return prepareToDraw(paint, kBuffered_DrawCategory);
bsalomon@google.com27847de2011-02-22 20:59:41 +00002084#else
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00002085 return prepareToDraw(paint, kUnbuffered_DrawCategory);
bsalomon@google.com27847de2011-02-22 20:59:41 +00002086#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +00002087}
2088
2089const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {
2090 return fGpu->getQuadIndexBuffer();
2091}
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00002092
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002093GrTexture* GrContext::gaussianBlur(GrTexture* srcTexture,
2094 GrAutoScratchTexture* temp1,
2095 GrAutoScratchTexture* temp2,
2096 const SkRect& rect,
2097 float sigmaX, float sigmaY) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00002098 ASSERT_OWNED_RESOURCE(srcTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002099 GrRenderTarget* oldRenderTarget = this->getRenderTarget();
2100 GrClip oldClip = this->getClip();
2101 GrTexture* origTexture = srcTexture;
2102 GrAutoMatrix avm(this, GrMatrix::I());
2103 SkIRect clearRect;
2104 int scaleFactorX, halfWidthX, kernelWidthX;
2105 int scaleFactorY, halfWidthY, kernelWidthY;
2106 sigmaX = adjust_sigma(sigmaX, &scaleFactorX, &halfWidthX, &kernelWidthX);
2107 sigmaY = adjust_sigma(sigmaY, &scaleFactorY, &halfWidthY, &kernelWidthY);
bsalomon@google.combc4b6542011-11-19 13:56:11 +00002108
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002109 SkRect srcRect(rect);
2110 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
2111 srcRect.roundOut();
2112 scale_rect(&srcRect, scaleFactorX, scaleFactorY);
2113 this->setClip(srcRect);
2114
2115 const GrTextureDesc desc = {
2116 kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit,
bungeman@google.comf8aa18c2012-03-19 21:04:52 +00002117 SkScalarFloorToInt(srcRect.width()),
2118 SkScalarFloorToInt(srcRect.height()),
bsalomon@google.comb9014f42012-03-30 14:22:41 +00002119 kRGBA_8888_PM_GrPixelConfig,
2120 0 // samples
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002121 };
2122
2123 temp1->set(this, desc);
2124 if (temp2) temp2->set(this, desc);
2125
2126 GrTexture* dstTexture = temp1->texture();
2127 GrPaint paint;
2128 paint.reset();
2129 paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
2130
2131 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
2132 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
2133 srcTexture->height());
2134 this->setRenderTarget(dstTexture->asRenderTarget());
2135 SkRect dstRect(srcRect);
2136 scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
2137 i < scaleFactorY ? 0.5f : 1.0f);
2138 paint.setTexture(0, srcTexture);
2139 this->drawRectToRect(paint, dstRect, srcRect);
2140 srcRect = dstRect;
2141 SkTSwap(srcTexture, dstTexture);
2142 // If temp2 is non-NULL, don't render back to origTexture
2143 if (temp2 && dstTexture == origTexture) dstTexture = temp2->texture();
2144 }
2145
2146 if (sigmaX > 0.0f) {
2147 SkAutoTMalloc<float> kernelStorageX(kernelWidthX);
2148 float* kernelX = kernelStorageX.get();
2149 build_kernel(sigmaX, kernelX, kernelWidthX);
2150
2151 if (scaleFactorX > 1) {
2152 // Clear out a halfWidth to the right of the srcRect to prevent the
2153 // X convolution from reading garbage.
2154 clearRect = SkIRect::MakeXYWH(
2155 srcRect.fRight, srcRect.fTop, halfWidthX, srcRect.height());
2156 this->clear(&clearRect, 0x0);
2157 }
2158
2159 this->setRenderTarget(dstTexture->asRenderTarget());
2160 convolve(fGpu, srcTexture, srcRect, kernelX, kernelWidthX,
2161 GrSamplerState::kX_FilterDirection);
2162 SkTSwap(srcTexture, dstTexture);
2163 if (temp2 && dstTexture == origTexture) dstTexture = temp2->texture();
2164 }
2165
2166 if (sigmaY > 0.0f) {
2167 SkAutoTMalloc<float> kernelStorageY(kernelWidthY);
2168 float* kernelY = kernelStorageY.get();
2169 build_kernel(sigmaY, kernelY, kernelWidthY);
2170
2171 if (scaleFactorY > 1 || sigmaX > 0.0f) {
2172 // Clear out a halfWidth below the srcRect to prevent the Y
2173 // convolution from reading garbage.
2174 clearRect = SkIRect::MakeXYWH(
2175 srcRect.fLeft, srcRect.fBottom, srcRect.width(), halfWidthY);
2176 this->clear(&clearRect, 0x0);
2177 }
2178
2179 this->setRenderTarget(dstTexture->asRenderTarget());
2180 convolve(fGpu, srcTexture, srcRect, kernelY, kernelWidthY,
2181 GrSamplerState::kY_FilterDirection);
2182 SkTSwap(srcTexture, dstTexture);
2183 if (temp2 && dstTexture == origTexture) dstTexture = temp2->texture();
2184 }
2185
2186 if (scaleFactorX > 1 || scaleFactorY > 1) {
2187 // Clear one pixel to the right and below, to accommodate bilinear
2188 // upsampling.
2189 clearRect = SkIRect::MakeXYWH(
2190 srcRect.fLeft, srcRect.fBottom, srcRect.width() + 1, 1);
2191 this->clear(&clearRect, 0x0);
2192 clearRect = SkIRect::MakeXYWH(
2193 srcRect.fRight, srcRect.fTop, 1, srcRect.height());
2194 this->clear(&clearRect, 0x0);
2195 // FIXME: This should be mitchell, not bilinear.
2196 paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
2197 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
2198 srcTexture->height());
2199 this->setRenderTarget(dstTexture->asRenderTarget());
2200 paint.setTexture(0, srcTexture);
2201 SkRect dstRect(srcRect);
2202 scale_rect(&dstRect, scaleFactorX, scaleFactorY);
2203 this->drawRectToRect(paint, dstRect, srcRect);
2204 srcRect = dstRect;
2205 SkTSwap(srcTexture, dstTexture);
2206 }
2207 this->setRenderTarget(oldRenderTarget);
2208 this->setClip(oldClip);
2209 return srcTexture;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00002210}
bsalomon@google.com1e266f82011-12-12 16:11:33 +00002211
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002212GrTexture* GrContext::applyMorphology(GrTexture* srcTexture,
2213 const GrRect& rect,
2214 GrTexture* temp1, GrTexture* temp2,
2215 GrSamplerState::Filter filter,
2216 SkISize radius) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00002217 ASSERT_OWNED_RESOURCE(srcTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002218 GrRenderTarget* oldRenderTarget = this->getRenderTarget();
2219 GrAutoMatrix avm(this, GrMatrix::I());
2220 GrClip oldClip = this->getClip();
2221 this->setClip(GrRect::MakeWH(srcTexture->width(), srcTexture->height()));
2222 if (radius.fWidth > 0) {
2223 this->setRenderTarget(temp1->asRenderTarget());
2224 apply_morphology(fGpu, srcTexture, rect, radius.fWidth, filter,
2225 GrSamplerState::kX_FilterDirection);
2226 SkIRect clearRect = SkIRect::MakeXYWH(rect.fLeft, rect.fBottom,
2227 rect.width(), radius.fHeight);
2228 this->clear(&clearRect, 0x0);
2229 srcTexture = temp1;
2230 }
2231 if (radius.fHeight > 0) {
2232 this->setRenderTarget(temp2->asRenderTarget());
2233 apply_morphology(fGpu, srcTexture, rect, radius.fHeight, filter,
2234 GrSamplerState::kY_FilterDirection);
2235 srcTexture = temp2;
2236 }
2237 this->setRenderTarget(oldRenderTarget);
2238 this->setClip(oldClip);
2239 return srcTexture;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002240}
bsalomon@google.comc4364992011-11-07 15:54:49 +00002241
2242///////////////////////////////////////////////////////////////////////////////