blob: 4a013fda031abd456ce03f01edb7e7824495833d [file] [log] [blame]
halcanary@google.comad04eb42013-11-21 15:32:08 +00001/*
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
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000011#include "SkDiscardableMemory.h"
halcanary@google.comad04eb42013-11-21 15:32:08 +000012#include "SkImageGenerator.h"
13#include "SkImageInfo.h"
halcanary@google.comedd370f2013-12-10 21:11:12 +000014#include "SkPixelRef.h"
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000015
halcanary@google.com3d50ea12014-01-02 13:15:13 +000016/**
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 * Since SkColorTable is reference-counted, we do not support indexed
22 * color with this class; there would be no way for the discardable
23 * memory system to unref the color table.
24 */
halcanary@google.comad04eb42013-11-21 15:32:08 +000025class SkDiscardablePixelRef : public SkPixelRef {
26public:
halcanary@google.comad04eb42013-11-21 15:32:08 +000027 SK_DECLARE_UNFLATTENABLE_OBJECT()
28
29protected:
30 ~SkDiscardablePixelRef();
reed@google.comd0419b12014-01-06 17:08:27 +000031
32 virtual bool onNewLockPixels(LockRec*) SK_OVERRIDE;
halcanary@google.comad04eb42013-11-21 15:32:08 +000033 virtual void onUnlockPixels() SK_OVERRIDE;
34 virtual bool onLockPixelsAreWritable() const SK_OVERRIDE { return false; }
35
36 virtual SkData* onRefEncodedData() SK_OVERRIDE {
37 return fGenerator->refEncodedData();
38 }
39
40private:
41 SkImageGenerator* const fGenerator;
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000042 SkDiscardableMemory::Factory* const fDMFactory;
halcanary@google.comad04eb42013-11-21 15:32:08 +000043 const size_t fRowBytes;
44 // These const members should not change over the life of the
45 // PixelRef, since the SkBitmap doesn't expect them to change.
46
47 SkDiscardableMemory* fDiscardableMemory;
48
49 /* Takes ownership of SkImageGenerator. */
reed@google.combf790232013-12-13 19:45:58 +000050 SkDiscardablePixelRef(const SkImageInfo&, SkImageGenerator*,
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000051 size_t rowBytes,
52 SkDiscardableMemory::Factory* factory);
reed@google.comd0419b12014-01-06 17:08:27 +000053
halcanary@google.comedd370f2013-12-10 21:11:12 +000054 friend bool SkInstallDiscardablePixelRef(SkImageGenerator*,
55 SkBitmap*,
56 SkDiscardableMemory::Factory*);
reed@google.comd0419b12014-01-06 17:08:27 +000057
halcanary@google.comedd370f2013-12-10 21:11:12 +000058 typedef SkPixelRef INHERITED;
halcanary@google.comad04eb42013-11-21 15:32:08 +000059};
reed@google.comd0419b12014-01-06 17:08:27 +000060
halcanary@google.comad04eb42013-11-21 15:32:08 +000061#endif // SkDiscardablePixelRef_DEFINED