blob: efa8e8c61070b18ae3a33cecd5088e8decc9975b [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
Greg Daniel51316782017-08-02 15:10:09 +00008#include "GrBackendSemaphore.h"
bsalomon@google.com1fadb202011-12-12 16:10:08 +00009#include "GrContext.h"
Brian Salomonc65aec92017-03-09 09:03:58 -050010#include "GrClip.h"
bsalomon682c2692015-05-22 14:01:46 -070011#include "GrContextOptions.h"
Brian Salomonc65aec92017-03-09 09:03:58 -050012#include "GrContextPriv.h"
robertphillips77a2e522015-10-17 07:43:27 -070013#include "GrDrawingManager.h"
Robert Phillips646e4292017-06-13 12:44:56 -040014#include "GrGpu.h"
Brian Osman11052242016-10-27 14:47:55 -040015#include "GrRenderTargetContext.h"
Brian Salomonc65aec92017-03-09 09:03:58 -050016#include "GrRenderTargetProxy.h"
bsalomon0ea80f42015-02-11 10:49:59 -080017#include "GrResourceCache.h"
bsalomond309e7a2015-04-30 14:18:54 -070018#include "GrResourceProvider.h"
Greg Danield85f97d2017-03-07 13:37:21 -050019#include "GrSemaphore.h"
robertphillips@google.com72176b22012-05-23 13:19:12 +000020#include "GrSoftwarePathRenderer.h"
Brian Osman45580d32016-11-23 09:37:01 -050021#include "GrSurfaceContext.h"
bsalomonafbf2d62014-09-30 12:18:44 -070022#include "GrSurfacePriv.h"
Robert Phillips757914d2017-01-25 15:48:30 -050023#include "GrSurfaceProxyPriv.h"
Robert Phillips646e4292017-06-13 12:44:56 -040024#include "GrTexture.h"
Brian Osman45580d32016-11-23 09:37:01 -050025#include "GrTextureContext.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040026#include "GrTracing.h"
Matt Sarett485c4992017-02-14 14:18:27 -050027#include "SkConvertPixels.h"
Brian Osman3b655982017-03-07 16:58:08 -050028#include "SkGr.h"
Brian Osman175af0d2017-08-09 14:29:36 -040029#include "SkJSONWriter.h"
Matt Sarettc7b29082017-02-09 16:22:39 -050030#include "SkUnPreMultiplyPriv.h"
joshualitt5478d422014-11-14 16:00:38 -080031#include "effects/GrConfigConversionEffect.h"
joshualitte8042922015-12-11 06:11:21 -080032#include "text/GrTextBlobCache.h"
joshualitt5478d422014-11-14 16:00:38 -080033
Greg Daniel02611d92017-07-25 10:05:01 -040034#include "gl/GrGLGpu.h"
35#include "mock/GrMockGpu.h"
Greg Danielb76a72a2017-07-13 15:07:54 -040036#ifdef SK_METAL
37#include "mtl/GrMtlTrampoline.h"
38#endif
Greg Daniel02611d92017-07-25 10:05:01 -040039#ifdef SK_VULKAN
40#include "vk/GrVkGpu.h"
41#endif
Greg Danielb76a72a2017-07-13 15:07:54 -040042
Robert Phillipse78b7252017-04-06 07:59:41 -040043#define ASSERT_OWNED_PROXY(P) \
44SkASSERT(!(P) || !((P)->priv().peekTexture()) || (P)->priv().peekTexture()->getContext() == this)
Robert Phillips7ee385e2017-03-30 08:02:11 -040045#define ASSERT_OWNED_PROXY_PRIV(P) \
46SkASSERT(!(P) || !((P)->priv().peekTexture()) || (P)->priv().peekTexture()->getContext() == fContext)
47
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000048#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
joshualitt1de610a2016-01-06 08:26:09 -080049#define ASSERT_SINGLE_OWNER \
50 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fSingleOwner);)
robertphillips4fd74ae2016-08-03 14:26:53 -070051#define ASSERT_SINGLE_OWNER_PRIV \
52 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fContext->fSingleOwner);)
robertphillips7761d612016-05-16 09:14:53 -070053#define RETURN_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return; }
Robert Phillips7ee385e2017-03-30 08:02:11 -040054#define RETURN_IF_ABANDONED_PRIV if (fContext->fDrawingManager->wasAbandoned()) { return; }
robertphillips7761d612016-05-16 09:14:53 -070055#define RETURN_FALSE_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return false; }
Robert Phillipse78b7252017-04-06 07:59:41 -040056#define RETURN_FALSE_IF_ABANDONED_PRIV if (fContext->fDrawingManager->wasAbandoned()) { return false; }
robertphillips7761d612016-05-16 09:14:53 -070057#define RETURN_NULL_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return nullptr; }
bsalomon@google.combc4b6542011-11-19 13:56:11 +000058
robertphillipsea461502015-05-26 11:38:03 -070059////////////////////////////////////////////////////////////////////////////////
60
bsalomon682c2692015-05-22 14:01:46 -070061GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext) {
62 GrContextOptions defaultOptions;
63 return Create(backend, backendContext, defaultOptions);
64}
bsalomonf28cff72015-05-22 12:25:41 -070065
bsalomon682c2692015-05-22 14:01:46 -070066GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext,
67 const GrContextOptions& options) {
Brian Salomon91a3e522017-06-23 10:58:19 -040068 sk_sp<GrContext> context(new GrContext);
bsalomon682c2692015-05-22 14:01:46 -070069
Brian Salomon91a3e522017-06-23 10:58:19 -040070 if (!context->init(backend, backendContext, options)) {
halcanary96fcdcc2015-08-27 07:41:13 -070071 return nullptr;
bsalomon@google.com27847de2011-02-22 20:59:41 +000072 }
Brian Salomon91a3e522017-06-23 10:58:19 -040073 return context.release();
bsalomon@google.com27847de2011-02-22 20:59:41 +000074}
75
Greg Daniel02611d92017-07-25 10:05:01 -040076sk_sp<GrContext> GrContext::MakeGL(const GrGLInterface* interface) {
77 GrContextOptions defaultOptions;
78 return MakeGL(interface, defaultOptions);
79}
80
81sk_sp<GrContext> GrContext::MakeGL(const GrGLInterface* interface,
82 const GrContextOptions& options) {
83 sk_sp<GrContext> context(new GrContext);
84 context->fGpu = GrGLGpu::Create(interface, options, context.get());
85 if (!context->fGpu) {
86 return nullptr;
87 }
88 context->fBackend = kOpenGL_GrBackend;
89 if (!context->init(options)) {
90 return nullptr;
91 }
92 return context;
93}
94
95sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions) {
96 GrContextOptions defaultOptions;
97 return MakeMock(mockOptions, defaultOptions);
98}
99
100sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions,
101 const GrContextOptions& options) {
102 sk_sp<GrContext> context(new GrContext);
103 context->fGpu = GrMockGpu::Create(mockOptions, options, context.get());
104 if (!context->fGpu) {
105 return nullptr;
106 }
107 context->fBackend = kMock_GrBackend;
108 if (!context->init(options)) {
109 return nullptr;
110 }
111 return context;
112}
113
114#ifdef SK_VULKAN
115sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext* backendContext) {
116 GrContextOptions defaultOptions;
117 return MakeVulkan(backendContext, defaultOptions);
118}
119
120sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext* backendContext,
121 const GrContextOptions& options) {
122 sk_sp<GrContext> context(new GrContext);
123 context->fGpu = GrVkGpu::Create(backendContext, options, context.get());
124 if (!context->fGpu) {
125 return nullptr;
126 }
127 context->fBackend = kVulkan_GrBackend;
128 if (!context->init(options)) {
129 return nullptr;
130 }
131 return context;
132}
133#endif
134
Greg Danielb76a72a2017-07-13 15:07:54 -0400135#ifdef SK_METAL
Greg Daniel02611d92017-07-25 10:05:01 -0400136sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue) {
137 GrContextOptions defaultOptions;
138 return MakeMetal(device, queue, defaultOptions);
139}
140
Greg Danielb76a72a2017-07-13 15:07:54 -0400141sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue, const GrContextOptions& options) {
142 sk_sp<GrContext> context(new GrContext);
143 context->fGpu = GrMtlTrampoline::CreateGpu(context.get(), options, device, queue);
144 if (!context->fGpu) {
145 return nullptr;
146 }
147 context->fBackend = kMetal_GrBackend;
148 if (!context->init(options)) {
149 return nullptr;
150 }
151 return context;
152}
153#endif
154
joshualitt0acd0d32015-05-07 08:23:19 -0700155static int32_t gNextID = 1;
156static int32_t next_id() {
157 int32_t id;
158 do {
159 id = sk_atomic_inc(&gNextID);
160 } while (id == SK_InvalidGenID);
161 return id;
162}
163
bsalomon682c2692015-05-22 14:01:46 -0700164GrContext::GrContext() : fUniqueID(next_id()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700165 fGpu = nullptr;
166 fCaps = nullptr;
167 fResourceCache = nullptr;
168 fResourceProvider = nullptr;
Brian Salomonf856fd12016-12-16 14:24:34 -0500169 fAtlasGlyphCache = nullptr;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000170}
171
bsalomon682c2692015-05-22 14:01:46 -0700172bool GrContext::init(GrBackend backend, GrBackendContext backendContext,
173 const GrContextOptions& options) {
joshualitt1de610a2016-01-06 08:26:09 -0800174 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700175 SkASSERT(!fGpu);
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000176
Greg Danielfc978fd2017-04-13 09:54:12 -0400177 fBackend = backend;
178
bsalomon682c2692015-05-22 14:01:46 -0700179 fGpu = GrGpu::Create(backend, backendContext, options, this);
robertphillipsea461502015-05-26 11:38:03 -0700180 if (!fGpu) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000181 return false;
182 }
Greg Danielb76a72a2017-07-13 15:07:54 -0400183 return this->init(options);
184}
joshualitt1de610a2016-01-06 08:26:09 -0800185
Greg Danielb76a72a2017-07-13 15:07:54 -0400186bool GrContext::init(const GrContextOptions& options) {
187 ASSERT_SINGLE_OWNER
bsalomon76228632015-05-29 08:02:10 -0700188 fCaps = SkRef(fGpu->caps());
Brian Osman13dddce2017-05-09 13:19:50 -0400189 fResourceCache = new GrResourceCache(fCaps, fUniqueID);
joshualitt6d0872d2016-01-11 08:27:48 -0800190 fResourceProvider = new GrResourceProvider(fGpu, fResourceCache, &fSingleOwner);
commit-bot@chromium.org1836d332013-07-16 22:55:03 +0000191
Brian Osman46da1cc2017-02-14 14:15:48 -0500192 fDisableGpuYUVConversion = options.fDisableGpuYUVConversion;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000193 fDidTestPMConversions = false;
194
bsalomon6b2552f2016-09-15 13:50:26 -0700195 GrPathRendererChain::Options prcOptions;
bsalomon39ef7fb2016-09-21 11:16:05 -0700196 prcOptions.fAllowPathMaskCaching = options.fAllowPathMaskCaching;
csmartdalton008b9d82017-02-22 12:00:42 -0700197 prcOptions.fGpuPathRenderers = options.fGpuPathRenderers;
Robert Phillips3ea17982017-06-02 12:43:04 -0400198 fDrawingManager.reset(new GrDrawingManager(this, prcOptions, &fSingleOwner));
joshualitt7c3a2f82015-03-31 13:32:05 -0700199
Eric Karl6d342282017-05-03 17:08:42 -0700200 fAtlasGlyphCache = new GrAtlasGlyphCache(this, options.fGlyphCacheTextureMaximumBytes);
joshualittb7133be2015-04-08 09:08:31 -0700201
halcanary385fe4d2015-08-26 13:07:48 -0700202 fTextBlobCache.reset(new GrTextBlobCache(TextBlobCacheOverBudgetCB, this));
Brian Salomon91a3e522017-06-23 10:58:19 -0400203
204 return true;
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000205}
206
bsalomon@google.com27847de2011-02-22 20:59:41 +0000207GrContext::~GrContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800208 ASSERT_SINGLE_OWNER
209
robertphillipsea461502015-05-26 11:38:03 -0700210 if (!fGpu) {
bsalomon76228632015-05-29 08:02:10 -0700211 SkASSERT(!fCaps);
bsalomon@google.com733c0622013-04-24 17:59:32 +0000212 return;
213 }
214
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000215 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000216
robertphillips77a2e522015-10-17 07:43:27 -0700217 fDrawingManager->cleanup();
robertphillips2334fb62015-06-17 05:43:33 -0700218
robertphillips@google.com950b1b02013-10-21 17:37:28 +0000219 for (int i = 0; i < fCleanUpData.count(); ++i) {
220 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo);
221 }
222
halcanary385fe4d2015-08-26 13:07:48 -0700223 delete fResourceProvider;
224 delete fResourceCache;
Brian Salomonf856fd12016-12-16 14:24:34 -0500225 delete fAtlasGlyphCache;
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000226
bsalomon@google.com205d4602011-04-25 12:43:45 +0000227 fGpu->unref();
bsalomon76228632015-05-29 08:02:10 -0700228 fCaps->unref();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000229}
230
bungeman6bd52842016-10-27 09:30:08 -0700231sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() {
bsalomon41b952c2016-03-11 06:46:33 -0800232 if (!fThreadSafeProxy) {
bungeman6bd52842016-10-27 09:30:08 -0700233 fThreadSafeProxy.reset(new GrContextThreadSafeProxy(sk_ref_sp(fCaps), this->uniqueID()));
bsalomon41b952c2016-03-11 06:46:33 -0800234 }
bungeman6bd52842016-10-27 09:30:08 -0700235 return fThreadSafeProxy;
bsalomon41b952c2016-03-11 06:46:33 -0800236}
237
bsalomon2354f842014-07-28 13:48:36 -0700238void GrContext::abandonContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800239 ASSERT_SINGLE_OWNER
240
bsalomond309e7a2015-04-30 14:18:54 -0700241 fResourceProvider->abandon();
robertphillips0dfa62c2015-11-16 06:23:31 -0800242
243 // Need to abandon the drawing manager first so all the render targets
244 // will be released/forgotten before they too are abandoned.
245 fDrawingManager->abandon();
246
bsalomon@google.com205d4602011-04-25 12:43:45 +0000247 // abandon first to so destructors
248 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800249 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700250
bsalomon6e2aad42016-04-01 11:54:31 -0700251 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
252
Brian Salomonf856fd12016-12-16 14:24:34 -0500253 fAtlasGlyphCache->freeAll();
bsalomon6e2aad42016-04-01 11:54:31 -0700254 fTextBlobCache->freeAll();
255}
256
257void GrContext::releaseResourcesAndAbandonContext() {
258 ASSERT_SINGLE_OWNER
259
260 fResourceProvider->abandon();
261
262 // Need to abandon the drawing manager first so all the render targets
263 // will be released/forgotten before they too are abandoned.
264 fDrawingManager->abandon();
265
266 // Release all resources in the backend 3D API.
267 fResourceCache->releaseAll();
268
269 fGpu->disconnect(GrGpu::DisconnectType::kCleanup);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000270
Brian Salomonf856fd12016-12-16 14:24:34 -0500271 fAtlasGlyphCache->freeAll();
joshualitt26ffc002015-04-16 11:24:04 -0700272 fTextBlobCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000273}
274
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000275void GrContext::resetContext(uint32_t state) {
joshualitt1de610a2016-01-06 08:26:09 -0800276 ASSERT_SINGLE_OWNER
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000277 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000278}
279
280void GrContext::freeGpuResources() {
joshualitt1de610a2016-01-06 08:26:09 -0800281 ASSERT_SINGLE_OWNER
282
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000283 this->flush();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000284
Brian Salomonf856fd12016-12-16 14:24:34 -0500285 fAtlasGlyphCache->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700286
287 fDrawingManager->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700288
289 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000290}
291
Brian Salomon5e150852017-03-22 14:53:13 -0400292void GrContext::purgeResourcesNotUsedInMs(std::chrono::milliseconds ms) {
293 ASSERT_SINGLE_OWNER
294 fResourceCache->purgeResourcesNotUsedSince(GrStdSteadyClock::now() - ms);
295}
296
Derek Sollenberger5480a182017-05-25 16:43:59 -0400297void GrContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
298 ASSERT_SINGLE_OWNER
299 fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources);
300}
301
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000302void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800303 ASSERT_SINGLE_OWNER
304
bsalomon71cb0c22014-11-14 12:10:14 -0800305 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800306 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800307 }
308 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800309 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800310 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000311}
312
Derek Sollenbergeree479142017-05-24 11:41:33 -0400313size_t GrContext::getResourceCachePurgeableBytes() const {
314 ASSERT_SINGLE_OWNER
315 return fResourceCache->getPurgeableBytes();
316}
317
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000318////////////////////////////////////////////////////////////////////////////////
319
joshualitt0db6dfa2015-04-10 07:01:30 -0700320void GrContext::TextBlobCacheOverBudgetCB(void* data) {
321 SkASSERT(data);
Brian Osman11052242016-10-27 14:47:55 -0400322 // TextBlobs are drawn at the SkGpuDevice level, therefore they cannot rely on
323 // GrRenderTargetContext to perform a necessary flush. The solution is to move drawText calls
324 // to below the GrContext level, but this is not trivial because they call drawPath on
325 // SkGpuDevice.
joshualitt0db6dfa2015-04-10 07:01:30 -0700326 GrContext* context = reinterpret_cast<GrContext*>(data);
327 context->flush();
328}
329
bsalomon@google.com27847de2011-02-22 20:59:41 +0000330////////////////////////////////////////////////////////////////////////////////
331
bsalomonb77a9072016-09-07 10:02:04 -0700332void GrContext::flush() {
joshualitt1de610a2016-01-06 08:26:09 -0800333 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700334 RETURN_IF_ABANDONED
Robert Phillips7ee385e2017-03-30 08:02:11 -0400335
336 fDrawingManager->flush(nullptr);
337}
338
Greg Daniel51316782017-08-02 15:10:09 +0000339GrSemaphoresSubmitted GrContext::flushAndSignalSemaphores(int numSemaphores,
340 GrBackendSemaphore signalSemaphores[]) {
341 ASSERT_SINGLE_OWNER
342 if (fDrawingManager->wasAbandoned()) { return GrSemaphoresSubmitted::kNo; }
343
344 return fDrawingManager->flush(nullptr, numSemaphores, signalSemaphores);
345}
346
Robert Phillips7ee385e2017-03-30 08:02:11 -0400347void GrContextPriv::flush(GrSurfaceProxy* proxy) {
348 ASSERT_SINGLE_OWNER_PRIV
349 RETURN_IF_ABANDONED_PRIV
350 ASSERT_OWNED_PROXY_PRIV(proxy);
351
352 fContext->fDrawingManager->flush(proxy);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000353}
354
bsalomon81beccc2014-10-13 12:32:55 -0700355bool sw_convert_to_premul(GrPixelConfig srcConfig, int width, int height, size_t inRowBytes,
356 const void* inPixels, size_t outRowBytes, void* outPixels) {
Matt Sarettc7b29082017-02-09 16:22:39 -0500357 SkColorType colorType;
358 if (!GrPixelConfigToColorType(srcConfig, &colorType) ||
359 4 != SkColorTypeBytesPerPixel(colorType))
360 {
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000361 return false;
362 }
bsalomon81beccc2014-10-13 12:32:55 -0700363
Matt Sarettc7b29082017-02-09 16:22:39 -0500364 for (int y = 0; y < height; y++) {
365 SkOpts::RGBA_to_rgbA((uint32_t*) outPixels, inPixels, width);
366 outPixels = SkTAddOffset<void>(outPixels, outRowBytes);
367 inPixels = SkTAddOffset<const void>(inPixels, inRowBytes);
368 }
bsalomon81beccc2014-10-13 12:32:55 -0700369
Matt Sarettc7b29082017-02-09 16:22:39 -0500370 return true;
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000371}
372
Brian Osmand2ca59a2017-04-13 14:03:57 -0400373static bool valid_premul_config(GrPixelConfig config) {
Brian Osmance425512017-03-22 14:37:50 -0400374 return GrPixelConfigIs8888Unorm(config) || kRGBA_half_GrPixelConfig == config;
375}
376
Brian Osmand2ca59a2017-04-13 14:03:57 -0400377static bool valid_pixel_conversion(GrPixelConfig srcConfig, GrPixelConfig dstConfig,
378 bool premulConversion) {
379 // We don't allow conversion between integer configs and float/fixed configs.
380 if (GrPixelConfigIsSint(srcConfig) != GrPixelConfigIsSint(dstConfig)) {
381 return false;
382 }
383
384 // We only allow premul <-> unpremul conversions for some formats
385 if (premulConversion && (!valid_premul_config(srcConfig) || !valid_premul_config(dstConfig))) {
386 return false;
387 }
388
389 return true;
390}
391
Brian Osman409e74f2017-04-17 11:48:28 -0400392static bool pm_upm_must_round_trip(GrPixelConfig config, SkColorSpace* colorSpace) {
393 return !colorSpace &&
394 (kRGBA_8888_GrPixelConfig == config || kBGRA_8888_GrPixelConfig == config);
395}
396
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400397bool GrContextPriv::writeSurfacePixels(GrSurfaceContext* dst,
Robert Phillipse78b7252017-04-06 07:59:41 -0400398 int left, int top, int width, int height,
399 GrPixelConfig srcConfig, SkColorSpace* srcColorSpace,
400 const void* buffer, size_t rowBytes,
401 uint32_t pixelOpsFlags) {
Brian Osmanb62ea222016-12-22 11:12:16 -0500402 // TODO: Color space conversion
403
Robert Phillipse78b7252017-04-06 07:59:41 -0400404 ASSERT_SINGLE_OWNER_PRIV
405 RETURN_FALSE_IF_ABANDONED_PRIV
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400406 SkASSERT(dst);
407 ASSERT_OWNED_PROXY_PRIV(dst->asSurfaceProxy());
Brian Salomondcbb9d92017-07-19 10:53:20 -0400408 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "writeSurfacePixels", fContext);
bsalomon6c6f6582015-09-10 08:12:46 -0700409
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400410 if (!dst->asSurfaceProxy()->instantiate(fContext->resourceProvider())) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400411 return false;
412 }
413
Robert Phillips16d8ec62017-07-27 16:16:25 -0400414 GrSurfaceProxy* dstProxy = dst->asSurfaceProxy();
415 GrSurface* dstSurface = dstProxy->priv().peekSurface();
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400416
Brian Osmand2ca59a2017-04-13 14:03:57 -0400417 // The src is unpremul but the dst is premul -> premul the src before or as part of the write
Brian Osman409e74f2017-04-17 11:48:28 -0400418 const bool premul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags);
Robert Phillips16d8ec62017-07-27 16:16:25 -0400419 if (!valid_pixel_conversion(srcConfig, dstProxy->config(), premul)) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400420 return false;
421 }
422
Brian Osman409e74f2017-04-17 11:48:28 -0400423 // We need to guarantee round-trip conversion if we are reading and writing 8888 non-sRGB data,
424 // without any color spaces attached, and the caller wants us to premul.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400425 bool useConfigConversionEffect =
426 premul &&
427 pm_upm_must_round_trip(srcConfig, srcColorSpace) &&
Robert Phillips16d8ec62017-07-27 16:16:25 -0400428 pm_upm_must_round_trip(dstProxy->config(), dst->getColorSpace());
Brian Osman409e74f2017-04-17 11:48:28 -0400429
430 // Are we going to try to premul as part of a draw? For the non-legacy case, we always allow
431 // this. GrConfigConversionEffect fails on some GPUs, so only allow this if it works perfectly.
432 bool premulOnGpu = premul &&
433 (!useConfigConversionEffect || fContext->validPMUPMConversionExists());
bsalomon81beccc2014-10-13 12:32:55 -0700434
bsalomone8d21e82015-07-16 08:23:13 -0700435 // 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 -0700436 // necessary and because GrGpu::getWritePixelsInfo requires it.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400437 if (!GrSurfacePriv::AdjustWritePixelParams(dstSurface->width(), dstSurface->height(),
bsalomone8d21e82015-07-16 08:23:13 -0700438 GrBytesPerPixel(srcConfig), &left, &top, &width,
439 &height, &buffer, &rowBytes)) {
440 return false;
441 }
442
Brian Osman409e74f2017-04-17 11:48:28 -0400443 GrGpu::DrawPreference drawPreference = premulOnGpu ? GrGpu::kCallerPrefersDraw_DrawPreference
444 : GrGpu::kNoDraw_DrawPreference;
bsalomonf0674512015-07-28 13:26:15 -0700445 GrGpu::WritePixelTempDrawInfo tempDrawInfo;
Robert Phillips7294b852017-08-01 13:51:44 +0000446 if (!fContext->fGpu->getWritePixelsInfo(dstSurface, width, height,
Robert Phillips16d8ec62017-07-27 16:16:25 -0400447 srcConfig, &drawPreference, &tempDrawInfo)) {
bsalomonf0674512015-07-28 13:26:15 -0700448 return false;
449 }
450
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400451 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && dstSurface->surfacePriv().hasPendingIO()) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400452 this->flush(nullptr); // MDB TODO: tighten this
bsalomonf0674512015-07-28 13:26:15 -0700453 }
454
Robert Phillips2f493142017-03-02 18:18:38 -0500455 sk_sp<GrTextureProxy> tempProxy;
bsalomonf0674512015-07-28 13:26:15 -0700456 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400457 tempProxy = GrSurfaceProxy::MakeDeferred(fContext->resourceProvider(),
Robert Phillips2f493142017-03-02 18:18:38 -0500458 tempDrawInfo.fTempSurfaceDesc,
459 SkBackingFit::kApprox,
460 SkBudgeted::kYes);
461 if (!tempProxy && GrGpu::kRequireDraw_DrawPreference == drawPreference) {
bsalomonf0674512015-07-28 13:26:15 -0700462 return false;
463 }
464 }
465
466 // temp buffer for doing sw premul conversion, if needed.
467 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
Brian Osman409e74f2017-04-17 11:48:28 -0400468 // We need to do sw premul if we were unable to create a RT for drawing, or if we can't do the
469 // premul on the GPU
470 if (premul && (!tempProxy || !premulOnGpu)) {
471 size_t tmpRowBytes = 4 * width;
472 tmpPixels.reset(width * height);
473 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
474 tmpPixels.get())) {
475 return false;
bsalomonf0674512015-07-28 13:26:15 -0700476 }
Brian Osman409e74f2017-04-17 11:48:28 -0400477 rowBytes = tmpRowBytes;
478 buffer = tmpPixels.get();
bsalomonf0674512015-07-28 13:26:15 -0700479 }
Brian Osman409e74f2017-04-17 11:48:28 -0400480
481 if (tempProxy) {
482 sk_sp<GrFragmentProcessor> fp = GrSimpleTextureEffect::Make(
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400483 tempProxy, nullptr, SkMatrix::I());
Brian Osman409e74f2017-04-17 11:48:28 -0400484 if (premulOnGpu) {
485 fp = fContext->createUPMToPMEffect(std::move(fp), useConfigConversionEffect);
bsalomon81beccc2014-10-13 12:32:55 -0700486 }
Brian Osman409e74f2017-04-17 11:48:28 -0400487 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle);
Robert Phillips1c9686b2017-06-30 08:40:28 -0400488 if (!fp) {
489 return false;
490 }
Brian Osman409e74f2017-04-17 11:48:28 -0400491
492 if (tempProxy->priv().hasPendingIO()) {
493 this->flush(tempProxy.get());
494 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400495 if (!tempProxy->instantiate(fContext->resourceProvider())) {
Brian Osman409e74f2017-04-17 11:48:28 -0400496 return false;
497 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400498 GrTexture* texture = tempProxy->priv().peekTexture();
Robert Phillips7294b852017-08-01 13:51:44 +0000499 if (!fContext->fGpu->writePixels(texture, 0, 0, width, height, tempDrawInfo.fWriteConfig,
500 buffer, rowBytes)) {
Brian Osman409e74f2017-04-17 11:48:28 -0400501 return false;
502 }
503 SkMatrix matrix;
504 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400505 GrRenderTargetContext* renderTargetContext = dst->asRenderTargetContext();
Brian Osman409e74f2017-04-17 11:48:28 -0400506 if (!renderTargetContext) {
507 return false;
508 }
509 GrPaint paint;
510 paint.addColorFragmentProcessor(std::move(fp));
511 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Osman7ab6a7f2017-04-25 15:01:46 -0400512 paint.setAllowSRGBInputs(SkToBool(dst->getColorSpace()) ||
513 GrPixelConfigIsSRGB(renderTargetContext->config()));
Brian Osman409e74f2017-04-17 11:48:28 -0400514 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
515 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, matrix, rect,
516 nullptr);
517
518 if (kFlushWrites_PixelOp & pixelOpsFlags) {
519 this->flushSurfaceWrites(renderTargetContext->asRenderTargetProxy());
520 }
521 } else {
Robert Phillips7294b852017-08-01 13:51:44 +0000522 return fContext->fGpu->writePixels(dstSurface, left, top, width, height, srcConfig,
523 buffer, rowBytes);
bsalomon81beccc2014-10-13 12:32:55 -0700524 }
bsalomon81beccc2014-10-13 12:32:55 -0700525 return true;
526}
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000527
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400528bool GrContextPriv::readSurfacePixels(GrSurfaceContext* src,
Robert Phillipse78b7252017-04-06 07:59:41 -0400529 int left, int top, int width, int height,
530 GrPixelConfig dstConfig, SkColorSpace* dstColorSpace,
531 void* buffer, size_t rowBytes, uint32_t flags) {
Brian Osmanb62ea222016-12-22 11:12:16 -0500532 // TODO: Color space conversion
533
Robert Phillipse78b7252017-04-06 07:59:41 -0400534 ASSERT_SINGLE_OWNER_PRIV
535 RETURN_FALSE_IF_ABANDONED_PRIV
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400536 SkASSERT(src);
537 ASSERT_OWNED_PROXY_PRIV(src->asSurfaceProxy());
Brian Salomondcbb9d92017-07-19 10:53:20 -0400538 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "readSurfacePixels", fContext);
bsalomon32ab2602015-09-09 18:57:49 -0700539
Robert Phillipse78b7252017-04-06 07:59:41 -0400540 // MDB TODO: delay this instantiation until later in the method
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400541 if (!src->asSurfaceProxy()->instantiate(fContext->resourceProvider())) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400542 return false;
543 }
544
Robert Phillips16d8ec62017-07-27 16:16:25 -0400545 GrSurfaceProxy* srcProxy = src->asSurfaceProxy();
546 GrSurface* srcSurface = srcProxy->priv().peekSurface();
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400547
Brian Osmand2ca59a2017-04-13 14:03:57 -0400548 // The src is premul but the dst is unpremul -> unpremul the src after or as part of the read
549 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
Robert Phillips16d8ec62017-07-27 16:16:25 -0400550 if (!valid_pixel_conversion(srcProxy->config(), dstConfig, unpremul)) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400551 return false;
552 }
553
Brian Osman409e74f2017-04-17 11:48:28 -0400554 // We need to guarantee round-trip conversion if we are reading and writing 8888 non-sRGB data,
555 // without any color spaces attached, and the caller wants us to unpremul.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400556 bool useConfigConversionEffect =
557 unpremul &&
Robert Phillips16d8ec62017-07-27 16:16:25 -0400558 pm_upm_must_round_trip(srcProxy->config(), src->getColorSpace()) &&
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400559 pm_upm_must_round_trip(dstConfig, dstColorSpace);
Brian Osman409e74f2017-04-17 11:48:28 -0400560
561 // Are we going to try to unpremul as part of a draw? For the non-legacy case, we always allow
562 // this. GrConfigConversionEffect fails on some GPUs, so only allow this if it works perfectly.
563 bool unpremulOnGpu = unpremul &&
564 (!useConfigConversionEffect || fContext->validPMUPMConversionExists());
bsalomon6c6f6582015-09-10 08:12:46 -0700565
bsalomone8d21e82015-07-16 08:23:13 -0700566 // Adjust the params so that if we wind up using an intermediate surface we've already done
567 // all the trimming and the temporary can be the min size required.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400568 if (!GrSurfacePriv::AdjustReadPixelParams(srcSurface->width(), srcSurface->height(),
bsalomone8d21e82015-07-16 08:23:13 -0700569 GrBytesPerPixel(dstConfig), &left,
570 &top, &width, &height, &buffer, &rowBytes)) {
571 return false;
572 }
573
Brian Osman409e74f2017-04-17 11:48:28 -0400574 GrGpu::DrawPreference drawPreference = unpremulOnGpu ? GrGpu::kCallerPrefersDraw_DrawPreference
575 : GrGpu::kNoDraw_DrawPreference;
bsalomon39826022015-07-23 08:07:21 -0700576 GrGpu::ReadPixelTempDrawInfo tempDrawInfo;
Robert Phillips7294b852017-08-01 13:51:44 +0000577 if (!fContext->fGpu->getReadPixelsInfo(srcSurface, width, height, rowBytes,
Robert Phillips16d8ec62017-07-27 16:16:25 -0400578 dstConfig, &drawPreference, &tempDrawInfo)) {
bsalomon39826022015-07-23 08:07:21 -0700579 return false;
580 }
bsalomon191bcc02014-11-14 11:31:13 -0800581
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400582 if (!(kDontFlush_PixelOpsFlag & flags) && srcSurface->surfacePriv().hasPendingWrite()) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400583 this->flush(nullptr); // MDB TODO: tighten this
584 }
585
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400586 sk_sp<GrSurfaceProxy> proxyToRead = src->asSurfaceProxyRef();
bsalomon39826022015-07-23 08:07:21 -0700587 bool didTempDraw = false;
588 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
bsalomonb117ff12016-07-19 07:24:40 -0700589 if (SkBackingFit::kExact == tempDrawInfo.fTempSurfaceFit) {
bsalomon39826022015-07-23 08:07:21 -0700590 // We only respect this when the entire src is being read. Otherwise we can trigger too
591 // many odd ball texture sizes and trash the cache.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400592 if (width != srcSurface->width() || height != srcSurface->height()) {
bsalomonb117ff12016-07-19 07:24:40 -0700593 tempDrawInfo.fTempSurfaceFit= SkBackingFit::kApprox;
bsalomon39826022015-07-23 08:07:21 -0700594 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000595 }
brianosmandfe4f2e2016-07-21 13:28:36 -0700596 // TODO: Need to decide the semantics of this function for color spaces. Do we support
597 // conversion to a passed-in color space? For now, specifying nullptr means that this
598 // path will do no conversion, so it will match the behavior of the non-draw path.
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400599 sk_sp<GrRenderTargetContext> tempRTC = fContext->makeDeferredRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400600 tempDrawInfo.fTempSurfaceFit,
bsalomonb117ff12016-07-19 07:24:40 -0700601 tempDrawInfo.fTempSurfaceDesc.fWidth,
602 tempDrawInfo.fTempSurfaceDesc.fHeight,
603 tempDrawInfo.fTempSurfaceDesc.fConfig,
brianosmandfe4f2e2016-07-21 13:28:36 -0700604 nullptr,
bsalomonb117ff12016-07-19 07:24:40 -0700605 tempDrawInfo.fTempSurfaceDesc.fSampleCnt,
606 tempDrawInfo.fTempSurfaceDesc.fOrigin);
Brian Osman693a5402016-10-27 15:13:22 -0400607 if (tempRTC) {
Robert Phillips67c18d62017-01-20 12:44:06 -0500608 SkMatrix textureMatrix = SkMatrix::MakeTrans(SkIntToScalar(left), SkIntToScalar(top));
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400609 sk_sp<GrTextureProxy> proxy = src->asTextureProxyRef();
Brian Osman409e74f2017-04-17 11:48:28 -0400610 sk_sp<GrFragmentProcessor> fp = GrSimpleTextureEffect::Make(
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400611 std::move(proxy), nullptr, textureMatrix);
Brian Osman409e74f2017-04-17 11:48:28 -0400612 if (unpremulOnGpu) {
613 fp = fContext->createPMToUPMEffect(std::move(fp), useConfigConversionEffect);
614 // We no longer need to do this on CPU after the read back.
615 unpremul = false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000616 }
Brian Osman409e74f2017-04-17 11:48:28 -0400617 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle);
Robert Phillips1c9686b2017-06-30 08:40:28 -0400618 if (!fp) {
619 return false;
620 }
Brian Osman60cd57e2017-04-06 10:19:06 -0400621
Brian Osman409e74f2017-04-17 11:48:28 -0400622 GrPaint paint;
623 paint.addColorFragmentProcessor(std::move(fp));
624 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
625 paint.setAllowSRGBInputs(true);
626 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
627 tempRTC->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect,
628 nullptr);
629 proxyToRead = tempRTC->asTextureProxyRef();
630 left = 0;
631 top = 0;
632 didTempDraw = true;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000633 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000634 }
joshualitt5c55fef2014-10-31 14:04:35 -0700635
Robert Phillipse78b7252017-04-06 07:59:41 -0400636 if (!proxyToRead) {
637 return false;
638 }
639
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400640 if (!proxyToRead->instantiate(fContext->resourceProvider())) {
Robert Phillips833dcf42016-11-18 08:44:13 -0500641 return false;
642 }
643
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400644 GrSurface* surfaceToRead = proxyToRead->priv().peekSurface();
645
bsalomon39826022015-07-23 08:07:21 -0700646 if (GrGpu::kRequireDraw_DrawPreference == drawPreference && !didTempDraw) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000647 return false;
648 }
bsalomon39826022015-07-23 08:07:21 -0700649 GrPixelConfig configToRead = dstConfig;
650 if (didTempDraw) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400651 this->flushSurfaceWrites(proxyToRead.get());
bsalomon6c9cd552016-01-22 07:17:34 -0800652 configToRead = tempDrawInfo.fReadConfig;
bsalomon39826022015-07-23 08:07:21 -0700653 }
Robert Phillips7294b852017-08-01 13:51:44 +0000654 if (!fContext->fGpu->readPixels(surfaceToRead,
Robert Phillips16d8ec62017-07-27 16:16:25 -0400655 left, top, width, height, configToRead, buffer, rowBytes)) {
bsalomon39826022015-07-23 08:07:21 -0700656 return false;
657 }
658
659 // Perform umpremul conversion if we weren't able to perform it as a draw.
660 if (unpremul) {
Matt Sarettc7b29082017-02-09 16:22:39 -0500661 SkColorType colorType;
662 if (!GrPixelConfigToColorType(dstConfig, &colorType) ||
663 4 != SkColorTypeBytesPerPixel(colorType))
664 {
reed@google.com7111d462014-03-25 16:20:24 +0000665 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000666 }
reed@google.com7111d462014-03-25 16:20:24 +0000667
Matt Sarettc7b29082017-02-09 16:22:39 -0500668 for (int y = 0; y < height; y++) {
669 SkUnpremultiplyRow<false>((uint32_t*) buffer, (const uint32_t*) buffer, width);
670 buffer = SkTAddOffset<void>(buffer, rowBytes);
671 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000672 }
673 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000674}
675
Robert Phillips7ee385e2017-03-30 08:02:11 -0400676void GrContextPriv::prepareSurfaceForExternalIO(GrSurfaceProxy* proxy) {
677 ASSERT_SINGLE_OWNER_PRIV
678 RETURN_IF_ABANDONED_PRIV
679 SkASSERT(proxy);
680 ASSERT_OWNED_PROXY_PRIV(proxy);
Greg Daniel51316782017-08-02 15:10:09 +0000681 fContext->fDrawingManager->prepareSurfaceForExternalIO(proxy, 0, nullptr);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000682}
683
Robert Phillips7ee385e2017-03-30 08:02:11 -0400684void GrContextPriv::flushSurfaceWrites(GrSurfaceProxy* proxy) {
685 ASSERT_SINGLE_OWNER_PRIV
686 RETURN_IF_ABANDONED_PRIV
687 SkASSERT(proxy);
688 ASSERT_OWNED_PROXY_PRIV(proxy);
689 if (proxy->priv().hasPendingWrite()) {
690 this->flush(proxy);
bsalomonf80bfed2014-10-07 05:56:02 -0700691 }
692}
693
Robert Phillips7ee385e2017-03-30 08:02:11 -0400694void GrContextPriv::flushSurfaceIO(GrSurfaceProxy* proxy) {
695 ASSERT_SINGLE_OWNER_PRIV
696 RETURN_IF_ABANDONED_PRIV
697 SkASSERT(proxy);
698 ASSERT_OWNED_PROXY_PRIV(proxy);
699 if (proxy->priv().hasPendingIO()) {
700 this->flush(proxy);
ajuma95243eb2016-08-24 08:19:02 -0700701 }
702}
703
bsalomon@google.com27847de2011-02-22 20:59:41 +0000704////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000705int GrContext::getRecommendedSampleCount(GrPixelConfig config,
706 SkScalar dpi) const {
joshualitt1de610a2016-01-06 08:26:09 -0800707 ASSERT_SINGLE_OWNER
708
bsalomon76228632015-05-29 08:02:10 -0700709 if (!this->caps()->isConfigRenderable(config, true)) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000710 return 0;
711 }
712 int chosenSampleCount = 0;
jvanverthe9c0fc62015-04-29 11:18:05 -0700713 if (fGpu->caps()->shaderCaps()->pathRenderingSupport()) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000714 if (dpi >= 250.0f) {
715 chosenSampleCount = 4;
716 } else {
717 chosenSampleCount = 16;
718 }
719 }
Greg Daniel81e7bf82017-07-19 14:47:42 -0400720 int supportedSampleCount = fGpu->caps()->getSampleCount(chosenSampleCount, config);
721 return chosenSampleCount <= supportedSampleCount ? supportedSampleCount : 0;
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000722}
723
Robert Phillips2c862492017-01-18 10:08:39 -0500724sk_sp<GrSurfaceContext> GrContextPriv::makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy> proxy,
725 sk_sp<SkColorSpace> colorSpace) {
Brian Osman45580d32016-11-23 09:37:01 -0500726 ASSERT_SINGLE_OWNER_PRIV
727
Brian Osman45580d32016-11-23 09:37:01 -0500728 if (proxy->asRenderTargetProxy()) {
Robert Phillips2c862492017-01-18 10:08:39 -0500729 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
730 std::move(colorSpace), nullptr);
Brian Osman45580d32016-11-23 09:37:01 -0500731 } else {
732 SkASSERT(proxy->asTextureProxy());
Robert Phillips2c862492017-01-18 10:08:39 -0500733 return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
Brian Osman45580d32016-11-23 09:37:01 -0500734 }
735}
736
Robert Phillipse2f7d182016-12-15 09:23:05 -0500737sk_sp<GrSurfaceContext> GrContextPriv::makeDeferredSurfaceContext(const GrSurfaceDesc& dstDesc,
738 SkBackingFit fit,
739 SkBudgeted isDstBudgeted) {
740
Brian Osman32342f02017-03-04 08:12:46 -0500741 sk_sp<GrTextureProxy> proxy = GrSurfaceProxy::MakeDeferred(fContext->resourceProvider(),
Robert Phillips26c90e02017-03-14 14:39:29 -0400742 dstDesc, fit, isDstBudgeted);
Robert Phillips77b3f322017-01-31 18:24:12 -0500743 if (!proxy) {
744 return nullptr;
745 }
Robert Phillipse2f7d182016-12-15 09:23:05 -0500746
Robert Phillips2c862492017-01-18 10:08:39 -0500747 return this->makeWrappedSurfaceContext(std::move(proxy), nullptr);
Robert Phillipse2f7d182016-12-15 09:23:05 -0500748}
749
Brian Salomond17f6582017-07-19 18:28:58 -0400750sk_sp<GrTextureContext> GrContextPriv::makeBackendTextureContext(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000751 GrSurfaceOrigin origin,
Brian Osmanc1e37052017-03-09 14:19:20 -0500752 sk_sp<SkColorSpace> colorSpace) {
Robert Phillips26caf892017-01-27 10:58:31 -0500753 ASSERT_SINGLE_OWNER_PRIV
754
Robert Phillips7294b852017-08-01 13:51:44 +0000755 sk_sp<GrSurface> surface(fContext->resourceProvider()->wrapBackendTexture(tex, origin));
Robert Phillips26caf892017-01-27 10:58:31 -0500756 if (!surface) {
757 return nullptr;
758 }
759
Robert Phillips066f0202017-07-25 10:16:35 -0400760 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface), origin));
Robert Phillips77b3f322017-01-31 18:24:12 -0500761 if (!proxy) {
762 return nullptr;
763 }
Robert Phillips26caf892017-01-27 10:58:31 -0500764
Brian Salomond17f6582017-07-19 18:28:58 -0400765 return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
Robert Phillips26caf892017-01-27 10:58:31 -0500766}
767
Brian Osman11052242016-10-27 14:47:55 -0400768sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000769 const GrBackendTexture& tex,
770 GrSurfaceOrigin origin,
771 int sampleCnt,
Brian Osman11052242016-10-27 14:47:55 -0400772 sk_sp<SkColorSpace> colorSpace,
Brian Osmanc1e37052017-03-09 14:19:20 -0500773 const SkSurfaceProps* props) {
robertphillips4fd74ae2016-08-03 14:26:53 -0700774 ASSERT_SINGLE_OWNER_PRIV
robertphillips4fd74ae2016-08-03 14:26:53 -0700775
Brian Salomond17f6582017-07-19 18:28:58 -0400776 sk_sp<GrSurface> surface(
Robert Phillips7294b852017-08-01 13:51:44 +0000777 fContext->resourceProvider()->wrapRenderableBackendTexture(tex, origin, sampleCnt));
robertphillips4fd74ae2016-08-03 14:26:53 -0700778 if (!surface) {
779 return nullptr;
780 }
781
Robert Phillips066f0202017-07-25 10:16:35 -0400782 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface), origin));
Robert Phillips77b3f322017-01-31 18:24:12 -0500783 if (!proxy) {
784 return nullptr;
785 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400786
Robert Phillips37430132016-11-09 06:50:43 -0500787 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Brian Osman11052242016-10-27 14:47:55 -0400788 std::move(colorSpace), props);
robertphillips4fd74ae2016-08-03 14:26:53 -0700789}
790
Brian Osman11052242016-10-27 14:47:55 -0400791sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendRenderTargetRenderTargetContext(
Greg Danielbcf612b2017-05-01 13:50:58 +0000792 const GrBackendRenderTarget& backendRT,
793 GrSurfaceOrigin origin,
robertphillips4fd74ae2016-08-03 14:26:53 -0700794 sk_sp<SkColorSpace> colorSpace,
795 const SkSurfaceProps* surfaceProps) {
796 ASSERT_SINGLE_OWNER_PRIV
797
Robert Phillips7294b852017-08-01 13:51:44 +0000798 sk_sp<GrRenderTarget> rt(fContext->resourceProvider()->wrapBackendRenderTarget(backendRT,
799 origin));
robertphillips4fd74ae2016-08-03 14:26:53 -0700800 if (!rt) {
801 return nullptr;
802 }
803
Robert Phillips066f0202017-07-25 10:16:35 -0400804 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(rt), origin));
Robert Phillips77b3f322017-01-31 18:24:12 -0500805 if (!proxy) {
806 return nullptr;
807 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400808
Robert Phillips37430132016-11-09 06:50:43 -0500809 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400810 std::move(colorSpace),
Brian Osman11052242016-10-27 14:47:55 -0400811 surfaceProps);
robertphillips4fd74ae2016-08-03 14:26:53 -0700812}
813
Brian Osman11052242016-10-27 14:47:55 -0400814sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureAsRenderTargetRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000815 const GrBackendTexture& tex,
816 GrSurfaceOrigin origin,
817 int sampleCnt,
robertphillips4fd74ae2016-08-03 14:26:53 -0700818 sk_sp<SkColorSpace> colorSpace,
819 const SkSurfaceProps* surfaceProps) {
820 ASSERT_SINGLE_OWNER_PRIV
robertphillips4fd74ae2016-08-03 14:26:53 -0700821
Greg Daniel7ef28f32017-04-20 16:41:55 +0000822 sk_sp<GrSurface> surface(fContext->resourceProvider()->wrapBackendTextureAsRenderTarget(
823 tex,
Robert Phillips7294b852017-08-01 13:51:44 +0000824 origin,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000825 sampleCnt));
robertphillips4fd74ae2016-08-03 14:26:53 -0700826 if (!surface) {
827 return nullptr;
828 }
829
Robert Phillips066f0202017-07-25 10:16:35 -0400830 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface), origin));
Robert Phillips77b3f322017-01-31 18:24:12 -0500831 if (!proxy) {
832 return nullptr;
833 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400834
Robert Phillips37430132016-11-09 06:50:43 -0500835 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400836 std::move(colorSpace),
837 surfaceProps);
robertphillips77a2e522015-10-17 07:43:27 -0700838}
839
Chris Daltonfe199b72017-05-05 11:26:15 -0400840void GrContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
841 fContext->fDrawingManager->addOnFlushCallbackObject(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400842}
843
844
robertphillips48fde9c2016-09-06 05:20:20 -0700845static inline GrPixelConfig GrPixelConfigFallback(GrPixelConfig config) {
Brian Osman78f20e02017-01-12 10:28:01 -0500846 switch (config) {
847 case kAlpha_8_GrPixelConfig:
848 case kRGB_565_GrPixelConfig:
849 case kRGBA_4444_GrPixelConfig:
850 case kBGRA_8888_GrPixelConfig:
851 return kRGBA_8888_GrPixelConfig;
852 case kSBGRA_8888_GrPixelConfig:
853 return kSRGBA_8888_GrPixelConfig;
854 case kAlpha_half_GrPixelConfig:
855 return kRGBA_half_GrPixelConfig;
856 default:
857 return kUnknown_GrPixelConfig;
858 }
robertphillips48fde9c2016-09-06 05:20:20 -0700859}
860
robertphillipsd728f0c2016-11-21 11:05:03 -0800861sk_sp<GrRenderTargetContext> GrContext::makeDeferredRenderTargetContextWithFallback(
862 SkBackingFit fit,
863 int width, int height,
864 GrPixelConfig config,
865 sk_sp<SkColorSpace> colorSpace,
866 int sampleCnt,
867 GrSurfaceOrigin origin,
868 const SkSurfaceProps* surfaceProps,
869 SkBudgeted budgeted) {
870 if (!this->caps()->isConfigRenderable(config, sampleCnt > 0)) {
871 config = GrPixelConfigFallback(config);
872 }
873
874 return this->makeDeferredRenderTargetContext(fit, width, height, config, std::move(colorSpace),
875 sampleCnt, origin, surfaceProps, budgeted);
876}
877
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400878sk_sp<GrRenderTargetContext> GrContext::makeDeferredRenderTargetContext(
879 SkBackingFit fit,
880 int width, int height,
881 GrPixelConfig config,
882 sk_sp<SkColorSpace> colorSpace,
883 int sampleCnt,
884 GrSurfaceOrigin origin,
885 const SkSurfaceProps* surfaceProps,
886 SkBudgeted budgeted) {
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400887 SkASSERT(kDefault_GrSurfaceOrigin != origin);
888
Brian Salomon79e4d1b2017-07-13 11:17:11 -0400889 if (this->abandoned()) {
890 return nullptr;
891 }
892
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400893 GrSurfaceDesc desc;
894 desc.fFlags = kRenderTarget_GrSurfaceFlag;
895 desc.fOrigin = origin;
896 desc.fWidth = width;
897 desc.fHeight = height;
898 desc.fConfig = config;
899 desc.fSampleCnt = sampleCnt;
900
Brian Osman32342f02017-03-04 08:12:46 -0500901 sk_sp<GrTextureProxy> rtp = GrSurfaceProxy::MakeDeferred(this->resourceProvider(),
Robert Phillips26c90e02017-03-14 14:39:29 -0400902 desc, fit, budgeted);
Robert Phillips08c5ec72017-01-30 12:26:47 -0500903 if (!rtp) {
904 return nullptr;
905 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400906
Robert Phillips1119dc32017-04-11 12:54:57 -0400907 sk_sp<GrRenderTargetContext> renderTargetContext(
908 fDrawingManager->makeRenderTargetContext(std::move(rtp),
909 std::move(colorSpace),
910 surfaceProps));
Robert Phillips1119dc32017-04-11 12:54:57 -0400911 if (!renderTargetContext) {
912 return nullptr;
913 }
914
915 renderTargetContext->discard();
916
917 return renderTargetContext;
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400918}
919
joshualitt1de610a2016-01-06 08:26:09 -0800920bool GrContext::abandoned() const {
921 ASSERT_SINGLE_OWNER
robertphillips7761d612016-05-16 09:14:53 -0700922 return fDrawingManager->wasAbandoned();
robertphillips77a2e522015-10-17 07:43:27 -0700923}
924
Brian Osman60cd57e2017-04-06 10:19:06 -0400925sk_sp<GrFragmentProcessor> GrContext::createPMToUPMEffect(sk_sp<GrFragmentProcessor> fp,
Brian Osman409e74f2017-04-17 11:48:28 -0400926 bool useConfigConversionEffect) {
Robert Phillips757914d2017-01-25 15:48:30 -0500927 ASSERT_SINGLE_OWNER
Brian Osman409e74f2017-04-17 11:48:28 -0400928 // We have specialized effects that guarantee round-trip conversion for some formats
929 if (useConfigConversionEffect) {
930 // We should have already called this->validPMUPMConversionExists() in this case
931 SkASSERT(fDidTestPMConversions);
932 // ...and it should have succeeded
933 SkASSERT(this->validPMUPMConversionExists());
934
Brian Osman28804f32017-04-20 10:24:36 -0400935 return GrConfigConversionEffect::Make(std::move(fp),
936 GrConfigConversionEffect::kToUnpremul_PMConversion);
Brian Osman2d2da4f2017-04-12 17:07:22 -0400937 } else {
938 // For everything else (sRGB, half-float, etc...), it doesn't make sense to try and
939 // explicitly round the results. Just do the obvious, naive thing in the shader.
940 return GrFragmentProcessor::UnpremulOutput(std::move(fp));
Robert Phillips757914d2017-01-25 15:48:30 -0500941 }
942}
943
Brian Osman60cd57e2017-04-06 10:19:06 -0400944sk_sp<GrFragmentProcessor> GrContext::createUPMToPMEffect(sk_sp<GrFragmentProcessor> fp,
Brian Osman409e74f2017-04-17 11:48:28 -0400945 bool useConfigConversionEffect) {
joshualitt1de610a2016-01-06 08:26:09 -0800946 ASSERT_SINGLE_OWNER
Brian Osman2d2da4f2017-04-12 17:07:22 -0400947 // We have specialized effects that guarantee round-trip conversion for these formats
Brian Osman409e74f2017-04-17 11:48:28 -0400948 if (useConfigConversionEffect) {
949 // We should have already called this->validPMUPMConversionExists() in this case
950 SkASSERT(fDidTestPMConversions);
951 // ...and it should have succeeded
952 SkASSERT(this->validPMUPMConversionExists());
953
Brian Osman28804f32017-04-20 10:24:36 -0400954 return GrConfigConversionEffect::Make(std::move(fp),
955 GrConfigConversionEffect::kToPremul_PMConversion);
Brian Osman2d2da4f2017-04-12 17:07:22 -0400956 } else {
957 // For everything else (sRGB, half-float, etc...), it doesn't make sense to try and
958 // explicitly round the results. Just do the obvious, naive thing in the shader.
959 return GrFragmentProcessor::PremulOutput(std::move(fp));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000960 }
961}
962
Brian Osman409e74f2017-04-17 11:48:28 -0400963bool GrContext::validPMUPMConversionExists() {
joshualitt1de610a2016-01-06 08:26:09 -0800964 ASSERT_SINGLE_OWNER
Brian Osman409e74f2017-04-17 11:48:28 -0400965 if (!fDidTestPMConversions) {
Brian Osman28804f32017-04-20 10:24:36 -0400966 fPMUPMConversionsRoundTrip = GrConfigConversionEffect::TestForPreservingPMConversions(this);
Brian Osman409e74f2017-04-17 11:48:28 -0400967 fDidTestPMConversions = true;
968 }
969
bsalomon636e8022015-07-29 06:08:46 -0700970 // The PM<->UPM tests fail or succeed together so we only need to check one.
Brian Osman28804f32017-04-20 10:24:36 -0400971 return fPMUPMConversionsRoundTrip;
bsalomon636e8022015-07-29 06:08:46 -0700972}
973
bsalomon37f9a262015-02-02 13:00:10 -0800974//////////////////////////////////////////////////////////////////////////////
975
976void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800977 ASSERT_SINGLE_OWNER
bsalomon37f9a262015-02-02 13:00:10 -0800978 if (maxTextures) {
bsalomon0ea80f42015-02-11 10:49:59 -0800979 *maxTextures = fResourceCache->getMaxResourceCount();
bsalomon37f9a262015-02-02 13:00:10 -0800980 }
981 if (maxTextureBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800982 *maxTextureBytes = fResourceCache->getMaxResourceBytes();
bsalomon37f9a262015-02-02 13:00:10 -0800983 }
984}
985
986void GrContext::setResourceCacheLimits(int maxTextures, size_t maxTextureBytes) {
joshualitt1de610a2016-01-06 08:26:09 -0800987 ASSERT_SINGLE_OWNER
bsalomon0ea80f42015-02-11 10:49:59 -0800988 fResourceCache->setLimits(maxTextures, maxTextureBytes);
bsalomon37f9a262015-02-02 13:00:10 -0800989}
990
ericrk0a5fa482015-09-15 14:16:10 -0700991//////////////////////////////////////////////////////////////////////////////
992
993void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -0800994 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -0700995 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
996}
Brian Osman175af0d2017-08-09 14:29:36 -0400997
998//////////////////////////////////////////////////////////////////////////////
999
1000SkString GrContext::dump() const {
1001 SkDynamicMemoryWStream stream;
1002 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
1003 writer.beginObject();
1004
1005 static const char* kBackendStr[] = {
1006 "Metal",
1007 "OpenGL",
1008 "Vulkan",
1009 "Mock",
1010 };
1011 GR_STATIC_ASSERT(0 == kMetal_GrBackend);
1012 GR_STATIC_ASSERT(1 == kOpenGL_GrBackend);
1013 GR_STATIC_ASSERT(2 == kVulkan_GrBackend);
1014 GR_STATIC_ASSERT(3 == kMock_GrBackend);
1015 writer.appendString("backend", kBackendStr[fBackend]);
1016
1017 writer.appendName("caps");
1018 fCaps->dumpJSON(&writer);
1019
1020 writer.appendName("gpu");
1021 fGpu->dumpJSON(&writer);
1022
1023 // Flush JSON to the memory stream
1024 writer.endObject();
1025 writer.flush();
1026
1027 // Null terminate the JSON data in the memory stream
1028 stream.write8(0);
1029
1030 // Allocate a string big enough to hold all the data, then copy out of the stream
1031 SkString result(stream.bytesWritten());
1032 stream.copyToAndReset(result.writable_str());
1033 return result;
1034}