| // Copyright (C) 2016 The Android Open Source Project |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| package memory |
| |
| import ( |
| "bytes" |
| "fmt" |
| "io" |
| |
| "android.googlesource.com/platform/tools/gpu/framework/binary" |
| "android.googlesource.com/platform/tools/gpu/framework/binary/endian" |
| "android.googlesource.com/platform/tools/gpu/framework/device" |
| "android.googlesource.com/platform/tools/gpu/framework/id" |
| "android.googlesource.com/platform/tools/gpu/framework/log" |
| "android.googlesource.com/platform/tools/gpu/gapid/database" |
| ) |
| |
| // Resource returns a Slice that wraps a resource stored in the database. |
| // resId is the identifier of the data and size is the size in bytes of the |
| // data. |
| func Resource(resId id.ID, size uint64) Slice { |
| return resource{resId, size} |
| } |
| |
| type resource struct { |
| resId id.ID |
| size uint64 |
| } |
| |
| func (r resource) Get(ctx log.Context, d database.Database, offset uint64, out []byte) error { |
| data, err := r.getData(ctx, d) |
| if err != nil { |
| return err |
| } |
| copy(out, data[offset:]) |
| return nil |
| } |
| |
| func (r resource) getData(ctx log.Context, d database.Database) ([]byte, error) { |
| res, err := database.Resolve(ctx, r.resId, d) |
| if err != nil { |
| return nil, err |
| } |
| data := res.([]byte) |
| if r.size != uint64(len(data)) { |
| return nil, fmt.Errorf("Loaded resource is unexpected size. Expected 0x%x, got 0x%x for resource %v", |
| r.size, len(data), r.resId) |
| } |
| return data, nil |
| } |
| |
| func (r resource) ResourceID(ctx log.Context, d database.Database) (id.ID, error) { |
| return r.resId, nil |
| } |
| |
| func (r resource) Size() uint64 { |
| return r.size |
| } |
| |
| func (r resource) Slice(rng Range) Slice { |
| return newResourceSlice(r, rng) |
| } |
| |
| func (r resource) ValidRanges() RangeList { |
| return RangeList{Range{Size: r.Size()}} |
| } |
| |
| func (r resource) String() string { |
| return fmt.Sprintf("Resource[%v]", r.resId) |
| } |
| |
| func (r resource) NewReader(ctx log.Context, d database.Database) io.Reader { |
| data, err := r.getData(ctx, d) |
| if err != nil { |
| return failedReader{err} |
| } |
| return bytes.NewReader(data) |
| } |
| |
| func newResourceSlice(src resource, rng Range) Slice { |
| if uint64(rng.Last()) > src.Size() { |
| panic(fmt.Errorf("Slice range %v out of bounds %v", rng, Range{Base: 0, Size: src.Size()})) |
| } |
| return resourceSlice{src, rng} |
| } |
| |
| type resourceSlice struct { |
| src resource |
| rng Range |
| } |
| |
| func (s resourceSlice) Get(ctx log.Context, d database.Database, offset uint64, out []byte) error { |
| trim := min(s.rng.Size-offset, uint64(len(out))) |
| return s.src.Get(ctx, d, s.rng.First()+offset, out[:trim]) |
| } |
| |
| func (r resourceSlice) NewReader(ctx log.Context, d database.Database) io.Reader { |
| data, err := r.src.getData(ctx, d) |
| if err != nil { |
| return failedReader{err} |
| } |
| return bytes.NewReader(data[r.rng.First() : r.rng.Last()+1]) |
| } |
| |
| func (s resourceSlice) ResourceID(ctx log.Context, d database.Database) (id.ID, error) { |
| subslice := subslice{ |
| source: s.src.resId, |
| from: s.rng.Base, |
| size: s.rng.Size, |
| } |
| return database.Store(ctx, &subslice, d) |
| } |
| |
| func (s resourceSlice) Size() uint64 { |
| return s.rng.Size |
| } |
| |
| func (s resourceSlice) Slice(rng Range) Slice { |
| return newResourceSlice(s.src, Range{Base: s.rng.Base + rng.Base, Size: rng.Size}) |
| } |
| |
| func (s resourceSlice) ValidRanges() RangeList { |
| return RangeList{Range{Size: s.rng.Size}} |
| } |
| |
| func (s resourceSlice) String() string { |
| return fmt.Sprintf("%v[%v]", s.src, s.rng) |
| } |
| |
| // subslice is a lazy builder that retrieves a subslice of another byte slice. |
| type subslice struct { |
| binary.Generate `java:"disable"` |
| source id.ID |
| from uint64 |
| size uint64 |
| } |
| |
| func (s *subslice) WriteForHash(w io.Writer) error { |
| u := endian.Writer(w, device.LittleEndian) |
| u.Uint64(s.from) |
| u.Uint64(s.size) |
| u.Data(s.source[:20]) |
| return u.Error() |
| } |
| |
| func (s *subslice) BuildLazy(ctx log.Context, c interface{}, d database.Database) (interface{}, error) { |
| res, err := database.Resolve(ctx, s.source, d) |
| if err != nil { |
| return nil, err |
| } |
| data := res.([]byte) |
| return data[s.from : s.from+s.size], nil |
| } |
| |
| type failedReader struct { |
| err error |
| } |
| |
| func (fr failedReader) Read([]byte) (int, error) { |
| return 0, fr.err |
| } |