blob: 3d5c097077e4ffcc2751b2eed8eb3bae8351ac97 [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();
djsollen@google.com5370cd92012-03-28 20:47:01 +000035
djsollen@google.com5370cd92012-03-28 20:47:01 +000036 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkFlipPixelRef)
37
38protected:
39 virtual void* onLockPixels(SkColorTable**);
40 virtual void onUnlockPixels();
41
42 SkFlipPixelRef(SkFlattenableReadBuffer&);
djsollen@google.com54924242012-03-29 15:18:04 +000043 virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000044
45private:
46 void getFrontBack(const void** front, void** back) const {
47 if (front) {
48 *front = fPage0;
49 }
50 if (back) {
51 *back = fPage1;
52 }
53 }
54
55 void swapPages();
56
57 // Helper to copy pixels from srcAddr to the dst bitmap, clipped to clip.
58 // srcAddr points to memory with the same config as dst.
59 static void CopyBitsFromAddr(const SkBitmap& dst, const SkRegion& clip,
60 const void* srcAddr);
61
reed@android.com8a1c16f2008-12-17 15:59:43 +000062 SkMutex fMutex;
63 SkPageFlipper fFlipper;
64
65 void* fStorage;
66 void* fPage0; // points into fStorage;
67 void* fPage1; // points into fStorage;
68 size_t fSize; // size of 1 page. fStorage holds 2 pages
69 SkBitmap::Config fConfig;
70
71 typedef SkPixelRef INHERITED;
72};
73
74class SkAutoFlipUpdate : SkNoncopyable {
75public:
76 SkAutoFlipUpdate(SkFlipPixelRef* ref) : fRef(ref) {
77 fDirty = &ref->beginUpdate(&fBitmap);
78 }
79 ~SkAutoFlipUpdate() {
80 if (fRef) {
81 fRef->endUpdate();
82 }
83 }
84
85 const SkBitmap& bitmap() const { return fBitmap; }
86 const SkRegion& dirty() const { return *fDirty; }
87
88 // optional. This gets automatically called in the destructor (only once)
89 void endUpdate() {
90 if (fRef) {
91 fRef->endUpdate();
92 fRef = NULL;
93 }
94 }
95
96private:
97 SkFlipPixelRef* fRef;
98 SkBitmap fBitmap;
99 const SkRegion* fDirty;
100};
101
102#endif