| OLD | NEW |
| 1 | |
| 2 /* | 1 /* |
| 3 * Copyright 2010 Google Inc. | 2 * Copyright 2010 Google Inc. |
| 4 * | 3 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 7 */ | 6 */ |
| 8 | 7 |
| 9 | 8 |
| 10 #include "GrGpu.h" | 9 #include "GrGpu.h" |
| 11 | 10 |
| 12 #include "GrCaps.h" | 11 #include "GrCaps.h" |
| 13 #include "GrContext.h" | 12 #include "GrContext.h" |
| 14 #include "GrGpuResourcePriv.h" | 13 #include "GrGpuResourcePriv.h" |
| 15 #include "GrIndexBuffer.h" | 14 #include "GrIndexBuffer.h" |
| 16 #include "GrPathRendering.h" | 15 #include "GrPathRendering.h" |
| 17 #include "GrPipeline.h" | 16 #include "GrPipeline.h" |
| 18 #include "GrResourceCache.h" | 17 #include "GrResourceCache.h" |
| 19 #include "GrResourceProvider.h" | 18 #include "GrResourceProvider.h" |
| 20 #include "GrRenderTargetPriv.h" | 19 #include "GrRenderTargetPriv.h" |
| 21 #include "GrStencilAttachment.h" | 20 #include "GrStencilAttachment.h" |
| 22 #include "GrSurfacePriv.h" | 21 #include "GrSurfacePriv.h" |
| 23 #include "GrTransferBuffer.h" | 22 #include "GrTransferBuffer.h" |
| 24 #include "GrVertexBuffer.h" | 23 #include "GrVertexBuffer.h" |
| 25 #include "GrVertices.h" | 24 #include "GrVertices.h" |
| 25 #include "SkTypes.h" |
| 26 | 26 |
| 27 GrVertices& GrVertices::operator =(const GrVertices& di) { | 27 GrVertices& GrVertices::operator =(const GrVertices& di) { |
| 28 fPrimitiveType = di.fPrimitiveType; | 28 fPrimitiveType = di.fPrimitiveType; |
| 29 fStartVertex = di.fStartVertex; | 29 fStartVertex = di.fStartVertex; |
| 30 fStartIndex = di.fStartIndex; | 30 fStartIndex = di.fStartIndex; |
| 31 fVertexCount = di.fVertexCount; | 31 fVertexCount = di.fVertexCount; |
| 32 fIndexCount = di.fIndexCount; | 32 fIndexCount = di.fIndexCount; |
| 33 | 33 |
| 34 fInstanceCount = di.fInstanceCount; | 34 fInstanceCount = di.fInstanceCount; |
| 35 fVerticesPerInstance = di.fVerticesPerInstance; | 35 fVerticesPerInstance = di.fVerticesPerInstance; |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 // By default, GrRenderTargets are GL's normal orientation so that they | 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 | 83 // can be drawn to by the outside world without the client having |
| 84 // to render upside down. | 84 // to render upside down. |
| 85 if (kDefault_GrSurfaceOrigin == origin) { | 85 if (kDefault_GrSurfaceOrigin == origin) { |
| 86 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOr
igin; | 86 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOr
igin; |
| 87 } else { | 87 } else { |
| 88 return origin; | 88 return origin; |
| 89 } | 89 } |
| 90 } | 90 } |
| 91 | 91 |
| 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 GrSurfaceDes
c& desc, |
| 101 bool* isRT) { |
| 102 if (!caps.isConfigTexturable(desc.fConfig)) { |
| 103 return false; |
| 104 } |
| 105 |
| 106 *isRT = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag); |
| 107 if (*isRT && !caps.isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) { |
| 108 return false; |
| 109 } |
| 110 |
| 111 // We currently do not support multisampled textures |
| 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 } |
| 127 return true; |
| 128 } |
| 129 |
| 92 GrTexture* GrGpu::createTexture(const GrSurfaceDesc& origDesc, bool budgeted, | 130 GrTexture* GrGpu::createTexture(const GrSurfaceDesc& origDesc, bool budgeted, |
| 93 const void* srcData, size_t rowBytes) { | 131 const SkTArray<SkMipMapLevel>& texels) { |
| 94 GrSurfaceDesc desc = origDesc; | 132 GrSurfaceDesc desc = origDesc; |
| 95 | 133 |
| 96 if (!this->caps()->isConfigTexturable(desc.fConfig)) { | 134 const GrCaps* caps = this->caps(); |
| 135 bool isRT = false; |
| 136 bool textureCreationParamsValid = check_texture_creation_params(*caps, desc,
&isRT); |
| 137 if (!textureCreationParamsValid) { |
| 97 return nullptr; | 138 return nullptr; |
| 98 } | 139 } |
| 99 | 140 |
| 100 bool isRT = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag); | 141 desc.fSampleCnt = SkTMin(desc.fSampleCnt, caps->maxSampleCount()); |
| 101 if (isRT && !this->caps()->isConfigRenderable(desc.fConfig, desc.fSampleCnt
> 0)) { | 142 // Attempt to catch un- or wrongly intialized sample counts; |
| 102 return nullptr; | |
| 103 } | |
| 104 | |
| 105 // We currently do not support multisampled textures | |
| 106 if (!isRT && desc.fSampleCnt > 0) { | |
| 107 return nullptr; | |
| 108 } | |
| 109 | |
| 110 GrTexture *tex = nullptr; | |
| 111 | |
| 112 if (isRT) { | |
| 113 int maxRTSize = this->caps()->maxRenderTargetSize(); | |
| 114 if (desc.fWidth > maxRTSize || desc.fHeight > maxRTSize) { | |
| 115 return nullptr; | |
| 116 } | |
| 117 } else { | |
| 118 int maxSize = this->caps()->maxTextureSize(); | |
| 119 if (desc.fWidth > maxSize || desc.fHeight > maxSize) { | |
| 120 return nullptr; | |
| 121 } | |
| 122 } | |
| 123 | |
| 124 GrGpuResource::LifeCycle lifeCycle = budgeted ? GrGpuResource::kCached_LifeC
ycle : | |
| 125 GrGpuResource::kUncached_Lif
eCycle; | |
| 126 | |
| 127 desc.fSampleCnt = SkTMin(desc.fSampleCnt, this->caps()->maxSampleCount()); | |
| 128 // Attempt to catch un- or wrongly initialized sample counts; | |
| 129 SkASSERT(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64); | 143 SkASSERT(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64); |
| 130 | 144 |
| 131 desc.fOrigin = resolve_origin(desc.fOrigin, isRT); | 145 desc.fOrigin = resolve_origin(desc.fOrigin, isRT); |
| 132 | 146 |
| 147 GrTexture* tex = nullptr; |
| 148 GrGpuResource::LifeCycle lifeCycle = budgeted ? GrGpuResource::kCached_LifeC
ycle : |
| 149 GrGpuResource::kUncached_Lif
eCycle; |
| 150 |
| 133 if (GrPixelConfigIsCompressed(desc.fConfig)) { | 151 if (GrPixelConfigIsCompressed(desc.fConfig)) { |
| 134 // We shouldn't be rendering into this | 152 // We shouldn't be rendering into this |
| 135 SkASSERT(!isRT); | 153 SkASSERT(!isRT); |
| 136 SkASSERT(0 == desc.fSampleCnt); | 154 SkASSERT(0 == desc.fSampleCnt); |
| 137 | 155 |
| 138 if (!this->caps()->npotTextureTileSupport() && | 156 if (!caps->npotTextureTileSupport() && |
| 139 (!SkIsPow2(desc.fWidth) || !SkIsPow2(desc.fHeight))) { | 157 (!SkIsPow2(desc.fWidth) || !SkIsPow2(desc.fHeight))) { |
| 140 return nullptr; | 158 return nullptr; |
| 141 } | 159 } |
| 142 | 160 |
| 143 this->handleDirtyContext(); | 161 this->handleDirtyContext(); |
| 144 tex = this->onCreateCompressedTexture(desc, lifeCycle, srcData); | 162 tex = this->onCreateCompressedTexture(desc, lifeCycle, texels); |
| 145 } else { | 163 } else { |
| 146 this->handleDirtyContext(); | 164 this->handleDirtyContext(); |
| 147 tex = this->onCreateTexture(desc, lifeCycle, srcData, rowBytes); | 165 tex = this->onCreateTexture(desc, lifeCycle, texels); |
| 148 } | |
| 149 if (!this->caps()->reuseScratchTextures() && !isRT) { | |
| 150 tex->resourcePriv().removeScratchKey(); | |
| 151 } | 166 } |
| 152 if (tex) { | 167 if (tex) { |
| 168 if (!caps->reuseScratchTextures() && !isRT) { |
| 169 tex->resourcePriv().removeScratchKey(); |
| 170 } |
| 153 fStats.incTextureCreates(); | 171 fStats.incTextureCreates(); |
| 154 if (srcData) { | 172 if (!texels.empty()) { |
| 155 fStats.incTextureUploads(); | 173 if (texels[0].fTexelsOrOffset) { |
| 174 fStats.incTextureUploads(); |
| 175 } |
| 156 } | 176 } |
| 157 } | 177 } |
| 158 return tex; | 178 return tex; |
| 159 } | 179 } |
| 160 | 180 |
| 181 GrTexture* GrGpu::createTexture(const GrSurfaceDesc& desc, bool budgeted, |
| 182 const void* srcData, size_t rowBytes) { |
| 183 SkMipMapLevel level(srcData, rowBytes, desc.fWidth, desc.fHeight); |
| 184 SkSTArray<1, SkMipMapLevel> levels; |
| 185 levels.push_back(level); |
| 186 |
| 187 return this->createTexture(desc, budgeted, levels); |
| 188 } |
| 189 |
| 161 GrTexture* GrGpu::wrapBackendTexture(const GrBackendTextureDesc& desc, GrWrapOwn
ership ownership) { | 190 GrTexture* GrGpu::wrapBackendTexture(const GrBackendTextureDesc& desc, GrWrapOwn
ership ownership) { |
| 162 this->handleDirtyContext(); | 191 this->handleDirtyContext(); |
| 163 if (!this->caps()->isConfigTexturable(desc.fConfig)) { | 192 if (!this->caps()->isConfigTexturable(desc.fConfig)) { |
| 164 return nullptr; | 193 return nullptr; |
| 165 } | 194 } |
| 166 if ((desc.fFlags & kRenderTarget_GrBackendTextureFlag) && | 195 if ((desc.fFlags & kRenderTarget_GrBackendTextureFlag) && |
| 167 !this->caps()->isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) { | 196 !this->caps()->isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) { |
| 168 return nullptr; | 197 return nullptr; |
| 169 } | 198 } |
| 170 GrTexture* tex = this->onWrapBackendTexture(desc, ownership); | 199 GrTexture* tex = this->onWrapBackendTexture(desc, ownership); |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 !this->caps()->isConfigRenderable(tempDrawInfo->fTempSurfaceDesc.fConfig
, false)) { | 291 !this->caps()->isConfigRenderable(tempDrawInfo->fTempSurfaceDesc.fConfig
, false)) { |
| 263 // If we don't have a fallback to a straight read then fail. | 292 // If we don't have a fallback to a straight read then fail. |
| 264 if (kRequireDraw_DrawPreference == *drawPreference) { | 293 if (kRequireDraw_DrawPreference == *drawPreference) { |
| 265 return false; | 294 return false; |
| 266 } | 295 } |
| 267 *drawPreference = kNoDraw_DrawPreference; | 296 *drawPreference = kNoDraw_DrawPreference; |
| 268 } | 297 } |
| 269 | 298 |
| 270 return true; | 299 return true; |
| 271 } | 300 } |
| 272 bool GrGpu::getWritePixelsInfo(GrSurface* dstSurface, int width, int height, siz
e_t rowBytes, | 301 bool GrGpu::getWritePixelsInfo(GrSurface* dstSurface, int width, int height, |
| 273 GrPixelConfig srcConfig, DrawPreference* drawPref
erence, | 302 GrPixelConfig srcConfig, DrawPreference* drawPref
erence, |
| 274 WritePixelTempDrawInfo* tempDrawInfo) { | 303 WritePixelTempDrawInfo* tempDrawInfo) { |
| 275 SkASSERT(drawPreference); | 304 SkASSERT(drawPreference); |
| 276 SkASSERT(tempDrawInfo); | 305 SkASSERT(tempDrawInfo); |
| 277 SkASSERT(kGpuPrefersDraw_DrawPreference != *drawPreference); | 306 SkASSERT(kGpuPrefersDraw_DrawPreference != *drawPreference); |
| 278 | 307 |
| 279 if (GrPixelConfigIsCompressed(dstSurface->desc().fConfig) && | 308 if (GrPixelConfigIsCompressed(dstSurface->desc().fConfig) && |
| 280 dstSurface->desc().fConfig != srcConfig) { | 309 dstSurface->desc().fConfig != srcConfig) { |
| 281 return false; | 310 return false; |
| 282 } | 311 } |
| 283 | 312 |
| 284 if (this->caps()->useDrawInsteadOfPartialRenderTargetWrite() && | 313 if (this->caps()->useDrawInsteadOfPartialRenderTargetWrite() && |
| 285 SkToBool(dstSurface->asRenderTarget()) && | 314 SkToBool(dstSurface->asRenderTarget()) && |
| 286 (width < dstSurface->width() || height < dstSurface->height())) { | 315 (width < dstSurface->width() || height < dstSurface->height())) { |
| 287 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference); | 316 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference); |
| 288 } | 317 } |
| 289 | 318 |
| 290 if (!this->onGetWritePixelsInfo(dstSurface, width, height, rowBytes, srcConf
ig, drawPreference, | 319 if (!this->onGetWritePixelsInfo(dstSurface, width, height, srcConfig, drawPr
eference, |
| 291 tempDrawInfo)) { | 320 tempDrawInfo)) { |
| 292 return false; | 321 return false; |
| 293 } | 322 } |
| 294 | 323 |
| 295 // Check to see if we're going to request that the caller draw when drawing
is not possible. | 324 // Check to see if we're going to request that the caller draw when drawing
is not possible. |
| 296 if (!dstSurface->asRenderTarget() || | 325 if (!dstSurface->asRenderTarget() || |
| 297 !this->caps()->isConfigTexturable(tempDrawInfo->fTempSurfaceDesc.fConfig
)) { | 326 !this->caps()->isConfigTexturable(tempDrawInfo->fTempSurfaceDesc.fConfig
)) { |
| 298 // If we don't have a fallback to a straight upload then fail. | 327 // If we don't have a fallback to a straight upload then fail. |
| 299 if (kRequireDraw_DrawPreference == *drawPreference || | 328 if (kRequireDraw_DrawPreference == *drawPreference || |
| 300 !this->caps()->isConfigTexturable(srcConfig)) { | 329 !this->caps()->isConfigTexturable(srcConfig)) { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 325 } | 354 } |
| 326 | 355 |
| 327 return this->onReadPixels(surface, | 356 return this->onReadPixels(surface, |
| 328 left, top, width, height, | 357 left, top, width, height, |
| 329 config, buffer, | 358 config, buffer, |
| 330 rowBytes); | 359 rowBytes); |
| 331 } | 360 } |
| 332 | 361 |
| 333 bool GrGpu::writePixels(GrSurface* surface, | 362 bool GrGpu::writePixels(GrSurface* surface, |
| 334 int left, int top, int width, int height, | 363 int left, int top, int width, int height, |
| 335 GrPixelConfig config, const void* buffer, | 364 GrPixelConfig config, const SkTArray<SkMipMapLevel>& tex
els) { |
| 336 size_t rowBytes) { | 365 if (!surface) { |
| 337 if (!buffer || !surface) { | 366 return false; |
| 367 } |
| 368 bool validMipDataFound = false; |
| 369 for (int currentMipLevel = 0; currentMipLevel < texels.count(); currentMipLe
vel++) { |
| 370 if (texels[currentMipLevel].fTexelsOrOffset != nullptr) { |
| 371 validMipDataFound = true; |
| 372 break; |
| 373 } |
| 374 } |
| 375 if (!validMipDataFound) { |
| 338 return false; | 376 return false; |
| 339 } | 377 } |
| 340 | 378 |
| 341 this->handleDirtyContext(); | 379 this->handleDirtyContext(); |
| 342 if (this->onWritePixels(surface, left, top, width, height, config, buffer, r
owBytes)) { | 380 if (this->onWritePixels(surface, left, top, width, height, config, texels))
{ |
| 343 fStats.incTextureUploads(); | 381 fStats.incTextureUploads(); |
| 344 return true; | 382 return true; |
| 345 } | 383 } |
| 346 return false; | 384 return false; |
| 347 } | 385 } |
| 348 | 386 |
| 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 SkMipMapLevel mipLevel(buffer, rowBytes, width, height); |
| 392 SkSTArray<1, SkMipMapLevel> texels; |
| 393 texels.push_back(mipLevel); |
| 394 |
| 395 return this->writePixels(surface, left, top, width, height, config, texels); |
| 396 } |
| 397 |
| 349 bool GrGpu::transferPixels(GrSurface* surface, | 398 bool GrGpu::transferPixels(GrSurface* surface, |
| 350 int left, int top, int width, int height, | 399 int left, int top, int width, int height, |
| 351 GrPixelConfig config, GrTransferBuffer* buffer, | 400 GrPixelConfig config, GrTransferBuffer* buffer, |
| 352 size_t offset, size_t rowBytes) { | 401 size_t offset, size_t rowBytes) { |
| 353 SkASSERT(buffer); | 402 SkASSERT(buffer); |
| 354 | 403 |
| 355 this->handleDirtyContext(); | 404 this->handleDirtyContext(); |
| 356 if (this->onTransferPixels(surface, left, top, width, height, config, | 405 if (this->onTransferPixels(surface, left, top, width, height, config, |
| 357 buffer, offset, rowBytes)) { | 406 buffer, offset, rowBytes)) { |
| 358 fStats.incTransfersToTexture(); | 407 fStats.incTransfersToTexture(); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 375 this->xferBarrier(args.fPipeline->getRenderTarget(), barrierType); | 424 this->xferBarrier(args.fPipeline->getRenderTarget(), barrierType); |
| 376 } | 425 } |
| 377 | 426 |
| 378 GrVertices::Iterator iter; | 427 GrVertices::Iterator iter; |
| 379 const GrNonInstancedVertices* verts = iter.init(vertices); | 428 const GrNonInstancedVertices* verts = iter.init(vertices); |
| 380 do { | 429 do { |
| 381 this->onDraw(args, *verts); | 430 this->onDraw(args, *verts); |
| 382 fStats.incNumDraws(); | 431 fStats.incNumDraws(); |
| 383 } while ((verts = iter.next())); | 432 } while ((verts = iter.next())); |
| 384 } | 433 } |
| OLD | NEW |