blob: 455a3dace3bd664fb4172f5acaf4bff8d70537ad [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);
caryclark@google.comd26147a2011-12-15 14:16:43 +000059
60 SK_DECLARE_PIXEL_REF_REGISTRAR()
61
reed@android.com8a1c16f2008-12-17 15:59:43 +000062protected:
63 virtual void* onLockPixels(SkColorTable**);
64 virtual void onUnlockPixels();
65
66 SkFlipPixelRef(SkFlattenableReadBuffer&);
67
68private:
69 SkMutex fMutex;
70 SkPageFlipper fFlipper;
71
72 void* fStorage;
73 void* fPage0; // points into fStorage;
74 void* fPage1; // points into fStorage;
75 size_t fSize; // size of 1 page. fStorage holds 2 pages
76 SkBitmap::Config fConfig;
77
78 typedef SkPixelRef INHERITED;
79};
80
81class SkAutoFlipUpdate : SkNoncopyable {
82public:
83 SkAutoFlipUpdate(SkFlipPixelRef* ref) : fRef(ref) {
84 fDirty = &ref->beginUpdate(&fBitmap);
85 }
86 ~SkAutoFlipUpdate() {
87 if (fRef) {
88 fRef->endUpdate();
89 }
90 }
91
92 const SkBitmap& bitmap() const { return fBitmap; }
93 const SkRegion& dirty() const { return *fDirty; }
94
95 // optional. This gets automatically called in the destructor (only once)
96 void endUpdate() {
97 if (fRef) {
98 fRef->endUpdate();
99 fRef = NULL;
100 }
101 }
102
103private:
104 SkFlipPixelRef* fRef;
105 SkBitmap fBitmap;
106 const SkRegion* fDirty;
107};
108
109#endif