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 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     SK_DECLARE_PIXEL_REF_REGISTRAR()
     61 
     62 protected:
     63     virtual void* onLockPixels(SkColorTable**);
     64     virtual void onUnlockPixels();
     65 
     66     SkFlipPixelRef(SkFlattenableReadBuffer&);
     67 
     68 private:
     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 
     81 class SkAutoFlipUpdate : SkNoncopyable {
     82 public:
     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 
    103 private:
    104     SkFlipPixelRef* fRef;
    105     SkBitmap        fBitmap;
    106     const SkRegion* fDirty;
    107 };
    108 
    109 #endif
    110