blob: d716d398856a7a326019fba100ee6f206340332d [file] [log] [blame]
Brian Osman45580d32016-11-23 09:37:01 -05001/*
2 * Copyright 2016 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.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/private/GrAuditTrail.h"
9#include "include/private/GrOpList.h"
10#include "include/private/GrRecordingContext.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040011#include "src/core/SkAutoPixmapStorage.h"
12#include "src/gpu/GrClip.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/GrContextPriv.h"
14#include "src/gpu/GrDrawingManager.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040015#include "src/gpu/GrGpu.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/GrRecordingContextPriv.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040017#include "src/gpu/GrRenderTargetContext.h"
Greg Daniel4c6f9b72019-06-06 13:40:59 -040018#include "src/gpu/GrSurfaceContext.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040019#include "src/gpu/GrSurfacePriv.h"
20#include "src/gpu/GrTextureContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/SkGr.h"
Brian Osman45580d32016-11-23 09:37:01 -050022
Robert Phillips2de8cfa2017-06-28 10:33:41 -040023#define ASSERT_SINGLE_OWNER \
24 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
Robert Phillips69893702019-02-22 11:16:30 -050025#define RETURN_FALSE_IF_ABANDONED if (this->fContext->priv().abandoned()) { return false; }
Brian Osman45580d32016-11-23 09:37:01 -050026
27// In MDB mode the reffing of the 'getLastOpList' call's result allows in-progress
28// GrOpLists to be picked up and added to by renderTargetContexts lower in the call
29// stack. When this occurs with a closed GrOpList, a new one will be allocated
30// when the renderTargetContext attempts to use it (via getOpList).
Robert Phillips69893702019-02-22 11:16:30 -050031GrSurfaceContext::GrSurfaceContext(GrRecordingContext* context,
Brian Salomonf3569f02017-10-24 12:52:33 -040032 GrPixelConfig config,
Robert Phillips0d075de2019-03-04 11:08:13 -050033 sk_sp<SkColorSpace> colorSpace)
Brian Salomonf3569f02017-10-24 12:52:33 -040034 : fContext(context)
Robert Phillips0d075de2019-03-04 11:08:13 -050035 , fColorSpaceInfo(std::move(colorSpace), config) {
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040036}
37
Robert Phillips0d075de2019-03-04 11:08:13 -050038GrAuditTrail* GrSurfaceContext::auditTrail() {
39 return fContext->priv().auditTrail();
40}
41
42GrDrawingManager* GrSurfaceContext::drawingManager() {
43 return fContext->priv().drawingManager();
44}
45
46const GrDrawingManager* GrSurfaceContext::drawingManager() const {
47 return fContext->priv().drawingManager();
48}
49
50#ifdef SK_DEBUG
51GrSingleOwner* GrSurfaceContext::singleOwner() {
52 return fContext->priv().singleOwner();
53}
54#endif
Greg Daniel6eb8c242019-06-05 10:22:24 -040055static bool valid_premul_color_type(GrColorType ct) {
56 switch (ct) {
57 case GrColorType::kUnknown: return false;
58 case GrColorType::kAlpha_8: return false;
59 case GrColorType::kRGB_565: return false;
60 case GrColorType::kABGR_4444: return true;
61 case GrColorType::kRGBA_8888: return true;
62 case GrColorType::kRGB_888x: return false;
63 case GrColorType::kRG_88: return false;
64 case GrColorType::kBGRA_8888: return true;
65 case GrColorType::kRGBA_1010102: return true;
66 case GrColorType::kGray_8: return false;
67 case GrColorType::kAlpha_F16: return false;
68 case GrColorType::kRGBA_F16: return true;
69 case GrColorType::kRGBA_F16_Clamped: return true;
70 case GrColorType::kRG_F32: return false;
71 case GrColorType::kRGBA_F32: return true;
72 case GrColorType::kRGB_ETC1: return false;
Robert Phillipsfe18de52019-06-06 17:21:50 -040073 // Experimental (for P016 and P010)
74 case GrColorType::kR_16: return false;
75 case GrColorType::kRG_1616: return false;
Greg Daniel6eb8c242019-06-05 10:22:24 -040076 }
77 SK_ABORT("Invalid GrColorType");
78 return false;
79}
80
81// TODO: This will be removed when GrSurfaceContexts are aware of their color types.
82// (skbug.com/6718)
83static bool valid_premul_config(GrPixelConfig config) {
84 switch (config) {
85 case kUnknown_GrPixelConfig: return false;
86 case kAlpha_8_GrPixelConfig: return false;
87 case kGray_8_GrPixelConfig: return false;
88 case kRGB_565_GrPixelConfig: return false;
89 case kRGBA_4444_GrPixelConfig: return true;
90 case kRGBA_8888_GrPixelConfig: return true;
91 case kRGB_888_GrPixelConfig: return false;
92 case kRGB_888X_GrPixelConfig: return false;
93 case kRG_88_GrPixelConfig: return false;
94 case kBGRA_8888_GrPixelConfig: return true;
95 case kSRGBA_8888_GrPixelConfig: return true;
96 case kSBGRA_8888_GrPixelConfig: return true;
97 case kRGBA_1010102_GrPixelConfig: return true;
98 case kRGBA_float_GrPixelConfig: return true;
99 case kRG_float_GrPixelConfig: return false;
100 case kAlpha_half_GrPixelConfig: return false;
101 case kRGBA_half_GrPixelConfig: return true;
102 case kRGBA_half_Clamped_GrPixelConfig: return true;
103 case kRGB_ETC1_GrPixelConfig: return false;
104 case kAlpha_8_as_Alpha_GrPixelConfig: return false;
105 case kAlpha_8_as_Red_GrPixelConfig: return false;
106 case kAlpha_half_as_Red_GrPixelConfig: return false;
107 case kGray_8_as_Lum_GrPixelConfig: return false;
108 case kGray_8_as_Red_GrPixelConfig: return false;
Robert Phillipsfe18de52019-06-06 17:21:50 -0400109 // Experimental (for P016 and P010)
110 case kR_16_GrPixelConfig: return false;
111 case kRG_1616_GrPixelConfig: return false;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400112 }
113 SK_ABORT("Invalid GrPixelConfig");
114 return false;
115}
116
117static bool valid_pixel_conversion(GrColorType cpuColorType, GrPixelConfig gpuConfig,
118 bool premulConversion) {
119 // We only allow premul <-> unpremul conversions for some formats
120 if (premulConversion &&
121 (!valid_premul_color_type(cpuColorType) || !valid_premul_config(gpuConfig))) {
122 return false;
123 }
124 return true;
125}
126
127bool GrSurfaceContext::readPixelsImpl(GrContext* direct, int left, int top, int width,
128 int height, GrColorType dstColorType,
129 SkColorSpace* dstColorSpace, void* buffer, size_t rowBytes,
130 uint32_t pixelOpsFlags) {
131 SkASSERT(buffer);
132
133 GrSurfaceProxy* srcProxy = this->asSurfaceProxy();
134
135 // MDB TODO: delay this instantiation until later in the method
136 if (!srcProxy->instantiate(direct->priv().resourceProvider())) {
137 return false;
138 }
139
140 GrSurface* srcSurface = srcProxy->peekSurface();
141
142 if (!GrSurfacePriv::AdjustReadPixelParams(srcSurface->width(), srcSurface->height(),
143 GrColorTypeBytesPerPixel(dstColorType), &left, &top,
144 &width, &height, &buffer, &rowBytes)) {
145 return false;
146 }
147
148 // TODO: Make GrSurfaceContext know its alpha type and pass dst buffer's alpha type.
149 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags);
150
151 if (!valid_pixel_conversion(dstColorType, srcProxy->config(), unpremul)) {
152 return false;
153 }
154
155 bool needColorConversion =
156 SkColorSpaceXformSteps::Required(this->colorSpaceInfo().colorSpace(), dstColorSpace);
157
158 const GrCaps* caps = direct->priv().caps();
159 // This is the getImageData equivalent to the canvas2D putImageData fast path. We probably don't
160 // care so much about getImageData performance. However, in order to ensure putImageData/
161 // getImageData in "legacy" mode are round-trippable we use the GPU to do the complementary
162 // unpremul step to writeSurfacePixels's premul step (which is determined empirically in
163 // fContext->vaildaPMUPMConversionExists()).
164 bool canvas2DFastPath =
165 unpremul &&
166 !needColorConversion &&
167 (GrColorType::kRGBA_8888 == dstColorType || GrColorType::kBGRA_8888 == dstColorType) &&
168 SkToBool(srcProxy->asTextureProxy()) &&
169 (srcProxy->config() == kRGBA_8888_GrPixelConfig ||
170 srcProxy->config() == kBGRA_8888_GrPixelConfig) &&
171 caps->isConfigRenderable(kRGBA_8888_GrPixelConfig) &&
172 direct->priv().validPMUPMConversionExists();
173
174 if (!caps->surfaceSupportsReadPixels(srcSurface) ||
175 canvas2DFastPath) {
176 GrBackendFormat format;
177 GrPixelConfig config;
178 if (canvas2DFastPath) {
179 config = kRGBA_8888_GrPixelConfig;
180 format = caps->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
181 } else {
182 config = srcProxy->config();
183 format = srcProxy->backendFormat().makeTexture2D();
184 if (!format.isValid()) {
185 return false;
186 }
187 }
188 sk_sp<SkColorSpace> cs = canvas2DFastPath ? nullptr : this->colorSpaceInfo().refColorSpace();
189
190 sk_sp<GrRenderTargetContext> tempCtx = direct->priv().makeDeferredRenderTargetContext(
191 format, SkBackingFit::kApprox, width, height, config, std::move(cs), 1,
192 GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin, nullptr, SkBudgeted::kYes);
193 if (!tempCtx) {
194 return false;
195 }
196
197 std::unique_ptr<GrFragmentProcessor> fp;
198 if (canvas2DFastPath) {
199 fp = direct->priv().createPMToUPMEffect(
200 GrSimpleTextureEffect::Make(sk_ref_sp(srcProxy->asTextureProxy()),
201 SkMatrix::I()));
202 if (dstColorType == GrColorType::kBGRA_8888) {
203 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
204 dstColorType = GrColorType::kRGBA_8888;
205 }
206 } else {
207 fp = GrSimpleTextureEffect::Make(sk_ref_sp(srcProxy->asTextureProxy()), SkMatrix::I());
208 }
209 if (!fp) {
210 return false;
211 }
212 GrPaint paint;
213 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
214 paint.addColorFragmentProcessor(std::move(fp));
215
216 tempCtx->asRenderTargetContext()->fillRectToRect(
217 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
218 SkRect::MakeWH(width, height), SkRect::MakeXYWH(left, top, width, height));
219
220 uint32_t flags = canvas2DFastPath ? 0 : pixelOpsFlags;
221 return tempCtx->readPixelsImpl(direct, 0, 0, width, height, dstColorType, dstColorSpace,
222 buffer, rowBytes, flags);
223 }
224
225 bool convert = unpremul || needColorConversion;
226
227 bool flip = srcProxy->origin() == kBottomLeft_GrSurfaceOrigin;
228 if (flip) {
229 top = srcSurface->height() - top - height;
230 }
231
232 GrColorType allowedColorType = caps->supportedReadPixelsColorType(srcProxy->config(),
233 dstColorType);
234 convert = convert || (dstColorType != allowedColorType);
235
236 SkAutoPixmapStorage tempPixmap;
237 SkPixmap finalPixmap;
238 if (convert) {
239 SkColorType srcSkColorType = GrColorTypeToSkColorType(allowedColorType);
240 SkColorType dstSkColorType = GrColorTypeToSkColorType(dstColorType);
241 bool srcAlwaysOpaque = SkColorTypeIsAlwaysOpaque(srcSkColorType);
242 bool dstAlwaysOpaque = SkColorTypeIsAlwaysOpaque(dstSkColorType);
243 if (kUnknown_SkColorType == srcSkColorType || kUnknown_SkColorType == dstSkColorType) {
244 return false;
245 }
246 auto tempAT = srcAlwaysOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
247 auto tempII = SkImageInfo::Make(width, height, srcSkColorType, tempAT,
248 this->colorSpaceInfo().refColorSpace());
249 SkASSERT(!unpremul || !dstAlwaysOpaque);
250 auto finalAT = (srcAlwaysOpaque || dstAlwaysOpaque)
251 ? kOpaque_SkAlphaType
252 : unpremul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType;
253 auto finalII =
254 SkImageInfo::Make(width, height, dstSkColorType, finalAT, sk_ref_sp(dstColorSpace));
255 if (!SkImageInfoValidConversion(finalII, tempII)) {
256 return false;
257 }
258 if (!tempPixmap.tryAlloc(tempII)) {
259 return false;
260 }
261 finalPixmap.reset(finalII, buffer, rowBytes);
262 buffer = tempPixmap.writable_addr();
263 rowBytes = tempPixmap.rowBytes();
264 // Chrome msan bots require this.
265 sk_bzero(buffer, tempPixmap.computeByteSize());
266 }
267
268 direct->priv().flushSurface(srcProxy);
269
270 if (!direct->priv().getGpu()->readPixels(srcSurface, left, top, width, height, allowedColorType,
271 buffer, rowBytes)) {
272 return false;
273 }
274
275 if (flip) {
276 size_t trimRowBytes = GrColorTypeBytesPerPixel(allowedColorType) * width;
277 std::unique_ptr<char[]> row(new char[trimRowBytes]);
278 char* upper = reinterpret_cast<char*>(buffer);
279 char* lower = reinterpret_cast<char*>(buffer) + (height - 1) * rowBytes;
280 for (int y = 0; y < height / 2; ++y, upper += rowBytes, lower -= rowBytes) {
281 memcpy(row.get(), upper, trimRowBytes);
282 memcpy(upper, lower, trimRowBytes);
283 memcpy(lower, row.get(), trimRowBytes);
284 }
285 }
286 if (convert) {
287 if (!tempPixmap.readPixels(finalPixmap)) {
288 return false;
289 }
290 }
291 return true;
292}
Robert Phillips0d075de2019-03-04 11:08:13 -0500293
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400294bool GrSurfaceContext::readPixels(const SkImageInfo& dstInfo, void* dstBuffer,
295 size_t dstRowBytes, int x, int y, uint32_t flags) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400296 ASSERT_SINGLE_OWNER
297 RETURN_FALSE_IF_ABANDONED
298 SkDEBUGCODE(this->validate();)
Robert Phillips0d075de2019-03-04 11:08:13 -0500299 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::readPixels");
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400300
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400301 // TODO: this seems to duplicate code in SkImage_Gpu::onReadPixels
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400302 if (kUnpremul_SkAlphaType == dstInfo.alphaType() &&
303 !GrPixelConfigIsOpaque(this->asSurfaceProxy()->config())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400304 flags |= kUnpremul_PixelOpsFlag;
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400305 }
Brian Salomonc320b152018-02-20 14:05:36 -0500306 auto colorType = SkColorTypeToGrColorType(dstInfo.colorType());
307 if (GrColorType::kUnknown == colorType) {
308 return false;
309 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500310
311 auto direct = fContext->priv().asDirectContext();
312 if (!direct) {
313 return false;
314 }
315
Greg Daniel6eb8c242019-06-05 10:22:24 -0400316 return this->readPixelsImpl(direct, x, y, dstInfo.width(), dstInfo.height(), colorType,
317 dstInfo.colorSpace(), dstBuffer, dstRowBytes, flags);
318}
319
320bool GrSurfaceContext::writePixelsImpl(GrContext* direct, int left, int top, int width, int height,
321 GrColorType srcColorType, SkColorSpace* srcColorSpace,
322 const void* srcBuffer, size_t srcRowBytes,
323 uint32_t pixelOpsFlags) {
324 if (GrColorType::kUnknown == srcColorType) {
325 return false;
326 }
327
328 GrSurfaceProxy* dstProxy = this->asSurfaceProxy();
329 if (!dstProxy->instantiate(direct->priv().resourceProvider())) {
330 return false;
331 }
332
333 GrSurface* dstSurface = dstProxy->peekSurface();
334
335 if (!GrSurfacePriv::AdjustWritePixelParams(dstSurface->width(), dstSurface->height(),
336 GrColorTypeBytesPerPixel(srcColorType), &left, &top,
337 &width, &height, &srcBuffer, &srcRowBytes)) {
338 return false;
339 }
340
341 // TODO: Make GrSurfaceContext know its alpha type and pass src buffer's alpha type.
342 bool premul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags);
343
344 bool needColorConversion =
345 SkColorSpaceXformSteps::Required(srcColorSpace, this->colorSpaceInfo().colorSpace());
346
347 const GrCaps* caps = direct->priv().caps();
348 // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs
349 // that are premultiplied on the GPU. This is kept as narrow as possible for now.
350 bool canvas2DFastPath =
351 !caps->avoidWritePixelsFastPath() &&
352 premul &&
353 !needColorConversion &&
354 (srcColorType == GrColorType::kRGBA_8888 || srcColorType == GrColorType::kBGRA_8888) &&
355 SkToBool(this->asRenderTargetContext()) &&
356 (dstProxy->config() == kRGBA_8888_GrPixelConfig ||
357 dstProxy->config() == kBGRA_8888_GrPixelConfig) &&
358 direct->priv().caps()->isConfigTexturable(kRGBA_8888_GrPixelConfig) &&
359 direct->priv().validPMUPMConversionExists();
360
361 if (!caps->surfaceSupportsWritePixels(dstSurface) || canvas2DFastPath) {
362 GrSurfaceDesc desc;
363 desc.fWidth = width;
364 desc.fHeight = height;
365 desc.fSampleCnt = 1;
366
367 GrBackendFormat format;
368 if (canvas2DFastPath) {
369 desc.fConfig = kRGBA_8888_GrPixelConfig;
370 format = caps->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
371 } else {
372 desc.fConfig = dstProxy->config();
373 format = dstProxy->backendFormat().makeTexture2D();
374 if (!format.isValid()) {
375 return false;
376 }
377 }
378
379 auto tempProxy = direct->priv().proxyProvider()->createProxy(
Greg Daniel4c6f9b72019-06-06 13:40:59 -0400380 format, desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kApprox, SkBudgeted::kYes);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400381 if (!tempProxy) {
382 return false;
383 }
384 auto tempCtx = direct->priv().drawingManager()->makeTextureContext(
385 tempProxy, this->colorSpaceInfo().refColorSpace());
386 if (!tempCtx) {
387 return false;
388 }
389 uint32_t flags = canvas2DFastPath ? 0 : pixelOpsFlags;
390
391 // In the fast path we always write the srcData to the temp context as though it were RGBA.
392 // When the data is really BGRA the write will cause the R and B channels to be swapped in
393 // the intermediate surface which gets corrected by a swizzle effect when drawing to the
394 // dst.
395 auto tmpColorType = canvas2DFastPath ? GrColorType::kRGBA_8888 : srcColorType;
396 if (!tempCtx->writePixelsImpl(direct, 0, 0, width, height, tmpColorType, srcColorSpace,
397 srcBuffer, srcRowBytes, flags)) {
398 return false;
399 }
400
401 if (this->asRenderTargetContext()) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -0400402 std::unique_ptr<GrFragmentProcessor> fp;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400403 if (canvas2DFastPath) {
404 fp = direct->priv().createUPMToPMEffect(
405 GrSimpleTextureEffect::Make(std::move(tempProxy), SkMatrix::I()));
406 if (srcColorType == GrColorType::kBGRA_8888) {
407 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
408 }
409 } else {
410 fp = GrSimpleTextureEffect::Make(std::move(tempProxy), SkMatrix::I());
411 }
412 if (!fp) {
413 return false;
414 }
415 GrPaint paint;
416 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
417 paint.addColorFragmentProcessor(std::move(fp));
418 this->asRenderTargetContext()->fillRectToRect(
419 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
420 SkRect::MakeXYWH(left, top, width, height), SkRect::MakeWH(width, height));
421 } else {
422 SkIRect srcRect = SkIRect::MakeWH(width, height);
423 SkIPoint dstPoint = SkIPoint::Make(left, top);
Greg Daniel4c6f9b72019-06-06 13:40:59 -0400424 if (!caps->canCopySurface(this->asSurfaceProxy(), tempProxy.get(), srcRect, dstPoint)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400425 return false;
426 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -0400427 SkAssertResult(this->copy(tempProxy.get(), srcRect, dstPoint));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400428 }
429 return true;
430 }
431
432 bool convert = premul || needColorConversion;
433
434 if (!valid_pixel_conversion(srcColorType, dstProxy->config(), premul)) {
435 return false;
436 }
437
438 GrColorType allowedColorType = caps->supportedWritePixelsColorType(dstProxy->config(),
439 srcColorType);
440 convert = convert || (srcColorType != allowedColorType);
441
442 std::unique_ptr<char[]> tempBuffer;
443 if (convert) {
444 auto srcSkColorType = GrColorTypeToSkColorType(srcColorType);
445 auto dstSkColorType = GrColorTypeToSkColorType(allowedColorType);
446 if (kUnknown_SkColorType == srcSkColorType || kUnknown_SkColorType == dstSkColorType) {
447 return false;
448 }
449 auto srcAlphaType = SkColorTypeIsAlwaysOpaque(srcSkColorType)
450 ? kOpaque_SkAlphaType
451 : (premul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType);
452 SkPixmap src(SkImageInfo::Make(width, height, srcSkColorType, srcAlphaType,
453 sk_ref_sp(srcColorSpace)),
454 srcBuffer, srcRowBytes);
455 auto tempSrcII = SkImageInfo::Make(width, height, dstSkColorType, kPremul_SkAlphaType,
456 this->colorSpaceInfo().refColorSpace());
457 auto size = tempSrcII.computeMinByteSize();
458 if (!size) {
459 return false;
460 }
461 tempBuffer.reset(new char[size]);
462 SkPixmap tempSrc(tempSrcII, tempBuffer.get(), tempSrcII.minRowBytes());
463 if (!src.readPixels(tempSrc)) {
464 return false;
465 }
466 srcColorType = allowedColorType;
467 srcBuffer = tempSrc.addr();
468 srcRowBytes = tempSrc.rowBytes();
469 if (dstProxy->origin() == kBottomLeft_GrSurfaceOrigin) {
470 std::unique_ptr<char[]> row(new char[srcRowBytes]);
471 for (int y = 0; y < height / 2; ++y) {
472 memcpy(row.get(), tempSrc.addr(0, y), srcRowBytes);
473 memcpy(tempSrc.writable_addr(0, y), tempSrc.addr(0, height - 1 - y), srcRowBytes);
474 memcpy(tempSrc.writable_addr(0, height - 1 - y), row.get(), srcRowBytes);
475 }
476 top = dstSurface->height() - top - height;
477 }
478 } else if (dstProxy->origin() == kBottomLeft_GrSurfaceOrigin) {
479 size_t trimRowBytes = GrColorTypeBytesPerPixel(srcColorType) * width;
480 tempBuffer.reset(new char[trimRowBytes * height]);
481 char* dst = reinterpret_cast<char*>(tempBuffer.get()) + trimRowBytes * (height - 1);
482 const char* src = reinterpret_cast<const char*>(srcBuffer);
483 for (int i = 0; i < height; ++i, src += srcRowBytes, dst -= trimRowBytes) {
484 memcpy(dst, src, trimRowBytes);
485 }
486 srcBuffer = tempBuffer.get();
487 srcRowBytes = trimRowBytes;
488 top = dstSurface->height() - top - height;
489 }
490
491 // On platforms that prefer flushes over VRAM use (i.e., ANGLE) we're better off forcing a
492 // complete flush here. On platforms that prefer VRAM use over flushes we're better off
493 // giving the drawing manager the chance of skipping the flush (i.e., by passing in the
494 // destination proxy)
495 // TODO: should this policy decision just be moved into the drawing manager?
496 direct->priv().flushSurface(caps->preferVRAMUseOverFlushes() ? dstProxy : nullptr);
497
498 return direct->priv().getGpu()->writePixels(dstSurface, left, top, width, height, srcColorType,
499 srcBuffer, srcRowBytes);
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400500}
501
502bool GrSurfaceContext::writePixels(const SkImageInfo& srcInfo, const void* srcBuffer,
503 size_t srcRowBytes, int x, int y, uint32_t flags) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400504 ASSERT_SINGLE_OWNER
505 RETURN_FALSE_IF_ABANDONED
506 SkDEBUGCODE(this->validate();)
Robert Phillips0d075de2019-03-04 11:08:13 -0500507 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::writePixels");
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400508
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400509 if (kUnpremul_SkAlphaType == srcInfo.alphaType()) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400510 flags |= kUnpremul_PixelOpsFlag;
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400511 }
Brian Salomonc320b152018-02-20 14:05:36 -0500512 auto colorType = SkColorTypeToGrColorType(srcInfo.colorType());
513 if (GrColorType::kUnknown == colorType) {
514 return false;
515 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500516
517 auto direct = fContext->priv().asDirectContext();
518 if (!direct) {
519 return false;
520 }
521
Greg Danielb3f82dd2019-05-29 14:24:32 -0400522 if (this->asSurfaceProxy()->readOnly()) {
523 return false;
524 }
525
Greg Daniel6eb8c242019-06-05 10:22:24 -0400526 return this->writePixelsImpl(direct, x, y, srcInfo.width(), srcInfo.height(), colorType,
527 srcInfo.colorSpace(), srcBuffer, srcRowBytes, flags);
Brian Osman45580d32016-11-23 09:37:01 -0500528}
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400529
530bool GrSurfaceContext::copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) {
531 ASSERT_SINGLE_OWNER
532 RETURN_FALSE_IF_ABANDONED
533 SkDEBUGCODE(this->validate();)
Greg Daniel4c6f9b72019-06-06 13:40:59 -0400534 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::copy");
Greg Daniel25af6712018-04-25 10:44:38 -0400535
Greg Daniel4c6f9b72019-06-06 13:40:59 -0400536 if (!fContext->priv().caps()->canCopySurface(this->asSurfaceProxy(), src, srcRect,
537 dstPoint)) {
Greg Daniel25af6712018-04-25 10:44:38 -0400538 return false;
539 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400540
Greg Daniel4c6f9b72019-06-06 13:40:59 -0400541 return this->getOpList()->copySurface(fContext, this->asSurfaceProxy(),
542 src, srcRect, dstPoint);
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400543}