blob: 0b292e932a3c0dd557a81572c59df59ffc6b7a98 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
vandebo@chromium.orgda912d62011-03-08 18:31:02 +000010#ifndef SkPDFShader_DEFINED
11#define SkPDFShader_DEFINED
12
13#include "SkPDFStream.h"
14#include "SkPDFTypes.h"
15#include "SkMatrix.h"
16#include "SkRefCnt.h"
17#include "SkShader.h"
18
19class SkObjRef;
20class SkPDFCatalog;
21
22/** \class SkPDFShader
23
24 In PDF parlance, this is a pattern, used in place of a color when the
25 pattern color space is selected.
26*/
27
vandebo@chromium.org421d6442011-07-20 17:39:01 +000028class SkPDFShader {
vandebo@chromium.orgda912d62011-03-08 18:31:02 +000029public:
vandebo@chromium.orgda912d62011-03-08 18:31:02 +000030 /** Get the PDF shader for the passed SkShader. If the SkShader is
31 * invalid in some way, returns NULL. The reference count of
32 * the object is incremented and it is the caller's responsibility to
33 * unreference it when done. This is needed to accommodate the weak
34 * reference pattern used when the returned object is new and has no
35 * other references.
36 * @param shader The SkShader to emulate.
37 * @param matrix The current transform. (PDF shaders are absolutely
38 * positioned, relative to where the page is drawn.)
39 * @param surfceBBox The bounding box of the drawing surface (with matrix
40 * already applied).
41 */
vandebo@chromium.org421d6442011-07-20 17:39:01 +000042 static SkPDFObject* GetPDFShader(const SkShader& shader,
vandebo@chromium.orgda912d62011-03-08 18:31:02 +000043 const SkMatrix& matrix,
44 const SkIRect& surfaceBBox);
45
vandebo@chromium.org421d6442011-07-20 17:39:01 +000046protected:
47 class State;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +000048
49 class ShaderCanonicalEntry {
50 public:
vandebo@chromium.org421d6442011-07-20 17:39:01 +000051 ShaderCanonicalEntry(SkPDFObject* pdfShader, const State* state);
52 bool operator==(const ShaderCanonicalEntry& b) const;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +000053
vandebo@chromium.org421d6442011-07-20 17:39:01 +000054 SkPDFObject* fPDFShader;
55 const State* fState;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +000056 };
57 // This should be made a hash table if performance is a problem.
vandebo@chromium.orgb88cfe52011-07-18 18:40:32 +000058 static SkTDArray<ShaderCanonicalEntry>& CanonicalShaders();
digit@google.com1771cbf2012-01-26 21:26:40 +000059 static SkBaseMutex& CanonicalShadersMutex();
commit-bot@chromium.org93a2e212013-07-23 23:16:03 +000060
61 // This is an internal method.
62 // CanonicalShadersMutex() should already be acquired.
63 // This also takes ownership of shaderState.
64 static SkPDFObject* GetPDFShaderByState(State* shaderState);
vandebo@chromium.org421d6442011-07-20 17:39:01 +000065 static void RemoveShader(SkPDFObject* shader);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +000066
vandebo@chromium.org421d6442011-07-20 17:39:01 +000067 SkPDFShader();
robertphillips@google.com05141c22012-04-27 12:54:01 +000068 virtual ~SkPDFShader() {};
vandebo@chromium.org386dfc02012-04-17 22:31:52 +000069
70 virtual bool isValid() = 0;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +000071};
72
73#endif