blob: ce15b4151f470395ac8016200f753a3a6b622a66 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2010 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
epoger@google.comec3ed6a2011-07-28 14:26:00 +00008
reed@google.comac10a2d2010-12-22 21:39:39 +00009#include "GrGpu.h"
bsalomon@google.com558a75b2011-08-08 17:01:14 +000010
bsalomoneb1cb5c2015-05-22 08:01:09 -070011#include "GrCaps.h"
bsalomon@google.com558a75b2011-08-08 17:01:14 +000012#include "GrContext.h"
bsalomon3582d3e2015-02-13 14:20:05 -080013#include "GrGpuResourcePriv.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000014#include "GrIndexBuffer.h"
egdaniel0e1853c2016-03-17 11:35:45 -070015#include "GrMesh.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"
cblume55f2d2d2016-02-26 13:20:48 -080025#include "SkTypes.h"
bsalomoncb8979d2015-05-05 09:51:38 -070026
egdaniel0e1853c2016-03-17 11:35:45 -070027GrMesh& GrMesh::operator =(const GrMesh& di) {
bsalomoncb8979d2015-05-05 09:51:38 -070028 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)
cdalton28f45b92016-03-07 13:58:26 -080050 , fMultisampleSpecsAllocator(1)
joshualitt3322fa42014-11-07 08:48:51 -080051 , fContext(context) {
reed@google.comac10a2d2010-12-22 21:39:39 +000052}
53
bsalomoned0bcad2015-05-04 10:36:42 -070054GrGpu::~GrGpu() {}
bsalomon1d89ddc2014-08-19 14:20:58 -070055
robertphillipse3371302014-09-17 06:01:06 -070056void GrGpu::contextAbandoned() {}
reed@google.comac10a2d2010-12-22 21:39:39 +000057
bsalomon@google.comd302f142011-03-03 13:54:13 +000058////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000059
bsalomon045802d2015-10-20 07:58:01 -070060bool GrGpu::makeCopyForTextureParams(int width, int height, const GrTextureParams& textureParams,
bsalomon89fe56b2015-10-29 10:49:28 -070061 GrTextureProducer::CopyParams* copyParams) const {
bsalomon045802d2015-10-20 07:58:01 -070062 const GrCaps& caps = *this->caps();
63 if (textureParams.isTiled() && !caps.npotTextureTileSupport() &&
64 (!SkIsPow2(width) || !SkIsPow2(height))) {
bsalomon100b8f82015-10-28 08:37:44 -070065 copyParams->fWidth = GrNextPow2(width);
66 copyParams->fHeight = GrNextPow2(height);
bsalomon045802d2015-10-20 07:58:01 -070067 switch (textureParams.filterMode()) {
68 case GrTextureParams::kNone_FilterMode:
69 copyParams->fFilter = GrTextureParams::kNone_FilterMode;
70 break;
71 case GrTextureParams::kBilerp_FilterMode:
72 case GrTextureParams::kMipMap_FilterMode:
73 // We are only ever scaling up so no reason to ever indicate kMipMap.
74 copyParams->fFilter = GrTextureParams::kBilerp_FilterMode;
75 break;
76 }
bsalomon100b8f82015-10-28 08:37:44 -070077 return true;
bsalomon045802d2015-10-20 07:58:01 -070078 }
bsalomon100b8f82015-10-28 08:37:44 -070079 return false;
bsalomon045802d2015-10-20 07:58:01 -070080}
81
egdanielcf614fd2015-04-22 13:58:58 -070082static GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) {
egdanielb0e1be22015-04-22 13:27:39 -070083 // By default, GrRenderTargets are GL's normal orientation so that they
84 // can be drawn to by the outside world without the client having
85 // to render upside down.
86 if (kDefault_GrSurfaceOrigin == origin) {
87 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
88 } else {
89 return origin;
90 }
91}
92
cblume55f2d2d2016-02-26 13:20:48 -080093/**
94 * Prior to creating a texture, make sure the type of texture being created is
95 * supported by calling check_texture_creation_params.
96 *
97 * @param caps The capabilities of the GL device.
98 * @param desc The descriptor of the texture to create.
99 * @param isRT Indicates if the texture can be a render target.
100 */
101static bool check_texture_creation_params(const GrCaps& caps, const GrSurfaceDesc& desc,
bsalomone699d0c2016-03-09 06:25:15 -0800102 bool* isRT, const SkTArray<GrMipLevel>& texels) {
cblume55f2d2d2016-02-26 13:20:48 -0800103 if (!caps.isConfigTexturable(desc.fConfig)) {
104 return false;
robertphillips@google.comd3eb3362012-10-31 13:56:35 +0000105 }
krajcevski9c0e6292014-06-02 07:38:14 -0700106
cblume55f2d2d2016-02-26 13:20:48 -0800107 *isRT = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
108 if (*isRT && !caps.isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) {
109 return false;
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +0000110 }
robertphillips@google.comd3eb3362012-10-31 13:56:35 +0000111
robertphillips6e83ac72015-08-13 05:19:14 -0700112 // We currently do not support multisampled textures
cblume55f2d2d2016-02-26 13:20:48 -0800113 if (!*isRT && desc.fSampleCnt > 0) {
114 return false;
115 }
116
117 if (*isRT) {
118 int maxRTSize = caps.maxRenderTargetSize();
119 if (desc.fWidth > maxRTSize || desc.fHeight > maxRTSize) {
120 return false;
121 }
122 } else {
123 int maxSize = caps.maxTextureSize();
124 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
125 return false;
126 }
127 }
bsalomone699d0c2016-03-09 06:25:15 -0800128
129 for (int i = 0; i < texels.count(); ++i) {
130 if (!texels[i].fPixels) {
131 return false;
132 }
133 }
cblume55f2d2d2016-02-26 13:20:48 -0800134 return true;
135}
136
137GrTexture* GrGpu::createTexture(const GrSurfaceDesc& origDesc, SkBudgeted budgeted,
138 const SkTArray<GrMipLevel>& texels) {
139 GrSurfaceDesc desc = origDesc;
140
141 const GrCaps* caps = this->caps();
142 bool isRT = false;
bsalomone699d0c2016-03-09 06:25:15 -0800143 bool textureCreationParamsValid = check_texture_creation_params(*caps, desc, &isRT, texels);
cblume55f2d2d2016-02-26 13:20:48 -0800144 if (!textureCreationParamsValid) {
halcanary96fcdcc2015-08-27 07:41:13 -0700145 return nullptr;
egdaniel8c9b6f12015-05-12 13:36:30 -0700146 }
147
cblume55f2d2d2016-02-26 13:20:48 -0800148 desc.fSampleCnt = SkTMin(desc.fSampleCnt, caps->maxSampleCount());
149 // Attempt to catch un- or wrongly intialized sample counts;
egdanielb0e1be22015-04-22 13:27:39 -0700150 SkASSERT(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
151
152 desc.fOrigin = resolve_origin(desc.fOrigin, isRT);
153
cblume55f2d2d2016-02-26 13:20:48 -0800154 GrTexture* tex = nullptr;
155 GrGpuResource::LifeCycle lifeCycle = SkBudgeted::kYes == budgeted ?
156 GrGpuResource::kCached_LifeCycle :
157 GrGpuResource::kUncached_LifeCycle;
158
krajcevski9c0e6292014-06-02 07:38:14 -0700159 if (GrPixelConfigIsCompressed(desc.fConfig)) {
160 // We shouldn't be rendering into this
egdanielb0e1be22015-04-22 13:27:39 -0700161 SkASSERT(!isRT);
162 SkASSERT(0 == desc.fSampleCnt);
krajcevski9c0e6292014-06-02 07:38:14 -0700163
cblume55f2d2d2016-02-26 13:20:48 -0800164 if (!caps->npotTextureTileSupport() &&
tfarinaf9dae782014-06-06 06:35:28 -0700165 (!SkIsPow2(desc.fWidth) || !SkIsPow2(desc.fHeight))) {
halcanary96fcdcc2015-08-27 07:41:13 -0700166 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000167 }
tfarinaf9dae782014-06-06 06:35:28 -0700168
krajcevski9c0e6292014-06-02 07:38:14 -0700169 this->handleDirtyContext();
cblume55f2d2d2016-02-26 13:20:48 -0800170 tex = this->onCreateCompressedTexture(desc, lifeCycle, texels);
krajcevski9c0e6292014-06-02 07:38:14 -0700171 } else {
172 this->handleDirtyContext();
cblume55f2d2d2016-02-26 13:20:48 -0800173 tex = this->onCreateTexture(desc, lifeCycle, texels);
bsalomondb558dd2015-01-23 13:19:00 -0800174 }
bsalomonb12ea412015-02-02 21:19:50 -0800175 if (tex) {
cblume55f2d2d2016-02-26 13:20:48 -0800176 if (!caps->reuseScratchTextures() && !isRT) {
177 tex->resourcePriv().removeScratchKey();
178 }
bsalomonb12ea412015-02-02 21:19:50 -0800179 fStats.incTextureCreates();
cblume55f2d2d2016-02-26 13:20:48 -0800180 if (!texels.empty()) {
181 if (texels[0].fPixels) {
182 fStats.incTextureUploads();
183 }
bsalomonb12ea412015-02-02 21:19:50 -0800184 }
185 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000186 return tex;
187}
188
bsalomon6dc6f5f2015-06-18 09:12:16 -0700189GrTexture* GrGpu::wrapBackendTexture(const GrBackendTextureDesc& desc, GrWrapOwnership ownership) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000190 this->handleDirtyContext();
bsalomon5b30c6f2015-12-17 14:17:34 -0800191 if (!this->caps()->isConfigTexturable(desc.fConfig)) {
192 return nullptr;
193 }
194 if ((desc.fFlags & kRenderTarget_GrBackendTextureFlag) &&
195 !this->caps()->isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) {
196 return nullptr;
197 }
ericrkf7b8b8a2016-02-24 14:49:51 -0800198 int maxSize = this->caps()->maxTextureSize();
199 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
200 return nullptr;
201 }
bsalomon6dc6f5f2015-06-18 09:12:16 -0700202 GrTexture* tex = this->onWrapBackendTexture(desc, ownership);
halcanary96fcdcc2015-08-27 07:41:13 -0700203 if (nullptr == tex) {
204 return nullptr;
bsalomon@google.coma14dd6d2012-01-03 21:08:12 +0000205 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000206 // TODO: defer this and attach dynamically
207 GrRenderTarget* tgt = tex->asRenderTarget();
egdanielec00d942015-09-14 12:56:10 -0700208 if (tgt && !fContext->resourceProvider()->attachStencilAttachment(tgt)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000209 tex->unref();
halcanary96fcdcc2015-08-27 07:41:13 -0700210 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000211 } else {
212 return tex;
213 }
214}
215
bsalomon6dc6f5f2015-06-18 09:12:16 -0700216GrRenderTarget* GrGpu::wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc,
217 GrWrapOwnership ownership) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800218 if (!this->caps()->isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) {
219 return nullptr;
220 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000221 this->handleDirtyContext();
bsalomon6dc6f5f2015-06-18 09:12:16 -0700222 return this->onWrapBackendRenderTarget(desc, ownership);
bsalomon@google.come269f212011-11-07 13:29:52 +0000223}
224
ericrkf7b8b8a2016-02-24 14:49:51 -0800225GrRenderTarget* GrGpu::wrapBackendTextureAsRenderTarget(const GrBackendTextureDesc& desc,
226 GrWrapOwnership ownership) {
227 this->handleDirtyContext();
228 if (!(desc.fFlags & kRenderTarget_GrBackendTextureFlag)) {
229 return nullptr;
230 }
231 if (!this->caps()->isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) {
232 return nullptr;
233 }
234 int maxSize = this->caps()->maxTextureSize();
235 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
236 return nullptr;
237 }
238 return this->onWrapBackendTextureAsRenderTarget(desc, ownership);
239}
240
robertphillips@google.comadacc702013-10-14 21:53:24 +0000241GrVertexBuffer* GrGpu::createVertexBuffer(size_t size, bool dynamic) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000242 this->handleDirtyContext();
robertphillips1b8e1b52015-06-24 06:54:10 -0700243 GrVertexBuffer* vb = this->onCreateVertexBuffer(size, dynamic);
244 if (!this->caps()->reuseScratchBuffers()) {
245 vb->resourcePriv().removeScratchKey();
246 }
247 return vb;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000248}
249
robertphillips@google.comadacc702013-10-14 21:53:24 +0000250GrIndexBuffer* GrGpu::createIndexBuffer(size_t size, bool dynamic) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000251 this->handleDirtyContext();
robertphillips1b8e1b52015-06-24 06:54:10 -0700252 GrIndexBuffer* ib = this->onCreateIndexBuffer(size, dynamic);
253 if (!this->caps()->reuseScratchBuffers()) {
254 ib->resourcePriv().removeScratchKey();
255 }
256 return ib;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000257}
258
jvanverth73063dc2015-12-03 09:15:47 -0800259GrTransferBuffer* GrGpu::createTransferBuffer(size_t size, TransferType type) {
260 this->handleDirtyContext();
261 GrTransferBuffer* tb = this->onCreateTransferBuffer(size, type);
262 return tb;
263}
264
egdaniel51c8d402015-08-06 10:54:13 -0700265void GrGpu::clear(const SkIRect& rect,
joshualitt3322fa42014-11-07 08:48:51 -0800266 GrColor color,
joshualitt3322fa42014-11-07 08:48:51 -0800267 GrRenderTarget* renderTarget) {
bsalomon89c62982014-11-03 12:08:42 -0800268 SkASSERT(renderTarget);
egdaniel51c8d402015-08-06 10:54:13 -0700269 SkASSERT(SkIRect::MakeWH(renderTarget->width(), renderTarget->height()).contains(rect));
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000270 this->handleDirtyContext();
egdaniel51c8d402015-08-06 10:54:13 -0700271 this->onClear(renderTarget, rect, color);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000272}
273
joshualitt6db519c2014-10-29 08:48:18 -0700274void GrGpu::clearStencilClip(const SkIRect& rect,
275 bool insideClip,
276 GrRenderTarget* renderTarget) {
joshualittd53a8272014-11-10 16:03:14 -0800277 SkASSERT(renderTarget);
joshualitt6db519c2014-10-29 08:48:18 -0700278 this->handleDirtyContext();
279 this->onClearStencilClip(renderTarget, rect, insideClip);
280}
281
joshualitt1cbdcde2015-08-21 11:53:29 -0700282bool GrGpu::copySurface(GrSurface* dst,
283 GrSurface* src,
284 const SkIRect& srcRect,
285 const SkIPoint& dstPoint) {
286 SkASSERT(dst && src);
287 this->handleDirtyContext();
288 return this->onCopySurface(dst, src, srcRect, dstPoint);
289}
290
bsalomonf0674512015-07-28 13:26:15 -0700291bool GrGpu::getReadPixelsInfo(GrSurface* srcSurface, int width, int height, size_t rowBytes,
292 GrPixelConfig readConfig, DrawPreference* drawPreference,
293 ReadPixelTempDrawInfo* tempDrawInfo) {
294 SkASSERT(drawPreference);
295 SkASSERT(tempDrawInfo);
296 SkASSERT(kGpuPrefersDraw_DrawPreference != *drawPreference);
297
egdaniel6d901da2015-07-30 12:02:15 -0700298 // We currently do not support reading into a compressed buffer
299 if (GrPixelConfigIsCompressed(readConfig)) {
300 return false;
301 }
302
bsalomonf0674512015-07-28 13:26:15 -0700303 if (!this->onGetReadPixelsInfo(srcSurface, width, height, rowBytes, readConfig, drawPreference,
304 tempDrawInfo)) {
305 return false;
306 }
307
308 // Check to see if we're going to request that the caller draw when drawing is not possible.
309 if (!srcSurface->asTexture() ||
310 !this->caps()->isConfigRenderable(tempDrawInfo->fTempSurfaceDesc.fConfig, false)) {
311 // If we don't have a fallback to a straight read then fail.
312 if (kRequireDraw_DrawPreference == *drawPreference) {
313 return false;
314 }
315 *drawPreference = kNoDraw_DrawPreference;
316 }
317
318 return true;
319}
cblumeed828002016-02-16 13:00:01 -0800320bool GrGpu::getWritePixelsInfo(GrSurface* dstSurface, int width, int height,
bsalomonf0674512015-07-28 13:26:15 -0700321 GrPixelConfig srcConfig, DrawPreference* drawPreference,
322 WritePixelTempDrawInfo* tempDrawInfo) {
323 SkASSERT(drawPreference);
324 SkASSERT(tempDrawInfo);
325 SkASSERT(kGpuPrefersDraw_DrawPreference != *drawPreference);
326
jvanverth2dc29942015-09-01 07:16:46 -0700327 if (GrPixelConfigIsCompressed(dstSurface->desc().fConfig) &&
328 dstSurface->desc().fConfig != srcConfig) {
329 return false;
330 }
331
bsalomonbabafcc2016-02-16 11:36:47 -0800332 if (SkToBool(dstSurface->asRenderTarget())) {
333 if (this->caps()->useDrawInsteadOfAllRenderTargetWrites()) {
334 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
335 } else if (this->caps()->useDrawInsteadOfPartialRenderTargetWrite() &&
336 (width < dstSurface->width() || height < dstSurface->height())) {
337 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
338 }
bsalomonf0674512015-07-28 13:26:15 -0700339 }
340
cblumeed828002016-02-16 13:00:01 -0800341 if (!this->onGetWritePixelsInfo(dstSurface, width, height, srcConfig, drawPreference,
bsalomonf0674512015-07-28 13:26:15 -0700342 tempDrawInfo)) {
343 return false;
344 }
345
346 // Check to see if we're going to request that the caller draw when drawing is not possible.
347 if (!dstSurface->asRenderTarget() ||
348 !this->caps()->isConfigTexturable(tempDrawInfo->fTempSurfaceDesc.fConfig)) {
349 // If we don't have a fallback to a straight upload then fail.
350 if (kRequireDraw_DrawPreference == *drawPreference ||
351 !this->caps()->isConfigTexturable(srcConfig)) {
352 return false;
353 }
354 *drawPreference = kNoDraw_DrawPreference;
355 }
356 return true;
357}
358
bsalomon6cb3cbe2015-07-30 07:34:27 -0700359bool GrGpu::readPixels(GrSurface* surface,
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000360 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000361 GrPixelConfig config, void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000362 size_t rowBytes) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000363 this->handleDirtyContext();
egdaniel6d901da2015-07-30 12:02:15 -0700364
365 // We cannot read pixels into a compressed buffer
366 if (GrPixelConfigIsCompressed(config)) {
367 return false;
368 }
369
370 size_t bpp = GrBytesPerPixel(config);
371 if (!GrSurfacePriv::AdjustReadPixelParams(surface->width(), surface->height(), bpp,
372 &left, &top, &width, &height,
373 &buffer,
374 &rowBytes)) {
375 return false;
376 }
377
378 return this->onReadPixels(surface,
379 left, top, width, height,
380 config, buffer,
381 rowBytes);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000382}
383
bsalomon6cb3cbe2015-07-30 07:34:27 -0700384bool GrGpu::writePixels(GrSurface* surface,
385 int left, int top, int width, int height,
cblume55f2d2d2016-02-26 13:20:48 -0800386 GrPixelConfig config, const SkTArray<GrMipLevel>& texels) {
387 if (!surface) {
388 return false;
389 }
cblume55f2d2d2016-02-26 13:20:48 -0800390 for (int currentMipLevel = 0; currentMipLevel < texels.count(); currentMipLevel++) {
bsalomone699d0c2016-03-09 06:25:15 -0800391 if (!texels[currentMipLevel].fPixels ) {
392 return false;
cblume55f2d2d2016-02-26 13:20:48 -0800393 }
394 }
jvanverth2dc29942015-09-01 07:16:46 -0700395
bsalomon@google.com6f379512011-11-16 20:36:03 +0000396 this->handleDirtyContext();
cblume55f2d2d2016-02-26 13:20:48 -0800397 if (this->onWritePixels(surface, left, top, width, height, config, texels)) {
bsalomonb12ea412015-02-02 21:19:50 -0800398 fStats.incTextureUploads();
399 return true;
400 }
401 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000402}
403
cblume55f2d2d2016-02-26 13:20:48 -0800404bool GrGpu::writePixels(GrSurface* surface,
405 int left, int top, int width, int height,
406 GrPixelConfig config, const void* buffer,
407 size_t rowBytes) {
408 GrMipLevel mipLevel;
409 mipLevel.fPixels = buffer;
410 mipLevel.fRowBytes = rowBytes;
411 SkSTArray<1, GrMipLevel> texels;
412 texels.push_back(mipLevel);
413
414 return this->writePixels(surface, left, top, width, height, config, texels);
415}
416
jvanverth17aa0472016-01-05 10:41:27 -0800417bool GrGpu::transferPixels(GrSurface* surface,
418 int left, int top, int width, int height,
419 GrPixelConfig config, GrTransferBuffer* buffer,
420 size_t offset, size_t rowBytes) {
421 SkASSERT(buffer);
422
423 this->handleDirtyContext();
cblume61214052016-01-26 09:10:48 -0800424 if (this->onTransferPixels(surface, left, top, width, height, config,
jvanverth17aa0472016-01-05 10:41:27 -0800425 buffer, offset, rowBytes)) {
426 fStats.incTransfersToTexture();
427 return true;
428 }
429 return false;
430}
431
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000432void GrGpu::resolveRenderTarget(GrRenderTarget* target) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000433 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000434 this->handleDirtyContext();
435 this->onResolveRenderTarget(target);
436}
437
cdalton28f45b92016-03-07 13:58:26 -0800438inline static uint8_t multisample_specs_id(uint8_t numSamples, GrSurfaceOrigin origin,
439 const GrCaps& caps) {
440 if (!caps.sampleLocationsSupport()) {
441 return numSamples;
442 }
443
444 SkASSERT(numSamples < 128);
445 SkASSERT(kTopLeft_GrSurfaceOrigin == origin || kBottomLeft_GrSurfaceOrigin == origin);
446 return (numSamples << 1) | (origin - 1);
447
448 GR_STATIC_ASSERT(1 == kTopLeft_GrSurfaceOrigin);
449 GR_STATIC_ASSERT(2 == kBottomLeft_GrSurfaceOrigin);
450}
451
452const GrGpu::MultisampleSpecs& GrGpu::getMultisampleSpecs(GrRenderTarget* rt,
453 const GrStencilSettings& stencil) {
454 const GrSurfaceDesc& desc = rt->desc();
455 uint8_t surfDescKey = multisample_specs_id(desc.fSampleCnt, desc.fOrigin, *this->caps());
456 if (fMultisampleSpecsMap.count() > surfDescKey && fMultisampleSpecsMap[surfDescKey]) {
457#if !defined(SK_DEBUG)
458 // In debug mode we query the multisample info every time and verify the caching is correct.
459 return *fMultisampleSpecsMap[surfDescKey];
460#endif
461 }
462 int effectiveSampleCnt;
463 SkAutoTDeleteArray<SkPoint> locations(nullptr);
464 this->onGetMultisampleSpecs(rt, stencil, &effectiveSampleCnt, &locations);
465 SkASSERT(effectiveSampleCnt && effectiveSampleCnt >= desc.fSampleCnt);
466 uint8_t effectiveKey = multisample_specs_id(effectiveSampleCnt, desc.fOrigin, *this->caps());
467 if (fMultisampleSpecsMap.count() > effectiveKey && fMultisampleSpecsMap[effectiveKey]) {
468 const MultisampleSpecs& specs = *fMultisampleSpecsMap[effectiveKey];
469 SkASSERT(effectiveKey == specs.fUniqueID);
470 SkASSERT(effectiveSampleCnt == specs.fEffectiveSampleCnt);
471 SkASSERT(!this->caps()->sampleLocationsSupport() ||
472 !memcmp(locations.get(), specs.fSampleLocations.get(),
473 effectiveSampleCnt * sizeof(SkPoint)));
474 SkASSERT(surfDescKey <= effectiveKey);
475 SkASSERT(!fMultisampleSpecsMap[surfDescKey] || fMultisampleSpecsMap[surfDescKey] == &specs);
476 fMultisampleSpecsMap[surfDescKey] = &specs;
477 return specs;
478 }
479 const MultisampleSpecs& specs = *new (&fMultisampleSpecsAllocator)
mtklein18300a32016-03-16 13:53:35 -0700480 MultisampleSpecs{effectiveKey, effectiveSampleCnt, locations.release()};
cdalton28f45b92016-03-07 13:58:26 -0800481 if (fMultisampleSpecsMap.count() <= effectiveKey) {
482 int n = 1 + effectiveKey - fMultisampleSpecsMap.count();
483 fMultisampleSpecsMap.push_back_n(n, (const MultisampleSpecs*) nullptr);
484 }
485 fMultisampleSpecsMap[effectiveKey] = &specs;
486 if (effectiveSampleCnt != desc.fSampleCnt) {
487 SkASSERT(surfDescKey < effectiveKey);
488 fMultisampleSpecsMap[surfDescKey] = &specs;
489 }
490 return specs;
491}
492
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000493////////////////////////////////////////////////////////////////////////////////
494
bsalomon7dbd45d2016-03-23 10:40:53 -0700495bool GrGpu::draw(const GrPipeline& pipeline,
egdaniel0e1853c2016-03-17 11:35:45 -0700496 const GrPrimitiveProcessor& primProc,
497 const GrMesh* meshes,
498 int meshCount) {
bsalomon7dbd45d2016-03-23 10:40:53 -0700499 if (primProc.numAttribs() > this->caps()->maxVertexAttributes()) {
500 return false;
501 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000502 this->handleDirtyContext();
bsalomoncb02b382015-08-12 11:14:50 -0700503
egdaniel0e1853c2016-03-17 11:35:45 -0700504 this->onDraw(pipeline, primProc, meshes, meshCount);
bsalomon7dbd45d2016-03-23 10:40:53 -0700505 return true;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000506}
ethannicholas22793252016-01-30 09:59:10 -0800507