| // 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 service |
| |
| import ( |
| "fmt" |
| |
| "android.googlesource.com/platform/tools/gpu/framework/id" |
| "android.googlesource.com/platform/tools/gpu/framework/log" |
| "android.googlesource.com/platform/tools/gpu/gapid/atom" |
| "android.googlesource.com/platform/tools/gpu/gapid/database" |
| "android.googlesource.com/platform/tools/gpu/gapid/image" |
| "android.googlesource.com/platform/tools/gpu/gapid/service/path" |
| ) |
| |
| // GetBlob calls s.Get with p and then safely casts the result to a []byte. |
| func GetBlob(ctx log.Context, p *path.Blob, s Service) ([]byte, error) { |
| if v, err := s.Get(ctx, p); err != nil { |
| return nil, err |
| } else if r, ok := v.([]byte); !ok { |
| return nil, fmt.Errorf("path %s gave %T, expected []byte", p, v) |
| } else { |
| return r, nil |
| } |
| } |
| |
| // ResolveAtomList resolves an AtomsID and then safely casts the result to a *atom.List. |
| func ResolveAtomList(ctx log.Context, atomsID AtomsID, d database.Database) (*atom.List, error) { |
| if v, err := database.Resolve(ctx, id.ID(atomsID), d); err != nil { |
| return nil, err |
| } else if r, ok := v.(*atom.List); !ok { |
| return nil, fmt.Errorf("ID %s gave %T, expected *atom.List", atomsID, v) |
| } else { |
| return r, nil |
| } |
| } |
| |
| // GetAtomList resolves an AtomsID and then safely casts the result to a *atom.List. |
| func GetAtomList(ctx log.Context, p *path.Atoms, s Service) (*atom.List, error) { |
| if v, err := s.Get(ctx, p); err != nil { |
| return nil, err |
| } else if r, ok := v.(*atom.List); !ok { |
| return nil, fmt.Errorf("path %s gave %T, expected *atom.List", p, v) |
| } else { |
| return r, nil |
| } |
| } |
| |
| // ResolveCapture resolves a id.ID and then safely casts the result to a *Capture. |
| func ResolveCapture(ctx log.Context, id id.ID, d database.Database) (*Capture, error) { |
| if v, err := database.Resolve(ctx, id, d); err != nil { |
| return nil, err |
| } else if r, ok := v.(*Capture); !ok { |
| return nil, fmt.Errorf("ID %s gave %T, expected Capture", id, v) |
| } else { |
| return r, nil |
| } |
| } |
| |
| // GetCapture calls s.Get with p and then safely casts the result to a *Capture. |
| func GetCapture(ctx log.Context, p *path.Capture, s Service) (*Capture, error) { |
| if v, err := s.Get(ctx, p); err != nil { |
| return nil, err |
| } else if r, ok := v.(*Capture); !ok { |
| return nil, fmt.Errorf("path %s gave %T, expected Capture", p, v) |
| } else { |
| return r, nil |
| } |
| } |
| |
| // ResolveImageInfo resolves a id.ID and then safely casts the result to a *image.Info. |
| func ResolveImageInfo(ctx log.Context, id id.ID, d database.Database) (*image.Info, error) { |
| if v, err := database.Resolve(ctx, id, d); err != nil { |
| return nil, err |
| } else if r, ok := v.(*image.Info); !ok { |
| return nil, fmt.Errorf("ID %s gave %T, expected image.Info", id, v) |
| } else { |
| return r, nil |
| } |
| } |
| |
| // GetImageInfo calls s.Get with p and then safely casts the result to a *image.Info. |
| func GetImageInfo(ctx log.Context, p path.Path, s Service) (*image.Info, error) { |
| if v, err := s.Get(ctx, p); err != nil { |
| return nil, err |
| } else if r, ok := v.(*image.Info); !ok { |
| return nil, fmt.Errorf("path %s gave %T, expected image.Info", p, v) |
| } else { |
| return r, nil |
| } |
| } |
| |
| // ResolveTimingInfo resolves a id.ID and then safely casts the result to a *TimingInfo. |
| func ResolveTimingInfo(ctx log.Context, id id.ID, d database.Database) (*TimingInfo, error) { |
| if v, err := database.Resolve(ctx, id, d); err != nil { |
| return nil, err |
| } else if r, ok := v.(*TimingInfo); !ok { |
| return nil, fmt.Errorf("ID %s gave %T, expected TimingInfo", id, v) |
| } else { |
| return r, nil |
| } |
| } |
| |
| // GetTimingInfo calls s.Get with p and then safely casts the result to a *TimingInfo. |
| func GetTimingInfo(ctx log.Context, p *path.TimingInfo, s Service) (*TimingInfo, error) { |
| if v, err := s.Get(ctx, p); err != nil { |
| return nil, err |
| } else if r, ok := v.(*TimingInfo); !ok { |
| return nil, fmt.Errorf("path %s gave %T, expected TimingInfo", p, v) |
| } else { |
| return r, nil |
| } |
| } |
| |
| type captureKeyTy string |
| |
| const captureKey = captureKeyTy("captureID") |
| |
| // PutContextCapture attaches a capture ID to a Context. |
| func PutContextCapture(ctx log.Context, c *path.Capture) log.Context { |
| return ctx.HiddenValue(captureKey, c.ID) |
| } |
| |
| // GetContextCapture retrieves the capture ID from a context |
| // previously annotated by PutContextCapture. |
| func GetContextCapture(ctx log.Context) *path.Capture { |
| val := ctx.Value(captureKey) |
| if val == nil { |
| panic("captureID not present") |
| } |
| return &path.Capture{ID: val.(id.ID)} |
| } |
| |
| // WithCaptureAndDatabase modifies a context by attaching a Capture and Database. |
| func WithCaptureAndDatabase(ctx log.Context, c *path.Capture, d database.Database) log.Context { |
| return PutContextCapture(database.With(ctx, d), c) |
| } |
| |
| // GetCaptureAndDatabase returns the Capture and Database attached to a context. |
| func GetCaptureAndDatabase(ctx log.Context) (*path.Capture, database.Database) { |
| return GetContextCapture(ctx), database.Get(ctx) |
| } |