| 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 | |
| robertphillips@google.com | 5088eb4 | 2012-06-28 20:59:13 +0000 | [diff] [blame] | 11 | #include "SkGrPixelRef.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. |
| digit@google.com | 1771cbf | 2012-01-26 21:26:40 +0000 | [diff] [blame] | 19 | SK_DECLARE_STATIC_MUTEX(gROLockPixelsPixelRefMutex); |
| reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 20 | |
| reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 21 | SkROLockPixelsPixelRef::SkROLockPixelsPixelRef(const SkImageInfo& info) |
| 22 | : INHERITED(info, &gROLockPixelsPixelRefMutex) {} |
| reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 23 | |
| reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 24 | SkROLockPixelsPixelRef::~SkROLockPixelsPixelRef() {} |
| reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 25 | |
| robertphillips@google.com | 0daa1ad | 2013-12-13 15:24:37 +0000 | [diff] [blame] | 26 | void* SkROLockPixelsPixelRef::onLockPixels(SkColorTable** ctable) { |
| 27 | if (ctable) { |
| 28 | *ctable = NULL; |
| 29 | } |
| reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 30 | fBitmap.reset(); |
| 31 | // SkDebugf("---------- calling readpixels in support of lockpixels\n"); |
| 32 | if (!this->onReadPixels(&fBitmap, NULL)) { |
| 33 | SkDebugf("SkROLockPixelsPixelRef::onLockPixels failed!\n"); |
| robertphillips@google.com | 0daa1ad | 2013-12-13 15:24:37 +0000 | [diff] [blame] | 34 | return NULL; |
| reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 35 | } |
| 36 | fBitmap.lockPixels(); |
| robertphillips@google.com | 0daa1ad | 2013-12-13 15:24:37 +0000 | [diff] [blame] | 37 | return fBitmap.getPixels(); |
| reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | void SkROLockPixelsPixelRef::onUnlockPixels() { |
| 41 | fBitmap.unlockPixels(); |
| 42 | } |
| 43 | |
| 44 | bool SkROLockPixelsPixelRef::onLockPixelsAreWritable() const { |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | /////////////////////////////////////////////////////////////////////////////// |
| bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 49 | |
| scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 50 | static SkGrPixelRef* copyToTexturePixelRef(GrTexture* texture, SkBitmap::Config dstConfig, |
| 51 | const SkIRect* subset) { |
| senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 52 | if (NULL == texture) { |
| 53 | return NULL; |
| 54 | } |
| 55 | GrContext* context = texture->getContext(); |
| 56 | if (NULL == context) { |
| 57 | return NULL; |
| 58 | } |
| 59 | GrTextureDesc desc; |
| 60 | |
| scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 61 | SkIPoint pointStorage; |
| 62 | SkIPoint* topLeft; |
| 63 | if (subset != NULL) { |
| 64 | SkASSERT(SkIRect::MakeWH(texture->width(), texture->height()).contains(*subset)); |
| 65 | // Create a new texture that is the size of subset. |
| 66 | desc.fWidth = subset->width(); |
| 67 | desc.fHeight = subset->height(); |
| 68 | pointStorage.set(subset->x(), subset->y()); |
| 69 | topLeft = &pointStorage; |
| 70 | } else { |
| 71 | desc.fWidth = texture->width(); |
| 72 | desc.fHeight = texture->height(); |
| 73 | topLeft = NULL; |
| 74 | } |
| senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 75 | desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit; |
| rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 76 | desc.fConfig = SkBitmapConfig2GrPixelConfig(dstConfig); |
| senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 77 | |
| reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 78 | SkImageInfo info; |
| 79 | if (!GrPixelConfig2ColorType(desc.fConfig, &info.fColorType)) { |
| 80 | return NULL; |
| 81 | } |
| 82 | info.fWidth = desc.fWidth; |
| 83 | info.fHeight = desc.fHeight; |
| 84 | info.fAlphaType = kPremul_SkAlphaType; |
| skia.committer@gmail.com | 96f5fa0 | 2013-12-16 07:01:40 +0000 | [diff] [blame] | 85 | |
| senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 86 | GrTexture* dst = context->createUncachedTexture(desc, NULL, 0); |
| 87 | if (NULL == dst) { |
| 88 | return NULL; |
| 89 | } |
| 90 | |
| scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 91 | context->copyTexture(texture, dst->asRenderTarget(), topLeft); |
| robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 92 | |
| robertphillips@google.com | 41efe04 | 2012-06-29 20:55:14 +0000 | [diff] [blame] | 93 | // TODO: figure out if this is responsible for Chrome canvas errors |
| 94 | #if 0 |
| robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 95 | // The render texture we have created (to perform the copy) isn't fully |
| 96 | // functional (since it doesn't have a stencil buffer). Release it here |
| 97 | // so the caller doesn't try to render to it. |
| 98 | // TODO: we can undo this release when dynamic stencil buffer attach/ |
| 99 | // detach has been implemented |
| 100 | dst->releaseRenderTarget(); |
| robertphillips@google.com | 41efe04 | 2012-06-29 20:55:14 +0000 | [diff] [blame] | 101 | #endif |
| robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 102 | |
| reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 103 | SkGrPixelRef* pixelRef = SkNEW_ARGS(SkGrPixelRef, (info, dst)); |
| commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 104 | SkSafeUnref(dst); |
| senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 105 | return pixelRef; |
| 106 | } |
| 107 | |
| 108 | /////////////////////////////////////////////////////////////////////////////// |
| 109 | |
| reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 110 | SkGrPixelRef::SkGrPixelRef(const SkImageInfo& info, GrSurface* surface, |
| 111 | bool transferCacheLock) : INHERITED(info) { |
| reed@google.com | 6186787 | 2013-12-09 13:41:06 +0000 | [diff] [blame] | 112 | // TODO: figure out if this is responsible for Chrome canvas errors |
| 113 | #if 0 |
| 114 | // The GrTexture has a ref to the GrRenderTarget but not vice versa. |
| 115 | // If the GrTexture exists take a ref to that (rather than the render |
| 116 | // target) |
| 117 | fSurface = surface->asTexture(); |
| 118 | #else |
| 119 | fSurface = NULL; |
| 120 | #endif |
| 121 | if (NULL == fSurface) { |
| 122 | fSurface = surface; |
| 123 | } |
| 124 | fUnlock = transferCacheLock; |
| 125 | SkSafeRef(surface); |
| 126 | } |
| 127 | |
| robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 128 | SkGrPixelRef::~SkGrPixelRef() { |
| bsalomon@google.com | 8090e65 | 2012-08-28 15:07:11 +0000 | [diff] [blame] | 129 | if (fUnlock) { |
| 130 | GrContext* context = fSurface->getContext(); |
| robertphillips@google.com | d07cb0c | 2012-08-30 19:22:29 +0000 | [diff] [blame] | 131 | GrTexture* texture = fSurface->asTexture(); |
| bsalomon@google.com | 8090e65 | 2012-08-28 15:07:11 +0000 | [diff] [blame] | 132 | if (NULL != context && NULL != texture) { |
| robertphillips@google.com | 9fbcad0 | 2012-09-09 14:44:15 +0000 | [diff] [blame] | 133 | context->unlockScratchTexture(texture); |
| bsalomon@google.com | 8090e65 | 2012-08-28 15:07:11 +0000 | [diff] [blame] | 134 | } |
| 135 | } |
| commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 136 | SkSafeUnref(fSurface); |
| bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| commit-bot@chromium.org | b8d00db | 2013-06-26 19:18:23 +0000 | [diff] [blame] | 139 | GrTexture* SkGrPixelRef::getTexture() { |
| robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 140 | if (NULL != fSurface) { |
| commit-bot@chromium.org | b8d00db | 2013-06-26 19:18:23 +0000 | [diff] [blame] | 141 | return fSurface->asTexture(); |
| bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 142 | } |
| 143 | return NULL; |
| 144 | } |
| 145 | |
| scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 146 | SkPixelRef* SkGrPixelRef::deepCopy(SkBitmap::Config dstConfig, const SkIRect* subset) { |
| robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 147 | if (NULL == fSurface) { |
| senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 148 | return NULL; |
| 149 | } |
| robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 150 | |
| 151 | // Note that when copying a render-target-backed pixel ref, we |
| 152 | // return a texture-backed pixel ref instead. This is because |
| 153 | // render-target pixel refs are usually created in conjunction with |
| 154 | // a GrTexture owned elsewhere (e.g., SkGpuDevice), and cannot live |
| 155 | // independently of that texture. Texture-backed pixel refs, on the other |
| 156 | // hand, own their GrTextures, and are thus self-contained. |
| scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 157 | return copyToTexturePixelRef(fSurface->asTexture(), dstConfig, subset); |
| senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 160 | bool SkGrPixelRef::onReadPixels(SkBitmap* dst, const SkIRect* subset) { |
| 161 | if (NULL == fSurface || !fSurface->isValid()) { |
| bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 162 | return false; |
| 163 | } |
| robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 164 | |
| 165 | int left, top, width, height; |
| 166 | if (NULL != subset) { |
| 167 | left = subset->fLeft; |
| 168 | width = subset->width(); |
| 169 | top = subset->fTop; |
| 170 | height = subset->height(); |
| 171 | } else { |
| 172 | left = 0; |
| 173 | width = fSurface->width(); |
| 174 | top = 0; |
| 175 | height = fSurface->height(); |
| 176 | } |
| 177 | dst->setConfig(SkBitmap::kARGB_8888_Config, width, height); |
| bsalomon@google.com | 009bcca | 2012-12-18 21:40:08 +0000 | [diff] [blame] | 178 | if (!dst->allocPixels()) { |
| 179 | SkDebugf("SkGrPixelRef::onReadPixels failed to alloc bitmap for result!\n"); |
| 180 | return false; |
| 181 | } |
| robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 182 | SkAutoLockPixels al(*dst); |
| 183 | void* buffer = dst->getPixels(); |
| 184 | return fSurface->readPixels(left, top, width, height, |
| bsalomon@google.com | fec0bc3 | 2013-02-07 14:43:04 +0000 | [diff] [blame] | 185 | kSkia8888_GrPixelConfig, |
| robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 186 | buffer, dst->rowBytes()); |
| reed@google.com | 4281d65 | 2011-04-08 18:54:20 +0000 | [diff] [blame] | 187 | } |