Home | History | Annotate | Download | only in gpu
      1 /*
      2  * Copyright 2015 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 "GrResourceProvider.h"
      9 
     10 #include "GrGpu.h"
     11 #include "GrResourceCache.h"
     12 #include "GrResourceKey.h"
     13 #include "GrVertexBuffer.h"
     14 
     15 GR_DECLARE_STATIC_UNIQUE_KEY(gQuadIndexBufferKey);
     16 
     17 GrResourceProvider::GrResourceProvider(GrGpu* gpu, GrResourceCache* cache) : INHERITED(gpu, cache) {
     18     GR_DEFINE_STATIC_UNIQUE_KEY(gQuadIndexBufferKey);
     19     fQuadIndexBufferKey = gQuadIndexBufferKey;
     20 }
     21 
     22 const GrIndexBuffer* GrResourceProvider::createInstancedIndexBuffer(const uint16_t* pattern,
     23                                                                     int patternSize,
     24                                                                     int reps,
     25                                                                     int vertCount,
     26                                                                     const GrUniqueKey& key) {
     27     size_t bufferSize = patternSize * reps * sizeof(uint16_t);
     28 
     29     GrIndexBuffer* buffer = this->gpu()->createIndexBuffer(bufferSize, /* dynamic = */ false);
     30     if (!buffer) {
     31         return NULL;
     32     }
     33     uint16_t* data = (uint16_t*) buffer->map();
     34     bool useTempData = (NULL == data);
     35     if (useTempData) {
     36         data = SkNEW_ARRAY(uint16_t, reps * patternSize);
     37     }
     38     for (int i = 0; i < reps; ++i) {
     39         int baseIdx = i * patternSize;
     40         uint16_t baseVert = (uint16_t)(i * vertCount);
     41         for (int j = 0; j < patternSize; ++j) {
     42             data[baseIdx+j] = baseVert + pattern[j];
     43         }
     44     }
     45     if (useTempData) {
     46         if (!buffer->updateData(data, bufferSize)) {
     47             buffer->unref();
     48             return NULL;
     49         }
     50         SkDELETE_ARRAY(data);
     51     } else {
     52         buffer->unmap();
     53     }
     54     this->assignUniqueKeyToResource(key, buffer);
     55     return buffer;
     56 }
     57 
     58 const GrIndexBuffer* GrResourceProvider::createQuadIndexBuffer() {
     59     static const int kMaxQuads = 1 << 12; // max possible: (1 << 14) - 1;
     60     GR_STATIC_ASSERT(4 * kMaxQuads <= 65535);
     61     static const uint16_t kPattern[] = { 0, 1, 2, 0, 2, 3 };
     62 
     63     return this->createInstancedIndexBuffer(kPattern, 6, kMaxQuads, 4, fQuadIndexBufferKey);
     64 }
     65 
     66