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