blob: c37f31c0cc4994c71bfbdbfe344f24f90c74409d [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
bsalomon@google.com1fadb202011-12-12 16:10:08 +00009#include "GrContext.h"
bsalomon682c2692015-05-22 14:01:46 -070010#include "GrContextOptions.h"
robertphillips77a2e522015-10-17 07:43:27 -070011#include "GrDrawingManager.h"
robertphillipsea461502015-05-26 11:38:03 -070012#include "GrDrawContext.h"
robertphillips@google.come930a072014-04-03 00:34:27 +000013#include "GrLayerCache.h"
bsalomon0ea80f42015-02-11 10:49:59 -080014#include "GrResourceCache.h"
bsalomond309e7a2015-04-30 14:18:54 -070015#include "GrResourceProvider.h"
robertphillips@google.com72176b22012-05-23 13:19:12 +000016#include "GrSoftwarePathRenderer.h"
bsalomonafbf2d62014-09-30 12:18:44 -070017#include "GrSurfacePriv.h"
robertphillips3dc6ae52015-10-20 09:54:32 -070018
bsalomon81beccc2014-10-13 12:32:55 -070019#include "SkConfig8888.h"
bsalomonf276ac52015-10-09 13:36:42 -070020#include "SkGrPriv.h"
joshualitt74417822015-08-07 11:42:16 -070021
joshualitt5478d422014-11-14 16:00:38 -080022#include "effects/GrConfigConversionEffect.h"
joshualitte8042922015-12-11 06:11:21 -080023#include "text/GrTextBlobCache.h"
joshualitt5478d422014-11-14 16:00:38 -080024
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000025#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
joshualitt1de610a2016-01-06 08:26:09 -080026#define ASSERT_SINGLE_OWNER \
27 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fSingleOwner);)
robertphillips77a2e522015-10-17 07:43:27 -070028#define RETURN_IF_ABANDONED if (fDrawingManager->abandoned()) { return; }
29#define RETURN_FALSE_IF_ABANDONED if (fDrawingManager->abandoned()) { return false; }
30#define RETURN_NULL_IF_ABANDONED if (fDrawingManager->abandoned()) { return nullptr; }
bsalomon@google.combc4b6542011-11-19 13:56:11 +000031
robertphillipsea461502015-05-26 11:38:03 -070032////////////////////////////////////////////////////////////////////////////////
33
bsalomon682c2692015-05-22 14:01:46 -070034GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext) {
35 GrContextOptions defaultOptions;
36 return Create(backend, backendContext, defaultOptions);
37}
bsalomonf28cff72015-05-22 12:25:41 -070038
bsalomon682c2692015-05-22 14:01:46 -070039GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext,
40 const GrContextOptions& options) {
halcanary385fe4d2015-08-26 13:07:48 -070041 GrContext* context = new GrContext;
bsalomon682c2692015-05-22 14:01:46 -070042
43 if (context->init(backend, backendContext, options)) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000044 return context;
45 } else {
46 context->unref();
halcanary96fcdcc2015-08-27 07:41:13 -070047 return nullptr;
bsalomon@google.com27847de2011-02-22 20:59:41 +000048 }
bsalomon@google.com27847de2011-02-22 20:59:41 +000049}
50
joshualitt0acd0d32015-05-07 08:23:19 -070051static int32_t gNextID = 1;
52static int32_t next_id() {
53 int32_t id;
54 do {
55 id = sk_atomic_inc(&gNextID);
56 } while (id == SK_InvalidGenID);
57 return id;
58}
59
bsalomon682c2692015-05-22 14:01:46 -070060GrContext::GrContext() : fUniqueID(next_id()) {
halcanary96fcdcc2015-08-27 07:41:13 -070061 fGpu = nullptr;
62 fCaps = nullptr;
63 fResourceCache = nullptr;
64 fResourceProvider = nullptr;
halcanary96fcdcc2015-08-27 07:41:13 -070065 fBatchFontCache = nullptr;
bsalomonf21dab92014-11-13 13:33:28 -080066 fFlushToReduceCacheSize = false;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000067}
68
bsalomon682c2692015-05-22 14:01:46 -070069bool GrContext::init(GrBackend backend, GrBackendContext backendContext,
70 const GrContextOptions& options) {
joshualitt1de610a2016-01-06 08:26:09 -080071 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -070072 SkASSERT(!fGpu);
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000073
bsalomon682c2692015-05-22 14:01:46 -070074 fGpu = GrGpu::Create(backend, backendContext, options, this);
robertphillipsea461502015-05-26 11:38:03 -070075 if (!fGpu) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000076 return false;
77 }
bsalomon69cfe952015-11-30 13:27:47 -080078 this->initCommon(options);
bsalomon33435572014-11-05 14:47:41 -080079 return true;
80}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000081
bsalomon69cfe952015-11-30 13:27:47 -080082void GrContext::initCommon(const GrContextOptions& options) {
joshualitt1de610a2016-01-06 08:26:09 -080083 ASSERT_SINGLE_OWNER
84
bsalomon76228632015-05-29 08:02:10 -070085 fCaps = SkRef(fGpu->caps());
halcanary385fe4d2015-08-26 13:07:48 -070086 fResourceCache = new GrResourceCache(fCaps);
bsalomon0ea80f42015-02-11 10:49:59 -080087 fResourceCache->setOverBudgetCallback(OverBudgetCB, this);
joshualitt6d0872d2016-01-11 08:27:48 -080088 fResourceProvider = new GrResourceProvider(fGpu, fResourceCache, &fSingleOwner);
commit-bot@chromium.org1836d332013-07-16 22:55:03 +000089
halcanary385fe4d2015-08-26 13:07:48 -070090 fLayerCache.reset(new GrLayerCache(this));
robertphillips@google.come930a072014-04-03 00:34:27 +000091
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000092 fDidTestPMConversions = false;
93
bsalomon69cfe952015-11-30 13:27:47 -080094 GrDrawTarget::Options dtOptions;
95 dtOptions.fClipBatchToBounds = options.fClipBatchToBounds;
bsalomon6dea83f2015-12-03 12:58:06 -080096 dtOptions.fDrawBatchBounds = options.fDrawBatchBounds;
bsalomon489147c2015-12-14 12:13:09 -080097 dtOptions.fMaxBatchLookback = options.fMaxBatchLookback;
joshualittde8dc7e2016-01-08 10:09:13 -080098 fDrawingManager.reset(new GrDrawingManager(this, dtOptions, &fSingleOwner));
joshualitt7c3a2f82015-03-31 13:32:05 -070099
100 // GrBatchFontCache will eventually replace GrFontCache
halcanary385fe4d2015-08-26 13:07:48 -0700101 fBatchFontCache = new GrBatchFontCache(this);
joshualittb7133be2015-04-08 09:08:31 -0700102
halcanary385fe4d2015-08-26 13:07:48 -0700103 fTextBlobCache.reset(new GrTextBlobCache(TextBlobCacheOverBudgetCB, this));
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000104}
105
bsalomon@google.com27847de2011-02-22 20:59:41 +0000106GrContext::~GrContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800107 ASSERT_SINGLE_OWNER
108
robertphillipsea461502015-05-26 11:38:03 -0700109 if (!fGpu) {
bsalomon76228632015-05-29 08:02:10 -0700110 SkASSERT(!fCaps);
bsalomon@google.com733c0622013-04-24 17:59:32 +0000111 return;
112 }
113
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000114 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000115
robertphillips77a2e522015-10-17 07:43:27 -0700116 fDrawingManager->cleanup();
robertphillips2334fb62015-06-17 05:43:33 -0700117
robertphillips@google.com950b1b02013-10-21 17:37:28 +0000118 for (int i = 0; i < fCleanUpData.count(); ++i) {
119 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo);
120 }
121
halcanary385fe4d2015-08-26 13:07:48 -0700122 delete fResourceProvider;
123 delete fResourceCache;
124 delete fBatchFontCache;
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000125
bsalomon@google.com205d4602011-04-25 12:43:45 +0000126 fGpu->unref();
bsalomon76228632015-05-29 08:02:10 -0700127 fCaps->unref();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000128}
129
bsalomon2354f842014-07-28 13:48:36 -0700130void GrContext::abandonContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800131 ASSERT_SINGLE_OWNER
132
bsalomond309e7a2015-04-30 14:18:54 -0700133 fResourceProvider->abandon();
robertphillips0dfa62c2015-11-16 06:23:31 -0800134
135 // Need to abandon the drawing manager first so all the render targets
136 // will be released/forgotten before they too are abandoned.
137 fDrawingManager->abandon();
138
bsalomon@google.com205d4602011-04-25 12:43:45 +0000139 // abandon first to so destructors
140 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800141 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700142
robertphillipse3371302014-09-17 06:01:06 -0700143 fGpu->contextAbandoned();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000144
joshualitt7c3a2f82015-03-31 13:32:05 -0700145 fBatchFontCache->freeAll();
robertphillips@google.come930a072014-04-03 00:34:27 +0000146 fLayerCache->freeAll();
joshualitt26ffc002015-04-16 11:24:04 -0700147 fTextBlobCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000148}
149
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000150void GrContext::resetContext(uint32_t state) {
joshualitt1de610a2016-01-06 08:26:09 -0800151 ASSERT_SINGLE_OWNER
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000152 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000153}
154
155void GrContext::freeGpuResources() {
joshualitt1de610a2016-01-06 08:26:09 -0800156 ASSERT_SINGLE_OWNER
157
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000158 this->flush();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000159
joshualitt7c3a2f82015-03-31 13:32:05 -0700160 fBatchFontCache->freeAll();
robertphillips@google.come930a072014-04-03 00:34:27 +0000161 fLayerCache->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700162
163 fDrawingManager->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700164
165 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000166}
167
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000168void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800169 ASSERT_SINGLE_OWNER
170
bsalomon71cb0c22014-11-14 12:10:14 -0800171 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800172 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800173 }
174 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800175 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800176 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000177}
178
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000179////////////////////////////////////////////////////////////////////////////////
180
bsalomon71cb0c22014-11-14 12:10:14 -0800181void GrContext::OverBudgetCB(void* data) {
bsalomon66a450f2014-11-13 13:19:10 -0800182 SkASSERT(data);
bsalomonf21dab92014-11-13 13:33:28 -0800183
bsalomon66a450f2014-11-13 13:19:10 -0800184 GrContext* context = reinterpret_cast<GrContext*>(data);
bsalomonf21dab92014-11-13 13:33:28 -0800185
joshualittb542bae2015-07-28 09:58:39 -0700186 // Flush the GrBufferedDrawTarget to possibly free up some textures
bsalomonf21dab92014-11-13 13:33:28 -0800187 context->fFlushToReduceCacheSize = true;
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000188}
189
joshualitt0db6dfa2015-04-10 07:01:30 -0700190void GrContext::TextBlobCacheOverBudgetCB(void* data) {
191 SkASSERT(data);
192
193 // Unlike the GrResourceCache, TextBlobs are drawn at the SkGpuDevice level, therefore they
194 // cannot use fFlushTorReduceCacheSize because it uses AutoCheckFlush. The solution is to move
195 // drawText calls to below the GrContext level, but this is not trivial because they call
196 // drawPath on SkGpuDevice
197 GrContext* context = reinterpret_cast<GrContext*>(data);
198 context->flush();
199}
200
bsalomon@google.com27847de2011-02-22 20:59:41 +0000201////////////////////////////////////////////////////////////////////////////////
202
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000203void GrContext::flush(int flagsBitfield) {
joshualitt1de610a2016-01-06 08:26:09 -0800204 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700205 RETURN_IF_ABANDONED
robertphillips@google.come7db8d62013-07-04 11:48:52 +0000206
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000207 if (kDiscard_FlushBit & flagsBitfield) {
robertphillips77a2e522015-10-17 07:43:27 -0700208 fDrawingManager->reset();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000209 } else {
robertphillips77a2e522015-10-17 07:43:27 -0700210 fDrawingManager->flush();
junov@google.com53a55842011-06-08 22:55:10 +0000211 }
bsalomon3f324322015-04-08 11:01:54 -0700212 fResourceCache->notifyFlushOccurred();
bsalomonf21dab92014-11-13 13:33:28 -0800213 fFlushToReduceCacheSize = false;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000214}
215
bsalomon81beccc2014-10-13 12:32:55 -0700216bool sw_convert_to_premul(GrPixelConfig srcConfig, int width, int height, size_t inRowBytes,
217 const void* inPixels, size_t outRowBytes, void* outPixels) {
218 SkSrcPixelInfo srcPI;
halcanary96fcdcc2015-08-27 07:41:13 -0700219 if (!GrPixelConfig2ColorAndProfileType(srcConfig, &srcPI.fColorType, nullptr)) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000220 return false;
221 }
bsalomon81beccc2014-10-13 12:32:55 -0700222 srcPI.fAlphaType = kUnpremul_SkAlphaType;
223 srcPI.fPixels = inPixels;
224 srcPI.fRowBytes = inRowBytes;
225
226 SkDstPixelInfo dstPI;
227 dstPI.fColorType = srcPI.fColorType;
228 dstPI.fAlphaType = kPremul_SkAlphaType;
229 dstPI.fPixels = outPixels;
230 dstPI.fRowBytes = outRowBytes;
231
232 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000233}
234
bsalomon81beccc2014-10-13 12:32:55 -0700235bool GrContext::writeSurfacePixels(GrSurface* surface,
236 int left, int top, int width, int height,
237 GrPixelConfig srcConfig, const void* buffer, size_t rowBytes,
238 uint32_t pixelOpsFlags) {
joshualitt1de610a2016-01-06 08:26:09 -0800239 ASSERT_SINGLE_OWNER
joshualitt5f5a8d72015-02-25 14:09:45 -0800240 RETURN_FALSE_IF_ABANDONED
bsalomon6c6f6582015-09-10 08:12:46 -0700241 ASSERT_OWNED_RESOURCE(surface);
242 SkASSERT(surface);
243
244 this->testPMConversionsIfNecessary(pixelOpsFlags);
bsalomon81beccc2014-10-13 12:32:55 -0700245
bsalomone8d21e82015-07-16 08:23:13 -0700246 // Trim the params here so that if we wind up making a temporary surface it can be as small as
bsalomonf0674512015-07-28 13:26:15 -0700247 // necessary and because GrGpu::getWritePixelsInfo requires it.
bsalomone8d21e82015-07-16 08:23:13 -0700248 if (!GrSurfacePriv::AdjustWritePixelParams(surface->width(), surface->height(),
249 GrBytesPerPixel(srcConfig), &left, &top, &width,
250 &height, &buffer, &rowBytes)) {
251 return false;
252 }
253
bsalomonf0674512015-07-28 13:26:15 -0700254 bool applyPremulToSrc = false;
bsalomon81beccc2014-10-13 12:32:55 -0700255 if (kUnpremul_PixelOpsFlag & pixelOpsFlags) {
256 if (!GrPixelConfigIs8888(srcConfig)) {
257 return false;
258 }
bsalomonf0674512015-07-28 13:26:15 -0700259 applyPremulToSrc = true;
260 }
bsalomon636e8022015-07-29 06:08:46 -0700261
262 GrGpu::DrawPreference drawPreference = GrGpu::kNoDraw_DrawPreference;
263 // Don't prefer to draw for the conversion (and thereby access a texture from the cache) when
264 // we've already determined that there isn't a roundtrip preserving conversion processor pair.
265 if (applyPremulToSrc && !this->didFailPMUPMConversionTest()) {
266 drawPreference = GrGpu::kCallerPrefersDraw_DrawPreference;
267 }
268
bsalomonf0674512015-07-28 13:26:15 -0700269 GrGpu::WritePixelTempDrawInfo tempDrawInfo;
270 if (!fGpu->getWritePixelsInfo(surface, width, height, rowBytes, srcConfig, &drawPreference,
271 &tempDrawInfo)) {
272 return false;
273 }
274
275 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && surface->surfacePriv().hasPendingIO()) {
276 this->flush();
277 }
278
279 SkAutoTUnref<GrTexture> tempTexture;
280 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
bsalomoneae62002015-07-31 13:59:30 -0700281 tempTexture.reset(
282 this->textureProvider()->createApproxTexture(tempDrawInfo.fTempSurfaceDesc));
bsalomonf0674512015-07-28 13:26:15 -0700283 if (!tempTexture && GrGpu::kRequireDraw_DrawPreference == drawPreference) {
284 return false;
285 }
286 }
287
288 // temp buffer for doing sw premul conversion, if needed.
benjaminwagner7d974f52015-10-19 13:55:55 -0700289#if defined(GOOGLE3)
290 // Stack frame size is limited in GOOGLE3.
291 SkAutoSTMalloc<48 * 48, uint32_t> tmpPixels(0);
292#else
bsalomonf0674512015-07-28 13:26:15 -0700293 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
benjaminwagner7d974f52015-10-19 13:55:55 -0700294#endif
bsalomonf0674512015-07-28 13:26:15 -0700295 if (tempTexture) {
296 SkAutoTUnref<const GrFragmentProcessor> fp;
297 SkMatrix textureMatrix;
298 textureMatrix.setIDiv(tempTexture->width(), tempTexture->height());
bsalomonf0674512015-07-28 13:26:15 -0700299 if (applyPremulToSrc) {
bsalomon4a339522015-10-06 08:40:50 -0700300 fp.reset(this->createUPMToPMEffect(tempTexture, tempDrawInfo.fSwapRAndB,
301 textureMatrix));
bsalomonf0674512015-07-28 13:26:15 -0700302 // If premultiplying was the only reason for the draw, fall back to a straight write.
303 if (!fp) {
304 if (GrGpu::kCallerPrefersDraw_DrawPreference == drawPreference) {
halcanary96fcdcc2015-08-27 07:41:13 -0700305 tempTexture.reset(nullptr);
bsalomonf0674512015-07-28 13:26:15 -0700306 }
307 } else {
308 applyPremulToSrc = false;
309 }
310 }
311 if (tempTexture) {
312 if (!fp) {
bsalomon4a339522015-10-06 08:40:50 -0700313 fp.reset(GrConfigConversionEffect::Create(tempTexture, tempDrawInfo.fSwapRAndB,
bsalomonf0674512015-07-28 13:26:15 -0700314 GrConfigConversionEffect::kNone_PMConversion, textureMatrix));
315 if (!fp) {
316 return false;
317 }
318 }
319 GrRenderTarget* renderTarget = surface->asRenderTarget();
320 SkASSERT(renderTarget);
321 if (tempTexture->surfacePriv().hasPendingIO()) {
322 this->flush();
323 }
324 if (applyPremulToSrc) {
325 size_t tmpRowBytes = 4 * width;
326 tmpPixels.reset(width * height);
327 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
328 tmpPixels.get())) {
329 return false;
330 }
331 rowBytes = tmpRowBytes;
332 buffer = tmpPixels.get();
333 applyPremulToSrc = false;
334 }
bsalomon6cb3cbe2015-07-30 07:34:27 -0700335 if (!fGpu->writePixels(tempTexture, 0, 0, width, height,
336 tempDrawInfo.fTempSurfaceDesc.fConfig, buffer,
337 rowBytes)) {
bsalomonf0674512015-07-28 13:26:15 -0700338 return false;
339 }
340 SkMatrix matrix;
341 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
robertphillips2e1e51f2015-10-15 08:01:48 -0700342 SkAutoTUnref<GrDrawContext> drawContext(this->drawContext(renderTarget));
bsalomonf0674512015-07-28 13:26:15 -0700343 if (!drawContext) {
344 return false;
345 }
egdanielc4b72722015-11-23 13:20:41 -0800346 GrPaint paint;
bsalomonac856c92015-08-27 06:30:17 -0700347 paint.addColorFragmentProcessor(fp);
egdanielc4b72722015-11-23 13:20:41 -0800348 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
bsalomonf0674512015-07-28 13:26:15 -0700349 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
robertphillips2e1e51f2015-10-15 08:01:48 -0700350 drawContext->drawRect(GrClip::WideOpen(), paint, matrix, rect, nullptr);
bsalomonf0674512015-07-28 13:26:15 -0700351
352 if (kFlushWrites_PixelOp & pixelOpsFlags) {
353 this->flushSurfaceWrites(surface);
354 }
355 }
356 }
357 if (!tempTexture) {
bsalomonf0674512015-07-28 13:26:15 -0700358 if (applyPremulToSrc) {
bsalomon81beccc2014-10-13 12:32:55 -0700359 size_t tmpRowBytes = 4 * width;
360 tmpPixels.reset(width * height);
361 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
362 tmpPixels.get())) {
363 return false;
364 }
365 rowBytes = tmpRowBytes;
366 buffer = tmpPixels.get();
bsalomonf0674512015-07-28 13:26:15 -0700367 applyPremulToSrc = false;
bsalomon81beccc2014-10-13 12:32:55 -0700368 }
bsalomon6cb3cbe2015-07-30 07:34:27 -0700369 return fGpu->writePixels(surface, left, top, width, height, srcConfig, buffer, rowBytes);
bsalomon81beccc2014-10-13 12:32:55 -0700370 }
bsalomon81beccc2014-10-13 12:32:55 -0700371 return true;
372}
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000373
bsalomone8d21e82015-07-16 08:23:13 -0700374bool GrContext::readSurfacePixels(GrSurface* src,
375 int left, int top, int width, int height,
376 GrPixelConfig dstConfig, void* buffer, size_t rowBytes,
377 uint32_t flags) {
joshualitt1de610a2016-01-06 08:26:09 -0800378 ASSERT_SINGLE_OWNER
joshualitt5f5a8d72015-02-25 14:09:45 -0800379 RETURN_FALSE_IF_ABANDONED
bsalomone8d21e82015-07-16 08:23:13 -0700380 ASSERT_OWNED_RESOURCE(src);
381 SkASSERT(src);
bsalomon32ab2602015-09-09 18:57:49 -0700382
bsalomon6c6f6582015-09-10 08:12:46 -0700383 this->testPMConversionsIfNecessary(flags);
384 SkAutoMutexAcquire ama(fReadPixelsMutex);
385
bsalomone8d21e82015-07-16 08:23:13 -0700386 // Adjust the params so that if we wind up using an intermediate surface we've already done
387 // all the trimming and the temporary can be the min size required.
388 if (!GrSurfacePriv::AdjustReadPixelParams(src->width(), src->height(),
389 GrBytesPerPixel(dstConfig), &left,
390 &top, &width, &height, &buffer, &rowBytes)) {
391 return false;
392 }
393
394 if (!(kDontFlush_PixelOpsFlag & flags) && src->surfacePriv().hasPendingWrite()) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000395 this->flush();
396 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000397
bsalomone8d21e82015-07-16 08:23:13 -0700398 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
bsalomon@google.com9c680582013-02-06 18:17:50 +0000399 if (unpremul && !GrPixelConfigIs8888(dstConfig)) {
bsalomon39826022015-07-23 08:07:21 -0700400 // The unpremul flag is only allowed for 8888 configs.
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000401 return false;
402 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000403
bsalomon636e8022015-07-29 06:08:46 -0700404 GrGpu::DrawPreference drawPreference = GrGpu::kNoDraw_DrawPreference;
405 // Don't prefer to draw for the conversion (and thereby access a texture from the cache) when
406 // we've already determined that there isn't a roundtrip preserving conversion processor pair.
407 if (unpremul && !this->didFailPMUPMConversionTest()) {
408 drawPreference = GrGpu::kCallerPrefersDraw_DrawPreference;
409 }
410
bsalomon39826022015-07-23 08:07:21 -0700411 GrGpu::ReadPixelTempDrawInfo tempDrawInfo;
412 if (!fGpu->getReadPixelsInfo(src, width, height, rowBytes, dstConfig, &drawPreference,
413 &tempDrawInfo)) {
414 return false;
415 }
bsalomon191bcc02014-11-14 11:31:13 -0800416
bsalomon6cb3cbe2015-07-30 07:34:27 -0700417 SkAutoTUnref<GrSurface> surfaceToRead(SkRef(src));
bsalomon39826022015-07-23 08:07:21 -0700418 bool didTempDraw = false;
419 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
bsalomon39826022015-07-23 08:07:21 -0700420 if (tempDrawInfo.fUseExactScratch) {
421 // We only respect this when the entire src is being read. Otherwise we can trigger too
422 // many odd ball texture sizes and trash the cache.
bsalomoneae62002015-07-31 13:59:30 -0700423 if (width != src->width() || height != src->height()) {
424 tempDrawInfo.fUseExactScratch = false;
bsalomon39826022015-07-23 08:07:21 -0700425 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000426 }
bsalomon39826022015-07-23 08:07:21 -0700427 SkAutoTUnref<GrTexture> temp;
bsalomoneae62002015-07-31 13:59:30 -0700428 if (tempDrawInfo.fUseExactScratch) {
429 temp.reset(this->textureProvider()->createTexture(tempDrawInfo.fTempSurfaceDesc, true));
430 } else {
431 temp.reset(this->textureProvider()->createApproxTexture(tempDrawInfo.fTempSurfaceDesc));
432 }
bsalomon39826022015-07-23 08:07:21 -0700433 if (temp) {
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000434 SkMatrix textureMatrix;
bsalomon39826022015-07-23 08:07:21 -0700435 textureMatrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000436 textureMatrix.postIDiv(src->width(), src->height());
joshualittb0a8a372014-09-23 09:50:21 -0700437 SkAutoTUnref<const GrFragmentProcessor> fp;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000438 if (unpremul) {
bsalomon4a339522015-10-06 08:40:50 -0700439 fp.reset(this->createPMToUPMEffect(src->asTexture(), tempDrawInfo.fSwapRAndB,
bsalomon39826022015-07-23 08:07:21 -0700440 textureMatrix));
joshualittb0a8a372014-09-23 09:50:21 -0700441 if (fp) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000442 unpremul = false; // we no longer need to do this on CPU after the read back.
bsalomon39826022015-07-23 08:07:21 -0700443 } else if (GrGpu::kCallerPrefersDraw_DrawPreference == drawPreference) {
444 // We only wanted to do the draw in order to perform the unpremul so don't
445 // bother.
halcanary96fcdcc2015-08-27 07:41:13 -0700446 temp.reset(nullptr);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000447 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000448 }
bsalomon39826022015-07-23 08:07:21 -0700449 if (!fp && temp) {
bsalomon4a339522015-10-06 08:40:50 -0700450 fp.reset(GrConfigConversionEffect::Create(src->asTexture(), tempDrawInfo.fSwapRAndB,
bsalomon39826022015-07-23 08:07:21 -0700451 GrConfigConversionEffect::kNone_PMConversion, textureMatrix));
452 }
453 if (fp) {
egdanielc4b72722015-11-23 13:20:41 -0800454 GrPaint paint;
bsalomonac856c92015-08-27 06:30:17 -0700455 paint.addColorFragmentProcessor(fp);
egdanielc4b72722015-11-23 13:20:41 -0800456 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
bsalomon39826022015-07-23 08:07:21 -0700457 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
robertphillips2e1e51f2015-10-15 08:01:48 -0700458 SkAutoTUnref<GrDrawContext> drawContext(this->drawContext(temp->asRenderTarget()));
459 drawContext->drawRect(GrClip::WideOpen(), paint, SkMatrix::I(), rect, nullptr);
bsalomon6cb3cbe2015-07-30 07:34:27 -0700460 surfaceToRead.reset(SkRef(temp.get()));
bsalomon39826022015-07-23 08:07:21 -0700461 left = 0;
462 top = 0;
463 didTempDraw = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000464 }
bsalomon@google.com0342a852012-08-20 19:22:38 +0000465 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000466 }
joshualitt5c55fef2014-10-31 14:04:35 -0700467
bsalomon39826022015-07-23 08:07:21 -0700468 if (GrGpu::kRequireDraw_DrawPreference == drawPreference && !didTempDraw) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000469 return false;
470 }
bsalomon39826022015-07-23 08:07:21 -0700471 GrPixelConfig configToRead = dstConfig;
472 if (didTempDraw) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700473 this->flushSurfaceWrites(surfaceToRead);
bsalomon39826022015-07-23 08:07:21 -0700474 // We swapped R and B while doing the temp draw. Swap back on the read.
475 if (tempDrawInfo.fSwapRAndB) {
476 configToRead = GrPixelConfigSwapRAndB(dstConfig);
477 }
478 }
bsalomon6cb3cbe2015-07-30 07:34:27 -0700479 if (!fGpu->readPixels(surfaceToRead, left, top, width, height, configToRead, buffer,
480 rowBytes)) {
bsalomon39826022015-07-23 08:07:21 -0700481 return false;
482 }
483
484 // Perform umpremul conversion if we weren't able to perform it as a draw.
485 if (unpremul) {
reed@google.com7111d462014-03-25 16:20:24 +0000486 SkDstPixelInfo dstPI;
halcanary96fcdcc2015-08-27 07:41:13 -0700487 if (!GrPixelConfig2ColorAndProfileType(dstConfig, &dstPI.fColorType, nullptr)) {
reed@google.com7111d462014-03-25 16:20:24 +0000488 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000489 }
reed@google.com7111d462014-03-25 16:20:24 +0000490 dstPI.fAlphaType = kUnpremul_SkAlphaType;
491 dstPI.fPixels = buffer;
492 dstPI.fRowBytes = rowBytes;
493
494 SkSrcPixelInfo srcPI;
bsalomon39826022015-07-23 08:07:21 -0700495 srcPI.fColorType = dstPI.fColorType;
reed@google.com7111d462014-03-25 16:20:24 +0000496 srcPI.fAlphaType = kPremul_SkAlphaType;
497 srcPI.fPixels = buffer;
498 srcPI.fRowBytes = rowBytes;
499
500 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000501 }
502 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000503}
504
bsalomonc49e8682015-06-30 11:37:35 -0700505void GrContext::prepareSurfaceForExternalIO(GrSurface* surface) {
joshualitt1de610a2016-01-06 08:26:09 -0800506 ASSERT_SINGLE_OWNER
joshualitt5f5a8d72015-02-25 14:09:45 -0800507 RETURN_IF_ABANDONED
bsalomon87a94eb2014-11-03 14:28:32 -0800508 SkASSERT(surface);
509 ASSERT_OWNED_RESOURCE(surface);
510 if (surface->surfacePriv().hasPendingIO()) {
511 this->flush();
512 }
513 GrRenderTarget* rt = surface->asRenderTarget();
514 if (fGpu && rt) {
515 fGpu->resolveRenderTarget(rt);
bsalomon41ebbdd2014-08-04 08:31:39 -0700516 }
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000517}
518
bsalomonf80bfed2014-10-07 05:56:02 -0700519void GrContext::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
520 const SkIPoint& dstPoint, uint32_t pixelOpsFlags) {
joshualitt1de610a2016-01-06 08:26:09 -0800521 ASSERT_SINGLE_OWNER
joshualitt5f5a8d72015-02-25 14:09:45 -0800522 RETURN_IF_ABANDONED
robertphillipsea461502015-05-26 11:38:03 -0700523 if (!src || !dst) {
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000524 return;
525 }
bsalomone3d4bf22014-09-23 09:15:03 -0700526 ASSERT_OWNED_RESOURCE(src);
junov2bb52102014-09-29 10:18:59 -0700527 ASSERT_OWNED_RESOURCE(dst);
Brian Salomon34a98952014-09-24 11:41:24 -0400528
bsalomonf80bfed2014-10-07 05:56:02 -0700529 // Since we're going to the draw target and not GPU, no need to check kNoFlush
530 // here.
robertphillipsea461502015-05-26 11:38:03 -0700531 if (!dst->asRenderTarget()) {
junov96c118e2014-09-26 13:09:47 -0700532 return;
533 }
robertphillipsea461502015-05-26 11:38:03 -0700534
robertphillips2e1e51f2015-10-15 08:01:48 -0700535 SkAutoTUnref<GrDrawContext> drawContext(this->drawContext(dst->asRenderTarget()));
robertphillipsea461502015-05-26 11:38:03 -0700536 if (!drawContext) {
537 return;
538 }
539
robertphillips2e1e51f2015-10-15 08:01:48 -0700540 drawContext->copySurface(src, srcRect, dstPoint);
bsalomonf80bfed2014-10-07 05:56:02 -0700541
542 if (kFlushWrites_PixelOp & pixelOpsFlags) {
543 this->flush();
544 }
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000545}
546
bsalomonf80bfed2014-10-07 05:56:02 -0700547void GrContext::flushSurfaceWrites(GrSurface* surface) {
joshualitt1de610a2016-01-06 08:26:09 -0800548 ASSERT_SINGLE_OWNER
joshualitt5f5a8d72015-02-25 14:09:45 -0800549 RETURN_IF_ABANDONED
bsalomonf80bfed2014-10-07 05:56:02 -0700550 if (surface->surfacePriv().hasPendingWrite()) {
551 this->flush();
552 }
553}
554
bsalomon@google.com27847de2011-02-22 20:59:41 +0000555////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000556int GrContext::getRecommendedSampleCount(GrPixelConfig config,
557 SkScalar dpi) const {
joshualitt1de610a2016-01-06 08:26:09 -0800558 ASSERT_SINGLE_OWNER
559
bsalomon76228632015-05-29 08:02:10 -0700560 if (!this->caps()->isConfigRenderable(config, true)) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000561 return 0;
562 }
563 int chosenSampleCount = 0;
jvanverthe9c0fc62015-04-29 11:18:05 -0700564 if (fGpu->caps()->shaderCaps()->pathRenderingSupport()) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000565 if (dpi >= 250.0f) {
566 chosenSampleCount = 4;
567 } else {
568 chosenSampleCount = 16;
569 }
570 }
571 return chosenSampleCount <= fGpu->caps()->maxSampleCount() ?
572 chosenSampleCount : 0;
573}
574
robertphillips77a2e522015-10-17 07:43:27 -0700575
576GrDrawContext* GrContext::drawContext(GrRenderTarget* rt, const SkSurfaceProps* surfaceProps) {
joshualitt1de610a2016-01-06 08:26:09 -0800577 ASSERT_SINGLE_OWNER
robertphillips77a2e522015-10-17 07:43:27 -0700578 return fDrawingManager->drawContext(rt, surfaceProps);
579}
580
joshualitt1de610a2016-01-06 08:26:09 -0800581bool GrContext::abandoned() const {
582 ASSERT_SINGLE_OWNER
robertphillips77a2e522015-10-17 07:43:27 -0700583 return fDrawingManager->abandoned();
584}
585
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000586namespace {
587void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
588 GrConfigConversionEffect::PMConversion pmToUPM;
589 GrConfigConversionEffect::PMConversion upmToPM;
590 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM);
591 *pmToUPMValue = pmToUPM;
592 *upmToPMValue = upmToPM;
593}
594}
595
bsalomon6c6f6582015-09-10 08:12:46 -0700596void GrContext::testPMConversionsIfNecessary(uint32_t flags) {
joshualitt1de610a2016-01-06 08:26:09 -0800597 ASSERT_SINGLE_OWNER
bsalomon6c6f6582015-09-10 08:12:46 -0700598 if (SkToBool(kUnpremul_PixelOpsFlag & flags)) {
599 SkAutoMutexAcquire ama(fTestPMConversionsMutex);
600 if (!fDidTestPMConversions) {
601 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
602 fDidTestPMConversions = true;
603 }
604 }
605}
606
bsalomon4a339522015-10-06 08:40:50 -0700607const GrFragmentProcessor* GrContext::createPMToUPMEffect(GrTexture* texture,
joshualittb0a8a372014-09-23 09:50:21 -0700608 bool swapRAndB,
bsalomon6c6f6582015-09-10 08:12:46 -0700609 const SkMatrix& matrix) const {
joshualitt1de610a2016-01-06 08:26:09 -0800610 ASSERT_SINGLE_OWNER
bsalomon6c6f6582015-09-10 08:12:46 -0700611 // We should have already called this->testPMConversionsIfNecessary().
612 SkASSERT(fDidTestPMConversions);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000613 GrConfigConversionEffect::PMConversion pmToUPM =
614 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
615 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
bsalomon4a339522015-10-06 08:40:50 -0700616 return GrConfigConversionEffect::Create(texture, swapRAndB, pmToUPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000617 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700618 return nullptr;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000619 }
620}
621
bsalomon4a339522015-10-06 08:40:50 -0700622const GrFragmentProcessor* GrContext::createUPMToPMEffect(GrTexture* texture,
joshualittb0a8a372014-09-23 09:50:21 -0700623 bool swapRAndB,
bsalomon6c6f6582015-09-10 08:12:46 -0700624 const SkMatrix& matrix) const {
joshualitt1de610a2016-01-06 08:26:09 -0800625 ASSERT_SINGLE_OWNER
bsalomon6c6f6582015-09-10 08:12:46 -0700626 // We should have already called this->testPMConversionsIfNecessary().
627 SkASSERT(fDidTestPMConversions);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000628 GrConfigConversionEffect::PMConversion upmToPM =
629 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion);
630 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) {
bsalomon4a339522015-10-06 08:40:50 -0700631 return GrConfigConversionEffect::Create(texture, swapRAndB, upmToPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000632 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700633 return nullptr;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000634 }
635}
636
bsalomon636e8022015-07-29 06:08:46 -0700637bool GrContext::didFailPMUPMConversionTest() const {
joshualitt1de610a2016-01-06 08:26:09 -0800638 ASSERT_SINGLE_OWNER
bsalomon6c6f6582015-09-10 08:12:46 -0700639 // We should have already called this->testPMConversionsIfNecessary().
640 SkASSERT(fDidTestPMConversions);
bsalomon636e8022015-07-29 06:08:46 -0700641 // The PM<->UPM tests fail or succeed together so we only need to check one.
bsalomon6c6f6582015-09-10 08:12:46 -0700642 return GrConfigConversionEffect::kNone_PMConversion == fPMToUPMConversion;
bsalomon636e8022015-07-29 06:08:46 -0700643}
644
bsalomon37f9a262015-02-02 13:00:10 -0800645//////////////////////////////////////////////////////////////////////////////
646
647void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800648 ASSERT_SINGLE_OWNER
bsalomon37f9a262015-02-02 13:00:10 -0800649 if (maxTextures) {
bsalomon0ea80f42015-02-11 10:49:59 -0800650 *maxTextures = fResourceCache->getMaxResourceCount();
bsalomon37f9a262015-02-02 13:00:10 -0800651 }
652 if (maxTextureBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800653 *maxTextureBytes = fResourceCache->getMaxResourceBytes();
bsalomon37f9a262015-02-02 13:00:10 -0800654 }
655}
656
657void GrContext::setResourceCacheLimits(int maxTextures, size_t maxTextureBytes) {
joshualitt1de610a2016-01-06 08:26:09 -0800658 ASSERT_SINGLE_OWNER
bsalomon0ea80f42015-02-11 10:49:59 -0800659 fResourceCache->setLimits(maxTextures, maxTextureBytes);
bsalomon37f9a262015-02-02 13:00:10 -0800660}
661
ericrk0a5fa482015-09-15 14:16:10 -0700662//////////////////////////////////////////////////////////////////////////////
663
664void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -0800665 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -0700666 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
667}