blob: 348cdbaacac3bb7a631d5447293cde668fcdfbb2 [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 }
robertphillipscaef3452015-11-11 13:18:11 -080075 this->initCommon();
bsalomon33435572014-11-05 14:47:41 -080076 return true;
77}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000078
robertphillipscaef3452015-11-11 13:18:11 -080079void GrContext::initCommon() {
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
robertphillipsa13e2022015-11-11 12:01:09 -080089 fDrawingManager.reset(new GrDrawingManager(this));
joshualitt7c3a2f82015-03-31 13:32:05 -070090
91 // GrBatchFontCache will eventually replace GrFontCache
halcanary385fe4d2015-08-26 13:07:48 -070092 fBatchFontCache = new GrBatchFontCache(this);
joshualittb7133be2015-04-08 09:08:31 -070093
halcanary385fe4d2015-08-26 13:07:48 -070094 fTextBlobCache.reset(new GrTextBlobCache(TextBlobCacheOverBudgetCB, this));
bsalomon@google.comc0af3172012-06-15 14:10:09 +000095}
96
bsalomon@google.com27847de2011-02-22 20:59:41 +000097GrContext::~GrContext() {
robertphillipsea461502015-05-26 11:38:03 -070098 if (!fGpu) {
bsalomon76228632015-05-29 08:02:10 -070099 SkASSERT(!fCaps);
bsalomon@google.com733c0622013-04-24 17:59:32 +0000100 return;
101 }
102
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000103 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000104
robertphillips77a2e522015-10-17 07:43:27 -0700105 fDrawingManager->cleanup();
robertphillips2334fb62015-06-17 05:43:33 -0700106
robertphillips@google.com950b1b02013-10-21 17:37:28 +0000107 for (int i = 0; i < fCleanUpData.count(); ++i) {
108 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo);
109 }
110
halcanary385fe4d2015-08-26 13:07:48 -0700111 delete fResourceProvider;
112 delete fResourceCache;
113 delete fBatchFontCache;
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000114
bsalomon@google.com205d4602011-04-25 12:43:45 +0000115 fGpu->unref();
bsalomon76228632015-05-29 08:02:10 -0700116 fCaps->unref();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000117}
118
bsalomon2354f842014-07-28 13:48:36 -0700119void GrContext::abandonContext() {
bsalomond309e7a2015-04-30 14:18:54 -0700120 fResourceProvider->abandon();
robertphillips0dfa62c2015-11-16 06:23:31 -0800121
122 // Need to abandon the drawing manager first so all the render targets
123 // will be released/forgotten before they too are abandoned.
124 fDrawingManager->abandon();
125
bsalomon@google.com205d4602011-04-25 12:43:45 +0000126 // abandon first to so destructors
127 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800128 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700129
robertphillipse3371302014-09-17 06:01:06 -0700130 fGpu->contextAbandoned();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000131
joshualitt7c3a2f82015-03-31 13:32:05 -0700132 fBatchFontCache->freeAll();
robertphillips@google.come930a072014-04-03 00:34:27 +0000133 fLayerCache->freeAll();
joshualitt26ffc002015-04-16 11:24:04 -0700134 fTextBlobCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000135}
136
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000137void GrContext::resetContext(uint32_t state) {
138 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000139}
140
141void GrContext::freeGpuResources() {
142 this->flush();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000143
joshualitt7c3a2f82015-03-31 13:32:05 -0700144 fBatchFontCache->freeAll();
robertphillips@google.come930a072014-04-03 00:34:27 +0000145 fLayerCache->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700146
147 fDrawingManager->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700148
149 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000150}
151
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000152void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
bsalomon71cb0c22014-11-14 12:10:14 -0800153 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800154 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800155 }
156 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800157 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800158 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000159}
160
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000161////////////////////////////////////////////////////////////////////////////////
162
bsalomon71cb0c22014-11-14 12:10:14 -0800163void GrContext::OverBudgetCB(void* data) {
bsalomon66a450f2014-11-13 13:19:10 -0800164 SkASSERT(data);
bsalomonf21dab92014-11-13 13:33:28 -0800165
bsalomon66a450f2014-11-13 13:19:10 -0800166 GrContext* context = reinterpret_cast<GrContext*>(data);
bsalomonf21dab92014-11-13 13:33:28 -0800167
joshualittb542bae2015-07-28 09:58:39 -0700168 // Flush the GrBufferedDrawTarget to possibly free up some textures
bsalomonf21dab92014-11-13 13:33:28 -0800169 context->fFlushToReduceCacheSize = true;
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000170}
171
joshualitt0db6dfa2015-04-10 07:01:30 -0700172void GrContext::TextBlobCacheOverBudgetCB(void* data) {
173 SkASSERT(data);
174
175 // Unlike the GrResourceCache, TextBlobs are drawn at the SkGpuDevice level, therefore they
176 // cannot use fFlushTorReduceCacheSize because it uses AutoCheckFlush. The solution is to move
177 // drawText calls to below the GrContext level, but this is not trivial because they call
178 // drawPath on SkGpuDevice
179 GrContext* context = reinterpret_cast<GrContext*>(data);
180 context->flush();
181}
182
bsalomon@google.com27847de2011-02-22 20:59:41 +0000183////////////////////////////////////////////////////////////////////////////////
184
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000185void GrContext::flush(int flagsBitfield) {
robertphillipsea461502015-05-26 11:38:03 -0700186 RETURN_IF_ABANDONED
robertphillips@google.come7db8d62013-07-04 11:48:52 +0000187
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000188 if (kDiscard_FlushBit & flagsBitfield) {
robertphillips77a2e522015-10-17 07:43:27 -0700189 fDrawingManager->reset();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000190 } else {
robertphillips77a2e522015-10-17 07:43:27 -0700191 fDrawingManager->flush();
junov@google.com53a55842011-06-08 22:55:10 +0000192 }
bsalomon3f324322015-04-08 11:01:54 -0700193 fResourceCache->notifyFlushOccurred();
bsalomonf21dab92014-11-13 13:33:28 -0800194 fFlushToReduceCacheSize = false;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000195}
196
bsalomon81beccc2014-10-13 12:32:55 -0700197bool sw_convert_to_premul(GrPixelConfig srcConfig, int width, int height, size_t inRowBytes,
198 const void* inPixels, size_t outRowBytes, void* outPixels) {
199 SkSrcPixelInfo srcPI;
halcanary96fcdcc2015-08-27 07:41:13 -0700200 if (!GrPixelConfig2ColorAndProfileType(srcConfig, &srcPI.fColorType, nullptr)) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000201 return false;
202 }
bsalomon81beccc2014-10-13 12:32:55 -0700203 srcPI.fAlphaType = kUnpremul_SkAlphaType;
204 srcPI.fPixels = inPixels;
205 srcPI.fRowBytes = inRowBytes;
206
207 SkDstPixelInfo dstPI;
208 dstPI.fColorType = srcPI.fColorType;
209 dstPI.fAlphaType = kPremul_SkAlphaType;
210 dstPI.fPixels = outPixels;
211 dstPI.fRowBytes = outRowBytes;
212
213 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000214}
215
bsalomon81beccc2014-10-13 12:32:55 -0700216bool GrContext::writeSurfacePixels(GrSurface* surface,
217 int left, int top, int width, int height,
218 GrPixelConfig srcConfig, const void* buffer, size_t rowBytes,
219 uint32_t pixelOpsFlags) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800220 RETURN_FALSE_IF_ABANDONED
bsalomon6c6f6582015-09-10 08:12:46 -0700221 ASSERT_OWNED_RESOURCE(surface);
222 SkASSERT(surface);
223
224 this->testPMConversionsIfNecessary(pixelOpsFlags);
bsalomon81beccc2014-10-13 12:32:55 -0700225
bsalomone8d21e82015-07-16 08:23:13 -0700226 // 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 -0700227 // necessary and because GrGpu::getWritePixelsInfo requires it.
bsalomone8d21e82015-07-16 08:23:13 -0700228 if (!GrSurfacePriv::AdjustWritePixelParams(surface->width(), surface->height(),
229 GrBytesPerPixel(srcConfig), &left, &top, &width,
230 &height, &buffer, &rowBytes)) {
231 return false;
232 }
233
bsalomonf0674512015-07-28 13:26:15 -0700234 bool applyPremulToSrc = false;
bsalomon81beccc2014-10-13 12:32:55 -0700235 if (kUnpremul_PixelOpsFlag & pixelOpsFlags) {
236 if (!GrPixelConfigIs8888(srcConfig)) {
237 return false;
238 }
bsalomonf0674512015-07-28 13:26:15 -0700239 applyPremulToSrc = true;
240 }
bsalomon636e8022015-07-29 06:08:46 -0700241
242 GrGpu::DrawPreference drawPreference = GrGpu::kNoDraw_DrawPreference;
243 // Don't prefer to draw for the conversion (and thereby access a texture from the cache) when
244 // we've already determined that there isn't a roundtrip preserving conversion processor pair.
245 if (applyPremulToSrc && !this->didFailPMUPMConversionTest()) {
246 drawPreference = GrGpu::kCallerPrefersDraw_DrawPreference;
247 }
248
bsalomonf0674512015-07-28 13:26:15 -0700249 GrGpu::WritePixelTempDrawInfo tempDrawInfo;
250 if (!fGpu->getWritePixelsInfo(surface, width, height, rowBytes, srcConfig, &drawPreference,
251 &tempDrawInfo)) {
252 return false;
253 }
254
255 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && surface->surfacePriv().hasPendingIO()) {
256 this->flush();
257 }
258
259 SkAutoTUnref<GrTexture> tempTexture;
260 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
bsalomoneae62002015-07-31 13:59:30 -0700261 tempTexture.reset(
262 this->textureProvider()->createApproxTexture(tempDrawInfo.fTempSurfaceDesc));
bsalomonf0674512015-07-28 13:26:15 -0700263 if (!tempTexture && GrGpu::kRequireDraw_DrawPreference == drawPreference) {
264 return false;
265 }
266 }
267
268 // temp buffer for doing sw premul conversion, if needed.
benjaminwagner7d974f52015-10-19 13:55:55 -0700269#if defined(GOOGLE3)
270 // Stack frame size is limited in GOOGLE3.
271 SkAutoSTMalloc<48 * 48, uint32_t> tmpPixels(0);
272#else
bsalomonf0674512015-07-28 13:26:15 -0700273 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
benjaminwagner7d974f52015-10-19 13:55:55 -0700274#endif
bsalomonf0674512015-07-28 13:26:15 -0700275 if (tempTexture) {
276 SkAutoTUnref<const GrFragmentProcessor> fp;
277 SkMatrix textureMatrix;
278 textureMatrix.setIDiv(tempTexture->width(), tempTexture->height());
279 GrPaint paint;
280 if (applyPremulToSrc) {
bsalomon4a339522015-10-06 08:40:50 -0700281 fp.reset(this->createUPMToPMEffect(tempTexture, tempDrawInfo.fSwapRAndB,
282 textureMatrix));
bsalomonf0674512015-07-28 13:26:15 -0700283 // If premultiplying was the only reason for the draw, fall back to a straight write.
284 if (!fp) {
285 if (GrGpu::kCallerPrefersDraw_DrawPreference == drawPreference) {
halcanary96fcdcc2015-08-27 07:41:13 -0700286 tempTexture.reset(nullptr);
bsalomonf0674512015-07-28 13:26:15 -0700287 }
288 } else {
289 applyPremulToSrc = false;
290 }
291 }
292 if (tempTexture) {
293 if (!fp) {
bsalomon4a339522015-10-06 08:40:50 -0700294 fp.reset(GrConfigConversionEffect::Create(tempTexture, tempDrawInfo.fSwapRAndB,
bsalomonf0674512015-07-28 13:26:15 -0700295 GrConfigConversionEffect::kNone_PMConversion, textureMatrix));
296 if (!fp) {
297 return false;
298 }
299 }
300 GrRenderTarget* renderTarget = surface->asRenderTarget();
301 SkASSERT(renderTarget);
302 if (tempTexture->surfacePriv().hasPendingIO()) {
303 this->flush();
304 }
305 if (applyPremulToSrc) {
306 size_t tmpRowBytes = 4 * width;
307 tmpPixels.reset(width * height);
308 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
309 tmpPixels.get())) {
310 return false;
311 }
312 rowBytes = tmpRowBytes;
313 buffer = tmpPixels.get();
314 applyPremulToSrc = false;
315 }
bsalomon6cb3cbe2015-07-30 07:34:27 -0700316 if (!fGpu->writePixels(tempTexture, 0, 0, width, height,
317 tempDrawInfo.fTempSurfaceDesc.fConfig, buffer,
318 rowBytes)) {
bsalomonf0674512015-07-28 13:26:15 -0700319 return false;
320 }
321 SkMatrix matrix;
322 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
robertphillips2e1e51f2015-10-15 08:01:48 -0700323 SkAutoTUnref<GrDrawContext> drawContext(this->drawContext(renderTarget));
bsalomonf0674512015-07-28 13:26:15 -0700324 if (!drawContext) {
325 return false;
326 }
bsalomonac856c92015-08-27 06:30:17 -0700327 paint.addColorFragmentProcessor(fp);
bsalomonf0674512015-07-28 13:26:15 -0700328 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
robertphillips2e1e51f2015-10-15 08:01:48 -0700329 drawContext->drawRect(GrClip::WideOpen(), paint, matrix, rect, nullptr);
bsalomonf0674512015-07-28 13:26:15 -0700330
331 if (kFlushWrites_PixelOp & pixelOpsFlags) {
332 this->flushSurfaceWrites(surface);
333 }
334 }
335 }
336 if (!tempTexture) {
bsalomonf0674512015-07-28 13:26:15 -0700337 if (applyPremulToSrc) {
bsalomon81beccc2014-10-13 12:32:55 -0700338 size_t tmpRowBytes = 4 * width;
339 tmpPixels.reset(width * height);
340 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
341 tmpPixels.get())) {
342 return false;
343 }
344 rowBytes = tmpRowBytes;
345 buffer = tmpPixels.get();
bsalomonf0674512015-07-28 13:26:15 -0700346 applyPremulToSrc = false;
bsalomon81beccc2014-10-13 12:32:55 -0700347 }
bsalomon6cb3cbe2015-07-30 07:34:27 -0700348 return fGpu->writePixels(surface, left, top, width, height, srcConfig, buffer, rowBytes);
bsalomon81beccc2014-10-13 12:32:55 -0700349 }
bsalomon81beccc2014-10-13 12:32:55 -0700350 return true;
351}
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000352
bsalomone8d21e82015-07-16 08:23:13 -0700353bool GrContext::readSurfacePixels(GrSurface* src,
354 int left, int top, int width, int height,
355 GrPixelConfig dstConfig, void* buffer, size_t rowBytes,
356 uint32_t flags) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800357 RETURN_FALSE_IF_ABANDONED
bsalomone8d21e82015-07-16 08:23:13 -0700358 ASSERT_OWNED_RESOURCE(src);
359 SkASSERT(src);
bsalomon32ab2602015-09-09 18:57:49 -0700360
bsalomon6c6f6582015-09-10 08:12:46 -0700361 this->testPMConversionsIfNecessary(flags);
362 SkAutoMutexAcquire ama(fReadPixelsMutex);
363
bsalomone8d21e82015-07-16 08:23:13 -0700364 // Adjust the params so that if we wind up using an intermediate surface we've already done
365 // all the trimming and the temporary can be the min size required.
366 if (!GrSurfacePriv::AdjustReadPixelParams(src->width(), src->height(),
367 GrBytesPerPixel(dstConfig), &left,
368 &top, &width, &height, &buffer, &rowBytes)) {
369 return false;
370 }
371
372 if (!(kDontFlush_PixelOpsFlag & flags) && src->surfacePriv().hasPendingWrite()) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000373 this->flush();
374 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000375
bsalomone8d21e82015-07-16 08:23:13 -0700376 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
bsalomon@google.com9c680582013-02-06 18:17:50 +0000377 if (unpremul && !GrPixelConfigIs8888(dstConfig)) {
bsalomon39826022015-07-23 08:07:21 -0700378 // The unpremul flag is only allowed for 8888 configs.
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000379 return false;
380 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000381
bsalomon636e8022015-07-29 06:08:46 -0700382 GrGpu::DrawPreference drawPreference = GrGpu::kNoDraw_DrawPreference;
383 // Don't prefer to draw for the conversion (and thereby access a texture from the cache) when
384 // we've already determined that there isn't a roundtrip preserving conversion processor pair.
385 if (unpremul && !this->didFailPMUPMConversionTest()) {
386 drawPreference = GrGpu::kCallerPrefersDraw_DrawPreference;
387 }
388
bsalomon39826022015-07-23 08:07:21 -0700389 GrGpu::ReadPixelTempDrawInfo tempDrawInfo;
390 if (!fGpu->getReadPixelsInfo(src, width, height, rowBytes, dstConfig, &drawPreference,
391 &tempDrawInfo)) {
392 return false;
393 }
bsalomon191bcc02014-11-14 11:31:13 -0800394
bsalomon6cb3cbe2015-07-30 07:34:27 -0700395 SkAutoTUnref<GrSurface> surfaceToRead(SkRef(src));
bsalomon39826022015-07-23 08:07:21 -0700396 bool didTempDraw = false;
397 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
bsalomon39826022015-07-23 08:07:21 -0700398 if (tempDrawInfo.fUseExactScratch) {
399 // We only respect this when the entire src is being read. Otherwise we can trigger too
400 // many odd ball texture sizes and trash the cache.
bsalomoneae62002015-07-31 13:59:30 -0700401 if (width != src->width() || height != src->height()) {
402 tempDrawInfo.fUseExactScratch = false;
bsalomon39826022015-07-23 08:07:21 -0700403 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000404 }
bsalomon39826022015-07-23 08:07:21 -0700405 SkAutoTUnref<GrTexture> temp;
bsalomoneae62002015-07-31 13:59:30 -0700406 if (tempDrawInfo.fUseExactScratch) {
407 temp.reset(this->textureProvider()->createTexture(tempDrawInfo.fTempSurfaceDesc, true));
408 } else {
409 temp.reset(this->textureProvider()->createApproxTexture(tempDrawInfo.fTempSurfaceDesc));
410 }
bsalomon39826022015-07-23 08:07:21 -0700411 if (temp) {
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000412 SkMatrix textureMatrix;
bsalomon39826022015-07-23 08:07:21 -0700413 textureMatrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000414 textureMatrix.postIDiv(src->width(), src->height());
joshualitt5f10b5c2015-07-09 10:24:35 -0700415 GrPaint paint;
joshualittb0a8a372014-09-23 09:50:21 -0700416 SkAutoTUnref<const GrFragmentProcessor> fp;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000417 if (unpremul) {
bsalomon4a339522015-10-06 08:40:50 -0700418 fp.reset(this->createPMToUPMEffect(src->asTexture(), tempDrawInfo.fSwapRAndB,
bsalomon39826022015-07-23 08:07:21 -0700419 textureMatrix));
joshualittb0a8a372014-09-23 09:50:21 -0700420 if (fp) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000421 unpremul = false; // we no longer need to do this on CPU after the read back.
bsalomon39826022015-07-23 08:07:21 -0700422 } else if (GrGpu::kCallerPrefersDraw_DrawPreference == drawPreference) {
423 // We only wanted to do the draw in order to perform the unpremul so don't
424 // bother.
halcanary96fcdcc2015-08-27 07:41:13 -0700425 temp.reset(nullptr);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000426 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000427 }
bsalomon39826022015-07-23 08:07:21 -0700428 if (!fp && temp) {
bsalomon4a339522015-10-06 08:40:50 -0700429 fp.reset(GrConfigConversionEffect::Create(src->asTexture(), tempDrawInfo.fSwapRAndB,
bsalomon39826022015-07-23 08:07:21 -0700430 GrConfigConversionEffect::kNone_PMConversion, textureMatrix));
431 }
432 if (fp) {
bsalomonac856c92015-08-27 06:30:17 -0700433 paint.addColorFragmentProcessor(fp);
bsalomon39826022015-07-23 08:07:21 -0700434 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
robertphillips2e1e51f2015-10-15 08:01:48 -0700435 SkAutoTUnref<GrDrawContext> drawContext(this->drawContext(temp->asRenderTarget()));
436 drawContext->drawRect(GrClip::WideOpen(), paint, SkMatrix::I(), rect, nullptr);
bsalomon6cb3cbe2015-07-30 07:34:27 -0700437 surfaceToRead.reset(SkRef(temp.get()));
bsalomon39826022015-07-23 08:07:21 -0700438 left = 0;
439 top = 0;
440 didTempDraw = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000441 }
bsalomon@google.com0342a852012-08-20 19:22:38 +0000442 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000443 }
joshualitt5c55fef2014-10-31 14:04:35 -0700444
bsalomon39826022015-07-23 08:07:21 -0700445 if (GrGpu::kRequireDraw_DrawPreference == drawPreference && !didTempDraw) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000446 return false;
447 }
bsalomon39826022015-07-23 08:07:21 -0700448 GrPixelConfig configToRead = dstConfig;
449 if (didTempDraw) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700450 this->flushSurfaceWrites(surfaceToRead);
bsalomon39826022015-07-23 08:07:21 -0700451 // We swapped R and B while doing the temp draw. Swap back on the read.
452 if (tempDrawInfo.fSwapRAndB) {
453 configToRead = GrPixelConfigSwapRAndB(dstConfig);
454 }
455 }
bsalomon6cb3cbe2015-07-30 07:34:27 -0700456 if (!fGpu->readPixels(surfaceToRead, left, top, width, height, configToRead, buffer,
457 rowBytes)) {
bsalomon39826022015-07-23 08:07:21 -0700458 return false;
459 }
460
461 // Perform umpremul conversion if we weren't able to perform it as a draw.
462 if (unpremul) {
reed@google.com7111d462014-03-25 16:20:24 +0000463 SkDstPixelInfo dstPI;
halcanary96fcdcc2015-08-27 07:41:13 -0700464 if (!GrPixelConfig2ColorAndProfileType(dstConfig, &dstPI.fColorType, nullptr)) {
reed@google.com7111d462014-03-25 16:20:24 +0000465 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000466 }
reed@google.com7111d462014-03-25 16:20:24 +0000467 dstPI.fAlphaType = kUnpremul_SkAlphaType;
468 dstPI.fPixels = buffer;
469 dstPI.fRowBytes = rowBytes;
470
471 SkSrcPixelInfo srcPI;
bsalomon39826022015-07-23 08:07:21 -0700472 srcPI.fColorType = dstPI.fColorType;
reed@google.com7111d462014-03-25 16:20:24 +0000473 srcPI.fAlphaType = kPremul_SkAlphaType;
474 srcPI.fPixels = buffer;
475 srcPI.fRowBytes = rowBytes;
476
477 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000478 }
479 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000480}
481
bsalomonc49e8682015-06-30 11:37:35 -0700482void GrContext::prepareSurfaceForExternalIO(GrSurface* surface) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800483 RETURN_IF_ABANDONED
bsalomon87a94eb2014-11-03 14:28:32 -0800484 SkASSERT(surface);
485 ASSERT_OWNED_RESOURCE(surface);
486 if (surface->surfacePriv().hasPendingIO()) {
487 this->flush();
488 }
489 GrRenderTarget* rt = surface->asRenderTarget();
490 if (fGpu && rt) {
491 fGpu->resolveRenderTarget(rt);
bsalomon41ebbdd2014-08-04 08:31:39 -0700492 }
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000493}
494
bsalomonf80bfed2014-10-07 05:56:02 -0700495void GrContext::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
496 const SkIPoint& dstPoint, uint32_t pixelOpsFlags) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800497 RETURN_IF_ABANDONED
robertphillipsea461502015-05-26 11:38:03 -0700498 if (!src || !dst) {
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000499 return;
500 }
bsalomone3d4bf22014-09-23 09:15:03 -0700501 ASSERT_OWNED_RESOURCE(src);
junov2bb52102014-09-29 10:18:59 -0700502 ASSERT_OWNED_RESOURCE(dst);
Brian Salomon34a98952014-09-24 11:41:24 -0400503
bsalomonf80bfed2014-10-07 05:56:02 -0700504 // Since we're going to the draw target and not GPU, no need to check kNoFlush
505 // here.
robertphillipsea461502015-05-26 11:38:03 -0700506 if (!dst->asRenderTarget()) {
junov96c118e2014-09-26 13:09:47 -0700507 return;
508 }
robertphillipsea461502015-05-26 11:38:03 -0700509
robertphillips2e1e51f2015-10-15 08:01:48 -0700510 SkAutoTUnref<GrDrawContext> drawContext(this->drawContext(dst->asRenderTarget()));
robertphillipsea461502015-05-26 11:38:03 -0700511 if (!drawContext) {
512 return;
513 }
514
robertphillips2e1e51f2015-10-15 08:01:48 -0700515 drawContext->copySurface(src, srcRect, dstPoint);
bsalomonf80bfed2014-10-07 05:56:02 -0700516
517 if (kFlushWrites_PixelOp & pixelOpsFlags) {
518 this->flush();
519 }
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000520}
521
bsalomonf80bfed2014-10-07 05:56:02 -0700522void GrContext::flushSurfaceWrites(GrSurface* surface) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800523 RETURN_IF_ABANDONED
bsalomonf80bfed2014-10-07 05:56:02 -0700524 if (surface->surfacePriv().hasPendingWrite()) {
525 this->flush();
526 }
527}
528
bsalomon@google.com27847de2011-02-22 20:59:41 +0000529////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000530int GrContext::getRecommendedSampleCount(GrPixelConfig config,
531 SkScalar dpi) const {
bsalomon76228632015-05-29 08:02:10 -0700532 if (!this->caps()->isConfigRenderable(config, true)) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000533 return 0;
534 }
535 int chosenSampleCount = 0;
jvanverthe9c0fc62015-04-29 11:18:05 -0700536 if (fGpu->caps()->shaderCaps()->pathRenderingSupport()) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000537 if (dpi >= 250.0f) {
538 chosenSampleCount = 4;
539 } else {
540 chosenSampleCount = 16;
541 }
542 }
543 return chosenSampleCount <= fGpu->caps()->maxSampleCount() ?
544 chosenSampleCount : 0;
545}
546
robertphillips77a2e522015-10-17 07:43:27 -0700547
548GrDrawContext* GrContext::drawContext(GrRenderTarget* rt, const SkSurfaceProps* surfaceProps) {
549 return fDrawingManager->drawContext(rt, surfaceProps);
550}
551
552bool GrContext::abandoned() const {
553 return fDrawingManager->abandoned();
554}
555
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000556namespace {
557void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
558 GrConfigConversionEffect::PMConversion pmToUPM;
559 GrConfigConversionEffect::PMConversion upmToPM;
560 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM);
561 *pmToUPMValue = pmToUPM;
562 *upmToPMValue = upmToPM;
563}
564}
565
bsalomon6c6f6582015-09-10 08:12:46 -0700566void GrContext::testPMConversionsIfNecessary(uint32_t flags) {
567 if (SkToBool(kUnpremul_PixelOpsFlag & flags)) {
568 SkAutoMutexAcquire ama(fTestPMConversionsMutex);
569 if (!fDidTestPMConversions) {
570 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
571 fDidTestPMConversions = true;
572 }
573 }
574}
575
bsalomon4a339522015-10-06 08:40:50 -0700576const GrFragmentProcessor* GrContext::createPMToUPMEffect(GrTexture* texture,
joshualittb0a8a372014-09-23 09:50:21 -0700577 bool swapRAndB,
bsalomon6c6f6582015-09-10 08:12:46 -0700578 const SkMatrix& matrix) const {
579 // We should have already called this->testPMConversionsIfNecessary().
580 SkASSERT(fDidTestPMConversions);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000581 GrConfigConversionEffect::PMConversion pmToUPM =
582 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
583 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
bsalomon4a339522015-10-06 08:40:50 -0700584 return GrConfigConversionEffect::Create(texture, swapRAndB, pmToUPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000585 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700586 return nullptr;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000587 }
588}
589
bsalomon4a339522015-10-06 08:40:50 -0700590const GrFragmentProcessor* GrContext::createUPMToPMEffect(GrTexture* texture,
joshualittb0a8a372014-09-23 09:50:21 -0700591 bool swapRAndB,
bsalomon6c6f6582015-09-10 08:12:46 -0700592 const SkMatrix& matrix) const {
593 // We should have already called this->testPMConversionsIfNecessary().
594 SkASSERT(fDidTestPMConversions);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000595 GrConfigConversionEffect::PMConversion upmToPM =
596 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion);
597 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) {
bsalomon4a339522015-10-06 08:40:50 -0700598 return GrConfigConversionEffect::Create(texture, swapRAndB, upmToPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000599 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700600 return nullptr;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000601 }
602}
603
bsalomon636e8022015-07-29 06:08:46 -0700604bool GrContext::didFailPMUPMConversionTest() const {
bsalomon6c6f6582015-09-10 08:12:46 -0700605 // We should have already called this->testPMConversionsIfNecessary().
606 SkASSERT(fDidTestPMConversions);
bsalomon636e8022015-07-29 06:08:46 -0700607 // The PM<->UPM tests fail or succeed together so we only need to check one.
bsalomon6c6f6582015-09-10 08:12:46 -0700608 return GrConfigConversionEffect::kNone_PMConversion == fPMToUPMConversion;
bsalomon636e8022015-07-29 06:08:46 -0700609}
610
bsalomon37f9a262015-02-02 13:00:10 -0800611//////////////////////////////////////////////////////////////////////////////
612
613void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes) const {
614 if (maxTextures) {
bsalomon0ea80f42015-02-11 10:49:59 -0800615 *maxTextures = fResourceCache->getMaxResourceCount();
bsalomon37f9a262015-02-02 13:00:10 -0800616 }
617 if (maxTextureBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800618 *maxTextureBytes = fResourceCache->getMaxResourceBytes();
bsalomon37f9a262015-02-02 13:00:10 -0800619 }
620}
621
622void GrContext::setResourceCacheLimits(int maxTextures, size_t maxTextureBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800623 fResourceCache->setLimits(maxTextures, maxTextureBytes);
bsalomon37f9a262015-02-02 13:00:10 -0800624}
625
ericrk0a5fa482015-09-15 14:16:10 -0700626//////////////////////////////////////////////////////////////////////////////
627
628void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
629 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
630}