| OLD | NEW |
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2010 Google Inc. | 3 * Copyright 2010 Google Inc. |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 | 9 |
| 10 | 10 |
| 11 #include "SkGrPixelRef.h" | 11 #include "SkGrPixelRef.h" |
| 12 | |
| 13 #include "GrContext.h" | 12 #include "GrContext.h" |
| 14 #include "GrTexture.h" | 13 #include "GrTexture.h" |
| 15 #include "SkBitmapCache.h" | |
| 16 #include "SkGr.h" | 14 #include "SkGr.h" |
| 17 #include "SkRect.h" | 15 #include "SkRect.h" |
| 18 | 16 |
| 19 // since we call lockPixels recursively on fBitmap, we need a distinct mutex, | 17 // since we call lockPixels recursively on fBitmap, we need a distinct mutex, |
| 20 // to avoid deadlock with the default one provided by SkPixelRef. | 18 // to avoid deadlock with the default one provided by SkPixelRef. |
| 21 SK_DECLARE_STATIC_MUTEX(gROLockPixelsPixelRefMutex); | 19 SK_DECLARE_STATIC_MUTEX(gROLockPixelsPixelRefMutex); |
| 22 | 20 |
| 23 SkROLockPixelsPixelRef::SkROLockPixelsPixelRef(const SkImageInfo& info) | 21 SkROLockPixelsPixelRef::SkROLockPixelsPixelRef(const SkImageInfo& info) |
| 24 : INHERITED(info, &gROLockPixelsPixelRefMutex) {} | 22 : INHERITED(info, &gROLockPixelsPixelRefMutex) {} |
| 25 | 23 |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 if (fSurface) { | 136 if (fSurface) { |
| 139 return fSurface->asTexture(); | 137 return fSurface->asTexture(); |
| 140 } | 138 } |
| 141 return NULL; | 139 return NULL; |
| 142 } | 140 } |
| 143 | 141 |
| 144 SkPixelRef* SkGrPixelRef::deepCopy(SkColorType dstCT, const SkIRect* subset) { | 142 SkPixelRef* SkGrPixelRef::deepCopy(SkColorType dstCT, const SkIRect* subset) { |
| 145 if (NULL == fSurface) { | 143 if (NULL == fSurface) { |
| 146 return NULL; | 144 return NULL; |
| 147 } | 145 } |
| 148 | 146 |
| 149 // Note that when copying a render-target-backed pixel ref, we | 147 // Note that when copying a render-target-backed pixel ref, we |
| 150 // return a texture-backed pixel ref instead. This is because | 148 // return a texture-backed pixel ref instead. This is because |
| 151 // render-target pixel refs are usually created in conjunction with | 149 // render-target pixel refs are usually created in conjunction with |
| 152 // a GrTexture owned elsewhere (e.g., SkGpuDevice), and cannot live | 150 // a GrTexture owned elsewhere (e.g., SkGpuDevice), and cannot live |
| 153 // independently of that texture. Texture-backed pixel refs, on the other | 151 // independently of that texture. Texture-backed pixel refs, on the other |
| 154 // hand, own their GrTextures, and are thus self-contained. | 152 // hand, own their GrTextures, and are thus self-contained. |
| 155 return copyToTexturePixelRef(fSurface->asTexture(), dstCT, subset); | 153 return copyToTexturePixelRef(fSurface->asTexture(), dstCT, subset); |
| 156 } | 154 } |
| 157 | 155 |
| 158 static bool tryAllocBitmapPixels(SkBitmap* bitmap) { | |
| 159 SkBitmap::Allocator* allocator = SkBitmapCache::GetAllocator(); | |
| 160 if (NULL != allocator) { | |
| 161 return allocator->allocPixelRef(bitmap, 0); | |
| 162 } else { | |
| 163 // DiscardableMemory is not available, fallback to default allocator | |
| 164 return bitmap->tryAllocPixels(); | |
| 165 } | |
| 166 } | |
| 167 | |
| 168 bool SkGrPixelRef::onReadPixels(SkBitmap* dst, const SkIRect* subset) { | 156 bool SkGrPixelRef::onReadPixels(SkBitmap* dst, const SkIRect* subset) { |
| 169 if (NULL == fSurface || fSurface->wasDestroyed()) { | 157 if (NULL == fSurface || fSurface->wasDestroyed()) { |
| 170 return false; | 158 return false; |
| 171 } | 159 } |
| 172 | 160 |
| 173 SkIRect bounds; | 161 int left, top, width, height; |
| 174 if (subset) { | 162 if (subset) { |
| 175 bounds = *subset; | 163 left = subset->fLeft; |
| 164 width = subset->width(); |
| 165 top = subset->fTop; |
| 166 height = subset->height(); |
| 176 } else { | 167 } else { |
| 177 bounds = SkIRect::MakeWH(this->info().width(), this->info().height()); | 168 left = 0; |
| 169 width = this->info().width(); |
| 170 top = 0; |
| 171 height = this->info().height(); |
| 178 } | 172 } |
| 179 | 173 if (!dst->tryAllocN32Pixels(width, height)) { |
| 180 //Check the cache | 174 SkDebugf("SkGrPixelRef::onReadPixels failed to alloc bitmap for result!\
n"); |
| 181 if(!SkBitmapCache::Find(this->getGenerationID(), bounds, dst)) { | 175 return false; |
| 182 //Cache miss | 176 } |
| 183 | 177 SkAutoLockPixels al(*dst); |
| 184 SkBitmap cachedBitmap; | 178 void* buffer = dst->getPixels(); |
| 185 cachedBitmap.setInfo(this->info().makeWH(bounds.width(), bounds.height()
)); | 179 return fSurface->readPixels(left, top, width, height, |
| 186 | |
| 187 // If we can't alloc the pixels, then fail | |
| 188 if (!tryAllocBitmapPixels(&cachedBitmap)) { | |
| 189 return false; | |
| 190 } | |
| 191 | |
| 192 // Try to read the pixels from the surface | |
| 193 void* buffer = cachedBitmap.getPixels(); | |
| 194 bool readPixelsOk = fSurface->readPixels(bounds.fLeft, bounds.fTop, | |
| 195 bounds.width(), bounds.height(), | |
| 196 kSkia8888_GrPixelConfig, | 180 kSkia8888_GrPixelConfig, |
| 197 buffer, cachedBitmap.rowBytes()); | 181 buffer, dst->rowBytes()); |
| 198 | |
| 199 if (!readPixelsOk) { | |
| 200 return false; | |
| 201 } | |
| 202 | |
| 203 // If we are here, pixels were read correctly from the surface. | |
| 204 cachedBitmap.setImmutable(); | |
| 205 //Add to the cache | |
| 206 SkBitmapCache::Add(this->getGenerationID(), bounds, cachedBitmap); | |
| 207 | |
| 208 dst->swap(cachedBitmap); | |
| 209 } | |
| 210 | |
| 211 return true; | |
| 212 | |
| 213 } | 182 } |
| OLD | NEW |