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 |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 // By default, GrRenderTargets are GL's normal orientation so that they | 89 // By default, GrRenderTargets are GL's normal orientation so that they |
91 // can be drawn to by the outside world without the client having | 90 // can be drawn to by the outside world without the client having |
92 // to render upside down. | 91 // to render upside down. |
93 if (kDefault_GrSurfaceOrigin == origin) { | 92 if (kDefault_GrSurfaceOrigin == origin) { |
94 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOr
igin; | 93 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOr
igin; |
95 } else { | 94 } else { |
96 return origin; | 95 return origin; |
97 } | 96 } |
98 } | 97 } |
99 | 98 |
100 GrTexture* GrGpu::createTexture(const GrSurfaceDesc& origDesc, bool budgeted, | 99 /** |
101 const void* srcData, size_t rowBytes) { | 100 * Prior to creating a texture, make sure the type of texture being created is |
102 GrSurfaceDesc desc = origDesc; | 101 * supported by calling check_texture_creation_params. |
103 | 102 * |
104 if (!this->caps()->isConfigTexturable(desc.fConfig)) { | 103 * @param caps The capabilities of the GL device. |
105 return nullptr; | 104 * @param desc The descriptor of the texture to create. |
| 105 * @param isRT Indicates if the texture can be a render target. |
| 106 */ |
| 107 static bool check_texture_creation_params(const GrCaps& caps, const GrSurfaceDes
c& desc, |
| 108 bool* isRT) { |
| 109 if (!caps.isConfigTexturable(desc.fConfig)) { |
| 110 return false; |
106 } | 111 } |
107 | 112 |
108 bool isRT = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag); | 113 *isRT = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag); |
109 if (isRT && !this->caps()->isConfigRenderable(desc.fConfig, desc.fSampleCnt
> 0)) { | 114 if (*isRT && !caps.isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) { |
110 return nullptr; | 115 return false; |
111 } | 116 } |
112 | 117 |
113 // We currently do not support multisampled textures | 118 // We currently do not support multisampled textures |
114 if (!isRT && desc.fSampleCnt > 0) { | 119 if (!*isRT && desc.fSampleCnt > 0) { |
115 return nullptr; | 120 return false; |
116 } | 121 } |
117 | 122 |
118 GrTexture *tex = nullptr; | 123 if (*isRT) { |
119 | 124 int maxRTSize = caps.maxRenderTargetSize(); |
120 if (isRT) { | |
121 int maxRTSize = this->caps()->maxRenderTargetSize(); | |
122 if (desc.fWidth > maxRTSize || desc.fHeight > maxRTSize) { | 125 if (desc.fWidth > maxRTSize || desc.fHeight > maxRTSize) { |
123 return nullptr; | 126 return false; |
124 } | 127 } |
125 } else { | 128 } else { |
126 int maxSize = this->caps()->maxTextureSize(); | 129 int maxSize = caps.maxTextureSize(); |
127 if (desc.fWidth > maxSize || desc.fHeight > maxSize) { | 130 if (desc.fWidth > maxSize || desc.fHeight > maxSize) { |
128 return nullptr; | 131 return false; |
129 } | 132 } |
130 } | 133 } |
| 134 return true; |
| 135 } |
131 | 136 |
132 GrGpuResource::LifeCycle lifeCycle = budgeted ? GrGpuResource::kCached_LifeC
ycle : | 137 GrTexture* GrGpu::createTexture(const GrSurfaceDesc& origDesc, bool budgeted, |
133 GrGpuResource::kUncached_Lif
eCycle; | 138 const SkTArray<SkMipMapLevel>& texels) { |
| 139 GrSurfaceDesc desc = origDesc; |
134 | 140 |
135 desc.fSampleCnt = SkTMin(desc.fSampleCnt, this->caps()->maxSampleCount()); | 141 const GrCaps* caps = this->caps(); |
136 // Attempt to catch un- or wrongly initialized sample counts; | 142 if (!caps) { |
137 SkASSERT(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64); | 143 return nullptr; |
138 | 144 } else { |
139 desc.fOrigin = resolve_origin(desc.fOrigin, isRT); | 145 bool isRT = false; |
140 | 146 bool textureCreationParamsValid = check_texture_creation_params(*caps, d
esc, &isRT); |
141 if (GrPixelConfigIsCompressed(desc.fConfig)) { | 147 if (!textureCreationParamsValid) { |
142 // We shouldn't be rendering into this | |
143 SkASSERT(!isRT); | |
144 SkASSERT(0 == desc.fSampleCnt); | |
145 | |
146 if (!this->caps()->npotTextureTileSupport() && | |
147 (!SkIsPow2(desc.fWidth) || !SkIsPow2(desc.fHeight))) { | |
148 return nullptr; | 148 return nullptr; |
149 } | 149 } |
150 | 150 |
151 this->handleDirtyContext(); | 151 desc.fSampleCnt = SkTMin(desc.fSampleCnt, caps->maxSampleCount()); |
152 tex = this->onCreateCompressedTexture(desc, lifeCycle, srcData); | 152 // Attempt to catch un- or wrongly intialized sample counts; |
153 } else { | 153 SkASSERT(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64); |
154 this->handleDirtyContext(); | 154 |
155 tex = this->onCreateTexture(desc, lifeCycle, srcData, rowBytes); | 155 desc.fOrigin = resolve_origin(desc.fOrigin, isRT); |
| 156 |
| 157 GrTexture* tex = nullptr; |
| 158 GrGpuResource::LifeCycle lifeCycle = budgeted ? GrGpuResource::kCached_L
ifeCycle : |
| 159 GrGpuResource::kUncached
_LifeCycle; |
| 160 |
| 161 if (GrPixelConfigIsCompressed(desc.fConfig)) { |
| 162 // We shouldn't be rendering into this |
| 163 SkASSERT(!isRT); |
| 164 SkASSERT(0 == desc.fSampleCnt); |
| 165 |
| 166 if (!caps->npotTextureTileSupport() && |
| 167 (!SkIsPow2(desc.fWidth) || !SkIsPow2(desc.fHeight))) { |
| 168 return nullptr; |
| 169 } |
| 170 |
| 171 this->handleDirtyContext(); |
| 172 tex = this->onCreateCompressedTexture(desc, lifeCycle, texels); |
| 173 } else { |
| 174 this->handleDirtyContext(); |
| 175 tex = this->onCreateTexture(desc, lifeCycle, texels); |
| 176 } |
| 177 if (tex) { |
| 178 if (!caps->reuseScratchTextures() && !isRT) { |
| 179 tex->resourcePriv().removeScratchKey(); |
| 180 } |
| 181 fStats.incTextureCreates(); |
| 182 if (!texels.empty()) { |
| 183 if (texels[0].fTexels) { |
| 184 fStats.incTextureUploads(); |
| 185 } |
| 186 } |
| 187 } |
| 188 return tex; |
156 } | 189 } |
157 if (!this->caps()->reuseScratchTextures() && !isRT) { | 190 } |
158 tex->resourcePriv().removeScratchKey(); | 191 |
| 192 GrTexture* GrGpu::createTexture(const GrSurfaceDesc& desc, bool budgeted, |
| 193 const void* srcData, size_t rowBytes) { |
| 194 const int width = desc.fWidth; |
| 195 const int height = desc.fHeight; |
| 196 if (width <= 0 || height <= 0) { |
| 197 return nullptr; |
159 } | 198 } |
160 if (tex) { | 199 const uint32_t baseLevelWidth = width; |
161 fStats.incTextureCreates(); | 200 const uint32_t baseLevelHeight = height; |
162 if (srcData) { | 201 |
163 fStats.incTextureUploads(); | 202 SkMipMapLevel level(srcData, rowBytes, baseLevelWidth, baseLevelHeight); |
164 } | 203 const int mipLevelCount = 1; |
165 } | 204 SkTArray<SkMipMapLevel> levels(mipLevelCount); |
166 return tex; | 205 levels.push_back(level); |
| 206 |
| 207 return this->createTexture(desc, budgeted, levels); |
167 } | 208 } |
168 | 209 |
169 GrTexture* GrGpu::wrapBackendTexture(const GrBackendTextureDesc& desc, GrWrapOwn
ership ownership) { | 210 GrTexture* GrGpu::wrapBackendTexture(const GrBackendTextureDesc& desc, GrWrapOwn
ership ownership) { |
170 this->handleDirtyContext(); | 211 this->handleDirtyContext(); |
171 GrTexture* tex = this->onWrapBackendTexture(desc, ownership); | 212 GrTexture* tex = this->onWrapBackendTexture(desc, ownership); |
172 if (nullptr == tex) { | 213 if (nullptr == tex) { |
173 return nullptr; | 214 return nullptr; |
174 } | 215 } |
175 // TODO: defer this and attach dynamically | 216 // TODO: defer this and attach dynamically |
176 GrRenderTarget* tgt = tex->asRenderTarget(); | 217 GrRenderTarget* tgt = tex->asRenderTarget(); |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 !this->caps()->isConfigRenderable(tempDrawInfo->fTempSurfaceDesc.fConfig
, false)) { | 295 !this->caps()->isConfigRenderable(tempDrawInfo->fTempSurfaceDesc.fConfig
, false)) { |
255 // If we don't have a fallback to a straight read then fail. | 296 // If we don't have a fallback to a straight read then fail. |
256 if (kRequireDraw_DrawPreference == *drawPreference) { | 297 if (kRequireDraw_DrawPreference == *drawPreference) { |
257 return false; | 298 return false; |
258 } | 299 } |
259 *drawPreference = kNoDraw_DrawPreference; | 300 *drawPreference = kNoDraw_DrawPreference; |
260 } | 301 } |
261 | 302 |
262 return true; | 303 return true; |
263 } | 304 } |
264 bool GrGpu::getWritePixelsInfo(GrSurface* dstSurface, int width, int height, siz
e_t rowBytes, | 305 bool GrGpu::getWritePixelsInfo(GrSurface* dstSurface, int width, int height, |
265 GrPixelConfig srcConfig, DrawPreference* drawPref
erence, | 306 GrPixelConfig srcConfig, DrawPreference* drawPref
erence, |
266 WritePixelTempDrawInfo* tempDrawInfo) { | 307 WritePixelTempDrawInfo* tempDrawInfo) { |
267 SkASSERT(drawPreference); | 308 SkASSERT(drawPreference); |
268 SkASSERT(tempDrawInfo); | 309 SkASSERT(tempDrawInfo); |
269 SkASSERT(kGpuPrefersDraw_DrawPreference != *drawPreference); | 310 SkASSERT(kGpuPrefersDraw_DrawPreference != *drawPreference); |
270 | 311 |
271 if (GrPixelConfigIsCompressed(dstSurface->desc().fConfig) && | 312 if (GrPixelConfigIsCompressed(dstSurface->desc().fConfig) && |
272 dstSurface->desc().fConfig != srcConfig) { | 313 dstSurface->desc().fConfig != srcConfig) { |
273 return false; | 314 return false; |
274 } | 315 } |
275 | 316 |
276 if (this->caps()->useDrawInsteadOfPartialRenderTargetWrite() && | 317 if (this->caps()->useDrawInsteadOfPartialRenderTargetWrite() && |
277 SkToBool(dstSurface->asRenderTarget()) && | 318 SkToBool(dstSurface->asRenderTarget()) && |
278 (width < dstSurface->width() || height < dstSurface->height())) { | 319 (width < dstSurface->width() || height < dstSurface->height())) { |
279 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference); | 320 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference); |
280 } | 321 } |
281 | 322 |
282 if (!this->onGetWritePixelsInfo(dstSurface, width, height, rowBytes, srcConf
ig, drawPreference, | 323 if (!this->onGetWritePixelsInfo(dstSurface, width, height, srcConfig, drawPr
eference, |
283 tempDrawInfo)) { | 324 tempDrawInfo)) { |
284 return false; | 325 return false; |
285 } | 326 } |
286 | 327 |
287 // Check to see if we're going to request that the caller draw when drawing
is not possible. | 328 // Check to see if we're going to request that the caller draw when drawing
is not possible. |
288 if (!dstSurface->asRenderTarget() || | 329 if (!dstSurface->asRenderTarget() || |
289 !this->caps()->isConfigTexturable(tempDrawInfo->fTempSurfaceDesc.fConfig
)) { | 330 !this->caps()->isConfigTexturable(tempDrawInfo->fTempSurfaceDesc.fConfig
)) { |
290 // If we don't have a fallback to a straight upload then fail. | 331 // If we don't have a fallback to a straight upload then fail. |
291 if (kRequireDraw_DrawPreference == *drawPreference || | 332 if (kRequireDraw_DrawPreference == *drawPreference || |
292 !this->caps()->isConfigTexturable(srcConfig)) { | 333 !this->caps()->isConfigTexturable(srcConfig)) { |
(...skipping 24 matching lines...) Expand all Loading... |
317 } | 358 } |
318 | 359 |
319 return this->onReadPixels(surface, | 360 return this->onReadPixels(surface, |
320 left, top, width, height, | 361 left, top, width, height, |
321 config, buffer, | 362 config, buffer, |
322 rowBytes); | 363 rowBytes); |
323 } | 364 } |
324 | 365 |
325 bool GrGpu::writePixels(GrSurface* surface, | 366 bool GrGpu::writePixels(GrSurface* surface, |
326 int left, int top, int width, int height, | 367 int left, int top, int width, int height, |
327 GrPixelConfig config, const void* buffer, | 368 GrPixelConfig config, const SkTArray<SkMipMapLevel>& tex
els) { |
328 size_t rowBytes) { | |
329 if (!buffer) { | |
330 return false; | |
331 } | |
332 | |
333 this->handleDirtyContext(); | 369 this->handleDirtyContext(); |
334 if (this->onWritePixels(surface, left, top, width, height, config, buffer, r
owBytes)) { | 370 if (this->onWritePixels(surface, left, top, width, height, config, texels))
{ |
335 fStats.incTextureUploads(); | 371 fStats.incTextureUploads(); |
336 return true; | 372 return true; |
337 } | 373 } |
338 return false; | 374 return false; |
339 } | 375 } |
340 | 376 |
| 377 bool GrGpu::writePixels(GrSurface* surface, |
| 378 int left, int top, int width, int height, |
| 379 GrPixelConfig config, const void* buffer, |
| 380 size_t rowBytes) { |
| 381 if (width <= 0 || height <= 0) { |
| 382 return false; |
| 383 } |
| 384 const uint32_t baseLevelWidth = width; |
| 385 const uint32_t baseLevelHeight = height; |
| 386 |
| 387 SkMipMapLevel mipLevel(buffer, rowBytes, baseLevelWidth, baseLevelHeight); |
| 388 const int mipLevelCount = 1; |
| 389 SkTArray<SkMipMapLevel> texels(mipLevelCount); |
| 390 texels.push_back(mipLevel); |
| 391 |
| 392 return this->writePixels(surface, left, top, width, height, config, texels); |
| 393 } |
| 394 |
341 void GrGpu::resolveRenderTarget(GrRenderTarget* target) { | 395 void GrGpu::resolveRenderTarget(GrRenderTarget* target) { |
342 SkASSERT(target); | 396 SkASSERT(target); |
343 this->handleDirtyContext(); | 397 this->handleDirtyContext(); |
344 this->onResolveRenderTarget(target); | 398 this->onResolveRenderTarget(target); |
345 } | 399 } |
346 | 400 |
347 //////////////////////////////////////////////////////////////////////////////// | 401 //////////////////////////////////////////////////////////////////////////////// |
348 | 402 |
349 void GrGpu::draw(const DrawArgs& args, const GrVertices& vertices) { | 403 void GrGpu::draw(const DrawArgs& args, const GrVertices& vertices) { |
350 this->handleDirtyContext(); | 404 this->handleDirtyContext(); |
351 if (GrXferBarrierType barrierType = args.fPipeline->xferBarrierType(*this->c
aps())) { | 405 if (GrXferBarrierType barrierType = args.fPipeline->xferBarrierType(*this->c
aps())) { |
352 this->xferBarrier(args.fPipeline->getRenderTarget(), barrierType); | 406 this->xferBarrier(args.fPipeline->getRenderTarget(), barrierType); |
353 } | 407 } |
354 | 408 |
355 GrVertices::Iterator iter; | 409 GrVertices::Iterator iter; |
356 const GrNonInstancedVertices* verts = iter.init(vertices); | 410 const GrNonInstancedVertices* verts = iter.init(vertices); |
357 do { | 411 do { |
358 this->onDraw(args, *verts); | 412 this->onDraw(args, *verts); |
359 fStats.incNumDraws(); | 413 fStats.incNumDraws(); |
360 } while ((verts = iter.next())); | 414 } while ((verts = iter.next())); |
361 } | 415 } |
OLD | NEW |