blob: 71113b48bcebd80f60b1e5ca58594a7ec4ff6ec0 [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
Greg Daniel46cfbc62019-06-07 11:43:30 -04008#include "src/gpu/GrSurfaceContext.h"
9
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/private/GrAuditTrail.h"
11#include "include/private/GrOpList.h"
12#include "include/private/GrRecordingContext.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040013#include "src/core/SkAutoPixmapStorage.h"
14#include "src/gpu/GrClip.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrContextPriv.h"
16#include "src/gpu/GrDrawingManager.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040017#include "src/gpu/GrGpu.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrRecordingContextPriv.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040019#include "src/gpu/GrRenderTargetContext.h"
Greg Daniel46cfbc62019-06-07 11:43:30 -040020#include "src/gpu/GrSurfaceContextPriv.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040021#include "src/gpu/GrSurfacePriv.h"
22#include "src/gpu/GrTextureContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/SkGr.h"
Brian Osman45580d32016-11-23 09:37:01 -050024
Robert Phillips2de8cfa2017-06-28 10:33:41 -040025#define ASSERT_SINGLE_OWNER \
26 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
Robert Phillips69893702019-02-22 11:16:30 -050027#define RETURN_FALSE_IF_ABANDONED if (this->fContext->priv().abandoned()) { return false; }
Brian Osman45580d32016-11-23 09:37:01 -050028
29// In MDB mode the reffing of the 'getLastOpList' call's result allows in-progress
30// GrOpLists to be picked up and added to by renderTargetContexts lower in the call
31// stack. When this occurs with a closed GrOpList, a new one will be allocated
32// when the renderTargetContext attempts to use it (via getOpList).
Robert Phillips69893702019-02-22 11:16:30 -050033GrSurfaceContext::GrSurfaceContext(GrRecordingContext* context,
Brian Salomonf3569f02017-10-24 12:52:33 -040034 GrPixelConfig config,
Robert Phillips0d075de2019-03-04 11:08:13 -050035 sk_sp<SkColorSpace> colorSpace)
Brian Salomonf3569f02017-10-24 12:52:33 -040036 : fContext(context)
Robert Phillips0d075de2019-03-04 11:08:13 -050037 , fColorSpaceInfo(std::move(colorSpace), config) {
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040038}
39
Robert Phillips0d075de2019-03-04 11:08:13 -050040GrAuditTrail* GrSurfaceContext::auditTrail() {
41 return fContext->priv().auditTrail();
42}
43
44GrDrawingManager* GrSurfaceContext::drawingManager() {
45 return fContext->priv().drawingManager();
46}
47
48const GrDrawingManager* GrSurfaceContext::drawingManager() const {
49 return fContext->priv().drawingManager();
50}
51
52#ifdef SK_DEBUG
53GrSingleOwner* GrSurfaceContext::singleOwner() {
54 return fContext->priv().singleOwner();
55}
56#endif
Greg Daniel6eb8c242019-06-05 10:22:24 -040057static bool valid_premul_color_type(GrColorType ct) {
58 switch (ct) {
59 case GrColorType::kUnknown: return false;
60 case GrColorType::kAlpha_8: return false;
61 case GrColorType::kRGB_565: return false;
62 case GrColorType::kABGR_4444: return true;
63 case GrColorType::kRGBA_8888: return true;
64 case GrColorType::kRGB_888x: return false;
65 case GrColorType::kRG_88: return false;
66 case GrColorType::kBGRA_8888: return true;
67 case GrColorType::kRGBA_1010102: return true;
68 case GrColorType::kGray_8: return false;
69 case GrColorType::kAlpha_F16: return false;
70 case GrColorType::kRGBA_F16: return true;
71 case GrColorType::kRGBA_F16_Clamped: return true;
72 case GrColorType::kRG_F32: return false;
73 case GrColorType::kRGBA_F32: return true;
74 case GrColorType::kRGB_ETC1: return false;
Robert Phillipsf83c4682019-06-13 16:49:21 +000075 // Experimental (for P016 and P010)
Robert Phillipsfe18de52019-06-06 17:21:50 -040076 case GrColorType::kR_16: return false;
77 case GrColorType::kRG_1616: return false;
Greg Daniel6eb8c242019-06-05 10:22:24 -040078 }
79 SK_ABORT("Invalid GrColorType");
80 return false;
81}
82
83// TODO: This will be removed when GrSurfaceContexts are aware of their color types.
84// (skbug.com/6718)
85static bool valid_premul_config(GrPixelConfig config) {
86 switch (config) {
87 case kUnknown_GrPixelConfig: return false;
88 case kAlpha_8_GrPixelConfig: return false;
89 case kGray_8_GrPixelConfig: return false;
90 case kRGB_565_GrPixelConfig: return false;
91 case kRGBA_4444_GrPixelConfig: return true;
92 case kRGBA_8888_GrPixelConfig: return true;
93 case kRGB_888_GrPixelConfig: return false;
94 case kRGB_888X_GrPixelConfig: return false;
95 case kRG_88_GrPixelConfig: return false;
96 case kBGRA_8888_GrPixelConfig: return true;
97 case kSRGBA_8888_GrPixelConfig: return true;
98 case kSBGRA_8888_GrPixelConfig: return true;
99 case kRGBA_1010102_GrPixelConfig: return true;
100 case kRGBA_float_GrPixelConfig: return true;
101 case kRG_float_GrPixelConfig: return false;
102 case kAlpha_half_GrPixelConfig: return false;
103 case kRGBA_half_GrPixelConfig: return true;
104 case kRGBA_half_Clamped_GrPixelConfig: return true;
105 case kRGB_ETC1_GrPixelConfig: return false;
106 case kAlpha_8_as_Alpha_GrPixelConfig: return false;
107 case kAlpha_8_as_Red_GrPixelConfig: return false;
108 case kAlpha_half_as_Red_GrPixelConfig: return false;
109 case kGray_8_as_Lum_GrPixelConfig: return false;
110 case kGray_8_as_Red_GrPixelConfig: return false;
Robert Phillipsf83c4682019-06-13 16:49:21 +0000111 // Experimental (for P016 and P010)
Robert Phillipsfe18de52019-06-06 17:21:50 -0400112 case kR_16_GrPixelConfig: return false;
113 case kRG_1616_GrPixelConfig: return false;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400114 }
115 SK_ABORT("Invalid GrPixelConfig");
116 return false;
117}
118
119static bool valid_pixel_conversion(GrColorType cpuColorType, GrPixelConfig gpuConfig,
120 bool premulConversion) {
121 // We only allow premul <-> unpremul conversions for some formats
122 if (premulConversion &&
123 (!valid_premul_color_type(cpuColorType) || !valid_premul_config(gpuConfig))) {
124 return false;
125 }
126 return true;
127}
128
129bool GrSurfaceContext::readPixelsImpl(GrContext* direct, int left, int top, int width,
130 int height, GrColorType dstColorType,
131 SkColorSpace* dstColorSpace, void* buffer, size_t rowBytes,
132 uint32_t pixelOpsFlags) {
133 SkASSERT(buffer);
134
135 GrSurfaceProxy* srcProxy = this->asSurfaceProxy();
136
137 // MDB TODO: delay this instantiation until later in the method
138 if (!srcProxy->instantiate(direct->priv().resourceProvider())) {
139 return false;
140 }
141
142 GrSurface* srcSurface = srcProxy->peekSurface();
143
144 if (!GrSurfacePriv::AdjustReadPixelParams(srcSurface->width(), srcSurface->height(),
145 GrColorTypeBytesPerPixel(dstColorType), &left, &top,
146 &width, &height, &buffer, &rowBytes)) {
147 return false;
148 }
149
150 // TODO: Make GrSurfaceContext know its alpha type and pass dst buffer's alpha type.
151 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags);
152
153 if (!valid_pixel_conversion(dstColorType, srcProxy->config(), unpremul)) {
154 return false;
155 }
156
157 bool needColorConversion =
158 SkColorSpaceXformSteps::Required(this->colorSpaceInfo().colorSpace(), dstColorSpace);
159
160 const GrCaps* caps = direct->priv().caps();
161 // This is the getImageData equivalent to the canvas2D putImageData fast path. We probably don't
162 // care so much about getImageData performance. However, in order to ensure putImageData/
163 // getImageData in "legacy" mode are round-trippable we use the GPU to do the complementary
164 // unpremul step to writeSurfacePixels's premul step (which is determined empirically in
165 // fContext->vaildaPMUPMConversionExists()).
166 bool canvas2DFastPath =
167 unpremul &&
168 !needColorConversion &&
169 (GrColorType::kRGBA_8888 == dstColorType || GrColorType::kBGRA_8888 == dstColorType) &&
170 SkToBool(srcProxy->asTextureProxy()) &&
171 (srcProxy->config() == kRGBA_8888_GrPixelConfig ||
172 srcProxy->config() == kBGRA_8888_GrPixelConfig) &&
173 caps->isConfigRenderable(kRGBA_8888_GrPixelConfig) &&
174 direct->priv().validPMUPMConversionExists();
175
176 if (!caps->surfaceSupportsReadPixels(srcSurface) ||
177 canvas2DFastPath) {
178 GrBackendFormat format;
179 GrPixelConfig config;
180 if (canvas2DFastPath) {
181 config = kRGBA_8888_GrPixelConfig;
182 format = caps->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
183 } else {
184 config = srcProxy->config();
185 format = srcProxy->backendFormat().makeTexture2D();
186 if (!format.isValid()) {
187 return false;
188 }
189 }
190 sk_sp<SkColorSpace> cs = canvas2DFastPath ? nullptr : this->colorSpaceInfo().refColorSpace();
191
192 sk_sp<GrRenderTargetContext> tempCtx = direct->priv().makeDeferredRenderTargetContext(
193 format, SkBackingFit::kApprox, width, height, config, std::move(cs), 1,
194 GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin, nullptr, SkBudgeted::kYes);
195 if (!tempCtx) {
196 return false;
197 }
198
199 std::unique_ptr<GrFragmentProcessor> fp;
200 if (canvas2DFastPath) {
201 fp = direct->priv().createPMToUPMEffect(
202 GrSimpleTextureEffect::Make(sk_ref_sp(srcProxy->asTextureProxy()),
203 SkMatrix::I()));
204 if (dstColorType == GrColorType::kBGRA_8888) {
205 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
206 dstColorType = GrColorType::kRGBA_8888;
207 }
208 } else {
209 fp = GrSimpleTextureEffect::Make(sk_ref_sp(srcProxy->asTextureProxy()), SkMatrix::I());
210 }
211 if (!fp) {
212 return false;
213 }
214 GrPaint paint;
215 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
216 paint.addColorFragmentProcessor(std::move(fp));
217
218 tempCtx->asRenderTargetContext()->fillRectToRect(
219 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
220 SkRect::MakeWH(width, height), SkRect::MakeXYWH(left, top, width, height));
221
222 uint32_t flags = canvas2DFastPath ? 0 : pixelOpsFlags;
223 return tempCtx->readPixelsImpl(direct, 0, 0, width, height, dstColorType, dstColorSpace,
224 buffer, rowBytes, flags);
225 }
226
Robert Phillipsc34d9932019-06-18 11:57:12 +0000227 bool convert = unpremul || needColorConversion;
228
Greg Daniel6eb8c242019-06-05 10:22:24 -0400229 bool flip = srcProxy->origin() == kBottomLeft_GrSurfaceOrigin;
Robert Phillipsc34d9932019-06-18 11:57:12 +0000230 if (flip) {
231 top = srcSurface->height() - top - height;
232 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400233
Robert Phillipsc34d9932019-06-18 11:57:12 +0000234 GrColorType allowedColorType = caps->supportedReadPixelsColorType(srcProxy->config(),
235 dstColorType);
236 convert = convert || (dstColorType != allowedColorType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400237
Robert Phillipsc34d9932019-06-18 11:57:12 +0000238 SkAutoPixmapStorage tempPixmap;
239 SkPixmap finalPixmap;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400240 if (convert) {
Robert Phillipsc34d9932019-06-18 11:57:12 +0000241 SkColorType srcSkColorType = GrColorTypeToSkColorType(allowedColorType);
242 SkColorType dstSkColorType = GrColorTypeToSkColorType(dstColorType);
243 bool srcAlwaysOpaque = SkColorTypeIsAlwaysOpaque(srcSkColorType);
244 bool dstAlwaysOpaque = SkColorTypeIsAlwaysOpaque(dstSkColorType);
245 if (kUnknown_SkColorType == srcSkColorType || kUnknown_SkColorType == dstSkColorType) {
246 return false;
247 }
248 auto tempAT = srcAlwaysOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
249 auto tempII = SkImageInfo::Make(width, height, srcSkColorType, tempAT,
250 this->colorSpaceInfo().refColorSpace());
251 SkASSERT(!unpremul || !dstAlwaysOpaque);
252 auto finalAT = (srcAlwaysOpaque || dstAlwaysOpaque)
253 ? kOpaque_SkAlphaType
254 : unpremul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType;
255 auto finalII =
256 SkImageInfo::Make(width, height, dstSkColorType, finalAT, sk_ref_sp(dstColorSpace));
257 if (!SkImageInfoValidConversion(finalII, tempII)) {
258 return false;
259 }
260 if (!tempPixmap.tryAlloc(tempII)) {
261 return false;
262 }
263 finalPixmap.reset(finalII, buffer, rowBytes);
264 buffer = tempPixmap.writable_addr();
265 rowBytes = tempPixmap.rowBytes();
266 // Chrome msan bots require this.
267 sk_bzero(buffer, tempPixmap.computeByteSize());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400268 }
269
270 direct->priv().flushSurface(srcProxy);
271
Robert Phillipsc34d9932019-06-18 11:57:12 +0000272 if (!direct->priv().getGpu()->readPixels(srcSurface, left, top, width, height, allowedColorType,
273 buffer, rowBytes)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400274 return false;
275 }
276
Robert Phillipsc34d9932019-06-18 11:57:12 +0000277 if (flip) {
278 size_t trimRowBytes = GrColorTypeBytesPerPixel(allowedColorType) * width;
279 std::unique_ptr<char[]> row(new char[trimRowBytes]);
280 char* upper = reinterpret_cast<char*>(buffer);
281 char* lower = reinterpret_cast<char*>(buffer) + (height - 1) * rowBytes;
282 for (int y = 0; y < height / 2; ++y, upper += rowBytes, lower -= rowBytes) {
283 memcpy(row.get(), upper, trimRowBytes);
284 memcpy(upper, lower, trimRowBytes);
285 memcpy(lower, row.get(), trimRowBytes);
286 }
287 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400288 if (convert) {
Robert Phillipsc34d9932019-06-18 11:57:12 +0000289 if (!tempPixmap.readPixels(finalPixmap)) {
290 return false;
291 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400292 }
293 return true;
294}
Robert Phillips0d075de2019-03-04 11:08:13 -0500295
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400296bool GrSurfaceContext::readPixels(const SkImageInfo& dstInfo, void* dstBuffer,
297 size_t dstRowBytes, int x, int y, uint32_t flags) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400298 ASSERT_SINGLE_OWNER
299 RETURN_FALSE_IF_ABANDONED
300 SkDEBUGCODE(this->validate();)
Robert Phillips0d075de2019-03-04 11:08:13 -0500301 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::readPixels");
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400302
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400303 // TODO: this seems to duplicate code in SkImage_Gpu::onReadPixels
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400304 if (kUnpremul_SkAlphaType == dstInfo.alphaType() &&
305 !GrPixelConfigIsOpaque(this->asSurfaceProxy()->config())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400306 flags |= kUnpremul_PixelOpsFlag;
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400307 }
Brian Salomonc320b152018-02-20 14:05:36 -0500308 auto colorType = SkColorTypeToGrColorType(dstInfo.colorType());
309 if (GrColorType::kUnknown == colorType) {
310 return false;
311 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500312
313 auto direct = fContext->priv().asDirectContext();
314 if (!direct) {
315 return false;
316 }
317
Greg Daniel6eb8c242019-06-05 10:22:24 -0400318 return this->readPixelsImpl(direct, x, y, dstInfo.width(), dstInfo.height(), colorType,
319 dstInfo.colorSpace(), dstBuffer, dstRowBytes, flags);
320}
321
322bool GrSurfaceContext::writePixelsImpl(GrContext* direct, int left, int top, int width, int height,
323 GrColorType srcColorType, SkColorSpace* srcColorSpace,
324 const void* srcBuffer, size_t srcRowBytes,
325 uint32_t pixelOpsFlags) {
326 if (GrColorType::kUnknown == srcColorType) {
327 return false;
328 }
329
330 GrSurfaceProxy* dstProxy = this->asSurfaceProxy();
331 if (!dstProxy->instantiate(direct->priv().resourceProvider())) {
332 return false;
333 }
334
335 GrSurface* dstSurface = dstProxy->peekSurface();
336
337 if (!GrSurfacePriv::AdjustWritePixelParams(dstSurface->width(), dstSurface->height(),
338 GrColorTypeBytesPerPixel(srcColorType), &left, &top,
339 &width, &height, &srcBuffer, &srcRowBytes)) {
340 return false;
341 }
342
343 // TODO: Make GrSurfaceContext know its alpha type and pass src buffer's alpha type.
344 bool premul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags);
345
346 bool needColorConversion =
347 SkColorSpaceXformSteps::Required(srcColorSpace, this->colorSpaceInfo().colorSpace());
348
349 const GrCaps* caps = direct->priv().caps();
350 // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs
351 // that are premultiplied on the GPU. This is kept as narrow as possible for now.
352 bool canvas2DFastPath =
353 !caps->avoidWritePixelsFastPath() &&
354 premul &&
355 !needColorConversion &&
356 (srcColorType == GrColorType::kRGBA_8888 || srcColorType == GrColorType::kBGRA_8888) &&
357 SkToBool(this->asRenderTargetContext()) &&
358 (dstProxy->config() == kRGBA_8888_GrPixelConfig ||
359 dstProxy->config() == kBGRA_8888_GrPixelConfig) &&
360 direct->priv().caps()->isConfigTexturable(kRGBA_8888_GrPixelConfig) &&
361 direct->priv().validPMUPMConversionExists();
362
363 if (!caps->surfaceSupportsWritePixels(dstSurface) || canvas2DFastPath) {
364 GrSurfaceDesc desc;
365 desc.fWidth = width;
366 desc.fHeight = height;
367 desc.fSampleCnt = 1;
368
369 GrBackendFormat format;
370 if (canvas2DFastPath) {
371 desc.fConfig = kRGBA_8888_GrPixelConfig;
372 format = caps->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
373 } else {
374 desc.fConfig = dstProxy->config();
375 format = dstProxy->backendFormat().makeTexture2D();
376 if (!format.isValid()) {
377 return false;
378 }
379 }
380
Greg Daniel2e52ad12019-06-13 10:04:16 -0400381 // It is more efficient for us to write pixels into a top left origin so we prefer that.
382 // However, if the final proxy isn't a render target then we must use a copy to move the
383 // data into it which requires the origins to match. If the final proxy is a render target
384 // we can use a draw instead which doesn't have this origin restriction. Thus for render
385 // targets we will use top left and otherwise we will make the origins match.
Robert Phillipsc34d9932019-06-18 11:57:12 +0000386 GrSurfaceOrigin tempOrigin = this->asRenderTargetContext() ? kTopLeft_GrSurfaceOrigin :
387 dstProxy->origin();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400388 auto tempProxy = direct->priv().proxyProvider()->createProxy(
Greg Daniel2e52ad12019-06-13 10:04:16 -0400389 format, desc, tempOrigin, SkBackingFit::kApprox, SkBudgeted::kYes);
390
Greg Daniel6eb8c242019-06-05 10:22:24 -0400391 if (!tempProxy) {
392 return false;
393 }
394 auto tempCtx = direct->priv().drawingManager()->makeTextureContext(
395 tempProxy, this->colorSpaceInfo().refColorSpace());
396 if (!tempCtx) {
397 return false;
398 }
399 uint32_t flags = canvas2DFastPath ? 0 : pixelOpsFlags;
400
401 // In the fast path we always write the srcData to the temp context as though it were RGBA.
402 // When the data is really BGRA the write will cause the R and B channels to be swapped in
403 // the intermediate surface which gets corrected by a swizzle effect when drawing to the
404 // dst.
405 auto tmpColorType = canvas2DFastPath ? GrColorType::kRGBA_8888 : srcColorType;
406 if (!tempCtx->writePixelsImpl(direct, 0, 0, width, height, tmpColorType, srcColorSpace,
407 srcBuffer, srcRowBytes, flags)) {
408 return false;
409 }
410
411 if (this->asRenderTargetContext()) {
Greg Daniel46cfbc62019-06-07 11:43:30 -0400412 std::unique_ptr<GrFragmentProcessor> fp;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400413 if (canvas2DFastPath) {
414 fp = direct->priv().createUPMToPMEffect(
415 GrSimpleTextureEffect::Make(std::move(tempProxy), SkMatrix::I()));
416 if (srcColorType == GrColorType::kBGRA_8888) {
417 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
418 }
419 } else {
420 fp = GrSimpleTextureEffect::Make(std::move(tempProxy), SkMatrix::I());
421 }
422 if (!fp) {
423 return false;
424 }
425 GrPaint paint;
426 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
427 paint.addColorFragmentProcessor(std::move(fp));
428 this->asRenderTargetContext()->fillRectToRect(
429 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
430 SkRect::MakeXYWH(left, top, width, height), SkRect::MakeWH(width, height));
431 } else {
432 SkIRect srcRect = SkIRect::MakeWH(width, height);
433 SkIPoint dstPoint = SkIPoint::Make(left, top);
Greg Daniel46cfbc62019-06-07 11:43:30 -0400434 if (!this->copy(tempProxy.get(), srcRect, dstPoint)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400435 return false;
436 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400437 }
438 return true;
439 }
440
Robert Phillipsc34d9932019-06-18 11:57:12 +0000441 bool convert = premul || needColorConversion;
442
Greg Daniel6eb8c242019-06-05 10:22:24 -0400443 if (!valid_pixel_conversion(srcColorType, dstProxy->config(), premul)) {
444 return false;
445 }
446
447 GrColorType allowedColorType = caps->supportedWritePixelsColorType(dstProxy->config(),
448 srcColorType);
Robert Phillipsc34d9932019-06-18 11:57:12 +0000449 convert = convert || (srcColorType != allowedColorType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400450
Robert Phillipsc34d9932019-06-18 11:57:12 +0000451 std::unique_ptr<char[]> tempBuffer;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400452 if (convert) {
Robert Phillipsc34d9932019-06-18 11:57:12 +0000453 auto srcSkColorType = GrColorTypeToSkColorType(srcColorType);
454 auto dstSkColorType = GrColorTypeToSkColorType(allowedColorType);
455 if (kUnknown_SkColorType == srcSkColorType || kUnknown_SkColorType == dstSkColorType) {
456 return false;
457 }
458 auto srcAlphaType = SkColorTypeIsAlwaysOpaque(srcSkColorType)
459 ? kOpaque_SkAlphaType
460 : (premul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType);
461 SkPixmap src(SkImageInfo::Make(width, height, srcSkColorType, srcAlphaType,
462 sk_ref_sp(srcColorSpace)),
463 srcBuffer, srcRowBytes);
464 auto tempSrcII = SkImageInfo::Make(width, height, dstSkColorType, kPremul_SkAlphaType,
465 this->colorSpaceInfo().refColorSpace());
466 auto size = tempSrcII.computeMinByteSize();
467 if (!size) {
468 return false;
469 }
470 tempBuffer.reset(new char[size]);
471 SkPixmap tempSrc(tempSrcII, tempBuffer.get(), tempSrcII.minRowBytes());
472 if (!src.readPixels(tempSrc)) {
473 return false;
474 }
475 srcColorType = allowedColorType;
476 srcBuffer = tempSrc.addr();
477 srcRowBytes = tempSrc.rowBytes();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400478 if (dstProxy->origin() == kBottomLeft_GrSurfaceOrigin) {
Robert Phillipsc34d9932019-06-18 11:57:12 +0000479 std::unique_ptr<char[]> row(new char[srcRowBytes]);
480 for (int y = 0; y < height / 2; ++y) {
481 memcpy(row.get(), tempSrc.addr(0, y), srcRowBytes);
482 memcpy(tempSrc.writable_addr(0, y), tempSrc.addr(0, height - 1 - y), srcRowBytes);
483 memcpy(tempSrc.writable_addr(0, height - 1 - y), row.get(), srcRowBytes);
484 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400485 top = dstSurface->height() - top - height;
486 }
Robert Phillipsc34d9932019-06-18 11:57:12 +0000487 } else if (dstProxy->origin() == kBottomLeft_GrSurfaceOrigin) {
488 size_t trimRowBytes = GrColorTypeBytesPerPixel(srcColorType) * width;
489 tempBuffer.reset(new char[trimRowBytes * height]);
490 char* dst = reinterpret_cast<char*>(tempBuffer.get()) + trimRowBytes * (height - 1);
491 const char* src = reinterpret_cast<const char*>(srcBuffer);
492 for (int i = 0; i < height; ++i, src += srcRowBytes, dst -= trimRowBytes) {
493 memcpy(dst, src, trimRowBytes);
494 }
495 srcBuffer = tempBuffer.get();
496 srcRowBytes = trimRowBytes;
497 top = dstSurface->height() - top - height;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400498 }
499
500 // On platforms that prefer flushes over VRAM use (i.e., ANGLE) we're better off forcing a
501 // complete flush here. On platforms that prefer VRAM use over flushes we're better off
502 // giving the drawing manager the chance of skipping the flush (i.e., by passing in the
503 // destination proxy)
504 // TODO: should this policy decision just be moved into the drawing manager?
505 direct->priv().flushSurface(caps->preferVRAMUseOverFlushes() ? dstProxy : nullptr);
506
507 return direct->priv().getGpu()->writePixels(dstSurface, left, top, width, height, srcColorType,
508 srcBuffer, srcRowBytes);
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400509}
510
511bool GrSurfaceContext::writePixels(const SkImageInfo& srcInfo, const void* srcBuffer,
512 size_t srcRowBytes, int x, int y, uint32_t flags) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400513 ASSERT_SINGLE_OWNER
514 RETURN_FALSE_IF_ABANDONED
515 SkDEBUGCODE(this->validate();)
Robert Phillips0d075de2019-03-04 11:08:13 -0500516 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::writePixels");
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400517
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400518 if (kUnpremul_SkAlphaType == srcInfo.alphaType()) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400519 flags |= kUnpremul_PixelOpsFlag;
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400520 }
Brian Salomonc320b152018-02-20 14:05:36 -0500521 auto colorType = SkColorTypeToGrColorType(srcInfo.colorType());
522 if (GrColorType::kUnknown == colorType) {
523 return false;
524 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500525
526 auto direct = fContext->priv().asDirectContext();
527 if (!direct) {
528 return false;
529 }
530
Greg Danielb3f82dd2019-05-29 14:24:32 -0400531 if (this->asSurfaceProxy()->readOnly()) {
532 return false;
533 }
534
Greg Daniel6eb8c242019-06-05 10:22:24 -0400535 return this->writePixelsImpl(direct, x, y, srcInfo.width(), srcInfo.height(), colorType,
536 srcInfo.colorSpace(), srcBuffer, srcRowBytes, flags);
Brian Osman45580d32016-11-23 09:37:01 -0500537}
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400538
539bool GrSurfaceContext::copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) {
540 ASSERT_SINGLE_OWNER
541 RETURN_FALSE_IF_ABANDONED
542 SkDEBUGCODE(this->validate();)
Greg Daniel46cfbc62019-06-07 11:43:30 -0400543 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy");
Greg Daniel25af6712018-04-25 10:44:38 -0400544
Greg Daniel46cfbc62019-06-07 11:43:30 -0400545 SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal);
546 SkASSERT(src->origin() == this->asSurfaceProxy()->origin());
547
548 GrSurfaceProxy* dst = this->asSurfaceProxy();
549
550 if (!fContext->priv().caps()->canCopySurface(dst, src, srcRect, dstPoint)) {
Greg Daniel25af6712018-04-25 10:44:38 -0400551 return false;
552 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400553
Greg Daniel46cfbc62019-06-07 11:43:30 -0400554 return this->getOpList()->copySurface(fContext, dst, src, srcRect, dstPoint);
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400555}
Greg Daniel46cfbc62019-06-07 11:43:30 -0400556