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 | |
bsalomon@google.com | 1fadb20 | 2011-12-12 16:10:08 +0000 | [diff] [blame] | 8 | #include "GrContext.h" |
Brian Salomon | 5f33a8c | 2018-02-26 14:32:39 -0500 | [diff] [blame] | 9 | #include "GrBackendSemaphore.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" |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 15 | #include "GrProxyProvider.h" |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 16 | #include "GrRenderTargetContext.h" |
Brian Salomon | c65aec9 | 2017-03-09 09:03:58 -0500 | [diff] [blame] | 17 | #include "GrRenderTargetProxy.h" |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 18 | #include "GrResourceCache.h" |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 19 | #include "GrResourceProvider.h" |
Greg Daniel | d85f97d | 2017-03-07 13:37:21 -0500 | [diff] [blame] | 20 | #include "GrSemaphore.h" |
robertphillips@google.com | 72176b2 | 2012-05-23 13:19:12 +0000 | [diff] [blame] | 21 | #include "GrSoftwarePathRenderer.h" |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 22 | #include "GrSurfaceContext.h" |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 23 | #include "GrSurfacePriv.h" |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame] | 24 | #include "GrSurfaceProxyPriv.h" |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 25 | #include "GrTexture.h" |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 26 | #include "GrTextureContext.h" |
Brian Salomon | dcbb9d9 | 2017-07-19 10:53:20 -0400 | [diff] [blame] | 27 | #include "GrTracing.h" |
Matt Sarett | 485c499 | 2017-02-14 14:18:27 -0500 | [diff] [blame] | 28 | #include "SkConvertPixels.h" |
Robert Phillips | d5f9cdd | 2018-01-31 09:29:48 -0500 | [diff] [blame] | 29 | #include "SkDeferredDisplayList.h" |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 30 | #include "SkGr.h" |
Mike Reed | 7fcfb62 | 2018-02-09 13:26:46 -0500 | [diff] [blame] | 31 | #include "SkImageInfoPriv.h" |
Brian Osman | 71a1889 | 2017-08-10 10:23:25 -0400 | [diff] [blame] | 32 | #include "SkJSONWriter.h" |
Brian Osman | 5127998 | 2017-08-23 10:12:00 -0400 | [diff] [blame] | 33 | #include "SkMakeUnique.h" |
| 34 | #include "SkTaskGroup.h" |
Matt Sarett | c7b2908 | 2017-02-09 16:22:39 -0500 | [diff] [blame] | 35 | #include "SkUnPreMultiplyPriv.h" |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 36 | #include "effects/GrConfigConversionEffect.h" |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 37 | #include "gl/GrGLGpu.h" |
| 38 | #include "mock/GrMockGpu.h" |
Brian Salomon | 5f33a8c | 2018-02-26 14:32:39 -0500 | [diff] [blame] | 39 | #include "text/GrTextBlobCache.h" |
Greg Daniel | b76a72a | 2017-07-13 15:07:54 -0400 | [diff] [blame] | 40 | #ifdef SK_METAL |
| 41 | #include "mtl/GrMtlTrampoline.h" |
| 42 | #endif |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 43 | #ifdef SK_VULKAN |
| 44 | #include "vk/GrVkGpu.h" |
| 45 | #endif |
Greg Daniel | b76a72a | 2017-07-13 15:07:54 -0400 | [diff] [blame] | 46 | |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 47 | #define ASSERT_OWNED_PROXY(P) \ |
| 48 | SkASSERT(!(P) || !((P)->priv().peekTexture()) || (P)->priv().peekTexture()->getContext() == this) |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 49 | #define ASSERT_OWNED_PROXY_PRIV(P) \ |
| 50 | SkASSERT(!(P) || !((P)->priv().peekTexture()) || (P)->priv().peekTexture()->getContext() == fContext) |
| 51 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 52 | #define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this) |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 53 | #define ASSERT_SINGLE_OWNER \ |
| 54 | SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fSingleOwner);) |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 55 | #define ASSERT_SINGLE_OWNER_PRIV \ |
| 56 | SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fContext->fSingleOwner);) |
robertphillips | 7761d61 | 2016-05-16 09:14:53 -0700 | [diff] [blame] | 57 | #define RETURN_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return; } |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 58 | #define RETURN_IF_ABANDONED_PRIV if (fContext->fDrawingManager->wasAbandoned()) { return; } |
robertphillips | 7761d61 | 2016-05-16 09:14:53 -0700 | [diff] [blame] | 59 | #define RETURN_FALSE_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return false; } |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 60 | #define RETURN_FALSE_IF_ABANDONED_PRIV if (fContext->fDrawingManager->wasAbandoned()) { return false; } |
robertphillips | 7761d61 | 2016-05-16 09:14:53 -0700 | [diff] [blame] | 61 | #define RETURN_NULL_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return nullptr; } |
bsalomon@google.com | bc4b654 | 2011-11-19 13:56:11 +0000 | [diff] [blame] | 62 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 63 | //////////////////////////////////////////////////////////////////////////////// |
| 64 | |
Robert Phillips | 88260b5 | 2018-01-19 12:56:09 -0500 | [diff] [blame] | 65 | class SK_API GrDirectContext : public GrContext { |
| 66 | public: |
Robert Phillips | e5b7cee | 2018-02-27 18:00:22 -0500 | [diff] [blame] | 67 | GrDirectContext(GrBackend backend) |
| 68 | : INHERITED(backend) |
| 69 | , fFullAtlasManager(nullptr) { |
| 70 | } |
| 71 | |
| 72 | ~GrDirectContext() override { |
| 73 | this->flush(); |
| 74 | |
| 75 | delete fFullAtlasManager; |
| 76 | } |
| 77 | |
| 78 | void abandonContext() override { |
| 79 | INHERITED::abandonContext(); |
| 80 | fFullAtlasManager->freeAll(); |
| 81 | } |
| 82 | |
| 83 | void releaseResourcesAndAbandonContext() override { |
| 84 | INHERITED::releaseResourcesAndAbandonContext(); |
| 85 | fFullAtlasManager->freeAll(); |
| 86 | } |
| 87 | |
| 88 | void freeGpuResources() override { |
| 89 | this->flush(); |
| 90 | fFullAtlasManager->freeAll(); |
| 91 | |
| 92 | INHERITED::freeGpuResources(); |
| 93 | } |
Robert Phillips | 88260b5 | 2018-01-19 12:56:09 -0500 | [diff] [blame] | 94 | |
| 95 | protected: |
Robert Phillips | e5b7cee | 2018-02-27 18:00:22 -0500 | [diff] [blame] | 96 | bool init(const GrContextOptions& options) override { |
| 97 | if (!INHERITED::init(options)) { |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | GrDrawOpAtlas::AllowMultitexturing allowMultitexturing; |
| 102 | if (GrContextOptions::Enable::kNo == options.fAllowMultipleGlyphCacheTextures || |
| 103 | // multitexturing supported only if range can represent the index + texcoords fully |
| 104 | !(fCaps->shaderCaps()->floatIs32Bits() || fCaps->shaderCaps()->integerSupport())) { |
| 105 | allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kNo; |
| 106 | } else { |
| 107 | allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kYes; |
| 108 | } |
| 109 | |
| 110 | GrGlyphCache* glyphCache = this->contextPriv().getGlyphCache(); |
| 111 | GrProxyProvider* proxyProvider = this->contextPriv().proxyProvider(); |
| 112 | |
| 113 | fFullAtlasManager = new GrAtlasManager(proxyProvider, glyphCache, |
| 114 | options.fGlyphCacheTextureMaximumBytes, |
| 115 | allowMultitexturing); |
| 116 | this->contextPriv().addOnFlushCallbackObject(fFullAtlasManager); |
| 117 | |
| 118 | glyphCache->setGlyphSizeLimit(fFullAtlasManager->getGlyphSizeLimit()); |
| 119 | return true; |
| 120 | } |
| 121 | |
| 122 | GrRestrictedAtlasManager* onGetRestrictedAtlasManager() override { return fFullAtlasManager; } |
| 123 | GrAtlasManager* onGetFullAtlasManager() override { return fFullAtlasManager; } |
Robert Phillips | 88260b5 | 2018-01-19 12:56:09 -0500 | [diff] [blame] | 124 | |
| 125 | private: |
Robert Phillips | e5b7cee | 2018-02-27 18:00:22 -0500 | [diff] [blame] | 126 | GrAtlasManager* fFullAtlasManager; |
| 127 | |
Robert Phillips | 88260b5 | 2018-01-19 12:56:09 -0500 | [diff] [blame] | 128 | typedef GrContext INHERITED; |
| 129 | }; |
| 130 | |
Robert Phillips | e5b7cee | 2018-02-27 18:00:22 -0500 | [diff] [blame] | 131 | /** |
| 132 | * The DDL Context is the one in effect during DDL Recording. It isn't backed by a GrGPU and |
| 133 | * cannot allocate any GPU resources. |
| 134 | */ |
Robert Phillips | 88260b5 | 2018-01-19 12:56:09 -0500 | [diff] [blame] | 135 | class SK_API GrDDLContext : public GrContext { |
| 136 | public: |
Robert Phillips | e5b7cee | 2018-02-27 18:00:22 -0500 | [diff] [blame] | 137 | GrDDLContext(GrContextThreadSafeProxy* proxy) |
| 138 | : INHERITED(proxy) |
| 139 | , fRestrictedAtlasManager(nullptr) { |
| 140 | } |
| 141 | |
| 142 | ~GrDDLContext() override { |
| 143 | // The GrDDLContext doesn't actually own the fRestrictedAtlasManager so don't delete it |
| 144 | } |
| 145 | |
| 146 | void abandonContext() override { |
| 147 | SkASSERT(0); // abandoning in a DDL Recorder doesn't make a whole lot of sense |
| 148 | INHERITED::abandonContext(); |
| 149 | } |
| 150 | |
| 151 | void releaseResourcesAndAbandonContext() override { |
| 152 | SkASSERT(0); // abandoning in a DDL Recorder doesn't make a whole lot of sense |
| 153 | INHERITED::releaseResourcesAndAbandonContext(); |
| 154 | } |
| 155 | |
| 156 | void freeGpuResources() override { |
| 157 | SkASSERT(0); // freeing resources in a DDL Recorder doesn't make a whole lot of sense |
| 158 | INHERITED::freeGpuResources(); |
| 159 | } |
Robert Phillips | 88260b5 | 2018-01-19 12:56:09 -0500 | [diff] [blame] | 160 | |
| 161 | protected: |
Robert Phillips | e5b7cee | 2018-02-27 18:00:22 -0500 | [diff] [blame] | 162 | bool init(const GrContextOptions& options) override { |
| 163 | if (!INHERITED::init(options)) { |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | // DDL TODO: in DDL-mode grab a GrRestrictedAtlasManager from the thread-proxy and |
| 168 | // do not add an onFlushCB |
| 169 | return true; |
| 170 | } |
| 171 | |
| 172 | GrRestrictedAtlasManager* onGetRestrictedAtlasManager() override { |
| 173 | return fRestrictedAtlasManager; |
| 174 | } |
| 175 | |
| 176 | GrAtlasManager* onGetFullAtlasManager() override { |
| 177 | SkASSERT(0); // the DDL Recorders should never invoke this |
| 178 | return nullptr; |
| 179 | } |
Robert Phillips | 88260b5 | 2018-01-19 12:56:09 -0500 | [diff] [blame] | 180 | |
| 181 | private: |
Robert Phillips | e5b7cee | 2018-02-27 18:00:22 -0500 | [diff] [blame] | 182 | GrRestrictedAtlasManager* fRestrictedAtlasManager; |
| 183 | |
Robert Phillips | 88260b5 | 2018-01-19 12:56:09 -0500 | [diff] [blame] | 184 | typedef GrContext INHERITED; |
| 185 | }; |
| 186 | |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 187 | GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext) { |
| 188 | GrContextOptions defaultOptions; |
| 189 | return Create(backend, backendContext, defaultOptions); |
| 190 | } |
bsalomon | f28cff7 | 2015-05-22 12:25:41 -0700 | [diff] [blame] | 191 | |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 192 | GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext, |
| 193 | const GrContextOptions& options) { |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 194 | |
Robert Phillips | 88260b5 | 2018-01-19 12:56:09 -0500 | [diff] [blame] | 195 | sk_sp<GrContext> context(new GrDirectContext(backend)); |
Robert Phillips | e42edcc | 2017-12-13 11:50:22 -0500 | [diff] [blame] | 196 | |
| 197 | context->fGpu = GrGpu::Make(backend, backendContext, options, context.get()); |
| 198 | if (!context->fGpu) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 199 | return nullptr; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 200 | } |
Robert Phillips | e42edcc | 2017-12-13 11:50:22 -0500 | [diff] [blame] | 201 | |
| 202 | if (!context->init(options)) { |
| 203 | return nullptr; |
| 204 | } |
| 205 | |
Brian Salomon | 91a3e52 | 2017-06-23 10:58:19 -0400 | [diff] [blame] | 206 | return context.release(); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 209 | sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> interface) { |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 210 | GrContextOptions defaultOptions; |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 211 | return MakeGL(std::move(interface), defaultOptions); |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 212 | } |
| 213 | |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 214 | sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> interface, |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 215 | const GrContextOptions& options) { |
Robert Phillips | 88260b5 | 2018-01-19 12:56:09 -0500 | [diff] [blame] | 216 | sk_sp<GrContext> context(new GrDirectContext(kOpenGL_GrBackend)); |
Robert Phillips | e42edcc | 2017-12-13 11:50:22 -0500 | [diff] [blame] | 217 | |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 218 | context->fGpu = GrGLGpu::Make(std::move(interface), options, context.get()); |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 219 | if (!context->fGpu) { |
| 220 | return nullptr; |
| 221 | } |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 222 | if (!context->init(options)) { |
| 223 | return nullptr; |
| 224 | } |
| 225 | return context; |
| 226 | } |
| 227 | |
Brian Salomon | 990014d | 2017-12-07 16:23:39 -0500 | [diff] [blame] | 228 | sk_sp<GrContext> GrContext::MakeGL(const GrGLInterface* interface) { |
| 229 | return MakeGL(sk_ref_sp(interface)); |
| 230 | } |
| 231 | |
| 232 | sk_sp<GrContext> GrContext::MakeGL(const GrGLInterface* interface, |
| 233 | const GrContextOptions& options) { |
| 234 | return MakeGL(sk_ref_sp(interface), options); |
| 235 | } |
| 236 | |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 237 | sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions) { |
| 238 | GrContextOptions defaultOptions; |
| 239 | return MakeMock(mockOptions, defaultOptions); |
| 240 | } |
| 241 | |
| 242 | sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions, |
| 243 | const GrContextOptions& options) { |
Robert Phillips | 88260b5 | 2018-01-19 12:56:09 -0500 | [diff] [blame] | 244 | sk_sp<GrContext> context(new GrDirectContext(kMock_GrBackend)); |
Robert Phillips | e42edcc | 2017-12-13 11:50:22 -0500 | [diff] [blame] | 245 | |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 246 | context->fGpu = GrMockGpu::Make(mockOptions, options, context.get()); |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 247 | if (!context->fGpu) { |
| 248 | return nullptr; |
| 249 | } |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 250 | if (!context->init(options)) { |
| 251 | return nullptr; |
| 252 | } |
| 253 | return context; |
| 254 | } |
| 255 | |
| 256 | #ifdef SK_VULKAN |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 257 | sk_sp<GrContext> GrContext::MakeVulkan(sk_sp<const GrVkBackendContext> backendContext) { |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 258 | GrContextOptions defaultOptions; |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 259 | return MakeVulkan(std::move(backendContext), defaultOptions); |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 260 | } |
| 261 | |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 262 | sk_sp<GrContext> GrContext::MakeVulkan(sk_sp<const GrVkBackendContext> backendContext, |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 263 | const GrContextOptions& options) { |
Robert Phillips | 88260b5 | 2018-01-19 12:56:09 -0500 | [diff] [blame] | 264 | sk_sp<GrContext> context(new GrDirectContext(kVulkan_GrBackend)); |
Robert Phillips | e42edcc | 2017-12-13 11:50:22 -0500 | [diff] [blame] | 265 | |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 266 | context->fGpu = GrVkGpu::Make(std::move(backendContext), options, context.get()); |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 267 | if (!context->fGpu) { |
| 268 | return nullptr; |
| 269 | } |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 270 | if (!context->init(options)) { |
| 271 | return nullptr; |
| 272 | } |
| 273 | return context; |
| 274 | } |
| 275 | #endif |
| 276 | |
Greg Daniel | b76a72a | 2017-07-13 15:07:54 -0400 | [diff] [blame] | 277 | #ifdef SK_METAL |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 278 | sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue) { |
| 279 | GrContextOptions defaultOptions; |
| 280 | return MakeMetal(device, queue, defaultOptions); |
| 281 | } |
| 282 | |
Greg Daniel | b76a72a | 2017-07-13 15:07:54 -0400 | [diff] [blame] | 283 | sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue, const GrContextOptions& options) { |
Robert Phillips | e5b7cee | 2018-02-27 18:00:22 -0500 | [diff] [blame] | 284 | sk_sp<GrContext> context(new GrDirectContext(kMetal_GrBackend)); |
Robert Phillips | e42edcc | 2017-12-13 11:50:22 -0500 | [diff] [blame] | 285 | |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 286 | context->fGpu = GrMtlTrampoline::MakeGpu(context.get(), options, device, queue); |
Greg Daniel | b76a72a | 2017-07-13 15:07:54 -0400 | [diff] [blame] | 287 | if (!context->fGpu) { |
| 288 | return nullptr; |
| 289 | } |
Greg Daniel | b76a72a | 2017-07-13 15:07:54 -0400 | [diff] [blame] | 290 | if (!context->init(options)) { |
| 291 | return nullptr; |
| 292 | } |
| 293 | return context; |
| 294 | } |
| 295 | #endif |
| 296 | |
joshualitt | 0acd0d3 | 2015-05-07 08:23:19 -0700 | [diff] [blame] | 297 | static int32_t gNextID = 1; |
| 298 | static int32_t next_id() { |
| 299 | int32_t id; |
| 300 | do { |
| 301 | id = sk_atomic_inc(&gNextID); |
| 302 | } while (id == SK_InvalidGenID); |
| 303 | return id; |
| 304 | } |
| 305 | |
Robert Phillips | e42edcc | 2017-12-13 11:50:22 -0500 | [diff] [blame] | 306 | sk_sp<GrContext> GrContextPriv::MakeDDL(GrContextThreadSafeProxy* proxy) { |
Robert Phillips | 88260b5 | 2018-01-19 12:56:09 -0500 | [diff] [blame] | 307 | sk_sp<GrContext> context(new GrDDLContext(proxy)); |
Robert Phillips | e42edcc | 2017-12-13 11:50:22 -0500 | [diff] [blame] | 308 | |
Robert Phillips | 88260b5 | 2018-01-19 12:56:09 -0500 | [diff] [blame] | 309 | // Note: we aren't creating a Gpu here. This causes the resource provider & cache to |
| 310 | // also not be created |
Robert Phillips | e42edcc | 2017-12-13 11:50:22 -0500 | [diff] [blame] | 311 | if (!context->init(proxy->fOptions)) { |
| 312 | return nullptr; |
| 313 | } |
| 314 | return context; |
| 315 | } |
| 316 | |
| 317 | GrContext::GrContext(GrBackend backend) |
| 318 | : fUniqueID(next_id()) |
| 319 | , fBackend(backend) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 320 | fResourceCache = nullptr; |
| 321 | fResourceProvider = nullptr; |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 322 | fProxyProvider = nullptr; |
Robert Phillips | acf1790 | 2018-02-27 16:43:18 -0500 | [diff] [blame] | 323 | fGlyphCache = nullptr; |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Robert Phillips | e42edcc | 2017-12-13 11:50:22 -0500 | [diff] [blame] | 326 | GrContext::GrContext(GrContextThreadSafeProxy* proxy) |
Robert Phillips | 88260b5 | 2018-01-19 12:56:09 -0500 | [diff] [blame] | 327 | : fCaps(proxy->fCaps) |
| 328 | , fUniqueID(proxy->fContextUniqueID) |
Robert Phillips | e42edcc | 2017-12-13 11:50:22 -0500 | [diff] [blame] | 329 | , fBackend(proxy->fBackend) { |
| 330 | fResourceCache = nullptr; |
| 331 | fResourceProvider = nullptr; |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 332 | fProxyProvider = nullptr; |
Robert Phillips | acf1790 | 2018-02-27 16:43:18 -0500 | [diff] [blame] | 333 | fGlyphCache = nullptr; |
Greg Daniel | b76a72a | 2017-07-13 15:07:54 -0400 | [diff] [blame] | 334 | } |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 335 | |
Greg Daniel | b76a72a | 2017-07-13 15:07:54 -0400 | [diff] [blame] | 336 | bool GrContext::init(const GrContextOptions& options) { |
| 337 | ASSERT_SINGLE_OWNER |
Robert Phillips | 88260b5 | 2018-01-19 12:56:09 -0500 | [diff] [blame] | 338 | |
| 339 | if (fGpu) { |
| 340 | fCaps = fGpu->refCaps(); |
| 341 | fResourceCache = new GrResourceCache(fCaps.get(), fUniqueID); |
Robert Phillips | 4150eea | 2018-02-07 17:08:21 -0500 | [diff] [blame] | 342 | fResourceProvider = new GrResourceProvider(fGpu.get(), fResourceCache, &fSingleOwner, |
| 343 | options.fExplicitlyAllocateGPUResources); |
Robert Phillips | 88260b5 | 2018-01-19 12:56:09 -0500 | [diff] [blame] | 344 | } |
| 345 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 346 | fProxyProvider = new GrProxyProvider(fResourceProvider, fResourceCache, fCaps, &fSingleOwner); |
Robert Phillips | 88260b5 | 2018-01-19 12:56:09 -0500 | [diff] [blame] | 347 | |
| 348 | if (fResourceCache) { |
| 349 | fResourceCache->setProxyProvider(fProxyProvider); |
| 350 | } |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 351 | |
Robert Phillips | e42edcc | 2017-12-13 11:50:22 -0500 | [diff] [blame] | 352 | // DDL TODO: we need to think through how the task group & persistent cache |
| 353 | // get passed on to/shared between all the DDLRecorders created with this context. |
| 354 | fThreadSafeProxy.reset(new GrContextThreadSafeProxy(fCaps, this->uniqueID(), fBackend, |
| 355 | options)); |
commit-bot@chromium.org | 1836d33 | 2013-07-16 22:55:03 +0000 | [diff] [blame] | 356 | |
Brian Osman | 46da1cc | 2017-02-14 14:15:48 -0500 | [diff] [blame] | 357 | fDisableGpuYUVConversion = options.fDisableGpuYUVConversion; |
Brian Osman | 8a83ca4 | 2018-02-12 14:32:17 -0500 | [diff] [blame] | 358 | fSharpenMipmappedTextures = options.fSharpenMipmappedTextures; |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 359 | fDidTestPMConversions = false; |
| 360 | |
bsalomon | 6b2552f | 2016-09-15 13:50:26 -0700 | [diff] [blame] | 361 | GrPathRendererChain::Options prcOptions; |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 362 | prcOptions.fAllowPathMaskCaching = options.fAllowPathMaskCaching; |
Brian Osman | 195c05b | 2017-08-30 15:14:04 -0400 | [diff] [blame] | 363 | #if GR_TEST_UTILS |
csmartdalton | 008b9d8 | 2017-02-22 12:00:42 -0700 | [diff] [blame] | 364 | prcOptions.fGpuPathRenderers = options.fGpuPathRenderers; |
Brian Osman | 195c05b | 2017-08-30 15:14:04 -0400 | [diff] [blame] | 365 | #endif |
Brian Osman | b350ae2 | 2017-08-29 16:15:39 -0400 | [diff] [blame] | 366 | if (options.fDisableDistanceFieldPaths) { |
Brian Osman | 195c05b | 2017-08-30 15:14:04 -0400 | [diff] [blame] | 367 | prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kSmall; |
Brian Osman | b350ae2 | 2017-08-29 16:15:39 -0400 | [diff] [blame] | 368 | } |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 369 | |
Robert Phillips | d5f9cdd | 2018-01-31 09:29:48 -0500 | [diff] [blame] | 370 | if (!fResourceCache) { |
| 371 | // DDL TODO: remove this crippling of the path renderer chain |
| 372 | // Disable the small path renderer bc of the proxies in the atlas. They need to be |
| 373 | // unified when the opLists are added back to the destination drawing manager. |
| 374 | prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kSmall; |
| 375 | } |
| 376 | |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 377 | GrAtlasTextContext::Options atlasTextContextOptions; |
| 378 | atlasTextContextOptions.fMaxDistanceFieldFontSize = options.fGlyphsAsPathsFontSize; |
| 379 | atlasTextContextOptions.fMinDistanceFieldFontSize = options.fMinDistanceFieldFontSize; |
Brian Salomon | b508696 | 2017-12-13 10:59:33 -0500 | [diff] [blame] | 380 | atlasTextContextOptions.fDistanceFieldVerticesAlwaysHaveW = false; |
| 381 | #if SK_SUPPORT_ATLAS_TEXT |
| 382 | if (GrContextOptions::Enable::kYes == options.fDistanceFieldGlyphVerticesAlwaysHaveW) { |
| 383 | atlasTextContextOptions.fDistanceFieldVerticesAlwaysHaveW = true; |
| 384 | } |
| 385 | #endif |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 386 | |
Robert Phillips | 4150eea | 2018-02-07 17:08:21 -0500 | [diff] [blame] | 387 | fDrawingManager.reset(new GrDrawingManager(this, prcOptions, atlasTextContextOptions, |
| 388 | &fSingleOwner, options.fSortRenderTargets)); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 389 | |
Robert Phillips | acf1790 | 2018-02-27 16:43:18 -0500 | [diff] [blame] | 390 | fGlyphCache = new GrGlyphCache; |
| 391 | |
Robert Phillips | 303cd58 | 2018-02-14 18:54:01 -0500 | [diff] [blame] | 392 | fTextBlobCache.reset(new GrTextBlobCache(TextBlobCacheOverBudgetCB, |
| 393 | this, this->uniqueID(), SkToBool(fGpu))); |
Brian Salomon | 91a3e52 | 2017-06-23 10:58:19 -0400 | [diff] [blame] | 394 | |
Brian Osman | 5127998 | 2017-08-23 10:12:00 -0400 | [diff] [blame] | 395 | if (options.fExecutor) { |
| 396 | fTaskGroup = skstd::make_unique<SkTaskGroup>(*options.fExecutor); |
| 397 | } |
| 398 | |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 399 | fPersistentCache = options.fPersistentCache; |
| 400 | |
Brian Salomon | 91a3e52 | 2017-06-23 10:58:19 -0400 | [diff] [blame] | 401 | return true; |
bsalomon@google.com | c0af317 | 2012-06-15 14:10:09 +0000 | [diff] [blame] | 402 | } |
| 403 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 404 | GrContext::~GrContext() { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 405 | ASSERT_SINGLE_OWNER |
| 406 | |
Robert Phillips | 2e6feed | 2018-01-22 15:27:20 -0500 | [diff] [blame] | 407 | if (fDrawingManager) { |
| 408 | fDrawingManager->cleanup(); |
| 409 | } |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 410 | |
robertphillips@google.com | 950b1b0 | 2013-10-21 17:37:28 +0000 | [diff] [blame] | 411 | for (int i = 0; i < fCleanUpData.count(); ++i) { |
| 412 | (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo); |
| 413 | } |
| 414 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 415 | delete fResourceProvider; |
| 416 | delete fResourceCache; |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 417 | delete fProxyProvider; |
Robert Phillips | acf1790 | 2018-02-27 16:43:18 -0500 | [diff] [blame] | 418 | delete fGlyphCache; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 419 | } |
| 420 | |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 421 | sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() { |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 422 | return fThreadSafeProxy; |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 423 | } |
| 424 | |
Robert Phillips | fc711a2 | 2018-02-13 17:03:00 -0500 | [diff] [blame] | 425 | SkSurfaceCharacterization GrContextThreadSafeProxy::createCharacterization( |
| 426 | size_t cacheMaxResourceBytes, |
| 427 | const SkImageInfo& ii, const GrBackendFormat& backendFormat, |
| 428 | int sampleCnt, GrSurfaceOrigin origin, |
| 429 | const SkSurfaceProps& surfaceProps, |
| 430 | bool isMipMapped) { |
| 431 | if (!backendFormat.isValid()) { |
| 432 | return SkSurfaceCharacterization(); // return an invalid characterization |
| 433 | } |
| 434 | |
| 435 | // We're assuming GrFSAAType::kMixedSamples will never be specified via this code path |
| 436 | GrFSAAType FSAAType = sampleCnt > 1 ? GrFSAAType::kUnifiedMSAA : GrFSAAType::kNone; |
| 437 | |
| 438 | if (!fCaps->mipMapSupport()) { |
| 439 | isMipMapped = false; |
| 440 | } |
| 441 | |
| 442 | GrPixelConfig config = kUnknown_GrPixelConfig; |
| 443 | if (!fCaps->getConfigFromBackendFormat(backendFormat, ii.colorType(), &config)) { |
| 444 | return SkSurfaceCharacterization(); // return an invalid characterization |
| 445 | } |
| 446 | |
| 447 | // This surface characterization factory assumes that the resulting characterization is |
| 448 | // textureable. |
| 449 | if (!fCaps->isConfigTexturable(config)) { |
| 450 | return SkSurfaceCharacterization(); // return an invalid characterization |
| 451 | } |
| 452 | |
| 453 | return SkSurfaceCharacterization(sk_ref_sp<GrContextThreadSafeProxy>(this), |
| 454 | cacheMaxResourceBytes, |
| 455 | origin, ii.width(), ii.height(), config, FSAAType, sampleCnt, |
| 456 | SkSurfaceCharacterization::Textureable(true), |
| 457 | SkSurfaceCharacterization::MipMapped(isMipMapped), |
| 458 | ii.refColorSpace(), surfaceProps); |
| 459 | } |
| 460 | |
bsalomon | 2354f84 | 2014-07-28 13:48:36 -0700 | [diff] [blame] | 461 | void GrContext::abandonContext() { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 462 | ASSERT_SINGLE_OWNER |
| 463 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 464 | fProxyProvider->abandon(); |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 465 | fResourceProvider->abandon(); |
robertphillips | 0dfa62c | 2015-11-16 06:23:31 -0800 | [diff] [blame] | 466 | |
| 467 | // Need to abandon the drawing manager first so all the render targets |
| 468 | // will be released/forgotten before they too are abandoned. |
| 469 | fDrawingManager->abandon(); |
| 470 | |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 471 | // abandon first to so destructors |
| 472 | // don't try to free the resources in the API. |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 473 | fResourceCache->abandonAll(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 474 | |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 475 | fGpu->disconnect(GrGpu::DisconnectType::kAbandon); |
| 476 | |
Robert Phillips | acf1790 | 2018-02-27 16:43:18 -0500 | [diff] [blame] | 477 | fGlyphCache->freeAll(); |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 478 | fTextBlobCache->freeAll(); |
| 479 | } |
| 480 | |
| 481 | void GrContext::releaseResourcesAndAbandonContext() { |
| 482 | ASSERT_SINGLE_OWNER |
| 483 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 484 | fProxyProvider->abandon(); |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 485 | fResourceProvider->abandon(); |
| 486 | |
| 487 | // Need to abandon the drawing manager first so all the render targets |
| 488 | // will be released/forgotten before they too are abandoned. |
| 489 | fDrawingManager->abandon(); |
| 490 | |
| 491 | // Release all resources in the backend 3D API. |
| 492 | fResourceCache->releaseAll(); |
| 493 | |
| 494 | fGpu->disconnect(GrGpu::DisconnectType::kCleanup); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 495 | |
Robert Phillips | acf1790 | 2018-02-27 16:43:18 -0500 | [diff] [blame] | 496 | fGlyphCache->freeAll(); |
joshualitt | 26ffc00 | 2015-04-16 11:24:04 -0700 | [diff] [blame] | 497 | fTextBlobCache->freeAll(); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 498 | } |
| 499 | |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 500 | void GrContext::resetContext(uint32_t state) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 501 | ASSERT_SINGLE_OWNER |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 502 | fGpu->markContextDirty(state); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | void GrContext::freeGpuResources() { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 506 | ASSERT_SINGLE_OWNER |
| 507 | |
Robert Phillips | acf1790 | 2018-02-27 16:43:18 -0500 | [diff] [blame] | 508 | fGlyphCache->freeAll(); |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 509 | |
| 510 | fDrawingManager->freeGpuResources(); |
bsalomon | 3033b9f | 2015-04-13 11:09:56 -0700 | [diff] [blame] | 511 | |
| 512 | fResourceCache->purgeAllUnlocked(); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 513 | } |
| 514 | |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 515 | void GrContext::performDeferredCleanup(std::chrono::milliseconds msNotUsed) { |
Brian Salomon | 5e15085 | 2017-03-22 14:53:13 -0400 | [diff] [blame] | 516 | ASSERT_SINGLE_OWNER |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 517 | fResourceCache->purgeAsNeeded(); |
| 518 | fResourceCache->purgeResourcesNotUsedSince(GrStdSteadyClock::now() - msNotUsed); |
| 519 | |
| 520 | fTextBlobCache->purgeStaleBlobs(); |
Brian Salomon | 5e15085 | 2017-03-22 14:53:13 -0400 | [diff] [blame] | 521 | } |
| 522 | |
Derek Sollenberger | 5480a18 | 2017-05-25 16:43:59 -0400 | [diff] [blame] | 523 | void GrContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) { |
| 524 | ASSERT_SINGLE_OWNER |
| 525 | fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources); |
| 526 | } |
| 527 | |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 528 | void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 529 | ASSERT_SINGLE_OWNER |
| 530 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 531 | if (resourceCount) { |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 532 | *resourceCount = fResourceCache->getBudgetedResourceCount(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 533 | } |
| 534 | if (resourceBytes) { |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 535 | *resourceBytes = fResourceCache->getBudgetedResourceBytes(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 536 | } |
commit-bot@chromium.org | d8a57af | 2014-03-19 21:19:16 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Derek Sollenberger | ee47914 | 2017-05-24 11:41:33 -0400 | [diff] [blame] | 539 | size_t GrContext::getResourceCachePurgeableBytes() const { |
| 540 | ASSERT_SINGLE_OWNER |
| 541 | return fResourceCache->getPurgeableBytes(); |
| 542 | } |
| 543 | |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 544 | //////////////////////////////////////////////////////////////////////////////// |
| 545 | |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 546 | bool GrContext::colorTypeSupportedAsImage(SkColorType colorType) const { |
| 547 | GrPixelConfig config = SkImageInfo2GrPixelConfig(colorType, nullptr, *this->caps()); |
| 548 | return this->caps()->isConfigTexturable(config); |
| 549 | } |
| 550 | |
| 551 | int GrContext::maxSurfaceSampleCountForColorType(SkColorType colorType) const { |
| 552 | GrPixelConfig config = SkImageInfo2GrPixelConfig(colorType, nullptr, *this->caps()); |
| 553 | return this->caps()->maxRenderTargetSampleCount(config); |
| 554 | } |
| 555 | |
| 556 | //////////////////////////////////////////////////////////////////////////////// |
| 557 | |
joshualitt | 0db6dfa | 2015-04-10 07:01:30 -0700 | [diff] [blame] | 558 | void GrContext::TextBlobCacheOverBudgetCB(void* data) { |
| 559 | SkASSERT(data); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 560 | // TextBlobs are drawn at the SkGpuDevice level, therefore they cannot rely on |
| 561 | // GrRenderTargetContext to perform a necessary flush. The solution is to move drawText calls |
| 562 | // to below the GrContext level, but this is not trivial because they call drawPath on |
| 563 | // SkGpuDevice. |
joshualitt | 0db6dfa | 2015-04-10 07:01:30 -0700 | [diff] [blame] | 564 | GrContext* context = reinterpret_cast<GrContext*>(data); |
| 565 | context->flush(); |
| 566 | } |
| 567 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 568 | //////////////////////////////////////////////////////////////////////////////// |
| 569 | |
bsalomon | b77a907 | 2016-09-07 10:02:04 -0700 | [diff] [blame] | 570 | void GrContext::flush() { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 571 | ASSERT_SINGLE_OWNER |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 572 | RETURN_IF_ABANDONED |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 573 | |
| 574 | fDrawingManager->flush(nullptr); |
| 575 | } |
| 576 | |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 577 | GrSemaphoresSubmitted GrContext::flushAndSignalSemaphores(int numSemaphores, |
| 578 | GrBackendSemaphore signalSemaphores[]) { |
| 579 | ASSERT_SINGLE_OWNER |
| 580 | if (fDrawingManager->wasAbandoned()) { return GrSemaphoresSubmitted::kNo; } |
| 581 | |
| 582 | return fDrawingManager->flush(nullptr, numSemaphores, signalSemaphores); |
| 583 | } |
| 584 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 585 | void GrContextPriv::flush(GrSurfaceProxy* proxy) { |
| 586 | ASSERT_SINGLE_OWNER_PRIV |
| 587 | RETURN_IF_ABANDONED_PRIV |
| 588 | ASSERT_OWNED_PROXY_PRIV(proxy); |
| 589 | |
| 590 | fContext->fDrawingManager->flush(proxy); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 591 | } |
| 592 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 593 | bool sw_convert_to_premul(GrColorType srcColorType, int width, int height, size_t inRowBytes, |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 594 | const void* inPixels, size_t outRowBytes, void* outPixels) { |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 595 | SkColorType colorType = GrColorTypeToSkColorType(srcColorType); |
| 596 | if (kUnknown_SkColorType == colorType || 4 != SkColorTypeBytesPerPixel(colorType)) { |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 597 | return false; |
| 598 | } |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 599 | |
Matt Sarett | c7b2908 | 2017-02-09 16:22:39 -0500 | [diff] [blame] | 600 | for (int y = 0; y < height; y++) { |
| 601 | SkOpts::RGBA_to_rgbA((uint32_t*) outPixels, inPixels, width); |
| 602 | outPixels = SkTAddOffset<void>(outPixels, outRowBytes); |
| 603 | inPixels = SkTAddOffset<const void>(inPixels, inRowBytes); |
| 604 | } |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 605 | |
Matt Sarett | c7b2908 | 2017-02-09 16:22:39 -0500 | [diff] [blame] | 606 | return true; |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 607 | } |
| 608 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 609 | // TODO: This will be removed when GrSurfaceContexts are aware of their color types. |
| 610 | // (skbug.com/6718) |
Brian Osman | d2ca59a | 2017-04-13 14:03:57 -0400 | [diff] [blame] | 611 | static bool valid_premul_config(GrPixelConfig config) { |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 612 | switch (config) { |
| 613 | case kUnknown_GrPixelConfig: return false; |
| 614 | case kAlpha_8_GrPixelConfig: return false; |
| 615 | case kGray_8_GrPixelConfig: return false; |
| 616 | case kRGB_565_GrPixelConfig: return false; |
| 617 | case kRGBA_4444_GrPixelConfig: return true; |
| 618 | case kRGBA_8888_GrPixelConfig: return true; |
| 619 | case kBGRA_8888_GrPixelConfig: return true; |
| 620 | case kSRGBA_8888_GrPixelConfig: return true; |
| 621 | case kSBGRA_8888_GrPixelConfig: return true; |
| 622 | case kRGBA_float_GrPixelConfig: return true; |
| 623 | case kRG_float_GrPixelConfig: return false; |
| 624 | case kAlpha_half_GrPixelConfig: return false; |
| 625 | case kRGBA_half_GrPixelConfig: return true; |
| 626 | case kAlpha_8_as_Alpha_GrPixelConfig: return false; |
| 627 | case kAlpha_8_as_Red_GrPixelConfig: return false; |
| 628 | case kAlpha_half_as_Red_GrPixelConfig: return false; |
| 629 | case kGray_8_as_Lum_GrPixelConfig: return false; |
| 630 | case kGray_8_as_Red_GrPixelConfig: return false; |
| 631 | } |
| 632 | SK_ABORT("Invalid GrPixelConfig"); |
| 633 | return false; |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 634 | } |
| 635 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 636 | static bool valid_premul_color_type(GrColorType ct) { |
| 637 | switch (ct) { |
| 638 | case GrColorType::kUnknown: return false; |
| 639 | case GrColorType::kAlpha_8: return false; |
| 640 | case GrColorType::kRGB_565: return false; |
| 641 | case GrColorType::kABGR_4444: return true; |
| 642 | case GrColorType::kRGBA_8888: return true; |
| 643 | case GrColorType::kBGRA_8888: return true; |
| 644 | case GrColorType::kGray_8: return false; |
| 645 | case GrColorType::kAlpha_F16: return false; |
| 646 | case GrColorType::kRGBA_F16: return true; |
| 647 | case GrColorType::kRG_F32: return false; |
| 648 | case GrColorType::kRGBA_F32: return true; |
| 649 | } |
| 650 | SK_ABORT("Invalid GrColorType"); |
| 651 | return false; |
| 652 | } |
| 653 | |
| 654 | static bool valid_pixel_conversion(GrColorType cpuColorType, GrPixelConfig gpuConfig, |
Brian Osman | d2ca59a | 2017-04-13 14:03:57 -0400 | [diff] [blame] | 655 | bool premulConversion) { |
Brian Osman | d2ca59a | 2017-04-13 14:03:57 -0400 | [diff] [blame] | 656 | // We only allow premul <-> unpremul conversions for some formats |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 657 | if (premulConversion && |
| 658 | (!valid_premul_color_type(cpuColorType) || !valid_premul_config(gpuConfig))) { |
Brian Osman | d2ca59a | 2017-04-13 14:03:57 -0400 | [diff] [blame] | 659 | return false; |
| 660 | } |
| 661 | |
| 662 | return true; |
| 663 | } |
| 664 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 665 | static bool pm_upm_must_round_trip(GrColorType cpuColorType, const SkColorSpace* cpuColorSpace) { |
| 666 | return !cpuColorSpace && |
| 667 | (GrColorType::kRGBA_8888 == cpuColorType || GrColorType::kBGRA_8888 == cpuColorType); |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 668 | } |
| 669 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 670 | // TODO: This will be removed when GrSurfaceContexts are aware of their color types. |
| 671 | // (skbug.com/6718) |
| 672 | static bool pm_upm_must_round_trip(GrPixelConfig surfaceConfig, |
| 673 | const SkColorSpace* surfaceColorSpace) { |
| 674 | return !surfaceColorSpace && |
| 675 | (kRGBA_8888_GrPixelConfig == surfaceConfig || kBGRA_8888_GrPixelConfig == surfaceConfig); |
| 676 | } |
| 677 | |
| 678 | static GrSRGBConversion determine_write_pixels_srgb_conversion(GrColorType srcColorType, |
| 679 | const SkColorSpace* srcColorSpace, |
| 680 | GrSRGBEncoded dstSRGBEncoded, |
| 681 | const SkColorSpace* dstColorSpace, |
| 682 | const GrCaps& caps) { |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 683 | // No support for sRGB-encoded alpha. |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 684 | if (GrColorTypeIsAlphaOnly(srcColorType)) { |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 685 | return GrSRGBConversion::kNone; |
| 686 | } |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 687 | // No conversions without GPU support for sRGB. (Legacy mode) |
| 688 | if (!caps.srgbSupport()) { |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 689 | return GrSRGBConversion::kNone; |
| 690 | } |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 691 | // If the GrSurfaceContext has no color space then it is in legacy mode. |
| 692 | if (!dstColorSpace) { |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 693 | return GrSRGBConversion::kNone; |
| 694 | } |
| 695 | |
| 696 | bool srcColorSpaceIsSRGB = srcColorSpace && srcColorSpace->gammaCloseToSRGB(); |
| 697 | bool dstColorSpaceIsSRGB = dstColorSpace->gammaCloseToSRGB(); |
| 698 | |
| 699 | // For now we are assuming that if color space of the dst does not have sRGB gamma then the |
| 700 | // texture format is not sRGB encoded and vice versa. Note that we already checked for "legacy" |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 701 | // mode being forced on by caps above. This may change in the future. We will then have to |
| 702 | // perform shader based conversions. |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 703 | SkASSERT(dstColorSpaceIsSRGB == (GrSRGBEncoded::kYes == dstSRGBEncoded)); |
| 704 | |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 705 | if (srcColorSpaceIsSRGB == dstColorSpaceIsSRGB) { |
| 706 | return GrSRGBConversion::kNone; |
| 707 | } |
| 708 | return srcColorSpaceIsSRGB ? GrSRGBConversion::kSRGBToLinear : GrSRGBConversion::kLinearToSRGB; |
| 709 | } |
| 710 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 711 | static GrSRGBConversion determine_read_pixels_srgb_conversion(GrSRGBEncoded srcSRGBEncoded, |
| 712 | const SkColorSpace* srcColorSpace, |
| 713 | GrColorType dstColorType, |
| 714 | const SkColorSpace* dstColorSpace, |
| 715 | const GrCaps& caps) { |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 716 | // This is symmetrical with the write version. |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 717 | switch (determine_write_pixels_srgb_conversion(dstColorType, dstColorSpace, srcSRGBEncoded, |
| 718 | srcColorSpace, caps)) { |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 719 | case GrSRGBConversion::kNone: return GrSRGBConversion::kNone; |
| 720 | case GrSRGBConversion::kLinearToSRGB: return GrSRGBConversion::kSRGBToLinear; |
| 721 | case GrSRGBConversion::kSRGBToLinear: return GrSRGBConversion::kLinearToSRGB; |
| 722 | } |
| 723 | return GrSRGBConversion::kNone; |
| 724 | } |
| 725 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 726 | bool GrContextPriv::writeSurfacePixels(GrSurfaceContext* dst, int left, int top, int width, |
| 727 | int height, GrColorType srcColorType, |
| 728 | SkColorSpace* srcColorSpace, const void* buffer, |
| 729 | size_t rowBytes, uint32_t pixelOpsFlags) { |
Brian Salomon | 5f33a8c | 2018-02-26 14:32:39 -0500 | [diff] [blame] | 730 | #ifndef SK_LEGACY_GPU_PIXEL_OPS |
| 731 | return this->writeSurfacePixels2(dst, left, top, width, height, srcColorType, srcColorSpace, |
| 732 | buffer, rowBytes, pixelOpsFlags); |
| 733 | #endif |
| 734 | |
Brian Osman | b62ea22 | 2016-12-22 11:12:16 -0500 | [diff] [blame] | 735 | // TODO: Color space conversion |
| 736 | |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 737 | ASSERT_SINGLE_OWNER_PRIV |
| 738 | RETURN_FALSE_IF_ABANDONED_PRIV |
Robert Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 739 | SkASSERT(dst); |
| 740 | ASSERT_OWNED_PROXY_PRIV(dst->asSurfaceProxy()); |
Brian Salomon | dcbb9d9 | 2017-07-19 10:53:20 -0400 | [diff] [blame] | 741 | GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "writeSurfacePixels", fContext); |
bsalomon | 6c6f658 | 2015-09-10 08:12:46 -0700 | [diff] [blame] | 742 | |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 743 | if (!dst->asSurfaceProxy()->instantiate(this->resourceProvider())) { |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 744 | return false; |
| 745 | } |
| 746 | |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 747 | GrSurfaceProxy* dstProxy = dst->asSurfaceProxy(); |
| 748 | GrSurface* dstSurface = dstProxy->priv().peekSurface(); |
Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 749 | |
Brian Osman | d2ca59a | 2017-04-13 14:03:57 -0400 | [diff] [blame] | 750 | // 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] | 751 | const bool premul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 752 | |
| 753 | if (!valid_pixel_conversion(srcColorType, dstProxy->config(), premul)) { |
Brian Osman | d2ca59a | 2017-04-13 14:03:57 -0400 | [diff] [blame] | 754 | return false; |
| 755 | } |
| 756 | |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 757 | // We need to guarantee round-trip conversion if we are reading and writing 8888 non-sRGB data, |
| 758 | // without any color spaces attached, and the caller wants us to premul. |
Robert Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 759 | bool useConfigConversionEffect = |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 760 | premul && pm_upm_must_round_trip(srcColorType, srcColorSpace) && |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 761 | pm_upm_must_round_trip(dstProxy->config(), dst->colorSpaceInfo().colorSpace()); |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 762 | |
| 763 | // Are we going to try to premul as part of a draw? For the non-legacy case, we always allow |
| 764 | // this. GrConfigConversionEffect fails on some GPUs, so only allow this if it works perfectly. |
| 765 | bool premulOnGpu = premul && |
| 766 | (!useConfigConversionEffect || fContext->validPMUPMConversionExists()); |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 767 | |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 768 | // 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] | 769 | // necessary and because GrGpu::getWritePixelsInfo requires it. |
Robert Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 770 | if (!GrSurfacePriv::AdjustWritePixelParams(dstSurface->width(), dstSurface->height(), |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 771 | GrColorTypeBytesPerPixel(srcColorType), &left, &top, |
| 772 | &width, &height, &buffer, &rowBytes)) { |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 773 | return false; |
| 774 | } |
| 775 | |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 776 | GrGpu::DrawPreference drawPreference = premulOnGpu ? GrGpu::kCallerPrefersDraw_DrawPreference |
| 777 | : GrGpu::kNoDraw_DrawPreference; |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 778 | GrGpu::WritePixelTempDrawInfo tempDrawInfo; |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 779 | GrSRGBConversion srgbConversion = determine_write_pixels_srgb_conversion( |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 780 | srcColorType, srcColorSpace, GrPixelConfigIsSRGBEncoded(dstProxy->config()), |
| 781 | dst->colorSpaceInfo().colorSpace(), *fContext->caps()); |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 782 | if (!fContext->fGpu->getWritePixelsInfo(dstSurface, dstProxy->origin(), width, height, |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 783 | srcColorType, srgbConversion, &drawPreference, |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 784 | &tempDrawInfo)) { |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 785 | return false; |
| 786 | } |
| 787 | |
Robert Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 788 | if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && dstSurface->surfacePriv().hasPendingIO()) { |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 789 | this->flush(nullptr); // MDB TODO: tighten this |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 790 | } |
| 791 | |
Robert Phillips | 2f49314 | 2017-03-02 18:18:38 -0500 | [diff] [blame] | 792 | sk_sp<GrTextureProxy> tempProxy; |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 793 | if (GrGpu::kNoDraw_DrawPreference != drawPreference) { |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 794 | tempProxy = this->proxyProvider()->createProxy(tempDrawInfo.fTempSurfaceDesc, |
| 795 | SkBackingFit::kApprox, |
| 796 | SkBudgeted::kYes); |
Robert Phillips | 2f49314 | 2017-03-02 18:18:38 -0500 | [diff] [blame] | 797 | if (!tempProxy && GrGpu::kRequireDraw_DrawPreference == drawPreference) { |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 798 | return false; |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | // temp buffer for doing sw premul conversion, if needed. |
| 803 | SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0); |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 804 | // We need to do sw premul if we were unable to create a RT for drawing, or if we can't do the |
| 805 | // premul on the GPU |
| 806 | if (premul && (!tempProxy || !premulOnGpu)) { |
| 807 | size_t tmpRowBytes = 4 * width; |
| 808 | tmpPixels.reset(width * height); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 809 | if (!sw_convert_to_premul(srcColorType, width, height, rowBytes, buffer, tmpRowBytes, |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 810 | tmpPixels.get())) { |
| 811 | return false; |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 812 | } |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 813 | rowBytes = tmpRowBytes; |
| 814 | buffer = tmpPixels.get(); |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 815 | } |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 816 | |
| 817 | if (tempProxy) { |
Brian Osman | 2240be9 | 2017-10-18 13:15:13 -0400 | [diff] [blame] | 818 | auto fp = GrSimpleTextureEffect::Make(tempProxy, SkMatrix::I()); |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 819 | if (premulOnGpu) { |
| 820 | fp = fContext->createUPMToPMEffect(std::move(fp), useConfigConversionEffect); |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 821 | } |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 822 | fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle); |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame] | 823 | if (!fp) { |
| 824 | return false; |
| 825 | } |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 826 | |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 827 | if (!tempProxy->instantiate(this->resourceProvider())) { |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 828 | return false; |
| 829 | } |
Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 830 | GrTexture* texture = tempProxy->priv().peekTexture(); |
Robert Phillips | 7bbbf62 | 2017-10-17 07:36:59 -0400 | [diff] [blame] | 831 | |
| 832 | if (tempProxy->priv().hasPendingIO()) { |
| 833 | this->flush(tempProxy.get()); |
| 834 | } |
| 835 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 836 | if (!fContext->fGpu->writePixels(texture, tempProxy->origin(), 0, 0, width, height, |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 837 | tempDrawInfo.fWriteColorType, buffer, rowBytes)) { |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 838 | return false; |
| 839 | } |
Robert Phillips | 7bbbf62 | 2017-10-17 07:36:59 -0400 | [diff] [blame] | 840 | tempProxy = nullptr; |
| 841 | |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 842 | SkMatrix matrix; |
| 843 | matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top)); |
Robert Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 844 | GrRenderTargetContext* renderTargetContext = dst->asRenderTargetContext(); |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 845 | if (!renderTargetContext) { |
| 846 | return false; |
| 847 | } |
| 848 | GrPaint paint; |
| 849 | paint.addColorFragmentProcessor(std::move(fp)); |
| 850 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 851 | paint.setAllowSRGBInputs(dst->colorSpaceInfo().isGammaCorrect() || |
| 852 | GrPixelConfigIsSRGB(dst->colorSpaceInfo().config())); |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 853 | SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)); |
| 854 | renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, matrix, rect, |
| 855 | nullptr); |
| 856 | |
| 857 | if (kFlushWrites_PixelOp & pixelOpsFlags) { |
| 858 | this->flushSurfaceWrites(renderTargetContext->asRenderTargetProxy()); |
| 859 | } |
| 860 | } else { |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 861 | return fContext->fGpu->writePixels(dstSurface, dstProxy->origin(), left, top, width, height, |
| 862 | srcColorType, buffer, rowBytes); |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 863 | } |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 864 | return true; |
| 865 | } |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 866 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 867 | bool GrContextPriv::readSurfacePixels(GrSurfaceContext* src, int left, int top, int width, |
| 868 | int height, GrColorType dstColorType, |
| 869 | SkColorSpace* dstColorSpace, void* buffer, size_t rowBytes, |
| 870 | uint32_t flags) { |
Brian Osman | b62ea22 | 2016-12-22 11:12:16 -0500 | [diff] [blame] | 871 | // TODO: Color space conversion |
| 872 | |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 873 | ASSERT_SINGLE_OWNER_PRIV |
| 874 | RETURN_FALSE_IF_ABANDONED_PRIV |
Robert Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 875 | SkASSERT(src); |
| 876 | ASSERT_OWNED_PROXY_PRIV(src->asSurfaceProxy()); |
Brian Salomon | dcbb9d9 | 2017-07-19 10:53:20 -0400 | [diff] [blame] | 877 | GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "readSurfacePixels", fContext); |
bsalomon | 32ab260 | 2015-09-09 18:57:49 -0700 | [diff] [blame] | 878 | |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 879 | // MDB TODO: delay this instantiation until later in the method |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 880 | if (!src->asSurfaceProxy()->instantiate(this->resourceProvider())) { |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 881 | return false; |
| 882 | } |
| 883 | |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 884 | GrSurfaceProxy* srcProxy = src->asSurfaceProxy(); |
| 885 | GrSurface* srcSurface = srcProxy->priv().peekSurface(); |
Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 886 | |
Brian Osman | d2ca59a | 2017-04-13 14:03:57 -0400 | [diff] [blame] | 887 | // The src is premul but the dst is unpremul -> unpremul the src after or as part of the read |
| 888 | bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 889 | |
| 890 | if (!valid_pixel_conversion(dstColorType, srcProxy->config(), unpremul)) { |
Brian Osman | d2ca59a | 2017-04-13 14:03:57 -0400 | [diff] [blame] | 891 | return false; |
| 892 | } |
| 893 | |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 894 | // We need to guarantee round-trip conversion if we are reading and writing 8888 non-sRGB data, |
| 895 | // without any color spaces attached, and the caller wants us to unpremul. |
Robert Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 896 | bool useConfigConversionEffect = |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 897 | unpremul && |
| 898 | pm_upm_must_round_trip(srcProxy->config(), src->colorSpaceInfo().colorSpace()) && |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 899 | pm_upm_must_round_trip(dstColorType, dstColorSpace); |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 900 | |
| 901 | // Are we going to try to unpremul as part of a draw? For the non-legacy case, we always allow |
| 902 | // this. GrConfigConversionEffect fails on some GPUs, so only allow this if it works perfectly. |
| 903 | bool unpremulOnGpu = unpremul && |
| 904 | (!useConfigConversionEffect || fContext->validPMUPMConversionExists()); |
bsalomon | 6c6f658 | 2015-09-10 08:12:46 -0700 | [diff] [blame] | 905 | |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 906 | // Adjust the params so that if we wind up using an intermediate surface we've already done |
| 907 | // all the trimming and the temporary can be the min size required. |
Robert Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 908 | if (!GrSurfacePriv::AdjustReadPixelParams(srcSurface->width(), srcSurface->height(), |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 909 | GrColorTypeBytesPerPixel(dstColorType), &left, &top, |
| 910 | &width, &height, &buffer, &rowBytes)) { |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 911 | return false; |
| 912 | } |
| 913 | |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 914 | GrGpu::DrawPreference drawPreference = unpremulOnGpu ? GrGpu::kCallerPrefersDraw_DrawPreference |
| 915 | : GrGpu::kNoDraw_DrawPreference; |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 916 | GrGpu::ReadPixelTempDrawInfo tempDrawInfo; |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 917 | GrSRGBConversion srgbConversion = determine_read_pixels_srgb_conversion( |
| 918 | GrPixelConfigIsSRGBEncoded(srcProxy->config()), src->colorSpaceInfo().colorSpace(), |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 919 | dstColorType, dstColorSpace, *fContext->caps()); |
| 920 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 921 | if (!fContext->fGpu->getReadPixelsInfo(srcSurface, srcProxy->origin(), width, height, rowBytes, |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 922 | dstColorType, srgbConversion, &drawPreference, |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 923 | &tempDrawInfo)) { |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 924 | return false; |
| 925 | } |
bsalomon | 191bcc0 | 2014-11-14 11:31:13 -0800 | [diff] [blame] | 926 | |
Robert Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 927 | if (!(kDontFlush_PixelOpsFlag & flags) && srcSurface->surfacePriv().hasPendingWrite()) { |
Brian Osman | d2ca59a | 2017-04-13 14:03:57 -0400 | [diff] [blame] | 928 | this->flush(nullptr); // MDB TODO: tighten this |
| 929 | } |
| 930 | |
Robert Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 931 | sk_sp<GrSurfaceProxy> proxyToRead = src->asSurfaceProxyRef(); |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 932 | bool didTempDraw = false; |
| 933 | if (GrGpu::kNoDraw_DrawPreference != drawPreference) { |
bsalomon | b117ff1 | 2016-07-19 07:24:40 -0700 | [diff] [blame] | 934 | if (SkBackingFit::kExact == tempDrawInfo.fTempSurfaceFit) { |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 935 | // We only respect this when the entire src is being read. Otherwise we can trigger too |
| 936 | // many odd ball texture sizes and trash the cache. |
Robert Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 937 | if (width != srcSurface->width() || height != srcSurface->height()) { |
bsalomon | b117ff1 | 2016-07-19 07:24:40 -0700 | [diff] [blame] | 938 | tempDrawInfo.fTempSurfaceFit= SkBackingFit::kApprox; |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 939 | } |
bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 940 | } |
brianosman | dfe4f2e | 2016-07-21 13:28:36 -0700 | [diff] [blame] | 941 | // TODO: Need to decide the semantics of this function for color spaces. Do we support |
| 942 | // conversion to a passed-in color space? For now, specifying nullptr means that this |
Brian Salomon | 366093f | 2018-02-13 09:25:22 -0500 | [diff] [blame] | 943 | // path will do no conversion, so it will match the behavior of the non-draw path. For |
| 944 | // now we simply infer an sRGB color space if the config is sRGB in order to avoid an |
| 945 | // illegal combination. |
| 946 | sk_sp<SkColorSpace> colorSpace; |
| 947 | if (GrPixelConfigIsSRGB(tempDrawInfo.fTempSurfaceDesc.fConfig)) { |
| 948 | colorSpace = SkColorSpace::MakeSRGB(); |
| 949 | } |
| 950 | sk_sp<GrRenderTargetContext> tempRTC = |
| 951 | fContext->makeDeferredRenderTargetContext(tempDrawInfo.fTempSurfaceFit, |
| 952 | tempDrawInfo.fTempSurfaceDesc.fWidth, |
| 953 | tempDrawInfo.fTempSurfaceDesc.fHeight, |
| 954 | tempDrawInfo.fTempSurfaceDesc.fConfig, |
| 955 | std::move(colorSpace), |
| 956 | tempDrawInfo.fTempSurfaceDesc.fSampleCnt, |
| 957 | GrMipMapped::kNo, |
| 958 | tempDrawInfo.fTempSurfaceDesc.fOrigin); |
Brian Osman | 693a540 | 2016-10-27 15:13:22 -0400 | [diff] [blame] | 959 | if (tempRTC) { |
Greg Daniel | f44cb48 | 2018-02-27 14:26:32 -0500 | [diff] [blame] | 960 | // Adding discard to appease vulkan validation warning about loading uninitialized data |
| 961 | // on draw |
| 962 | tempRTC->discard(); |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 963 | SkMatrix textureMatrix = SkMatrix::MakeTrans(SkIntToScalar(left), SkIntToScalar(top)); |
Robert Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 964 | sk_sp<GrTextureProxy> proxy = src->asTextureProxyRef(); |
Brian Osman | 2240be9 | 2017-10-18 13:15:13 -0400 | [diff] [blame] | 965 | auto fp = GrSimpleTextureEffect::Make(std::move(proxy), textureMatrix); |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 966 | if (unpremulOnGpu) { |
| 967 | fp = fContext->createPMToUPMEffect(std::move(fp), useConfigConversionEffect); |
| 968 | // We no longer need to do this on CPU after the read back. |
| 969 | unpremul = false; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 970 | } |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 971 | fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle); |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame] | 972 | if (!fp) { |
| 973 | return false; |
| 974 | } |
Brian Osman | 60cd57e | 2017-04-06 10:19:06 -0400 | [diff] [blame] | 975 | |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 976 | GrPaint paint; |
| 977 | paint.addColorFragmentProcessor(std::move(fp)); |
| 978 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
| 979 | paint.setAllowSRGBInputs(true); |
| 980 | SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)); |
| 981 | tempRTC->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect, |
| 982 | nullptr); |
| 983 | proxyToRead = tempRTC->asTextureProxyRef(); |
| 984 | left = 0; |
| 985 | top = 0; |
| 986 | didTempDraw = true; |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 987 | } |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 988 | } |
joshualitt | 5c55fef | 2014-10-31 14:04:35 -0700 | [diff] [blame] | 989 | |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 990 | if (!proxyToRead) { |
| 991 | return false; |
| 992 | } |
| 993 | |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 994 | if (GrGpu::kRequireDraw_DrawPreference == drawPreference && !didTempDraw) { |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 995 | return false; |
| 996 | } |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 997 | GrColorType colorTypeToRead = dstColorType; |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 998 | if (didTempDraw) { |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 999 | this->flushSurfaceWrites(proxyToRead.get()); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 1000 | colorTypeToRead = tempDrawInfo.fReadColorType; |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 1001 | } |
Robert Phillips | 09dfc47 | 2017-09-13 15:25:47 -0400 | [diff] [blame] | 1002 | |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 1003 | if (!proxyToRead->instantiate(this->resourceProvider())) { |
Robert Phillips | 09dfc47 | 2017-09-13 15:25:47 -0400 | [diff] [blame] | 1004 | return false; |
| 1005 | } |
| 1006 | |
| 1007 | GrSurface* surfaceToRead = proxyToRead->priv().peekSurface(); |
| 1008 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 1009 | if (!fContext->fGpu->readPixels(surfaceToRead, proxyToRead->origin(), left, top, width, height, |
| 1010 | colorTypeToRead, buffer, rowBytes)) { |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 1011 | return false; |
| 1012 | } |
| 1013 | |
| 1014 | // Perform umpremul conversion if we weren't able to perform it as a draw. |
| 1015 | if (unpremul) { |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 1016 | SkColorType colorType = GrColorTypeToSkColorType(dstColorType); |
| 1017 | if (kUnknown_SkColorType == colorType || 4 != SkColorTypeBytesPerPixel(colorType)) { |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 1018 | return false; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1019 | } |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 1020 | |
Matt Sarett | c7b2908 | 2017-02-09 16:22:39 -0500 | [diff] [blame] | 1021 | for (int y = 0; y < height; y++) { |
| 1022 | SkUnpremultiplyRow<false>((uint32_t*) buffer, (const uint32_t*) buffer, width); |
| 1023 | buffer = SkTAddOffset<void>(buffer, rowBytes); |
| 1024 | } |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1025 | } |
| 1026 | return true; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1027 | } |
| 1028 | |
Brian Salomon | 5f33a8c | 2018-02-26 14:32:39 -0500 | [diff] [blame] | 1029 | bool GrContextPriv::writeSurfacePixels2(GrSurfaceContext* dst, int left, int top, int width, |
| 1030 | int height, GrColorType srcColorType, |
| 1031 | SkColorSpace* srcColorSpace, const void* buffer, |
| 1032 | size_t rowBytes, uint32_t pixelOpsFlags) { |
| 1033 | ASSERT_SINGLE_OWNER_PRIV |
| 1034 | RETURN_FALSE_IF_ABANDONED_PRIV |
| 1035 | SkASSERT(dst); |
| 1036 | SkASSERT(buffer); |
| 1037 | ASSERT_OWNED_PROXY_PRIV(dst->asSurfaceProxy()); |
| 1038 | GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "writeSurfacePixels2", fContext); |
| 1039 | |
| 1040 | if (GrColorType::kUnknown == srcColorType) { |
| 1041 | return false; |
| 1042 | } |
| 1043 | |
| 1044 | if (!dst->asSurfaceProxy()->instantiate(this->resourceProvider())) { |
| 1045 | return false; |
| 1046 | } |
| 1047 | |
| 1048 | GrSurfaceProxy* dstProxy = dst->asSurfaceProxy(); |
| 1049 | GrSurface* dstSurface = dstProxy->priv().peekSurface(); |
| 1050 | |
| 1051 | if (!GrSurfacePriv::AdjustWritePixelParams(dstSurface->width(), dstSurface->height(), |
| 1052 | GrColorTypeBytesPerPixel(srcColorType), &left, &top, |
| 1053 | &width, &height, &buffer, &rowBytes)) { |
| 1054 | return false; |
| 1055 | } |
| 1056 | |
Brian Salomon | 3d86a19 | 2018-02-27 16:46:11 -0500 | [diff] [blame] | 1057 | if (!fContext->caps()->surfaceSupportsWritePixels(dstSurface)) { |
Brian Salomon | 5f33a8c | 2018-02-26 14:32:39 -0500 | [diff] [blame] | 1058 | GrSurfaceDesc desc; |
| 1059 | desc.fConfig = dstProxy->config(); |
| 1060 | desc.fWidth = width; |
| 1061 | desc.fHeight = height; |
| 1062 | desc.fSampleCnt = 1; |
| 1063 | desc.fOrigin = kTopLeft_GrSurfaceOrigin; |
| 1064 | auto tempProxy = |
| 1065 | this->proxyProvider()->createProxy(desc, SkBackingFit::kApprox, SkBudgeted::kYes); |
| 1066 | if (!tempProxy) { |
| 1067 | return false; |
| 1068 | } |
| 1069 | auto tempCtx = this->drawingManager()->makeTextureContext( |
| 1070 | tempProxy, dst->colorSpaceInfo().refColorSpace()); |
| 1071 | if (!tempCtx) { |
| 1072 | return false; |
| 1073 | } |
| 1074 | if (!this->writeSurfacePixels2(tempCtx.get(), 0, 0, width, height, srcColorType, |
| 1075 | srcColorSpace, buffer, rowBytes, pixelOpsFlags)) { |
| 1076 | return false; |
| 1077 | } |
Brian Salomon | 3d86a19 | 2018-02-27 16:46:11 -0500 | [diff] [blame] | 1078 | return dst->copy(tempProxy.get(), SkIRect::MakeWH(width, height), {left, top}); |
Brian Salomon | 5f33a8c | 2018-02-26 14:32:39 -0500 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | // TODO: Make GrSurfaceContext know its alpha type and pass src buffer's alpha type. |
| 1082 | bool premul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags); |
| 1083 | bool convert = premul; |
| 1084 | |
| 1085 | if (!valid_pixel_conversion(srcColorType, dstProxy->config(), premul)) { |
| 1086 | return false; |
| 1087 | } |
| 1088 | |
| 1089 | GrColorType allowedColorType = |
| 1090 | fContext->caps()->supportedWritePixelsColorType(dstProxy->config(), srcColorType); |
| 1091 | convert = convert || (srcColorType != allowedColorType); |
| 1092 | |
| 1093 | if (!dst->colorSpaceInfo().colorSpace()) { |
| 1094 | // "Legacy" mode - no color space conversions. |
| 1095 | srcColorSpace = nullptr; |
| 1096 | } |
| 1097 | convert = convert || !SkColorSpace::Equals(srcColorSpace, dst->colorSpaceInfo().colorSpace()); |
| 1098 | |
| 1099 | std::unique_ptr<char[]> tempBuffer; |
| 1100 | if (convert) { |
| 1101 | auto srcSkColorType = GrColorTypeToSkColorType(srcColorType); |
| 1102 | auto dstSkColorType = GrColorTypeToSkColorType(allowedColorType); |
| 1103 | if (kUnknown_SkColorType == srcSkColorType || kUnknown_SkColorType == dstSkColorType) { |
| 1104 | return false; |
| 1105 | } |
| 1106 | auto srcAlphaType = premul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType; |
| 1107 | SkPixmap src(SkImageInfo::Make(width, height, srcSkColorType, srcAlphaType, |
| 1108 | sk_ref_sp(srcColorSpace)), |
| 1109 | buffer, rowBytes); |
| 1110 | auto tempSrcII = SkImageInfo::Make(width, height, dstSkColorType, kPremul_SkAlphaType, |
| 1111 | dst->colorSpaceInfo().refColorSpace()); |
| 1112 | auto size = tempSrcII.computeMinByteSize(); |
| 1113 | if (!size) { |
| 1114 | return false; |
| 1115 | } |
| 1116 | tempBuffer.reset(new char[size]); |
| 1117 | SkPixmap tempSrc(tempSrcII, tempBuffer.get(), tempSrcII.minRowBytes()); |
| 1118 | if (!src.readPixels(tempSrc)) { |
| 1119 | return false; |
| 1120 | } |
| 1121 | srcColorType = allowedColorType; |
| 1122 | buffer = tempSrc.addr(); |
| 1123 | rowBytes = tempSrc.rowBytes(); |
| 1124 | if (dstProxy->origin() == kBottomLeft_GrSurfaceOrigin) { |
| 1125 | std::unique_ptr<char[]> row(new char[rowBytes]); |
| 1126 | for (int y = 0; y < height / 2; ++y) { |
| 1127 | memcpy(row.get(), tempSrc.addr(0, y), rowBytes); |
| 1128 | memcpy(tempSrc.writable_addr(0, y), tempSrc.addr(0, height - 1 - y), rowBytes); |
| 1129 | memcpy(tempSrc.writable_addr(0, height - 1 - y), row.get(), rowBytes); |
| 1130 | } |
| 1131 | top = dstProxy->height() - top - height; |
| 1132 | } |
| 1133 | } else if (dstProxy->origin() == kBottomLeft_GrSurfaceOrigin) { |
| 1134 | size_t trimRowBytes = GrColorTypeBytesPerPixel(srcColorType) * width; |
| 1135 | tempBuffer.reset(new char[trimRowBytes * height]); |
| 1136 | char* dst = reinterpret_cast<char*>(tempBuffer.get()) + trimRowBytes * (height - 1); |
| 1137 | const char* src = reinterpret_cast<const char*>(buffer); |
| 1138 | for (int i = 0; i < height; ++i, src += rowBytes, dst -= trimRowBytes) { |
| 1139 | memcpy(dst, src, trimRowBytes); |
| 1140 | } |
| 1141 | buffer = tempBuffer.get(); |
| 1142 | rowBytes = trimRowBytes; |
| 1143 | top = dstProxy->height() - top - height; |
| 1144 | } |
| 1145 | |
| 1146 | if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && dstSurface->surfacePriv().hasPendingIO()) { |
| 1147 | this->flush(nullptr); // MDB TODO: tighten this |
| 1148 | } |
| 1149 | |
| 1150 | return this->getGpu()->writePixels(dstSurface, left, top, width, height, srcColorType, buffer, |
| 1151 | rowBytes); |
| 1152 | } |
| 1153 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 1154 | void GrContextPriv::prepareSurfaceForExternalIO(GrSurfaceProxy* proxy) { |
| 1155 | ASSERT_SINGLE_OWNER_PRIV |
| 1156 | RETURN_IF_ABANDONED_PRIV |
| 1157 | SkASSERT(proxy); |
| 1158 | ASSERT_OWNED_PROXY_PRIV(proxy); |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 1159 | fContext->fDrawingManager->prepareSurfaceForExternalIO(proxy, 0, nullptr); |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 1162 | void GrContextPriv::flushSurfaceWrites(GrSurfaceProxy* proxy) { |
| 1163 | ASSERT_SINGLE_OWNER_PRIV |
| 1164 | RETURN_IF_ABANDONED_PRIV |
| 1165 | SkASSERT(proxy); |
| 1166 | ASSERT_OWNED_PROXY_PRIV(proxy); |
| 1167 | if (proxy->priv().hasPendingWrite()) { |
| 1168 | this->flush(proxy); |
bsalomon | f80bfed | 2014-10-07 05:56:02 -0700 | [diff] [blame] | 1169 | } |
| 1170 | } |
| 1171 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 1172 | void GrContextPriv::flushSurfaceIO(GrSurfaceProxy* proxy) { |
| 1173 | ASSERT_SINGLE_OWNER_PRIV |
| 1174 | RETURN_IF_ABANDONED_PRIV |
| 1175 | SkASSERT(proxy); |
| 1176 | ASSERT_OWNED_PROXY_PRIV(proxy); |
| 1177 | if (proxy->priv().hasPendingIO()) { |
| 1178 | this->flush(proxy); |
ajuma | 95243eb | 2016-08-24 08:19:02 -0700 | [diff] [blame] | 1179 | } |
| 1180 | } |
| 1181 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1182 | //////////////////////////////////////////////////////////////////////////////// |
commit-bot@chromium.org | b471a32 | 2014-03-10 07:40:03 +0000 | [diff] [blame] | 1183 | |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 1184 | sk_sp<GrSurfaceContext> GrContextPriv::makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy> proxy, |
Robert Phillips | d5f9cdd | 2018-01-31 09:29:48 -0500 | [diff] [blame] | 1185 | sk_sp<SkColorSpace> colorSpace, |
| 1186 | const SkSurfaceProps* props) { |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 1187 | ASSERT_SINGLE_OWNER_PRIV |
| 1188 | |
Brian Salomon | 366093f | 2018-02-13 09:25:22 -0500 | [diff] [blame] | 1189 | // sRGB pixel configs may only be used with near-sRGB gamma color spaces. |
| 1190 | if (GrPixelConfigIsSRGB(proxy->config())) { |
| 1191 | if (!colorSpace || !colorSpace->gammaCloseToSRGB()) { |
| 1192 | return nullptr; |
| 1193 | } |
| 1194 | } |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 1195 | if (proxy->asRenderTargetProxy()) { |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 1196 | return this->drawingManager()->makeRenderTargetContext(std::move(proxy), |
Robert Phillips | d5f9cdd | 2018-01-31 09:29:48 -0500 | [diff] [blame] | 1197 | std::move(colorSpace), props); |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 1198 | } else { |
| 1199 | SkASSERT(proxy->asTextureProxy()); |
Robert Phillips | d5f9cdd | 2018-01-31 09:29:48 -0500 | [diff] [blame] | 1200 | SkASSERT(!props); |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 1201 | return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace)); |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 1202 | } |
| 1203 | } |
| 1204 | |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 1205 | sk_sp<GrSurfaceContext> GrContextPriv::makeDeferredSurfaceContext(const GrSurfaceDesc& dstDesc, |
Greg Daniel | 65c7f66 | 2017-10-30 13:39:09 -0400 | [diff] [blame] | 1206 | GrMipMapped mipMapped, |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 1207 | SkBackingFit fit, |
Brian Salomon | 366093f | 2018-02-13 09:25:22 -0500 | [diff] [blame] | 1208 | SkBudgeted isDstBudgeted, |
| 1209 | sk_sp<SkColorSpace> colorSpace, |
| 1210 | const SkSurfaceProps* props) { |
Greg Daniel | 65c7f66 | 2017-10-30 13:39:09 -0400 | [diff] [blame] | 1211 | sk_sp<GrTextureProxy> proxy; |
| 1212 | if (GrMipMapped::kNo == mipMapped) { |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 1213 | proxy = this->proxyProvider()->createProxy(dstDesc, fit, isDstBudgeted); |
Greg Daniel | 65c7f66 | 2017-10-30 13:39:09 -0400 | [diff] [blame] | 1214 | } else { |
| 1215 | SkASSERT(SkBackingFit::kExact == fit); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 1216 | proxy = this->proxyProvider()->createMipMapProxy(dstDesc, isDstBudgeted); |
Greg Daniel | 65c7f66 | 2017-10-30 13:39:09 -0400 | [diff] [blame] | 1217 | } |
Robert Phillips | 77b3f32 | 2017-01-31 18:24:12 -0500 | [diff] [blame] | 1218 | if (!proxy) { |
| 1219 | return nullptr; |
| 1220 | } |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 1221 | |
Brian Salomon | 366093f | 2018-02-13 09:25:22 -0500 | [diff] [blame] | 1222 | return this->makeWrappedSurfaceContext(std::move(proxy), std::move(colorSpace), props); |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 1223 | } |
| 1224 | |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 1225 | sk_sp<GrTextureContext> GrContextPriv::makeBackendTextureContext(const GrBackendTexture& tex, |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 1226 | GrSurfaceOrigin origin, |
Brian Osman | c1e3705 | 2017-03-09 14:19:20 -0500 | [diff] [blame] | 1227 | sk_sp<SkColorSpace> colorSpace) { |
Robert Phillips | 26caf89 | 2017-01-27 10:58:31 -0500 | [diff] [blame] | 1228 | ASSERT_SINGLE_OWNER_PRIV |
| 1229 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 1230 | sk_sp<GrSurfaceProxy> proxy = this->proxyProvider()->createWrappedTextureProxy(tex, origin); |
Robert Phillips | 77b3f32 | 2017-01-31 18:24:12 -0500 | [diff] [blame] | 1231 | if (!proxy) { |
| 1232 | return nullptr; |
| 1233 | } |
Robert Phillips | 26caf89 | 2017-01-27 10:58:31 -0500 | [diff] [blame] | 1234 | |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 1235 | return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace)); |
Robert Phillips | 26caf89 | 2017-01-27 10:58:31 -0500 | [diff] [blame] | 1236 | } |
| 1237 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 1238 | sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureRenderTargetContext( |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 1239 | const GrBackendTexture& tex, |
| 1240 | GrSurfaceOrigin origin, |
| 1241 | int sampleCnt, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 1242 | sk_sp<SkColorSpace> colorSpace, |
Brian Osman | c1e3705 | 2017-03-09 14:19:20 -0500 | [diff] [blame] | 1243 | const SkSurfaceProps* props) { |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 1244 | ASSERT_SINGLE_OWNER_PRIV |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1245 | SkASSERT(sampleCnt > 0); |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 1246 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 1247 | sk_sp<GrTextureProxy> proxy(this->proxyProvider()->createWrappedTextureProxy(tex, origin, |
| 1248 | sampleCnt)); |
Robert Phillips | 77b3f32 | 2017-01-31 18:24:12 -0500 | [diff] [blame] | 1249 | if (!proxy) { |
| 1250 | return nullptr; |
| 1251 | } |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 1252 | |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 1253 | return this->drawingManager()->makeRenderTargetContext(std::move(proxy), |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 1254 | std::move(colorSpace), props); |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 1255 | } |
| 1256 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 1257 | sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendRenderTargetRenderTargetContext( |
Greg Daniel | bcf612b | 2017-05-01 13:50:58 +0000 | [diff] [blame] | 1258 | const GrBackendRenderTarget& backendRT, |
| 1259 | GrSurfaceOrigin origin, |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 1260 | sk_sp<SkColorSpace> colorSpace, |
| 1261 | const SkSurfaceProps* surfaceProps) { |
| 1262 | ASSERT_SINGLE_OWNER_PRIV |
| 1263 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 1264 | sk_sp<GrSurfaceProxy> proxy = this->proxyProvider()->createWrappedRenderTargetProxy(backendRT, |
| 1265 | origin); |
Robert Phillips | 77b3f32 | 2017-01-31 18:24:12 -0500 | [diff] [blame] | 1266 | if (!proxy) { |
| 1267 | return nullptr; |
| 1268 | } |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 1269 | |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 1270 | return this->drawingManager()->makeRenderTargetContext(std::move(proxy), |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 1271 | std::move(colorSpace), |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 1272 | surfaceProps); |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 1273 | } |
| 1274 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 1275 | sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureAsRenderTargetRenderTargetContext( |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 1276 | const GrBackendTexture& tex, |
| 1277 | GrSurfaceOrigin origin, |
| 1278 | int sampleCnt, |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 1279 | sk_sp<SkColorSpace> colorSpace, |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 1280 | const SkSurfaceProps* props) { |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 1281 | ASSERT_SINGLE_OWNER_PRIV |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1282 | SkASSERT(sampleCnt > 0); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 1283 | sk_sp<GrSurfaceProxy> proxy(this->proxyProvider()->createWrappedRenderTargetProxy(tex, origin, |
| 1284 | sampleCnt)); |
Robert Phillips | 77b3f32 | 2017-01-31 18:24:12 -0500 | [diff] [blame] | 1285 | if (!proxy) { |
| 1286 | return nullptr; |
| 1287 | } |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 1288 | |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 1289 | return this->drawingManager()->makeRenderTargetContext(std::move(proxy), |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 1290 | std::move(colorSpace), |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 1291 | props); |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 1292 | } |
| 1293 | |
Chris Dalton | fe199b7 | 2017-05-05 11:26:15 -0400 | [diff] [blame] | 1294 | void GrContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) { |
| 1295 | fContext->fDrawingManager->addOnFlushCallbackObject(onFlushCBObject); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 1296 | } |
| 1297 | |
Robert Phillips | 6200036 | 2018-02-01 09:10:04 -0500 | [diff] [blame] | 1298 | void GrContextPriv::moveOpListsToDDL(SkDeferredDisplayList* ddl) { |
| 1299 | fContext->fDrawingManager->moveOpListsToDDL(ddl); |
| 1300 | } |
| 1301 | |
| 1302 | void GrContextPriv::copyOpListsFromDDL(const SkDeferredDisplayList* ddl, |
| 1303 | GrRenderTargetProxy* newDest) { |
| 1304 | fContext->fDrawingManager->copyOpListsFromDDL(ddl, newDest); |
| 1305 | } |
| 1306 | |
robertphillips | 48fde9c | 2016-09-06 05:20:20 -0700 | [diff] [blame] | 1307 | static inline GrPixelConfig GrPixelConfigFallback(GrPixelConfig config) { |
Brian Osman | 78f20e0 | 2017-01-12 10:28:01 -0500 | [diff] [blame] | 1308 | switch (config) { |
| 1309 | case kAlpha_8_GrPixelConfig: |
| 1310 | case kRGB_565_GrPixelConfig: |
| 1311 | case kRGBA_4444_GrPixelConfig: |
| 1312 | case kBGRA_8888_GrPixelConfig: |
| 1313 | return kRGBA_8888_GrPixelConfig; |
| 1314 | case kSBGRA_8888_GrPixelConfig: |
| 1315 | return kSRGBA_8888_GrPixelConfig; |
| 1316 | case kAlpha_half_GrPixelConfig: |
| 1317 | return kRGBA_half_GrPixelConfig; |
| 1318 | default: |
| 1319 | return kUnknown_GrPixelConfig; |
| 1320 | } |
robertphillips | 48fde9c | 2016-09-06 05:20:20 -0700 | [diff] [blame] | 1321 | } |
| 1322 | |
robertphillips | d728f0c | 2016-11-21 11:05:03 -0800 | [diff] [blame] | 1323 | sk_sp<GrRenderTargetContext> GrContext::makeDeferredRenderTargetContextWithFallback( |
| 1324 | SkBackingFit fit, |
| 1325 | int width, int height, |
| 1326 | GrPixelConfig config, |
| 1327 | sk_sp<SkColorSpace> colorSpace, |
| 1328 | int sampleCnt, |
Greg Daniel | 45d6303 | 2017-10-30 13:41:26 -0400 | [diff] [blame] | 1329 | GrMipMapped mipMapped, |
robertphillips | d728f0c | 2016-11-21 11:05:03 -0800 | [diff] [blame] | 1330 | GrSurfaceOrigin origin, |
| 1331 | const SkSurfaceProps* surfaceProps, |
| 1332 | SkBudgeted budgeted) { |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1333 | SkASSERT(sampleCnt > 0); |
| 1334 | if (0 == this->caps()->getRenderTargetSampleCount(sampleCnt, config)) { |
robertphillips | d728f0c | 2016-11-21 11:05:03 -0800 | [diff] [blame] | 1335 | config = GrPixelConfigFallback(config); |
| 1336 | } |
| 1337 | |
| 1338 | return this->makeDeferredRenderTargetContext(fit, width, height, config, std::move(colorSpace), |
Greg Daniel | 45d6303 | 2017-10-30 13:41:26 -0400 | [diff] [blame] | 1339 | sampleCnt, mipMapped, origin, surfaceProps, |
Greg Daniel | e1da1d9 | 2017-10-06 15:59:27 -0400 | [diff] [blame] | 1340 | budgeted); |
robertphillips | d728f0c | 2016-11-21 11:05:03 -0800 | [diff] [blame] | 1341 | } |
| 1342 | |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 1343 | sk_sp<GrRenderTargetContext> GrContext::makeDeferredRenderTargetContext( |
| 1344 | SkBackingFit fit, |
| 1345 | int width, int height, |
| 1346 | GrPixelConfig config, |
| 1347 | sk_sp<SkColorSpace> colorSpace, |
| 1348 | int sampleCnt, |
Greg Daniel | 45d6303 | 2017-10-30 13:41:26 -0400 | [diff] [blame] | 1349 | GrMipMapped mipMapped, |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 1350 | GrSurfaceOrigin origin, |
| 1351 | const SkSurfaceProps* surfaceProps, |
| 1352 | SkBudgeted budgeted) { |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1353 | SkASSERT(sampleCnt > 0); |
Brian Salomon | 79e4d1b | 2017-07-13 11:17:11 -0400 | [diff] [blame] | 1354 | if (this->abandoned()) { |
| 1355 | return nullptr; |
| 1356 | } |
| 1357 | |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 1358 | GrSurfaceDesc desc; |
| 1359 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 1360 | desc.fOrigin = origin; |
| 1361 | desc.fWidth = width; |
| 1362 | desc.fHeight = height; |
| 1363 | desc.fConfig = config; |
| 1364 | desc.fSampleCnt = sampleCnt; |
| 1365 | |
Greg Daniel | e1da1d9 | 2017-10-06 15:59:27 -0400 | [diff] [blame] | 1366 | sk_sp<GrTextureProxy> rtp; |
Greg Daniel | 45d6303 | 2017-10-30 13:41:26 -0400 | [diff] [blame] | 1367 | if (GrMipMapped::kNo == mipMapped) { |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 1368 | rtp = fProxyProvider->createProxy(desc, fit, budgeted); |
Greg Daniel | e1da1d9 | 2017-10-06 15:59:27 -0400 | [diff] [blame] | 1369 | } else { |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 1370 | rtp = fProxyProvider->createMipMapProxy(desc, budgeted); |
Greg Daniel | e1da1d9 | 2017-10-06 15:59:27 -0400 | [diff] [blame] | 1371 | } |
Robert Phillips | 08c5ec7 | 2017-01-30 12:26:47 -0500 | [diff] [blame] | 1372 | if (!rtp) { |
| 1373 | return nullptr; |
| 1374 | } |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 1375 | |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame] | 1376 | sk_sp<GrRenderTargetContext> renderTargetContext( |
| 1377 | fDrawingManager->makeRenderTargetContext(std::move(rtp), |
| 1378 | std::move(colorSpace), |
| 1379 | surfaceProps)); |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame] | 1380 | if (!renderTargetContext) { |
| 1381 | return nullptr; |
| 1382 | } |
| 1383 | |
| 1384 | renderTargetContext->discard(); |
| 1385 | |
| 1386 | return renderTargetContext; |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 1387 | } |
| 1388 | |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 1389 | bool GrContext::abandoned() const { |
| 1390 | ASSERT_SINGLE_OWNER |
robertphillips | 7761d61 | 2016-05-16 09:14:53 -0700 | [diff] [blame] | 1391 | return fDrawingManager->wasAbandoned(); |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 1392 | } |
| 1393 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 1394 | std::unique_ptr<GrFragmentProcessor> GrContext::createPMToUPMEffect( |
| 1395 | std::unique_ptr<GrFragmentProcessor> fp, bool useConfigConversionEffect) { |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame] | 1396 | ASSERT_SINGLE_OWNER |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 1397 | // We have specialized effects that guarantee round-trip conversion for some formats |
| 1398 | if (useConfigConversionEffect) { |
| 1399 | // We should have already called this->validPMUPMConversionExists() in this case |
| 1400 | SkASSERT(fDidTestPMConversions); |
| 1401 | // ...and it should have succeeded |
| 1402 | SkASSERT(this->validPMUPMConversionExists()); |
| 1403 | |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 1404 | return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToUnpremul); |
Brian Osman | 2d2da4f | 2017-04-12 17:07:22 -0400 | [diff] [blame] | 1405 | } else { |
| 1406 | // For everything else (sRGB, half-float, etc...), it doesn't make sense to try and |
| 1407 | // explicitly round the results. Just do the obvious, naive thing in the shader. |
| 1408 | return GrFragmentProcessor::UnpremulOutput(std::move(fp)); |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame] | 1409 | } |
| 1410 | } |
| 1411 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 1412 | std::unique_ptr<GrFragmentProcessor> GrContext::createUPMToPMEffect( |
| 1413 | std::unique_ptr<GrFragmentProcessor> fp, bool useConfigConversionEffect) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 1414 | ASSERT_SINGLE_OWNER |
Brian Osman | 2d2da4f | 2017-04-12 17:07:22 -0400 | [diff] [blame] | 1415 | // We have specialized effects that guarantee round-trip conversion for these formats |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 1416 | if (useConfigConversionEffect) { |
| 1417 | // We should have already called this->validPMUPMConversionExists() in this case |
| 1418 | SkASSERT(fDidTestPMConversions); |
| 1419 | // ...and it should have succeeded |
| 1420 | SkASSERT(this->validPMUPMConversionExists()); |
| 1421 | |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 1422 | return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToPremul); |
Brian Osman | 2d2da4f | 2017-04-12 17:07:22 -0400 | [diff] [blame] | 1423 | } else { |
| 1424 | // For everything else (sRGB, half-float, etc...), it doesn't make sense to try and |
| 1425 | // explicitly round the results. Just do the obvious, naive thing in the shader. |
| 1426 | return GrFragmentProcessor::PremulOutput(std::move(fp)); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1427 | } |
| 1428 | } |
| 1429 | |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 1430 | bool GrContext::validPMUPMConversionExists() { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 1431 | ASSERT_SINGLE_OWNER |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 1432 | if (!fDidTestPMConversions) { |
Brian Osman | 28804f3 | 2017-04-20 10:24:36 -0400 | [diff] [blame] | 1433 | fPMUPMConversionsRoundTrip = GrConfigConversionEffect::TestForPreservingPMConversions(this); |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 1434 | fDidTestPMConversions = true; |
| 1435 | } |
| 1436 | |
bsalomon | 636e802 | 2015-07-29 06:08:46 -0700 | [diff] [blame] | 1437 | // 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] | 1438 | return fPMUPMConversionsRoundTrip; |
bsalomon | 636e802 | 2015-07-29 06:08:46 -0700 | [diff] [blame] | 1439 | } |
| 1440 | |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 1441 | ////////////////////////////////////////////////////////////////////////////// |
| 1442 | |
Robert Phillips | fc711a2 | 2018-02-13 17:03:00 -0500 | [diff] [blame] | 1443 | // DDL TODO: remove 'maxResources' |
Robert Phillips | 8d1e67e | 2017-12-04 13:48:14 -0500 | [diff] [blame] | 1444 | void GrContext::getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 1445 | ASSERT_SINGLE_OWNER |
Robert Phillips | 8d1e67e | 2017-12-04 13:48:14 -0500 | [diff] [blame] | 1446 | if (maxResources) { |
| 1447 | *maxResources = fResourceCache->getMaxResourceCount(); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 1448 | } |
Robert Phillips | 8d1e67e | 2017-12-04 13:48:14 -0500 | [diff] [blame] | 1449 | if (maxResourceBytes) { |
| 1450 | *maxResourceBytes = fResourceCache->getMaxResourceBytes(); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 1451 | } |
| 1452 | } |
| 1453 | |
Robert Phillips | 8d1e67e | 2017-12-04 13:48:14 -0500 | [diff] [blame] | 1454 | void GrContext::setResourceCacheLimits(int maxResources, size_t maxResourceBytes) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 1455 | ASSERT_SINGLE_OWNER |
Robert Phillips | 8d1e67e | 2017-12-04 13:48:14 -0500 | [diff] [blame] | 1456 | fResourceCache->setLimits(maxResources, maxResourceBytes); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 1457 | } |
| 1458 | |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 1459 | ////////////////////////////////////////////////////////////////////////////// |
| 1460 | |
| 1461 | void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 1462 | ASSERT_SINGLE_OWNER |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 1463 | fResourceCache->dumpMemoryStatistics(traceMemoryDump); |
| 1464 | } |
Brian Osman | 71a1889 | 2017-08-10 10:23:25 -0400 | [diff] [blame] | 1465 | |
| 1466 | ////////////////////////////////////////////////////////////////////////////// |
| 1467 | |
| 1468 | SkString GrContext::dump() const { |
| 1469 | SkDynamicMemoryWStream stream; |
| 1470 | SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty); |
| 1471 | writer.beginObject(); |
| 1472 | |
| 1473 | static const char* kBackendStr[] = { |
| 1474 | "Metal", |
| 1475 | "OpenGL", |
| 1476 | "Vulkan", |
| 1477 | "Mock", |
| 1478 | }; |
| 1479 | GR_STATIC_ASSERT(0 == kMetal_GrBackend); |
| 1480 | GR_STATIC_ASSERT(1 == kOpenGL_GrBackend); |
| 1481 | GR_STATIC_ASSERT(2 == kVulkan_GrBackend); |
| 1482 | GR_STATIC_ASSERT(3 == kMock_GrBackend); |
| 1483 | writer.appendString("backend", kBackendStr[fBackend]); |
| 1484 | |
| 1485 | writer.appendName("caps"); |
| 1486 | fCaps->dumpJSON(&writer); |
| 1487 | |
| 1488 | writer.appendName("gpu"); |
| 1489 | fGpu->dumpJSON(&writer); |
| 1490 | |
| 1491 | // Flush JSON to the memory stream |
| 1492 | writer.endObject(); |
| 1493 | writer.flush(); |
| 1494 | |
| 1495 | // Null terminate the JSON data in the memory stream |
| 1496 | stream.write8(0); |
| 1497 | |
| 1498 | // Allocate a string big enough to hold all the data, then copy out of the stream |
| 1499 | SkString result(stream.bytesWritten()); |
| 1500 | stream.copyToAndReset(result.writable_str()); |
| 1501 | return result; |
| 1502 | } |