blob: e5e1c9f0e19e50a54056664c3c91cf548afe7748 [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.
commit-bot@chromium.org924205a2014-05-28 16:16:08 +000020 *
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.
halcanary@google.com3d50ea12014-01-02 13:15:13 +000024 */
halcanary@google.comad04eb42013-11-21 15:32:08 +000025class SkDiscardablePixelRef : public SkPixelRef {
26public:
commit-bot@chromium.org227c2462014-01-24 18:33:07 +000027 SK_DECLARE_INST_COUNT(SkDiscardablePixelRef)
halcanary@google.comad04eb42013-11-21 15:32:08 +000028 SK_DECLARE_UNFLATTENABLE_OBJECT()
29
30protected:
31 ~SkDiscardablePixelRef();
reed@google.comd0419b12014-01-06 17:08:27 +000032
33 virtual bool onNewLockPixels(LockRec*) SK_OVERRIDE;
halcanary@google.comad04eb42013-11-21 15:32:08 +000034 virtual void onUnlockPixels() SK_OVERRIDE;
35 virtual bool onLockPixelsAreWritable() const SK_OVERRIDE { return false; }
36
37 virtual SkData* onRefEncodedData() SK_OVERRIDE {
38 return fGenerator->refEncodedData();
39 }
40
41private:
42 SkImageGenerator* const fGenerator;
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000043 SkDiscardableMemory::Factory* const fDMFactory;
halcanary@google.comad04eb42013-11-21 15:32:08 +000044 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
50 /* Takes ownership of SkImageGenerator. */
reed@google.combf790232013-12-13 19:45:58 +000051 SkDiscardablePixelRef(const SkImageInfo&, SkImageGenerator*,
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000052 size_t rowBytes,
53 SkDiscardableMemory::Factory* factory);
reed@google.comd0419b12014-01-06 17:08:27 +000054
commit-bot@chromium.org2d970b52014-05-27 14:14:22 +000055 friend bool SkInstallDiscardablePixelRef(SkImageGenerator*, SkBitmap*,
halcanary@google.comedd370f2013-12-10 21:11:12 +000056 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