blob: abd77b9b9c07cfd45f3a099b641cb7959a4fdf2f [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
8#include "GrSurfaceContext.h"
Robert Phillipsa90aa2b2017-04-10 08:19:26 -04009
10#include "GrContextPriv.h"
Robert Phillips2de8cfa2017-06-28 10:33:41 -040011#include "GrDrawingManager.h"
12#include "GrOpList.h"
Brian Osman47299142017-03-07 13:22:22 -050013#include "SkColorSpace_Base.h"
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040014#include "SkGr.h"
Brian Osman45580d32016-11-23 09:37:01 -050015
16#include "../private/GrAuditTrail.h"
17
Robert Phillips2de8cfa2017-06-28 10:33:41 -040018#define ASSERT_SINGLE_OWNER \
19 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
20#define RETURN_FALSE_IF_ABANDONED if (this->drawingManager()->wasAbandoned()) { return false; }
Brian Osman45580d32016-11-23 09:37:01 -050021
22// In MDB mode the reffing of the 'getLastOpList' call's result allows in-progress
23// GrOpLists to be picked up and added to by renderTargetContexts lower in the call
24// stack. When this occurs with a closed GrOpList, a new one will be allocated
25// when the renderTargetContext attempts to use it (via getOpList).
26GrSurfaceContext::GrSurfaceContext(GrContext* context,
Robert Phillips72152832017-01-25 17:31:35 -050027 GrDrawingManager* drawingMgr,
Brian Salomonf3569f02017-10-24 12:52:33 -040028 GrPixelConfig config,
Robert Phillips2c862492017-01-18 10:08:39 -050029 sk_sp<SkColorSpace> colorSpace,
Brian Osman45580d32016-11-23 09:37:01 -050030 GrAuditTrail* auditTrail,
31 GrSingleOwner* singleOwner)
Brian Salomonf3569f02017-10-24 12:52:33 -040032 : fContext(context)
33 , fAuditTrail(auditTrail)
34 , fColorSpaceInfo(std::move(colorSpace), config)
35 , fDrawingManager(drawingMgr)
Brian Osman45580d32016-11-23 09:37:01 -050036#ifdef SK_DEBUG
Brian Salomonf3569f02017-10-24 12:52:33 -040037 , fSingleOwner(singleOwner)
Brian Osman45580d32016-11-23 09:37:01 -050038#endif
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040039{
40}
41
42bool GrSurfaceContext::readPixels(const SkImageInfo& dstInfo, void* dstBuffer,
43 size_t dstRowBytes, int x, int y, uint32_t flags) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -040044 ASSERT_SINGLE_OWNER
45 RETURN_FALSE_IF_ABANDONED
46 SkDEBUGCODE(this->validate();)
47 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrSurfaceContext::readPixels");
48
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040049 // TODO: teach GrRenderTarget to take ImageInfo directly to specify the src pixels
50 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo, *fContext->caps());
51 if (kUnknown_GrPixelConfig == config) {
52 return false;
53 }
54
55 // TODO: this seems to duplicate code in SkImage_Gpu::onReadPixels
56 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
57 flags |= GrContextPriv::kUnpremul_PixelOpsFlag;
58 }
59
Robert Phillipsf41c22f2017-04-18 07:48:58 -040060 return fContext->contextPriv().readSurfacePixels(this, x, y,
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040061 dstInfo.width(), dstInfo.height(), config,
62 dstInfo.colorSpace(),
63 dstBuffer, dstRowBytes, flags);
64}
65
66bool GrSurfaceContext::writePixels(const SkImageInfo& srcInfo, const void* srcBuffer,
67 size_t srcRowBytes, int x, int y, uint32_t flags) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -040068 ASSERT_SINGLE_OWNER
69 RETURN_FALSE_IF_ABANDONED
70 SkDEBUGCODE(this->validate();)
71 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrSurfaceContext::writePixels");
72
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040073 // TODO: teach GrRenderTarget to take ImageInfo directly to specify the src pixels
74 GrPixelConfig config = SkImageInfo2GrPixelConfig(srcInfo, *fContext->caps());
75 if (kUnknown_GrPixelConfig == config) {
76 return false;
77 }
78 if (kUnpremul_SkAlphaType == srcInfo.alphaType()) {
79 flags |= GrContextPriv::kUnpremul_PixelOpsFlag;
80 }
81
Robert Phillipsf41c22f2017-04-18 07:48:58 -040082 return fContext->contextPriv().writeSurfacePixels(this, x, y,
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040083 srcInfo.width(), srcInfo.height(),
84 config, srcInfo.colorSpace(),
85 srcBuffer, srcRowBytes, flags);
Brian Osman45580d32016-11-23 09:37:01 -050086}
Robert Phillips2de8cfa2017-06-28 10:33:41 -040087
88bool GrSurfaceContext::copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) {
89 ASSERT_SINGLE_OWNER
90 RETURN_FALSE_IF_ABANDONED
91 SkDEBUGCODE(this->validate();)
92 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrSurfaceContext::onCopy");
93
94 return this->getOpList()->copySurface(*fContext->caps(),
95 this->asSurfaceProxy(), src, srcRect, dstPoint);
96}