blob: 49a76149f07c1c99bafde81bc8aab8d57dc14afd [file] [log] [blame]
bsalomon@google.comaa5b6732011-07-29 15:13:20 +00001
2/*
3 * Copyright 2011 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.
7 */
8
9
10#include "GrRenderTarget.h"
11
12#include "GrContext.h"
13#include "GrGpu.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000014#include "GrStencilBuffer.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000015
robertphillips@google.com4d73ac22012-06-13 18:54:08 +000016SK_DEFINE_INST_COUNT(GrRenderTarget)
17
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000018bool GrRenderTarget::readPixels(int left, int top, int width, int height,
bsalomon@google.com0342a852012-08-20 19:22:38 +000019 GrPixelConfig config,
20 void* buffer,
21 size_t rowBytes,
22 uint32_t pixelOpsFlags) {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000023 // go through context so that all necessary flushing occurs
bsalomon@google.com6f379512011-11-16 20:36:03 +000024 GrContext* context = this->getContext();
25 if (NULL == context) {
26 return false;
27 }
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000028 return context->readRenderTargetPixels(this,
bsalomon@google.com0342a852012-08-20 19:22:38 +000029 left, top, width, height,
30 config, buffer, rowBytes,
31 pixelOpsFlags);
bsalomon@google.com6f379512011-11-16 20:36:03 +000032}
33
34void GrRenderTarget::writePixels(int left, int top, int width, int height,
bsalomon@google.com0342a852012-08-20 19:22:38 +000035 GrPixelConfig config,
36 const void* buffer,
37 size_t rowBytes,
38 uint32_t pixelOpsFlags) {
bsalomon@google.com6f379512011-11-16 20:36:03 +000039 // go through context so that all necessary flushing occurs
40 GrContext* context = this->getContext();
41 if (NULL == context) {
42 return;
43 }
44 context->writeRenderTargetPixels(this,
bsalomon@google.com0342a852012-08-20 19:22:38 +000045 left, top, width, height,
46 config, buffer, rowBytes,
47 pixelOpsFlags);
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000048}
49
bsalomon@google.com75f9f252012-01-31 13:35:56 +000050void GrRenderTarget::resolve() {
51 // go through context so that all necessary flushing occurs
52 GrContext* context = this->getContext();
53 if (NULL == context) {
54 return;
55 }
56 context->resolveRenderTarget(this);
57}
58
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000059size_t GrRenderTarget::sizeInBytes() const {
robertphillips@google.comadacc702013-10-14 21:53:24 +000060 size_t colorBits;
robertphillips@google.come98ade42012-06-13 12:53:07 +000061 if (kUnknown_GrPixelConfig == fDesc.fConfig) {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000062 colorBits = 32; // don't know, make a guess
63 } else {
robertphillips@google.come98ade42012-06-13 12:53:07 +000064 colorBits = GrBytesPerPixel(fDesc.fConfig);
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000065 }
robertphillips@google.come98ade42012-06-13 12:53:07 +000066 uint64_t size = fDesc.fWidth;
67 size *= fDesc.fHeight;
bsalomon@google.comf6ff4952011-08-09 13:32:14 +000068 size *= colorBits;
robertphillips@google.come98ade42012-06-13 12:53:07 +000069 size *= GrMax(1, fDesc.fSampleCnt);
bsalomon@google.comf6ff4952011-08-09 13:32:14 +000070 return (size_t)(size / 8);
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000071}
72
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000073void GrRenderTarget::flagAsNeedingResolve(const SkIRect* rect) {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000074 if (kCanResolve_ResolveType == getResolveType()) {
75 if (NULL != rect) {
76 fResolveRect.join(*rect);
77 if (!fResolveRect.intersect(0, 0, this->width(), this->height())) {
78 fResolveRect.setEmpty();
79 }
80 } else {
81 fResolveRect.setLTRB(0, 0, this->width(), this->height());
82 }
83 }
84}
85
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000086void GrRenderTarget::overrideResolveRect(const SkIRect rect) {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000087 fResolveRect = rect;
88 if (fResolveRect.isEmpty()) {
89 fResolveRect.setLargestInverted();
90 } else {
91 if (!fResolveRect.intersect(0, 0, this->width(), this->height())) {
92 fResolveRect.setLargestInverted();
93 }
94 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000095}
96
97void GrRenderTarget::setStencilBuffer(GrStencilBuffer* stencilBuffer) {
bsalomon@google.com76202b82013-05-10 19:08:22 +000098 SkRefCnt_SafeAssign(fStencilBuffer, stencilBuffer);
bsalomon@google.comf6ff4952011-08-09 13:32:14 +000099}
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000100
101void GrRenderTarget::onRelease() {
102 this->setStencilBuffer(NULL);
robertphillips@google.comd3645542012-09-05 18:37:39 +0000103
104 INHERITED::onRelease();
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000105}
106
107void GrRenderTarget::onAbandon() {
108 this->setStencilBuffer(NULL);
robertphillips@google.comd3645542012-09-05 18:37:39 +0000109
110 INHERITED::onAbandon();
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000111}