bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * 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.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 8 | #include "GrBackendSemaphore.h" |
bsalomon@google.com | 1fadb20 | 2011-12-12 16:10:08 +0000 | [diff] [blame] | 9 | #include "GrContext.h" |
Brian Salomon | c65aec9 | 2017-03-09 09:03:58 -0500 | [diff] [blame] | 10 | #include "GrClip.h" |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 11 | #include "GrContextOptions.h" |
Brian Salomon | c65aec9 | 2017-03-09 09:03:58 -0500 | [diff] [blame] | 12 | #include "GrContextPriv.h" |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 13 | #include "GrDrawingManager.h" |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 14 | #include "GrGpu.h" |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 15 | #include "GrRenderTargetContext.h" |
Brian Salomon | c65aec9 | 2017-03-09 09:03:58 -0500 | [diff] [blame] | 16 | #include "GrRenderTargetProxy.h" |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 17 | #include "GrResourceCache.h" |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 18 | #include "GrResourceProvider.h" |
Greg Daniel | d85f97d | 2017-03-07 13:37:21 -0500 | [diff] [blame] | 19 | #include "GrSemaphore.h" |
robertphillips@google.com | 72176b2 | 2012-05-23 13:19:12 +0000 | [diff] [blame] | 20 | #include "GrSoftwarePathRenderer.h" |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 21 | #include "GrSurfaceContext.h" |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 22 | #include "GrSurfacePriv.h" |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame] | 23 | #include "GrSurfaceProxyPriv.h" |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 24 | #include "GrTexture.h" |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 25 | #include "GrTextureContext.h" |
Brian Salomon | dcbb9d9 | 2017-07-19 10:53:20 -0400 | [diff] [blame] | 26 | #include "GrTracing.h" |
Matt Sarett | 485c499 | 2017-02-14 14:18:27 -0500 | [diff] [blame] | 27 | #include "SkConvertPixels.h" |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 28 | #include "SkGr.h" |
Brian Osman | 71a1889 | 2017-08-10 10:23:25 -0400 | [diff] [blame] | 29 | #include "SkJSONWriter.h" |
Brian Osman | 5127998 | 2017-08-23 10:12:00 -0400 | [diff] [blame] | 30 | #include "SkMakeUnique.h" |
| 31 | #include "SkTaskGroup.h" |
Matt Sarett | c7b2908 | 2017-02-09 16:22:39 -0500 | [diff] [blame] | 32 | #include "SkUnPreMultiplyPriv.h" |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 33 | #include "effects/GrConfigConversionEffect.h" |
joshualitt | e804292 | 2015-12-11 06:11:21 -0800 | [diff] [blame] | 34 | #include "text/GrTextBlobCache.h" |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 35 | |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 36 | #include "gl/GrGLGpu.h" |
| 37 | #include "mock/GrMockGpu.h" |
Greg Daniel | b76a72a | 2017-07-13 15:07:54 -0400 | [diff] [blame] | 38 | #ifdef SK_METAL |
| 39 | #include "mtl/GrMtlTrampoline.h" |
| 40 | #endif |
Robert Phillips | e42edcc | 2017-12-13 11:50:22 -0500 | [diff] [blame] | 41 | #include "ddl/GrDDLGpu.h" |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 42 | #ifdef SK_VULKAN |
| 43 | #include "vk/GrVkGpu.h" |
| 44 | #endif |
Greg Daniel | b76a72a | 2017-07-13 15:07:54 -0400 | [diff] [blame] | 45 | |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 46 | #define ASSERT_OWNED_PROXY(P) \ |
| 47 | SkASSERT(!(P) || !((P)->priv().peekTexture()) || (P)->priv().peekTexture()->getContext() == this) |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 48 | #define ASSERT_OWNED_PROXY_PRIV(P) \ |
| 49 | SkASSERT(!(P) || !((P)->priv().peekTexture()) || (P)->priv().peekTexture()->getContext() == fContext) |
| 50 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 51 | #define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this) |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 52 | #define ASSERT_SINGLE_OWNER \ |
| 53 | SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fSingleOwner);) |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 54 | #define ASSERT_SINGLE_OWNER_PRIV \ |
| 55 | SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fContext->fSingleOwner);) |
robertphillips | 7761d61 | 2016-05-16 09:14:53 -0700 | [diff] [blame] | 56 | #define RETURN_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return; } |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 57 | #define RETURN_IF_ABANDONED_PRIV if (fContext->fDrawingManager->wasAbandoned()) { return; } |
robertphillips | 7761d61 | 2016-05-16 09:14:53 -0700 | [diff] [blame] | 58 | #define RETURN_FALSE_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return false; } |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 59 | #define RETURN_FALSE_IF_ABANDONED_PRIV if (fContext->fDrawingManager->wasAbandoned()) { return false; } |
robertphillips | 7761d61 | 2016-05-16 09:14:53 -0700 | [diff] [blame] | 60 | #define RETURN_NULL_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return nullptr; } |
bsalomon@google.com | bc4b654 | 2011-11-19 13:56:11 +0000 | [diff] [blame] | 61 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 62 | //////////////////////////////////////////////////////////////////////////////// |
| 63 | |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 64 | GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext) { |
| 65 | GrContextOptions defaultOptions; |
| 66 | return Create(backend, backendContext, defaultOptions); |
| 67 | } |
bsalomon | f28cff7 | 2015-05-22 12:25:41 -0700 | [diff] [blame] | 68 | |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 69 | GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext, |
| 70 | const GrContextOptions& options) { |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 71 | |
Robert Phillips | e42edcc | 2017-12-13 11:50:22 -0500 | [diff] [blame] | 72 | sk_sp<GrContext> context(new GrContext(backend)); |
| 73 | |
| 74 | context->fGpu = GrGpu::Make(backend, backendContext, options, context.get()); |
| 75 | if (!context->fGpu) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 76 | return nullptr; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 77 | } |
Robert Phillips | e42edcc | 2017-12-13 11:50:22 -0500 | [diff] [blame] | 78 | |
| 79 | if (!context->init(options)) { |
| 80 | return nullptr; |
| 81 | } |
| 82 | |
Brian Salomon | 91a3e52 | 2017-06-23 10:58:19 -0400 | [diff] [blame] | 83 | return context.release(); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 86 | sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> interface) { |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 87 | GrContextOptions defaultOptions; |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 88 | return MakeGL(std::move(interface), defaultOptions); |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 89 | } |
| 90 | |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 91 | sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> interface, |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 92 | const GrContextOptions& options) { |
Robert Phillips | e42edcc | 2017-12-13 11:50:22 -0500 | [diff] [blame] | 93 | sk_sp<GrContext> context(new GrContext(kOpenGL_GrBackend)); |
| 94 | |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 95 | context->fGpu = GrGLGpu::Make(std::move(interface), options, context.get()); |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 96 | if (!context->fGpu) { |
| 97 | return nullptr; |
| 98 | } |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 99 | if (!context->init(options)) { |
| 100 | return nullptr; |
| 101 | } |
| 102 | return context; |
| 103 | } |
| 104 | |
Brian Salomon | 990014d | 2017-12-07 16:23:39 -0500 | [diff] [blame] | 105 | sk_sp<GrContext> GrContext::MakeGL(const GrGLInterface* interface) { |
| 106 | return MakeGL(sk_ref_sp(interface)); |
| 107 | } |
| 108 | |
| 109 | sk_sp<GrContext> GrContext::MakeGL(const GrGLInterface* interface, |
| 110 | const GrContextOptions& options) { |
| 111 | return MakeGL(sk_ref_sp(interface), options); |
| 112 | } |
| 113 | |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 114 | sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions) { |
| 115 | GrContextOptions defaultOptions; |
| 116 | return MakeMock(mockOptions, defaultOptions); |
| 117 | } |
| 118 | |
| 119 | sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions, |
| 120 | const GrContextOptions& options) { |
Robert Phillips | e42edcc | 2017-12-13 11:50:22 -0500 | [diff] [blame] | 121 | sk_sp<GrContext> context(new GrContext(kMock_GrBackend)); |
| 122 | |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 123 | context->fGpu = GrMockGpu::Make(mockOptions, options, context.get()); |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 124 | if (!context->fGpu) { |
| 125 | return nullptr; |
| 126 | } |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 127 | if (!context->init(options)) { |
| 128 | return nullptr; |
| 129 | } |
| 130 | return context; |
| 131 | } |
| 132 | |
| 133 | #ifdef SK_VULKAN |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 134 | sk_sp<GrContext> GrContext::MakeVulkan(sk_sp<const GrVkBackendContext> backendContext) { |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 135 | GrContextOptions defaultOptions; |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 136 | return MakeVulkan(std::move(backendContext), defaultOptions); |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 137 | } |
| 138 | |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 139 | sk_sp<GrContext> GrContext::MakeVulkan(sk_sp<const GrVkBackendContext> backendContext, |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 140 | const GrContextOptions& options) { |
Robert Phillips | e42edcc | 2017-12-13 11:50:22 -0500 | [diff] [blame] | 141 | sk_sp<GrContext> context(new GrContext(kVulkan_GrBackend)); |
| 142 | |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 143 | context->fGpu = GrVkGpu::Make(std::move(backendContext), options, context.get()); |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 144 | if (!context->fGpu) { |
| 145 | return nullptr; |
| 146 | } |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 147 | if (!context->init(options)) { |
| 148 | return nullptr; |
| 149 | } |
| 150 | return context; |
| 151 | } |
| 152 | #endif |
| 153 | |
Greg Daniel | b76a72a | 2017-07-13 15:07:54 -0400 | [diff] [blame] | 154 | #ifdef SK_METAL |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 155 | sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue) { |
| 156 | GrContextOptions defaultOptions; |
| 157 | return MakeMetal(device, queue, defaultOptions); |
| 158 | } |
| 159 | |
Greg Daniel | b76a72a | 2017-07-13 15:07:54 -0400 | [diff] [blame] | 160 | sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue, const GrContextOptions& options) { |
Robert Phillips | e42edcc | 2017-12-13 11:50:22 -0500 | [diff] [blame] | 161 | sk_sp<GrContext> context(new GrContext(kMetal_GrBackend)); |
| 162 | |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 163 | context->fGpu = GrMtlTrampoline::MakeGpu(context.get(), options, device, queue); |
Greg Daniel | b76a72a | 2017-07-13 15:07:54 -0400 | [diff] [blame] | 164 | if (!context->fGpu) { |
| 165 | return nullptr; |
| 166 | } |
Greg Daniel | b76a72a | 2017-07-13 15:07:54 -0400 | [diff] [blame] | 167 | if (!context->init(options)) { |
| 168 | return nullptr; |
| 169 | } |
| 170 | return context; |
| 171 | } |
| 172 | #endif |
| 173 | |
joshualitt | 0acd0d3 | 2015-05-07 08:23:19 -0700 | [diff] [blame] | 174 | static int32_t gNextID = 1; |
| 175 | static int32_t next_id() { |
| 176 | int32_t id; |
| 177 | do { |
| 178 | id = sk_atomic_inc(&gNextID); |
| 179 | } while (id == SK_InvalidGenID); |
| 180 | return id; |
| 181 | } |
| 182 | |
Robert Phillips | e42edcc | 2017-12-13 11:50:22 -0500 | [diff] [blame] | 183 | sk_sp<GrContext> GrContextPriv::MakeDDL(GrContextThreadSafeProxy* proxy) { |
| 184 | sk_sp<GrContext> context(new GrContext(proxy)); |
| 185 | |
| 186 | context->fGpu = GrDDLGpu::Make(context.get(), proxy->fCaps); |
| 187 | if (!context->fGpu) { |
| 188 | return nullptr; |
| 189 | } |
| 190 | if (!context->init(proxy->fOptions)) { |
| 191 | return nullptr; |
| 192 | } |
| 193 | return context; |
| 194 | } |
| 195 | |
| 196 | GrContext::GrContext(GrBackend backend) |
| 197 | : fUniqueID(next_id()) |
| 198 | , fBackend(backend) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 199 | fResourceCache = nullptr; |
| 200 | fResourceProvider = nullptr; |
Brian Salomon | f856fd1 | 2016-12-16 14:24:34 -0500 | [diff] [blame] | 201 | fAtlasGlyphCache = nullptr; |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Robert Phillips | e42edcc | 2017-12-13 11:50:22 -0500 | [diff] [blame] | 204 | GrContext::GrContext(GrContextThreadSafeProxy* proxy) |
| 205 | : fUniqueID(proxy->fContextUniqueID) |
| 206 | , fBackend(proxy->fBackend) { |
| 207 | fResourceCache = nullptr; |
| 208 | fResourceProvider = nullptr; |
| 209 | fAtlasGlyphCache = nullptr; |
Greg Daniel | b76a72a | 2017-07-13 15:07:54 -0400 | [diff] [blame] | 210 | } |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 211 | |
Greg Daniel | b76a72a | 2017-07-13 15:07:54 -0400 | [diff] [blame] | 212 | bool GrContext::init(const GrContextOptions& options) { |
| 213 | ASSERT_SINGLE_OWNER |
Robert Phillips | e42edcc | 2017-12-13 11:50:22 -0500 | [diff] [blame] | 214 | fCaps = fGpu->refCaps(); |
Brian Salomon | eace8cd | 2017-12-08 13:11:11 -0500 | [diff] [blame] | 215 | fResourceCache = new GrResourceCache(fCaps.get(), fUniqueID); |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 216 | fResourceProvider = new GrResourceProvider(fGpu.get(), fResourceCache, &fSingleOwner); |
Robert Phillips | e42edcc | 2017-12-13 11:50:22 -0500 | [diff] [blame] | 217 | // DDL TODO: we need to think through how the task group & persistent cache |
| 218 | // get passed on to/shared between all the DDLRecorders created with this context. |
| 219 | fThreadSafeProxy.reset(new GrContextThreadSafeProxy(fCaps, this->uniqueID(), fBackend, |
| 220 | options)); |
commit-bot@chromium.org | 1836d33 | 2013-07-16 22:55:03 +0000 | [diff] [blame] | 221 | |
Brian Osman | 46da1cc | 2017-02-14 14:15:48 -0500 | [diff] [blame] | 222 | fDisableGpuYUVConversion = options.fDisableGpuYUVConversion; |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 223 | fDidTestPMConversions = false; |
| 224 | |
bsalomon | 6b2552f | 2016-09-15 13:50:26 -0700 | [diff] [blame] | 225 | GrPathRendererChain::Options prcOptions; |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 226 | prcOptions.fAllowPathMaskCaching = options.fAllowPathMaskCaching; |
Brian Osman | 195c05b | 2017-08-30 15:14:04 -0400 | [diff] [blame] | 227 | #if GR_TEST_UTILS |
csmartdalton | 008b9d8 | 2017-02-22 12:00:42 -0700 | [diff] [blame] | 228 | prcOptions.fGpuPathRenderers = options.fGpuPathRenderers; |
Brian Osman | 195c05b | 2017-08-30 15:14:04 -0400 | [diff] [blame] | 229 | #endif |
Brian Osman | b350ae2 | 2017-08-29 16:15:39 -0400 | [diff] [blame] | 230 | if (options.fDisableDistanceFieldPaths) { |
Brian Osman | 195c05b | 2017-08-30 15:14:04 -0400 | [diff] [blame] | 231 | prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kSmall; |
Brian Osman | b350ae2 | 2017-08-29 16:15:39 -0400 | [diff] [blame] | 232 | } |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 233 | |
| 234 | GrAtlasTextContext::Options atlasTextContextOptions; |
| 235 | atlasTextContextOptions.fMaxDistanceFieldFontSize = options.fGlyphsAsPathsFontSize; |
| 236 | atlasTextContextOptions.fMinDistanceFieldFontSize = options.fMinDistanceFieldFontSize; |
Brian Salomon | b508696 | 2017-12-13 10:59:33 -0500 | [diff] [blame^] | 237 | atlasTextContextOptions.fDistanceFieldVerticesAlwaysHaveW = false; |
| 238 | #if SK_SUPPORT_ATLAS_TEXT |
| 239 | if (GrContextOptions::Enable::kYes == options.fDistanceFieldGlyphVerticesAlwaysHaveW) { |
| 240 | atlasTextContextOptions.fDistanceFieldVerticesAlwaysHaveW = true; |
| 241 | } |
| 242 | #endif |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 243 | |
| 244 | fDrawingManager.reset( |
| 245 | new GrDrawingManager(this, prcOptions, atlasTextContextOptions, &fSingleOwner)); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 246 | |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 247 | GrDrawOpAtlas::AllowMultitexturing allowMultitexturing; |
Jim Van Verth | c02cb8a | 2017-11-20 22:11:30 +0000 | [diff] [blame] | 248 | switch (options.fAllowMultipleGlyphCacheTextures) { |
| 249 | case GrContextOptions::Enable::kDefault: |
| 250 | #ifdef SK_BUILD_FOR_IOS |
| 251 | allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kNo; |
| 252 | #else |
| 253 | allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kYes; |
| 254 | #endif |
| 255 | break; |
| 256 | case GrContextOptions::Enable::kNo: |
| 257 | allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kNo; |
| 258 | break; |
| 259 | case GrContextOptions::Enable::kYes: |
| 260 | allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kYes; |
| 261 | break; |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 262 | } |
| 263 | fAtlasGlyphCache = new GrAtlasGlyphCache(this, options.fGlyphCacheTextureMaximumBytes, |
| 264 | allowMultitexturing); |
Jim Van Verth | 106b5c4 | 2017-09-26 12:45:29 -0400 | [diff] [blame] | 265 | this->contextPriv().addOnFlushCallbackObject(fAtlasGlyphCache); |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 266 | |
Jim Van Verth | 474d687 | 2017-12-14 13:00:05 -0500 | [diff] [blame] | 267 | fTextBlobCache.reset(new GrTextBlobCache(TextBlobCacheOverBudgetCB, this, this->uniqueID())); |
Brian Salomon | 91a3e52 | 2017-06-23 10:58:19 -0400 | [diff] [blame] | 268 | |
Brian Osman | 5127998 | 2017-08-23 10:12:00 -0400 | [diff] [blame] | 269 | if (options.fExecutor) { |
| 270 | fTaskGroup = skstd::make_unique<SkTaskGroup>(*options.fExecutor); |
| 271 | } |
| 272 | |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 273 | fPersistentCache = options.fPersistentCache; |
| 274 | |
Brian Salomon | 91a3e52 | 2017-06-23 10:58:19 -0400 | [diff] [blame] | 275 | return true; |
bsalomon@google.com | c0af317 | 2012-06-15 14:10:09 +0000 | [diff] [blame] | 276 | } |
| 277 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 278 | GrContext::~GrContext() { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 279 | ASSERT_SINGLE_OWNER |
| 280 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 281 | if (!fGpu) { |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 282 | SkASSERT(!fCaps); |
bsalomon@google.com | 733c062 | 2013-04-24 17:59:32 +0000 | [diff] [blame] | 283 | return; |
| 284 | } |
| 285 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 286 | this->flush(); |
robertphillips@google.com | 5acc0e3 | 2012-05-17 12:01:02 +0000 | [diff] [blame] | 287 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 288 | fDrawingManager->cleanup(); |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 289 | |
robertphillips@google.com | 950b1b0 | 2013-10-21 17:37:28 +0000 | [diff] [blame] | 290 | for (int i = 0; i < fCleanUpData.count(); ++i) { |
| 291 | (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo); |
| 292 | } |
| 293 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 294 | delete fResourceProvider; |
| 295 | delete fResourceCache; |
Brian Salomon | f856fd1 | 2016-12-16 14:24:34 -0500 | [diff] [blame] | 296 | delete fAtlasGlyphCache; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 297 | } |
| 298 | |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 299 | sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() { |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 300 | return fThreadSafeProxy; |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 301 | } |
| 302 | |
bsalomon | 2354f84 | 2014-07-28 13:48:36 -0700 | [diff] [blame] | 303 | void GrContext::abandonContext() { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 304 | ASSERT_SINGLE_OWNER |
| 305 | |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 306 | fResourceProvider->abandon(); |
robertphillips | 0dfa62c | 2015-11-16 06:23:31 -0800 | [diff] [blame] | 307 | |
| 308 | // Need to abandon the drawing manager first so all the render targets |
| 309 | // will be released/forgotten before they too are abandoned. |
| 310 | fDrawingManager->abandon(); |
| 311 | |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 312 | // abandon first to so destructors |
| 313 | // don't try to free the resources in the API. |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 314 | fResourceCache->abandonAll(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 315 | |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 316 | fGpu->disconnect(GrGpu::DisconnectType::kAbandon); |
| 317 | |
Brian Salomon | f856fd1 | 2016-12-16 14:24:34 -0500 | [diff] [blame] | 318 | fAtlasGlyphCache->freeAll(); |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 319 | fTextBlobCache->freeAll(); |
| 320 | } |
| 321 | |
| 322 | void GrContext::releaseResourcesAndAbandonContext() { |
| 323 | ASSERT_SINGLE_OWNER |
| 324 | |
| 325 | fResourceProvider->abandon(); |
| 326 | |
| 327 | // Need to abandon the drawing manager first so all the render targets |
| 328 | // will be released/forgotten before they too are abandoned. |
| 329 | fDrawingManager->abandon(); |
| 330 | |
| 331 | // Release all resources in the backend 3D API. |
| 332 | fResourceCache->releaseAll(); |
| 333 | |
| 334 | fGpu->disconnect(GrGpu::DisconnectType::kCleanup); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 335 | |
Brian Salomon | f856fd1 | 2016-12-16 14:24:34 -0500 | [diff] [blame] | 336 | fAtlasGlyphCache->freeAll(); |
joshualitt | 26ffc00 | 2015-04-16 11:24:04 -0700 | [diff] [blame] | 337 | fTextBlobCache->freeAll(); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 338 | } |
| 339 | |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 340 | void GrContext::resetContext(uint32_t state) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 341 | ASSERT_SINGLE_OWNER |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 342 | fGpu->markContextDirty(state); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | void GrContext::freeGpuResources() { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 346 | ASSERT_SINGLE_OWNER |
| 347 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 348 | this->flush(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 349 | |
Brian Salomon | f856fd1 | 2016-12-16 14:24:34 -0500 | [diff] [blame] | 350 | fAtlasGlyphCache->freeAll(); |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 351 | |
| 352 | fDrawingManager->freeGpuResources(); |
bsalomon | 3033b9f | 2015-04-13 11:09:56 -0700 | [diff] [blame] | 353 | |
| 354 | fResourceCache->purgeAllUnlocked(); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 357 | void GrContext::performDeferredCleanup(std::chrono::milliseconds msNotUsed) { |
Brian Salomon | 5e15085 | 2017-03-22 14:53:13 -0400 | [diff] [blame] | 358 | ASSERT_SINGLE_OWNER |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 359 | fResourceCache->purgeAsNeeded(); |
| 360 | fResourceCache->purgeResourcesNotUsedSince(GrStdSteadyClock::now() - msNotUsed); |
| 361 | |
| 362 | fTextBlobCache->purgeStaleBlobs(); |
Brian Salomon | 5e15085 | 2017-03-22 14:53:13 -0400 | [diff] [blame] | 363 | } |
| 364 | |
Derek Sollenberger | 5480a18 | 2017-05-25 16:43:59 -0400 | [diff] [blame] | 365 | void GrContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) { |
| 366 | ASSERT_SINGLE_OWNER |
| 367 | fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources); |
| 368 | } |
| 369 | |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 370 | void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 371 | ASSERT_SINGLE_OWNER |
| 372 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 373 | if (resourceCount) { |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 374 | *resourceCount = fResourceCache->getBudgetedResourceCount(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 375 | } |
| 376 | if (resourceBytes) { |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 377 | *resourceBytes = fResourceCache->getBudgetedResourceBytes(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 378 | } |
commit-bot@chromium.org | d8a57af | 2014-03-19 21:19:16 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Derek Sollenberger | ee47914 | 2017-05-24 11:41:33 -0400 | [diff] [blame] | 381 | size_t GrContext::getResourceCachePurgeableBytes() const { |
| 382 | ASSERT_SINGLE_OWNER |
| 383 | return fResourceCache->getPurgeableBytes(); |
| 384 | } |
| 385 | |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 386 | //////////////////////////////////////////////////////////////////////////////// |
| 387 | |
joshualitt | 0db6dfa | 2015-04-10 07:01:30 -0700 | [diff] [blame] | 388 | void GrContext::TextBlobCacheOverBudgetCB(void* data) { |
| 389 | SkASSERT(data); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 390 | // TextBlobs are drawn at the SkGpuDevice level, therefore they cannot rely on |
| 391 | // GrRenderTargetContext to perform a necessary flush. The solution is to move drawText calls |
| 392 | // to below the GrContext level, but this is not trivial because they call drawPath on |
| 393 | // SkGpuDevice. |
joshualitt | 0db6dfa | 2015-04-10 07:01:30 -0700 | [diff] [blame] | 394 | GrContext* context = reinterpret_cast<GrContext*>(data); |
| 395 | context->flush(); |
| 396 | } |
| 397 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 398 | //////////////////////////////////////////////////////////////////////////////// |
| 399 | |
bsalomon | b77a907 | 2016-09-07 10:02:04 -0700 | [diff] [blame] | 400 | void GrContext::flush() { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 401 | ASSERT_SINGLE_OWNER |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 402 | RETURN_IF_ABANDONED |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 403 | |
| 404 | fDrawingManager->flush(nullptr); |
| 405 | } |
| 406 | |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 407 | GrSemaphoresSubmitted GrContext::flushAndSignalSemaphores(int numSemaphores, |
| 408 | GrBackendSemaphore signalSemaphores[]) { |
| 409 | ASSERT_SINGLE_OWNER |
| 410 | if (fDrawingManager->wasAbandoned()) { return GrSemaphoresSubmitted::kNo; } |
| 411 | |
| 412 | return fDrawingManager->flush(nullptr, numSemaphores, signalSemaphores); |
| 413 | } |
| 414 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 415 | void GrContextPriv::flush(GrSurfaceProxy* proxy) { |
| 416 | ASSERT_SINGLE_OWNER_PRIV |
| 417 | RETURN_IF_ABANDONED_PRIV |
| 418 | ASSERT_OWNED_PROXY_PRIV(proxy); |
| 419 | |
| 420 | fContext->fDrawingManager->flush(proxy); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 421 | } |
| 422 | |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 423 | bool sw_convert_to_premul(GrPixelConfig srcConfig, int width, int height, size_t inRowBytes, |
| 424 | const void* inPixels, size_t outRowBytes, void* outPixels) { |
Matt Sarett | c7b2908 | 2017-02-09 16:22:39 -0500 | [diff] [blame] | 425 | SkColorType colorType; |
| 426 | if (!GrPixelConfigToColorType(srcConfig, &colorType) || |
| 427 | 4 != SkColorTypeBytesPerPixel(colorType)) |
| 428 | { |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 429 | return false; |
| 430 | } |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 431 | |
Matt Sarett | c7b2908 | 2017-02-09 16:22:39 -0500 | [diff] [blame] | 432 | for (int y = 0; y < height; y++) { |
| 433 | SkOpts::RGBA_to_rgbA((uint32_t*) outPixels, inPixels, width); |
| 434 | outPixels = SkTAddOffset<void>(outPixels, outRowBytes); |
| 435 | inPixels = SkTAddOffset<const void>(inPixels, inRowBytes); |
| 436 | } |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 437 | |
Matt Sarett | c7b2908 | 2017-02-09 16:22:39 -0500 | [diff] [blame] | 438 | return true; |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 439 | } |
| 440 | |
Brian Osman | d2ca59a | 2017-04-13 14:03:57 -0400 | [diff] [blame] | 441 | static bool valid_premul_config(GrPixelConfig config) { |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 442 | return GrPixelConfigIs8888Unorm(config) || kRGBA_half_GrPixelConfig == config; |
| 443 | } |
| 444 | |
Brian Osman | d2ca59a | 2017-04-13 14:03:57 -0400 | [diff] [blame] | 445 | static bool valid_pixel_conversion(GrPixelConfig srcConfig, GrPixelConfig dstConfig, |
| 446 | bool premulConversion) { |
| 447 | // We don't allow conversion between integer configs and float/fixed configs. |
| 448 | if (GrPixelConfigIsSint(srcConfig) != GrPixelConfigIsSint(dstConfig)) { |
| 449 | return false; |
| 450 | } |
| 451 | |
| 452 | // We only allow premul <-> unpremul conversions for some formats |
| 453 | if (premulConversion && (!valid_premul_config(srcConfig) || !valid_premul_config(dstConfig))) { |
| 454 | return false; |
| 455 | } |
| 456 | |
| 457 | return true; |
| 458 | } |
| 459 | |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 460 | static bool pm_upm_must_round_trip(GrPixelConfig config, SkColorSpace* colorSpace) { |
| 461 | return !colorSpace && |
| 462 | (kRGBA_8888_GrPixelConfig == config || kBGRA_8888_GrPixelConfig == config); |
| 463 | } |
| 464 | |
Robert Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 465 | bool GrContextPriv::writeSurfacePixels(GrSurfaceContext* dst, |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 466 | int left, int top, int width, int height, |
| 467 | GrPixelConfig srcConfig, SkColorSpace* srcColorSpace, |
| 468 | const void* buffer, size_t rowBytes, |
| 469 | uint32_t pixelOpsFlags) { |
Brian Osman | b62ea22 | 2016-12-22 11:12:16 -0500 | [diff] [blame] | 470 | // TODO: Color space conversion |
| 471 | |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 472 | ASSERT_SINGLE_OWNER_PRIV |
| 473 | RETURN_FALSE_IF_ABANDONED_PRIV |
Robert Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 474 | SkASSERT(dst); |
| 475 | ASSERT_OWNED_PROXY_PRIV(dst->asSurfaceProxy()); |
Brian Salomon | dcbb9d9 | 2017-07-19 10:53:20 -0400 | [diff] [blame] | 476 | GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "writeSurfacePixels", fContext); |
bsalomon | 6c6f658 | 2015-09-10 08:12:46 -0700 | [diff] [blame] | 477 | |
Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 478 | if (!dst->asSurfaceProxy()->instantiate(fContext->resourceProvider())) { |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 479 | return false; |
| 480 | } |
| 481 | |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 482 | GrSurfaceProxy* dstProxy = dst->asSurfaceProxy(); |
| 483 | GrSurface* dstSurface = dstProxy->priv().peekSurface(); |
Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 484 | |
Brian Osman | d2ca59a | 2017-04-13 14:03:57 -0400 | [diff] [blame] | 485 | // The src is unpremul but the dst is premul -> premul the src before or as part of the write |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 486 | const bool premul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags); |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 487 | if (!valid_pixel_conversion(srcConfig, dstProxy->config(), premul)) { |
Brian Osman | d2ca59a | 2017-04-13 14:03:57 -0400 | [diff] [blame] | 488 | return false; |
| 489 | } |
| 490 | |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 491 | // We need to guarantee round-trip conversion if we are reading and writing 8888 non-sRGB data, |
| 492 | // without any color spaces attached, and the caller wants us to premul. |
Robert Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 493 | bool useConfigConversionEffect = |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 494 | premul && pm_upm_must_round_trip(srcConfig, srcColorSpace) && |
| 495 | pm_upm_must_round_trip(dstProxy->config(), dst->colorSpaceInfo().colorSpace()); |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 496 | |
| 497 | // Are we going to try to premul as part of a draw? For the non-legacy case, we always allow |
| 498 | // this. GrConfigConversionEffect fails on some GPUs, so only allow this if it works perfectly. |
| 499 | bool premulOnGpu = premul && |
| 500 | (!useConfigConversionEffect || fContext->validPMUPMConversionExists()); |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 501 | |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 502 | // Trim the params here so that if we wind up making a temporary surface it can be as small as |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 503 | // necessary and because GrGpu::getWritePixelsInfo requires it. |
Robert Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 504 | if (!GrSurfacePriv::AdjustWritePixelParams(dstSurface->width(), dstSurface->height(), |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 505 | GrBytesPerPixel(srcConfig), &left, &top, &width, |
| 506 | &height, &buffer, &rowBytes)) { |
| 507 | return false; |
| 508 | } |
| 509 | |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 510 | GrGpu::DrawPreference drawPreference = premulOnGpu ? GrGpu::kCallerPrefersDraw_DrawPreference |
| 511 | : GrGpu::kNoDraw_DrawPreference; |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 512 | GrGpu::WritePixelTempDrawInfo tempDrawInfo; |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 513 | if (!fContext->fGpu->getWritePixelsInfo(dstSurface, dstProxy->origin(), width, height, |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 514 | srcConfig, &drawPreference, &tempDrawInfo)) { |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 515 | return false; |
| 516 | } |
| 517 | |
Robert Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 518 | if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && dstSurface->surfacePriv().hasPendingIO()) { |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 519 | this->flush(nullptr); // MDB TODO: tighten this |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 520 | } |
| 521 | |
Robert Phillips | 2f49314 | 2017-03-02 18:18:38 -0500 | [diff] [blame] | 522 | sk_sp<GrTextureProxy> tempProxy; |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 523 | if (GrGpu::kNoDraw_DrawPreference != drawPreference) { |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 524 | tempProxy = GrSurfaceProxy::MakeDeferred(fContext->resourceProvider(), |
Robert Phillips | 2f49314 | 2017-03-02 18:18:38 -0500 | [diff] [blame] | 525 | tempDrawInfo.fTempSurfaceDesc, |
| 526 | SkBackingFit::kApprox, |
| 527 | SkBudgeted::kYes); |
| 528 | if (!tempProxy && GrGpu::kRequireDraw_DrawPreference == drawPreference) { |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 529 | return false; |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | // temp buffer for doing sw premul conversion, if needed. |
| 534 | SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0); |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 535 | // We need to do sw premul if we were unable to create a RT for drawing, or if we can't do the |
| 536 | // premul on the GPU |
| 537 | if (premul && (!tempProxy || !premulOnGpu)) { |
| 538 | size_t tmpRowBytes = 4 * width; |
| 539 | tmpPixels.reset(width * height); |
| 540 | if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes, |
| 541 | tmpPixels.get())) { |
| 542 | return false; |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 543 | } |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 544 | rowBytes = tmpRowBytes; |
| 545 | buffer = tmpPixels.get(); |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 546 | } |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 547 | |
| 548 | if (tempProxy) { |
Brian Osman | 2240be9 | 2017-10-18 13:15:13 -0400 | [diff] [blame] | 549 | auto fp = GrSimpleTextureEffect::Make(tempProxy, SkMatrix::I()); |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 550 | if (premulOnGpu) { |
| 551 | fp = fContext->createUPMToPMEffect(std::move(fp), useConfigConversionEffect); |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 552 | } |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 553 | fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle); |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame] | 554 | if (!fp) { |
| 555 | return false; |
| 556 | } |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 557 | |
Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 558 | if (!tempProxy->instantiate(fContext->resourceProvider())) { |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 559 | return false; |
| 560 | } |
Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 561 | GrTexture* texture = tempProxy->priv().peekTexture(); |
Robert Phillips | 7bbbf62 | 2017-10-17 07:36:59 -0400 | [diff] [blame] | 562 | |
| 563 | if (tempProxy->priv().hasPendingIO()) { |
| 564 | this->flush(tempProxy.get()); |
| 565 | } |
| 566 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 567 | if (!fContext->fGpu->writePixels(texture, tempProxy->origin(), 0, 0, width, height, |
| 568 | tempDrawInfo.fWriteConfig, buffer, rowBytes)) { |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 569 | return false; |
| 570 | } |
Robert Phillips | 7bbbf62 | 2017-10-17 07:36:59 -0400 | [diff] [blame] | 571 | tempProxy = nullptr; |
| 572 | |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 573 | SkMatrix matrix; |
| 574 | matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top)); |
Robert Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 575 | GrRenderTargetContext* renderTargetContext = dst->asRenderTargetContext(); |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 576 | if (!renderTargetContext) { |
| 577 | return false; |
| 578 | } |
| 579 | GrPaint paint; |
| 580 | paint.addColorFragmentProcessor(std::move(fp)); |
| 581 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 582 | paint.setAllowSRGBInputs(dst->colorSpaceInfo().isGammaCorrect() || |
| 583 | GrPixelConfigIsSRGB(dst->colorSpaceInfo().config())); |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 584 | SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)); |
| 585 | renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, matrix, rect, |
| 586 | nullptr); |
| 587 | |
| 588 | if (kFlushWrites_PixelOp & pixelOpsFlags) { |
| 589 | this->flushSurfaceWrites(renderTargetContext->asRenderTargetProxy()); |
| 590 | } |
| 591 | } else { |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 592 | return fContext->fGpu->writePixels(dstSurface, dstProxy->origin(), left, top, width, |
| 593 | height, srcConfig, buffer, rowBytes); |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 594 | } |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 595 | return true; |
| 596 | } |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 597 | |
Robert Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 598 | bool GrContextPriv::readSurfacePixels(GrSurfaceContext* src, |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 599 | int left, int top, int width, int height, |
| 600 | GrPixelConfig dstConfig, SkColorSpace* dstColorSpace, |
| 601 | void* buffer, size_t rowBytes, uint32_t flags) { |
Brian Osman | b62ea22 | 2016-12-22 11:12:16 -0500 | [diff] [blame] | 602 | // TODO: Color space conversion |
| 603 | |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 604 | ASSERT_SINGLE_OWNER_PRIV |
| 605 | RETURN_FALSE_IF_ABANDONED_PRIV |
Robert Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 606 | SkASSERT(src); |
| 607 | ASSERT_OWNED_PROXY_PRIV(src->asSurfaceProxy()); |
Brian Salomon | dcbb9d9 | 2017-07-19 10:53:20 -0400 | [diff] [blame] | 608 | GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "readSurfacePixels", fContext); |
bsalomon | 32ab260 | 2015-09-09 18:57:49 -0700 | [diff] [blame] | 609 | |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 610 | // MDB TODO: delay this instantiation until later in the method |
Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 611 | if (!src->asSurfaceProxy()->instantiate(fContext->resourceProvider())) { |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 612 | return false; |
| 613 | } |
| 614 | |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 615 | GrSurfaceProxy* srcProxy = src->asSurfaceProxy(); |
| 616 | GrSurface* srcSurface = srcProxy->priv().peekSurface(); |
Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 617 | |
Brian Osman | d2ca59a | 2017-04-13 14:03:57 -0400 | [diff] [blame] | 618 | // The src is premul but the dst is unpremul -> unpremul the src after or as part of the read |
| 619 | bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags); |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 620 | if (!valid_pixel_conversion(srcProxy->config(), dstConfig, unpremul)) { |
Brian Osman | d2ca59a | 2017-04-13 14:03:57 -0400 | [diff] [blame] | 621 | return false; |
| 622 | } |
| 623 | |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 624 | // We need to guarantee round-trip conversion if we are reading and writing 8888 non-sRGB data, |
| 625 | // without any color spaces attached, and the caller wants us to unpremul. |
Robert Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 626 | bool useConfigConversionEffect = |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 627 | unpremul && |
| 628 | pm_upm_must_round_trip(srcProxy->config(), src->colorSpaceInfo().colorSpace()) && |
| 629 | pm_upm_must_round_trip(dstConfig, dstColorSpace); |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 630 | |
| 631 | // Are we going to try to unpremul as part of a draw? For the non-legacy case, we always allow |
| 632 | // this. GrConfigConversionEffect fails on some GPUs, so only allow this if it works perfectly. |
| 633 | bool unpremulOnGpu = unpremul && |
| 634 | (!useConfigConversionEffect || fContext->validPMUPMConversionExists()); |
bsalomon | 6c6f658 | 2015-09-10 08:12:46 -0700 | [diff] [blame] | 635 | |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 636 | // Adjust the params so that if we wind up using an intermediate surface we've already done |
| 637 | // all the trimming and the temporary can be the min size required. |
Robert Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 638 | if (!GrSurfacePriv::AdjustReadPixelParams(srcSurface->width(), srcSurface->height(), |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 639 | GrBytesPerPixel(dstConfig), &left, |
| 640 | &top, &width, &height, &buffer, &rowBytes)) { |
| 641 | return false; |
| 642 | } |
| 643 | |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 644 | GrGpu::DrawPreference drawPreference = unpremulOnGpu ? GrGpu::kCallerPrefersDraw_DrawPreference |
| 645 | : GrGpu::kNoDraw_DrawPreference; |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 646 | GrGpu::ReadPixelTempDrawInfo tempDrawInfo; |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 647 | if (!fContext->fGpu->getReadPixelsInfo(srcSurface, srcProxy->origin(), width, height, rowBytes, |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 648 | dstConfig, &drawPreference, &tempDrawInfo)) { |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 649 | return false; |
| 650 | } |
bsalomon | 191bcc0 | 2014-11-14 11:31:13 -0800 | [diff] [blame] | 651 | |
Robert Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 652 | if (!(kDontFlush_PixelOpsFlag & flags) && srcSurface->surfacePriv().hasPendingWrite()) { |
Brian Osman | d2ca59a | 2017-04-13 14:03:57 -0400 | [diff] [blame] | 653 | this->flush(nullptr); // MDB TODO: tighten this |
| 654 | } |
| 655 | |
Robert Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 656 | sk_sp<GrSurfaceProxy> proxyToRead = src->asSurfaceProxyRef(); |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 657 | bool didTempDraw = false; |
| 658 | if (GrGpu::kNoDraw_DrawPreference != drawPreference) { |
bsalomon | b117ff1 | 2016-07-19 07:24:40 -0700 | [diff] [blame] | 659 | if (SkBackingFit::kExact == tempDrawInfo.fTempSurfaceFit) { |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 660 | // We only respect this when the entire src is being read. Otherwise we can trigger too |
| 661 | // many odd ball texture sizes and trash the cache. |
Robert Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 662 | if (width != srcSurface->width() || height != srcSurface->height()) { |
bsalomon | b117ff1 | 2016-07-19 07:24:40 -0700 | [diff] [blame] | 663 | tempDrawInfo.fTempSurfaceFit= SkBackingFit::kApprox; |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 664 | } |
bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 665 | } |
brianosman | dfe4f2e | 2016-07-21 13:28:36 -0700 | [diff] [blame] | 666 | // TODO: Need to decide the semantics of this function for color spaces. Do we support |
| 667 | // conversion to a passed-in color space? For now, specifying nullptr means that this |
| 668 | // path will do no conversion, so it will match the behavior of the non-draw path. |
Robert Phillips | dd3b3f4 | 2017-04-24 10:57:28 -0400 | [diff] [blame] | 669 | sk_sp<GrRenderTargetContext> tempRTC = fContext->makeDeferredRenderTargetContext( |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 670 | tempDrawInfo.fTempSurfaceFit, |
bsalomon | b117ff1 | 2016-07-19 07:24:40 -0700 | [diff] [blame] | 671 | tempDrawInfo.fTempSurfaceDesc.fWidth, |
| 672 | tempDrawInfo.fTempSurfaceDesc.fHeight, |
| 673 | tempDrawInfo.fTempSurfaceDesc.fConfig, |
brianosman | dfe4f2e | 2016-07-21 13:28:36 -0700 | [diff] [blame] | 674 | nullptr, |
bsalomon | b117ff1 | 2016-07-19 07:24:40 -0700 | [diff] [blame] | 675 | tempDrawInfo.fTempSurfaceDesc.fSampleCnt, |
Greg Daniel | 45d6303 | 2017-10-30 13:41:26 -0400 | [diff] [blame] | 676 | GrMipMapped::kNo, |
bsalomon | b117ff1 | 2016-07-19 07:24:40 -0700 | [diff] [blame] | 677 | tempDrawInfo.fTempSurfaceDesc.fOrigin); |
Brian Osman | 693a540 | 2016-10-27 15:13:22 -0400 | [diff] [blame] | 678 | if (tempRTC) { |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 679 | SkMatrix textureMatrix = SkMatrix::MakeTrans(SkIntToScalar(left), SkIntToScalar(top)); |
Robert Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 680 | sk_sp<GrTextureProxy> proxy = src->asTextureProxyRef(); |
Brian Osman | 2240be9 | 2017-10-18 13:15:13 -0400 | [diff] [blame] | 681 | auto fp = GrSimpleTextureEffect::Make(std::move(proxy), textureMatrix); |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 682 | if (unpremulOnGpu) { |
| 683 | fp = fContext->createPMToUPMEffect(std::move(fp), useConfigConversionEffect); |
| 684 | // We no longer need to do this on CPU after the read back. |
| 685 | unpremul = false; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 686 | } |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 687 | fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle); |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame] | 688 | if (!fp) { |
| 689 | return false; |
| 690 | } |
Brian Osman | 60cd57e | 2017-04-06 10:19:06 -0400 | [diff] [blame] | 691 | |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 692 | GrPaint paint; |
| 693 | paint.addColorFragmentProcessor(std::move(fp)); |
| 694 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
| 695 | paint.setAllowSRGBInputs(true); |
| 696 | SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)); |
| 697 | tempRTC->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect, |
| 698 | nullptr); |
| 699 | proxyToRead = tempRTC->asTextureProxyRef(); |
| 700 | left = 0; |
| 701 | top = 0; |
| 702 | didTempDraw = true; |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 703 | } |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 704 | } |
joshualitt | 5c55fef | 2014-10-31 14:04:35 -0700 | [diff] [blame] | 705 | |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 706 | if (!proxyToRead) { |
| 707 | return false; |
| 708 | } |
| 709 | |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 710 | if (GrGpu::kRequireDraw_DrawPreference == drawPreference && !didTempDraw) { |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 711 | return false; |
| 712 | } |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 713 | GrPixelConfig configToRead = dstConfig; |
| 714 | if (didTempDraw) { |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 715 | this->flushSurfaceWrites(proxyToRead.get()); |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 716 | configToRead = tempDrawInfo.fReadConfig; |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 717 | } |
Robert Phillips | 09dfc47 | 2017-09-13 15:25:47 -0400 | [diff] [blame] | 718 | |
| 719 | if (!proxyToRead->instantiate(fContext->resourceProvider())) { |
| 720 | return false; |
| 721 | } |
| 722 | |
| 723 | GrSurface* surfaceToRead = proxyToRead->priv().peekSurface(); |
| 724 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 725 | if (!fContext->fGpu->readPixels(surfaceToRead, proxyToRead->origin(), |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 726 | left, top, width, height, configToRead, buffer, rowBytes)) { |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 727 | return false; |
| 728 | } |
| 729 | |
| 730 | // Perform umpremul conversion if we weren't able to perform it as a draw. |
| 731 | if (unpremul) { |
Matt Sarett | c7b2908 | 2017-02-09 16:22:39 -0500 | [diff] [blame] | 732 | SkColorType colorType; |
| 733 | if (!GrPixelConfigToColorType(dstConfig, &colorType) || |
| 734 | 4 != SkColorTypeBytesPerPixel(colorType)) |
| 735 | { |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 736 | return false; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 737 | } |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 738 | |
Matt Sarett | c7b2908 | 2017-02-09 16:22:39 -0500 | [diff] [blame] | 739 | for (int y = 0; y < height; y++) { |
| 740 | SkUnpremultiplyRow<false>((uint32_t*) buffer, (const uint32_t*) buffer, width); |
| 741 | buffer = SkTAddOffset<void>(buffer, rowBytes); |
| 742 | } |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 743 | } |
| 744 | return true; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 745 | } |
| 746 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 747 | void GrContextPriv::prepareSurfaceForExternalIO(GrSurfaceProxy* proxy) { |
| 748 | ASSERT_SINGLE_OWNER_PRIV |
| 749 | RETURN_IF_ABANDONED_PRIV |
| 750 | SkASSERT(proxy); |
| 751 | ASSERT_OWNED_PROXY_PRIV(proxy); |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 752 | fContext->fDrawingManager->prepareSurfaceForExternalIO(proxy, 0, nullptr); |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 753 | } |
| 754 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 755 | void GrContextPriv::flushSurfaceWrites(GrSurfaceProxy* proxy) { |
| 756 | ASSERT_SINGLE_OWNER_PRIV |
| 757 | RETURN_IF_ABANDONED_PRIV |
| 758 | SkASSERT(proxy); |
| 759 | ASSERT_OWNED_PROXY_PRIV(proxy); |
| 760 | if (proxy->priv().hasPendingWrite()) { |
| 761 | this->flush(proxy); |
bsalomon | f80bfed | 2014-10-07 05:56:02 -0700 | [diff] [blame] | 762 | } |
| 763 | } |
| 764 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 765 | void GrContextPriv::flushSurfaceIO(GrSurfaceProxy* proxy) { |
| 766 | ASSERT_SINGLE_OWNER_PRIV |
| 767 | RETURN_IF_ABANDONED_PRIV |
| 768 | SkASSERT(proxy); |
| 769 | ASSERT_OWNED_PROXY_PRIV(proxy); |
| 770 | if (proxy->priv().hasPendingIO()) { |
| 771 | this->flush(proxy); |
ajuma | 95243eb | 2016-08-24 08:19:02 -0700 | [diff] [blame] | 772 | } |
| 773 | } |
| 774 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 775 | //////////////////////////////////////////////////////////////////////////////// |
commit-bot@chromium.org | b471a32 | 2014-03-10 07:40:03 +0000 | [diff] [blame] | 776 | int GrContext::getRecommendedSampleCount(GrPixelConfig config, |
| 777 | SkScalar dpi) const { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 778 | ASSERT_SINGLE_OWNER |
| 779 | |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 780 | if (!this->caps()->isConfigRenderable(config, true)) { |
commit-bot@chromium.org | b471a32 | 2014-03-10 07:40:03 +0000 | [diff] [blame] | 781 | return 0; |
| 782 | } |
| 783 | int chosenSampleCount = 0; |
jvanverth | e9c0fc6 | 2015-04-29 11:18:05 -0700 | [diff] [blame] | 784 | if (fGpu->caps()->shaderCaps()->pathRenderingSupport()) { |
commit-bot@chromium.org | b471a32 | 2014-03-10 07:40:03 +0000 | [diff] [blame] | 785 | if (dpi >= 250.0f) { |
| 786 | chosenSampleCount = 4; |
| 787 | } else { |
| 788 | chosenSampleCount = 16; |
| 789 | } |
| 790 | } |
Greg Daniel | 81e7bf8 | 2017-07-19 14:47:42 -0400 | [diff] [blame] | 791 | int supportedSampleCount = fGpu->caps()->getSampleCount(chosenSampleCount, config); |
| 792 | return chosenSampleCount <= supportedSampleCount ? supportedSampleCount : 0; |
commit-bot@chromium.org | b471a32 | 2014-03-10 07:40:03 +0000 | [diff] [blame] | 793 | } |
| 794 | |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 795 | sk_sp<GrSurfaceContext> GrContextPriv::makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy> proxy, |
| 796 | sk_sp<SkColorSpace> colorSpace) { |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 797 | ASSERT_SINGLE_OWNER_PRIV |
| 798 | |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 799 | if (proxy->asRenderTargetProxy()) { |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 800 | return this->drawingManager()->makeRenderTargetContext(std::move(proxy), |
| 801 | std::move(colorSpace), nullptr); |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 802 | } else { |
| 803 | SkASSERT(proxy->asTextureProxy()); |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 804 | return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace)); |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 805 | } |
| 806 | } |
| 807 | |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 808 | sk_sp<GrSurfaceContext> GrContextPriv::makeDeferredSurfaceContext(const GrSurfaceDesc& dstDesc, |
Greg Daniel | 65c7f66 | 2017-10-30 13:39:09 -0400 | [diff] [blame] | 809 | GrMipMapped mipMapped, |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 810 | SkBackingFit fit, |
| 811 | SkBudgeted isDstBudgeted) { |
| 812 | |
Greg Daniel | 65c7f66 | 2017-10-30 13:39:09 -0400 | [diff] [blame] | 813 | sk_sp<GrTextureProxy> proxy; |
| 814 | if (GrMipMapped::kNo == mipMapped) { |
| 815 | proxy = GrSurfaceProxy::MakeDeferred(fContext->resourceProvider(), dstDesc, fit, |
| 816 | isDstBudgeted); |
| 817 | } else { |
| 818 | SkASSERT(SkBackingFit::kExact == fit); |
| 819 | proxy = GrSurfaceProxy::MakeDeferredMipMap(fContext->resourceProvider(), dstDesc, |
| 820 | isDstBudgeted); |
| 821 | } |
Robert Phillips | 77b3f32 | 2017-01-31 18:24:12 -0500 | [diff] [blame] | 822 | if (!proxy) { |
| 823 | return nullptr; |
| 824 | } |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 825 | |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 826 | return this->makeWrappedSurfaceContext(std::move(proxy), nullptr); |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 827 | } |
| 828 | |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 829 | sk_sp<GrTextureContext> GrContextPriv::makeBackendTextureContext(const GrBackendTexture& tex, |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 830 | GrSurfaceOrigin origin, |
Brian Osman | c1e3705 | 2017-03-09 14:19:20 -0500 | [diff] [blame] | 831 | sk_sp<SkColorSpace> colorSpace) { |
Robert Phillips | 26caf89 | 2017-01-27 10:58:31 -0500 | [diff] [blame] | 832 | ASSERT_SINGLE_OWNER_PRIV |
| 833 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 834 | sk_sp<GrSurface> surface(fContext->resourceProvider()->wrapBackendTexture(tex)); |
Robert Phillips | 26caf89 | 2017-01-27 10:58:31 -0500 | [diff] [blame] | 835 | if (!surface) { |
| 836 | return nullptr; |
| 837 | } |
| 838 | |
Robert Phillips | 066f020 | 2017-07-25 10:16:35 -0400 | [diff] [blame] | 839 | sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface), origin)); |
Robert Phillips | 77b3f32 | 2017-01-31 18:24:12 -0500 | [diff] [blame] | 840 | if (!proxy) { |
| 841 | return nullptr; |
| 842 | } |
Robert Phillips | 26caf89 | 2017-01-27 10:58:31 -0500 | [diff] [blame] | 843 | |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 844 | return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace)); |
Robert Phillips | 26caf89 | 2017-01-27 10:58:31 -0500 | [diff] [blame] | 845 | } |
| 846 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 847 | sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureRenderTargetContext( |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 848 | const GrBackendTexture& tex, |
| 849 | GrSurfaceOrigin origin, |
| 850 | int sampleCnt, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 851 | sk_sp<SkColorSpace> colorSpace, |
Brian Osman | c1e3705 | 2017-03-09 14:19:20 -0500 | [diff] [blame] | 852 | const SkSurfaceProps* props) { |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 853 | ASSERT_SINGLE_OWNER_PRIV |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 854 | |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 855 | sk_sp<GrSurface> surface( |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 856 | fContext->resourceProvider()->wrapRenderableBackendTexture(tex, sampleCnt)); |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 857 | if (!surface) { |
| 858 | return nullptr; |
| 859 | } |
| 860 | |
Robert Phillips | 066f020 | 2017-07-25 10:16:35 -0400 | [diff] [blame] | 861 | sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface), origin)); |
Robert Phillips | 77b3f32 | 2017-01-31 18:24:12 -0500 | [diff] [blame] | 862 | if (!proxy) { |
| 863 | return nullptr; |
| 864 | } |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 865 | |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 866 | return this->drawingManager()->makeRenderTargetContext(std::move(proxy), |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 867 | std::move(colorSpace), props); |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 868 | } |
| 869 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 870 | sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendRenderTargetRenderTargetContext( |
Greg Daniel | bcf612b | 2017-05-01 13:50:58 +0000 | [diff] [blame] | 871 | const GrBackendRenderTarget& backendRT, |
| 872 | GrSurfaceOrigin origin, |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 873 | sk_sp<SkColorSpace> colorSpace, |
| 874 | const SkSurfaceProps* surfaceProps) { |
| 875 | ASSERT_SINGLE_OWNER_PRIV |
| 876 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 877 | sk_sp<GrRenderTarget> rt(fContext->resourceProvider()->wrapBackendRenderTarget(backendRT)); |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 878 | if (!rt) { |
| 879 | return nullptr; |
| 880 | } |
| 881 | |
Robert Phillips | 066f020 | 2017-07-25 10:16:35 -0400 | [diff] [blame] | 882 | sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(rt), origin)); |
Robert Phillips | 77b3f32 | 2017-01-31 18:24:12 -0500 | [diff] [blame] | 883 | if (!proxy) { |
| 884 | return nullptr; |
| 885 | } |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 886 | |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 887 | return this->drawingManager()->makeRenderTargetContext(std::move(proxy), |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 888 | std::move(colorSpace), |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 889 | surfaceProps); |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 890 | } |
| 891 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 892 | sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureAsRenderTargetRenderTargetContext( |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 893 | const GrBackendTexture& tex, |
| 894 | GrSurfaceOrigin origin, |
| 895 | int sampleCnt, |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 896 | sk_sp<SkColorSpace> colorSpace, |
| 897 | const SkSurfaceProps* surfaceProps) { |
| 898 | ASSERT_SINGLE_OWNER_PRIV |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 899 | |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 900 | sk_sp<GrSurface> surface(fContext->resourceProvider()->wrapBackendTextureAsRenderTarget( |
| 901 | tex, |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 902 | sampleCnt)); |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 903 | if (!surface) { |
| 904 | return nullptr; |
| 905 | } |
| 906 | |
Robert Phillips | 066f020 | 2017-07-25 10:16:35 -0400 | [diff] [blame] | 907 | sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface), origin)); |
Robert Phillips | 77b3f32 | 2017-01-31 18:24:12 -0500 | [diff] [blame] | 908 | if (!proxy) { |
| 909 | return nullptr; |
| 910 | } |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 911 | |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 912 | return this->drawingManager()->makeRenderTargetContext(std::move(proxy), |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 913 | std::move(colorSpace), |
| 914 | surfaceProps); |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 915 | } |
| 916 | |
Chris Dalton | fe199b7 | 2017-05-05 11:26:15 -0400 | [diff] [blame] | 917 | void GrContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) { |
| 918 | fContext->fDrawingManager->addOnFlushCallbackObject(onFlushCBObject); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | |
robertphillips | 48fde9c | 2016-09-06 05:20:20 -0700 | [diff] [blame] | 922 | static inline GrPixelConfig GrPixelConfigFallback(GrPixelConfig config) { |
Brian Osman | 78f20e0 | 2017-01-12 10:28:01 -0500 | [diff] [blame] | 923 | switch (config) { |
| 924 | case kAlpha_8_GrPixelConfig: |
| 925 | case kRGB_565_GrPixelConfig: |
| 926 | case kRGBA_4444_GrPixelConfig: |
| 927 | case kBGRA_8888_GrPixelConfig: |
| 928 | return kRGBA_8888_GrPixelConfig; |
| 929 | case kSBGRA_8888_GrPixelConfig: |
| 930 | return kSRGBA_8888_GrPixelConfig; |
| 931 | case kAlpha_half_GrPixelConfig: |
| 932 | return kRGBA_half_GrPixelConfig; |
| 933 | default: |
| 934 | return kUnknown_GrPixelConfig; |
| 935 | } |
robertphillips | 48fde9c | 2016-09-06 05:20:20 -0700 | [diff] [blame] | 936 | } |
| 937 | |
robertphillips | d728f0c | 2016-11-21 11:05:03 -0800 | [diff] [blame] | 938 | sk_sp<GrRenderTargetContext> GrContext::makeDeferredRenderTargetContextWithFallback( |
| 939 | SkBackingFit fit, |
| 940 | int width, int height, |
| 941 | GrPixelConfig config, |
| 942 | sk_sp<SkColorSpace> colorSpace, |
| 943 | int sampleCnt, |
Greg Daniel | 45d6303 | 2017-10-30 13:41:26 -0400 | [diff] [blame] | 944 | GrMipMapped mipMapped, |
robertphillips | d728f0c | 2016-11-21 11:05:03 -0800 | [diff] [blame] | 945 | GrSurfaceOrigin origin, |
| 946 | const SkSurfaceProps* surfaceProps, |
| 947 | SkBudgeted budgeted) { |
| 948 | if (!this->caps()->isConfigRenderable(config, sampleCnt > 0)) { |
| 949 | config = GrPixelConfigFallback(config); |
| 950 | } |
| 951 | |
| 952 | return this->makeDeferredRenderTargetContext(fit, width, height, config, std::move(colorSpace), |
Greg Daniel | 45d6303 | 2017-10-30 13:41:26 -0400 | [diff] [blame] | 953 | sampleCnt, mipMapped, origin, surfaceProps, |
Greg Daniel | e1da1d9 | 2017-10-06 15:59:27 -0400 | [diff] [blame] | 954 | budgeted); |
robertphillips | d728f0c | 2016-11-21 11:05:03 -0800 | [diff] [blame] | 955 | } |
| 956 | |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 957 | sk_sp<GrRenderTargetContext> GrContext::makeDeferredRenderTargetContext( |
| 958 | SkBackingFit fit, |
| 959 | int width, int height, |
| 960 | GrPixelConfig config, |
| 961 | sk_sp<SkColorSpace> colorSpace, |
| 962 | int sampleCnt, |
Greg Daniel | 45d6303 | 2017-10-30 13:41:26 -0400 | [diff] [blame] | 963 | GrMipMapped mipMapped, |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 964 | GrSurfaceOrigin origin, |
| 965 | const SkSurfaceProps* surfaceProps, |
| 966 | SkBudgeted budgeted) { |
Brian Salomon | 79e4d1b | 2017-07-13 11:17:11 -0400 | [diff] [blame] | 967 | if (this->abandoned()) { |
| 968 | return nullptr; |
| 969 | } |
| 970 | |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 971 | GrSurfaceDesc desc; |
| 972 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 973 | desc.fOrigin = origin; |
| 974 | desc.fWidth = width; |
| 975 | desc.fHeight = height; |
| 976 | desc.fConfig = config; |
| 977 | desc.fSampleCnt = sampleCnt; |
| 978 | |
Greg Daniel | e1da1d9 | 2017-10-06 15:59:27 -0400 | [diff] [blame] | 979 | sk_sp<GrTextureProxy> rtp; |
Greg Daniel | 45d6303 | 2017-10-30 13:41:26 -0400 | [diff] [blame] | 980 | if (GrMipMapped::kNo == mipMapped) { |
Greg Daniel | e1da1d9 | 2017-10-06 15:59:27 -0400 | [diff] [blame] | 981 | rtp = GrSurfaceProxy::MakeDeferred(this->resourceProvider(), desc, fit, budgeted); |
| 982 | } else { |
| 983 | rtp = GrSurfaceProxy::MakeDeferredMipMap(this->resourceProvider(), desc, budgeted); |
| 984 | } |
Robert Phillips | 08c5ec7 | 2017-01-30 12:26:47 -0500 | [diff] [blame] | 985 | if (!rtp) { |
| 986 | return nullptr; |
| 987 | } |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 988 | |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame] | 989 | sk_sp<GrRenderTargetContext> renderTargetContext( |
| 990 | fDrawingManager->makeRenderTargetContext(std::move(rtp), |
| 991 | std::move(colorSpace), |
| 992 | surfaceProps)); |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame] | 993 | if (!renderTargetContext) { |
| 994 | return nullptr; |
| 995 | } |
| 996 | |
| 997 | renderTargetContext->discard(); |
| 998 | |
| 999 | return renderTargetContext; |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 1000 | } |
| 1001 | |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 1002 | bool GrContext::abandoned() const { |
| 1003 | ASSERT_SINGLE_OWNER |
robertphillips | 7761d61 | 2016-05-16 09:14:53 -0700 | [diff] [blame] | 1004 | return fDrawingManager->wasAbandoned(); |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 1005 | } |
| 1006 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 1007 | std::unique_ptr<GrFragmentProcessor> GrContext::createPMToUPMEffect( |
| 1008 | std::unique_ptr<GrFragmentProcessor> fp, bool useConfigConversionEffect) { |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame] | 1009 | ASSERT_SINGLE_OWNER |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 1010 | // We have specialized effects that guarantee round-trip conversion for some formats |
| 1011 | if (useConfigConversionEffect) { |
| 1012 | // We should have already called this->validPMUPMConversionExists() in this case |
| 1013 | SkASSERT(fDidTestPMConversions); |
| 1014 | // ...and it should have succeeded |
| 1015 | SkASSERT(this->validPMUPMConversionExists()); |
| 1016 | |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 1017 | return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToUnpremul); |
Brian Osman | 2d2da4f | 2017-04-12 17:07:22 -0400 | [diff] [blame] | 1018 | } else { |
| 1019 | // For everything else (sRGB, half-float, etc...), it doesn't make sense to try and |
| 1020 | // explicitly round the results. Just do the obvious, naive thing in the shader. |
| 1021 | return GrFragmentProcessor::UnpremulOutput(std::move(fp)); |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame] | 1022 | } |
| 1023 | } |
| 1024 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 1025 | std::unique_ptr<GrFragmentProcessor> GrContext::createUPMToPMEffect( |
| 1026 | std::unique_ptr<GrFragmentProcessor> fp, bool useConfigConversionEffect) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 1027 | ASSERT_SINGLE_OWNER |
Brian Osman | 2d2da4f | 2017-04-12 17:07:22 -0400 | [diff] [blame] | 1028 | // We have specialized effects that guarantee round-trip conversion for these formats |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 1029 | if (useConfigConversionEffect) { |
| 1030 | // We should have already called this->validPMUPMConversionExists() in this case |
| 1031 | SkASSERT(fDidTestPMConversions); |
| 1032 | // ...and it should have succeeded |
| 1033 | SkASSERT(this->validPMUPMConversionExists()); |
| 1034 | |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 1035 | return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToPremul); |
Brian Osman | 2d2da4f | 2017-04-12 17:07:22 -0400 | [diff] [blame] | 1036 | } else { |
| 1037 | // For everything else (sRGB, half-float, etc...), it doesn't make sense to try and |
| 1038 | // explicitly round the results. Just do the obvious, naive thing in the shader. |
| 1039 | return GrFragmentProcessor::PremulOutput(std::move(fp)); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1040 | } |
| 1041 | } |
| 1042 | |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 1043 | bool GrContext::validPMUPMConversionExists() { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 1044 | ASSERT_SINGLE_OWNER |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 1045 | if (!fDidTestPMConversions) { |
Brian Osman | 28804f3 | 2017-04-20 10:24:36 -0400 | [diff] [blame] | 1046 | fPMUPMConversionsRoundTrip = GrConfigConversionEffect::TestForPreservingPMConversions(this); |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 1047 | fDidTestPMConversions = true; |
| 1048 | } |
| 1049 | |
bsalomon | 636e802 | 2015-07-29 06:08:46 -0700 | [diff] [blame] | 1050 | // The PM<->UPM tests fail or succeed together so we only need to check one. |
Brian Osman | 28804f3 | 2017-04-20 10:24:36 -0400 | [diff] [blame] | 1051 | return fPMUPMConversionsRoundTrip; |
bsalomon | 636e802 | 2015-07-29 06:08:46 -0700 | [diff] [blame] | 1052 | } |
| 1053 | |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 1054 | ////////////////////////////////////////////////////////////////////////////// |
| 1055 | |
Robert Phillips | 8d1e67e | 2017-12-04 13:48:14 -0500 | [diff] [blame] | 1056 | void GrContext::getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 1057 | ASSERT_SINGLE_OWNER |
Robert Phillips | 8d1e67e | 2017-12-04 13:48:14 -0500 | [diff] [blame] | 1058 | if (maxResources) { |
| 1059 | *maxResources = fResourceCache->getMaxResourceCount(); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 1060 | } |
Robert Phillips | 8d1e67e | 2017-12-04 13:48:14 -0500 | [diff] [blame] | 1061 | if (maxResourceBytes) { |
| 1062 | *maxResourceBytes = fResourceCache->getMaxResourceBytes(); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 1063 | } |
| 1064 | } |
| 1065 | |
Robert Phillips | 8d1e67e | 2017-12-04 13:48:14 -0500 | [diff] [blame] | 1066 | void GrContext::setResourceCacheLimits(int maxResources, size_t maxResourceBytes) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 1067 | ASSERT_SINGLE_OWNER |
Robert Phillips | 8d1e67e | 2017-12-04 13:48:14 -0500 | [diff] [blame] | 1068 | fResourceCache->setLimits(maxResources, maxResourceBytes); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 1069 | } |
| 1070 | |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 1071 | ////////////////////////////////////////////////////////////////////////////// |
| 1072 | |
| 1073 | void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 1074 | ASSERT_SINGLE_OWNER |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 1075 | fResourceCache->dumpMemoryStatistics(traceMemoryDump); |
| 1076 | } |
Brian Osman | 71a1889 | 2017-08-10 10:23:25 -0400 | [diff] [blame] | 1077 | |
| 1078 | ////////////////////////////////////////////////////////////////////////////// |
| 1079 | |
| 1080 | SkString GrContext::dump() const { |
| 1081 | SkDynamicMemoryWStream stream; |
| 1082 | SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty); |
| 1083 | writer.beginObject(); |
| 1084 | |
| 1085 | static const char* kBackendStr[] = { |
| 1086 | "Metal", |
| 1087 | "OpenGL", |
| 1088 | "Vulkan", |
| 1089 | "Mock", |
| 1090 | }; |
| 1091 | GR_STATIC_ASSERT(0 == kMetal_GrBackend); |
| 1092 | GR_STATIC_ASSERT(1 == kOpenGL_GrBackend); |
| 1093 | GR_STATIC_ASSERT(2 == kVulkan_GrBackend); |
| 1094 | GR_STATIC_ASSERT(3 == kMock_GrBackend); |
| 1095 | writer.appendString("backend", kBackendStr[fBackend]); |
| 1096 | |
| 1097 | writer.appendName("caps"); |
| 1098 | fCaps->dumpJSON(&writer); |
| 1099 | |
| 1100 | writer.appendName("gpu"); |
| 1101 | fGpu->dumpJSON(&writer); |
| 1102 | |
| 1103 | // Flush JSON to the memory stream |
| 1104 | writer.endObject(); |
| 1105 | writer.flush(); |
| 1106 | |
| 1107 | // Null terminate the JSON data in the memory stream |
| 1108 | stream.write8(0); |
| 1109 | |
| 1110 | // Allocate a string big enough to hold all the data, then copy out of the stream |
| 1111 | SkString result(stream.bytesWritten()); |
| 1112 | stream.copyToAndReset(result.writable_str()); |
| 1113 | return result; |
| 1114 | } |