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 | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 19 | GrPixelConfig config, |
| 20 | void* buffer, |
| 21 | size_t rowBytes, |
| 22 | uint32_t pixelOpsFlags) { |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 23 | // go through context so that all necessary flushing occurs |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 24 | GrContext* context = this->getContext(); |
| 25 | if (NULL == context) { |
| 26 | return false; |
| 27 | } |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 28 | return context->readRenderTargetPixels(this, |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 29 | left, top, width, height, |
| 30 | config, buffer, rowBytes, |
| 31 | pixelOpsFlags); |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | void GrRenderTarget::writePixels(int left, int top, int width, int height, |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 35 | GrPixelConfig config, |
| 36 | const void* buffer, |
| 37 | size_t rowBytes, |
| 38 | uint32_t pixelOpsFlags) { |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 39 | // 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.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 45 | left, top, width, height, |
| 46 | config, buffer, rowBytes, |
| 47 | pixelOpsFlags); |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 48 | } |
| 49 | |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 50 | void 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.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 59 | size_t GrRenderTarget::sizeInBytes() const { |
robertphillips@google.com | adacc70 | 2013-10-14 21:53:24 +0000 | [diff] [blame] | 60 | size_t colorBits; |
robertphillips@google.com | e98ade4 | 2012-06-13 12:53:07 +0000 | [diff] [blame] | 61 | if (kUnknown_GrPixelConfig == fDesc.fConfig) { |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 62 | colorBits = 32; // don't know, make a guess |
| 63 | } else { |
robertphillips@google.com | e98ade4 | 2012-06-13 12:53:07 +0000 | [diff] [blame] | 64 | colorBits = GrBytesPerPixel(fDesc.fConfig); |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 65 | } |
robertphillips@google.com | e98ade4 | 2012-06-13 12:53:07 +0000 | [diff] [blame] | 66 | uint64_t size = fDesc.fWidth; |
| 67 | size *= fDesc.fHeight; |
bsalomon@google.com | f6ff495 | 2011-08-09 13:32:14 +0000 | [diff] [blame] | 68 | size *= colorBits; |
robertphillips@google.com | e98ade4 | 2012-06-13 12:53:07 +0000 | [diff] [blame] | 69 | size *= GrMax(1, fDesc.fSampleCnt); |
bsalomon@google.com | f6ff495 | 2011-08-09 13:32:14 +0000 | [diff] [blame] | 70 | return (size_t)(size / 8); |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 71 | } |
| 72 | |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 73 | void GrRenderTarget::flagAsNeedingResolve(const SkIRect* rect) { |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 74 | 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.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 86 | void GrRenderTarget::overrideResolveRect(const SkIRect rect) { |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 87 | 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.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void GrRenderTarget::setStencilBuffer(GrStencilBuffer* stencilBuffer) { |
bsalomon@google.com | 76202b8 | 2013-05-10 19:08:22 +0000 | [diff] [blame] | 98 | SkRefCnt_SafeAssign(fStencilBuffer, stencilBuffer); |
bsalomon@google.com | f6ff495 | 2011-08-09 13:32:14 +0000 | [diff] [blame] | 99 | } |
robertphillips@google.com | d6bbbf8 | 2012-09-05 15:46:34 +0000 | [diff] [blame] | 100 | |
| 101 | void GrRenderTarget::onRelease() { |
| 102 | this->setStencilBuffer(NULL); |
robertphillips@google.com | d364554 | 2012-09-05 18:37:39 +0000 | [diff] [blame] | 103 | |
| 104 | INHERITED::onRelease(); |
robertphillips@google.com | d6bbbf8 | 2012-09-05 15:46:34 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | void GrRenderTarget::onAbandon() { |
| 108 | this->setStencilBuffer(NULL); |
robertphillips@google.com | d364554 | 2012-09-05 18:37:39 +0000 | [diff] [blame] | 109 | |
| 110 | INHERITED::onAbandon(); |
robertphillips@google.com | d6bbbf8 | 2012-09-05 15:46:34 +0000 | [diff] [blame] | 111 | } |