epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2010 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. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 10 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 11 | #include "SkGrTexturePixelRef.h" |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame^] | 12 | #include "GrContext.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 13 | #include "GrTexture.h" |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame^] | 14 | #include "SkGr.h" |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 15 | #include "SkRect.h" |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 16 | |
| 17 | // since we call lockPixels recursively on fBitmap, we need a distinct mutex, |
| 18 | // to avoid deadlock with the default one provided by SkPixelRef. |
| 19 | static SkMutex gROLockPixelsPixelRefMutex; |
| 20 | |
| 21 | SkROLockPixelsPixelRef::SkROLockPixelsPixelRef() : INHERITED(&gROLockPixelsPixelRefMutex) { |
| 22 | } |
| 23 | |
| 24 | SkROLockPixelsPixelRef::~SkROLockPixelsPixelRef() { |
| 25 | } |
| 26 | |
| 27 | void* SkROLockPixelsPixelRef::onLockPixels(SkColorTable** ctable) { |
| 28 | if (ctable) { |
| 29 | *ctable = NULL; |
| 30 | } |
| 31 | fBitmap.reset(); |
| 32 | // SkDebugf("---------- calling readpixels in support of lockpixels\n"); |
| 33 | if (!this->onReadPixels(&fBitmap, NULL)) { |
| 34 | SkDebugf("SkROLockPixelsPixelRef::onLockPixels failed!\n"); |
| 35 | return NULL; |
| 36 | } |
| 37 | fBitmap.lockPixels(); |
| 38 | return fBitmap.getPixels(); |
| 39 | } |
| 40 | |
| 41 | void SkROLockPixelsPixelRef::onUnlockPixels() { |
| 42 | fBitmap.unlockPixels(); |
| 43 | } |
| 44 | |
| 45 | bool SkROLockPixelsPixelRef::onLockPixelsAreWritable() const { |
| 46 | return false; |
| 47 | } |
| 48 | |
| 49 | /////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 50 | |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame^] | 51 | static SkGrTexturePixelRef* copyToTexturePixelRef(GrTexture* texture, |
| 52 | SkBitmap::Config dstConfig) { |
| 53 | if (NULL == texture) { |
| 54 | return NULL; |
| 55 | } |
| 56 | GrContext* context = texture->getContext(); |
| 57 | if (NULL == context) { |
| 58 | return NULL; |
| 59 | } |
| 60 | GrTextureDesc desc; |
| 61 | |
| 62 | desc.fWidth = texture->width(); |
| 63 | desc.fHeight = texture->height(); |
| 64 | desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit; |
| 65 | desc.fConfig = SkGr::BitmapConfig2PixelConfig(dstConfig, false); |
| 66 | desc.fAALevel = kNone_GrAALevel; |
| 67 | |
| 68 | GrTexture* dst = context->createUncachedTexture(desc, NULL, 0); |
| 69 | if (NULL == dst) { |
| 70 | return NULL; |
| 71 | } |
| 72 | |
| 73 | context->copyTexture(texture, dst->asRenderTarget()); |
| 74 | SkGrTexturePixelRef* pixelRef = new SkGrTexturePixelRef(dst); |
| 75 | GrSafeUnref(dst); |
| 76 | return pixelRef; |
| 77 | } |
| 78 | |
| 79 | /////////////////////////////////////////////////////////////////////////////// |
| 80 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 81 | SkGrTexturePixelRef::SkGrTexturePixelRef(GrTexture* tex) { |
| 82 | fTexture = tex; |
| 83 | GrSafeRef(tex); |
| 84 | } |
| 85 | |
| 86 | SkGrTexturePixelRef::~SkGrTexturePixelRef() { |
| 87 | GrSafeUnref(fTexture); |
| 88 | } |
| 89 | |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 90 | SkGpuTexture* SkGrTexturePixelRef::getTexture() { |
| 91 | return (SkGpuTexture*)fTexture; |
| 92 | } |
| 93 | |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame^] | 94 | SkPixelRef* SkGrTexturePixelRef::deepCopy(SkBitmap::Config dstConfig) { |
| 95 | return copyToTexturePixelRef(fTexture, dstConfig); |
| 96 | } |
| 97 | |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 98 | bool SkGrTexturePixelRef::onReadPixels(SkBitmap* dst, const SkIRect* subset) { |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 99 | if (NULL != fTexture && fTexture->isValid()) { |
| 100 | int left, top, width, height; |
| 101 | if (NULL != subset) { |
| 102 | left = subset->fLeft; |
| 103 | width = subset->width(); |
| 104 | top = subset->fTop; |
| 105 | height = subset->height(); |
| 106 | } else { |
| 107 | left = 0; |
| 108 | width = fTexture->width(); |
| 109 | top = 0; |
| 110 | height = fTexture->height(); |
| 111 | } |
| 112 | dst->setConfig(SkBitmap::kARGB_8888_Config, width, height); |
| 113 | dst->allocPixels(); |
| 114 | SkAutoLockPixels al(*dst); |
| 115 | void* buffer = dst->getPixels(); |
| 116 | return fTexture->readPixels(left, top, width, height, |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 117 | kSkia8888_PM_GrPixelConfig, |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 118 | buffer, dst->rowBytes()); |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 119 | } else { |
| 120 | return false; |
| 121 | } |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 122 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 123 | |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 124 | /////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 125 | |
| 126 | SkGrRenderTargetPixelRef::SkGrRenderTargetPixelRef(GrRenderTarget* rt) { |
| 127 | fRenderTarget = rt; |
| 128 | GrSafeRef(fRenderTarget); |
| 129 | } |
| 130 | |
| 131 | SkGrRenderTargetPixelRef::~SkGrRenderTargetPixelRef() { |
| 132 | GrSafeUnref(fRenderTarget); |
| 133 | } |
| 134 | |
| 135 | SkGpuTexture* SkGrRenderTargetPixelRef::getTexture() { |
| 136 | if (NULL != fRenderTarget) { |
| 137 | return (SkGpuTexture*) fRenderTarget->asTexture(); |
| 138 | } |
| 139 | return NULL; |
| 140 | } |
| 141 | |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame^] | 142 | SkPixelRef* SkGrRenderTargetPixelRef::deepCopy(SkBitmap::Config dstConfig) { |
| 143 | if (NULL == fRenderTarget) { |
| 144 | return NULL; |
| 145 | } |
| 146 | // Note that when copying an SkGrRenderTargetPixelRef, we actually |
| 147 | // return an SkGrTexturePixelRef instead. This is because |
| 148 | // SkGrRenderTargetPixelRef is usually created in conjunction with |
| 149 | // GrTexture owned elsewhere (e.g., SkGpuDevice), and cannot live |
| 150 | // independently of that texture. SkGrTexturePixelRef, on the other |
| 151 | // hand, owns its own GrTexture, and is thus self-contained. |
| 152 | return copyToTexturePixelRef(fRenderTarget->asTexture(), dstConfig); |
| 153 | } |
| 154 | |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 155 | bool SkGrRenderTargetPixelRef::onReadPixels(SkBitmap* dst, const SkIRect* subset) { |
| 156 | if (NULL != fRenderTarget && fRenderTarget->isValid()) { |
| 157 | int left, top, width, height; |
| 158 | if (NULL != subset) { |
| 159 | left = subset->fLeft; |
| 160 | width = subset->width(); |
| 161 | top = subset->fTop; |
| 162 | height = subset->height(); |
| 163 | } else { |
| 164 | left = 0; |
| 165 | width = fRenderTarget->width(); |
| 166 | top = 0; |
| 167 | height = fRenderTarget->height(); |
| 168 | } |
| 169 | dst->setConfig(SkBitmap::kARGB_8888_Config, width, height); |
| 170 | dst->allocPixels(); |
| 171 | SkAutoLockPixels al(*dst); |
| 172 | void* buffer = dst->getPixels(); |
| 173 | return fRenderTarget->readPixels(left, top, width, height, |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 174 | kSkia8888_PM_GrPixelConfig, |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 175 | buffer, dst->rowBytes()); |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 176 | } else { |
| 177 | return false; |
| 178 | } |
reed@google.com | 4281d65 | 2011-04-08 18:54:20 +0000 | [diff] [blame] | 179 | } |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 180 | |