blob: db8863fca0c4497fd30d244159d532ccb0d84477 [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"
Robert Phillipsc7635fa2016-10-28 13:25:24 -040015#include "GrRenderTargetProxy.h"
robertphillips@google.com72176b22012-05-23 13:19:12 +000016#include "GrSoftwarePathRenderer.h"
Brian Osman45580d32016-11-23 09:37:01 -050017#include "GrSurfaceContext.h"
bsalomonafbf2d62014-09-30 12:18:44 -070018#include "GrSurfacePriv.h"
Robert Phillips757914d2017-01-25 15:48:30 -050019#include "GrSurfaceProxyPriv.h"
Brian Osman45580d32016-11-23 09:37:01 -050020#include "GrTextureContext.h"
robertphillips3dc6ae52015-10-20 09:54:32 -070021
bsalomon81beccc2014-10-13 12:32:55 -070022#include "SkConfig8888.h"
bsalomonf276ac52015-10-09 13:36:42 -070023#include "SkGrPriv.h"
joshualitt74417822015-08-07 11:42:16 -070024
joshualitt5478d422014-11-14 16:00:38 -080025#include "effects/GrConfigConversionEffect.h"
joshualitte8042922015-12-11 06:11:21 -080026#include "text/GrTextBlobCache.h"
joshualitt5478d422014-11-14 16:00:38 -080027
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000028#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
joshualitt1de610a2016-01-06 08:26:09 -080029#define ASSERT_SINGLE_OWNER \
30 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fSingleOwner);)
robertphillips4fd74ae2016-08-03 14:26:53 -070031#define ASSERT_SINGLE_OWNER_PRIV \
32 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fContext->fSingleOwner);)
robertphillips7761d612016-05-16 09:14:53 -070033#define RETURN_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return; }
34#define RETURN_FALSE_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return false; }
35#define RETURN_NULL_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return nullptr; }
bsalomon@google.combc4b6542011-11-19 13:56:11 +000036
robertphillipsea461502015-05-26 11:38:03 -070037////////////////////////////////////////////////////////////////////////////////
38
bsalomon682c2692015-05-22 14:01:46 -070039GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext) {
40 GrContextOptions defaultOptions;
41 return Create(backend, backendContext, defaultOptions);
42}
bsalomonf28cff72015-05-22 12:25:41 -070043
bsalomon682c2692015-05-22 14:01:46 -070044GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext,
45 const GrContextOptions& options) {
halcanary385fe4d2015-08-26 13:07:48 -070046 GrContext* context = new GrContext;
bsalomon682c2692015-05-22 14:01:46 -070047
48 if (context->init(backend, backendContext, options)) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000049 return context;
50 } else {
51 context->unref();
halcanary96fcdcc2015-08-27 07:41:13 -070052 return nullptr;
bsalomon@google.com27847de2011-02-22 20:59:41 +000053 }
bsalomon@google.com27847de2011-02-22 20:59:41 +000054}
55
joshualitt0acd0d32015-05-07 08:23:19 -070056static int32_t gNextID = 1;
57static int32_t next_id() {
58 int32_t id;
59 do {
60 id = sk_atomic_inc(&gNextID);
61 } while (id == SK_InvalidGenID);
62 return id;
63}
64
bsalomon682c2692015-05-22 14:01:46 -070065GrContext::GrContext() : fUniqueID(next_id()) {
halcanary96fcdcc2015-08-27 07:41:13 -070066 fGpu = nullptr;
67 fCaps = nullptr;
68 fResourceCache = nullptr;
69 fResourceProvider = nullptr;
Brian Salomonf856fd12016-12-16 14:24:34 -050070 fAtlasGlyphCache = nullptr;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000071}
72
bsalomon682c2692015-05-22 14:01:46 -070073bool GrContext::init(GrBackend backend, GrBackendContext backendContext,
74 const GrContextOptions& options) {
joshualitt1de610a2016-01-06 08:26:09 -080075 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -070076 SkASSERT(!fGpu);
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000077
bsalomon682c2692015-05-22 14:01:46 -070078 fGpu = GrGpu::Create(backend, backendContext, options, this);
robertphillipsea461502015-05-26 11:38:03 -070079 if (!fGpu) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000080 return false;
81 }
bsalomon69cfe952015-11-30 13:27:47 -080082 this->initCommon(options);
bsalomon33435572014-11-05 14:47:41 -080083 return true;
84}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000085
bsalomon69cfe952015-11-30 13:27:47 -080086void GrContext::initCommon(const GrContextOptions& options) {
joshualitt1de610a2016-01-06 08:26:09 -080087 ASSERT_SINGLE_OWNER
88
bsalomon76228632015-05-29 08:02:10 -070089 fCaps = SkRef(fGpu->caps());
halcanary385fe4d2015-08-26 13:07:48 -070090 fResourceCache = new GrResourceCache(fCaps);
joshualitt6d0872d2016-01-11 08:27:48 -080091 fResourceProvider = new GrResourceProvider(fGpu, fResourceCache, &fSingleOwner);
commit-bot@chromium.org1836d332013-07-16 22:55:03 +000092
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000093 fDidTestPMConversions = false;
94
Robert Phillipsf2361d22016-10-25 14:20:06 -040095 GrRenderTargetOpList::Options rtOpListOptions;
Brian Salomon09d994e2016-12-21 11:14:46 -050096 rtOpListOptions.fClipDrawOpsToBounds = options.fClipDrawOpsToBounds;
97 rtOpListOptions.fMaxOpCombineLookback = options.fMaxOpCombineLookback;
98 rtOpListOptions.fMaxOpCombineLookahead = options.fMaxOpCombineLookahead;
bsalomon6b2552f2016-09-15 13:50:26 -070099 GrPathRendererChain::Options prcOptions;
100 prcOptions.fDisableDistanceFieldRenderer = options.fDisableDistanceFieldPaths;
bsalomon39ef7fb2016-09-21 11:16:05 -0700101 prcOptions.fAllowPathMaskCaching = options.fAllowPathMaskCaching;
102 prcOptions.fDisableAllPathRenderers = options.fForceSWPathMasks;
Robert Phillipsf2361d22016-10-25 14:20:06 -0400103 fDrawingManager.reset(new GrDrawingManager(this, rtOpListOptions, prcOptions,
104 options.fImmediateMode, &fSingleOwner));
joshualitt7c3a2f82015-03-31 13:32:05 -0700105
Brian Salomonf856fd12016-12-16 14:24:34 -0500106 fAtlasGlyphCache = new GrAtlasGlyphCache(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;
Brian Salomonf856fd12016-12-16 14:24:34 -0500129 delete fAtlasGlyphCache;
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
Brian Salomonf856fd12016-12-16 14:24:34 -0500157 fAtlasGlyphCache->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
Brian Salomonf856fd12016-12-16 14:24:34 -0500175 fAtlasGlyphCache->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
Brian Salomonf856fd12016-12-16 14:24:34 -0500189 fAtlasGlyphCache->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
Brian Osmanb62ea222016-12-22 11:12:16 -0500246bool GrContext::writeSurfacePixels(GrSurface* surface, SkColorSpace* dstColorSpace,
bsalomon81beccc2014-10-13 12:32:55 -0700247 int left, int top, int width, int height,
Brian Osmanb62ea222016-12-22 11:12:16 -0500248 GrPixelConfig srcConfig, SkColorSpace* srcColorSpace,
249 const void* buffer, size_t rowBytes, uint32_t pixelOpsFlags) {
250 // TODO: Color space conversion
251
joshualitt1de610a2016-01-06 08:26:09 -0800252 ASSERT_SINGLE_OWNER
joshualitt5f5a8d72015-02-25 14:09:45 -0800253 RETURN_FALSE_IF_ABANDONED
bsalomon6c6f6582015-09-10 08:12:46 -0700254 ASSERT_OWNED_RESOURCE(surface);
255 SkASSERT(surface);
joshualittbc907352016-01-13 06:45:40 -0800256 GR_AUDIT_TRAIL_AUTO_FRAME(&fAuditTrail, "GrContext::writeSurfacePixels");
bsalomon6c6f6582015-09-10 08:12:46 -0700257
258 this->testPMConversionsIfNecessary(pixelOpsFlags);
bsalomon81beccc2014-10-13 12:32:55 -0700259
bsalomone8d21e82015-07-16 08:23:13 -0700260 // 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 -0700261 // necessary and because GrGpu::getWritePixelsInfo requires it.
bsalomone8d21e82015-07-16 08:23:13 -0700262 if (!GrSurfacePriv::AdjustWritePixelParams(surface->width(), surface->height(),
263 GrBytesPerPixel(srcConfig), &left, &top, &width,
264 &height, &buffer, &rowBytes)) {
265 return false;
266 }
267
bsalomonf0674512015-07-28 13:26:15 -0700268 bool applyPremulToSrc = false;
bsalomon81beccc2014-10-13 12:32:55 -0700269 if (kUnpremul_PixelOpsFlag & pixelOpsFlags) {
Brian Salomonbf7b6202016-11-11 16:08:03 -0500270 if (!GrPixelConfigIs8888Unorm(srcConfig)) {
bsalomon81beccc2014-10-13 12:32:55 -0700271 return false;
272 }
bsalomonf0674512015-07-28 13:26:15 -0700273 applyPremulToSrc = true;
274 }
Brian Salomonbf7b6202016-11-11 16:08:03 -0500275 // We don't allow conversion between integer configs and float/fixed configs.
276 if (GrPixelConfigIsSint(surface->config()) != GrPixelConfigIsSint(srcConfig)) {
277 return false;
278 }
bsalomon636e8022015-07-29 06:08:46 -0700279
280 GrGpu::DrawPreference drawPreference = GrGpu::kNoDraw_DrawPreference;
281 // Don't prefer to draw for the conversion (and thereby access a texture from the cache) when
282 // we've already determined that there isn't a roundtrip preserving conversion processor pair.
283 if (applyPremulToSrc && !this->didFailPMUPMConversionTest()) {
284 drawPreference = GrGpu::kCallerPrefersDraw_DrawPreference;
285 }
286
bsalomonf0674512015-07-28 13:26:15 -0700287 GrGpu::WritePixelTempDrawInfo tempDrawInfo;
cblumeed828002016-02-16 13:00:01 -0800288 if (!fGpu->getWritePixelsInfo(surface, width, height, srcConfig, &drawPreference,
bsalomonf0674512015-07-28 13:26:15 -0700289 &tempDrawInfo)) {
290 return false;
291 }
292
293 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && surface->surfacePriv().hasPendingIO()) {
294 this->flush();
295 }
296
Robert Phillips757914d2017-01-25 15:48:30 -0500297 sk_sp<GrTextureProxy> tempTextureProxy;
bsalomonf0674512015-07-28 13:26:15 -0700298 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
Robert Phillips757914d2017-01-25 15:48:30 -0500299 sk_sp<GrSurfaceProxy> temp = GrSurfaceProxy::MakeDeferred(*this->caps(),
300 tempDrawInfo.fTempSurfaceDesc,
301 SkBackingFit::kApprox,
302 SkBudgeted::kYes);
303 if (temp) {
304 tempTextureProxy = sk_ref_sp(temp->asTextureProxy());
305 }
306 if (!tempTextureProxy && GrGpu::kRequireDraw_DrawPreference == drawPreference) {
bsalomonf0674512015-07-28 13:26:15 -0700307 return false;
308 }
309 }
310
311 // temp buffer for doing sw premul conversion, if needed.
312 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
Robert Phillips757914d2017-01-25 15:48:30 -0500313 if (tempTextureProxy) {
bungeman06ca8ec2016-06-09 08:01:03 -0700314 sk_sp<GrFragmentProcessor> fp;
bsalomonf0674512015-07-28 13:26:15 -0700315 if (applyPremulToSrc) {
Robert Phillips757914d2017-01-25 15:48:30 -0500316 fp = this->createUPMToPMEffect(tempTextureProxy, tempDrawInfo.fSwizzle, SkMatrix::I());
bsalomonf0674512015-07-28 13:26:15 -0700317 // If premultiplying was the only reason for the draw, fall back to a straight write.
318 if (!fp) {
319 if (GrGpu::kCallerPrefersDraw_DrawPreference == drawPreference) {
Robert Phillips757914d2017-01-25 15:48:30 -0500320 tempTextureProxy.reset(nullptr);
bsalomonf0674512015-07-28 13:26:15 -0700321 }
322 } else {
323 applyPremulToSrc = false;
324 }
325 }
Robert Phillips757914d2017-01-25 15:48:30 -0500326 if (tempTextureProxy) {
bsalomonf0674512015-07-28 13:26:15 -0700327 if (!fp) {
Robert Phillips757914d2017-01-25 15:48:30 -0500328 fp = GrConfigConversionEffect::Make(this, tempTextureProxy, tempDrawInfo.fSwizzle,
bungeman06ca8ec2016-06-09 08:01:03 -0700329 GrConfigConversionEffect::kNone_PMConversion,
Robert Phillips67c18d62017-01-20 12:44:06 -0500330 SkMatrix::I());
bsalomonf0674512015-07-28 13:26:15 -0700331 if (!fp) {
332 return false;
333 }
334 }
Robert Phillips757914d2017-01-25 15:48:30 -0500335 GrTexture* texture = tempTextureProxy->instantiate(this->textureProvider());
336 if (!texture) {
337 return false;
338 }
339 if (texture->surfacePriv().hasPendingIO()) {
bsalomonf0674512015-07-28 13:26:15 -0700340 this->flush();
341 }
342 if (applyPremulToSrc) {
343 size_t tmpRowBytes = 4 * width;
344 tmpPixels.reset(width * height);
345 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
346 tmpPixels.get())) {
347 return false;
348 }
349 rowBytes = tmpRowBytes;
350 buffer = tmpPixels.get();
351 applyPremulToSrc = false;
352 }
Robert Phillips757914d2017-01-25 15:48:30 -0500353 if (!fGpu->writePixels(texture, 0, 0, width, height,
bsalomon6c9cd552016-01-22 07:17:34 -0800354 tempDrawInfo.fWriteConfig, buffer,
bsalomon6cb3cbe2015-07-30 07:34:27 -0700355 rowBytes)) {
bsalomonf0674512015-07-28 13:26:15 -0700356 return false;
357 }
358 SkMatrix matrix;
359 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
brianosmandfe4f2e2016-07-21 13:28:36 -0700360 // TODO: Need to decide the semantics of this function for color spaces. Do we support
361 // conversion from a passed-in color space? For now, specifying nullptr means that this
362 // path will do no conversion, so it will match the behavior of the non-draw path.
Robert Phillips757914d2017-01-25 15:48:30 -0500363 GrRenderTarget* renderTarget = surface->asRenderTarget();
364 SkASSERT(renderTarget);
Brian Osman11052242016-10-27 14:47:55 -0400365 sk_sp<GrRenderTargetContext> renderTargetContext(
366 this->contextPriv().makeWrappedRenderTargetContext(sk_ref_sp(renderTarget),
367 nullptr));
368 if (!renderTargetContext) {
bsalomonf0674512015-07-28 13:26:15 -0700369 return false;
370 }
egdanielc4b72722015-11-23 13:20:41 -0800371 GrPaint paint;
bungeman06ca8ec2016-06-09 08:01:03 -0700372 paint.addColorFragmentProcessor(std::move(fp));
reed374772b2016-10-05 17:33:02 -0700373 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
brianosmana167e742016-05-24 06:18:48 -0700374 paint.setAllowSRGBInputs(true);
bsalomonf0674512015-07-28 13:26:15 -0700375 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
Brian Salomon82f44312017-01-11 13:42:54 -0500376 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, matrix, rect,
377 nullptr);
bsalomonf0674512015-07-28 13:26:15 -0700378
379 if (kFlushWrites_PixelOp & pixelOpsFlags) {
380 this->flushSurfaceWrites(surface);
381 }
382 }
383 }
Robert Phillips757914d2017-01-25 15:48:30 -0500384 if (!tempTextureProxy) {
bsalomonf0674512015-07-28 13:26:15 -0700385 if (applyPremulToSrc) {
bsalomon81beccc2014-10-13 12:32:55 -0700386 size_t tmpRowBytes = 4 * width;
387 tmpPixels.reset(width * height);
388 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
389 tmpPixels.get())) {
390 return false;
391 }
392 rowBytes = tmpRowBytes;
393 buffer = tmpPixels.get();
bsalomonf0674512015-07-28 13:26:15 -0700394 applyPremulToSrc = false;
bsalomon81beccc2014-10-13 12:32:55 -0700395 }
bsalomon6cb3cbe2015-07-30 07:34:27 -0700396 return fGpu->writePixels(surface, left, top, width, height, srcConfig, buffer, rowBytes);
bsalomon81beccc2014-10-13 12:32:55 -0700397 }
bsalomon81beccc2014-10-13 12:32:55 -0700398 return true;
399}
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000400
Brian Osmanb62ea222016-12-22 11:12:16 -0500401bool GrContext::readSurfacePixels(GrSurface* src, SkColorSpace* srcColorSpace,
bsalomone8d21e82015-07-16 08:23:13 -0700402 int left, int top, int width, int height,
Brian Osmanb62ea222016-12-22 11:12:16 -0500403 GrPixelConfig dstConfig, SkColorSpace* dstColorSpace,
404 void* buffer, size_t rowBytes, uint32_t flags) {
405 // TODO: Color space conversion
406
joshualitt1de610a2016-01-06 08:26:09 -0800407 ASSERT_SINGLE_OWNER
joshualitt5f5a8d72015-02-25 14:09:45 -0800408 RETURN_FALSE_IF_ABANDONED
bsalomone8d21e82015-07-16 08:23:13 -0700409 ASSERT_OWNED_RESOURCE(src);
410 SkASSERT(src);
joshualittbc907352016-01-13 06:45:40 -0800411 GR_AUDIT_TRAIL_AUTO_FRAME(&fAuditTrail, "GrContext::readSurfacePixels");
bsalomon32ab2602015-09-09 18:57:49 -0700412
bsalomon6c6f6582015-09-10 08:12:46 -0700413 this->testPMConversionsIfNecessary(flags);
bsalomon6c6f6582015-09-10 08:12:46 -0700414
bsalomone8d21e82015-07-16 08:23:13 -0700415 // Adjust the params so that if we wind up using an intermediate surface we've already done
416 // all the trimming and the temporary can be the min size required.
417 if (!GrSurfacePriv::AdjustReadPixelParams(src->width(), src->height(),
418 GrBytesPerPixel(dstConfig), &left,
419 &top, &width, &height, &buffer, &rowBytes)) {
420 return false;
421 }
422
423 if (!(kDontFlush_PixelOpsFlag & flags) && src->surfacePriv().hasPendingWrite()) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000424 this->flush();
425 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000426
bsalomone8d21e82015-07-16 08:23:13 -0700427 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
Brian Salomonbf7b6202016-11-11 16:08:03 -0500428 if (unpremul && !GrPixelConfigIs8888Unorm(dstConfig)) {
bsalomon39826022015-07-23 08:07:21 -0700429 // The unpremul flag is only allowed for 8888 configs.
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000430 return false;
431 }
Brian Salomonbf7b6202016-11-11 16:08:03 -0500432 // We don't allow conversion between integer configs and float/fixed configs.
433 if (GrPixelConfigIsSint(src->config()) != GrPixelConfigIsSint(dstConfig)) {
434 return false;
435 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000436
bsalomon636e8022015-07-29 06:08:46 -0700437 GrGpu::DrawPreference drawPreference = GrGpu::kNoDraw_DrawPreference;
438 // Don't prefer to draw for the conversion (and thereby access a texture from the cache) when
439 // we've already determined that there isn't a roundtrip preserving conversion processor pair.
440 if (unpremul && !this->didFailPMUPMConversionTest()) {
441 drawPreference = GrGpu::kCallerPrefersDraw_DrawPreference;
442 }
443
bsalomon39826022015-07-23 08:07:21 -0700444 GrGpu::ReadPixelTempDrawInfo tempDrawInfo;
445 if (!fGpu->getReadPixelsInfo(src, width, height, rowBytes, dstConfig, &drawPreference,
446 &tempDrawInfo)) {
447 return false;
448 }
bsalomon191bcc02014-11-14 11:31:13 -0800449
Hal Canary144caf52016-11-07 17:57:18 -0500450 sk_sp<GrSurface> surfaceToRead(SkRef(src));
bsalomon39826022015-07-23 08:07:21 -0700451 bool didTempDraw = false;
452 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
bsalomonb117ff12016-07-19 07:24:40 -0700453 if (SkBackingFit::kExact == tempDrawInfo.fTempSurfaceFit) {
bsalomon39826022015-07-23 08:07:21 -0700454 // We only respect this when the entire src is being read. Otherwise we can trigger too
455 // many odd ball texture sizes and trash the cache.
bsalomoneae62002015-07-31 13:59:30 -0700456 if (width != src->width() || height != src->height()) {
bsalomonb117ff12016-07-19 07:24:40 -0700457 tempDrawInfo.fTempSurfaceFit= SkBackingFit::kApprox;
bsalomon39826022015-07-23 08:07:21 -0700458 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000459 }
brianosmandfe4f2e2016-07-21 13:28:36 -0700460 // TODO: Need to decide the semantics of this function for color spaces. Do we support
461 // conversion to a passed-in color space? For now, specifying nullptr means that this
462 // path will do no conversion, so it will match the behavior of the non-draw path.
Brian Osman693a5402016-10-27 15:13:22 -0400463 sk_sp<GrRenderTargetContext> tempRTC = this->makeRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400464 tempDrawInfo.fTempSurfaceFit,
bsalomonb117ff12016-07-19 07:24:40 -0700465 tempDrawInfo.fTempSurfaceDesc.fWidth,
466 tempDrawInfo.fTempSurfaceDesc.fHeight,
467 tempDrawInfo.fTempSurfaceDesc.fConfig,
brianosmandfe4f2e2016-07-21 13:28:36 -0700468 nullptr,
bsalomonb117ff12016-07-19 07:24:40 -0700469 tempDrawInfo.fTempSurfaceDesc.fSampleCnt,
470 tempDrawInfo.fTempSurfaceDesc.fOrigin);
Brian Osman693a5402016-10-27 15:13:22 -0400471 if (tempRTC) {
Robert Phillips67c18d62017-01-20 12:44:06 -0500472 SkMatrix textureMatrix = SkMatrix::MakeTrans(SkIntToScalar(left), SkIntToScalar(top));
bungeman06ca8ec2016-06-09 08:01:03 -0700473 sk_sp<GrFragmentProcessor> fp;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000474 if (unpremul) {
bungeman06ca8ec2016-06-09 08:01:03 -0700475 fp = this->createPMToUPMEffect(src->asTexture(), tempDrawInfo.fSwizzle,
476 textureMatrix);
joshualittb0a8a372014-09-23 09:50:21 -0700477 if (fp) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000478 unpremul = false; // we no longer need to do this on CPU after the read back.
bsalomon39826022015-07-23 08:07:21 -0700479 } else if (GrGpu::kCallerPrefersDraw_DrawPreference == drawPreference) {
480 // We only wanted to do the draw in order to perform the unpremul so don't
481 // bother.
Brian Osman693a5402016-10-27 15:13:22 -0400482 tempRTC.reset(nullptr);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000483 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000484 }
Brian Osman693a5402016-10-27 15:13:22 -0400485 if (!fp && tempRTC) {
bungeman06ca8ec2016-06-09 08:01:03 -0700486 fp = GrConfigConversionEffect::Make(src->asTexture(), tempDrawInfo.fSwizzle,
487 GrConfigConversionEffect::kNone_PMConversion,
488 textureMatrix);
bsalomon39826022015-07-23 08:07:21 -0700489 }
490 if (fp) {
egdanielc4b72722015-11-23 13:20:41 -0800491 GrPaint paint;
bungeman06ca8ec2016-06-09 08:01:03 -0700492 paint.addColorFragmentProcessor(std::move(fp));
reed374772b2016-10-05 17:33:02 -0700493 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
brianosmana167e742016-05-24 06:18:48 -0700494 paint.setAllowSRGBInputs(true);
bsalomon39826022015-07-23 08:07:21 -0700495 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
Brian Salomon82f44312017-01-11 13:42:54 -0500496 tempRTC->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect,
497 nullptr);
Brian Osman693a5402016-10-27 15:13:22 -0400498 surfaceToRead.reset(tempRTC->asTexture().release());
bsalomon39826022015-07-23 08:07:21 -0700499 left = 0;
500 top = 0;
501 didTempDraw = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000502 }
bsalomon@google.com0342a852012-08-20 19:22:38 +0000503 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000504 }
joshualitt5c55fef2014-10-31 14:04:35 -0700505
Robert Phillips833dcf42016-11-18 08:44:13 -0500506 if (!surfaceToRead) {
507 return false;
508 }
509
bsalomon39826022015-07-23 08:07:21 -0700510 if (GrGpu::kRequireDraw_DrawPreference == drawPreference && !didTempDraw) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000511 return false;
512 }
bsalomon39826022015-07-23 08:07:21 -0700513 GrPixelConfig configToRead = dstConfig;
514 if (didTempDraw) {
Hal Canary144caf52016-11-07 17:57:18 -0500515 this->flushSurfaceWrites(surfaceToRead.get());
bsalomon6c9cd552016-01-22 07:17:34 -0800516 configToRead = tempDrawInfo.fReadConfig;
bsalomon39826022015-07-23 08:07:21 -0700517 }
Hal Canary144caf52016-11-07 17:57:18 -0500518 if (!fGpu->readPixels(surfaceToRead.get(), left, top, width, height, configToRead, buffer,
519 rowBytes)) {
bsalomon39826022015-07-23 08:07:21 -0700520 return false;
521 }
522
523 // Perform umpremul conversion if we weren't able to perform it as a draw.
524 if (unpremul) {
reed@google.com7111d462014-03-25 16:20:24 +0000525 SkDstPixelInfo dstPI;
brianosman396fcdb2016-07-22 06:26:11 -0700526 if (!GrPixelConfigToColorType(dstConfig, &dstPI.fColorType)) {
reed@google.com7111d462014-03-25 16:20:24 +0000527 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000528 }
reed@google.com7111d462014-03-25 16:20:24 +0000529 dstPI.fAlphaType = kUnpremul_SkAlphaType;
530 dstPI.fPixels = buffer;
531 dstPI.fRowBytes = rowBytes;
532
533 SkSrcPixelInfo srcPI;
bsalomon39826022015-07-23 08:07:21 -0700534 srcPI.fColorType = dstPI.fColorType;
reed@google.com7111d462014-03-25 16:20:24 +0000535 srcPI.fAlphaType = kPremul_SkAlphaType;
536 srcPI.fPixels = buffer;
537 srcPI.fRowBytes = rowBytes;
538
539 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000540 }
541 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000542}
543
bsalomonc49e8682015-06-30 11:37:35 -0700544void GrContext::prepareSurfaceForExternalIO(GrSurface* surface) {
joshualitt1de610a2016-01-06 08:26:09 -0800545 ASSERT_SINGLE_OWNER
joshualitt5f5a8d72015-02-25 14:09:45 -0800546 RETURN_IF_ABANDONED
bsalomon87a94eb2014-11-03 14:28:32 -0800547 SkASSERT(surface);
548 ASSERT_OWNED_RESOURCE(surface);
bsalomon6a2b1942016-09-08 11:28:59 -0700549 fDrawingManager->prepareSurfaceForExternalIO(surface);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000550}
551
bsalomonf80bfed2014-10-07 05:56:02 -0700552void GrContext::flushSurfaceWrites(GrSurface* surface) {
joshualitt1de610a2016-01-06 08:26:09 -0800553 ASSERT_SINGLE_OWNER
joshualitt5f5a8d72015-02-25 14:09:45 -0800554 RETURN_IF_ABANDONED
bsalomonf80bfed2014-10-07 05:56:02 -0700555 if (surface->surfacePriv().hasPendingWrite()) {
556 this->flush();
557 }
558}
559
ajuma95243eb2016-08-24 08:19:02 -0700560void GrContext::flushSurfaceIO(GrSurface* surface) {
561 ASSERT_SINGLE_OWNER
562 RETURN_IF_ABANDONED
563 if (surface->surfacePriv().hasPendingIO()) {
564 this->flush();
565 }
566}
567
bsalomon@google.com27847de2011-02-22 20:59:41 +0000568////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000569int GrContext::getRecommendedSampleCount(GrPixelConfig config,
570 SkScalar dpi) const {
joshualitt1de610a2016-01-06 08:26:09 -0800571 ASSERT_SINGLE_OWNER
572
bsalomon76228632015-05-29 08:02:10 -0700573 if (!this->caps()->isConfigRenderable(config, true)) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000574 return 0;
575 }
576 int chosenSampleCount = 0;
jvanverthe9c0fc62015-04-29 11:18:05 -0700577 if (fGpu->caps()->shaderCaps()->pathRenderingSupport()) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000578 if (dpi >= 250.0f) {
579 chosenSampleCount = 4;
580 } else {
581 chosenSampleCount = 16;
582 }
583 }
egdanieleed519e2016-01-15 11:36:18 -0800584 return chosenSampleCount <= fGpu->caps()->maxSampleCount() ? chosenSampleCount : 0;
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000585}
586
Brian Osman11052242016-10-27 14:47:55 -0400587sk_sp<GrRenderTargetContext> GrContextPriv::makeWrappedRenderTargetContext(
588 sk_sp<GrRenderTarget> rt,
589 sk_sp<SkColorSpace> colorSpace,
590 const SkSurfaceProps* surfaceProps) {
robertphillips4fd74ae2016-08-03 14:26:53 -0700591 ASSERT_SINGLE_OWNER_PRIV
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400592
Robert Phillips37430132016-11-09 06:50:43 -0500593 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(rt)));
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400594
Robert Phillips37430132016-11-09 06:50:43 -0500595 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400596 std::move(colorSpace),
Brian Osman11052242016-10-27 14:47:55 -0400597 surfaceProps);
robertphillips4fd74ae2016-08-03 14:26:53 -0700598}
robertphillips77a2e522015-10-17 07:43:27 -0700599
Robert Phillips2c862492017-01-18 10:08:39 -0500600sk_sp<GrSurfaceContext> GrContextPriv::makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy> proxy,
601 sk_sp<SkColorSpace> colorSpace) {
Brian Osman45580d32016-11-23 09:37:01 -0500602 ASSERT_SINGLE_OWNER_PRIV
603
Brian Osman45580d32016-11-23 09:37:01 -0500604 if (proxy->asRenderTargetProxy()) {
Robert Phillips2c862492017-01-18 10:08:39 -0500605 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
606 std::move(colorSpace), nullptr);
Brian Osman45580d32016-11-23 09:37:01 -0500607 } else {
608 SkASSERT(proxy->asTextureProxy());
Robert Phillips2c862492017-01-18 10:08:39 -0500609 return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
Brian Osman45580d32016-11-23 09:37:01 -0500610 }
611}
612
Robert Phillips31c26082016-12-14 15:12:15 -0500613sk_sp<GrSurfaceContext> GrContextPriv::makeWrappedSurfaceContext(sk_sp<GrSurface> surface) {
614 ASSERT_SINGLE_OWNER_PRIV
615
616 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface)));
617
Robert Phillips2c862492017-01-18 10:08:39 -0500618 return this->makeWrappedSurfaceContext(std::move(proxy), nullptr);
Robert Phillips31c26082016-12-14 15:12:15 -0500619}
620
Robert Phillipse2f7d182016-12-15 09:23:05 -0500621sk_sp<GrSurfaceContext> GrContextPriv::makeDeferredSurfaceContext(const GrSurfaceDesc& dstDesc,
622 SkBackingFit fit,
623 SkBudgeted isDstBudgeted) {
624
625 sk_sp<GrSurfaceProxy> proxy = GrSurfaceProxy::MakeDeferred(*fContext->caps(), dstDesc,
626 fit, isDstBudgeted);
627
Robert Phillips2c862492017-01-18 10:08:39 -0500628 return this->makeWrappedSurfaceContext(std::move(proxy), nullptr);
Robert Phillipse2f7d182016-12-15 09:23:05 -0500629}
630
Brian Osman11052242016-10-27 14:47:55 -0400631sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureRenderTargetContext(
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500632 const GrBackendTextureDesc& desc,
Brian Osman11052242016-10-27 14:47:55 -0400633 sk_sp<SkColorSpace> colorSpace,
634 const SkSurfaceProps* props,
635 GrWrapOwnership ownership) {
robertphillips4fd74ae2016-08-03 14:26:53 -0700636 ASSERT_SINGLE_OWNER_PRIV
637 SkASSERT(desc.fFlags & kRenderTarget_GrBackendTextureFlag);
638
639 sk_sp<GrSurface> surface(fContext->textureProvider()->wrapBackendTexture(desc, ownership));
640 if (!surface) {
641 return nullptr;
642 }
643
Robert Phillips37430132016-11-09 06:50:43 -0500644 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface)));
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400645
Robert Phillips37430132016-11-09 06:50:43 -0500646 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Brian Osman11052242016-10-27 14:47:55 -0400647 std::move(colorSpace), props);
robertphillips4fd74ae2016-08-03 14:26:53 -0700648}
649
Brian Osman11052242016-10-27 14:47:55 -0400650sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendRenderTargetRenderTargetContext(
robertphillips4fd74ae2016-08-03 14:26:53 -0700651 const GrBackendRenderTargetDesc& desc,
652 sk_sp<SkColorSpace> colorSpace,
653 const SkSurfaceProps* surfaceProps) {
654 ASSERT_SINGLE_OWNER_PRIV
655
656 sk_sp<GrRenderTarget> rt(fContext->textureProvider()->wrapBackendRenderTarget(desc));
657 if (!rt) {
658 return nullptr;
659 }
660
Robert Phillips37430132016-11-09 06:50:43 -0500661 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(rt)));
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400662
Robert Phillips37430132016-11-09 06:50:43 -0500663 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400664 std::move(colorSpace),
Brian Osman11052242016-10-27 14:47:55 -0400665 surfaceProps);
robertphillips4fd74ae2016-08-03 14:26:53 -0700666}
667
Brian Osman11052242016-10-27 14:47:55 -0400668sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureAsRenderTargetRenderTargetContext(
egdaniela95d46b2016-08-15 08:06:29 -0700669 const GrBackendTextureDesc& desc,
robertphillips4fd74ae2016-08-03 14:26:53 -0700670 sk_sp<SkColorSpace> colorSpace,
671 const SkSurfaceProps* surfaceProps) {
672 ASSERT_SINGLE_OWNER_PRIV
673 SkASSERT(desc.fFlags & kRenderTarget_GrBackendTextureFlag);
674
675 sk_sp<GrSurface> surface(fContext->resourceProvider()->wrapBackendTextureAsRenderTarget(desc));
676 if (!surface) {
677 return nullptr;
678 }
679
Robert Phillips37430132016-11-09 06:50:43 -0500680 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface)));
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400681
Robert Phillips37430132016-11-09 06:50:43 -0500682 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400683 std::move(colorSpace),
684 surfaceProps);
robertphillips77a2e522015-10-17 07:43:27 -0700685}
686
robertphillips48fde9c2016-09-06 05:20:20 -0700687static inline GrPixelConfig GrPixelConfigFallback(GrPixelConfig config) {
Brian Osman78f20e02017-01-12 10:28:01 -0500688 switch (config) {
689 case kAlpha_8_GrPixelConfig:
690 case kRGB_565_GrPixelConfig:
691 case kRGBA_4444_GrPixelConfig:
692 case kBGRA_8888_GrPixelConfig:
693 return kRGBA_8888_GrPixelConfig;
694 case kSBGRA_8888_GrPixelConfig:
695 return kSRGBA_8888_GrPixelConfig;
696 case kAlpha_half_GrPixelConfig:
697 return kRGBA_half_GrPixelConfig;
698 default:
699 return kUnknown_GrPixelConfig;
700 }
robertphillips48fde9c2016-09-06 05:20:20 -0700701}
702
Brian Osman11052242016-10-27 14:47:55 -0400703sk_sp<GrRenderTargetContext> GrContext::makeRenderTargetContextWithFallback(
704 SkBackingFit fit,
705 int width, int height,
706 GrPixelConfig config,
707 sk_sp<SkColorSpace> colorSpace,
708 int sampleCnt,
709 GrSurfaceOrigin origin,
710 const SkSurfaceProps* surfaceProps,
711 SkBudgeted budgeted) {
robertphillips48fde9c2016-09-06 05:20:20 -0700712 if (!this->caps()->isConfigRenderable(config, sampleCnt > 0)) {
713 config = GrPixelConfigFallback(config);
714 }
715
Brian Osman11052242016-10-27 14:47:55 -0400716 return this->makeRenderTargetContext(fit, width, height, config, std::move(colorSpace),
717 sampleCnt, origin, surfaceProps, budgeted);
robertphillips48fde9c2016-09-06 05:20:20 -0700718}
719
robertphillipsd728f0c2016-11-21 11:05:03 -0800720sk_sp<GrRenderTargetContext> GrContext::makeDeferredRenderTargetContextWithFallback(
721 SkBackingFit fit,
722 int width, int height,
723 GrPixelConfig config,
724 sk_sp<SkColorSpace> colorSpace,
725 int sampleCnt,
726 GrSurfaceOrigin origin,
727 const SkSurfaceProps* surfaceProps,
728 SkBudgeted budgeted) {
729 if (!this->caps()->isConfigRenderable(config, sampleCnt > 0)) {
730 config = GrPixelConfigFallback(config);
731 }
732
733 return this->makeDeferredRenderTargetContext(fit, width, height, config, std::move(colorSpace),
734 sampleCnt, origin, surfaceProps, budgeted);
735}
736
Brian Osman11052242016-10-27 14:47:55 -0400737sk_sp<GrRenderTargetContext> GrContext::makeRenderTargetContext(SkBackingFit fit,
738 int width, int height,
739 GrPixelConfig config,
740 sk_sp<SkColorSpace> colorSpace,
741 int sampleCnt,
742 GrSurfaceOrigin origin,
743 const SkSurfaceProps* surfaceProps,
744 SkBudgeted budgeted) {
robertphillips48fde9c2016-09-06 05:20:20 -0700745 if (!this->caps()->isConfigRenderable(config, sampleCnt > 0)) {
746 return nullptr;
747 }
748
robertphillipsd4c741e2016-04-28 09:55:15 -0700749 GrSurfaceDesc desc;
750 desc.fFlags = kRenderTarget_GrSurfaceFlag;
751 desc.fOrigin = origin;
752 desc.fWidth = width;
753 desc.fHeight = height;
754 desc.fConfig = config;
755 desc.fSampleCnt = sampleCnt;
756
757 sk_sp<GrTexture> tex;
robertphillips76948d42016-05-04 12:47:41 -0700758 if (SkBackingFit::kExact == fit) {
robertphillipsca6eafc2016-05-17 09:57:46 -0700759 tex.reset(this->textureProvider()->createTexture(desc, budgeted));
robertphillipsd4c741e2016-04-28 09:55:15 -0700760 } else {
761 tex.reset(this->textureProvider()->createApproxTexture(desc));
762 }
763 if (!tex) {
764 return nullptr;
765 }
766
Brian Osman11052242016-10-27 14:47:55 -0400767 sk_sp<GrRenderTargetContext> renderTargetContext(
768 this->contextPriv().makeWrappedRenderTargetContext(sk_ref_sp(tex->asRenderTarget()),
robertphillips6738c702016-07-27 12:13:51 -0700769 std::move(colorSpace), surfaceProps));
Brian Osman11052242016-10-27 14:47:55 -0400770 if (!renderTargetContext) {
robertphillipsd4c741e2016-04-28 09:55:15 -0700771 return nullptr;
772 }
773
Brian Osman11052242016-10-27 14:47:55 -0400774 return renderTargetContext;
robertphillipsd4c741e2016-04-28 09:55:15 -0700775}
776
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400777sk_sp<GrRenderTargetContext> GrContext::makeDeferredRenderTargetContext(
778 SkBackingFit fit,
779 int width, int height,
780 GrPixelConfig config,
781 sk_sp<SkColorSpace> colorSpace,
782 int sampleCnt,
783 GrSurfaceOrigin origin,
784 const SkSurfaceProps* surfaceProps,
785 SkBudgeted budgeted) {
786 GrSurfaceDesc desc;
787 desc.fFlags = kRenderTarget_GrSurfaceFlag;
788 desc.fOrigin = origin;
789 desc.fWidth = width;
790 desc.fHeight = height;
791 desc.fConfig = config;
792 desc.fSampleCnt = sampleCnt;
793
Robert Phillips37430132016-11-09 06:50:43 -0500794 sk_sp<GrSurfaceProxy> rtp = GrSurfaceProxy::MakeDeferred(*this->caps(), desc, fit, budgeted);
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400795
796 return fDrawingManager->makeRenderTargetContext(std::move(rtp),
797 std::move(colorSpace),
798 surfaceProps);
799}
800
joshualitt1de610a2016-01-06 08:26:09 -0800801bool GrContext::abandoned() const {
802 ASSERT_SINGLE_OWNER
robertphillips7761d612016-05-16 09:14:53 -0700803 return fDrawingManager->wasAbandoned();
robertphillips77a2e522015-10-17 07:43:27 -0700804}
805
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000806namespace {
807void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
808 GrConfigConversionEffect::PMConversion pmToUPM;
809 GrConfigConversionEffect::PMConversion upmToPM;
810 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM);
811 *pmToUPMValue = pmToUPM;
812 *upmToPMValue = upmToPM;
813}
814}
815
bsalomon6c6f6582015-09-10 08:12:46 -0700816void GrContext::testPMConversionsIfNecessary(uint32_t flags) {
joshualitt1de610a2016-01-06 08:26:09 -0800817 ASSERT_SINGLE_OWNER
bsalomon6c6f6582015-09-10 08:12:46 -0700818 if (SkToBool(kUnpremul_PixelOpsFlag & flags)) {
bsalomon6c6f6582015-09-10 08:12:46 -0700819 if (!fDidTestPMConversions) {
820 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
821 fDidTestPMConversions = true;
822 }
823 }
824}
825
bungeman06ca8ec2016-06-09 08:01:03 -0700826sk_sp<GrFragmentProcessor> GrContext::createPMToUPMEffect(GrTexture* texture,
bsalomon6c9cd552016-01-22 07:17:34 -0800827 const GrSwizzle& swizzle,
Robert Phillips757914d2017-01-25 15:48:30 -0500828 const SkMatrix& matrix) {
joshualitt1de610a2016-01-06 08:26:09 -0800829 ASSERT_SINGLE_OWNER
bsalomon6c6f6582015-09-10 08:12:46 -0700830 // We should have already called this->testPMConversionsIfNecessary().
831 SkASSERT(fDidTestPMConversions);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000832 GrConfigConversionEffect::PMConversion pmToUPM =
833 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
834 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
bungeman06ca8ec2016-06-09 08:01:03 -0700835 return GrConfigConversionEffect::Make(texture, swizzle, pmToUPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000836 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700837 return nullptr;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000838 }
839}
840
Robert Phillips757914d2017-01-25 15:48:30 -0500841sk_sp<GrFragmentProcessor> GrContext::createPMToUPMEffect(sk_sp<GrTextureProxy> proxy,
bsalomon6c9cd552016-01-22 07:17:34 -0800842 const GrSwizzle& swizzle,
Robert Phillips757914d2017-01-25 15:48:30 -0500843 const SkMatrix& matrix) {
844 ASSERT_SINGLE_OWNER
845 // We should have already called this->testPMConversionsIfNecessary().
846 SkASSERT(fDidTestPMConversions);
847 GrConfigConversionEffect::PMConversion pmToUPM =
848 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
849 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
850 return GrConfigConversionEffect::Make(this, proxy, swizzle, pmToUPM, matrix);
851 } else {
852 return nullptr;
853 }
854}
855
856sk_sp<GrFragmentProcessor> GrContext::createUPMToPMEffect(sk_sp<GrTextureProxy> proxy,
857 const GrSwizzle& swizzle,
858 const SkMatrix& matrix) {
joshualitt1de610a2016-01-06 08:26:09 -0800859 ASSERT_SINGLE_OWNER
bsalomon6c6f6582015-09-10 08:12:46 -0700860 // We should have already called this->testPMConversionsIfNecessary().
861 SkASSERT(fDidTestPMConversions);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000862 GrConfigConversionEffect::PMConversion upmToPM =
863 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion);
864 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) {
Robert Phillips757914d2017-01-25 15:48:30 -0500865 return GrConfigConversionEffect::Make(this, std::move(proxy), swizzle, upmToPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000866 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700867 return nullptr;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000868 }
869}
870
bsalomon636e8022015-07-29 06:08:46 -0700871bool GrContext::didFailPMUPMConversionTest() const {
joshualitt1de610a2016-01-06 08:26:09 -0800872 ASSERT_SINGLE_OWNER
bsalomon6c6f6582015-09-10 08:12:46 -0700873 // We should have already called this->testPMConversionsIfNecessary().
874 SkASSERT(fDidTestPMConversions);
bsalomon636e8022015-07-29 06:08:46 -0700875 // The PM<->UPM tests fail or succeed together so we only need to check one.
bsalomon6c6f6582015-09-10 08:12:46 -0700876 return GrConfigConversionEffect::kNone_PMConversion == fPMToUPMConversion;
bsalomon636e8022015-07-29 06:08:46 -0700877}
878
bsalomon37f9a262015-02-02 13:00:10 -0800879//////////////////////////////////////////////////////////////////////////////
880
881void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800882 ASSERT_SINGLE_OWNER
bsalomon37f9a262015-02-02 13:00:10 -0800883 if (maxTextures) {
bsalomon0ea80f42015-02-11 10:49:59 -0800884 *maxTextures = fResourceCache->getMaxResourceCount();
bsalomon37f9a262015-02-02 13:00:10 -0800885 }
886 if (maxTextureBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800887 *maxTextureBytes = fResourceCache->getMaxResourceBytes();
bsalomon37f9a262015-02-02 13:00:10 -0800888 }
889}
890
891void GrContext::setResourceCacheLimits(int maxTextures, size_t maxTextureBytes) {
joshualitt1de610a2016-01-06 08:26:09 -0800892 ASSERT_SINGLE_OWNER
bsalomon0ea80f42015-02-11 10:49:59 -0800893 fResourceCache->setLimits(maxTextures, maxTextureBytes);
bsalomon37f9a262015-02-02 13:00:10 -0800894}
895
ericrk0a5fa482015-09-15 14:16:10 -0700896//////////////////////////////////////////////////////////////////////////////
897
898void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -0800899 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -0700900 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
901}