blob: 306f7141699c3eff476cd0654c6405e27ba00465 [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
reed680fb9e2014-08-26 09:08:04 -070011#include "SkBitmapCache.h"
halcanary@google.com36d08c52013-12-05 14:00:03 +000012#include "SkImageInfo.h"
13#include "SkImageGenerator.h"
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000014#include "SkPixelRef.h"
15
16class SkColorTable;
17
18/**
19 * PixelRef which defers decoding until SkBitmap::lockPixels() is
20 * called. Caches the decoded images in the global
21 * SkScaledImageCache. When the pixels are unlocked, this cache may
22 * or be destroyed before the next lock. If so, onLockPixels will
23 * attempt to re-decode.
24 *
halcanary@google.com36d08c52013-12-05 14:00:03 +000025 * Decoding is handled by the SkImageGenerator
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000026 */
27class SkCachingPixelRef : public SkPixelRef {
28public:
commit-bot@chromium.org227c2462014-01-24 18:33:07 +000029 SK_DECLARE_INST_COUNT(SkCachingPixelRef)
halcanary@google.com36d08c52013-12-05 14:00:03 +000030 /**
31 * Takes ownership of SkImageGenerator. If this method fails for
32 * whatever reason, it will return false and immediatetely delete
33 * the generator. If it succeeds, it will modify destination
34 * bitmap.
35 *
36 * If Install fails or when the SkCachingPixelRef that is
37 * installed into destination is destroyed, it will call
38 * SkDELETE() on the generator. Therefore, generator should be
39 * allocated with SkNEW() or SkNEW_ARGS().
40 */
41 static bool Install(SkImageGenerator* gen, SkBitmap* dst);
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000042
43protected:
halcanary@google.com36d08c52013-12-05 14:00:03 +000044 virtual ~SkCachingPixelRef();
reed@google.comd0419b12014-01-06 17:08:27 +000045 virtual bool onNewLockPixels(LockRec*) SK_OVERRIDE;
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000046 virtual void onUnlockPixels() SK_OVERRIDE;
47 virtual bool onLockPixelsAreWritable() const SK_OVERRIDE { return false; }
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000048
halcanary@google.com36d08c52013-12-05 14:00:03 +000049 virtual SkData* onRefEncodedData() SK_OVERRIDE {
50 return fImageGenerator->refEncodedData();
51 }
reed9a9eae22014-07-07 14:32:06 -070052
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000053private:
halcanary@google.com36d08c52013-12-05 14:00:03 +000054 SkImageGenerator* const fImageGenerator;
55 bool fErrorInDecoding;
halcanary@google.com36d08c52013-12-05 14:00:03 +000056 const size_t fRowBytes;
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000057
reed680fb9e2014-08-26 09:08:04 -070058 SkBitmap fLockedBitmap;
59
reed@google.combf790232013-12-13 19:45:58 +000060 SkCachingPixelRef(const SkImageInfo&, SkImageGenerator*, size_t rowBytes);
61
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000062 typedef SkPixelRef INHERITED;
63};
64
65#endif // SkCachingPixelRef_DEFINED