blob: b73a2ca443ba932ffdbd3fc953e2b6154d716ac4 [file] [log] [blame]
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +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 SkCachingPixelRef_DEFINED
9#define SkCachingPixelRef_DEFINED
10
halcanary@google.com36d08c52013-12-05 14:00:03 +000011#include "SkImageInfo.h"
12#include "SkImageGenerator.h"
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000013#include "SkPixelRef.h"
14
15class SkColorTable;
16
17/**
18 * PixelRef which defers decoding until SkBitmap::lockPixels() is
19 * called. Caches the decoded images in the global
20 * SkScaledImageCache. When the pixels are unlocked, this cache may
21 * or be destroyed before the next lock. If so, onLockPixels will
22 * attempt to re-decode.
23 *
halcanary@google.com36d08c52013-12-05 14:00:03 +000024 * Decoding is handled by the SkImageGenerator
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000025 */
26class SkCachingPixelRef : public SkPixelRef {
27public:
commit-bot@chromium.org227c2462014-01-24 18:33:07 +000028 SK_DECLARE_INST_COUNT(SkCachingPixelRef)
halcanary@google.com36d08c52013-12-05 14:00:03 +000029 /**
30 * Takes ownership of SkImageGenerator. If this method fails for
31 * whatever reason, it will return false and immediatetely delete
32 * the generator. If it succeeds, it will modify destination
33 * bitmap.
34 *
35 * If Install fails or when the SkCachingPixelRef that is
36 * installed into destination is destroyed, it will call
37 * SkDELETE() on the generator. Therefore, generator should be
38 * allocated with SkNEW() or SkNEW_ARGS().
39 */
40 static bool Install(SkImageGenerator* gen, SkBitmap* dst);
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000041
42protected:
halcanary@google.com36d08c52013-12-05 14:00:03 +000043 virtual ~SkCachingPixelRef();
reed@google.comd0419b12014-01-06 17:08:27 +000044 virtual bool onNewLockPixels(LockRec*) SK_OVERRIDE;
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000045 virtual void onUnlockPixels() SK_OVERRIDE;
46 virtual bool onLockPixelsAreWritable() const SK_OVERRIDE { return false; }
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000047
halcanary@google.com36d08c52013-12-05 14:00:03 +000048 virtual SkData* onRefEncodedData() SK_OVERRIDE {
49 return fImageGenerator->refEncodedData();
50 }
51 // No need to flatten this object. When flattening an SkBitmap,
52 // SkOrderedWriteBuffer will check the encoded data and write that
53 // instead.
54 // Future implementations of SkFlattenableWriteBuffer will need to
55 // special case for onRefEncodedData as well.
56 SK_DECLARE_UNFLATTENABLE_OBJECT()
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000057
58private:
halcanary@google.com36d08c52013-12-05 14:00:03 +000059 SkImageGenerator* const fImageGenerator;
60 bool fErrorInDecoding;
61 void* fScaledCacheId;
halcanary@google.com36d08c52013-12-05 14:00:03 +000062 const size_t fRowBytes;
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000063
reed@google.combf790232013-12-13 19:45:58 +000064 SkCachingPixelRef(const SkImageInfo&, SkImageGenerator*, size_t rowBytes);
65
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000066 typedef SkPixelRef INHERITED;
67};
68
69#endif // SkCachingPixelRef_DEFINED