| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
| 3 * | 3 * |
| 4 * 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 |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "gl/GrGLPathRendering.h" | 8 #include "gl/GrGLPathRendering.h" |
| 9 #include "gl/GrGLNameAllocator.h" | |
| 10 #include "gl/GrGLUtil.h" | 9 #include "gl/GrGLUtil.h" |
| 11 #include "gl/GrGLGpu.h" | 10 #include "gl/GrGLGpu.h" |
| 12 | 11 |
| 13 #include "GrGLPath.h" | 12 #include "GrGLPath.h" |
| 14 #include "GrGLPathRange.h" | 13 #include "GrGLPathRange.h" |
| 15 #include "GrGLPathRendering.h" | 14 #include "GrGLPathRendering.h" |
| 16 | 15 |
| 17 #include "SkStream.h" | 16 #include "SkStream.h" |
| 18 #include "SkTypeface.h" | 17 #include "SkTypeface.h" |
| 19 | 18 |
| 20 #define GL_CALL(X) GR_GL_CALL(this->gpu()->glInterface(), X) | 19 #define GL_CALL(X) GR_GL_CALL(this->gpu()->glInterface(), X) |
| 21 #define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->gpu()->glInterface(), RET, X) | 20 #define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->gpu()->glInterface(), RET, X) |
| 22 | 21 |
| 22 // Number of paths to allocate per glGenPaths call. The call can be overly slow
on command buffer GL |
| 23 // implementation. The call has a result value, and thus waiting for the call co
mpletion is needed. |
| 24 static const GrGLsizei kPathIDPreallocationAmount = 65536; |
| 23 | 25 |
| 24 static const GrGLenum gIndexType2GLType[] = { | 26 static const GrGLenum gIndexType2GLType[] = { |
| 25 GR_GL_UNSIGNED_BYTE, | 27 GR_GL_UNSIGNED_BYTE, |
| 26 GR_GL_UNSIGNED_SHORT, | 28 GR_GL_UNSIGNED_SHORT, |
| 27 GR_GL_UNSIGNED_INT | 29 GR_GL_UNSIGNED_INT |
| 28 }; | 30 }; |
| 29 | 31 |
| 30 GR_STATIC_ASSERT(0 == GrPathRange::kU8_PathIndexType); | 32 GR_STATIC_ASSERT(0 == GrPathRange::kU8_PathIndexType); |
| 31 GR_STATIC_ASSERT(1 == GrPathRange::kU16_PathIndexType); | 33 GR_STATIC_ASSERT(1 == GrPathRange::kU16_PathIndexType); |
| 32 GR_STATIC_ASSERT(2 == GrPathRange::kU32_PathIndexType); | 34 GR_STATIC_ASSERT(2 == GrPathRange::kU32_PathIndexType); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 53 SkFAIL("Unexpected path fill."); | 55 SkFAIL("Unexpected path fill."); |
| 54 /* fallthrough */; | 56 /* fallthrough */; |
| 55 case kIncClamp_StencilOp: | 57 case kIncClamp_StencilOp: |
| 56 return GR_GL_COUNT_UP; | 58 return GR_GL_COUNT_UP; |
| 57 case kInvert_StencilOp: | 59 case kInvert_StencilOp: |
| 58 return GR_GL_INVERT; | 60 return GR_GL_INVERT; |
| 59 } | 61 } |
| 60 } | 62 } |
| 61 | 63 |
| 62 GrGLPathRendering::GrGLPathRendering(GrGLGpu* gpu) | 64 GrGLPathRendering::GrGLPathRendering(GrGLGpu* gpu) |
| 63 : GrPathRendering(gpu) { | 65 : GrPathRendering(gpu) |
| 66 , fPreallocatedPathCount(0) { |
| 64 const GrGLInterface* glInterface = gpu->glInterface(); | 67 const GrGLInterface* glInterface = gpu->glInterface(); |
| 65 fCaps.bindFragmentInputSupport = | 68 fCaps.bindFragmentInputSupport = |
| 66 nullptr != glInterface->fFunctions.fBindFragmentInputLocation; | 69 nullptr != glInterface->fFunctions.fBindFragmentInputLocation; |
| 67 } | 70 } |
| 68 | 71 |
| 69 GrGLPathRendering::~GrGLPathRendering() { | 72 GrGLPathRendering::~GrGLPathRendering() { |
| 73 if (fPreallocatedPathCount > 0) { |
| 74 this->deletePaths(fFirstPreallocatedPathID, fPreallocatedPathCount); |
| 75 } |
| 70 } | 76 } |
| 71 | 77 |
| 72 void GrGLPathRendering::abandonGpuResources() { | 78 void GrGLPathRendering::abandonGpuResources() { |
| 73 fPathNameAllocator.reset(nullptr); | 79 fPreallocatedPathCount = 0; |
| 74 } | 80 } |
| 75 | 81 |
| 76 void GrGLPathRendering::resetContext() { | 82 void GrGLPathRendering::resetContext() { |
| 77 fHWProjectionMatrixState.invalidate(); | 83 fHWProjectionMatrixState.invalidate(); |
| 78 // we don't use the model view matrix. | 84 // we don't use the model view matrix. |
| 79 GL_CALL(MatrixLoadIdentity(GR_GL_PATH_MODELVIEW)); | 85 GL_CALL(MatrixLoadIdentity(GR_GL_PATH_MODELVIEW)); |
| 80 | 86 |
| 81 fHWPathStencilSettings.invalidate(); | 87 fHWPathStencilSettings.invalidate(); |
| 82 } | 88 } |
| 83 | 89 |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 fHWProjectionMatrixState.fViewMatrix = matrix; | 229 fHWProjectionMatrixState.fViewMatrix = matrix; |
| 224 fHWProjectionMatrixState.fRenderTargetSize = renderTargetSize; | 230 fHWProjectionMatrixState.fRenderTargetSize = renderTargetSize; |
| 225 fHWProjectionMatrixState.fRenderTargetOrigin = renderTargetOrigin; | 231 fHWProjectionMatrixState.fRenderTargetOrigin = renderTargetOrigin; |
| 226 | 232 |
| 227 float glMatrix[4 * 4]; | 233 float glMatrix[4 * 4]; |
| 228 fHWProjectionMatrixState.getRTAdjustedGLMatrix<4>(glMatrix); | 234 fHWProjectionMatrixState.getRTAdjustedGLMatrix<4>(glMatrix); |
| 229 GL_CALL(MatrixLoadf(GR_GL_PATH_PROJECTION, glMatrix)); | 235 GL_CALL(MatrixLoadf(GR_GL_PATH_PROJECTION, glMatrix)); |
| 230 } | 236 } |
| 231 | 237 |
| 232 GrGLuint GrGLPathRendering::genPaths(GrGLsizei range) { | 238 GrGLuint GrGLPathRendering::genPaths(GrGLsizei range) { |
| 233 if (range > 1) { | 239 SkASSERT(range > 0); |
| 234 GrGLuint name; | 240 GrGLuint firstID; |
| 235 GL_CALL_RET(name, GenPaths(range)); | 241 if (fPreallocatedPathCount >= range) { |
| 236 return name; | 242 firstID = fFirstPreallocatedPathID; |
| 243 fPreallocatedPathCount -= range; |
| 244 fFirstPreallocatedPathID += range; |
| 245 return firstID; |
| 246 } |
| 247 // Allocate range + the amount to fill up preallocation amount. If succeed,
either join with |
| 248 // the existing preallocation range or delete the existing and use the new (
potentially partial) |
| 249 // preallocation range. |
| 250 GrGLsizei allocAmount = range + (kPathIDPreallocationAmount - fPreallocatedP
athCount); |
| 251 if (allocAmount >= range) { |
| 252 GL_CALL_RET(firstID, GenPaths(allocAmount)); |
| 253 |
| 254 if (firstID != 0) { |
| 255 if (fPreallocatedPathCount > 0 && |
| 256 firstID == fFirstPreallocatedPathID + fPreallocatedPathCount) { |
| 257 firstID = fFirstPreallocatedPathID; |
| 258 fPreallocatedPathCount += allocAmount - range; |
| 259 fFirstPreallocatedPathID += range; |
| 260 return firstID; |
| 261 } |
| 262 |
| 263 if (allocAmount > range) { |
| 264 if (fPreallocatedPathCount > 0) { |
| 265 this->deletePaths(fFirstPreallocatedPathID, fPreallocatedPat
hCount); |
| 266 } |
| 267 fFirstPreallocatedPathID = firstID + range; |
| 268 fPreallocatedPathCount = allocAmount - range; |
| 269 } |
| 270 // Special case: if allocAmount == range, we have full preallocated
range. |
| 271 return firstID; |
| 272 } |
| 273 } |
| 274 // Failed to allocate with preallocation. Remove existing preallocation and
try to allocate just |
| 275 // the range. |
| 276 if (fPreallocatedPathCount > 0) { |
| 277 this->deletePaths(fFirstPreallocatedPathID, fPreallocatedPathCount); |
| 278 fPreallocatedPathCount = 0; |
| 237 } | 279 } |
| 238 | 280 |
| 239 if (nullptr == fPathNameAllocator.get()) { | 281 GL_CALL_RET(firstID, GenPaths(range)); |
| 240 static const int range = 65536; | 282 if (firstID == 0) { |
| 241 GrGLuint firstName; | 283 SkDebugf("Warning: Failed to allocate path\n"); |
| 242 GL_CALL_RET(firstName, GenPaths(range)); | |
| 243 fPathNameAllocator.reset(new GrGLNameAllocator(firstName, firstName + ra
nge)); | |
| 244 } | 284 } |
| 245 | 285 return firstID; |
| 246 // When allocating names one at a time, pull from a client-side pool of | |
| 247 // available names in order to save a round trip to the GL server. | |
| 248 GrGLuint name = fPathNameAllocator->allocateName(); | |
| 249 | |
| 250 if (0 == name) { | |
| 251 // Our reserved path names are all in use. Fall back on GenPaths. | |
| 252 GL_CALL_RET(name, GenPaths(1)); | |
| 253 } | |
| 254 | |
| 255 return name; | |
| 256 } | 286 } |
| 257 | 287 |
| 258 void GrGLPathRendering::deletePaths(GrGLuint path, GrGLsizei range) { | 288 void GrGLPathRendering::deletePaths(GrGLuint path, GrGLsizei range) { |
| 259 if (range > 1) { | 289 GL_CALL(DeletePaths(path, range)); |
| 260 // It is not supported to delete names in ranges that were allocated | |
| 261 // individually using GrGLPathNameAllocator. | |
| 262 SkASSERT(nullptr == fPathNameAllocator.get() || | |
| 263 path + range <= fPathNameAllocator->firstName() || | |
| 264 path >= fPathNameAllocator->endName()); | |
| 265 GL_CALL(DeletePaths(path, range)); | |
| 266 return; | |
| 267 } | |
| 268 | |
| 269 if (nullptr == fPathNameAllocator.get() || | |
| 270 path < fPathNameAllocator->firstName() || | |
| 271 path >= fPathNameAllocator->endName()) { | |
| 272 // If we aren't inside fPathNameAllocator's range then this name was | |
| 273 // generated by the GenPaths fallback (or else was never allocated). | |
| 274 GL_CALL(DeletePaths(path, 1)); | |
| 275 return; | |
| 276 } | |
| 277 | |
| 278 // Make the path empty to save memory, but don't free the name in the driver
. | |
| 279 GL_CALL(PathCommands(path, 0, nullptr, 0, GR_GL_FLOAT, nullptr)); | |
| 280 fPathNameAllocator->free(path); | |
| 281 } | 290 } |
| 282 | 291 |
| 283 void GrGLPathRendering::flushPathStencilSettings(const GrStencilSettings& stenci
lSettings) { | 292 void GrGLPathRendering::flushPathStencilSettings(const GrStencilSettings& stenci
lSettings) { |
| 284 if (fHWPathStencilSettings != stencilSettings) { | 293 if (fHWPathStencilSettings != stencilSettings) { |
| 285 // Just the func, ref, and mask is set here. The op and write mask are p
arams to the call | 294 // Just the func, ref, and mask is set here. The op and write mask are p
arams to the call |
| 286 // that draws the path to the SB (glStencilFillPath) | 295 // that draws the path to the SB (glStencilFillPath) |
| 287 GrGLenum func = | 296 GrGLenum func = |
| 288 GrToGLStencilFunc(stencilSettings.func(GrStencilSettings::kFront_Fac
e)); | 297 GrToGLStencilFunc(stencilSettings.func(GrStencilSettings::kFront_Fac
e)); |
| 289 GL_CALL(PathStencilFunc(func, stencilSettings.funcRef(GrStencilSettings:
:kFront_Face), | 298 GL_CALL(PathStencilFunc(func, stencilSettings.funcRef(GrStencilSettings:
:kFront_Face), |
| 290 stencilSettings.funcMask(GrStencilSettings::kFro
nt_Face))); | 299 stencilSettings.funcMask(GrStencilSettings::kFro
nt_Face))); |
| 291 | 300 |
| 292 fHWPathStencilSettings = stencilSettings; | 301 fHWPathStencilSettings = stencilSettings; |
| 293 } | 302 } |
| 294 } | 303 } |
| 295 | 304 |
| 296 inline GrGLGpu* GrGLPathRendering::gpu() { | 305 inline GrGLGpu* GrGLPathRendering::gpu() { |
| 297 return static_cast<GrGLGpu*>(fGpu); | 306 return static_cast<GrGLGpu*>(fGpu); |
| 298 } | 307 } |
| OLD | NEW |