blob: 2d8741479b8414355e7aa83fd79d555043e103fb [file] [log] [blame]
bsalomon@google.com1744f972013-02-26 21:46:32 +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#include "gl/GrGLExtensions.h"
9#include "gl/GrGLDefines.h"
10#include "gl/GrGLUtil.h"
11
bsalomon@google.comff436612013-02-27 19:07:32 +000012#include "SkTSearch.h"
13#include "SkTSort.h"
14
15namespace {
16inline int extension_compare(const SkString* a, const SkString* b) {
17 return strcmp(a->c_str(), b->c_str());
18}
19}
20
bsalomon@google.com1744f972013-02-26 21:46:32 +000021bool GrGLExtensions::init(GrGLBinding binding,
22 GrGLGetStringProc getString,
23 GrGLGetStringiProc getStringi,
24 GrGLGetIntegervProc getIntegerv) {
25 fStrings.reset();
26 if (NULL == getString) {
27 return false;
28 }
29 bool indexed = false;
30 if (kDesktop_GrGLBinding == binding) {
31 const GrGLubyte* verString = getString(GR_GL_VERSION);
32 if (NULL == verString) {
33 return false;
34 }
35 GrGLVersion version = GrGLGetVersionFromString((const char*) verString);
36 indexed = version >= GR_GL_VER(3, 0);
37 }
38 if (indexed) {
39 if (NULL == getStringi || NULL == getIntegerv) {
40 return false;
41 }
42 GrGLint extensionCnt = 0;
43 getIntegerv(GR_GL_NUM_EXTENSIONS, &extensionCnt);
44 fStrings.push_back_n(extensionCnt);
45 for (int i = 0; i < extensionCnt; ++i) {
46 const char* ext = (const char*) getStringi(GR_GL_EXTENSIONS, i);
47 fStrings[i] = ext;
48 }
49 } else {
50 const char* extensions = (const char*) getString(GR_GL_EXTENSIONS);
51 if (NULL == extensions) {
52 return false;
53 }
54 // First count the extensions so that we don't cause the array to malloc multiple times.
55 int extensionCnt = 1;
56 const char* e = (const char*) extensions;
57 while (NULL != (e = strchr(e+1, ' '))) {
58 e += 1;
59 ++extensionCnt;
60 }
61 fStrings.push_back_n(extensionCnt);
skia.committer@gmail.com12eea2b2013-02-27 07:10:10 +000062
bsalomon@google.com1744f972013-02-26 21:46:32 +000063 int i = 0;
64 while (true) {
65 size_t length = strcspn(extensions, " ");
66 GrAssert(i < extensionCnt);
67 fStrings[i].set(extensions, length);
68 ++i;
69 if ('\0' == extensions[length]) {
70 break;
71 }
72 extensions += length + 1;
73 }
74 GrAssert(i == extensionCnt);
75 }
bsalomon@google.comff436612013-02-27 19:07:32 +000076 SkTSearchCompareLTFunctor<SkString, extension_compare> cmp;
77 SkTQSort(&fStrings.front(), &fStrings.back(), cmp);
bsalomon@google.com1744f972013-02-26 21:46:32 +000078 return true;
79}
80
81bool GrGLExtensions::has(const char* ext) const {
bsalomon@google.comff436612013-02-27 19:07:32 +000082 SkString extensionStr(ext);
83 int idx = SkTSearch<SkString, extension_compare>(&fStrings.front(),
84 fStrings.count(),
85 extensionStr,
86 sizeof(SkString));
87 return idx >= 0;
bsalomon@google.com1744f972013-02-26 21:46:32 +000088}