• 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 "SkImageGeneratorPriv.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 
diagnostic_only_getDiscardable()24     SkDiscardableMemory* diagnostic_only_getDiscardable() const override {
25         return fDiscardableMemory;
26     }
27 
28 protected:
29     ~SkDiscardablePixelRef();
30 
31     bool onNewLockPixels(LockRec*) override;
32     void onUnlockPixels() override;
onLockPixelsAreWritable()33     bool onLockPixelsAreWritable() const override { return false; }
34 
onRefEncodedData()35     SkData* onRefEncodedData() override {
36         return fGenerator->refEncodedData();
37     }
38 
onIsLazyGenerated()39     bool onIsLazyGenerated() const override { return true; }
40 
41 private:
42     SkImageGenerator* const fGenerator;
43     SkDiscardableMemory::Factory* const fDMFactory;
44     const size_t fRowBytes;
45     // These const members should not change over the life of the
46     // PixelRef, since the SkBitmap doesn't expect them to change.
47 
48     SkDiscardableMemory* fDiscardableMemory;
49     bool                 fDiscardableMemoryIsLocked;
50     SkAutoTUnref<SkColorTable> fCTable;
51 
52     /* Takes ownership of SkImageGenerator. */
53     SkDiscardablePixelRef(const SkImageInfo&, SkImageGenerator*,
54                           size_t rowBytes,
55                           SkDiscardableMemory::Factory* factory);
56 
onGetYUV8Planes(SkISize sizes[3],void * planes[3],size_t rowBytes[3],SkYUVColorSpace * colorSpace)57     bool onGetYUV8Planes(SkISize sizes[3],
58                          void* planes[3],
59                          size_t rowBytes[3],
60                          SkYUVColorSpace* colorSpace) override {
61         // If the image was already decoded with lockPixels(), favor not
62         // re-decoding to YUV8 planes.
63         if (fDiscardableMemory) {
64             return false;
65         }
66         return fGenerator->getYUV8Planes(sizes, planes, rowBytes, colorSpace);
67     }
68 
69     friend bool SkDEPRECATED_InstallDiscardablePixelRef(SkImageGenerator*, const SkIRect*, SkBitmap*,
70                                              SkDiscardableMemory::Factory*);
71 
72     typedef SkPixelRef INHERITED;
73 };
74 
75 #endif  // SkDiscardablePixelRef_DEFINED
76