blob: 48fc740ae0d9697059c7356ff8f3e1bc6e4ceca6 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2010 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@google.comac10a2d2010-12-22 21:39:39 +000010#include "GrGpu.h"
bsalomon@google.com558a75b2011-08-08 17:01:14 +000011
bsalomoneb1cb5c2015-05-22 08:01:09 -070012#include "GrCaps.h"
bsalomon@google.com558a75b2011-08-08 17:01:14 +000013#include "GrContext.h"
bsalomon3582d3e2015-02-13 14:20:05 -080014#include "GrGpuResourcePriv.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000015#include "GrIndexBuffer.h"
kkinnunencabe20c2015-06-01 01:37:26 -070016#include "GrPathRendering.h"
bsalomoncb02b382015-08-12 11:14:50 -070017#include "GrPipeline.h"
bsalomon0ea80f42015-02-11 10:49:59 -080018#include "GrResourceCache.h"
egdanielec00d942015-09-14 12:56:10 -070019#include "GrResourceProvider.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080020#include "GrRenderTargetPriv.h"
egdaniel8dc7c3a2015-04-16 11:22:42 -070021#include "GrStencilAttachment.h"
egdaniel6d901da2015-07-30 12:02:15 -070022#include "GrSurfacePriv.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000023#include "GrVertexBuffer.h"
bsalomoncb8979d2015-05-05 09:51:38 -070024#include "GrVertices.h"
25
26GrVertices& GrVertices::operator =(const GrVertices& di) {
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;
bsalomone64eb572015-05-07 11:35:55 -070036 fMaxInstancesPerDraw = di.fMaxInstancesPerDraw;
bsalomoncb8979d2015-05-05 09:51:38 -070037
38 fVertexBuffer.reset(di.vertexBuffer());
39 fIndexBuffer.reset(di.indexBuffer());
40
41 return *this;
42}
bsalomon@google.com1c13c962011-02-14 16:51:21 +000043
bsalomon@google.comd302f142011-03-03 13:54:13 +000044////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000045
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000046GrGpu::GrGpu(GrContext* context)
joshualitt3322fa42014-11-07 08:48:51 -080047 : fResetTimestamp(kExpiredTimestamp+1)
bsalomon@google.com0a208a12013-06-28 18:57:35 +000048 , fResetBits(kAll_GrBackendState)
joshualitt3322fa42014-11-07 08:48:51 -080049 , fContext(context) {
reed@google.comac10a2d2010-12-22 21:39:39 +000050}
51
bsalomoned0bcad2015-05-04 10:36:42 -070052GrGpu::~GrGpu() {}
bsalomon1d89ddc2014-08-19 14:20:58 -070053
robertphillipse3371302014-09-17 06:01:06 -070054void GrGpu::contextAbandoned() {}
reed@google.comac10a2d2010-12-22 21:39:39 +000055
bsalomon@google.comd302f142011-03-03 13:54:13 +000056////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000057
bsalomon045802d2015-10-20 07:58:01 -070058bool GrGpu::makeCopyForTextureParams(int width, int height, const GrTextureParams& textureParams,
59 GrTextureParamsAdjuster::CopyParams* copyParams) const {
60 bool doCopy = false;
61 const GrCaps& caps = *this->caps();
62 if (textureParams.isTiled() && !caps.npotTextureTileSupport() &&
63 (!SkIsPow2(width) || !SkIsPow2(height))) {
64 doCopy = true;
65 copyParams->fWidth = GrNextPow2(SkTMax(width, caps.minTextureSize()));
66 copyParams->fHeight = GrNextPow2(SkTMax(height, caps.minTextureSize()));
67 } else if (width < caps.minTextureSize() || height < caps.minTextureSize()) {
68 // The small texture issues appear to be with tiling. Hence it seems ok to scale
69 // them up using the GPU. If issues persist we may need to CPU-stretch.
70 doCopy = true;
71 copyParams->fWidth = SkTMax(width, caps.minTextureSize());
72 copyParams->fHeight = SkTMax(height, caps.minTextureSize());
73 }
74 if (doCopy) {
75 switch (textureParams.filterMode()) {
76 case GrTextureParams::kNone_FilterMode:
77 copyParams->fFilter = GrTextureParams::kNone_FilterMode;
78 break;
79 case GrTextureParams::kBilerp_FilterMode:
80 case GrTextureParams::kMipMap_FilterMode:
81 // We are only ever scaling up so no reason to ever indicate kMipMap.
82 copyParams->fFilter = GrTextureParams::kBilerp_FilterMode;
83 break;
84 }
85 }
86 return doCopy;
87}
88
egdanielcf614fd2015-04-22 13:58:58 -070089static GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) {
egdanielb0e1be22015-04-22 13:27:39 -070090 // By default, GrRenderTargets are GL's normal orientation so that they
91 // can be drawn to by the outside world without the client having
92 // to render upside down.
93 if (kDefault_GrSurfaceOrigin == origin) {
94 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
95 } else {
96 return origin;
97 }
98}
99
egdanielb0e1be22015-04-22 13:27:39 -0700100GrTexture* GrGpu::createTexture(const GrSurfaceDesc& origDesc, bool budgeted,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000101 const void* srcData, size_t rowBytes) {
egdanielb0e1be22015-04-22 13:27:39 -0700102 GrSurfaceDesc desc = origDesc;
103
krajcevski9c0e6292014-06-02 07:38:14 -0700104 if (!this->caps()->isConfigTexturable(desc.fConfig)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700105 return nullptr;
robertphillips@google.comd3eb3362012-10-31 13:56:35 +0000106 }
krajcevski9c0e6292014-06-02 07:38:14 -0700107
bsalomondb558dd2015-01-23 13:19:00 -0800108 bool isRT = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
109 if (isRT && !this->caps()->isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700110 return nullptr;
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +0000111 }
robertphillips@google.comd3eb3362012-10-31 13:56:35 +0000112
robertphillips6e83ac72015-08-13 05:19:14 -0700113 // We currently do not support multisampled textures
egdaniel8c9b6f12015-05-12 13:36:30 -0700114 if (!isRT && desc.fSampleCnt > 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700115 return nullptr;
egdaniel8c9b6f12015-05-12 13:36:30 -0700116 }
117
halcanary96fcdcc2015-08-27 07:41:13 -0700118 GrTexture *tex = nullptr;
egdanielb0e1be22015-04-22 13:27:39 -0700119
120 if (isRT) {
121 int maxRTSize = this->caps()->maxRenderTargetSize();
122 if (desc.fWidth > maxRTSize || desc.fHeight > maxRTSize) {
halcanary96fcdcc2015-08-27 07:41:13 -0700123 return nullptr;
egdanielb0e1be22015-04-22 13:27:39 -0700124 }
125 } else {
126 int maxSize = this->caps()->maxTextureSize();
127 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
halcanary96fcdcc2015-08-27 07:41:13 -0700128 return nullptr;
egdanielb0e1be22015-04-22 13:27:39 -0700129 }
130 }
131
132 GrGpuResource::LifeCycle lifeCycle = budgeted ? GrGpuResource::kCached_LifeCycle :
133 GrGpuResource::kUncached_LifeCycle;
134
135 desc.fSampleCnt = SkTMin(desc.fSampleCnt, this->caps()->maxSampleCount());
136 // Attempt to catch un- or wrongly initialized sample counts;
137 SkASSERT(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
138
139 desc.fOrigin = resolve_origin(desc.fOrigin, isRT);
140
krajcevski9c0e6292014-06-02 07:38:14 -0700141 if (GrPixelConfigIsCompressed(desc.fConfig)) {
142 // We shouldn't be rendering into this
egdanielb0e1be22015-04-22 13:27:39 -0700143 SkASSERT(!isRT);
144 SkASSERT(0 == desc.fSampleCnt);
krajcevski9c0e6292014-06-02 07:38:14 -0700145
146 if (!this->caps()->npotTextureTileSupport() &&
tfarinaf9dae782014-06-06 06:35:28 -0700147 (!SkIsPow2(desc.fWidth) || !SkIsPow2(desc.fHeight))) {
halcanary96fcdcc2015-08-27 07:41:13 -0700148 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000149 }
tfarinaf9dae782014-06-06 06:35:28 -0700150
krajcevski9c0e6292014-06-02 07:38:14 -0700151 this->handleDirtyContext();
egdanielb0e1be22015-04-22 13:27:39 -0700152 tex = this->onCreateCompressedTexture(desc, lifeCycle, srcData);
krajcevski9c0e6292014-06-02 07:38:14 -0700153 } else {
154 this->handleDirtyContext();
egdanielb0e1be22015-04-22 13:27:39 -0700155 tex = this->onCreateTexture(desc, lifeCycle, srcData, rowBytes);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000156 }
bsalomondb558dd2015-01-23 13:19:00 -0800157 if (!this->caps()->reuseScratchTextures() && !isRT) {
bsalomon3582d3e2015-02-13 14:20:05 -0800158 tex->resourcePriv().removeScratchKey();
bsalomondb558dd2015-01-23 13:19:00 -0800159 }
bsalomonb12ea412015-02-02 21:19:50 -0800160 if (tex) {
161 fStats.incTextureCreates();
162 if (srcData) {
163 fStats.incTextureUploads();
164 }
165 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000166 return tex;
167}
168
bsalomon6dc6f5f2015-06-18 09:12:16 -0700169GrTexture* GrGpu::wrapBackendTexture(const GrBackendTextureDesc& desc, GrWrapOwnership ownership) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000170 this->handleDirtyContext();
bsalomon6dc6f5f2015-06-18 09:12:16 -0700171 GrTexture* tex = this->onWrapBackendTexture(desc, ownership);
halcanary96fcdcc2015-08-27 07:41:13 -0700172 if (nullptr == tex) {
173 return nullptr;
bsalomon@google.coma14dd6d2012-01-03 21:08:12 +0000174 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000175 // TODO: defer this and attach dynamically
176 GrRenderTarget* tgt = tex->asRenderTarget();
egdanielec00d942015-09-14 12:56:10 -0700177 if (tgt && !fContext->resourceProvider()->attachStencilAttachment(tgt)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000178 tex->unref();
halcanary96fcdcc2015-08-27 07:41:13 -0700179 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000180 } else {
181 return tex;
182 }
183}
184
bsalomon6dc6f5f2015-06-18 09:12:16 -0700185GrRenderTarget* GrGpu::wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc,
186 GrWrapOwnership ownership) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000187 this->handleDirtyContext();
bsalomon6dc6f5f2015-06-18 09:12:16 -0700188 return this->onWrapBackendRenderTarget(desc, ownership);
bsalomon@google.come269f212011-11-07 13:29:52 +0000189}
190
robertphillips@google.comadacc702013-10-14 21:53:24 +0000191GrVertexBuffer* GrGpu::createVertexBuffer(size_t size, bool dynamic) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000192 this->handleDirtyContext();
robertphillips1b8e1b52015-06-24 06:54:10 -0700193 GrVertexBuffer* vb = this->onCreateVertexBuffer(size, dynamic);
194 if (!this->caps()->reuseScratchBuffers()) {
195 vb->resourcePriv().removeScratchKey();
196 }
197 return vb;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000198}
199
robertphillips@google.comadacc702013-10-14 21:53:24 +0000200GrIndexBuffer* GrGpu::createIndexBuffer(size_t size, bool dynamic) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000201 this->handleDirtyContext();
robertphillips1b8e1b52015-06-24 06:54:10 -0700202 GrIndexBuffer* ib = this->onCreateIndexBuffer(size, dynamic);
203 if (!this->caps()->reuseScratchBuffers()) {
204 ib->resourcePriv().removeScratchKey();
205 }
206 return ib;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000207}
208
egdaniel51c8d402015-08-06 10:54:13 -0700209void GrGpu::clear(const SkIRect& rect,
joshualitt3322fa42014-11-07 08:48:51 -0800210 GrColor color,
joshualitt3322fa42014-11-07 08:48:51 -0800211 GrRenderTarget* renderTarget) {
bsalomon89c62982014-11-03 12:08:42 -0800212 SkASSERT(renderTarget);
egdaniel51c8d402015-08-06 10:54:13 -0700213 SkASSERT(SkIRect::MakeWH(renderTarget->width(), renderTarget->height()).contains(rect));
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000214 this->handleDirtyContext();
egdaniel51c8d402015-08-06 10:54:13 -0700215 this->onClear(renderTarget, rect, color);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000216}
217
joshualitt6db519c2014-10-29 08:48:18 -0700218void GrGpu::clearStencilClip(const SkIRect& rect,
219 bool insideClip,
220 GrRenderTarget* renderTarget) {
joshualittd53a8272014-11-10 16:03:14 -0800221 SkASSERT(renderTarget);
joshualitt6db519c2014-10-29 08:48:18 -0700222 this->handleDirtyContext();
223 this->onClearStencilClip(renderTarget, rect, insideClip);
224}
225
joshualitt1cbdcde2015-08-21 11:53:29 -0700226bool GrGpu::copySurface(GrSurface* dst,
227 GrSurface* src,
228 const SkIRect& srcRect,
229 const SkIPoint& dstPoint) {
230 SkASSERT(dst && src);
231 this->handleDirtyContext();
232 return this->onCopySurface(dst, src, srcRect, dstPoint);
233}
234
bsalomonf0674512015-07-28 13:26:15 -0700235bool GrGpu::getReadPixelsInfo(GrSurface* srcSurface, int width, int height, size_t rowBytes,
236 GrPixelConfig readConfig, DrawPreference* drawPreference,
237 ReadPixelTempDrawInfo* tempDrawInfo) {
238 SkASSERT(drawPreference);
239 SkASSERT(tempDrawInfo);
240 SkASSERT(kGpuPrefersDraw_DrawPreference != *drawPreference);
241
egdaniel6d901da2015-07-30 12:02:15 -0700242 // We currently do not support reading into a compressed buffer
243 if (GrPixelConfigIsCompressed(readConfig)) {
244 return false;
245 }
246
bsalomonf0674512015-07-28 13:26:15 -0700247 if (!this->onGetReadPixelsInfo(srcSurface, width, height, rowBytes, readConfig, drawPreference,
248 tempDrawInfo)) {
249 return false;
250 }
251
252 // Check to see if we're going to request that the caller draw when drawing is not possible.
253 if (!srcSurface->asTexture() ||
254 !this->caps()->isConfigRenderable(tempDrawInfo->fTempSurfaceDesc.fConfig, false)) {
255 // If we don't have a fallback to a straight read then fail.
256 if (kRequireDraw_DrawPreference == *drawPreference) {
257 return false;
258 }
259 *drawPreference = kNoDraw_DrawPreference;
260 }
261
262 return true;
263}
264bool GrGpu::getWritePixelsInfo(GrSurface* dstSurface, int width, int height, size_t rowBytes,
265 GrPixelConfig srcConfig, DrawPreference* drawPreference,
266 WritePixelTempDrawInfo* tempDrawInfo) {
267 SkASSERT(drawPreference);
268 SkASSERT(tempDrawInfo);
269 SkASSERT(kGpuPrefersDraw_DrawPreference != *drawPreference);
270
jvanverth2dc29942015-09-01 07:16:46 -0700271 if (GrPixelConfigIsCompressed(dstSurface->desc().fConfig) &&
272 dstSurface->desc().fConfig != srcConfig) {
273 return false;
274 }
275
bsalomonf0674512015-07-28 13:26:15 -0700276 if (this->caps()->useDrawInsteadOfPartialRenderTargetWrite() &&
277 SkToBool(dstSurface->asRenderTarget()) &&
278 (width < dstSurface->width() || height < dstSurface->height())) {
279 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
280 }
281
282 if (!this->onGetWritePixelsInfo(dstSurface, width, height, rowBytes, srcConfig, drawPreference,
283 tempDrawInfo)) {
284 return false;
285 }
286
287 // Check to see if we're going to request that the caller draw when drawing is not possible.
288 if (!dstSurface->asRenderTarget() ||
289 !this->caps()->isConfigTexturable(tempDrawInfo->fTempSurfaceDesc.fConfig)) {
290 // If we don't have a fallback to a straight upload then fail.
291 if (kRequireDraw_DrawPreference == *drawPreference ||
292 !this->caps()->isConfigTexturable(srcConfig)) {
293 return false;
294 }
295 *drawPreference = kNoDraw_DrawPreference;
296 }
297 return true;
298}
299
bsalomon6cb3cbe2015-07-30 07:34:27 -0700300bool GrGpu::readPixels(GrSurface* surface,
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000301 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000302 GrPixelConfig config, void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000303 size_t rowBytes) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000304 this->handleDirtyContext();
egdaniel6d901da2015-07-30 12:02:15 -0700305
306 // We cannot read pixels into a compressed buffer
307 if (GrPixelConfigIsCompressed(config)) {
308 return false;
309 }
310
311 size_t bpp = GrBytesPerPixel(config);
312 if (!GrSurfacePriv::AdjustReadPixelParams(surface->width(), surface->height(), bpp,
313 &left, &top, &width, &height,
314 &buffer,
315 &rowBytes)) {
316 return false;
317 }
318
319 return this->onReadPixels(surface,
320 left, top, width, height,
321 config, buffer,
322 rowBytes);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000323}
324
bsalomon6cb3cbe2015-07-30 07:34:27 -0700325bool GrGpu::writePixels(GrSurface* surface,
326 int left, int top, int width, int height,
327 GrPixelConfig config, const void* buffer,
328 size_t rowBytes) {
jvanverth2dc29942015-09-01 07:16:46 -0700329 if (!buffer) {
330 return false;
331 }
332
bsalomon@google.com6f379512011-11-16 20:36:03 +0000333 this->handleDirtyContext();
bsalomon6cb3cbe2015-07-30 07:34:27 -0700334 if (this->onWritePixels(surface, left, top, width, height, config, buffer, rowBytes)) {
bsalomonb12ea412015-02-02 21:19:50 -0800335 fStats.incTextureUploads();
336 return true;
337 }
338 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000339}
340
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000341void GrGpu::resolveRenderTarget(GrRenderTarget* target) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000342 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000343 this->handleDirtyContext();
344 this->onResolveRenderTarget(target);
345}
346
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000347////////////////////////////////////////////////////////////////////////////////
348
bsalomoncb8979d2015-05-05 09:51:38 -0700349void GrGpu::draw(const DrawArgs& args, const GrVertices& vertices) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000350 this->handleDirtyContext();
bsalomoncb02b382015-08-12 11:14:50 -0700351 if (GrXferBarrierType barrierType = args.fPipeline->xferBarrierType(*this->caps())) {
352 this->xferBarrier(args.fPipeline->getRenderTarget(), barrierType);
353 }
354
bsalomone64eb572015-05-07 11:35:55 -0700355 GrVertices::Iterator iter;
356 const GrNonInstancedVertices* verts = iter.init(vertices);
357 do {
358 this->onDraw(args, *verts);
joshualitt336cda32015-09-09 08:29:47 -0700359 fStats.incNumDraws();
bsalomone64eb572015-05-07 11:35:55 -0700360 } while ((verts = iter.next()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000361}