blob: a4887e3350b6c623e67a34c7d0a533ec6084f168 [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"
joshualittb7133be2015-04-08 09:08:31 -070018#include "GrTextBlobCache.h"
robertphillips3dc6ae52015-10-20 09:54:32 -070019
bsalomon81beccc2014-10-13 12:32:55 -070020#include "SkConfig8888.h"
bsalomonf276ac52015-10-09 13:36:42 -070021#include "SkGrPriv.h"
joshualitt74417822015-08-07 11:42:16 -070022
joshualitt5478d422014-11-14 16:00:38 -080023#include "effects/GrConfigConversionEffect.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;
bsalomon69cfe952015-11-30 13:27:47 -080092 fDrawingManager.reset(new GrDrawingManager(this, dtOptions));
joshualitt7c3a2f82015-03-31 13:32:05 -070093
94 // GrBatchFontCache will eventually replace GrFontCache
halcanary385fe4d2015-08-26 13:07:48 -070095 fBatchFontCache = new GrBatchFontCache(this);
joshualittb7133be2015-04-08 09:08:31 -070096
halcanary385fe4d2015-08-26 13:07:48 -070097 fTextBlobCache.reset(new GrTextBlobCache(TextBlobCacheOverBudgetCB, this));
bsalomon@google.comc0af3172012-06-15 14:10:09 +000098}
99
bsalomon@google.com27847de2011-02-22 20:59:41 +0000100GrContext::~GrContext() {
robertphillipsea461502015-05-26 11:38:03 -0700101 if (!fGpu) {
bsalomon76228632015-05-29 08:02:10 -0700102 SkASSERT(!fCaps);
bsalomon@google.com733c0622013-04-24 17:59:32 +0000103 return;
104 }
105
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000106 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000107
robertphillips77a2e522015-10-17 07:43:27 -0700108 fDrawingManager->cleanup();
robertphillips2334fb62015-06-17 05:43:33 -0700109
robertphillips@google.com950b1b02013-10-21 17:37:28 +0000110 for (int i = 0; i < fCleanUpData.count(); ++i) {
111 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo);
112 }
113
halcanary385fe4d2015-08-26 13:07:48 -0700114 delete fResourceProvider;
115 delete fResourceCache;
116 delete fBatchFontCache;
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000117
bsalomon@google.com205d4602011-04-25 12:43:45 +0000118 fGpu->unref();
bsalomon76228632015-05-29 08:02:10 -0700119 fCaps->unref();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000120}
121
bsalomon2354f842014-07-28 13:48:36 -0700122void GrContext::abandonContext() {
bsalomond309e7a2015-04-30 14:18:54 -0700123 fResourceProvider->abandon();
robertphillips0dfa62c2015-11-16 06:23:31 -0800124
125 // Need to abandon the drawing manager first so all the render targets
126 // will be released/forgotten before they too are abandoned.
127 fDrawingManager->abandon();
128
bsalomon@google.com205d4602011-04-25 12:43:45 +0000129 // abandon first to so destructors
130 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800131 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700132
robertphillipse3371302014-09-17 06:01:06 -0700133 fGpu->contextAbandoned();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000134
joshualitt7c3a2f82015-03-31 13:32:05 -0700135 fBatchFontCache->freeAll();
robertphillips@google.come930a072014-04-03 00:34:27 +0000136 fLayerCache->freeAll();
joshualitt26ffc002015-04-16 11:24:04 -0700137 fTextBlobCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000138}
139
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000140void GrContext::resetContext(uint32_t state) {
141 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000142}
143
144void GrContext::freeGpuResources() {
145 this->flush();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000146
joshualitt7c3a2f82015-03-31 13:32:05 -0700147 fBatchFontCache->freeAll();
robertphillips@google.come930a072014-04-03 00:34:27 +0000148 fLayerCache->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700149
150 fDrawingManager->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700151
152 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000153}
154
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000155void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
bsalomon71cb0c22014-11-14 12:10:14 -0800156 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800157 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800158 }
159 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800160 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800161 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000162}
163
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000164////////////////////////////////////////////////////////////////////////////////
165
bsalomon71cb0c22014-11-14 12:10:14 -0800166void GrContext::OverBudgetCB(void* data) {
bsalomon66a450f2014-11-13 13:19:10 -0800167 SkASSERT(data);
bsalomonf21dab92014-11-13 13:33:28 -0800168
bsalomon66a450f2014-11-13 13:19:10 -0800169 GrContext* context = reinterpret_cast<GrContext*>(data);
bsalomonf21dab92014-11-13 13:33:28 -0800170
joshualittb542bae2015-07-28 09:58:39 -0700171 // Flush the GrBufferedDrawTarget to possibly free up some textures
bsalomonf21dab92014-11-13 13:33:28 -0800172 context->fFlushToReduceCacheSize = true;
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000173}
174
joshualitt0db6dfa2015-04-10 07:01:30 -0700175void GrContext::TextBlobCacheOverBudgetCB(void* data) {
176 SkASSERT(data);
177
178 // Unlike the GrResourceCache, TextBlobs are drawn at the SkGpuDevice level, therefore they
179 // cannot use fFlushTorReduceCacheSize because it uses AutoCheckFlush. The solution is to move
180 // drawText calls to below the GrContext level, but this is not trivial because they call
181 // drawPath on SkGpuDevice
182 GrContext* context = reinterpret_cast<GrContext*>(data);
183 context->flush();
184}
185
bsalomon@google.com27847de2011-02-22 20:59:41 +0000186////////////////////////////////////////////////////////////////////////////////
187
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000188void GrContext::flush(int flagsBitfield) {
robertphillipsea461502015-05-26 11:38:03 -0700189 RETURN_IF_ABANDONED
robertphillips@google.come7db8d62013-07-04 11:48:52 +0000190
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000191 if (kDiscard_FlushBit & flagsBitfield) {
robertphillips77a2e522015-10-17 07:43:27 -0700192 fDrawingManager->reset();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000193 } else {
robertphillips77a2e522015-10-17 07:43:27 -0700194 fDrawingManager->flush();
junov@google.com53a55842011-06-08 22:55:10 +0000195 }
bsalomon3f324322015-04-08 11:01:54 -0700196 fResourceCache->notifyFlushOccurred();
bsalomonf21dab92014-11-13 13:33:28 -0800197 fFlushToReduceCacheSize = false;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000198}
199
bsalomon81beccc2014-10-13 12:32:55 -0700200bool sw_convert_to_premul(GrPixelConfig srcConfig, int width, int height, size_t inRowBytes,
201 const void* inPixels, size_t outRowBytes, void* outPixels) {
202 SkSrcPixelInfo srcPI;
halcanary96fcdcc2015-08-27 07:41:13 -0700203 if (!GrPixelConfig2ColorAndProfileType(srcConfig, &srcPI.fColorType, nullptr)) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000204 return false;
205 }
bsalomon81beccc2014-10-13 12:32:55 -0700206 srcPI.fAlphaType = kUnpremul_SkAlphaType;
207 srcPI.fPixels = inPixels;
208 srcPI.fRowBytes = inRowBytes;
209
210 SkDstPixelInfo dstPI;
211 dstPI.fColorType = srcPI.fColorType;
212 dstPI.fAlphaType = kPremul_SkAlphaType;
213 dstPI.fPixels = outPixels;
214 dstPI.fRowBytes = outRowBytes;
215
216 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000217}
218
bsalomon81beccc2014-10-13 12:32:55 -0700219bool GrContext::writeSurfacePixels(GrSurface* surface,
220 int left, int top, int width, int height,
221 GrPixelConfig srcConfig, const void* buffer, size_t rowBytes,
222 uint32_t pixelOpsFlags) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800223 RETURN_FALSE_IF_ABANDONED
bsalomon6c6f6582015-09-10 08:12:46 -0700224 ASSERT_OWNED_RESOURCE(surface);
225 SkASSERT(surface);
226
227 this->testPMConversionsIfNecessary(pixelOpsFlags);
bsalomon81beccc2014-10-13 12:32:55 -0700228
bsalomone8d21e82015-07-16 08:23:13 -0700229 // 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 -0700230 // necessary and because GrGpu::getWritePixelsInfo requires it.
bsalomone8d21e82015-07-16 08:23:13 -0700231 if (!GrSurfacePriv::AdjustWritePixelParams(surface->width(), surface->height(),
232 GrBytesPerPixel(srcConfig), &left, &top, &width,
233 &height, &buffer, &rowBytes)) {
234 return false;
235 }
236
bsalomonf0674512015-07-28 13:26:15 -0700237 bool applyPremulToSrc = false;
bsalomon81beccc2014-10-13 12:32:55 -0700238 if (kUnpremul_PixelOpsFlag & pixelOpsFlags) {
239 if (!GrPixelConfigIs8888(srcConfig)) {
240 return false;
241 }
bsalomonf0674512015-07-28 13:26:15 -0700242 applyPremulToSrc = true;
243 }
bsalomon636e8022015-07-29 06:08:46 -0700244
245 GrGpu::DrawPreference drawPreference = GrGpu::kNoDraw_DrawPreference;
246 // Don't prefer to draw for the conversion (and thereby access a texture from the cache) when
247 // we've already determined that there isn't a roundtrip preserving conversion processor pair.
248 if (applyPremulToSrc && !this->didFailPMUPMConversionTest()) {
249 drawPreference = GrGpu::kCallerPrefersDraw_DrawPreference;
250 }
251
bsalomonf0674512015-07-28 13:26:15 -0700252 GrGpu::WritePixelTempDrawInfo tempDrawInfo;
253 if (!fGpu->getWritePixelsInfo(surface, width, height, rowBytes, srcConfig, &drawPreference,
254 &tempDrawInfo)) {
255 return false;
256 }
257
258 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && surface->surfacePriv().hasPendingIO()) {
259 this->flush();
260 }
261
262 SkAutoTUnref<GrTexture> tempTexture;
263 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
bsalomoneae62002015-07-31 13:59:30 -0700264 tempTexture.reset(
265 this->textureProvider()->createApproxTexture(tempDrawInfo.fTempSurfaceDesc));
bsalomonf0674512015-07-28 13:26:15 -0700266 if (!tempTexture && GrGpu::kRequireDraw_DrawPreference == drawPreference) {
267 return false;
268 }
269 }
270
271 // temp buffer for doing sw premul conversion, if needed.
benjaminwagner7d974f52015-10-19 13:55:55 -0700272#if defined(GOOGLE3)
273 // Stack frame size is limited in GOOGLE3.
274 SkAutoSTMalloc<48 * 48, uint32_t> tmpPixels(0);
275#else
bsalomonf0674512015-07-28 13:26:15 -0700276 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
benjaminwagner7d974f52015-10-19 13:55:55 -0700277#endif
bsalomonf0674512015-07-28 13:26:15 -0700278 if (tempTexture) {
279 SkAutoTUnref<const GrFragmentProcessor> fp;
280 SkMatrix textureMatrix;
281 textureMatrix.setIDiv(tempTexture->width(), tempTexture->height());
bsalomonf0674512015-07-28 13:26:15 -0700282 if (applyPremulToSrc) {
bsalomon4a339522015-10-06 08:40:50 -0700283 fp.reset(this->createUPMToPMEffect(tempTexture, tempDrawInfo.fSwapRAndB,
284 textureMatrix));
bsalomonf0674512015-07-28 13:26:15 -0700285 // If premultiplying was the only reason for the draw, fall back to a straight write.
286 if (!fp) {
287 if (GrGpu::kCallerPrefersDraw_DrawPreference == drawPreference) {
halcanary96fcdcc2015-08-27 07:41:13 -0700288 tempTexture.reset(nullptr);
bsalomonf0674512015-07-28 13:26:15 -0700289 }
290 } else {
291 applyPremulToSrc = false;
292 }
293 }
294 if (tempTexture) {
295 if (!fp) {
bsalomon4a339522015-10-06 08:40:50 -0700296 fp.reset(GrConfigConversionEffect::Create(tempTexture, tempDrawInfo.fSwapRAndB,
bsalomonf0674512015-07-28 13:26:15 -0700297 GrConfigConversionEffect::kNone_PMConversion, textureMatrix));
298 if (!fp) {
299 return false;
300 }
301 }
302 GrRenderTarget* renderTarget = surface->asRenderTarget();
303 SkASSERT(renderTarget);
304 if (tempTexture->surfacePriv().hasPendingIO()) {
305 this->flush();
306 }
307 if (applyPremulToSrc) {
308 size_t tmpRowBytes = 4 * width;
309 tmpPixels.reset(width * height);
310 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
311 tmpPixels.get())) {
312 return false;
313 }
314 rowBytes = tmpRowBytes;
315 buffer = tmpPixels.get();
316 applyPremulToSrc = false;
317 }
bsalomon6cb3cbe2015-07-30 07:34:27 -0700318 if (!fGpu->writePixels(tempTexture, 0, 0, width, height,
319 tempDrawInfo.fTempSurfaceDesc.fConfig, buffer,
320 rowBytes)) {
bsalomonf0674512015-07-28 13:26:15 -0700321 return false;
322 }
323 SkMatrix matrix;
324 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
robertphillips2e1e51f2015-10-15 08:01:48 -0700325 SkAutoTUnref<GrDrawContext> drawContext(this->drawContext(renderTarget));
bsalomonf0674512015-07-28 13:26:15 -0700326 if (!drawContext) {
327 return false;
328 }
egdanielc4b72722015-11-23 13:20:41 -0800329 GrPaint paint;
bsalomonac856c92015-08-27 06:30:17 -0700330 paint.addColorFragmentProcessor(fp);
egdanielc4b72722015-11-23 13:20:41 -0800331 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
bsalomonf0674512015-07-28 13:26:15 -0700332 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
robertphillips2e1e51f2015-10-15 08:01:48 -0700333 drawContext->drawRect(GrClip::WideOpen(), paint, matrix, rect, nullptr);
bsalomonf0674512015-07-28 13:26:15 -0700334
335 if (kFlushWrites_PixelOp & pixelOpsFlags) {
336 this->flushSurfaceWrites(surface);
337 }
338 }
339 }
340 if (!tempTexture) {
bsalomonf0674512015-07-28 13:26:15 -0700341 if (applyPremulToSrc) {
bsalomon81beccc2014-10-13 12:32:55 -0700342 size_t tmpRowBytes = 4 * width;
343 tmpPixels.reset(width * height);
344 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
345 tmpPixels.get())) {
346 return false;
347 }
348 rowBytes = tmpRowBytes;
349 buffer = tmpPixels.get();
bsalomonf0674512015-07-28 13:26:15 -0700350 applyPremulToSrc = false;
bsalomon81beccc2014-10-13 12:32:55 -0700351 }
bsalomon6cb3cbe2015-07-30 07:34:27 -0700352 return fGpu->writePixels(surface, left, top, width, height, srcConfig, buffer, rowBytes);
bsalomon81beccc2014-10-13 12:32:55 -0700353 }
bsalomon81beccc2014-10-13 12:32:55 -0700354 return true;
355}
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000356
bsalomone8d21e82015-07-16 08:23:13 -0700357bool GrContext::readSurfacePixels(GrSurface* src,
358 int left, int top, int width, int height,
359 GrPixelConfig dstConfig, void* buffer, size_t rowBytes,
360 uint32_t flags) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800361 RETURN_FALSE_IF_ABANDONED
bsalomone8d21e82015-07-16 08:23:13 -0700362 ASSERT_OWNED_RESOURCE(src);
363 SkASSERT(src);
bsalomon32ab2602015-09-09 18:57:49 -0700364
bsalomon6c6f6582015-09-10 08:12:46 -0700365 this->testPMConversionsIfNecessary(flags);
366 SkAutoMutexAcquire ama(fReadPixelsMutex);
367
bsalomone8d21e82015-07-16 08:23:13 -0700368 // Adjust the params so that if we wind up using an intermediate surface we've already done
369 // all the trimming and the temporary can be the min size required.
370 if (!GrSurfacePriv::AdjustReadPixelParams(src->width(), src->height(),
371 GrBytesPerPixel(dstConfig), &left,
372 &top, &width, &height, &buffer, &rowBytes)) {
373 return false;
374 }
375
376 if (!(kDontFlush_PixelOpsFlag & flags) && src->surfacePriv().hasPendingWrite()) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000377 this->flush();
378 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000379
bsalomone8d21e82015-07-16 08:23:13 -0700380 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
bsalomon@google.com9c680582013-02-06 18:17:50 +0000381 if (unpremul && !GrPixelConfigIs8888(dstConfig)) {
bsalomon39826022015-07-23 08:07:21 -0700382 // The unpremul flag is only allowed for 8888 configs.
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000383 return false;
384 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000385
bsalomon636e8022015-07-29 06:08:46 -0700386 GrGpu::DrawPreference drawPreference = GrGpu::kNoDraw_DrawPreference;
387 // Don't prefer to draw for the conversion (and thereby access a texture from the cache) when
388 // we've already determined that there isn't a roundtrip preserving conversion processor pair.
389 if (unpremul && !this->didFailPMUPMConversionTest()) {
390 drawPreference = GrGpu::kCallerPrefersDraw_DrawPreference;
391 }
392
bsalomon39826022015-07-23 08:07:21 -0700393 GrGpu::ReadPixelTempDrawInfo tempDrawInfo;
394 if (!fGpu->getReadPixelsInfo(src, width, height, rowBytes, dstConfig, &drawPreference,
395 &tempDrawInfo)) {
396 return false;
397 }
bsalomon191bcc02014-11-14 11:31:13 -0800398
bsalomon6cb3cbe2015-07-30 07:34:27 -0700399 SkAutoTUnref<GrSurface> surfaceToRead(SkRef(src));
bsalomon39826022015-07-23 08:07:21 -0700400 bool didTempDraw = false;
401 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
bsalomon39826022015-07-23 08:07:21 -0700402 if (tempDrawInfo.fUseExactScratch) {
403 // We only respect this when the entire src is being read. Otherwise we can trigger too
404 // many odd ball texture sizes and trash the cache.
bsalomoneae62002015-07-31 13:59:30 -0700405 if (width != src->width() || height != src->height()) {
406 tempDrawInfo.fUseExactScratch = false;
bsalomon39826022015-07-23 08:07:21 -0700407 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000408 }
bsalomon39826022015-07-23 08:07:21 -0700409 SkAutoTUnref<GrTexture> temp;
bsalomoneae62002015-07-31 13:59:30 -0700410 if (tempDrawInfo.fUseExactScratch) {
411 temp.reset(this->textureProvider()->createTexture(tempDrawInfo.fTempSurfaceDesc, true));
412 } else {
413 temp.reset(this->textureProvider()->createApproxTexture(tempDrawInfo.fTempSurfaceDesc));
414 }
bsalomon39826022015-07-23 08:07:21 -0700415 if (temp) {
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000416 SkMatrix textureMatrix;
bsalomon39826022015-07-23 08:07:21 -0700417 textureMatrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000418 textureMatrix.postIDiv(src->width(), src->height());
joshualittb0a8a372014-09-23 09:50:21 -0700419 SkAutoTUnref<const GrFragmentProcessor> fp;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000420 if (unpremul) {
bsalomon4a339522015-10-06 08:40:50 -0700421 fp.reset(this->createPMToUPMEffect(src->asTexture(), tempDrawInfo.fSwapRAndB,
bsalomon39826022015-07-23 08:07:21 -0700422 textureMatrix));
joshualittb0a8a372014-09-23 09:50:21 -0700423 if (fp) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000424 unpremul = false; // we no longer need to do this on CPU after the read back.
bsalomon39826022015-07-23 08:07:21 -0700425 } else if (GrGpu::kCallerPrefersDraw_DrawPreference == drawPreference) {
426 // We only wanted to do the draw in order to perform the unpremul so don't
427 // bother.
halcanary96fcdcc2015-08-27 07:41:13 -0700428 temp.reset(nullptr);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000429 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000430 }
bsalomon39826022015-07-23 08:07:21 -0700431 if (!fp && temp) {
bsalomon4a339522015-10-06 08:40:50 -0700432 fp.reset(GrConfigConversionEffect::Create(src->asTexture(), tempDrawInfo.fSwapRAndB,
bsalomon39826022015-07-23 08:07:21 -0700433 GrConfigConversionEffect::kNone_PMConversion, textureMatrix));
434 }
435 if (fp) {
egdanielc4b72722015-11-23 13:20:41 -0800436 GrPaint paint;
bsalomonac856c92015-08-27 06:30:17 -0700437 paint.addColorFragmentProcessor(fp);
egdanielc4b72722015-11-23 13:20:41 -0800438 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
bsalomon39826022015-07-23 08:07:21 -0700439 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
robertphillips2e1e51f2015-10-15 08:01:48 -0700440 SkAutoTUnref<GrDrawContext> drawContext(this->drawContext(temp->asRenderTarget()));
441 drawContext->drawRect(GrClip::WideOpen(), paint, SkMatrix::I(), rect, nullptr);
bsalomon6cb3cbe2015-07-30 07:34:27 -0700442 surfaceToRead.reset(SkRef(temp.get()));
bsalomon39826022015-07-23 08:07:21 -0700443 left = 0;
444 top = 0;
445 didTempDraw = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000446 }
bsalomon@google.com0342a852012-08-20 19:22:38 +0000447 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000448 }
joshualitt5c55fef2014-10-31 14:04:35 -0700449
bsalomon39826022015-07-23 08:07:21 -0700450 if (GrGpu::kRequireDraw_DrawPreference == drawPreference && !didTempDraw) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000451 return false;
452 }
bsalomon39826022015-07-23 08:07:21 -0700453 GrPixelConfig configToRead = dstConfig;
454 if (didTempDraw) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700455 this->flushSurfaceWrites(surfaceToRead);
bsalomon39826022015-07-23 08:07:21 -0700456 // We swapped R and B while doing the temp draw. Swap back on the read.
457 if (tempDrawInfo.fSwapRAndB) {
458 configToRead = GrPixelConfigSwapRAndB(dstConfig);
459 }
460 }
bsalomon6cb3cbe2015-07-30 07:34:27 -0700461 if (!fGpu->readPixels(surfaceToRead, left, top, width, height, configToRead, buffer,
462 rowBytes)) {
bsalomon39826022015-07-23 08:07:21 -0700463 return false;
464 }
465
466 // Perform umpremul conversion if we weren't able to perform it as a draw.
467 if (unpremul) {
reed@google.com7111d462014-03-25 16:20:24 +0000468 SkDstPixelInfo dstPI;
halcanary96fcdcc2015-08-27 07:41:13 -0700469 if (!GrPixelConfig2ColorAndProfileType(dstConfig, &dstPI.fColorType, nullptr)) {
reed@google.com7111d462014-03-25 16:20:24 +0000470 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000471 }
reed@google.com7111d462014-03-25 16:20:24 +0000472 dstPI.fAlphaType = kUnpremul_SkAlphaType;
473 dstPI.fPixels = buffer;
474 dstPI.fRowBytes = rowBytes;
475
476 SkSrcPixelInfo srcPI;
bsalomon39826022015-07-23 08:07:21 -0700477 srcPI.fColorType = dstPI.fColorType;
reed@google.com7111d462014-03-25 16:20:24 +0000478 srcPI.fAlphaType = kPremul_SkAlphaType;
479 srcPI.fPixels = buffer;
480 srcPI.fRowBytes = rowBytes;
481
482 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000483 }
484 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000485}
486
bsalomonc49e8682015-06-30 11:37:35 -0700487void GrContext::prepareSurfaceForExternalIO(GrSurface* surface) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800488 RETURN_IF_ABANDONED
bsalomon87a94eb2014-11-03 14:28:32 -0800489 SkASSERT(surface);
490 ASSERT_OWNED_RESOURCE(surface);
491 if (surface->surfacePriv().hasPendingIO()) {
492 this->flush();
493 }
494 GrRenderTarget* rt = surface->asRenderTarget();
495 if (fGpu && rt) {
496 fGpu->resolveRenderTarget(rt);
bsalomon41ebbdd2014-08-04 08:31:39 -0700497 }
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000498}
499
bsalomonf80bfed2014-10-07 05:56:02 -0700500void GrContext::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
501 const SkIPoint& dstPoint, uint32_t pixelOpsFlags) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800502 RETURN_IF_ABANDONED
robertphillipsea461502015-05-26 11:38:03 -0700503 if (!src || !dst) {
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000504 return;
505 }
bsalomone3d4bf22014-09-23 09:15:03 -0700506 ASSERT_OWNED_RESOURCE(src);
junov2bb52102014-09-29 10:18:59 -0700507 ASSERT_OWNED_RESOURCE(dst);
Brian Salomon34a98952014-09-24 11:41:24 -0400508
bsalomonf80bfed2014-10-07 05:56:02 -0700509 // Since we're going to the draw target and not GPU, no need to check kNoFlush
510 // here.
robertphillipsea461502015-05-26 11:38:03 -0700511 if (!dst->asRenderTarget()) {
junov96c118e2014-09-26 13:09:47 -0700512 return;
513 }
robertphillipsea461502015-05-26 11:38:03 -0700514
robertphillips2e1e51f2015-10-15 08:01:48 -0700515 SkAutoTUnref<GrDrawContext> drawContext(this->drawContext(dst->asRenderTarget()));
robertphillipsea461502015-05-26 11:38:03 -0700516 if (!drawContext) {
517 return;
518 }
519
robertphillips2e1e51f2015-10-15 08:01:48 -0700520 drawContext->copySurface(src, srcRect, dstPoint);
bsalomonf80bfed2014-10-07 05:56:02 -0700521
522 if (kFlushWrites_PixelOp & pixelOpsFlags) {
523 this->flush();
524 }
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000525}
526
bsalomonf80bfed2014-10-07 05:56:02 -0700527void GrContext::flushSurfaceWrites(GrSurface* surface) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800528 RETURN_IF_ABANDONED
bsalomonf80bfed2014-10-07 05:56:02 -0700529 if (surface->surfacePriv().hasPendingWrite()) {
530 this->flush();
531 }
532}
533
bsalomon@google.com27847de2011-02-22 20:59:41 +0000534////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000535int GrContext::getRecommendedSampleCount(GrPixelConfig config,
536 SkScalar dpi) const {
bsalomon76228632015-05-29 08:02:10 -0700537 if (!this->caps()->isConfigRenderable(config, true)) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000538 return 0;
539 }
540 int chosenSampleCount = 0;
jvanverthe9c0fc62015-04-29 11:18:05 -0700541 if (fGpu->caps()->shaderCaps()->pathRenderingSupport()) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000542 if (dpi >= 250.0f) {
543 chosenSampleCount = 4;
544 } else {
545 chosenSampleCount = 16;
546 }
547 }
548 return chosenSampleCount <= fGpu->caps()->maxSampleCount() ?
549 chosenSampleCount : 0;
550}
551
robertphillips77a2e522015-10-17 07:43:27 -0700552
553GrDrawContext* GrContext::drawContext(GrRenderTarget* rt, const SkSurfaceProps* surfaceProps) {
554 return fDrawingManager->drawContext(rt, surfaceProps);
555}
556
557bool GrContext::abandoned() const {
558 return fDrawingManager->abandoned();
559}
560
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000561namespace {
562void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
563 GrConfigConversionEffect::PMConversion pmToUPM;
564 GrConfigConversionEffect::PMConversion upmToPM;
565 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM);
566 *pmToUPMValue = pmToUPM;
567 *upmToPMValue = upmToPM;
568}
569}
570
bsalomon6c6f6582015-09-10 08:12:46 -0700571void GrContext::testPMConversionsIfNecessary(uint32_t flags) {
572 if (SkToBool(kUnpremul_PixelOpsFlag & flags)) {
573 SkAutoMutexAcquire ama(fTestPMConversionsMutex);
574 if (!fDidTestPMConversions) {
575 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
576 fDidTestPMConversions = true;
577 }
578 }
579}
580
bsalomon4a339522015-10-06 08:40:50 -0700581const GrFragmentProcessor* GrContext::createPMToUPMEffect(GrTexture* texture,
joshualittb0a8a372014-09-23 09:50:21 -0700582 bool swapRAndB,
bsalomon6c6f6582015-09-10 08:12:46 -0700583 const SkMatrix& matrix) const {
584 // We should have already called this->testPMConversionsIfNecessary().
585 SkASSERT(fDidTestPMConversions);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000586 GrConfigConversionEffect::PMConversion pmToUPM =
587 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
588 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
bsalomon4a339522015-10-06 08:40:50 -0700589 return GrConfigConversionEffect::Create(texture, swapRAndB, pmToUPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000590 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700591 return nullptr;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000592 }
593}
594
bsalomon4a339522015-10-06 08:40:50 -0700595const GrFragmentProcessor* GrContext::createUPMToPMEffect(GrTexture* texture,
joshualittb0a8a372014-09-23 09:50:21 -0700596 bool swapRAndB,
bsalomon6c6f6582015-09-10 08:12:46 -0700597 const SkMatrix& matrix) const {
598 // We should have already called this->testPMConversionsIfNecessary().
599 SkASSERT(fDidTestPMConversions);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000600 GrConfigConversionEffect::PMConversion upmToPM =
601 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion);
602 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) {
bsalomon4a339522015-10-06 08:40:50 -0700603 return GrConfigConversionEffect::Create(texture, swapRAndB, upmToPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000604 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700605 return nullptr;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000606 }
607}
608
bsalomon636e8022015-07-29 06:08:46 -0700609bool GrContext::didFailPMUPMConversionTest() const {
bsalomon6c6f6582015-09-10 08:12:46 -0700610 // We should have already called this->testPMConversionsIfNecessary().
611 SkASSERT(fDidTestPMConversions);
bsalomon636e8022015-07-29 06:08:46 -0700612 // The PM<->UPM tests fail or succeed together so we only need to check one.
bsalomon6c6f6582015-09-10 08:12:46 -0700613 return GrConfigConversionEffect::kNone_PMConversion == fPMToUPMConversion;
bsalomon636e8022015-07-29 06:08:46 -0700614}
615
bsalomon37f9a262015-02-02 13:00:10 -0800616//////////////////////////////////////////////////////////////////////////////
617
618void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes) const {
619 if (maxTextures) {
bsalomon0ea80f42015-02-11 10:49:59 -0800620 *maxTextures = fResourceCache->getMaxResourceCount();
bsalomon37f9a262015-02-02 13:00:10 -0800621 }
622 if (maxTextureBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800623 *maxTextureBytes = fResourceCache->getMaxResourceBytes();
bsalomon37f9a262015-02-02 13:00:10 -0800624 }
625}
626
627void GrContext::setResourceCacheLimits(int maxTextures, size_t maxTextureBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800628 fResourceCache->setLimits(maxTextures, maxTextureBytes);
bsalomon37f9a262015-02-02 13:00:10 -0800629}
630
ericrk0a5fa482015-09-15 14:16:10 -0700631//////////////////////////////////////////////////////////////////////////////
632
633void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
634 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
635}