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