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