blob: d442b190056153299b5e43ccbb31349e95a8d9eb [file] [log] [blame]
bsalomon@google.com27847de2011-02-22 20:59:41 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
bsalomon@google.com27847de2011-02-22 20:59:41 +00006 */
7
bsalomon@google.com1fadb202011-12-12 16:10:08 +00008#include "GrContext.h"
robertphillips4fd74ae2016-08-03 14:26:53 -07009#include "GrContextPriv.h"
bsalomon682c2692015-05-22 14:01:46 -070010#include "GrContextOptions.h"
robertphillips77a2e522015-10-17 07:43:27 -070011#include "GrDrawingManager.h"
Brian Osman11052242016-10-27 14:47:55 -040012#include "GrRenderTargetContext.h"
bsalomon0ea80f42015-02-11 10:49:59 -080013#include "GrResourceCache.h"
bsalomond309e7a2015-04-30 14:18:54 -070014#include "GrResourceProvider.h"
robertphillips@google.com72176b22012-05-23 13:19:12 +000015#include "GrSoftwarePathRenderer.h"
bsalomonafbf2d62014-09-30 12:18:44 -070016#include "GrSurfacePriv.h"
robertphillips3dc6ae52015-10-20 09:54:32 -070017
bsalomon81beccc2014-10-13 12:32:55 -070018#include "SkConfig8888.h"
bsalomonf276ac52015-10-09 13:36:42 -070019#include "SkGrPriv.h"
joshualitt74417822015-08-07 11:42:16 -070020
bsalomonb8fea972016-02-16 07:34:17 -080021#include "batches/GrCopySurfaceBatch.h"
joshualitt5478d422014-11-14 16:00:38 -080022#include "effects/GrConfigConversionEffect.h"
brianosman2d1ee792016-05-05 12:24:31 -070023#include "effects/GrGammaEffect.h"
joshualitte8042922015-12-11 06:11:21 -080024#include "text/GrTextBlobCache.h"
joshualitt5478d422014-11-14 16:00:38 -080025
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000026#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
joshualitt1de610a2016-01-06 08:26:09 -080027#define ASSERT_SINGLE_OWNER \
28 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fSingleOwner);)
robertphillips4fd74ae2016-08-03 14:26:53 -070029#define ASSERT_SINGLE_OWNER_PRIV \
30 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fContext->fSingleOwner);)
robertphillips7761d612016-05-16 09:14:53 -070031#define RETURN_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return; }
32#define RETURN_FALSE_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return false; }
33#define RETURN_NULL_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return nullptr; }
bsalomon@google.combc4b6542011-11-19 13:56:11 +000034
robertphillipsea461502015-05-26 11:38:03 -070035////////////////////////////////////////////////////////////////////////////////
36
bsalomon682c2692015-05-22 14:01:46 -070037GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext) {
38 GrContextOptions defaultOptions;
39 return Create(backend, backendContext, defaultOptions);
40}
bsalomonf28cff72015-05-22 12:25:41 -070041
bsalomon682c2692015-05-22 14:01:46 -070042GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext,
43 const GrContextOptions& options) {
halcanary385fe4d2015-08-26 13:07:48 -070044 GrContext* context = new GrContext;
bsalomon682c2692015-05-22 14:01:46 -070045
46 if (context->init(backend, backendContext, options)) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000047 return context;
48 } else {
49 context->unref();
halcanary96fcdcc2015-08-27 07:41:13 -070050 return nullptr;
bsalomon@google.com27847de2011-02-22 20:59:41 +000051 }
bsalomon@google.com27847de2011-02-22 20:59:41 +000052}
53
joshualitt0acd0d32015-05-07 08:23:19 -070054static int32_t gNextID = 1;
55static int32_t next_id() {
56 int32_t id;
57 do {
58 id = sk_atomic_inc(&gNextID);
59 } while (id == SK_InvalidGenID);
60 return id;
61}
62
bsalomon682c2692015-05-22 14:01:46 -070063GrContext::GrContext() : fUniqueID(next_id()) {
halcanary96fcdcc2015-08-27 07:41:13 -070064 fGpu = nullptr;
65 fCaps = nullptr;
66 fResourceCache = nullptr;
67 fResourceProvider = nullptr;
halcanary96fcdcc2015-08-27 07:41:13 -070068 fBatchFontCache = nullptr;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000069}
70
bsalomon682c2692015-05-22 14:01:46 -070071bool GrContext::init(GrBackend backend, GrBackendContext backendContext,
72 const GrContextOptions& options) {
joshualitt1de610a2016-01-06 08:26:09 -080073 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -070074 SkASSERT(!fGpu);
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000075
bsalomon682c2692015-05-22 14:01:46 -070076 fGpu = GrGpu::Create(backend, backendContext, options, this);
robertphillipsea461502015-05-26 11:38:03 -070077 if (!fGpu) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000078 return false;
79 }
bsalomon69cfe952015-11-30 13:27:47 -080080 this->initCommon(options);
bsalomon33435572014-11-05 14:47:41 -080081 return true;
82}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000083
bsalomon69cfe952015-11-30 13:27:47 -080084void GrContext::initCommon(const GrContextOptions& options) {
joshualitt1de610a2016-01-06 08:26:09 -080085 ASSERT_SINGLE_OWNER
86
bsalomon76228632015-05-29 08:02:10 -070087 fCaps = SkRef(fGpu->caps());
halcanary385fe4d2015-08-26 13:07:48 -070088 fResourceCache = new GrResourceCache(fCaps);
joshualitt6d0872d2016-01-11 08:27:48 -080089 fResourceProvider = new GrResourceProvider(fGpu, fResourceCache, &fSingleOwner);
commit-bot@chromium.org1836d332013-07-16 22:55:03 +000090
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000091 fDidTestPMConversions = false;
92
Robert Phillipsf2361d22016-10-25 14:20:06 -040093 GrRenderTargetOpList::Options rtOpListOptions;
94 rtOpListOptions.fClipBatchToBounds = options.fClipBatchToBounds;
95 rtOpListOptions.fDrawBatchBounds = options.fDrawBatchBounds;
96 rtOpListOptions.fMaxBatchLookback = options.fMaxBatchLookback;
97 rtOpListOptions.fMaxBatchLookahead = options.fMaxBatchLookahead;
bsalomon6b2552f2016-09-15 13:50:26 -070098 GrPathRendererChain::Options prcOptions;
99 prcOptions.fDisableDistanceFieldRenderer = options.fDisableDistanceFieldPaths;
bsalomon39ef7fb2016-09-21 11:16:05 -0700100 prcOptions.fAllowPathMaskCaching = options.fAllowPathMaskCaching;
101 prcOptions.fDisableAllPathRenderers = options.fForceSWPathMasks;
Robert Phillipsf2361d22016-10-25 14:20:06 -0400102 fDrawingManager.reset(new GrDrawingManager(this, rtOpListOptions, prcOptions,
103 options.fImmediateMode, &fSingleOwner));
joshualitt7c3a2f82015-03-31 13:32:05 -0700104
105 // GrBatchFontCache will eventually replace GrFontCache
halcanary385fe4d2015-08-26 13:07:48 -0700106 fBatchFontCache = new GrBatchFontCache(this);
joshualittb7133be2015-04-08 09:08:31 -0700107
halcanary385fe4d2015-08-26 13:07:48 -0700108 fTextBlobCache.reset(new GrTextBlobCache(TextBlobCacheOverBudgetCB, this));
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000109}
110
bsalomon@google.com27847de2011-02-22 20:59:41 +0000111GrContext::~GrContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800112 ASSERT_SINGLE_OWNER
113
robertphillipsea461502015-05-26 11:38:03 -0700114 if (!fGpu) {
bsalomon76228632015-05-29 08:02:10 -0700115 SkASSERT(!fCaps);
bsalomon@google.com733c0622013-04-24 17:59:32 +0000116 return;
117 }
118
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000119 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000120
robertphillips77a2e522015-10-17 07:43:27 -0700121 fDrawingManager->cleanup();
robertphillips2334fb62015-06-17 05:43:33 -0700122
robertphillips@google.com950b1b02013-10-21 17:37:28 +0000123 for (int i = 0; i < fCleanUpData.count(); ++i) {
124 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo);
125 }
126
halcanary385fe4d2015-08-26 13:07:48 -0700127 delete fResourceProvider;
128 delete fResourceCache;
129 delete fBatchFontCache;
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000130
bsalomon@google.com205d4602011-04-25 12:43:45 +0000131 fGpu->unref();
bsalomon76228632015-05-29 08:02:10 -0700132 fCaps->unref();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000133}
134
bungeman6bd52842016-10-27 09:30:08 -0700135sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() {
bsalomon41b952c2016-03-11 06:46:33 -0800136 if (!fThreadSafeProxy) {
bungeman6bd52842016-10-27 09:30:08 -0700137 fThreadSafeProxy.reset(new GrContextThreadSafeProxy(sk_ref_sp(fCaps), this->uniqueID()));
bsalomon41b952c2016-03-11 06:46:33 -0800138 }
bungeman6bd52842016-10-27 09:30:08 -0700139 return fThreadSafeProxy;
bsalomon41b952c2016-03-11 06:46:33 -0800140}
141
bsalomon2354f842014-07-28 13:48:36 -0700142void GrContext::abandonContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800143 ASSERT_SINGLE_OWNER
144
bsalomond309e7a2015-04-30 14:18:54 -0700145 fResourceProvider->abandon();
robertphillips0dfa62c2015-11-16 06:23:31 -0800146
147 // Need to abandon the drawing manager first so all the render targets
148 // will be released/forgotten before they too are abandoned.
149 fDrawingManager->abandon();
150
bsalomon@google.com205d4602011-04-25 12:43:45 +0000151 // abandon first to so destructors
152 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800153 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700154
bsalomon6e2aad42016-04-01 11:54:31 -0700155 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
156
157 fBatchFontCache->freeAll();
bsalomon6e2aad42016-04-01 11:54:31 -0700158 fTextBlobCache->freeAll();
159}
160
161void GrContext::releaseResourcesAndAbandonContext() {
162 ASSERT_SINGLE_OWNER
163
164 fResourceProvider->abandon();
165
166 // Need to abandon the drawing manager first so all the render targets
167 // will be released/forgotten before they too are abandoned.
168 fDrawingManager->abandon();
169
170 // Release all resources in the backend 3D API.
171 fResourceCache->releaseAll();
172
173 fGpu->disconnect(GrGpu::DisconnectType::kCleanup);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000174
joshualitt7c3a2f82015-03-31 13:32:05 -0700175 fBatchFontCache->freeAll();
joshualitt26ffc002015-04-16 11:24:04 -0700176 fTextBlobCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000177}
178
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000179void GrContext::resetContext(uint32_t state) {
joshualitt1de610a2016-01-06 08:26:09 -0800180 ASSERT_SINGLE_OWNER
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000181 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000182}
183
184void GrContext::freeGpuResources() {
joshualitt1de610a2016-01-06 08:26:09 -0800185 ASSERT_SINGLE_OWNER
186
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000187 this->flush();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000188
joshualitt7c3a2f82015-03-31 13:32:05 -0700189 fBatchFontCache->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700190
191 fDrawingManager->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700192
193 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000194}
195
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000196void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800197 ASSERT_SINGLE_OWNER
198
bsalomon71cb0c22014-11-14 12:10:14 -0800199 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800200 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800201 }
202 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800203 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800204 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000205}
206
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000207////////////////////////////////////////////////////////////////////////////////
208
joshualitt0db6dfa2015-04-10 07:01:30 -0700209void GrContext::TextBlobCacheOverBudgetCB(void* data) {
210 SkASSERT(data);
Brian Osman11052242016-10-27 14:47:55 -0400211 // TextBlobs are drawn at the SkGpuDevice level, therefore they cannot rely on
212 // GrRenderTargetContext to perform a necessary flush. The solution is to move drawText calls
213 // to below the GrContext level, but this is not trivial because they call drawPath on
214 // SkGpuDevice.
joshualitt0db6dfa2015-04-10 07:01:30 -0700215 GrContext* context = reinterpret_cast<GrContext*>(data);
216 context->flush();
217}
218
bsalomon@google.com27847de2011-02-22 20:59:41 +0000219////////////////////////////////////////////////////////////////////////////////
220
bsalomonb77a9072016-09-07 10:02:04 -0700221void GrContext::flush() {
joshualitt1de610a2016-01-06 08:26:09 -0800222 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700223 RETURN_IF_ABANDONED
bsalomonb77a9072016-09-07 10:02:04 -0700224 fDrawingManager->flush();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000225}
226
bsalomon81beccc2014-10-13 12:32:55 -0700227bool sw_convert_to_premul(GrPixelConfig srcConfig, int width, int height, size_t inRowBytes,
228 const void* inPixels, size_t outRowBytes, void* outPixels) {
229 SkSrcPixelInfo srcPI;
brianosman396fcdb2016-07-22 06:26:11 -0700230 if (!GrPixelConfigToColorType(srcConfig, &srcPI.fColorType)) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000231 return false;
232 }
bsalomon81beccc2014-10-13 12:32:55 -0700233 srcPI.fAlphaType = kUnpremul_SkAlphaType;
234 srcPI.fPixels = inPixels;
235 srcPI.fRowBytes = inRowBytes;
236
237 SkDstPixelInfo dstPI;
238 dstPI.fColorType = srcPI.fColorType;
239 dstPI.fAlphaType = kPremul_SkAlphaType;
240 dstPI.fPixels = outPixels;
241 dstPI.fRowBytes = outRowBytes;
242
243 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000244}
245
bsalomon81beccc2014-10-13 12:32:55 -0700246bool GrContext::writeSurfacePixels(GrSurface* surface,
247 int left, int top, int width, int height,
248 GrPixelConfig srcConfig, const void* buffer, size_t rowBytes,
249 uint32_t pixelOpsFlags) {
joshualitt1de610a2016-01-06 08:26:09 -0800250 ASSERT_SINGLE_OWNER
joshualitt5f5a8d72015-02-25 14:09:45 -0800251 RETURN_FALSE_IF_ABANDONED
bsalomon6c6f6582015-09-10 08:12:46 -0700252 ASSERT_OWNED_RESOURCE(surface);
253 SkASSERT(surface);
joshualittbc907352016-01-13 06:45:40 -0800254 GR_AUDIT_TRAIL_AUTO_FRAME(&fAuditTrail, "GrContext::writeSurfacePixels");
bsalomon6c6f6582015-09-10 08:12:46 -0700255
256 this->testPMConversionsIfNecessary(pixelOpsFlags);
bsalomon81beccc2014-10-13 12:32:55 -0700257
bsalomone8d21e82015-07-16 08:23:13 -0700258 // 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 -0700259 // necessary and because GrGpu::getWritePixelsInfo requires it.
bsalomone8d21e82015-07-16 08:23:13 -0700260 if (!GrSurfacePriv::AdjustWritePixelParams(surface->width(), surface->height(),
261 GrBytesPerPixel(srcConfig), &left, &top, &width,
262 &height, &buffer, &rowBytes)) {
263 return false;
264 }
265
bsalomonf0674512015-07-28 13:26:15 -0700266 bool applyPremulToSrc = false;
bsalomon81beccc2014-10-13 12:32:55 -0700267 if (kUnpremul_PixelOpsFlag & pixelOpsFlags) {
268 if (!GrPixelConfigIs8888(srcConfig)) {
269 return false;
270 }
bsalomonf0674512015-07-28 13:26:15 -0700271 applyPremulToSrc = true;
272 }
bsalomon636e8022015-07-29 06:08:46 -0700273
274 GrGpu::DrawPreference drawPreference = GrGpu::kNoDraw_DrawPreference;
275 // Don't prefer to draw for the conversion (and thereby access a texture from the cache) when
276 // we've already determined that there isn't a roundtrip preserving conversion processor pair.
277 if (applyPremulToSrc && !this->didFailPMUPMConversionTest()) {
278 drawPreference = GrGpu::kCallerPrefersDraw_DrawPreference;
279 }
280
bsalomonf0674512015-07-28 13:26:15 -0700281 GrGpu::WritePixelTempDrawInfo tempDrawInfo;
cblumeed828002016-02-16 13:00:01 -0800282 if (!fGpu->getWritePixelsInfo(surface, width, height, srcConfig, &drawPreference,
bsalomonf0674512015-07-28 13:26:15 -0700283 &tempDrawInfo)) {
284 return false;
285 }
286
287 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && surface->surfacePriv().hasPendingIO()) {
288 this->flush();
289 }
290
291 SkAutoTUnref<GrTexture> tempTexture;
292 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
bsalomoneae62002015-07-31 13:59:30 -0700293 tempTexture.reset(
294 this->textureProvider()->createApproxTexture(tempDrawInfo.fTempSurfaceDesc));
bsalomonf0674512015-07-28 13:26:15 -0700295 if (!tempTexture && GrGpu::kRequireDraw_DrawPreference == drawPreference) {
296 return false;
297 }
298 }
299
300 // temp buffer for doing sw premul conversion, if needed.
301 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
302 if (tempTexture) {
bungeman06ca8ec2016-06-09 08:01:03 -0700303 sk_sp<GrFragmentProcessor> fp;
bsalomonf0674512015-07-28 13:26:15 -0700304 SkMatrix textureMatrix;
305 textureMatrix.setIDiv(tempTexture->width(), tempTexture->height());
bsalomonf0674512015-07-28 13:26:15 -0700306 if (applyPremulToSrc) {
bungeman06ca8ec2016-06-09 08:01:03 -0700307 fp = this->createUPMToPMEffect(tempTexture, tempDrawInfo.fSwizzle, textureMatrix);
bsalomonf0674512015-07-28 13:26:15 -0700308 // If premultiplying was the only reason for the draw, fall back to a straight write.
309 if (!fp) {
310 if (GrGpu::kCallerPrefersDraw_DrawPreference == drawPreference) {
halcanary96fcdcc2015-08-27 07:41:13 -0700311 tempTexture.reset(nullptr);
bsalomonf0674512015-07-28 13:26:15 -0700312 }
313 } else {
314 applyPremulToSrc = false;
315 }
316 }
317 if (tempTexture) {
318 if (!fp) {
bungeman06ca8ec2016-06-09 08:01:03 -0700319 fp = GrConfigConversionEffect::Make(tempTexture, tempDrawInfo.fSwizzle,
320 GrConfigConversionEffect::kNone_PMConversion,
321 textureMatrix);
bsalomonf0674512015-07-28 13:26:15 -0700322 if (!fp) {
323 return false;
324 }
325 }
326 GrRenderTarget* renderTarget = surface->asRenderTarget();
327 SkASSERT(renderTarget);
328 if (tempTexture->surfacePriv().hasPendingIO()) {
329 this->flush();
330 }
331 if (applyPremulToSrc) {
332 size_t tmpRowBytes = 4 * width;
333 tmpPixels.reset(width * height);
334 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
335 tmpPixels.get())) {
336 return false;
337 }
338 rowBytes = tmpRowBytes;
339 buffer = tmpPixels.get();
340 applyPremulToSrc = false;
341 }
bsalomon6cb3cbe2015-07-30 07:34:27 -0700342 if (!fGpu->writePixels(tempTexture, 0, 0, width, height,
bsalomon6c9cd552016-01-22 07:17:34 -0800343 tempDrawInfo.fWriteConfig, buffer,
bsalomon6cb3cbe2015-07-30 07:34:27 -0700344 rowBytes)) {
bsalomonf0674512015-07-28 13:26:15 -0700345 return false;
346 }
347 SkMatrix matrix;
348 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
brianosmandfe4f2e2016-07-21 13:28:36 -0700349 // TODO: Need to decide the semantics of this function for color spaces. Do we support
350 // conversion from a passed-in color space? For now, specifying nullptr means that this
351 // path will do no conversion, so it will match the behavior of the non-draw path.
Brian Osman11052242016-10-27 14:47:55 -0400352 sk_sp<GrRenderTargetContext> renderTargetContext(
353 this->contextPriv().makeWrappedRenderTargetContext(sk_ref_sp(renderTarget),
354 nullptr));
355 if (!renderTargetContext) {
bsalomonf0674512015-07-28 13:26:15 -0700356 return false;
357 }
egdanielc4b72722015-11-23 13:20:41 -0800358 GrPaint paint;
bungeman06ca8ec2016-06-09 08:01:03 -0700359 paint.addColorFragmentProcessor(std::move(fp));
reed374772b2016-10-05 17:33:02 -0700360 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
brianosmana167e742016-05-24 06:18:48 -0700361 paint.setAllowSRGBInputs(true);
bsalomonf0674512015-07-28 13:26:15 -0700362 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
Brian Osman11052242016-10-27 14:47:55 -0400363 renderTargetContext->drawRect(GrNoClip(), paint, matrix, rect, nullptr);
bsalomonf0674512015-07-28 13:26:15 -0700364
365 if (kFlushWrites_PixelOp & pixelOpsFlags) {
366 this->flushSurfaceWrites(surface);
367 }
368 }
369 }
370 if (!tempTexture) {
bsalomonf0674512015-07-28 13:26:15 -0700371 if (applyPremulToSrc) {
bsalomon81beccc2014-10-13 12:32:55 -0700372 size_t tmpRowBytes = 4 * width;
373 tmpPixels.reset(width * height);
374 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
375 tmpPixels.get())) {
376 return false;
377 }
378 rowBytes = tmpRowBytes;
379 buffer = tmpPixels.get();
bsalomonf0674512015-07-28 13:26:15 -0700380 applyPremulToSrc = false;
bsalomon81beccc2014-10-13 12:32:55 -0700381 }
bsalomon6cb3cbe2015-07-30 07:34:27 -0700382 return fGpu->writePixels(surface, left, top, width, height, srcConfig, buffer, rowBytes);
bsalomon81beccc2014-10-13 12:32:55 -0700383 }
bsalomon81beccc2014-10-13 12:32:55 -0700384 return true;
385}
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000386
bsalomone8d21e82015-07-16 08:23:13 -0700387bool GrContext::readSurfacePixels(GrSurface* src,
388 int left, int top, int width, int height,
389 GrPixelConfig dstConfig, void* buffer, size_t rowBytes,
390 uint32_t flags) {
joshualitt1de610a2016-01-06 08:26:09 -0800391 ASSERT_SINGLE_OWNER
joshualitt5f5a8d72015-02-25 14:09:45 -0800392 RETURN_FALSE_IF_ABANDONED
bsalomone8d21e82015-07-16 08:23:13 -0700393 ASSERT_OWNED_RESOURCE(src);
394 SkASSERT(src);
joshualittbc907352016-01-13 06:45:40 -0800395 GR_AUDIT_TRAIL_AUTO_FRAME(&fAuditTrail, "GrContext::readSurfacePixels");
bsalomon32ab2602015-09-09 18:57:49 -0700396
bsalomon6c6f6582015-09-10 08:12:46 -0700397 this->testPMConversionsIfNecessary(flags);
398 SkAutoMutexAcquire ama(fReadPixelsMutex);
399
bsalomone8d21e82015-07-16 08:23:13 -0700400 // Adjust the params so that if we wind up using an intermediate surface we've already done
401 // all the trimming and the temporary can be the min size required.
402 if (!GrSurfacePriv::AdjustReadPixelParams(src->width(), src->height(),
403 GrBytesPerPixel(dstConfig), &left,
404 &top, &width, &height, &buffer, &rowBytes)) {
405 return false;
406 }
407
408 if (!(kDontFlush_PixelOpsFlag & flags) && src->surfacePriv().hasPendingWrite()) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000409 this->flush();
410 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000411
bsalomone8d21e82015-07-16 08:23:13 -0700412 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
bsalomon@google.com9c680582013-02-06 18:17:50 +0000413 if (unpremul && !GrPixelConfigIs8888(dstConfig)) {
bsalomon39826022015-07-23 08:07:21 -0700414 // The unpremul flag is only allowed for 8888 configs.
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000415 return false;
416 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000417
bsalomon636e8022015-07-29 06:08:46 -0700418 GrGpu::DrawPreference drawPreference = GrGpu::kNoDraw_DrawPreference;
419 // Don't prefer to draw for the conversion (and thereby access a texture from the cache) when
420 // we've already determined that there isn't a roundtrip preserving conversion processor pair.
421 if (unpremul && !this->didFailPMUPMConversionTest()) {
422 drawPreference = GrGpu::kCallerPrefersDraw_DrawPreference;
423 }
424
bsalomon39826022015-07-23 08:07:21 -0700425 GrGpu::ReadPixelTempDrawInfo tempDrawInfo;
426 if (!fGpu->getReadPixelsInfo(src, width, height, rowBytes, dstConfig, &drawPreference,
427 &tempDrawInfo)) {
428 return false;
429 }
bsalomon191bcc02014-11-14 11:31:13 -0800430
bsalomon6cb3cbe2015-07-30 07:34:27 -0700431 SkAutoTUnref<GrSurface> surfaceToRead(SkRef(src));
bsalomon39826022015-07-23 08:07:21 -0700432 bool didTempDraw = false;
433 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
bsalomonb117ff12016-07-19 07:24:40 -0700434 if (SkBackingFit::kExact == tempDrawInfo.fTempSurfaceFit) {
bsalomon39826022015-07-23 08:07:21 -0700435 // We only respect this when the entire src is being read. Otherwise we can trigger too
436 // many odd ball texture sizes and trash the cache.
bsalomoneae62002015-07-31 13:59:30 -0700437 if (width != src->width() || height != src->height()) {
bsalomonb117ff12016-07-19 07:24:40 -0700438 tempDrawInfo.fTempSurfaceFit= SkBackingFit::kApprox;
bsalomon39826022015-07-23 08:07:21 -0700439 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000440 }
brianosmandfe4f2e2016-07-21 13:28:36 -0700441 // TODO: Need to decide the semantics of this function for color spaces. Do we support
442 // conversion to a passed-in color space? For now, specifying nullptr means that this
443 // path will do no conversion, so it will match the behavior of the non-draw path.
Brian Osman693a5402016-10-27 15:13:22 -0400444 sk_sp<GrRenderTargetContext> tempRTC = this->makeRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400445 tempDrawInfo.fTempSurfaceFit,
bsalomonb117ff12016-07-19 07:24:40 -0700446 tempDrawInfo.fTempSurfaceDesc.fWidth,
447 tempDrawInfo.fTempSurfaceDesc.fHeight,
448 tempDrawInfo.fTempSurfaceDesc.fConfig,
brianosmandfe4f2e2016-07-21 13:28:36 -0700449 nullptr,
bsalomonb117ff12016-07-19 07:24:40 -0700450 tempDrawInfo.fTempSurfaceDesc.fSampleCnt,
451 tempDrawInfo.fTempSurfaceDesc.fOrigin);
Brian Osman693a5402016-10-27 15:13:22 -0400452 if (tempRTC) {
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000453 SkMatrix textureMatrix;
bsalomon39826022015-07-23 08:07:21 -0700454 textureMatrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000455 textureMatrix.postIDiv(src->width(), src->height());
bungeman06ca8ec2016-06-09 08:01:03 -0700456 sk_sp<GrFragmentProcessor> fp;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000457 if (unpremul) {
bungeman06ca8ec2016-06-09 08:01:03 -0700458 fp = this->createPMToUPMEffect(src->asTexture(), tempDrawInfo.fSwizzle,
459 textureMatrix);
joshualittb0a8a372014-09-23 09:50:21 -0700460 if (fp) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000461 unpremul = false; // we no longer need to do this on CPU after the read back.
bsalomon39826022015-07-23 08:07:21 -0700462 } else if (GrGpu::kCallerPrefersDraw_DrawPreference == drawPreference) {
463 // We only wanted to do the draw in order to perform the unpremul so don't
464 // bother.
Brian Osman693a5402016-10-27 15:13:22 -0400465 tempRTC.reset(nullptr);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000466 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000467 }
Brian Osman693a5402016-10-27 15:13:22 -0400468 if (!fp && tempRTC) {
bungeman06ca8ec2016-06-09 08:01:03 -0700469 fp = GrConfigConversionEffect::Make(src->asTexture(), tempDrawInfo.fSwizzle,
470 GrConfigConversionEffect::kNone_PMConversion,
471 textureMatrix);
bsalomon39826022015-07-23 08:07:21 -0700472 }
473 if (fp) {
egdanielc4b72722015-11-23 13:20:41 -0800474 GrPaint paint;
bungeman06ca8ec2016-06-09 08:01:03 -0700475 paint.addColorFragmentProcessor(std::move(fp));
reed374772b2016-10-05 17:33:02 -0700476 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
brianosmana167e742016-05-24 06:18:48 -0700477 paint.setAllowSRGBInputs(true);
bsalomon39826022015-07-23 08:07:21 -0700478 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
Brian Osman693a5402016-10-27 15:13:22 -0400479 tempRTC->drawRect(GrNoClip(), paint, SkMatrix::I(), rect, nullptr);
480 surfaceToRead.reset(tempRTC->asTexture().release());
bsalomon39826022015-07-23 08:07:21 -0700481 left = 0;
482 top = 0;
483 didTempDraw = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000484 }
bsalomon@google.com0342a852012-08-20 19:22:38 +0000485 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000486 }
joshualitt5c55fef2014-10-31 14:04:35 -0700487
bsalomon39826022015-07-23 08:07:21 -0700488 if (GrGpu::kRequireDraw_DrawPreference == drawPreference && !didTempDraw) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000489 return false;
490 }
bsalomon39826022015-07-23 08:07:21 -0700491 GrPixelConfig configToRead = dstConfig;
492 if (didTempDraw) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700493 this->flushSurfaceWrites(surfaceToRead);
bsalomon6c9cd552016-01-22 07:17:34 -0800494 configToRead = tempDrawInfo.fReadConfig;
bsalomon39826022015-07-23 08:07:21 -0700495 }
bsalomon6cb3cbe2015-07-30 07:34:27 -0700496 if (!fGpu->readPixels(surfaceToRead, left, top, width, height, configToRead, buffer,
497 rowBytes)) {
bsalomon39826022015-07-23 08:07:21 -0700498 return false;
499 }
500
501 // Perform umpremul conversion if we weren't able to perform it as a draw.
502 if (unpremul) {
reed@google.com7111d462014-03-25 16:20:24 +0000503 SkDstPixelInfo dstPI;
brianosman396fcdb2016-07-22 06:26:11 -0700504 if (!GrPixelConfigToColorType(dstConfig, &dstPI.fColorType)) {
reed@google.com7111d462014-03-25 16:20:24 +0000505 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000506 }
reed@google.com7111d462014-03-25 16:20:24 +0000507 dstPI.fAlphaType = kUnpremul_SkAlphaType;
508 dstPI.fPixels = buffer;
509 dstPI.fRowBytes = rowBytes;
510
511 SkSrcPixelInfo srcPI;
bsalomon39826022015-07-23 08:07:21 -0700512 srcPI.fColorType = dstPI.fColorType;
reed@google.com7111d462014-03-25 16:20:24 +0000513 srcPI.fAlphaType = kPremul_SkAlphaType;
514 srcPI.fPixels = buffer;
515 srcPI.fRowBytes = rowBytes;
516
517 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000518 }
519 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000520}
521
bsalomonc49e8682015-06-30 11:37:35 -0700522void GrContext::prepareSurfaceForExternalIO(GrSurface* surface) {
joshualitt1de610a2016-01-06 08:26:09 -0800523 ASSERT_SINGLE_OWNER
joshualitt5f5a8d72015-02-25 14:09:45 -0800524 RETURN_IF_ABANDONED
bsalomon87a94eb2014-11-03 14:28:32 -0800525 SkASSERT(surface);
526 ASSERT_OWNED_RESOURCE(surface);
bsalomon6a2b1942016-09-08 11:28:59 -0700527 fDrawingManager->prepareSurfaceForExternalIO(surface);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000528}
529
bsalomonb8fea972016-02-16 07:34:17 -0800530bool GrContext::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
531 const SkIPoint& dstPoint) {
joshualitt1de610a2016-01-06 08:26:09 -0800532 ASSERT_SINGLE_OWNER
bsalomonb8fea972016-02-16 07:34:17 -0800533 RETURN_FALSE_IF_ABANDONED
joshualittbc907352016-01-13 06:45:40 -0800534 GR_AUDIT_TRAIL_AUTO_FRAME(&fAuditTrail, "GrContext::copySurface");
535
robertphillipsea461502015-05-26 11:38:03 -0700536 if (!src || !dst) {
bsalomonb8fea972016-02-16 07:34:17 -0800537 return false;
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000538 }
bsalomone3d4bf22014-09-23 09:15:03 -0700539 ASSERT_OWNED_RESOURCE(src);
junov2bb52102014-09-29 10:18:59 -0700540 ASSERT_OWNED_RESOURCE(dst);
Brian Salomon34a98952014-09-24 11:41:24 -0400541
robertphillipsea461502015-05-26 11:38:03 -0700542 if (!dst->asRenderTarget()) {
bsalomonb8fea972016-02-16 07:34:17 -0800543 SkIRect clippedSrcRect;
544 SkIPoint clippedDstPoint;
545 if (!GrCopySurfaceBatch::ClipSrcRectAndDstPoint(dst, src, srcRect, dstPoint,
546 &clippedSrcRect, &clippedDstPoint)) {
547 return false;
548 }
Brian Osman11052242016-10-27 14:47:55 -0400549 // If we don't have an RT for the dst then we won't have a GrRenderTargetContext to insert
bsalomonb8fea972016-02-16 07:34:17 -0800550 // the copy surface into. In the future we plan to have a more limited Context type
Brian Osman11052242016-10-27 14:47:55 -0400551 // (GrCopyContext?) that has the subset of GrRenderTargetContext operations that should be
bsalomonb8fea972016-02-16 07:34:17 -0800552 // allowed on textures that aren't render targets.
553 // For now we just flush any writes to the src and issue an immediate copy to the dst.
554 src->flushWrites();
555 return fGpu->copySurface(dst, src, clippedSrcRect, clippedDstPoint);
robertphillipsea461502015-05-26 11:38:03 -0700556 }
Brian Osman11052242016-10-27 14:47:55 -0400557 sk_sp<GrRenderTargetContext> renderTargetContext(
558 this->contextPriv().makeWrappedRenderTargetContext(sk_ref_sp(dst->asRenderTarget()),
559 nullptr));
560 if (!renderTargetContext) {
bsalomonb8fea972016-02-16 07:34:17 -0800561 return false;
bsalomonf80bfed2014-10-07 05:56:02 -0700562 }
kjlubick0eed9452016-02-11 12:05:24 -0800563
Brian Osman11052242016-10-27 14:47:55 -0400564 if (!renderTargetContext->copySurface(src, srcRect, dstPoint)) {
bsalomonb8fea972016-02-16 07:34:17 -0800565 return false;
kjlubick0eed9452016-02-11 12:05:24 -0800566 }
bsalomonb8fea972016-02-16 07:34:17 -0800567 return true;
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000568}
569
bsalomonf80bfed2014-10-07 05:56:02 -0700570void GrContext::flushSurfaceWrites(GrSurface* surface) {
joshualitt1de610a2016-01-06 08:26:09 -0800571 ASSERT_SINGLE_OWNER
joshualitt5f5a8d72015-02-25 14:09:45 -0800572 RETURN_IF_ABANDONED
bsalomonf80bfed2014-10-07 05:56:02 -0700573 if (surface->surfacePriv().hasPendingWrite()) {
574 this->flush();
575 }
576}
577
ajuma95243eb2016-08-24 08:19:02 -0700578void GrContext::flushSurfaceIO(GrSurface* surface) {
579 ASSERT_SINGLE_OWNER
580 RETURN_IF_ABANDONED
581 if (surface->surfacePriv().hasPendingIO()) {
582 this->flush();
583 }
584}
585
bsalomon@google.com27847de2011-02-22 20:59:41 +0000586////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000587int GrContext::getRecommendedSampleCount(GrPixelConfig config,
588 SkScalar dpi) const {
joshualitt1de610a2016-01-06 08:26:09 -0800589 ASSERT_SINGLE_OWNER
590
bsalomon76228632015-05-29 08:02:10 -0700591 if (!this->caps()->isConfigRenderable(config, true)) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000592 return 0;
593 }
594 int chosenSampleCount = 0;
jvanverthe9c0fc62015-04-29 11:18:05 -0700595 if (fGpu->caps()->shaderCaps()->pathRenderingSupport()) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000596 if (dpi >= 250.0f) {
597 chosenSampleCount = 4;
598 } else {
599 chosenSampleCount = 16;
600 }
601 }
egdanieleed519e2016-01-15 11:36:18 -0800602 return chosenSampleCount <= fGpu->caps()->maxSampleCount() ? chosenSampleCount : 0;
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000603}
604
Brian Osman11052242016-10-27 14:47:55 -0400605sk_sp<GrRenderTargetContext> GrContextPriv::makeWrappedRenderTargetContext(
606 sk_sp<GrRenderTarget> rt,
607 sk_sp<SkColorSpace> colorSpace,
608 const SkSurfaceProps* surfaceProps) {
robertphillips4fd74ae2016-08-03 14:26:53 -0700609 ASSERT_SINGLE_OWNER_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400610 return this->drawingManager()->makeRenderTargetContext(std::move(rt), std::move(colorSpace),
611 surfaceProps);
robertphillips4fd74ae2016-08-03 14:26:53 -0700612}
robertphillips77a2e522015-10-17 07:43:27 -0700613
Brian Osman11052242016-10-27 14:47:55 -0400614sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureRenderTargetContext(
615 const GrBackendTextureDesc& desc,
616 sk_sp<SkColorSpace> colorSpace,
617 const SkSurfaceProps* props,
618 GrWrapOwnership ownership) {
robertphillips4fd74ae2016-08-03 14:26:53 -0700619 ASSERT_SINGLE_OWNER_PRIV
620 SkASSERT(desc.fFlags & kRenderTarget_GrBackendTextureFlag);
621
622 sk_sp<GrSurface> surface(fContext->textureProvider()->wrapBackendTexture(desc, ownership));
623 if (!surface) {
624 return nullptr;
625 }
626
Brian Osman11052242016-10-27 14:47:55 -0400627 return this->drawingManager()->makeRenderTargetContext(sk_ref_sp(surface->asRenderTarget()),
628 std::move(colorSpace), props);
robertphillips4fd74ae2016-08-03 14:26:53 -0700629}
630
Brian Osman11052242016-10-27 14:47:55 -0400631sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendRenderTargetRenderTargetContext(
robertphillips4fd74ae2016-08-03 14:26:53 -0700632 const GrBackendRenderTargetDesc& desc,
633 sk_sp<SkColorSpace> colorSpace,
634 const SkSurfaceProps* surfaceProps) {
635 ASSERT_SINGLE_OWNER_PRIV
636
637 sk_sp<GrRenderTarget> rt(fContext->textureProvider()->wrapBackendRenderTarget(desc));
638 if (!rt) {
639 return nullptr;
640 }
641
Brian Osman11052242016-10-27 14:47:55 -0400642 return this->drawingManager()->makeRenderTargetContext(std::move(rt), std::move(colorSpace),
643 surfaceProps);
robertphillips4fd74ae2016-08-03 14:26:53 -0700644}
645
Brian Osman11052242016-10-27 14:47:55 -0400646sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureAsRenderTargetRenderTargetContext(
egdaniela95d46b2016-08-15 08:06:29 -0700647 const GrBackendTextureDesc& desc,
robertphillips4fd74ae2016-08-03 14:26:53 -0700648 sk_sp<SkColorSpace> colorSpace,
649 const SkSurfaceProps* surfaceProps) {
650 ASSERT_SINGLE_OWNER_PRIV
651 SkASSERT(desc.fFlags & kRenderTarget_GrBackendTextureFlag);
652
653 sk_sp<GrSurface> surface(fContext->resourceProvider()->wrapBackendTextureAsRenderTarget(desc));
654 if (!surface) {
655 return nullptr;
656 }
657
Brian Osman11052242016-10-27 14:47:55 -0400658 return this->drawingManager()->makeRenderTargetContext(sk_ref_sp(surface->asRenderTarget()),
659 std::move(colorSpace), surfaceProps);
robertphillips77a2e522015-10-17 07:43:27 -0700660}
661
robertphillips48fde9c2016-09-06 05:20:20 -0700662static inline GrPixelConfig GrPixelConfigFallback(GrPixelConfig config) {
663 static const GrPixelConfig kFallback[] = {
664 kUnknown_GrPixelConfig, // kUnknown_GrPixelConfig
665 kRGBA_8888_GrPixelConfig, // kAlpha_8_GrPixelConfig
666 kUnknown_GrPixelConfig, // kIndex_8_GrPixelConfig
667 kRGBA_8888_GrPixelConfig, // kRGB_565_GrPixelConfig
668 kRGBA_8888_GrPixelConfig, // kRGBA_4444_GrPixelConfig
669 kUnknown_GrPixelConfig, // kRGBA_8888_GrPixelConfig
670 kRGBA_8888_GrPixelConfig, // kBGRA_8888_GrPixelConfig
671 kUnknown_GrPixelConfig, // kSRGBA_8888_GrPixelConfig
672 kSRGBA_8888_GrPixelConfig, // kSBGRA_8888_GrPixelConfig
673 kUnknown_GrPixelConfig, // kETC1_GrPixelConfig
674 kUnknown_GrPixelConfig, // kLATC_GrPixelConfig
675 kUnknown_GrPixelConfig, // kR11_EAC_GrPixelConfig
676 kUnknown_GrPixelConfig, // kASTC_12x12_GrPixelConfig
677 kUnknown_GrPixelConfig, // kRGBA_float_GrPixelConfig
678 kRGBA_half_GrPixelConfig, // kAlpha_half_GrPixelConfig
679 kUnknown_GrPixelConfig, // kRGBA_half_GrPixelConfig
680 };
681 return kFallback[config];
682
683 GR_STATIC_ASSERT(0 == kUnknown_GrPixelConfig);
684 GR_STATIC_ASSERT(1 == kAlpha_8_GrPixelConfig);
685 GR_STATIC_ASSERT(2 == kIndex_8_GrPixelConfig);
686 GR_STATIC_ASSERT(3 == kRGB_565_GrPixelConfig);
687 GR_STATIC_ASSERT(4 == kRGBA_4444_GrPixelConfig);
688 GR_STATIC_ASSERT(5 == kRGBA_8888_GrPixelConfig);
689 GR_STATIC_ASSERT(6 == kBGRA_8888_GrPixelConfig);
690 GR_STATIC_ASSERT(7 == kSRGBA_8888_GrPixelConfig);
691 GR_STATIC_ASSERT(8 == kSBGRA_8888_GrPixelConfig);
692 GR_STATIC_ASSERT(9 == kETC1_GrPixelConfig);
693 GR_STATIC_ASSERT(10 == kLATC_GrPixelConfig);
694 GR_STATIC_ASSERT(11 == kR11_EAC_GrPixelConfig);
695 GR_STATIC_ASSERT(12 == kASTC_12x12_GrPixelConfig);
696 GR_STATIC_ASSERT(13 == kRGBA_float_GrPixelConfig);
697 GR_STATIC_ASSERT(14 == kAlpha_half_GrPixelConfig);
698 GR_STATIC_ASSERT(15 == kRGBA_half_GrPixelConfig);
699 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kFallback) == kGrPixelConfigCnt);
700}
701
Brian Osman11052242016-10-27 14:47:55 -0400702sk_sp<GrRenderTargetContext> GrContext::makeRenderTargetContextWithFallback(
703 SkBackingFit fit,
704 int width, int height,
705 GrPixelConfig config,
706 sk_sp<SkColorSpace> colorSpace,
707 int sampleCnt,
708 GrSurfaceOrigin origin,
709 const SkSurfaceProps* surfaceProps,
710 SkBudgeted budgeted) {
robertphillips48fde9c2016-09-06 05:20:20 -0700711 if (!this->caps()->isConfigRenderable(config, sampleCnt > 0)) {
712 config = GrPixelConfigFallback(config);
713 }
714
Brian Osman11052242016-10-27 14:47:55 -0400715 return this->makeRenderTargetContext(fit, width, height, config, std::move(colorSpace),
716 sampleCnt, origin, surfaceProps, budgeted);
robertphillips48fde9c2016-09-06 05:20:20 -0700717}
718
Brian Osman11052242016-10-27 14:47:55 -0400719sk_sp<GrRenderTargetContext> GrContext::makeRenderTargetContext(SkBackingFit fit,
720 int width, int height,
721 GrPixelConfig config,
722 sk_sp<SkColorSpace> colorSpace,
723 int sampleCnt,
724 GrSurfaceOrigin origin,
725 const SkSurfaceProps* surfaceProps,
726 SkBudgeted budgeted) {
robertphillips48fde9c2016-09-06 05:20:20 -0700727 if (!this->caps()->isConfigRenderable(config, sampleCnt > 0)) {
728 return nullptr;
729 }
730
robertphillipsd4c741e2016-04-28 09:55:15 -0700731 GrSurfaceDesc desc;
732 desc.fFlags = kRenderTarget_GrSurfaceFlag;
733 desc.fOrigin = origin;
734 desc.fWidth = width;
735 desc.fHeight = height;
736 desc.fConfig = config;
737 desc.fSampleCnt = sampleCnt;
738
739 sk_sp<GrTexture> tex;
robertphillips76948d42016-05-04 12:47:41 -0700740 if (SkBackingFit::kExact == fit) {
robertphillipsca6eafc2016-05-17 09:57:46 -0700741 tex.reset(this->textureProvider()->createTexture(desc, budgeted));
robertphillipsd4c741e2016-04-28 09:55:15 -0700742 } else {
743 tex.reset(this->textureProvider()->createApproxTexture(desc));
744 }
745 if (!tex) {
746 return nullptr;
747 }
748
Brian Osman11052242016-10-27 14:47:55 -0400749 sk_sp<GrRenderTargetContext> renderTargetContext(
750 this->contextPriv().makeWrappedRenderTargetContext(sk_ref_sp(tex->asRenderTarget()),
robertphillips6738c702016-07-27 12:13:51 -0700751 std::move(colorSpace), surfaceProps));
Brian Osman11052242016-10-27 14:47:55 -0400752 if (!renderTargetContext) {
robertphillipsd4c741e2016-04-28 09:55:15 -0700753 return nullptr;
754 }
755
Brian Osman11052242016-10-27 14:47:55 -0400756 return renderTargetContext;
robertphillipsd4c741e2016-04-28 09:55:15 -0700757}
758
joshualitt1de610a2016-01-06 08:26:09 -0800759bool GrContext::abandoned() const {
760 ASSERT_SINGLE_OWNER
robertphillips7761d612016-05-16 09:14:53 -0700761 return fDrawingManager->wasAbandoned();
robertphillips77a2e522015-10-17 07:43:27 -0700762}
763
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000764namespace {
765void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
766 GrConfigConversionEffect::PMConversion pmToUPM;
767 GrConfigConversionEffect::PMConversion upmToPM;
768 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM);
769 *pmToUPMValue = pmToUPM;
770 *upmToPMValue = upmToPM;
771}
772}
773
bsalomon6c6f6582015-09-10 08:12:46 -0700774void GrContext::testPMConversionsIfNecessary(uint32_t flags) {
joshualitt1de610a2016-01-06 08:26:09 -0800775 ASSERT_SINGLE_OWNER
bsalomon6c6f6582015-09-10 08:12:46 -0700776 if (SkToBool(kUnpremul_PixelOpsFlag & flags)) {
777 SkAutoMutexAcquire ama(fTestPMConversionsMutex);
778 if (!fDidTestPMConversions) {
779 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
780 fDidTestPMConversions = true;
781 }
782 }
783}
784
bungeman06ca8ec2016-06-09 08:01:03 -0700785sk_sp<GrFragmentProcessor> GrContext::createPMToUPMEffect(GrTexture* texture,
bsalomon6c9cd552016-01-22 07:17:34 -0800786 const GrSwizzle& swizzle,
bsalomon6c6f6582015-09-10 08:12:46 -0700787 const SkMatrix& matrix) const {
joshualitt1de610a2016-01-06 08:26:09 -0800788 ASSERT_SINGLE_OWNER
bsalomon6c6f6582015-09-10 08:12:46 -0700789 // We should have already called this->testPMConversionsIfNecessary().
790 SkASSERT(fDidTestPMConversions);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000791 GrConfigConversionEffect::PMConversion pmToUPM =
792 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
793 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
bungeman06ca8ec2016-06-09 08:01:03 -0700794 return GrConfigConversionEffect::Make(texture, swizzle, pmToUPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000795 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700796 return nullptr;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000797 }
798}
799
bungeman06ca8ec2016-06-09 08:01:03 -0700800sk_sp<GrFragmentProcessor> GrContext::createUPMToPMEffect(GrTexture* texture,
bsalomon6c9cd552016-01-22 07:17:34 -0800801 const GrSwizzle& swizzle,
bsalomon6c6f6582015-09-10 08:12:46 -0700802 const SkMatrix& matrix) const {
joshualitt1de610a2016-01-06 08:26:09 -0800803 ASSERT_SINGLE_OWNER
bsalomon6c6f6582015-09-10 08:12:46 -0700804 // We should have already called this->testPMConversionsIfNecessary().
805 SkASSERT(fDidTestPMConversions);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000806 GrConfigConversionEffect::PMConversion upmToPM =
807 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion);
808 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) {
bungeman06ca8ec2016-06-09 08:01:03 -0700809 return GrConfigConversionEffect::Make(texture, swizzle, upmToPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000810 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700811 return nullptr;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000812 }
813}
814
bsalomon636e8022015-07-29 06:08:46 -0700815bool GrContext::didFailPMUPMConversionTest() const {
joshualitt1de610a2016-01-06 08:26:09 -0800816 ASSERT_SINGLE_OWNER
bsalomon6c6f6582015-09-10 08:12:46 -0700817 // We should have already called this->testPMConversionsIfNecessary().
818 SkASSERT(fDidTestPMConversions);
bsalomon636e8022015-07-29 06:08:46 -0700819 // The PM<->UPM tests fail or succeed together so we only need to check one.
bsalomon6c6f6582015-09-10 08:12:46 -0700820 return GrConfigConversionEffect::kNone_PMConversion == fPMToUPMConversion;
bsalomon636e8022015-07-29 06:08:46 -0700821}
822
bsalomon37f9a262015-02-02 13:00:10 -0800823//////////////////////////////////////////////////////////////////////////////
824
825void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800826 ASSERT_SINGLE_OWNER
bsalomon37f9a262015-02-02 13:00:10 -0800827 if (maxTextures) {
bsalomon0ea80f42015-02-11 10:49:59 -0800828 *maxTextures = fResourceCache->getMaxResourceCount();
bsalomon37f9a262015-02-02 13:00:10 -0800829 }
830 if (maxTextureBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800831 *maxTextureBytes = fResourceCache->getMaxResourceBytes();
bsalomon37f9a262015-02-02 13:00:10 -0800832 }
833}
834
835void GrContext::setResourceCacheLimits(int maxTextures, size_t maxTextureBytes) {
joshualitt1de610a2016-01-06 08:26:09 -0800836 ASSERT_SINGLE_OWNER
bsalomon0ea80f42015-02-11 10:49:59 -0800837 fResourceCache->setLimits(maxTextures, maxTextureBytes);
bsalomon37f9a262015-02-02 13:00:10 -0800838}
839
ericrk0a5fa482015-09-15 14:16:10 -0700840//////////////////////////////////////////////////////////////////////////////
841
842void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -0800843 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -0700844 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
845}