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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 // By default, GrRenderTargets are GL's normal orientation so that they | 58 // By default, GrRenderTargets are GL's normal orientation so that they |
60 // can be drawn to by the outside world without the client having | 59 // can be drawn to by the outside world without the client having |
61 // to render upside down. | 60 // to render upside down. |
62 if (kDefault_GrSurfaceOrigin == origin) { | 61 if (kDefault_GrSurfaceOrigin == origin) { |
63 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOr
igin; | 62 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOr
igin; |
64 } else { | 63 } else { |
65 return origin; | 64 return origin; |
66 } | 65 } |
67 } | 66 } |
68 | 67 |
69 GrTexture* GrGpu::createTexture(const GrSurfaceDesc& origDesc, bool budgeted, | 68 /** |
70 const void* srcData, size_t rowBytes) { | 69 * Prior to creating a texture, make sure the type of texture being created is |
71 GrSurfaceDesc desc = origDesc; | 70 * supported by calling check_texture_creation_params. |
72 | 71 * |
73 if (!this->caps()->isConfigTexturable(desc.fConfig)) { | 72 * @param caps The capabilities of the GL device. |
74 return nullptr; | 73 * @param desc The descriptor of the texture to create. |
| 74 * @param isRT Indicates if the texture can be a render target. |
| 75 */ |
| 76 static bool check_texture_creation_params(const GrCaps& caps, const GrSurfaceDes
c& desc, |
| 77 bool* isRT) { |
| 78 if (!caps.isConfigTexturable(desc.fConfig)) { |
| 79 return false; |
75 } | 80 } |
76 | 81 |
77 bool isRT = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag); | 82 *isRT = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag); |
78 if (isRT && !this->caps()->isConfigRenderable(desc.fConfig, desc.fSampleCnt
> 0)) { | 83 if (*isRT && !caps.isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) { |
79 return nullptr; | 84 return false; |
80 } | 85 } |
81 | 86 |
82 // We currently do not support multisampled textures | 87 // We currently do not support multisampled textures |
83 if (!isRT && desc.fSampleCnt > 0) { | 88 if (!*isRT && desc.fSampleCnt > 0) { |
84 return nullptr; | 89 return false; |
85 } | 90 } |
86 | 91 |
87 GrTexture *tex = nullptr; | 92 if (*isRT) { |
88 | 93 int maxRTSize = caps.maxRenderTargetSize(); |
89 if (isRT) { | |
90 int maxRTSize = this->caps()->maxRenderTargetSize(); | |
91 if (desc.fWidth > maxRTSize || desc.fHeight > maxRTSize) { | 94 if (desc.fWidth > maxRTSize || desc.fHeight > maxRTSize) { |
92 return nullptr; | 95 return false; |
93 } | 96 } |
94 } else { | 97 } else { |
95 int maxSize = this->caps()->maxTextureSize(); | 98 int maxSize = caps.maxTextureSize(); |
96 if (desc.fWidth > maxSize || desc.fHeight > maxSize) { | 99 if (desc.fWidth > maxSize || desc.fHeight > maxSize) { |
97 return nullptr; | 100 return false; |
98 } | 101 } |
99 } | 102 } |
| 103 return true; |
| 104 } |
100 | 105 |
101 GrGpuResource::LifeCycle lifeCycle = budgeted ? GrGpuResource::kCached_LifeC
ycle : | 106 GrTexture* GrGpu::createTexture(const GrSurfaceDesc& origDesc, bool budgeted, |
102 GrGpuResource::kUncached_Lif
eCycle; | 107 const SkTArray<SkMipMapLevel>& texels) { |
| 108 GrSurfaceDesc desc = origDesc; |
103 | 109 |
104 desc.fSampleCnt = SkTMin(desc.fSampleCnt, this->caps()->maxSampleCount()); | 110 const GrCaps* caps = this->caps(); |
105 // Attempt to catch un- or wrongly initialized sample counts; | 111 if (!caps) { |
106 SkASSERT(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64); | 112 return nullptr; |
107 | 113 } else { |
108 desc.fOrigin = resolve_origin(desc.fOrigin, isRT); | 114 bool isRT = false; |
109 | 115 bool textureCreationParamsValid = check_texture_creation_params(*caps, d
esc, &isRT); |
110 if (GrPixelConfigIsCompressed(desc.fConfig)) { | 116 if (!textureCreationParamsValid) { |
111 // We shouldn't be rendering into this | |
112 SkASSERT(!isRT); | |
113 SkASSERT(0 == desc.fSampleCnt); | |
114 | |
115 if (!this->caps()->npotTextureTileSupport() && | |
116 (!SkIsPow2(desc.fWidth) || !SkIsPow2(desc.fHeight))) { | |
117 return nullptr; | 117 return nullptr; |
118 } | 118 } |
119 | 119 |
120 this->handleDirtyContext(); | 120 desc.fSampleCnt = SkTMin(desc.fSampleCnt, caps->maxSampleCount()); |
121 tex = this->onCreateCompressedTexture(desc, lifeCycle, srcData); | 121 // Attempt to catch un- or wrongly intialized sample counts; |
122 } else { | 122 SkASSERT(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64); |
123 this->handleDirtyContext(); | 123 |
124 tex = this->onCreateTexture(desc, lifeCycle, srcData, rowBytes); | 124 desc.fOrigin = resolve_origin(desc.fOrigin, isRT); |
| 125 |
| 126 GrTexture* tex = nullptr; |
| 127 GrGpuResource::LifeCycle lifeCycle = budgeted ? GrGpuResource::kCached_L
ifeCycle : |
| 128 GrGpuResource::kUncached
_LifeCycle; |
| 129 |
| 130 if (GrPixelConfigIsCompressed(desc.fConfig)) { |
| 131 // We shouldn't be rendering into this |
| 132 SkASSERT(!isRT); |
| 133 SkASSERT(0 == desc.fSampleCnt); |
| 134 |
| 135 if (!caps->npotTextureTileSupport() && |
| 136 (!SkIsPow2(desc.fWidth) || !SkIsPow2(desc.fHeight))) { |
| 137 return nullptr; |
| 138 } |
| 139 |
| 140 this->handleDirtyContext(); |
| 141 tex = this->onCreateCompressedTexture(desc, lifeCycle, texels); |
| 142 } else { |
| 143 this->handleDirtyContext(); |
| 144 tex = this->onCreateTexture(desc, lifeCycle, texels); |
| 145 } |
| 146 if (tex) { |
| 147 if (!caps->reuseScratchTextures() && !isRT) { |
| 148 tex->resourcePriv().removeScratchKey(); |
| 149 } |
| 150 fStats.incTextureCreates(); |
| 151 if (!texels.empty()) { |
| 152 if (texels[0].fTexels) { |
| 153 fStats.incTextureUploads(); |
| 154 } |
| 155 } |
| 156 } |
| 157 return tex; |
125 } | 158 } |
126 if (!this->caps()->reuseScratchTextures() && !isRT) { | 159 } |
127 tex->resourcePriv().removeScratchKey(); | 160 |
128 } | 161 GrTexture* GrGpu::createTexture(const GrSurfaceDesc& desc, bool budgeted, |
129 if (tex) { | 162 const void* srcData, size_t rowBytes) { |
130 fStats.incTextureCreates(); | 163 SkMipMapLevel level(srcData, rowBytes); |
131 if (srcData) { | 164 |
132 fStats.incTextureUploads(); | 165 const int mipLevelCount = 1; |
133 } | 166 SkTArray<SkMipMapLevel> levels(mipLevelCount); |
134 } | 167 levels.push_back(level); |
135 return tex; | 168 |
| 169 return this->createTexture(desc, budgeted, levels); |
136 } | 170 } |
137 | 171 |
138 GrTexture* GrGpu::wrapBackendTexture(const GrBackendTextureDesc& desc, GrWrapOwn
ership ownership) { | 172 GrTexture* GrGpu::wrapBackendTexture(const GrBackendTextureDesc& desc, GrWrapOwn
ership ownership) { |
139 this->handleDirtyContext(); | 173 this->handleDirtyContext(); |
140 GrTexture* tex = this->onWrapBackendTexture(desc, ownership); | 174 GrTexture* tex = this->onWrapBackendTexture(desc, ownership); |
141 if (nullptr == tex) { | 175 if (nullptr == tex) { |
142 return nullptr; | 176 return nullptr; |
143 } | 177 } |
144 // TODO: defer this and attach dynamically | 178 // TODO: defer this and attach dynamically |
145 GrRenderTarget* tgt = tex->asRenderTarget(); | 179 GrRenderTarget* tgt = tex->asRenderTarget(); |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 !this->caps()->isConfigRenderable(tempDrawInfo->fTempSurfaceDesc.fConfig
, false)) { | 257 !this->caps()->isConfigRenderable(tempDrawInfo->fTempSurfaceDesc.fConfig
, false)) { |
224 // If we don't have a fallback to a straight read then fail. | 258 // If we don't have a fallback to a straight read then fail. |
225 if (kRequireDraw_DrawPreference == *drawPreference) { | 259 if (kRequireDraw_DrawPreference == *drawPreference) { |
226 return false; | 260 return false; |
227 } | 261 } |
228 *drawPreference = kNoDraw_DrawPreference; | 262 *drawPreference = kNoDraw_DrawPreference; |
229 } | 263 } |
230 | 264 |
231 return true; | 265 return true; |
232 } | 266 } |
233 bool GrGpu::getWritePixelsInfo(GrSurface* dstSurface, int width, int height, siz
e_t rowBytes, | 267 bool GrGpu::getWritePixelsInfo(GrSurface* dstSurface, int width, int height, |
234 GrPixelConfig srcConfig, DrawPreference* drawPref
erence, | 268 GrPixelConfig srcConfig, DrawPreference* drawPref
erence, |
235 WritePixelTempDrawInfo* tempDrawInfo) { | 269 WritePixelTempDrawInfo* tempDrawInfo) { |
236 SkASSERT(drawPreference); | 270 SkASSERT(drawPreference); |
237 SkASSERT(tempDrawInfo); | 271 SkASSERT(tempDrawInfo); |
238 SkASSERT(kGpuPrefersDraw_DrawPreference != *drawPreference); | 272 SkASSERT(kGpuPrefersDraw_DrawPreference != *drawPreference); |
239 | 273 |
240 if (GrPixelConfigIsCompressed(dstSurface->desc().fConfig) && | 274 if (GrPixelConfigIsCompressed(dstSurface->desc().fConfig) && |
241 dstSurface->desc().fConfig != srcConfig) { | 275 dstSurface->desc().fConfig != srcConfig) { |
242 return false; | 276 return false; |
243 } | 277 } |
244 | 278 |
245 if (this->caps()->useDrawInsteadOfPartialRenderTargetWrite() && | 279 if (this->caps()->useDrawInsteadOfPartialRenderTargetWrite() && |
246 SkToBool(dstSurface->asRenderTarget()) && | 280 SkToBool(dstSurface->asRenderTarget()) && |
247 (width < dstSurface->width() || height < dstSurface->height())) { | 281 (width < dstSurface->width() || height < dstSurface->height())) { |
248 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference); | 282 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference); |
249 } | 283 } |
250 | 284 |
251 if (!this->onGetWritePixelsInfo(dstSurface, width, height, rowBytes, srcConf
ig, drawPreference, | 285 if (!this->onGetWritePixelsInfo(dstSurface, width, height, srcConfig, drawPr
eference, |
252 tempDrawInfo)) { | 286 tempDrawInfo)) { |
253 return false; | 287 return false; |
254 } | 288 } |
255 | 289 |
256 // Check to see if we're going to request that the caller draw when drawing
is not possible. | 290 // Check to see if we're going to request that the caller draw when drawing
is not possible. |
257 if (!dstSurface->asRenderTarget() || | 291 if (!dstSurface->asRenderTarget() || |
258 !this->caps()->isConfigTexturable(tempDrawInfo->fTempSurfaceDesc.fConfig
)) { | 292 !this->caps()->isConfigTexturable(tempDrawInfo->fTempSurfaceDesc.fConfig
)) { |
259 // If we don't have a fallback to a straight upload then fail. | 293 // If we don't have a fallback to a straight upload then fail. |
260 if (kRequireDraw_DrawPreference == *drawPreference || | 294 if (kRequireDraw_DrawPreference == *drawPreference || |
261 !this->caps()->isConfigTexturable(srcConfig)) { | 295 !this->caps()->isConfigTexturable(srcConfig)) { |
(...skipping 24 matching lines...) Expand all Loading... |
286 } | 320 } |
287 | 321 |
288 return this->onReadPixels(surface, | 322 return this->onReadPixels(surface, |
289 left, top, width, height, | 323 left, top, width, height, |
290 config, buffer, | 324 config, buffer, |
291 rowBytes); | 325 rowBytes); |
292 } | 326 } |
293 | 327 |
294 bool GrGpu::writePixels(GrSurface* surface, | 328 bool GrGpu::writePixels(GrSurface* surface, |
295 int left, int top, int width, int height, | 329 int left, int top, int width, int height, |
296 GrPixelConfig config, const void* buffer, | 330 GrPixelConfig config, const SkTArray<SkMipMapLevel>& tex
els) { |
297 size_t rowBytes) { | |
298 if (!buffer) { | |
299 return false; | |
300 } | |
301 | |
302 this->handleDirtyContext(); | 331 this->handleDirtyContext(); |
303 if (this->onWritePixels(surface, left, top, width, height, config, buffer, r
owBytes)) { | 332 if (this->onWritePixels(surface, left, top, width, height, config, texels))
{ |
304 fStats.incTextureUploads(); | 333 fStats.incTextureUploads(); |
305 return true; | 334 return true; |
306 } | 335 } |
307 return false; | 336 return false; |
308 } | 337 } |
309 | 338 |
| 339 bool GrGpu::writePixels(GrSurface* surface, |
| 340 int left, int top, int width, int height, |
| 341 GrPixelConfig config, const void* buffer, |
| 342 size_t rowBytes) { |
| 343 SkMipMapLevel mipLevel(buffer, rowBytes); |
| 344 SkTArray<SkMipMapLevel> texels; |
| 345 texels.push_back(mipLevel); |
| 346 return this->writePixels(surface, left, top, width, height, config, texels); |
| 347 } |
| 348 |
310 void GrGpu::resolveRenderTarget(GrRenderTarget* target) { | 349 void GrGpu::resolveRenderTarget(GrRenderTarget* target) { |
311 SkASSERT(target); | 350 SkASSERT(target); |
312 this->handleDirtyContext(); | 351 this->handleDirtyContext(); |
313 this->onResolveRenderTarget(target); | 352 this->onResolveRenderTarget(target); |
314 } | 353 } |
315 | 354 |
316 //////////////////////////////////////////////////////////////////////////////// | 355 //////////////////////////////////////////////////////////////////////////////// |
317 | 356 |
318 void GrGpu::draw(const DrawArgs& args, const GrVertices& vertices) { | 357 void GrGpu::draw(const DrawArgs& args, const GrVertices& vertices) { |
319 this->handleDirtyContext(); | 358 this->handleDirtyContext(); |
320 if (GrXferBarrierType barrierType = args.fPipeline->xferBarrierType(*this->c
aps())) { | 359 if (GrXferBarrierType barrierType = args.fPipeline->xferBarrierType(*this->c
aps())) { |
321 this->xferBarrier(args.fPipeline->getRenderTarget(), barrierType); | 360 this->xferBarrier(args.fPipeline->getRenderTarget(), barrierType); |
322 } | 361 } |
323 | 362 |
324 GrVertices::Iterator iter; | 363 GrVertices::Iterator iter; |
325 const GrNonInstancedVertices* verts = iter.init(vertices); | 364 const GrNonInstancedVertices* verts = iter.init(vertices); |
326 do { | 365 do { |
327 this->onDraw(args, *verts); | 366 this->onDraw(args, *verts); |
328 fStats.incNumDraws(); | 367 fStats.incNumDraws(); |
329 } while ((verts = iter.next())); | 368 } while ((verts = iter.next())); |
330 } | 369 } |
OLD | NEW |