blob: 2b20e17ae16369155ba60ab6045f689db5f44341 [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)
robertphillips77a2e522015-10-17 07:43:27 -070026#define RETURN_IF_ABANDONED if (fDrawingManager->abandoned()) { return; }
27#define RETURN_FALSE_IF_ABANDONED if (fDrawingManager->abandoned()) { return false; }
28#define RETURN_NULL_IF_ABANDONED if (fDrawingManager->abandoned()) { return nullptr; }
bsalomon@google.combc4b6542011-11-19 13:56:11 +000029
robertphillipsea461502015-05-26 11:38:03 -070030////////////////////////////////////////////////////////////////////////////////
31
bsalomon682c2692015-05-22 14:01:46 -070032GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext) {
33 GrContextOptions defaultOptions;
34 return Create(backend, backendContext, defaultOptions);
35}
bsalomonf28cff72015-05-22 12:25:41 -070036
bsalomon682c2692015-05-22 14:01:46 -070037GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext,
38 const GrContextOptions& options) {
halcanary385fe4d2015-08-26 13:07:48 -070039 GrContext* context = new GrContext;
bsalomon682c2692015-05-22 14:01:46 -070040
41 if (context->init(backend, backendContext, options)) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000042 return context;
43 } else {
44 context->unref();
halcanary96fcdcc2015-08-27 07:41:13 -070045 return nullptr;
bsalomon@google.com27847de2011-02-22 20:59:41 +000046 }
bsalomon@google.com27847de2011-02-22 20:59:41 +000047}
48
joshualitt0acd0d32015-05-07 08:23:19 -070049static int32_t gNextID = 1;
50static int32_t next_id() {
51 int32_t id;
52 do {
53 id = sk_atomic_inc(&gNextID);
54 } while (id == SK_InvalidGenID);
55 return id;
56}
57
bsalomon682c2692015-05-22 14:01:46 -070058GrContext::GrContext() : fUniqueID(next_id()) {
halcanary96fcdcc2015-08-27 07:41:13 -070059 fGpu = nullptr;
60 fCaps = nullptr;
61 fResourceCache = nullptr;
62 fResourceProvider = nullptr;
halcanary96fcdcc2015-08-27 07:41:13 -070063 fBatchFontCache = nullptr;
bsalomonf21dab92014-11-13 13:33:28 -080064 fFlushToReduceCacheSize = false;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000065}
66
bsalomon682c2692015-05-22 14:01:46 -070067bool GrContext::init(GrBackend backend, GrBackendContext backendContext,
68 const GrContextOptions& options) {
robertphillipsea461502015-05-26 11:38:03 -070069 SkASSERT(!fGpu);
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000070
bsalomon682c2692015-05-22 14:01:46 -070071 fGpu = GrGpu::Create(backend, backendContext, options, this);
robertphillipsea461502015-05-26 11:38:03 -070072 if (!fGpu) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000073 return false;
74 }
bsalomon69cfe952015-11-30 13:27:47 -080075 this->initCommon(options);
bsalomon33435572014-11-05 14:47:41 -080076 return true;
77}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000078
bsalomon69cfe952015-11-30 13:27:47 -080079void GrContext::initCommon(const GrContextOptions& options) {
bsalomon76228632015-05-29 08:02:10 -070080 fCaps = SkRef(fGpu->caps());
halcanary385fe4d2015-08-26 13:07:48 -070081 fResourceCache = new GrResourceCache(fCaps);
bsalomon0ea80f42015-02-11 10:49:59 -080082 fResourceCache->setOverBudgetCallback(OverBudgetCB, this);
halcanary385fe4d2015-08-26 13:07:48 -070083 fResourceProvider = new GrResourceProvider(fGpu, fResourceCache);
commit-bot@chromium.org1836d332013-07-16 22:55:03 +000084
halcanary385fe4d2015-08-26 13:07:48 -070085 fLayerCache.reset(new GrLayerCache(this));
robertphillips@google.come930a072014-04-03 00:34:27 +000086
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000087 fDidTestPMConversions = false;
88
bsalomon69cfe952015-11-30 13:27:47 -080089 GrDrawTarget::Options dtOptions;
90 dtOptions.fClipBatchToBounds = options.fClipBatchToBounds;
bsalomon6dea83f2015-12-03 12:58:06 -080091 dtOptions.fDrawBatchBounds = options.fDrawBatchBounds;
bsalomon489147c2015-12-14 12:13:09 -080092 dtOptions.fMaxBatchLookback = options.fMaxBatchLookback;
bsalomon69cfe952015-11-30 13:27:47 -080093 fDrawingManager.reset(new GrDrawingManager(this, dtOptions));
joshualitt7c3a2f82015-03-31 13:32:05 -070094
95 // GrBatchFontCache will eventually replace GrFontCache
halcanary385fe4d2015-08-26 13:07:48 -070096 fBatchFontCache = new GrBatchFontCache(this);
joshualittb7133be2015-04-08 09:08:31 -070097
halcanary385fe4d2015-08-26 13:07:48 -070098 fTextBlobCache.reset(new GrTextBlobCache(TextBlobCacheOverBudgetCB, this));
bsalomon@google.comc0af3172012-06-15 14:10:09 +000099}
100
bsalomon@google.com27847de2011-02-22 20:59:41 +0000101GrContext::~GrContext() {
robertphillipsea461502015-05-26 11:38:03 -0700102 if (!fGpu) {
bsalomon76228632015-05-29 08:02:10 -0700103 SkASSERT(!fCaps);
bsalomon@google.com733c0622013-04-24 17:59:32 +0000104 return;
105 }
106
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000107 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000108
robertphillips77a2e522015-10-17 07:43:27 -0700109 fDrawingManager->cleanup();
robertphillips2334fb62015-06-17 05:43:33 -0700110
robertphillips@google.com950b1b02013-10-21 17:37:28 +0000111 for (int i = 0; i < fCleanUpData.count(); ++i) {
112 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo);
113 }
114
halcanary385fe4d2015-08-26 13:07:48 -0700115 delete fResourceProvider;
116 delete fResourceCache;
117 delete fBatchFontCache;
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000118
bsalomon@google.com205d4602011-04-25 12:43:45 +0000119 fGpu->unref();
bsalomon76228632015-05-29 08:02:10 -0700120 fCaps->unref();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000121}
122
bsalomon2354f842014-07-28 13:48:36 -0700123void GrContext::abandonContext() {
bsalomond309e7a2015-04-30 14:18:54 -0700124 fResourceProvider->abandon();
robertphillips0dfa62c2015-11-16 06:23:31 -0800125
126 // Need to abandon the drawing manager first so all the render targets
127 // will be released/forgotten before they too are abandoned.
128 fDrawingManager->abandon();
129
bsalomon@google.com205d4602011-04-25 12:43:45 +0000130 // abandon first to so destructors
131 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800132 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700133
robertphillipse3371302014-09-17 06:01:06 -0700134 fGpu->contextAbandoned();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000135
joshualitt7c3a2f82015-03-31 13:32:05 -0700136 fBatchFontCache->freeAll();
robertphillips@google.come930a072014-04-03 00:34:27 +0000137 fLayerCache->freeAll();
joshualitt26ffc002015-04-16 11:24:04 -0700138 fTextBlobCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000139}
140
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000141void GrContext::resetContext(uint32_t state) {
142 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000143}
144
145void GrContext::freeGpuResources() {
146 this->flush();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000147
joshualitt7c3a2f82015-03-31 13:32:05 -0700148 fBatchFontCache->freeAll();
robertphillips@google.come930a072014-04-03 00:34:27 +0000149 fLayerCache->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700150
151 fDrawingManager->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700152
153 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000154}
155
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000156void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
bsalomon71cb0c22014-11-14 12:10:14 -0800157 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800158 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800159 }
160 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800161 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800162 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000163}
164
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000165////////////////////////////////////////////////////////////////////////////////
166
bsalomon71cb0c22014-11-14 12:10:14 -0800167void GrContext::OverBudgetCB(void* data) {
bsalomon66a450f2014-11-13 13:19:10 -0800168 SkASSERT(data);
bsalomonf21dab92014-11-13 13:33:28 -0800169
bsalomon66a450f2014-11-13 13:19:10 -0800170 GrContext* context = reinterpret_cast<GrContext*>(data);
bsalomonf21dab92014-11-13 13:33:28 -0800171
joshualittb542bae2015-07-28 09:58:39 -0700172 // Flush the GrBufferedDrawTarget to possibly free up some textures
bsalomonf21dab92014-11-13 13:33:28 -0800173 context->fFlushToReduceCacheSize = true;
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000174}
175
joshualitt0db6dfa2015-04-10 07:01:30 -0700176void GrContext::TextBlobCacheOverBudgetCB(void* data) {
177 SkASSERT(data);
178
179 // Unlike the GrResourceCache, TextBlobs are drawn at the SkGpuDevice level, therefore they
180 // cannot use fFlushTorReduceCacheSize because it uses AutoCheckFlush. The solution is to move
181 // drawText calls to below the GrContext level, but this is not trivial because they call
182 // drawPath on SkGpuDevice
183 GrContext* context = reinterpret_cast<GrContext*>(data);
184 context->flush();
185}
186
bsalomon@google.com27847de2011-02-22 20:59:41 +0000187////////////////////////////////////////////////////////////////////////////////
188
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000189void GrContext::flush(int flagsBitfield) {
robertphillipsea461502015-05-26 11:38:03 -0700190 RETURN_IF_ABANDONED
robertphillips@google.come7db8d62013-07-04 11:48:52 +0000191
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000192 if (kDiscard_FlushBit & flagsBitfield) {
robertphillips77a2e522015-10-17 07:43:27 -0700193 fDrawingManager->reset();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000194 } else {
robertphillips77a2e522015-10-17 07:43:27 -0700195 fDrawingManager->flush();
junov@google.com53a55842011-06-08 22:55:10 +0000196 }
bsalomon3f324322015-04-08 11:01:54 -0700197 fResourceCache->notifyFlushOccurred();
bsalomonf21dab92014-11-13 13:33:28 -0800198 fFlushToReduceCacheSize = false;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000199}
200
bsalomon81beccc2014-10-13 12:32:55 -0700201bool sw_convert_to_premul(GrPixelConfig srcConfig, int width, int height, size_t inRowBytes,
202 const void* inPixels, size_t outRowBytes, void* outPixels) {
203 SkSrcPixelInfo srcPI;
halcanary96fcdcc2015-08-27 07:41:13 -0700204 if (!GrPixelConfig2ColorAndProfileType(srcConfig, &srcPI.fColorType, nullptr)) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000205 return false;
206 }
bsalomon81beccc2014-10-13 12:32:55 -0700207 srcPI.fAlphaType = kUnpremul_SkAlphaType;
208 srcPI.fPixels = inPixels;
209 srcPI.fRowBytes = inRowBytes;
210
211 SkDstPixelInfo dstPI;
212 dstPI.fColorType = srcPI.fColorType;
213 dstPI.fAlphaType = kPremul_SkAlphaType;
214 dstPI.fPixels = outPixels;
215 dstPI.fRowBytes = outRowBytes;
216
217 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000218}
219
bsalomon81beccc2014-10-13 12:32:55 -0700220bool GrContext::writeSurfacePixels(GrSurface* surface,
221 int left, int top, int width, int height,
222 GrPixelConfig srcConfig, const void* buffer, size_t rowBytes,
223 uint32_t pixelOpsFlags) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800224 RETURN_FALSE_IF_ABANDONED
bsalomon6c6f6582015-09-10 08:12:46 -0700225 ASSERT_OWNED_RESOURCE(surface);
226 SkASSERT(surface);
227
228 this->testPMConversionsIfNecessary(pixelOpsFlags);
bsalomon81beccc2014-10-13 12:32:55 -0700229
bsalomone8d21e82015-07-16 08:23:13 -0700230 // 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 -0700231 // necessary and because GrGpu::getWritePixelsInfo requires it.
bsalomone8d21e82015-07-16 08:23:13 -0700232 if (!GrSurfacePriv::AdjustWritePixelParams(surface->width(), surface->height(),
233 GrBytesPerPixel(srcConfig), &left, &top, &width,
234 &height, &buffer, &rowBytes)) {
235 return false;
236 }
237
bsalomonf0674512015-07-28 13:26:15 -0700238 bool applyPremulToSrc = false;
bsalomon81beccc2014-10-13 12:32:55 -0700239 if (kUnpremul_PixelOpsFlag & pixelOpsFlags) {
240 if (!GrPixelConfigIs8888(srcConfig)) {
241 return false;
242 }
bsalomonf0674512015-07-28 13:26:15 -0700243 applyPremulToSrc = true;
244 }
bsalomon636e8022015-07-29 06:08:46 -0700245
246 GrGpu::DrawPreference drawPreference = GrGpu::kNoDraw_DrawPreference;
247 // Don't prefer to draw for the conversion (and thereby access a texture from the cache) when
248 // we've already determined that there isn't a roundtrip preserving conversion processor pair.
249 if (applyPremulToSrc && !this->didFailPMUPMConversionTest()) {
250 drawPreference = GrGpu::kCallerPrefersDraw_DrawPreference;
251 }
252
bsalomonf0674512015-07-28 13:26:15 -0700253 GrGpu::WritePixelTempDrawInfo tempDrawInfo;
254 if (!fGpu->getWritePixelsInfo(surface, width, height, rowBytes, srcConfig, &drawPreference,
255 &tempDrawInfo)) {
256 return false;
257 }
258
259 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && surface->surfacePriv().hasPendingIO()) {
260 this->flush();
261 }
262
263 SkAutoTUnref<GrTexture> tempTexture;
264 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
bsalomoneae62002015-07-31 13:59:30 -0700265 tempTexture.reset(
266 this->textureProvider()->createApproxTexture(tempDrawInfo.fTempSurfaceDesc));
bsalomonf0674512015-07-28 13:26:15 -0700267 if (!tempTexture && GrGpu::kRequireDraw_DrawPreference == drawPreference) {
268 return false;
269 }
270 }
271
272 // temp buffer for doing sw premul conversion, if needed.
benjaminwagner7d974f52015-10-19 13:55:55 -0700273#if defined(GOOGLE3)
274 // Stack frame size is limited in GOOGLE3.
275 SkAutoSTMalloc<48 * 48, uint32_t> tmpPixels(0);
276#else
bsalomonf0674512015-07-28 13:26:15 -0700277 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
benjaminwagner7d974f52015-10-19 13:55:55 -0700278#endif
bsalomonf0674512015-07-28 13:26:15 -0700279 if (tempTexture) {
280 SkAutoTUnref<const GrFragmentProcessor> fp;
281 SkMatrix textureMatrix;
282 textureMatrix.setIDiv(tempTexture->width(), tempTexture->height());
bsalomonf0674512015-07-28 13:26:15 -0700283 if (applyPremulToSrc) {
bsalomon4a339522015-10-06 08:40:50 -0700284 fp.reset(this->createUPMToPMEffect(tempTexture, tempDrawInfo.fSwapRAndB,
285 textureMatrix));
bsalomonf0674512015-07-28 13:26:15 -0700286 // If premultiplying was the only reason for the draw, fall back to a straight write.
287 if (!fp) {
288 if (GrGpu::kCallerPrefersDraw_DrawPreference == drawPreference) {
halcanary96fcdcc2015-08-27 07:41:13 -0700289 tempTexture.reset(nullptr);
bsalomonf0674512015-07-28 13:26:15 -0700290 }
291 } else {
292 applyPremulToSrc = false;
293 }
294 }
295 if (tempTexture) {
296 if (!fp) {
bsalomon4a339522015-10-06 08:40:50 -0700297 fp.reset(GrConfigConversionEffect::Create(tempTexture, tempDrawInfo.fSwapRAndB,
bsalomonf0674512015-07-28 13:26:15 -0700298 GrConfigConversionEffect::kNone_PMConversion, textureMatrix));
299 if (!fp) {
300 return false;
301 }
302 }
303 GrRenderTarget* renderTarget = surface->asRenderTarget();
304 SkASSERT(renderTarget);
305 if (tempTexture->surfacePriv().hasPendingIO()) {
306 this->flush();
307 }
308 if (applyPremulToSrc) {
309 size_t tmpRowBytes = 4 * width;
310 tmpPixels.reset(width * height);
311 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
312 tmpPixels.get())) {
313 return false;
314 }
315 rowBytes = tmpRowBytes;
316 buffer = tmpPixels.get();
317 applyPremulToSrc = false;
318 }
bsalomon6cb3cbe2015-07-30 07:34:27 -0700319 if (!fGpu->writePixels(tempTexture, 0, 0, width, height,
320 tempDrawInfo.fTempSurfaceDesc.fConfig, buffer,
321 rowBytes)) {
bsalomonf0674512015-07-28 13:26:15 -0700322 return false;
323 }
324 SkMatrix matrix;
325 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
robertphillips2e1e51f2015-10-15 08:01:48 -0700326 SkAutoTUnref<GrDrawContext> drawContext(this->drawContext(renderTarget));
bsalomonf0674512015-07-28 13:26:15 -0700327 if (!drawContext) {
328 return false;
329 }
egdanielc4b72722015-11-23 13:20:41 -0800330 GrPaint paint;
bsalomonac856c92015-08-27 06:30:17 -0700331 paint.addColorFragmentProcessor(fp);
egdanielc4b72722015-11-23 13:20:41 -0800332 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
bsalomonf0674512015-07-28 13:26:15 -0700333 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
robertphillips2e1e51f2015-10-15 08:01:48 -0700334 drawContext->drawRect(GrClip::WideOpen(), paint, matrix, rect, nullptr);
bsalomonf0674512015-07-28 13:26:15 -0700335
336 if (kFlushWrites_PixelOp & pixelOpsFlags) {
337 this->flushSurfaceWrites(surface);
338 }
339 }
340 }
341 if (!tempTexture) {
bsalomonf0674512015-07-28 13:26:15 -0700342 if (applyPremulToSrc) {
bsalomon81beccc2014-10-13 12:32:55 -0700343 size_t tmpRowBytes = 4 * width;
344 tmpPixels.reset(width * height);
345 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
346 tmpPixels.get())) {
347 return false;
348 }
349 rowBytes = tmpRowBytes;
350 buffer = tmpPixels.get();
bsalomonf0674512015-07-28 13:26:15 -0700351 applyPremulToSrc = false;
bsalomon81beccc2014-10-13 12:32:55 -0700352 }
bsalomon6cb3cbe2015-07-30 07:34:27 -0700353 return fGpu->writePixels(surface, left, top, width, height, srcConfig, buffer, rowBytes);
bsalomon81beccc2014-10-13 12:32:55 -0700354 }
bsalomon81beccc2014-10-13 12:32:55 -0700355 return true;
356}
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000357
bsalomone8d21e82015-07-16 08:23:13 -0700358bool GrContext::readSurfacePixels(GrSurface* src,
359 int left, int top, int width, int height,
360 GrPixelConfig dstConfig, void* buffer, size_t rowBytes,
361 uint32_t flags) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800362 RETURN_FALSE_IF_ABANDONED
bsalomone8d21e82015-07-16 08:23:13 -0700363 ASSERT_OWNED_RESOURCE(src);
364 SkASSERT(src);
bsalomon32ab2602015-09-09 18:57:49 -0700365
bsalomon6c6f6582015-09-10 08:12:46 -0700366 this->testPMConversionsIfNecessary(flags);
367 SkAutoMutexAcquire ama(fReadPixelsMutex);
368
bsalomone8d21e82015-07-16 08:23:13 -0700369 // Adjust the params so that if we wind up using an intermediate surface we've already done
370 // all the trimming and the temporary can be the min size required.
371 if (!GrSurfacePriv::AdjustReadPixelParams(src->width(), src->height(),
372 GrBytesPerPixel(dstConfig), &left,
373 &top, &width, &height, &buffer, &rowBytes)) {
374 return false;
375 }
376
377 if (!(kDontFlush_PixelOpsFlag & flags) && src->surfacePriv().hasPendingWrite()) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000378 this->flush();
379 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000380
bsalomone8d21e82015-07-16 08:23:13 -0700381 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
bsalomon@google.com9c680582013-02-06 18:17:50 +0000382 if (unpremul && !GrPixelConfigIs8888(dstConfig)) {
bsalomon39826022015-07-23 08:07:21 -0700383 // The unpremul flag is only allowed for 8888 configs.
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000384 return false;
385 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000386
bsalomon636e8022015-07-29 06:08:46 -0700387 GrGpu::DrawPreference drawPreference = GrGpu::kNoDraw_DrawPreference;
388 // Don't prefer to draw for the conversion (and thereby access a texture from the cache) when
389 // we've already determined that there isn't a roundtrip preserving conversion processor pair.
390 if (unpremul && !this->didFailPMUPMConversionTest()) {
391 drawPreference = GrGpu::kCallerPrefersDraw_DrawPreference;
392 }
393
bsalomon39826022015-07-23 08:07:21 -0700394 GrGpu::ReadPixelTempDrawInfo tempDrawInfo;
395 if (!fGpu->getReadPixelsInfo(src, width, height, rowBytes, dstConfig, &drawPreference,
396 &tempDrawInfo)) {
397 return false;
398 }
bsalomon191bcc02014-11-14 11:31:13 -0800399
bsalomon6cb3cbe2015-07-30 07:34:27 -0700400 SkAutoTUnref<GrSurface> surfaceToRead(SkRef(src));
bsalomon39826022015-07-23 08:07:21 -0700401 bool didTempDraw = false;
402 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
bsalomon39826022015-07-23 08:07:21 -0700403 if (tempDrawInfo.fUseExactScratch) {
404 // We only respect this when the entire src is being read. Otherwise we can trigger too
405 // many odd ball texture sizes and trash the cache.
bsalomoneae62002015-07-31 13:59:30 -0700406 if (width != src->width() || height != src->height()) {
407 tempDrawInfo.fUseExactScratch = false;
bsalomon39826022015-07-23 08:07:21 -0700408 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000409 }
bsalomon39826022015-07-23 08:07:21 -0700410 SkAutoTUnref<GrTexture> temp;
bsalomoneae62002015-07-31 13:59:30 -0700411 if (tempDrawInfo.fUseExactScratch) {
412 temp.reset(this->textureProvider()->createTexture(tempDrawInfo.fTempSurfaceDesc, true));
413 } else {
414 temp.reset(this->textureProvider()->createApproxTexture(tempDrawInfo.fTempSurfaceDesc));
415 }
bsalomon39826022015-07-23 08:07:21 -0700416 if (temp) {
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000417 SkMatrix textureMatrix;
bsalomon39826022015-07-23 08:07:21 -0700418 textureMatrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000419 textureMatrix.postIDiv(src->width(), src->height());
joshualittb0a8a372014-09-23 09:50:21 -0700420 SkAutoTUnref<const GrFragmentProcessor> fp;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000421 if (unpremul) {
bsalomon4a339522015-10-06 08:40:50 -0700422 fp.reset(this->createPMToUPMEffect(src->asTexture(), tempDrawInfo.fSwapRAndB,
bsalomon39826022015-07-23 08:07:21 -0700423 textureMatrix));
joshualittb0a8a372014-09-23 09:50:21 -0700424 if (fp) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000425 unpremul = false; // we no longer need to do this on CPU after the read back.
bsalomon39826022015-07-23 08:07:21 -0700426 } else if (GrGpu::kCallerPrefersDraw_DrawPreference == drawPreference) {
427 // We only wanted to do the draw in order to perform the unpremul so don't
428 // bother.
halcanary96fcdcc2015-08-27 07:41:13 -0700429 temp.reset(nullptr);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000430 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000431 }
bsalomon39826022015-07-23 08:07:21 -0700432 if (!fp && temp) {
bsalomon4a339522015-10-06 08:40:50 -0700433 fp.reset(GrConfigConversionEffect::Create(src->asTexture(), tempDrawInfo.fSwapRAndB,
bsalomon39826022015-07-23 08:07:21 -0700434 GrConfigConversionEffect::kNone_PMConversion, textureMatrix));
435 }
436 if (fp) {
egdanielc4b72722015-11-23 13:20:41 -0800437 GrPaint paint;
bsalomonac856c92015-08-27 06:30:17 -0700438 paint.addColorFragmentProcessor(fp);
egdanielc4b72722015-11-23 13:20:41 -0800439 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
bsalomon39826022015-07-23 08:07:21 -0700440 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
robertphillips2e1e51f2015-10-15 08:01:48 -0700441 SkAutoTUnref<GrDrawContext> drawContext(this->drawContext(temp->asRenderTarget()));
442 drawContext->drawRect(GrClip::WideOpen(), paint, SkMatrix::I(), rect, nullptr);
bsalomon6cb3cbe2015-07-30 07:34:27 -0700443 surfaceToRead.reset(SkRef(temp.get()));
bsalomon39826022015-07-23 08:07:21 -0700444 left = 0;
445 top = 0;
446 didTempDraw = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000447 }
bsalomon@google.com0342a852012-08-20 19:22:38 +0000448 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000449 }
joshualitt5c55fef2014-10-31 14:04:35 -0700450
bsalomon39826022015-07-23 08:07:21 -0700451 if (GrGpu::kRequireDraw_DrawPreference == drawPreference && !didTempDraw) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000452 return false;
453 }
bsalomon39826022015-07-23 08:07:21 -0700454 GrPixelConfig configToRead = dstConfig;
455 if (didTempDraw) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700456 this->flushSurfaceWrites(surfaceToRead);
bsalomon39826022015-07-23 08:07:21 -0700457 // We swapped R and B while doing the temp draw. Swap back on the read.
458 if (tempDrawInfo.fSwapRAndB) {
459 configToRead = GrPixelConfigSwapRAndB(dstConfig);
460 }
461 }
bsalomon6cb3cbe2015-07-30 07:34:27 -0700462 if (!fGpu->readPixels(surfaceToRead, left, top, width, height, configToRead, buffer,
463 rowBytes)) {
bsalomon39826022015-07-23 08:07:21 -0700464 return false;
465 }
466
467 // Perform umpremul conversion if we weren't able to perform it as a draw.
468 if (unpremul) {
reed@google.com7111d462014-03-25 16:20:24 +0000469 SkDstPixelInfo dstPI;
halcanary96fcdcc2015-08-27 07:41:13 -0700470 if (!GrPixelConfig2ColorAndProfileType(dstConfig, &dstPI.fColorType, nullptr)) {
reed@google.com7111d462014-03-25 16:20:24 +0000471 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000472 }
reed@google.com7111d462014-03-25 16:20:24 +0000473 dstPI.fAlphaType = kUnpremul_SkAlphaType;
474 dstPI.fPixels = buffer;
475 dstPI.fRowBytes = rowBytes;
476
477 SkSrcPixelInfo srcPI;
bsalomon39826022015-07-23 08:07:21 -0700478 srcPI.fColorType = dstPI.fColorType;
reed@google.com7111d462014-03-25 16:20:24 +0000479 srcPI.fAlphaType = kPremul_SkAlphaType;
480 srcPI.fPixels = buffer;
481 srcPI.fRowBytes = rowBytes;
482
483 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000484 }
485 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000486}
487
bsalomonc49e8682015-06-30 11:37:35 -0700488void GrContext::prepareSurfaceForExternalIO(GrSurface* surface) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800489 RETURN_IF_ABANDONED
bsalomon87a94eb2014-11-03 14:28:32 -0800490 SkASSERT(surface);
491 ASSERT_OWNED_RESOURCE(surface);
492 if (surface->surfacePriv().hasPendingIO()) {
493 this->flush();
494 }
495 GrRenderTarget* rt = surface->asRenderTarget();
496 if (fGpu && rt) {
497 fGpu->resolveRenderTarget(rt);
bsalomon41ebbdd2014-08-04 08:31:39 -0700498 }
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000499}
500
bsalomonf80bfed2014-10-07 05:56:02 -0700501void GrContext::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
502 const SkIPoint& dstPoint, uint32_t pixelOpsFlags) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800503 RETURN_IF_ABANDONED
robertphillipsea461502015-05-26 11:38:03 -0700504 if (!src || !dst) {
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000505 return;
506 }
bsalomone3d4bf22014-09-23 09:15:03 -0700507 ASSERT_OWNED_RESOURCE(src);
junov2bb52102014-09-29 10:18:59 -0700508 ASSERT_OWNED_RESOURCE(dst);
Brian Salomon34a98952014-09-24 11:41:24 -0400509
bsalomonf80bfed2014-10-07 05:56:02 -0700510 // Since we're going to the draw target and not GPU, no need to check kNoFlush
511 // here.
robertphillipsea461502015-05-26 11:38:03 -0700512 if (!dst->asRenderTarget()) {
junov96c118e2014-09-26 13:09:47 -0700513 return;
514 }
robertphillipsea461502015-05-26 11:38:03 -0700515
robertphillips2e1e51f2015-10-15 08:01:48 -0700516 SkAutoTUnref<GrDrawContext> drawContext(this->drawContext(dst->asRenderTarget()));
robertphillipsea461502015-05-26 11:38:03 -0700517 if (!drawContext) {
518 return;
519 }
520
robertphillips2e1e51f2015-10-15 08:01:48 -0700521 drawContext->copySurface(src, srcRect, dstPoint);
bsalomonf80bfed2014-10-07 05:56:02 -0700522
523 if (kFlushWrites_PixelOp & pixelOpsFlags) {
524 this->flush();
525 }
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000526}
527
bsalomonf80bfed2014-10-07 05:56:02 -0700528void GrContext::flushSurfaceWrites(GrSurface* surface) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800529 RETURN_IF_ABANDONED
bsalomonf80bfed2014-10-07 05:56:02 -0700530 if (surface->surfacePriv().hasPendingWrite()) {
531 this->flush();
532 }
533}
534
bsalomon@google.com27847de2011-02-22 20:59:41 +0000535////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000536int GrContext::getRecommendedSampleCount(GrPixelConfig config,
537 SkScalar dpi) const {
bsalomon76228632015-05-29 08:02:10 -0700538 if (!this->caps()->isConfigRenderable(config, true)) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000539 return 0;
540 }
541 int chosenSampleCount = 0;
jvanverthe9c0fc62015-04-29 11:18:05 -0700542 if (fGpu->caps()->shaderCaps()->pathRenderingSupport()) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000543 if (dpi >= 250.0f) {
544 chosenSampleCount = 4;
545 } else {
546 chosenSampleCount = 16;
547 }
548 }
549 return chosenSampleCount <= fGpu->caps()->maxSampleCount() ?
550 chosenSampleCount : 0;
551}
552
robertphillips77a2e522015-10-17 07:43:27 -0700553
554GrDrawContext* GrContext::drawContext(GrRenderTarget* rt, const SkSurfaceProps* surfaceProps) {
555 return fDrawingManager->drawContext(rt, surfaceProps);
556}
557
558bool GrContext::abandoned() const {
559 return fDrawingManager->abandoned();
560}
561
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000562namespace {
563void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
564 GrConfigConversionEffect::PMConversion pmToUPM;
565 GrConfigConversionEffect::PMConversion upmToPM;
566 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM);
567 *pmToUPMValue = pmToUPM;
568 *upmToPMValue = upmToPM;
569}
570}
571
bsalomon6c6f6582015-09-10 08:12:46 -0700572void GrContext::testPMConversionsIfNecessary(uint32_t flags) {
573 if (SkToBool(kUnpremul_PixelOpsFlag & flags)) {
574 SkAutoMutexAcquire ama(fTestPMConversionsMutex);
575 if (!fDidTestPMConversions) {
576 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
577 fDidTestPMConversions = true;
578 }
579 }
580}
581
bsalomon4a339522015-10-06 08:40:50 -0700582const GrFragmentProcessor* GrContext::createPMToUPMEffect(GrTexture* texture,
joshualittb0a8a372014-09-23 09:50:21 -0700583 bool swapRAndB,
bsalomon6c6f6582015-09-10 08:12:46 -0700584 const SkMatrix& matrix) const {
585 // We should have already called this->testPMConversionsIfNecessary().
586 SkASSERT(fDidTestPMConversions);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000587 GrConfigConversionEffect::PMConversion pmToUPM =
588 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
589 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
bsalomon4a339522015-10-06 08:40:50 -0700590 return GrConfigConversionEffect::Create(texture, swapRAndB, pmToUPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000591 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700592 return nullptr;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000593 }
594}
595
bsalomon4a339522015-10-06 08:40:50 -0700596const GrFragmentProcessor* GrContext::createUPMToPMEffect(GrTexture* texture,
joshualittb0a8a372014-09-23 09:50:21 -0700597 bool swapRAndB,
bsalomon6c6f6582015-09-10 08:12:46 -0700598 const SkMatrix& matrix) const {
599 // We should have already called this->testPMConversionsIfNecessary().
600 SkASSERT(fDidTestPMConversions);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000601 GrConfigConversionEffect::PMConversion upmToPM =
602 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion);
603 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) {
bsalomon4a339522015-10-06 08:40:50 -0700604 return GrConfigConversionEffect::Create(texture, swapRAndB, upmToPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000605 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700606 return nullptr;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000607 }
608}
609
bsalomon636e8022015-07-29 06:08:46 -0700610bool GrContext::didFailPMUPMConversionTest() const {
bsalomon6c6f6582015-09-10 08:12:46 -0700611 // We should have already called this->testPMConversionsIfNecessary().
612 SkASSERT(fDidTestPMConversions);
bsalomon636e8022015-07-29 06:08:46 -0700613 // The PM<->UPM tests fail or succeed together so we only need to check one.
bsalomon6c6f6582015-09-10 08:12:46 -0700614 return GrConfigConversionEffect::kNone_PMConversion == fPMToUPMConversion;
bsalomon636e8022015-07-29 06:08:46 -0700615}
616
bsalomon37f9a262015-02-02 13:00:10 -0800617//////////////////////////////////////////////////////////////////////////////
618
619void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes) const {
620 if (maxTextures) {
bsalomon0ea80f42015-02-11 10:49:59 -0800621 *maxTextures = fResourceCache->getMaxResourceCount();
bsalomon37f9a262015-02-02 13:00:10 -0800622 }
623 if (maxTextureBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800624 *maxTextureBytes = fResourceCache->getMaxResourceBytes();
bsalomon37f9a262015-02-02 13:00:10 -0800625 }
626}
627
628void GrContext::setResourceCacheLimits(int maxTextures, size_t maxTextureBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800629 fResourceCache->setLimits(maxTextures, maxTextureBytes);
bsalomon37f9a262015-02-02 13:00:10 -0800630}
631
ericrk0a5fa482015-09-15 14:16:10 -0700632//////////////////////////////////////////////////////////////////////////////
633
634void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
635 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
636}