blob: a9b283f3cec8a836a743a2078bd5d6a95da31ada [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"
jvanverth73063dc2015-12-03 09:15:47 -080023#include "GrTransferBuffer.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000024#include "GrVertexBuffer.h"
bsalomoncb8979d2015-05-05 09:51:38 -070025#include "GrVertices.h"
26
27GrVertices& GrVertices::operator =(const GrVertices& di) {
28 fPrimitiveType = di.fPrimitiveType;
29 fStartVertex = di.fStartVertex;
30 fStartIndex = di.fStartIndex;
31 fVertexCount = di.fVertexCount;
32 fIndexCount = di.fIndexCount;
33
34 fInstanceCount = di.fInstanceCount;
35 fVerticesPerInstance = di.fVerticesPerInstance;
36 fIndicesPerInstance = di.fIndicesPerInstance;
bsalomone64eb572015-05-07 11:35:55 -070037 fMaxInstancesPerDraw = di.fMaxInstancesPerDraw;
bsalomoncb8979d2015-05-05 09:51:38 -070038
39 fVertexBuffer.reset(di.vertexBuffer());
40 fIndexBuffer.reset(di.indexBuffer());
41
42 return *this;
43}
bsalomon@google.com1c13c962011-02-14 16:51:21 +000044
bsalomon@google.comd302f142011-03-03 13:54:13 +000045////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000046
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000047GrGpu::GrGpu(GrContext* context)
joshualitt3322fa42014-11-07 08:48:51 -080048 : fResetTimestamp(kExpiredTimestamp+1)
bsalomon@google.com0a208a12013-06-28 18:57:35 +000049 , fResetBits(kAll_GrBackendState)
joshualitt3322fa42014-11-07 08:48:51 -080050 , fContext(context) {
reed@google.comac10a2d2010-12-22 21:39:39 +000051}
52
bsalomoned0bcad2015-05-04 10:36:42 -070053GrGpu::~GrGpu() {}
bsalomon1d89ddc2014-08-19 14:20:58 -070054
robertphillipse3371302014-09-17 06:01:06 -070055void GrGpu::contextAbandoned() {}
reed@google.comac10a2d2010-12-22 21:39:39 +000056
bsalomon@google.comd302f142011-03-03 13:54:13 +000057////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000058
bsalomon045802d2015-10-20 07:58:01 -070059bool GrGpu::makeCopyForTextureParams(int width, int height, const GrTextureParams& textureParams,
bsalomon89fe56b2015-10-29 10:49:28 -070060 GrTextureProducer::CopyParams* copyParams) const {
bsalomon045802d2015-10-20 07:58:01 -070061 const GrCaps& caps = *this->caps();
62 if (textureParams.isTiled() && !caps.npotTextureTileSupport() &&
63 (!SkIsPow2(width) || !SkIsPow2(height))) {
bsalomon100b8f82015-10-28 08:37:44 -070064 copyParams->fWidth = GrNextPow2(width);
65 copyParams->fHeight = GrNextPow2(height);
bsalomon045802d2015-10-20 07:58:01 -070066 switch (textureParams.filterMode()) {
67 case GrTextureParams::kNone_FilterMode:
68 copyParams->fFilter = GrTextureParams::kNone_FilterMode;
69 break;
70 case GrTextureParams::kBilerp_FilterMode:
71 case GrTextureParams::kMipMap_FilterMode:
72 // We are only ever scaling up so no reason to ever indicate kMipMap.
73 copyParams->fFilter = GrTextureParams::kBilerp_FilterMode;
74 break;
75 }
bsalomon100b8f82015-10-28 08:37:44 -070076 return true;
bsalomon045802d2015-10-20 07:58:01 -070077 }
bsalomon100b8f82015-10-28 08:37:44 -070078 return false;
bsalomon045802d2015-10-20 07:58:01 -070079}
80
egdanielcf614fd2015-04-22 13:58:58 -070081static GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) {
egdanielb0e1be22015-04-22 13:27:39 -070082 // By default, GrRenderTargets are GL's normal orientation so that they
83 // can be drawn to by the outside world without the client having
84 // to render upside down.
85 if (kDefault_GrSurfaceOrigin == origin) {
86 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
87 } else {
88 return origin;
89 }
90}
91
egdanielb0e1be22015-04-22 13:27:39 -070092GrTexture* GrGpu::createTexture(const GrSurfaceDesc& origDesc, bool budgeted,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000093 const void* srcData, size_t rowBytes) {
egdanielb0e1be22015-04-22 13:27:39 -070094 GrSurfaceDesc desc = origDesc;
95
krajcevski9c0e6292014-06-02 07:38:14 -070096 if (!this->caps()->isConfigTexturable(desc.fConfig)) {
halcanary96fcdcc2015-08-27 07:41:13 -070097 return nullptr;
robertphillips@google.comd3eb3362012-10-31 13:56:35 +000098 }
krajcevski9c0e6292014-06-02 07:38:14 -070099
bsalomondb558dd2015-01-23 13:19:00 -0800100 bool isRT = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
101 if (isRT && !this->caps()->isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700102 return nullptr;
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +0000103 }
robertphillips@google.comd3eb3362012-10-31 13:56:35 +0000104
robertphillips6e83ac72015-08-13 05:19:14 -0700105 // We currently do not support multisampled textures
egdaniel8c9b6f12015-05-12 13:36:30 -0700106 if (!isRT && desc.fSampleCnt > 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700107 return nullptr;
egdaniel8c9b6f12015-05-12 13:36:30 -0700108 }
109
halcanary96fcdcc2015-08-27 07:41:13 -0700110 GrTexture *tex = nullptr;
egdanielb0e1be22015-04-22 13:27:39 -0700111
112 if (isRT) {
113 int maxRTSize = this->caps()->maxRenderTargetSize();
114 if (desc.fWidth > maxRTSize || desc.fHeight > maxRTSize) {
halcanary96fcdcc2015-08-27 07:41:13 -0700115 return nullptr;
egdanielb0e1be22015-04-22 13:27:39 -0700116 }
117 } else {
118 int maxSize = this->caps()->maxTextureSize();
119 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
halcanary96fcdcc2015-08-27 07:41:13 -0700120 return nullptr;
egdanielb0e1be22015-04-22 13:27:39 -0700121 }
122 }
123
124 GrGpuResource::LifeCycle lifeCycle = budgeted ? GrGpuResource::kCached_LifeCycle :
125 GrGpuResource::kUncached_LifeCycle;
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);
130
131 desc.fOrigin = resolve_origin(desc.fOrigin, isRT);
132
krajcevski9c0e6292014-06-02 07:38:14 -0700133 if (GrPixelConfigIsCompressed(desc.fConfig)) {
134 // We shouldn't be rendering into this
egdanielb0e1be22015-04-22 13:27:39 -0700135 SkASSERT(!isRT);
136 SkASSERT(0 == desc.fSampleCnt);
krajcevski9c0e6292014-06-02 07:38:14 -0700137
138 if (!this->caps()->npotTextureTileSupport() &&
tfarinaf9dae782014-06-06 06:35:28 -0700139 (!SkIsPow2(desc.fWidth) || !SkIsPow2(desc.fHeight))) {
halcanary96fcdcc2015-08-27 07:41:13 -0700140 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000141 }
tfarinaf9dae782014-06-06 06:35:28 -0700142
krajcevski9c0e6292014-06-02 07:38:14 -0700143 this->handleDirtyContext();
egdanielb0e1be22015-04-22 13:27:39 -0700144 tex = this->onCreateCompressedTexture(desc, lifeCycle, srcData);
krajcevski9c0e6292014-06-02 07:38:14 -0700145 } else {
146 this->handleDirtyContext();
egdanielb0e1be22015-04-22 13:27:39 -0700147 tex = this->onCreateTexture(desc, lifeCycle, srcData, rowBytes);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000148 }
bsalomondb558dd2015-01-23 13:19:00 -0800149 if (!this->caps()->reuseScratchTextures() && !isRT) {
bsalomon3582d3e2015-02-13 14:20:05 -0800150 tex->resourcePriv().removeScratchKey();
bsalomondb558dd2015-01-23 13:19:00 -0800151 }
bsalomonb12ea412015-02-02 21:19:50 -0800152 if (tex) {
153 fStats.incTextureCreates();
154 if (srcData) {
155 fStats.incTextureUploads();
156 }
157 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000158 return tex;
159}
160
bsalomon6dc6f5f2015-06-18 09:12:16 -0700161GrTexture* GrGpu::wrapBackendTexture(const GrBackendTextureDesc& desc, GrWrapOwnership ownership) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000162 this->handleDirtyContext();
bsalomon6dc6f5f2015-06-18 09:12:16 -0700163 GrTexture* tex = this->onWrapBackendTexture(desc, ownership);
halcanary96fcdcc2015-08-27 07:41:13 -0700164 if (nullptr == tex) {
165 return nullptr;
bsalomon@google.coma14dd6d2012-01-03 21:08:12 +0000166 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000167 // TODO: defer this and attach dynamically
168 GrRenderTarget* tgt = tex->asRenderTarget();
egdanielec00d942015-09-14 12:56:10 -0700169 if (tgt && !fContext->resourceProvider()->attachStencilAttachment(tgt)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000170 tex->unref();
halcanary96fcdcc2015-08-27 07:41:13 -0700171 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000172 } else {
173 return tex;
174 }
175}
176
bsalomon6dc6f5f2015-06-18 09:12:16 -0700177GrRenderTarget* GrGpu::wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc,
178 GrWrapOwnership ownership) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000179 this->handleDirtyContext();
bsalomon6dc6f5f2015-06-18 09:12:16 -0700180 return this->onWrapBackendRenderTarget(desc, ownership);
bsalomon@google.come269f212011-11-07 13:29:52 +0000181}
182
robertphillips@google.comadacc702013-10-14 21:53:24 +0000183GrVertexBuffer* GrGpu::createVertexBuffer(size_t size, bool dynamic) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000184 this->handleDirtyContext();
robertphillips1b8e1b52015-06-24 06:54:10 -0700185 GrVertexBuffer* vb = this->onCreateVertexBuffer(size, dynamic);
186 if (!this->caps()->reuseScratchBuffers()) {
187 vb->resourcePriv().removeScratchKey();
188 }
189 return vb;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000190}
191
robertphillips@google.comadacc702013-10-14 21:53:24 +0000192GrIndexBuffer* GrGpu::createIndexBuffer(size_t size, bool dynamic) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000193 this->handleDirtyContext();
robertphillips1b8e1b52015-06-24 06:54:10 -0700194 GrIndexBuffer* ib = this->onCreateIndexBuffer(size, dynamic);
195 if (!this->caps()->reuseScratchBuffers()) {
196 ib->resourcePriv().removeScratchKey();
197 }
198 return ib;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000199}
200
jvanverth73063dc2015-12-03 09:15:47 -0800201GrTransferBuffer* GrGpu::createTransferBuffer(size_t size, TransferType type) {
202 this->handleDirtyContext();
203 GrTransferBuffer* tb = this->onCreateTransferBuffer(size, type);
204 return tb;
205}
206
egdaniel51c8d402015-08-06 10:54:13 -0700207void GrGpu::clear(const SkIRect& rect,
joshualitt3322fa42014-11-07 08:48:51 -0800208 GrColor color,
joshualitt3322fa42014-11-07 08:48:51 -0800209 GrRenderTarget* renderTarget) {
bsalomon89c62982014-11-03 12:08:42 -0800210 SkASSERT(renderTarget);
egdaniel51c8d402015-08-06 10:54:13 -0700211 SkASSERT(SkIRect::MakeWH(renderTarget->width(), renderTarget->height()).contains(rect));
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000212 this->handleDirtyContext();
egdaniel51c8d402015-08-06 10:54:13 -0700213 this->onClear(renderTarget, rect, color);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000214}
215
joshualitt6db519c2014-10-29 08:48:18 -0700216void GrGpu::clearStencilClip(const SkIRect& rect,
217 bool insideClip,
218 GrRenderTarget* renderTarget) {
joshualittd53a8272014-11-10 16:03:14 -0800219 SkASSERT(renderTarget);
joshualitt6db519c2014-10-29 08:48:18 -0700220 this->handleDirtyContext();
221 this->onClearStencilClip(renderTarget, rect, insideClip);
222}
223
joshualitt1cbdcde2015-08-21 11:53:29 -0700224bool GrGpu::copySurface(GrSurface* dst,
225 GrSurface* src,
226 const SkIRect& srcRect,
227 const SkIPoint& dstPoint) {
228 SkASSERT(dst && src);
229 this->handleDirtyContext();
230 return this->onCopySurface(dst, src, srcRect, dstPoint);
231}
232
bsalomonf0674512015-07-28 13:26:15 -0700233bool GrGpu::getReadPixelsInfo(GrSurface* srcSurface, int width, int height, size_t rowBytes,
234 GrPixelConfig readConfig, DrawPreference* drawPreference,
235 ReadPixelTempDrawInfo* tempDrawInfo) {
236 SkASSERT(drawPreference);
237 SkASSERT(tempDrawInfo);
238 SkASSERT(kGpuPrefersDraw_DrawPreference != *drawPreference);
239
egdaniel6d901da2015-07-30 12:02:15 -0700240 // We currently do not support reading into a compressed buffer
241 if (GrPixelConfigIsCompressed(readConfig)) {
242 return false;
243 }
244
bsalomonf0674512015-07-28 13:26:15 -0700245 if (!this->onGetReadPixelsInfo(srcSurface, width, height, rowBytes, readConfig, drawPreference,
246 tempDrawInfo)) {
247 return false;
248 }
249
250 // Check to see if we're going to request that the caller draw when drawing is not possible.
251 if (!srcSurface->asTexture() ||
252 !this->caps()->isConfigRenderable(tempDrawInfo->fTempSurfaceDesc.fConfig, false)) {
253 // If we don't have a fallback to a straight read then fail.
254 if (kRequireDraw_DrawPreference == *drawPreference) {
255 return false;
256 }
257 *drawPreference = kNoDraw_DrawPreference;
258 }
259
260 return true;
261}
262bool GrGpu::getWritePixelsInfo(GrSurface* dstSurface, int width, int height, size_t rowBytes,
263 GrPixelConfig srcConfig, DrawPreference* drawPreference,
264 WritePixelTempDrawInfo* tempDrawInfo) {
265 SkASSERT(drawPreference);
266 SkASSERT(tempDrawInfo);
267 SkASSERT(kGpuPrefersDraw_DrawPreference != *drawPreference);
268
jvanverth2dc29942015-09-01 07:16:46 -0700269 if (GrPixelConfigIsCompressed(dstSurface->desc().fConfig) &&
270 dstSurface->desc().fConfig != srcConfig) {
271 return false;
272 }
273
bsalomonf0674512015-07-28 13:26:15 -0700274 if (this->caps()->useDrawInsteadOfPartialRenderTargetWrite() &&
275 SkToBool(dstSurface->asRenderTarget()) &&
276 (width < dstSurface->width() || height < dstSurface->height())) {
277 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
278 }
279
280 if (!this->onGetWritePixelsInfo(dstSurface, width, height, rowBytes, srcConfig, drawPreference,
281 tempDrawInfo)) {
282 return false;
283 }
284
285 // Check to see if we're going to request that the caller draw when drawing is not possible.
286 if (!dstSurface->asRenderTarget() ||
287 !this->caps()->isConfigTexturable(tempDrawInfo->fTempSurfaceDesc.fConfig)) {
288 // If we don't have a fallback to a straight upload then fail.
289 if (kRequireDraw_DrawPreference == *drawPreference ||
290 !this->caps()->isConfigTexturable(srcConfig)) {
291 return false;
292 }
293 *drawPreference = kNoDraw_DrawPreference;
294 }
295 return true;
296}
297
bsalomon6cb3cbe2015-07-30 07:34:27 -0700298bool GrGpu::readPixels(GrSurface* surface,
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000299 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000300 GrPixelConfig config, void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000301 size_t rowBytes) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000302 this->handleDirtyContext();
egdaniel6d901da2015-07-30 12:02:15 -0700303
304 // We cannot read pixels into a compressed buffer
305 if (GrPixelConfigIsCompressed(config)) {
306 return false;
307 }
308
309 size_t bpp = GrBytesPerPixel(config);
310 if (!GrSurfacePriv::AdjustReadPixelParams(surface->width(), surface->height(), bpp,
311 &left, &top, &width, &height,
312 &buffer,
313 &rowBytes)) {
314 return false;
315 }
316
317 return this->onReadPixels(surface,
318 left, top, width, height,
319 config, buffer,
320 rowBytes);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000321}
322
bsalomon6cb3cbe2015-07-30 07:34:27 -0700323bool GrGpu::writePixels(GrSurface* surface,
324 int left, int top, int width, int height,
325 GrPixelConfig config, const void* buffer,
326 size_t rowBytes) {
jvanverth2dc29942015-09-01 07:16:46 -0700327 if (!buffer) {
328 return false;
329 }
330
bsalomon@google.com6f379512011-11-16 20:36:03 +0000331 this->handleDirtyContext();
bsalomon6cb3cbe2015-07-30 07:34:27 -0700332 if (this->onWritePixels(surface, left, top, width, height, config, buffer, rowBytes)) {
bsalomonb12ea412015-02-02 21:19:50 -0800333 fStats.incTextureUploads();
334 return true;
335 }
336 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000337}
338
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000339void GrGpu::resolveRenderTarget(GrRenderTarget* target) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000340 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000341 this->handleDirtyContext();
342 this->onResolveRenderTarget(target);
343}
344
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000345////////////////////////////////////////////////////////////////////////////////
346
bsalomoncb8979d2015-05-05 09:51:38 -0700347void GrGpu::draw(const DrawArgs& args, const GrVertices& vertices) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000348 this->handleDirtyContext();
bsalomoncb02b382015-08-12 11:14:50 -0700349 if (GrXferBarrierType barrierType = args.fPipeline->xferBarrierType(*this->caps())) {
350 this->xferBarrier(args.fPipeline->getRenderTarget(), barrierType);
351 }
352
bsalomone64eb572015-05-07 11:35:55 -0700353 GrVertices::Iterator iter;
354 const GrNonInstancedVertices* verts = iter.init(vertices);
355 do {
356 this->onDraw(args, *verts);
joshualitt336cda32015-09-09 08:29:47 -0700357 fStats.incNumDraws();
bsalomone64eb572015-05-07 11:35:55 -0700358 } while ((verts = iter.next()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000359}