epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 1 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 3 | * Copyright 2008 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 9 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #ifndef SkFlipPixelRef_DEFINED |
| 11 | #define SkFlipPixelRef_DEFINED |
| 12 | |
| 13 | #include "SkBitmap.h" |
| 14 | #include "SkPageFlipper.h" |
| 15 | #include "SkPixelRef.h" |
| 16 | #include "SkThread.h" |
| 17 | |
| 18 | class SkRegion; |
| 19 | |
| 20 | class SkFlipPixelRef : public SkPixelRef { |
| 21 | public: |
| 22 | SkFlipPixelRef(SkBitmap::Config, int width, int height); |
| 23 | virtual ~SkFlipPixelRef(); |
| 24 | |
| 25 | bool isDirty() const { return fFlipper.isDirty(); } |
| 26 | const SkRegion& dirtyRgn() const { return fFlipper.dirtyRgn(); } |
| 27 | |
| 28 | void inval() { fFlipper.inval(); } |
| 29 | void inval(const SkIRect& rect) { fFlipper.inval(rect); } |
| 30 | void inval(const SkRegion& rgn) { fFlipper.inval(rgn); } |
| 31 | void inval(const SkRect& r, bool doAA) { fFlipper.inval(r, doAA); } |
| 32 | |
| 33 | const SkRegion& beginUpdate(SkBitmap* device); |
| 34 | void endUpdate(); |
| 35 | |
| 36 | private: |
| 37 | void getFrontBack(const void** front, void** back) const { |
| 38 | if (front) { |
| 39 | *front = fPage0; |
| 40 | } |
| 41 | if (back) { |
| 42 | *back = fPage1; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | void swapPages(); |
| 47 | |
| 48 | // Helper to copy pixels from srcAddr to the dst bitmap, clipped to clip. |
| 49 | // srcAddr points to memory with the same config as dst. |
| 50 | static void CopyBitsFromAddr(const SkBitmap& dst, const SkRegion& clip, |
| 51 | const void* srcAddr); |
| 52 | |
| 53 | // serialization |
| 54 | |
| 55 | public: |
| 56 | virtual Factory getFactory() const { return Create; } |
| 57 | virtual void flatten(SkFlattenableWriteBuffer&) const; |
| 58 | static SkPixelRef* Create(SkFlattenableReadBuffer& buffer); |
| 59 | |
| 60 | protected: |
| 61 | virtual void* onLockPixels(SkColorTable**); |
| 62 | virtual void onUnlockPixels(); |
| 63 | |
| 64 | SkFlipPixelRef(SkFlattenableReadBuffer&); |
| 65 | |
| 66 | private: |
| 67 | SkMutex fMutex; |
| 68 | SkPageFlipper fFlipper; |
| 69 | |
| 70 | void* fStorage; |
| 71 | void* fPage0; // points into fStorage; |
| 72 | void* fPage1; // points into fStorage; |
| 73 | size_t fSize; // size of 1 page. fStorage holds 2 pages |
| 74 | SkBitmap::Config fConfig; |
| 75 | |
| 76 | typedef SkPixelRef INHERITED; |
| 77 | }; |
| 78 | |
| 79 | class SkAutoFlipUpdate : SkNoncopyable { |
| 80 | public: |
| 81 | SkAutoFlipUpdate(SkFlipPixelRef* ref) : fRef(ref) { |
| 82 | fDirty = &ref->beginUpdate(&fBitmap); |
| 83 | } |
| 84 | ~SkAutoFlipUpdate() { |
| 85 | if (fRef) { |
| 86 | fRef->endUpdate(); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | const SkBitmap& bitmap() const { return fBitmap; } |
| 91 | const SkRegion& dirty() const { return *fDirty; } |
| 92 | |
| 93 | // optional. This gets automatically called in the destructor (only once) |
| 94 | void endUpdate() { |
| 95 | if (fRef) { |
| 96 | fRef->endUpdate(); |
| 97 | fRef = NULL; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | private: |
| 102 | SkFlipPixelRef* fRef; |
| 103 | SkBitmap fBitmap; |
| 104 | const SkRegion* fDirty; |
| 105 | }; |
| 106 | |
| 107 | #endif |