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 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 11 | #include "GrBuffer.h" |
bsalomon | eb1cb5c | 2015-05-22 08:01:09 -0700 | [diff] [blame] | 12 | #include "GrCaps.h" |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 13 | #include "GrContext.h" |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 14 | #include "GrGpuResourcePriv.h" |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 15 | #include "GrMesh.h" |
kkinnunen | cabe20c | 2015-06-01 01:37:26 -0700 | [diff] [blame] | 16 | #include "GrPathRendering.h" |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 17 | #include "GrPipeline.h" |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 18 | #include "GrResourceCache.h" |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 19 | #include "GrResourceProvider.h" |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 20 | #include "GrRenderTargetPriv.h" |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 21 | #include "GrStencilAttachment.h" |
egdaniel | 6d901da | 2015-07-30 12:02:15 -0700 | [diff] [blame] | 22 | #include "GrSurfacePriv.h" |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 23 | #include "SkTypes.h" |
bsalomon | cb8979d | 2015-05-05 09:51:38 -0700 | [diff] [blame] | 24 | |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 25 | GrMesh& GrMesh::operator =(const GrMesh& di) { |
bsalomon | cb8979d | 2015-05-05 09:51:38 -0700 | [diff] [blame] | 26 | fPrimitiveType = di.fPrimitiveType; |
| 27 | fStartVertex = di.fStartVertex; |
| 28 | fStartIndex = di.fStartIndex; |
| 29 | fVertexCount = di.fVertexCount; |
| 30 | fIndexCount = di.fIndexCount; |
| 31 | |
| 32 | fInstanceCount = di.fInstanceCount; |
| 33 | fVerticesPerInstance = di.fVerticesPerInstance; |
| 34 | fIndicesPerInstance = di.fIndicesPerInstance; |
bsalomon | e64eb57 | 2015-05-07 11:35:55 -0700 | [diff] [blame] | 35 | fMaxInstancesPerDraw = di.fMaxInstancesPerDraw; |
bsalomon | cb8979d | 2015-05-05 09:51:38 -0700 | [diff] [blame] | 36 | |
| 37 | fVertexBuffer.reset(di.vertexBuffer()); |
| 38 | fIndexBuffer.reset(di.indexBuffer()); |
| 39 | |
| 40 | return *this; |
| 41 | } |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 42 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 43 | //////////////////////////////////////////////////////////////////////////////// |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 44 | |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 45 | GrGpu::GrGpu(GrContext* context) |
joshualitt | 3322fa4 | 2014-11-07 08:48:51 -0800 | [diff] [blame] | 46 | : fResetTimestamp(kExpiredTimestamp+1) |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 47 | , fResetBits(kAll_GrBackendState) |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 48 | , fMultisampleSpecsAllocator(1) |
joshualitt | 3322fa4 | 2014-11-07 08:48:51 -0800 | [diff] [blame] | 49 | , fContext(context) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 50 | } |
| 51 | |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 52 | GrGpu::~GrGpu() {} |
bsalomon | 1d89ddc | 2014-08-19 14:20:58 -0700 | [diff] [blame] | 53 | |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 54 | void GrGpu::disconnect(DisconnectType) {} |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 55 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 56 | //////////////////////////////////////////////////////////////////////////////// |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 57 | |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 58 | bool GrGpu::makeCopyForTextureParams(int width, int height, const GrTextureParams& textureParams, |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 59 | GrTextureProducer::CopyParams* copyParams) const { |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 60 | const GrCaps& caps = *this->caps(); |
| 61 | if (textureParams.isTiled() && !caps.npotTextureTileSupport() && |
| 62 | (!SkIsPow2(width) || !SkIsPow2(height))) { |
bsalomon | 100b8f8 | 2015-10-28 08:37:44 -0700 | [diff] [blame] | 63 | copyParams->fWidth = GrNextPow2(width); |
| 64 | copyParams->fHeight = GrNextPow2(height); |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 65 | switch (textureParams.filterMode()) { |
| 66 | case GrTextureParams::kNone_FilterMode: |
| 67 | copyParams->fFilter = GrTextureParams::kNone_FilterMode; |
| 68 | break; |
| 69 | case GrTextureParams::kBilerp_FilterMode: |
| 70 | case GrTextureParams::kMipMap_FilterMode: |
| 71 | // We are only ever scaling up so no reason to ever indicate kMipMap. |
| 72 | copyParams->fFilter = GrTextureParams::kBilerp_FilterMode; |
| 73 | break; |
| 74 | } |
bsalomon | 100b8f8 | 2015-10-28 08:37:44 -0700 | [diff] [blame] | 75 | return true; |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 76 | } |
bsalomon | 100b8f8 | 2015-10-28 08:37:44 -0700 | [diff] [blame] | 77 | return false; |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 78 | } |
| 79 | |
egdaniel | cf614fd | 2015-04-22 13:58:58 -0700 | [diff] [blame] | 80 | static GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) { |
egdaniel | b0e1be2 | 2015-04-22 13:27:39 -0700 | [diff] [blame] | 81 | // By default, GrRenderTargets are GL's normal orientation so that they |
| 82 | // can be drawn to by the outside world without the client having |
| 83 | // to render upside down. |
| 84 | if (kDefault_GrSurfaceOrigin == origin) { |
| 85 | return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin; |
| 86 | } else { |
| 87 | return origin; |
| 88 | } |
| 89 | } |
| 90 | |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 91 | /** |
| 92 | * Prior to creating a texture, make sure the type of texture being created is |
| 93 | * supported by calling check_texture_creation_params. |
| 94 | * |
| 95 | * @param caps The capabilities of the GL device. |
| 96 | * @param desc The descriptor of the texture to create. |
| 97 | * @param isRT Indicates if the texture can be a render target. |
| 98 | */ |
| 99 | static bool check_texture_creation_params(const GrCaps& caps, const GrSurfaceDesc& desc, |
bsalomon | e699d0c | 2016-03-09 06:25:15 -0800 | [diff] [blame] | 100 | bool* isRT, const SkTArray<GrMipLevel>& texels) { |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 101 | if (!caps.isConfigTexturable(desc.fConfig)) { |
| 102 | return false; |
robertphillips@google.com | d3eb336 | 2012-10-31 13:56:35 +0000 | [diff] [blame] | 103 | } |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 104 | |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 105 | *isRT = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag); |
| 106 | if (*isRT && !caps.isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) { |
| 107 | return false; |
commit-bot@chromium.org | 6b7938f | 2013-10-15 14:18:16 +0000 | [diff] [blame] | 108 | } |
robertphillips@google.com | d3eb336 | 2012-10-31 13:56:35 +0000 | [diff] [blame] | 109 | |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 110 | // We currently do not support multisampled textures |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 111 | if (!*isRT && desc.fSampleCnt > 0) { |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | if (*isRT) { |
| 116 | int maxRTSize = caps.maxRenderTargetSize(); |
| 117 | if (desc.fWidth > maxRTSize || desc.fHeight > maxRTSize) { |
| 118 | return false; |
| 119 | } |
| 120 | } else { |
| 121 | int maxSize = caps.maxTextureSize(); |
| 122 | if (desc.fWidth > maxSize || desc.fHeight > maxSize) { |
| 123 | return false; |
| 124 | } |
| 125 | } |
bsalomon | e699d0c | 2016-03-09 06:25:15 -0800 | [diff] [blame] | 126 | |
| 127 | for (int i = 0; i < texels.count(); ++i) { |
| 128 | if (!texels[i].fPixels) { |
| 129 | return false; |
| 130 | } |
| 131 | } |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 132 | return true; |
| 133 | } |
| 134 | |
| 135 | GrTexture* GrGpu::createTexture(const GrSurfaceDesc& origDesc, SkBudgeted budgeted, |
| 136 | const SkTArray<GrMipLevel>& texels) { |
| 137 | GrSurfaceDesc desc = origDesc; |
| 138 | |
| 139 | const GrCaps* caps = this->caps(); |
| 140 | bool isRT = false; |
bsalomon | e699d0c | 2016-03-09 06:25:15 -0800 | [diff] [blame] | 141 | bool textureCreationParamsValid = check_texture_creation_params(*caps, desc, &isRT, texels); |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 142 | if (!textureCreationParamsValid) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 143 | return nullptr; |
egdaniel | 8c9b6f1 | 2015-05-12 13:36:30 -0700 | [diff] [blame] | 144 | } |
| 145 | |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 146 | desc.fSampleCnt = SkTMin(desc.fSampleCnt, caps->maxSampleCount()); |
| 147 | // Attempt to catch un- or wrongly intialized sample counts; |
egdaniel | b0e1be2 | 2015-04-22 13:27:39 -0700 | [diff] [blame] | 148 | SkASSERT(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64); |
| 149 | |
| 150 | desc.fOrigin = resolve_origin(desc.fOrigin, isRT); |
| 151 | |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 152 | GrTexture* tex = nullptr; |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 153 | |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 154 | if (GrPixelConfigIsCompressed(desc.fConfig)) { |
| 155 | // We shouldn't be rendering into this |
egdaniel | b0e1be2 | 2015-04-22 13:27:39 -0700 | [diff] [blame] | 156 | SkASSERT(!isRT); |
| 157 | SkASSERT(0 == desc.fSampleCnt); |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 158 | |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 159 | if (!caps->npotTextureTileSupport() && |
tfarina | f9dae78 | 2014-06-06 06:35:28 -0700 | [diff] [blame] | 160 | (!SkIsPow2(desc.fWidth) || !SkIsPow2(desc.fHeight))) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 161 | return nullptr; |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 162 | } |
tfarina | f9dae78 | 2014-06-06 06:35:28 -0700 | [diff] [blame] | 163 | |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 164 | this->handleDirtyContext(); |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame^] | 165 | tex = this->onCreateCompressedTexture(desc, budgeted, texels); |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 166 | } else { |
| 167 | this->handleDirtyContext(); |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame^] | 168 | tex = this->onCreateTexture(desc, budgeted, texels); |
bsalomon | db558dd | 2015-01-23 13:19:00 -0800 | [diff] [blame] | 169 | } |
bsalomon | b12ea41 | 2015-02-02 21:19:50 -0800 | [diff] [blame] | 170 | if (tex) { |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 171 | if (!caps->reuseScratchTextures() && !isRT) { |
| 172 | tex->resourcePriv().removeScratchKey(); |
| 173 | } |
bsalomon | b12ea41 | 2015-02-02 21:19:50 -0800 | [diff] [blame] | 174 | fStats.incTextureCreates(); |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 175 | if (!texels.empty()) { |
| 176 | if (texels[0].fPixels) { |
| 177 | fStats.incTextureUploads(); |
| 178 | } |
bsalomon | b12ea41 | 2015-02-02 21:19:50 -0800 | [diff] [blame] | 179 | } |
| 180 | } |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 181 | return tex; |
| 182 | } |
| 183 | |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 184 | GrTexture* GrGpu::wrapBackendTexture(const GrBackendTextureDesc& desc, GrWrapOwnership ownership) { |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 185 | this->handleDirtyContext(); |
bsalomon | 5b30c6f | 2015-12-17 14:17:34 -0800 | [diff] [blame] | 186 | if (!this->caps()->isConfigTexturable(desc.fConfig)) { |
| 187 | return nullptr; |
| 188 | } |
| 189 | if ((desc.fFlags & kRenderTarget_GrBackendTextureFlag) && |
| 190 | !this->caps()->isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) { |
| 191 | return nullptr; |
| 192 | } |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 193 | int maxSize = this->caps()->maxTextureSize(); |
| 194 | if (desc.fWidth > maxSize || desc.fHeight > maxSize) { |
| 195 | return nullptr; |
| 196 | } |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 197 | GrTexture* tex = this->onWrapBackendTexture(desc, ownership); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 198 | if (nullptr == tex) { |
| 199 | return nullptr; |
bsalomon@google.com | a14dd6d | 2012-01-03 21:08:12 +0000 | [diff] [blame] | 200 | } |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 201 | // TODO: defer this and attach dynamically |
| 202 | GrRenderTarget* tgt = tex->asRenderTarget(); |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 203 | if (tgt && !fContext->resourceProvider()->attachStencilAttachment(tgt)) { |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 204 | tex->unref(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 205 | return nullptr; |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 206 | } else { |
| 207 | return tex; |
| 208 | } |
| 209 | } |
| 210 | |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 211 | GrRenderTarget* GrGpu::wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc, |
| 212 | GrWrapOwnership ownership) { |
bsalomon | 5b30c6f | 2015-12-17 14:17:34 -0800 | [diff] [blame] | 213 | if (!this->caps()->isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) { |
| 214 | return nullptr; |
| 215 | } |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 216 | this->handleDirtyContext(); |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 217 | return this->onWrapBackendRenderTarget(desc, ownership); |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 218 | } |
| 219 | |
kkinnunen | 49c4c22 | 2016-04-01 04:50:37 -0700 | [diff] [blame] | 220 | GrRenderTarget* GrGpu::wrapBackendTextureAsRenderTarget(const GrBackendTextureDesc& desc) { |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 221 | this->handleDirtyContext(); |
| 222 | if (!(desc.fFlags & kRenderTarget_GrBackendTextureFlag)) { |
| 223 | return nullptr; |
| 224 | } |
| 225 | if (!this->caps()->isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) { |
| 226 | return nullptr; |
| 227 | } |
| 228 | int maxSize = this->caps()->maxTextureSize(); |
| 229 | if (desc.fWidth > maxSize || desc.fHeight > maxSize) { |
| 230 | return nullptr; |
| 231 | } |
kkinnunen | 49c4c22 | 2016-04-01 04:50:37 -0700 | [diff] [blame] | 232 | return this->onWrapBackendTextureAsRenderTarget(desc); |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 233 | } |
| 234 | |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 235 | GrBuffer* GrGpu::createBuffer(size_t size, GrBufferType intendedType, |
cdalton | 1bf3e71 | 2016-04-19 10:00:02 -0700 | [diff] [blame] | 236 | GrAccessPattern accessPattern, const void* data) { |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 237 | this->handleDirtyContext(); |
cdalton | 1bf3e71 | 2016-04-19 10:00:02 -0700 | [diff] [blame] | 238 | GrBuffer* buffer = this->onCreateBuffer(size, intendedType, accessPattern, data); |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 239 | if (!this->caps()->reuseScratchBuffers()) { |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 240 | buffer->resourcePriv().removeScratchKey(); |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 241 | } |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 242 | return buffer; |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 243 | } |
| 244 | |
egdaniel | 51c8d40 | 2015-08-06 10:54:13 -0700 | [diff] [blame] | 245 | void GrGpu::clear(const SkIRect& rect, |
joshualitt | 3322fa4 | 2014-11-07 08:48:51 -0800 | [diff] [blame] | 246 | GrColor color, |
joshualitt | 3322fa4 | 2014-11-07 08:48:51 -0800 | [diff] [blame] | 247 | GrRenderTarget* renderTarget) { |
bsalomon | 89c6298 | 2014-11-03 12:08:42 -0800 | [diff] [blame] | 248 | SkASSERT(renderTarget); |
egdaniel | 51c8d40 | 2015-08-06 10:54:13 -0700 | [diff] [blame] | 249 | SkASSERT(SkIRect::MakeWH(renderTarget->width(), renderTarget->height()).contains(rect)); |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 250 | this->handleDirtyContext(); |
egdaniel | 51c8d40 | 2015-08-06 10:54:13 -0700 | [diff] [blame] | 251 | this->onClear(renderTarget, rect, color); |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 252 | } |
| 253 | |
joshualitt | 6db519c | 2014-10-29 08:48:18 -0700 | [diff] [blame] | 254 | void GrGpu::clearStencilClip(const SkIRect& rect, |
| 255 | bool insideClip, |
| 256 | GrRenderTarget* renderTarget) { |
joshualitt | d53a827 | 2014-11-10 16:03:14 -0800 | [diff] [blame] | 257 | SkASSERT(renderTarget); |
joshualitt | 6db519c | 2014-10-29 08:48:18 -0700 | [diff] [blame] | 258 | this->handleDirtyContext(); |
| 259 | this->onClearStencilClip(renderTarget, rect, insideClip); |
| 260 | } |
| 261 | |
joshualitt | 1cbdcde | 2015-08-21 11:53:29 -0700 | [diff] [blame] | 262 | bool GrGpu::copySurface(GrSurface* dst, |
| 263 | GrSurface* src, |
| 264 | const SkIRect& srcRect, |
| 265 | const SkIPoint& dstPoint) { |
| 266 | SkASSERT(dst && src); |
| 267 | this->handleDirtyContext(); |
| 268 | return this->onCopySurface(dst, src, srcRect, dstPoint); |
| 269 | } |
| 270 | |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 271 | bool GrGpu::getReadPixelsInfo(GrSurface* srcSurface, int width, int height, size_t rowBytes, |
| 272 | GrPixelConfig readConfig, DrawPreference* drawPreference, |
| 273 | ReadPixelTempDrawInfo* tempDrawInfo) { |
| 274 | SkASSERT(drawPreference); |
| 275 | SkASSERT(tempDrawInfo); |
| 276 | SkASSERT(kGpuPrefersDraw_DrawPreference != *drawPreference); |
| 277 | |
egdaniel | 6d901da | 2015-07-30 12:02:15 -0700 | [diff] [blame] | 278 | // We currently do not support reading into a compressed buffer |
| 279 | if (GrPixelConfigIsCompressed(readConfig)) { |
| 280 | return false; |
| 281 | } |
| 282 | |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 283 | if (!this->onGetReadPixelsInfo(srcSurface, width, height, rowBytes, readConfig, drawPreference, |
| 284 | tempDrawInfo)) { |
| 285 | return false; |
| 286 | } |
| 287 | |
| 288 | // Check to see if we're going to request that the caller draw when drawing is not possible. |
| 289 | if (!srcSurface->asTexture() || |
| 290 | !this->caps()->isConfigRenderable(tempDrawInfo->fTempSurfaceDesc.fConfig, false)) { |
| 291 | // If we don't have a fallback to a straight read then fail. |
| 292 | if (kRequireDraw_DrawPreference == *drawPreference) { |
| 293 | return false; |
| 294 | } |
| 295 | *drawPreference = kNoDraw_DrawPreference; |
| 296 | } |
| 297 | |
| 298 | return true; |
| 299 | } |
cblume | ed82800 | 2016-02-16 13:00:01 -0800 | [diff] [blame] | 300 | bool GrGpu::getWritePixelsInfo(GrSurface* dstSurface, int width, int height, |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 301 | GrPixelConfig srcConfig, DrawPreference* drawPreference, |
| 302 | WritePixelTempDrawInfo* tempDrawInfo) { |
| 303 | SkASSERT(drawPreference); |
| 304 | SkASSERT(tempDrawInfo); |
| 305 | SkASSERT(kGpuPrefersDraw_DrawPreference != *drawPreference); |
| 306 | |
jvanverth | 2dc2994 | 2015-09-01 07:16:46 -0700 | [diff] [blame] | 307 | if (GrPixelConfigIsCompressed(dstSurface->desc().fConfig) && |
| 308 | dstSurface->desc().fConfig != srcConfig) { |
| 309 | return false; |
| 310 | } |
| 311 | |
bsalomon | babafcc | 2016-02-16 11:36:47 -0800 | [diff] [blame] | 312 | if (SkToBool(dstSurface->asRenderTarget())) { |
| 313 | if (this->caps()->useDrawInsteadOfAllRenderTargetWrites()) { |
| 314 | ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference); |
| 315 | } else if (this->caps()->useDrawInsteadOfPartialRenderTargetWrite() && |
| 316 | (width < dstSurface->width() || height < dstSurface->height())) { |
| 317 | ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference); |
| 318 | } |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 319 | } |
| 320 | |
cblume | ed82800 | 2016-02-16 13:00:01 -0800 | [diff] [blame] | 321 | if (!this->onGetWritePixelsInfo(dstSurface, width, height, srcConfig, drawPreference, |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 322 | tempDrawInfo)) { |
| 323 | return false; |
| 324 | } |
| 325 | |
| 326 | // Check to see if we're going to request that the caller draw when drawing is not possible. |
| 327 | if (!dstSurface->asRenderTarget() || |
| 328 | !this->caps()->isConfigTexturable(tempDrawInfo->fTempSurfaceDesc.fConfig)) { |
| 329 | // If we don't have a fallback to a straight upload then fail. |
| 330 | if (kRequireDraw_DrawPreference == *drawPreference || |
| 331 | !this->caps()->isConfigTexturable(srcConfig)) { |
| 332 | return false; |
| 333 | } |
| 334 | *drawPreference = kNoDraw_DrawPreference; |
| 335 | } |
| 336 | return true; |
| 337 | } |
| 338 | |
bsalomon | 6cb3cbe | 2015-07-30 07:34:27 -0700 | [diff] [blame] | 339 | bool GrGpu::readPixels(GrSurface* surface, |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 340 | int left, int top, int width, int height, |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 341 | GrPixelConfig config, void* buffer, |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 342 | size_t rowBytes) { |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 343 | this->handleDirtyContext(); |
egdaniel | 6d901da | 2015-07-30 12:02:15 -0700 | [diff] [blame] | 344 | |
| 345 | // We cannot read pixels into a compressed buffer |
| 346 | if (GrPixelConfigIsCompressed(config)) { |
| 347 | return false; |
| 348 | } |
| 349 | |
| 350 | size_t bpp = GrBytesPerPixel(config); |
| 351 | if (!GrSurfacePriv::AdjustReadPixelParams(surface->width(), surface->height(), bpp, |
| 352 | &left, &top, &width, &height, |
| 353 | &buffer, |
| 354 | &rowBytes)) { |
| 355 | return false; |
| 356 | } |
| 357 | |
| 358 | return this->onReadPixels(surface, |
| 359 | left, top, width, height, |
| 360 | config, buffer, |
| 361 | rowBytes); |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 362 | } |
| 363 | |
bsalomon | 6cb3cbe | 2015-07-30 07:34:27 -0700 | [diff] [blame] | 364 | bool GrGpu::writePixels(GrSurface* surface, |
| 365 | int left, int top, int width, int height, |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 366 | GrPixelConfig config, const SkTArray<GrMipLevel>& texels) { |
| 367 | if (!surface) { |
| 368 | return false; |
| 369 | } |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 370 | for (int currentMipLevel = 0; currentMipLevel < texels.count(); currentMipLevel++) { |
bsalomon | e699d0c | 2016-03-09 06:25:15 -0800 | [diff] [blame] | 371 | if (!texels[currentMipLevel].fPixels ) { |
| 372 | return false; |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 373 | } |
| 374 | } |
jvanverth | 2dc2994 | 2015-09-01 07:16:46 -0700 | [diff] [blame] | 375 | |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 376 | this->handleDirtyContext(); |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 377 | if (this->onWritePixels(surface, left, top, width, height, config, texels)) { |
bsalomon | b12ea41 | 2015-02-02 21:19:50 -0800 | [diff] [blame] | 378 | fStats.incTextureUploads(); |
| 379 | return true; |
| 380 | } |
| 381 | return false; |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 382 | } |
| 383 | |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 384 | bool GrGpu::writePixels(GrSurface* surface, |
| 385 | int left, int top, int width, int height, |
| 386 | GrPixelConfig config, const void* buffer, |
| 387 | size_t rowBytes) { |
| 388 | GrMipLevel mipLevel; |
| 389 | mipLevel.fPixels = buffer; |
| 390 | mipLevel.fRowBytes = rowBytes; |
| 391 | SkSTArray<1, GrMipLevel> texels; |
| 392 | texels.push_back(mipLevel); |
| 393 | |
| 394 | return this->writePixels(surface, left, top, width, height, config, texels); |
| 395 | } |
| 396 | |
jvanverth | c3d706f | 2016-04-20 10:33:27 -0700 | [diff] [blame] | 397 | bool GrGpu::transferPixels(GrSurface* surface, |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 398 | int left, int top, int width, int height, |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 399 | GrPixelConfig config, GrBuffer* transferBuffer, |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 400 | size_t offset, size_t rowBytes) { |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 401 | SkASSERT(transferBuffer); |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 402 | |
| 403 | this->handleDirtyContext(); |
jvanverth | c3d706f | 2016-04-20 10:33:27 -0700 | [diff] [blame] | 404 | if (this->onTransferPixels(surface, left, top, width, height, config, |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 405 | transferBuffer, offset, rowBytes)) { |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 406 | fStats.incTransfersToTexture(); |
| 407 | return true; |
| 408 | } |
| 409 | return false; |
| 410 | } |
| 411 | |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 412 | void GrGpu::resolveRenderTarget(GrRenderTarget* target) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 413 | SkASSERT(target); |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 414 | this->handleDirtyContext(); |
| 415 | this->onResolveRenderTarget(target); |
| 416 | } |
| 417 | |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 418 | inline static uint8_t multisample_specs_id(uint8_t numSamples, GrSurfaceOrigin origin, |
| 419 | const GrCaps& caps) { |
| 420 | if (!caps.sampleLocationsSupport()) { |
| 421 | return numSamples; |
| 422 | } |
| 423 | |
| 424 | SkASSERT(numSamples < 128); |
| 425 | SkASSERT(kTopLeft_GrSurfaceOrigin == origin || kBottomLeft_GrSurfaceOrigin == origin); |
| 426 | return (numSamples << 1) | (origin - 1); |
| 427 | |
| 428 | GR_STATIC_ASSERT(1 == kTopLeft_GrSurfaceOrigin); |
| 429 | GR_STATIC_ASSERT(2 == kBottomLeft_GrSurfaceOrigin); |
| 430 | } |
| 431 | |
| 432 | const GrGpu::MultisampleSpecs& GrGpu::getMultisampleSpecs(GrRenderTarget* rt, |
| 433 | const GrStencilSettings& stencil) { |
| 434 | const GrSurfaceDesc& desc = rt->desc(); |
| 435 | uint8_t surfDescKey = multisample_specs_id(desc.fSampleCnt, desc.fOrigin, *this->caps()); |
| 436 | if (fMultisampleSpecsMap.count() > surfDescKey && fMultisampleSpecsMap[surfDescKey]) { |
| 437 | #if !defined(SK_DEBUG) |
| 438 | // In debug mode we query the multisample info every time and verify the caching is correct. |
| 439 | return *fMultisampleSpecsMap[surfDescKey]; |
| 440 | #endif |
| 441 | } |
| 442 | int effectiveSampleCnt; |
| 443 | SkAutoTDeleteArray<SkPoint> locations(nullptr); |
| 444 | this->onGetMultisampleSpecs(rt, stencil, &effectiveSampleCnt, &locations); |
| 445 | SkASSERT(effectiveSampleCnt && effectiveSampleCnt >= desc.fSampleCnt); |
| 446 | uint8_t effectiveKey = multisample_specs_id(effectiveSampleCnt, desc.fOrigin, *this->caps()); |
| 447 | if (fMultisampleSpecsMap.count() > effectiveKey && fMultisampleSpecsMap[effectiveKey]) { |
| 448 | const MultisampleSpecs& specs = *fMultisampleSpecsMap[effectiveKey]; |
| 449 | SkASSERT(effectiveKey == specs.fUniqueID); |
| 450 | SkASSERT(effectiveSampleCnt == specs.fEffectiveSampleCnt); |
| 451 | SkASSERT(!this->caps()->sampleLocationsSupport() || |
| 452 | !memcmp(locations.get(), specs.fSampleLocations.get(), |
| 453 | effectiveSampleCnt * sizeof(SkPoint))); |
| 454 | SkASSERT(surfDescKey <= effectiveKey); |
| 455 | SkASSERT(!fMultisampleSpecsMap[surfDescKey] || fMultisampleSpecsMap[surfDescKey] == &specs); |
| 456 | fMultisampleSpecsMap[surfDescKey] = &specs; |
| 457 | return specs; |
| 458 | } |
| 459 | const MultisampleSpecs& specs = *new (&fMultisampleSpecsAllocator) |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 460 | MultisampleSpecs{effectiveKey, effectiveSampleCnt, locations.release()}; |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 461 | if (fMultisampleSpecsMap.count() <= effectiveKey) { |
| 462 | int n = 1 + effectiveKey - fMultisampleSpecsMap.count(); |
| 463 | fMultisampleSpecsMap.push_back_n(n, (const MultisampleSpecs*) nullptr); |
| 464 | } |
| 465 | fMultisampleSpecsMap[effectiveKey] = &specs; |
| 466 | if (effectiveSampleCnt != desc.fSampleCnt) { |
| 467 | SkASSERT(surfDescKey < effectiveKey); |
| 468 | fMultisampleSpecsMap[surfDescKey] = &specs; |
| 469 | } |
| 470 | return specs; |
| 471 | } |
| 472 | |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 473 | //////////////////////////////////////////////////////////////////////////////// |
| 474 | |
bsalomon | 7dbd45d | 2016-03-23 10:40:53 -0700 | [diff] [blame] | 475 | bool GrGpu::draw(const GrPipeline& pipeline, |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 476 | const GrPrimitiveProcessor& primProc, |
| 477 | const GrMesh* meshes, |
| 478 | int meshCount) { |
bsalomon | 7dbd45d | 2016-03-23 10:40:53 -0700 | [diff] [blame] | 479 | if (primProc.numAttribs() > this->caps()->maxVertexAttributes()) { |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 480 | fStats.incNumFailedDraws(); |
bsalomon | 7dbd45d | 2016-03-23 10:40:53 -0700 | [diff] [blame] | 481 | return false; |
| 482 | } |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 483 | this->handleDirtyContext(); |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 484 | |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 485 | this->onDraw(pipeline, primProc, meshes, meshCount); |
bsalomon | 7dbd45d | 2016-03-23 10:40:53 -0700 | [diff] [blame] | 486 | return true; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 487 | } |