OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkCachingPixelRef.h" | 8 #include "SkCachingPixelRef.h" |
9 #include "SkBitmapCache.h" | 9 #include "SkBitmapCache.h" |
10 #include "SkRect.h" | 10 #include "SkRect.h" |
11 | 11 |
12 bool SkCachingPixelRef::Install(SkImageGenerator* generator, | 12 bool SkCachingPixelRef::Install(SkImageGenerator* generator, |
13 SkBitmap* dst) { | 13 SkBitmap* dst) { |
14 SkASSERT(dst != NULL); | 14 SkASSERT(dst != nullptr); |
15 if (NULL == generator) { | 15 if (nullptr == generator) { |
16 return false; | 16 return false; |
17 } | 17 } |
18 const SkImageInfo info = generator->getInfo(); | 18 const SkImageInfo info = generator->getInfo(); |
19 if (!dst->setInfo(info)) { | 19 if (!dst->setInfo(info)) { |
20 delete generator; | 20 delete generator; |
21 return false; | 21 return false; |
22 } | 22 } |
23 SkAutoTUnref<SkCachingPixelRef> ref(new SkCachingPixelRef(info, generator, d
st->rowBytes())); | 23 SkAutoTUnref<SkCachingPixelRef> ref(new SkCachingPixelRef(info, generator, d
st->rowBytes())); |
24 dst->setPixelRef(ref); | 24 dst->setPixelRef(ref); |
25 return true; | 25 return true; |
26 } | 26 } |
27 | 27 |
28 SkCachingPixelRef::SkCachingPixelRef(const SkImageInfo& info, | 28 SkCachingPixelRef::SkCachingPixelRef(const SkImageInfo& info, |
29 SkImageGenerator* generator, | 29 SkImageGenerator* generator, |
30 size_t rowBytes) | 30 size_t rowBytes) |
31 : INHERITED(info) | 31 : INHERITED(info) |
32 , fImageGenerator(generator) | 32 , fImageGenerator(generator) |
33 , fErrorInDecoding(false) | 33 , fErrorInDecoding(false) |
34 , fRowBytes(rowBytes) { | 34 , fRowBytes(rowBytes) { |
35 SkASSERT(fImageGenerator != NULL); | 35 SkASSERT(fImageGenerator != nullptr); |
36 } | 36 } |
37 SkCachingPixelRef::~SkCachingPixelRef() { | 37 SkCachingPixelRef::~SkCachingPixelRef() { |
38 delete fImageGenerator; | 38 delete fImageGenerator; |
39 // Assert always unlock before unref. | 39 // Assert always unlock before unref. |
40 } | 40 } |
41 | 41 |
42 bool SkCachingPixelRef::onNewLockPixels(LockRec* rec) { | 42 bool SkCachingPixelRef::onNewLockPixels(LockRec* rec) { |
43 if (fErrorInDecoding) { | 43 if (fErrorInDecoding) { |
44 return false; // don't try again. | 44 return false; // don't try again. |
45 } | 45 } |
46 | 46 |
47 const SkImageInfo& info = this->info(); | 47 const SkImageInfo& info = this->info(); |
48 if (!SkBitmapCache::Find( | 48 if (!SkBitmapCache::Find( |
49 this->getGenerationID(), info.bounds(), &fLockedBitmap)) { | 49 this->getGenerationID(), info.bounds(), &fLockedBitmap)) { |
50 // Cache has been purged, must re-decode. | 50 // Cache has been purged, must re-decode. |
51 if (!fLockedBitmap.tryAllocPixels(info, fRowBytes)) { | 51 if (!fLockedBitmap.tryAllocPixels(info, fRowBytes)) { |
52 fErrorInDecoding = true; | 52 fErrorInDecoding = true; |
53 return false; | 53 return false; |
54 } | 54 } |
55 if (!fImageGenerator->getPixels(info, fLockedBitmap.getPixels(), fRowByt
es)) { | 55 if (!fImageGenerator->getPixels(info, fLockedBitmap.getPixels(), fRowByt
es)) { |
56 fErrorInDecoding = true; | 56 fErrorInDecoding = true; |
57 return false; | 57 return false; |
58 } | 58 } |
59 fLockedBitmap.setImmutable(); | 59 fLockedBitmap.setImmutable(); |
60 SkBitmapCache::Add(this, info.bounds(), fLockedBitmap); | 60 SkBitmapCache::Add(this, info.bounds(), fLockedBitmap); |
61 } | 61 } |
62 | 62 |
63 // Now bitmap should contain a concrete PixelRef of the decoded image. | 63 // Now bitmap should contain a concrete PixelRef of the decoded image. |
64 void* pixels = fLockedBitmap.getPixels(); | 64 void* pixels = fLockedBitmap.getPixels(); |
65 SkASSERT(pixels != NULL); | 65 SkASSERT(pixels != nullptr); |
66 rec->fPixels = pixels; | 66 rec->fPixels = pixels; |
67 rec->fColorTable = NULL; | 67 rec->fColorTable = nullptr; |
68 rec->fRowBytes = fLockedBitmap.rowBytes(); | 68 rec->fRowBytes = fLockedBitmap.rowBytes(); |
69 return true; | 69 return true; |
70 } | 70 } |
71 | 71 |
72 void SkCachingPixelRef::onUnlockPixels() { | 72 void SkCachingPixelRef::onUnlockPixels() { |
73 fLockedBitmap.reset(); | 73 fLockedBitmap.reset(); |
74 } | 74 } |
OLD | NEW |