bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 1 | |
| 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.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 14 | #include "GrStencilBuffer.h" |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 15 | |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame^] | 16 | SK_DEFINE_INST_COUNT(GrRenderTarget) |
| 17 | |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 18 | bool GrRenderTarget::readPixels(int left, int top, int width, int height, |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 19 | GrPixelConfig config, void* buffer, |
| 20 | size_t rowBytes) { |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 21 | // go through context so that all necessary flushing occurs |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 22 | GrContext* context = this->getContext(); |
| 23 | if (NULL == context) { |
| 24 | return false; |
| 25 | } |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 26 | return context->readRenderTargetPixels(this, |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 27 | left, top, |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 28 | width, height, |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 29 | config, buffer, rowBytes); |
| 30 | } |
| 31 | |
| 32 | void 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.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 44 | } |
| 45 | |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 46 | void 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.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 55 | size_t GrRenderTarget::sizeInBytes() const { |
| 56 | int colorBits; |
robertphillips@google.com | e98ade4 | 2012-06-13 12:53:07 +0000 | [diff] [blame] | 57 | if (kUnknown_GrPixelConfig == fDesc.fConfig) { |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 58 | colorBits = 32; // don't know, make a guess |
| 59 | } else { |
robertphillips@google.com | e98ade4 | 2012-06-13 12:53:07 +0000 | [diff] [blame] | 60 | colorBits = GrBytesPerPixel(fDesc.fConfig); |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 61 | } |
robertphillips@google.com | e98ade4 | 2012-06-13 12:53:07 +0000 | [diff] [blame] | 62 | uint64_t size = fDesc.fWidth; |
| 63 | size *= fDesc.fHeight; |
bsalomon@google.com | f6ff495 | 2011-08-09 13:32:14 +0000 | [diff] [blame] | 64 | size *= colorBits; |
robertphillips@google.com | e98ade4 | 2012-06-13 12:53:07 +0000 | [diff] [blame] | 65 | size *= GrMax(1, fDesc.fSampleCnt); |
bsalomon@google.com | f6ff495 | 2011-08-09 13:32:14 +0000 | [diff] [blame] | 66 | return (size_t)(size / 8); |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void 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 | |
| 82 | void 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.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | void GrRenderTarget::setStencilBuffer(GrStencilBuffer* stencilBuffer) { |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 94 | 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.com | f6ff495 | 2011-08-09 13:32:14 +0000 | [diff] [blame] | 103 | } |