blob: 071c885caf227d0af17ec65dc9d9dd9cafc71cbc [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.com6f379512011-11-16 20:36:03 +000019 GrPixelConfig config, void* buffer,
20 size_t rowBytes) {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000021 // go through context so that all necessary flushing occurs
bsalomon@google.com6f379512011-11-16 20:36:03 +000022 GrContext* context = this->getContext();
23 if (NULL == context) {
24 return false;
25 }
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000026 return context->readRenderTargetPixels(this,
bsalomon@google.com6f379512011-11-16 20:36:03 +000027 left, top,
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000028 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +000029 config, buffer, rowBytes);
30}
31
32void GrRenderTarget::writePixels(int left, int top, int width, int height,
33 GrPixelConfig config, const void* buffer,
34 size_t rowBytes) {
35 // go through context so that all necessary flushing occurs
36 GrContext* context = this->getContext();
37 if (NULL == context) {
38 return;
39 }
40 context->writeRenderTargetPixels(this,
41 left, top,
42 width, height,
43 config, buffer, rowBytes);
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000044}
45
bsalomon@google.com75f9f252012-01-31 13:35:56 +000046void GrRenderTarget::resolve() {
47 // go through context so that all necessary flushing occurs
48 GrContext* context = this->getContext();
49 if (NULL == context) {
50 return;
51 }
52 context->resolveRenderTarget(this);
53}
54
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000055size_t GrRenderTarget::sizeInBytes() const {
56 int colorBits;
robertphillips@google.come98ade42012-06-13 12:53:07 +000057 if (kUnknown_GrPixelConfig == fDesc.fConfig) {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000058 colorBits = 32; // don't know, make a guess
59 } else {
robertphillips@google.come98ade42012-06-13 12:53:07 +000060 colorBits = GrBytesPerPixel(fDesc.fConfig);
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000061 }
robertphillips@google.come98ade42012-06-13 12:53:07 +000062 uint64_t size = fDesc.fWidth;
63 size *= fDesc.fHeight;
bsalomon@google.comf6ff4952011-08-09 13:32:14 +000064 size *= colorBits;
robertphillips@google.come98ade42012-06-13 12:53:07 +000065 size *= GrMax(1, fDesc.fSampleCnt);
bsalomon@google.comf6ff4952011-08-09 13:32:14 +000066 return (size_t)(size / 8);
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000067}
68
69void GrRenderTarget::flagAsNeedingResolve(const GrIRect* rect) {
70 if (kCanResolve_ResolveType == getResolveType()) {
71 if (NULL != rect) {
72 fResolveRect.join(*rect);
73 if (!fResolveRect.intersect(0, 0, this->width(), this->height())) {
74 fResolveRect.setEmpty();
75 }
76 } else {
77 fResolveRect.setLTRB(0, 0, this->width(), this->height());
78 }
79 }
80}
81
82void GrRenderTarget::overrideResolveRect(const GrIRect rect) {
83 fResolveRect = rect;
84 if (fResolveRect.isEmpty()) {
85 fResolveRect.setLargestInverted();
86 } else {
87 if (!fResolveRect.intersect(0, 0, this->width(), this->height())) {
88 fResolveRect.setLargestInverted();
89 }
90 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000091}
92
93void GrRenderTarget::setStencilBuffer(GrStencilBuffer* stencilBuffer) {
bsalomon@google.com558a75b2011-08-08 17:01:14 +000094 if (NULL != fStencilBuffer) {
95 fStencilBuffer->wasDetachedFromRenderTarget(this);
96 fStencilBuffer->unref();
97 }
98 fStencilBuffer = stencilBuffer;
99 if (NULL != fStencilBuffer) {
100 fStencilBuffer->wasAttachedToRenderTarget(this);
101 fStencilBuffer->ref();
102 }
bsalomon@google.comf6ff4952011-08-09 13:32:14 +0000103}