blob: e82c713a31aed56814d01d734d6393552978a4be [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@android.com8a1c16f2008-12-17 15:59:43 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2008 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#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
18class SkRegion;
19
20class SkFlipPixelRef : public SkPixelRef {
21public:
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
36private:
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
55public:
56 virtual Factory getFactory() const { return Create; }
57 virtual void flatten(SkFlattenableWriteBuffer&) const;
58 static SkPixelRef* Create(SkFlattenableReadBuffer& buffer);
59
60protected:
61 virtual void* onLockPixels(SkColorTable**);
62 virtual void onUnlockPixels();
63
64 SkFlipPixelRef(SkFlattenableReadBuffer&);
65
66private:
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
79class SkAutoFlipUpdate : SkNoncopyable {
80public:
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
101private:
102 SkFlipPixelRef* fRef;
103 SkBitmap fBitmap;
104 const SkRegion* fDirty;
105};
106
107#endif