reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2010 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. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 8 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 9 | #include "GrGpu.h" |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 10 | |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 11 | #include "GrBackendSurface.h" |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 12 | #include "GrBuffer.h" |
bsalomon | eb1cb5c | 2015-05-22 08:01:09 -0700 | [diff] [blame] | 13 | #include "GrCaps.h" |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 14 | #include "GrContext.h" |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 15 | #include "GrGpuResourcePriv.h" |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 16 | #include "GrMesh.h" |
kkinnunen | cabe20c | 2015-06-01 01:37:26 -0700 | [diff] [blame] | 17 | #include "GrPathRendering.h" |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 18 | #include "GrPipeline.h" |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 19 | #include "GrResourceCache.h" |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 20 | #include "GrResourceProvider.h" |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 21 | #include "GrRenderTargetPriv.h" |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 22 | #include "GrStencilAttachment.h" |
csmartdalton | c633abb | 2016-11-01 08:55:55 -0700 | [diff] [blame] | 23 | #include "GrStencilSettings.h" |
egdaniel | 6d901da | 2015-07-30 12:02:15 -0700 | [diff] [blame] | 24 | #include "GrSurfacePriv.h" |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 25 | #include "GrTexturePriv.h" |
halcanary | 4dbbd04 | 2016-06-07 17:21:10 -0700 | [diff] [blame] | 26 | #include "SkMathPriv.h" |
bsalomon | cb8979d | 2015-05-05 09:51:38 -0700 | [diff] [blame] | 27 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 28 | //////////////////////////////////////////////////////////////////////////////// |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 29 | |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 30 | GrGpu::GrGpu(GrContext* context) |
joshualitt | 3322fa4 | 2014-11-07 08:48:51 -0800 | [diff] [blame] | 31 | : fResetTimestamp(kExpiredTimestamp+1) |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 32 | , fResetBits(kAll_GrBackendState) |
joshualitt | 3322fa4 | 2014-11-07 08:48:51 -0800 | [diff] [blame] | 33 | , fContext(context) { |
csmartdalton | 0d28e57 | 2016-07-06 09:59:43 -0700 | [diff] [blame] | 34 | fMultisampleSpecs.emplace_back(0, 0, nullptr); // Index 0 is an invalid unique id. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 35 | } |
| 36 | |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 37 | GrGpu::~GrGpu() {} |
bsalomon | 1d89ddc | 2014-08-19 14:20:58 -0700 | [diff] [blame] | 38 | |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 39 | void GrGpu::disconnect(DisconnectType) {} |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 40 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 41 | //////////////////////////////////////////////////////////////////////////////// |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 42 | |
Robert Phillips | 81444fb | 2017-03-21 09:14:35 -0400 | [diff] [blame] | 43 | bool GrGpu::isACopyNeededForTextureParams(int width, int height, |
| 44 | const GrSamplerParams& textureParams, |
| 45 | GrTextureProducer::CopyParams* copyParams, |
| 46 | SkScalar scaleAdjust[2]) const { |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 47 | const GrCaps& caps = *this->caps(); |
| 48 | if (textureParams.isTiled() && !caps.npotTextureTileSupport() && |
| 49 | (!SkIsPow2(width) || !SkIsPow2(height))) { |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 50 | SkASSERT(scaleAdjust); |
bsalomon | 100b8f8 | 2015-10-28 08:37:44 -0700 | [diff] [blame] | 51 | copyParams->fWidth = GrNextPow2(width); |
| 52 | copyParams->fHeight = GrNextPow2(height); |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 53 | scaleAdjust[0] = ((SkScalar) copyParams->fWidth) / width; |
| 54 | scaleAdjust[1] = ((SkScalar) copyParams->fHeight) / height; |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 55 | switch (textureParams.filterMode()) { |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 56 | case GrSamplerParams::kNone_FilterMode: |
| 57 | copyParams->fFilter = GrSamplerParams::kNone_FilterMode; |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 58 | break; |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 59 | case GrSamplerParams::kBilerp_FilterMode: |
| 60 | case GrSamplerParams::kMipMap_FilterMode: |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 61 | // We are only ever scaling up so no reason to ever indicate kMipMap. |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 62 | copyParams->fFilter = GrSamplerParams::kBilerp_FilterMode; |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 63 | break; |
| 64 | } |
bsalomon | 100b8f8 | 2015-10-28 08:37:44 -0700 | [diff] [blame] | 65 | return true; |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 66 | } |
bsalomon | 100b8f8 | 2015-10-28 08:37:44 -0700 | [diff] [blame] | 67 | return false; |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 68 | } |
| 69 | |
egdaniel | cf614fd | 2015-04-22 13:58:58 -0700 | [diff] [blame] | 70 | static GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) { |
egdaniel | b0e1be2 | 2015-04-22 13:27:39 -0700 | [diff] [blame] | 71 | // By default, GrRenderTargets are GL's normal orientation so that they |
| 72 | // can be drawn to by the outside world without the client having |
| 73 | // to render upside down. |
| 74 | if (kDefault_GrSurfaceOrigin == origin) { |
| 75 | return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin; |
| 76 | } else { |
| 77 | return origin; |
| 78 | } |
| 79 | } |
| 80 | |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 81 | /** |
| 82 | * Prior to creating a texture, make sure the type of texture being created is |
| 83 | * supported by calling check_texture_creation_params. |
| 84 | * |
| 85 | * @param caps The capabilities of the GL device. |
| 86 | * @param desc The descriptor of the texture to create. |
| 87 | * @param isRT Indicates if the texture can be a render target. |
| 88 | */ |
| 89 | static bool check_texture_creation_params(const GrCaps& caps, const GrSurfaceDesc& desc, |
bsalomon | e699d0c | 2016-03-09 06:25:15 -0800 | [diff] [blame] | 90 | bool* isRT, const SkTArray<GrMipLevel>& texels) { |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 91 | if (!caps.isConfigTexturable(desc.fConfig)) { |
| 92 | return false; |
robertphillips@google.com | d3eb336 | 2012-10-31 13:56:35 +0000 | [diff] [blame] | 93 | } |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 94 | |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 95 | if (GrPixelConfigIsSint(desc.fConfig) && texels.count() > 1) { |
| 96 | return false; |
| 97 | } |
| 98 | |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 99 | *isRT = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag); |
| 100 | if (*isRT && !caps.isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) { |
| 101 | return false; |
commit-bot@chromium.org | 6b7938f | 2013-10-15 14:18:16 +0000 | [diff] [blame] | 102 | } |
robertphillips@google.com | d3eb336 | 2012-10-31 13:56:35 +0000 | [diff] [blame] | 103 | |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 104 | // We currently do not support multisampled textures |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 105 | if (!*isRT && desc.fSampleCnt > 0) { |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | if (*isRT) { |
| 110 | int maxRTSize = caps.maxRenderTargetSize(); |
| 111 | if (desc.fWidth > maxRTSize || desc.fHeight > maxRTSize) { |
| 112 | return false; |
| 113 | } |
| 114 | } else { |
| 115 | int maxSize = caps.maxTextureSize(); |
| 116 | if (desc.fWidth > maxSize || desc.fHeight > maxSize) { |
| 117 | return false; |
| 118 | } |
| 119 | } |
bsalomon | e699d0c | 2016-03-09 06:25:15 -0800 | [diff] [blame] | 120 | |
| 121 | for (int i = 0; i < texels.count(); ++i) { |
| 122 | if (!texels[i].fPixels) { |
| 123 | return false; |
| 124 | } |
| 125 | } |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 126 | return true; |
| 127 | } |
| 128 | |
| 129 | GrTexture* GrGpu::createTexture(const GrSurfaceDesc& origDesc, SkBudgeted budgeted, |
| 130 | const SkTArray<GrMipLevel>& texels) { |
| 131 | GrSurfaceDesc desc = origDesc; |
| 132 | |
| 133 | const GrCaps* caps = this->caps(); |
| 134 | bool isRT = false; |
bsalomon | e699d0c | 2016-03-09 06:25:15 -0800 | [diff] [blame] | 135 | bool textureCreationParamsValid = check_texture_creation_params(*caps, desc, &isRT, texels); |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 136 | if (!textureCreationParamsValid) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 137 | return nullptr; |
egdaniel | 8c9b6f1 | 2015-05-12 13:36:30 -0700 | [diff] [blame] | 138 | } |
| 139 | |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 140 | desc.fSampleCnt = SkTMin(desc.fSampleCnt, caps->maxSampleCount()); |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 141 | // Attempt to catch un- or wrongly initialized sample counts. |
egdaniel | b0e1be2 | 2015-04-22 13:27:39 -0700 | [diff] [blame] | 142 | SkASSERT(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64); |
| 143 | |
| 144 | desc.fOrigin = resolve_origin(desc.fOrigin, isRT); |
| 145 | |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 146 | if (texels.count() && (desc.fFlags & kPerformInitialClear_GrSurfaceFlag)) { |
| 147 | return nullptr; |
| 148 | } |
| 149 | |
Robert Phillips | 92de631 | 2017-05-23 07:43:48 -0400 | [diff] [blame] | 150 | this->handleDirtyContext(); |
| 151 | GrTexture* tex = this->onCreateTexture(desc, budgeted, texels); |
bsalomon | b12ea41 | 2015-02-02 21:19:50 -0800 | [diff] [blame] | 152 | if (tex) { |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 153 | if (!caps->reuseScratchTextures() && !isRT) { |
| 154 | tex->resourcePriv().removeScratchKey(); |
| 155 | } |
bsalomon | b12ea41 | 2015-02-02 21:19:50 -0800 | [diff] [blame] | 156 | fStats.incTextureCreates(); |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 157 | if (!texels.empty()) { |
| 158 | if (texels[0].fPixels) { |
| 159 | fStats.incTextureUploads(); |
| 160 | } |
bsalomon | b12ea41 | 2015-02-02 21:19:50 -0800 | [diff] [blame] | 161 | } |
| 162 | } |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 163 | return tex; |
| 164 | } |
| 165 | |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 166 | sk_sp<GrTexture> GrGpu::wrapBackendTexture(const GrBackendTexture& backendTex, |
| 167 | GrSurfaceOrigin origin, |
| 168 | GrBackendTextureFlags flags, |
| 169 | int sampleCnt, |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 170 | GrWrapOwnership ownership) { |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 171 | this->handleDirtyContext(); |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 172 | if (!this->caps()->isConfigTexturable(backendTex.config())) { |
bsalomon | 5b30c6f | 2015-12-17 14:17:34 -0800 | [diff] [blame] | 173 | return nullptr; |
| 174 | } |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 175 | if ((flags & kRenderTarget_GrBackendTextureFlag) && |
| 176 | !this->caps()->isConfigRenderable(backendTex.config(), sampleCnt > 0)) { |
bsalomon | 5b30c6f | 2015-12-17 14:17:34 -0800 | [diff] [blame] | 177 | return nullptr; |
| 178 | } |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 179 | int maxSize = this->caps()->maxTextureSize(); |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 180 | if (backendTex.width() > maxSize || backendTex.height() > maxSize) { |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 181 | return nullptr; |
| 182 | } |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 183 | sk_sp<GrTexture> tex = this->onWrapBackendTexture(backendTex, origin, flags, sampleCnt, |
| 184 | ownership); |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 185 | if (!tex) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 186 | return nullptr; |
bsalomon@google.com | a14dd6d | 2012-01-03 21:08:12 +0000 | [diff] [blame] | 187 | } |
Eric Karl | 5c77975 | 2017-05-08 12:02:07 -0700 | [diff] [blame] | 188 | |
| 189 | if (!this->caps()->avoidStencilBuffers()) { |
| 190 | // TODO: defer this and attach dynamically |
| 191 | GrRenderTarget* tgt = tex->asRenderTarget(); |
| 192 | if (tgt && !fContext->resourceProvider()->attachStencilAttachment(tgt)) { |
| 193 | return nullptr; |
| 194 | } |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 195 | } |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 196 | return tex; |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Greg Daniel | bcf612b | 2017-05-01 13:50:58 +0000 | [diff] [blame] | 199 | sk_sp<GrRenderTarget> GrGpu::wrapBackendRenderTarget(const GrBackendRenderTarget& backendRT, |
| 200 | GrSurfaceOrigin origin) { |
| 201 | if (!this->caps()->isConfigRenderable(backendRT.config(), backendRT.sampleCnt() > 0)) { |
bsalomon | 5b30c6f | 2015-12-17 14:17:34 -0800 | [diff] [blame] | 202 | return nullptr; |
| 203 | } |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 204 | this->handleDirtyContext(); |
Greg Daniel | bcf612b | 2017-05-01 13:50:58 +0000 | [diff] [blame] | 205 | return this->onWrapBackendRenderTarget(backendRT, origin); |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 208 | sk_sp<GrRenderTarget> GrGpu::wrapBackendTextureAsRenderTarget(const GrBackendTexture& tex, |
| 209 | GrSurfaceOrigin origin, |
| 210 | int sampleCnt) { |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 211 | this->handleDirtyContext(); |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 212 | if (!this->caps()->isConfigRenderable(tex.config(), sampleCnt > 0)) { |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 213 | return nullptr; |
| 214 | } |
| 215 | int maxSize = this->caps()->maxTextureSize(); |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 216 | if (tex.width() > maxSize || tex.height() > maxSize) { |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 217 | return nullptr; |
| 218 | } |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 219 | return this->onWrapBackendTextureAsRenderTarget(tex, origin, sampleCnt); |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 220 | } |
| 221 | |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 222 | GrBuffer* GrGpu::createBuffer(size_t size, GrBufferType intendedType, |
cdalton | 1bf3e71 | 2016-04-19 10:00:02 -0700 | [diff] [blame] | 223 | GrAccessPattern accessPattern, const void* data) { |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 224 | this->handleDirtyContext(); |
cdalton | 1bf3e71 | 2016-04-19 10:00:02 -0700 | [diff] [blame] | 225 | GrBuffer* buffer = this->onCreateBuffer(size, intendedType, accessPattern, data); |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 226 | if (!this->caps()->reuseScratchBuffers()) { |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 227 | buffer->resourcePriv().removeScratchKey(); |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 228 | } |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 229 | return buffer; |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 230 | } |
| 231 | |
Robert Phillips | e3302df | 2017-04-24 07:31:02 -0400 | [diff] [blame] | 232 | std::unique_ptr<gr_instanced::OpAllocator> GrGpu::createInstancedRenderingAllocator() { |
| 233 | SkASSERT(GrCaps::InstancedSupport::kNone != this->caps()->instancedSupport()); |
| 234 | return this->onCreateInstancedRenderingAllocator(); |
| 235 | } |
| 236 | |
csmartdalton | e0d3629 | 2016-07-29 08:14:20 -0700 | [diff] [blame] | 237 | gr_instanced::InstancedRendering* GrGpu::createInstancedRendering() { |
| 238 | SkASSERT(GrCaps::InstancedSupport::kNone != this->caps()->instancedSupport()); |
| 239 | return this->onCreateInstancedRendering(); |
| 240 | } |
| 241 | |
joshualitt | 1cbdcde | 2015-08-21 11:53:29 -0700 | [diff] [blame] | 242 | bool GrGpu::copySurface(GrSurface* dst, |
| 243 | GrSurface* src, |
| 244 | const SkIRect& srcRect, |
| 245 | const SkIPoint& dstPoint) { |
| 246 | SkASSERT(dst && src); |
| 247 | this->handleDirtyContext(); |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 248 | // We don't allow conversion between integer configs and float/fixed configs. |
| 249 | if (GrPixelConfigIsSint(dst->config()) != GrPixelConfigIsSint(src->config())) { |
| 250 | return false; |
| 251 | } |
joshualitt | 1cbdcde | 2015-08-21 11:53:29 -0700 | [diff] [blame] | 252 | return this->onCopySurface(dst, src, srcRect, dstPoint); |
| 253 | } |
| 254 | |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 255 | bool GrGpu::getReadPixelsInfo(GrSurface* srcSurface, int width, int height, size_t rowBytes, |
| 256 | GrPixelConfig readConfig, DrawPreference* drawPreference, |
| 257 | ReadPixelTempDrawInfo* tempDrawInfo) { |
| 258 | SkASSERT(drawPreference); |
| 259 | SkASSERT(tempDrawInfo); |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 260 | SkASSERT(srcSurface); |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 261 | SkASSERT(kGpuPrefersDraw_DrawPreference != *drawPreference); |
| 262 | |
egdaniel | 3fe0327 | 2016-08-15 10:59:17 -0700 | [diff] [blame] | 263 | // We currently do not support reading into the packed formats 565 or 4444 as they are not |
| 264 | // required to have read back support on all devices and backends. |
| 265 | if (kRGB_565_GrPixelConfig == readConfig || kRGBA_4444_GrPixelConfig == readConfig) { |
| 266 | return false; |
| 267 | } |
| 268 | |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 269 | if (!this->onGetReadPixelsInfo(srcSurface, width, height, rowBytes, readConfig, drawPreference, |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 270 | tempDrawInfo)) { |
| 271 | return false; |
| 272 | } |
| 273 | |
| 274 | // Check to see if we're going to request that the caller draw when drawing is not possible. |
| 275 | if (!srcSurface->asTexture() || |
| 276 | !this->caps()->isConfigRenderable(tempDrawInfo->fTempSurfaceDesc.fConfig, false)) { |
| 277 | // If we don't have a fallback to a straight read then fail. |
| 278 | if (kRequireDraw_DrawPreference == *drawPreference) { |
| 279 | return false; |
| 280 | } |
| 281 | *drawPreference = kNoDraw_DrawPreference; |
| 282 | } |
| 283 | |
| 284 | return true; |
| 285 | } |
cblume | ed82800 | 2016-02-16 13:00:01 -0800 | [diff] [blame] | 286 | bool GrGpu::getWritePixelsInfo(GrSurface* dstSurface, int width, int height, |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 287 | GrPixelConfig srcConfig, DrawPreference* drawPreference, |
| 288 | WritePixelTempDrawInfo* tempDrawInfo) { |
| 289 | SkASSERT(drawPreference); |
| 290 | SkASSERT(tempDrawInfo); |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 291 | SkASSERT(dstSurface); |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 292 | SkASSERT(kGpuPrefersDraw_DrawPreference != *drawPreference); |
| 293 | |
bsalomon | babafcc | 2016-02-16 11:36:47 -0800 | [diff] [blame] | 294 | if (SkToBool(dstSurface->asRenderTarget())) { |
| 295 | if (this->caps()->useDrawInsteadOfAllRenderTargetWrites()) { |
| 296 | ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference); |
| 297 | } else if (this->caps()->useDrawInsteadOfPartialRenderTargetWrite() && |
| 298 | (width < dstSurface->width() || height < dstSurface->height())) { |
| 299 | ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference); |
| 300 | } |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 301 | } |
| 302 | |
cblume | ed82800 | 2016-02-16 13:00:01 -0800 | [diff] [blame] | 303 | if (!this->onGetWritePixelsInfo(dstSurface, width, height, srcConfig, drawPreference, |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 304 | tempDrawInfo)) { |
| 305 | return false; |
| 306 | } |
| 307 | |
| 308 | // Check to see if we're going to request that the caller draw when drawing is not possible. |
| 309 | if (!dstSurface->asRenderTarget() || |
| 310 | !this->caps()->isConfigTexturable(tempDrawInfo->fTempSurfaceDesc.fConfig)) { |
| 311 | // If we don't have a fallback to a straight upload then fail. |
| 312 | if (kRequireDraw_DrawPreference == *drawPreference || |
| 313 | !this->caps()->isConfigTexturable(srcConfig)) { |
| 314 | return false; |
| 315 | } |
| 316 | *drawPreference = kNoDraw_DrawPreference; |
| 317 | } |
| 318 | return true; |
| 319 | } |
| 320 | |
bsalomon | 6cb3cbe | 2015-07-30 07:34:27 -0700 | [diff] [blame] | 321 | bool GrGpu::readPixels(GrSurface* surface, |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 322 | int left, int top, int width, int height, |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 323 | GrPixelConfig config, void* buffer, |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 324 | size_t rowBytes) { |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 325 | SkASSERT(surface); |
| 326 | |
| 327 | // We don't allow conversion between integer configs and float/fixed configs. |
| 328 | if (GrPixelConfigIsSint(surface->config()) != GrPixelConfigIsSint(config)) { |
| 329 | return false; |
| 330 | } |
egdaniel | 6d901da | 2015-07-30 12:02:15 -0700 | [diff] [blame] | 331 | |
egdaniel | 6d901da | 2015-07-30 12:02:15 -0700 | [diff] [blame] | 332 | size_t bpp = GrBytesPerPixel(config); |
| 333 | if (!GrSurfacePriv::AdjustReadPixelParams(surface->width(), surface->height(), bpp, |
| 334 | &left, &top, &width, &height, |
| 335 | &buffer, |
| 336 | &rowBytes)) { |
| 337 | return false; |
| 338 | } |
| 339 | |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 340 | this->handleDirtyContext(); |
| 341 | |
egdaniel | 6d901da | 2015-07-30 12:02:15 -0700 | [diff] [blame] | 342 | return this->onReadPixels(surface, |
| 343 | left, top, width, height, |
| 344 | config, buffer, |
| 345 | rowBytes); |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 346 | } |
| 347 | |
bsalomon | 6cb3cbe | 2015-07-30 07:34:27 -0700 | [diff] [blame] | 348 | bool GrGpu::writePixels(GrSurface* surface, |
| 349 | int left, int top, int width, int height, |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 350 | GrPixelConfig config, const SkTArray<GrMipLevel>& texels) { |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 351 | SkASSERT(surface); |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 352 | |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 353 | for (int currentMipLevel = 0; currentMipLevel < texels.count(); currentMipLevel++) { |
bsalomon | e699d0c | 2016-03-09 06:25:15 -0800 | [diff] [blame] | 354 | if (!texels[currentMipLevel].fPixels ) { |
| 355 | return false; |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 356 | } |
| 357 | } |
jvanverth | 2dc2994 | 2015-09-01 07:16:46 -0700 | [diff] [blame] | 358 | |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 359 | // We don't allow conversion between integer configs and float/fixed configs. |
| 360 | if (GrPixelConfigIsSint(surface->config()) != GrPixelConfigIsSint(config)) { |
| 361 | return false; |
| 362 | } |
| 363 | |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 364 | this->handleDirtyContext(); |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 365 | if (this->onWritePixels(surface, left, top, width, height, config, texels)) { |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 366 | SkIRect rect = SkIRect::MakeXYWH(left, top, width, height); |
| 367 | this->didWriteToSurface(surface, &rect, texels.count()); |
bsalomon | b12ea41 | 2015-02-02 21:19:50 -0800 | [diff] [blame] | 368 | fStats.incTextureUploads(); |
| 369 | return true; |
| 370 | } |
| 371 | return false; |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 372 | } |
| 373 | |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 374 | bool GrGpu::writePixels(GrSurface* surface, |
| 375 | int left, int top, int width, int height, |
| 376 | GrPixelConfig config, const void* buffer, |
| 377 | size_t rowBytes) { |
| 378 | GrMipLevel mipLevel; |
| 379 | mipLevel.fPixels = buffer; |
| 380 | mipLevel.fRowBytes = rowBytes; |
| 381 | SkSTArray<1, GrMipLevel> texels; |
| 382 | texels.push_back(mipLevel); |
| 383 | |
| 384 | return this->writePixels(surface, left, top, width, height, config, texels); |
| 385 | } |
| 386 | |
jvanverth | c3d706f | 2016-04-20 10:33:27 -0700 | [diff] [blame] | 387 | bool GrGpu::transferPixels(GrSurface* surface, |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 388 | int left, int top, int width, int height, |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 389 | GrPixelConfig config, GrBuffer* transferBuffer, |
jvanverth | 84741b3 | 2016-09-30 08:39:02 -0700 | [diff] [blame] | 390 | size_t offset, size_t rowBytes, GrFence* fence) { |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 391 | SkASSERT(transferBuffer); |
jvanverth | 84741b3 | 2016-09-30 08:39:02 -0700 | [diff] [blame] | 392 | SkASSERT(fence); |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 393 | |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 394 | // We don't allow conversion between integer configs and float/fixed configs. |
| 395 | if (GrPixelConfigIsSint(surface->config()) != GrPixelConfigIsSint(config)) { |
| 396 | return false; |
| 397 | } |
| 398 | |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 399 | this->handleDirtyContext(); |
jvanverth | c3d706f | 2016-04-20 10:33:27 -0700 | [diff] [blame] | 400 | if (this->onTransferPixels(surface, left, top, width, height, config, |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 401 | transferBuffer, offset, rowBytes)) { |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 402 | SkIRect rect = SkIRect::MakeXYWH(left, top, width, height); |
| 403 | this->didWriteToSurface(surface, &rect); |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 404 | fStats.incTransfersToTexture(); |
jvanverth | 84741b3 | 2016-09-30 08:39:02 -0700 | [diff] [blame] | 405 | |
| 406 | if (*fence) { |
| 407 | this->deleteFence(*fence); |
| 408 | } |
| 409 | *fence = this->insertFence(); |
| 410 | |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 411 | return true; |
| 412 | } |
| 413 | return false; |
| 414 | } |
| 415 | |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 416 | void GrGpu::resolveRenderTarget(GrRenderTarget* target) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 417 | SkASSERT(target); |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 418 | this->handleDirtyContext(); |
| 419 | this->onResolveRenderTarget(target); |
| 420 | } |
| 421 | |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 422 | void GrGpu::didWriteToSurface(GrSurface* surface, const SkIRect* bounds, uint32_t mipLevels) const { |
| 423 | SkASSERT(surface); |
| 424 | // Mark any MIP chain and resolve buffer as dirty if and only if there is a non-empty bounds. |
| 425 | if (nullptr == bounds || !bounds->isEmpty()) { |
| 426 | if (GrRenderTarget* target = surface->asRenderTarget()) { |
| 427 | target->flagAsNeedingResolve(bounds); |
| 428 | } |
| 429 | GrTexture* texture = surface->asTexture(); |
| 430 | if (texture && 1 == mipLevels) { |
| 431 | texture->texturePriv().dirtyMipMaps(true); |
| 432 | } |
| 433 | } |
| 434 | } |
| 435 | |
csmartdalton | c633abb | 2016-11-01 08:55:55 -0700 | [diff] [blame] | 436 | const GrGpu::MultisampleSpecs& GrGpu::queryMultisampleSpecs(const GrPipeline& pipeline) { |
| 437 | GrRenderTarget* rt = pipeline.getRenderTarget(); |
Brian Salomon | 154ce91 | 2017-05-17 10:40:02 -0400 | [diff] [blame] | 438 | SkASSERT(rt->numStencilSamples() > 1); |
csmartdalton | 0d28e57 | 2016-07-06 09:59:43 -0700 | [diff] [blame] | 439 | |
csmartdalton | c633abb | 2016-11-01 08:55:55 -0700 | [diff] [blame] | 440 | GrStencilSettings stencil; |
| 441 | if (pipeline.isStencilEnabled()) { |
| 442 | // TODO: attach stencil and create settings during render target flush. |
| 443 | SkASSERT(rt->renderTargetPriv().getStencilAttachment()); |
| 444 | stencil.reset(*pipeline.getUserStencil(), pipeline.hasStencilClip(), |
| 445 | rt->renderTargetPriv().numStencilBits()); |
| 446 | } |
| 447 | |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 448 | int effectiveSampleCnt; |
csmartdalton | 0d28e57 | 2016-07-06 09:59:43 -0700 | [diff] [blame] | 449 | SkSTArray<16, SkPoint, true> pattern; |
csmartdalton | c25c5d7 | 2016-11-01 07:03:59 -0700 | [diff] [blame] | 450 | this->onQueryMultisampleSpecs(rt, stencil, &effectiveSampleCnt, &pattern); |
Brian Salomon | 154ce91 | 2017-05-17 10:40:02 -0400 | [diff] [blame] | 451 | SkASSERT(effectiveSampleCnt >= rt->numStencilSamples()); |
csmartdalton | 0d28e57 | 2016-07-06 09:59:43 -0700 | [diff] [blame] | 452 | |
| 453 | uint8_t id; |
| 454 | if (this->caps()->sampleLocationsSupport()) { |
| 455 | SkASSERT(pattern.count() == effectiveSampleCnt); |
| 456 | const auto& insertResult = fMultisampleSpecsIdMap.insert( |
| 457 | MultisampleSpecsIdMap::value_type(pattern, SkTMin(fMultisampleSpecs.count(), 255))); |
| 458 | id = insertResult.first->second; |
| 459 | if (insertResult.second) { |
| 460 | // This means the insert did not find the pattern in the map already, and therefore an |
| 461 | // actual insertion took place. (We don't expect to see many unique sample patterns.) |
| 462 | const SkPoint* sampleLocations = insertResult.first->first.begin(); |
| 463 | SkASSERT(id == fMultisampleSpecs.count()); |
| 464 | fMultisampleSpecs.emplace_back(id, effectiveSampleCnt, sampleLocations); |
| 465 | } |
| 466 | } else { |
| 467 | id = effectiveSampleCnt; |
| 468 | for (int i = fMultisampleSpecs.count(); i <= id; ++i) { |
| 469 | fMultisampleSpecs.emplace_back(i, i, nullptr); |
| 470 | } |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 471 | } |
csmartdalton | 0d28e57 | 2016-07-06 09:59:43 -0700 | [diff] [blame] | 472 | SkASSERT(id > 0); |
csmartdalton | 0d28e57 | 2016-07-06 09:59:43 -0700 | [diff] [blame] | 473 | |
csmartdalton | 0d28e57 | 2016-07-06 09:59:43 -0700 | [diff] [blame] | 474 | return fMultisampleSpecs[id]; |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 475 | } |
| 476 | |
csmartdalton | 0d28e57 | 2016-07-06 09:59:43 -0700 | [diff] [blame] | 477 | bool GrGpu::SamplePatternComparator::operator()(const SamplePattern& a, |
| 478 | const SamplePattern& b) const { |
| 479 | if (a.count() != b.count()) { |
| 480 | return a.count() < b.count(); |
| 481 | } |
| 482 | for (int i = 0; i < a.count(); ++i) { |
| 483 | // This doesn't have geometric meaning. We just need to define an ordering for std::map. |
| 484 | if (a[i].x() != b[i].x()) { |
| 485 | return a[i].x() < b[i].x(); |
| 486 | } |
| 487 | if (a[i].y() != b[i].y()) { |
| 488 | return a[i].y() < b[i].y(); |
| 489 | } |
| 490 | } |
| 491 | return false; // Equal. |
| 492 | } |