Home | History | Annotate | Download | only in images
      1 
      2 /*
      3  * Copyright 2008 The Android Open Source Project
      4  *
      5  * Use of this source code is governed by a BSD-style license that can be
      6  * found in the LICENSE file.
      7  */
      8 
      9 
     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     SK_DECLARE_UNFLATTENABLE_OBJECT()
     37 private:
     38     void getFrontBack(const void** front, void** back) const {
     39         if (front) {
     40             *front = fPage0;
     41         }
     42         if (back) {
     43             *back = fPage1;
     44         }
     45     }
     46 
     47     void    swapPages();
     48 
     49     // Helper to copy pixels from srcAddr to the dst bitmap, clipped to clip.
     50     // srcAddr points to memory with the same config as dst.
     51     static void CopyBitsFromAddr(const SkBitmap& dst, const SkRegion& clip,
     52                                  const void* srcAddr);
     53 
     54 protected:
     55     virtual void* onLockPixels(SkColorTable**);
     56     virtual void onUnlockPixels();
     57 
     58 private:
     59     SkMutex         fMutex;
     60     SkPageFlipper   fFlipper;
     61 
     62     void*           fStorage;
     63     void*           fPage0; // points into fStorage;
     64     void*           fPage1; // points into fStorage;
     65     size_t          fSize;  // size of 1 page. fStorage holds 2 pages
     66     SkBitmap::Config fConfig;
     67 
     68     typedef SkPixelRef INHERITED;
     69 };
     70 
     71 class SkAutoFlipUpdate : SkNoncopyable {
     72 public:
     73     SkAutoFlipUpdate(SkFlipPixelRef* ref) : fRef(ref) {
     74         fDirty = &ref->beginUpdate(&fBitmap);
     75     }
     76     ~SkAutoFlipUpdate() {
     77         if (fRef) {
     78             fRef->endUpdate();
     79         }
     80     }
     81 
     82     const SkBitmap& bitmap() const { return fBitmap; }
     83     const SkRegion& dirty() const { return *fDirty; }
     84 
     85     // optional. This gets automatically called in the destructor (only once)
     86     void endUpdate() {
     87         if (fRef) {
     88             fRef->endUpdate();
     89             fRef = NULL;
     90         }
     91     }
     92 
     93 private:
     94     SkFlipPixelRef* fRef;
     95     SkBitmap        fBitmap;
     96     const SkRegion* fDirty;
     97 };
     98 
     99 #endif
    100