• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2013 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef SkDiscardablePixelRef_DEFINED
9 #define SkDiscardablePixelRef_DEFINED
10 
11 #include "SkDiscardableMemory.h"
12 #include "SkImageGenerator.h"
13 #include "SkImageInfo.h"
14 #include "SkPixelRef.h"
15 
16 /**
17  *  A PixelRef backed by SkDiscardableMemory, with the ability to
18  *  re-generate the pixels (via a SkImageGenerator) if the DM is
19  *  purged.
20  */
21 class SkDiscardablePixelRef : public SkPixelRef {
22 public:
23     SK_DECLARE_INST_COUNT(SkDiscardablePixelRef)
24     SK_DECLARE_UNFLATTENABLE_OBJECT()
25 
26 protected:
27     ~SkDiscardablePixelRef();
28 
29     virtual bool onNewLockPixels(LockRec*) SK_OVERRIDE;
30     virtual void onUnlockPixels() SK_OVERRIDE;
onLockPixelsAreWritable()31     virtual bool onLockPixelsAreWritable() const SK_OVERRIDE { return false; }
32 
onRefEncodedData()33     virtual SkData* onRefEncodedData() SK_OVERRIDE {
34         return fGenerator->refEncodedData();
35     }
36 
37 private:
38     SkImageGenerator* const fGenerator;
39     SkDiscardableMemory::Factory* const fDMFactory;
40     const size_t fRowBytes;
41     // These const members should not change over the life of the
42     // PixelRef, since the SkBitmap doesn't expect them to change.
43 
44     SkDiscardableMemory* fDiscardableMemory;
45     SkAutoTUnref<SkColorTable> fCTable;
46 
47     /* Takes ownership of SkImageGenerator. */
48     SkDiscardablePixelRef(const SkImageInfo&, SkImageGenerator*,
49                           size_t rowBytes,
50                           SkDiscardableMemory::Factory* factory);
51 
52     friend bool SkInstallDiscardablePixelRef(SkImageGenerator*, SkBitmap*,
53                                              SkDiscardableMemory::Factory*);
54 
55     typedef SkPixelRef INHERITED;
56 };
57 
58 #endif  // SkDiscardablePixelRef_DEFINED
59